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