blob: 3852bd485f75a643e7b7b03510f78971b93f1bb9 [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Jeremy Kerr672c8852019-03-01 12:18:07 +08002
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
14static const mctp_eid_t local_eid = 8;
15static const mctp_eid_t remote_eid = 9;
16
17static const uint8_t echo_req = 1;
18static const uint8_t echo_resp = 2;
19
20struct ctx {
21 struct mctp *mctp;
22};
23
24static 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",
Sumanth Bhatf39c3852022-01-10 17:04:10 +053031 eid, len, type);
32 mctp_message_tx(ctx->mctp, eid, 0, MCTP_MESSAGE_TO_SRC, msg, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +080033}
34
Sumanth Bhatf39c3852022-01-10 17:04:10 +053035static void rx_message(uint8_t eid, uint8_t msg_tag, bool tag_owner, void *data,
36 void *msg, size_t len)
Jeremy Kerr672c8852019-03-01 12:18:07 +080037{
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
52int 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
Andrew Jefferyd98b2b22020-06-15 12:24:11 +093062 astlpc = mctp_astlpc_init_fileio();
Jeremy Kerr672c8852019-03-01 12:18:07 +080063 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}