blob: 35dc577862ebd3af1a199a620db4621cbf700b23 [file] [log] [blame]
Jeremy Kerr4cdc2002019-02-07 16:49:12 +08001
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
13static 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
20int main(void)
21{
22 struct mctp_binding_serial *serial[2];
23 mctp_eid_t eids[] = {8, 9};
24 struct pollfd pollfds[3];
Jeremy Kerr73d8bc82019-02-08 09:34:20 +080025 int rc, n, mctp_fds[2];
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080026 struct mctp *mctp[2];
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080027
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 Kerr73d8bc82019-02-08 09:34:20 +080038 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, mctp_fds);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080039 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 Kerr73d8bc82019-02-08 09:34:20 +080056 n = 3;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080057
58 for (;;) {
59 uint8_t buf[1024];
60
Jeremy Kerr73d8bc82019-02-08 09:34:20 +080061 rc = poll(pollfds, n, -1);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080062 if (rc < 0)
63 return EXIT_FAILURE;
64
Jeremy Kerr73d8bc82019-02-08 09:34:20 +080065 if (pollfds[0].revents) {
66 rc = mctp_serial_read(serial[0]);
67 if (rc)
68 pollfds[0].fd = -1;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080069 }
Jeremy Kerr73d8bc82019-02-08 09:34:20 +080070
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 Kerr4cdc2002019-02-07 16:49:12 +080092 }
93
94 return EXIT_SUCCESS;
95
96}