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"
49 #include "libserialport.h"
60 /* Standard baud rates. */
62 #define BAUD_TYPE DWORD
63 #define BAUD(n) {CBR_##n, n}
65 #define BAUD_TYPE speed_t
66 #define BAUD(n) {B##n, n}
74 const struct std_baudrate std_baudrates[] = {
77 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
78 * have documented CBR_* macros.
80 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
81 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
82 BAUD(115200), BAUD(128000), BAUD(256000),
84 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
85 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
86 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
88 #if !defined(__APPLE__) && !defined(__OpenBSD__)
94 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
95 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
97 /* Helper functions. */
98 static enum sp_return validate_port(struct sp_port *port);
99 static struct sp_port **list_append(struct sp_port **list, const char *portname);
100 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
101 struct sp_port_config *config);
102 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
103 const struct sp_port_config *config);
105 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
107 struct sp_port *port;
118 if (!(port = malloc(sizeof(struct sp_port))))
121 len = strlen(portname) + 1;
123 if (!(port->name = malloc(len))) {
128 memcpy(port->name, portname, len);
131 port->hdl = INVALID_HANDLE_VALUE;
141 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
148 if (!port || !port->name)
151 return sp_get_port_by_name(port->name, copy_ptr);
154 void sp_free_port(struct sp_port *port)
165 static struct sp_port **list_append(struct sp_port **list, const char *portname)
170 for (count = 0; list[count]; count++);
171 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
174 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
176 list[count + 1] = NULL;
180 sp_free_port_list(list);
184 enum sp_return sp_list_ports(struct sp_port ***list_ptr)
186 struct sp_port **list;
189 if (!(list = malloc(sizeof(struct sp_port **))))
197 DWORD max_value_len, max_data_size, max_data_len;
198 DWORD value_len, data_size, data_len;
199 DWORD type, index = 0;
203 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
204 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
208 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
209 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
213 max_data_len = max_data_size / sizeof(TCHAR);
214 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
218 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
223 value_len = max_value_len + 1,
224 data_size = max_data_size,
225 RegEnumValue(key, index, value, &value_len,
226 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
228 data_len = data_size / sizeof(TCHAR);
229 data[data_len] = '\0';
231 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
233 name_len = data_len + 1;
235 if (!(name = malloc(name_len))) {
240 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
244 if (type == REG_SZ && !(list = list_append(list, name))) {
260 CFMutableDictionaryRef classes;
267 if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
272 if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
277 CFDictionarySetValue(classes,
278 CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
280 if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
285 if (!(path = malloc(PATH_MAX))) {
290 while ((port = IOIteratorNext(iter))) {
291 cf_path = IORegistryEntryCreateCFProperty(port,
292 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
294 result = CFStringGetCString(cf_path,
295 path, PATH_MAX, kCFStringEncodingASCII);
297 if (result && !(list = list_append(list, path))) {
299 IOObjectRelease(port);
303 IOObjectRelease(port);
308 IOObjectRelease(iter);
313 struct udev_enumerate *ud_enumerate;
314 struct udev_list_entry *ud_list;
315 struct udev_list_entry *ud_entry;
317 struct udev_device *ud_dev, *ud_parent;
320 int fd, ioctl_result;
321 struct serial_struct serial_info;
324 ud_enumerate = udev_enumerate_new(ud);
325 udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
326 udev_enumerate_scan_devices(ud_enumerate);
327 ud_list = udev_enumerate_get_list_entry(ud_enumerate);
328 udev_list_entry_foreach(ud_entry, ud_list) {
329 path = udev_list_entry_get_name(ud_entry);
330 ud_dev = udev_device_new_from_syspath(ud, path);
331 /* If there is no parent device, this is a virtual tty. */
332 ud_parent = udev_device_get_parent(ud_dev);
333 if (ud_parent == NULL) {
334 udev_device_unref(ud_dev);
337 name = udev_device_get_devnode(ud_dev);
338 /* The serial8250 driver has a hardcoded number of ports.
339 * The only way to tell which actually exist on a given system
340 * is to try to open them and make an ioctl call. */
341 driver = udev_device_get_driver(ud_parent);
342 if (driver && !strcmp(driver, "serial8250")) {
343 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
345 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
347 if (ioctl_result != 0)
349 if (serial_info.type == PORT_UNKNOWN)
352 list = list_append(list, name);
354 udev_device_unref(ud_dev);
361 udev_enumerate_unref(ud_enumerate);
369 sp_free_port_list(list);
376 void sp_free_port_list(struct sp_port **list)
380 for (i = 0; list[i]; i++)
381 sp_free_port(list[i]);
385 static enum sp_return validate_port(struct sp_port *port)
390 if (port->hdl == INVALID_HANDLE_VALUE)
399 #define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0)
401 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
407 DWORD desired_access = 0, flags_and_attributes = 0;
408 char *escaped_port_name;
410 /* Prefix port name with '\\.\' to work with ports above COM9. */
411 if (!(escaped_port_name = malloc(strlen(port->name + 5))))
413 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
415 /* Map 'flags' to the OS-specific settings. */
416 desired_access |= GENERIC_READ;
417 flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
418 if (flags & SP_MODE_RDWR)
419 desired_access |= GENERIC_WRITE;
420 if (flags & SP_MODE_NONBLOCK)
421 flags_and_attributes |= FILE_FLAG_OVERLAPPED;
423 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
424 OPEN_EXISTING, flags_and_attributes, 0);
426 free(escaped_port_name);
428 if (port->hdl == INVALID_HANDLE_VALUE)
432 struct port_data data;
433 struct sp_port_config config;
436 /* Map 'flags' to the OS-specific settings. */
437 if (flags & SP_MODE_RDWR)
438 flags_local |= O_RDWR;
439 if (flags & SP_MODE_RDONLY)
440 flags_local |= O_RDONLY;
441 if (flags & SP_MODE_NONBLOCK)
442 flags_local |= O_NONBLOCK;
444 if ((port->fd = open(port->name, flags_local)) < 0)
447 ret = get_config(port, &data, &config);
454 /* Turn off all serial port cooking. */
455 data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
456 data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
457 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
458 data.term.c_oflag &= ~OFILL;
460 /* Disable canonical mode, and don't echo input characters. */
461 data.term.c_lflag &= ~(ICANON | ECHO);
463 /* Ignore modem status lines; enable receiver */
464 data.term.c_cflag |= (CLOCAL | CREAD);
466 ret = set_config(port, &data, &config);
477 enum sp_return sp_close(struct sp_port *port)
482 /* Returns non-zero upon success, 0 upon failure. */
483 if (CloseHandle(port->hdl) == 0)
485 port->hdl = INVALID_HANDLE_VALUE;
487 /* Returns 0 upon success, -1 upon failure. */
488 if (close(port->fd) == -1)
496 enum sp_return sp_flush(struct sp_port *port)
501 /* Returns non-zero upon success, 0 upon failure. */
502 if (PurgeComm(port->hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0)
505 /* Returns 0 upon success, -1 upon failure. */
506 if (tcflush(port->fd, TCIOFLUSH) < 0)
512 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
522 /* Returns non-zero upon success, 0 upon failure. */
523 if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
527 /* Returns the number of bytes written, or -1 upon failure. */
528 ssize_t written = write(port->fd, buf, count);
537 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
545 DWORD bytes_read = 0;
547 /* Returns non-zero upon success, 0 upon failure. */
548 if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
554 /* Returns the number of bytes read, or -1 upon failure. */
555 if ((bytes_read = read(port->fd, buf, count)) < 0)
561 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
562 struct sp_port_config *config)
567 if (!GetCommState(port->hdl, &data->dcb))
570 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
571 if (data->dcb.BaudRate == std_baudrates[i].index) {
572 config->baudrate = std_baudrates[i].value;
577 if (i == NUM_STD_BAUDRATES)
578 /* BaudRate field can be either an index or a custom baud rate. */
579 config->baudrate = data->dcb.BaudRate;
581 config->bits = data->dcb.ByteSize;
583 if (data->dcb.fParity)
584 switch (data->dcb.Parity) {
586 config->parity = SP_PARITY_NONE;
589 config->parity = SP_PARITY_EVEN;
592 config->parity = SP_PARITY_ODD;
598 config->parity = SP_PARITY_NONE;
600 switch (data->dcb.StopBits) {
602 config->stopbits = 1;
605 config->stopbits = 2;
608 config->stopbits = -1;
611 switch (data->dcb.fRtsControl) {
612 case RTS_CONTROL_DISABLE:
613 config->rts = SP_RTS_OFF;
615 case RTS_CONTROL_ENABLE:
616 config->rts = SP_RTS_ON;
618 case RTS_CONTROL_HANDSHAKE:
619 config->rts = SP_RTS_FLOW_CONTROL;
625 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
627 switch (data->dcb.fDtrControl) {
628 case DTR_CONTROL_DISABLE:
629 config->dtr = SP_DTR_OFF;
631 case DTR_CONTROL_ENABLE:
632 config->dtr = SP_DTR_ON;
634 case DTR_CONTROL_HANDSHAKE:
635 config->dtr = SP_DTR_FLOW_CONTROL;
641 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
643 if (data->dcb.fInX) {
645 config->xon_xoff = SP_XONXOFF_INOUT;
647 config->xon_xoff = SP_XONXOFF_IN;
650 config->xon_xoff = SP_XONXOFF_OUT;
652 config->xon_xoff = SP_XONXOFF_DISABLED;
657 if (tcgetattr(port->fd, &data->term) < 0)
660 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
662 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
663 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
664 config->baudrate = std_baudrates[i].value;
669 if (i == NUM_STD_BAUDRATES) {
671 config->baudrate = (int)data->term.c_ispeed;
673 config->baudrate = -1;
677 switch (data->term.c_cflag & CSIZE) {
694 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
695 config->parity = SP_PARITY_NONE;
696 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
699 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
701 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
703 if (data->term.c_cflag & CRTSCTS) {
704 config->rts = SP_RTS_FLOW_CONTROL;
705 config->cts = SP_CTS_FLOW_CONTROL;
707 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
708 config->cts = SP_CTS_IGNORE;
711 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
712 config->dsr = SP_DSR_IGNORE;
714 if (data->term.c_iflag & IXOFF) {
715 if (data->term.c_iflag & IXON)
716 config->xon_xoff = SP_XONXOFF_INOUT;
718 config->xon_xoff = SP_XONXOFF_IN;
720 if (data->term.c_iflag & IXON)
721 config->xon_xoff = SP_XONXOFF_OUT;
723 config->xon_xoff = SP_XONXOFF_DISABLED;
730 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
731 const struct sp_port_config *config)
735 BAUD_TYPE baud_nonstd;
741 if (config->baudrate >= 0) {
742 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
743 if (config->baudrate == std_baudrates[i].value) {
744 data->dcb.BaudRate = std_baudrates[i].index;
749 if (i == NUM_STD_BAUDRATES)
750 data->dcb.BaudRate = config->baudrate;
753 if (config->bits >= 0)
754 data->dcb.ByteSize = config->bits;
756 if (config->parity >= 0) {
757 switch (config->parity) {
758 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
760 data->dcb.Parity = NOPARITY;
763 data->dcb.Parity = EVENPARITY;
766 data->dcb.Parity = ODDPARITY;
773 if (config->stopbits >= 0) {
774 switch (config->stopbits) {
775 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
777 data->dcb.StopBits = ONESTOPBIT;
780 data->dcb.StopBits = TWOSTOPBITS;
787 if (config->rts >= 0) {
788 switch (config->rts) {
790 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
793 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
795 case SP_RTS_FLOW_CONTROL:
796 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
803 if (config->cts >= 0) {
804 switch (config->cts) {
806 data->dcb.fOutxCtsFlow = FALSE;
808 case SP_CTS_FLOW_CONTROL:
809 data->dcb.fOutxCtsFlow = TRUE;
816 if (config->dtr >= 0) {
817 switch (config->dtr) {
819 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
822 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
824 case SP_DTR_FLOW_CONTROL:
825 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
832 if (config->dsr >= 0) {
833 switch (config->dsr) {
835 data->dcb.fOutxDsrFlow = FALSE;
837 case SP_DSR_FLOW_CONTROL:
838 data->dcb.fOutxDsrFlow = TRUE;
845 if (config->xon_xoff >= 0) {
846 switch (config->xon_xoff) {
847 case SP_XONXOFF_DISABLED:
848 data->dcb.fInX = FALSE;
849 data->dcb.fOutX = FALSE;
852 data->dcb.fInX = TRUE;
853 data->dcb.fOutX = FALSE;
856 data->dcb.fInX = FALSE;
857 data->dcb.fOutX = TRUE;
859 case SP_XONXOFF_INOUT:
860 data->dcb.fInX = TRUE;
861 data->dcb.fOutX = TRUE;
868 if (!SetCommState(port->hdl, &data->dcb))
873 if (config->baudrate >= 0) {
874 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
875 if (config->baudrate == std_baudrates[i].value) {
876 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
879 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
885 /* Non-standard baud rate */
886 if (i == NUM_STD_BAUDRATES) {
888 /* Set "dummy" baud rate */
889 if (cfsetspeed(&data->term, B9600) < 0)
891 baud_nonstd = config->baudrate;
898 if (config->bits >= 0) {
899 data->term.c_cflag &= ~CSIZE;
900 switch (config->bits) {
902 data->term.c_cflag |= CS8;
905 data->term.c_cflag |= CS7;
908 data->term.c_cflag |= CS6;
911 data->term.c_cflag |= CS5;
918 if (config->parity >= 0) {
919 data->term.c_iflag &= ~IGNPAR;
920 data->term.c_cflag &= ~(PARENB | PARODD);
921 switch (config->parity) {
923 data->term.c_iflag |= IGNPAR;
926 data->term.c_cflag |= PARENB;
929 data->term.c_cflag |= PARENB | PARODD;
936 if (config->stopbits >= 0) {
937 data->term.c_cflag &= ~CSTOPB;
938 switch (config->stopbits) {
940 data->term.c_cflag &= ~CSTOPB;
943 data->term.c_cflag |= CSTOPB;
950 if (config->rts >= 0 || config->cts >= 0) {
951 /* Asymmetric use of RTS/CTS not supported yet. */
953 if (data->term.c_iflag & CRTSCTS) {
954 /* Flow control can only be disabled for both RTS & CTS together. */
955 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
956 if (config->cts != SP_CTS_IGNORE)
959 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
960 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
964 /* Flow control can only be enabled for both RTS & CTS together. */
965 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
966 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
970 if (config->rts >= 0) {
971 if (config->rts == SP_RTS_FLOW_CONTROL) {
972 data->term.c_iflag |= CRTSCTS;
974 int controlbits = TIOCM_RTS;
975 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
982 if (config->dtr >= 0 || config->dsr >= 0) {
983 /* DTR/DSR flow control not supported yet. */
984 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
987 if (config->dtr >= 0) {
988 int controlbits = TIOCM_DTR;
989 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
995 if (config->xon_xoff >= 0) {
996 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
997 switch (config->xon_xoff) {
998 case SP_XONXOFF_DISABLED:
1001 data->term.c_iflag |= IXOFF;
1003 case SP_XONXOFF_OUT:
1004 data->term.c_iflag |= IXON | IXANY;
1006 case SP_XONXOFF_INOUT:
1007 data->term.c_iflag |= IXON | IXOFF | IXANY;
1014 if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
1018 if (baud_nonstd != B0) {
1019 if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
1021 /* Set baud rates in data->term to correct, but incompatible
1022 * with tcsetattr() value, same as delivered by tcgetattr(). */
1023 if (cfsetspeed(&data->term, baud_nonstd) < 0)
1026 #endif /* __APPLE__ */
1028 #endif /* !_WIN32 */
1033 #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
1035 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
1037 struct port_data data;
1038 struct sp_port_config prev_config;
1045 TRY(get_config(port, &data, &prev_config));
1046 TRY(set_config(port, &data, config));
1051 #define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \
1052 struct port_data data; \
1053 struct sp_port_config config; \
1055 TRY(get_config(port, &data, &config)); \
1057 TRY(set_config(port, &data, &config)); \
1061 CREATE_SETTER(baudrate, int)
1062 CREATE_SETTER(bits, int)
1063 CREATE_SETTER(parity, enum sp_parity)
1064 CREATE_SETTER(stopbits, int)
1065 CREATE_SETTER(rts, enum sp_rts)
1066 CREATE_SETTER(cts, enum sp_cts)
1067 CREATE_SETTER(dtr, enum sp_dtr)
1068 CREATE_SETTER(dsr, enum sp_dsr)
1069 CREATE_SETTER(xon_xoff, enum sp_xonxoff)
1071 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
1073 struct port_data data;
1074 struct sp_port_config config;
1078 TRY(get_config(port, &data, &config));
1080 if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
1081 config.xon_xoff = SP_XONXOFF_INOUT;
1083 config.xon_xoff = SP_XONXOFF_DISABLED;
1085 if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
1086 config.rts = SP_RTS_FLOW_CONTROL;
1087 config.cts = SP_CTS_FLOW_CONTROL;
1089 if (config.rts == SP_RTS_FLOW_CONTROL)
1090 config.rts = SP_RTS_ON;
1091 config.cts = SP_CTS_IGNORE;
1094 if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
1095 config.dtr = SP_DTR_FLOW_CONTROL;
1096 config.dsr = SP_DSR_FLOW_CONTROL;
1098 if (config.dtr == SP_DTR_FLOW_CONTROL)
1099 config.dtr = SP_DTR_ON;
1100 config.dsr = SP_DSR_IGNORE;
1103 TRY(set_config(port, &data, &config));
1108 int sp_last_error_code(void)
1111 return GetLastError();
1117 char *sp_last_error_message(void)
1121 DWORD error = GetLastError();
1124 FORMAT_MESSAGE_ALLOCATE_BUFFER |
1125 FORMAT_MESSAGE_FROM_SYSTEM |
1126 FORMAT_MESSAGE_IGNORE_INSERTS,
1129 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1135 return strerror(errno);
1139 void sp_free_error_message(char *message)