This repository has been archived by the owner on Sep 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
serial.c
356 lines (293 loc) · 8.04 KB
/
serial.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef MCTP_HAVE_FILEIO
#include <unistd.h>
#include <fcntl.h>
#else
static const size_t write(int fd, void *buf, size_t len)
{
return -1;
}
#endif
#define pr_fmt(x) "serial: " x
/* Post-condition: All bytes written or an error has occurred */
#define mctp_write_all(fn, dst, src, len) \
({ \
ssize_t wrote; \
while (len) { \
wrote = fn(dst, src, len); \
if (wrote < 0) \
break; \
len -= wrote; \
} \
len ? -1 : 0; \
})
#include "libmctp.h"
#include "libmctp-alloc.h"
#include "libmctp-log.h"
#include "libmctp-serial.h"
#include "container_of.h"
struct mctp_binding_serial {
struct mctp_binding binding;
int fd;
unsigned long bus_id;
mctp_serial_tx_fn tx_fn;
void *tx_fn_data;
/* receive buffer and state */
uint8_t rxbuf[1024];
struct mctp_pktbuf *rx_pkt;
uint8_t rx_exp_len;
uint16_t rx_fcs;
enum { STATE_WAIT_SYNC_START,
STATE_WAIT_REVISION,
STATE_WAIT_LEN,
STATE_DATA,
STATE_DATA_ESCAPED,
STATE_WAIT_FCS1,
STATE_WAIT_FCS2,
STATE_WAIT_SYNC_END,
} rx_state;
/* temporary transmit buffer */
uint8_t txbuf[256];
};
#define binding_to_serial(b) \
container_of(b, struct mctp_binding_serial, binding)
#define MCTP_SERIAL_REVISION 0x01
#define MCTP_SERIAL_FRAMING_FLAG 0x7e
#define MCTP_SERIAL_ESCAPE 0x7d
struct mctp_serial_header {
uint8_t flag;
uint8_t revision;
uint8_t len;
};
struct mctp_serial_trailer {
uint8_t fcs_msb;
uint8_t fcs_lsb;
uint8_t flag;
};
static size_t mctp_serial_pkt_escape(struct mctp_pktbuf *pkt, uint8_t *buf)
{
uint8_t total_len;
uint8_t *p;
int i, j;
total_len = pkt->end - pkt->mctp_hdr_off;
p = (void *)mctp_pktbuf_hdr(pkt);
for (i = 0, j = 0; i < total_len; i++, j++) {
uint8_t c = p[i];
if (c == 0x7e || c == 0x7d) {
if (buf)
buf[j] = 0x7d;
j++;
c ^= 0x20;
}
if (buf)
buf[j] = c;
}
return j;
}
static int mctp_binding_serial_tx(struct mctp_binding *b,
struct mctp_pktbuf *pkt)
{
struct mctp_binding_serial *serial = binding_to_serial(b);
struct mctp_serial_header *hdr;
struct mctp_serial_trailer *tlr;
uint8_t *buf;
size_t len;
/* the length field in the header excludes serial framing
* and escape sequences */
len = mctp_pktbuf_size(pkt);
hdr = (void *)serial->txbuf;
hdr->flag = MCTP_SERIAL_FRAMING_FLAG;
hdr->revision = MCTP_SERIAL_REVISION;
hdr->len = len;
buf = (void *)(hdr + 1);
len = mctp_serial_pkt_escape(pkt, NULL);
if (len + sizeof(*hdr) + sizeof(*tlr) > sizeof(serial->txbuf))
return -1;
mctp_serial_pkt_escape(pkt, buf);
buf += len;
tlr = (void *)buf;
tlr->flag = MCTP_SERIAL_FRAMING_FLAG;
/* todo: trailer FCS */
tlr->fcs_msb = 0;
tlr->fcs_lsb = 0;
len += sizeof(*hdr) + sizeof(*tlr);
if (!serial->tx_fn)
return mctp_write_all(write, serial->fd, serial->txbuf, len);
return mctp_write_all(serial->tx_fn, serial->tx_fn_data, serial->txbuf,
len);
}
static void mctp_serial_finish_packet(struct mctp_binding_serial *serial,
bool valid)
{
struct mctp_pktbuf *pkt = serial->rx_pkt;
assert(pkt);
if (valid)
mctp_bus_rx(&serial->binding, pkt);
serial->rx_pkt = NULL;
}
static void mctp_serial_start_packet(struct mctp_binding_serial *serial,
uint8_t len)
{
serial->rx_pkt = mctp_pktbuf_alloc(&serial->binding, len);
}
static void mctp_rx_consume_one(struct mctp_binding_serial *serial, uint8_t c)
{
struct mctp_pktbuf *pkt = serial->rx_pkt;
mctp_prdebug("state: %d, char 0x%02x", serial->rx_state, c);
assert(!pkt == (serial->rx_state == STATE_WAIT_SYNC_START ||
serial->rx_state == STATE_WAIT_REVISION ||
serial->rx_state == STATE_WAIT_LEN));
switch (serial->rx_state) {
case STATE_WAIT_SYNC_START:
if (c != MCTP_SERIAL_FRAMING_FLAG) {
mctp_prdebug("lost sync, dropping packet");
if (pkt)
mctp_serial_finish_packet(serial, false);
} else {
serial->rx_state = STATE_WAIT_REVISION;
}
break;
case STATE_WAIT_REVISION:
if (c == MCTP_SERIAL_REVISION) {
serial->rx_state = STATE_WAIT_LEN;
} else {
mctp_prdebug("invalid revision 0x%02x", c);
serial->rx_state = STATE_WAIT_SYNC_START;
}
break;
case STATE_WAIT_LEN:
if (c > serial->binding.pkt_size ||
c < sizeof(struct mctp_hdr)) {
mctp_prdebug("invalid size %d", c);
serial->rx_state = STATE_WAIT_SYNC_START;
} else {
mctp_serial_start_packet(serial, 0);
pkt = serial->rx_pkt;
serial->rx_exp_len = c;
serial->rx_state = STATE_DATA;
}
break;
case STATE_DATA:
if (c == MCTP_SERIAL_ESCAPE) {
serial->rx_state = STATE_DATA_ESCAPED;
} else {
mctp_pktbuf_push(pkt, &c, 1);
if (pkt->end - pkt->mctp_hdr_off == serial->rx_exp_len)
serial->rx_state = STATE_WAIT_FCS1;
}
break;
case STATE_DATA_ESCAPED:
c ^= 0x20;
mctp_pktbuf_push(pkt, &c, 1);
if (pkt->end - pkt->mctp_hdr_off == serial->rx_exp_len)
serial->rx_state = STATE_WAIT_FCS1;
else
serial->rx_state = STATE_DATA;
break;
case STATE_WAIT_FCS1:
serial->rx_fcs = c << 8;
serial->rx_state = STATE_WAIT_FCS2;
break;
case STATE_WAIT_FCS2:
serial->rx_fcs |= c;
/* todo: check fcs */
serial->rx_state = STATE_WAIT_SYNC_END;
break;
case STATE_WAIT_SYNC_END:
if (c == MCTP_SERIAL_FRAMING_FLAG) {
mctp_serial_finish_packet(serial, true);
} else {
mctp_prdebug("missing end frame marker");
mctp_serial_finish_packet(serial, false);
}
serial->rx_state = STATE_WAIT_SYNC_START;
break;
}
mctp_prdebug(" -> state: %d", serial->rx_state);
}
static void mctp_rx_consume(struct mctp_binding_serial *serial, const void *buf,
size_t len)
{
size_t i;
for (i = 0; i < len; i++)
mctp_rx_consume_one(serial, *(uint8_t *)(buf + i));
}
#ifdef MCTP_HAVE_FILEIO
int mctp_serial_read(struct mctp_binding_serial *serial)
{
ssize_t len;
len = read(serial->fd, serial->rxbuf, sizeof(serial->rxbuf));
if (len == 0)
return -1;
if (len < 0) {
mctp_prerr("can't read from serial device: %m");
return -1;
}
mctp_rx_consume(serial, serial->rxbuf, len);
return 0;
}
int mctp_serial_get_fd(struct mctp_binding_serial *serial)
{
return serial->fd;
}
int mctp_serial_open_path(struct mctp_binding_serial *serial,
const char *device)
{
serial->fd = open(device, O_RDWR);
if (serial->fd < 0)
mctp_prerr("can't open device %s: %m", device);
return 0;
}
void mctp_serial_open_fd(struct mctp_binding_serial *serial, int fd)
{
serial->fd = fd;
}
#endif
void mctp_serial_set_tx_fn(struct mctp_binding_serial *serial,
mctp_serial_tx_fn fn, void *data)
{
serial->tx_fn = fn;
serial->tx_fn_data = data;
}
int mctp_serial_rx(struct mctp_binding_serial *serial, const void *buf,
size_t len)
{
mctp_rx_consume(serial, buf, len);
return 0;
}
static int mctp_serial_core_start(struct mctp_binding *binding)
{
mctp_binding_set_tx_enabled(binding, true);
return 0;
}
struct mctp_binding *mctp_binding_serial_core(struct mctp_binding_serial *b)
{
return &b->binding;
}
struct mctp_binding_serial *mctp_serial_init(void)
{
struct mctp_binding_serial *serial;
serial = __mctp_alloc(sizeof(*serial));
memset(serial, 0, sizeof(*serial));
serial->fd = -1;
serial->rx_state = STATE_WAIT_SYNC_START;
serial->rx_pkt = NULL;
serial->binding.name = "serial";
serial->binding.version = 1;
serial->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
serial->binding.pkt_pad = 0;
serial->binding.start = mctp_serial_core_start;
serial->binding.tx = mctp_binding_serial_tx;
return serial;
}
void mctp_serial_destroy(struct mctp_binding_serial *serial)
{
__mctp_free(serial);
}