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