Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +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-serial.h" |
| 12 | |
| 13 | static void rx_message(uint8_t eid, void *data, void *msg, size_t len) |
| 14 | { |
| 15 | (void)eid; |
| 16 | (void)data; |
| 17 | write(STDOUT_FILENO, msg, len); |
| 18 | } |
| 19 | |
| 20 | int main(void) |
| 21 | { |
| 22 | struct mctp_binding_serial *serial[2]; |
| 23 | mctp_eid_t eids[] = {8, 9}; |
| 24 | struct pollfd pollfds[3]; |
Jeremy Kerr | 73d8bc8 | 2019-02-08 09:34:20 +0800 | [diff] [blame] | 25 | int rc, n, mctp_fds[2]; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 26 | struct mctp *mctp[2]; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 27 | |
| 28 | mctp[0] = mctp_init(); |
| 29 | mctp[1] = mctp_init(); |
| 30 | |
| 31 | assert(mctp[0] && mctp[1]); |
| 32 | |
| 33 | serial[0] = mctp_serial_init(); |
| 34 | serial[1] = mctp_serial_init(); |
| 35 | |
| 36 | assert(serial[0] && serial[1]); |
| 37 | |
Jeremy Kerr | 73d8bc8 | 2019-02-08 09:34:20 +0800 | [diff] [blame] | 38 | rc = socketpair(AF_UNIX, SOCK_STREAM, 0, mctp_fds); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 39 | if (rc) |
| 40 | err(EXIT_FAILURE, "Can't create sockets"); |
| 41 | |
| 42 | mctp_serial_open_fd(serial[0], mctp_fds[0]); |
| 43 | mctp_serial_open_fd(serial[1], mctp_fds[1]); |
| 44 | |
| 45 | mctp_serial_register_bus(serial[0], mctp[0], eids[0]); |
| 46 | mctp_serial_register_bus(serial[1], mctp[1], eids[1]); |
| 47 | |
| 48 | mctp_set_rx_all(mctp[1], rx_message, NULL); |
| 49 | |
| 50 | pollfds[0].fd = mctp_fds[0]; |
| 51 | pollfds[0].events = POLLIN; |
| 52 | pollfds[1].fd = mctp_fds[1]; |
| 53 | pollfds[1].events = POLLIN; |
| 54 | pollfds[2].fd = STDIN_FILENO; |
| 55 | pollfds[2].events = POLLIN; |
Jeremy Kerr | 73d8bc8 | 2019-02-08 09:34:20 +0800 | [diff] [blame] | 56 | n = 3; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 57 | |
| 58 | for (;;) { |
| 59 | uint8_t buf[1024]; |
| 60 | |
Jeremy Kerr | 73d8bc8 | 2019-02-08 09:34:20 +0800 | [diff] [blame] | 61 | rc = poll(pollfds, n, -1); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 62 | if (rc < 0) |
| 63 | return EXIT_FAILURE; |
| 64 | |
Jeremy Kerr | 73d8bc8 | 2019-02-08 09:34:20 +0800 | [diff] [blame] | 65 | if (pollfds[0].revents) { |
| 66 | rc = mctp_serial_read(serial[0]); |
| 67 | if (rc) |
| 68 | pollfds[0].fd = -1; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 69 | } |
Jeremy Kerr | 73d8bc8 | 2019-02-08 09:34:20 +0800 | [diff] [blame] | 70 | |
| 71 | if (pollfds[1].revents) { |
| 72 | rc = mctp_serial_read(serial[1]); |
| 73 | if (rc) |
| 74 | pollfds[1].fd = -1; |
| 75 | } |
| 76 | |
| 77 | if (n > 2 && pollfds[2].revents) { |
| 78 | rc = read(STDIN_FILENO, buf, sizeof(buf)); |
| 79 | if (rc == 0) { |
| 80 | n = 2; |
| 81 | close(mctp_fds[0]); |
| 82 | pollfds[0].fd = -1; |
| 83 | } else if (rc < 0) { |
| 84 | err(EXIT_FAILURE, "read"); |
| 85 | } else { |
| 86 | mctp_message_tx(mctp[0], eids[1], buf, rc); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (n == 2 && pollfds[0].fd < 0 && pollfds[1].fd < 0) |
| 91 | break; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return EXIT_SUCCESS; |
| 95 | |
| 96 | } |