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