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 		mctp_astlpc_init_pollfd(astlpc, &pollfds[1]);
81 
82 		rc = poll(pollfds, 2, -1);
83 		if (rc < 0)
84 			err(EXIT_FAILURE, "poll");
85 
86 		if (pollfds[0].revents) {
87 			uint8_t buf[1024];
88 			rc = read(STDIN_FILENO, buf, sizeof(buf));
89 			if (rc == 0)
90 				break;
91 			if (rc < 0)
92 				err(EXIT_FAILURE, "read");
93 			tx_message(ctx, remote_eid, buf, rc);
94 		}
95 
96 		if (pollfds[1].revents) {
97 			rc = mctp_astlpc_poll(astlpc);
98 			if (rc)
99 				break;
100 		}
101 #else
102 		(void)remote_eid;
103 		rc = mctp_astlpc_poll(astlpc);
104 		if (rc)
105 			break;
106 
107 #endif
108 	}
109 
110 	return EXIT_SUCCESS;
111 }
112