]> sigrok.org Git - libsigrok.git/blob - hardware/common/serial.c
serial: avoid Cyrillic conversion specifiers
[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 /**
53  * Open the specified serial port.
54  *
55  * @param pathname OS-specific serial port specification. Examples:
56  *                 "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
57  * @param flags Flags to use when opening the serial port.
58  *
59  * @return 0 upon success, -1 upon failure.
60  */
61 SR_PRIV int serial_open(const char *pathname, int flags)
62 {
63         /* TODO: Abstract 'flags', currently they're OS-specific! */
64
65         sr_dbg("Opening serial port '%s' (flags = %d).", pathname, flags);
66
67 #ifdef _WIN32
68         pathname = "COM1"; /* FIXME: Don't hardcode COM1. */
69
70         hdl = CreateFile(pathname, GENERIC_READ | GENERIC_WRITE, 0, 0,
71                          OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
72         if (hdl == INVALID_HANDLE_VALUE) {
73                 sr_err("Error opening serial port '%s'.", pathname);
74                 return -1;
75         }
76         return 0;
77 #else
78         int fd;
79
80         if ((fd = open(pathname, flags)) < 0) {
81                 /*
82                  * Should be sr_err(), but since some drivers try to open all
83                  * ports on a system and see if they succeed, this would
84                  * yield ugly output for e.g. "sigrok-cli -D".
85                  */
86                 sr_dbg("Error opening serial port '%s': %s.", pathname,
87                        strerror(errno));
88         } else {
89                 sr_dbg("Opened serial port '%s' as FD %d.", pathname, fd);
90         }
91
92         return fd;
93 #endif
94 }
95
96 /**
97  * Close the specified serial port.
98  *
99  * @param fd File descriptor of the serial port.
100  *
101  * @return 0 upon success, -1 upon failure.
102  */
103 SR_PRIV int serial_close(int fd)
104 {
105         sr_dbg("FD %d: Closing serial port.", fd);
106
107 #ifdef _WIN32
108         /* Returns non-zero upon success, 0 upon failure. */
109         return (CloseHandle(hdl) == 0) ? -1 : 0;
110 #else
111         int ret;
112
113         /* Returns 0 upon success, -1 upon failure. */
114         if ((ret = close(fd)) < 0) {
115                 sr_dbg("FD %d: Error closing serial port: %s.",
116                        fd, strerror(errno));
117         }
118
119         return ret;
120 #endif
121 }
122
123 /**
124  * Flush serial port buffers (if any).
125  *
126  * @param fd File descriptor of the serial port.
127  *
128  * @return 0 upon success, -1 upon failure.
129  */
130 SR_PRIV int serial_flush(int fd)
131 {
132         sr_dbg("FD %d: Flushing serial port.", fd);
133
134 #ifdef _WIN32
135         /* Returns non-zero upon success, 0 upon failure. */
136         return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
137 #else
138         int ret;
139
140         /* Returns 0 upon success, -1 upon failure. */
141         if ((ret = tcflush(fd, TCIOFLUSH)) < 0)
142                 sr_err("Error flushing serial port: %s.", strerror(errno));
143
144         return ret;
145 #endif
146 }
147
148 /**
149  * Write a number of bytes to the specified serial port.
150  *
151  * @param fd File descriptor of the serial port.
152  * @param buf Buffer containing the bytes to write.
153  * @param count Number of bytes to write.
154  *
155  * @return The number of bytes written, or -1 upon failure.
156  */
157 SR_PRIV int serial_write(int fd, const void *buf, size_t count)
158 {
159 #ifdef _WIN32
160         DWORD tmp = 0;
161
162         /* FIXME */
163         /* Returns non-zero upon success, 0 upon failure. */
164         WriteFile(hdl, buf, count, &tmp, NULL);
165 #else
166         ssize_t ret;
167
168         /* Returns the number of bytes written, or -1 upon failure. */
169         ret = write(fd, buf, count);
170         if (ret < 0)
171                 sr_err("FD %d: Write error: %s.", fd, strerror(errno));
172         else
173                 sr_spew("FD %d: Wrote %d/%d bytes.", fd, ret, count);
174
175         return ret;
176 #endif
177 }
178
179 /**
180  * Read a number of bytes from the specified serial port.
181  *
182  * @param fd File descriptor of the serial port.
183  * @param buf Buffer where to store the bytes that are read.
184  * @param count The number of bytes to read.
185  *
186  * @return The number of bytes read, or -1 upon failure.
187  */
188 SR_PRIV int serial_read(int fd, void *buf, size_t count)
189 {
190 #ifdef _WIN32
191         DWORD tmp = 0;
192
193         /* FIXME */
194         /* Returns non-zero upon success, 0 upon failure. */
195         return ReadFile(hdl, buf, count, &tmp, NULL);
196 #else
197         ssize_t ret;
198
199         /* Returns the number of bytes read, or -1 upon failure. */
200         ret = read(fd, buf, count);
201         if (ret < 0) {
202                 /*
203                  * Should be sr_err(), but that would yield lots of
204                  * "Resource temporarily unavailable" messages.
205                  */
206                 sr_spew("FD %d: Read error: %s.", fd, strerror(errno));
207         } else {
208                 sr_spew("FD %d: Read %d/%d bytes.", fd, ret, count);
209         }
210
211         return ret;
212 #endif
213 }
214
215 /**
216  * Set serial parameters for the specified serial port.
217  *
218  * @param baudrate The baudrate to set.
219  * @param bits The number of data bits to use.
220  * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd).
221  * @param stopbits The number of stop bits to use (1 or 2).
222  * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS,
223  *                    2 = XON/XOFF).
224  *
225  * @return SR_OK upon success, SR_ERR upon failure.
226  */
227 SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
228                               int stopbits, int flowcontrol)
229 {
230         sr_dbg("FD %d: Setting serial parameters.", fd);
231
232 #ifdef _WIN32
233         DCB dcb;
234
235         if (!GetCommState(hdl, &dcb)) {
236                 /* TODO: Error handling. */
237                 return SR_ERR;
238         }
239
240         switch (baudrate) {
241         /* TODO: Support for higher baud rates. */
242         case 115200:
243                 dcb.BaudRate = CBR_115200;
244                 break;
245         case 57600:
246                 dcb.BaudRate = CBR_57600;
247                 break;
248         case 38400:
249                 dcb.BaudRate = CBR_38400;
250                 break;
251         case 19200:
252                 dcb.BaudRate = CBR_19200;
253                 break;
254         case 9600:
255                 dcb.BaudRate = CBR_9600;
256                 break;
257         case 4800:
258                 dcb.BaudRate = CBR_4800;
259                 break;
260         case 2400:
261                 dcb.BaudRate = CBR_2400;
262                 break;
263         default:
264                 sr_err("Unsupported baudrate: %d.", baudrate);
265                 return SR_ERR;
266         }
267         dcb.ByteSize = bits;
268         dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
269         dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
270
271         if (!SetCommState(hdl, &dcb)) {
272                 /* TODO: Error handling. */
273                 return SR_ERR;
274         }
275 #else
276         struct termios term;
277         speed_t baud;
278
279         sr_dbg("FD %d: Getting terminal settings.", fd);
280         if (tcgetattr(fd, &term) < 0) {
281                 sr_err("tcgetattr() error: %s.", strerror(errno));
282                 return SR_ERR;
283         }
284
285         switch (baudrate) {
286         case 50:
287                 baud = B50;
288                 break;
289         case 75:
290                 baud = B75;
291                 break;
292         case 110:
293                 baud = B110;
294                 break;
295         case 134:
296                 baud = B134;
297                 break;
298         case 150:
299                 baud = B150;
300                 break;
301         case 200:
302                 baud = B200;
303                 break;
304         case 300:
305                 baud = B300;
306                 break;
307         case 600:
308                 baud = B600;
309                 break;
310         case 1200:
311                 baud = B1200;
312                 break;
313         case 1800:
314                 baud = B1800;
315                 break;
316         case 2400:
317                 baud = B2400;
318                 break;
319         case 4800:
320                 baud = B4800;
321                 break;
322         case 9600:
323                 baud = B9600;
324                 break;
325         case 19200:
326                 baud = B19200;
327                 break;
328         case 38400:
329                 baud = B38400;
330                 break;
331         case 57600:
332                 baud = B57600;
333                 break;
334         case 115200:
335                 baud = B115200;
336                 break;
337         case 230400:
338                 baud = B230400;
339                 break;
340 #ifndef __APPLE__
341         case 460800:
342                 baud = B460800;
343                 break;
344 #endif
345         default:
346                 sr_err("Unsupported baudrate: %d.", baudrate);
347                 return SR_ERR;
348         }
349
350         sr_dbg("FD %d: Configuring output baudrate to %d (%d).",
351                 fd, baudrate, baud);
352         if (cfsetospeed(&term, baud) < 0) {
353                 sr_err("cfsetospeed() error: %s.", strerror(errno));
354                 return SR_ERR;
355         }
356
357         sr_dbg("FD %d: Configuring input baudrate to %d (%d).",
358                 fd, baudrate, baud);
359         if (cfsetispeed(&term, baud) < 0) {
360                 sr_err("cfsetispeed() error: %s.", strerror(errno));
361                 return SR_ERR;
362         }
363
364         sr_dbg("FD %d: Configuring %d data bits.", fd, bits);
365         term.c_cflag &= ~CSIZE;
366         switch (bits) {
367         case 8:
368                 term.c_cflag |= CS8;
369                 break;
370         case 7:
371                 term.c_cflag |= CS7;
372                 break;
373         default:
374                 sr_err("Unsupported data bits number: %d.", bits);
375                 return SR_ERR;
376         }
377
378         sr_dbg("FD %d: Configuring %d stop bits.", fd, stopbits);
379         term.c_cflag &= ~CSTOPB;
380         switch (stopbits) {
381         case 1:
382                 /* Do nothing, a cleared CSTOPB entry means "1 stop bit". */
383                 break;
384         case 2:
385                 term.c_cflag |= CSTOPB;
386                 break;
387         default:
388                 sr_err("Unsupported stopbits number: %d.", stopbits);
389                 return SR_ERR;
390         }
391
392         term.c_iflag &= ~(IXON | IXOFF);
393         term.c_cflag &= ~CRTSCTS;
394         switch (flowcontrol) {
395         case 0:
396                 /* No flow control. */
397                 sr_dbg("FD %d: Configuring no flow control.", fd);
398                 break;
399         case 1:
400                 sr_dbg("FD %d: Configuring RTS/CTS flow control.", fd);
401                 term.c_cflag |= CRTSCTS;
402                 break;
403         case 2:
404                 sr_dbg("FD %d: Configuring XON/XOFF flow control.", fd);
405                 term.c_iflag |= IXON | IXOFF;
406                 break;
407         default:
408                 sr_err("Unsupported flow control setting: %d.", flowcontrol);
409                 return SR_ERR;
410         }
411
412         term.c_iflag &= ~IGNPAR;
413         term.c_cflag &= ~(PARODD | PARENB);
414         switch (parity) {
415         case SERIAL_PARITY_NONE:
416                 sr_dbg("FD %d: Configuring no parity.", fd);
417                 term.c_iflag |= IGNPAR;
418                 break;
419         case SERIAL_PARITY_EVEN:
420                 sr_dbg("FD %d: Configuring even parity.", fd);
421                 term.c_cflag |= PARENB;
422                 break;
423         case SERIAL_PARITY_ODD:
424                 sr_dbg("FD %d: Configuring odd parity.", fd);
425                 term.c_cflag |= PARENB | PARODD;
426                 break;
427         default:
428                 sr_err("Unsupported parity setting: %d.", parity);
429                 return SR_ERR;
430         }
431
432         /* Do NOT translate carriage return to newline on input. */
433         term.c_iflag &= ~(ICRNL);
434
435         /* Disable canonical mode, and don't echo input characters. */
436         term.c_lflag &= ~(ICANON | ECHO);
437
438         /* Write the configured settings. */
439         if (tcsetattr(fd, TCSADRAIN, &term) < 0) {
440                 sr_err("tcsetattr() error: %ѕ.", strerror(errno));
441                 return SR_ERR;
442         }
443 #endif
444
445         return SR_OK;
446 }
447
448 #define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])$"
449 SR_PRIV int serial_set_paramstr(int fd, const char *paramstr)
450 {
451         GRegex *reg;
452         GMatchInfo *match;
453         int speed, databits, parity, stopbits;
454         char *mstr;
455
456         speed = databits = parity = stopbits = 0;
457         reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
458         if (g_regex_match(reg, paramstr, 0, &match)) {
459                 if ((mstr = g_match_info_fetch(match, 1)))
460                         speed = strtoul(mstr, NULL, 10);
461                 g_free(mstr);
462                 if ((mstr = g_match_info_fetch(match, 2)))
463                         databits = strtoul(mstr, NULL, 10);
464                 g_free(mstr);
465                 if ((mstr = g_match_info_fetch(match, 3))) {
466                         switch (mstr[0]) {
467                         case 'n':
468                                 parity = SERIAL_PARITY_NONE;
469                                 break;
470                         case 'e':
471                                 parity = SERIAL_PARITY_EVEN;
472                                 break;
473                         case 'o':
474                                 parity = SERIAL_PARITY_ODD;
475                                 break;
476                         }
477                 }
478                 g_free(mstr);
479                 if ((mstr = g_match_info_fetch(match, 4)))
480                         stopbits = strtoul(mstr, NULL, 10);
481                 g_free(mstr);
482         }
483         g_match_info_unref(match);
484         g_regex_unref(reg);
485
486         if (speed)
487                 return serial_set_params(fd, speed, databits, parity, stopbits, 0);
488         else
489                 return SR_ERR_ARG;
490 }
491
492 SR_PRIV int serial_readline(int fd, char **buf, int *buflen,
493                             gint64 timeout_ms)
494 {
495         gint64 start;
496         int maxlen, len;
497
498         timeout_ms *= 1000;
499         start = g_get_monotonic_time();
500
501         maxlen = *buflen;
502         *buflen = len = 0;
503         while(1) {
504                 len = maxlen - *buflen - 1;
505                 if (len < 1)
506                         break;
507                 len = serial_read(fd, *buf + *buflen, 1);
508                 if (len > 0) {
509                         *buflen += len;
510                         *(*buf + *buflen) = '\0';
511                         if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r'
512                                         || *(*buf + *buflen - 1) == '\n')) {
513                                 /* Strip CR/LF and terminate. */
514                                 *(*buf + --*buflen) = '\0';
515                                 break;
516                         }
517                 }
518                 if (g_get_monotonic_time() - start > timeout_ms)
519                         /* Timeout */
520                         break;
521                 g_usleep(2000);
522         }
523         if (*buflen)
524                 sr_dbg("Received %d: '%s'.", *buflen, *buf);
525
526         return SR_OK;
527 }