-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
249 lines (188 loc) · 4.83 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <poll.h>
#include "emdb.h"
#include "log.h"
#include "slcan.h"
#include "isotp.h"
struct _chan {
const char *name;
int fd;
char inbuf[32];
size_t inbuf_pos;
size_t inbuf_size;
char outbuf[32];
size_t outbuf_pos;
size_t outbuf_size;
slcan_state_t slcan;
isotp_state_t isotp;
};
static int
_isotp_recv_hook(isotp_state_t *t, uint8_t *buf, size_t len) {
LOG_INFO("isotp %s completed message received: %s", t->name, _make_printable(buf, len));
}
static int
_isotp_xmit_hook(isotp_state_t *t, uint8_t ext, uint32_t can_id, uint8_t *buf, size_t len)
{
struct _chan *chan = (struct _chan *)t->data;
return slcan_recv_data_frame(&chan->slcan, ext, can_id, buf, len);
}
static int
_slcan_xmit_hook(slcan_state_t *s, uint8_t ext, uint32_t id, uint8_t *buf, size_t len)
{
struct _chan *chan = (struct _chan *)s->data;
LOG_INFO("chan %s request to xmit (%d ext) (0x%X id) (%d bytes): %s", chan->name, ext, id, len, _make_printable(buf, len));
if (id == chan->isotp.src_can_id) {
isotp_ingest_frame(&chan->isotp, buf, len);
}
return 0;
}
static int
_slcan_resp_hook(slcan_state_t *s, const char *msg)
{
struct _chan *chan = (struct _chan *)s->data;
size_t msglen = strlen(msg);
int ret;
LOG_DEBUG("chan %s writing out: %s", chan->name, _make_printable(msg, strlen(msg)));
ret = write(chan->fd, msg, msglen);
if (ret != msglen) {
LOG_ERROR("chan %s failed to write %d bytes: %s", chan->name, msglen, strerror(errno));
return -1;
}
return msglen;
}
int
chan_ingest(struct _chan *chan)
{
if (chan->inbuf_pos < 1) {
LOG_WARN("got called with an empty buffer. why?");
return 0;
}
LOG_INFO("channel %s ingesting (%d bytes): %s", chan->name, chan->inbuf_pos, _make_printable(chan->inbuf, chan->inbuf_pos));
int found = -1;
// See if we have a CR.
for (int i = 0; i < chan->inbuf_pos; i++) {
#if 0
// XXX - Do this for testing only.
if (chan->inbuf[i] == '\n') {
chan->inbuf[i] = '\r';
}
#endif
if (chan->inbuf[i] == '\r') {
found = i;
break;
}
}
if (found > -1) {
found += 1;
LOG_INFO("channel %s found command (%d bytes): %s", chan->name, found, _make_printable(chan->inbuf, found));
slcan_handle_cmd(&(chan->slcan), chan->inbuf, found);
memmove(chan->inbuf + found, chan->inbuf, chan->inbuf_pos - found);
chan->inbuf_pos = 0;
}
return 0;
}
int
mainloop(struct _chan *chan, size_t nchan)
{
int done = 0;
int ret;
int nfds = 1;
struct pollfd fds[nchan];
for (int i = 0; i < nchan; i++) {
fds[i].fd = chan[i].fd;
fds[i].events = POLLIN;
fds[i].revents = 0;
}
while (!done) {
ret = poll(fds, nfds, 500);
if (ret == 0) {
continue;
}
if (ret == -1) {
LOG_ERROR("poll failed: %s", strerror(errno));
}
for (int i = 0; i < nchan; i++) {
if (fds[i].revents & POLLIN) {
if ((chan[i].inbuf_size - chan[i].inbuf_pos) < 0) {
LOG_WARN("channel %s has no free buffer for reading", chan[i].name);
continue;
}
ret = read(fds[i].fd, chan[i].inbuf + chan[i].inbuf_pos, chan[i].inbuf_size - chan[i].inbuf_pos);
if (ret == -1 ) {
LOG_ERROR("failed to read channel %s: %s", chan[i].name, strerror(errno));
return -1;
}
chan[i].inbuf_pos += ret;
ret = chan_ingest(&chan[i]);
if (ret < 0) {
return -1;
}
}
}
}
}
int
usage(const char *progname)
{
printf("%s serialPort0 [.. serialPortN]\n", progname);
return -1;
}
int
main(int argc, const char *argv[])
{
int ret = 0;
if (argc < 2) {
return usage(argv[0]);
}
int nchans = argc - 1;
struct _chan chans[nchans];
memset(chans, 0, sizeof(chans));
// Open the PTY/serial port.
for (int i = 0; i < nchans; i++) {
struct _chan *chan;
chan = &chans[i];
const char *fname = argv[i + 1];
int iofd = open(fname, O_RDWR | O_NONBLOCK);
LOG_DEBUG("opening %s...", fname);
if (iofd < 0) {
LOG_ERROR("failed to open I/O port '%s': %s", fname, strerror(errno));
ret = -1;
goto bail;
}
chan->name = fname;
chan->fd = iofd;
chan->outbuf_size = sizeof(chan->outbuf);
chan->outbuf_pos = 0;
chan->inbuf_size = sizeof(chan->inbuf);
chan->inbuf_pos = 0;
slcan_init(&(chan->slcan));
chan->slcan.data = (void *)chan;
chan->slcan.resp_hook = _slcan_resp_hook;
chan->slcan.xmit_hook = _slcan_xmit_hook;
// Start with it open, since Linux will assume we are.
chan->slcan.is_open = 1;
isotp_init(&(chan->isotp));
chan->isotp.name = "0";
chan->isotp.data = (void *)chan;
chan->isotp.src_can_id = 0x12345678;
chan->isotp.dest_can_id = 0x12345679;
chan->isotp.xmit_hook = _isotp_xmit_hook;
chan->isotp.recv_hook = _isotp_recv_hook;
}
mainloop(chans, nchans);
bail:
for (int i = 0; i < nchans; i++) {
if (chans[i].fd != 0) {
close(chans[i].fd);
}
}
return ret;
}