1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
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, 0, MCTP_MESSAGE_TO_SRC, msg, len);
33 }
34 
35 static void rx_message(uint8_t eid, uint8_t msg_tag, bool tag_owner, void *data,
36 		       void *msg, size_t len)
37 {
38 	struct ctx *ctx = data;
39 	uint8_t type;
40 
41 	type = len > 0 ? *(uint8_t *)(msg) : 0x00;
42 
43 	fprintf(stderr, "RX: src EID 0x%02x: %zd bytes, first byte [0x%02x]\n",
44 			eid, len, type);
45 
46 	if (type == echo_req) {
47 		*(uint8_t *)(msg) = echo_resp;
48 		tx_message(ctx, eid, msg, len);
49 	}
50 }
51 
52 int main(void)
53 {
54 	struct mctp_binding_astlpc *astlpc;
55 	struct mctp *mctp;
56 	struct ctx *ctx, _ctx;
57 	int rc;
58 
59 	mctp = mctp_init();
60 	assert(mctp);
61 
62 	astlpc = mctp_astlpc_init_fileio();
63 	assert(astlpc);
64 
65 	mctp_astlpc_register_bus(astlpc, mctp, local_eid);
66 
67 	ctx = &_ctx;
68 	ctx->mctp = mctp;
69 
70 	mctp_set_rx_all(mctp, rx_message, ctx);
71 
72 	for (;;) {
73 #if 0
74 
75 		struct pollfd pollfds[2];
76 
77 		pollfds[0].fd = STDIN_FILENO;
78 		pollfds[0].events = POLLIN;
79 
80 		pollfds[1].fd = mctp_astlpc_get_fd(astlpc);
81 		pollfds[1].events = POLLIN;
82 
83 		rc = poll(pollfds, 2, -1);
84 		if (rc < 0)
85 			err(EXIT_FAILURE, "poll");
86 
87 		if (pollfds[0].revents) {
88 			uint8_t buf[1024];
89 			rc = read(STDIN_FILENO, buf, sizeof(buf));
90 			if (rc == 0)
91 				break;
92 			if (rc < 0)
93 				err(EXIT_FAILURE, "read");
94 			tx_message(ctx, remote_eid, buf, rc);
95 		}
96 
97 		if (pollfds[1].revents) {
98 			rc = mctp_astlpc_poll(astlpc);
99 			if (rc)
100 				break;
101 		}
102 #else
103 		(void)remote_eid;
104 		rc = mctp_astlpc_poll(astlpc);
105 		if (rc)
106 			break;
107 
108 #endif
109 
110 	}
111 
112 	return EXIT_SUCCESS;
113 
114 }
115