]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
a1bb33af UH |
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 | 25 | #ifdef _WIN32 |
a9f54bcd | 26 | #include <windows.h> |
926b866c UH |
27 | #else |
28 | #include <glob.h> | |
d02a535e | 29 | #include <termios.h> |
926b866c | 30 | #endif |
d02a535e | 31 | #include <stdlib.h> |
a1bb33af | 32 | #include <glib.h> |
45c59c8b BV |
33 | #include "libsigrok.h" |
34 | #include "libsigrok-internal.h" | |
a1bb33af | 35 | |
926b866c UH |
36 | // FIXME: Must be moved, or rather passed as function argument. |
37 | #ifdef _WIN32 | |
a0ecd83b | 38 | static HANDLE hdl; |
926b866c UH |
39 | #endif |
40 | ||
1a081ca6 | 41 | static const 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 | ||
1a081ca6 | 53 | SR_PRIV GSList *list_serial_ports(void) |
a1bb33af | 54 | { |
2119ab03 UH |
55 | GSList *ports; |
56 | ||
926b866c UH |
57 | #ifdef _WIN32 |
58 | /* TODO */ | |
2119ab03 | 59 | ports = NULL; |
133a37bf | 60 | ports = g_slist_append(ports, g_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++) | |
133a37bf | 70 | ports = g_slist_append(ports, g_strdup(g.gl_pathv[j])); |
71dda106 | 71 | globfree(&g); |
a1bb33af | 72 | } |
2119ab03 | 73 | #endif |
a1bb33af UH |
74 | |
75 | return ports; | |
76 | } | |
77 | ||
1a081ca6 | 78 | SR_PRIV int serial_open(const char *pathname, int flags) |
d02a535e | 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 | */ | |
1a081ca6 | 97 | SR_PRIV int serial_close(int fd) |
d02a535e | 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 | */ | |
1a081ca6 | 112 | SR_PRIV int serial_flush(int fd) |
06d64eb8 | 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 | */ | |
1a081ca6 | 127 | SR_PRIV int serial_write(int fd, const void *buf, size_t count) |
2119ab03 UH |
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 | */ | |
1a081ca6 | 145 | SR_PRIV int serial_read(int fd, void *buf, size_t count) |
2119ab03 UH |
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 | ||
1a081ca6 | 159 | SR_PRIV void *serial_backup_params(int fd) |
d02a535e | 160 | { |
926b866c UH |
161 | #ifdef _WIN32 |
162 | /* TODO */ | |
163 | #else | |
d02a535e BV |
164 | struct termios *term; |
165 | ||
c548332c UH |
166 | /* TODO: 'term' is never g_free()'d? */ |
167 | if (!(term = g_try_malloc(sizeof(struct termios)))) { | |
168 | sr_err("serial: %s: term malloc failed", __func__); | |
886a52b6 | 169 | return -1; |
c548332c UH |
170 | } |
171 | ||
d02a535e BV |
172 | tcgetattr(fd, term); |
173 | ||
174 | return term; | |
926b866c | 175 | #endif |
d02a535e BV |
176 | } |
177 | ||
1a081ca6 | 178 | SR_PRIV void serial_restore_params(int fd, void *backup) |
d02a535e | 179 | { |
926b866c UH |
180 | #ifdef _WIN32 |
181 | /* TODO */ | |
182 | #else | |
986f7270 | 183 | tcsetattr(fd, TCSADRAIN, (struct termios *)backup); |
926b866c | 184 | #endif |
d02a535e BV |
185 | } |
186 | ||
1ff7712c | 187 | /* |
2119ab03 UH |
188 | * Set serial parameters. |
189 | * | |
190 | * flowcontrol: 1 = rts/cts, 2 = xon/xoff | |
191 | * parity: 0 = none, 1 = even, 2 = odd | |
1ff7712c | 192 | */ |
0abee507 | 193 | SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity, |
1a081ca6 | 194 | int stopbits, int flowcontrol) |
d02a535e | 195 | { |
926b866c UH |
196 | #ifdef _WIN32 |
197 | DCB dcb; | |
198 | ||
199 | if (!GetCommState(hdl, &dcb)) { | |
200 | /* TODO: Error handling. */ | |
e46b8fb1 | 201 | return SR_ERR; |
926b866c UH |
202 | } |
203 | ||
0abee507 | 204 | switch (baudrate) { |
1fdb75e1 | 205 | /* TODO: Support for higher baud rates. */ |
926b866c UH |
206 | case 115200: |
207 | dcb.BaudRate = CBR_115200; | |
208 | break; | |
209 | case 57600: | |
210 | dcb.BaudRate = CBR_57600; | |
211 | break; | |
212 | case 38400: | |
213 | dcb.BaudRate = CBR_38400; | |
214 | break; | |
215 | case 19200: | |
216 | dcb.BaudRate = CBR_19200; | |
217 | break; | |
218 | case 9600: | |
219 | dcb.BaudRate = CBR_9600; | |
220 | break; | |
e8e9dcdd AG |
221 | case 4800: |
222 | dcb.BaudRate = CBR_4800; | |
223 | break; | |
0f708301 AG |
224 | case 2400: |
225 | dcb.BaudRate = CBR_2400; | |
226 | break; | |
926b866c UH |
227 | default: |
228 | /* TODO: Error handling. */ | |
229 | break; | |
230 | } | |
231 | dcb.ByteSize = bits; | |
232 | dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */ | |
233 | dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */ | |
234 | ||
235 | if (!SetCommState(hdl, &dcb)) { | |
236 | /* TODO: Error handling. */ | |
e46b8fb1 | 237 | return SR_ERR; |
926b866c UH |
238 | } |
239 | #else | |
d02a535e | 240 | struct termios term; |
1ff7712c | 241 | speed_t baud; |
d02a535e | 242 | |
6a6e23ab BV |
243 | if (tcgetattr(fd, &term) < 0) |
244 | return SR_ERR; | |
245 | ||
0abee507 | 246 | switch (baudrate) { |
0f708301 AG |
247 | case 2400: |
248 | baud = B2400; | |
249 | break; | |
e8e9dcdd AG |
250 | case 4800: |
251 | baud = B4800; | |
252 | break; | |
1ff7712c DR |
253 | case 9600: |
254 | baud = B9600; | |
255 | break; | |
256 | case 38400: | |
257 | baud = B38400; | |
258 | break; | |
259 | case 57600: | |
260 | baud = B57600; | |
261 | break; | |
262 | case 115200: | |
263 | baud = B115200; | |
264 | break; | |
9a751023 | 265 | #ifndef __APPLE__ |
1ff7712c DR |
266 | case 460800: |
267 | baud = B460800; | |
268 | break; | |
9a751023 | 269 | #endif |
1ff7712c | 270 | default: |
e46b8fb1 | 271 | return SR_ERR; |
1ff7712c | 272 | } |
6a6e23ab | 273 | if (cfsetospeed(&term, baud) < 0) |
e46b8fb1 | 274 | return SR_ERR; |
1ff7712c | 275 | if (cfsetispeed(&term, baud) < 0) |
e46b8fb1 | 276 | return SR_ERR; |
1ff7712c | 277 | |
d02a535e | 278 | term.c_cflag &= ~CSIZE; |
1ff7712c DR |
279 | switch (bits) { |
280 | case 8: | |
281 | term.c_cflag |= CS8; | |
282 | break; | |
283 | case 7: | |
284 | term.c_cflag |= CS7; | |
285 | break; | |
286 | default: | |
e46b8fb1 | 287 | return SR_ERR; |
1ff7712c DR |
288 | } |
289 | ||
d02a535e | 290 | term.c_cflag &= ~CSTOPB; |
1ff7712c DR |
291 | switch (stopbits) { |
292 | case 1: | |
293 | break; | |
294 | case 2: | |
295 | term.c_cflag |= CSTOPB; | |
d7c776b9 | 296 | break; |
1ff7712c | 297 | default: |
e46b8fb1 | 298 | return SR_ERR; |
1ff7712c DR |
299 | } |
300 | ||
f38b9763 BV |
301 | term.c_iflag &= ~(IXON | IXOFF); |
302 | term.c_cflag &= ~CRTSCTS; | |
1ff7712c | 303 | switch (flowcontrol) { |
f38b9763 BV |
304 | case 0: |
305 | /* No flow control. */ | |
1ff7712c DR |
306 | break; |
307 | case 1: | |
308 | term.c_cflag |= CRTSCTS; | |
d7c776b9 | 309 | break; |
f38b9763 BV |
310 | case 2: |
311 | term.c_iflag |= IXON | IXOFF; | |
312 | break; | |
1ff7712c | 313 | default: |
e46b8fb1 | 314 | return SR_ERR; |
1ff7712c DR |
315 | } |
316 | ||
ac4a2ea4 DR |
317 | term.c_iflag &= ~IGNPAR; |
318 | term.c_cflag &= ~(PARODD | PARENB); | |
1ff7712c | 319 | switch (parity) { |
f8c1fcda | 320 | case SERIAL_PARITY_NONE: |
1ff7712c DR |
321 | term.c_iflag |= IGNPAR; |
322 | break; | |
f8c1fcda | 323 | case SERIAL_PARITY_EVEN: |
ac4a2ea4 | 324 | term.c_cflag |= PARENB; |
1ff7712c | 325 | break; |
f8c1fcda | 326 | case SERIAL_PARITY_ODD: |
ac4a2ea4 | 327 | term.c_cflag |= PARENB | PARODD; |
1ff7712c DR |
328 | break; |
329 | default: | |
e46b8fb1 | 330 | return SR_ERR; |
1ff7712c DR |
331 | } |
332 | ||
5c51e098 | 333 | /* Some default parameters */ |
fb9d3bf9 | 334 | term.c_iflag &= ~(ICRNL); |
5c51e098 BV |
335 | term.c_lflag &= ~(ICANON | ECHO); |
336 | ||
986f7270 | 337 | if (tcsetattr(fd, TCSADRAIN, &term) < 0) |
e46b8fb1 | 338 | return SR_ERR; |
926b866c | 339 | #endif |
d02a535e | 340 | |
e46b8fb1 | 341 | return SR_OK; |
d02a535e | 342 | } |
792fc686 BV |
343 | |
344 | #define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])$" | |
345 | SR_PRIV int serial_set_paramstr(int fd, const char *paramstr) | |
346 | { | |
347 | GRegex *reg; | |
348 | GMatchInfo *match; | |
349 | int speed, databits, parity, stopbits; | |
350 | char *mstr; | |
351 | ||
352 | speed = databits = parity = stopbits = 0; | |
353 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); | |
354 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
355 | if ((mstr = g_match_info_fetch(match, 1))) | |
356 | speed = strtoul(mstr, NULL, 10); | |
357 | g_free(mstr); | |
358 | if ((mstr = g_match_info_fetch(match, 2))) | |
359 | databits = strtoul(mstr, NULL, 10); | |
360 | g_free(mstr); | |
361 | if ((mstr = g_match_info_fetch(match, 3))) { | |
362 | switch (mstr[0]) { | |
363 | case 'n': | |
364 | parity = SERIAL_PARITY_NONE; | |
365 | break; | |
366 | case 'e': | |
367 | parity = SERIAL_PARITY_EVEN; | |
368 | break; | |
369 | case 'o': | |
370 | parity = SERIAL_PARITY_ODD; | |
371 | break; | |
372 | } | |
373 | } | |
374 | g_free(mstr); | |
375 | if ((mstr = g_match_info_fetch(match, 4))) | |
376 | stopbits = strtoul(mstr, NULL, 10); | |
377 | g_free(mstr); | |
378 | } | |
379 | g_match_info_unref(match); | |
380 | g_regex_unref(reg); | |
381 | ||
382 | if (speed) | |
383 | return serial_set_params(fd, speed, databits, parity, stopbits, 0); | |
384 | else | |
385 | return SR_ERR_ARG; | |
386 | } | |
387 | ||
6f22a8ef UH |
388 | SR_PRIV int serial_readline(int fd, char **buf, int *buflen, |
389 | uint64_t timeout_ms) | |
390 | { | |
391 | uint64_t start; | |
392 | int maxlen, len; | |
393 | ||
394 | timeout_ms *= 1000; | |
395 | start = g_get_monotonic_time(); | |
396 | ||
397 | maxlen = *buflen; | |
398 | *buflen = len = 0; | |
399 | while(1) { | |
400 | len = maxlen - *buflen - 1; | |
401 | if (len < 1) | |
402 | break; | |
403 | len = serial_read(fd, *buf + *buflen, 1); | |
404 | if (len > 0) { | |
405 | *buflen += len; | |
406 | *(*buf + *buflen) = '\0'; | |
407 | if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') { | |
408 | /* Strip LF and terminate. */ | |
409 | *(*buf + --*buflen) = '\0'; | |
410 | break; | |
411 | } | |
412 | } | |
413 | if (g_get_monotonic_time() - start > timeout_ms) | |
414 | /* Timeout */ | |
415 | break; | |
416 | g_usleep(2000); | |
417 | } | |
418 | sr_dbg("Received %d: '%s'.", *buflen, *buf); | |
419 | ||
420 | return SR_OK; | |
421 | } |