xref: /openbmc/libmctp/tests/test_cmds.c (revision f39c3857)
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,
35*f39c3857SSumanth Bhat 					       bool tag_owner __unused,
36*f39c3857SSumanth 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);
53ba6727e6SWiktor Gołgowski }
54ba6727e6SWiktor Gołgowski 
setup_test_binding(struct mctp_binding * test_binding,struct mctp * test_endpoint,void * callback_ctx)55ba6727e6SWiktor Gołgowski static void setup_test_binding(struct mctp_binding *test_binding,
56ba6727e6SWiktor Gołgowski 			       struct mctp *test_endpoint, void *callback_ctx)
57ba6727e6SWiktor Gołgowski {
58ba6727e6SWiktor Gołgowski 	assert(test_binding != NULL);
59ba6727e6SWiktor Gołgowski 	assert(test_endpoint != NULL);
60ba6727e6SWiktor Gołgowski 	assert(callback_ctx != NULL);
61ba6727e6SWiktor Gołgowski 
62ba6727e6SWiktor Gołgowski 	memset(test_binding, 0, sizeof(*test_binding));
63ba6727e6SWiktor Gołgowski 	test_binding->name = "test";
64ba6727e6SWiktor Gołgowski 	test_binding->version = 1;
65ba6727e6SWiktor Gołgowski 	test_binding->tx = NULL;
66ba6727e6SWiktor Gołgowski 	test_binding->pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
6739da3d03SAndrew Jeffery 	test_binding->pkt_header = 0;
6839da3d03SAndrew Jeffery 	test_binding->pkt_trailer = 0;
69ba6727e6SWiktor Gołgowski 	test_binding->control_rx = control_message_transport_callback;
70ba6727e6SWiktor Gołgowski 	test_binding->control_rx_data = callback_ctx;
71ba6727e6SWiktor Gołgowski 
72ba6727e6SWiktor Gołgowski 	mctp_register_bus(test_endpoint, test_binding, eid_1);
73ba6727e6SWiktor Gołgowski 	mctp_binding_set_tx_enabled(test_binding, true);
74ba6727e6SWiktor Gołgowski }
75ba6727e6SWiktor Gołgowski 
send_transport_control_message(void)76ba6727e6SWiktor Gołgowski static void send_transport_control_message(void)
77ba6727e6SWiktor Gołgowski {
78ba6727e6SWiktor Gołgowski 	struct mctp *endpoint = mctp_init();
79ba6727e6SWiktor Gołgowski 	struct mctp_binding binding;
80ba6727e6SWiktor Gołgowski 	struct callback_data ctx;
81ba6727e6SWiktor Gołgowski 	static const struct msg_payload send_control_message_payload = {
82ba6727e6SWiktor Gołgowski 		.hdr = {
83ba6727e6SWiktor Gołgowski 			.dest = eid_1,
84ba6727e6SWiktor Gołgowski 			.src = eid_2,
85ba6727e6SWiktor Gołgowski 			.flags_seq_tag = MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM,
86ba6727e6SWiktor Gołgowski 		},
87ba6727e6SWiktor Gołgowski 		.ctrl_hdr = {
88ba6727e6SWiktor Gołgowski 			.ic_msg_type = MCTP_CTRL_HDR_MSG_TYPE,
89ba6727e6SWiktor Gołgowski 			.rq_dgram_inst = MCTP_CTRL_HDR_FLAG_REQUEST,
90ba6727e6SWiktor Gołgowski 			.command_code = 0xF2,
91ba6727e6SWiktor Gołgowski 		},
92ba6727e6SWiktor Gołgowski 	};
93ba6727e6SWiktor Gołgowski 
94ba6727e6SWiktor Gołgowski 	memset(&ctx, 0, sizeof(ctx));
95ba6727e6SWiktor Gołgowski 	setup_test_binding(&binding, endpoint, &ctx);
96ba6727e6SWiktor Gołgowski 	ctx.command_code = send_control_message_payload.ctrl_hdr.command_code;
97ba6727e6SWiktor Gołgowski 	printf("Sending transport control message: 0x%X\n",
98ba6727e6SWiktor Gołgowski 	       send_control_message_payload.ctrl_hdr.command_code);
99ba6727e6SWiktor Gołgowski 	rcv_ctrl_msg(&binding, (void *)&send_control_message_payload,
100ba6727e6SWiktor Gołgowski 		     sizeof(send_control_message_payload));
101ba6727e6SWiktor Gołgowski 	assert(ctx.invoked == 1);
102ba6727e6SWiktor Gołgowski 
103ba6727e6SWiktor Gołgowski 	mctp_destroy(endpoint);
104ba6727e6SWiktor Gołgowski }
105ba6727e6SWiktor Gołgowski 
main(void)106b93b6112SAndrew Jeffery int main(void)
107ba6727e6SWiktor Gołgowski {
108ba6727e6SWiktor Gołgowski 	send_transport_control_message();
109ba6727e6SWiktor Gołgowski 
110ba6727e6SWiktor Gołgowski 	return EXIT_SUCCESS;
111ba6727e6SWiktor Gołgowski }
112