-
Notifications
You must be signed in to change notification settings - Fork 127
/
gbser_posix.cc
473 lines (395 loc) · 11.5 KB
/
gbser_posix.cc
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
Serial interface for POSIX tty handling.
Copyright (C) 2006 Robert Lipe, [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "defs.h"
#include "gbser.h"
#include "gbser_private.h"
#include <cassert>
#include <cerrno>
#include <cstdarg>
#include <cstdio>
#include <fcntl.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
struct gbser_handle {
struct termios old_tio;
struct termios new_tio;
int fd;
unsigned vmin, vtime;
unsigned long magic;
unsigned char inbuf[BUFSIZE];
unsigned inbuf_used;
};
/* Wrapper to safely cast a void * into a gbser_handle */
static gbser_handle* gbser_get_handle(void* p)
{
auto* h = (gbser_handle*) p;
assert(h->magic == MYMAGIC);
return h;
}
speed_t mkspeed(unsigned br)
{
switch (br) {
case 1200:
return B1200;
case 2400:
return B2400;
case 4800:
return B4800;
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
#if defined B57600
case 57600:
return B57600;
#endif
#if defined B115200
case 115200:
return B115200;
#endif
#if defined B230400
case 230400:
return B230400;
#endif
default:
gbFatal("Unsupported serial speed: %d\n", br);
return 0; /* keep compiler happy */
}
}
using hp_time = struct timeval;
static void get_time(hp_time* tv)
{
gettimeofday(tv, nullptr);
}
static double elapsed(hp_time* tv)
{
hp_time now;
double ot = (double) tv->tv_sec * 1000 +
(double) tv->tv_usec / 1000;
double nt;
gettimeofday(&now, nullptr);
nt = (double) now.tv_sec * 1000 +
(double) now.tv_usec / 1000;
/*printf("elapsed -> %f\n", nt - ot);*/
return nt - ot;
}
static int set_rx_timeout(gbser_handle* h, unsigned vmin, unsigned vtime)
{
if (vmin > 255) {
vmin = 255;
}
if (vtime > 255) {
vtime = 255;
}
if (vmin != h->vmin || vtime != h->vtime) {
h->vmin = h->new_tio.c_cc[VMIN] = vmin;
h->vtime = h->new_tio.c_cc[VTIME] = vtime;
/*printf("VMIN=%d, VTIME=%d\n", h->vmin, h->vtime);*/
return tcsetattr(h->fd, TCSANOW, &h->new_tio) ? gbser_ERROR : gbser_OK;
} else {
return 0;
}
}
/* Open a serial port. |port_name| is the (platform specific) name
* of the serial device to open. Under WIN32 familiar DOS port names
* ('com1:') are translated into the equivalent name required by
* WIN32
*/
void* gbser_init(const char* port_name)
{
gbser_handle* h;
gbser_db(4, "gbser_init(\"%s\")\n", port_name);
h = (gbser_handle*) xcalloc(sizeof *h, 1);
h->magic = MYMAGIC;
h->vmin = h->vtime = 0;
if (0 == strcmp(port_name, "-")) {
h->fd = 0;
return h;
} else if (h->fd = open(port_name, O_RDWR | O_NOCTTY), h->fd == -1) {
gbWarning("Failed to open port (%s)\n", strerror(errno));
goto failed;
}
if (!isatty(h->fd)) {
gbWarning("%s is not a TTY\n", port_name);
goto failed;
}
if (gbser_set_port(h, 4800, 8, 0, 1)) {
gbWarning("gbser_set_port() failed\n");
goto failed;
}
return h;
failed:
if (h->fd != -1) {
close(h->fd);
}
xfree(h);
return nullptr;
}
/* Close a serial port
*/
void gbser_deinit(void* handle)
{
gbser_handle* h = gbser_get_handle(handle);
tcsetattr(h->fd, TCSAFLUSH, &h->old_tio);
close(h->fd);
xfree(h);
}
int gbser_set_port(void* handle, unsigned speed, unsigned bits, unsigned parity, unsigned stop)
{
gbser_handle* h = gbser_get_handle(handle);
speed_t s;
static const unsigned bit_flags[] = {
0, 0, 0, 0, 0, CS5, CS6, CS7, CS8
};
if (bits < 5 || bits > 8) {
gbFatal("Unsupported bits setting: %d\n", bits);
}
if (parity > 2) {
gbFatal("Unsupported parity setting: %d\n", parity);
}
if (stop < 1 || stop > 2) {
gbFatal("Unsupported stop setting: %d\n", stop);
}
s = mkspeed(speed);
/* TODO: We don't /fully/ initialise the port's stat here... */
tcgetattr(h->fd, &h->old_tio);
h->new_tio = h->old_tio;
/* clear bits */
// cfmakeraw(&h->new_tio);
h->new_tio.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
|INLCR|IGNCR|ICRNL|IXON);
h->new_tio.c_oflag &= ~OPOST;
h->new_tio.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
h->new_tio.c_cflag &= ~(CSIZE|PARENB);
h->new_tio.c_cflag |= CS8;
h->new_tio.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |
INLCR | IGNCR | IXON);
h->new_tio.c_cflag &= ~(CSIZE | PARENB | PARODD | CSTOPB);
/* set data bits, */
h->new_tio.c_cflag |= bit_flags[bits];
/* stop bits and... */
if (stop == 2) {
h->new_tio.c_cflag |= CSTOPB;
}
/* parity */
if (parity != 0) {
h->new_tio.c_cflag |= PARENB;
if (parity == 1) {
h->new_tio.c_cflag |= PARODD;
}
}
h->new_tio.c_oflag = 0;
h->new_tio.c_lflag = 0;
h->new_tio.c_cc[VMIN] = h->vmin;
h->new_tio.c_cc[VTIME] = h->vtime;
cfsetospeed(&h->new_tio, s);
cfsetispeed(&h->new_tio, s);
return tcsetattr(h->fd, TCSADRAIN, &h->new_tio) ? gbser_ERROR : gbser_OK;
}
unsigned gbser_read_buffer(void* handle, void** buf, unsigned* len)
{
gbser_handle* h = gbser_get_handle(handle);
unsigned count = *len;
auto* cp = (unsigned char*) *buf;
if (count > h->inbuf_used) {
count = h->inbuf_used;
}
memcpy(cp, h->inbuf, count);
memmove(h->inbuf, h->inbuf + count,
h->inbuf_used - count);
h->inbuf_used -= count;
*len -= count;
cp += count;
*buf = (void*) cp;
return count;
}
/* Return when the input buffer contains at least |want| bytes or |*ms|
* milliseconds have elapsed. |ms| may be NULL or |*ms| may be zero to
* poll the port for available bytes and return immediately. |*ms| will
* be updated to indicate the remaining time on exit.
* Returns the number of bytes available (>=0) or an error code (<0).
*/
int gbser_fill_buffer(void* handle, unsigned want, unsigned* ms)
{
int rc;
gbser_handle* h = gbser_get_handle(handle);
if (want > BUFSIZE) {
want = BUFSIZE;
}
/* Already got enough bytes? */
if (h->inbuf_used >= want) {
return h->inbuf_used;
}
if (nullptr == ms || 0 == *ms) {
if ((rc = set_rx_timeout(h, 0, 0), rc < 0) ||
(rc = read(h->fd, h->inbuf + h->inbuf_used,
want - h->inbuf_used), rc < 0)) {
return gbser_ERROR;
}
h->inbuf_used += rc;
/*printf("Got %d bytes\n", rc);*/
} else {
double time_left = *ms;
hp_time tv;
get_time(&tv);
for (;;) {
fd_set rec;
struct timeval t;
time_left = *ms - elapsed(&tv);
if (time_left <= 0 || h->inbuf_used >= want) {
break;
}
FD_ZERO(&rec);
FD_SET(h->fd, &rec);
t.tv_sec = (time_t) time_left / 1000;
t.tv_usec = ((unsigned) time_left % 1000) * 1000;
if (select(h->fd + 1, &rec, nullptr, nullptr, &t) < 0) {
return gbser_ERROR;
}
time_left = *ms - elapsed(&tv);
if (FD_ISSET(h->fd, &rec)) {
#if 0
// See below comment.
unsigned vmin = 0, vtime = 0;
if (time_left >= 100) {
vmin = want - h->inbuf_used;
vtime = (unsigned) time_left / 100;
}
#endif
// The commented out call to set_rx_timeout here is totally
// legal by POSIX standards but does result in a flurry of
// of tcsetattrs that slightly tweak VMIN/VTIME while there
// is incoming data. This has been shown to trigger driver
// bugs in the Prolific drivers for Mac and in certain Linux
// kernels, thought the latter has since been fixed.
// So although removing this means that the timeout behaviour
// is actually different on POSIX and WIN32, it triggers
// fewer buts this way. 2/12/2008 RJL
if (/* (rc = set_rx_timeout(h, vmin, vtime), rc < 0) || */
(rc = read(h->fd, h->inbuf + h->inbuf_used,
want - h->inbuf_used), rc < 0)) {
return gbser_ERROR;
}
h->inbuf_used += rc;
/*printf("Got %d bytes\n", rc);*/
}
}
*ms = (time_left < 0) ? 0 : time_left;
}
return h->inbuf_used;
}
/* Discard any pending input on the serial port.
*/
int gbser_flush(void* handle)
{
gbser_handle* h = gbser_get_handle(handle);
h->inbuf_used = 0;
if (tcflush(h->fd, TCIFLUSH)) {
return gbser_ERROR;
}
return gbser_OK;
}
/* Write |len| bytes from |buf| to the serial port.
*/
int gbser_write(void* handle, const void* buf, unsigned len)
{
gbser_handle* h = gbser_get_handle(handle);
const char* bp = (const char*) buf;
int rc;
while (len > 0) {
/*printf("write(%d, %p, %d)\n", h->fd, bp, len);*/
if (rc = write(h->fd, bp, len), rc < 0) {
printf("rc = %d, errno = %d (%s)\n", rc, errno, strerror(errno));
return gbser_ERROR;
}
len -= rc;
bp += rc;
}
return gbser_OK;
}
/* Return true if a port name seems to refer to a serial port.
* On Windows this tests the filename (against the regex
* /^(\\\\\.\\\\)?com\d+:?$/i). On Posix it returns the value of
* isatty()
*/
int gbser_is_serial(const char* port_name)
{
int fd;
int is_port = 0;
if (fd = open(port_name, O_RDWR | O_NOCTTY), fd == -1) {
gbser_db(1, "Failed to open port (%s) to check its type\n", strerror(errno));
return 0;
}
is_port = isatty(fd);
close(fd);
return is_port;
}
/* This isn't part of the above abstraction; it's just a helper for
* the other serial modules in the tree.
*
* Windows does a weird thing with serial ports.
* COM ports 1 - 9 are "COM1:" through "COM9:"
* The one after that is \\.\\com10 - this function tries to plaster over
* that.
* It returns a pointer to a statically allocated buffer and is therefore not
* thread safe. The buffer pointed to remains valid only until the next
* call to this function.
*/
const char* fix_win_serial_name_r(const char* comname, char* obuf, size_t len)
{
strncpy(obuf, comname, len);
return obuf;
}
static char gb_com_buffer[100];
const char* fix_win_serial_name(const char* comname)
{
return fix_win_serial_name_r(comname, gb_com_buffer, sizeof(gb_com_buffer));
}
/* Read from the serial port until the specified |eol| character is
* found. Any character matching |discard| will be discarded. To
* read lines terminated by 0x0A, 0x0D discarding linefeeds use
* gbser_read_line(h, buf, len, 1000, 0x0D, 0x0A);
* The terminating character and any discarded characters are not
* stored in the buffer.
*/
int gbser_read_line(void* handle, void* buf, unsigned len, unsigned ms, int eol, int discard)
{
char* bp = (char*) buf;
unsigned pos = 0;
hp_time tv;
get_time(&tv);
bp[pos] = '\0';
for (;;) {
signed time_left = ms - elapsed(&tv);
int c;
if (time_left <= 0) {
return gbser_TIMEOUT;
}
c = gbser_readc_wait(handle, time_left);
if (c == gbser_ERROR) {
return c;
} else if (c == eol) {
return gbser_OK;
}
if (c != gbser_NOTHING && c != discard && pos < len - 1) {
bp[pos++] = c;
bp[pos] = '\0';
}
}
}