-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialsim.c
176 lines (154 loc) · 4.18 KB
/
serialsim.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
/*
* gensio - A library for abstracting stream I/O
* Copyright (C) 2018 Corey Minyard <minyard@acm.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/ioctl.h>
#include <asm/termbits.h>
#include <string.h>
#include <stdbool.h>
#include <malloc.h>
#include <errno.h>
#include <linux/serial.h>
#include <linux/serialsim.h>
/*
* Stolen from glibc, but we can't directly include it from there
* because the "termios" struct there conflicts with the one from the
* kernel.
*/
#define UNCCS 32
struct user_termios {
tcflag_t c_iflag; /* input mode flags */
tcflag_t c_oflag; /* output mode flags */
tcflag_t c_cflag; /* control mode flags */
tcflag_t c_lflag; /* local mode flags */
cc_t c_line; /* line discipline */
cc_t c_cc[UNCCS]; /* control characters */
speed_t c_ispeed; /* input speed */
speed_t c_ospeed; /* output speed */
};
#include <linux/serialsim.h>
int remote_termios(struct termios *itermios, int fd)
{
struct user_termios *termios = (struct user_termios *) itermios;
#ifdef TCGETS2
struct termios2 ktermios;
#else
struct termios ktermios;
#endif
int rv = ioctl(fd, TIOCSERGREMTERMIOS, &ktermios);
int i;
if (rv)
return errno;
memset(termios, 0, sizeof(*termios));
termios->c_iflag = ktermios.c_iflag;
termios->c_oflag = ktermios.c_oflag;
termios->c_cflag = ktermios.c_cflag;
termios->c_lflag = ktermios.c_lflag;
termios->c_line = ktermios.c_line;
termios->c_ispeed = ktermios.c_ispeed;
termios->c_ospeed = ktermios.c_ospeed;
for (i = 0; i < NCCS; i++)
termios->c_cc[i] = ktermios.c_cc[i];
return 0;
}
int
sremote_mctl(unsigned int mctl, int fd)
{
if (ioctl(fd, TIOCSERSREMMCTRL, mctl))
return errno;
return 0;
}
int
sremote_sererr(unsigned int err, int fd)
{
if (ioctl(fd, TIOCSERSREMERR, err))
return errno;
return 0;
}
int
sremote_null_modem(bool val, int fd)
{
if (ioctl(fd, TIOCSERSREMNULLMODEM, (int) val))
return errno;
return 0;
}
int
gremote_mctl(unsigned int *mctl, int fd)
{
if (ioctl(fd, TIOCSERGREMMCTRL, mctl))
return errno;
return 0;
}
int
gremote_sererr(unsigned int *err, int fd)
{
if (ioctl(fd, TIOCSERGREMERR, err))
return errno;
return 0;
}
int
gremote_null_modem(int *val, int fd)
{
if (ioctl(fd, TIOCSERGREMNULLMODEM, val))
return errno;
return 0;
}
void
strdupcat(char **str, const char *cat)
{
char *s;
if (!*str)
return;
s = malloc(strlen(*str) + strlen(cat) + 2);
if (!s) {
free(*str);
*str = NULL;
return;
}
strcpy(s, *str);
strcat(s, " ");
strcat(s, cat);
free(*str);
*str = s;
}
int
remote_rs485(int fd, char **rstr)
{
struct serial_rs485 rs485;
char *str = NULL, tmpstr[20];
if (ioctl(fd, TIOCSERGREMRS485, &rs485))
return errno;
snprintf(tmpstr, sizeof(tmpstr), "%d %d",
rs485.delay_rts_before_send, rs485.delay_rts_after_send);
str = strdup(tmpstr);
if (rs485.flags & SER_RS485_ENABLED)
strdupcat(&str, "enabled");
if (rs485.flags & SER_RS485_RTS_ON_SEND)
strdupcat(&str, "rts_on_send");
if (rs485.flags & SER_RS485_RTS_AFTER_SEND)
strdupcat(&str, "rts_after_send");
if (rs485.flags & SER_RS485_RX_DURING_TX)
strdupcat(&str, "rx_during_tx");
#ifdef SER_RS485_TERMINATE_BUS
if (rs485.flags & SER_RS485_TERMINATE_BUS)
strdupcat(&str, "terminate_bus");
#endif
if (!str)
return ENOMEM;
*rstr = str;
return 0;
}