blob: 905f6648a6f4ef027a102ebafd9b94e0d2f6ce20 [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
5#include "config.h"
6
7#include "libmctp-log.h"
8#include "libmctp-serial.h"
9
10#ifdef NDEBUG
11#undef NDEBUG
12#endif
13
14#include <assert.h>
15#include <fcntl.h>
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103016#include <stdbool.h>
Andrew Jeffery25de0932020-02-06 15:56:12 +103017#include <stdio.h>
18#include <string.h>
19#include <unistd.h>
20
21struct mctp_binding_serial_pipe {
22 int ingress;
23 int egress;
24
25 struct mctp_binding_serial *serial;
26};
27
28static int mctp_binding_serial_pipe_tx(void *data, void *buf, size_t len)
29{
30 struct mctp_binding_serial_pipe *ctx = data;
31 ssize_t rc;
32
33 rc = write(ctx->egress, buf, len);
34 assert(rc == len);
35}
36
37uint8_t mctp_msg_src[2 * MCTP_BTU];
38
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103039static bool seen;
40
Andrew Jeffery25de0932020-02-06 15:56:12 +103041static void rx_message(uint8_t eid, void *data, void *msg, size_t len)
42{
43 uint8_t type;
44
45 type = *(uint8_t *)msg;
46
47 mctp_prdebug("MCTP message received: len %zd, type %d", len, type);
48
49 assert(sizeof(mctp_msg_src) == len);
50 assert(!memcmp(mctp_msg_src, msg, len));
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103051
52 seen = true;
Andrew Jeffery25de0932020-02-06 15:56:12 +103053}
54
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103055struct serial_test {
56 struct mctp_binding_serial_pipe binding;
57 struct mctp *mctp;
58};
59
Andrew Jeffery25de0932020-02-06 15:56:12 +103060int main(void)
61{
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103062 struct serial_test scenario[2];
63
64 struct mctp_binding_serial_pipe *a = &scenario[0].binding;
65 struct mctp_binding_serial_pipe *b = &scenario[1].binding;
Andrew Jeffery25de0932020-02-06 15:56:12 +103066 int p[2][2];
67 int rc;
68
69 mctp_set_log_stdio(MCTP_LOG_DEBUG);
70
71 memset(&mctp_msg_src[0], 0x5a, MCTP_BTU);
72 memset(&mctp_msg_src[MCTP_BTU], 0xa5, MCTP_BTU);
73
74 rc = pipe(p[0]);
75 assert(!rc);
76
77 rc = pipe(p[1]);
78 assert(!rc);
79
Andrew Jeffery25de0932020-02-06 15:56:12 +103080 /* Instantiate the A side of the serial pipe */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103081 scenario[0].mctp = mctp_init();
82 assert(scenario[0].mctp);
Andrew Jeffery25de0932020-02-06 15:56:12 +103083 a->serial = mctp_serial_init();
84 assert(a->serial);
85 a->ingress = p[0][0];
86 a->egress = p[1][1];
87 mctp_serial_open_fd(a->serial, a->ingress);
88 mctp_serial_set_tx_fn(a->serial, mctp_binding_serial_pipe_tx, a);
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103089 mctp_register_bus(scenario[0].mctp, mctp_binding_serial_core(a->serial), 8);
Andrew Jeffery25de0932020-02-06 15:56:12 +103090
91 /* Instantiate the B side of the serial pipe */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103092 scenario[1].mctp = mctp_init();
93 assert(scenario[1].mctp);
94 mctp_set_rx_all(scenario[1].mctp, rx_message, NULL);
Andrew Jeffery25de0932020-02-06 15:56:12 +103095 b->serial = mctp_serial_init();
96 assert(b->serial);
97 b->ingress = p[1][0];
98 b->egress = p[0][1];
99 mctp_serial_open_fd(b->serial, b->ingress);
100 mctp_serial_set_tx_fn(b->serial, mctp_binding_serial_pipe_tx, a);
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030101 mctp_register_bus(scenario[1].mctp, mctp_binding_serial_core(b->serial), 9);
Andrew Jeffery25de0932020-02-06 15:56:12 +1030102
103 /* Transmit a message from A to B */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030104 rc = mctp_message_tx(scenario[0].mctp, 9, mctp_msg_src, sizeof(mctp_msg_src));
Andrew Jeffery25de0932020-02-06 15:56:12 +1030105 assert(rc == 0);
106
107 /* Read the message at B from A */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030108 seen = false;
Andrew Jeffery25de0932020-02-06 15:56:12 +1030109 mctp_serial_read(b->serial);
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030110 assert(seen);
Andrew Jeffery25de0932020-02-06 15:56:12 +1030111
112 return 0;
113}