2 * This file is part of the libserialport project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2010-2015 Uwe Hermann <uwe@hermann-uwe.de>
6 * Copyright (C) 2013-2015 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>
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.
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.
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/>.
25 #include "libserialport.h"
26 #include "libserialport_internal.h"
28 static const struct std_baudrate std_baudrates[] = {
31 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
32 * have documented CBR_* macros.
34 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
35 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
36 BAUD(115200), BAUD(128000), BAUD(256000),
38 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
39 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
40 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
42 #if !defined(__APPLE__) && !defined(__OpenBSD__)
48 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
50 void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
52 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
53 struct sp_port_config *config);
55 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
56 const struct sp_port_config *config);
58 SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
61 #ifndef NO_PORT_METADATA
66 TRACE("%s, %p", portname, port_ptr);
69 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
74 RETURN_ERROR(SP_ERR_ARG, "Null port name");
76 DEBUG_FMT("Building structure for port %s", portname);
78 #if !defined(_WIN32) && defined(HAVE_REALPATH)
80 * get_port_details() below tries to be too smart and figure out
81 * some transport properties from the port name which breaks with
82 * symlinks. Therefore we canonicalize the portname first.
84 char pathbuf[PATH_MAX + 1];
85 char *res = realpath(portname, pathbuf);
87 RETURN_ERROR(SP_ERR_ARG, "Could not retrieve realpath behind port name");
92 if (!(port = malloc(sizeof(struct sp_port))))
93 RETURN_ERROR(SP_ERR_MEM, "Port structure malloc failed");
95 len = strlen(portname) + 1;
97 if (!(port->name = malloc(len))) {
99 RETURN_ERROR(SP_ERR_MEM, "Port name malloc failed");
102 memcpy(port->name, portname, len);
105 port->usb_path = NULL;
106 port->hdl = INVALID_HANDLE_VALUE;
111 port->description = NULL;
112 port->transport = SP_TRANSPORT_NATIVE;
114 port->usb_address = -1;
117 port->usb_manufacturer = NULL;
118 port->usb_product = NULL;
119 port->usb_serial = NULL;
120 port->bluetooth_address = NULL;
122 #ifndef NO_PORT_METADATA
123 if ((ret = get_port_details(port)) != SP_OK) {
134 SP_API char *sp_get_port_name(const struct sp_port *port)
141 RETURN_STRING(port->name);
144 SP_API char *sp_get_port_description(const struct sp_port *port)
148 if (!port || !port->description)
151 RETURN_STRING(port->description);
154 SP_API enum sp_transport sp_get_port_transport(const struct sp_port *port)
159 RETURN_ERROR(SP_ERR_ARG, "Null port");
161 RETURN_INT(port->transport);
164 SP_API enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port,
165 int *usb_bus,int *usb_address)
170 RETURN_ERROR(SP_ERR_ARG, "Null port");
171 if (port->transport != SP_TRANSPORT_USB)
172 RETURN_ERROR(SP_ERR_ARG, "Port does not use USB transport");
173 if (port->usb_bus < 0 || port->usb_address < 0)
174 RETURN_ERROR(SP_ERR_SUPP, "Bus and address values are not available");
177 *usb_bus = port->usb_bus;
179 *usb_address = port->usb_address;
184 SP_API enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port,
185 int *usb_vid, int *usb_pid)
190 RETURN_ERROR(SP_ERR_ARG, "Null port");
191 if (port->transport != SP_TRANSPORT_USB)
192 RETURN_ERROR(SP_ERR_ARG, "Port does not use USB transport");
193 if (port->usb_vid < 0 || port->usb_pid < 0)
194 RETURN_ERROR(SP_ERR_SUPP, "VID:PID values are not available");
197 *usb_vid = port->usb_vid;
199 *usb_pid = port->usb_pid;
204 SP_API char *sp_get_port_usb_manufacturer(const struct sp_port *port)
208 if (!port || port->transport != SP_TRANSPORT_USB || !port->usb_manufacturer)
211 RETURN_STRING(port->usb_manufacturer);
214 SP_API char *sp_get_port_usb_product(const struct sp_port *port)
218 if (!port || port->transport != SP_TRANSPORT_USB || !port->usb_product)
221 RETURN_STRING(port->usb_product);
224 SP_API char *sp_get_port_usb_serial(const struct sp_port *port)
228 if (!port || port->transport != SP_TRANSPORT_USB || !port->usb_serial)
231 RETURN_STRING(port->usb_serial);
234 SP_API char *sp_get_port_bluetooth_address(const struct sp_port *port)
238 if (!port || port->transport != SP_TRANSPORT_BLUETOOTH
239 || !port->bluetooth_address)
242 RETURN_STRING(port->bluetooth_address);
245 SP_API enum sp_return sp_get_port_handle(const struct sp_port *port,
248 TRACE("%p, %p", port, result_ptr);
251 RETURN_ERROR(SP_ERR_ARG, "Null port");
253 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
256 HANDLE *handle_ptr = result_ptr;
257 *handle_ptr = port->hdl;
259 int *fd_ptr = result_ptr;
266 SP_API enum sp_return sp_copy_port(const struct sp_port *port,
267 struct sp_port **copy_ptr)
269 TRACE("%p, %p", port, copy_ptr);
272 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
277 RETURN_ERROR(SP_ERR_ARG, "Null port");
280 RETURN_ERROR(SP_ERR_ARG, "Null port name");
282 DEBUG("Copying port structure");
284 RETURN_INT(sp_get_port_by_name(port->name, copy_ptr));
287 SP_API void sp_free_port(struct sp_port *port)
296 DEBUG("Freeing port structure");
300 if (port->description)
301 free(port->description);
302 if (port->usb_manufacturer)
303 free(port->usb_manufacturer);
304 if (port->usb_product)
305 free(port->usb_product);
306 if (port->usb_serial)
307 free(port->usb_serial);
308 if (port->bluetooth_address)
309 free(port->bluetooth_address);
312 free(port->usb_path);
320 SP_PRIV struct sp_port **list_append(struct sp_port **list,
321 const char *portname)
326 for (count = 0; list[count]; count++);
327 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
330 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
332 list[count + 1] = NULL;
336 sp_free_port_list(list);
340 SP_API enum sp_return sp_list_ports(struct sp_port ***list_ptr)
342 #ifndef NO_ENUMERATION
343 struct sp_port **list;
347 TRACE("%p", list_ptr);
350 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
354 #ifdef NO_ENUMERATION
355 RETURN_ERROR(SP_ERR_SUPP, "Enumeration not supported on this platform");
357 DEBUG("Enumerating ports");
359 if (!(list = malloc(sizeof(struct sp_port *))))
360 RETURN_ERROR(SP_ERR_MEM, "Port list malloc failed");
364 ret = list_ports(&list);
369 sp_free_port_list(list);
377 SP_API void sp_free_port_list(struct sp_port **list)
388 DEBUG("Freeing port list");
390 for (i = 0; list[i]; i++)
391 sp_free_port(list[i]);
397 #define CHECK_PORT() do { \
399 RETURN_ERROR(SP_ERR_ARG, "Null port"); \
401 RETURN_ERROR(SP_ERR_ARG, "Null port name"); \
404 #define CHECK_PORT_HANDLE() do { \
405 if (port->hdl == INVALID_HANDLE_VALUE) \
406 RETURN_ERROR(SP_ERR_ARG, "Port not open"); \
409 #define CHECK_PORT_HANDLE() do { \
411 RETURN_ERROR(SP_ERR_ARG, "Port not open"); \
414 #define CHECK_OPEN_PORT() do { \
416 CHECK_PORT_HANDLE(); \
420 /** To be called after port receive buffer is emptied. */
421 static enum sp_return restart_wait(struct sp_port *port)
425 if (port->wait_running) {
426 /* Check status of running wait operation. */
427 if (GetOverlappedResult(port->hdl, &port->wait_ovl,
428 &wait_result, FALSE)) {
429 DEBUG("Previous wait completed");
430 port->wait_running = FALSE;
431 } else if (GetLastError() == ERROR_IO_INCOMPLETE) {
432 DEBUG("Previous wait still running");
435 RETURN_FAIL("GetOverlappedResult() failed");
439 if (!port->wait_running) {
440 /* Start new wait operation. */
441 if (WaitCommEvent(port->hdl, &port->events,
443 DEBUG("New wait returned, events already pending");
444 } else if (GetLastError() == ERROR_IO_PENDING) {
445 DEBUG("New wait running in background");
446 port->wait_running = TRUE;
448 RETURN_FAIL("WaitCommEvent() failed");
456 SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
458 struct port_data data;
459 struct sp_port_config config;
462 TRACE("%p, 0x%x", port, flags);
466 if (flags > SP_MODE_READ_WRITE)
467 RETURN_ERROR(SP_ERR_ARG, "Invalid flags");
469 DEBUG_FMT("Opening port %s", port->name);
472 DWORD desired_access = 0, flags_and_attributes = 0, errors;
473 char *escaped_port_name;
476 /* Prefix port name with '\\.\' to work with ports above COM9. */
477 if (!(escaped_port_name = malloc(strlen(port->name) + 5)))
478 RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed");
479 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
481 /* Map 'flags' to the OS-specific settings. */
482 flags_and_attributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED;
483 if (flags & SP_MODE_READ)
484 desired_access |= GENERIC_READ;
485 if (flags & SP_MODE_WRITE)
486 desired_access |= GENERIC_WRITE;
488 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
489 OPEN_EXISTING, flags_and_attributes, 0);
491 free(escaped_port_name);
493 if (port->hdl == INVALID_HANDLE_VALUE)
494 RETURN_FAIL("Port CreateFile() failed");
496 /* All timeouts initially disabled. */
497 port->timeouts.ReadIntervalTimeout = 0;
498 port->timeouts.ReadTotalTimeoutMultiplier = 0;
499 port->timeouts.ReadTotalTimeoutConstant = 0;
500 port->timeouts.WriteTotalTimeoutMultiplier = 0;
501 port->timeouts.WriteTotalTimeoutConstant = 0;
503 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) {
505 RETURN_FAIL("SetCommTimeouts() failed");
508 /* Prepare OVERLAPPED structures. */
509 #define INIT_OVERLAPPED(ovl) do { \
510 memset(&port->ovl, 0, sizeof(port->ovl)); \
511 port->ovl.hEvent = INVALID_HANDLE_VALUE; \
512 if ((port->ovl.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL)) \
513 == INVALID_HANDLE_VALUE) { \
515 RETURN_FAIL(#ovl "CreateEvent() failed"); \
519 INIT_OVERLAPPED(read_ovl);
520 INIT_OVERLAPPED(write_ovl);
521 INIT_OVERLAPPED(wait_ovl);
523 /* Set event mask for RX and error events. */
524 if (SetCommMask(port->hdl, EV_RXCHAR | EV_ERR) == 0) {
526 RETURN_FAIL("SetCommMask() failed");
529 port->writing = FALSE;
530 port->wait_running = FALSE;
532 ret = restart_wait(port);
539 int flags_local = O_NONBLOCK | O_NOCTTY;
541 /* Map 'flags' to the OS-specific settings. */
542 if ((flags & SP_MODE_READ_WRITE) == SP_MODE_READ_WRITE)
543 flags_local |= O_RDWR;
544 else if (flags & SP_MODE_READ)
545 flags_local |= O_RDONLY;
546 else if (flags & SP_MODE_WRITE)
547 flags_local |= O_WRONLY;
549 if ((port->fd = open(port->name, flags_local)) < 0)
550 RETURN_FAIL("open() failed");
553 ret = get_config(port, &data, &config);
560 /* Set sane port settings. */
562 data.dcb.fBinary = TRUE;
563 data.dcb.fDsrSensitivity = FALSE;
564 data.dcb.fErrorChar = FALSE;
565 data.dcb.fNull = FALSE;
566 data.dcb.fAbortOnError = FALSE;
568 /* Turn off all fancy termios tricks, give us a raw channel. */
569 data.term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IMAXBEL);
571 data.term.c_iflag &= ~IUCLC;
573 data.term.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
575 data.term.c_oflag &= ~OLCUC;
578 data.term.c_oflag &= ~NLDLY;
581 data.term.c_oflag &= ~CRDLY;
584 data.term.c_oflag &= ~TABDLY;
587 data.term.c_oflag &= ~BSDLY;
590 data.term.c_oflag &= ~VTDLY;
593 data.term.c_oflag &= ~FFDLY;
596 data.term.c_oflag &= ~OFILL;
598 data.term.c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN);
599 data.term.c_cc[VMIN] = 0;
600 data.term.c_cc[VTIME] = 0;
602 /* Ignore modem status lines; enable receiver; leave control lines alone on close. */
603 data.term.c_cflag |= (CLOCAL | CREAD | HUPCL);
607 if (ClearCommError(port->hdl, &errors, &status) == 0)
608 RETURN_FAIL("ClearCommError() failed");
611 ret = set_config(port, &data, &config);
621 SP_API enum sp_return sp_close(struct sp_port *port)
627 DEBUG_FMT("Closing port %s", port->name);
630 /* Returns non-zero upon success, 0 upon failure. */
631 if (CloseHandle(port->hdl) == 0)
632 RETURN_FAIL("Port CloseHandle() failed");
633 port->hdl = INVALID_HANDLE_VALUE;
635 /* Close event handles for overlapped structures. */
636 #define CLOSE_OVERLAPPED(ovl) do { \
637 if (port->ovl.hEvent != INVALID_HANDLE_VALUE && \
638 CloseHandle(port->ovl.hEvent) == 0) \
639 RETURN_FAIL(# ovl "event CloseHandle() failed"); \
641 CLOSE_OVERLAPPED(read_ovl);
642 CLOSE_OVERLAPPED(write_ovl);
643 CLOSE_OVERLAPPED(wait_ovl);
646 /* Returns 0 upon success, -1 upon failure. */
647 if (close(port->fd) == -1)
648 RETURN_FAIL("close() failed");
655 SP_API enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
657 TRACE("%p, 0x%x", port, buffers);
661 if (buffers > SP_BUF_BOTH)
662 RETURN_ERROR(SP_ERR_ARG, "Invalid buffer selection");
664 const char *buffer_names[] = {"no", "input", "output", "both"};
666 DEBUG_FMT("Flushing %s buffers on port %s",
667 buffer_names[buffers], port->name);
671 if (buffers & SP_BUF_INPUT)
672 flags |= PURGE_RXCLEAR;
673 if (buffers & SP_BUF_OUTPUT)
674 flags |= PURGE_TXCLEAR;
676 /* Returns non-zero upon success, 0 upon failure. */
677 if (PurgeComm(port->hdl, flags) == 0)
678 RETURN_FAIL("PurgeComm() failed");
680 if (buffers & SP_BUF_INPUT)
681 TRY(restart_wait(port));
684 if (buffers == SP_BUF_BOTH)
686 else if (buffers == SP_BUF_INPUT)
688 else if (buffers == SP_BUF_OUTPUT)
691 /* Returns 0 upon success, -1 upon failure. */
692 if (tcflush(port->fd, flags) < 0)
693 RETURN_FAIL("tcflush() failed");
698 SP_API enum sp_return sp_drain(struct sp_port *port)
704 DEBUG_FMT("Draining port %s", port->name);
707 /* Returns non-zero upon success, 0 upon failure. */
708 if (FlushFileBuffers(port->hdl) == 0)
709 RETURN_FAIL("FlushFileBuffers() failed");
716 result = ioctl(port->fd, TCSBRK, &arg);
718 result = tcdrain(port->fd);
721 if (errno == EINTR) {
722 DEBUG("tcdrain() was interrupted");
725 RETURN_FAIL("tcdrain() failed");
735 static enum sp_return await_write_completion(struct sp_port *port)
741 /* Wait for previous non-blocking write to complete, if any. */
743 DEBUG("Waiting for previous write to complete");
744 result = GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
747 RETURN_FAIL("Previous write failed to complete");
748 DEBUG("Previous write completed");
755 SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
756 size_t count, unsigned int timeout_ms)
758 TRACE("%p, %p, %d, %d", port, buf, count, timeout_ms);
763 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
766 DEBUG_FMT("Writing %d bytes to port %s, timeout %d ms",
767 count, port->name, timeout_ms);
769 DEBUG_FMT("Writing %d bytes to port %s, no timeout",
776 DWORD bytes_written = 0;
778 TRY(await_write_completion(port));
781 if (port->timeouts.WriteTotalTimeoutConstant != timeout_ms) {
782 port->timeouts.WriteTotalTimeoutConstant = timeout_ms;
783 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
784 RETURN_FAIL("SetCommTimeouts() failed");
788 if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl)) {
789 DEBUG("Write completed immediately");
791 } else if (GetLastError() == ERROR_IO_PENDING) {
792 DEBUG("Waiting for write to complete");
793 if (GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE) == 0) {
794 if (GetLastError() == ERROR_SEM_TIMEOUT) {
795 DEBUG("Write timed out");
798 RETURN_FAIL("GetOverlappedResult() failed");
801 DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count);
802 RETURN_INT(bytes_written);
804 RETURN_FAIL("WriteFile() failed");
807 size_t bytes_written = 0;
808 unsigned char *ptr = (unsigned char *) buf;
809 struct timeval start, delta, now, end = {0, 0};
815 /* Get time at start of operation. */
816 gettimeofday(&start, NULL);
817 /* Define duration of timeout. */
818 delta.tv_sec = timeout_ms / 1000;
819 delta.tv_usec = (timeout_ms % 1000) * 1000;
820 /* Calculate time at which we should give up. */
821 timeradd(&start, &delta, &end);
825 FD_SET(port->fd, &fds);
827 /* Loop until we have written the requested number of bytes. */
828 while (bytes_written < count) {
830 * Check timeout only if we have run select() at least once,
831 * to avoid any issues if a short timeout is reached before
832 * select() is even run.
834 if (timeout_ms && started) {
835 gettimeofday(&now, NULL);
836 if (timercmp(&now, &end, >))
837 /* Timeout has expired. */
839 timersub(&end, &now, &delta);
841 result = select(port->fd + 1, NULL, &fds, NULL, timeout_ms ? &delta : NULL);
844 if (errno == EINTR) {
845 DEBUG("select() call was interrupted, repeating");
848 RETURN_FAIL("select() failed");
850 } else if (result == 0) {
851 /* Timeout has expired. */
856 result = write(port->fd, ptr, count - bytes_written);
860 /* This shouldn't happen because we did a select() first, but handle anyway. */
863 /* This is an actual failure. */
864 RETURN_FAIL("write() failed");
867 bytes_written += result;
871 if (bytes_written < count)
872 DEBUG("Write timed out");
874 RETURN_INT(bytes_written);
878 SP_API enum sp_return sp_nonblocking_write(struct sp_port *port,
879 const void *buf, size_t count)
881 TRACE("%p, %p, %d", port, buf, count);
886 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
888 DEBUG_FMT("Writing up to %d bytes to port %s", count, port->name);
895 BYTE *ptr = (BYTE *) buf;
897 /* Check whether previous write is complete. */
899 if (HasOverlappedIoCompleted(&port->write_ovl)) {
900 DEBUG("Previous write completed");
903 DEBUG("Previous write not complete");
904 /* Can't take a new write until the previous one finishes. */
910 if (port->timeouts.WriteTotalTimeoutConstant != 0) {
911 port->timeouts.WriteTotalTimeoutConstant = 0;
912 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
913 RETURN_FAIL("SetCommTimeouts() failed");
917 * Keep writing data until the OS has to actually start an async IO
918 * for it. At that point we know the buffer is full.
920 while (written < count) {
921 /* Copy first byte of user buffer. */
922 port->pending_byte = *ptr++;
924 /* Start asynchronous write. */
925 if (WriteFile(port->hdl, &port->pending_byte, 1, NULL, &port->write_ovl) == 0) {
926 if (GetLastError() == ERROR_IO_PENDING) {
927 if (HasOverlappedIoCompleted(&port->write_ovl)) {
928 DEBUG("Asynchronous write completed immediately");
933 DEBUG("Asynchronous write running");
935 RETURN_INT(++written);
938 /* Actual failure of some kind. */
939 RETURN_FAIL("WriteFile() failed");
942 DEBUG("Single byte written immediately");
947 DEBUG("All bytes written immediately");
951 /* Returns the number of bytes written, or -1 upon failure. */
952 ssize_t written = write(port->fd, buf, count);
955 RETURN_FAIL("write() failed");
962 /* Restart wait operation if buffer was emptied. */
963 static enum sp_return restart_wait_if_needed(struct sp_port *port, unsigned int bytes_read)
971 if (ClearCommError(port->hdl, &errors, &comstat) == 0)
972 RETURN_FAIL("ClearCommError() failed");
974 if (comstat.cbInQue == 0)
975 TRY(restart_wait(port));
981 SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
982 size_t count, unsigned int timeout_ms)
984 TRACE("%p, %p, %d, %d", port, buf, count, timeout_ms);
989 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
992 DEBUG_FMT("Reading %d bytes from port %s, timeout %d ms",
993 count, port->name, timeout_ms);
995 DEBUG_FMT("Reading %d bytes from port %s, no timeout",
1002 DWORD bytes_read = 0;
1005 if (port->timeouts.ReadIntervalTimeout != 0 ||
1006 port->timeouts.ReadTotalTimeoutMultiplier != 0 ||
1007 port->timeouts.ReadTotalTimeoutConstant != timeout_ms) {
1008 port->timeouts.ReadIntervalTimeout = 0;
1009 port->timeouts.ReadTotalTimeoutMultiplier = 0;
1010 port->timeouts.ReadTotalTimeoutConstant = timeout_ms;
1011 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
1012 RETURN_FAIL("SetCommTimeouts() failed");
1016 if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl)) {
1017 DEBUG("Read completed immediately");
1019 } else if (GetLastError() == ERROR_IO_PENDING) {
1020 DEBUG("Waiting for read to complete");
1021 if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0)
1022 RETURN_FAIL("GetOverlappedResult() failed");
1023 DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count);
1025 RETURN_FAIL("ReadFile() failed");
1028 TRY(restart_wait_if_needed(port, bytes_read));
1030 RETURN_INT(bytes_read);
1033 size_t bytes_read = 0;
1034 unsigned char *ptr = (unsigned char *) buf;
1035 struct timeval start, delta, now, end = {0, 0};
1041 /* Get time at start of operation. */
1042 gettimeofday(&start, NULL);
1043 /* Define duration of timeout. */
1044 delta.tv_sec = timeout_ms / 1000;
1045 delta.tv_usec = (timeout_ms % 1000) * 1000;
1046 /* Calculate time at which we should give up. */
1047 timeradd(&start, &delta, &end);
1051 FD_SET(port->fd, &fds);
1053 /* Loop until we have the requested number of bytes. */
1054 while (bytes_read < count) {
1056 * Check timeout only if we have run select() at least once,
1057 * to avoid any issues if a short timeout is reached before
1058 * select() is even run.
1060 if (timeout_ms && started) {
1061 gettimeofday(&now, NULL);
1062 if (timercmp(&now, &end, >))
1063 /* Timeout has expired. */
1065 timersub(&end, &now, &delta);
1067 result = select(port->fd + 1, &fds, NULL, NULL, timeout_ms ? &delta : NULL);
1070 if (errno == EINTR) {
1071 DEBUG("select() call was interrupted, repeating");
1074 RETURN_FAIL("select() failed");
1076 } else if (result == 0) {
1077 /* Timeout has expired. */
1082 result = read(port->fd, ptr, count - bytes_read);
1085 if (errno == EAGAIN)
1087 * This shouldn't happen because we did a
1088 * select() first, but handle anyway.
1092 /* This is an actual failure. */
1093 RETURN_FAIL("read() failed");
1096 bytes_read += result;
1100 if (bytes_read < count)
1101 DEBUG("Read timed out");
1103 RETURN_INT(bytes_read);
1107 SP_API enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf,
1108 size_t count, unsigned int timeout_ms)
1110 TRACE("%p, %p, %d, %d", port, buf, count, timeout_ms);
1115 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
1118 RETURN_ERROR(SP_ERR_ARG, "Zero count");
1121 DEBUG_FMT("Reading next max %d bytes from port %s, timeout %d ms",
1122 count, port->name, timeout_ms);
1124 DEBUG_FMT("Reading next max %d bytes from port %s, no timeout",
1128 DWORD bytes_read = 0;
1130 /* If timeout_ms == 0, set maximum timeout. */
1131 DWORD timeout_val = (timeout_ms == 0 ? MAXDWORD - 1 : timeout_ms);
1134 if (port->timeouts.ReadIntervalTimeout != MAXDWORD ||
1135 port->timeouts.ReadTotalTimeoutMultiplier != MAXDWORD ||
1136 port->timeouts.ReadTotalTimeoutConstant != timeout_val) {
1137 port->timeouts.ReadIntervalTimeout = MAXDWORD;
1138 port->timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
1139 port->timeouts.ReadTotalTimeoutConstant = timeout_val;
1140 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
1141 RETURN_FAIL("SetCommTimeouts() failed");
1144 /* Loop until we have at least one byte, or timeout is reached. */
1145 while (bytes_read == 0) {
1147 if (ReadFile(port->hdl, buf, count, &bytes_read, &port->read_ovl)) {
1148 DEBUG("Read completed immediately");
1149 } else if (GetLastError() == ERROR_IO_PENDING) {
1150 DEBUG("Waiting for read to complete");
1151 if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0)
1152 RETURN_FAIL("GetOverlappedResult() failed");
1153 if (bytes_read > 0) {
1154 DEBUG("Read completed");
1155 } else if (timeout_ms > 0) {
1156 DEBUG("Read timed out");
1159 DEBUG("Restarting read");
1162 RETURN_FAIL("ReadFile() failed");
1166 TRY(restart_wait_if_needed(port, bytes_read));
1168 RETURN_INT(bytes_read);
1171 size_t bytes_read = 0;
1172 struct timeval start, delta, now, end = {0, 0};
1178 /* Get time at start of operation. */
1179 gettimeofday(&start, NULL);
1180 /* Define duration of timeout. */
1181 delta.tv_sec = timeout_ms / 1000;
1182 delta.tv_usec = (timeout_ms % 1000) * 1000;
1183 /* Calculate time at which we should give up. */
1184 timeradd(&start, &delta, &end);
1188 FD_SET(port->fd, &fds);
1190 /* Loop until we have at least one byte, or timeout is reached. */
1191 while (bytes_read == 0) {
1193 * Check timeout only if we have run select() at least once,
1194 * to avoid any issues if a short timeout is reached before
1195 * select() is even run.
1197 if (timeout_ms && started) {
1198 gettimeofday(&now, NULL);
1199 if (timercmp(&now, &end, >))
1200 /* Timeout has expired. */
1202 timersub(&end, &now, &delta);
1204 result = select(port->fd + 1, &fds, NULL, NULL, timeout_ms ? &delta : NULL);
1207 if (errno == EINTR) {
1208 DEBUG("select() call was interrupted, repeating");
1211 RETURN_FAIL("select() failed");
1213 } else if (result == 0) {
1214 /* Timeout has expired. */
1219 result = read(port->fd, buf, count);
1222 if (errno == EAGAIN)
1223 /* This shouldn't happen because we did a select() first, but handle anyway. */
1226 /* This is an actual failure. */
1227 RETURN_FAIL("read() failed");
1230 bytes_read = result;
1233 if (bytes_read == 0)
1234 DEBUG("Read timed out");
1236 RETURN_INT(bytes_read);
1240 SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf,
1243 TRACE("%p, %p, %d", port, buf, count);
1248 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
1250 DEBUG_FMT("Reading up to %d bytes from port %s", count, port->name);
1256 if (port->timeouts.ReadIntervalTimeout != MAXDWORD ||
1257 port->timeouts.ReadTotalTimeoutMultiplier != 0 ||
1258 port->timeouts.ReadTotalTimeoutConstant != 0) {
1259 port->timeouts.ReadIntervalTimeout = MAXDWORD;
1260 port->timeouts.ReadTotalTimeoutMultiplier = 0;
1261 port->timeouts.ReadTotalTimeoutConstant = 0;
1262 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
1263 RETURN_FAIL("SetCommTimeouts() failed");
1267 if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0)
1268 if (GetLastError() != ERROR_IO_PENDING)
1269 RETURN_FAIL("ReadFile() failed");
1271 /* Get number of bytes read. */
1272 if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, FALSE) == 0)
1273 RETURN_FAIL("GetOverlappedResult() failed");
1275 TRY(restart_wait_if_needed(port, bytes_read));
1277 RETURN_INT(bytes_read);
1281 /* Returns the number of bytes read, or -1 upon failure. */
1282 if ((bytes_read = read(port->fd, buf, count)) < 0) {
1283 if (errno == EAGAIN)
1284 /* No bytes available. */
1287 /* This is an actual failure. */
1288 RETURN_FAIL("read() failed");
1290 RETURN_INT(bytes_read);
1294 SP_API enum sp_return sp_input_waiting(struct sp_port *port)
1300 DEBUG_FMT("Checking input bytes waiting on port %s", port->name);
1306 if (ClearCommError(port->hdl, &errors, &comstat) == 0)
1307 RETURN_FAIL("ClearCommError() failed");
1308 RETURN_INT(comstat.cbInQue);
1311 if (ioctl(port->fd, TIOCINQ, &bytes_waiting) < 0)
1312 RETURN_FAIL("TIOCINQ ioctl failed");
1313 RETURN_INT(bytes_waiting);
1317 SP_API enum sp_return sp_output_waiting(struct sp_port *port)
1323 DEBUG_FMT("Checking output bytes waiting on port %s", port->name);
1329 if (ClearCommError(port->hdl, &errors, &comstat) == 0)
1330 RETURN_FAIL("ClearCommError() failed");
1331 RETURN_INT(comstat.cbOutQue);
1334 if (ioctl(port->fd, TIOCOUTQ, &bytes_waiting) < 0)
1335 RETURN_FAIL("TIOCOUTQ ioctl failed");
1336 RETURN_INT(bytes_waiting);
1340 SP_API enum sp_return sp_new_event_set(struct sp_event_set **result_ptr)
1342 struct sp_event_set *result;
1344 TRACE("%p", result_ptr);
1347 RETURN_ERROR(SP_ERR_ARG, "Null result");
1351 if (!(result = malloc(sizeof(struct sp_event_set))))
1352 RETURN_ERROR(SP_ERR_MEM, "sp_event_set malloc() failed");
1354 memset(result, 0, sizeof(struct sp_event_set));
1356 *result_ptr = result;
1361 static enum sp_return add_handle(struct sp_event_set *event_set,
1362 event_handle handle, enum sp_event mask)
1365 enum sp_event *new_masks;
1367 TRACE("%p, %d, %d", event_set, handle, mask);
1369 if (!(new_handles = realloc(event_set->handles,
1370 sizeof(event_handle) * (event_set->count + 1))))
1371 RETURN_ERROR(SP_ERR_MEM, "Handle array realloc() failed");
1373 event_set->handles = new_handles;
1375 if (!(new_masks = realloc(event_set->masks,
1376 sizeof(enum sp_event) * (event_set->count + 1))))
1377 RETURN_ERROR(SP_ERR_MEM, "Mask array realloc() failed");
1379 event_set->masks = new_masks;
1381 ((event_handle *) event_set->handles)[event_set->count] = handle;
1382 event_set->masks[event_set->count] = mask;
1389 SP_API enum sp_return sp_add_port_events(struct sp_event_set *event_set,
1390 const struct sp_port *port, enum sp_event mask)
1392 TRACE("%p, %p, %d", event_set, port, mask);
1395 RETURN_ERROR(SP_ERR_ARG, "Null event set");
1398 RETURN_ERROR(SP_ERR_ARG, "Null port");
1400 if (mask > (SP_EVENT_RX_READY | SP_EVENT_TX_READY | SP_EVENT_ERROR))
1401 RETURN_ERROR(SP_ERR_ARG, "Invalid event mask");
1407 enum sp_event handle_mask;
1408 if ((handle_mask = mask & SP_EVENT_TX_READY))
1409 TRY(add_handle(event_set, port->write_ovl.hEvent, handle_mask));
1410 if ((handle_mask = mask & (SP_EVENT_RX_READY | SP_EVENT_ERROR)))
1411 TRY(add_handle(event_set, port->wait_ovl.hEvent, handle_mask));
1413 TRY(add_handle(event_set, port->fd, mask));
1419 SP_API void sp_free_event_set(struct sp_event_set *event_set)
1421 TRACE("%p", event_set);
1424 DEBUG("Null event set");
1428 DEBUG("Freeing event set");
1430 if (event_set->handles)
1431 free(event_set->handles);
1432 if (event_set->masks)
1433 free(event_set->masks);
1440 SP_API enum sp_return sp_wait(struct sp_event_set *event_set,
1441 unsigned int timeout_ms)
1443 TRACE("%p, %d", event_set, timeout_ms);
1446 RETURN_ERROR(SP_ERR_ARG, "Null event set");
1449 if (WaitForMultipleObjects(event_set->count, event_set->handles, FALSE,
1450 timeout_ms ? timeout_ms : INFINITE) == WAIT_FAILED)
1451 RETURN_FAIL("WaitForMultipleObjects() failed");
1455 struct timeval start, delta, now, end = {0, 0};
1456 const struct timeval max_delta = {
1457 (INT_MAX / 1000), (INT_MAX % 1000) * 1000};
1458 int started = 0, timeout_overflow = 0;
1459 int result, timeout_remaining_ms;
1460 struct pollfd *pollfds;
1463 if (!(pollfds = malloc(sizeof(struct pollfd) * event_set->count)))
1464 RETURN_ERROR(SP_ERR_MEM, "pollfds malloc() failed");
1466 for (i = 0; i < event_set->count; i++) {
1467 pollfds[i].fd = ((int *) event_set->handles)[i];
1468 pollfds[i].events = 0;
1469 pollfds[i].revents = 0;
1470 if (event_set->masks[i] & SP_EVENT_RX_READY)
1471 pollfds[i].events |= POLLIN;
1472 if (event_set->masks[i] & SP_EVENT_TX_READY)
1473 pollfds[i].events |= POLLOUT;
1474 if (event_set->masks[i] & SP_EVENT_ERROR)
1475 pollfds[i].events |= POLLERR;
1479 /* Get time at start of operation. */
1480 gettimeofday(&start, NULL);
1481 /* Define duration of timeout. */
1482 delta.tv_sec = timeout_ms / 1000;
1483 delta.tv_usec = (timeout_ms % 1000) * 1000;
1484 /* Calculate time at which we should give up. */
1485 timeradd(&start, &delta, &end);
1488 /* Loop until an event occurs. */
1491 * Check timeout only if we have run poll() at least once,
1492 * to avoid any issues if a short timeout is reached before
1493 * poll() is even run.
1496 timeout_remaining_ms = -1;
1497 } else if (!started) {
1498 timeout_overflow = (timeout_ms > INT_MAX);
1499 timeout_remaining_ms = timeout_overflow ? INT_MAX : timeout_ms;
1501 gettimeofday(&now, NULL);
1502 if (timercmp(&now, &end, >)) {
1503 DEBUG("Wait timed out");
1506 timersub(&end, &now, &delta);
1507 if ((timeout_overflow = timercmp(&delta, &max_delta, >)))
1509 timeout_remaining_ms = delta.tv_sec * 1000 + delta.tv_usec / 1000;
1512 result = poll(pollfds, event_set->count, timeout_remaining_ms);
1516 if (errno == EINTR) {
1517 DEBUG("poll() call was interrupted, repeating");
1521 RETURN_FAIL("poll() failed");
1523 } else if (result == 0) {
1524 DEBUG("poll() timed out");
1525 if (!timeout_overflow)
1528 DEBUG("poll() completed");
1538 #ifdef USE_TERMIOS_SPEED
1539 static enum sp_return get_baudrate(int fd, int *baudrate)
1543 TRACE("%d, %p", fd, baudrate);
1545 DEBUG("Getting baud rate");
1547 if (!(data = malloc(get_termios_size())))
1548 RETURN_ERROR(SP_ERR_MEM, "termios malloc failed");
1550 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
1552 RETURN_FAIL("Getting termios failed");
1555 *baudrate = get_termios_speed(data);
1562 static enum sp_return set_baudrate(int fd, int baudrate)
1566 TRACE("%d, %d", fd, baudrate);
1568 DEBUG("Getting baud rate");
1570 if (!(data = malloc(get_termios_size())))
1571 RETURN_ERROR(SP_ERR_MEM, "termios malloc failed");
1573 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
1575 RETURN_FAIL("Getting termios failed");
1578 DEBUG("Setting baud rate");
1580 set_termios_speed(data, baudrate);
1582 if (ioctl(fd, get_termios_set_ioctl(), data) < 0) {
1584 RETURN_FAIL("Setting termios failed");
1591 #endif /* USE_TERMIOS_SPEED */
1594 static enum sp_return get_flow(int fd, struct port_data *data)
1598 TRACE("%d, %p", fd, data);
1600 DEBUG("Getting advanced flow control");
1602 if (!(termx = malloc(get_termiox_size())))
1603 RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed");
1605 if (ioctl(fd, TCGETX, termx) < 0) {
1607 RETURN_FAIL("Getting termiox failed");
1610 get_termiox_flow(termx, &data->rts_flow, &data->cts_flow,
1611 &data->dtr_flow, &data->dsr_flow);
1618 static enum sp_return set_flow(int fd, struct port_data *data)
1622 TRACE("%d, %p", fd, data);
1624 DEBUG("Getting advanced flow control");
1626 if (!(termx = malloc(get_termiox_size())))
1627 RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed");
1629 if (ioctl(fd, TCGETX, termx) < 0) {
1631 RETURN_FAIL("Getting termiox failed");
1634 DEBUG("Setting advanced flow control");
1636 set_termiox_flow(termx, data->rts_flow, data->cts_flow,
1637 data->dtr_flow, data->dsr_flow);
1639 if (ioctl(fd, TCSETX, termx) < 0) {
1641 RETURN_FAIL("Setting termiox failed");
1648 #endif /* USE_TERMIOX */
1650 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
1651 struct sp_port_config *config)
1655 TRACE("%p, %p, %p", port, data, config);
1657 DEBUG_FMT("Getting configuration for port %s", port->name);
1660 if (!GetCommState(port->hdl, &data->dcb))
1661 RETURN_FAIL("GetCommState() failed");
1663 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1664 if (data->dcb.BaudRate == std_baudrates[i].index) {
1665 config->baudrate = std_baudrates[i].value;
1670 if (i == NUM_STD_BAUDRATES)
1671 /* BaudRate field can be either an index or a custom baud rate. */
1672 config->baudrate = data->dcb.BaudRate;
1674 config->bits = data->dcb.ByteSize;
1676 if (data->dcb.fParity)
1677 switch (data->dcb.Parity) {
1679 config->parity = SP_PARITY_NONE;
1682 config->parity = SP_PARITY_ODD;
1685 config->parity = SP_PARITY_EVEN;
1688 config->parity = SP_PARITY_MARK;
1691 config->parity = SP_PARITY_SPACE;
1694 config->parity = -1;
1697 config->parity = SP_PARITY_NONE;
1699 switch (data->dcb.StopBits) {
1701 config->stopbits = 1;
1704 config->stopbits = 2;
1707 config->stopbits = -1;
1710 switch (data->dcb.fRtsControl) {
1711 case RTS_CONTROL_DISABLE:
1712 config->rts = SP_RTS_OFF;
1714 case RTS_CONTROL_ENABLE:
1715 config->rts = SP_RTS_ON;
1717 case RTS_CONTROL_HANDSHAKE:
1718 config->rts = SP_RTS_FLOW_CONTROL;
1724 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
1726 switch (data->dcb.fDtrControl) {
1727 case DTR_CONTROL_DISABLE:
1728 config->dtr = SP_DTR_OFF;
1730 case DTR_CONTROL_ENABLE:
1731 config->dtr = SP_DTR_ON;
1733 case DTR_CONTROL_HANDSHAKE:
1734 config->dtr = SP_DTR_FLOW_CONTROL;
1740 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
1742 if (data->dcb.fInX) {
1743 if (data->dcb.fOutX)
1744 config->xon_xoff = SP_XONXOFF_INOUT;
1746 config->xon_xoff = SP_XONXOFF_IN;
1748 if (data->dcb.fOutX)
1749 config->xon_xoff = SP_XONXOFF_OUT;
1751 config->xon_xoff = SP_XONXOFF_DISABLED;
1756 if (tcgetattr(port->fd, &data->term) < 0)
1757 RETURN_FAIL("tcgetattr() failed");
1759 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
1760 RETURN_FAIL("TIOCMGET ioctl failed");
1763 int ret = get_flow(port->fd, data);
1765 if (ret == SP_ERR_FAIL && errno == EINVAL)
1766 data->termiox_supported = 0;
1768 RETURN_CODEVAL(ret);
1770 data->termiox_supported = 1;
1772 data->termiox_supported = 0;
1775 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1776 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
1777 config->baudrate = std_baudrates[i].value;
1782 if (i == NUM_STD_BAUDRATES) {
1784 config->baudrate = (int)data->term.c_ispeed;
1785 #elif defined(USE_TERMIOS_SPEED)
1786 TRY(get_baudrate(port->fd, &config->baudrate));
1788 config->baudrate = -1;
1792 switch (data->term.c_cflag & CSIZE) {
1809 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
1810 config->parity = SP_PARITY_NONE;
1811 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
1812 config->parity = -1;
1814 else if (data->term.c_cflag & CMSPAR)
1815 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_MARK : SP_PARITY_SPACE;
1818 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
1820 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
1822 if (data->term.c_cflag & CRTSCTS) {
1823 config->rts = SP_RTS_FLOW_CONTROL;
1824 config->cts = SP_CTS_FLOW_CONTROL;
1826 if (data->termiox_supported && data->rts_flow)
1827 config->rts = SP_RTS_FLOW_CONTROL;
1829 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
1831 config->cts = (data->termiox_supported && data->cts_flow) ?
1832 SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
1835 if (data->termiox_supported && data->dtr_flow)
1836 config->dtr = SP_DTR_FLOW_CONTROL;
1838 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
1840 config->dsr = (data->termiox_supported && data->dsr_flow) ?
1841 SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
1843 if (data->term.c_iflag & IXOFF) {
1844 if (data->term.c_iflag & IXON)
1845 config->xon_xoff = SP_XONXOFF_INOUT;
1847 config->xon_xoff = SP_XONXOFF_IN;
1849 if (data->term.c_iflag & IXON)
1850 config->xon_xoff = SP_XONXOFF_OUT;
1852 config->xon_xoff = SP_XONXOFF_DISABLED;
1859 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
1860 const struct sp_port_config *config)
1864 BAUD_TYPE baud_nonstd;
1868 #ifdef USE_TERMIOS_SPEED
1869 int baud_nonstd = 0;
1872 TRACE("%p, %p, %p", port, data, config);
1874 DEBUG_FMT("Setting configuration for port %s", port->name);
1877 if (config->baudrate >= 0) {
1878 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1879 if (config->baudrate == std_baudrates[i].value) {
1880 data->dcb.BaudRate = std_baudrates[i].index;
1885 if (i == NUM_STD_BAUDRATES)
1886 data->dcb.BaudRate = config->baudrate;
1889 if (config->bits >= 0)
1890 data->dcb.ByteSize = config->bits;
1892 if (config->parity >= 0) {
1893 switch (config->parity) {
1894 case SP_PARITY_NONE:
1895 data->dcb.Parity = NOPARITY;
1898 data->dcb.Parity = ODDPARITY;
1900 case SP_PARITY_EVEN:
1901 data->dcb.Parity = EVENPARITY;
1903 case SP_PARITY_MARK:
1904 data->dcb.Parity = MARKPARITY;
1906 case SP_PARITY_SPACE:
1907 data->dcb.Parity = SPACEPARITY;
1910 RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting");
1914 if (config->stopbits >= 0) {
1915 switch (config->stopbits) {
1916 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
1918 data->dcb.StopBits = ONESTOPBIT;
1921 data->dcb.StopBits = TWOSTOPBITS;
1924 RETURN_ERROR(SP_ERR_ARG, "Invalid stop bit setting");
1928 if (config->rts >= 0) {
1929 switch (config->rts) {
1931 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
1934 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
1936 case SP_RTS_FLOW_CONTROL:
1937 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
1940 RETURN_ERROR(SP_ERR_ARG, "Invalid RTS setting");
1944 if (config->cts >= 0) {
1945 switch (config->cts) {
1947 data->dcb.fOutxCtsFlow = FALSE;
1949 case SP_CTS_FLOW_CONTROL:
1950 data->dcb.fOutxCtsFlow = TRUE;
1953 RETURN_ERROR(SP_ERR_ARG, "Invalid CTS setting");
1957 if (config->dtr >= 0) {
1958 switch (config->dtr) {
1960 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
1963 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
1965 case SP_DTR_FLOW_CONTROL:
1966 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
1969 RETURN_ERROR(SP_ERR_ARG, "Invalid DTR setting");
1973 if (config->dsr >= 0) {
1974 switch (config->dsr) {
1976 data->dcb.fOutxDsrFlow = FALSE;
1978 case SP_DSR_FLOW_CONTROL:
1979 data->dcb.fOutxDsrFlow = TRUE;
1982 RETURN_ERROR(SP_ERR_ARG, "Invalid DSR setting");
1986 if (config->xon_xoff >= 0) {
1987 switch (config->xon_xoff) {
1988 case SP_XONXOFF_DISABLED:
1989 data->dcb.fInX = FALSE;
1990 data->dcb.fOutX = FALSE;
1993 data->dcb.fInX = TRUE;
1994 data->dcb.fOutX = FALSE;
1996 case SP_XONXOFF_OUT:
1997 data->dcb.fInX = FALSE;
1998 data->dcb.fOutX = TRUE;
2000 case SP_XONXOFF_INOUT:
2001 data->dcb.fInX = TRUE;
2002 data->dcb.fOutX = TRUE;
2005 RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting");
2009 if (!SetCommState(port->hdl, &data->dcb))
2010 RETURN_FAIL("SetCommState() failed");
2016 if (config->baudrate >= 0) {
2017 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
2018 if (config->baudrate == std_baudrates[i].value) {
2019 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
2020 RETURN_FAIL("cfsetospeed() failed");
2022 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
2023 RETURN_FAIL("cfsetispeed() failed");
2028 /* Non-standard baud rate */
2029 if (i == NUM_STD_BAUDRATES) {
2031 /* Set "dummy" baud rate. */
2032 if (cfsetspeed(&data->term, B9600) < 0)
2033 RETURN_FAIL("cfsetspeed() failed");
2034 baud_nonstd = config->baudrate;
2035 #elif defined(USE_TERMIOS_SPEED)
2038 RETURN_ERROR(SP_ERR_SUPP, "Non-standard baudrate not supported");
2043 if (config->bits >= 0) {
2044 data->term.c_cflag &= ~CSIZE;
2045 switch (config->bits) {
2047 data->term.c_cflag |= CS8;
2050 data->term.c_cflag |= CS7;
2053 data->term.c_cflag |= CS6;
2056 data->term.c_cflag |= CS5;
2059 RETURN_ERROR(SP_ERR_ARG, "Invalid data bits setting");
2063 if (config->parity >= 0) {
2064 data->term.c_iflag &= ~IGNPAR;
2065 data->term.c_cflag &= ~(PARENB | PARODD);
2067 data->term.c_cflag &= ~CMSPAR;
2069 switch (config->parity) {
2070 case SP_PARITY_NONE:
2071 data->term.c_iflag |= IGNPAR;
2073 case SP_PARITY_EVEN:
2074 data->term.c_cflag |= PARENB;
2077 data->term.c_cflag |= PARENB | PARODD;
2080 case SP_PARITY_MARK:
2081 data->term.c_cflag |= PARENB | PARODD;
2082 data->term.c_cflag |= CMSPAR;
2084 case SP_PARITY_SPACE:
2085 data->term.c_cflag |= PARENB;
2086 data->term.c_cflag |= CMSPAR;
2089 case SP_PARITY_MARK:
2090 case SP_PARITY_SPACE:
2091 RETURN_ERROR(SP_ERR_SUPP, "Mark/space parity not supported");
2094 RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting");
2098 if (config->stopbits >= 0) {
2099 data->term.c_cflag &= ~CSTOPB;
2100 switch (config->stopbits) {
2102 data->term.c_cflag &= ~CSTOPB;
2105 data->term.c_cflag |= CSTOPB;
2108 RETURN_ERROR(SP_ERR_ARG, "Invalid stop bits setting");
2112 if (config->rts >= 0 || config->cts >= 0) {
2113 if (data->termiox_supported) {
2114 data->rts_flow = data->cts_flow = 0;
2115 switch (config->rts) {
2118 controlbits = TIOCM_RTS;
2119 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
2120 RETURN_FAIL("Setting RTS signal level failed");
2122 case SP_RTS_FLOW_CONTROL:
2128 if (config->cts == SP_CTS_FLOW_CONTROL)
2131 if (data->rts_flow && data->cts_flow)
2132 data->term.c_iflag |= CRTSCTS;
2134 data->term.c_iflag &= ~CRTSCTS;
2136 /* Asymmetric use of RTS/CTS not supported. */
2137 if (data->term.c_iflag & CRTSCTS) {
2138 /* Flow control can only be disabled for both RTS & CTS together. */
2139 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
2140 if (config->cts != SP_CTS_IGNORE)
2141 RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together");
2143 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
2144 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
2145 RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together");
2148 /* Flow control can only be enabled for both RTS & CTS together. */
2149 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
2150 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
2151 RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be enabled together");
2154 if (config->rts >= 0) {
2155 if (config->rts == SP_RTS_FLOW_CONTROL) {
2156 data->term.c_iflag |= CRTSCTS;
2158 controlbits = TIOCM_RTS;
2159 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
2161 RETURN_FAIL("Setting RTS signal level failed");
2167 if (config->dtr >= 0 || config->dsr >= 0) {
2168 if (data->termiox_supported) {
2169 data->dtr_flow = data->dsr_flow = 0;
2170 switch (config->dtr) {
2173 controlbits = TIOCM_DTR;
2174 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
2175 RETURN_FAIL("Setting DTR signal level failed");
2177 case SP_DTR_FLOW_CONTROL:
2183 if (config->dsr == SP_DSR_FLOW_CONTROL)
2186 /* DTR/DSR flow control not supported. */
2187 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
2188 RETURN_ERROR(SP_ERR_SUPP, "DTR/DSR flow control not supported");
2190 if (config->dtr >= 0) {
2191 controlbits = TIOCM_DTR;
2192 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
2194 RETURN_FAIL("Setting DTR signal level failed");
2199 if (config->xon_xoff >= 0) {
2200 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
2201 switch (config->xon_xoff) {
2202 case SP_XONXOFF_DISABLED:
2205 data->term.c_iflag |= IXOFF;
2207 case SP_XONXOFF_OUT:
2208 data->term.c_iflag |= IXON | IXANY;
2210 case SP_XONXOFF_INOUT:
2211 data->term.c_iflag |= IXON | IXOFF | IXANY;
2214 RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting");
2218 if (tcsetattr(port->fd, TCSANOW, &data->term) < 0)
2219 RETURN_FAIL("tcsetattr() failed");
2222 if (baud_nonstd != B0) {
2223 if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
2224 RETURN_FAIL("IOSSIOSPEED ioctl failed");
2226 * Set baud rates in data->term to correct, but incompatible
2227 * with tcsetattr() value, same as delivered by tcgetattr().
2229 if (cfsetspeed(&data->term, baud_nonstd) < 0)
2230 RETURN_FAIL("cfsetspeed() failed");
2232 #elif defined(__linux__)
2233 #ifdef USE_TERMIOS_SPEED
2235 TRY(set_baudrate(port->fd, config->baudrate));
2238 if (data->termiox_supported)
2239 TRY(set_flow(port->fd, data));
2243 #endif /* !_WIN32 */
2248 SP_API enum sp_return sp_new_config(struct sp_port_config **config_ptr)
2250 struct sp_port_config *config;
2252 TRACE("%p", config_ptr);
2255 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
2259 if (!(config = malloc(sizeof(struct sp_port_config))))
2260 RETURN_ERROR(SP_ERR_MEM, "Config malloc failed");
2262 config->baudrate = -1;
2264 config->parity = -1;
2265 config->stopbits = -1;
2271 *config_ptr = config;
2276 SP_API void sp_free_config(struct sp_port_config *config)
2278 TRACE("%p", config);
2281 DEBUG("Null config");
2288 SP_API enum sp_return sp_get_config(struct sp_port *port,
2289 struct sp_port_config *config)
2291 struct port_data data;
2293 TRACE("%p, %p", port, config);
2298 RETURN_ERROR(SP_ERR_ARG, "Null config");
2300 TRY(get_config(port, &data, config));
2305 SP_API enum sp_return sp_set_config(struct sp_port *port,
2306 const struct sp_port_config *config)
2308 struct port_data data;
2309 struct sp_port_config prev_config;
2311 TRACE("%p, %p", port, config);
2316 RETURN_ERROR(SP_ERR_ARG, "Null config");
2318 TRY(get_config(port, &data, &prev_config));
2319 TRY(set_config(port, &data, config));
2324 #define CREATE_ACCESSORS(x, type) \
2325 SP_API enum sp_return sp_set_##x(struct sp_port *port, type x) { \
2326 struct port_data data; \
2327 struct sp_port_config config; \
2328 TRACE("%p, %d", port, x); \
2329 CHECK_OPEN_PORT(); \
2330 TRY(get_config(port, &data, &config)); \
2332 TRY(set_config(port, &data, &config)); \
2335 SP_API enum sp_return sp_get_config_##x(const struct sp_port_config *config, \
2337 TRACE("%p, %p", config, x); \
2339 RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); \
2341 RETURN_ERROR(SP_ERR_ARG, "Null config"); \
2345 SP_API enum sp_return sp_set_config_##x(struct sp_port_config *config, \
2347 TRACE("%p, %d", config, x); \
2349 RETURN_ERROR(SP_ERR_ARG, "Null config"); \
2354 CREATE_ACCESSORS(baudrate, int)
2355 CREATE_ACCESSORS(bits, int)
2356 CREATE_ACCESSORS(parity, enum sp_parity)
2357 CREATE_ACCESSORS(stopbits, int)
2358 CREATE_ACCESSORS(rts, enum sp_rts)
2359 CREATE_ACCESSORS(cts, enum sp_cts)
2360 CREATE_ACCESSORS(dtr, enum sp_dtr)
2361 CREATE_ACCESSORS(dsr, enum sp_dsr)
2362 CREATE_ACCESSORS(xon_xoff, enum sp_xonxoff)
2364 SP_API enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config,
2365 enum sp_flowcontrol flowcontrol)
2368 RETURN_ERROR(SP_ERR_ARG, "Null configuration");
2370 if (flowcontrol > SP_FLOWCONTROL_DTRDSR)
2371 RETURN_ERROR(SP_ERR_ARG, "Invalid flow control setting");
2373 if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
2374 config->xon_xoff = SP_XONXOFF_INOUT;
2376 config->xon_xoff = SP_XONXOFF_DISABLED;
2378 if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
2379 config->rts = SP_RTS_FLOW_CONTROL;
2380 config->cts = SP_CTS_FLOW_CONTROL;
2382 if (config->rts == SP_RTS_FLOW_CONTROL)
2383 config->rts = SP_RTS_ON;
2384 config->cts = SP_CTS_IGNORE;
2387 if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
2388 config->dtr = SP_DTR_FLOW_CONTROL;
2389 config->dsr = SP_DSR_FLOW_CONTROL;
2391 if (config->dtr == SP_DTR_FLOW_CONTROL)
2392 config->dtr = SP_DTR_ON;
2393 config->dsr = SP_DSR_IGNORE;
2399 SP_API enum sp_return sp_set_flowcontrol(struct sp_port *port,
2400 enum sp_flowcontrol flowcontrol)
2402 struct port_data data;
2403 struct sp_port_config config;
2405 TRACE("%p, %d", port, flowcontrol);
2409 TRY(get_config(port, &data, &config));
2411 TRY(sp_set_config_flowcontrol(&config, flowcontrol));
2413 TRY(set_config(port, &data, &config));
2418 SP_API enum sp_return sp_get_signals(struct sp_port *port,
2419 enum sp_signal *signals)
2421 TRACE("%p, %p", port, signals);
2426 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
2428 DEBUG_FMT("Getting control signals for port %s", port->name);
2433 if (GetCommModemStatus(port->hdl, &bits) == 0)
2434 RETURN_FAIL("GetCommModemStatus() failed");
2435 if (bits & MS_CTS_ON)
2436 *signals |= SP_SIG_CTS;
2437 if (bits & MS_DSR_ON)
2438 *signals |= SP_SIG_DSR;
2439 if (bits & MS_RLSD_ON)
2440 *signals |= SP_SIG_DCD;
2441 if (bits & MS_RING_ON)
2442 *signals |= SP_SIG_RI;
2445 if (ioctl(port->fd, TIOCMGET, &bits) < 0)
2446 RETURN_FAIL("TIOCMGET ioctl failed");
2447 if (bits & TIOCM_CTS)
2448 *signals |= SP_SIG_CTS;
2449 if (bits & TIOCM_DSR)
2450 *signals |= SP_SIG_DSR;
2451 if (bits & TIOCM_CAR)
2452 *signals |= SP_SIG_DCD;
2453 if (bits & TIOCM_RNG)
2454 *signals |= SP_SIG_RI;
2459 SP_API enum sp_return sp_start_break(struct sp_port *port)
2465 if (SetCommBreak(port->hdl) == 0)
2466 RETURN_FAIL("SetCommBreak() failed");
2468 if (ioctl(port->fd, TIOCSBRK, 1) < 0)
2469 RETURN_FAIL("TIOCSBRK ioctl failed");
2475 SP_API enum sp_return sp_end_break(struct sp_port *port)
2481 if (ClearCommBreak(port->hdl) == 0)
2482 RETURN_FAIL("ClearCommBreak() failed");
2484 if (ioctl(port->fd, TIOCCBRK, 1) < 0)
2485 RETURN_FAIL("TIOCCBRK ioctl failed");
2491 SP_API int sp_last_error_code(void)
2495 RETURN_INT(GetLastError());
2501 SP_API char *sp_last_error_message(void)
2507 DWORD error = GetLastError();
2509 DWORD length = FormatMessage(
2510 FORMAT_MESSAGE_ALLOCATE_BUFFER |
2511 FORMAT_MESSAGE_FROM_SYSTEM |
2512 FORMAT_MESSAGE_IGNORE_INSERTS,
2515 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
2519 if (length >= 2 && message[length - 2] == '\r')
2520 message[length - 2] = '\0';
2522 RETURN_STRING(message);
2524 RETURN_STRING(strerror(errno));
2528 SP_API void sp_free_error_message(char *message)
2530 TRACE("%s", message);
2541 SP_API void sp_set_debug_handler(void (*handler)(const char *format, ...))
2543 TRACE("%p", handler);
2545 sp_debug_handler = handler;
2550 SP_API void sp_default_debug_handler(const char *format, ...)
2553 va_start(args, format);
2554 if (getenv("LIBSERIALPORT_DEBUG")) {
2555 fputs("sp: ", stderr);
2556 vfprintf(stderr, format, args);
2561 SP_API int sp_get_major_package_version(void)
2563 return SP_PACKAGE_VERSION_MAJOR;
2566 SP_API int sp_get_minor_package_version(void)
2568 return SP_PACKAGE_VERSION_MINOR;
2571 SP_API int sp_get_micro_package_version(void)
2573 return SP_PACKAGE_VERSION_MICRO;
2576 SP_API const char *sp_get_package_version_string(void)
2578 return SP_PACKAGE_VERSION_STRING;
2581 SP_API int sp_get_current_lib_version(void)
2583 return SP_LIB_VERSION_CURRENT;
2586 SP_API int sp_get_revision_lib_version(void)
2588 return SP_LIB_VERSION_REVISION;
2591 SP_API int sp_get_age_lib_version(void)
2593 return SP_LIB_VERSION_AGE;
2596 SP_API const char *sp_get_lib_version_string(void)
2598 return SP_LIB_VERSION_STRING;