forked from cbrake/linux-serial-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux-serial-test.c
823 lines (733 loc) · 19.4 KB
/
linux-serial-test.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
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
// SPDX-License-Identifier: MIT
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <poll.h>
#include <unistd.h>
#include <getopt.h>
#include <time.h>
#include <linux/serial.h>
#include <errno.h>
/*
* glibc for MIPS has its own bits/termios.h which does not define
* CMSPAR, so we vampirise the value from the generic bits/termios.h
*/
#ifndef CMSPAR
#define CMSPAR 010000000000
#endif
/*
* Define modem line bits
*/
#ifndef TIOCM_LOOP
#define TIOCM_LOOP 0x8000
#endif
// command line args
int _cl_baud = 0;
char *_cl_port = NULL;
int _cl_divisor = 0;
int _cl_rx_dump = 0;
int _cl_rx_dump_ascii = 0;
int _cl_tx_detailed = 0;
int _cl_stats = 0;
int _cl_stop_on_error = 0;
int _cl_single_byte = -1;
int _cl_another_byte = -1;
int _cl_rts_cts = 0;
int _cl_2_stop_bit = 0;
int _cl_parity = 0;
int _cl_odd_parity = 0;
int _cl_stick_parity = 0;
int _cl_loopback = 0;
int _cl_dump_err = 0;
int _cl_no_rx = 0;
int _cl_no_tx = 0;
int _cl_rx_delay = 0;
int _cl_tx_delay = 0;
int _cl_tx_bytes = 0;
int _cl_rs485_after_delay = -1;
int _cl_rs485_before_delay = 0;
int _cl_rs485_rts_after_send = 0;
int _cl_tx_time = 0;
int _cl_rx_time = 0;
int _cl_ascii_range = 0;
int _cl_write_after_read = 0;
// Module variables
unsigned char _write_count_value = 0;
unsigned char _read_count_value = 0;
int _fd = -1;
unsigned char * _write_data;
ssize_t _write_size;
// keep our own counts for cases where the driver stats don't work
long long int _write_count = 0;
long long int _read_count = 0;
long long int _error_count = 0;
static void dump_data(unsigned char * b, int count)
{
printf("%i bytes: ", count);
int i;
for (i=0; i < count; i++) {
printf("%02x ", b[i]);
}
printf("\n");
}
static void dump_data_ascii(unsigned char * b, int count)
{
int i;
for (i=0; i < count; i++) {
printf("%c", b[i]);
}
}
static void set_baud_divisor(int speed, int custom_divisor)
{
// default baud was not found, so try to set a custom divisor
struct serial_struct ss;
int ret;
ret = ioctl(_fd, TIOCGSERIAL, &ss);
if (ret < 0) {
perror("TIOCGSERIAL failed");
exit(ret);
}
ss.flags = (ss.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST;
if (custom_divisor) {
ss.custom_divisor = custom_divisor;
} else {
ss.custom_divisor = (ss.baud_base + (speed/2)) / speed;
int closest_speed = ss.baud_base / ss.custom_divisor;
if (closest_speed < speed * 98 / 100 || closest_speed > speed * 102 / 100) {
fprintf(stderr, "Cannot set speed to %d, closest is %d\n", speed, closest_speed);
exit(-EINVAL);
}
printf("closest baud = %i, base = %i, divisor = %i\n", closest_speed, ss.baud_base,
ss.custom_divisor);
}
ret = ioctl(_fd, TIOCSSERIAL, &ss);
if (ret < 0) {
perror("TIOCSSERIAL failed");
exit(ret);
}
}
static void clear_custom_speed_flag()
{
struct serial_struct ss;
int ret;
if (ioctl(_fd, TIOCGSERIAL, &ss) < 0) {
// return silently as some devices do not support TIOCGSERIAL
return;
}
if ((ss.flags & ASYNC_SPD_MASK) != ASYNC_SPD_CUST)
return;
ss.flags &= ~ASYNC_SPD_MASK;
ret = ioctl(_fd, TIOCSSERIAL, &ss);
if (ret < 0) {
perror("TIOCSSERIAL failed");
exit(ret);
}
}
// converts integer baud to Linux define
static int get_baud(int baud)
{
switch (baud) {
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
case 230400:
return B230400;
case 460800:
return B460800;
case 500000:
return B500000;
case 576000:
return B576000;
case 921600:
return B921600;
#ifdef B1000000
case 1000000:
return B1000000;
#endif
#ifdef B1152000
case 1152000:
return B1152000;
#endif
#ifdef B1500000
case 1500000:
return B1500000;
#endif
#ifdef B2000000
case 2000000:
return B2000000;
#endif
#ifdef B2500000
case 2500000:
return B2500000;
#endif
#ifdef B3000000
case 3000000:
return B3000000;
#endif
#ifdef B3500000
case 3500000:
return B3500000;
#endif
#ifdef B4000000
case 4000000:
return B4000000;
#endif
default:
return -1;
}
}
void set_modem_lines(int fd, int bits, int mask)
{
int status, ret;
ret = ioctl(fd, TIOCMGET, &status);
if (ret < 0) {
perror("TIOCMGET failed");
exit(ret);
}
status = (status & ~mask) | (bits & mask);
ret = ioctl(fd, TIOCMSET, &status);
if (ret < 0) {
perror("TIOCMSET failed");
exit(ret);
}
}
static void display_help(void)
{
printf("Usage: linux-serial-test [OPTION]\n"
"\n"
" -h, --help\n"
" -b, --baud Baud rate, 115200, etc (115200 is default)\n"
" -p, --port Port (/dev/ttyS0, etc) (must be specified)\n"
" -d, --divisor UART Baud rate divisor (can be used to set custom baud rates)\n"
" -R, --rx_dump Dump Rx data (ascii, raw)\n"
" -T, --detailed_tx Detailed Tx data\n"
" -s, --stats Dump serial port stats every 5s\n"
" -S, --stop-on-err Stop program if we encounter an error\n"
" -y, --single-byte Send specified byte to the serial port\n"
" -z, --second-byte Send another specified byte to the serial port\n"
" -c, --rts-cts Enable RTS/CTS flow control\n"
" -B, --2-stop-bit Use two stop bits per character\n"
" -P, --parity Use parity bit (odd, even, mark, space)\n"
" -k, --loopback Use internal hardware loop back\n"
" -K, --write-follow Write follows the read count (can be used for multi-serial loopback)\n"
" -e, --dump-err Display errors\n"
" -r, --no-rx Don't receive data (can be used to test flow control)\n"
" when serial driver buffer is full\n"
" -t, --no-tx Don't transmit data\n"
" -l, --rx-delay Delay between reading data (ms) (can be used to test flow control)\n"
" -a, --tx-delay Delay between writing data (ms)\n"
" -w, --tx-bytes Number of bytes for each write (default is to repeatedly write 1024 bytes\n"
" until no more are accepted)\n"
" -q, --rs485 Enable RS485 direction control on port, and set delay from when TX is\n"
" finished and RS485 driver enable is de-asserted. Delay is specified in\n"
" bit times. To optionally specify a delay from when the driver is enabled\n"
" to start of TX use 'after_delay.before_delay' (-q 1.1)\n"
" -Q, --rs485_rts Deassert RTS on send, assert after send. Omitting -Q inverts this logic.\n"
" -o, --tx-time Number of seconds to transmit for (defaults to 0, meaning no limit)\n"
" -i, --rx-time Number of seconds to receive for (defaults to 0, meaning no limit)\n"
" -A, --ascii Output bytes range from 32 to 126 (default is 0 to 255)\n"
"\n"
);
}
static void process_options(int argc, char * argv[])
{
for (;;) {
int option_index = 0;
static const char *short_options = "hb:p:d:R:TsSy:z:cBertq:Ql:a:w:o:i:P:kKA";
static const struct option long_options[] = {
{"help", no_argument, 0, 0},
{"baud", required_argument, 0, 'b'},
{"port", required_argument, 0, 'p'},
{"divisor", required_argument, 0, 'd'},
{"rx_dump", required_argument, 0, 'R'},
{"detailed_tx", no_argument, 0, 'T'},
{"stats", no_argument, 0, 's'},
{"stop-on-err", no_argument, 0, 'S'},
{"single-byte", no_argument, 0, 'y'},
{"second-byte", no_argument, 0, 'z'},
{"rts-cts", no_argument, 0, 'c'},
{"2-stop-bit", no_argument, 0, 'B'},
{"parity", required_argument, 0, 'P'},
{"loopback", no_argument, 0, 'k'},
{"write-follows", no_argument, 0, 'K'},
{"dump-err", no_argument, 0, 'e'},
{"no-rx", no_argument, 0, 'r'},
{"no-tx", no_argument, 0, 't'},
{"rx-delay", required_argument, 0, 'l'},
{"tx-delay", required_argument, 0, 'a'},
{"tx-bytes", required_argument, 0, 'w'},
{"rs485", required_argument, 0, 'q'},
{"rs485_rts", no_argument, 0, 'Q'},
{"tx-time", required_argument, 0, 'o'},
{"rx-time", required_argument, 0, 'i'},
{"ascii", no_argument, 0, 'A'},
{0,0,0,0},
};
int c = getopt_long(argc, argv, short_options,
long_options, &option_index);
if (c == EOF) {
break;
}
switch (c) {
case 0:
case 'h':
display_help();
exit(0);
break;
case 'b':
_cl_baud = atoi(optarg);
break;
case 'p':
_cl_port = strdup(optarg);
break;
case 'd':
_cl_divisor = strtol(optarg, NULL, 0);
break;
case 'R':
_cl_rx_dump = 1;
_cl_rx_dump_ascii = !strcmp(optarg, "ascii");
break;
case 'T':
_cl_tx_detailed = 1;
break;
case 's':
_cl_stats = 1;
break;
case 'S':
_cl_stop_on_error = 1;
break;
case 'y': {
char * endptr;
_cl_single_byte = strtol(optarg, &endptr, 0);
break;
}
case 'z': {
char * endptr;
_cl_another_byte = strtol(optarg, &endptr, 0);
break;
}
case 'c':
_cl_rts_cts = 1;
break;
case 'B':
_cl_2_stop_bit = 1;
break;
case 'P':
_cl_parity = 1;
_cl_odd_parity = (!strcmp(optarg, "mark")||!strcmp(optarg, "odd"));
_cl_stick_parity = (!strcmp(optarg, "mark")||!strcmp(optarg, "space"));
break;
case 'k':
_cl_loopback = 1;
break;
case 'K':
_cl_write_after_read = 1;
break;
case 'e':
_cl_dump_err = 1;
break;
case 'r':
_cl_no_rx = 1;
break;
case 't':
_cl_no_tx = 1;
break;
case 'l': {
char *endptr;
_cl_rx_delay = strtol(optarg, &endptr, 0);
break;
}
case 'a': {
char *endptr;
_cl_tx_delay = strtol(optarg, &endptr, 0);
break;
}
case 'w': {
char *endptr;
_cl_tx_bytes = strtol(optarg, &endptr, 0);
break;
}
case 'q': {
char *endptr;
_cl_rs485_after_delay = strtol(optarg, &endptr, 0);
_cl_rs485_before_delay = strtol(endptr+1, &endptr, 0);
break;
}
case 'Q':
_cl_rs485_rts_after_send = 1;
break;
case 'o': {
char *endptr;
_cl_tx_time = strtol(optarg, &endptr, 0);
break;
}
case 'i': {
char *endptr;
_cl_rx_time = strtol(optarg, &endptr, 0);
break;
}
case 'A':
_cl_ascii_range = 1;
break;
}
}
}
static void dump_serial_port_stats(void)
{
struct serial_icounter_struct icount = { 0 };
printf("%s: count for this session: rx=%lld, tx=%lld, rx err=%lld\n", _cl_port, _read_count, _write_count, _error_count);
int ret = ioctl(_fd, TIOCGICOUNT, &icount);
if (ret < 0) {
perror("Error getting TIOCGICOUNT");
} else {
printf("%s: TIOCGICOUNT: ret=%i, rx=%i, tx=%i, frame = %i, overrun = %i, parity = %i, brk = %i, buf_overrun = %i\n",
_cl_port, ret, icount.rx, icount.tx, icount.frame, icount.overrun, icount.parity, icount.brk,
icount.buf_overrun);
}
}
static unsigned char next_count_value(unsigned char c)
{
c++;
if (_cl_ascii_range && c == 127)
c = 32;
return c;
}
static void process_read_data(void)
{
unsigned char rb[1024];
int c = read(_fd, &rb, sizeof(rb));
if (c > 0) {
if (_cl_rx_dump) {
if (_cl_rx_dump_ascii)
dump_data_ascii(rb, c);
else
dump_data(rb, c);
}
// verify read count is incrementing
int i;
for (i = 0; i < c; i++) {
if (rb[i] != _read_count_value) {
if (_cl_dump_err) {
printf("Error, count: %lld, expected %02x, got %02x\n",
_read_count + i, _read_count_value, rb[i]);
}
_error_count++;
if (_cl_stop_on_error) {
dump_serial_port_stats();
exit(-EIO);
}
_read_count_value = rb[i];
}
_read_count_value = next_count_value(_read_count_value);
}
_read_count += c;
}
}
static void process_write_data(void)
{
ssize_t count = 0;
ssize_t actual_write_size = 0;
int repeat = (_cl_tx_bytes == 0);
do
{
if (_cl_write_after_read == 0) {
actual_write_size = _write_size;
} else {
actual_write_size = _read_count > _write_count ? _read_count - _write_count : 0;
if (actual_write_size > _write_size) {
actual_write_size = _write_size;
}
}
if (actual_write_size == 0) {
break;
}
ssize_t i;
for (i = 0; i < actual_write_size; i++) {
_write_data[i] = _write_count_value;
_write_count_value = next_count_value(_write_count_value);
}
ssize_t c = write(_fd, _write_data, actual_write_size);
if (c < 0) {
if (errno != EAGAIN) {
printf("write failed - errno=%d (%s)\n", errno, strerror(errno));
}
c = 0;
}
count += c;
if (c < actual_write_size) {
_write_count_value = _write_data[c];
repeat = 0;
}
} while (repeat);
_write_count += count;
if (_cl_tx_detailed)
printf("wrote %zd bytes\n", count);
}
static void setup_serial_port(int baud)
{
struct termios newtio;
struct serial_rs485 rs485;
_fd = open(_cl_port, O_RDWR | O_NONBLOCK);
if (_fd < 0) {
perror("Error opening serial port");
free(_cl_port);
exit(_fd);
}
bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
/* man termios get more info on below settings */
newtio.c_cflag = baud | CS8 | CLOCAL | CREAD;
if (_cl_rts_cts) {
newtio.c_cflag |= CRTSCTS;
}
if (_cl_2_stop_bit) {
newtio.c_cflag |= CSTOPB;
}
if (_cl_parity) {
newtio.c_cflag |= PARENB;
if (_cl_odd_parity) {
newtio.c_cflag |= PARODD;
}
if (_cl_stick_parity) {
newtio.c_cflag |= CMSPAR;
}
}
newtio.c_iflag = 0;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
// block for up till 128 characters
newtio.c_cc[VMIN] = 128;
// 0.5 seconds read timeout
newtio.c_cc[VTIME] = 5;
/* now clean the modem line and activate the settings for the port */
tcflush(_fd, TCIOFLUSH);
tcsetattr(_fd,TCSANOW,&newtio);
/* enable/disable rs485 direction control, first check if RS485 is supported */
if(ioctl(_fd, TIOCGRS485, &rs485) < 0) {
if (_cl_rs485_after_delay >= 0) {
/* error could be because hardware is missing rs485 support so only print when actually trying to activate it */
perror("Error getting RS-485 mode");
}
} else {
if (_cl_rs485_after_delay >= 0) {
/* enable RS485 */
rs485.flags |= SER_RS485_ENABLED | SER_RS485_RX_DURING_TX |
(_cl_rs485_rts_after_send ? SER_RS485_RTS_AFTER_SEND : SER_RS485_RTS_ON_SEND);
rs485.flags &= ~(_cl_rs485_rts_after_send ? SER_RS485_RTS_ON_SEND : SER_RS485_RTS_AFTER_SEND);
rs485.delay_rts_after_send = _cl_rs485_after_delay;
rs485.delay_rts_before_send = _cl_rs485_before_delay;
if(ioctl(_fd, TIOCSRS485, &rs485) < 0) {
perror("Error setting RS-485 mode");
}
} else {
/* disable RS485 */
rs485.flags &= ~(SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND);
rs485.delay_rts_after_send = 0;
rs485.delay_rts_before_send = 0;
if(ioctl(_fd, TIOCSRS485, &rs485) < 0) {
perror("Error setting RS-232 mode");
}
}
}
}
static int diff_ms(const struct timespec *t1, const struct timespec *t2)
{
struct timespec diff;
diff.tv_sec = t1->tv_sec - t2->tv_sec;
diff.tv_nsec = t1->tv_nsec - t2->tv_nsec;
if (diff.tv_nsec < 0) {
diff.tv_sec--;
diff.tv_nsec += 1000000000;
}
return (diff.tv_sec * 1000 + diff.tv_nsec/1000000);
}
static long long int compute_error_count()
{
long long int result;
if (_cl_no_rx == 1 || _cl_no_tx == 1) {
result = _error_count;
} else {
result = llabs(_write_count - _read_count) + _error_count;
}
return (result > 125) ? 125 : (int)result;
}
int main(int argc, char * argv[])
{
printf("Linux serial test app\n");
process_options(argc, argv);
if (!_cl_port) {
fprintf(stderr, "ERROR: Port argument required\n");
display_help();
exit(-EINVAL);
}
int baud = B115200;
if (_cl_baud && !_cl_divisor)
baud = get_baud(_cl_baud);
if (baud <= 0 || _cl_divisor) {
printf("NOTE: non standard baud rate, trying custom divisor\n");
baud = B38400;
setup_serial_port(B38400);
set_baud_divisor(_cl_baud, _cl_divisor);
} else {
setup_serial_port(baud);
/*
* The flag ASYNC_SPD_CUST might have already been set, so
* clear it to avoid confusing the kernel uart dirver.
*/
clear_custom_speed_flag();
}
set_modem_lines(_fd, _cl_loopback ? TIOCM_LOOP : 0, TIOCM_LOOP);
if (_cl_single_byte >= 0) {
unsigned char data[2];
int bytes = 1;
int written;
data[0] = (unsigned char)_cl_single_byte;
if (_cl_another_byte >= 0) {
data[1] = (unsigned char)_cl_another_byte;
bytes++;
}
written = write(_fd, &data, bytes);
if (written < 0) {
perror("write()");
exit(written);
} else if (written != bytes) {
fprintf(stderr, "ERROR: write() returned %d, not %d\n", written, bytes);
exit(-EIO);
}
return 0;
}
_write_size = (_cl_tx_bytes == 0) ? 1024 : _cl_tx_bytes;
_write_data = malloc(_write_size);
if (_write_data == NULL) {
fprintf(stderr, "ERROR: Memory allocation failed\n");
exit(-ENOMEM);
}
if (_cl_ascii_range) {
_read_count_value = _write_count_value = 32;
}
struct pollfd serial_poll;
serial_poll.fd = _fd;
if (!_cl_no_rx) {
serial_poll.events |= POLLIN;
} else {
serial_poll.events &= ~POLLIN;
}
if (!_cl_no_tx) {
serial_poll.events |= POLLOUT;
} else {
serial_poll.events &= ~POLLOUT;
}
struct timespec start_time, last_stat, last_timeout, last_read, last_write;
clock_gettime(CLOCK_MONOTONIC, &start_time);
last_stat = start_time;
last_timeout = start_time;
last_read = start_time;
last_write = start_time;
while (!(_cl_no_rx && _cl_no_tx)) {
struct timespec current;
int retval = poll(&serial_poll, 1, 1000);
clock_gettime(CLOCK_MONOTONIC, ¤t);
if (retval == -1) {
perror("poll()");
} else if (retval) {
if (serial_poll.revents & POLLIN) {
if (_cl_rx_delay) {
// only read if it has been rx-delay ms
// since the last read
if (diff_ms(¤t, &last_read) > _cl_rx_delay) {
process_read_data();
last_read = current;
}
} else {
process_read_data();
last_read = current;
}
}
if (serial_poll.revents & POLLOUT) {
if (_cl_tx_delay) {
// only write if it has been tx-delay ms
// since the last write
if (diff_ms(¤t, &last_write) > _cl_tx_delay) {
process_write_data();
last_write = current;
}
} else {
process_write_data();
last_write = current;
}
}
}
// Has it been at least a second since we reported a timeout?
if (diff_ms(¤t, &last_timeout) > 1000) {
int rx_timeout, tx_timeout;
// Has it been over two seconds since we transmitted or received data?
rx_timeout = (!_cl_no_rx && diff_ms(¤t, &last_read) > 2000);
tx_timeout = (!_cl_no_tx && diff_ms(¤t, &last_write) > 2000);
// Special case - we don't want to warn about receive
// timeouts at the end of a loopback test (where we are
// no longer transmitting and the receive count equals
// the transmit count).
if (_cl_no_tx && _write_count != 0 && _write_count == _read_count) {
rx_timeout = 0;
}
if (rx_timeout || tx_timeout) {
const char *s;
if (rx_timeout) {
printf("%s: No data received for %.1fs.",
_cl_port, (double)diff_ms(¤t, &last_read) / 1000);
s = " ";
} else {
s = "";
}
if (tx_timeout) {
printf("%sNo data transmitted for %.1fs.",
s, (double)diff_ms(¤t, &last_write) / 1000);
}
printf("\n");
last_timeout = current;
}
}
if (_cl_stats) {
if (current.tv_sec - last_stat.tv_sec > 5) {
dump_serial_port_stats();
last_stat = current;
}
}
if (_cl_tx_time) {
if (current.tv_sec - start_time.tv_sec >= _cl_tx_time) {
_cl_tx_time = 0;
_cl_no_tx = 1;
serial_poll.events &= ~POLLOUT;
printf("Stopped transmitting.\n");
}
}
if (_cl_rx_time) {
if (current.tv_sec - start_time.tv_sec >= _cl_rx_time) {
_cl_rx_time = 0;
_cl_no_rx = 1;
serial_poll.events &= ~POLLIN;
printf("Stopped receiving.\n");
}
}
}
tcdrain(_fd);
dump_serial_port_stats();
set_modem_lines(_fd, 0, TIOCM_LOOP);
tcflush(_fd, TCIOFLUSH);
free(_cl_port);
free(_write_data);
return compute_error_count();
}