Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
| 2 | |
| 3 | #include <assert.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include <libmctp.h> |
| 9 | #include <libmctp-alloc.h> |
| 10 | |
| 11 | #include "test-utils.h" |
| 12 | |
| 13 | struct mctp_binding_bridge { |
| 14 | struct mctp_binding binding; |
| 15 | int rx_count; |
| 16 | int tx_count; |
| 17 | uint8_t last_pkt_data; |
| 18 | }; |
| 19 | |
| 20 | struct test_ctx { |
| 21 | struct mctp *mctp; |
| 22 | struct mctp_binding_bridge *bindings[2]; |
| 23 | }; |
| 24 | |
| 25 | static int mctp_binding_bridge_tx(struct mctp_binding *b, |
| 26 | struct mctp_pktbuf *pkt) |
| 27 | { |
| 28 | struct mctp_binding_bridge *binding = container_of(b, |
| 29 | struct mctp_binding_bridge, binding); |
| 30 | |
| 31 | binding->tx_count++; |
| 32 | assert(mctp_pktbuf_size(pkt) == sizeof(struct mctp_hdr) + 1); |
| 33 | binding->last_pkt_data = *(uint8_t *)mctp_pktbuf_data(pkt); |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static int mctp_binding_bridge_rx(struct mctp_binding_bridge *binding, |
| 39 | uint8_t key) |
| 40 | { |
| 41 | struct mctp_pktbuf *pkt; |
| 42 | struct mctp_hdr *hdr; |
| 43 | uint8_t *buf; |
| 44 | |
| 45 | pkt = mctp_pktbuf_alloc(&binding->binding, |
| 46 | sizeof(struct mctp_hdr) + 1); |
| 47 | |
| 48 | hdr = mctp_pktbuf_hdr(pkt); |
| 49 | hdr->flags_seq_tag = MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM; |
| 50 | |
| 51 | /* arbitrary src/dest, as we're bridging */ |
| 52 | hdr->src = 1; |
| 53 | hdr->dest = 2; |
| 54 | |
| 55 | buf = mctp_pktbuf_data(pkt); |
| 56 | *buf = key; |
| 57 | |
| 58 | binding->rx_count++; |
| 59 | mctp_bus_rx(&binding->binding, pkt); |
| 60 | } |
| 61 | |
| 62 | static struct mctp_binding_bridge *mctp_binding_bridge_init(void) |
| 63 | { |
| 64 | struct mctp_binding_bridge *binding; |
| 65 | |
| 66 | binding = __mctp_alloc(sizeof(*binding)); |
| 67 | memset(binding, 0, sizeof(*binding)); |
| 68 | binding->binding.name = "test"; |
| 69 | binding->binding.version = 1; |
| 70 | binding->binding.tx = mctp_binding_bridge_tx; |
| 71 | binding->binding.pkt_size = MCTP_BMTU; |
| 72 | binding->binding.pkt_pad = 0; |
| 73 | return binding; |
| 74 | } |
| 75 | |
| 76 | int main(void) |
| 77 | { |
| 78 | struct test_ctx _ctx, *ctx = &_ctx; |
| 79 | |
| 80 | ctx->mctp = mctp_init(); |
| 81 | ctx->bindings[0] = mctp_binding_bridge_init(); |
| 82 | ctx->bindings[1] = mctp_binding_bridge_init(); |
| 83 | |
| 84 | mctp_bridge_busses(ctx->mctp, |
| 85 | &ctx->bindings[0]->binding, |
| 86 | &ctx->bindings[1]->binding); |
| 87 | |
| 88 | mctp_binding_set_tx_enabled(&ctx->bindings[0]->binding, true); |
| 89 | mctp_binding_set_tx_enabled(&ctx->bindings[1]->binding, true); |
| 90 | |
| 91 | mctp_binding_bridge_rx(ctx->bindings[0], 0xaa); |
| 92 | assert(ctx->bindings[0]->tx_count == 0); |
| 93 | assert(ctx->bindings[1]->tx_count == 1); |
| 94 | assert(ctx->bindings[1]->last_pkt_data == 0xaa); |
| 95 | |
| 96 | mctp_binding_bridge_rx(ctx->bindings[1], 0x55); |
| 97 | assert(ctx->bindings[1]->tx_count == 1); |
| 98 | assert(ctx->bindings[0]->tx_count == 1); |
| 99 | assert(ctx->bindings[0]->last_pkt_data == 0x55); |
| 100 | |
| 101 | return EXIT_SUCCESS; |
| 102 | } |