11a4ec3cdSJeremy Kerr /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
21a4ec3cdSJeremy Kerr
3e915aad4SRashmica Gupta #ifdef NDEBUG
4e915aad4SRashmica Gupta #undef NDEBUG
5e915aad4SRashmica Gupta #endif
6e915aad4SRashmica Gupta
71a4ec3cdSJeremy Kerr #include <assert.h>
81a4ec3cdSJeremy Kerr #include <stdlib.h>
91a4ec3cdSJeremy Kerr #include <stdio.h>
101a4ec3cdSJeremy Kerr #include <string.h>
111a4ec3cdSJeremy Kerr
121a4ec3cdSJeremy Kerr #include <libmctp.h>
131a4ec3cdSJeremy Kerr #include <libmctp-alloc.h>
141a4ec3cdSJeremy Kerr
151a4ec3cdSJeremy Kerr #include "test-utils.h"
161a4ec3cdSJeremy Kerr
171a4ec3cdSJeremy Kerr struct mctp_binding_bridge {
181a4ec3cdSJeremy Kerr struct mctp_binding binding;
191a4ec3cdSJeremy Kerr int rx_count;
201a4ec3cdSJeremy Kerr int tx_count;
211a4ec3cdSJeremy Kerr uint8_t last_pkt_data;
22*4a09e1dcSMatt Johnston uint8_t tx_storage[MCTP_PKTBUF_SIZE(MCTP_BTU)];
231a4ec3cdSJeremy Kerr };
241a4ec3cdSJeremy Kerr
251a4ec3cdSJeremy Kerr struct test_ctx {
261a4ec3cdSJeremy Kerr struct mctp *mctp;
271a4ec3cdSJeremy Kerr struct mctp_binding_bridge *bindings[2];
281a4ec3cdSJeremy Kerr };
291a4ec3cdSJeremy Kerr
mctp_binding_bridge_tx(struct mctp_binding * b,struct mctp_pktbuf * pkt)301a4ec3cdSJeremy Kerr static int mctp_binding_bridge_tx(struct mctp_binding *b,
311a4ec3cdSJeremy Kerr struct mctp_pktbuf *pkt)
321a4ec3cdSJeremy Kerr {
33a721c2d8SPatrick Williams struct mctp_binding_bridge *binding =
34a721c2d8SPatrick Williams container_of(b, struct mctp_binding_bridge, binding);
351a4ec3cdSJeremy Kerr
361a4ec3cdSJeremy Kerr binding->tx_count++;
371a4ec3cdSJeremy Kerr assert(mctp_pktbuf_size(pkt) == sizeof(struct mctp_hdr) + 1);
381a4ec3cdSJeremy Kerr binding->last_pkt_data = *(uint8_t *)mctp_pktbuf_data(pkt);
391a4ec3cdSJeremy Kerr
401a4ec3cdSJeremy Kerr return 0;
411a4ec3cdSJeremy Kerr }
421a4ec3cdSJeremy Kerr
mctp_binding_bridge_rx(struct mctp_binding_bridge * binding,uint8_t key)43cdfafd91SAndrew Jeffery static void mctp_binding_bridge_rx(struct mctp_binding_bridge *binding,
441a4ec3cdSJeremy Kerr uint8_t key)
451a4ec3cdSJeremy Kerr {
461a4ec3cdSJeremy Kerr struct mctp_pktbuf *pkt;
471a4ec3cdSJeremy Kerr struct mctp_hdr *hdr;
481a4ec3cdSJeremy Kerr uint8_t *buf;
491a4ec3cdSJeremy Kerr
50a721c2d8SPatrick Williams pkt = mctp_pktbuf_alloc(&binding->binding, sizeof(struct mctp_hdr) + 1);
51cdfafd91SAndrew Jeffery assert(pkt);
521a4ec3cdSJeremy Kerr
531a4ec3cdSJeremy Kerr hdr = mctp_pktbuf_hdr(pkt);
541a4ec3cdSJeremy Kerr hdr->flags_seq_tag = MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM;
551a4ec3cdSJeremy Kerr
561a4ec3cdSJeremy Kerr /* arbitrary src/dest, as we're bridging */
571a4ec3cdSJeremy Kerr hdr->src = 1;
581a4ec3cdSJeremy Kerr hdr->dest = 2;
591a4ec3cdSJeremy Kerr
601a4ec3cdSJeremy Kerr buf = mctp_pktbuf_data(pkt);
611a4ec3cdSJeremy Kerr *buf = key;
621a4ec3cdSJeremy Kerr
631a4ec3cdSJeremy Kerr binding->rx_count++;
641a4ec3cdSJeremy Kerr mctp_bus_rx(&binding->binding, pkt);
65*4a09e1dcSMatt Johnston mctp_pktbuf_free(pkt);
661a4ec3cdSJeremy Kerr }
671a4ec3cdSJeremy Kerr
mctp_binding_bridge_init(char * name)68ca85ad86SRashmica Gupta static struct mctp_binding_bridge *mctp_binding_bridge_init(char *name)
691a4ec3cdSJeremy Kerr {
701a4ec3cdSJeremy Kerr struct mctp_binding_bridge *binding;
711a4ec3cdSJeremy Kerr
721a4ec3cdSJeremy Kerr binding = __mctp_alloc(sizeof(*binding));
731a4ec3cdSJeremy Kerr memset(binding, 0, sizeof(*binding));
74ca85ad86SRashmica Gupta binding->binding.name = name;
751a4ec3cdSJeremy Kerr binding->binding.version = 1;
761a4ec3cdSJeremy Kerr binding->binding.tx = mctp_binding_bridge_tx;
7773c268e4SAndrew Jeffery binding->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
7839da3d03SAndrew Jeffery binding->binding.pkt_header = 0;
7939da3d03SAndrew Jeffery binding->binding.pkt_trailer = 0;
80*4a09e1dcSMatt Johnston binding->binding.tx_storage = binding->tx_storage;
811a4ec3cdSJeremy Kerr return binding;
821a4ec3cdSJeremy Kerr }
831a4ec3cdSJeremy Kerr
main(void)841a4ec3cdSJeremy Kerr int main(void)
851a4ec3cdSJeremy Kerr {
861a4ec3cdSJeremy Kerr struct test_ctx _ctx, *ctx = &_ctx;
871a4ec3cdSJeremy Kerr ctx->mctp = mctp_init();
881a4ec3cdSJeremy Kerr
89ca85ad86SRashmica Gupta ctx->bindings[0] = mctp_binding_bridge_init("binding0");
90ca85ad86SRashmica Gupta ctx->bindings[1] = mctp_binding_bridge_init("binding1");
91a721c2d8SPatrick Williams mctp_bridge_busses(ctx->mctp, &ctx->bindings[0]->binding,
921a4ec3cdSJeremy Kerr &ctx->bindings[1]->binding);
931a4ec3cdSJeremy Kerr
941a4ec3cdSJeremy Kerr mctp_binding_set_tx_enabled(&ctx->bindings[0]->binding, true);
951a4ec3cdSJeremy Kerr mctp_binding_set_tx_enabled(&ctx->bindings[1]->binding, true);
961a4ec3cdSJeremy Kerr
971a4ec3cdSJeremy Kerr mctp_binding_bridge_rx(ctx->bindings[0], 0xaa);
981a4ec3cdSJeremy Kerr assert(ctx->bindings[0]->tx_count == 0);
991a4ec3cdSJeremy Kerr assert(ctx->bindings[1]->tx_count == 1);
1001a4ec3cdSJeremy Kerr assert(ctx->bindings[1]->last_pkt_data == 0xaa);
1011a4ec3cdSJeremy Kerr
1021a4ec3cdSJeremy Kerr mctp_binding_bridge_rx(ctx->bindings[1], 0x55);
1031a4ec3cdSJeremy Kerr assert(ctx->bindings[1]->tx_count == 1);
1041a4ec3cdSJeremy Kerr assert(ctx->bindings[0]->tx_count == 1);
1051a4ec3cdSJeremy Kerr assert(ctx->bindings[0]->last_pkt_data == 0x55);
1061a4ec3cdSJeremy Kerr
107c8908acbSAndrew Jeffery __mctp_free(ctx->bindings[1]);
108c8908acbSAndrew Jeffery __mctp_free(ctx->bindings[0]);
109c8908acbSAndrew Jeffery mctp_destroy(ctx->mctp);
110c8908acbSAndrew Jeffery
1111a4ec3cdSJeremy Kerr return EXIT_SUCCESS;
1121a4ec3cdSJeremy Kerr }
113