2 * This file is part of the libserialport project.
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>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h>
39 #include <sys/ioctl.h>
44 #include <IOKit/IOKitLib.h>
45 #include <IOKit/serial/IOSerialKeys.h>
46 #include <IOKit/serial/ioss.h>
47 #include <sys/syslimits.h>
54 #include "linux/serial.h"
56 #include "linux_termios.h"
58 /* TCGETX/TCSETX is not available everywhere. */
59 #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX)
64 /* TIOCINQ/TIOCOUTQ is not available everywhere. */
65 #if !defined(TIOCINQ) && defined(FIONREAD)
66 #define TIOCINQ FIONREAD
68 #if !defined(TIOCOUTQ) && defined(FIONWRITE)
69 #define TIOCOUTQ FIONWRITE
73 #include "linux_termios.h"
76 #include "libserialport.h"
82 COMMTIMEOUTS timeouts;
92 struct sp_port_config {
95 enum sp_parity parity;
101 enum sp_xonxoff xon_xoff;
110 int termiox_supported;
115 /* Standard baud rates. */
117 #define BAUD_TYPE DWORD
118 #define BAUD(n) {CBR_##n, n}
120 #define BAUD_TYPE speed_t
121 #define BAUD(n) {B##n, n}
124 struct std_baudrate {
129 const struct std_baudrate std_baudrates[] = {
132 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
133 * have documented CBR_* macros.
135 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
136 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
137 BAUD(115200), BAUD(128000), BAUD(256000),
139 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
140 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
141 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
143 #if !defined(__APPLE__) && !defined(__OpenBSD__)
149 void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
151 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
152 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
154 /* Debug output macros. */
155 #define DEBUG(fmt, ...) do { if (sp_debug_handler) sp_debug_handler(fmt ".\n", ##__VA_ARGS__); } while (0)
156 #define DEBUG_ERROR(err, msg) DEBUG("%s returning " #err ": " msg, __func__)
157 #define DEBUG_FAIL(msg) do { \
158 char *errmsg = sp_last_error_message(); \
159 DEBUG("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
160 sp_free_error_message(errmsg); \
162 #define RETURN() do { DEBUG("%s returning", __func__); return; } while(0)
163 #define RETURN_CODE(x) do { DEBUG("%s returning " #x, __func__); return x; } while (0)
164 #define RETURN_CODEVAL(x) do { \
166 case SP_OK: RETURN_CODE(SP_OK); \
167 case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \
168 case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \
169 case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \
170 case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \
173 #define RETURN_OK() RETURN_CODE(SP_OK);
174 #define RETURN_ERROR(err, msg) do { DEBUG_ERROR(err, msg); return err; } while (0)
175 #define RETURN_FAIL(msg) do { DEBUG_FAIL(msg); return SP_ERR_FAIL; } while (0)
176 #define RETURN_VALUE(fmt, x) do { \
178 DEBUG("%s returning " fmt, __func__, _x); \
181 #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
182 #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
183 #define TRACE(fmt, ...) DEBUG("%s(" fmt ") called", __func__, ##__VA_ARGS__)
185 #define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0)
187 /* Helper functions. */
188 static struct sp_port **list_append(struct sp_port **list, const char *portname);
189 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
190 struct sp_port_config *config);
191 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
192 const struct sp_port_config *config);
194 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
196 struct sp_port *port;
199 TRACE("%s, %p", portname, port_ptr);
202 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
207 RETURN_ERROR(SP_ERR_ARG, "Null port name");
209 DEBUG("Building structure for port %s", portname);
211 if (!(port = malloc(sizeof(struct sp_port))))
212 RETURN_ERROR(SP_ERR_MEM, "Port structure malloc failed");
214 len = strlen(portname) + 1;
216 if (!(port->name = malloc(len))) {
218 RETURN_ERROR(SP_ERR_MEM, "Port name malloc failed");
221 memcpy(port->name, portname, len);
224 port->hdl = INVALID_HANDLE_VALUE;
234 char *sp_get_port_name(const struct sp_port *port)
241 RETURN_VALUE("%s", port->name);
244 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr)
246 TRACE("%p, %p", port, result_ptr);
249 RETURN_ERROR(SP_ERR_ARG, "Null port");
252 HANDLE *handle_ptr = result_ptr;
253 *handle_ptr = port->hdl;
255 int *fd_ptr = result_ptr;
262 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
264 TRACE("%p, %p", port, copy_ptr);
267 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
272 RETURN_ERROR(SP_ERR_ARG, "Null port");
275 RETURN_ERROR(SP_ERR_ARG, "Null port name");
277 DEBUG("Copying port structure");
279 RETURN_VALUE("%p", sp_get_port_by_name(port->name, copy_ptr));
282 void sp_free_port(struct sp_port *port)
291 DEBUG("Freeing port structure");
301 static struct sp_port **list_append(struct sp_port **list, const char *portname)
306 for (count = 0; list[count]; count++);
307 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
310 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
312 list[count + 1] = NULL;
316 sp_free_port_list(list);
320 enum sp_return sp_list_ports(struct sp_port ***list_ptr)
322 struct sp_port **list;
323 int ret = SP_ERR_SUPP;
325 TRACE("%p", list_ptr);
328 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
330 DEBUG("Enumerating ports");
332 if (!(list = malloc(sizeof(struct sp_port **))))
333 RETURN_ERROR(SP_ERR_MEM, "Port list malloc failed");
340 DWORD max_value_len, max_data_size, max_data_len;
341 DWORD value_len, data_size, data_len;
342 DWORD type, index = 0;
348 DEBUG("Opening registry key");
349 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
350 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
351 SET_FAIL(ret, "RegOpenKeyEx() failed");
354 DEBUG("Querying registry key value and data sizes");
355 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
356 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
357 SET_FAIL(ret, "RegQueryInfoKey() failed");
360 max_data_len = max_data_size / sizeof(TCHAR);
361 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
362 SET_ERROR(ret, SP_ERR_MEM, "registry value malloc failed");
365 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
366 SET_ERROR(ret, SP_ERR_MEM, "registry data malloc failed");
369 DEBUG("Iterating over values");
371 value_len = max_value_len + 1,
372 data_size = max_data_size,
373 RegEnumValue(key, index, value, &value_len,
374 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
376 data_len = data_size / sizeof(TCHAR);
377 data[data_len] = '\0';
379 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
381 name_len = data_len + 1;
383 if (!(name = malloc(name_len))) {
384 SET_ERROR(ret, SP_ERR_MEM, "registry port name malloc failed");
388 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
392 if (type == REG_SZ) {
393 DEBUG("Found port %s", name);
394 if (!(list = list_append(list, name))) {
395 SET_ERROR(ret, SP_ERR_MEM, "list append failed");
411 CFMutableDictionaryRef classes;
420 DEBUG("Getting IOKit master port");
421 if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
422 SET_FAIL(ret, "IOMasterPort() failed");
426 DEBUG("Creating matching dictionary");
427 if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
428 SET_FAIL(ret, "IOServiceMatching() failed");
432 CFDictionarySetValue(classes,
433 CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
435 DEBUG("Getting matching services");
436 if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
437 SET_FAIL(ret, "IOServiceGetMatchingServices() failed");
441 if (!(path = malloc(PATH_MAX))) {
442 SET_ERROR(ret, SP_ERR_MEM, "device path malloc failed");
446 DEBUG("Iterating over results");
447 while ((port = IOIteratorNext(iter))) {
448 cf_path = IORegistryEntryCreateCFProperty(port,
449 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
451 result = CFStringGetCString(cf_path,
452 path, PATH_MAX, kCFStringEncodingASCII);
455 DEBUG("Found port %s", path);
456 if (!(list = list_append(list, path))) {
457 SET_ERROR(ret, SP_ERR_MEM, "list append failed");
458 IOObjectRelease(port);
463 IOObjectRelease(port);
468 IOObjectRelease(iter);
471 #if defined(__linux__) && defined(HAVE_LIBUDEV)
473 struct udev_enumerate *ud_enumerate;
474 struct udev_list_entry *ud_list;
475 struct udev_list_entry *ud_entry;
477 struct udev_device *ud_dev, *ud_parent;
480 int fd, ioctl_result;
481 struct serial_struct serial_info;
485 DEBUG("Enumerating tty devices");
487 ud_enumerate = udev_enumerate_new(ud);
488 udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
489 udev_enumerate_scan_devices(ud_enumerate);
490 ud_list = udev_enumerate_get_list_entry(ud_enumerate);
491 DEBUG("Iterating over results");
492 udev_list_entry_foreach(ud_entry, ud_list) {
493 path = udev_list_entry_get_name(ud_entry);
494 DEBUG("Found device %s", path);
495 ud_dev = udev_device_new_from_syspath(ud, path);
496 /* If there is no parent device, this is a virtual tty. */
497 ud_parent = udev_device_get_parent(ud_dev);
498 if (ud_parent == NULL) {
499 DEBUG("No parent device, assuming virtual tty");
500 udev_device_unref(ud_dev);
503 name = udev_device_get_devnode(ud_dev);
504 /* The serial8250 driver has a hardcoded number of ports.
505 * The only way to tell which actually exist on a given system
506 * is to try to open them and make an ioctl call. */
507 driver = udev_device_get_driver(ud_parent);
508 if (driver && !strcmp(driver, "serial8250")) {
509 DEBUG("serial8250 device, attempting to open");
510 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
511 DEBUG("open failed, skipping");
514 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
516 if (ioctl_result != 0) {
517 DEBUG("ioctl failed, skipping");
520 if (serial_info.type == PORT_UNKNOWN) {
521 DEBUG("port type is unknown, skipping");
525 DEBUG("Found port %s", name);
526 list = list_append(list, name);
528 udev_device_unref(ud_dev);
530 SET_ERROR(ret, SP_ERR_MEM, "list append failed");
535 udev_enumerate_unref(ud_enumerate);
544 DEBUG_ERROR(SP_ERR_SUPP, "Enumeration not supported on this platform.");
547 sp_free_port_list(list);
553 void sp_free_port_list(struct sp_port **list)
564 DEBUG("Freeing port list");
566 for (i = 0; list[i]; i++)
567 sp_free_port(list[i]);
573 #define CHECK_PORT() do { \
575 RETURN_ERROR(SP_ERR_ARG, "Null port"); \
576 if (port->name == NULL) \
577 RETURN_ERROR(SP_ERR_ARG, "Null port name"); \
580 #define CHECK_PORT_HANDLE() do { \
581 if (port->hdl == INVALID_HANDLE_VALUE) \
582 RETURN_ERROR(SP_ERR_ARG, "Invalid port handle"); \
585 #define CHECK_PORT_HANDLE() do { \
587 RETURN_ERROR(SP_ERR_ARG, "Invalid port fd"); \
590 #define CHECK_OPEN_PORT() do { \
592 CHECK_PORT_HANDLE(); \
595 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
597 struct port_data data;
598 struct sp_port_config config;
601 TRACE("%p, 0x%x", port, flags);
605 if (flags > (SP_MODE_READ | SP_MODE_WRITE))
606 RETURN_ERROR(SP_ERR_ARG, "Invalid flags");
608 DEBUG("Opening port %s", port->name);
611 DWORD desired_access = 0, flags_and_attributes = 0;
612 char *escaped_port_name;
614 /* Prefix port name with '\\.\' to work with ports above COM9. */
615 if (!(escaped_port_name = malloc(strlen(port->name + 5))))
616 RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed");
617 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
619 /* Map 'flags' to the OS-specific settings. */
620 flags_and_attributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED;
621 if (flags & SP_MODE_READ)
622 desired_access |= GENERIC_READ;
623 if (flags & SP_MODE_WRITE)
624 desired_access |= GENERIC_WRITE;
626 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
627 OPEN_EXISTING, flags_and_attributes, 0);
629 free(escaped_port_name);
631 if (port->hdl == INVALID_HANDLE_VALUE)
632 RETURN_FAIL("port CreateFile() failed");
634 /* All timeouts initially disabled. */
635 port->timeouts.ReadIntervalTimeout = 0;
636 port->timeouts.ReadTotalTimeoutMultiplier = 0;
637 port->timeouts.ReadTotalTimeoutConstant = 0;
638 port->timeouts.WriteTotalTimeoutMultiplier = 0;
639 port->timeouts.WriteTotalTimeoutConstant = 0;
641 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) {
643 RETURN_FAIL("SetCommTimeouts() failed");
646 /* Prepare OVERLAPPED structures. */
647 memset(&port->read_ovl, 0, sizeof(port->read_ovl));
648 memset(&port->write_ovl, 0, sizeof(port->write_ovl));
649 port->read_ovl.hEvent = INVALID_HANDLE_VALUE;
650 port->write_ovl.hEvent = INVALID_HANDLE_VALUE;
651 if ((port->read_ovl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == INVALID_HANDLE_VALUE) {
653 RETURN_FAIL("read event CreateEvent() failed");
655 if ((port->write_ovl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == INVALID_HANDLE_VALUE) {
657 RETURN_FAIL("write event CreateEvent() failed");
660 port->writing = FALSE;
663 int flags_local = O_NONBLOCK | O_NOCTTY;
665 /* Map 'flags' to the OS-specific settings. */
666 if (flags & (SP_MODE_READ | SP_MODE_WRITE))
667 flags_local |= O_RDWR;
668 else if (flags & SP_MODE_READ)
669 flags_local |= O_RDONLY;
670 else if (flags & SP_MODE_WRITE)
671 flags_local |= O_WRONLY;
673 if ((port->fd = open(port->name, flags_local)) < 0)
674 RETURN_FAIL("open() failed");
677 ret = get_config(port, &data, &config);
684 /* Set sane port settings. */
686 data.dcb.fBinary = TRUE;
687 data.dcb.fDsrSensitivity = FALSE;
688 data.dcb.fErrorChar = FALSE;
689 data.dcb.fNull = FALSE;
690 data.dcb.fAbortOnError = TRUE;
692 /* Turn off all fancy termios tricks, give us a raw channel. */
693 data.term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IMAXBEL);
695 data.term.c_iflag &= ~IUCLC;
697 data.term.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
699 data.term.c_oflag &= ~OLCUC;
702 data.term.c_oflag &= ~NLDLY;
705 data.term.c_oflag &= ~CRDLY;
708 data.term.c_oflag &= ~TABDLY;
711 data.term.c_oflag &= ~BSDLY;
714 data.term.c_oflag &= ~VTDLY;
717 data.term.c_oflag &= ~FFDLY;
720 data.term.c_oflag &= ~OFILL;
722 data.term.c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN);
723 data.term.c_cc[VMIN] = 0;
724 data.term.c_cc[VTIME] = 0;
726 /* Ignore modem status lines; enable receiver; leave control lines alone on close. */
727 data.term.c_cflag |= (CLOCAL | CREAD | HUPCL);
730 ret = set_config(port, &data, &config);
740 enum sp_return sp_close(struct sp_port *port)
746 DEBUG("Closing port %s", port->name);
749 /* Returns non-zero upon success, 0 upon failure. */
750 if (CloseHandle(port->hdl) == 0)
751 RETURN_FAIL("port CloseHandle() failed");
752 port->hdl = INVALID_HANDLE_VALUE;
753 /* Close event handle created for overlapped reads. */
754 if (port->read_ovl.hEvent != INVALID_HANDLE_VALUE && CloseHandle(port->read_ovl.hEvent) == 0)
755 RETURN_FAIL("read event CloseHandle() failed");
756 /* Close event handle created for overlapped writes. */
757 if (port->write_ovl.hEvent != INVALID_HANDLE_VALUE && CloseHandle(port->write_ovl.hEvent) == 0)
758 RETURN_FAIL("write event CloseHandle() failed");
760 /* Returns 0 upon success, -1 upon failure. */
761 if (close(port->fd) == -1)
762 RETURN_FAIL("close() failed");
769 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
771 TRACE("%p, 0x%x", port, buffers);
775 if (buffers > SP_BUF_BOTH)
776 RETURN_ERROR(SP_ERR_ARG, "Invalid buffer selection");
778 const char *buffer_names[] = {"no", "input", "output", "both"};
780 DEBUG("Flushing %s buffers on port %s", buffer_names[buffers], port->name);
784 if (buffers & SP_BUF_INPUT)
785 flags |= PURGE_RXCLEAR;
786 if (buffers & SP_BUF_OUTPUT)
787 flags |= PURGE_TXCLEAR;
789 /* Returns non-zero upon success, 0 upon failure. */
790 if (PurgeComm(port->hdl, flags) == 0)
791 RETURN_FAIL("PurgeComm() failed");
794 if (buffers & SP_BUF_BOTH)
796 else if (buffers & SP_BUF_INPUT)
798 else if (buffers & SP_BUF_OUTPUT)
801 /* Returns 0 upon success, -1 upon failure. */
802 if (tcflush(port->fd, flags) < 0)
803 RETURN_FAIL("tcflush() failed");
808 enum sp_return sp_drain(struct sp_port *port)
814 DEBUG("Draining port %s", port->name);
817 /* Returns non-zero upon success, 0 upon failure. */
818 if (FlushFileBuffers(port->hdl) == 0)
819 RETURN_FAIL("FlushFileBuffers() failed");
826 result = ioctl(port->fd, TCSBRK, &arg);
828 result = tcdrain(port->fd);
831 if (errno == EINTR) {
832 DEBUG("tcdrain() was interrupted");
835 RETURN_FAIL("tcdrain() failed");
844 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout)
846 TRACE("%p, %p, %d, %d", port, buf, count, timeout);
851 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
854 DEBUG("Writing %d bytes to port %s, timeout %d ms", count, port->name, timeout);
856 DEBUG("Writing %d bytes to port %s, no timeout", count, port->name);
859 RETURN_VALUE("0", 0);
862 DWORD bytes_written = 0;
865 /* Wait for previous non-blocking write to complete, if any. */
867 DEBUG("Waiting for previous write to complete");
868 result = GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
871 RETURN_FAIL("Previous write failed to complete");
872 DEBUG("Previous write completed");
876 port->timeouts.WriteTotalTimeoutConstant = timeout;
877 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
878 RETURN_FAIL("SetCommTimeouts() failed");
881 if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl) == 0) {
882 if (GetLastError() == ERROR_IO_PENDING) {
883 DEBUG("Waiting for write to complete");
884 GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
885 DEBUG("Write completed, %d/%d bytes written", bytes_written, count);
886 RETURN_VALUE("%d", bytes_written);
888 RETURN_FAIL("WriteFile() failed");
891 DEBUG("Write completed immediately");
892 RETURN_VALUE("%d", count);
895 size_t bytes_written = 0;
896 unsigned char *ptr = (unsigned char *) buf;
897 struct timeval start, delta, now, end = {0, 0};
902 /* Get time at start of operation. */
903 gettimeofday(&start, NULL);
904 /* Define duration of timeout. */
905 delta.tv_sec = timeout / 1000;
906 delta.tv_usec = (timeout % 1000) * 1000;
907 /* Calculate time at which we should give up. */
908 timeradd(&start, &delta, &end);
911 /* Loop until we have written the requested number of bytes. */
912 while (bytes_written < count)
914 /* Wait until space is available. */
916 FD_SET(port->fd, &fds);
918 gettimeofday(&now, NULL);
919 if (timercmp(&now, &end, >)) {
920 DEBUG("write timed out");
921 RETURN_VALUE("%d", bytes_written);
923 timersub(&end, &now, &delta);
925 result = select(port->fd + 1, NULL, &fds, NULL, timeout ? &delta : NULL);
927 if (errno == EINTR) {
928 DEBUG("select() call was interrupted, repeating");
931 RETURN_FAIL("select() failed");
933 } else if (result == 0) {
934 DEBUG("write timed out");
935 RETURN_VALUE("%d", bytes_written);
939 result = write(port->fd, ptr, count - bytes_written);
943 /* This shouldn't happen because we did a select() first, but handle anyway. */
946 /* This is an actual failure. */
947 RETURN_FAIL("write() failed");
950 bytes_written += result;
954 RETURN_VALUE("%d", bytes_written);
958 enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count)
960 TRACE("%p, %p, %d", port, buf, count);
965 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
967 DEBUG("Writing up to %d bytes to port %s", count, port->name);
970 RETURN_VALUE("0", 0);
974 BYTE *ptr = (BYTE *) buf;
976 /* Check whether previous write is complete. */
978 if (HasOverlappedIoCompleted(&port->write_ovl)) {
979 DEBUG("Previous write completed");
982 DEBUG("Previous write not complete");
983 /* Can't take a new write until the previous one finishes. */
984 RETURN_VALUE("0", 0);
989 port->timeouts.WriteTotalTimeoutConstant = 0;
990 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
991 RETURN_FAIL("SetCommTimeouts() failed");
993 /* Keep writing data until the OS has to actually start an async IO for it.
994 * At that point we know the buffer is full. */
995 while (written < count)
997 /* Copy first byte of user buffer. */
998 port->pending_byte = *ptr++;
1000 /* Start asynchronous write. */
1001 if (WriteFile(port->hdl, &port->pending_byte, 1, NULL, &port->write_ovl) == 0) {
1002 if (GetLastError() == ERROR_IO_PENDING) {
1003 DEBUG("Asynchronous write started");
1005 RETURN_VALUE("%d", ++written);
1007 /* Actual failure of some kind. */
1008 RETURN_FAIL("WriteFile() failed");
1011 DEBUG("Single byte written immediately.");
1016 DEBUG("All bytes written immediately.");
1018 RETURN_VALUE("%d", written);
1020 /* Returns the number of bytes written, or -1 upon failure. */
1021 ssize_t written = write(port->fd, buf, count);
1024 RETURN_FAIL("write() failed");
1026 RETURN_VALUE("%d", written);
1030 enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout)
1032 TRACE("%p, %p, %d, %d", port, buf, count, timeout);
1037 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
1040 DEBUG("Reading %d bytes from port %s, timeout %d ms", count, port->name, timeout);
1042 DEBUG("Reading %d bytes from port %s, no timeout", count, port->name);
1045 RETURN_VALUE("0", 0);
1048 DWORD bytes_read = 0;
1051 port->timeouts.ReadIntervalTimeout = 0;
1052 port->timeouts.ReadTotalTimeoutConstant = timeout;
1053 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
1054 RETURN_FAIL("SetCommTimeouts() failed");
1057 if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0) {
1058 if (GetLastError() == ERROR_IO_PENDING) {
1059 DEBUG("Waiting for read to complete");
1060 GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE);
1061 DEBUG("Read completed, %d/%d bytes read", bytes_read, count);
1062 RETURN_VALUE("%d", bytes_read);
1064 RETURN_FAIL("ReadFile() failed");
1067 DEBUG("Read completed immediately");
1068 RETURN_VALUE("%d", count);
1071 size_t bytes_read = 0;
1072 unsigned char *ptr = (unsigned char *) buf;
1073 struct timeval start, delta, now, end = {0, 0};
1078 /* Get time at start of operation. */
1079 gettimeofday(&start, NULL);
1080 /* Define duration of timeout. */
1081 delta.tv_sec = timeout / 1000;
1082 delta.tv_usec = (timeout % 1000) * 1000;
1083 /* Calculate time at which we should give up. */
1084 timeradd(&start, &delta, &end);
1087 /* Loop until we have the requested number of bytes. */
1088 while (bytes_read < count)
1090 /* Wait until data is available. */
1092 FD_SET(port->fd, &fds);
1094 gettimeofday(&now, NULL);
1095 if (timercmp(&now, &end, >))
1096 /* Timeout has expired. */
1097 RETURN_VALUE("%d", bytes_read);
1098 timersub(&end, &now, &delta);
1100 result = select(port->fd + 1, &fds, NULL, NULL, timeout ? &delta : NULL);
1102 if (errno == EINTR) {
1103 DEBUG("select() call was interrupted, repeating");
1106 RETURN_FAIL("select() failed");
1108 } else if (result == 0) {
1109 DEBUG("read timed out");
1110 RETURN_VALUE("%d", bytes_read);
1114 result = read(port->fd, ptr, count - bytes_read);
1117 if (errno == EAGAIN)
1118 /* This shouldn't happen because we did a select() first, but handle anyway. */
1121 /* This is an actual failure. */
1122 RETURN_FAIL("read() failed");
1125 bytes_read += result;
1129 RETURN_VALUE("%d", bytes_read);
1133 enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count)
1135 TRACE("%p, %p, %d", port, buf, count);
1140 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
1142 DEBUG("Reading up to %d bytes from port %s", count, port->name);
1148 port->timeouts.ReadIntervalTimeout = MAXDWORD;
1149 port->timeouts.ReadTotalTimeoutConstant = 0;
1150 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
1151 RETURN_FAIL("SetCommTimeouts() failed");
1154 if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0)
1155 RETURN_FAIL("ReadFile() failed");
1157 /* Get number of bytes read. */
1158 if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0)
1159 RETURN_FAIL("GetOverlappedResult() failed");
1161 RETURN_VALUE("%d", bytes_read);
1165 /* Returns the number of bytes read, or -1 upon failure. */
1166 if ((bytes_read = read(port->fd, buf, count)) < 0) {
1167 if (errno == EAGAIN)
1168 /* No bytes available. */
1171 /* This is an actual failure. */
1172 RETURN_FAIL("read() failed");
1174 RETURN_VALUE("%d", bytes_read);
1178 enum sp_return sp_input_waiting(struct sp_port *port)
1184 DEBUG("Checking input bytes waiting on port %s", port->name);
1190 if (ClearCommError(port->hdl, &errors, &comstat) == 0)
1191 RETURN_FAIL("ClearComError() failed");
1192 RETURN_VALUE("%d", comstat.cbInQue);
1195 if (ioctl(port->fd, TIOCINQ, &bytes_waiting) < 0)
1196 RETURN_FAIL("TIOCINQ ioctl failed");
1197 RETURN_VALUE("%d", bytes_waiting);
1201 enum sp_return sp_output_waiting(struct sp_port *port)
1207 DEBUG("Checking output bytes waiting on port %s", port->name);
1213 if (ClearCommError(port->hdl, &errors, &comstat) == 0)
1214 RETURN_FAIL("ClearComError() failed");
1215 RETURN_VALUE("%d", comstat.cbOutQue);
1218 if (ioctl(port->fd, TIOCOUTQ, &bytes_waiting) < 0)
1219 RETURN_FAIL("TIOCOUTQ ioctl failed");
1220 RETURN_VALUE("%d", bytes_waiting);
1225 static enum sp_return get_baudrate(int fd, int *baudrate)
1229 TRACE("%d, %p", fd, baudrate);
1231 DEBUG("Getting baud rate");
1233 if (!(data = malloc(get_termios_size())))
1234 RETURN_ERROR(SP_ERR_MEM, "termios malloc failed");
1236 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
1238 RETURN_FAIL("getting termios failed");
1241 *baudrate = get_termios_speed(data);
1248 static enum sp_return set_baudrate(int fd, int baudrate)
1252 TRACE("%d, %d", fd, baudrate);
1254 DEBUG("Getting baud rate");
1256 if (!(data = malloc(get_termios_size())))
1257 RETURN_ERROR(SP_ERR_MEM, "termios malloc failed");
1259 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
1261 RETURN_FAIL("getting termios failed");
1264 DEBUG("Setting baud rate");
1266 set_termios_speed(data, baudrate);
1268 if (ioctl(fd, get_termios_set_ioctl(), data) < 0) {
1270 RETURN_FAIL("setting termios failed");
1279 static enum sp_return get_flow(int fd, int *flow)
1283 TRACE("%d, %p", fd, flow);
1285 DEBUG("Getting advanced flow control");
1287 if (!(data = malloc(get_termiox_size())))
1288 RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed");
1290 if (ioctl(fd, TCGETX, data) < 0) {
1292 RETURN_FAIL("getting termiox failed");
1295 *flow = get_termiox_flow(data);
1302 static enum sp_return set_flow(int fd, int flow)
1306 TRACE("%d, %d", fd, flow);
1308 DEBUG("Getting advanced flow control");
1310 if (!(data = malloc(get_termiox_size())))
1311 RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed");
1313 if (ioctl(fd, TCGETX, data) < 0) {
1315 RETURN_FAIL("getting termiox failed");
1318 DEBUG("Setting advanced flow control");
1320 set_termiox_flow(data, flow);
1322 if (ioctl(fd, TCSETX, data) < 0) {
1324 RETURN_FAIL("setting termiox failed");
1331 #endif /* USE_TERMIOX */
1332 #endif /* __linux__ */
1334 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
1335 struct sp_port_config *config)
1339 TRACE("%p, %p, %p", port, data, config);
1341 DEBUG("Getting configuration for port %s", port->name);
1344 if (!GetCommState(port->hdl, &data->dcb))
1345 RETURN_FAIL("GetCommState() failed");
1347 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1348 if (data->dcb.BaudRate == std_baudrates[i].index) {
1349 config->baudrate = std_baudrates[i].value;
1354 if (i == NUM_STD_BAUDRATES)
1355 /* BaudRate field can be either an index or a custom baud rate. */
1356 config->baudrate = data->dcb.BaudRate;
1358 config->bits = data->dcb.ByteSize;
1360 if (data->dcb.fParity)
1361 switch (data->dcb.Parity) {
1363 config->parity = SP_PARITY_NONE;
1366 config->parity = SP_PARITY_ODD;
1369 config->parity = SP_PARITY_EVEN;
1372 config->parity = SP_PARITY_MARK;
1375 config->parity = SP_PARITY_SPACE;
1378 config->parity = -1;
1381 config->parity = SP_PARITY_NONE;
1383 switch (data->dcb.StopBits) {
1385 config->stopbits = 1;
1388 config->stopbits = 2;
1391 config->stopbits = -1;
1394 switch (data->dcb.fRtsControl) {
1395 case RTS_CONTROL_DISABLE:
1396 config->rts = SP_RTS_OFF;
1398 case RTS_CONTROL_ENABLE:
1399 config->rts = SP_RTS_ON;
1401 case RTS_CONTROL_HANDSHAKE:
1402 config->rts = SP_RTS_FLOW_CONTROL;
1408 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
1410 switch (data->dcb.fDtrControl) {
1411 case DTR_CONTROL_DISABLE:
1412 config->dtr = SP_DTR_OFF;
1414 case DTR_CONTROL_ENABLE:
1415 config->dtr = SP_DTR_ON;
1417 case DTR_CONTROL_HANDSHAKE:
1418 config->dtr = SP_DTR_FLOW_CONTROL;
1424 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
1426 if (data->dcb.fInX) {
1427 if (data->dcb.fOutX)
1428 config->xon_xoff = SP_XONXOFF_INOUT;
1430 config->xon_xoff = SP_XONXOFF_IN;
1432 if (data->dcb.fOutX)
1433 config->xon_xoff = SP_XONXOFF_OUT;
1435 config->xon_xoff = SP_XONXOFF_DISABLED;
1440 if (tcgetattr(port->fd, &data->term) < 0)
1441 RETURN_FAIL("tcgetattr() failed");
1443 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
1444 RETURN_FAIL("TIOCMGET ioctl failed");
1447 int ret = get_flow(port->fd, &data->flow);
1449 if (ret == SP_ERR_FAIL && errno == EINVAL)
1450 data->termiox_supported = 0;
1452 RETURN_CODEVAL(ret);
1454 data->termiox_supported = 1;
1456 data->termiox_supported = 0;
1459 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1460 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
1461 config->baudrate = std_baudrates[i].value;
1466 if (i == NUM_STD_BAUDRATES) {
1468 config->baudrate = (int)data->term.c_ispeed;
1469 #elif defined(__linux__)
1470 TRY(get_baudrate(port->fd, &config->baudrate));
1472 config->baudrate = -1;
1476 switch (data->term.c_cflag & CSIZE) {
1493 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
1494 config->parity = SP_PARITY_NONE;
1495 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
1496 config->parity = -1;
1498 else if (data->term.c_cflag & CMSPAR)
1499 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_MARK : SP_PARITY_SPACE;
1502 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
1504 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
1506 if (data->term.c_cflag & CRTSCTS) {
1507 config->rts = SP_RTS_FLOW_CONTROL;
1508 config->cts = SP_CTS_FLOW_CONTROL;
1510 if (data->termiox_supported && data->flow & RTS_FLOW)
1511 config->rts = SP_RTS_FLOW_CONTROL;
1513 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
1515 config->cts = (data->termiox_supported && data->flow & CTS_FLOW) ?
1516 SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
1519 if (data->termiox_supported && data->flow & DTR_FLOW)
1520 config->dtr = SP_DTR_FLOW_CONTROL;
1522 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
1524 config->dsr = (data->termiox_supported && data->flow & DSR_FLOW) ?
1525 SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
1527 if (data->term.c_iflag & IXOFF) {
1528 if (data->term.c_iflag & IXON)
1529 config->xon_xoff = SP_XONXOFF_INOUT;
1531 config->xon_xoff = SP_XONXOFF_IN;
1533 if (data->term.c_iflag & IXON)
1534 config->xon_xoff = SP_XONXOFF_OUT;
1536 config->xon_xoff = SP_XONXOFF_DISABLED;
1543 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
1544 const struct sp_port_config *config)
1548 BAUD_TYPE baud_nonstd;
1553 int baud_nonstd = 0;
1556 TRACE("%p, %p, %p", port, data, config);
1558 DEBUG("Setting configuration for port %s", port->name);
1561 if (config->baudrate >= 0) {
1562 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1563 if (config->baudrate == std_baudrates[i].value) {
1564 data->dcb.BaudRate = std_baudrates[i].index;
1569 if (i == NUM_STD_BAUDRATES)
1570 data->dcb.BaudRate = config->baudrate;
1573 if (config->bits >= 0)
1574 data->dcb.ByteSize = config->bits;
1576 if (config->parity >= 0) {
1577 switch (config->parity) {
1578 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
1579 case SP_PARITY_NONE:
1580 data->dcb.Parity = NOPARITY;
1583 data->dcb.Parity = ODDPARITY;
1585 case SP_PARITY_EVEN:
1586 data->dcb.Parity = EVENPARITY;
1588 case SP_PARITY_MARK:
1589 data->dcb.Parity = MARKPARITY;
1591 case SP_PARITY_SPACE:
1592 data->dcb.Parity = SPACEPARITY;
1595 RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting");
1599 if (config->stopbits >= 0) {
1600 switch (config->stopbits) {
1601 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
1603 data->dcb.StopBits = ONESTOPBIT;
1606 data->dcb.StopBits = TWOSTOPBITS;
1609 RETURN_ERROR(SP_ERR_ARG, "Invalid stop bit setting");
1613 if (config->rts >= 0) {
1614 switch (config->rts) {
1616 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
1619 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
1621 case SP_RTS_FLOW_CONTROL:
1622 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
1625 RETURN_ERROR(SP_ERR_ARG, "Invalid RTS setting");
1629 if (config->cts >= 0) {
1630 switch (config->cts) {
1632 data->dcb.fOutxCtsFlow = FALSE;
1634 case SP_CTS_FLOW_CONTROL:
1635 data->dcb.fOutxCtsFlow = TRUE;
1638 RETURN_ERROR(SP_ERR_ARG, "Invalid CTS setting");
1642 if (config->dtr >= 0) {
1643 switch (config->dtr) {
1645 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
1648 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
1650 case SP_DTR_FLOW_CONTROL:
1651 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
1654 RETURN_ERROR(SP_ERR_ARG, "Invalid DTR setting");
1658 if (config->dsr >= 0) {
1659 switch (config->dsr) {
1661 data->dcb.fOutxDsrFlow = FALSE;
1663 case SP_DSR_FLOW_CONTROL:
1664 data->dcb.fOutxDsrFlow = TRUE;
1667 RETURN_ERROR(SP_ERR_ARG, "Invalid DSR setting");
1671 if (config->xon_xoff >= 0) {
1672 switch (config->xon_xoff) {
1673 case SP_XONXOFF_DISABLED:
1674 data->dcb.fInX = FALSE;
1675 data->dcb.fOutX = FALSE;
1678 data->dcb.fInX = TRUE;
1679 data->dcb.fOutX = FALSE;
1681 case SP_XONXOFF_OUT:
1682 data->dcb.fInX = FALSE;
1683 data->dcb.fOutX = TRUE;
1685 case SP_XONXOFF_INOUT:
1686 data->dcb.fInX = TRUE;
1687 data->dcb.fOutX = TRUE;
1690 RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting");
1694 if (!SetCommState(port->hdl, &data->dcb))
1695 RETURN_FAIL("SetCommState() failed");
1701 if (config->baudrate >= 0) {
1702 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1703 if (config->baudrate == std_baudrates[i].value) {
1704 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
1705 RETURN_FAIL("cfsetospeed() failed");
1707 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
1708 RETURN_FAIL("cfsetispeed() failed");
1713 /* Non-standard baud rate */
1714 if (i == NUM_STD_BAUDRATES) {
1716 /* Set "dummy" baud rate. */
1717 if (cfsetspeed(&data->term, B9600) < 0)
1718 RETURN_FAIL("cfsetspeed() failed");
1719 baud_nonstd = config->baudrate;
1720 #elif defined(__linux__)
1723 RETURN_ERROR(SP_ERR_SUPP, "Non-standard baudrate not supported");
1728 if (config->bits >= 0) {
1729 data->term.c_cflag &= ~CSIZE;
1730 switch (config->bits) {
1732 data->term.c_cflag |= CS8;
1735 data->term.c_cflag |= CS7;
1738 data->term.c_cflag |= CS6;
1741 data->term.c_cflag |= CS5;
1744 RETURN_ERROR(SP_ERR_ARG, "Invalid data bits setting");
1748 if (config->parity >= 0) {
1749 data->term.c_iflag &= ~IGNPAR;
1750 data->term.c_cflag &= ~(PARENB | PARODD);
1752 data->term.c_cflag &= ~CMSPAR;
1754 switch (config->parity) {
1755 case SP_PARITY_NONE:
1756 data->term.c_iflag |= IGNPAR;
1758 case SP_PARITY_EVEN:
1759 data->term.c_cflag |= PARENB;
1762 data->term.c_cflag |= PARENB | PARODD;
1765 case SP_PARITY_MARK:
1766 data->term.c_cflag |= PARENB | PARODD;
1767 data->term.c_cflag |= CMSPAR;
1769 case SP_PARITY_SPACE:
1770 data->term.c_cflag |= PARENB;
1771 data->term.c_cflag |= CMSPAR;
1774 case SP_PARITY_MARK:
1775 case SP_PARITY_SPACE:
1776 RETURN_ERROR(SP_ERR_SUPP, "Mark/space parity not supported");
1779 RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting");
1783 if (config->stopbits >= 0) {
1784 data->term.c_cflag &= ~CSTOPB;
1785 switch (config->stopbits) {
1787 data->term.c_cflag &= ~CSTOPB;
1790 data->term.c_cflag |= CSTOPB;
1793 RETURN_ERROR(SP_ERR_ARG, "Invalid stop bits setting");
1797 if (config->rts >= 0 || config->cts >= 0) {
1798 if (data->termiox_supported) {
1799 data->flow &= ~(RTS_FLOW | CTS_FLOW);
1800 switch (config->rts) {
1803 controlbits = TIOCM_RTS;
1804 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1805 RETURN_FAIL("Setting RTS signal level failed");
1807 case SP_RTS_FLOW_CONTROL:
1808 data->flow |= RTS_FLOW;
1813 if (config->cts == SP_CTS_FLOW_CONTROL)
1814 data->flow |= CTS_FLOW;
1816 if (data->flow & (RTS_FLOW | CTS_FLOW))
1817 data->term.c_iflag |= CRTSCTS;
1819 data->term.c_iflag &= ~CRTSCTS;
1821 /* Asymmetric use of RTS/CTS not supported. */
1822 if (data->term.c_iflag & CRTSCTS) {
1823 /* Flow control can only be disabled for both RTS & CTS together. */
1824 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
1825 if (config->cts != SP_CTS_IGNORE)
1826 RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together");
1828 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
1829 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
1830 RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together");
1833 /* Flow control can only be enabled for both RTS & CTS together. */
1834 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
1835 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
1836 RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be enabled together");
1839 if (config->rts >= 0) {
1840 if (config->rts == SP_RTS_FLOW_CONTROL) {
1841 data->term.c_iflag |= CRTSCTS;
1843 controlbits = TIOCM_RTS;
1844 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
1846 RETURN_FAIL("Setting RTS signal level failed");
1852 if (config->dtr >= 0 || config->dsr >= 0) {
1853 if (data->termiox_supported) {
1854 data->flow &= ~(DTR_FLOW | DSR_FLOW);
1855 switch (config->dtr) {
1858 controlbits = TIOCM_DTR;
1859 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1860 RETURN_FAIL("Setting DTR signal level failed");
1862 case SP_DTR_FLOW_CONTROL:
1863 data->flow |= DTR_FLOW;
1868 if (config->dsr == SP_DSR_FLOW_CONTROL)
1869 data->flow |= DSR_FLOW;
1871 /* DTR/DSR flow control not supported. */
1872 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
1873 RETURN_ERROR(SP_ERR_SUPP, "DTR/DSR flow control not supported");
1875 if (config->dtr >= 0) {
1876 controlbits = TIOCM_DTR;
1877 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
1879 RETURN_FAIL("Setting DTR signal level failed");
1884 if (config->xon_xoff >= 0) {
1885 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
1886 switch (config->xon_xoff) {
1887 case SP_XONXOFF_DISABLED:
1890 data->term.c_iflag |= IXOFF;
1892 case SP_XONXOFF_OUT:
1893 data->term.c_iflag |= IXON | IXANY;
1895 case SP_XONXOFF_INOUT:
1896 data->term.c_iflag |= IXON | IXOFF | IXANY;
1899 RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting");
1903 if (tcsetattr(port->fd, TCSANOW, &data->term) < 0)
1904 RETURN_FAIL("tcsetattr() failed");
1907 if (baud_nonstd != B0) {
1908 if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
1909 RETURN_FAIL("IOSSIOSPEED ioctl failed");
1910 /* Set baud rates in data->term to correct, but incompatible
1911 * with tcsetattr() value, same as delivered by tcgetattr(). */
1912 if (cfsetspeed(&data->term, baud_nonstd) < 0)
1913 RETURN_FAIL("cfsetspeed() failed");
1915 #elif defined(__linux__)
1917 TRY(set_baudrate(port->fd, config->baudrate));
1919 if (data->termiox_supported)
1920 TRY(set_flow(port->fd, data->flow));
1924 #endif /* !_WIN32 */
1929 enum sp_return sp_new_config(struct sp_port_config **config_ptr)
1931 struct sp_port_config *config;
1933 TRACE("%p", config_ptr);
1936 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
1940 if (!(config = malloc(sizeof(struct sp_port_config))))
1941 RETURN_ERROR(SP_ERR_MEM, "config malloc failed");
1943 config->baudrate = -1;
1945 config->parity = -1;
1946 config->stopbits = -1;
1952 *config_ptr = config;
1957 void sp_free_config(struct sp_port_config *config)
1959 TRACE("%p", config);
1962 DEBUG("Null config");
1969 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config)
1971 struct port_data data;
1973 TRACE("%p, %p", port, config);
1978 RETURN_ERROR(SP_ERR_ARG, "Null config");
1980 TRY(get_config(port, &data, config));
1985 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
1987 struct port_data data;
1988 struct sp_port_config prev_config;
1990 TRACE("%p, %p", port, config);
1995 RETURN_ERROR(SP_ERR_ARG, "Null config");
1997 TRY(get_config(port, &data, &prev_config));
1998 TRY(set_config(port, &data, config));
2003 #define CREATE_ACCESSORS(x, type) \
2004 enum sp_return sp_set_##x(struct sp_port *port, type x) { \
2005 struct port_data data; \
2006 struct sp_port_config config; \
2007 TRACE("%p, %d", port, x); \
2008 CHECK_OPEN_PORT(); \
2009 TRY(get_config(port, &data, &config)); \
2011 TRY(set_config(port, &data, &config)); \
2014 enum sp_return sp_get_config_##x(const struct sp_port_config *config, type *x) { \
2015 TRACE("%p, %p", config, x); \
2017 RETURN_ERROR(SP_ERR_ARG, "Null config"); \
2021 enum sp_return sp_set_config_##x(struct sp_port_config *config, type x) { \
2022 TRACE("%p, %d", config, x); \
2024 RETURN_ERROR(SP_ERR_ARG, "Null config"); \
2029 CREATE_ACCESSORS(baudrate, int)
2030 CREATE_ACCESSORS(bits, int)
2031 CREATE_ACCESSORS(parity, enum sp_parity)
2032 CREATE_ACCESSORS(stopbits, int)
2033 CREATE_ACCESSORS(rts, enum sp_rts)
2034 CREATE_ACCESSORS(cts, enum sp_cts)
2035 CREATE_ACCESSORS(dtr, enum sp_dtr)
2036 CREATE_ACCESSORS(dsr, enum sp_dsr)
2037 CREATE_ACCESSORS(xon_xoff, enum sp_xonxoff)
2039 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol)
2042 RETURN_ERROR(SP_ERR_ARG, "Null configuration");
2044 if (flowcontrol > SP_FLOWCONTROL_DTRDSR)
2045 RETURN_ERROR(SP_ERR_ARG, "Invalid flow control setting");
2047 if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
2048 config->xon_xoff = SP_XONXOFF_INOUT;
2050 config->xon_xoff = SP_XONXOFF_DISABLED;
2052 if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
2053 config->rts = SP_RTS_FLOW_CONTROL;
2054 config->cts = SP_CTS_FLOW_CONTROL;
2056 if (config->rts == SP_RTS_FLOW_CONTROL)
2057 config->rts = SP_RTS_ON;
2058 config->cts = SP_CTS_IGNORE;
2061 if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
2062 config->dtr = SP_DTR_FLOW_CONTROL;
2063 config->dsr = SP_DSR_FLOW_CONTROL;
2065 if (config->dtr == SP_DTR_FLOW_CONTROL)
2066 config->dtr = SP_DTR_ON;
2067 config->dsr = SP_DSR_IGNORE;
2073 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
2075 struct port_data data;
2076 struct sp_port_config config;
2078 TRACE("%p, %d", port, flowcontrol);
2082 TRY(get_config(port, &data, &config));
2084 TRY(sp_set_config_flowcontrol(&config, flowcontrol));
2086 TRY(set_config(port, &data, &config));
2091 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals)
2093 TRACE("%p, %p", port, signals);
2098 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
2100 DEBUG("Getting control signals for port %s", port->name);
2105 if (GetCommModemStatus(port->hdl, &bits) == 0)
2106 RETURN_FAIL("GetCommModemStatus() failed");
2107 if (bits & MS_CTS_ON)
2108 *signals |= SP_SIG_CTS;
2109 if (bits & MS_DSR_ON)
2110 *signals |= SP_SIG_DSR;
2111 if (bits & MS_RLSD_ON)
2112 *signals |= SP_SIG_DCD;
2113 if (bits & MS_RING_ON)
2114 *signals |= SP_SIG_RI;
2117 if (ioctl(port->fd, TIOCMGET, &bits) < 0)
2118 RETURN_FAIL("TIOCMGET ioctl failed");
2119 if (bits & TIOCM_CTS)
2120 *signals |= SP_SIG_CTS;
2121 if (bits & TIOCM_DSR)
2122 *signals |= SP_SIG_DSR;
2123 if (bits & TIOCM_CAR)
2124 *signals |= SP_SIG_DCD;
2125 if (bits & TIOCM_RNG)
2126 *signals |= SP_SIG_RI;
2131 enum sp_return sp_start_break(struct sp_port *port)
2137 if (SetCommBreak(port->hdl) == 0)
2138 RETURN_FAIL("SetCommBreak() failed");
2140 if (ioctl(port->fd, TIOCSBRK, 1) < 0)
2141 RETURN_FAIL("TIOCSBRK ioctl failed");
2147 enum sp_return sp_end_break(struct sp_port *port)
2153 if (ClearCommBreak(port->hdl) == 0)
2154 RETURN_FAIL("ClearCommBreak() failed");
2156 if (ioctl(port->fd, TIOCCBRK, 1) < 0)
2157 RETURN_FAIL("TIOCCBRK ioctl failed");
2163 int sp_last_error_code(void)
2167 RETURN_VALUE("%d", GetLastError());
2169 RETURN_VALUE("%d", errno);
2173 char *sp_last_error_message(void)
2179 DWORD error = GetLastError();
2182 FORMAT_MESSAGE_ALLOCATE_BUFFER |
2183 FORMAT_MESSAGE_FROM_SYSTEM |
2184 FORMAT_MESSAGE_IGNORE_INSERTS,
2187 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
2191 RETURN_VALUE("%s", message);
2193 RETURN_VALUE("%s", strerror(errno));
2197 void sp_free_error_message(char *message)
2199 TRACE("%s", message);
2210 void sp_set_debug_handler(void (*handler)(const char *format, ...))
2212 TRACE("%p", handler);
2214 sp_debug_handler = handler;
2219 void sp_default_debug_handler(const char *format, ...)
2222 va_start(args, format);
2223 if (getenv("LIBSERIALPORT_DEBUG")) {
2224 fputs("sp: ", stderr);
2225 vfprintf(stderr, format, args);