]> sigrok.org Git - libsigrok.git/blob - hardware/common/serial.c
serial: More error-checking & logging, add baudrates.
[libsigrok.git] / hardware / common / serial.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #ifdef _WIN32
27 #include <windows.h>
28 #else
29 #include <glob.h>
30 #include <termios.h>
31 #endif
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <glib.h>
35 #include "libsigrok.h"
36 #include "libsigrok-internal.h"
37
38 /* Message logging helpers with driver-specific prefix string. */
39 #define DRIVER_LOG_DOMAIN "serial: "
40 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
41 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
42 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
43 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
44 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
45 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
46
47 // FIXME: Must be moved, or rather passed as function argument.
48 #ifdef _WIN32
49 static HANDLE hdl;
50 #endif
51
52 static const char *serial_port_glob[] = {
53         /* Linux */
54         "/dev/ttyS*",
55         "/dev/ttyUSB*",
56         "/dev/ttyACM*",
57         /* MacOS X */
58         "/dev/ttys*",
59         "/dev/tty.USB-*",
60         "/dev/tty.Modem-*",
61         NULL,
62 };
63
64 SR_PRIV GSList *list_serial_ports(void)
65 {
66         GSList *ports;
67
68         sr_dbg("Getting list of serial ports on the system.");
69
70 #ifdef _WIN32
71         /* TODO */
72         ports = NULL;
73         ports = g_slist_append(ports, g_strdup("COM1"));
74 #else
75         glob_t g;
76         unsigned int i, j;
77
78         ports = NULL;
79         for (i = 0; serial_port_glob[i]; i++) {
80                 if (glob(serial_port_glob[i], 0, NULL, &g))
81                         continue;
82                 for (j = 0; j < g.gl_pathc; j++) {
83                         ports = g_slist_append(ports, g_strdup(g.gl_pathv[j]));
84                         sr_dbg("Found serial port '%s'.", g.gl_pathv[j]);
85                 }
86                 globfree(&g);
87         }
88 #endif
89
90         return ports;
91 }
92
93 /**
94  * Open the specified serial port.
95  *
96  * @param pathname OS-specific serial port specification. Examples:
97  *                 "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
98  * @param flags Flags to use when opening the serial port.
99  *
100  * @return 0 upon success, -1 upon failure.
101  */
102 SR_PRIV int serial_open(const char *pathname, int flags)
103 {
104         /* TODO: Abstract 'flags', currently they're OS-specific! */
105
106         sr_dbg("Opening serial port '%s' (flags = %d).", pathname, flags);
107
108 #ifdef _WIN32
109         pathname = "COM1"; /* FIXME: Don't hardcode COM1. */
110
111         hdl = CreateFile(pathname, GENERIC_READ | GENERIC_WRITE, 0, 0,
112                          OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
113         if (hdl == INVALID_HANDLE_VALUE) {
114                 sr_err("Error opening serial port '%s'.", pathname);
115                 return -1;
116         }
117         return 0;
118 #else
119         int fd;
120
121         if ((fd = open(pathname, flags)) < 0) {
122                 /*
123                  * Should be sr_err(), but since some drivers try to open all
124                  * ports on a system and see if they succeed, this would
125                  * yield ugly output for e.g. "sigrok-cli -D".
126                  */
127                 sr_dbg("Error opening serial port '%s': %s.", pathname,
128                        strerror(errno));
129         }
130
131         return fd;
132 #endif
133 }
134
135 /**
136  * Close the specified serial port.
137  *
138  * @param fd File descriptor of the serial port.
139  *
140  * @return 0 upon success, -1 upon failure.
141  */
142 SR_PRIV int serial_close(int fd)
143 {
144         sr_dbg("FD %d: Closing serial port.", fd);
145
146 #ifdef _WIN32
147         /* Returns non-zero upon success, 0 upon failure. */
148         return (CloseHandle(hdl) == 0) ? -1 : 0;
149 #else
150         int ret;
151
152         /* Returns 0 upon success, -1 upon failure. */
153         if ((ret = close(fd)) < 0) {
154                 sr_dbg("FD %d: Error closing serial port: %s.",
155                        fd, strerror(errno));
156         }
157
158         return ret;
159 #endif
160 }
161
162 /**
163  * Flush serial port buffers (if any).
164  *
165  * @param fd File descriptor of the serial port.
166  *
167  * @return 0 upon success, -1 upon failure.
168  */
169 SR_PRIV int serial_flush(int fd)
170 {
171         sr_dbg("FD %d: Flushing serial port.", fd);
172
173 #ifdef _WIN32
174         /* Returns non-zero upon success, 0 upon failure. */
175         return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
176 #else
177         int ret;
178
179         /* Returns 0 upon success, -1 upon failure. */
180         if ((ret = tcflush(fd, TCIOFLUSH)) < 0)
181                 sr_err("Error flushing serial port: %s.", strerror(errno));
182
183         return ret;
184 #endif
185 }
186
187 /**
188  * Write a number of bytes to the specified serial port.
189  *
190  * @param fd File descriptor of the serial port.
191  * @param buf Buffer containing the bytes to write.
192  * @param count Number of bytes to write.
193  *
194  * @return The number of bytes written, or -1 upon failure.
195  */
196 SR_PRIV int serial_write(int fd, const void *buf, size_t count)
197 {
198         sr_spew("FD %d: Writing %d bytes.", fd, count);
199
200 #ifdef _WIN32
201         DWORD tmp = 0;
202
203         /* FIXME */
204         /* Returns non-zero upon success, 0 upon failure. */
205         WriteFile(hdl, buf, count, &tmp, NULL);
206 #else
207         ssize_t ret;
208
209         /* Returns the number of bytes written, or -1 upon failure. */
210         ret = write(fd, buf, count);
211         if (ret < 0)
212                 sr_err("FD %d: Write error: %s.", fd, strerror(errno));
213         else if ((size_t)ret != count)
214                 sr_spew("FD %d: Only wrote %d/%d bytes.", fd, ret, count);
215
216         return ret;
217 #endif
218 }
219
220 /**
221  * Read a number of bytes from the specified serial port.
222  *
223  * @param fd File descriptor of the serial port.
224  * @param buf Buffer where to store the bytes that are read.
225  * @param count The number of bytes to read.
226  *
227  * @return The number of bytes read, or -1 upon failure.
228  */
229 SR_PRIV int serial_read(int fd, void *buf, size_t count)
230 {
231         sr_spew("FD %d: Reading %d bytes.", fd, count);
232
233 #ifdef _WIN32
234         DWORD tmp = 0;
235
236         /* FIXME */
237         /* Returns non-zero upon success, 0 upon failure. */
238         return ReadFile(hdl, buf, count, &tmp, NULL);
239 #else
240         ssize_t ret;
241
242         /* Returns the number of bytes read, or -1 upon failure. */
243         ret = read(fd, buf, count);
244         if (ret < 0) {
245                 /*
246                  * Should be sr_err(), but that would yield lots of
247                  * "Resource temporarily unavailable" messages.
248                  */
249                 sr_spew("FD %d: Read error: %s.", fd, strerror(errno));
250         } else if ((size_t)ret != count) {
251                 sr_spew("FD %d: Only read %d/%d bytes.", fd, ret, count);
252         }
253
254         return ret;
255 #endif
256 }
257
258 /**
259  * Create a backup of the current parameters of the specified serial port.
260  *
261  * @param fd File descriptor of the serial port.
262  *
263  * @return Pointer to a struct termios upon success, NULL upon errors.
264  *         It is the caller's responsibility to g_free() the pointer if no
265  *         longer needed.
266  */
267 SR_PRIV void *serial_backup_params(int fd)
268 {
269         sr_dbg("FD %d: Creating serial parameters backup.", fd);
270
271 #ifdef _WIN32
272         /* TODO */
273 #else
274         struct termios *term;
275
276         if (!(term = g_try_malloc(sizeof(struct termios)))) {
277                 sr_err("termios struct malloc failed.");
278                 return NULL;
279         }
280
281         /* Returns 0 upon success, -1 upon failure. */
282         if (tcgetattr(fd, term) < 0) {
283                 sr_err("FD %d: Error getting serial parameters: %s.",
284                        strerror(errno));
285                 g_free(term);
286                 return NULL;
287         }
288
289         return term;
290 #endif
291 }
292
293 /**
294  * Restore serial port settings from a previously created backup.
295  *
296  * @param fd File descriptor of the serial port.
297  * @param backup Pointer to a struct termios which contains the settings
298  *               to restore.
299  *
300  * @return 0 upon success, -1 upon failure.
301  */
302 SR_PRIV int serial_restore_params(int fd, void *backup)
303 {
304         sr_dbg("FD %d: Restoring serial parameters from backup.", fd);
305
306 #ifdef _WIN32
307         /* TODO */
308 #else
309         int ret;
310
311         /* Returns 0 upon success, -1 upon failure. */
312         if ((ret = tcsetattr(fd, TCSADRAIN, (struct termios *)backup)) < 0) {
313                 sr_err("FD %d: Error restoring serial parameters: %s.",
314                        strerror(errno));
315         }
316
317         return ret;
318 #endif
319 }
320
321 /**
322  * Set serial parameters for the specified serial port.
323  *
324  * @param baudrate The baudrate to set.
325  * @param bits The number of data bits to use.
326  * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd).
327  * @param stopbits The number of stop bits to use (1 or 2).
328  * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS,
329  *                    2 = XON/XOFF).
330  *
331  * @return SR_OK upon success, SR_ERR upon failure.
332  */
333 SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
334                               int stopbits, int flowcontrol)
335 {
336         sr_dbg("FD %d: Setting serial parameters.", fd);
337
338 #ifdef _WIN32
339         DCB dcb;
340
341         if (!GetCommState(hdl, &dcb)) {
342                 /* TODO: Error handling. */
343                 return SR_ERR;
344         }
345
346         switch (baudrate) {
347         /* TODO: Support for higher baud rates. */
348         case 115200:
349                 dcb.BaudRate = CBR_115200;
350                 break;
351         case 57600:
352                 dcb.BaudRate = CBR_57600;
353                 break;
354         case 38400:
355                 dcb.BaudRate = CBR_38400;
356                 break;
357         case 19200:
358                 dcb.BaudRate = CBR_19200;
359                 break;
360         case 9600:
361                 dcb.BaudRate = CBR_9600;
362                 break;
363         case 4800:
364                 dcb.BaudRate = CBR_4800;
365                 break;
366         case 2400:
367                 dcb.BaudRate = CBR_2400;
368                 break;
369         default:
370                 sr_err("Unsupported baudrate: %d.", baudrate);
371                 return SR_ERR;
372         }
373         dcb.ByteSize = bits;
374         dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
375         dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
376
377         if (!SetCommState(hdl, &dcb)) {
378                 /* TODO: Error handling. */
379                 return SR_ERR;
380         }
381 #else
382         struct termios term;
383         speed_t baud;
384
385         sr_dbg("FD %d: Getting terminal settings.", fd);
386         if (tcgetattr(fd, &term) < 0) {
387                 sr_err("tcgetattr() error: %ѕ.", strerror(errno));
388                 return SR_ERR;
389         }
390
391         switch (baudrate) {
392         case 50:
393                 baud = B50;
394                 break;
395         case 75:
396                 baud = B75;
397                 break;
398         case 110:
399                 baud = B110;
400                 break;
401         case 134:
402                 baud = B134;
403                 break;
404         case 150:
405                 baud = B150;
406                 break;
407         case 200:
408                 baud = B200;
409                 break;
410         case 300:
411                 baud = B300;
412                 break;
413         case 600:
414                 baud = B600;
415                 break;
416         case 1200:
417                 baud = B1200;
418                 break;
419         case 1800:
420                 baud = B1800;
421                 break;
422         case 2400:
423                 baud = B2400;
424                 break;
425         case 4800:
426                 baud = B4800;
427                 break;
428         case 9600:
429                 baud = B9600;
430                 break;
431         case 19200:
432                 baud = B19200;
433                 break;
434         case 38400:
435                 baud = B38400;
436                 break;
437         case 57600:
438                 baud = B57600;
439                 break;
440         case 115200:
441                 baud = B115200;
442                 break;
443         case 230400:
444                 baud = B230400;
445                 break;
446 #ifndef __APPLE__
447         case 460800:
448                 baud = B460800;
449                 break;
450 #endif
451         default:
452                 sr_err("Unsupported baudrate: %d.", baudrate);
453                 return SR_ERR;
454         }
455
456         sr_dbg("FD %d: Configuring output baudrate to %d (%d).",
457                 fd, baudrate, baud);
458         if (cfsetospeed(&term, baud) < 0) {
459                 sr_err("cfsetospeed() error: %ѕ.", strerror(errno));
460                 return SR_ERR;
461         }
462
463         sr_dbg("FD %d: Configuring input baudrate to %d (%d).",
464                 fd, baudrate, baud);
465         if (cfsetispeed(&term, baud) < 0) {
466                 sr_err("cfsetispeed() error: %ѕ.", strerror(errno));
467                 return SR_ERR;
468         }
469
470         sr_dbg("FD %d: Configuring %d data bits.", fd, bits);
471         term.c_cflag &= ~CSIZE;
472         switch (bits) {
473         case 8:
474                 term.c_cflag |= CS8;
475                 break;
476         case 7:
477                 term.c_cflag |= CS7;
478                 break;
479         default:
480                 sr_err("Unsupported data bits number: %d.", bits);
481                 return SR_ERR;
482         }
483
484         sr_dbg("FD %d: Configuring %d stop bits.", fd, stopbits);
485         term.c_cflag &= ~CSTOPB;
486         switch (stopbits) {
487         case 1:
488                 /* Do nothing, a cleared CSTOPB entry means "1 stop bit". */
489                 break;
490         case 2:
491                 term.c_cflag |= CSTOPB;
492                 break;
493         default:
494                 sr_err("Unsupported stopbits number: %d.", stopbits);
495                 return SR_ERR;
496         }
497
498         term.c_iflag &= ~(IXON | IXOFF);
499         term.c_cflag &= ~CRTSCTS;
500         switch (flowcontrol) {
501         case 0:
502                 /* No flow control. */
503                 sr_dbg("FD %d: Configuring no flow control.", fd);
504                 break;
505         case 1:
506                 sr_dbg("FD %d: Configuring RTS/CTS flow control.", fd);
507                 term.c_cflag |= CRTSCTS;
508                 break;
509         case 2:
510                 sr_dbg("FD %d: Configuring XON/XOFF flow control.", fd);
511                 term.c_iflag |= IXON | IXOFF;
512                 break;
513         default:
514                 sr_err("Unsupported flow control setting: %d.", flowcontrol);
515                 return SR_ERR;
516         }
517
518         term.c_iflag &= ~IGNPAR;
519         term.c_cflag &= ~(PARODD | PARENB);
520         switch (parity) {
521         case SERIAL_PARITY_NONE:
522                 sr_dbg("FD %d: Configuring no parity.", fd);
523                 term.c_iflag |= IGNPAR;
524                 break;
525         case SERIAL_PARITY_EVEN:
526                 sr_dbg("FD %d: Configuring even parity.", fd);
527                 term.c_cflag |= PARENB;
528                 break;
529         case SERIAL_PARITY_ODD:
530                 sr_dbg("FD %d: Configuring odd parity.", fd);
531                 term.c_cflag |= PARENB | PARODD;
532                 break;
533         default:
534                 sr_err("Unsupported parity setting: %d.", parity);
535                 return SR_ERR;
536         }
537
538         /* Do NOT translate carriage return to newline on input. */
539         term.c_iflag &= ~(ICRNL);
540
541         /* Disable canonical mode, and don't echo input characters. */
542         term.c_lflag &= ~(ICANON | ECHO);
543
544         /* Write the configured settings. */
545         if (tcsetattr(fd, TCSADRAIN, &term) < 0) {
546                 sr_err("tcsetattr() error: %ѕ.", strerror(errno));
547                 return SR_ERR;
548         }
549 #endif
550
551         return SR_OK;
552 }
553
554 #define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])$"
555 SR_PRIV int serial_set_paramstr(int fd, const char *paramstr)
556 {
557         GRegex *reg;
558         GMatchInfo *match;
559         int speed, databits, parity, stopbits;
560         char *mstr;
561
562         speed = databits = parity = stopbits = 0;
563         reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
564         if (g_regex_match(reg, paramstr, 0, &match)) {
565                 if ((mstr = g_match_info_fetch(match, 1)))
566                         speed = strtoul(mstr, NULL, 10);
567                 g_free(mstr);
568                 if ((mstr = g_match_info_fetch(match, 2)))
569                         databits = strtoul(mstr, NULL, 10);
570                 g_free(mstr);
571                 if ((mstr = g_match_info_fetch(match, 3))) {
572                         switch (mstr[0]) {
573                         case 'n':
574                                 parity = SERIAL_PARITY_NONE;
575                                 break;
576                         case 'e':
577                                 parity = SERIAL_PARITY_EVEN;
578                                 break;
579                         case 'o':
580                                 parity = SERIAL_PARITY_ODD;
581                                 break;
582                         }
583                 }
584                 g_free(mstr);
585                 if ((mstr = g_match_info_fetch(match, 4)))
586                         stopbits = strtoul(mstr, NULL, 10);
587                 g_free(mstr);
588         }
589         g_match_info_unref(match);
590         g_regex_unref(reg);
591
592         if (speed)
593                 return serial_set_params(fd, speed, databits, parity, stopbits, 0);
594         else
595                 return SR_ERR_ARG;
596 }
597
598 SR_PRIV int serial_readline(int fd, char **buf, int *buflen,
599                             uint64_t timeout_ms)
600 {
601         uint64_t start;
602         int maxlen, len;
603
604         timeout_ms *= 1000;
605         start = g_get_monotonic_time();
606
607         maxlen = *buflen;
608         *buflen = len = 0;
609         while(1) {
610                 len = maxlen - *buflen - 1;
611                 if (len < 1)
612                         break;
613                 len = serial_read(fd, *buf + *buflen, 1);
614                 if (len > 0) {
615                         *buflen += len;
616                         *(*buf + *buflen) = '\0';
617                         if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') {
618                                 /* Strip LF and terminate. */
619                                 *(*buf + --*buflen) = '\0';
620                                 break;
621                         }
622                 }
623                 if (g_get_monotonic_time() - start > timeout_ms)
624                         /* Timeout */
625                         break;
626                 g_usleep(2000);
627         }
628         sr_dbg("Received %d: '%s'.", *buflen, *buf);
629
630         return SR_OK;
631 }