]> sigrok.org Git - libserialport.git/blob - serialport.c
Implement non-blocking I/O on Windows.
[libserialport.git] / serialport.c
1 /*
2  * This file is part of the libserialport project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de>
6  * Copyright (C) 2013 Martin Ling <martin-libserialport@earth.li>
7  * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation, either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32 #ifdef _WIN32
33 #include <windows.h>
34 #include <tchar.h>
35 #include <stdio.h>
36 #else
37 #include <termios.h>
38 #include <sys/ioctl.h>
39 #endif
40 #ifdef __APPLE__
41 #include <IOKit/IOKitLib.h>
42 #include <IOKit/serial/IOSerialKeys.h>
43 #include <IOKit/serial/ioss.h>
44 #include <sys/syslimits.h>
45 #endif
46 #ifdef __linux__
47 #include "libudev.h"
48 #include "linux/serial.h"
49 #include "linux_termios.h"
50 #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX)
51 #define USE_TERMIOX
52 #endif
53 #endif
54
55 #ifndef _WIN32
56 #include "linux_termios.h"
57 #endif
58
59 #include "libserialport.h"
60
61 struct sp_port {
62         char *name;
63         int nonblocking;
64 #ifdef _WIN32
65         HANDLE hdl;
66         OVERLAPPED write_ovl;
67         BYTE *write_buf;
68         BOOL writing;
69 #else
70         int fd;
71 #endif
72 };
73
74 struct sp_port_config {
75         int baudrate;
76         int bits;
77         enum sp_parity parity;
78         int stopbits;
79         enum sp_rts rts;
80         enum sp_cts cts;
81         enum sp_dtr dtr;
82         enum sp_dsr dsr;
83         enum sp_xonxoff xon_xoff;
84 };
85
86 struct port_data {
87 #ifdef _WIN32
88         DCB dcb;
89 #else
90         struct termios term;
91         int controlbits;
92         int termiox_supported;
93         int flow;
94 #endif
95 };
96
97 /* Standard baud rates. */
98 #ifdef _WIN32
99 #define BAUD_TYPE DWORD
100 #define BAUD(n) {CBR_##n, n}
101 #else
102 #define BAUD_TYPE speed_t
103 #define BAUD(n) {B##n, n}
104 #endif
105
106 struct std_baudrate {
107         BAUD_TYPE index;
108         int value;
109 };
110
111 const struct std_baudrate std_baudrates[] = {
112 #ifdef _WIN32
113         /*
114          * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
115          * have documented CBR_* macros.
116          */
117         BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
118         BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
119         BAUD(115200), BAUD(128000), BAUD(256000),
120 #else
121         BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
122         BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
123         BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
124         BAUD(230400),
125 #if !defined(__APPLE__) && !defined(__OpenBSD__)
126         BAUD(460800),
127 #endif
128 #endif
129 };
130
131 void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
132
133 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
134 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
135
136 /* Debug output macros. */
137 #define DEBUG(fmt, ...) do { if (sp_debug_handler) sp_debug_handler(fmt ".\n", ##__VA_ARGS__); } while (0)
138 #define DEBUG_ERROR(err, msg) DEBUG("%s returning " #err ": " msg, __func__)
139 #define DEBUG_FAIL(msg) do { \
140         char *errmsg = sp_last_error_message(); \
141         DEBUG("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
142         sp_free_error_message(errmsg); \
143 } while (0);
144 #define RETURN() do { DEBUG("%s returning", __func__); return; } while(0)
145 #define RETURN_CODE(x) do { DEBUG("%s returning " #x, __func__); return x; } while (0)
146 #define RETURN_CODEVAL(x) do { \
147         switch (x) { \
148                 case SP_OK: RETURN_CODE(SP_OK); \
149                 case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \
150                 case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \
151                 case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \
152                 case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \
153         } \
154 } while (0)
155 #define RETURN_OK() RETURN_CODE(SP_OK);
156 #define RETURN_ERROR(err, msg) do { DEBUG_ERROR(err, msg); return err; } while (0)
157 #define RETURN_FAIL(msg) do { DEBUG_FAIL(msg); return SP_ERR_FAIL; } while (0)
158 #define RETURN_VALUE(fmt, x) do { DEBUG("%s returning " fmt, __func__, x); return x; } while (0)
159 #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
160 #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
161 #define TRACE(fmt, ...) DEBUG("%s(" fmt ") called", __func__, ##__VA_ARGS__)
162
163 #define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0)
164
165 /* Helper functions. */
166 static struct sp_port **list_append(struct sp_port **list, const char *portname);
167 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
168         struct sp_port_config *config);
169 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
170         const struct sp_port_config *config);
171
172 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
173 {
174         struct sp_port *port;
175         int len;
176
177         TRACE("%s, %p", portname, port_ptr);
178
179         if (!port_ptr)
180                 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
181
182         *port_ptr = NULL;
183
184         if (!portname)
185                 RETURN_ERROR(SP_ERR_ARG, "Null port name");
186
187         DEBUG("Building structure for port %s", portname);
188
189         if (!(port = malloc(sizeof(struct sp_port))))
190                 RETURN_ERROR(SP_ERR_MEM, "Port structure malloc failed");
191
192         len = strlen(portname) + 1;
193
194         if (!(port->name = malloc(len))) {
195                 free(port);
196                 RETURN_ERROR(SP_ERR_MEM, "Port name malloc failed");
197         }
198
199         memcpy(port->name, portname, len);
200
201 #ifdef _WIN32
202         port->hdl = INVALID_HANDLE_VALUE;
203 #else
204         port->fd = -1;
205 #endif
206
207         *port_ptr = port;
208
209         RETURN_OK();
210 }
211
212 char *sp_get_port_name(const struct sp_port *port)
213 {
214         TRACE("%p", port);
215
216         if (!port)
217                 return NULL;
218
219         RETURN_VALUE("%s", port->name);
220 }
221
222 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr)
223 {
224         TRACE("%p", port);
225
226         if (!port)
227                 RETURN_ERROR(SP_ERR_ARG, "Null port");
228
229 #ifdef _WIN32
230         HANDLE *handle_ptr = result_ptr;
231         *handle_ptr = port->hdl;
232 #else
233         int *fd_ptr = result_ptr;
234         *fd_ptr = port->fd;
235 #endif
236
237         RETURN_OK();
238 }
239
240 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
241 {
242         TRACE("%p, %p", port, copy_ptr);
243
244         if (!copy_ptr)
245                 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
246
247         *copy_ptr = NULL;
248
249         if (!port)
250                 RETURN_ERROR(SP_ERR_ARG, "Null port");
251
252         if (!port->name)
253                 RETURN_ERROR(SP_ERR_ARG, "Null port name");
254
255         DEBUG("Copying port structure");
256
257         RETURN_VALUE("%p", sp_get_port_by_name(port->name, copy_ptr));
258 }
259
260 void sp_free_port(struct sp_port *port)
261 {
262         TRACE("%p", port);
263
264         if (!port)
265         {
266                 DEBUG("Null port");
267                 RETURN();
268         }
269
270         DEBUG("Freeing port structure");
271
272         if (port->name)
273                 free(port->name);
274
275         free(port);
276
277         RETURN();
278 }
279
280 static struct sp_port **list_append(struct sp_port **list, const char *portname)
281 {
282         void *tmp;
283         unsigned int count;
284
285         for (count = 0; list[count]; count++);
286         if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
287                 goto fail;
288         list = tmp;
289         if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
290                 goto fail;
291         list[count + 1] = NULL;
292         return list;
293
294 fail:
295         sp_free_port_list(list);
296         return NULL;
297 }
298
299 enum sp_return sp_list_ports(struct sp_port ***list_ptr)
300 {
301         struct sp_port **list;
302         int ret = SP_ERR_SUPP;
303
304         TRACE("%p", list_ptr);
305
306         if (!list_ptr)
307                 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
308
309         DEBUG("Enumerating ports");
310
311         if (!(list = malloc(sizeof(struct sp_port **))))
312                 RETURN_ERROR(SP_ERR_MEM, "Port list malloc failed");
313
314         list[0] = NULL;
315
316 #ifdef _WIN32
317         HKEY key;
318         TCHAR *value, *data;
319         DWORD max_value_len, max_data_size, max_data_len;
320         DWORD value_len, data_size, data_len;
321         DWORD type, index = 0;
322         char *name;
323         int name_len;
324
325         ret = SP_OK;
326
327         DEBUG("Opening registry key");
328         if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
329                         0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
330                 SET_FAIL(ret, "RegOpenKeyEx() failed");
331                 goto out_done;
332         }
333         DEBUG("Querying registry key value and data sizes");
334         if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
335                                 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
336                 SET_FAIL(ret, "RegQueryInfoKey() failed");
337                 goto out_close;
338         }
339         max_data_len = max_data_size / sizeof(TCHAR);
340         if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
341                 SET_ERROR(ret, SP_ERR_MEM, "registry value malloc failed");
342                 goto out_close;
343         }
344         if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
345                 SET_ERROR(ret, SP_ERR_MEM, "registry data malloc failed");
346                 goto out_free_value;
347         }
348         DEBUG("Iterating over values");
349         while (
350                 value_len = max_value_len + 1,
351                 data_size = max_data_size,
352                 RegEnumValue(key, index, value, &value_len,
353                         NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
354         {
355                 data_len = data_size / sizeof(TCHAR);
356                 data[data_len] = '\0';
357 #ifdef UNICODE
358                 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
359 #else
360                 name_len = data_len + 1;
361 #endif
362                 if (!(name = malloc(name_len))) {
363                         SET_ERROR(ret, SP_ERR_MEM, "registry port name malloc failed");
364                         goto out;
365                 }
366 #ifdef UNICODE
367                 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
368 #else
369                 strcpy(name, data);
370 #endif
371                 if (type == REG_SZ) {
372                         DEBUG("Found port %s", name);
373                         if (!(list = list_append(list, name))) {
374                                 SET_ERROR(ret, SP_ERR_MEM, "list append failed");
375                                 goto out;
376                         }
377                 }
378                 index++;
379         }
380 out:
381         free(data);
382 out_free_value:
383         free(value);
384 out_close:
385         RegCloseKey(key);
386 out_done:
387 #endif
388 #ifdef __APPLE__
389         mach_port_t master;
390         CFMutableDictionaryRef classes;
391         io_iterator_t iter;
392         char *path;
393         io_object_t port;
394         CFTypeRef cf_path;
395         Boolean result;
396
397         ret = SP_OK;
398
399         DEBUG("Getting IOKit master port");
400         if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
401                 SET_FAIL(ret, "IOMasterPort() failed");
402                 goto out_done;
403         }
404
405         DEBUG("Creating matching dictionary");
406         if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
407                 SET_FAIL(ret, "IOServiceMatching() failed");
408                 goto out_done;
409         }
410
411         CFDictionarySetValue(classes,
412                         CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
413
414         DEBUG("Getting matching services");
415         if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
416                 SET_FAIL(ret, "IOServiceGetMatchingServices() failed");
417                 goto out_done;
418         }
419
420         if (!(path = malloc(PATH_MAX))) {
421                 SET_ERROR(ret, SP_ERR_MEM, "device path malloc failed");
422                 goto out_release;
423         }
424
425         DEBUG("Iterating over results");
426         while ((port = IOIteratorNext(iter))) {
427                 cf_path = IORegistryEntryCreateCFProperty(port,
428                                 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
429                 if (cf_path) {
430                         result = CFStringGetCString(cf_path,
431                                         path, PATH_MAX, kCFStringEncodingASCII);
432                         CFRelease(cf_path);
433                         if (result) {
434                                 DEBUG("Found port %s", path);
435                                 if (!(list = list_append(list, path))) {
436                                         SET_ERROR(ret, SP_ERR_MEM, "list append failed");
437                                         IOObjectRelease(port);
438                                         goto out;
439                                 }
440                         }
441                 }
442                 IOObjectRelease(port);
443         }
444 out:
445         free(path);
446 out_release:
447         IOObjectRelease(iter);
448 out_done:
449 #endif
450 #ifdef __linux__
451         struct udev *ud;
452         struct udev_enumerate *ud_enumerate;
453         struct udev_list_entry *ud_list;
454         struct udev_list_entry *ud_entry;
455         const char *path;
456         struct udev_device *ud_dev, *ud_parent;
457         const char *name;
458         const char *driver;
459         int fd, ioctl_result;
460         struct serial_struct serial_info;
461
462         ret = SP_OK;
463
464         DEBUG("Enumerating tty devices");
465         ud = udev_new();
466         ud_enumerate = udev_enumerate_new(ud);
467         udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
468         udev_enumerate_scan_devices(ud_enumerate);
469         ud_list = udev_enumerate_get_list_entry(ud_enumerate);
470         DEBUG("Iterating over results");
471         udev_list_entry_foreach(ud_entry, ud_list) {
472                 path = udev_list_entry_get_name(ud_entry);
473                 DEBUG("Found device %s", path);
474                 ud_dev = udev_device_new_from_syspath(ud, path);
475                 /* If there is no parent device, this is a virtual tty. */
476                 ud_parent = udev_device_get_parent(ud_dev);
477                 if (ud_parent == NULL) {
478                         DEBUG("No parent device, assuming virtual tty");
479                         udev_device_unref(ud_dev);
480                         continue;
481                 }
482                 name = udev_device_get_devnode(ud_dev);
483                 /* The serial8250 driver has a hardcoded number of ports.
484                  * The only way to tell which actually exist on a given system
485                  * is to try to open them and make an ioctl call. */
486                 driver = udev_device_get_driver(ud_parent);
487                 if (driver && !strcmp(driver, "serial8250")) {
488                         DEBUG("serial8250 device, attempting to open");
489                         if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
490                                 DEBUG("open failed, skipping");
491                                 goto skip;
492                         }
493                         ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
494                         close(fd);
495                         if (ioctl_result != 0) {
496                                 DEBUG("ioctl failed, skipping");
497                                 goto skip;
498                         }
499                         if (serial_info.type == PORT_UNKNOWN) {
500                                 DEBUG("port type is unknown, skipping");
501                                 goto skip;
502                         }
503                 }
504                 DEBUG("Found port %s", name);
505                 list = list_append(list, name);
506 skip:
507                 udev_device_unref(ud_dev);
508                 if (!list) {
509                         SET_ERROR(ret, SP_ERR_MEM, "list append failed");
510                         goto out;
511                 }
512         }
513 out:
514         udev_enumerate_unref(ud_enumerate);
515         udev_unref(ud);
516 #endif
517
518         switch (ret) {
519         case SP_OK:
520                 *list_ptr = list;
521                 RETURN_OK();
522         case SP_ERR_SUPP:
523                 DEBUG_ERROR(SP_ERR_SUPP, "Enumeration not supported on this platform.");
524         default:
525                 if (list)
526                         sp_free_port_list(list);
527                 *list_ptr = NULL;
528                 return ret;
529         }
530 }
531
532 void sp_free_port_list(struct sp_port **list)
533 {
534         unsigned int i;
535
536         TRACE("%p", list);
537
538         if (!list) {
539                 DEBUG("Null list");
540                 RETURN();
541         }
542
543         DEBUG("Freeing port list");
544
545         for (i = 0; list[i]; i++)
546                 sp_free_port(list[i]);
547         free(list);
548
549         RETURN();
550 }
551
552 #define CHECK_PORT() do { \
553         if (port == NULL) \
554                 RETURN_ERROR(SP_ERR_ARG, "Null port"); \
555         if (port->name == NULL) \
556                 RETURN_ERROR(SP_ERR_ARG, "Null port name"); \
557 } while (0)
558 #ifdef _WIN32
559 #define CHECK_PORT_HANDLE() do { \
560         if (port->hdl == INVALID_HANDLE_VALUE) \
561                 RETURN_ERROR(SP_ERR_ARG, "Invalid port handle"); \
562 } while (0)
563 #else
564 #define CHECK_PORT_HANDLE() do { \
565         if (port->fd < 0) \
566                 RETURN_ERROR(SP_ERR_ARG, "Invalid port fd"); \
567 } while (0)
568 #endif
569 #define CHECK_OPEN_PORT() do { \
570         CHECK_PORT(); \
571         CHECK_PORT_HANDLE(); \
572 } while (0)
573
574 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
575 {
576         TRACE("%p, %x", port, flags);
577
578         CHECK_PORT();
579
580         if (flags > (SP_MODE_READ | SP_MODE_WRITE | SP_MODE_NONBLOCK))
581                 RETURN_ERROR(SP_ERR_ARG, "Invalid flags");
582
583         DEBUG("Opening port %s", port->name);
584
585         port->nonblocking = (flags & SP_MODE_NONBLOCK) ? 1 : 0;
586
587 #ifdef _WIN32
588         DWORD desired_access = 0, flags_and_attributes = 0;
589         COMMTIMEOUTS timeouts;
590         char *escaped_port_name;
591
592         /* Prefix port name with '\\.\' to work with ports above COM9. */
593         if (!(escaped_port_name = malloc(strlen(port->name + 5))))
594                 RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed");
595         sprintf(escaped_port_name, "\\\\.\\%s", port->name);
596
597         /* Map 'flags' to the OS-specific settings. */
598         flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
599         if (flags & SP_MODE_READ)
600                 desired_access |= GENERIC_READ;
601         if (flags & SP_MODE_WRITE)
602                 desired_access |= GENERIC_WRITE;
603         if (flags & SP_MODE_NONBLOCK)
604                 flags_and_attributes |= FILE_FLAG_OVERLAPPED;
605
606         port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
607                          OPEN_EXISTING, flags_and_attributes, 0);
608
609         free(escaped_port_name);
610
611         if (port->hdl == INVALID_HANDLE_VALUE)
612                 RETURN_FAIL("CreateFile() failed");
613
614         /* All timeouts disabled. */
615         timeouts.ReadIntervalTimeout = 0;
616         timeouts.ReadTotalTimeoutMultiplier = 0;
617         timeouts.ReadTotalTimeoutConstant = 0;
618         timeouts.WriteTotalTimeoutMultiplier = 0;
619         timeouts.WriteTotalTimeoutConstant = 0;
620
621         if (port->nonblocking) {
622                 /* Set read timeout such that all reads return immediately. */
623                 timeouts.ReadIntervalTimeout = MAXDWORD;
624                 /* Prepare OVERLAPPED structure for non-blocking writes. */
625                 memset(&port->write_ovl, 0, sizeof(port->write_ovl));
626                 if (!(port->write_ovl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL))) {
627                         sp_close(port);
628                         RETURN_FAIL("CreateEvent() failed");
629                 }
630                 port->write_buf = NULL;
631                 port->writing = FALSE;
632         }
633
634         if (SetCommTimeouts(port->hdl, &timeouts) == 0) {
635                 sp_close(port);
636                 RETURN_FAIL("SetCommTimeouts() failed");
637         }
638 #else
639         int flags_local = 0;
640         struct port_data data;
641         struct sp_port_config config;
642         int ret;
643
644         /* Map 'flags' to the OS-specific settings. */
645         if (flags & (SP_MODE_READ | SP_MODE_WRITE))
646                 flags_local |= O_RDWR;
647         else if (flags & SP_MODE_READ)
648                 flags_local |= O_RDONLY;
649         else if (flags & SP_MODE_WRITE)
650                 flags_local |= O_WRONLY;
651         if (flags & SP_MODE_NONBLOCK)
652                 flags_local |= O_NONBLOCK;
653
654         if ((port->fd = open(port->name, flags_local)) < 0)
655                 RETURN_FAIL("open() failed");
656
657         ret = get_config(port, &data, &config);
658
659         if (ret < 0) {
660                 sp_close(port);
661                 RETURN_CODEVAL(ret);
662         }
663
664         /* Turn off all serial port cooking. */
665         data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
666         data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
667 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
668         data.term.c_oflag &= ~OFILL;
669 #endif
670         /* Disable canonical mode, and don't echo input characters. */
671         data.term.c_lflag &= ~(ICANON | ECHO);
672
673         /* Ignore modem status lines; enable receiver */
674         data.term.c_cflag |= (CLOCAL | CREAD);
675
676         ret = set_config(port, &data, &config);
677
678         if (ret < 0) {
679                 sp_close(port);
680                 RETURN_CODEVAL(ret);
681         }
682 #endif
683
684         RETURN_OK();
685 }
686
687 enum sp_return sp_close(struct sp_port *port)
688 {
689         TRACE("%p", port);
690
691         CHECK_OPEN_PORT();
692
693         DEBUG("Closing port %s", port->name);
694
695 #ifdef _WIN32
696         /* Returns non-zero upon success, 0 upon failure. */
697         if (CloseHandle(port->hdl) == 0)
698                 RETURN_FAIL("CloseHandle() failed");
699         port->hdl = INVALID_HANDLE_VALUE;
700         if (port->nonblocking) {
701                 if (port->writing)
702                         /* Write should have been stopped by closing the port, so safe to free buffer. */
703                         free(port->write_buf);
704                 /* Close event handle created for overlapped writes. */
705                 if (CloseHandle(port->write_ovl.hEvent) == 0)
706                         RETURN_FAIL("CloseHandle() failed");
707         }
708 #else
709         /* Returns 0 upon success, -1 upon failure. */
710         if (close(port->fd) == -1)
711                 RETURN_FAIL("close() failed");
712         port->fd = -1;
713 #endif
714
715         RETURN_OK();
716 }
717
718 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
719 {
720         TRACE("%p, %x", port, buffers);
721
722         CHECK_OPEN_PORT();
723
724         if (buffers > SP_BUF_BOTH)
725                 RETURN_ERROR(SP_ERR_ARG, "Invalid buffer selection");
726
727         const char *buffer_names[] = {"no", "input", "output", "both"};
728
729         DEBUG("Flushing %s buffers on port %s", buffer_names[buffers], port->name);
730
731 #ifdef _WIN32
732         DWORD flags = 0;
733         if (buffers & SP_BUF_INPUT)
734                 flags |= PURGE_RXCLEAR;
735         if (buffers & SP_BUF_OUTPUT)
736                 flags |= PURGE_TXCLEAR;
737
738         /* Returns non-zero upon success, 0 upon failure. */
739         if (PurgeComm(port->hdl, flags) == 0)
740                 RETURN_FAIL("PurgeComm() failed");
741 #else
742         int flags = 0;
743         if (buffers & SP_BUF_BOTH)
744                 flags = TCIOFLUSH;
745         else if (buffers & SP_BUF_INPUT)
746                 flags = TCIFLUSH;
747         else if (buffers & SP_BUF_OUTPUT)
748                 flags = TCOFLUSH;
749
750         /* Returns 0 upon success, -1 upon failure. */
751         if (tcflush(port->fd, flags) < 0)
752                 RETURN_FAIL("tcflush() failed");
753 #endif
754         RETURN_OK();
755 }
756
757 enum sp_return sp_drain(struct sp_port *port)
758 {
759         TRACE("%p", port);
760
761         CHECK_OPEN_PORT();
762
763         DEBUG("Draining port %s", port->name);
764
765 #ifdef _WIN32
766         /* Returns non-zero upon success, 0 upon failure. */
767         if (FlushFileBuffers(port->hdl) == 0)
768                 RETURN_FAIL("FlushFileBuffers() failed");
769 #else
770         /* Returns 0 upon success, -1 upon failure. */
771         if (tcdrain(port->fd) < 0)
772                 RETURN_FAIL("tcdrain() failed");
773 #endif
774
775         RETURN_OK();
776 }
777
778 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
779 {
780         TRACE("%p, %p, %d", port, buf, count);
781
782         CHECK_OPEN_PORT();
783
784         if (!buf)
785                 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
786
787         DEBUG("Writing up to %d bytes to port %s", count, port->name);
788
789         if (count == 0)
790                 RETURN_VALUE("0", 0);
791
792 #ifdef _WIN32
793         DWORD written = 0;
794
795         if (port->nonblocking) {
796                 /* Non-blocking write. */
797
798                 /* Check whether previous write is complete. */
799                 if (port->writing) {
800                         if (HasOverlappedIoCompleted(&port->write_ovl)) {
801                                 DEBUG("Previous write completed");
802                                 port->writing = 0;
803                                 free(port->write_buf);
804                                 port->write_buf = NULL;
805                         } else {
806                                 DEBUG("Previous write not complete");
807                                 /* Can't take a new write until the previous one finishes. */
808                                 RETURN_VALUE("0", 0);
809                         }
810                 }
811
812                 /* Copy user buffer. */
813                 if (!(port->write_buf = malloc(count)))
814                         RETURN_ERROR(SP_ERR_MEM, "buffer copy malloc failed");
815                 memcpy(port->write_buf, buf, count);
816
817                 /* Start asynchronous write. */
818                 if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl) == 0) {
819                         if (GetLastError() == ERROR_IO_PENDING) {
820                                 DEBUG("Asynchronous write started");
821                                 port->writing = 1;
822                                 RETURN_VALUE("%d", count);
823                         } else {
824                                 free(port->write_buf);
825                                 port->write_buf = NULL;
826                                 /* Actual failure of some kind. */
827                                 RETURN_FAIL("WriteFile() failed");
828                         }
829                 } else {
830                         DEBUG("Write completed immediately");
831                         free(port->write_buf);
832                         port->write_buf = NULL;
833                         RETURN_VALUE("%d", count);
834                 }
835         } else {
836                 /* Blocking write. */
837                 if (WriteFile(port->hdl, buf, count, &written, NULL) == 0) {
838                         RETURN_FAIL("WriteFile() failed");
839                 }
840         }
841
842         RETURN_VALUE("%d", written);
843 #else
844         /* Returns the number of bytes written, or -1 upon failure. */
845         ssize_t written = write(port->fd, buf, count);
846
847         if (written < 0)
848                 RETURN_FAIL("write() failed");
849         else
850                 RETURN_VALUE("%d", written);
851 #endif
852 }
853
854 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
855 {
856         TRACE("%p, %p, %d", port, buf, count);
857
858         CHECK_OPEN_PORT();
859
860         if (!buf)
861                 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
862
863         DEBUG("Reading up to %d bytes from port %s", count, port->name);
864
865 #ifdef _WIN32
866         DWORD bytes_read = 0;
867
868         /* Returns non-zero upon success, 0 upon failure. */
869         if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
870                 RETURN_FAIL("ReadFile() failed");
871         RETURN_VALUE("%d", bytes_read);
872 #else
873         ssize_t bytes_read;
874
875         /* Returns the number of bytes read, or -1 upon failure. */
876         if ((bytes_read = read(port->fd, buf, count)) < 0) {
877                 if (port->nonblocking && errno == EAGAIN)
878                         /* Port is opened in nonblocking mode and there are no bytes available. */
879                         bytes_read = 0;
880                 else
881                         /* This is an actual failure. */
882                         RETURN_FAIL("read() failed");
883         }
884         RETURN_VALUE("%d", bytes_read);
885 #endif
886 }
887
888 #ifdef __linux__
889 static enum sp_return get_baudrate(int fd, int *baudrate)
890 {
891         void *data;
892
893         TRACE("%d, %p", fd, baudrate);
894
895         DEBUG("Getting baud rate");
896
897         if (!(data = malloc(get_termios_size())))
898                 RETURN_ERROR(SP_ERR_MEM, "termios malloc failed");
899
900         if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
901                 free(data);
902                 RETURN_FAIL("getting termios failed");
903         }
904
905         *baudrate = get_termios_speed(data);
906
907         free(data);
908
909         RETURN_OK();
910 }
911
912 static enum sp_return set_baudrate(int fd, int baudrate)
913 {
914         void *data;
915
916         TRACE("%d, %d", fd, baudrate);
917
918         DEBUG("Getting baud rate");
919
920         if (!(data = malloc(get_termios_size())))
921                 RETURN_ERROR(SP_ERR_MEM, "termios malloc failed");
922
923         if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
924                 free(data);
925                 RETURN_FAIL("getting termios failed");
926         }
927
928         DEBUG("Setting baud rate");
929
930         set_termios_speed(data, baudrate);
931
932         if (ioctl(fd, get_termios_set_ioctl(), data) < 0) {
933                 free(data);
934                 RETURN_FAIL("setting termios failed");
935         }
936
937         free(data);
938
939         RETURN_OK();
940 }
941
942 #ifdef USE_TERMIOX
943 static enum sp_return get_flow(int fd, int *flow)
944 {
945         void *data;
946
947         TRACE("%d, %p", fd, flow);
948
949         DEBUG("Getting advanced flow control");
950
951         if (!(data = malloc(get_termiox_size())))
952                 RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed");
953
954         if (ioctl(fd, TCGETX, data) < 0) {
955                 free(data);
956                 RETURN_FAIL("getting termiox failed");
957         }
958
959         *flow = get_termiox_flow(data);
960
961         free(data);
962
963         RETURN_OK();
964 }
965
966 static enum sp_return set_flow(int fd, int flow)
967 {
968         void *data;
969
970         TRACE("%d, %d", fd, flow);
971
972         DEBUG("Getting advanced flow control");
973
974         if (!(data = malloc(get_termiox_size())))
975                 RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed");
976
977         if (ioctl(fd, TCGETX, data) < 0) {
978                 free(data);
979                 RETURN_FAIL("getting termiox failed");
980         }
981
982         DEBUG("Setting advanced flow control");
983
984         set_termiox_flow(data, flow);
985
986         if (ioctl(fd, TCSETX, data) < 0) {
987                 free(data);
988                 RETURN_FAIL("setting termiox failed");
989         }
990
991         free(data);
992
993         RETURN_OK();
994 }
995 #endif /* USE_TERMIOX */
996 #endif /* __linux__ */
997
998 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
999         struct sp_port_config *config)
1000 {
1001         unsigned int i;
1002
1003         TRACE("%p, %p, %p", port, data, config);
1004
1005         DEBUG("Getting configuration for port %s", port->name);
1006
1007 #ifdef _WIN32
1008         if (!GetCommState(port->hdl, &data->dcb))
1009                 RETURN_FAIL("GetCommState() failed");
1010
1011         for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1012                 if (data->dcb.BaudRate == std_baudrates[i].index) {
1013                         config->baudrate = std_baudrates[i].value;
1014                         break;
1015                 }
1016         }
1017
1018         if (i == NUM_STD_BAUDRATES)
1019                 /* BaudRate field can be either an index or a custom baud rate. */
1020                 config->baudrate = data->dcb.BaudRate;
1021
1022         config->bits = data->dcb.ByteSize;
1023
1024         if (data->dcb.fParity)
1025                 switch (data->dcb.Parity) {
1026                 case NOPARITY:
1027                         config->parity = SP_PARITY_NONE;
1028                         break;
1029                 case EVENPARITY:
1030                         config->parity = SP_PARITY_EVEN;
1031                         break;
1032                 case ODDPARITY:
1033                         config->parity = SP_PARITY_ODD;
1034                         break;
1035                 default:
1036                         config->parity = -1;
1037                 }
1038         else
1039                 config->parity = SP_PARITY_NONE;
1040
1041         switch (data->dcb.StopBits) {
1042         case ONESTOPBIT:
1043                 config->stopbits = 1;
1044                 break;
1045         case TWOSTOPBITS:
1046                 config->stopbits = 2;
1047                 break;
1048         default:
1049                 config->stopbits = -1;
1050         }
1051
1052         switch (data->dcb.fRtsControl) {
1053         case RTS_CONTROL_DISABLE:
1054                 config->rts = SP_RTS_OFF;
1055                 break;
1056         case RTS_CONTROL_ENABLE:
1057                 config->rts = SP_RTS_ON;
1058                 break;
1059         case RTS_CONTROL_HANDSHAKE:
1060                 config->rts = SP_RTS_FLOW_CONTROL;
1061                 break;
1062         default:
1063                 config->rts = -1;
1064         }
1065
1066         config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
1067
1068         switch (data->dcb.fDtrControl) {
1069         case DTR_CONTROL_DISABLE:
1070                 config->dtr = SP_DTR_OFF;
1071                 break;
1072         case DTR_CONTROL_ENABLE:
1073                 config->dtr = SP_DTR_ON;
1074                 break;
1075         case DTR_CONTROL_HANDSHAKE:
1076                 config->dtr = SP_DTR_FLOW_CONTROL;
1077                 break;
1078         default:
1079                 config->dtr = -1;
1080         }
1081
1082         config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
1083
1084         if (data->dcb.fInX) {
1085                 if (data->dcb.fOutX)
1086                         config->xon_xoff = SP_XONXOFF_INOUT;
1087                 else
1088                         config->xon_xoff = SP_XONXOFF_IN;
1089         } else {
1090                 if (data->dcb.fOutX)
1091                         config->xon_xoff = SP_XONXOFF_OUT;
1092                 else
1093                         config->xon_xoff = SP_XONXOFF_DISABLED;
1094         }
1095
1096 #else // !_WIN32
1097
1098         if (tcgetattr(port->fd, &data->term) < 0)
1099                 RETURN_FAIL("tcgetattr() failed");
1100
1101         if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
1102                 RETURN_FAIL("TIOCMGET ioctl failed");
1103
1104 #ifdef USE_TERMIOX
1105         int ret = get_flow(port->fd, &data->flow);
1106
1107         if (ret == SP_ERR_FAIL && errno == EINVAL)
1108                 data->termiox_supported = 0;
1109         else if (ret < 0)
1110                 RETURN_CODEVAL(ret);
1111         else
1112                 data->termiox_supported = 1;
1113 #else
1114         data->termiox_supported = 0;
1115 #endif
1116
1117         for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1118                 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
1119                         config->baudrate = std_baudrates[i].value;
1120                         break;
1121                 }
1122         }
1123
1124         if (i == NUM_STD_BAUDRATES) {
1125 #ifdef __APPLE__
1126                 config->baudrate = (int)data->term.c_ispeed;
1127 #elif defined(__linux__)
1128                 TRY(get_baudrate(port->fd, &config->baudrate));
1129 #else
1130                 config->baudrate = -1;
1131 #endif
1132         }
1133
1134         switch (data->term.c_cflag & CSIZE) {
1135         case CS8:
1136                 config->bits = 8;
1137                 break;
1138         case CS7:
1139                 config->bits = 7;
1140                 break;
1141         case CS6:
1142                 config->bits = 6;
1143                 break;
1144         case CS5:
1145                 config->bits = 5;
1146                 break;
1147         default:
1148                 config->bits = -1;
1149         }
1150
1151         if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
1152                 config->parity = SP_PARITY_NONE;
1153         else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
1154                 config->parity = -1;
1155         else
1156                 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
1157
1158         config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
1159
1160         if (data->term.c_cflag & CRTSCTS) {
1161                 config->rts = SP_RTS_FLOW_CONTROL;
1162                 config->cts = SP_CTS_FLOW_CONTROL;
1163         } else {
1164                 if (data->termiox_supported && data->flow & RTS_FLOW)
1165                         config->rts = SP_RTS_FLOW_CONTROL;
1166                 else
1167                         config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
1168
1169                 config->cts = (data->termiox_supported && data->flow & CTS_FLOW) ?
1170                         SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
1171         }
1172
1173         if (data->termiox_supported && data->flow & DTR_FLOW)
1174                 config->dtr = SP_DTR_FLOW_CONTROL;
1175         else
1176                 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
1177
1178         config->dsr = (data->termiox_supported && data->flow & DSR_FLOW) ?
1179                 SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
1180
1181         if (data->term.c_iflag & IXOFF) {
1182                 if (data->term.c_iflag & IXON)
1183                         config->xon_xoff = SP_XONXOFF_INOUT;
1184                 else
1185                         config->xon_xoff = SP_XONXOFF_IN;
1186         } else {
1187                 if (data->term.c_iflag & IXON)
1188                         config->xon_xoff = SP_XONXOFF_OUT;
1189                 else
1190                         config->xon_xoff = SP_XONXOFF_DISABLED;
1191         }
1192 #endif
1193
1194         RETURN_OK();
1195 }
1196
1197 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
1198         const struct sp_port_config *config)
1199 {
1200         unsigned int i;
1201 #ifdef __APPLE__
1202         BAUD_TYPE baud_nonstd;
1203
1204         baud_nonstd = B0;
1205 #endif
1206 #ifdef __linux__
1207         int baud_nonstd = 0;
1208 #endif
1209
1210         TRACE("%p, %p, %p", port, data, config);
1211
1212         DEBUG("Setting configuration for port %s", port->name);
1213
1214 #ifdef _WIN32
1215         if (config->baudrate >= 0) {
1216                 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1217                         if (config->baudrate == std_baudrates[i].value) {
1218                                 data->dcb.BaudRate = std_baudrates[i].index;
1219                                 break;
1220                         }
1221                 }
1222
1223                 if (i == NUM_STD_BAUDRATES)
1224                         data->dcb.BaudRate = config->baudrate;
1225         }
1226
1227         if (config->bits >= 0)
1228                 data->dcb.ByteSize = config->bits;
1229
1230         if (config->parity >= 0) {
1231                 switch (config->parity) {
1232                 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
1233                 case SP_PARITY_NONE:
1234                         data->dcb.Parity = NOPARITY;
1235                         break;
1236                 case SP_PARITY_EVEN:
1237                         data->dcb.Parity = EVENPARITY;
1238                         break;
1239                 case SP_PARITY_ODD:
1240                         data->dcb.Parity = ODDPARITY;
1241                         break;
1242                 default:
1243                         RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting");
1244                 }
1245         }
1246
1247         if (config->stopbits >= 0) {
1248                 switch (config->stopbits) {
1249                 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
1250                 case 1:
1251                         data->dcb.StopBits = ONESTOPBIT;
1252                         break;
1253                 case 2:
1254                         data->dcb.StopBits = TWOSTOPBITS;
1255                         break;
1256                 default:
1257                         RETURN_ERROR(SP_ERR_ARG, "Invalid stop bit setting");
1258                 }
1259         }
1260
1261         if (config->rts >= 0) {
1262                 switch (config->rts) {
1263                 case SP_RTS_OFF:
1264                         data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
1265                         break;
1266                 case SP_RTS_ON:
1267                         data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
1268                         break;
1269                 case SP_RTS_FLOW_CONTROL:
1270                         data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
1271                         break;
1272                 default:
1273                         RETURN_ERROR(SP_ERR_ARG, "Invalid RTS setting");
1274                 }
1275         }
1276
1277         if (config->cts >= 0) {
1278                 switch (config->cts) {
1279                 case SP_CTS_IGNORE:
1280                         data->dcb.fOutxCtsFlow = FALSE;
1281                         break;
1282                 case SP_CTS_FLOW_CONTROL:
1283                         data->dcb.fOutxCtsFlow = TRUE;
1284                         break;
1285                 default:
1286                         RETURN_ERROR(SP_ERR_ARG, "Invalid CTS setting");
1287                 }
1288         }
1289
1290         if (config->dtr >= 0) {
1291                 switch (config->dtr) {
1292                 case SP_DTR_OFF:
1293                         data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
1294                         break;
1295                 case SP_DTR_ON:
1296                         data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
1297                         break;
1298                 case SP_DTR_FLOW_CONTROL:
1299                         data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
1300                         break;
1301                 default:
1302                         RETURN_ERROR(SP_ERR_ARG, "Invalid DTR setting");
1303                 }
1304         }
1305
1306         if (config->dsr >= 0) {
1307                 switch (config->dsr) {
1308                 case SP_DSR_IGNORE:
1309                         data->dcb.fOutxDsrFlow = FALSE;
1310                         break;
1311                 case SP_DSR_FLOW_CONTROL:
1312                         data->dcb.fOutxDsrFlow = TRUE;
1313                         break;
1314                 default:
1315                         RETURN_ERROR(SP_ERR_ARG, "Invalid DSR setting");
1316                 }
1317         }
1318
1319         if (config->xon_xoff >= 0) {
1320                 switch (config->xon_xoff) {
1321                 case SP_XONXOFF_DISABLED:
1322                         data->dcb.fInX = FALSE;
1323                         data->dcb.fOutX = FALSE;
1324                         break;
1325                 case SP_XONXOFF_IN:
1326                         data->dcb.fInX = TRUE;
1327                         data->dcb.fOutX = FALSE;
1328                         break;
1329                 case SP_XONXOFF_OUT:
1330                         data->dcb.fInX = FALSE;
1331                         data->dcb.fOutX = TRUE;
1332                         break;
1333                 case SP_XONXOFF_INOUT:
1334                         data->dcb.fInX = TRUE;
1335                         data->dcb.fOutX = TRUE;
1336                         break;
1337                 default:
1338                         RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting");
1339                 }
1340         }
1341
1342         if (!SetCommState(port->hdl, &data->dcb))
1343                 RETURN_FAIL("SetCommState() failed");
1344
1345 #else /* !_WIN32 */
1346
1347         int controlbits;
1348
1349         if (config->baudrate >= 0) {
1350                 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1351                         if (config->baudrate == std_baudrates[i].value) {
1352                                 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
1353                                         RETURN_FAIL("cfsetospeed() failed");
1354
1355                                 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
1356                                         RETURN_FAIL("cfsetispeed() failed");
1357                                 break;
1358                         }
1359                 }
1360
1361                 /* Non-standard baud rate */
1362                 if (i == NUM_STD_BAUDRATES) {
1363 #ifdef __APPLE__
1364                         /* Set "dummy" baud rate. */
1365                         if (cfsetspeed(&data->term, B9600) < 0)
1366                                 RETURN_FAIL("cfsetspeed() failed");
1367                         baud_nonstd = config->baudrate;
1368 #elif defined(__linux__)
1369                         baud_nonstd = 1;
1370 #else
1371                         RETURN_ERROR(SP_ERR_SUPP, "Non-standard baudrate not supported");
1372 #endif
1373                 }
1374         }
1375
1376         if (config->bits >= 0) {
1377                 data->term.c_cflag &= ~CSIZE;
1378                 switch (config->bits) {
1379                 case 8:
1380                         data->term.c_cflag |= CS8;
1381                         break;
1382                 case 7:
1383                         data->term.c_cflag |= CS7;
1384                         break;
1385                 case 6:
1386                         data->term.c_cflag |= CS6;
1387                         break;
1388                 case 5:
1389                         data->term.c_cflag |= CS5;
1390                         break;
1391                 default:
1392                         RETURN_ERROR(SP_ERR_ARG, "Invalid data bits setting");
1393                 }
1394         }
1395
1396         if (config->parity >= 0) {
1397                 data->term.c_iflag &= ~IGNPAR;
1398                 data->term.c_cflag &= ~(PARENB | PARODD);
1399                 switch (config->parity) {
1400                 case SP_PARITY_NONE:
1401                         data->term.c_iflag |= IGNPAR;
1402                         break;
1403                 case SP_PARITY_EVEN:
1404                         data->term.c_cflag |= PARENB;
1405                         break;
1406                 case SP_PARITY_ODD:
1407                         data->term.c_cflag |= PARENB | PARODD;
1408                         break;
1409                 default:
1410                         RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting");
1411                 }
1412         }
1413
1414         if (config->stopbits >= 0) {
1415                 data->term.c_cflag &= ~CSTOPB;
1416                 switch (config->stopbits) {
1417                 case 1:
1418                         data->term.c_cflag &= ~CSTOPB;
1419                         break;
1420                 case 2:
1421                         data->term.c_cflag |= CSTOPB;
1422                         break;
1423                 default:
1424                         RETURN_ERROR(SP_ERR_ARG, "Invalid stop bits setting");
1425                 }
1426         }
1427
1428         if (config->rts >= 0 || config->cts >= 0) {
1429                 if (data->termiox_supported) {
1430                         data->flow &= ~(RTS_FLOW | CTS_FLOW);
1431                         switch (config->rts) {
1432                         case SP_RTS_OFF:
1433                         case SP_RTS_ON:
1434                                 controlbits = TIOCM_RTS;
1435                                 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1436                                         RETURN_FAIL("Setting RTS signal level failed");
1437                                 break;
1438                         case SP_RTS_FLOW_CONTROL:
1439                                 data->flow |= RTS_FLOW;
1440                                 break;
1441                         default:
1442                                 break;
1443                         }
1444                         if (config->cts == SP_CTS_FLOW_CONTROL)
1445                                 data->flow |= CTS_FLOW;
1446
1447                         if (data->flow & (RTS_FLOW | CTS_FLOW))
1448                                 data->term.c_iflag |= CRTSCTS;
1449                         else
1450                                 data->term.c_iflag &= ~CRTSCTS;
1451                 } else {
1452                         /* Asymmetric use of RTS/CTS not supported. */
1453                         if (data->term.c_iflag & CRTSCTS) {
1454                                 /* Flow control can only be disabled for both RTS & CTS together. */
1455                                 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
1456                                         if (config->cts != SP_CTS_IGNORE)
1457                                                 RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together");
1458                                 }
1459                                 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
1460                                         if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
1461                                                 RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together");
1462                                 }
1463                         } else {
1464                                 /* Flow control can only be enabled for both RTS & CTS together. */
1465                                 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
1466                                         ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
1467                                         RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be enabled together");
1468                         }
1469
1470                         if (config->rts >= 0) {
1471                                 if (config->rts == SP_RTS_FLOW_CONTROL) {
1472                                         data->term.c_iflag |= CRTSCTS;
1473                                 } else {
1474                                         controlbits = TIOCM_RTS;
1475                                         if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
1476                                                         &controlbits) < 0)
1477                                                 RETURN_FAIL("Setting RTS signal level failed");
1478                                 }
1479                         }
1480                 }
1481         }
1482
1483         if (config->dtr >= 0 || config->dsr >= 0) {
1484                 if (data->termiox_supported) {
1485                         data->flow &= ~(DTR_FLOW | DSR_FLOW);
1486                         switch (config->dtr) {
1487                         case SP_DTR_OFF:
1488                         case SP_DTR_ON:
1489                                 controlbits = TIOCM_DTR;
1490                                 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1491                                         RETURN_FAIL("Setting DTR signal level failed");
1492                                 break;
1493                         case SP_DTR_FLOW_CONTROL:
1494                                 data->flow |= DTR_FLOW;
1495                                 break;
1496                         default:
1497                                 break;
1498                         }
1499                         if (config->dsr == SP_DSR_FLOW_CONTROL)
1500                                 data->flow |= DSR_FLOW;
1501                 } else {
1502                         /* DTR/DSR flow control not supported. */
1503                         if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
1504                                 RETURN_ERROR(SP_ERR_SUPP, "DTR/DSR flow control not supported");
1505
1506                         if (config->dtr >= 0) {
1507                                 controlbits = TIOCM_DTR;
1508                                 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
1509                                                 &controlbits) < 0)
1510                                         RETURN_FAIL("Setting DTR signal level failed");
1511                         }
1512                 }
1513         }
1514
1515         if (config->xon_xoff >= 0) {
1516                 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
1517                 switch (config->xon_xoff) {
1518                 case SP_XONXOFF_DISABLED:
1519                         break;
1520                 case SP_XONXOFF_IN:
1521                         data->term.c_iflag |= IXOFF;
1522                         break;
1523                 case SP_XONXOFF_OUT:
1524                         data->term.c_iflag |= IXON | IXANY;
1525                         break;
1526                 case SP_XONXOFF_INOUT:
1527                         data->term.c_iflag |= IXON | IXOFF | IXANY;
1528                         break;
1529                 default:
1530                         RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting");
1531                 }
1532         }
1533
1534         if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
1535                 RETURN_FAIL("tcsetattr() failed");
1536
1537 #ifdef __APPLE__
1538         if (baud_nonstd != B0) {
1539                 if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
1540                         RETURN_FAIL("IOSSIOSPEED ioctl failed");
1541                 /* Set baud rates in data->term to correct, but incompatible
1542                  * with tcsetattr() value, same as delivered by tcgetattr(). */
1543                 if (cfsetspeed(&data->term, baud_nonstd) < 0)
1544                         RETURN_FAIL("cfsetspeed() failed");
1545         }
1546 #elif defined(__linux__)
1547         if (baud_nonstd)
1548                 TRY(set_baudrate(port->fd, config->baudrate));
1549 #ifdef USE_TERMIOX
1550         if (data->termiox_supported)
1551                 TRY(set_flow(port->fd, data->flow));
1552 #endif
1553 #endif
1554
1555 #endif /* !_WIN32 */
1556
1557         RETURN_OK();
1558 }
1559
1560 enum sp_return sp_new_config(struct sp_port_config **config_ptr)
1561 {
1562         TRACE("%p", config_ptr);
1563         struct sp_port_config *config;
1564
1565         if (!config_ptr)
1566                 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
1567
1568         *config_ptr = NULL;
1569
1570         if (!(config = malloc(sizeof(struct sp_port_config))))
1571                 RETURN_ERROR(SP_ERR_MEM, "config malloc failed");
1572
1573         config->baudrate = -1;
1574         config->bits = -1;
1575         config->parity = -1;
1576         config->stopbits = -1;
1577         config->rts = -1;
1578         config->cts = -1;
1579         config->dtr = -1;
1580         config->dsr = -1;
1581
1582         *config_ptr = config;
1583
1584         RETURN_OK();
1585 }
1586
1587 void sp_free_config(struct sp_port_config *config)
1588 {
1589         TRACE("%p", config);
1590
1591         if (!config)
1592                 DEBUG("Null config");
1593         else
1594                 free(config);
1595
1596         RETURN();
1597 }
1598
1599 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config)
1600 {
1601         struct port_data data;
1602
1603         TRACE("%p, %p", port, config);
1604
1605         CHECK_OPEN_PORT();
1606
1607         if (!config)
1608                 RETURN_ERROR(SP_ERR_ARG, "Null config");
1609
1610         TRY(get_config(port, &data, config));
1611
1612         RETURN_OK();
1613 }
1614
1615 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
1616 {
1617         struct port_data data;
1618         struct sp_port_config prev_config;
1619
1620         TRACE("%p, %p", port, config);
1621
1622         CHECK_OPEN_PORT();
1623
1624         if (!config)
1625                 RETURN_ERROR(SP_ERR_ARG, "Null config");
1626
1627         TRY(get_config(port, &data, &prev_config));
1628         TRY(set_config(port, &data, config));
1629
1630         RETURN_OK();
1631 }
1632
1633 #define CREATE_ACCESSORS(x, type) \
1634 enum sp_return sp_set_##x(struct sp_port *port, type x) { \
1635         struct port_data data; \
1636         struct sp_port_config config; \
1637         TRACE("%p, %d", port, x); \
1638         CHECK_OPEN_PORT(); \
1639         TRY(get_config(port, &data, &config)); \
1640         config.x = x; \
1641         TRY(set_config(port, &data, &config)); \
1642         RETURN_OK(); \
1643 } \
1644 enum sp_return sp_get_config_##x(const struct sp_port_config *config, type *x) { \
1645         TRACE("%p", config); \
1646         if (!config) \
1647                 RETURN_ERROR(SP_ERR_ARG, "Null config"); \
1648         *x = config->x; \
1649         RETURN_OK(); \
1650 } \
1651 enum sp_return sp_set_config_##x(struct sp_port_config *config, type x) { \
1652         TRACE("%p, %d", config, x); \
1653         if (!config) \
1654                 RETURN_ERROR(SP_ERR_ARG, "Null config"); \
1655         config->x = x; \
1656         RETURN_OK(); \
1657 }
1658
1659 CREATE_ACCESSORS(baudrate, int)
1660 CREATE_ACCESSORS(bits, int)
1661 CREATE_ACCESSORS(parity, enum sp_parity)
1662 CREATE_ACCESSORS(stopbits, int)
1663 CREATE_ACCESSORS(rts, enum sp_rts)
1664 CREATE_ACCESSORS(cts, enum sp_cts)
1665 CREATE_ACCESSORS(dtr, enum sp_dtr)
1666 CREATE_ACCESSORS(dsr, enum sp_dsr)
1667 CREATE_ACCESSORS(xon_xoff, enum sp_xonxoff)
1668
1669 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol)
1670 {
1671         if (!config)
1672                 RETURN_ERROR(SP_ERR_ARG, "Null configuration");
1673
1674         if (flowcontrol > SP_FLOWCONTROL_DTRDSR)
1675                 RETURN_ERROR(SP_ERR_ARG, "Invalid flow control setting");
1676
1677         if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
1678                 config->xon_xoff = SP_XONXOFF_INOUT;
1679         else
1680                 config->xon_xoff = SP_XONXOFF_DISABLED;
1681
1682         if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
1683                 config->rts = SP_RTS_FLOW_CONTROL;
1684                 config->cts = SP_CTS_FLOW_CONTROL;
1685         } else {
1686                 if (config->rts == SP_RTS_FLOW_CONTROL)
1687                         config->rts = SP_RTS_ON;
1688                 config->cts = SP_CTS_IGNORE;
1689         }
1690
1691         if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
1692                 config->dtr = SP_DTR_FLOW_CONTROL;
1693                 config->dsr = SP_DSR_FLOW_CONTROL;
1694         } else {
1695                 if (config->dtr == SP_DTR_FLOW_CONTROL)
1696                         config->dtr = SP_DTR_ON;
1697                 config->dsr = SP_DSR_IGNORE;
1698         }
1699
1700         RETURN_OK();
1701 }
1702
1703 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
1704 {
1705         struct port_data data;
1706         struct sp_port_config config;
1707
1708         TRACE("%p, %d", port, flowcontrol);
1709
1710         CHECK_OPEN_PORT();
1711
1712         TRY(get_config(port, &data, &config));
1713
1714         TRY(sp_set_config_flowcontrol(&config, flowcontrol));
1715
1716         TRY(set_config(port, &data, &config));
1717
1718         RETURN_OK();
1719 }
1720
1721 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals)
1722 {
1723         TRACE("%p, %p", port, signals);
1724
1725         CHECK_OPEN_PORT();
1726
1727         if (!signals)
1728                 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
1729
1730         DEBUG("Getting control signals for port %s", port->name);
1731
1732         *signals = 0;
1733 #ifdef _WIN32
1734         DWORD bits;
1735         if (GetCommModemStatus(port->hdl, &bits) == 0)
1736                 RETURN_FAIL("GetCommModemStatus() failed");
1737         if (bits & MS_CTS_ON)
1738                 *signals |= SP_SIG_CTS;
1739         if (bits & MS_DSR_ON)
1740                 *signals |= SP_SIG_DSR;
1741         if (bits & MS_RLSD_ON)
1742                 *signals |= SP_SIG_DCD;
1743         if (bits & MS_RING_ON)
1744                 *signals |= SP_SIG_RI;
1745 #else
1746         int bits;
1747         if (ioctl(port->fd, TIOCMGET, &bits) < 0)
1748                 RETURN_FAIL("TIOCMGET ioctl failed");
1749         if (bits & TIOCM_CTS)
1750                 *signals |= SP_SIG_CTS;
1751         if (bits & TIOCM_DSR)
1752                 *signals |= SP_SIG_DSR;
1753         if (bits & TIOCM_CAR)
1754                 *signals |= SP_SIG_DCD;
1755         if (bits & TIOCM_RNG)
1756                 *signals |= SP_SIG_RI;
1757 #endif
1758         RETURN_OK();
1759 }
1760
1761 enum sp_return sp_start_break(struct sp_port *port)
1762 {
1763         TRACE("%p", port);
1764
1765         CHECK_OPEN_PORT();
1766 #ifdef _WIN32
1767         if (SetCommBreak(port->hdl) == 0)
1768                 RETURN_FAIL("SetCommBreak() failed");
1769 #else
1770         if (ioctl(port->fd, TIOCSBRK, 1) < 0)
1771                 RETURN_FAIL("TIOCSBRK ioctl failed");
1772 #endif
1773
1774         RETURN_OK();
1775 }
1776
1777 enum sp_return sp_end_break(struct sp_port *port)
1778 {
1779         TRACE("%p", port);
1780
1781         CHECK_OPEN_PORT();
1782 #ifdef _WIN32
1783         if (ClearCommBreak(port->hdl) == 0)
1784                 RETURN_FAIL("ClearCommBreak() failed");
1785 #else
1786         if (ioctl(port->fd, TIOCCBRK, 1) < 0)
1787                 RETURN_FAIL("TIOCCBRK ioctl failed");
1788 #endif
1789
1790         RETURN_OK();
1791 }
1792
1793 int sp_last_error_code(void)
1794 {
1795         TRACE("");
1796 #ifdef _WIN32
1797         RETURN_VALUE("%d", GetLastError());
1798 #else
1799         RETURN_VALUE("%d", errno);
1800 #endif
1801 }
1802
1803 char *sp_last_error_message(void)
1804 {
1805         TRACE("");
1806
1807 #ifdef _WIN32
1808         LPVOID message;
1809         DWORD error = GetLastError();
1810
1811         FormatMessage(
1812                 FORMAT_MESSAGE_ALLOCATE_BUFFER |
1813                 FORMAT_MESSAGE_FROM_SYSTEM |
1814                 FORMAT_MESSAGE_IGNORE_INSERTS,
1815                 NULL,
1816                 error,
1817                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1818                 (LPTSTR) &message,
1819                 0, NULL );
1820
1821         RETURN_VALUE("%s", message);
1822 #else
1823         RETURN_VALUE("%s", strerror(errno));
1824 #endif
1825 }
1826
1827 void sp_free_error_message(char *message)
1828 {
1829         TRACE("%s", message);
1830
1831 #ifdef _WIN32
1832         LocalFree(message);
1833 #else
1834         (void)message;
1835 #endif
1836
1837         RETURN();
1838 }
1839
1840 void sp_set_debug_handler(void (*handler)(const char *format, ...))
1841 {
1842         TRACE("%p", handler);
1843
1844         sp_debug_handler = handler;
1845
1846         RETURN();
1847 }
1848
1849 void sp_default_debug_handler(const char *format, ...)
1850 {
1851         va_list args;
1852         va_start(args, format);
1853         if (getenv("LIBSERIALPORT_DEBUG")) {
1854                 fputs("libserialport: ", stderr);
1855                 vfprintf(stderr, format, args);
1856         }
1857         va_end(args);
1858 }