blob: e3a53a67c40a2030ee2a2e9c70817917cb1c5905 [file] [log] [blame]
Andrew Jeffery25de0932020-02-06 15:56:12 +10301/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2
3#define _GNU_SOURCE
4
Andrew Jeffery2cda40f2020-02-28 15:26:20 +10305#if HAVE_CONFIG_H
Andrew Jeffery25de0932020-02-06 15:56:12 +10306#include "config.h"
Andrew Jeffery2cda40f2020-02-28 15:26:20 +10307#endif
Andrew Jeffery25de0932020-02-06 15:56:12 +10308
Andrew Jeffery5ab78252022-02-17 21:04:59 +10309#include "compiler.h"
Andrew Jeffery25de0932020-02-06 15:56:12 +103010#include "libmctp-log.h"
11#include "libmctp-serial.h"
12
13#ifdef NDEBUG
14#undef NDEBUG
15#endif
16
17#include <assert.h>
Andrew Jeffery89a28782022-09-29 11:46:31 +093018#include <errno.h>
Andrew Jeffery25de0932020-02-06 15:56:12 +103019#include <fcntl.h>
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103020#include <stdbool.h>
Andrew Jeffery25de0932020-02-06 15:56:12 +103021#include <stdio.h>
22#include <string.h>
23#include <unistd.h>
24
25struct mctp_binding_serial_pipe {
26 int ingress;
27 int egress;
28
29 struct mctp_binding_serial *serial;
30};
31
32static int mctp_binding_serial_pipe_tx(void *data, void *buf, size_t len)
33{
34 struct mctp_binding_serial_pipe *ctx = data;
35 ssize_t rc;
36
37 rc = write(ctx->egress, buf, len);
Andrew Jefferyb93b6112020-06-05 14:13:44 +093038 assert(rc >= 0);
39 assert((size_t)rc == len);
Andrew Jeffery89a2b8e2020-02-07 17:07:38 +103040
41 return rc;
Andrew Jeffery25de0932020-02-06 15:56:12 +103042}
43
44uint8_t mctp_msg_src[2 * MCTP_BTU];
45
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103046static bool seen;
Sumanth Bhatf39c3852022-01-10 17:04:10 +053047static bool received_tag_owner;
48static uint8_t received_msg_tag;
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103049
Sumanth Bhatf39c3852022-01-10 17:04:10 +053050static void rx_message(uint8_t eid __unused, bool tag_owner, uint8_t msg_tag,
51 void *data __unused, void *msg, size_t len)
Andrew Jeffery25de0932020-02-06 15:56:12 +103052{
53 uint8_t type;
54
55 type = *(uint8_t *)msg;
56
Sumanth Bhatf39c3852022-01-10 17:04:10 +053057 mctp_prdebug("MCTP message received: len %zd, type %d, tag %d", len,
58 type, msg_tag);
Andrew Jeffery25de0932020-02-06 15:56:12 +103059
60 assert(sizeof(mctp_msg_src) == len);
61 assert(!memcmp(mctp_msg_src, msg, len));
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103062
63 seen = true;
Sumanth Bhatf39c3852022-01-10 17:04:10 +053064 received_msg_tag = msg_tag;
65 received_tag_owner = tag_owner;
Andrew Jeffery25de0932020-02-06 15:56:12 +103066}
67
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103068struct serial_test {
69 struct mctp_binding_serial_pipe binding;
70 struct mctp *mctp;
71};
72
Andrew Jeffery25de0932020-02-06 15:56:12 +103073int main(void)
74{
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103075 struct serial_test scenario[2];
76
Andrew Jefferyf0315a32020-03-10 23:49:41 +103077 struct mctp_binding_serial_pipe *a;
78 struct mctp_binding_serial_pipe *b;
Sumanth Bhatf39c3852022-01-10 17:04:10 +053079 uint8_t msg_tag = 2;
80 bool tag_owner = false;
Andrew Jeffery25de0932020-02-06 15:56:12 +103081 int p[2][2];
82 int rc;
83
84 mctp_set_log_stdio(MCTP_LOG_DEBUG);
85
86 memset(&mctp_msg_src[0], 0x5a, MCTP_BTU);
87 memset(&mctp_msg_src[MCTP_BTU], 0xa5, MCTP_BTU);
88
89 rc = pipe(p[0]);
90 assert(!rc);
91
92 rc = pipe(p[1]);
93 assert(!rc);
94
Andrew Jeffery25de0932020-02-06 15:56:12 +103095 /* Instantiate the A side of the serial pipe */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103096 scenario[0].mctp = mctp_init();
97 assert(scenario[0].mctp);
Andrew Jefferyf0315a32020-03-10 23:49:41 +103098 scenario[0].binding.serial = mctp_serial_init();
99 assert(scenario[0].binding.serial);
100 a = &scenario[0].binding;
Andrew Jeffery25de0932020-02-06 15:56:12 +1030101 a->ingress = p[0][0];
102 a->egress = p[1][1];
103 mctp_serial_open_fd(a->serial, a->ingress);
104 mctp_serial_set_tx_fn(a->serial, mctp_binding_serial_pipe_tx, a);
Andrew Jeffery695deed2022-09-30 14:28:24 +0930105 mctp_register_bus(scenario[0].mctp, mctp_binding_serial_core(a->serial),
106 8);
Andrew Jeffery25de0932020-02-06 15:56:12 +1030107
108 /* Instantiate the B side of the serial pipe */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030109 scenario[1].mctp = mctp_init();
110 assert(scenario[1].mctp);
111 mctp_set_rx_all(scenario[1].mctp, rx_message, NULL);
Andrew Jefferyf0315a32020-03-10 23:49:41 +1030112 scenario[1].binding.serial = mctp_serial_init();
113 assert(scenario[1].binding.serial);
114 b = &scenario[1].binding;
Andrew Jeffery25de0932020-02-06 15:56:12 +1030115 b->ingress = p[1][0];
116 b->egress = p[0][1];
117 mctp_serial_open_fd(b->serial, b->ingress);
118 mctp_serial_set_tx_fn(b->serial, mctp_binding_serial_pipe_tx, a);
Andrew Jeffery695deed2022-09-30 14:28:24 +0930119 mctp_register_bus(scenario[1].mctp, mctp_binding_serial_core(b->serial),
120 9);
Andrew Jeffery25de0932020-02-06 15:56:12 +1030121
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530122 /* Transmit a message from A to B, with message tag */
123 rc = mctp_message_tx(scenario[0].mctp, 9, tag_owner, msg_tag,
124 mctp_msg_src, sizeof(mctp_msg_src));
Andrew Jeffery25de0932020-02-06 15:56:12 +1030125 assert(rc == 0);
126
127 /* Read the message at B from A */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030128 seen = false;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530129 received_tag_owner = true;
130 received_msg_tag = 0;
Andrew Jeffery25de0932020-02-06 15:56:12 +1030131 mctp_serial_read(b->serial);
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030132 assert(seen);
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530133 assert(received_tag_owner == tag_owner);
134 assert(received_msg_tag == msg_tag);
Andrew Jeffery25de0932020-02-06 15:56:12 +1030135
Andrew Jefferyf0315a32020-03-10 23:49:41 +1030136 mctp_serial_destroy(scenario[1].binding.serial);
137 mctp_destroy(scenario[1].mctp);
138 mctp_serial_destroy(scenario[0].binding.serial);
139 mctp_destroy(scenario[0].mctp);
140
Andrew Jeffery25de0932020-02-06 15:56:12 +1030141 return 0;
142}