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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
|
/**
* WinPR: Windows Portable Runtime
* Serial Communication API
*
* Copyright 2011 O.S. Systems Software Ltda.
* Copyright 2011 Eduardo Fiss Beloni <beloni@ossystems.com.br>
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2014 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if defined __linux__ && !defined ANDROID
#include <winpr/assert.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include "comm_serial_sys.h"
#ifdef __UCLIBC__
#include "comm.h"
#endif
#include <winpr/crt.h>
#include <winpr/wlog.h>
/* Undocumented flag, not supported everywhere.
* Provide a sensible fallback to avoid compilation problems. */
#ifndef CMSPAR
#define CMSPAR 010000000000
#endif
/* hard-coded in N_TTY */
#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
#define TTY_THRESHOLD_UNTHROTTLE 128
#define N_TTY_BUF_SIZE 4096
#define BAUD_TABLE_END 0010020 /* __MAX_BAUD + 1 */
/* 0: B* (Linux termios)
* 1: CBR_* or actual baud rate
* 2: BAUD_* (identical to SERIAL_BAUD_*)
*/
static const speed_t _BAUD_TABLE[][3] = {
#ifdef B0
{ B0, 0, 0 }, /* hang up */
#endif
#ifdef B50
{ B50, 50, 0 },
#endif
#ifdef B75
{ B75, 75, BAUD_075 },
#endif
#ifdef B110
{ B110, CBR_110, BAUD_110 },
#endif
#ifdef B134
{ B134, 134, 0 /*BAUD_134_5*/ },
#endif
#ifdef B150
{ B150, 150, BAUD_150 },
#endif
#ifdef B200
{ B200, 200, 0 },
#endif
#ifdef B300
{ B300, CBR_300, BAUD_300 },
#endif
#ifdef B600
{ B600, CBR_600, BAUD_600 },
#endif
#ifdef B1200
{ B1200, CBR_1200, BAUD_1200 },
#endif
#ifdef B1800
{ B1800, 1800, BAUD_1800 },
#endif
#ifdef B2400
{ B2400, CBR_2400, BAUD_2400 },
#endif
#ifdef B4800
{ B4800, CBR_4800, BAUD_4800 },
#endif
/* {, ,BAUD_7200} */
#ifdef B9600
{ B9600, CBR_9600, BAUD_9600 },
#endif
/* {, CBR_14400, BAUD_14400}, /\* unsupported on Linux *\/ */
#ifdef B19200
{ B19200, CBR_19200, BAUD_19200 },
#endif
#ifdef B38400
{ B38400, CBR_38400, BAUD_38400 },
#endif
/* {, CBR_56000, BAUD_56K}, /\* unsupported on Linux *\/ */
#ifdef B57600
{ B57600, CBR_57600, BAUD_57600 },
#endif
#ifdef B115200
{ B115200, CBR_115200, BAUD_115200 },
#endif
/* {, CBR_128000, BAUD_128K}, /\* unsupported on Linux *\/ */
/* {, CBR_256000, BAUD_USER}, /\* unsupported on Linux *\/ */
#ifdef B230400
{ B230400, 230400, BAUD_USER },
#endif
#ifdef B460800
{ B460800, 460800, BAUD_USER },
#endif
#ifdef B500000
{ B500000, 500000, BAUD_USER },
#endif
#ifdef B576000
{ B576000, 576000, BAUD_USER },
#endif
#ifdef B921600
{ B921600, 921600, BAUD_USER },
#endif
#ifdef B1000000
{ B1000000, 1000000, BAUD_USER },
#endif
#ifdef B1152000
{ B1152000, 1152000, BAUD_USER },
#endif
#ifdef B1500000
{ B1500000, 1500000, BAUD_USER },
#endif
#ifdef B2000000
{ B2000000, 2000000, BAUD_USER },
#endif
#ifdef B2500000
{ B2500000, 2500000, BAUD_USER },
#endif
#ifdef B3000000
{ B3000000, 3000000, BAUD_USER },
#endif
#ifdef B3500000
{ B3500000, 3500000, BAUD_USER },
#endif
#ifdef B4000000
{ B4000000, 4000000, BAUD_USER }, /* __MAX_BAUD */
#endif
{ BAUD_TABLE_END, 0, 0 }
};
static BOOL _get_properties(WINPR_COMM* pComm, COMMPROP* pProperties)
{
/* http://msdn.microsoft.com/en-us/library/windows/hardware/jj680684%28v=vs.85%29.aspx
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa363189%28v=vs.85%29.aspx
*/
/* FIXME: properties should be better probed. The current
* implementation just relies on the Linux' implementation.
*/
WINPR_ASSERT(pProperties);
if (pProperties->dwProvSpec1 != COMMPROP_INITIALIZED)
{
ZeroMemory(pProperties, sizeof(COMMPROP));
pProperties->wPacketLength = sizeof(COMMPROP);
}
pProperties->wPacketVersion = 2;
pProperties->dwServiceMask = SERIAL_SP_SERIALCOMM;
/* pProperties->Reserved1; not used */
/* FIXME: could be implemented on top of N_TTY */
pProperties->dwMaxTxQueue = N_TTY_BUF_SIZE;
pProperties->dwMaxRxQueue = N_TTY_BUF_SIZE;
/* FIXME: to be probe on the device? */
pProperties->dwMaxBaud = BAUD_USER;
/* FIXME: what about PST_RS232? see also: serial_struct */
pProperties->dwProvSubType = PST_UNSPECIFIED;
/* TODO: to be finalized */
pProperties->dwProvCapabilities =
/*PCF_16BITMODE |*/ PCF_DTRDSR | PCF_INTTIMEOUTS | PCF_PARITY_CHECK | /*PCF_RLSD |*/
PCF_RTSCTS | PCF_SETXCHAR | /*PCF_SPECIALCHARS |*/ PCF_TOTALTIMEOUTS | PCF_XONXOFF;
/* TODO: double check SP_RLSD */
pProperties->dwSettableParams = SP_BAUD | SP_DATABITS | SP_HANDSHAKING | SP_PARITY |
SP_PARITY_CHECK | /*SP_RLSD |*/ SP_STOPBITS;
pProperties->dwSettableBaud = 0;
for (int i = 0; _BAUD_TABLE[i][0] < BAUD_TABLE_END; i++)
{
pProperties->dwSettableBaud |= _BAUD_TABLE[i][2];
}
pProperties->wSettableData =
DATABITS_5 | DATABITS_6 | DATABITS_7 | DATABITS_8 /*| DATABITS_16 | DATABITS_16X*/;
pProperties->wSettableStopParity = STOPBITS_10 | /*STOPBITS_15 |*/ STOPBITS_20 | PARITY_NONE |
PARITY_ODD | PARITY_EVEN | PARITY_MARK | PARITY_SPACE;
/* FIXME: additional input and output buffers could be implemented on top of N_TTY */
pProperties->dwCurrentTxQueue = N_TTY_BUF_SIZE;
pProperties->dwCurrentRxQueue = N_TTY_BUF_SIZE;
/* pProperties->ProvSpec1; see above */
/* pProperties->ProvSpec2; ignored */
/* pProperties->ProvChar[1]; ignored */
return TRUE;
}
static BOOL _set_baud_rate(WINPR_COMM* pComm, const SERIAL_BAUD_RATE* pBaudRate)
{
speed_t newSpeed = 0;
struct termios futureState;
ZeroMemory(&futureState, sizeof(struct termios));
if (tcgetattr(pComm->fd, &futureState) <
0) /* NB: preserves current settings not directly handled by the Communication Functions */
{
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
for (int i = 0; _BAUD_TABLE[i][0] < BAUD_TABLE_END; i++)
{
if (_BAUD_TABLE[i][1] == pBaudRate->BaudRate)
{
newSpeed = _BAUD_TABLE[i][0];
if (cfsetspeed(&futureState, newSpeed) < 0)
{
CommLog_Print(WLOG_WARN, "failed to set speed 0x%x (%" PRIu32 ")", newSpeed,
pBaudRate->BaudRate);
return FALSE;
}
WINPR_ASSERT(cfgetispeed(&futureState) == newSpeed);
if (_comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &futureState) < 0)
{
CommLog_Print(WLOG_WARN, "_comm_ioctl_tcsetattr failure: last-error: 0x%" PRIX32 "",
GetLastError());
return FALSE;
}
return TRUE;
}
}
CommLog_Print(WLOG_WARN, "could not find a matching speed for the baud rate %" PRIu32 "",
pBaudRate->BaudRate);
SetLastError(ERROR_INVALID_DATA);
return FALSE;
}
static BOOL _get_baud_rate(WINPR_COMM* pComm, SERIAL_BAUD_RATE* pBaudRate)
{
speed_t currentSpeed = 0;
struct termios currentState;
ZeroMemory(¤tState, sizeof(struct termios));
if (tcgetattr(pComm->fd, ¤tState) < 0)
{
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
currentSpeed = cfgetispeed(¤tState);
for (int i = 0; _BAUD_TABLE[i][0] < BAUD_TABLE_END; i++)
{
if (_BAUD_TABLE[i][0] == currentSpeed)
{
pBaudRate->BaudRate = _BAUD_TABLE[i][1];
return TRUE;
}
}
CommLog_Print(WLOG_WARN, "could not find a matching baud rate for the speed 0x%x",
currentSpeed);
SetLastError(ERROR_INVALID_DATA);
return FALSE;
}
/**
* NOTE: Only XonChar and XoffChar are plenty supported with the Linux
* N_TTY line discipline.
*
* ERRORS:
* ERROR_IO_DEVICE
* ERROR_INVALID_PARAMETER when Xon and Xoff chars are the same;
* ERROR_NOT_SUPPORTED
*/
static BOOL _set_serial_chars(WINPR_COMM* pComm, const SERIAL_CHARS* pSerialChars)
{
BOOL result = TRUE;
struct termios upcomingTermios;
ZeroMemory(&upcomingTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, &upcomingTermios) < 0)
{
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
if (pSerialChars->XonChar == pSerialChars->XoffChar)
{
/* https://msdn.microsoft.com/en-us/library/windows/hardware/ff546688?v=vs.85.aspx */
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
/* termios(3): (..) above symbolic subscript values are all
* different, except that VTIME, VMIN may have the same value
* as VEOL, VEOF, respectively. In noncanonical mode the
* special character meaning is replaced by the timeout
* meaning.
*
* EofChar and c_cc[VEOF] are not quite the same, prefer to
* don't use c_cc[VEOF] at all.
*
* FIXME: might be implemented during read/write I/O
*/
if (pSerialChars->EofChar != '\0')
{
CommLog_Print(WLOG_WARN, "EofChar %02" PRIX8 " cannot be set\n", pSerialChars->EofChar);
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
/* According the Linux's n_tty discipline, charaters with a
* parity error can only be let unchanged, replaced by \0 or
* get the prefix the prefix \377 \0
*/
/* FIXME: see also: _set_handflow() */
if (pSerialChars->ErrorChar != '\0')
{
CommLog_Print(WLOG_WARN, "ErrorChar 0x%02" PRIX8 " ('%c') cannot be set (unsupported).\n",
pSerialChars->ErrorChar, (char)pSerialChars->ErrorChar);
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
/* FIXME: see also: _set_handflow() */
if (pSerialChars->BreakChar != '\0')
{
CommLog_Print(WLOG_WARN, "BreakChar 0x%02" PRIX8 " ('%c') cannot be set (unsupported).\n",
pSerialChars->BreakChar, (char)pSerialChars->BreakChar);
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
if (pSerialChars->EventChar != '\0')
{
pComm->eventChar = pSerialChars->EventChar;
}
upcomingTermios.c_cc[VSTART] = pSerialChars->XonChar;
upcomingTermios.c_cc[VSTOP] = pSerialChars->XoffChar;
if (_comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &upcomingTermios) < 0)
{
CommLog_Print(WLOG_WARN, "_comm_ioctl_tcsetattr failure: last-error: 0x%08" PRIX32 "",
GetLastError());
return FALSE;
}
return result;
}
static BOOL _get_serial_chars(WINPR_COMM* pComm, SERIAL_CHARS* pSerialChars)
{
struct termios currentTermios;
ZeroMemory(¤tTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, ¤tTermios) < 0)
{
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
ZeroMemory(pSerialChars, sizeof(SERIAL_CHARS));
/* EofChar unsupported */
/* ErrorChar unsupported */
/* BreakChar unsupported */
/* FIXME: see also: _set_serial_chars() */
/* EventChar */
pSerialChars->XonChar = currentTermios.c_cc[VSTART];
pSerialChars->XoffChar = currentTermios.c_cc[VSTOP];
return TRUE;
}
static BOOL _set_line_control(WINPR_COMM* pComm, const SERIAL_LINE_CONTROL* pLineControl)
{
BOOL result = TRUE;
struct termios upcomingTermios;
/* http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214%28v=vs.85%29.aspx
*
* The use of 5 data bits with 2 stop bits is an invalid
* combination, as is 6, 7, or 8 data bits with 1.5 stop bits.
*
* FIXME: prefered to let the underlying driver to deal with
* this issue. At least produce a warning message?
*/
ZeroMemory(&upcomingTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, &upcomingTermios) < 0)
{
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
/* FIXME: use of a COMMPROP to validate new settings? */
switch (pLineControl->StopBits)
{
case STOP_BIT_1:
upcomingTermios.c_cflag &= ~CSTOPB;
break;
case STOP_BITS_1_5:
CommLog_Print(WLOG_WARN, "Unsupported one and a half stop bits.");
break;
case STOP_BITS_2:
upcomingTermios.c_cflag |= CSTOPB;
break;
default:
CommLog_Print(WLOG_WARN, "unexpected number of stop bits: %" PRIu8 "\n",
pLineControl->StopBits);
result = FALSE; /* but keep on */
break;
}
switch (pLineControl->Parity)
{
case NO_PARITY:
upcomingTermios.c_cflag &= ~(PARENB | PARODD | CMSPAR);
break;
case ODD_PARITY:
upcomingTermios.c_cflag &= ~CMSPAR;
upcomingTermios.c_cflag |= PARENB | PARODD;
break;
case EVEN_PARITY:
upcomingTermios.c_cflag &= ~(PARODD | CMSPAR);
upcomingTermios.c_cflag |= PARENB;
break;
case MARK_PARITY:
upcomingTermios.c_cflag |= PARENB | PARODD | CMSPAR;
break;
case SPACE_PARITY:
upcomingTermios.c_cflag &= ~PARODD;
upcomingTermios.c_cflag |= PARENB | CMSPAR;
break;
default:
CommLog_Print(WLOG_WARN, "unexpected type of parity: %" PRIu8 "\n",
pLineControl->Parity);
result = FALSE; /* but keep on */
break;
}
switch (pLineControl->WordLength)
{
case 5:
upcomingTermios.c_cflag &= ~CSIZE;
upcomingTermios.c_cflag |= CS5;
break;
case 6:
upcomingTermios.c_cflag &= ~CSIZE;
upcomingTermios.c_cflag |= CS6;
break;
case 7:
upcomingTermios.c_cflag &= ~CSIZE;
upcomingTermios.c_cflag |= CS7;
break;
case 8:
upcomingTermios.c_cflag &= ~CSIZE;
upcomingTermios.c_cflag |= CS8;
break;
default:
CommLog_Print(WLOG_WARN, "unexpected number od data bits per character: %" PRIu8 "\n",
pLineControl->WordLength);
result = FALSE; /* but keep on */
break;
}
if (_comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &upcomingTermios) < 0)
{
CommLog_Print(WLOG_WARN, "_comm_ioctl_tcsetattr failure: last-error: 0x%08" PRIX32 "",
GetLastError());
return FALSE;
}
return result;
}
static BOOL _get_line_control(WINPR_COMM* pComm, SERIAL_LINE_CONTROL* pLineControl)
{
struct termios currentTermios;
ZeroMemory(¤tTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, ¤tTermios) < 0)
{
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
pLineControl->StopBits = (currentTermios.c_cflag & CSTOPB) ? STOP_BITS_2 : STOP_BIT_1;
if (!(currentTermios.c_cflag & PARENB))
{
pLineControl->Parity = NO_PARITY;
}
else if (currentTermios.c_cflag & CMSPAR)
{
pLineControl->Parity = (currentTermios.c_cflag & PARODD) ? MARK_PARITY : SPACE_PARITY;
}
else
{
/* PARENB is set */
pLineControl->Parity = (currentTermios.c_cflag & PARODD) ? ODD_PARITY : EVEN_PARITY;
}
switch (currentTermios.c_cflag & CSIZE)
{
case CS5:
pLineControl->WordLength = 5;
break;
case CS6:
pLineControl->WordLength = 6;
break;
case CS7:
pLineControl->WordLength = 7;
break;
default:
pLineControl->WordLength = 8;
break;
}
return TRUE;
}
static BOOL _set_handflow(WINPR_COMM* pComm, const SERIAL_HANDFLOW* pHandflow)
{
BOOL result = TRUE;
struct termios upcomingTermios;
ZeroMemory(&upcomingTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, &upcomingTermios) < 0)
{
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
/* HUPCL */
/* logical XOR */
if ((!(pHandflow->ControlHandShake & SERIAL_DTR_CONTROL) &&
(pHandflow->FlowReplace & SERIAL_RTS_CONTROL)) ||
((pHandflow->ControlHandShake & SERIAL_DTR_CONTROL) &&
!(pHandflow->FlowReplace & SERIAL_RTS_CONTROL)))
{
CommLog_Print(WLOG_WARN,
"SERIAL_DTR_CONTROL:%s and SERIAL_RTS_CONTROL:%s cannot be different, HUPCL "
"will be set since it is claimed for one of the both lines.",
(pHandflow->ControlHandShake & SERIAL_DTR_CONTROL) ? "ON" : "OFF",
(pHandflow->FlowReplace & SERIAL_RTS_CONTROL) ? "ON" : "OFF");
}
if ((pHandflow->ControlHandShake & SERIAL_DTR_CONTROL) ||
(pHandflow->FlowReplace & SERIAL_RTS_CONTROL))
{
upcomingTermios.c_cflag |= HUPCL;
}
else
{
upcomingTermios.c_cflag &= ~HUPCL;
/* FIXME: is the DTR line also needs to be forced to a disable state according
* SERIAL_DTR_CONTROL? */
/* FIXME: is the RTS line also needs to be forced to a disable state according
* SERIAL_RTS_CONTROL? */
}
/* CRTSCTS */
/* logical XOR */
if ((!(pHandflow->ControlHandShake & SERIAL_CTS_HANDSHAKE) &&
(pHandflow->FlowReplace & SERIAL_RTS_HANDSHAKE)) ||
((pHandflow->ControlHandShake & SERIAL_CTS_HANDSHAKE) &&
!(pHandflow->FlowReplace & SERIAL_RTS_HANDSHAKE)))
{
CommLog_Print(WLOG_WARN,
"SERIAL_CTS_HANDSHAKE:%s and SERIAL_RTS_HANDSHAKE:%s cannot be different, "
"CRTSCTS will be set since it is claimed for one of the both lines.",
(pHandflow->ControlHandShake & SERIAL_CTS_HANDSHAKE) ? "ON" : "OFF",
(pHandflow->FlowReplace & SERIAL_RTS_HANDSHAKE) ? "ON" : "OFF");
}
if ((pHandflow->ControlHandShake & SERIAL_CTS_HANDSHAKE) ||
(pHandflow->FlowReplace & SERIAL_RTS_HANDSHAKE))
{
upcomingTermios.c_cflag |= CRTSCTS;
}
else
{
upcomingTermios.c_cflag &= ~CRTSCTS;
}
/* ControlHandShake */
if (pHandflow->ControlHandShake & SERIAL_DTR_HANDSHAKE)
{
/* DTR/DSR flow control not supported on Linux */
CommLog_Print(WLOG_WARN, "Attempt to use the unsupported SERIAL_DTR_HANDSHAKE feature.");
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
if (pHandflow->ControlHandShake & SERIAL_DSR_HANDSHAKE)
{
/* DTR/DSR flow control not supported on Linux */
CommLog_Print(WLOG_WARN, "Attempt to use the unsupported SERIAL_DSR_HANDSHAKE feature.");
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
if (pHandflow->ControlHandShake & SERIAL_DCD_HANDSHAKE)
{
/* DCD flow control not supported on Linux */
CommLog_Print(WLOG_WARN, "Attempt to use the unsupported SERIAL_DCD_HANDSHAKE feature.");
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
// FIXME: could be implemented during read/write I/O
if (pHandflow->ControlHandShake & SERIAL_DSR_SENSITIVITY)
{
/* DSR line control not supported on Linux */
CommLog_Print(WLOG_WARN, "Attempt to use the unsupported SERIAL_DSR_SENSITIVITY feature.");
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
// FIXME: could be implemented during read/write I/O
if (pHandflow->ControlHandShake & SERIAL_ERROR_ABORT)
{
/* Aborting operations on error not supported on Linux */
CommLog_Print(WLOG_WARN, "Attempt to use the unsupported SERIAL_ERROR_ABORT feature.");
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
/* FlowReplace */
if (pHandflow->FlowReplace & SERIAL_AUTO_TRANSMIT)
{
upcomingTermios.c_iflag |= IXON;
}
else
{
upcomingTermios.c_iflag &= ~IXON;
}
if (pHandflow->FlowReplace & SERIAL_AUTO_RECEIVE)
{
upcomingTermios.c_iflag |= IXOFF;
}
else
{
upcomingTermios.c_iflag &= ~IXOFF;
}
// FIXME: could be implemented during read/write I/O, as of today ErrorChar is necessary '\0'
if (pHandflow->FlowReplace & SERIAL_ERROR_CHAR)
{
/* errors will be replaced by the character '\0'. */
upcomingTermios.c_iflag &= ~IGNPAR;
}
else
{
upcomingTermios.c_iflag |= IGNPAR;
}
if (pHandflow->FlowReplace & SERIAL_NULL_STRIPPING)
{
upcomingTermios.c_iflag |= IGNBRK;
}
else
{
upcomingTermios.c_iflag &= ~IGNBRK;
}
// FIXME: could be implemented during read/write I/O
if (pHandflow->FlowReplace & SERIAL_BREAK_CHAR)
{
CommLog_Print(WLOG_WARN, "Attempt to use the unsupported SERIAL_BREAK_CHAR feature.");
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
// FIXME: could be implemented during read/write I/O
if (pHandflow->FlowReplace & SERIAL_XOFF_CONTINUE)
{
/* not supported on Linux */
CommLog_Print(WLOG_WARN, "Attempt to use the unsupported SERIAL_XOFF_CONTINUE feature.");
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
/* XonLimit */
// FIXME: could be implemented during read/write I/O
if (pHandflow->XonLimit != TTY_THRESHOLD_UNTHROTTLE)
{
CommLog_Print(WLOG_WARN, "Attempt to set XonLimit with an unsupported value: %" PRId32 "",
pHandflow->XonLimit);
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
/* XoffChar */
// FIXME: could be implemented during read/write I/O
if (pHandflow->XoffLimit != TTY_THRESHOLD_THROTTLE)
{
CommLog_Print(WLOG_WARN, "Attempt to set XoffLimit with an unsupported value: %" PRId32 "",
pHandflow->XoffLimit);
SetLastError(ERROR_NOT_SUPPORTED);
result = FALSE; /* but keep on */
}
if (_comm_ioctl_tcsetattr(pComm->fd, TCSANOW, &upcomingTermios) < 0)
{
CommLog_Print(WLOG_WARN, "_comm_ioctl_tcsetattr failure: last-error: 0x%" PRIX32 "",
GetLastError());
return FALSE;
}
return result;
}
static BOOL _get_handflow(WINPR_COMM* pComm, SERIAL_HANDFLOW* pHandflow)
{
struct termios currentTermios;
ZeroMemory(¤tTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, ¤tTermios) < 0)
{
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
/* ControlHandShake */
pHandflow->ControlHandShake = 0;
if (currentTermios.c_cflag & HUPCL)
pHandflow->ControlHandShake |= SERIAL_DTR_CONTROL;
/* SERIAL_DTR_HANDSHAKE unsupported */
if (currentTermios.c_cflag & CRTSCTS)
pHandflow->ControlHandShake |= SERIAL_CTS_HANDSHAKE;
/* SERIAL_DSR_HANDSHAKE unsupported */
/* SERIAL_DCD_HANDSHAKE unsupported */
/* SERIAL_DSR_SENSITIVITY unsupported */
/* SERIAL_ERROR_ABORT unsupported */
/* FlowReplace */
pHandflow->FlowReplace = 0;
if (currentTermios.c_iflag & IXON)
pHandflow->FlowReplace |= SERIAL_AUTO_TRANSMIT;
if (currentTermios.c_iflag & IXOFF)
pHandflow->FlowReplace |= SERIAL_AUTO_RECEIVE;
if (!(currentTermios.c_iflag & IGNPAR))
pHandflow->FlowReplace |= SERIAL_ERROR_CHAR;
if (currentTermios.c_iflag & IGNBRK)
pHandflow->FlowReplace |= SERIAL_NULL_STRIPPING;
/* SERIAL_BREAK_CHAR unsupported */
if (currentTermios.c_cflag & HUPCL)
pHandflow->FlowReplace |= SERIAL_RTS_CONTROL;
if (currentTermios.c_cflag & CRTSCTS)
pHandflow->FlowReplace |= SERIAL_RTS_HANDSHAKE;
/* SERIAL_XOFF_CONTINUE unsupported */
/* XonLimit */
pHandflow->XonLimit = TTY_THRESHOLD_UNTHROTTLE;
/* XoffLimit */
pHandflow->XoffLimit = TTY_THRESHOLD_THROTTLE;
return TRUE;
}
static BOOL _set_timeouts(WINPR_COMM* pComm, const SERIAL_TIMEOUTS* pTimeouts)
{
/* NB: timeouts are applied on system during read/write I/O */
/* http://msdn.microsoft.com/en-us/library/windows/hardware/hh439614%28v=vs.85%29.aspx */
if ((pTimeouts->ReadIntervalTimeout == MAXULONG) &&
(pTimeouts->ReadTotalTimeoutConstant == MAXULONG))
{
CommLog_Print(
WLOG_WARN,
"ReadIntervalTimeout and ReadTotalTimeoutConstant cannot be both set to MAXULONG");
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
pComm->timeouts.ReadIntervalTimeout = pTimeouts->ReadIntervalTimeout;
pComm->timeouts.ReadTotalTimeoutMultiplier = pTimeouts->ReadTotalTimeoutMultiplier;
pComm->timeouts.ReadTotalTimeoutConstant = pTimeouts->ReadTotalTimeoutConstant;
pComm->timeouts.WriteTotalTimeoutMultiplier = pTimeouts->WriteTotalTimeoutMultiplier;
pComm->timeouts.WriteTotalTimeoutConstant = pTimeouts->WriteTotalTimeoutConstant;
CommLog_Print(WLOG_DEBUG, "ReadIntervalTimeout %" PRIu32 "",
pComm->timeouts.ReadIntervalTimeout);
CommLog_Print(WLOG_DEBUG, "ReadTotalTimeoutMultiplier %" PRIu32 "",
pComm->timeouts.ReadTotalTimeoutMultiplier);
CommLog_Print(WLOG_DEBUG, "ReadTotalTimeoutConstant %" PRIu32 "",
pComm->timeouts.ReadTotalTimeoutConstant);
CommLog_Print(WLOG_DEBUG, "WriteTotalTimeoutMultiplier %" PRIu32 "",
pComm->timeouts.WriteTotalTimeoutMultiplier);
CommLog_Print(WLOG_DEBUG, "WriteTotalTimeoutConstant %" PRIu32 "",
pComm->timeouts.WriteTotalTimeoutConstant);
return TRUE;
}
static BOOL _get_timeouts(WINPR_COMM* pComm, SERIAL_TIMEOUTS* pTimeouts)
{
pTimeouts->ReadIntervalTimeout = pComm->timeouts.ReadIntervalTimeout;
pTimeouts->ReadTotalTimeoutMultiplier = pComm->timeouts.ReadTotalTimeoutMultiplier;
pTimeouts->ReadTotalTimeoutConstant = pComm->timeouts.ReadTotalTimeoutConstant;
pTimeouts->WriteTotalTimeoutMultiplier = pComm->timeouts.WriteTotalTimeoutMultiplier;
pTimeouts->WriteTotalTimeoutConstant = pComm->timeouts.WriteTotalTimeoutConstant;
return TRUE;
}
static BOOL _set_lines(WINPR_COMM* pComm, UINT32 lines)
{
if (ioctl(pComm->fd, TIOCMBIS, &lines) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TIOCMBIS ioctl failed, lines=0x%" PRIX32 ", errno=[%d] %s", lines,
errno, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
return TRUE;
}
static BOOL _clear_lines(WINPR_COMM* pComm, UINT32 lines)
{
if (ioctl(pComm->fd, TIOCMBIC, &lines) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TIOCMBIC ioctl failed, lines=0x%" PRIX32 ", errno=[%d] %s", lines,
errno, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
return TRUE;
}
static BOOL _set_dtr(WINPR_COMM* pComm)
{
SERIAL_HANDFLOW handflow;
if (!_get_handflow(pComm, &handflow))
return FALSE;
/* SERIAL_DTR_HANDSHAKE not supported as of today */
WINPR_ASSERT((handflow.ControlHandShake & SERIAL_DTR_HANDSHAKE) == 0);
if (handflow.ControlHandShake & SERIAL_DTR_HANDSHAKE)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
return _set_lines(pComm, TIOCM_DTR);
}
static BOOL _clear_dtr(WINPR_COMM* pComm)
{
SERIAL_HANDFLOW handflow;
if (!_get_handflow(pComm, &handflow))
return FALSE;
/* SERIAL_DTR_HANDSHAKE not supported as of today */
WINPR_ASSERT((handflow.ControlHandShake & SERIAL_DTR_HANDSHAKE) == 0);
if (handflow.ControlHandShake & SERIAL_DTR_HANDSHAKE)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
return _clear_lines(pComm, TIOCM_DTR);
}
static BOOL _set_rts(WINPR_COMM* pComm)
{
SERIAL_HANDFLOW handflow;
if (!_get_handflow(pComm, &handflow))
return FALSE;
if (handflow.FlowReplace & SERIAL_RTS_HANDSHAKE)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
return _set_lines(pComm, TIOCM_RTS);
}
static BOOL _clear_rts(WINPR_COMM* pComm)
{
SERIAL_HANDFLOW handflow;
if (!_get_handflow(pComm, &handflow))
return FALSE;
if (handflow.FlowReplace & SERIAL_RTS_HANDSHAKE)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
return _clear_lines(pComm, TIOCM_RTS);
}
static BOOL _get_modemstatus(WINPR_COMM* pComm, ULONG* pRegister)
{
UINT32 lines = 0;
if (ioctl(pComm->fd, TIOCMGET, &lines) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TIOCMGET ioctl failed, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
ZeroMemory(pRegister, sizeof(ULONG));
/* FIXME: Is the last read of the MSR register available or
* cached somewhere? Not quite sure we need to return the 4
* LSBits anyway. A direct access to the register -- which
* would reset the register -- is likely not expected from
* this function.
*/
/* #define SERIAL_MSR_DCTS 0x01 */
/* #define SERIAL_MSR_DDSR 0x02 */
/* #define SERIAL_MSR_TERI 0x04 */
/* #define SERIAL_MSR_DDCD 0x08 */
if (lines & TIOCM_CTS)
*pRegister |= SERIAL_MSR_CTS;
if (lines & TIOCM_DSR)
*pRegister |= SERIAL_MSR_DSR;
if (lines & TIOCM_RI)
*pRegister |= SERIAL_MSR_RI;
if (lines & TIOCM_CD)
*pRegister |= SERIAL_MSR_DCD;
return TRUE;
}
/* http://msdn.microsoft.com/en-us/library/windows/hardware/hh439605%28v=vs.85%29.aspx */
static const ULONG _SERIAL_SYS_SUPPORTED_EV_MASK =
SERIAL_EV_RXCHAR | SERIAL_EV_RXFLAG | SERIAL_EV_TXEMPTY | SERIAL_EV_CTS | SERIAL_EV_DSR |
SERIAL_EV_RLSD | SERIAL_EV_BREAK | SERIAL_EV_ERR | SERIAL_EV_RING |
/* SERIAL_EV_PERR | */
SERIAL_EV_RX80FULL /*|
SERIAL_EV_EVENT1 |
SERIAL_EV_EVENT2*/
;
static BOOL is_wait_set(WINPR_COMM* pComm)
{
WINPR_ASSERT(pComm);
EnterCriticalSection(&pComm->EventsLock);
const BOOL isWaiting = (pComm->PendingEvents & SERIAL_EV_WINPR_WAITING) != 0;
LeaveCriticalSection(&pComm->EventsLock);
return isWaiting;
}
static BOOL _set_wait_mask(WINPR_COMM* pComm, const ULONG* pWaitMask)
{
ULONG possibleMask = 0;
WINPR_ASSERT(pComm);
WINPR_ASSERT(pWaitMask);
/* Stops pending IOCTL_SERIAL_WAIT_ON_MASK
* http://msdn.microsoft.com/en-us/library/ff546805%28v=vs.85%29.aspx
*/
if (is_wait_set(pComm))
{
/* FIXME: any doubt on reading PendingEvents out of a critical section? */
EnterCriticalSection(&pComm->EventsLock);
pComm->PendingEvents |= SERIAL_EV_WINPR_STOP;
LeaveCriticalSection(&pComm->EventsLock);
/* waiting the end of the pending _wait_on_mask() */
while (is_wait_set(pComm))
Sleep(10); /* 10ms */
}
/* NB: ensure to leave the critical section before to return */
EnterCriticalSection(&pComm->EventsLock);
if (*pWaitMask == 0)
{
/* clearing pending events */
if (ioctl(pComm->fd, TIOCGICOUNT, &(pComm->counters)) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TIOCGICOUNT ioctl failed, errno=[%d] %s.", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
if (pComm->permissive)
{
/* counters could not be reset but keep on */
ZeroMemory(&(pComm->counters), sizeof(struct serial_icounter_struct));
}
else
{
SetLastError(ERROR_IO_DEVICE);
LeaveCriticalSection(&pComm->EventsLock);
return FALSE;
}
}
pComm->PendingEvents = 0;
}
possibleMask = *pWaitMask & _SERIAL_SYS_SUPPORTED_EV_MASK;
if (possibleMask != *pWaitMask)
{
CommLog_Print(WLOG_WARN,
"Not all wait events supported (Serial.sys), requested events= 0x%08" PRIX32
", possible events= 0x%08" PRIX32 "",
*pWaitMask, possibleMask);
/* FIXME: shall we really set the possibleMask and return FALSE? */
pComm->WaitEventMask = possibleMask;
LeaveCriticalSection(&pComm->EventsLock);
return FALSE;
}
pComm->WaitEventMask = possibleMask;
LeaveCriticalSection(&pComm->EventsLock);
return TRUE;
}
static BOOL _get_wait_mask(WINPR_COMM* pComm, ULONG* pWaitMask)
{
*pWaitMask = pComm->WaitEventMask;
return TRUE;
}
static BOOL _set_queue_size(WINPR_COMM* pComm, const SERIAL_QUEUE_SIZE* pQueueSize)
{
if ((pQueueSize->InSize <= N_TTY_BUF_SIZE) && (pQueueSize->OutSize <= N_TTY_BUF_SIZE))
return TRUE; /* nothing to do */
/* FIXME: could be implemented on top of N_TTY */
if (pQueueSize->InSize > N_TTY_BUF_SIZE)
CommLog_Print(WLOG_WARN,
"Requested an incompatible input buffer size: %" PRIu32
", keeping on with a %" PRIu32 " bytes buffer.",
pQueueSize->InSize, N_TTY_BUF_SIZE);
if (pQueueSize->OutSize > N_TTY_BUF_SIZE)
CommLog_Print(WLOG_WARN,
"Requested an incompatible output buffer size: %" PRIu32
", keeping on with a %" PRIu32 " bytes buffer.",
pQueueSize->OutSize, N_TTY_BUF_SIZE);
SetLastError(ERROR_CANCELLED);
return FALSE;
}
static BOOL _purge(WINPR_COMM* pComm, const ULONG* pPurgeMask)
{
if ((*pPurgeMask & ~(SERIAL_PURGE_TXABORT | SERIAL_PURGE_RXABORT | SERIAL_PURGE_TXCLEAR |
SERIAL_PURGE_RXCLEAR)) > 0)
{
CommLog_Print(WLOG_WARN, "Invalid purge mask: 0x%" PRIX32 "\n", *pPurgeMask);
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
/* FIXME: currently relying too much on the fact the server
* sends a single IRP_MJ_WRITE or IRP_MJ_READ at a time
* (taking care though that one IRP_MJ_WRITE and one
* IRP_MJ_READ can be sent simultaneously) */
if (*pPurgeMask & SERIAL_PURGE_TXABORT)
{
/* Purges all write (IRP_MJ_WRITE) requests. */
if (eventfd_write(pComm->fd_write_event, WINPR_PURGE_TXABORT) < 0)
{
if (errno != EAGAIN)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "eventfd_write failed, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
}
WINPR_ASSERT(errno == EAGAIN); /* no reader <=> no pending IRP_MJ_WRITE */
}
}
if (*pPurgeMask & SERIAL_PURGE_RXABORT)
{
/* Purges all read (IRP_MJ_READ) requests. */
if (eventfd_write(pComm->fd_read_event, WINPR_PURGE_RXABORT) < 0)
{
if (errno != EAGAIN)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "eventfd_write failed, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
}
WINPR_ASSERT(errno == EAGAIN); /* no reader <=> no pending IRP_MJ_READ */
}
}
if (*pPurgeMask & SERIAL_PURGE_TXCLEAR)
{
/* Purges the transmit buffer, if one exists. */
if (tcflush(pComm->fd, TCOFLUSH) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "tcflush(TCOFLUSH) failure, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_CANCELLED);
return FALSE;
}
}
if (*pPurgeMask & SERIAL_PURGE_RXCLEAR)
{
/* Purges the receive buffer, if one exists. */
if (tcflush(pComm->fd, TCIFLUSH) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "tcflush(TCIFLUSH) failure, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_CANCELLED);
return FALSE;
}
}
return TRUE;
}
/* NB: _get_commstatus also produces most of the events consumed by _wait_on_mask(). Exceptions:
* - SERIAL_EV_RXFLAG: FIXME: once EventChar supported
*
*/
static BOOL _get_commstatus(WINPR_COMM* pComm, SERIAL_STATUS* pCommstatus)
{
/* http://msdn.microsoft.com/en-us/library/jj673022%28v=vs.85%29.aspx */
struct serial_icounter_struct currentCounters;
/* NB: ensure to leave the critical section before to return */
EnterCriticalSection(&pComm->EventsLock);
ZeroMemory(pCommstatus, sizeof(SERIAL_STATUS));
ZeroMemory(¤tCounters, sizeof(struct serial_icounter_struct));
if (ioctl(pComm->fd, TIOCGICOUNT, ¤tCounters) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TIOCGICOUNT ioctl failed, errno=[%d] %s.", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
CommLog_Print(WLOG_WARN, " could not read counters.");
if (pComm->permissive)
{
/* Errors and events based on counters could not be
* detected but keep on.
*/
ZeroMemory(¤tCounters, sizeof(struct serial_icounter_struct));
}
else
{
SetLastError(ERROR_IO_DEVICE);
LeaveCriticalSection(&pComm->EventsLock);
return FALSE;
}
}
/* NB: preferred below (currentCounters.* != pComm->counters.*) over (currentCounters.* >
* pComm->counters.*) thinking the counters can loop */
/* Errors */
if (currentCounters.buf_overrun != pComm->counters.buf_overrun)
{
pCommstatus->Errors |= SERIAL_ERROR_QUEUEOVERRUN;
}
if (currentCounters.overrun != pComm->counters.overrun)
{
pCommstatus->Errors |= SERIAL_ERROR_OVERRUN;
pComm->PendingEvents |= SERIAL_EV_ERR;
}
if (currentCounters.brk != pComm->counters.brk)
{
pCommstatus->Errors |= SERIAL_ERROR_BREAK;
pComm->PendingEvents |= SERIAL_EV_BREAK;
}
if (currentCounters.parity != pComm->counters.parity)
{
pCommstatus->Errors |= SERIAL_ERROR_PARITY;
pComm->PendingEvents |= SERIAL_EV_ERR;
}
if (currentCounters.frame != pComm->counters.frame)
{
pCommstatus->Errors |= SERIAL_ERROR_FRAMING;
pComm->PendingEvents |= SERIAL_EV_ERR;
}
/* HoldReasons */
/* TODO: SERIAL_TX_WAITING_FOR_CTS */
/* TODO: SERIAL_TX_WAITING_FOR_DSR */
/* TODO: SERIAL_TX_WAITING_FOR_DCD */
/* TODO: SERIAL_TX_WAITING_FOR_XON */
/* TODO: SERIAL_TX_WAITING_ON_BREAK, see LCR's bit 6 */
/* TODO: SERIAL_TX_WAITING_XOFF_SENT */
/* AmountInInQueue */
if (ioctl(pComm->fd, TIOCINQ, &(pCommstatus->AmountInInQueue)) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TIOCINQ ioctl failed, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_IO_DEVICE);
LeaveCriticalSection(&pComm->EventsLock);
return FALSE;
}
/* AmountInOutQueue */
if (ioctl(pComm->fd, TIOCOUTQ, &(pCommstatus->AmountInOutQueue)) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TIOCOUTQ ioctl failed, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_IO_DEVICE);
LeaveCriticalSection(&pComm->EventsLock);
return FALSE;
}
/* BOOLEAN EofReceived; FIXME: once EofChar supported */
/* BOOLEAN WaitForImmediate; TODO: once IOCTL_SERIAL_IMMEDIATE_CHAR fully supported */
/* other events based on counters */
if (currentCounters.rx != pComm->counters.rx)
{
pComm->PendingEvents |= SERIAL_EV_RXFLAG | SERIAL_EV_RXCHAR;
}
if ((currentCounters.tx != pComm->counters.tx) && /* at least a transmission occurred AND ...*/
(pCommstatus->AmountInOutQueue == 0)) /* output bufer is now empty */
{
pComm->PendingEvents |= SERIAL_EV_TXEMPTY;
}
else
{
/* FIXME: "now empty" from the specs is ambiguous, need to track previous completed
* transmission? */
pComm->PendingEvents &= ~SERIAL_EV_TXEMPTY;
}
if (currentCounters.cts != pComm->counters.cts)
{
pComm->PendingEvents |= SERIAL_EV_CTS;
}
if (currentCounters.dsr != pComm->counters.dsr)
{
pComm->PendingEvents |= SERIAL_EV_DSR;
}
if (currentCounters.dcd != pComm->counters.dcd)
{
pComm->PendingEvents |= SERIAL_EV_RLSD;
}
if (currentCounters.rng != pComm->counters.rng)
{
pComm->PendingEvents |= SERIAL_EV_RING;
}
if (pCommstatus->AmountInInQueue > (0.8 * N_TTY_BUF_SIZE))
{
pComm->PendingEvents |= SERIAL_EV_RX80FULL;
}
else
{
/* FIXME: "is 80 percent full" from the specs is ambiguous, need to track when it previously
* * occurred? */
pComm->PendingEvents &= ~SERIAL_EV_RX80FULL;
}
pComm->counters = currentCounters;
LeaveCriticalSection(&pComm->EventsLock);
return TRUE;
}
static BOOL _refresh_PendingEvents(WINPR_COMM* pComm)
{
SERIAL_STATUS serialStatus;
/* NB: also ensures PendingEvents to be up to date */
ZeroMemory(&serialStatus, sizeof(SERIAL_STATUS));
if (!_get_commstatus(pComm, &serialStatus))
{
return FALSE;
}
return TRUE;
}
static void _consume_event(WINPR_COMM* pComm, ULONG* pOutputMask, ULONG event)
{
if ((pComm->WaitEventMask & event) && (pComm->PendingEvents & event))
{
pComm->PendingEvents &= ~event; /* consumed */
*pOutputMask |= event;
}
}
/*
* NB: see also: _set_wait_mask()
*/
static BOOL _wait_on_mask(WINPR_COMM* pComm, ULONG* pOutputMask)
{
WINPR_ASSERT(*pOutputMask == 0);
EnterCriticalSection(&pComm->EventsLock);
pComm->PendingEvents |= SERIAL_EV_WINPR_WAITING;
LeaveCriticalSection(&pComm->EventsLock);
while (TRUE)
{
/* NB: EventsLock also used by _refresh_PendingEvents() */
if (!_refresh_PendingEvents(pComm))
{
EnterCriticalSection(&pComm->EventsLock);
pComm->PendingEvents &= ~SERIAL_EV_WINPR_WAITING;
LeaveCriticalSection(&pComm->EventsLock);
return FALSE;
}
/* NB: ensure to leave the critical section before to return */
EnterCriticalSection(&pComm->EventsLock);
if (pComm->PendingEvents & SERIAL_EV_WINPR_STOP)
{
pComm->PendingEvents &= ~SERIAL_EV_WINPR_STOP;
/* pOutputMask must remain empty but should
* not have been modified.
*
* http://msdn.microsoft.com/en-us/library/ff546805%28v=vs.85%29.aspx
*/
WINPR_ASSERT(*pOutputMask == 0);
pComm->PendingEvents &= ~SERIAL_EV_WINPR_WAITING;
LeaveCriticalSection(&pComm->EventsLock);
return TRUE;
}
_consume_event(pComm, pOutputMask, SERIAL_EV_RXCHAR);
_consume_event(pComm, pOutputMask, SERIAL_EV_RXFLAG);
_consume_event(pComm, pOutputMask, SERIAL_EV_TXEMPTY);
_consume_event(pComm, pOutputMask, SERIAL_EV_CTS);
_consume_event(pComm, pOutputMask, SERIAL_EV_DSR);
_consume_event(pComm, pOutputMask, SERIAL_EV_RLSD);
_consume_event(pComm, pOutputMask, SERIAL_EV_BREAK);
_consume_event(pComm, pOutputMask, SERIAL_EV_ERR);
_consume_event(pComm, pOutputMask, SERIAL_EV_RING);
_consume_event(pComm, pOutputMask, SERIAL_EV_RX80FULL);
LeaveCriticalSection(&pComm->EventsLock);
/* NOTE: PendingEvents can be modified from now on but
* not pOutputMask */
if (*pOutputMask != 0)
{
/* at least an event occurred */
EnterCriticalSection(&pComm->EventsLock);
pComm->PendingEvents &= ~SERIAL_EV_WINPR_WAITING;
LeaveCriticalSection(&pComm->EventsLock);
return TRUE;
}
/* waiting for a modification of PendingEvents.
*
* NOTE: previously used a semaphore but used
* sem_timedwait() anyway. Finally preferred a simpler
* solution with Sleep() without the burden of the
* semaphore initialization and destroying.
*/
Sleep(100); /* 100 ms */
}
}
static BOOL _set_break_on(WINPR_COMM* pComm)
{
if (ioctl(pComm->fd, TIOCSBRK, NULL) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TIOCSBRK ioctl failed, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
return TRUE;
}
static BOOL _set_break_off(WINPR_COMM* pComm)
{
if (ioctl(pComm->fd, TIOCCBRK, NULL) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TIOCSBRK ioctl failed, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
return TRUE;
}
static BOOL _set_xoff(WINPR_COMM* pComm)
{
if (tcflow(pComm->fd, TCIOFF) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TCIOFF failure, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
return TRUE;
}
static BOOL _set_xon(WINPR_COMM* pComm)
{
if (tcflow(pComm->fd, TCION) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TCION failure, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
return TRUE;
}
static BOOL _get_dtrrts(WINPR_COMM* pComm, ULONG* pMask)
{
UINT32 lines = 0;
if (ioctl(pComm->fd, TIOCMGET, &lines) < 0)
{
char ebuffer[256] = { 0 };
CommLog_Print(WLOG_WARN, "TIOCMGET ioctl failed, errno=[%d] %s", errno,
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
SetLastError(ERROR_IO_DEVICE);
return FALSE;
}
*pMask = 0;
if (!(lines & TIOCM_DTR))
*pMask |= SERIAL_DTR_STATE;
if (!(lines & TIOCM_RTS))
*pMask |= SERIAL_RTS_STATE;
return TRUE;
}
static BOOL _config_size(WINPR_COMM* pComm, ULONG* pSize)
{
/* http://msdn.microsoft.com/en-us/library/ff546548%28v=vs.85%29.aspx */
if (!pSize)
return FALSE;
*pSize = 0;
return TRUE;
}
static BOOL _immediate_char(WINPR_COMM* pComm, const UCHAR* pChar)
{
BOOL result = 0;
DWORD nbBytesWritten = -1;
/* FIXME: CommWriteFile uses a critical section, shall it be
* interrupted?
*
* FIXME: see also _get_commstatus()'s WaitForImmediate boolean
*/
result = CommWriteFile(pComm, pChar, 1, &nbBytesWritten, NULL);
WINPR_ASSERT(nbBytesWritten == 1);
return result;
}
static BOOL _reset_device(WINPR_COMM* pComm)
{
/* http://msdn.microsoft.com/en-us/library/dn265347%28v=vs.85%29.aspx */
return TRUE;
}
static SERIAL_DRIVER _SerialSys = {
.id = SerialDriverSerialSys,
.name = _T("Serial.sys"),
.set_baud_rate = _set_baud_rate,
.get_baud_rate = _get_baud_rate,
.get_properties = _get_properties,
.set_serial_chars = _set_serial_chars,
.get_serial_chars = _get_serial_chars,
.set_line_control = _set_line_control,
.get_line_control = _get_line_control,
.set_handflow = _set_handflow,
.get_handflow = _get_handflow,
.set_timeouts = _set_timeouts,
.get_timeouts = _get_timeouts,
.set_dtr = _set_dtr,
.clear_dtr = _clear_dtr,
.set_rts = _set_rts,
.clear_rts = _clear_rts,
.get_modemstatus = _get_modemstatus,
.set_wait_mask = _set_wait_mask,
.get_wait_mask = _get_wait_mask,
.wait_on_mask = _wait_on_mask,
.set_queue_size = _set_queue_size,
.purge = _purge,
.get_commstatus = _get_commstatus,
.set_break_on = _set_break_on,
.set_break_off = _set_break_off,
.set_xoff = _set_xoff,
.set_xon = _set_xon,
.get_dtrrts = _get_dtrrts,
.config_size = _config_size,
.immediate_char = _immediate_char,
.reset_device = _reset_device,
};
SERIAL_DRIVER* SerialSys_s(void)
{
return &_SerialSys;
}
#endif /* __linux__ */
|