xref: /openbmc/libmctp/tests/test_cmds.c (revision a3830d259a53269f7b9c8b46129e863ebed1b188)
1ba6727e6SWiktor Gołgowski /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2ba6727e6SWiktor Gołgowski 
3ba6727e6SWiktor Gołgowski #include "test-utils.h"
4ba6727e6SWiktor Gołgowski 
55ab78259SAndrew Jeffery #include "compiler.h"
65ab78259SAndrew Jeffery #include "libmctp.h"
75ab78259SAndrew Jeffery #include "libmctp-alloc.h"
85ab78259SAndrew Jeffery #include "libmctp-cmds.h"
95ab78259SAndrew Jeffery 
10ba6727e6SWiktor Gołgowski #include <stdio.h>
11ba6727e6SWiktor Gołgowski #include <string.h>
12ba6727e6SWiktor Gołgowski #include <assert.h>
13ba6727e6SWiktor Gołgowski 
14ba6727e6SWiktor Gołgowski #ifdef NDEBUG
15ba6727e6SWiktor Gołgowski #undef NDEBUG
16ba6727e6SWiktor Gołgowski #endif
17ba6727e6SWiktor Gołgowski 
18ba6727e6SWiktor Gołgowski static const mctp_eid_t eid_1 = 9;
19ba6727e6SWiktor Gołgowski static const mctp_eid_t eid_2 = 10;
20ba6727e6SWiktor Gołgowski 
21ba6727e6SWiktor Gołgowski struct msg_payload {
22ba6727e6SWiktor Gołgowski 	struct mctp_hdr hdr;
23ba6727e6SWiktor Gołgowski 	struct mctp_ctrl_msg_hdr ctrl_hdr;
24ba6727e6SWiktor Gołgowski };
25ba6727e6SWiktor Gołgowski 
26ba6727e6SWiktor Gołgowski struct callback_data {
27ba6727e6SWiktor Gołgowski 	uint8_t invoked;
28ba6727e6SWiktor Gołgowski 	union {
29ba6727e6SWiktor Gołgowski 		uint8_t command_code;
30ba6727e6SWiktor Gołgowski 		uint8_t completion_code;
31ba6727e6SWiktor Gołgowski 	};
32ba6727e6SWiktor Gołgowski };
33ba6727e6SWiktor Gołgowski 
control_message_transport_callback(mctp_eid_t src __unused,bool tag_owner __unused,uint8_t msg_tag __unused,void * data,void * buf,size_t len __unused)34b93b6112SAndrew Jeffery static void control_message_transport_callback(mctp_eid_t src __unused,
35f39c3857SSumanth Bhat 					       bool tag_owner __unused,
36f39c3857SSumanth Bhat 					       uint8_t msg_tag __unused,
37b93b6112SAndrew Jeffery 					       void *data, void *buf,
38b93b6112SAndrew Jeffery 					       size_t len __unused)
39ba6727e6SWiktor Gołgowski {
40ba6727e6SWiktor Gołgowski 	struct callback_data *ctx = data;
41ba6727e6SWiktor Gołgowski 	struct mctp_ctrl_msg_hdr *msg_hdr = buf;
42ba6727e6SWiktor Gołgowski 	printf("Transport control message received - command code: 0x%X\n",
43ba6727e6SWiktor Gołgowski 	       msg_hdr->command_code);
44ba6727e6SWiktor Gołgowski 	ctx->invoked++;
45ba6727e6SWiktor Gołgowski 	assert(msg_hdr->command_code == ctx->command_code);
46ba6727e6SWiktor Gołgowski }
47ba6727e6SWiktor Gołgowski 
rcv_ctrl_msg(struct mctp_binding * b,const void * buf,size_t len)48ba6727e6SWiktor Gołgowski static void rcv_ctrl_msg(struct mctp_binding *b, const void *buf, size_t len)
49ba6727e6SWiktor Gołgowski {
50ba6727e6SWiktor Gołgowski 	struct mctp_pktbuf *pkt = mctp_pktbuf_alloc(b, len);
51ba6727e6SWiktor Gołgowski 	memcpy(mctp_pktbuf_hdr(pkt), buf, len);
52ba6727e6SWiktor Gołgowski 	mctp_bus_rx(b, pkt);
534a09e1dcSMatt Johnston 	mctp_pktbuf_free(pkt);
54ba6727e6SWiktor Gołgowski }
55ba6727e6SWiktor Gołgowski 
setup_test_binding(struct mctp_binding * test_binding,struct mctp * test_endpoint,void * callback_ctx)56ba6727e6SWiktor Gołgowski static void setup_test_binding(struct mctp_binding *test_binding,
57ba6727e6SWiktor Gołgowski 			       struct mctp *test_endpoint, void *callback_ctx)
58ba6727e6SWiktor Gołgowski {
59ba6727e6SWiktor Gołgowski 	assert(test_binding != NULL);
60ba6727e6SWiktor Gołgowski 	assert(test_endpoint != NULL);
61ba6727e6SWiktor Gołgowski 	assert(callback_ctx != NULL);
62ba6727e6SWiktor Gołgowski 
63*a3830d25SMatt Johnston 	uint8_t tx_storage[MCTP_PKTBUF_SIZE(MCTP_BTU)] PKTBUF_STORAGE_ALIGN;
64ba6727e6SWiktor Gołgowski 	memset(test_binding, 0, sizeof(*test_binding));
65ba6727e6SWiktor Gołgowski 	test_binding->name = "test";
66ba6727e6SWiktor Gołgowski 	test_binding->version = 1;
67ba6727e6SWiktor Gołgowski 	test_binding->tx = NULL;
68ba6727e6SWiktor Gołgowski 	test_binding->pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
6939da3d03SAndrew Jeffery 	test_binding->pkt_header = 0;
7039da3d03SAndrew Jeffery 	test_binding->pkt_trailer = 0;
71ba6727e6SWiktor Gołgowski 	test_binding->control_rx = control_message_transport_callback;
72ba6727e6SWiktor Gołgowski 	test_binding->control_rx_data = callback_ctx;
734a09e1dcSMatt Johnston 	test_binding->tx_storage = tx_storage;
74ba6727e6SWiktor Gołgowski 
75ba6727e6SWiktor Gołgowski 	mctp_register_bus(test_endpoint, test_binding, eid_1);
76ba6727e6SWiktor Gołgowski 	mctp_binding_set_tx_enabled(test_binding, true);
77ba6727e6SWiktor Gołgowski }
78ba6727e6SWiktor Gołgowski 
send_transport_control_message(void)79ba6727e6SWiktor Gołgowski static void send_transport_control_message(void)
80ba6727e6SWiktor Gołgowski {
81ba6727e6SWiktor Gołgowski 	struct mctp *endpoint = mctp_init();
82ba6727e6SWiktor Gołgowski 	struct mctp_binding binding;
83ba6727e6SWiktor Gołgowski 	struct callback_data ctx;
84ba6727e6SWiktor Gołgowski 	static const struct msg_payload send_control_message_payload = {
85ba6727e6SWiktor Gołgowski 		.hdr = {
86ba6727e6SWiktor Gołgowski 			.dest = eid_1,
87ba6727e6SWiktor Gołgowski 			.src = eid_2,
88ba6727e6SWiktor Gołgowski 			.flags_seq_tag = MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM,
89ba6727e6SWiktor Gołgowski 		},
90ba6727e6SWiktor Gołgowski 		.ctrl_hdr = {
91ba6727e6SWiktor Gołgowski 			.ic_msg_type = MCTP_CTRL_HDR_MSG_TYPE,
92ba6727e6SWiktor Gołgowski 			.rq_dgram_inst = MCTP_CTRL_HDR_FLAG_REQUEST,
93ba6727e6SWiktor Gołgowski 			.command_code = 0xF2,
94ba6727e6SWiktor Gołgowski 		},
95ba6727e6SWiktor Gołgowski 	};
96ba6727e6SWiktor Gołgowski 
97ba6727e6SWiktor Gołgowski 	memset(&ctx, 0, sizeof(ctx));
98ba6727e6SWiktor Gołgowski 	setup_test_binding(&binding, endpoint, &ctx);
99ba6727e6SWiktor Gołgowski 	ctx.command_code = send_control_message_payload.ctrl_hdr.command_code;
100ba6727e6SWiktor Gołgowski 	printf("Sending transport control message: 0x%X\n",
101ba6727e6SWiktor Gołgowski 	       send_control_message_payload.ctrl_hdr.command_code);
102ba6727e6SWiktor Gołgowski 	rcv_ctrl_msg(&binding, (void *)&send_control_message_payload,
103ba6727e6SWiktor Gołgowski 		     sizeof(send_control_message_payload));
104ba6727e6SWiktor Gołgowski 	assert(ctx.invoked == 1);
105ba6727e6SWiktor Gołgowski 
106ba6727e6SWiktor Gołgowski 	mctp_destroy(endpoint);
107ba6727e6SWiktor Gołgowski }
108ba6727e6SWiktor Gołgowski 
main(void)109b93b6112SAndrew Jeffery int main(void)
110ba6727e6SWiktor Gołgowski {
111ba6727e6SWiktor Gołgowski 	send_transport_control_message();
112ba6727e6SWiktor Gołgowski 
113ba6727e6SWiktor Gołgowski 	return EXIT_SUCCESS;
114ba6727e6SWiktor Gołgowski }
115