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"
61 int termiox_supported;
66 /* Standard baud rates. */
68 #define BAUD_TYPE DWORD
69 #define BAUD(n) {CBR_##n, n}
71 #define BAUD_TYPE speed_t
72 #define BAUD(n) {B##n, n}
80 const struct std_baudrate std_baudrates[] = {
83 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
84 * have documented CBR_* macros.
86 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
87 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
88 BAUD(115200), BAUD(128000), BAUD(256000),
90 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
91 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
92 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
94 #if !defined(__APPLE__) && !defined(__OpenBSD__)
100 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
101 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
103 #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
105 /* Helper functions. */
106 static enum sp_return validate_port(struct sp_port *port);
107 static struct sp_port **list_append(struct sp_port **list, const char *portname);
108 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
109 struct sp_port_config *config);
110 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
111 const struct sp_port_config *config);
113 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
115 struct sp_port *port;
126 if (!(port = malloc(sizeof(struct sp_port))))
129 len = strlen(portname) + 1;
131 if (!(port->name = malloc(len))) {
136 memcpy(port->name, portname, len);
139 port->hdl = INVALID_HANDLE_VALUE;
149 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
156 if (!port || !port->name)
159 return sp_get_port_by_name(port->name, copy_ptr);
162 void sp_free_port(struct sp_port *port)
173 static struct sp_port **list_append(struct sp_port **list, const char *portname)
178 for (count = 0; list[count]; count++);
179 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
182 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
184 list[count + 1] = NULL;
188 sp_free_port_list(list);
192 enum sp_return sp_list_ports(struct sp_port ***list_ptr)
194 struct sp_port **list;
197 if (!(list = malloc(sizeof(struct sp_port **))))
205 DWORD max_value_len, max_data_size, max_data_len;
206 DWORD value_len, data_size, data_len;
207 DWORD type, index = 0;
211 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
212 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
216 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
217 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
221 max_data_len = max_data_size / sizeof(TCHAR);
222 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
226 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
231 value_len = max_value_len + 1,
232 data_size = max_data_size,
233 RegEnumValue(key, index, value, &value_len,
234 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
236 data_len = data_size / sizeof(TCHAR);
237 data[data_len] = '\0';
239 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
241 name_len = data_len + 1;
243 if (!(name = malloc(name_len))) {
248 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
252 if (type == REG_SZ && !(list = list_append(list, name))) {
268 CFMutableDictionaryRef classes;
275 if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
280 if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
285 CFDictionarySetValue(classes,
286 CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
288 if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
293 if (!(path = malloc(PATH_MAX))) {
298 while ((port = IOIteratorNext(iter))) {
299 cf_path = IORegistryEntryCreateCFProperty(port,
300 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
302 result = CFStringGetCString(cf_path,
303 path, PATH_MAX, kCFStringEncodingASCII);
305 if (result && !(list = list_append(list, path))) {
307 IOObjectRelease(port);
311 IOObjectRelease(port);
316 IOObjectRelease(iter);
321 struct udev_enumerate *ud_enumerate;
322 struct udev_list_entry *ud_list;
323 struct udev_list_entry *ud_entry;
325 struct udev_device *ud_dev, *ud_parent;
328 int fd, ioctl_result;
329 struct serial_struct serial_info;
332 ud_enumerate = udev_enumerate_new(ud);
333 udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
334 udev_enumerate_scan_devices(ud_enumerate);
335 ud_list = udev_enumerate_get_list_entry(ud_enumerate);
336 udev_list_entry_foreach(ud_entry, ud_list) {
337 path = udev_list_entry_get_name(ud_entry);
338 ud_dev = udev_device_new_from_syspath(ud, path);
339 /* If there is no parent device, this is a virtual tty. */
340 ud_parent = udev_device_get_parent(ud_dev);
341 if (ud_parent == NULL) {
342 udev_device_unref(ud_dev);
345 name = udev_device_get_devnode(ud_dev);
346 /* The serial8250 driver has a hardcoded number of ports.
347 * The only way to tell which actually exist on a given system
348 * is to try to open them and make an ioctl call. */
349 driver = udev_device_get_driver(ud_parent);
350 if (driver && !strcmp(driver, "serial8250")) {
351 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
353 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
355 if (ioctl_result != 0)
357 if (serial_info.type == PORT_UNKNOWN)
360 list = list_append(list, name);
362 udev_device_unref(ud_dev);
369 udev_enumerate_unref(ud_enumerate);
377 sp_free_port_list(list);
384 void sp_free_port_list(struct sp_port **list)
388 for (i = 0; list[i]; i++)
389 sp_free_port(list[i]);
393 static enum sp_return validate_port(struct sp_port *port)
398 if (port->hdl == INVALID_HANDLE_VALUE)
407 #define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0)
409 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
415 DWORD desired_access = 0, flags_and_attributes = 0;
416 char *escaped_port_name;
418 /* Prefix port name with '\\.\' to work with ports above COM9. */
419 if (!(escaped_port_name = malloc(strlen(port->name + 5))))
421 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
423 /* Map 'flags' to the OS-specific settings. */
424 flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
425 if (flags & SP_MODE_READ)
426 desired_access |= GENERIC_READ;
427 if (flags & SP_MODE_WRITE)
428 desired_access |= GENERIC_WRITE;
429 if (flags & SP_MODE_NONBLOCK)
430 flags_and_attributes |= FILE_FLAG_OVERLAPPED;
432 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
433 OPEN_EXISTING, flags_and_attributes, 0);
435 free(escaped_port_name);
437 if (port->hdl == INVALID_HANDLE_VALUE)
441 struct port_data data;
442 struct sp_port_config config;
445 /* Map 'flags' to the OS-specific settings. */
446 if (flags & (SP_MODE_READ | SP_MODE_WRITE))
447 flags_local |= O_RDWR;
448 else if (flags & SP_MODE_READ)
449 flags_local |= O_RDONLY;
450 else if (flags & SP_MODE_WRITE)
451 flags_local |= O_WRONLY;
452 if (flags & SP_MODE_NONBLOCK)
453 flags_local |= O_NONBLOCK;
455 if ((port->fd = open(port->name, flags_local)) < 0)
458 ret = get_config(port, &data, &config);
465 /* Turn off all serial port cooking. */
466 data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
467 data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
468 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
469 data.term.c_oflag &= ~OFILL;
471 /* Disable canonical mode, and don't echo input characters. */
472 data.term.c_lflag &= ~(ICANON | ECHO);
474 /* Ignore modem status lines; enable receiver */
475 data.term.c_cflag |= (CLOCAL | CREAD);
477 ret = set_config(port, &data, &config);
488 enum sp_return sp_close(struct sp_port *port)
493 /* Returns non-zero upon success, 0 upon failure. */
494 if (CloseHandle(port->hdl) == 0)
496 port->hdl = INVALID_HANDLE_VALUE;
498 /* Returns 0 upon success, -1 upon failure. */
499 if (close(port->fd) == -1)
507 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
513 if (buffers & SP_BUF_INPUT)
514 flags |= PURGE_RXCLEAR;
515 if (buffers & SP_BUF_OUTPUT)
516 flags |= PURGE_TXCLEAR;
518 /* Returns non-zero upon success, 0 upon failure. */
519 if (PurgeComm(port->hdl, flags) == 0)
523 if (buffers & SP_BUF_BOTH)
525 else if (buffers & SP_BUF_INPUT)
527 else if (buffers & SP_BUF_OUTPUT)
530 /* Returns 0 upon success, -1 upon failure. */
531 if (tcflush(port->fd, flags) < 0)
537 enum sp_return sp_drain(struct sp_port *port)
542 /* Returns non-zero upon success, 0 upon failure. */
543 if (FlushFileBuffers(port->hdl) == 0)
546 /* Returns 0 upon success, -1 upon failure. */
547 if (tcdrain(port->fd) < 0)
554 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
564 /* Returns non-zero upon success, 0 upon failure. */
565 if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
569 /* Returns the number of bytes written, or -1 upon failure. */
570 ssize_t written = write(port->fd, buf, count);
579 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
587 DWORD bytes_read = 0;
589 /* Returns non-zero upon success, 0 upon failure. */
590 if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
596 /* Returns the number of bytes read, or -1 upon failure. */
597 if ((bytes_read = read(port->fd, buf, count)) < 0)
604 static enum sp_return get_baudrate(int fd, int *baudrate)
608 if (!(data = malloc(get_termios_size())))
611 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
616 *baudrate = get_termios_speed(data);
623 static enum sp_return set_baudrate(int fd, int baudrate)
627 if (!(data = malloc(get_termios_size())))
630 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
635 set_termios_speed(data, baudrate);
637 if (ioctl(fd, get_termios_set_ioctl(), data) < 0) {
648 static enum sp_return get_flow(int fd, int *flow)
652 if (!(data = malloc(get_termiox_size())))
655 if (ioctl(fd, TCGETX, data) < 0) {
660 *flow = get_termiox_flow(data);
667 static enum sp_return set_flow(int fd, int flow)
671 if (!(data = malloc(get_termiox_size())))
674 if (ioctl(fd, TCGETX, data) < 0) {
679 set_termiox_flow(data, flow);
681 if (ioctl(fd, TCSETX, data) < 0) {
690 #endif /* USE_TERMIOX */
691 #endif /* __linux__ */
693 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
694 struct sp_port_config *config)
699 if (!GetCommState(port->hdl, &data->dcb))
702 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
703 if (data->dcb.BaudRate == std_baudrates[i].index) {
704 config->baudrate = std_baudrates[i].value;
709 if (i == NUM_STD_BAUDRATES)
710 /* BaudRate field can be either an index or a custom baud rate. */
711 config->baudrate = data->dcb.BaudRate;
713 config->bits = data->dcb.ByteSize;
715 if (data->dcb.fParity)
716 switch (data->dcb.Parity) {
718 config->parity = SP_PARITY_NONE;
721 config->parity = SP_PARITY_EVEN;
724 config->parity = SP_PARITY_ODD;
730 config->parity = SP_PARITY_NONE;
732 switch (data->dcb.StopBits) {
734 config->stopbits = 1;
737 config->stopbits = 2;
740 config->stopbits = -1;
743 switch (data->dcb.fRtsControl) {
744 case RTS_CONTROL_DISABLE:
745 config->rts = SP_RTS_OFF;
747 case RTS_CONTROL_ENABLE:
748 config->rts = SP_RTS_ON;
750 case RTS_CONTROL_HANDSHAKE:
751 config->rts = SP_RTS_FLOW_CONTROL;
757 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
759 switch (data->dcb.fDtrControl) {
760 case DTR_CONTROL_DISABLE:
761 config->dtr = SP_DTR_OFF;
763 case DTR_CONTROL_ENABLE:
764 config->dtr = SP_DTR_ON;
766 case DTR_CONTROL_HANDSHAKE:
767 config->dtr = SP_DTR_FLOW_CONTROL;
773 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
775 if (data->dcb.fInX) {
777 config->xon_xoff = SP_XONXOFF_INOUT;
779 config->xon_xoff = SP_XONXOFF_IN;
782 config->xon_xoff = SP_XONXOFF_OUT;
784 config->xon_xoff = SP_XONXOFF_DISABLED;
789 if (tcgetattr(port->fd, &data->term) < 0)
792 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
796 int ret = get_flow(port->fd, &data->flow);
798 if (ret == SP_ERR_FAIL && errno == EINVAL)
799 data->termiox_supported = 0;
803 data->termiox_supported = 1;
805 data->termiox_supported = 0;
808 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
809 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
810 config->baudrate = std_baudrates[i].value;
815 if (i == NUM_STD_BAUDRATES) {
817 config->baudrate = (int)data->term.c_ispeed;
818 #elif defined(__linux__)
819 TRY(get_baudrate(port->fd, &config->baudrate));
821 config->baudrate = -1;
825 switch (data->term.c_cflag & CSIZE) {
842 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
843 config->parity = SP_PARITY_NONE;
844 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
847 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
849 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
851 if (data->term.c_cflag & CRTSCTS) {
852 config->rts = SP_RTS_FLOW_CONTROL;
853 config->cts = SP_CTS_FLOW_CONTROL;
855 if (data->termiox_supported && data->flow & RTS_FLOW)
856 config->rts = SP_RTS_FLOW_CONTROL;
858 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
860 config->cts = (data->termiox_supported && data->flow & CTS_FLOW) ?
861 SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
864 if (data->termiox_supported && data->flow & DTR_FLOW)
865 config->dtr = SP_DTR_FLOW_CONTROL;
867 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
869 config->dsr = (data->termiox_supported && data->flow & DSR_FLOW) ?
870 SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
872 if (data->term.c_iflag & IXOFF) {
873 if (data->term.c_iflag & IXON)
874 config->xon_xoff = SP_XONXOFF_INOUT;
876 config->xon_xoff = SP_XONXOFF_IN;
878 if (data->term.c_iflag & IXON)
879 config->xon_xoff = SP_XONXOFF_OUT;
881 config->xon_xoff = SP_XONXOFF_DISABLED;
888 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
889 const struct sp_port_config *config)
893 BAUD_TYPE baud_nonstd;
902 if (config->baudrate >= 0) {
903 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
904 if (config->baudrate == std_baudrates[i].value) {
905 data->dcb.BaudRate = std_baudrates[i].index;
910 if (i == NUM_STD_BAUDRATES)
911 data->dcb.BaudRate = config->baudrate;
914 if (config->bits >= 0)
915 data->dcb.ByteSize = config->bits;
917 if (config->parity >= 0) {
918 switch (config->parity) {
919 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
921 data->dcb.Parity = NOPARITY;
924 data->dcb.Parity = EVENPARITY;
927 data->dcb.Parity = ODDPARITY;
934 if (config->stopbits >= 0) {
935 switch (config->stopbits) {
936 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
938 data->dcb.StopBits = ONESTOPBIT;
941 data->dcb.StopBits = TWOSTOPBITS;
948 if (config->rts >= 0) {
949 switch (config->rts) {
951 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
954 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
956 case SP_RTS_FLOW_CONTROL:
957 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
964 if (config->cts >= 0) {
965 switch (config->cts) {
967 data->dcb.fOutxCtsFlow = FALSE;
969 case SP_CTS_FLOW_CONTROL:
970 data->dcb.fOutxCtsFlow = TRUE;
977 if (config->dtr >= 0) {
978 switch (config->dtr) {
980 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
983 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
985 case SP_DTR_FLOW_CONTROL:
986 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
993 if (config->dsr >= 0) {
994 switch (config->dsr) {
996 data->dcb.fOutxDsrFlow = FALSE;
998 case SP_DSR_FLOW_CONTROL:
999 data->dcb.fOutxDsrFlow = TRUE;
1006 if (config->xon_xoff >= 0) {
1007 switch (config->xon_xoff) {
1008 case SP_XONXOFF_DISABLED:
1009 data->dcb.fInX = FALSE;
1010 data->dcb.fOutX = FALSE;
1013 data->dcb.fInX = TRUE;
1014 data->dcb.fOutX = FALSE;
1016 case SP_XONXOFF_OUT:
1017 data->dcb.fInX = FALSE;
1018 data->dcb.fOutX = TRUE;
1020 case SP_XONXOFF_INOUT:
1021 data->dcb.fInX = TRUE;
1022 data->dcb.fOutX = TRUE;
1029 if (!SetCommState(port->hdl, &data->dcb))
1036 if (config->baudrate >= 0) {
1037 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1038 if (config->baudrate == std_baudrates[i].value) {
1039 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
1042 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
1048 /* Non-standard baud rate */
1049 if (i == NUM_STD_BAUDRATES) {
1051 /* Set "dummy" baud rate. */
1052 if (cfsetspeed(&data->term, B9600) < 0)
1054 baud_nonstd = config->baudrate;
1055 #elif defined(__linux__)
1063 if (config->bits >= 0) {
1064 data->term.c_cflag &= ~CSIZE;
1065 switch (config->bits) {
1067 data->term.c_cflag |= CS8;
1070 data->term.c_cflag |= CS7;
1073 data->term.c_cflag |= CS6;
1076 data->term.c_cflag |= CS5;
1083 if (config->parity >= 0) {
1084 data->term.c_iflag &= ~IGNPAR;
1085 data->term.c_cflag &= ~(PARENB | PARODD);
1086 switch (config->parity) {
1087 case SP_PARITY_NONE:
1088 data->term.c_iflag |= IGNPAR;
1090 case SP_PARITY_EVEN:
1091 data->term.c_cflag |= PARENB;
1094 data->term.c_cflag |= PARENB | PARODD;
1101 if (config->stopbits >= 0) {
1102 data->term.c_cflag &= ~CSTOPB;
1103 switch (config->stopbits) {
1105 data->term.c_cflag &= ~CSTOPB;
1108 data->term.c_cflag |= CSTOPB;
1115 if (config->rts >= 0 || config->cts >= 0) {
1116 if (data->termiox_supported) {
1117 data->flow &= ~(RTS_FLOW | CTS_FLOW);
1118 switch (config->rts) {
1121 controlbits = TIOCM_RTS;
1122 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1125 case SP_RTS_FLOW_CONTROL:
1126 data->flow |= RTS_FLOW;
1131 if (config->cts == SP_CTS_FLOW_CONTROL)
1132 data->flow |= CTS_FLOW;
1134 if (data->flow & (RTS_FLOW | CTS_FLOW))
1135 data->term.c_iflag |= CRTSCTS;
1137 data->term.c_iflag &= ~CRTSCTS;
1139 /* Asymmetric use of RTS/CTS not supported. */
1140 if (data->term.c_iflag & CRTSCTS) {
1141 /* Flow control can only be disabled for both RTS & CTS together. */
1142 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
1143 if (config->cts != SP_CTS_IGNORE)
1146 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
1147 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
1151 /* Flow control can only be enabled for both RTS & CTS together. */
1152 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
1153 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
1157 if (config->rts >= 0) {
1158 if (config->rts == SP_RTS_FLOW_CONTROL) {
1159 data->term.c_iflag |= CRTSCTS;
1161 controlbits = TIOCM_RTS;
1162 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
1170 if (config->dtr >= 0 || config->dsr >= 0) {
1171 if (data->termiox_supported) {
1172 data->flow &= ~(DTR_FLOW | DSR_FLOW);
1173 switch (config->dtr) {
1176 controlbits = TIOCM_DTR;
1177 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1180 case SP_DTR_FLOW_CONTROL:
1181 data->flow |= DTR_FLOW;
1186 if (config->dsr == SP_DSR_FLOW_CONTROL)
1187 data->flow |= DSR_FLOW;
1189 /* DTR/DSR flow control not supported. */
1190 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
1193 if (config->dtr >= 0) {
1194 controlbits = TIOCM_DTR;
1195 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
1202 if (config->xon_xoff >= 0) {
1203 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
1204 switch (config->xon_xoff) {
1205 case SP_XONXOFF_DISABLED:
1208 data->term.c_iflag |= IXOFF;
1210 case SP_XONXOFF_OUT:
1211 data->term.c_iflag |= IXON | IXANY;
1213 case SP_XONXOFF_INOUT:
1214 data->term.c_iflag |= IXON | IXOFF | IXANY;
1221 if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
1225 if (baud_nonstd != B0) {
1226 if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
1228 /* Set baud rates in data->term to correct, but incompatible
1229 * with tcsetattr() value, same as delivered by tcgetattr(). */
1230 if (cfsetspeed(&data->term, baud_nonstd) < 0)
1233 #elif defined(__linux__)
1235 TRY(set_baudrate(port->fd, config->baudrate));
1237 if (data->termiox_supported)
1238 TRY(set_flow(port->fd, data->flow));
1242 #endif /* !_WIN32 */
1247 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
1249 struct port_data data;
1250 struct sp_port_config prev_config;
1257 TRY(get_config(port, &data, &prev_config));
1258 TRY(set_config(port, &data, config));
1263 #define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \
1264 struct port_data data; \
1265 struct sp_port_config config; \
1267 TRY(get_config(port, &data, &config)); \
1269 TRY(set_config(port, &data, &config)); \
1273 CREATE_SETTER(baudrate, int)
1274 CREATE_SETTER(bits, int)
1275 CREATE_SETTER(parity, enum sp_parity)
1276 CREATE_SETTER(stopbits, int)
1277 CREATE_SETTER(rts, enum sp_rts)
1278 CREATE_SETTER(cts, enum sp_cts)
1279 CREATE_SETTER(dtr, enum sp_dtr)
1280 CREATE_SETTER(dsr, enum sp_dsr)
1281 CREATE_SETTER(xon_xoff, enum sp_xonxoff)
1283 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
1285 struct port_data data;
1286 struct sp_port_config config;
1290 TRY(get_config(port, &data, &config));
1292 if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
1293 config.xon_xoff = SP_XONXOFF_INOUT;
1295 config.xon_xoff = SP_XONXOFF_DISABLED;
1297 if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
1298 config.rts = SP_RTS_FLOW_CONTROL;
1299 config.cts = SP_CTS_FLOW_CONTROL;
1301 if (config.rts == SP_RTS_FLOW_CONTROL)
1302 config.rts = SP_RTS_ON;
1303 config.cts = SP_CTS_IGNORE;
1306 if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
1307 config.dtr = SP_DTR_FLOW_CONTROL;
1308 config.dsr = SP_DSR_FLOW_CONTROL;
1310 if (config.dtr == SP_DTR_FLOW_CONTROL)
1311 config.dtr = SP_DTR_ON;
1312 config.dsr = SP_DSR_IGNORE;
1315 TRY(set_config(port, &data, &config));
1320 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals)
1330 if (GetCommModemStatus(port->hdl, &bits) == 0)
1332 if (bits & MS_CTS_ON)
1333 *signals |= SP_SIG_CTS;
1334 if (bits & MS_DSR_ON)
1335 *signals |= SP_SIG_DSR;
1336 if (bits & MS_RING_ON)
1337 *signals |= SP_SIG_DCD;
1338 if (bits & MS_RLSD_ON)
1339 *signals |= SP_SIG_RI;
1342 if (ioctl(port->fd, TIOCMGET, &bits) < 0)
1344 if (bits & TIOCM_CTS)
1345 *signals |= SP_SIG_CTS;
1346 if (bits & TIOCM_DSR)
1347 *signals |= SP_SIG_DSR;
1348 if (bits & TIOCM_CAR)
1349 *signals |= SP_SIG_DCD;
1350 if (bits & TIOCM_RNG)
1351 *signals |= SP_SIG_RI;
1356 enum sp_return sp_start_break(struct sp_port *port)
1360 if (SetCommBreak(port->hdl) == 0)
1363 if (ioctl(port->fd, TIOCSBRK, 1) < 0)
1370 enum sp_return sp_end_break(struct sp_port *port)
1374 if (ClearCommBreak(port->hdl) == 0)
1377 if (ioctl(port->fd, TIOCCBRK, 1) < 0)
1384 int sp_last_error_code(void)
1387 return GetLastError();
1393 char *sp_last_error_message(void)
1397 DWORD error = GetLastError();
1400 FORMAT_MESSAGE_ALLOCATE_BUFFER |
1401 FORMAT_MESSAGE_FROM_SYSTEM |
1402 FORMAT_MESSAGE_IGNORE_INSERTS,
1405 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1411 return strerror(errno);
1415 void sp_free_error_message(char *message)