blob: 4728b28988d654ec67e80eb43d061eb47090ce51 [file] [log] [blame]
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +08001/* 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
13struct mctp_binding_bridge {
14 struct mctp_binding binding;
15 int rx_count;
16 int tx_count;
17 uint8_t last_pkt_data;
18};
19
20struct test_ctx {
21 struct mctp *mctp;
22 struct mctp_binding_bridge *bindings[2];
23};
24
25static 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
Andrew Jefferycdfafd92020-02-07 17:10:34 +103038static void mctp_binding_bridge_rx(struct mctp_binding_bridge *binding,
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080039 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);
Andrew Jefferycdfafd92020-02-07 17:10:34 +103047 assert(pkt);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080048
49 hdr = mctp_pktbuf_hdr(pkt);
50 hdr->flags_seq_tag = MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM;
51
52 /* arbitrary src/dest, as we're bridging */
53 hdr->src = 1;
54 hdr->dest = 2;
55
56 buf = mctp_pktbuf_data(pkt);
57 *buf = key;
58
59 binding->rx_count++;
60 mctp_bus_rx(&binding->binding, pkt);
61}
62
63static struct mctp_binding_bridge *mctp_binding_bridge_init(void)
64{
65 struct mctp_binding_bridge *binding;
66
67 binding = __mctp_alloc(sizeof(*binding));
68 memset(binding, 0, sizeof(*binding));
69 binding->binding.name = "test";
70 binding->binding.version = 1;
71 binding->binding.tx = mctp_binding_bridge_tx;
Andrew Jeffery73c268e2020-01-30 10:16:09 +103072 binding->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080073 binding->binding.pkt_pad = 0;
74 return binding;
75}
76
77int main(void)
78{
79 struct test_ctx _ctx, *ctx = &_ctx;
80
81 ctx->mctp = mctp_init();
82 ctx->bindings[0] = mctp_binding_bridge_init();
83 ctx->bindings[1] = mctp_binding_bridge_init();
84
85 mctp_bridge_busses(ctx->mctp,
86 &ctx->bindings[0]->binding,
87 &ctx->bindings[1]->binding);
88
89 mctp_binding_set_tx_enabled(&ctx->bindings[0]->binding, true);
90 mctp_binding_set_tx_enabled(&ctx->bindings[1]->binding, true);
91
92 mctp_binding_bridge_rx(ctx->bindings[0], 0xaa);
93 assert(ctx->bindings[0]->tx_count == 0);
94 assert(ctx->bindings[1]->tx_count == 1);
95 assert(ctx->bindings[1]->last_pkt_data == 0xaa);
96
97 mctp_binding_bridge_rx(ctx->bindings[1], 0x55);
98 assert(ctx->bindings[1]->tx_count == 1);
99 assert(ctx->bindings[0]->tx_count == 1);
100 assert(ctx->bindings[0]->last_pkt_data == 0x55);
101
102 return EXIT_SUCCESS;
103}