blob: a7c37527e38367ee1aab8e7fd200a767671d4a20 [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);
36 assert(rc == len);
Andrew Jeffery89a2b8e2020-02-07 17:07:38 +103037
38 return rc;
Andrew Jeffery25de0932020-02-06 15:56:12 +103039}
40
41uint8_t mctp_msg_src[2 * MCTP_BTU];
42
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103043static bool seen;
44
Andrew Jeffery25de0932020-02-06 15:56:12 +103045static void rx_message(uint8_t eid, void *data, void *msg, size_t len)
46{
47 uint8_t type;
48
49 type = *(uint8_t *)msg;
50
51 mctp_prdebug("MCTP message received: len %zd, type %d", len, type);
52
53 assert(sizeof(mctp_msg_src) == len);
54 assert(!memcmp(mctp_msg_src, msg, len));
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103055
56 seen = true;
Andrew Jeffery25de0932020-02-06 15:56:12 +103057}
58
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103059struct serial_test {
60 struct mctp_binding_serial_pipe binding;
61 struct mctp *mctp;
62};
63
Andrew Jeffery25de0932020-02-06 15:56:12 +103064int main(void)
65{
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103066 struct serial_test scenario[2];
67
68 struct mctp_binding_serial_pipe *a = &scenario[0].binding;
69 struct mctp_binding_serial_pipe *b = &scenario[1].binding;
Andrew Jeffery25de0932020-02-06 15:56:12 +103070 int p[2][2];
71 int rc;
72
73 mctp_set_log_stdio(MCTP_LOG_DEBUG);
74
75 memset(&mctp_msg_src[0], 0x5a, MCTP_BTU);
76 memset(&mctp_msg_src[MCTP_BTU], 0xa5, MCTP_BTU);
77
78 rc = pipe(p[0]);
79 assert(!rc);
80
81 rc = pipe(p[1]);
82 assert(!rc);
83
Andrew Jeffery25de0932020-02-06 15:56:12 +103084 /* Instantiate the A side of the serial pipe */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103085 scenario[0].mctp = mctp_init();
86 assert(scenario[0].mctp);
Andrew Jeffery25de0932020-02-06 15:56:12 +103087 a->serial = mctp_serial_init();
88 assert(a->serial);
89 a->ingress = p[0][0];
90 a->egress = p[1][1];
91 mctp_serial_open_fd(a->serial, a->ingress);
92 mctp_serial_set_tx_fn(a->serial, mctp_binding_serial_pipe_tx, a);
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103093 mctp_register_bus(scenario[0].mctp, mctp_binding_serial_core(a->serial), 8);
Andrew Jeffery25de0932020-02-06 15:56:12 +103094
95 /* Instantiate the B side of the serial pipe */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +103096 scenario[1].mctp = mctp_init();
97 assert(scenario[1].mctp);
98 mctp_set_rx_all(scenario[1].mctp, rx_message, NULL);
Andrew Jeffery25de0932020-02-06 15:56:12 +103099 b->serial = mctp_serial_init();
100 assert(b->serial);
101 b->ingress = p[1][0];
102 b->egress = p[0][1];
103 mctp_serial_open_fd(b->serial, b->ingress);
104 mctp_serial_set_tx_fn(b->serial, mctp_binding_serial_pipe_tx, a);
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030105 mctp_register_bus(scenario[1].mctp, mctp_binding_serial_core(b->serial), 9);
Andrew Jeffery25de0932020-02-06 15:56:12 +1030106
107 /* Transmit a message from A to B */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030108 rc = mctp_message_tx(scenario[0].mctp, 9, mctp_msg_src, sizeof(mctp_msg_src));
Andrew Jeffery25de0932020-02-06 15:56:12 +1030109 assert(rc == 0);
110
111 /* Read the message at B from A */
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030112 seen = false;
Andrew Jeffery25de0932020-02-06 15:56:12 +1030113 mctp_serial_read(b->serial);
Andrew Jefferyb633b3f2020-02-25 15:49:22 +1030114 assert(seen);
Andrew Jeffery25de0932020-02-06 15:56:12 +1030115
116 return 0;
117}