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)
49 // FIXME: Temporarily disabled, breaks all lib functionality on some systems.
50 // #define USE_TERMIOX
54 #include "libserialport.h"
68 /* Standard baud rates. */
70 #define BAUD_TYPE DWORD
71 #define BAUD(n) {CBR_##n, n}
73 #define BAUD_TYPE speed_t
74 #define BAUD(n) {B##n, n}
82 const struct std_baudrate std_baudrates[] = {
85 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
86 * have documented CBR_* macros.
88 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
89 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
90 BAUD(115200), BAUD(128000), BAUD(256000),
92 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
93 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
94 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
96 #if !defined(__APPLE__) && !defined(__OpenBSD__)
102 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
103 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
105 #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
107 /* Helper functions. */
108 static enum sp_return validate_port(struct sp_port *port);
109 static struct sp_port **list_append(struct sp_port **list, const char *portname);
110 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
111 struct sp_port_config *config);
112 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
113 const struct sp_port_config *config);
115 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
117 struct sp_port *port;
128 if (!(port = malloc(sizeof(struct sp_port))))
131 len = strlen(portname) + 1;
133 if (!(port->name = malloc(len))) {
138 memcpy(port->name, portname, len);
141 port->hdl = INVALID_HANDLE_VALUE;
151 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
158 if (!port || !port->name)
161 return sp_get_port_by_name(port->name, copy_ptr);
164 void sp_free_port(struct sp_port *port)
175 static struct sp_port **list_append(struct sp_port **list, const char *portname)
180 for (count = 0; list[count]; count++);
181 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
184 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
186 list[count + 1] = NULL;
190 sp_free_port_list(list);
194 enum sp_return sp_list_ports(struct sp_port ***list_ptr)
196 struct sp_port **list;
199 if (!(list = malloc(sizeof(struct sp_port **))))
207 DWORD max_value_len, max_data_size, max_data_len;
208 DWORD value_len, data_size, data_len;
209 DWORD type, index = 0;
213 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
214 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
218 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
219 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
223 max_data_len = max_data_size / sizeof(TCHAR);
224 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
228 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
233 value_len = max_value_len + 1,
234 data_size = max_data_size,
235 RegEnumValue(key, index, value, &value_len,
236 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
238 data_len = data_size / sizeof(TCHAR);
239 data[data_len] = '\0';
241 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
243 name_len = data_len + 1;
245 if (!(name = malloc(name_len))) {
250 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
254 if (type == REG_SZ && !(list = list_append(list, name))) {
270 CFMutableDictionaryRef classes;
277 if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
282 if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
287 CFDictionarySetValue(classes,
288 CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
290 if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
295 if (!(path = malloc(PATH_MAX))) {
300 while ((port = IOIteratorNext(iter))) {
301 cf_path = IORegistryEntryCreateCFProperty(port,
302 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
304 result = CFStringGetCString(cf_path,
305 path, PATH_MAX, kCFStringEncodingASCII);
307 if (result && !(list = list_append(list, path))) {
309 IOObjectRelease(port);
313 IOObjectRelease(port);
318 IOObjectRelease(iter);
323 struct udev_enumerate *ud_enumerate;
324 struct udev_list_entry *ud_list;
325 struct udev_list_entry *ud_entry;
327 struct udev_device *ud_dev, *ud_parent;
330 int fd, ioctl_result;
331 struct serial_struct serial_info;
334 ud_enumerate = udev_enumerate_new(ud);
335 udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
336 udev_enumerate_scan_devices(ud_enumerate);
337 ud_list = udev_enumerate_get_list_entry(ud_enumerate);
338 udev_list_entry_foreach(ud_entry, ud_list) {
339 path = udev_list_entry_get_name(ud_entry);
340 ud_dev = udev_device_new_from_syspath(ud, path);
341 /* If there is no parent device, this is a virtual tty. */
342 ud_parent = udev_device_get_parent(ud_dev);
343 if (ud_parent == NULL) {
344 udev_device_unref(ud_dev);
347 name = udev_device_get_devnode(ud_dev);
348 /* The serial8250 driver has a hardcoded number of ports.
349 * The only way to tell which actually exist on a given system
350 * is to try to open them and make an ioctl call. */
351 driver = udev_device_get_driver(ud_parent);
352 if (driver && !strcmp(driver, "serial8250")) {
353 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
355 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
357 if (ioctl_result != 0)
359 if (serial_info.type == PORT_UNKNOWN)
362 list = list_append(list, name);
364 udev_device_unref(ud_dev);
371 udev_enumerate_unref(ud_enumerate);
379 sp_free_port_list(list);
386 void sp_free_port_list(struct sp_port **list)
390 for (i = 0; list[i]; i++)
391 sp_free_port(list[i]);
395 static enum sp_return validate_port(struct sp_port *port)
400 if (port->hdl == INVALID_HANDLE_VALUE)
409 #define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0)
411 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
417 DWORD desired_access = 0, flags_and_attributes = 0;
418 char *escaped_port_name;
420 /* Prefix port name with '\\.\' to work with ports above COM9. */
421 if (!(escaped_port_name = malloc(strlen(port->name + 5))))
423 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
425 /* Map 'flags' to the OS-specific settings. */
426 flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
427 if (flags & SP_MODE_READ)
428 desired_access |= GENERIC_READ;
429 if (flags & SP_MODE_WRITE)
430 desired_access |= GENERIC_WRITE;
431 if (flags & SP_MODE_NONBLOCK)
432 flags_and_attributes |= FILE_FLAG_OVERLAPPED;
434 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
435 OPEN_EXISTING, flags_and_attributes, 0);
437 free(escaped_port_name);
439 if (port->hdl == INVALID_HANDLE_VALUE)
443 struct port_data data;
444 struct sp_port_config config;
447 /* Map 'flags' to the OS-specific settings. */
448 if (flags & (SP_MODE_READ | SP_MODE_WRITE))
449 flags_local |= O_RDWR;
450 else if (flags & SP_MODE_READ)
451 flags_local |= O_RDONLY;
452 else if (flags & SP_MODE_WRITE)
453 flags_local |= O_WRONLY;
454 if (flags & SP_MODE_NONBLOCK)
455 flags_local |= O_NONBLOCK;
457 if ((port->fd = open(port->name, flags_local)) < 0)
460 ret = get_config(port, &data, &config);
467 /* Turn off all serial port cooking. */
468 data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
469 data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
470 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
471 data.term.c_oflag &= ~OFILL;
473 /* Disable canonical mode, and don't echo input characters. */
474 data.term.c_lflag &= ~(ICANON | ECHO);
476 /* Ignore modem status lines; enable receiver */
477 data.term.c_cflag |= (CLOCAL | CREAD);
479 ret = set_config(port, &data, &config);
490 enum sp_return sp_close(struct sp_port *port)
495 /* Returns non-zero upon success, 0 upon failure. */
496 if (CloseHandle(port->hdl) == 0)
498 port->hdl = INVALID_HANDLE_VALUE;
500 /* Returns 0 upon success, -1 upon failure. */
501 if (close(port->fd) == -1)
509 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
515 if (buffers & SP_BUF_INPUT)
516 flags |= PURGE_RXCLEAR;
517 if (buffers & SP_BUF_OUTPUT)
518 flags |= PURGE_TXCLEAR;
520 /* Returns non-zero upon success, 0 upon failure. */
521 if (PurgeComm(port->hdl, flags) == 0)
525 if (buffers & SP_BUF_BOTH)
527 else if (buffers & SP_BUF_INPUT)
529 else if (buffers & SP_BUF_OUTPUT)
532 /* Returns 0 upon success, -1 upon failure. */
533 if (tcflush(port->fd, flags) < 0)
539 enum sp_return sp_drain(struct sp_port *port)
544 /* Returns non-zero upon success, 0 upon failure. */
545 if (FlushFileBuffers(port->hdl) == 0)
548 /* Returns 0 upon success, -1 upon failure. */
549 if (tcdrain(port->fd) < 0)
556 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
566 /* Returns non-zero upon success, 0 upon failure. */
567 if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
571 /* Returns the number of bytes written, or -1 upon failure. */
572 ssize_t written = write(port->fd, buf, count);
581 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
589 DWORD bytes_read = 0;
591 /* Returns non-zero upon success, 0 upon failure. */
592 if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
598 /* Returns the number of bytes read, or -1 upon failure. */
599 if ((bytes_read = read(port->fd, buf, count)) < 0)
606 static enum sp_return get_baudrate(int fd, int *baudrate)
610 if (!(data = malloc(get_termios_size())))
613 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
618 *baudrate = get_termios_speed(data);
625 static enum sp_return set_baudrate(int fd, int baudrate)
629 if (!(data = malloc(get_termios_size())))
632 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
637 set_termios_speed(data, baudrate);
639 if (ioctl(fd, get_termios_set_ioctl(), data) < 0) {
650 static enum sp_return get_flow(int fd, int *flow)
654 if (!(data = malloc(get_termiox_size())))
657 if (ioctl(fd, TCGETX, data) < 0) {
662 *flow = get_termiox_flow(data);
669 static enum sp_return set_flow(int fd, int flow)
673 if (!(data = malloc(get_termiox_size())))
676 if (ioctl(fd, TCGETX, data) < 0) {
681 set_termiox_flow(data, flow);
683 if (ioctl(fd, TCSETX, data) < 0) {
692 #endif /* USE_TERMIOX */
693 #endif /* __linux__ */
695 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
696 struct sp_port_config *config)
701 if (!GetCommState(port->hdl, &data->dcb))
704 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
705 if (data->dcb.BaudRate == std_baudrates[i].index) {
706 config->baudrate = std_baudrates[i].value;
711 if (i == NUM_STD_BAUDRATES)
712 /* BaudRate field can be either an index or a custom baud rate. */
713 config->baudrate = data->dcb.BaudRate;
715 config->bits = data->dcb.ByteSize;
717 if (data->dcb.fParity)
718 switch (data->dcb.Parity) {
720 config->parity = SP_PARITY_NONE;
723 config->parity = SP_PARITY_EVEN;
726 config->parity = SP_PARITY_ODD;
732 config->parity = SP_PARITY_NONE;
734 switch (data->dcb.StopBits) {
736 config->stopbits = 1;
739 config->stopbits = 2;
742 config->stopbits = -1;
745 switch (data->dcb.fRtsControl) {
746 case RTS_CONTROL_DISABLE:
747 config->rts = SP_RTS_OFF;
749 case RTS_CONTROL_ENABLE:
750 config->rts = SP_RTS_ON;
752 case RTS_CONTROL_HANDSHAKE:
753 config->rts = SP_RTS_FLOW_CONTROL;
759 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
761 switch (data->dcb.fDtrControl) {
762 case DTR_CONTROL_DISABLE:
763 config->dtr = SP_DTR_OFF;
765 case DTR_CONTROL_ENABLE:
766 config->dtr = SP_DTR_ON;
768 case DTR_CONTROL_HANDSHAKE:
769 config->dtr = SP_DTR_FLOW_CONTROL;
775 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
777 if (data->dcb.fInX) {
779 config->xon_xoff = SP_XONXOFF_INOUT;
781 config->xon_xoff = SP_XONXOFF_IN;
784 config->xon_xoff = SP_XONXOFF_OUT;
786 config->xon_xoff = SP_XONXOFF_DISABLED;
791 if (tcgetattr(port->fd, &data->term) < 0)
794 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
798 TRY(get_flow(port->fd, &data->flow));
801 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
802 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
803 config->baudrate = std_baudrates[i].value;
808 if (i == NUM_STD_BAUDRATES) {
810 config->baudrate = (int)data->term.c_ispeed;
811 #elif defined(__linux__)
812 TRY(get_baudrate(port->fd, &config->baudrate));
814 config->baudrate = -1;
818 switch (data->term.c_cflag & CSIZE) {
835 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
836 config->parity = SP_PARITY_NONE;
837 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
840 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
842 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
844 if (data->term.c_cflag & CRTSCTS) {
845 config->rts = SP_RTS_FLOW_CONTROL;
846 config->cts = SP_CTS_FLOW_CONTROL;
849 if (data->flow & RTS_FLOW)
850 config->rts = SP_RTS_FLOW_CONTROL;
852 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
854 config->cts = (data->flow & CTS_FLOW) ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
856 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
857 config->cts = SP_CTS_IGNORE;
862 if (data->flow & DTR_FLOW)
863 config->dtr = SP_DTR_FLOW_CONTROL;
865 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
867 config->dsr = (data->flow & DSR_FLOW) ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
869 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
870 config->dsr = SP_DSR_IGNORE;
873 if (data->term.c_iflag & IXOFF) {
874 if (data->term.c_iflag & IXON)
875 config->xon_xoff = SP_XONXOFF_INOUT;
877 config->xon_xoff = SP_XONXOFF_IN;
879 if (data->term.c_iflag & IXON)
880 config->xon_xoff = SP_XONXOFF_OUT;
882 config->xon_xoff = SP_XONXOFF_DISABLED;
889 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
890 const struct sp_port_config *config)
894 BAUD_TYPE baud_nonstd;
903 if (config->baudrate >= 0) {
904 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
905 if (config->baudrate == std_baudrates[i].value) {
906 data->dcb.BaudRate = std_baudrates[i].index;
911 if (i == NUM_STD_BAUDRATES)
912 data->dcb.BaudRate = config->baudrate;
915 if (config->bits >= 0)
916 data->dcb.ByteSize = config->bits;
918 if (config->parity >= 0) {
919 switch (config->parity) {
920 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
922 data->dcb.Parity = NOPARITY;
925 data->dcb.Parity = EVENPARITY;
928 data->dcb.Parity = ODDPARITY;
935 if (config->stopbits >= 0) {
936 switch (config->stopbits) {
937 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
939 data->dcb.StopBits = ONESTOPBIT;
942 data->dcb.StopBits = TWOSTOPBITS;
949 if (config->rts >= 0) {
950 switch (config->rts) {
952 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
955 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
957 case SP_RTS_FLOW_CONTROL:
958 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
965 if (config->cts >= 0) {
966 switch (config->cts) {
968 data->dcb.fOutxCtsFlow = FALSE;
970 case SP_CTS_FLOW_CONTROL:
971 data->dcb.fOutxCtsFlow = TRUE;
978 if (config->dtr >= 0) {
979 switch (config->dtr) {
981 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
984 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
986 case SP_DTR_FLOW_CONTROL:
987 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
994 if (config->dsr >= 0) {
995 switch (config->dsr) {
997 data->dcb.fOutxDsrFlow = FALSE;
999 case SP_DSR_FLOW_CONTROL:
1000 data->dcb.fOutxDsrFlow = TRUE;
1007 if (config->xon_xoff >= 0) {
1008 switch (config->xon_xoff) {
1009 case SP_XONXOFF_DISABLED:
1010 data->dcb.fInX = FALSE;
1011 data->dcb.fOutX = FALSE;
1014 data->dcb.fInX = TRUE;
1015 data->dcb.fOutX = FALSE;
1017 case SP_XONXOFF_OUT:
1018 data->dcb.fInX = FALSE;
1019 data->dcb.fOutX = TRUE;
1021 case SP_XONXOFF_INOUT:
1022 data->dcb.fInX = TRUE;
1023 data->dcb.fOutX = TRUE;
1030 if (!SetCommState(port->hdl, &data->dcb))
1037 if (config->baudrate >= 0) {
1038 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1039 if (config->baudrate == std_baudrates[i].value) {
1040 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
1043 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
1049 /* Non-standard baud rate */
1050 if (i == NUM_STD_BAUDRATES) {
1052 /* Set "dummy" baud rate. */
1053 if (cfsetspeed(&data->term, B9600) < 0)
1055 baud_nonstd = config->baudrate;
1056 #elif defined(__linux__)
1064 if (config->bits >= 0) {
1065 data->term.c_cflag &= ~CSIZE;
1066 switch (config->bits) {
1068 data->term.c_cflag |= CS8;
1071 data->term.c_cflag |= CS7;
1074 data->term.c_cflag |= CS6;
1077 data->term.c_cflag |= CS5;
1084 if (config->parity >= 0) {
1085 data->term.c_iflag &= ~IGNPAR;
1086 data->term.c_cflag &= ~(PARENB | PARODD);
1087 switch (config->parity) {
1088 case SP_PARITY_NONE:
1089 data->term.c_iflag |= IGNPAR;
1091 case SP_PARITY_EVEN:
1092 data->term.c_cflag |= PARENB;
1095 data->term.c_cflag |= PARENB | PARODD;
1102 if (config->stopbits >= 0) {
1103 data->term.c_cflag &= ~CSTOPB;
1104 switch (config->stopbits) {
1106 data->term.c_cflag &= ~CSTOPB;
1109 data->term.c_cflag |= CSTOPB;
1116 if (config->rts >= 0 || config->cts >= 0) {
1118 data->flow &= ~(RTS_FLOW | CTS_FLOW);
1119 switch (config->rts) {
1122 controlbits = TIOCM_RTS;
1123 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1126 case SP_RTS_FLOW_CONTROL:
1127 data->flow |= RTS_FLOW;
1132 if (config->cts == SP_CTS_FLOW_CONTROL)
1133 data->flow |= CTS_FLOW;
1135 if (data->flow & (RTS_FLOW | CTS_FLOW))
1136 data->term.c_iflag |= CRTSCTS;
1138 data->term.c_iflag &= ~CRTSCTS;
1140 /* Asymmetric use of RTS/CTS not supported. */
1141 if (data->term.c_iflag & CRTSCTS) {
1142 /* Flow control can only be disabled for both RTS & CTS together. */
1143 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
1144 if (config->cts != SP_CTS_IGNORE)
1147 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
1148 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
1152 /* Flow control can only be enabled for both RTS & CTS together. */
1153 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
1154 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
1158 if (config->rts >= 0) {
1159 if (config->rts == SP_RTS_FLOW_CONTROL) {
1160 data->term.c_iflag |= CRTSCTS;
1162 controlbits = TIOCM_RTS;
1163 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
1171 if (config->dtr >= 0 || config->dsr >= 0) {
1173 data->flow &= ~(DTR_FLOW | DSR_FLOW);
1174 switch (config->dtr) {
1177 controlbits = TIOCM_DTR;
1178 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1181 case SP_DTR_FLOW_CONTROL:
1182 data->flow |= DTR_FLOW;
1187 if (config->dsr == SP_DSR_FLOW_CONTROL)
1188 data->flow |= DSR_FLOW;
1190 /* DTR/DSR flow control not supported. */
1191 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
1194 if (config->dtr >= 0) {
1195 controlbits = TIOCM_DTR;
1196 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
1203 if (config->xon_xoff >= 0) {
1204 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
1205 switch (config->xon_xoff) {
1206 case SP_XONXOFF_DISABLED:
1209 data->term.c_iflag |= IXOFF;
1211 case SP_XONXOFF_OUT:
1212 data->term.c_iflag |= IXON | IXANY;
1214 case SP_XONXOFF_INOUT:
1215 data->term.c_iflag |= IXON | IXOFF | IXANY;
1222 if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
1226 if (baud_nonstd != B0) {
1227 if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
1229 /* Set baud rates in data->term to correct, but incompatible
1230 * with tcsetattr() value, same as delivered by tcgetattr(). */
1231 if (cfsetspeed(&data->term, baud_nonstd) < 0)
1234 #elif defined(__linux__)
1236 TRY(set_baudrate(port->fd, config->baudrate));
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)