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 | |
Andrew Jeffery | 2cda40f | 2020-02-28 15:26:20 +1030 | [diff] [blame] | 5 | #if HAVE_CONFIG_H |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 6 | #include "config.h" |
Andrew Jeffery | 2cda40f | 2020-02-28 15:26:20 +1030 | [diff] [blame] | 7 | #endif |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 8 | |
| 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 Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 18 | #include <stdbool.h> |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | struct mctp_binding_serial_pipe { |
| 24 | int ingress; |
| 25 | int egress; |
| 26 | |
| 27 | struct mctp_binding_serial *serial; |
| 28 | }; |
| 29 | |
| 30 | static 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 Jeffery | 89a2b8e | 2020-02-07 17:07:38 +1030 | [diff] [blame] | 37 | |
| 38 | return rc; |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | uint8_t mctp_msg_src[2 * MCTP_BTU]; |
| 42 | |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 43 | static bool seen; |
| 44 | |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 45 | static 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 Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 55 | |
| 56 | seen = true; |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 57 | } |
| 58 | |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 59 | struct serial_test { |
| 60 | struct mctp_binding_serial_pipe binding; |
| 61 | struct mctp *mctp; |
| 62 | }; |
| 63 | |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 64 | int main(void) |
| 65 | { |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 66 | 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 Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 70 | 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 Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 84 | /* Instantiate the A side of the serial pipe */ |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 85 | scenario[0].mctp = mctp_init(); |
| 86 | assert(scenario[0].mctp); |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 87 | 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 Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 93 | mctp_register_bus(scenario[0].mctp, mctp_binding_serial_core(a->serial), 8); |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 94 | |
| 95 | /* Instantiate the B side of the serial pipe */ |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 96 | scenario[1].mctp = mctp_init(); |
| 97 | assert(scenario[1].mctp); |
| 98 | mctp_set_rx_all(scenario[1].mctp, rx_message, NULL); |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 99 | 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 Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 105 | mctp_register_bus(scenario[1].mctp, mctp_binding_serial_core(b->serial), 9); |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 106 | |
| 107 | /* Transmit a message from A to B */ |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 108 | rc = mctp_message_tx(scenario[0].mctp, 9, mctp_msg_src, sizeof(mctp_msg_src)); |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 109 | assert(rc == 0); |
| 110 | |
| 111 | /* Read the message at B from A */ |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 112 | seen = false; |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 113 | mctp_serial_read(b->serial); |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 114 | assert(seen); |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 115 | |
| 116 | return 0; |
| 117 | } |