blob: 12d1476d89f52d7e37d73c49cdc276784da0b2d6 [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
9#include "libmctp-log.h"
10#include "libmctp-serial.h"
11
12#ifdef NDEBUG
13#undef NDEBUG
14#endif
15
16#include <assert.h>
17#include <fcntl.h>
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103018#include <stdbool.h>
Andrew Jeffery25de0932020-02-06 15:56:12 +103019#include <stdio.h>
20#include <string.h>
21#include <unistd.h>
22
23struct mctp_binding_serial_pipe {
24 int ingress;
25 int egress;
26
27 struct mctp_binding_serial *serial;
28};
29
30static int mctp_binding_serial_pipe_tx(void *data, void *buf, size_t len)
31{
32 struct mctp_binding_serial_pipe *ctx = data;
33 ssize_t rc;
34
35 rc = write(ctx->egress, buf, len);
Andrew Jefferyb93b6112020-06-05 14:13:44 +093036 assert(rc >= 0);
37 assert((size_t)rc == len);
Andrew Jeffery89a2b8e2020-02-07 17:07:38 +103038
39 return rc;
Andrew Jeffery25de0932020-02-06 15:56:12 +103040}
41
42uint8_t mctp_msg_src[2 * MCTP_BTU];
43
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103044static bool seen;
45
Andrew Jefferyb93b6112020-06-05 14:13:44 +093046#define __unused __attribute__((unused))
47
48static void rx_message(uint8_t eid __unused, void *data __unused, void *msg,
49 size_t len)
Andrew Jeffery25de0932020-02-06 15:56:12 +103050{
51 uint8_t type;
52
53 type = *(uint8_t *)msg;
54
55 mctp_prdebug("MCTP message received: len %zd, type %d", len, type);
56
57 assert(sizeof(mctp_msg_src) == len);
58 assert(!memcmp(mctp_msg_src, msg, len));
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103059
60 seen = true;
Andrew Jeffery25de0932020-02-06 15:56:12 +103061}
62
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103063struct serial_test {
64 struct mctp_binding_serial_pipe binding;
65 struct mctp *mctp;
66};
67
Andrew Jeffery25de0932020-02-06 15:56:12 +103068int main(void)
69{
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103070 struct serial_test scenario[2];
71
Andrew Jefferyf0315a32020-03-10 23:49:41 +103072 struct mctp_binding_serial_pipe *a;
73 struct mctp_binding_serial_pipe *b;
Andrew Jeffery25de0932020-02-06 15:56:12 +103074 int p[2][2];
75 int rc;
76
77 mctp_set_log_stdio(MCTP_LOG_DEBUG);
78
79 memset(&mctp_msg_src[0], 0x5a, MCTP_BTU);
80 memset(&mctp_msg_src[MCTP_BTU], 0xa5, MCTP_BTU);
81
82 rc = pipe(p[0]);
83 assert(!rc);
84
85 rc = pipe(p[1]);
86 assert(!rc);
87
Andrew Jeffery25de0932020-02-06 15:56:12 +103088 /* Instantiate the A side of the serial pipe */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103089 scenario[0].mctp = mctp_init();
90 assert(scenario[0].mctp);
Andrew Jefferyf0315a32020-03-10 23:49:41 +103091 scenario[0].binding.serial = mctp_serial_init();
92 assert(scenario[0].binding.serial);
93 a = &scenario[0].binding;
Andrew Jeffery25de0932020-02-06 15:56:12 +103094 a->ingress = p[0][0];
95 a->egress = p[1][1];
96 mctp_serial_open_fd(a->serial, a->ingress);
97 mctp_serial_set_tx_fn(a->serial, mctp_binding_serial_pipe_tx, a);
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103098 mctp_register_bus(scenario[0].mctp, mctp_binding_serial_core(a->serial), 8);
Andrew Jeffery25de0932020-02-06 15:56:12 +103099
100 /* Instantiate the B side of the serial pipe */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030101 scenario[1].mctp = mctp_init();
102 assert(scenario[1].mctp);
103 mctp_set_rx_all(scenario[1].mctp, rx_message, NULL);
Andrew Jefferyf0315a32020-03-10 23:49:41 +1030104 scenario[1].binding.serial = mctp_serial_init();
105 assert(scenario[1].binding.serial);
106 b = &scenario[1].binding;
Andrew Jeffery25de0932020-02-06 15:56:12 +1030107 b->ingress = p[1][0];
108 b->egress = p[0][1];
109 mctp_serial_open_fd(b->serial, b->ingress);
110 mctp_serial_set_tx_fn(b->serial, mctp_binding_serial_pipe_tx, a);
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030111 mctp_register_bus(scenario[1].mctp, mctp_binding_serial_core(b->serial), 9);
Andrew Jeffery25de0932020-02-06 15:56:12 +1030112
113 /* Transmit a message from A to B */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030114 rc = mctp_message_tx(scenario[0].mctp, 9, mctp_msg_src, sizeof(mctp_msg_src));
Andrew Jeffery25de0932020-02-06 15:56:12 +1030115 assert(rc == 0);
116
117 /* Read the message at B from A */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030118 seen = false;
Andrew Jeffery25de0932020-02-06 15:56:12 +1030119 mctp_serial_read(b->serial);
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030120 assert(seen);
Andrew Jeffery25de0932020-02-06 15:56:12 +1030121
Andrew Jefferyf0315a32020-03-10 23:49:41 +1030122 mctp_serial_destroy(scenario[1].binding.serial);
123 mctp_destroy(scenario[1].mctp);
124 mctp_serial_destroy(scenario[0].binding.serial);
125 mctp_destroy(scenario[0].mctp);
126
Andrew Jeffery25de0932020-02-06 15:56:12 +1030127 return 0;
128}