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>
36 #include <sys/ioctl.h>
39 #include <IOKit/IOKitLib.h>
40 #include <IOKit/serial/IOSerialKeys.h>
41 #include <IOKit/serial/ioss.h>
42 #include <sys/syslimits.h>
46 #include "linux/serial.h"
47 #include "linux_termios.h"
48 #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX)
54 #include "linux_termios.h"
57 #include "libserialport.h"
65 int termiox_supported;
70 /* Standard baud rates. */
72 #define BAUD_TYPE DWORD
73 #define BAUD(n) {CBR_##n, n}
75 #define BAUD_TYPE speed_t
76 #define BAUD(n) {B##n, n}
84 const struct std_baudrate std_baudrates[] = {
87 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
88 * have documented CBR_* macros.
90 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
91 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
92 BAUD(115200), BAUD(128000), BAUD(256000),
94 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
95 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
96 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
98 #if !defined(__APPLE__) && !defined(__OpenBSD__)
104 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
105 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
107 #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
109 /* Helper functions. */
110 static enum sp_return validate_port(struct sp_port *port);
111 static struct sp_port **list_append(struct sp_port **list, const char *portname);
112 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
113 struct sp_port_config *config);
114 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
115 const struct sp_port_config *config);
117 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
119 struct sp_port *port;
130 if (!(port = malloc(sizeof(struct sp_port))))
133 len = strlen(portname) + 1;
135 if (!(port->name = malloc(len))) {
140 memcpy(port->name, portname, len);
143 port->hdl = INVALID_HANDLE_VALUE;
153 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
160 if (!port || !port->name)
163 return sp_get_port_by_name(port->name, copy_ptr);
166 void sp_free_port(struct sp_port *port)
177 static struct sp_port **list_append(struct sp_port **list, const char *portname)
182 for (count = 0; list[count]; count++);
183 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
186 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
188 list[count + 1] = NULL;
192 sp_free_port_list(list);
196 enum sp_return sp_list_ports(struct sp_port ***list_ptr)
198 struct sp_port **list;
201 if (!(list = malloc(sizeof(struct sp_port **))))
209 DWORD max_value_len, max_data_size, max_data_len;
210 DWORD value_len, data_size, data_len;
211 DWORD type, index = 0;
215 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
216 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
220 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
221 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
225 max_data_len = max_data_size / sizeof(TCHAR);
226 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
230 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
235 value_len = max_value_len + 1,
236 data_size = max_data_size,
237 RegEnumValue(key, index, value, &value_len,
238 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
240 data_len = data_size / sizeof(TCHAR);
241 data[data_len] = '\0';
243 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
245 name_len = data_len + 1;
247 if (!(name = malloc(name_len))) {
252 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
256 if (type == REG_SZ && !(list = list_append(list, name))) {
272 CFMutableDictionaryRef classes;
279 if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
284 if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
289 CFDictionarySetValue(classes,
290 CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
292 if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
297 if (!(path = malloc(PATH_MAX))) {
302 while ((port = IOIteratorNext(iter))) {
303 cf_path = IORegistryEntryCreateCFProperty(port,
304 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
306 result = CFStringGetCString(cf_path,
307 path, PATH_MAX, kCFStringEncodingASCII);
309 if (result && !(list = list_append(list, path))) {
311 IOObjectRelease(port);
315 IOObjectRelease(port);
320 IOObjectRelease(iter);
325 struct udev_enumerate *ud_enumerate;
326 struct udev_list_entry *ud_list;
327 struct udev_list_entry *ud_entry;
329 struct udev_device *ud_dev, *ud_parent;
332 int fd, ioctl_result;
333 struct serial_struct serial_info;
336 ud_enumerate = udev_enumerate_new(ud);
337 udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
338 udev_enumerate_scan_devices(ud_enumerate);
339 ud_list = udev_enumerate_get_list_entry(ud_enumerate);
340 udev_list_entry_foreach(ud_entry, ud_list) {
341 path = udev_list_entry_get_name(ud_entry);
342 ud_dev = udev_device_new_from_syspath(ud, path);
343 /* If there is no parent device, this is a virtual tty. */
344 ud_parent = udev_device_get_parent(ud_dev);
345 if (ud_parent == NULL) {
346 udev_device_unref(ud_dev);
349 name = udev_device_get_devnode(ud_dev);
350 /* The serial8250 driver has a hardcoded number of ports.
351 * The only way to tell which actually exist on a given system
352 * is to try to open them and make an ioctl call. */
353 driver = udev_device_get_driver(ud_parent);
354 if (driver && !strcmp(driver, "serial8250")) {
355 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
357 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
359 if (ioctl_result != 0)
361 if (serial_info.type == PORT_UNKNOWN)
364 list = list_append(list, name);
366 udev_device_unref(ud_dev);
373 udev_enumerate_unref(ud_enumerate);
381 sp_free_port_list(list);
388 void sp_free_port_list(struct sp_port **list)
392 for (i = 0; list[i]; i++)
393 sp_free_port(list[i]);
397 static enum sp_return validate_port(struct sp_port *port)
402 if (port->hdl == INVALID_HANDLE_VALUE)
411 #define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0)
413 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
419 DWORD desired_access = 0, flags_and_attributes = 0;
420 char *escaped_port_name;
422 /* Prefix port name with '\\.\' to work with ports above COM9. */
423 if (!(escaped_port_name = malloc(strlen(port->name + 5))))
425 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
427 /* Map 'flags' to the OS-specific settings. */
428 flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
429 if (flags & SP_MODE_READ)
430 desired_access |= GENERIC_READ;
431 if (flags & SP_MODE_WRITE)
432 desired_access |= GENERIC_WRITE;
433 if (flags & SP_MODE_NONBLOCK)
434 flags_and_attributes |= FILE_FLAG_OVERLAPPED;
436 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
437 OPEN_EXISTING, flags_and_attributes, 0);
439 free(escaped_port_name);
441 if (port->hdl == INVALID_HANDLE_VALUE)
445 struct port_data data;
446 struct sp_port_config config;
449 /* Map 'flags' to the OS-specific settings. */
450 if (flags & (SP_MODE_READ | SP_MODE_WRITE))
451 flags_local |= O_RDWR;
452 else if (flags & SP_MODE_READ)
453 flags_local |= O_RDONLY;
454 else if (flags & SP_MODE_WRITE)
455 flags_local |= O_WRONLY;
456 if (flags & SP_MODE_NONBLOCK)
457 flags_local |= O_NONBLOCK;
459 if ((port->fd = open(port->name, flags_local)) < 0)
462 ret = get_config(port, &data, &config);
469 /* Turn off all serial port cooking. */
470 data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
471 data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
472 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
473 data.term.c_oflag &= ~OFILL;
475 /* Disable canonical mode, and don't echo input characters. */
476 data.term.c_lflag &= ~(ICANON | ECHO);
478 /* Ignore modem status lines; enable receiver */
479 data.term.c_cflag |= (CLOCAL | CREAD);
481 ret = set_config(port, &data, &config);
492 enum sp_return sp_close(struct sp_port *port)
497 /* Returns non-zero upon success, 0 upon failure. */
498 if (CloseHandle(port->hdl) == 0)
500 port->hdl = INVALID_HANDLE_VALUE;
502 /* Returns 0 upon success, -1 upon failure. */
503 if (close(port->fd) == -1)
511 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
517 if (buffers & SP_BUF_INPUT)
518 flags |= PURGE_RXCLEAR;
519 if (buffers & SP_BUF_OUTPUT)
520 flags |= PURGE_TXCLEAR;
522 /* Returns non-zero upon success, 0 upon failure. */
523 if (PurgeComm(port->hdl, flags) == 0)
527 if (buffers & SP_BUF_BOTH)
529 else if (buffers & SP_BUF_INPUT)
531 else if (buffers & SP_BUF_OUTPUT)
534 /* Returns 0 upon success, -1 upon failure. */
535 if (tcflush(port->fd, flags) < 0)
541 enum sp_return sp_drain(struct sp_port *port)
546 /* Returns non-zero upon success, 0 upon failure. */
547 if (FlushFileBuffers(port->hdl) == 0)
550 /* Returns 0 upon success, -1 upon failure. */
551 if (tcdrain(port->fd) < 0)
558 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
568 /* Returns non-zero upon success, 0 upon failure. */
569 if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
573 /* Returns the number of bytes written, or -1 upon failure. */
574 ssize_t written = write(port->fd, buf, count);
583 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
591 DWORD bytes_read = 0;
593 /* Returns non-zero upon success, 0 upon failure. */
594 if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
600 /* Returns the number of bytes read, or -1 upon failure. */
601 if ((bytes_read = read(port->fd, buf, count)) < 0)
608 static enum sp_return get_baudrate(int fd, int *baudrate)
612 if (!(data = malloc(get_termios_size())))
615 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
620 *baudrate = get_termios_speed(data);
627 static enum sp_return set_baudrate(int fd, int baudrate)
631 if (!(data = malloc(get_termios_size())))
634 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
639 set_termios_speed(data, baudrate);
641 if (ioctl(fd, get_termios_set_ioctl(), data) < 0) {
652 static enum sp_return get_flow(int fd, int *flow)
656 if (!(data = malloc(get_termiox_size())))
659 if (ioctl(fd, TCGETX, data) < 0) {
664 *flow = get_termiox_flow(data);
671 static enum sp_return set_flow(int fd, int flow)
675 if (!(data = malloc(get_termiox_size())))
678 if (ioctl(fd, TCGETX, data) < 0) {
683 set_termiox_flow(data, flow);
685 if (ioctl(fd, TCSETX, data) < 0) {
694 #endif /* USE_TERMIOX */
695 #endif /* __linux__ */
697 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
698 struct sp_port_config *config)
703 if (!GetCommState(port->hdl, &data->dcb))
706 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
707 if (data->dcb.BaudRate == std_baudrates[i].index) {
708 config->baudrate = std_baudrates[i].value;
713 if (i == NUM_STD_BAUDRATES)
714 /* BaudRate field can be either an index or a custom baud rate. */
715 config->baudrate = data->dcb.BaudRate;
717 config->bits = data->dcb.ByteSize;
719 if (data->dcb.fParity)
720 switch (data->dcb.Parity) {
722 config->parity = SP_PARITY_NONE;
725 config->parity = SP_PARITY_EVEN;
728 config->parity = SP_PARITY_ODD;
734 config->parity = SP_PARITY_NONE;
736 switch (data->dcb.StopBits) {
738 config->stopbits = 1;
741 config->stopbits = 2;
744 config->stopbits = -1;
747 switch (data->dcb.fRtsControl) {
748 case RTS_CONTROL_DISABLE:
749 config->rts = SP_RTS_OFF;
751 case RTS_CONTROL_ENABLE:
752 config->rts = SP_RTS_ON;
754 case RTS_CONTROL_HANDSHAKE:
755 config->rts = SP_RTS_FLOW_CONTROL;
761 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
763 switch (data->dcb.fDtrControl) {
764 case DTR_CONTROL_DISABLE:
765 config->dtr = SP_DTR_OFF;
767 case DTR_CONTROL_ENABLE:
768 config->dtr = SP_DTR_ON;
770 case DTR_CONTROL_HANDSHAKE:
771 config->dtr = SP_DTR_FLOW_CONTROL;
777 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
779 if (data->dcb.fInX) {
781 config->xon_xoff = SP_XONXOFF_INOUT;
783 config->xon_xoff = SP_XONXOFF_IN;
786 config->xon_xoff = SP_XONXOFF_OUT;
788 config->xon_xoff = SP_XONXOFF_DISABLED;
793 if (tcgetattr(port->fd, &data->term) < 0)
796 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
800 int ret = get_flow(port->fd, &data->flow);
802 if (ret == SP_ERR_FAIL && errno == EINVAL)
803 data->termiox_supported = 0;
807 data->termiox_supported = 1;
809 data->termiox_supported = 0;
812 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
813 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
814 config->baudrate = std_baudrates[i].value;
819 if (i == NUM_STD_BAUDRATES) {
821 config->baudrate = (int)data->term.c_ispeed;
822 #elif defined(__linux__)
823 TRY(get_baudrate(port->fd, &config->baudrate));
825 config->baudrate = -1;
829 switch (data->term.c_cflag & CSIZE) {
846 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
847 config->parity = SP_PARITY_NONE;
848 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
851 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
853 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
855 if (data->term.c_cflag & CRTSCTS) {
856 config->rts = SP_RTS_FLOW_CONTROL;
857 config->cts = SP_CTS_FLOW_CONTROL;
859 if (data->termiox_supported && data->flow & RTS_FLOW)
860 config->rts = SP_RTS_FLOW_CONTROL;
862 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
864 config->cts = (data->termiox_supported && data->flow & CTS_FLOW) ?
865 SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
868 if (data->termiox_supported && data->flow & DTR_FLOW)
869 config->dtr = SP_DTR_FLOW_CONTROL;
871 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
873 config->dsr = (data->termiox_supported && data->flow & DSR_FLOW) ?
874 SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
876 if (data->term.c_iflag & IXOFF) {
877 if (data->term.c_iflag & IXON)
878 config->xon_xoff = SP_XONXOFF_INOUT;
880 config->xon_xoff = SP_XONXOFF_IN;
882 if (data->term.c_iflag & IXON)
883 config->xon_xoff = SP_XONXOFF_OUT;
885 config->xon_xoff = SP_XONXOFF_DISABLED;
892 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
893 const struct sp_port_config *config)
897 BAUD_TYPE baud_nonstd;
906 if (config->baudrate >= 0) {
907 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
908 if (config->baudrate == std_baudrates[i].value) {
909 data->dcb.BaudRate = std_baudrates[i].index;
914 if (i == NUM_STD_BAUDRATES)
915 data->dcb.BaudRate = config->baudrate;
918 if (config->bits >= 0)
919 data->dcb.ByteSize = config->bits;
921 if (config->parity >= 0) {
922 switch (config->parity) {
923 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
925 data->dcb.Parity = NOPARITY;
928 data->dcb.Parity = EVENPARITY;
931 data->dcb.Parity = ODDPARITY;
938 if (config->stopbits >= 0) {
939 switch (config->stopbits) {
940 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
942 data->dcb.StopBits = ONESTOPBIT;
945 data->dcb.StopBits = TWOSTOPBITS;
952 if (config->rts >= 0) {
953 switch (config->rts) {
955 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
958 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
960 case SP_RTS_FLOW_CONTROL:
961 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
968 if (config->cts >= 0) {
969 switch (config->cts) {
971 data->dcb.fOutxCtsFlow = FALSE;
973 case SP_CTS_FLOW_CONTROL:
974 data->dcb.fOutxCtsFlow = TRUE;
981 if (config->dtr >= 0) {
982 switch (config->dtr) {
984 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
987 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
989 case SP_DTR_FLOW_CONTROL:
990 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
997 if (config->dsr >= 0) {
998 switch (config->dsr) {
1000 data->dcb.fOutxDsrFlow = FALSE;
1002 case SP_DSR_FLOW_CONTROL:
1003 data->dcb.fOutxDsrFlow = TRUE;
1010 if (config->xon_xoff >= 0) {
1011 switch (config->xon_xoff) {
1012 case SP_XONXOFF_DISABLED:
1013 data->dcb.fInX = FALSE;
1014 data->dcb.fOutX = FALSE;
1017 data->dcb.fInX = TRUE;
1018 data->dcb.fOutX = FALSE;
1020 case SP_XONXOFF_OUT:
1021 data->dcb.fInX = FALSE;
1022 data->dcb.fOutX = TRUE;
1024 case SP_XONXOFF_INOUT:
1025 data->dcb.fInX = TRUE;
1026 data->dcb.fOutX = TRUE;
1033 if (!SetCommState(port->hdl, &data->dcb))
1040 if (config->baudrate >= 0) {
1041 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1042 if (config->baudrate == std_baudrates[i].value) {
1043 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
1046 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
1052 /* Non-standard baud rate */
1053 if (i == NUM_STD_BAUDRATES) {
1055 /* Set "dummy" baud rate. */
1056 if (cfsetspeed(&data->term, B9600) < 0)
1058 baud_nonstd = config->baudrate;
1059 #elif defined(__linux__)
1067 if (config->bits >= 0) {
1068 data->term.c_cflag &= ~CSIZE;
1069 switch (config->bits) {
1071 data->term.c_cflag |= CS8;
1074 data->term.c_cflag |= CS7;
1077 data->term.c_cflag |= CS6;
1080 data->term.c_cflag |= CS5;
1087 if (config->parity >= 0) {
1088 data->term.c_iflag &= ~IGNPAR;
1089 data->term.c_cflag &= ~(PARENB | PARODD);
1090 switch (config->parity) {
1091 case SP_PARITY_NONE:
1092 data->term.c_iflag |= IGNPAR;
1094 case SP_PARITY_EVEN:
1095 data->term.c_cflag |= PARENB;
1098 data->term.c_cflag |= PARENB | PARODD;
1105 if (config->stopbits >= 0) {
1106 data->term.c_cflag &= ~CSTOPB;
1107 switch (config->stopbits) {
1109 data->term.c_cflag &= ~CSTOPB;
1112 data->term.c_cflag |= CSTOPB;
1119 if (config->rts >= 0 || config->cts >= 0) {
1120 if (data->termiox_supported) {
1121 data->flow &= ~(RTS_FLOW | CTS_FLOW);
1122 switch (config->rts) {
1125 controlbits = TIOCM_RTS;
1126 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1129 case SP_RTS_FLOW_CONTROL:
1130 data->flow |= RTS_FLOW;
1135 if (config->cts == SP_CTS_FLOW_CONTROL)
1136 data->flow |= CTS_FLOW;
1138 if (data->flow & (RTS_FLOW | CTS_FLOW))
1139 data->term.c_iflag |= CRTSCTS;
1141 data->term.c_iflag &= ~CRTSCTS;
1143 /* Asymmetric use of RTS/CTS not supported. */
1144 if (data->term.c_iflag & CRTSCTS) {
1145 /* Flow control can only be disabled for both RTS & CTS together. */
1146 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
1147 if (config->cts != SP_CTS_IGNORE)
1150 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
1151 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
1155 /* Flow control can only be enabled for both RTS & CTS together. */
1156 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
1157 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
1161 if (config->rts >= 0) {
1162 if (config->rts == SP_RTS_FLOW_CONTROL) {
1163 data->term.c_iflag |= CRTSCTS;
1165 controlbits = TIOCM_RTS;
1166 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
1174 if (config->dtr >= 0 || config->dsr >= 0) {
1175 if (data->termiox_supported) {
1176 data->flow &= ~(DTR_FLOW | DSR_FLOW);
1177 switch (config->dtr) {
1180 controlbits = TIOCM_DTR;
1181 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1184 case SP_DTR_FLOW_CONTROL:
1185 data->flow |= DTR_FLOW;
1190 if (config->dsr == SP_DSR_FLOW_CONTROL)
1191 data->flow |= DSR_FLOW;
1193 /* DTR/DSR flow control not supported. */
1194 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
1197 if (config->dtr >= 0) {
1198 controlbits = TIOCM_DTR;
1199 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
1206 if (config->xon_xoff >= 0) {
1207 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
1208 switch (config->xon_xoff) {
1209 case SP_XONXOFF_DISABLED:
1212 data->term.c_iflag |= IXOFF;
1214 case SP_XONXOFF_OUT:
1215 data->term.c_iflag |= IXON | IXANY;
1217 case SP_XONXOFF_INOUT:
1218 data->term.c_iflag |= IXON | IXOFF | IXANY;
1225 if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
1229 if (baud_nonstd != B0) {
1230 if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
1232 /* Set baud rates in data->term to correct, but incompatible
1233 * with tcsetattr() value, same as delivered by tcgetattr(). */
1234 if (cfsetspeed(&data->term, baud_nonstd) < 0)
1237 #elif defined(__linux__)
1239 TRY(set_baudrate(port->fd, config->baudrate));
1241 if (data->termiox_supported)
1242 TRY(set_flow(port->fd, data->flow));
1246 #endif /* !_WIN32 */
1251 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
1253 struct port_data data;
1254 struct sp_port_config prev_config;
1261 TRY(get_config(port, &data, &prev_config));
1262 TRY(set_config(port, &data, config));
1267 #define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \
1268 struct port_data data; \
1269 struct sp_port_config config; \
1271 TRY(get_config(port, &data, &config)); \
1273 TRY(set_config(port, &data, &config)); \
1277 CREATE_SETTER(baudrate, int)
1278 CREATE_SETTER(bits, int)
1279 CREATE_SETTER(parity, enum sp_parity)
1280 CREATE_SETTER(stopbits, int)
1281 CREATE_SETTER(rts, enum sp_rts)
1282 CREATE_SETTER(cts, enum sp_cts)
1283 CREATE_SETTER(dtr, enum sp_dtr)
1284 CREATE_SETTER(dsr, enum sp_dsr)
1285 CREATE_SETTER(xon_xoff, enum sp_xonxoff)
1287 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
1289 struct port_data data;
1290 struct sp_port_config config;
1294 TRY(get_config(port, &data, &config));
1296 if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
1297 config.xon_xoff = SP_XONXOFF_INOUT;
1299 config.xon_xoff = SP_XONXOFF_DISABLED;
1301 if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
1302 config.rts = SP_RTS_FLOW_CONTROL;
1303 config.cts = SP_CTS_FLOW_CONTROL;
1305 if (config.rts == SP_RTS_FLOW_CONTROL)
1306 config.rts = SP_RTS_ON;
1307 config.cts = SP_CTS_IGNORE;
1310 if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
1311 config.dtr = SP_DTR_FLOW_CONTROL;
1312 config.dsr = SP_DSR_FLOW_CONTROL;
1314 if (config.dtr == SP_DTR_FLOW_CONTROL)
1315 config.dtr = SP_DTR_ON;
1316 config.dsr = SP_DSR_IGNORE;
1319 TRY(set_config(port, &data, &config));
1324 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals)
1334 if (GetCommModemStatus(port->hdl, &bits) == 0)
1336 if (bits & MS_CTS_ON)
1337 *signals |= SP_SIG_CTS;
1338 if (bits & MS_DSR_ON)
1339 *signals |= SP_SIG_DSR;
1340 if (bits & MS_RING_ON)
1341 *signals |= SP_SIG_DCD;
1342 if (bits & MS_RLSD_ON)
1343 *signals |= SP_SIG_RI;
1346 if (ioctl(port->fd, TIOCMGET, &bits) < 0)
1348 if (bits & TIOCM_CTS)
1349 *signals |= SP_SIG_CTS;
1350 if (bits & TIOCM_DSR)
1351 *signals |= SP_SIG_DSR;
1352 if (bits & TIOCM_CAR)
1353 *signals |= SP_SIG_DCD;
1354 if (bits & TIOCM_RNG)
1355 *signals |= SP_SIG_RI;
1360 enum sp_return sp_start_break(struct sp_port *port)
1364 if (SetCommBreak(port->hdl) == 0)
1367 if (ioctl(port->fd, TIOCSBRK, 1) < 0)
1374 enum sp_return sp_end_break(struct sp_port *port)
1378 if (ClearCommBreak(port->hdl) == 0)
1381 if (ioctl(port->fd, TIOCCBRK, 1) < 0)
1388 int sp_last_error_code(void)
1391 return GetLastError();
1397 char *sp_last_error_message(void)
1401 DWORD error = GetLastError();
1404 FORMAT_MESSAGE_ALLOCATE_BUFFER |
1405 FORMAT_MESSAGE_FROM_SYSTEM |
1406 FORMAT_MESSAGE_IGNORE_INSERTS,
1409 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1415 return strerror(errno);
1419 void sp_free_error_message(char *message)