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>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
35 #include <sys/ioctl.h>
38 #include <IOKit/IOKitLib.h>
39 #include <IOKit/serial/IOSerialKeys.h>
40 #include <sys/syslimits.h>
44 #include "linux/serial.h"
47 #include "libserialport.h"
58 /* Standard baud rates. */
60 #define BAUD_TYPE DWORD
61 #define BAUD(n) {CBR_##n, n}
63 #define BAUD_TYPE speed_t
64 #define BAUD(n) {B##n, n}
72 const struct std_baudrate std_baudrates[] = {
75 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
76 * have documented CBR_* macros.
78 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
79 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
80 BAUD(115200), BAUD(128000), BAUD(256000),
82 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
83 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
84 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
86 #if !defined(__APPLE__) && !defined(__OpenBSD__)
92 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
93 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
95 /* Helper functions. */
96 static enum sp_return validate_port(struct sp_port *port);
97 static struct sp_port **list_append(struct sp_port **list, const char *portname);
98 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
99 struct sp_port_config *config);
100 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
101 const struct sp_port_config *config);
103 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
105 struct sp_port *port;
116 if (!(port = malloc(sizeof(struct sp_port))))
119 len = strlen(portname) + 1;
121 if (!(port->name = malloc(len))) {
126 memcpy(port->name, portname, len);
129 port->hdl = INVALID_HANDLE_VALUE;
139 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
146 if (!port || !port->name)
149 return sp_get_port_by_name(port->name, copy_ptr);
152 void sp_free_port(struct sp_port *port)
163 static struct sp_port **list_append(struct sp_port **list, const char *portname)
168 for (count = 0; list[count]; count++);
169 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
172 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
174 list[count + 1] = NULL;
178 sp_free_port_list(list);
182 enum sp_return sp_list_ports(struct sp_port ***list_ptr)
184 struct sp_port **list;
187 if (!(list = malloc(sizeof(struct sp_port **))))
195 DWORD max_value_len, max_data_size, max_data_len;
196 DWORD value_len, data_size, data_len;
197 DWORD type, index = 0;
201 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
202 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
206 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
207 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
211 max_data_len = max_data_size / sizeof(TCHAR);
212 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
216 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
221 value_len = max_value_len + 1,
222 data_size = max_data_size,
223 RegEnumValue(key, index, value, &value_len,
224 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
226 data_len = data_size / sizeof(TCHAR);
227 data[data_len] = '\0';
229 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
231 name_len = data_len + 1;
233 if (!(name = malloc(name_len))) {
238 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
242 if (type == REG_SZ && !(list = list_append(list, name))) {
258 CFMutableDictionaryRef classes;
265 if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
270 if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
275 CFDictionarySetValue(classes,
276 CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
278 if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
283 if (!(path = malloc(PATH_MAX))) {
288 while ((port = IOIteratorNext(iter))) {
289 cf_path = IORegistryEntryCreateCFProperty(port,
290 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
292 result = CFStringGetCString(cf_path,
293 path, PATH_MAX, kCFStringEncodingASCII);
295 if (result && !(list = list_append(list, path))) {
297 IOObjectRelease(port);
301 IOObjectRelease(port);
306 IOObjectRelease(iter);
311 struct udev_enumerate *ud_enumerate;
312 struct udev_list_entry *ud_list;
313 struct udev_list_entry *ud_entry;
315 struct udev_device *ud_dev, *ud_parent;
318 int fd, ioctl_result;
319 struct serial_struct serial_info;
322 ud_enumerate = udev_enumerate_new(ud);
323 udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
324 udev_enumerate_scan_devices(ud_enumerate);
325 ud_list = udev_enumerate_get_list_entry(ud_enumerate);
326 udev_list_entry_foreach(ud_entry, ud_list) {
327 path = udev_list_entry_get_name(ud_entry);
328 ud_dev = udev_device_new_from_syspath(ud, path);
329 /* If there is no parent device, this is a virtual tty. */
330 ud_parent = udev_device_get_parent(ud_dev);
331 if (ud_parent == NULL) {
332 udev_device_unref(ud_dev);
335 name = udev_device_get_devnode(ud_dev);
336 /* The serial8250 driver has a hardcoded number of ports.
337 * The only way to tell which actually exist on a given system
338 * is to try to open them and make an ioctl call. */
339 driver = udev_device_get_driver(ud_parent);
340 if (driver && !strcmp(driver, "serial8250")) {
341 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
343 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
345 if (ioctl_result != 0)
347 if (serial_info.type == PORT_UNKNOWN)
350 list = list_append(list, name);
352 udev_device_unref(ud_dev);
359 udev_enumerate_unref(ud_enumerate);
367 sp_free_port_list(list);
374 void sp_free_port_list(struct sp_port **list)
378 for (i = 0; list[i]; i++)
379 sp_free_port(list[i]);
383 static enum sp_return validate_port(struct sp_port *port)
388 if (port->hdl == INVALID_HANDLE_VALUE)
397 #define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0)
399 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
405 DWORD desired_access = 0, flags_and_attributes = 0;
406 char *escaped_port_name;
408 /* Prefix port name with '\\.\' to work with ports above COM9. */
409 if (!(escaped_port_name = malloc(strlen(port->name + 5))))
411 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
413 /* Map 'flags' to the OS-specific settings. */
414 desired_access |= GENERIC_READ;
415 flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
416 if (flags & SP_MODE_RDWR)
417 desired_access |= GENERIC_WRITE;
418 if (flags & SP_MODE_NONBLOCK)
419 flags_and_attributes |= FILE_FLAG_OVERLAPPED;
421 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
422 OPEN_EXISTING, flags_and_attributes, 0);
424 free(escaped_port_name);
426 if (port->hdl == INVALID_HANDLE_VALUE)
430 struct port_data data;
431 struct sp_port_config config;
434 /* Map 'flags' to the OS-specific settings. */
435 if (flags & SP_MODE_RDWR)
436 flags_local |= O_RDWR;
437 if (flags & SP_MODE_RDONLY)
438 flags_local |= O_RDONLY;
439 if (flags & SP_MODE_NONBLOCK)
440 flags_local |= O_NONBLOCK;
442 if ((port->fd = open(port->name, flags_local)) < 0)
445 ret = get_config(port, &data, &config);
452 /* Turn off all serial port cooking. */
453 data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
454 data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
455 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
456 data.term.c_oflag &= ~OFILL;
458 /* Disable canonical mode, and don't echo input characters. */
459 data.term.c_lflag &= ~(ICANON | ECHO);
461 /* Ignore modem status lines; enable receiver */
462 data.term.c_cflag |= (CLOCAL | CREAD);
464 ret = set_config(port, &data, &config);
475 enum sp_return sp_close(struct sp_port *port)
480 /* Returns non-zero upon success, 0 upon failure. */
481 if (CloseHandle(port->hdl) == 0)
483 port->hdl = INVALID_HANDLE_VALUE;
485 /* Returns 0 upon success, -1 upon failure. */
486 if (close(port->fd) == -1)
494 enum sp_return sp_flush(struct sp_port *port)
499 /* Returns non-zero upon success, 0 upon failure. */
500 if (PurgeComm(port->hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0)
503 /* Returns 0 upon success, -1 upon failure. */
504 if (tcflush(port->fd, TCIOFLUSH) < 0)
510 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
520 /* Returns non-zero upon success, 0 upon failure. */
521 if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
525 /* Returns the number of bytes written, or -1 upon failure. */
526 ssize_t written = write(port->fd, buf, count);
535 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
543 DWORD bytes_read = 0;
545 /* Returns non-zero upon success, 0 upon failure. */
546 if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
552 /* Returns the number of bytes read, or -1 upon failure. */
553 if ((bytes_read = read(port->fd, buf, count)) < 0)
559 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
560 struct sp_port_config *config)
565 if (!GetCommState(port->hdl, &data->dcb))
568 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
569 if (data->dcb.BaudRate == std_baudrates[i].index) {
570 config->baudrate = std_baudrates[i].value;
575 if (i == NUM_STD_BAUDRATES)
576 /* BaudRate field can be either an index or a custom baud rate. */
577 config->baudrate = data->dcb.BaudRate;
579 config->bits = data->dcb.ByteSize;
581 if (data->dcb.fParity)
582 switch (data->dcb.Parity) {
584 config->parity = SP_PARITY_NONE;
587 config->parity = SP_PARITY_EVEN;
590 config->parity = SP_PARITY_ODD;
596 config->parity = SP_PARITY_NONE;
598 switch (data->dcb.StopBits) {
600 config->stopbits = 1;
603 config->stopbits = 2;
606 config->stopbits = -1;
609 switch (data->dcb.fRtsControl) {
610 case RTS_CONTROL_DISABLE:
611 config->rts = SP_RTS_OFF;
613 case RTS_CONTROL_ENABLE:
614 config->rts = SP_RTS_ON;
616 case RTS_CONTROL_HANDSHAKE:
617 config->rts = SP_RTS_FLOW_CONTROL;
623 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
625 switch (data->dcb.fDtrControl) {
626 case DTR_CONTROL_DISABLE:
627 config->dtr = SP_DTR_OFF;
629 case DTR_CONTROL_ENABLE:
630 config->dtr = SP_DTR_ON;
632 case DTR_CONTROL_HANDSHAKE:
633 config->dtr = SP_DTR_FLOW_CONTROL;
639 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
641 if (data->dcb.fInX) {
643 config->xon_xoff = SP_XONXOFF_INOUT;
645 config->xon_xoff = SP_XONXOFF_IN;
648 config->xon_xoff = SP_XONXOFF_OUT;
650 config->xon_xoff = SP_XONXOFF_DISABLED;
655 if (tcgetattr(port->fd, &data->term) < 0)
658 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
660 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
661 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
662 config->baudrate = std_baudrates[i].value;
667 if (i == NUM_STD_BAUDRATES)
668 config->baudrate = -1;
670 switch (data->term.c_cflag & CSIZE) {
687 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
688 config->parity = SP_PARITY_NONE;
689 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
692 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
694 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
696 if (data->term.c_cflag & CRTSCTS) {
697 config->rts = SP_RTS_FLOW_CONTROL;
698 config->cts = SP_CTS_FLOW_CONTROL;
700 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
701 config->cts = SP_CTS_IGNORE;
704 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
705 config->dsr = SP_DSR_IGNORE;
707 /* FIXME: Set config->xon_xoff properly, depending on data->term. */
708 config->xon_xoff = SP_XONXOFF_DISABLED;
714 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
715 const struct sp_port_config *config)
720 if (config->baudrate >= 0) {
721 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
722 if (config->baudrate == std_baudrates[i].value) {
723 data->dcb.BaudRate = std_baudrates[i].index;
728 if (i == NUM_STD_BAUDRATES)
729 data->dcb.BaudRate = config->baudrate;
732 if (config->bits >= 0)
733 data->dcb.ByteSize = config->bits;
735 if (config->parity >= 0) {
736 switch (config->parity) {
737 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
739 data->dcb.Parity = NOPARITY;
742 data->dcb.Parity = EVENPARITY;
745 data->dcb.Parity = ODDPARITY;
752 if (config->stopbits >= 0) {
753 switch (config->stopbits) {
754 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
756 data->dcb.StopBits = ONESTOPBIT;
759 data->dcb.StopBits = TWOSTOPBITS;
766 if (config->rts >= 0) {
767 switch (config->rts) {
769 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
772 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
774 case SP_RTS_FLOW_CONTROL:
775 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
782 if (config->cts >= 0) {
783 switch (config->cts) {
785 data->dcb.fOutxCtsFlow = FALSE;
787 case SP_CTS_FLOW_CONTROL:
788 data->dcb.fOutxCtsFlow = TRUE;
795 if (config->dtr >= 0) {
796 switch (config->dtr) {
798 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
801 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
803 case SP_DTR_FLOW_CONTROL:
804 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
811 if (config->dsr >= 0) {
812 switch (config->dsr) {
814 data->dcb.fOutxDsrFlow = FALSE;
816 case SP_DSR_FLOW_CONTROL:
817 data->dcb.fOutxDsrFlow = TRUE;
824 if (config->xon_xoff >= 0) {
825 switch (config->xon_xoff) {
826 case SP_XONXOFF_DISABLED:
827 data->dcb.fInX = FALSE;
828 data->dcb.fOutX = FALSE;
831 data->dcb.fInX = TRUE;
832 data->dcb.fOutX = FALSE;
835 data->dcb.fInX = FALSE;
836 data->dcb.fOutX = TRUE;
838 case SP_XONXOFF_INOUT:
839 data->dcb.fInX = TRUE;
840 data->dcb.fOutX = TRUE;
847 if (!SetCommState(port->hdl, &data->dcb))
852 if (config->baudrate >= 0) {
853 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
854 if (config->baudrate == std_baudrates[i].value) {
855 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
858 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
864 if (i == NUM_STD_BAUDRATES)
868 if (config->bits >= 0) {
869 data->term.c_cflag &= ~CSIZE;
870 switch (config->bits) {
872 data->term.c_cflag |= CS8;
875 data->term.c_cflag |= CS7;
878 data->term.c_cflag |= CS6;
881 data->term.c_cflag |= CS5;
888 if (config->parity >= 0) {
889 data->term.c_iflag &= ~IGNPAR;
890 data->term.c_cflag &= ~(PARENB | PARODD);
891 switch (config->parity) {
893 data->term.c_iflag |= IGNPAR;
896 data->term.c_cflag |= PARENB;
899 data->term.c_cflag |= PARENB | PARODD;
906 if (config->stopbits >= 0) {
907 data->term.c_cflag &= ~CSTOPB;
908 switch (config->stopbits) {
910 data->term.c_cflag &= ~CSTOPB;
913 data->term.c_cflag |= CSTOPB;
920 if (config->rts >= 0 || config->cts >= 0) {
921 /* Asymmetric use of RTS/CTS not supported yet. */
923 if (data->term.c_iflag & CRTSCTS) {
924 /* Flow control can only be disabled for both RTS & CTS together. */
925 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
926 if (config->cts != SP_CTS_IGNORE)
929 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
930 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
934 /* Flow control can only be enabled for both RTS & CTS together. */
935 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
936 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
940 if (config->rts >= 0) {
941 if (config->rts == SP_RTS_FLOW_CONTROL) {
942 data->term.c_iflag |= CRTSCTS;
944 int controlbits = TIOCM_RTS;
945 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
952 if (config->dtr >= 0 || config->dsr >= 0) {
953 /* DTR/DSR flow control not supported yet. */
954 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
957 if (config->dtr >= 0) {
958 int controlbits = TIOCM_DTR;
959 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
965 if (config->xon_xoff >= 0) {
966 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
967 switch (config->xon_xoff) {
968 case SP_XONXOFF_DISABLED:
971 data->term.c_iflag |= IXOFF;
974 data->term.c_iflag |= IXON | IXANY;
976 case SP_XONXOFF_INOUT:
977 data->term.c_iflag |= IXON | IXOFF | IXANY;
984 if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
991 #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
993 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
995 struct port_data data;
996 struct sp_port_config prev_config;
1003 TRY(get_config(port, &data, &prev_config));
1004 TRY(set_config(port, &data, config));
1009 #define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \
1010 struct port_data data; \
1011 struct sp_port_config config; \
1013 TRY(get_config(port, &data, &config)); \
1015 TRY(set_config(port, &data, &config)); \
1019 CREATE_SETTER(baudrate, int)
1020 CREATE_SETTER(bits, int)
1021 CREATE_SETTER(parity, enum sp_parity)
1022 CREATE_SETTER(stopbits, int)
1023 CREATE_SETTER(rts, enum sp_rts)
1024 CREATE_SETTER(cts, enum sp_cts)
1025 CREATE_SETTER(dtr, enum sp_dtr)
1026 CREATE_SETTER(dsr, enum sp_dsr)
1027 CREATE_SETTER(xon_xoff, enum sp_xonxoff)
1029 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
1031 struct port_data data;
1032 struct sp_port_config config;
1036 TRY(get_config(port, &data, &config));
1038 if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
1039 config.xon_xoff = SP_XONXOFF_INOUT;
1041 config.xon_xoff = SP_XONXOFF_DISABLED;
1043 if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
1044 config.rts = SP_RTS_FLOW_CONTROL;
1045 config.cts = SP_CTS_FLOW_CONTROL;
1047 if (config.rts == SP_RTS_FLOW_CONTROL)
1048 config.rts = SP_RTS_ON;
1049 config.cts = SP_CTS_IGNORE;
1052 if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
1053 config.dtr = SP_DTR_FLOW_CONTROL;
1054 config.dsr = SP_DSR_FLOW_CONTROL;
1056 if (config.dtr == SP_DTR_FLOW_CONTROL)
1057 config.dtr = SP_DTR_ON;
1058 config.dsr = SP_DSR_IGNORE;
1061 TRY(set_config(port, &data, &config));
1066 int sp_last_error_code(void)
1069 return GetLastError();
1075 char *sp_last_error_message(void)
1079 DWORD error = GetLastError();
1082 FORMAT_MESSAGE_ALLOCATE_BUFFER |
1083 FORMAT_MESSAGE_FROM_SYSTEM |
1084 FORMAT_MESSAGE_IGNORE_INSERTS,
1087 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1093 return strerror(errno);
1097 void sp_free_error_message(char *message)