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