blob: 5e7847457636f5106b14c44fae5113e2bbea522d [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Jeremy Kerr18925112019-03-13 15:43:08 +08002
Rashmica Guptae915aad2022-08-10 16:28:59 +10003#ifdef NDEBUG
4#undef NDEBUG
5#endif
6
Jeremy Kerr18925112019-03-13 15:43:08 +08007#include <string.h>
8#include <assert.h>
9
10#include <libmctp.h>
11#include <libmctp-alloc.h>
12
13#include "test-utils.h"
14
Matt Johnston0a965442024-09-16 16:49:20 +080015/* mctp_binding_test can be used for loopback in tests. Senders must use
16 * the local EID as the destination */
Jeremy Kerr18925112019-03-13 15:43:08 +080017struct mctp_binding_test {
Andrew Jeffery69eda312022-09-30 14:22:32 +093018 struct mctp_binding binding;
Matt Johnston4a09e1d2024-09-13 14:55:58 +080019 uint8_t tx_storage[MCTP_PKTBUF_SIZE(MCTP_BTU)];
Jeremy Kerr18925112019-03-13 15:43:08 +080020};
21
Matt Johnston0a965442024-09-16 16:49:20 +080022static int mctp_binding_test_tx(struct mctp_binding *b, struct mctp_pktbuf *pkt)
Jeremy Kerr18925112019-03-13 15:43:08 +080023{
Matt Johnston0a965442024-09-16 16:49:20 +080024 mctp_bus_rx(b, pkt);
Andrew Jefferya501a582020-03-10 22:25:16 +103025 return 0;
Jeremy Kerr18925112019-03-13 15:43:08 +080026}
27
28struct mctp_binding_test *mctp_binding_test_init(void)
29{
30 struct mctp_binding_test *test;
Jeremy Kerr18925112019-03-13 15:43:08 +080031 test = __mctp_alloc(sizeof(*test));
Andrew Jeffery225c46d2020-03-10 23:20:01 +103032 memset(test, '\0', sizeof(*test));
Jeremy Kerr18925112019-03-13 15:43:08 +080033 test->binding.name = "test";
34 test->binding.version = 1;
35 test->binding.tx = mctp_binding_test_tx;
Andrew Jeffery73c268e2020-01-30 10:16:09 +103036 test->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
Andrew Jeffery39da3d02021-03-12 16:51:26 +103037 test->binding.pkt_header = 0;
38 test->binding.pkt_trailer = 0;
Matt Johnston4a09e1d2024-09-13 14:55:58 +080039 test->binding.tx_storage = test->tx_storage;
Jeremy Kerr18925112019-03-13 15:43:08 +080040 return test;
41}
42
Andrew Jeffery5c5673b2020-03-10 23:23:58 +103043void mctp_binding_test_destroy(struct mctp_binding_test *test)
44{
45 __mctp_free(test);
46}
47
Andrew Jeffery69eda312022-09-30 14:22:32 +093048void mctp_binding_test_rx_raw(struct mctp_binding_test *test, void *buf,
49 size_t len)
Jeremy Kerr18925112019-03-13 15:43:08 +080050{
51 struct mctp_pktbuf *pkt;
52
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080053 pkt = mctp_pktbuf_alloc(&test->binding, len);
Jeremy Kerr18925112019-03-13 15:43:08 +080054 assert(pkt);
55 memcpy(mctp_pktbuf_hdr(pkt), buf, len);
56 mctp_bus_rx(&test->binding, pkt);
Matt Johnston4a09e1d2024-09-13 14:55:58 +080057 mctp_pktbuf_free(pkt);
Jeremy Kerr18925112019-03-13 15:43:08 +080058}
59
60void mctp_binding_test_register_bus(struct mctp_binding_test *binding,
Andrew Jeffery69eda312022-09-30 14:22:32 +093061 struct mctp *mctp, mctp_eid_t eid)
Jeremy Kerr18925112019-03-13 15:43:08 +080062{
63 mctp_register_bus(mctp, &binding->binding, eid);
64}
65
66void mctp_test_stack_init(struct mctp **mctp,
Andrew Jeffery69eda312022-09-30 14:22:32 +093067 struct mctp_binding_test **binding, mctp_eid_t eid)
Jeremy Kerr18925112019-03-13 15:43:08 +080068{
69 *mctp = mctp_init();
70 assert(*mctp);
71
72 *binding = mctp_binding_test_init();
73 assert(*binding);
74
75 mctp_binding_test_register_bus(*binding, *mctp, eid);
Matt Johnston0a965442024-09-16 16:49:20 +080076 mctp_binding_set_tx_enabled(&(*binding)->binding, true);
Jeremy Kerr18925112019-03-13 15:43:08 +080077}