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