Jeremy Kerr | 3d36ee2 | 2019-05-30 11:15:37 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 2 | |
| 3 | #include <assert.h> |
| 4 | #include <err.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <sys/poll.h> |
| 9 | #include <sys/socket.h> |
| 10 | |
| 11 | #include "libmctp.h" |
| 12 | #include "libmctp-astlpc.h" |
| 13 | |
| 14 | static const mctp_eid_t local_eid = 8; |
| 15 | static const mctp_eid_t remote_eid = 9; |
| 16 | |
| 17 | static const uint8_t echo_req = 1; |
| 18 | static const uint8_t echo_resp = 2; |
| 19 | |
| 20 | struct ctx { |
| 21 | struct mctp *mctp; |
| 22 | }; |
| 23 | |
| 24 | static void tx_message(struct ctx *ctx, mctp_eid_t eid, void *msg, size_t len) |
| 25 | { |
| 26 | uint8_t type; |
| 27 | |
| 28 | type = len > 0 ? *(uint8_t *)(msg) : 0x00; |
| 29 | |
| 30 | fprintf(stderr, "TX: dest EID 0x%02x: %zd bytes, first byte [0x%02x]\n", |
| 31 | eid, len, type); |
| 32 | mctp_message_tx(ctx->mctp, eid, msg, len); |
| 33 | } |
| 34 | |
| 35 | static void rx_message(uint8_t eid, void *data, void *msg, size_t len) |
| 36 | { |
| 37 | struct ctx *ctx = data; |
| 38 | uint8_t type; |
| 39 | |
| 40 | type = len > 0 ? *(uint8_t *)(msg) : 0x00; |
| 41 | |
| 42 | fprintf(stderr, "RX: src EID 0x%02x: %zd bytes, first byte [0x%02x]\n", |
| 43 | eid, len, type); |
| 44 | |
| 45 | if (type == echo_req) { |
| 46 | *(uint8_t *)(msg) = echo_resp; |
| 47 | tx_message(ctx, eid, msg, len); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | int main(void) |
| 52 | { |
| 53 | struct mctp_binding_astlpc *astlpc; |
| 54 | struct mctp *mctp; |
| 55 | struct ctx *ctx, _ctx; |
| 56 | int rc; |
| 57 | |
| 58 | mctp = mctp_init(); |
| 59 | assert(mctp); |
| 60 | |
Andrew Jeffery | d98b2b2 | 2020-06-15 12:24:11 +0930 | [diff] [blame] | 61 | astlpc = mctp_astlpc_init_fileio(); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 62 | assert(astlpc); |
| 63 | |
| 64 | mctp_astlpc_register_bus(astlpc, mctp, local_eid); |
| 65 | |
| 66 | ctx = &_ctx; |
| 67 | ctx->mctp = mctp; |
| 68 | |
| 69 | mctp_set_rx_all(mctp, rx_message, ctx); |
| 70 | |
| 71 | for (;;) { |
| 72 | #if 0 |
| 73 | |
| 74 | struct pollfd pollfds[2]; |
| 75 | |
| 76 | pollfds[0].fd = STDIN_FILENO; |
| 77 | pollfds[0].events = POLLIN; |
| 78 | |
| 79 | pollfds[1].fd = mctp_astlpc_get_fd(astlpc); |
| 80 | pollfds[1].events = POLLIN; |
| 81 | |
| 82 | rc = poll(pollfds, 2, -1); |
| 83 | if (rc < 0) |
| 84 | err(EXIT_FAILURE, "poll"); |
| 85 | |
| 86 | if (pollfds[0].revents) { |
| 87 | uint8_t buf[1024]; |
| 88 | rc = read(STDIN_FILENO, buf, sizeof(buf)); |
| 89 | if (rc == 0) |
| 90 | break; |
| 91 | if (rc < 0) |
| 92 | err(EXIT_FAILURE, "read"); |
| 93 | tx_message(ctx, remote_eid, buf, rc); |
| 94 | } |
| 95 | |
| 96 | if (pollfds[1].revents) { |
| 97 | rc = mctp_astlpc_poll(astlpc); |
| 98 | if (rc) |
| 99 | break; |
| 100 | } |
| 101 | #else |
| 102 | (void)remote_eid; |
| 103 | rc = mctp_astlpc_poll(astlpc); |
| 104 | if (rc) |
| 105 | break; |
| 106 | |
| 107 | #endif |
| 108 | |
| 109 | } |
| 110 | |
| 111 | return EXIT_SUCCESS; |
| 112 | |
| 113 | } |