| |
| #include <assert.h> |
| #include <err.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <unistd.h> |
| #include <sys/poll.h> |
| #include <sys/socket.h> |
| |
| #include "libmctp.h" |
| #include "libmctp-astlpc.h" |
| |
| static const mctp_eid_t local_eid = 8; |
| static const mctp_eid_t remote_eid = 9; |
| |
| static const uint8_t echo_req = 1; |
| static const uint8_t echo_resp = 2; |
| |
| struct ctx { |
| struct mctp *mctp; |
| }; |
| |
| static void tx_message(struct ctx *ctx, mctp_eid_t eid, void *msg, size_t len) |
| { |
| uint8_t type; |
| |
| type = len > 0 ? *(uint8_t *)(msg) : 0x00; |
| |
| fprintf(stderr, "TX: dest EID 0x%02x: %zd bytes, first byte [0x%02x]\n", |
| eid, len, type); |
| mctp_message_tx(ctx->mctp, eid, msg, len); |
| } |
| |
| static void rx_message(uint8_t eid, void *data, void *msg, size_t len) |
| { |
| struct ctx *ctx = data; |
| uint8_t type; |
| |
| type = len > 0 ? *(uint8_t *)(msg) : 0x00; |
| |
| fprintf(stderr, "RX: src EID 0x%02x: %zd bytes, first byte [0x%02x]\n", |
| eid, len, type); |
| |
| if (type == echo_req) { |
| *(uint8_t *)(msg) = echo_resp; |
| tx_message(ctx, eid, msg, len); |
| } |
| } |
| |
| int main(void) |
| { |
| struct mctp_binding_astlpc *astlpc; |
| struct mctp *mctp; |
| struct ctx *ctx, _ctx; |
| int rc; |
| |
| mctp = mctp_init(); |
| assert(mctp); |
| |
| astlpc = mctp_astlpc_init(); |
| assert(astlpc); |
| |
| mctp_astlpc_register_bus(astlpc, mctp, local_eid); |
| |
| ctx = &_ctx; |
| ctx->mctp = mctp; |
| |
| mctp_set_rx_all(mctp, rx_message, ctx); |
| |
| for (;;) { |
| #if 0 |
| |
| struct pollfd pollfds[2]; |
| |
| pollfds[0].fd = STDIN_FILENO; |
| pollfds[0].events = POLLIN; |
| |
| pollfds[1].fd = mctp_astlpc_get_fd(astlpc); |
| pollfds[1].events = POLLIN; |
| |
| rc = poll(pollfds, 2, -1); |
| if (rc < 0) |
| err(EXIT_FAILURE, "poll"); |
| |
| if (pollfds[0].revents) { |
| uint8_t buf[1024]; |
| rc = read(STDIN_FILENO, buf, sizeof(buf)); |
| if (rc == 0) |
| break; |
| if (rc < 0) |
| err(EXIT_FAILURE, "read"); |
| tx_message(ctx, remote_eid, buf, rc); |
| } |
| |
| if (pollfds[1].revents) { |
| rc = mctp_astlpc_poll(astlpc); |
| if (rc) |
| break; |
| } |
| #else |
| (void)remote_eid; |
| rc = mctp_astlpc_poll(astlpc); |
| if (rc) |
| break; |
| |
| #endif |
| |
| } |
| |
| return EXIT_SUCCESS; |
| |
| } |