-
Notifications
You must be signed in to change notification settings - Fork 73
/
dataxfer.c
1738 lines (1462 loc) · 41.2 KB
/
dataxfer.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-2020 Corey Minyard <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-only
*
* In addition, as a special exception, the copyright holders of
* ser2net give you permission to combine ser2net with free software
* programs or libraries that are released under the GNU LGPL and
* with code included in the standard release of OpenSSL under the
* OpenSSL license (or modified versions of such code, with unchanged
* license). You may copy and distribute such a system following the
* terms of the GNU GPL for ser2net and the licenses of the other code
* concerned, provided that you include the source code of that
* other code when and as the GNU GPL requires distribution of source
* code.
*
* Note that people who make modified versions of ser2net are not
* obligated to grant this special exception for their modified
* versions; it is their choice whether to do so. The GNU General
* Public License gives permission to release a modified version
* without this exception; this exception also makes it possible to
* release a modified version which carries forward this exception.
*/
/* This code handles the actual transfer of data between the serial
ports and the TCP ports. */
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <stdint.h>
#include <gensio/gensio.h>
#ifndef GENSIO_EVENT_SER_MODEMSTATE
#include <gensio/sergensio.h>
#endif
#include "port.h"
#include "ser2net.h"
#include "dataxfer.h"
#include "readconfig.h"
#include "led.h"
/* For tracing. */
#define SERIAL "term"
#define NET "tcp "
static void setup_port(port_info_t *port, net_info_t *netcon);
static int handle_net_event(struct gensio *net, void *user_data,
int event, int err,
unsigned char *buf, gensiods *buflen,
const char *const *auxdata);
static int
all_net_connectbacks_done(port_info_t *port)
{
net_info_t *netcon;
for_each_connection(port, netcon) {
if (netcon->connect_back && !netcon->net)
return false;
}
return true;
}
static bool
any_net_data_to_write(port_info_t *port)
{
net_info_t *netcon;
for_each_connection(port, netcon) {
if (!netcon->net)
continue;
if (netcon->write_pos < port->dev_to_net.cursize)
return true;
}
return false;
}
static void
start_net_send(port_info_t *port)
{
net_info_t *netcon;
if (port->dev_to_net_state == PORT_WAITING_OUTPUT_CLEAR)
return;
gensio_set_read_callback_enable(port->io, false);
for_each_connection(port, netcon) {
if (!netcon->net)
continue;
netcon->write_pos = 0;
gensio_set_write_callback_enable(netcon->net, true);
}
port->dev_to_net_state = PORT_WAITING_OUTPUT_CLEAR;
}
static void
disable_all_net_read(port_info_t *port)
{
net_info_t *netcon;
for_each_connection(port, netcon) {
if (netcon->net)
gensio_set_read_callback_enable(netcon->net, false);
}
}
static void
enable_all_net_read(port_info_t *port)
{
net_info_t *netcon;
for_each_connection(port, netcon) {
if (netcon->net)
gensio_set_read_callback_enable(netcon->net, true);
}
}
static void
report_newcon(port_info_t *port, net_info_t *netcon)
{
if (!net_raddr_str(netcon->net, netcon->remaddr, sizeof(netcon->remaddr)))
strcpy(netcon->remaddr, "*unknown*");
cntlr_report_conchange("new connection", port->name, netcon->remaddr);
}
void
report_disconnect(port_info_t *port, net_info_t *netcon)
{
cntlr_report_conchange("disconnect", port->name, netcon->remaddr);
}
static void
connect_back_done(struct gensio *net, int err, void *cb_data)
{
net_info_t *netcon = cb_data;
port_info_t *port = netcon->port;
so->lock(port->lock);
if (err) {
netcon->net = NULL;
gensio_free(net);
} else {
report_newcon(port, netcon);
setup_port(port, netcon);
}
assert(port->num_waiting_connect_backs > 0);
port->num_waiting_connect_backs--;
if (port->num_waiting_connect_backs == 0) {
if (!all_net_connectbacks_done(port))
/* Not all connections back could be made. */
port->nocon_read_enable_time_left = port->accepter_retry_time;
else
gensio_set_read_callback_enable(port->io, true);
}
so->unlock(port->lock);
}
static int
port_check_connect_backs(port_info_t *port)
{
net_info_t *netcon;
bool tried = false;
if (!port->connbacks)
return 0;
if (port->net_to_dev_state == PORT_CLOSING) {
/*
* Some data came in while we were shutting down the port.
* Just ignore it for now, when the port is opened back up we
* wills tart the connections.
*/
return 1;
}
for_each_connection(port, netcon) {
if (netcon->connect_back && !netcon->net) {
int err;
tried = true;
err = gensio_acc_str_to_gensio(port->accepter, netcon->remote_str,
handle_net_event, netcon,
&netcon->net);
if (err) {
seout.out(&seout, "Unable to allocate connect back port %s,"
" addr %s: %s\n", port->name, netcon->remote_str,
gensio_err_to_str(err));
continue;
}
err = gensio_open(netcon->net, connect_back_done, netcon);
if (err) {
gensio_free(netcon->net);
netcon->net = NULL;
seout.out(&seout, "Unable to open connect back port %s,"
" addr %s: %s\n", port->name, netcon->remote_str,
gensio_err_to_str(err));
continue;
}
port->num_waiting_connect_backs++;
}
}
if (tried && !port->num_waiting_connect_backs && !num_connected_net(port)) {
/*
* This is kind of a bad situation. We got some data, attempted
* connects, but failed. Shut down the read enable for a while.
*/
port->nocon_read_enable_time_left = port->accepter_retry_time;
}
return port->num_waiting_connect_backs;
}
/* Data is ready to read on the serial port. */
static int
handle_dev_read(port_info_t *port, int err, unsigned char *buf,
gensiods buflen)
{
gensiods count = 0;
bool send_now = false;
int nr_handlers = 0;
so->lock(port->lock);
if (port->dev_to_net_state != PORT_WAITING_INPUT) {
gensio_set_read_callback_enable(port->io, false);
goto out_unlock;
}
if (err) {
if (port->dev_to_net.cursize) {
/* Let the output drain before shutdown. */
count = 0;
send_now = true;
goto do_send;
}
/* Got an error on the read, shut down the port. */
seout.out(&seout, "dev read error for device on port %s: %s",
port->name, gensio_err_to_str(err));
shutdown_port(port, "dev read error");
}
nr_handlers = port_check_connect_backs(port);
if (nr_handlers > 0) {
gensio_set_read_callback_enable(port->io, false);
goto out_unlock;
}
if (port->no_dev_to_net) {
count = buflen;
goto out_unlock;
}
if (gbuf_room_left(&port->dev_to_net) < buflen)
buflen = gbuf_room_left(&port->dev_to_net);
count = buflen;
if (count == 0) {
gensio_set_read_callback_enable(port->io, false);
goto out_unlock;
}
if (port->closeon) {
int i;
for (i = 0; i < count; i++) {
if (buf[i] == port->closeon[port->closeon_pos]) {
port->closeon_pos++;
if (port->closeon_pos >= port->closeon_len) {
net_info_t *netcon;
for_each_connection(port, netcon)
netcon->close_on_output_done = true;
/* Ignore everything after the closeon string */
count = i + 1;
break;
}
} else {
port->closeon_pos = 0;
}
}
}
if (port->tr)
/* Do read tracing, ignore errors. */
do_trace(port, port->tr, buf, count, SERIAL);
if (port->tb)
/* Do both tracing, ignore errors. */
do_trace(port, port->tb, buf, count, SERIAL);
if (port->led_rx)
led_flash(port->led_rx);
if (port->dev_monitor != NULL)
controller_write(port->dev_monitor, (char *) buf, count);
do_send:
if (nr_handlers < 0) /* Nobody to handle the data. */
goto out_unlock;
if (port->sendon_len != 0) {
int i;
for (i = 0; i < count; i++) {
if (buf[i] == port->sendon[port->sendon_pos]) {
port->sendon_pos++;
if (port->sendon_pos >= port->sendon_len) {
count = i + 1;
send_now = true;
port->sendon_pos = 0;
break;
}
} else {
port->sendon_pos = 0;
}
}
}
gbuf_append(&port->dev_to_net, buf, count);
port->dev_bytes_received += count;
if (send_now || gbuf_room_left(&port->dev_to_net) == 0 ||
port->chardelay == 0) {
send_it:
start_net_send(port);
} else {
gensio_time then;
int delay;
so->get_monotonic_time(so, &then);
if (port->send_timer_running) {
so->stop_timer(port->send_timer);
} else {
port->send_time = then;
add_usec_to_time(&port->send_time, port->chardelay_max);
}
delay = sub_time(&port->send_time, &then);
if (delay > port->chardelay)
delay = port->chardelay;
else if (delay < 0) {
port->send_timer_running = false;
goto send_it;
}
add_usec_to_time(&then, delay);
so->start_timer_abs(port->send_timer, &then);
port->send_timer_running = true;
}
out_unlock:
so->unlock(port->lock);
return count;
}
static void
handle_dev_write_ready(port_info_t *port)
{
so->lock(port->lock);
port->dev_write_handler(port);
so->unlock(port->lock);
}
static void handle_ser_modemstate(port_info_t *port, net_info_t *netcon);
static void handle_ser_linestate(port_info_t *port, net_info_t *netcon);
int
handle_dev_event(struct gensio *io, void *user_data, int event, int err,
unsigned char *buf, gensiods *buflen,
const char *const *auxdata)
{
port_info_t *port = user_data;
net_info_t *netcon;
gensiods len = 0;
if (buflen)
len = *buflen;
switch (event) {
case GENSIO_EVENT_READ:
if (gensio_str_in_auxdata(auxdata, "oob"))
/* Ignore out of bound data. */
return 0;
len = handle_dev_read(port, err, buf, len);
if (buflen)
*buflen = len;
return 0;
case GENSIO_EVENT_WRITE_READY:
handle_dev_write_ready(port);
return 0;
case GENSIO_EVENT_SER_MODEMSTATE:
so->lock(port->lock);
port->last_modemstate = *((unsigned int *) buf);
for_each_connection(port, netcon)
handle_ser_modemstate(port, netcon);
so->unlock(port->lock);
return 0;
case GENSIO_EVENT_SER_LINESTATE:
so->lock(port->lock);
port->last_linestate = *((unsigned int *) buf);
for_each_connection(port, netcon)
handle_ser_linestate(port, netcon);
so->unlock(port->lock);
return 0;
#ifdef GENSIO_EVENT_PARMLOG
case GENSIO_EVENT_PARMLOG: {
struct gensio_parmlog_data *d = (struct gensio_parmlog_data *) buf;
seout.vout(&seout, d->log, d->args);
return 0;
}
#endif
default:
return GE_NOTSUP;
}
}
void
port_send_timeout(struct gensio_timer *timer, void *data)
{
port_info_t *port = (port_info_t *) data;
so->lock(port->lock);
port->send_timer_running = false;
if (port->dev_to_net_state == PORT_CLOSING ||
port->dev_to_net_state == PORT_CLOSED) {
so->unlock(port->lock);
return;
}
if (port->dev_to_net.cursize)
start_net_send(port);
so->unlock(port->lock);
}
int
gbuf_write(port_info_t *port, struct gbuf *buf)
{
int err;
gensiods written;
err = gensio_write(port->io, &written, buf->buf + buf->pos,
buf->cursize - buf->pos, NULL);
if (err)
return err;
buf->pos += written;
port->dev_bytes_sent += written;
if (buf->pos >= buf->cursize)
gbuf_reset(buf);
return 0;
}
/* The serial port has room to write some data. This is only activated
if a write fails to complete, it is deactivated as soon as writing
is available again. */
static void
dev_fd_write(port_info_t *port, struct gbuf *buf)
{
int err;
err = gbuf_write(port, buf);
if (err) {
seout.out(&seout, "The dev write for port %s had error: %s",
port->name, gensio_err_to_str(err));
shutdown_port(port, "dev write error");
return;
}
if (gbuf_cursize(buf) == 0) {
/* We are done writing, turn the reader back on. */
enable_all_net_read(port);
gensio_set_write_callback_enable(port->io, false);
port->net_to_dev_state = PORT_WAITING_INPUT;
}
}
static void
handle_dev_fd_normal_write(port_info_t *port)
{
dev_fd_write(port, &port->net_to_dev);
}
/* Output the devstr buffer */
static void
handle_dev_fd_devstr_write(port_info_t *port)
{
dev_fd_write(port, port->devstr);
if (gbuf_cursize(port->devstr) == 0) {
port->dev_write_handler = handle_dev_fd_normal_write;
gbuf_free(port->devstr);
port->devstr = NULL;
/* Send out any data we got on the TCP port. */
handle_dev_fd_normal_write(port);
}
}
/* Data is ready to read on the network port. */
static gensiods
handle_net_fd_read(net_info_t *netcon, int readerr,
unsigned char *buf, gensiods buflen)
{
port_info_t *port = netcon->port;
gensiods rv = 0;
char *reason;
int err;
so->lock(port->lock);
if (port->net_to_dev_state == PORT_WAITING_OUTPUT_CLEAR)
/* Catch a race here. */
goto out_unlock;
if (readerr) {
if (readerr == GE_REMCLOSE) {
reason = "network read close";
} else {
/* Got an error on the read, shut down the port. */
seout.out(&seout, "read error for port %s: %s", port->name,
gensio_err_to_str(readerr));
reason = "network read error";
}
goto out_shutdown;
}
if (port->no_net_to_dev) {
rv = buflen;
goto out_unlock;
}
if (buflen > port->net_to_dev.maxsize)
buflen = port->net_to_dev.maxsize;
netcon->bytes_received += buflen;
if (port->net_monitor != NULL)
controller_write(port->net_monitor, (char *) buf, buflen);
if (port->tw)
/* Do write tracing, ignore errors. */
do_trace(port, port->tw, buf, buflen, NET);
if (port->tb)
/* Do both tracing, ignore errors. */
do_trace(port, port->tb, buf, buflen, NET);
memcpy(port->net_to_dev.buf, buf, buflen);
port->net_to_dev.cursize = buflen;
port->net_to_dev.pos = 0;
/*
* Don't write anything to the device until devstr is written.
* This can happen on UDP ports, we get the first packet before
* the port is enabled, so there will be data in the output buffer
* but there will also possibly be devstr data. We want the
* devstr data to go out first.
*/
if (port->devstr)
goto stop_read_start_write;
err = gbuf_write(port, &port->net_to_dev);
if (err) {
seout.out(&seout, "The dev write(2) for port %s had error: %s",
port->name, gensio_err_to_str(err));
shutdown_port(port, "dev write error");
rv = buflen;
goto out_unlock;
} else {
if (port->led_tx)
led_flash(port->led_tx);
}
if (gbuf_cursize(&port->net_to_dev)) {
/* We didn't write all the data, shut off the reader and
start the write monitor. */
stop_read_start_write:
disable_all_net_read(port);
gensio_set_write_callback_enable(port->io, true);
port->net_to_dev_state = PORT_WAITING_OUTPUT_CLEAR;
}
reset_timer(netcon);
rv = buflen;
out_unlock:
so->unlock(port->lock);
return rv;
out_shutdown:
shutdown_one_netcon(netcon, reason);
goto out_unlock;
}
/*
* Write some network data from a buffer. Returns -1 on something
* causing the netcon to shut down, 0 if the write was incomplete, and
* 1 if the write was completed.
*/
static int
net_fd_write(port_info_t *port, net_info_t *netcon,
struct gbuf *buf, gensiods *pos)
{
int reterr, to_send;
gensiods count = 0;
to_send = buf->cursize - *pos;
if (to_send <= 0)
/* Don't send empty packets, that can confuse UDP clients. */
return 1;
/* Can't use buffer send operation here, multiple writers can send
from the buffers. */
reterr = gensio_write(netcon->net, &count, buf->buf + *pos, to_send, NULL);
if (reterr == GE_REMCLOSE) {
shutdown_one_netcon(netcon, "Remote closed");
return -1;
} else if (reterr) {
/* Some other bad error. */
seout.out(&seout, "The network write for port %s had error: %s",
port->name, gensio_err_to_str(reterr));
shutdown_one_netcon(netcon, "network write error");
return -1;
}
*pos += count;
netcon->bytes_sent += count;
if (*pos < buf->cursize)
return 0;
return 1;
}
static void
finish_dev_to_net_write(port_info_t *port)
{
if (any_net_data_to_write(port))
return;
port->dev_to_net.cursize = 0;
port->dev_to_net.pos = 0;
if (port->net_to_dev_state != PORT_CLOSING) {
/* We are done writing on this port, turn the reader back on. */
gensio_set_read_callback_enable(port->io, true);
port->dev_to_net_state = PORT_WAITING_INPUT;
}
}
/* The network fd has room to write some data. This is only activated
if a write fails to complete, it is deactivated as soon as writing
is available again. */
static void
handle_net_fd_write_ready(net_info_t *netcon)
{
port_info_t *port = netcon->port;
int rv = 1;
so->lock(port->lock);
if (netcon->banner) {
rv = net_fd_write(port, netcon, netcon->banner, &netcon->banner->pos);
if (rv <= 0)
goto out_unlock;
gbuf_free(netcon->banner);
netcon->banner = NULL;
}
if (port->dev_to_net_state == PORT_WAITING_OUTPUT_CLEAR) {
rv = net_fd_write(port, netcon,
&port->dev_to_net, &netcon->write_pos);
if (rv == 0)
goto out_unlock;
if (netcon->close_on_output_done) {
shutdown_one_netcon(netcon, "port closing");
rv = -1;
}
finish_dev_to_net_write(port);
}
out_unlock:
if (rv != 0)
gensio_set_write_callback_enable(netcon->net, false);
if (rv >= 0)
reset_timer(netcon);
so->unlock(port->lock);
}
enum s2n_ser_ops {
S2N_BAUD = 0,
S2N_DATASIZE,
S2N_PARITY,
S2N_STOPBITS,
S2N_FLOWCONTROL,
S2N_IFLOWCONTROL,
S2N_BREAK,
S2N_DTR,
S2N_RTS
};
#ifdef GENSIO_ACONTROL_SER_BAUD
static void
handle_ser_modemstate(port_info_t *port, net_info_t *netcon)
{
if (!netcon->net)
return;
/*
* The 0xf below is non-standard, but the spec makes no
* sense in this case. From what I can tell, the
* modemstate top 4 bits is the settings, and the bottom 4
* bits is telling you what changed. So you don't want to
* report a value unless something changed, and only if it
* was in the modemstate mask.
*/
if (port->last_modemstate & netcon->modemstate_mask & 0xf) {
char s[20];
gensiods len = snprintf(s, sizeof(s), "%d", (port->last_modemstate &
netcon->modemstate_mask));
gensio_control(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_CONTROL_SER_MODEMSTATE, s, &len);
}
}
static void
handle_ser_linestate(port_info_t *port, net_info_t *netcon)
{
if (!netcon->net)
return;
if (port->last_linestate & netcon->linestate_mask) {
char s[20];
gensiods len = snprintf(s, sizeof(s), "%d", (port->last_linestate &
netcon->linestate_mask));
gensio_control(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_CONTROL_SER_LINESTATE, s, &len);
}
}
static void
ser_control_set(struct gensio *io, int err,
const char *val, gensiods len, void *cb_data)
{
port_info_t *port = gensio_get_user_data(io);
enum s2n_ser_ops op = (intptr_t) cb_data;
net_info_t *netcon;
so->lock(port->lock);
for_each_connection(port, netcon) {
struct gensio *io = netcon->net;
if (!io)
continue;
switch (op) {
case S2N_BAUD:
port->bps = strtol(val, NULL, 0);
gensio_acontrol(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_BAUD, val, strlen(val),
NULL, NULL, NULL);
break;
case S2N_DATASIZE:
port->bpc = strtol(val, NULL, 0);
gensio_acontrol(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_DATASIZE, val, strlen(val),
NULL, NULL, NULL);
break;
case S2N_PARITY:
if (strcmp(val, "none") == 0)
port->paritybits = 0;
else
port->paritybits = 1;
gensio_acontrol(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_PARITY, val, strlen(val),
NULL, NULL, NULL);
break;
case S2N_STOPBITS:
port->stopbits = strtol(val, NULL, 0);
gensio_acontrol(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_STOPBITS, val, strlen(val),
NULL, NULL, NULL);
break;
case S2N_FLOWCONTROL:
gensio_acontrol(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_FLOWCONTROL, val, strlen(val),
NULL, NULL, NULL);
break;
case S2N_IFLOWCONTROL:
gensio_acontrol(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_IFLOWCONTROL, val, strlen(val),
NULL, NULL, NULL);
break;
case S2N_BREAK:
gensio_acontrol(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_SBREAK, val, strlen(val),
NULL, NULL, NULL);
break;
case S2N_DTR:
gensio_acontrol(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_DTR, val, strlen(val),
NULL, NULL, NULL);
break;
case S2N_RTS:
gensio_acontrol(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_RTS, val, strlen(val),
NULL, NULL, NULL);
break;
}
}
so->unlock(port->lock);
}
static void
s2n_modemstate(net_info_t *netcon, struct gensio *io, unsigned int modemstate)
{
port_info_t *port = netcon->port;
char s[20];
gensiods len;
netcon->modemstate_mask = modemstate;
#ifdef GENSIO_ACONTROL_SER_SET_MODEMSTATE_MASK
len = snprintf(s, sizeof(s), "%d", modemstate);
gensio_acontrol(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_SET_MODEMSTATE_MASK, s, len,
NULL, NULL, NULL);
len = snprintf(s, sizeof(s), "%d",
port->last_modemstate & netcon->modemstate_mask);
gensio_control(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_CONTROL_SER_SEND_MODEMSTATE, s, &len);
#else
len = snprintf(s, sizeof(s), "%d",
port->last_modemstate & netcon->modemstate_mask);
gensio_control(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_CONTROL_SER_MODEMSTATE, s, &len);
#endif
}
static void
s2n_linestate(net_info_t *netcon, struct gensio *io, unsigned int linestate)
{
port_info_t *port = netcon->port;
char s[20];
gensiods len;
netcon->linestate_mask = linestate;
#ifdef GENSIO_ACONTROL_SER_SET_LINESTATE_MASK
len = snprintf(s, sizeof(s), "%d", linestate);
gensio_acontrol(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_SET_LINESTATE_MASK, s, len,
NULL, NULL, NULL);
len = snprintf(s, sizeof(s), "%d",
port->last_linestate & netcon->linestate_mask);
gensio_control(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_CONTROL_SER_SEND_LINESTATE, s, &len);
#else
len = snprintf(s, sizeof(s), "%d",
port->last_linestate & netcon->linestate_mask);
gensio_control(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_CONTROL_SER_LINESTATE, s, &len);
#endif
}
static void
s2n_flowcontrol_state(net_info_t *netcon, bool val)
{
char s[20];
gensiods len;
len = snprintf(s, sizeof(s), "%d", val);
gensio_control(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_CONTROL_SER_FLOWCONTROL_STATE, s, &len);
}
#ifndef GENSIO_SER_FLUSH_BOTH
/* This was missed in versions of gensio before 2.8.2. */
const char *
gensio_flush_to_str(unsigned int ival)
{
switch(ival) {
case 0:
return "0";
case 1:
return "recv";
case 2:
return "xmit";
case 3:
return "both";
default:
return "?";
}
}
#endif
static void
s2n_flush(net_info_t *netcon, int val)
{
char s[20];
gensiods len;
strncpy(s, gensio_flush_to_str(val), sizeof(s) - 1);
len = strlen(s);
if (netcon->port->io)
gensio_control(netcon->port->io, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_CONTROL_SER_FLUSH, s, &len);
if (netcon->net)
gensio_control(netcon->net, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_CONTROL_SER_FLUSH, s, &len);
}
static void
s2n_baud(net_info_t *netcon, int baud)
{
struct gensio *io = netcon->port->io;
char s[20];
gensiods len;
if (!io)
return;
len = snprintf(s, sizeof(s), "%d", baud);
gensio_acontrol(io, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_BAUD, s, len, ser_control_set,
(void *) (long) S2N_BAUD, NULL);
}
static void
s2n_datasize(net_info_t *netcon, int datasize)
{
struct gensio *io = netcon->port->io;
char s[20];
gensiods len;
if (!io)
return;
len = snprintf(s, sizeof(s), "%d", datasize);
gensio_acontrol(io, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,
GENSIO_ACONTROL_SER_DATASIZE, s, len, ser_control_set,
(void *) (long) S2N_DATASIZE, NULL);
}
static void
s2n_parity(net_info_t *netcon, int parity)
{
struct gensio *io = netcon->port->io;
char s[20];
gensiods len;
if (!io)
return;
len = snprintf(s, sizeof(s), "%s", gensio_parity_to_str(parity));
gensio_acontrol(io, GENSIO_CONTROL_DEPTH_FIRST,
GENSIO_CONTROL_SET,