/
serial.c
216 lines (177 loc) · 5.42 KB
/
serial.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
/*
* Copyright (c) 2011 Tomasz Moń <desowin@gmail.com>
*
* 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; under version 3 of the License.
*
* 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, see <http://www.gnu.org/licenses>.
*/
#include <gio/gio.h>
#include <fcntl.h>
/* required for CRTSCTS */
#define __USE_MISC
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include "serial.h"
#include "conf.h"
#if 0
/* FIXME: do runtime check if currently running kernel supports CDTRDSR */
#define CDTRDSR 004000000000 /* DTR/DSR flow control */
#endif
static tcflag_t get_cflag(Configuration *cfg)
{
tcflag_t cflag;
switch (cfg->rate)
{
case GUART_B150: cflag = B150; break;
case GUART_B300: cflag = B300; break;
case GUART_B600: cflag = B600; break;
case GUART_B1200: cflag = B1200; break;
case GUART_B1800: cflag = B1800; break;
case GUART_B2400: cflag = B2400; break;
case GUART_B4800: cflag = B4800; break;
case GUART_B9600: cflag = B9600; break;
case GUART_B19200: cflag = B19200; break;
case GUART_B38400: cflag = B38400; break;
case GUART_B57600: cflag = B57600; break;
default:
g_message("Invalid BaudRate, assuming 115200");
case GUART_B115200: cflag = B115200; break;
}
switch (cfg->databits)
{
case GUART_BITS5: cflag |= CS5; break;
case GUART_BITS6: cflag |= CS6; break;
case GUART_BITS7: cflag |= CS7; break;
default:
g_message("Invalid DataBits, assuming 8");
case GUART_BITS8: cflag |= CS8; break;
}
#ifdef CRTSCTS
if (cfg->flow == GUART_FLOW_RTSCTS)
{
cflag |= CRTSCTS;
}
#endif
#ifdef CDTRDSR
/* http://lkml.org/lkml/2008/8/6/478 */
if (cfg->flow == GUART_FLOW_DTRDSR)
{
cflag |= CDTRDSR;
}
#endif
switch (cfg->stopbits)
{
default:
g_message("Invalied StopBits, assuming 1");
case GUART_STOPBITS1: break; /* it's default */
case GUART_STOPBITS2: cflag |= CSTOPB;
}
switch (cfg->parity)
{
default:
g_message("Invalid Parity, assuming None");
case GUART_PARITY_NONE: break;
case GUART_PARITY_EVEN: cflag |= PARENB; break;
case GUART_PARITY_ODD: cflag |= PARENB | PARODD; break;
}
cflag |= CREAD; /* Enable receiver */
/* TODO: make this configureable */
cflag |= CLOCAL; /* Ignore modem control lines */
return cflag;
}
GIOChannel *serial_connect(Configuration *cfg, int *serial_fd)
{
GIOChannel *io;
int fd;
struct termios config;
/* O_RDWR - Opens the port for reading and writing
* O_NOCTTY - The port never becomes the controlling terminal of the process.
* O_NDELAY - Use non-blocking I/O. On some systems this also means the RS232 DCD signal line is ignored.
*/
fd = open(cfg->port, O_RDWR | O_NOCTTY);// | O_NDELAY);
tcgetattr(fd, &config);
if (fd < -1)
{
g_message("Unable to connect to %s: %s(%d)!", cfg->port, strerror(errno), errno);
return NULL;
}
config.c_cflag = get_cflag(cfg);
config.c_iflag = IGNPAR | IGNBRK;
if (cfg->flow == GUART_FLOW_XONXOFF)
{
config.c_iflag |= IXON | IXOFF;
}
config.c_oflag = 0;
config.c_lflag = 0;
config.c_cc[VTIME] = 0;
config.c_cc[VMIN] = 1;
if (tcsetattr(fd, TCSANOW, &config) < 0) {
g_message("Can't change serial settings: %s(%d)", strerror(errno), errno);
close(fd);
return NULL;
}
tcflush(fd, TCOFLUSH);
tcflush(fd, TCIFLUSH);
io = g_io_channel_unix_new(fd);
g_io_channel_set_close_on_unref(io, TRUE);
if (g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL) != G_IO_STATUS_NORMAL) {
g_io_channel_unref(io);
return NULL;
}
*serial_fd = fd;
return io;
}
gboolean get_control_lines(int fd, gchar *dtr, gchar *dsr, gchar *rts, gchar *cts)
{
int status;
if (ioctl(fd, TIOCMGET, &status) == -1) {
g_message("TIOCMGET failed");
return FALSE;
}
if (status & TIOCM_DTR) *dtr = 1; else *dtr = 0;
if (status & TIOCM_DSR) *dsr = 1; else *dsr = 0;
if (status & TIOCM_RTS) *rts = 1; else *rts = 0;
if (status & TIOCM_CTS) *cts = 1; else *cts = 0;
return TRUE;
}
void set_rts(int fd, gchar state)
{
int status;
if (ioctl(fd, TIOCMGET, &status) == -1) {
g_message("setRTS(): TIOCMGET failed");
return;
}
if (state)
status |= TIOCM_RTS;
else
status &= ~TIOCM_RTS;
if (ioctl(fd, TIOCMSET, &status) == -1) {
g_message("setRTS(): TIOCMSET failed");
}
}
void set_dtr(int fd, gchar state)
{
int status;
if (ioctl(fd, TIOCMGET, &status) == -1) {
g_message("setRTS(): TIOCMGET failed");
return;
}
if (state)
status |= TIOCM_DTR;
else
status &= ~TIOCM_DTR;
if (ioctl(fd, TIOCMSET, &status) == -1) {
g_message("setRTS(): TIOCMSET failed");
}
}