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)
53 #include "libserialport.h"
67 /* Standard baud rates. */
69 #define BAUD_TYPE DWORD
70 #define BAUD(n) {CBR_##n, n}
72 #define BAUD_TYPE speed_t
73 #define BAUD(n) {B##n, n}
81 const struct std_baudrate std_baudrates[] = {
84 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
85 * have documented CBR_* macros.
87 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
88 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
89 BAUD(115200), BAUD(128000), BAUD(256000),
91 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
92 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
93 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
95 #if !defined(__APPLE__) && !defined(__OpenBSD__)
101 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
102 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
104 #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
106 /* Helper functions. */
107 static enum sp_return validate_port(struct sp_port *port);
108 static struct sp_port **list_append(struct sp_port **list, const char *portname);
109 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
110 struct sp_port_config *config);
111 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
112 const struct sp_port_config *config);
114 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
116 struct sp_port *port;
127 if (!(port = malloc(sizeof(struct sp_port))))
130 len = strlen(portname) + 1;
132 if (!(port->name = malloc(len))) {
137 memcpy(port->name, portname, len);
140 port->hdl = INVALID_HANDLE_VALUE;
150 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
157 if (!port || !port->name)
160 return sp_get_port_by_name(port->name, copy_ptr);
163 void sp_free_port(struct sp_port *port)
174 static struct sp_port **list_append(struct sp_port **list, const char *portname)
179 for (count = 0; list[count]; count++);
180 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
183 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
185 list[count + 1] = NULL;
189 sp_free_port_list(list);
193 enum sp_return sp_list_ports(struct sp_port ***list_ptr)
195 struct sp_port **list;
198 if (!(list = malloc(sizeof(struct sp_port **))))
206 DWORD max_value_len, max_data_size, max_data_len;
207 DWORD value_len, data_size, data_len;
208 DWORD type, index = 0;
212 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
213 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
217 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
218 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
222 max_data_len = max_data_size / sizeof(TCHAR);
223 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
227 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
232 value_len = max_value_len + 1,
233 data_size = max_data_size,
234 RegEnumValue(key, index, value, &value_len,
235 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
237 data_len = data_size / sizeof(TCHAR);
238 data[data_len] = '\0';
240 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
242 name_len = data_len + 1;
244 if (!(name = malloc(name_len))) {
249 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
253 if (type == REG_SZ && !(list = list_append(list, name))) {
269 CFMutableDictionaryRef classes;
276 if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
281 if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
286 CFDictionarySetValue(classes,
287 CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
289 if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
294 if (!(path = malloc(PATH_MAX))) {
299 while ((port = IOIteratorNext(iter))) {
300 cf_path = IORegistryEntryCreateCFProperty(port,
301 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
303 result = CFStringGetCString(cf_path,
304 path, PATH_MAX, kCFStringEncodingASCII);
306 if (result && !(list = list_append(list, path))) {
308 IOObjectRelease(port);
312 IOObjectRelease(port);
317 IOObjectRelease(iter);
322 struct udev_enumerate *ud_enumerate;
323 struct udev_list_entry *ud_list;
324 struct udev_list_entry *ud_entry;
326 struct udev_device *ud_dev, *ud_parent;
329 int fd, ioctl_result;
330 struct serial_struct serial_info;
333 ud_enumerate = udev_enumerate_new(ud);
334 udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
335 udev_enumerate_scan_devices(ud_enumerate);
336 ud_list = udev_enumerate_get_list_entry(ud_enumerate);
337 udev_list_entry_foreach(ud_entry, ud_list) {
338 path = udev_list_entry_get_name(ud_entry);
339 ud_dev = udev_device_new_from_syspath(ud, path);
340 /* If there is no parent device, this is a virtual tty. */
341 ud_parent = udev_device_get_parent(ud_dev);
342 if (ud_parent == NULL) {
343 udev_device_unref(ud_dev);
346 name = udev_device_get_devnode(ud_dev);
347 /* The serial8250 driver has a hardcoded number of ports.
348 * The only way to tell which actually exist on a given system
349 * is to try to open them and make an ioctl call. */
350 driver = udev_device_get_driver(ud_parent);
351 if (driver && !strcmp(driver, "serial8250")) {
352 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
354 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
356 if (ioctl_result != 0)
358 if (serial_info.type == PORT_UNKNOWN)
361 list = list_append(list, name);
363 udev_device_unref(ud_dev);
370 udev_enumerate_unref(ud_enumerate);
378 sp_free_port_list(list);
385 void sp_free_port_list(struct sp_port **list)
389 for (i = 0; list[i]; i++)
390 sp_free_port(list[i]);
394 static enum sp_return validate_port(struct sp_port *port)
399 if (port->hdl == INVALID_HANDLE_VALUE)
408 #define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0)
410 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
416 DWORD desired_access = 0, flags_and_attributes = 0;
417 char *escaped_port_name;
419 /* Prefix port name with '\\.\' to work with ports above COM9. */
420 if (!(escaped_port_name = malloc(strlen(port->name + 5))))
422 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
424 /* Map 'flags' to the OS-specific settings. */
425 flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
426 if (flags & SP_MODE_READ)
427 desired_access |= GENERIC_READ;
428 if (flags & SP_MODE_WRITE)
429 desired_access |= GENERIC_WRITE;
430 if (flags & SP_MODE_NONBLOCK)
431 flags_and_attributes |= FILE_FLAG_OVERLAPPED;
433 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
434 OPEN_EXISTING, flags_and_attributes, 0);
436 free(escaped_port_name);
438 if (port->hdl == INVALID_HANDLE_VALUE)
442 struct port_data data;
443 struct sp_port_config config;
446 /* Map 'flags' to the OS-specific settings. */
447 if (flags & (SP_MODE_READ | SP_MODE_WRITE))
448 flags_local |= O_RDWR;
449 else if (flags & SP_MODE_READ)
450 flags_local |= O_RDONLY;
451 else if (flags & SP_MODE_WRITE)
452 flags_local |= O_WRONLY;
453 if (flags & SP_MODE_NONBLOCK)
454 flags_local |= O_NONBLOCK;
456 if ((port->fd = open(port->name, flags_local)) < 0)
459 ret = get_config(port, &data, &config);
466 /* Turn off all serial port cooking. */
467 data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
468 data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
469 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
470 data.term.c_oflag &= ~OFILL;
472 /* Disable canonical mode, and don't echo input characters. */
473 data.term.c_lflag &= ~(ICANON | ECHO);
475 /* Ignore modem status lines; enable receiver */
476 data.term.c_cflag |= (CLOCAL | CREAD);
478 ret = set_config(port, &data, &config);
489 enum sp_return sp_close(struct sp_port *port)
494 /* Returns non-zero upon success, 0 upon failure. */
495 if (CloseHandle(port->hdl) == 0)
497 port->hdl = INVALID_HANDLE_VALUE;
499 /* Returns 0 upon success, -1 upon failure. */
500 if (close(port->fd) == -1)
508 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
514 if (buffers & SP_BUF_INPUT)
515 flags |= PURGE_RXCLEAR;
516 if (buffers & SP_BUF_OUTPUT)
517 flags |= PURGE_TXCLEAR;
519 /* Returns non-zero upon success, 0 upon failure. */
520 if (PurgeComm(port->hdl, flags) == 0)
524 if (buffers & SP_BUF_BOTH)
526 else if (buffers & SP_BUF_INPUT)
528 if (buffers & SP_BUF_OUTPUT)
531 /* Returns 0 upon success, -1 upon failure. */
532 if (tcflush(port->fd, flags) < 0)
538 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
548 /* Returns non-zero upon success, 0 upon failure. */
549 if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
553 /* Returns the number of bytes written, or -1 upon failure. */
554 ssize_t written = write(port->fd, buf, count);
563 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
571 DWORD bytes_read = 0;
573 /* Returns non-zero upon success, 0 upon failure. */
574 if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
580 /* Returns the number of bytes read, or -1 upon failure. */
581 if ((bytes_read = read(port->fd, buf, count)) < 0)
588 static enum sp_return get_baudrate(int fd, int *baudrate)
592 if (!(data = malloc(get_termios_size())))
595 if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
598 *baudrate = get_termios_speed(data);
603 static enum sp_return set_baudrate(int fd, int baudrate)
607 if (!(data = malloc(get_termios_size())))
610 if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
613 set_termios_speed(data, baudrate);
615 if (ioctl(fd, get_termios_set_ioctl(), data) < 0)
622 static enum sp_return get_flow(int fd, int *flow)
626 if (!(data = malloc(get_termiox_size())))
629 if (ioctl(fd, TCGETX, data) < 0)
632 *flow = get_termiox_flow(data);
637 static enum sp_return set_flow(int fd, int flow)
641 if (!(data = malloc(get_termiox_size())))
644 if (ioctl(fd, TCGETX, data) < 0)
647 set_termiox_flow(data, flow);
649 if (ioctl(fd, TCSETX, data) < 0)
654 #endif /* USE_TERMIOX */
655 #endif /* __linux__ */
657 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
658 struct sp_port_config *config)
663 if (!GetCommState(port->hdl, &data->dcb))
666 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
667 if (data->dcb.BaudRate == std_baudrates[i].index) {
668 config->baudrate = std_baudrates[i].value;
673 if (i == NUM_STD_BAUDRATES)
674 /* BaudRate field can be either an index or a custom baud rate. */
675 config->baudrate = data->dcb.BaudRate;
677 config->bits = data->dcb.ByteSize;
679 if (data->dcb.fParity)
680 switch (data->dcb.Parity) {
682 config->parity = SP_PARITY_NONE;
685 config->parity = SP_PARITY_EVEN;
688 config->parity = SP_PARITY_ODD;
694 config->parity = SP_PARITY_NONE;
696 switch (data->dcb.StopBits) {
698 config->stopbits = 1;
701 config->stopbits = 2;
704 config->stopbits = -1;
707 switch (data->dcb.fRtsControl) {
708 case RTS_CONTROL_DISABLE:
709 config->rts = SP_RTS_OFF;
711 case RTS_CONTROL_ENABLE:
712 config->rts = SP_RTS_ON;
714 case RTS_CONTROL_HANDSHAKE:
715 config->rts = SP_RTS_FLOW_CONTROL;
721 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
723 switch (data->dcb.fDtrControl) {
724 case DTR_CONTROL_DISABLE:
725 config->dtr = SP_DTR_OFF;
727 case DTR_CONTROL_ENABLE:
728 config->dtr = SP_DTR_ON;
730 case DTR_CONTROL_HANDSHAKE:
731 config->dtr = SP_DTR_FLOW_CONTROL;
737 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
739 if (data->dcb.fInX) {
741 config->xon_xoff = SP_XONXOFF_INOUT;
743 config->xon_xoff = SP_XONXOFF_IN;
746 config->xon_xoff = SP_XONXOFF_OUT;
748 config->xon_xoff = SP_XONXOFF_DISABLED;
753 if (tcgetattr(port->fd, &data->term) < 0)
756 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
760 TRY(get_flow(port->fd, &data->flow));
763 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
764 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
765 config->baudrate = std_baudrates[i].value;
770 if (i == NUM_STD_BAUDRATES) {
772 config->baudrate = (int)data->term.c_ispeed;
773 #elif defined(__linux__)
774 TRY(get_baudrate(port->fd, &config->baudrate));
776 config->baudrate = -1;
780 switch (data->term.c_cflag & CSIZE) {
797 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
798 config->parity = SP_PARITY_NONE;
799 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
802 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
804 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
806 if (data->term.c_cflag & CRTSCTS) {
807 config->rts = SP_RTS_FLOW_CONTROL;
808 config->cts = SP_CTS_FLOW_CONTROL;
811 if (data->flow & RTS_FLOW)
812 config->rts = SP_RTS_FLOW_CONTROL;
814 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
816 config->cts = (data->flow & CTS_FLOW) ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
818 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
819 config->cts = SP_CTS_IGNORE;
824 if (data->flow & DTR_FLOW)
825 config->dtr = SP_DTR_FLOW_CONTROL;
827 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
829 config->dsr = (data->flow & DSR_FLOW) ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
831 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
832 config->dsr = SP_DSR_IGNORE;
835 if (data->term.c_iflag & IXOFF) {
836 if (data->term.c_iflag & IXON)
837 config->xon_xoff = SP_XONXOFF_INOUT;
839 config->xon_xoff = SP_XONXOFF_IN;
841 if (data->term.c_iflag & IXON)
842 config->xon_xoff = SP_XONXOFF_OUT;
844 config->xon_xoff = SP_XONXOFF_DISABLED;
851 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
852 const struct sp_port_config *config)
856 BAUD_TYPE baud_nonstd;
865 if (config->baudrate >= 0) {
866 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
867 if (config->baudrate == std_baudrates[i].value) {
868 data->dcb.BaudRate = std_baudrates[i].index;
873 if (i == NUM_STD_BAUDRATES)
874 data->dcb.BaudRate = config->baudrate;
877 if (config->bits >= 0)
878 data->dcb.ByteSize = config->bits;
880 if (config->parity >= 0) {
881 switch (config->parity) {
882 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
884 data->dcb.Parity = NOPARITY;
887 data->dcb.Parity = EVENPARITY;
890 data->dcb.Parity = ODDPARITY;
897 if (config->stopbits >= 0) {
898 switch (config->stopbits) {
899 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
901 data->dcb.StopBits = ONESTOPBIT;
904 data->dcb.StopBits = TWOSTOPBITS;
911 if (config->rts >= 0) {
912 switch (config->rts) {
914 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
917 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
919 case SP_RTS_FLOW_CONTROL:
920 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
927 if (config->cts >= 0) {
928 switch (config->cts) {
930 data->dcb.fOutxCtsFlow = FALSE;
932 case SP_CTS_FLOW_CONTROL:
933 data->dcb.fOutxCtsFlow = TRUE;
940 if (config->dtr >= 0) {
941 switch (config->dtr) {
943 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
946 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
948 case SP_DTR_FLOW_CONTROL:
949 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
956 if (config->dsr >= 0) {
957 switch (config->dsr) {
959 data->dcb.fOutxDsrFlow = FALSE;
961 case SP_DSR_FLOW_CONTROL:
962 data->dcb.fOutxDsrFlow = TRUE;
969 if (config->xon_xoff >= 0) {
970 switch (config->xon_xoff) {
971 case SP_XONXOFF_DISABLED:
972 data->dcb.fInX = FALSE;
973 data->dcb.fOutX = FALSE;
976 data->dcb.fInX = TRUE;
977 data->dcb.fOutX = FALSE;
980 data->dcb.fInX = FALSE;
981 data->dcb.fOutX = TRUE;
983 case SP_XONXOFF_INOUT:
984 data->dcb.fInX = TRUE;
985 data->dcb.fOutX = TRUE;
992 if (!SetCommState(port->hdl, &data->dcb))
999 if (config->baudrate >= 0) {
1000 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1001 if (config->baudrate == std_baudrates[i].value) {
1002 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
1005 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
1011 /* Non-standard baud rate */
1012 if (i == NUM_STD_BAUDRATES) {
1014 /* Set "dummy" baud rate */
1015 if (cfsetspeed(&data->term, B9600) < 0)
1017 baud_nonstd = config->baudrate;
1018 #elif defined(__linux__)
1026 if (config->bits >= 0) {
1027 data->term.c_cflag &= ~CSIZE;
1028 switch (config->bits) {
1030 data->term.c_cflag |= CS8;
1033 data->term.c_cflag |= CS7;
1036 data->term.c_cflag |= CS6;
1039 data->term.c_cflag |= CS5;
1046 if (config->parity >= 0) {
1047 data->term.c_iflag &= ~IGNPAR;
1048 data->term.c_cflag &= ~(PARENB | PARODD);
1049 switch (config->parity) {
1050 case SP_PARITY_NONE:
1051 data->term.c_iflag |= IGNPAR;
1053 case SP_PARITY_EVEN:
1054 data->term.c_cflag |= PARENB;
1057 data->term.c_cflag |= PARENB | PARODD;
1064 if (config->stopbits >= 0) {
1065 data->term.c_cflag &= ~CSTOPB;
1066 switch (config->stopbits) {
1068 data->term.c_cflag &= ~CSTOPB;
1071 data->term.c_cflag |= CSTOPB;
1078 if (config->rts >= 0 || config->cts >= 0) {
1080 data->flow &= ~(RTS_FLOW | CTS_FLOW);
1081 switch (config->rts) {
1084 controlbits = TIOCM_RTS;
1085 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
1089 case SP_RTS_FLOW_CONTROL:
1090 data->flow |= RTS_FLOW;
1095 if (config->cts == SP_CTS_FLOW_CONTROL)
1096 data->flow |= CTS_FLOW;
1098 if (data->flow & (RTS_FLOW | CTS_FLOW))
1099 data->term.c_iflag |= CRTSCTS;
1101 data->term.c_iflag &= ~CRTSCTS;
1103 /* Asymmetric use of RTS/CTS not supported. */
1104 if (data->term.c_iflag & CRTSCTS) {
1105 /* Flow control can only be disabled for both RTS & CTS together. */
1106 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
1107 if (config->cts != SP_CTS_IGNORE)
1110 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
1111 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
1115 /* Flow control can only be enabled for both RTS & CTS together. */
1116 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
1117 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
1121 if (config->rts >= 0) {
1122 if (config->rts == SP_RTS_FLOW_CONTROL) {
1123 data->term.c_iflag |= CRTSCTS;
1125 controlbits = TIOCM_RTS;
1126 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
1134 if (config->dtr >= 0 || config->dsr >= 0) {
1136 data->flow &= ~(DTR_FLOW | DSR_FLOW);
1137 switch (config->dtr) {
1140 controlbits = TIOCM_DTR;
1141 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
1145 case SP_DTR_FLOW_CONTROL:
1146 data->flow |= DTR_FLOW;
1151 if (config->dsr == SP_DSR_FLOW_CONTROL)
1152 data->flow |= DSR_FLOW;
1154 /* DTR/DSR flow control not supported. */
1155 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
1158 if (config->dtr >= 0) {
1159 controlbits = TIOCM_DTR;
1160 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
1167 if (config->xon_xoff >= 0) {
1168 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
1169 switch (config->xon_xoff) {
1170 case SP_XONXOFF_DISABLED:
1173 data->term.c_iflag |= IXOFF;
1175 case SP_XONXOFF_OUT:
1176 data->term.c_iflag |= IXON | IXANY;
1178 case SP_XONXOFF_INOUT:
1179 data->term.c_iflag |= IXON | IXOFF | IXANY;
1186 if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
1190 if (baud_nonstd != B0) {
1191 if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
1193 /* Set baud rates in data->term to correct, but incompatible
1194 * with tcsetattr() value, same as delivered by tcgetattr(). */
1195 if (cfsetspeed(&data->term, baud_nonstd) < 0)
1198 #elif defined(__linux__)
1200 TRY(set_baudrate(port->fd, config->baudrate));
1202 TRY(set_flow(port->fd, data->flow));
1206 #endif /* !_WIN32 */
1211 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
1213 struct port_data data;
1214 struct sp_port_config prev_config;
1221 TRY(get_config(port, &data, &prev_config));
1222 TRY(set_config(port, &data, config));
1227 #define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \
1228 struct port_data data; \
1229 struct sp_port_config config; \
1231 TRY(get_config(port, &data, &config)); \
1233 TRY(set_config(port, &data, &config)); \
1237 CREATE_SETTER(baudrate, int)
1238 CREATE_SETTER(bits, int)
1239 CREATE_SETTER(parity, enum sp_parity)
1240 CREATE_SETTER(stopbits, int)
1241 CREATE_SETTER(rts, enum sp_rts)
1242 CREATE_SETTER(cts, enum sp_cts)
1243 CREATE_SETTER(dtr, enum sp_dtr)
1244 CREATE_SETTER(dsr, enum sp_dsr)
1245 CREATE_SETTER(xon_xoff, enum sp_xonxoff)
1247 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
1249 struct port_data data;
1250 struct sp_port_config config;
1254 TRY(get_config(port, &data, &config));
1256 if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
1257 config.xon_xoff = SP_XONXOFF_INOUT;
1259 config.xon_xoff = SP_XONXOFF_DISABLED;
1261 if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
1262 config.rts = SP_RTS_FLOW_CONTROL;
1263 config.cts = SP_CTS_FLOW_CONTROL;
1265 if (config.rts == SP_RTS_FLOW_CONTROL)
1266 config.rts = SP_RTS_ON;
1267 config.cts = SP_CTS_IGNORE;
1270 if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
1271 config.dtr = SP_DTR_FLOW_CONTROL;
1272 config.dsr = SP_DSR_FLOW_CONTROL;
1274 if (config.dtr == SP_DTR_FLOW_CONTROL)
1275 config.dtr = SP_DTR_ON;
1276 config.dsr = SP_DSR_IGNORE;
1279 TRY(set_config(port, &data, &config));
1284 int sp_last_error_code(void)
1287 return GetLastError();
1293 char *sp_last_error_message(void)
1297 DWORD error = GetLastError();
1300 FORMAT_MESSAGE_ALLOCATE_BUFFER |
1301 FORMAT_MESSAGE_FROM_SYSTEM |
1302 FORMAT_MESSAGE_IGNORE_INSERTS,
1305 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1311 return strerror(errno);
1315 void sp_free_error_message(char *message)