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); |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 36 | assert(rc >= 0); |
| 37 | assert((size_t)rc == len); |
Andrew Jeffery | 89a2b8e | 2020-02-07 17:07:38 +1030 | [diff] [blame] | 38 | |
| 39 | return rc; |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | uint8_t mctp_msg_src[2 * MCTP_BTU]; |
| 43 | |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 44 | static bool seen; |
| 45 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 46 | #define __unused __attribute__((unused)) |
| 47 | |
| 48 | static void rx_message(uint8_t eid __unused, void *data __unused, void *msg, |
| 49 | size_t len) |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 50 | { |
| 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 Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 59 | |
| 60 | seen = true; |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 61 | } |
| 62 | |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 63 | struct serial_test { |
| 64 | struct mctp_binding_serial_pipe binding; |
| 65 | struct mctp *mctp; |
| 66 | }; |
| 67 | |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 68 | int main(void) |
| 69 | { |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 70 | struct serial_test scenario[2]; |
| 71 | |
Andrew Jeffery | f0315a3 | 2020-03-10 23:49:41 +1030 | [diff] [blame] | 72 | struct mctp_binding_serial_pipe *a; |
| 73 | struct mctp_binding_serial_pipe *b; |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 74 | 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 Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 88 | /* Instantiate the A side of the serial pipe */ |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 89 | scenario[0].mctp = mctp_init(); |
| 90 | assert(scenario[0].mctp); |
Andrew Jeffery | f0315a3 | 2020-03-10 23:49:41 +1030 | [diff] [blame] | 91 | scenario[0].binding.serial = mctp_serial_init(); |
| 92 | assert(scenario[0].binding.serial); |
| 93 | a = &scenario[0].binding; |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 94 | 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 Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 98 | 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] | 99 | |
| 100 | /* Instantiate the B side of the serial pipe */ |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 101 | scenario[1].mctp = mctp_init(); |
| 102 | assert(scenario[1].mctp); |
| 103 | mctp_set_rx_all(scenario[1].mctp, rx_message, NULL); |
Andrew Jeffery | f0315a3 | 2020-03-10 23:49:41 +1030 | [diff] [blame] | 104 | scenario[1].binding.serial = mctp_serial_init(); |
| 105 | assert(scenario[1].binding.serial); |
| 106 | b = &scenario[1].binding; |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 107 | 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 Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 111 | 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] | 112 | |
| 113 | /* Transmit a message from A to B */ |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 114 | 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] | 115 | assert(rc == 0); |
| 116 | |
| 117 | /* Read the message at B from A */ |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 118 | seen = false; |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 119 | mctp_serial_read(b->serial); |
Andrew Jeffery | b633b3f | 2020-02-25 15:49:22 +1030 | [diff] [blame] | 120 | assert(seen); |
Andrew Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 121 | |
Andrew Jeffery | f0315a3 | 2020-03-10 23:49:41 +1030 | [diff] [blame] | 122 | 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 Jeffery | 25de093 | 2020-02-06 15:56:12 +1030 | [diff] [blame] | 127 | return 0; |
| 128 | } |