]> sigrok.org Git - libsigrok.git/blame - hardware/common/serial.c
add ontinuous mode for demo driver
[libsigrok.git] / hardware / common / serial.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
54b38f64 20#include <string.h>
d02a535e
BV
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <unistd.h>
926b866c
UH
25#ifdef _WIN32
26#include <conio.h>
27#else
28#include <glob.h>
d02a535e 29#include <termios.h>
926b866c 30#endif
d02a535e 31#include <stdlib.h>
a1bb33af 32#include <glib.h>
986f7270 33#include <sigrok.h>
a1bb33af 34
926b866c
UH
35// FIXME: Must be moved, or rather passed as function argument.
36#ifdef _WIN32
37HANDLE hdl;
38#endif
39
a1bb33af
UH
40char *serial_port_glob[] = {
41 /* Linux */
42 "/dev/ttyS*",
43 "/dev/ttyUSB*",
44 "/dev/ttyACM*",
45 /* MacOS X */
46 "/dev/ttys*",
47 "/dev/tty.USB-*",
48 "/dev/tty.Modem-*",
986f7270 49 NULL,
a1bb33af
UH
50};
51
a1bb33af
UH
52GSList *list_serial_ports(void)
53{
926b866c
UH
54#ifdef _WIN32
55 /* TODO */
56#else
a1bb33af
UH
57 glob_t g;
58 GSList *ports;
afc8e4de 59 unsigned int i, j;
a1bb33af
UH
60
61 ports = NULL;
986f7270 62 for (i = 0; serial_port_glob[i]; i++) {
71dda106
PS
63 if (glob(serial_port_glob[i], 0, NULL, &g))
64 continue;
65 for (j = 0; j < g.gl_pathc; j++)
66 ports = g_slist_append(ports, strdup(g.gl_pathv[j]));
67 globfree(&g);
a1bb33af
UH
68 }
69
70 return ports;
926b866c 71#endif
a1bb33af
UH
72}
73
d02a535e
BV
74int serial_open(const char *pathname, int flags)
75{
926b866c
UH
76#ifdef _WIN32
77 /* FIXME: Don't hardcode COM1. */
78 hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
79 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
80 if (hdl == INVALID_HANDLE_VALUE) {
81 /* TODO: Error handling. */
82 }
83 return 0;
84#else
d02a535e 85 return open(pathname, flags);
926b866c 86#endif
d02a535e
BV
87}
88
d02a535e
BV
89int serial_close(int fd)
90{
926b866c
UH
91#ifdef _WIN32
92 CloseHandle(hdl);
93#else
d02a535e 94 return close(fd);
926b866c 95#endif
d02a535e
BV
96}
97
1fdb75e1
UH
98/*
99 * Flush serial port buffers (if any).
100 * Returns 0 upon success, -1 upon failure.
101 */
06d64eb8
BV
102int serial_flush(int fd)
103{
1fdb75e1
UH
104#ifdef _WIN32
105 /* Returns non-zero upon success, 0 upon failure. */
106 if (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0)
107 return -1;
108 else
109 return 0;
110#else
111 /* Returns 0 upon success, -1 upon failure. */
f0551a65 112 return tcflush(fd, TCIOFLUSH);
1fdb75e1 113#endif
06d64eb8
BV
114}
115
d02a535e
BV
116void *serial_backup_params(int fd)
117{
926b866c
UH
118#ifdef _WIN32
119 /* TODO */
120#else
d02a535e
BV
121 struct termios *term;
122
123 term = malloc(sizeof(struct termios));
124 tcgetattr(fd, term);
125
126 return term;
926b866c 127#endif
d02a535e
BV
128}
129
d02a535e
BV
130void serial_restore_params(int fd, void *backup)
131{
926b866c
UH
132#ifdef _WIN32
133 /* TODO */
134#else
986f7270 135 tcsetattr(fd, TCSADRAIN, (struct termios *)backup);
926b866c 136#endif
d02a535e
BV
137}
138
1ff7712c
DR
139/*
140 * flowcontrol 1 = rts/cts 2 = xon/xoff
141 * parity 0 = none, 1 = even, 2 = odd
142 */
986f7270
UH
143int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
144 int flowcontrol)
d02a535e 145{
926b866c
UH
146#ifdef _WIN32
147 DCB dcb;
148
149 if (!GetCommState(hdl, &dcb)) {
150 /* TODO: Error handling. */
151 }
152
153 /* TODO: Rename 'speed' to 'baudrate'. */
154 switch(speed) {
1fdb75e1 155 /* TODO: Support for higher baud rates. */
926b866c
UH
156 case 115200:
157 dcb.BaudRate = CBR_115200;
158 break;
159 case 57600:
160 dcb.BaudRate = CBR_57600;
161 break;
162 case 38400:
163 dcb.BaudRate = CBR_38400;
164 break;
165 case 19200:
166 dcb.BaudRate = CBR_19200;
167 break;
168 case 9600:
169 dcb.BaudRate = CBR_9600;
170 break;
171 default:
172 /* TODO: Error handling. */
173 break;
174 }
175 dcb.ByteSize = bits;
176 dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
177 dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
178
179 if (!SetCommState(hdl, &dcb)) {
180 /* TODO: Error handling. */
181 }
182#else
d02a535e 183 struct termios term;
1ff7712c 184 speed_t baud;
d02a535e 185
1ff7712c
DR
186 switch (speed) {
187 case 9600:
188 baud = B9600;
189 break;
190 case 38400:
191 baud = B38400;
192 break;
193 case 57600:
194 baud = B57600;
195 break;
196 case 115200:
197 baud = B115200;
198 break;
199 case 460800:
200 baud = B460800;
201 break;
202 default:
d02a535e 203 return SIGROK_ERR;
1ff7712c 204 }
d02a535e 205
986f7270 206 if (tcgetattr(fd, &term) < 0)
d02a535e 207 return SIGROK_ERR;
1ff7712c 208 if (cfsetispeed(&term, baud) < 0)
d02a535e 209 return SIGROK_ERR;
1ff7712c 210
d02a535e 211 term.c_cflag &= ~CSIZE;
1ff7712c
DR
212 switch (bits) {
213 case 8:
214 term.c_cflag |= CS8;
215 break;
216 case 7:
217 term.c_cflag |= CS7;
218 break;
219 default:
220 return SIGROK_ERR;
221 }
222
d02a535e 223 term.c_cflag &= ~CSTOPB;
1ff7712c
DR
224 switch (stopbits) {
225 case 1:
226 break;
227 case 2:
228 term.c_cflag |= CSTOPB;
229 default:
230 return SIGROK_ERR;
231 }
232
233 term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
234 switch (flowcontrol) {
235 case 2:
236 term.c_cflag |= IXON | IXOFF;
237 break;
238 case 1:
239 term.c_cflag |= CRTSCTS;
240 default:
241 return SIGROK_ERR;
242 }
243
ac4a2ea4
DR
244 term.c_iflag &= ~IGNPAR;
245 term.c_cflag &= ~(PARODD | PARENB);
1ff7712c
DR
246 switch (parity) {
247 case 0:
248 term.c_iflag |= IGNPAR;
249 break;
250 case 1:
ac4a2ea4 251 term.c_cflag |= PARENB;
1ff7712c
DR
252 break;
253 case 2:
ac4a2ea4 254 term.c_cflag |= PARENB | PARODD;
1ff7712c
DR
255 break;
256 default:
257 return SIGROK_ERR;
258 }
259
986f7270 260 if (tcsetattr(fd, TCSADRAIN, &term) < 0)
d02a535e 261 return SIGROK_ERR;
926b866c 262#endif
d02a535e
BV
263
264 return SIGROK_OK;
265}