-
Notifications
You must be signed in to change notification settings - Fork 14
/
devcfg.c
1243 lines (1097 loc) · 28.7 KB
/
devcfg.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
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
/*
* ser2net - A program for allowing telnet connection to serial ports
* Copyright (C) 2001 Corey Minyard <[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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* This code handles generating the configuration for the serial port. */
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <syslog.h>
#include "ser2net.h"
#include "selector.h"
#include "utils.h"
#include "dataxfer.h"
#include "devio.h"
#include <assert.h>
struct devcfg_data {
/* Information about the terminal device. */
char *devname; /* The full path to the device */
int devfd; /* The file descriptor for the
device, only valid if the
TCP port is open. */
struct termios termctl;
/* Holds whether break is on or not. */
int break_set;
/* Disable break-commands */
int disablebreak;
#ifdef USE_RS485_FEATURE
struct serial_rs485 *conf;
#endif
};
static struct baud_rates_s {
int real_rate;
int val;
int cisco_ios_val;
} baud_rates[] =
{
{ 50, B50, -1 },
{ 75, B75, -1 },
{ 110, B110, -1 },
{ 134, B134, -1 },
{ 150, B150, -1 },
{ 200, B200, -1 },
{ 300, B300, 3 },
{ 600, B600 , 4},
{ 1200, B1200, 5 },
{ 1800, B1800, -1 },
{ 2400, B2400, 6 },
{ 4800, B4800, 7 },
{ 9600, B9600, 8 },
/* We don't support 14400 baud */
{ 19200, B19200, 10 },
/* We don't support 28800 baud */
{ 38400, B38400, 12 },
{ 57600, B57600, 13 },
{ 115200, B115200, 14 },
{ 230400, B230400, 15 },
/* We don't support 460800 baud */
};
#define BAUD_RATES_LEN ((sizeof(baud_rates) / sizeof(struct baud_rates_s)))
static int
get_baud_rate(int rate, int *val, int cisco)
{
unsigned int i;
for (i=0; i<BAUD_RATES_LEN; i++) {
if (cisco) {
if (rate == baud_rates[i].cisco_ios_val) {
*val = baud_rates[i].val;
return 1;
}
} else {
if (rate == baud_rates[i].real_rate) {
*val = baud_rates[i].val;
return 1;
}
}
}
return 0;
}
static void
get_rate_from_baud_rate(int baud_rate, int *val, int cisco)
{
unsigned int i;
for (i=0; i<BAUD_RATES_LEN; i++) {
if (baud_rate == baud_rates[i].val) {
if (cisco) {
if (baud_rates[i].cisco_ios_val < 0)
/* We are at a baud rate unsupported by the
enumeration, just return zero. */
*val = 0;
else
*val = baud_rates[i].cisco_ios_val;
} else {
*val = baud_rates[i].real_rate;
}
return;
}
}
}
#ifdef USE_UUCP_LOCKING
static char *uucp_lck_dir = "/var/lock";
static int
uucp_fname_lock_size(char *devname)
{
char *ptr;
(ptr = strrchr(devname, '/'));
if (ptr == NULL) {
ptr = devname;
} else {
ptr = ptr + 1;
}
return 7 + strlen(uucp_lck_dir) + strlen(ptr);
}
static void
uucp_fname_lock(char *buf, char *devname)
{
char *ptr;
(ptr = strrchr(devname, '/'));
if (ptr == NULL) {
ptr = devname;
} else {
ptr = ptr + 1;
}
sprintf(buf, "%s/LCK..%s", uucp_lck_dir, ptr);
}
static void
uucp_rm_lock(char *devname)
{
char *lck_file;
if (!uucp_locking_enabled) return;
lck_file = malloc(uucp_fname_lock_size(devname));
if (lck_file == NULL) {
return;
}
uucp_fname_lock(lck_file, devname);
unlink(lck_file);
free(lck_file);
}
/* return 0=OK, -1=error, 1=locked by other proces */
static int
uucp_mk_lock(char *devname)
{
struct stat stt;
int pid = -1;
if (!uucp_locking_enabled)
return 0;
if (stat(uucp_lck_dir, &stt) == 0) { /* is lock file directory present? */
char *lck_file;
union {
uint32_t ival;
char str[64];
} buf;
int fd;
lck_file = malloc(uucp_fname_lock_size(devname));
if (lck_file == NULL)
return -1;
uucp_fname_lock(lck_file, devname);
pid = 0;
if ((fd = open(lck_file, O_RDONLY)) >= 0) {
int n;
n = read(fd, &buf, sizeof(buf));
close(fd);
if( n == 4 ) /* Kermit-style lockfile. */
pid = buf.ival;
else if (n > 0) { /* Ascii lockfile. */
buf.str[n] = 0;
sscanf(buf.str, "%d", &pid);
}
if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) {
/* death lockfile - remove it */
unlink(lck_file);
sleep(1);
pid = 0;
} else
pid = 1;
}
if (pid == 0) {
int mask;
mask = umask(022);
fd = open(lck_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
umask(mask);
if (fd >= 0) {
ssize_t rv;
snprintf(buf.str, sizeof(buf), "%10ld\n",
(long)getpid());
rv = write_full(fd, buf.str, strlen(buf.str));
close(fd);
if (rv < 0) {
pid = -1;
unlink(lck_file);
}
} else {
pid = -1;
}
}
free(lck_file);
}
return pid;
}
#endif /* USE_UUCP_LOCKING */
#ifdef __CYGWIN__
static void cfmakeraw(struct termios *termios_p) {
termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
termios_p->c_oflag &= ~OPOST;
termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
termios_p->c_cflag &= ~(CSIZE|PARENB);
termios_p->c_cflag |= CS8;
}
#endif
/* Initialize a serial port control structure for the first time.
This should only be called when the port is created. It sets the
port to the default 9600N81. */
static void
devinit(struct termios *termctl)
{
cfmakeraw(termctl);
cfsetospeed(termctl, B9600);
cfsetispeed(termctl, B9600);
termctl->c_cflag &= ~(CSTOPB);
termctl->c_cflag &= ~(CSIZE);
termctl->c_cflag |= CS8;
termctl->c_cflag &= ~(PARENB);
termctl->c_cflag &= ~(CLOCAL);
termctl->c_cflag &= ~(HUPCL);
termctl->c_cflag |= CREAD;
termctl->c_cflag &= ~(CRTSCTS);
termctl->c_iflag &= ~(IXON | IXOFF | IXANY);
termctl->c_iflag |= IGNBRK;
}
/* Configure a serial port control structure based upon input strings
in instr. These strings are described in the man page for this
program. */
static int
devconfig(struct devcfg_data *d, struct absout *eout, const char *instr,
int (*otherconfig)(void *data, struct absout *eout, const char *item),
void *data)
{
struct termios *termctl = &d->termctl;
char *str;
char *pos;
char *strtok_data;
int rv = 0;
devinit(termctl);
str = strdup(instr);
if (str == NULL) {
return -1;
}
pos = strtok_r(str, ", \t", &strtok_data);
while (pos != NULL) {
if (strcmp(pos, "300") == 0) {
cfsetospeed(termctl, B300);
cfsetispeed(termctl, B300);
} else if (strcmp(pos, "600") == 0) {
cfsetospeed(termctl, B600);
cfsetispeed(termctl, B600);
} else if (strcmp(pos, "1200") == 0) {
cfsetospeed(termctl, B1200);
cfsetispeed(termctl, B1200);
} else if (strcmp(pos, "2400") == 0) {
cfsetospeed(termctl, B2400);
cfsetispeed(termctl, B2400);
} else if (strcmp(pos, "4800") == 0) {
cfsetospeed(termctl, B4800);
cfsetispeed(termctl, B4800);
} else if (strcmp(pos, "9600") == 0) {
cfsetospeed(termctl, B9600);
cfsetispeed(termctl, B9600);
} else if (strcmp(pos, "19200") == 0) {
cfsetospeed(termctl, B19200);
cfsetispeed(termctl, B19200);
} else if (strcmp(pos, "38400") == 0) {
cfsetospeed(termctl, B38400);
cfsetispeed(termctl, B38400);
} else if (strcmp(pos, "57600") == 0) {
cfsetospeed(termctl, B57600);
cfsetispeed(termctl, B57600);
} else if (strcmp(pos, "115200") == 0) {
cfsetospeed(termctl, B115200);
cfsetispeed(termctl, B115200);
#ifdef B230400
} else if (strcmp(pos, "230400") == 0) {
cfsetospeed(termctl, B230400);
cfsetispeed(termctl, B230400);
#endif
#ifdef B460800
} else if (strcmp(pos, "460800") == 0) {
cfsetospeed(termctl, B460800);
cfsetispeed(termctl, B460800);
#endif
#ifdef B500000
} else if (strcmp(pos, "500000") == 0) {
cfsetospeed(termctl, B500000);
cfsetispeed(termctl, B500000);
#endif
#ifdef B576000
} else if (strcmp(pos, "576000") == 0) {
cfsetospeed(termctl, B576000);
cfsetispeed(termctl, B576000);
#endif
#ifdef B921600
} else if (strcmp(pos, "921600") == 0) {
cfsetospeed(termctl, B921600);
cfsetispeed(termctl, B921600);
#endif
#ifdef B1000000
} else if (strcmp(pos, "1000000") == 0) {
cfsetospeed(termctl, B1000000);
cfsetispeed(termctl, B1000000);
#endif
#ifdef B1152000
} else if (strcmp(pos, "1152000") == 0) {
cfsetospeed(termctl, B1152000);
cfsetispeed(termctl, B1152000);
#endif
#ifdef B1500000
} else if (strcmp(pos, "1500000") == 0) {
cfsetospeed(termctl, B1500000);
cfsetispeed(termctl, B1500000);
#endif
#ifdef B2000000
} else if (strcmp(pos, "2000000") == 0) {
cfsetospeed(termctl, B2000000);
cfsetispeed(termctl, B2000000);
#endif
#ifdef B2500000
} else if (strcmp(pos, "2500000") == 0) {
cfsetospeed(termctl, B2500000);
cfsetispeed(termctl, B2500000);
#endif
#ifdef B3000000
} else if (strcmp(pos, "3000000") == 0) {
cfsetospeed(termctl, B3000000);
cfsetispeed(termctl, B3000000);
#endif
#ifdef B3500000
} else if (strcmp(pos, "3500000") == 0) {
cfsetospeed(termctl, B3500000);
cfsetispeed(termctl, B3500000);
#endif
#ifdef B4000000
} else if (strcmp(pos, "4000000") == 0) {
cfsetospeed(termctl, B4000000);
cfsetispeed(termctl, B4000000);
#endif
} else if (strcmp(pos, "1STOPBIT") == 0) {
termctl->c_cflag &= ~(CSTOPB);
} else if (strcmp(pos, "2STOPBITS") == 0) {
termctl->c_cflag |= CSTOPB;
} else if (strcmp(pos, "7DATABITS") == 0) {
termctl->c_cflag &= ~(CSIZE);
termctl->c_cflag |= CS7;
} else if (strcmp(pos, "8DATABITS") == 0) {
termctl->c_cflag &= ~(CSIZE);
termctl->c_cflag |= CS8;
} else if (strcmp(pos, "NONE") == 0) {
termctl->c_cflag &= ~(PARENB);
} else if (strcmp(pos, "EVEN") == 0) {
termctl->c_cflag |= PARENB;
termctl->c_cflag &= ~(PARODD);
} else if (strcmp(pos, "ODD") == 0) {
termctl->c_cflag |= PARENB | PARODD;
} else if (strcmp(pos, "XONXOFF") == 0) {
termctl->c_iflag |= (IXON | IXOFF | IXANY);
termctl->c_cc[VSTART] = 17;
termctl->c_cc[VSTOP] = 19;
} else if (strcmp(pos, "-XONXOFF") == 0) {
termctl->c_iflag &= ~(IXON | IXOFF | IXANY);
} else if (strcmp(pos, "RTSCTS") == 0) {
termctl->c_cflag |= CRTSCTS;
} else if (strcmp(pos, "-RTSCTS") == 0) {
termctl->c_cflag &= ~CRTSCTS;
} else if (strcmp(pos, "LOCAL") == 0) {
termctl->c_cflag |= CLOCAL;
} else if (strcmp(pos, "-LOCAL") == 0) {
termctl->c_cflag &= ~CLOCAL;
} else if (strcmp(pos, "HANGUP_WHEN_DONE") == 0) {
termctl->c_cflag |= HUPCL;
} else if (strcmp(pos, "-HANGUP_WHEN_DONE") == 0) {
termctl->c_cflag &= ~HUPCL;
} else if (strcmp(pos, "NOBREAK") == 0) {
d->disablebreak = 1;
} else {
if (otherconfig(data, eout, pos) == -1)
goto out;
}
pos = strtok_r(NULL, ", \t", &strtok_data);
}
#ifdef USE_RS485_FEATURE
d->conf = get_rs485_conf(data);
#endif
out:
free(str);
return rv;
}
static char *
baud_string(int speed)
{
char *str;
switch (speed) {
case B300: str = "300"; break;
case B600: str = "600"; break;
case B1200: str = "1200"; break;
case B2400: str = "2400"; break;
case B4800: str = "4800"; break;
case B9600: str = "9600"; break;
case B19200: str = "19200"; break;
case B38400: str = "38400"; break;
case B57600: str = "57600"; break;
case B115200: str = "115200"; break;
#ifdef B230400
case B230400: str = "230400"; break;
#endif
#ifdef B460800
case B460800: str = "460800"; break;
#endif
#ifdef B500000
case B500000: str = "500000"; break;
#endif
#ifdef B576000
case B576000: str = "576000"; break;
#endif
#ifdef B921600
case B921600: str = "921600"; break;
#endif
#ifdef B1000000
case B1000000: str = "1000000"; break;
#endif
#ifdef B1152000
case B1152000: str = "1152000"; break;
#endif
#ifdef B1500000
case B1500000: str = "1500000"; break;
#endif
#ifdef B2000000
case B2000000: str = "2000000"; break;
#endif
#ifdef B2500000
case B2500000: str = "2500000"; break;
#endif
#ifdef B3000000
case B3000000: str = "3000000"; break;
#endif
#ifdef B3500000
case B3500000: str = "3500000"; break;
#endif
#ifdef B4000000
case B4000000: str = "4000000"; break;
#endif
default: str = "unknown speed";
}
return str;
}
static void
devcfg_serparm_to_str(struct devio *io, char *str, int strlen)
{
struct devcfg_data *d = io->my_data;
struct termios *termctl = &d->termctl;
speed_t speed = cfgetospeed(termctl);
int stopbits = termctl->c_cflag & CSTOPB;
int databits = termctl->c_cflag & CSIZE;
int parity_enabled = termctl->c_cflag & PARENB;
int parity = termctl->c_cflag & PARODD;
char *sstr;
char pchar, schar, dchar;
sstr = baud_string(speed);
if (stopbits)
schar = '2';
else
schar = '1';
switch (databits) {
case CS7: dchar = '7'; break;
case CS8: dchar = '8'; break;
default: dchar = '?';
}
if (parity_enabled) {
if (parity) {
pchar = 'O';
} else {
pchar = 'E';
}
} else {
pchar = 'N';
}
snprintf(str, strlen, "%s %c%c%c", sstr, pchar, dchar, schar);
}
/* Send the serial port device configuration to the control port. */
static void
devcfg_show_devcfg(struct devio *io, struct absout *out)
{
struct devcfg_data *d = io->my_data;
struct termios *termctl = &d->termctl;
speed_t speed = cfgetospeed(termctl);
int stopbits = termctl->c_cflag & CSTOPB;
int databits = termctl->c_cflag & CSIZE;
int parity_enabled = termctl->c_cflag & PARENB;
int parity = termctl->c_cflag & PARODD;
int xon = termctl->c_iflag & IXON;
int xoff = termctl->c_iflag & IXOFF;
int xany = termctl->c_iflag & IXANY;
int flow_rtscts = termctl->c_cflag & CRTSCTS;
int clocal = termctl->c_cflag & CLOCAL;
int hangup_when_done = termctl->c_cflag & HUPCL;
char *str;
out->out(out, "%s ", baud_string(speed));
if (xon && xoff && xany) {
out->out(out, "XONXOFF ");
}
if (flow_rtscts) {
out->out(out, "RTSCTS ");
}
if (clocal) {
out->out(out, "LOCAL ");
}
if (hangup_when_done) {
out->out(out, "HANGUP_WHEN_DONE ");
}
if (stopbits) {
str = "2STOPBITS";
} else {
str = "1STOPBIT";
}
out->out(out, "%s ", str);
switch (databits) {
case CS7: str = "7DATABITS"; break;
case CS8: str = "8DATABITS"; break;
default: str = "unknown databits";
}
out->out(out, "%s ", str);
if (parity_enabled) {
if (parity) {
str = "ODD";
} else {
str = "EVEN";
}
} else {
str = "NONE";
}
out->out(out, "%s", str);
}
static int
devcfg_set_devcontrol(struct devio *io, const char *instr)
{
struct devcfg_data *d = io->my_data;
int fd = d->devfd;
int rv = 0;
char *str;
char *pos;
int status;
char *strtok_data;
str = malloc(strlen(instr) + 1);
if (str == NULL) {
return -1;
}
strcpy(str, instr);
pos = strtok_r(str, " \t", &strtok_data);
while (pos != NULL) {
if (strcmp(pos, "RTSHI") == 0) {
ioctl(fd, TIOCMGET, &status);
status |= TIOCM_RTS;
ioctl(fd, TIOCMSET, &status);
} else if (strcmp(pos, "RTSLO") == 0) {
ioctl(fd, TIOCMGET, &status);
status &= ~TIOCM_RTS;
ioctl(fd, TIOCMSET, &status);
} else if (strcmp(pos, "DTRHI") == 0) {
ioctl(fd, TIOCMGET, &status);
status |= TIOCM_DTR;
ioctl(fd, TIOCMSET, &status);
} else if (strcmp(pos, "DTRLO") == 0) {
ioctl(fd, TIOCMGET, &status);
status &= ~TIOCM_DTR; /* AKA drop DTR */
ioctl(fd, TIOCMSET, &status);
} else {
rv = -1;
goto out;
}
pos = strtok_r(NULL, " \t", &strtok_data);
}
out:
free(str);
return rv;
}
static void
devcfg_show_devcontrol(struct devio *io, struct absout *out)
{
struct devcfg_data *d = io->my_data;
char *str;
int status;
ioctl(d->devfd, TIOCMGET, &status);
if (status & TIOCM_RTS) {
str = "RTSHI";
} else {
str = "RTSLO";
}
out->out(out, "%s ", str);
if (status & TIOCM_DTR) {
str = "DTRHI";
} else {
str = "DTRLO";
}
out->out(out, "%s ", str);
}
static void
do_read(int fd, void *data)
{
struct devio *io = data;
io->read_handler(io);
}
static void
do_write(int fd, void *data)
{
struct devio *io = data;
io->write_handler(io);
}
static void
do_except(int fd, void *data)
{
struct devio *io = data;
io->except_handler(io);
}
static int devcfg_setup(struct devio *io, const char *name, const char **errstr)
{
struct devcfg_data *d = io->my_data;
int options;
#ifdef USE_UUCP_LOCKING
{
int rv;
rv = uucp_mk_lock(io->devname);
if (rv > 0 ) {
*errstr = "Port already in use by another process\n\r";
return -1;
} else if (rv < 0) {
*errstr = "Error creating port lock file\n\r";
return -1;
}
}
#endif /* USE_UUCP_LOCKING */
/* Oct 05 2001 druzus: NOCTTY - don't make
device control tty for our process */
options = O_NONBLOCK | O_NOCTTY;
if (io->read_disabled) {
options |= O_WRONLY;
} else {
options |= O_RDWR;
}
d->devfd = open(io->devname, options);
if (d->devfd == -1) {
syslog(LOG_ERR, "Could not open device %s for port %s: %m",
io->devname,
name);
#ifdef USE_UUCP_LOCKING
uucp_rm_lock(io->devname);
#endif /* USE_UUCP_LOCKING */
return -1;
}
if (!io->read_disabled && !d->disablebreak
&& tcsetattr(d->devfd, TCSANOW, &d->termctl) == -1)
{
close(d->devfd);
d->devfd = -1;
syslog(LOG_ERR, "Could not set up device %s for port %s: %m",
io->devname,
name);
#ifdef USE_UUCP_LOCKING
uucp_rm_lock(io->devname);
#endif /* USE_UUCP_LOCKING */
return -1;
}
/* Turn off BREAK. */
if (!io->read_disabled && ioctl(d->devfd, TIOCCBRK) == -1) {
/* Probably not critical, but we should at least log something. */
syslog(LOG_ERR, "Could not turn off break for device %s port %s: %m",
io->devname,
name);
}
#ifdef USE_RS485_FEATURE
if (d->conf) {
if (d->conf->flags & SER_RS485_ENABLED) {
if (ioctl(d->devfd , TIOCSRS485, d->conf ) < 0) {
syslog(LOG_ERR, "Could not set RS485 config for device %s port %s: %m",
io->devname,
name);
return -1;
}
}
}
#endif
sel_set_fd_handlers(ser2net_sel, d->devfd, io,
io->read_disabled ? NULL : do_read,
do_write, do_except);
return 0;
}
static void devcfg_shutdown(struct devio *io)
{
struct devcfg_data *d = io->my_data;
/* To avoid blocking on close if we have written bytes and are in
flow-control, we flush the output queue. */
if (d->devfd != -1) {
sel_clear_fd_handlers(ser2net_sel, d->devfd);
tcflush(d->devfd, TCOFLUSH);
close(d->devfd);
d->devfd = -1;
}
#ifdef USE_UUCP_LOCKING
uucp_rm_lock(io->devname);
#endif /* USE_UUCP_LOCKING */
}
static int devcfg_read(struct devio *io, void *buf, size_t size)
{
struct devcfg_data *d = io->my_data;
return read(d->devfd, buf, size);
}
static int devcfg_write(struct devio *io, void *buf, size_t size)
{
struct devcfg_data *d = io->my_data;
return write(d->devfd, buf, size);
}
static void devcfg_read_handler_enable(struct devio *io, int enabled)
{
struct devcfg_data *d = io->my_data;
sel_set_fd_read_handler(ser2net_sel, d->devfd,
enabled ? SEL_FD_HANDLER_ENABLED :
SEL_FD_HANDLER_DISABLED);
}
static void devcfg_write_handler_enable(struct devio *io, int enabled)
{
struct devcfg_data *d = io->my_data;
sel_set_fd_write_handler(ser2net_sel, d->devfd,
enabled ? SEL_FD_HANDLER_ENABLED :
SEL_FD_HANDLER_DISABLED);
}
static void devcfg_except_handler_enable(struct devio *io, int enabled)
{
struct devcfg_data *d = io->my_data;
sel_set_fd_except_handler(ser2net_sel, d->devfd,
enabled ? SEL_FD_HANDLER_ENABLED :
SEL_FD_HANDLER_DISABLED);
}
static int devcfg_send_break(struct devio *io)
{
struct devcfg_data *d = io->my_data;
tcsendbreak(d->devfd, 0);
return 0;
}
static int devcfg_get_modem_state(struct devio *io, unsigned char *modemstate)
{
struct devcfg_data *d = io->my_data;
int val;
if (ioctl(d->devfd, TIOCMGET, &val) != -1)
return -1;
*modemstate = 0;
if (val & TIOCM_CD)
*modemstate |= 0x80;
if (val & TIOCM_RI)
*modemstate |= 0x40;
if (val & TIOCM_DSR)
*modemstate |= 0x20;
if (val & TIOCM_CTS)
*modemstate |= 0x10;
return 0;
}
static int devcfg_baud_rate(struct devio *io, int *val, int cisco)
{
struct devcfg_data *d = io->my_data;
struct termios termio;
if (tcgetattr(d->devfd, &termio) == -1) {
*val = 0;
return -1;
}
if ((*val != 0) && (get_baud_rate(*val, val, cisco))) {
/* We have a valid baud rate. */
cfsetispeed(&termio, *val);
cfsetospeed(&termio, *val);
tcsetattr(d->devfd, TCSANOW, &termio);
}
tcgetattr(d->devfd, &termio);
*val = cfgetispeed(&termio);
get_rate_from_baud_rate(*val, val, cisco);
return 0;
}
static int devcfg_data_size(struct devio *io, unsigned char *val)
{
struct devcfg_data *d = io->my_data;
struct termios termio;
if (tcgetattr(d->devfd, &termio) == -1) {
*val = 0;
return -1;
}
if ((*val >= 5) && (*val <= 8)) {
termio.c_cflag &= ~CSIZE;
switch (*val) {
case 5: termio.c_cflag |= CS5; break;
case 6: termio.c_cflag |= CS6; break;
case 7: termio.c_cflag |= CS7; break;
case 8: termio.c_cflag |= CS8; break;
}
tcsetattr(d->devfd, TCSANOW, &termio);
}
switch (termio.c_cflag & CSIZE) {
case CS5: *val = 5; break;
case CS6: *val = 6; break;
case CS7: *val = 7; break;
case CS8: *val = 8; break;
default: *val = 0;
}
return 0;
}
static int devcfg_parity(struct devio *io, unsigned char *val)
{
struct devcfg_data *d = io->my_data;
struct termios termio;
if (tcgetattr(d->devfd, &termio) == -1) {
*val = 0;
return -1;
}
/* We don't support MARK or SPACE parity. */
if ((*val >= 1) && (*val <= 3)) {
termio.c_cflag &= ~(PARENB | PARODD);
switch (*val) {
case 1: break; /* NONE */
case 2: termio.c_cflag |= PARENB | PARODD; break; /* ODD */
case 3: termio.c_cflag |= PARENB; break; /* EVEN */
}
tcsetattr(d->devfd, TCSANOW, &termio);
}
if (termio.c_cflag & PARENB) {
if (termio.c_cflag & PARODD)
*val = 2; /* ODD */
else
*val = 3; /* EVEN */
} else
*val = 1; /* NONE */
return 0;
}
static int devcfg_stop_size(struct devio *io, unsigned char *val)
{
struct devcfg_data *d = io->my_data;
struct termios termio;
if (tcgetattr(d->devfd, &termio) == -1) {
*val = 0;
return -1;
}
if ((*val >= 1) && (*val <= 2)) {
termio.c_cflag &= ~CSTOPB;
switch (*val) {
case 1: break; /* 1 stop bit */
case 2: termio.c_cflag |= CSTOPB; break; /* 2 stop bits */
}
tcsetattr(d->devfd, TCSANOW, &termio);
}
if (termio.c_cflag & CSTOPB)
*val = 2; /* 2 stop bits. */
else
*val = 1; /* 1 stop bit. */
return 0;
}
static int devcfg_flow_control(struct devio *io, unsigned char val)
{
struct devcfg_data *d = io->my_data;
tcflow(d->devfd, val ? TCIOFF : TCION);