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"
50 #include "libserialport.h"
61 /* Standard baud rates. */
63 #define BAUD_TYPE DWORD
64 #define BAUD(n) {CBR_##n, n}
66 #define BAUD_TYPE speed_t
67 #define BAUD(n) {B##n, n}
75 const struct std_baudrate std_baudrates[] = {
78 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
79 * have documented CBR_* macros.
81 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
82 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
83 BAUD(115200), BAUD(128000), BAUD(256000),
85 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
86 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
87 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
89 #if !defined(__APPLE__) && !defined(__OpenBSD__)
95 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
96 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
98 #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
100 /* Helper functions. */
101 static enum sp_return validate_port(struct sp_port *port);
102 static struct sp_port **list_append(struct sp_port **list, const char *portname);
103 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
104 struct sp_port_config *config);
105 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
106 const struct sp_port_config *config);
108 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
110 struct sp_port *port;
121 if (!(port = malloc(sizeof(struct sp_port))))
124 len = strlen(portname) + 1;
126 if (!(port->name = malloc(len))) {
131 memcpy(port->name, portname, len);
134 port->hdl = INVALID_HANDLE_VALUE;
144 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
151 if (!port || !port->name)
154 return sp_get_port_by_name(port->name, copy_ptr);
157 void sp_free_port(struct sp_port *port)
168 static struct sp_port **list_append(struct sp_port **list, const char *portname)
173 for (count = 0; list[count]; count++);
174 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
177 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
179 list[count + 1] = NULL;
183 sp_free_port_list(list);
187 enum sp_return sp_list_ports(struct sp_port ***list_ptr)
189 struct sp_port **list;
192 if (!(list = malloc(sizeof(struct sp_port **))))
200 DWORD max_value_len, max_data_size, max_data_len;
201 DWORD value_len, data_size, data_len;
202 DWORD type, index = 0;
206 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
207 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
211 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
212 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
216 max_data_len = max_data_size / sizeof(TCHAR);
217 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
221 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
226 value_len = max_value_len + 1,
227 data_size = max_data_size,
228 RegEnumValue(key, index, value, &value_len,
229 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
231 data_len = data_size / sizeof(TCHAR);
232 data[data_len] = '\0';
234 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
236 name_len = data_len + 1;
238 if (!(name = malloc(name_len))) {
243 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
247 if (type == REG_SZ && !(list = list_append(list, name))) {
263 CFMutableDictionaryRef classes;
270 if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
275 if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
280 CFDictionarySetValue(classes,
281 CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
283 if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
288 if (!(path = malloc(PATH_MAX))) {
293 while ((port = IOIteratorNext(iter))) {
294 cf_path = IORegistryEntryCreateCFProperty(port,
295 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
297 result = CFStringGetCString(cf_path,
298 path, PATH_MAX, kCFStringEncodingASCII);
300 if (result && !(list = list_append(list, path))) {
302 IOObjectRelease(port);
306 IOObjectRelease(port);
311 IOObjectRelease(iter);
316 struct udev_enumerate *ud_enumerate;
317 struct udev_list_entry *ud_list;
318 struct udev_list_entry *ud_entry;
320 struct udev_device *ud_dev, *ud_parent;
323 int fd, ioctl_result;
324 struct serial_struct serial_info;
327 ud_enumerate = udev_enumerate_new(ud);
328 udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
329 udev_enumerate_scan_devices(ud_enumerate);
330 ud_list = udev_enumerate_get_list_entry(ud_enumerate);
331 udev_list_entry_foreach(ud_entry, ud_list) {
332 path = udev_list_entry_get_name(ud_entry);
333 ud_dev = udev_device_new_from_syspath(ud, path);
334 /* If there is no parent device, this is a virtual tty. */
335 ud_parent = udev_device_get_parent(ud_dev);
336 if (ud_parent == NULL) {
337 udev_device_unref(ud_dev);
340 name = udev_device_get_devnode(ud_dev);
341 /* The serial8250 driver has a hardcoded number of ports.
342 * The only way to tell which actually exist on a given system
343 * is to try to open them and make an ioctl call. */
344 driver = udev_device_get_driver(ud_parent);
345 if (driver && !strcmp(driver, "serial8250")) {
346 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
348 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
350 if (ioctl_result != 0)
352 if (serial_info.type == PORT_UNKNOWN)
355 list = list_append(list, name);
357 udev_device_unref(ud_dev);
364 udev_enumerate_unref(ud_enumerate);
372 sp_free_port_list(list);
379 void sp_free_port_list(struct sp_port **list)
383 for (i = 0; list[i]; i++)
384 sp_free_port(list[i]);
388 static enum sp_return validate_port(struct sp_port *port)
393 if (port->hdl == INVALID_HANDLE_VALUE)
402 #define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0)
404 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
410 DWORD desired_access = 0, flags_and_attributes = 0;
411 char *escaped_port_name;
413 /* Prefix port name with '\\.\' to work with ports above COM9. */
414 if (!(escaped_port_name = malloc(strlen(port->name + 5))))
416 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
418 /* Map 'flags' to the OS-specific settings. */
419 desired_access |= GENERIC_READ;
420 flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
421 if (flags & SP_MODE_RDWR)
422 desired_access |= GENERIC_WRITE;
423 if (flags & SP_MODE_NONBLOCK)
424 flags_and_attributes |= FILE_FLAG_OVERLAPPED;
426 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
427 OPEN_EXISTING, flags_and_attributes, 0);
429 free(escaped_port_name);
431 if (port->hdl == INVALID_HANDLE_VALUE)
435 struct port_data data;
436 struct sp_port_config config;
439 /* Map 'flags' to the OS-specific settings. */
440 if (flags & SP_MODE_RDWR)
441 flags_local |= O_RDWR;
442 if (flags & SP_MODE_RDONLY)
443 flags_local |= O_RDONLY;
444 if (flags & SP_MODE_NONBLOCK)
445 flags_local |= O_NONBLOCK;
447 if ((port->fd = open(port->name, flags_local)) < 0)
450 ret = get_config(port, &data, &config);
457 /* Turn off all serial port cooking. */
458 data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
459 data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
460 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
461 data.term.c_oflag &= ~OFILL;
463 /* Disable canonical mode, and don't echo input characters. */
464 data.term.c_lflag &= ~(ICANON | ECHO);
466 /* Ignore modem status lines; enable receiver */
467 data.term.c_cflag |= (CLOCAL | CREAD);
469 ret = set_config(port, &data, &config);
480 enum sp_return sp_close(struct sp_port *port)
485 /* Returns non-zero upon success, 0 upon failure. */
486 if (CloseHandle(port->hdl) == 0)
488 port->hdl = INVALID_HANDLE_VALUE;
490 /* Returns 0 upon success, -1 upon failure. */
491 if (close(port->fd) == -1)
499 enum sp_return sp_flush(struct sp_port *port)
504 /* Returns non-zero upon success, 0 upon failure. */
505 if (PurgeComm(port->hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0)
508 /* Returns 0 upon success, -1 upon failure. */
509 if (tcflush(port->fd, TCIOFLUSH) < 0)
515 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
525 /* Returns non-zero upon success, 0 upon failure. */
526 if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
530 /* Returns the number of bytes written, or -1 upon failure. */
531 ssize_t written = write(port->fd, buf, count);
540 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
548 DWORD bytes_read = 0;
550 /* Returns non-zero upon success, 0 upon failure. */
551 if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
557 /* Returns the number of bytes read, or -1 upon failure. */
558 if ((bytes_read = read(port->fd, buf, count)) < 0)
565 static enum sp_return get_baudrate(int fd, int *baudrate)
569 if (!(data = malloc(get_termios_size())))
572 if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
575 *baudrate = get_termios_speed(data);
580 static enum sp_return set_baudrate(int fd, int baudrate)
584 if (!(data = malloc(get_termios_size())))
587 if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
590 set_termios_speed(data, baudrate);
592 if (ioctl(fd, get_termios_set_ioctl(), data) < 0)
599 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
600 struct sp_port_config *config)
605 if (!GetCommState(port->hdl, &data->dcb))
608 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
609 if (data->dcb.BaudRate == std_baudrates[i].index) {
610 config->baudrate = std_baudrates[i].value;
615 if (i == NUM_STD_BAUDRATES)
616 /* BaudRate field can be either an index or a custom baud rate. */
617 config->baudrate = data->dcb.BaudRate;
619 config->bits = data->dcb.ByteSize;
621 if (data->dcb.fParity)
622 switch (data->dcb.Parity) {
624 config->parity = SP_PARITY_NONE;
627 config->parity = SP_PARITY_EVEN;
630 config->parity = SP_PARITY_ODD;
636 config->parity = SP_PARITY_NONE;
638 switch (data->dcb.StopBits) {
640 config->stopbits = 1;
643 config->stopbits = 2;
646 config->stopbits = -1;
649 switch (data->dcb.fRtsControl) {
650 case RTS_CONTROL_DISABLE:
651 config->rts = SP_RTS_OFF;
653 case RTS_CONTROL_ENABLE:
654 config->rts = SP_RTS_ON;
656 case RTS_CONTROL_HANDSHAKE:
657 config->rts = SP_RTS_FLOW_CONTROL;
663 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
665 switch (data->dcb.fDtrControl) {
666 case DTR_CONTROL_DISABLE:
667 config->dtr = SP_DTR_OFF;
669 case DTR_CONTROL_ENABLE:
670 config->dtr = SP_DTR_ON;
672 case DTR_CONTROL_HANDSHAKE:
673 config->dtr = SP_DTR_FLOW_CONTROL;
679 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
681 if (data->dcb.fInX) {
683 config->xon_xoff = SP_XONXOFF_INOUT;
685 config->xon_xoff = SP_XONXOFF_IN;
688 config->xon_xoff = SP_XONXOFF_OUT;
690 config->xon_xoff = SP_XONXOFF_DISABLED;
695 if (tcgetattr(port->fd, &data->term) < 0)
698 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
700 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
701 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
702 config->baudrate = std_baudrates[i].value;
707 if (i == NUM_STD_BAUDRATES) {
709 config->baudrate = (int)data->term.c_ispeed;
710 #elif defined(__linux__)
711 TRY(get_baudrate(port->fd, &config->baudrate));
713 config->baudrate = -1;
717 switch (data->term.c_cflag & CSIZE) {
734 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
735 config->parity = SP_PARITY_NONE;
736 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
739 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
741 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
743 if (data->term.c_cflag & CRTSCTS) {
744 config->rts = SP_RTS_FLOW_CONTROL;
745 config->cts = SP_CTS_FLOW_CONTROL;
747 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
748 config->cts = SP_CTS_IGNORE;
751 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
752 config->dsr = SP_DSR_IGNORE;
754 if (data->term.c_iflag & IXOFF) {
755 if (data->term.c_iflag & IXON)
756 config->xon_xoff = SP_XONXOFF_INOUT;
758 config->xon_xoff = SP_XONXOFF_IN;
760 if (data->term.c_iflag & IXON)
761 config->xon_xoff = SP_XONXOFF_OUT;
763 config->xon_xoff = SP_XONXOFF_DISABLED;
770 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
771 const struct sp_port_config *config)
775 BAUD_TYPE baud_nonstd;
784 if (config->baudrate >= 0) {
785 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
786 if (config->baudrate == std_baudrates[i].value) {
787 data->dcb.BaudRate = std_baudrates[i].index;
792 if (i == NUM_STD_BAUDRATES)
793 data->dcb.BaudRate = config->baudrate;
796 if (config->bits >= 0)
797 data->dcb.ByteSize = config->bits;
799 if (config->parity >= 0) {
800 switch (config->parity) {
801 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
803 data->dcb.Parity = NOPARITY;
806 data->dcb.Parity = EVENPARITY;
809 data->dcb.Parity = ODDPARITY;
816 if (config->stopbits >= 0) {
817 switch (config->stopbits) {
818 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
820 data->dcb.StopBits = ONESTOPBIT;
823 data->dcb.StopBits = TWOSTOPBITS;
830 if (config->rts >= 0) {
831 switch (config->rts) {
833 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
836 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
838 case SP_RTS_FLOW_CONTROL:
839 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
846 if (config->cts >= 0) {
847 switch (config->cts) {
849 data->dcb.fOutxCtsFlow = FALSE;
851 case SP_CTS_FLOW_CONTROL:
852 data->dcb.fOutxCtsFlow = TRUE;
859 if (config->dtr >= 0) {
860 switch (config->dtr) {
862 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
865 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
867 case SP_DTR_FLOW_CONTROL:
868 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
875 if (config->dsr >= 0) {
876 switch (config->dsr) {
878 data->dcb.fOutxDsrFlow = FALSE;
880 case SP_DSR_FLOW_CONTROL:
881 data->dcb.fOutxDsrFlow = TRUE;
888 if (config->xon_xoff >= 0) {
889 switch (config->xon_xoff) {
890 case SP_XONXOFF_DISABLED:
891 data->dcb.fInX = FALSE;
892 data->dcb.fOutX = FALSE;
895 data->dcb.fInX = TRUE;
896 data->dcb.fOutX = FALSE;
899 data->dcb.fInX = FALSE;
900 data->dcb.fOutX = TRUE;
902 case SP_XONXOFF_INOUT:
903 data->dcb.fInX = TRUE;
904 data->dcb.fOutX = TRUE;
911 if (!SetCommState(port->hdl, &data->dcb))
918 if (config->baudrate >= 0) {
919 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
920 if (config->baudrate == std_baudrates[i].value) {
921 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
924 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
930 /* Non-standard baud rate */
931 if (i == NUM_STD_BAUDRATES) {
933 /* Set "dummy" baud rate */
934 if (cfsetspeed(&data->term, B9600) < 0)
936 baud_nonstd = config->baudrate;
937 #elif defined(__linux__)
945 if (config->bits >= 0) {
946 data->term.c_cflag &= ~CSIZE;
947 switch (config->bits) {
949 data->term.c_cflag |= CS8;
952 data->term.c_cflag |= CS7;
955 data->term.c_cflag |= CS6;
958 data->term.c_cflag |= CS5;
965 if (config->parity >= 0) {
966 data->term.c_iflag &= ~IGNPAR;
967 data->term.c_cflag &= ~(PARENB | PARODD);
968 switch (config->parity) {
970 data->term.c_iflag |= IGNPAR;
973 data->term.c_cflag |= PARENB;
976 data->term.c_cflag |= PARENB | PARODD;
983 if (config->stopbits >= 0) {
984 data->term.c_cflag &= ~CSTOPB;
985 switch (config->stopbits) {
987 data->term.c_cflag &= ~CSTOPB;
990 data->term.c_cflag |= CSTOPB;
997 if (config->rts >= 0 || config->cts >= 0) {
998 /* Asymmetric use of RTS/CTS not supported yet. */
1000 if (data->term.c_iflag & CRTSCTS) {
1001 /* Flow control can only be disabled for both RTS & CTS together. */
1002 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
1003 if (config->cts != SP_CTS_IGNORE)
1006 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
1007 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
1011 /* Flow control can only be enabled for both RTS & CTS together. */
1012 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
1013 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
1017 if (config->rts >= 0) {
1018 if (config->rts == SP_RTS_FLOW_CONTROL) {
1019 data->term.c_iflag |= CRTSCTS;
1021 controlbits = TIOCM_RTS;
1022 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
1029 if (config->dtr >= 0 || config->dsr >= 0) {
1030 /* DTR/DSR flow control not supported yet. */
1031 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
1034 if (config->dtr >= 0) {
1035 controlbits = TIOCM_DTR;
1036 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
1042 if (config->xon_xoff >= 0) {
1043 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
1044 switch (config->xon_xoff) {
1045 case SP_XONXOFF_DISABLED:
1048 data->term.c_iflag |= IXOFF;
1050 case SP_XONXOFF_OUT:
1051 data->term.c_iflag |= IXON | IXANY;
1053 case SP_XONXOFF_INOUT:
1054 data->term.c_iflag |= IXON | IXOFF | IXANY;
1061 if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
1065 if (baud_nonstd != B0) {
1066 if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
1068 /* Set baud rates in data->term to correct, but incompatible
1069 * with tcsetattr() value, same as delivered by tcgetattr(). */
1070 if (cfsetspeed(&data->term, baud_nonstd) < 0)
1073 #elif defined(__linux__)
1075 TRY(set_baudrate(port->fd, config->baudrate));
1078 #endif /* !_WIN32 */
1083 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
1085 struct port_data data;
1086 struct sp_port_config prev_config;
1093 TRY(get_config(port, &data, &prev_config));
1094 TRY(set_config(port, &data, config));
1099 #define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \
1100 struct port_data data; \
1101 struct sp_port_config config; \
1103 TRY(get_config(port, &data, &config)); \
1105 TRY(set_config(port, &data, &config)); \
1109 CREATE_SETTER(baudrate, int)
1110 CREATE_SETTER(bits, int)
1111 CREATE_SETTER(parity, enum sp_parity)
1112 CREATE_SETTER(stopbits, int)
1113 CREATE_SETTER(rts, enum sp_rts)
1114 CREATE_SETTER(cts, enum sp_cts)
1115 CREATE_SETTER(dtr, enum sp_dtr)
1116 CREATE_SETTER(dsr, enum sp_dsr)
1117 CREATE_SETTER(xon_xoff, enum sp_xonxoff)
1119 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
1121 struct port_data data;
1122 struct sp_port_config config;
1126 TRY(get_config(port, &data, &config));
1128 if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
1129 config.xon_xoff = SP_XONXOFF_INOUT;
1131 config.xon_xoff = SP_XONXOFF_DISABLED;
1133 if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
1134 config.rts = SP_RTS_FLOW_CONTROL;
1135 config.cts = SP_CTS_FLOW_CONTROL;
1137 if (config.rts == SP_RTS_FLOW_CONTROL)
1138 config.rts = SP_RTS_ON;
1139 config.cts = SP_CTS_IGNORE;
1142 if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
1143 config.dtr = SP_DTR_FLOW_CONTROL;
1144 config.dsr = SP_DSR_FLOW_CONTROL;
1146 if (config.dtr == SP_DTR_FLOW_CONTROL)
1147 config.dtr = SP_DTR_ON;
1148 config.dsr = SP_DSR_IGNORE;
1151 TRY(set_config(port, &data, &config));
1156 int sp_last_error_code(void)
1159 return GetLastError();
1165 char *sp_last_error_message(void)
1169 DWORD error = GetLastError();
1172 FORMAT_MESSAGE_ALLOCATE_BUFFER |
1173 FORMAT_MESSAGE_FROM_SYSTEM |
1174 FORMAT_MESSAGE_IGNORE_INSERTS,
1177 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1183 return strerror(errno);
1187 void sp_free_error_message(char *message)