]> sigrok.org Git - libsigrok.git/blame - hardware/common/serial.c
disable MSO-19 specific stuff for 0.1 release
[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>
1483577e 34#include <sigrok-internal.h>
a1bb33af 35
926b866c
UH
36// FIXME: Must be moved, or rather passed as function argument.
37#ifdef _WIN32
38HANDLE hdl;
39#endif
40
1483577e 41const char *serial_port_glob[] = {
a1bb33af
UH
42 /* Linux */
43 "/dev/ttyS*",
44 "/dev/ttyUSB*",
45 "/dev/ttyACM*",
46 /* MacOS X */
47 "/dev/ttys*",
48 "/dev/tty.USB-*",
49 "/dev/tty.Modem-*",
986f7270 50 NULL,
a1bb33af
UH
51};
52
a1bb33af
UH
53GSList *list_serial_ports(void)
54{
2119ab03
UH
55 GSList *ports;
56
926b866c
UH
57#ifdef _WIN32
58 /* TODO */
2119ab03
UH
59 ports = NULL;
60 ports = g_slist_append(ports, strdup("COM1"));
926b866c 61#else
a1bb33af 62 glob_t g;
afc8e4de 63 unsigned int i, j;
a1bb33af
UH
64
65 ports = NULL;
986f7270 66 for (i = 0; serial_port_glob[i]; i++) {
71dda106
PS
67 if (glob(serial_port_glob[i], 0, NULL, &g))
68 continue;
69 for (j = 0; j < g.gl_pathc; j++)
70 ports = g_slist_append(ports, strdup(g.gl_pathv[j]));
71 globfree(&g);
a1bb33af 72 }
2119ab03 73#endif
a1bb33af
UH
74
75 return ports;
76}
77
d02a535e
BV
78int serial_open(const char *pathname, int flags)
79{
926b866c
UH
80#ifdef _WIN32
81 /* FIXME: Don't hardcode COM1. */
82 hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
83 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
84 if (hdl == INVALID_HANDLE_VALUE) {
85 /* TODO: Error handling. */
86 }
87 return 0;
88#else
d02a535e 89 return open(pathname, flags);
926b866c 90#endif
d02a535e
BV
91}
92
2119ab03
UH
93/*
94 * Close the serial port.
95 * Returns 0 upon success, -1 upon failure.
96 */
d02a535e
BV
97int serial_close(int fd)
98{
926b866c 99#ifdef _WIN32
2119ab03
UH
100 /* Returns non-zero upon success, 0 upon failure. */
101 return (CloseHandle(hdl) == 0) ? -1 : 0;
926b866c 102#else
2119ab03 103 /* Returns 0 upon success, -1 upon failure. */
d02a535e 104 return close(fd);
926b866c 105#endif
d02a535e
BV
106}
107
1fdb75e1
UH
108/*
109 * Flush serial port buffers (if any).
110 * Returns 0 upon success, -1 upon failure.
111 */
06d64eb8
BV
112int serial_flush(int fd)
113{
1fdb75e1
UH
114#ifdef _WIN32
115 /* Returns non-zero upon success, 0 upon failure. */
2119ab03 116 return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
1fdb75e1
UH
117#else
118 /* Returns 0 upon success, -1 upon failure. */
f0551a65 119 return tcflush(fd, TCIOFLUSH);
1fdb75e1 120#endif
06d64eb8
BV
121}
122
2119ab03
UH
123/*
124 * Write a number of bytes to the specified serial port.
125 * Returns the number of bytes written, or -1 upon failure.
126 */
127int serial_write(int fd, const void *buf, size_t count)
128{
129#ifdef _WIN32
130 DWORD tmp = 0;
131
132 /* FIXME */
133 /* Returns non-zero upon success, 0 upon failure. */
134 WriteFile(hdl, buf, count, &tmp, NULL);
135#else
136 /* Returns the number of bytes written, or -1 upon failure. */
137 return write(fd, buf, count);
138#endif
139}
140
141/*
142 * Read a number of bytes from the specified serial port.
143 * Returns the number of bytes read, or -1 upon failure.
144 */
145int serial_read(int fd, void *buf, size_t count)
146{
147#ifdef _WIN32
148 DWORD tmp = 0;
149
150 /* FIXME */
151 /* Returns non-zero upon success, 0 upon failure. */
152 return ReadFile(hdl, buf, count, &tmp, NULL);
153#else
154 /* Returns the number of bytes read, or -1 upon failure. */
155 return read(fd, buf, count);
156#endif
157}
158
d02a535e
BV
159void *serial_backup_params(int fd)
160{
926b866c
UH
161#ifdef _WIN32
162 /* TODO */
163#else
d02a535e
BV
164 struct termios *term;
165
166 term = malloc(sizeof(struct termios));
167 tcgetattr(fd, term);
168
169 return term;
926b866c 170#endif
d02a535e
BV
171}
172
d02a535e
BV
173void serial_restore_params(int fd, void *backup)
174{
926b866c
UH
175#ifdef _WIN32
176 /* TODO */
177#else
986f7270 178 tcsetattr(fd, TCSADRAIN, (struct termios *)backup);
926b866c 179#endif
d02a535e
BV
180}
181
1ff7712c 182/*
2119ab03
UH
183 * Set serial parameters.
184 *
185 * flowcontrol: 1 = rts/cts, 2 = xon/xoff
186 * parity: 0 = none, 1 = even, 2 = odd
1ff7712c 187 */
986f7270
UH
188int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
189 int flowcontrol)
d02a535e 190{
926b866c
UH
191#ifdef _WIN32
192 DCB dcb;
193
194 if (!GetCommState(hdl, &dcb)) {
195 /* TODO: Error handling. */
2119ab03 196 return SIGROK_ERR;
926b866c
UH
197 }
198
199 /* TODO: Rename 'speed' to 'baudrate'. */
200 switch(speed) {
1fdb75e1 201 /* TODO: Support for higher baud rates. */
926b866c
UH
202 case 115200:
203 dcb.BaudRate = CBR_115200;
204 break;
205 case 57600:
206 dcb.BaudRate = CBR_57600;
207 break;
208 case 38400:
209 dcb.BaudRate = CBR_38400;
210 break;
211 case 19200:
212 dcb.BaudRate = CBR_19200;
213 break;
214 case 9600:
215 dcb.BaudRate = CBR_9600;
216 break;
217 default:
218 /* TODO: Error handling. */
219 break;
220 }
221 dcb.ByteSize = bits;
222 dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
223 dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
224
225 if (!SetCommState(hdl, &dcb)) {
226 /* TODO: Error handling. */
2119ab03 227 return SIGROK_ERR;
926b866c
UH
228 }
229#else
d02a535e 230 struct termios term;
1ff7712c 231 speed_t baud;
d02a535e 232
1ff7712c
DR
233 switch (speed) {
234 case 9600:
235 baud = B9600;
236 break;
237 case 38400:
238 baud = B38400;
239 break;
240 case 57600:
241 baud = B57600;
242 break;
243 case 115200:
244 baud = B115200;
245 break;
576790ff 246/* removed for 0.1 release
1ff7712c
DR
247 case 460800:
248 baud = B460800;
249 break;
576790ff 250*/
1ff7712c 251 default:
d02a535e 252 return SIGROK_ERR;
1ff7712c 253 }
d02a535e 254
986f7270 255 if (tcgetattr(fd, &term) < 0)
d02a535e 256 return SIGROK_ERR;
1ff7712c 257 if (cfsetispeed(&term, baud) < 0)
d02a535e 258 return SIGROK_ERR;
1ff7712c 259
d02a535e 260 term.c_cflag &= ~CSIZE;
1ff7712c
DR
261 switch (bits) {
262 case 8:
263 term.c_cflag |= CS8;
264 break;
265 case 7:
266 term.c_cflag |= CS7;
267 break;
268 default:
269 return SIGROK_ERR;
270 }
271
d02a535e 272 term.c_cflag &= ~CSTOPB;
1ff7712c
DR
273 switch (stopbits) {
274 case 1:
275 break;
276 case 2:
277 term.c_cflag |= CSTOPB;
278 default:
279 return SIGROK_ERR;
280 }
281
282 term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
283 switch (flowcontrol) {
284 case 2:
285 term.c_cflag |= IXON | IXOFF;
286 break;
287 case 1:
288 term.c_cflag |= CRTSCTS;
289 default:
290 return SIGROK_ERR;
291 }
292
ac4a2ea4
DR
293 term.c_iflag &= ~IGNPAR;
294 term.c_cflag &= ~(PARODD | PARENB);
1ff7712c
DR
295 switch (parity) {
296 case 0:
297 term.c_iflag |= IGNPAR;
298 break;
299 case 1:
ac4a2ea4 300 term.c_cflag |= PARENB;
1ff7712c
DR
301 break;
302 case 2:
ac4a2ea4 303 term.c_cflag |= PARENB | PARODD;
1ff7712c
DR
304 break;
305 default:
306 return SIGROK_ERR;
307 }
308
986f7270 309 if (tcsetattr(fd, TCSADRAIN, &term) < 0)
d02a535e 310 return SIGROK_ERR;
926b866c 311#endif
d02a535e
BV
312
313 return SIGROK_OK;
314}