Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 1 | /* 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> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | struct mctp_binding_serial_pipe { |
| 21 | int ingress; |
| 22 | int egress; |
| 23 | |
| 24 | struct mctp_binding_serial *serial; |
| 25 | }; |
| 26 | |
| 27 | static int mctp_binding_serial_pipe_tx(void *data, void *buf, size_t len) |
| 28 | { |
| 29 | struct mctp_binding_serial_pipe *ctx = data; |
| 30 | ssize_t rc; |
| 31 | |
| 32 | rc = write(ctx->egress, buf, len); |
| 33 | assert(rc == len); |
| 34 | } |
| 35 | |
| 36 | uint8_t mctp_msg_src[2 * MCTP_BTU]; |
| 37 | |
| 38 | static void rx_message(uint8_t eid, void *data, void *msg, size_t len) |
| 39 | { |
| 40 | uint8_t type; |
| 41 | |
| 42 | type = *(uint8_t *)msg; |
| 43 | |
| 44 | mctp_prdebug("MCTP message received: len %zd, type %d", len, type); |
| 45 | |
| 46 | assert(sizeof(mctp_msg_src) == len); |
| 47 | assert(!memcmp(mctp_msg_src, msg, len)); |
| 48 | } |
| 49 | |
| 50 | int main(void) |
| 51 | { |
| 52 | struct mctp_binding_serial_pipe _a, *a = &_a; |
| 53 | struct mctp_binding_serial_pipe _b, *b = &_b; |
| 54 | struct mctp *mctp; |
| 55 | int p[2][2]; |
| 56 | int rc; |
| 57 | |
| 58 | mctp_set_log_stdio(MCTP_LOG_DEBUG); |
| 59 | |
| 60 | memset(&mctp_msg_src[0], 0x5a, MCTP_BTU); |
| 61 | memset(&mctp_msg_src[MCTP_BTU], 0xa5, MCTP_BTU); |
| 62 | |
| 63 | rc = pipe(p[0]); |
| 64 | assert(!rc); |
| 65 | |
| 66 | rc = pipe(p[1]); |
| 67 | assert(!rc); |
| 68 | |
| 69 | mctp = mctp_init(); |
| 70 | assert(mctp); |
| 71 | |
| 72 | mctp_set_rx_all(mctp, rx_message, NULL); |
| 73 | |
| 74 | /* Instantiate the A side of the serial pipe */ |
| 75 | a->serial = mctp_serial_init(); |
| 76 | assert(a->serial); |
| 77 | a->ingress = p[0][0]; |
| 78 | a->egress = p[1][1]; |
| 79 | mctp_serial_open_fd(a->serial, a->ingress); |
| 80 | mctp_serial_set_tx_fn(a->serial, mctp_binding_serial_pipe_tx, a); |
| 81 | mctp_register_bus(mctp, mctp_binding_serial_core(a->serial), 8); |
| 82 | |
| 83 | /* Instantiate the B side of the serial pipe */ |
| 84 | b->serial = mctp_serial_init(); |
| 85 | assert(b->serial); |
| 86 | b->ingress = p[1][0]; |
| 87 | b->egress = p[0][1]; |
| 88 | mctp_serial_open_fd(b->serial, b->ingress); |
| 89 | mctp_serial_set_tx_fn(b->serial, mctp_binding_serial_pipe_tx, a); |
| 90 | mctp_register_bus(mctp, mctp_binding_serial_core(b->serial), 9); |
| 91 | |
| 92 | /* Transmit a message from A to B */ |
| 93 | rc = mctp_message_tx(mctp, 9, mctp_msg_src, sizeof(mctp_msg_src)); |
| 94 | assert(rc == 0); |
| 95 | |
| 96 | /* Read the message at B from A */ |
| 97 | mctp_serial_read(b->serial); |
| 98 | |
| 99 | return 0; |
| 100 | } |