Jeremy Kerr | 3d36ee2 | 2019-05-30 11:15:37 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 2 | |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include <libmctp.h> |
| 7 | #include <libmctp-alloc.h> |
| 8 | |
| 9 | #include "test-utils.h" |
| 10 | |
| 11 | struct mctp_binding_test { |
Andrew Jeffery | 69eda31 | 2022-09-30 14:22:32 +0930 | [diff] [blame] | 12 | struct mctp_binding binding; |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 13 | }; |
| 14 | |
| 15 | static int mctp_binding_test_tx(struct mctp_binding *b __attribute__((unused)), |
Andrew Jeffery | 69eda31 | 2022-09-30 14:22:32 +0930 | [diff] [blame] | 16 | struct mctp_pktbuf *pkt __attribute__((unused))) |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 17 | { |
| 18 | /* we are not expecting TX packets */ |
| 19 | assert(0); |
Andrew Jeffery | a501a58 | 2020-03-10 22:25:16 +1030 | [diff] [blame] | 20 | return 0; |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | struct mctp_binding_test *mctp_binding_test_init(void) |
| 24 | { |
| 25 | struct mctp_binding_test *test; |
| 26 | |
| 27 | test = __mctp_alloc(sizeof(*test)); |
Andrew Jeffery | 225c46d | 2020-03-10 23:20:01 +1030 | [diff] [blame] | 28 | memset(test, '\0', sizeof(*test)); |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 29 | test->binding.name = "test"; |
| 30 | test->binding.version = 1; |
| 31 | test->binding.tx = mctp_binding_test_tx; |
Andrew Jeffery | 73c268e | 2020-01-30 10:16:09 +1030 | [diff] [blame] | 32 | test->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU); |
Andrew Jeffery | 39da3d0 | 2021-03-12 16:51:26 +1030 | [diff] [blame] | 33 | test->binding.pkt_header = 0; |
| 34 | test->binding.pkt_trailer = 0; |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 35 | return test; |
| 36 | } |
| 37 | |
Andrew Jeffery | 5c5673b | 2020-03-10 23:23:58 +1030 | [diff] [blame] | 38 | void mctp_binding_test_destroy(struct mctp_binding_test *test) |
| 39 | { |
| 40 | __mctp_free(test); |
| 41 | } |
| 42 | |
Andrew Jeffery | 69eda31 | 2022-09-30 14:22:32 +0930 | [diff] [blame] | 43 | void mctp_binding_test_rx_raw(struct mctp_binding_test *test, void *buf, |
| 44 | size_t len) |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 45 | { |
| 46 | struct mctp_pktbuf *pkt; |
| 47 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 48 | pkt = mctp_pktbuf_alloc(&test->binding, len); |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 49 | assert(pkt); |
| 50 | memcpy(mctp_pktbuf_hdr(pkt), buf, len); |
| 51 | mctp_bus_rx(&test->binding, pkt); |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void mctp_binding_test_register_bus(struct mctp_binding_test *binding, |
Andrew Jeffery | 69eda31 | 2022-09-30 14:22:32 +0930 | [diff] [blame] | 55 | struct mctp *mctp, mctp_eid_t eid) |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 56 | { |
| 57 | mctp_register_bus(mctp, &binding->binding, eid); |
| 58 | } |
| 59 | |
| 60 | void mctp_test_stack_init(struct mctp **mctp, |
Andrew Jeffery | 69eda31 | 2022-09-30 14:22:32 +0930 | [diff] [blame] | 61 | struct mctp_binding_test **binding, mctp_eid_t eid) |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 62 | { |
| 63 | *mctp = mctp_init(); |
| 64 | assert(*mctp); |
| 65 | |
| 66 | *binding = mctp_binding_test_init(); |
| 67 | assert(*binding); |
| 68 | |
| 69 | mctp_binding_test_register_bus(*binding, *mctp, eid); |
| 70 | } |