-
Notifications
You must be signed in to change notification settings - Fork 0
/
COMPorts0.c
523 lines (491 loc) · 8.85 KB
/
COMPorts0.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
Alexander Shiryaev, 2021.06
*/
#include "COMPorts0.h"
#include <assert.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#define COMPORTS0_IN_QUEUE 8192
#define COMPORTS0_OUT_QUEUE 8192
// #include <winbase.h>
#include <windows.h>
static void EncodeBaud (COMPORTS0_BAUD baud, COMPORTS0_BAUD *res, int *ok)
{
*ok = 1;
switch (baud) {
case 110:
*res = CBR_110;
break;
case 300:
*res = CBR_300;
break;
case 600:
*res = CBR_600;
break;
case 1200:
*res = CBR_1200;
break;
case 2400:
*res = CBR_2400;
break;
case 4800:
*res = CBR_4800;
break;
case 9600:
*res = CBR_9600;
break;
case 14400:
*res = CBR_14400;
break;
case 19200:
*res = CBR_19200;
break;
case 38400:
*res = CBR_38400;
break;
case 56000:
*res = CBR_56000;
break;
case 57600:
*res = CBR_57600;
break;
case 115200:
*res = CBR_115200;
break;
case 128000:
*res = CBR_128000;
break;
case 230400:
*res = 230400;
break;
case 256000:
*res = CBR_256000;
break;
case 460800:
*res = 460800;
break;
case 500000:
*res = 500000;
break;
case 576000:
*res = 576000;
break;
case 921600:
*res = 921600;
break;
case 1000000:
*res = 1000000;
break;
case 1152000:
*res = 1152000;
break;
case 1500000:
*res = 1500000;
break;
case 2000000:
*res = 2000000;
break;
case 2500000:
*res = 2500000;
break;
case 3000000:
*res = 3000000;
break;
case 3500000:
*res = 3500000;
break;
case 4000000:
*res = 4000000;
break;
default:
*ok = 0;
}
}
inline static COMPORTS0_FD CreateFile0 (const char *lpFileName)
{
return (COMPORTS0_FD)CreateFile((LPCTSTR)lpFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, /* FILE_ATTRIBUTE_NORMAL */ 0, NULL);
}
inline static void CloseHandle0 (COMPORTS0_FD hObject)
{
CloseHandle((HANDLE)hObject);
}
static void SetupPort (COMPORTS0_FD fd, COMPORTS0_BAUD baud, int parity, int *res)
{
DCB dcb = {0}; dcb.DCBlength = sizeof(dcb);
if (GetCommState((HANDLE)fd, &dcb) != 0) {
dcb.BaudRate = (DWORD)baud;
dcb.fBinary = 1; dcb.fAbortOnError = 1; dcb.ByteSize = 8; dcb.StopBits = ONESTOPBIT;
switch (parity) {
case COMPORTS0_PARITY_NONE:
dcb.Parity = NOPARITY;
break;
case COMPORTS0_PARITY_ODD:
dcb.Parity = ODDPARITY;
break;
case COMPORTS0_PARITY_EVEN:
dcb.Parity = EVENPARITY;
break;
default:
assert(0);
}
if (SetCommState((HANDLE)fd, &dcb) != 0) {
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = MAXDWORD; /* "non-blocking" read */
if (SetCommTimeouts((HANDLE)fd, &timeouts) != 0) {
if (SetupComm((HANDLE)fd, COMPORTS0_IN_QUEUE, COMPORTS0_OUT_QUEUE) != 0) {
if (SetCommMask((HANDLE)fd, 0) != 0) {
if (PurgeComm((HANDLE)fd, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR) != 0) {
EscapeCommFunction((HANDLE)fd, CLRRTS);
EscapeCommFunction((HANDLE)fd, CLRDTR);
*res = 0;
} else {
*res = 8;
}
} else {
*res = 7;
}
} else {
*res = 6;
}
} else {
*res = 5;
}
} else {
*res = 4;
}
} else {
*res = 3;
}
}
void COMPORTS0_Open (const char *dev, COMPORTS0_BAUD baud, COMPORTS0_Parity parity, COMPORTS0_FD *fd, int *res)
{
COMPORTS0_BAUD encodedBaud;
int ok;
assert((parity == COMPORTS0_PARITY_NONE) || (parity == COMPORTS0_PARITY_ODD) || (parity == COMPORTS0_PARITY_EVEN));
EncodeBaud(baud, &encodedBaud, &ok);
if (ok) {
*fd = CreateFile0(dev);
if (((HANDLE)(*fd)) != INVALID_HANDLE_VALUE) {
SetupPort(*fd, encodedBaud, parity, res);
if (*res != 0) {
CloseHandle0(*fd);
}
} else {
*res = 2; /* CreateFile failed */
}
} else {
*res = 1; /* unexpected baud */
}
}
void COMPORTS0_Close (COMPORTS0_FD fd)
{
CloseHandle0(fd);
}
void COMPORTS0_Write (COMPORTS0_FD fd, const void *adr, int len, int *wr, int *res)
{
DWORD err;
if (WriteFile((HANDLE)fd, (LPCVOID)adr, (DWORD)len, (LPDWORD)wr, NULL) != 0) {
assert(*wr == len);
*res = 0;
} else {
err = GetLastError();
assert(err > 0);
if (err == ERROR_OPERATION_ABORTED) {
*res = -1;
} else {
*res = (int)err;
}
}
}
void COMPORTS0_Read (COMPORTS0_FD fd, void *adr, int len, int *rd, int *res)
{
DWORD err;
if (ReadFile((HANDLE)fd, (LPVOID)adr, (DWORD)len, (LPDWORD)rd, NULL) != 0) {
*res = 0;
} else {
err = GetLastError();
assert(err > 0);
*res = err;
}
}
#else
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
inline static void close0 (COMPORTS0_FD fd)
{
close((int)fd);
}
static void EncodeBaud (COMPORTS0_BAUD baud, COMPORTS0_BAUD *res, int *ok)
{
*ok = 1;
switch (baud) {
case 50:
*res = B50;
break;
case 75:
*res = B75;
break;
case 110:
*res = B110;
break;
case 134:
*res = B134;
break;
case 150:
*res = B150;
break;
case 200:
*res = B200;
break;
case 300:
*res = B300;
break;
case 600:
*res = B600;
break;
case 1200:
*res = B1200;
break;
case 1800:
*res = B1800;
break;
case 2400:
*res = B2400;
break;
case 4800:
*res = B4800;
break;
case 9600:
*res = B9600;
break;
case 19200:
*res = B19200;
break;
case 38400:
*res = B38400;
break;
case 57600:
*res = B57600;
break;
case 115200:
*res = B115200;
break;
case 230400:
*res = B230400;
break;
#ifdef B460800
case 460800:
*res = B460800;
break;
#else
case 460800:
*res = 460800;
break;
#endif
#ifdef B500000
case 500000:
*res = B500000;
break;
#else
case 500000:
*res = 500000;
break;
#endif
#ifdef B576000
case 576000:
*res = B576000;
break;
#else
case 576000:
*res = 576000;
break;
#endif
#ifdef B921600
case 921600:
*res = B921600;
break;
#else
case 921600:
*res = 921600;
break;
#endif
#ifdef B1000000
case 1000000:
*res = B1000000;
break;
#else
case 1000000:
*res = 1000000;
break;
#endif
#ifdef B1152000
case 1152000:
*res = B1152000;
break;
#else
case 1152000:
*res = 1152000;
break;
#endif
#ifdef B1500000
case 1500000:
*res = B1500000;
break;
#else
case 1500000:
*res = 1500000;
break;
#endif
#ifdef B2000000
case 2000000:
*res = B2000000;
break;
#else
case 2000000:
*res = 2000000;
break;
#endif
#ifdef B2500000
case 2500000:
*res = B2500000;
break;
#else
case 2500000:
*res = 2500000;
break;
#endif
#ifdef B3000000
case 3000000:
*res = B3000000;
break;
#else
case 3000000:
*res = 3000000;
break;
#endif
#ifdef B3500000
case 3500000:
*res = B3500000;
break;
#else
case 3500000:
*res = 3500000;
break;
#endif
#ifdef B4000000
case 4000000:
*res = B4000000;
break;
#else
case 4000000:
*res = 4000000;
break;
#endif
default:
*ok = 0;
}
}
inline static COMPORTS0_FD open0 (const char *path)
{
return (COMPORTS0_FD)open(path, O_RDWR | O_NONBLOCK);
}
static void SetupPort (COMPORTS0_FD d, COMPORTS0_BAUD baud, int parity, int *res)
{
struct termios t;
int modemBits;
if (tcgetattr((int)d, &t) == 0) {
t.c_iflag = IGNBRK | IGNPAR; t.c_oflag = 0; t.c_cflag = CS8 | CREAD | CLOCAL; t.c_lflag = 0; t.c_cc[VMIN] = 1; t.c_cc[VTIME] = 0;
if (parity == COMPORTS0_PARITY_ODD) {
t.c_cflag |= PARENB | PARODD;
} else if (parity == COMPORTS0_PARITY_EVEN) {
t.c_cflag |= PARENB;
}
if (cfsetspeed(&t, (speed_t)baud) != -1) {
if (tcsetattr((int)d, TCSANOW, &t) == 0) {
if (ioctl((int)d, TIOCMGET, &modemBits) != -1) {
modemBits &= ~TIOCM_RTS;
if (ioctl((int)d, TIOCMSET, &modemBits) != -1) {
modemBits &= ~TIOCM_DTR;
if (ioctl((int)d, TIOCMSET, &modemBits) != -1) {
*res = 0;
} else {
*res = 8;
}
} else {
*res = 7;
}
} else {
*res = 0;
}
if (*res == 0) {
if (tcflush((int)d, TCIFLUSH) != 0) {
*res = 6;
}
}
} else {
*res = 5;
}
} else {
*res = 4;
}
} else {
*res = 3;
}
}
void COMPORTS0_Open (const char *dev, COMPORTS0_BAUD baud, COMPORTS0_Parity parity, COMPORTS0_FD *fd, int *res)
{
COMPORTS0_BAUD encodedBaud;
int ok;
assert((parity == COMPORTS0_PARITY_NONE) || (parity == COMPORTS0_PARITY_ODD) || (parity == COMPORTS0_PARITY_EVEN));
EncodeBaud(baud, &encodedBaud, &ok);
if (ok) {
*fd = open0(dev);
if (*fd != -1) {
SetupPort(*fd, encodedBaud, parity, res);
if (*res != 0) {
close0(*fd);
}
} else {
*res = 2; /* open failed */
}
} else {
*res = 1; /* unexpected baud */
}
}
void COMPORTS0_Close (COMPORTS0_FD fd)
{
close0(fd);
}
void COMPORTS0_Write (COMPORTS0_FD fd, const void *adr, int len, int *wr, int *res)
{
*wr = (int)write((int)fd, adr, (size_t)len);
if (*wr == -1) {
*wr = 0;
*res = errno;
if (*res == EAGAIN) {
*res = 0;
} else if (*res == 0) {
*res = -1;
}
} else {
*res = 0;
}
}
void COMPORTS0_Read (COMPORTS0_FD fd, void *adr, int len, int *rd, int *res)
{
*rd = (int)read((int)fd, adr, (size_t)len);
if (*rd == -1) {
*rd = 0;
*res = errno;
if (*res == EAGAIN) {
*res = 0;
} else if (*res == 0) {
*res = -1;
}
} else {
*res = 0;
}
}
#endif