]> sigrok.org Git - libserialport.git/blame - serialport.c
Temporarily disable USE_TERMIOX code (breaks the lib).
[libserialport.git] / serialport.c
CommitLineData
74510d4b
ML
1/*
2 * This file is part of the libserialport project.
3 *
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>
31b3a8f5 7 * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
74510d4b
ML
8 *
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.
13 *
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.
18 *
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/>.
21 */
22
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <unistd.h>
3b63f34d
ML
28#include <stdlib.h>
29#include <errno.h>
74510d4b
ML
30#ifdef _WIN32
31#include <windows.h>
3b63f34d 32#include <tchar.h>
767c5ba8 33#include <stdio.h>
74510d4b 34#else
74510d4b
ML
35#include <termios.h>
36#include <sys/ioctl.h>
37#endif
3b63f34d 38#ifdef __APPLE__
1ebf4347
ML
39#include <IOKit/IOKitLib.h>
40#include <IOKit/serial/IOSerialKeys.h>
31b3a8f5 41#include <IOKit/serial/ioss.h>
1ebf4347 42#include <sys/syslimits.h>
3b63f34d
ML
43#endif
44#ifdef __linux__
45#include "libudev.h"
4b97c9fc 46#include "linux/serial.h"
7a6d2196 47#include "linux_termios.h"
40978c2b 48#if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX)
1d5d2a16
UH
49// FIXME: Temporarily disabled, breaks all lib functionality on some systems.
50// #define USE_TERMIOX
40978c2b 51#endif
3b63f34d 52#endif
74510d4b 53
f6a1fb65 54#include "libserialport.h"
74510d4b 55
8f189c4c 56struct port_data {
8094e4a0
ML
57#ifdef _WIN32
58 DCB dcb;
59#else
60 struct termios term;
824dcb45 61 int controlbits;
40978c2b
ML
62#ifdef USE_TERMIOX
63 int flow;
64#endif
8094e4a0
ML
65#endif
66};
67
da2748bf
ML
68/* Standard baud rates. */
69#ifdef _WIN32
70#define BAUD_TYPE DWORD
71#define BAUD(n) {CBR_##n, n}
72#else
73#define BAUD_TYPE speed_t
74#define BAUD(n) {B##n, n}
75#endif
76
77struct std_baudrate {
78 BAUD_TYPE index;
79 int value;
80};
81
82const struct std_baudrate std_baudrates[] = {
83#ifdef _WIN32
84 /*
85 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
86 * have documented CBR_* macros.
87 */
88 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
89 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
eac329d2 90 BAUD(115200), BAUD(128000), BAUD(256000),
da2748bf 91#else
eac329d2
UH
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),
95 BAUD(230400),
da2748bf 96#if !defined(__APPLE__) && !defined(__OpenBSD__)
eac329d2 97 BAUD(460800),
da2748bf
ML
98#endif
99#endif
100};
101
102#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
103#define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
104
7a6d2196
ML
105#define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
106
348e23cc 107/* Helper functions. */
eb6ed20f 108static enum sp_return validate_port(struct sp_port *port);
348e23cc 109static struct sp_port **list_append(struct sp_port **list, const char *portname);
eb6ed20f
ML
110static enum sp_return get_config(struct sp_port *port, struct port_data *data,
111 struct sp_port_config *config);
112static enum sp_return set_config(struct sp_port *port, struct port_data *data,
113 const struct sp_port_config *config);
80186526 114
eb6ed20f 115enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
d54e9004
ML
116{
117 struct sp_port *port;
5919c913 118 int len;
d54e9004 119
32b5ac05
ML
120 if (!port_ptr)
121 return SP_ERR_ARG;
122
77f262c4
ML
123 *port_ptr = NULL;
124
d4babed2 125 if (!portname)
77f262c4 126 return SP_ERR_ARG;
d4babed2 127
d54e9004 128 if (!(port = malloc(sizeof(struct sp_port))))
77f262c4 129 return SP_ERR_MEM;
d54e9004 130
5919c913
ML
131 len = strlen(portname) + 1;
132
eac329d2 133 if (!(port->name = malloc(len))) {
d54e9004 134 free(port);
77f262c4 135 return SP_ERR_MEM;
d54e9004
ML
136 }
137
138 memcpy(port->name, portname, len);
139
8f471c66
ML
140#ifdef _WIN32
141 port->hdl = INVALID_HANDLE_VALUE;
142#else
143 port->fd = -1;
144#endif
145
77f262c4
ML
146 *port_ptr = port;
147
148 return SP_OK;
d54e9004
ML
149}
150
eb6ed20f 151enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
32b5ac05
ML
152{
153 if (!copy_ptr)
154 return SP_ERR_ARG;
155
156 *copy_ptr = NULL;
157
158 if (!port || !port->name)
159 return SP_ERR_ARG;
160
161 return sp_get_port_by_name(port->name, copy_ptr);
162}
163
e3b2f7a4
ML
164void sp_free_port(struct sp_port *port)
165{
166 if (!port)
167 return;
168
169 if (port->name)
170 free(port->name);
171
172 free(port);
173}
174
348e23cc 175static struct sp_port **list_append(struct sp_port **list, const char *portname)
3b63f34d
ML
176{
177 void *tmp;
178 unsigned int count;
f92f1f0c 179
3b63f34d 180 for (count = 0; list[count]; count++);
d54e9004 181 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
3b63f34d
ML
182 goto fail;
183 list = tmp;
77f262c4 184 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
3b63f34d 185 goto fail;
db2794ce 186 list[count + 1] = NULL;
3b63f34d 187 return list;
f92f1f0c 188
3b63f34d
ML
189fail:
190 sp_free_port_list(list);
191 return NULL;
192}
193
eb6ed20f 194enum sp_return sp_list_ports(struct sp_port ***list_ptr)
3b63f34d 195{
d54e9004 196 struct sp_port **list;
77f262c4 197 int ret = SP_OK;
24c1a4bb 198
d54e9004 199 if (!(list = malloc(sizeof(struct sp_port **))))
f92f1f0c 200 return SP_ERR_MEM;
24c1a4bb
ML
201
202 list[0] = NULL;
3b63f34d
ML
203
204#ifdef _WIN32
205 HKEY key;
bdfb5b8c
ML
206 TCHAR *value, *data;
207 DWORD max_value_len, max_data_size, max_data_len;
208 DWORD value_len, data_size, data_len;
3b63f34d 209 DWORD type, index = 0;
8b532d9c
ML
210 char *name;
211 int name_len;
3b63f34d
ML
212
213 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
eac329d2 214 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
77f262c4
ML
215 ret = SP_ERR_FAIL;
216 goto out_done;
217 }
3b63f34d 218 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
eac329d2 219 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
77f262c4 220 ret = SP_ERR_FAIL;
3b63f34d 221 goto out_close;
77f262c4 222 }
3b63f34d 223 max_data_len = max_data_size / sizeof(TCHAR);
eac329d2 224 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
77f262c4 225 ret = SP_ERR_MEM;
3b63f34d 226 goto out_close;
77f262c4 227 }
eac329d2 228 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
77f262c4 229 ret = SP_ERR_MEM;
bdfb5b8c 230 goto out_free_value;
77f262c4 231 }
3b63f34d 232 while (
d9573bad 233 value_len = max_value_len + 1,
3b63f34d 234 data_size = max_data_size,
bdfb5b8c 235 RegEnumValue(key, index, value, &value_len,
3b63f34d
ML
236 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
237 {
238 data_len = data_size / sizeof(TCHAR);
239 data[data_len] = '\0';
8b532d9c
ML
240#ifdef UNICODE
241 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
242#else
243 name_len = data_len + 1;
244#endif
eac329d2 245 if (!(name = malloc(name_len))) {
77f262c4 246 ret = SP_ERR_MEM;
8b532d9c 247 goto out;
77f262c4 248 }
8b532d9c
ML
249#ifdef UNICODE
250 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
251#else
252 strcpy(name, data);
253#endif
eac329d2 254 if (type == REG_SZ && !(list = list_append(list, name))) {
77f262c4
ML
255 ret = SP_ERR_MEM;
256 goto out;
257 }
3b63f34d
ML
258 index++;
259 }
260out:
261 free(data);
bdfb5b8c
ML
262out_free_value:
263 free(value);
3b63f34d
ML
264out_close:
265 RegCloseKey(key);
77f262c4 266out_done:
3b63f34d
ML
267#endif
268#ifdef __APPLE__
269 mach_port_t master;
270 CFMutableDictionaryRef classes;
271 io_iterator_t iter;
272 char *path;
273 io_object_t port;
274 CFTypeRef cf_path;
275 Boolean result;
276
eac329d2 277 if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
77f262c4
ML
278 ret = SP_ERR_FAIL;
279 goto out_done;
280 }
3b63f34d 281
eac329d2 282 if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
77f262c4
ML
283 ret = SP_ERR_FAIL;
284 goto out_done;
285 }
3b63f34d
ML
286
287 CFDictionarySetValue(classes,
288 CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
289
eac329d2 290 if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
77f262c4
ML
291 ret = SP_ERR_FAIL;
292 goto out_done;
293 }
3b63f34d 294
eac329d2 295 if (!(path = malloc(PATH_MAX))) {
77f262c4 296 ret = SP_ERR_MEM;
3b63f34d 297 goto out_release;
77f262c4 298 }
3b63f34d 299
1ebf4347 300 while ((port = IOIteratorNext(iter))) {
3b63f34d
ML
301 cf_path = IORegistryEntryCreateCFProperty(port,
302 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
303 if (cf_path) {
304 result = CFStringGetCString(cf_path,
305 path, PATH_MAX, kCFStringEncodingASCII);
306 CFRelease(cf_path);
eac329d2 307 if (result && !(list = list_append(list, path))) {
77f262c4
ML
308 ret = SP_ERR_MEM;
309 IOObjectRelease(port);
310 goto out;
311 }
3b63f34d
ML
312 }
313 IOObjectRelease(port);
314 }
3b63f34d
ML
315out:
316 free(path);
317out_release:
318 IOObjectRelease(iter);
77f262c4 319out_done:
3b63f34d
ML
320#endif
321#ifdef __linux__
322 struct udev *ud;
323 struct udev_enumerate *ud_enumerate;
324 struct udev_list_entry *ud_list;
325 struct udev_list_entry *ud_entry;
326 const char *path;
08fe0bdb 327 struct udev_device *ud_dev, *ud_parent;
3b63f34d 328 const char *name;
4b97c9fc
ML
329 const char *driver;
330 int fd, ioctl_result;
331 struct serial_struct serial_info;
3b63f34d
ML
332
333 ud = udev_new();
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);
eac329d2 338 udev_list_entry_foreach(ud_entry, ud_list) {
3b63f34d
ML
339 path = udev_list_entry_get_name(ud_entry);
340 ud_dev = udev_device_new_from_syspath(ud, path);
08fe0bdb
ML
341 /* If there is no parent device, this is a virtual tty. */
342 ud_parent = udev_device_get_parent(ud_dev);
eac329d2 343 if (ud_parent == NULL) {
08fe0bdb
ML
344 udev_device_unref(ud_dev);
345 continue;
346 }
3b63f34d 347 name = udev_device_get_devnode(ud_dev);
4b97c9fc
ML
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);
eac329d2 352 if (driver && !strcmp(driver, "serial8250")) {
4b97c9fc
ML
353 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
354 goto skip;
355 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
356 close(fd);
357 if (ioctl_result != 0)
358 goto skip;
359 if (serial_info.type == PORT_UNKNOWN)
360 goto skip;
361 }
348e23cc 362 list = list_append(list, name);
4b97c9fc 363skip:
3b63f34d 364 udev_device_unref(ud_dev);
eac329d2 365 if (!list) {
77f262c4 366 ret = SP_ERR_MEM;
3b63f34d 367 goto out;
77f262c4 368 }
3b63f34d
ML
369 }
370out:
371 udev_enumerate_unref(ud_enumerate);
372 udev_unref(ud);
3b63f34d 373#endif
77f262c4 374
eac329d2 375 if (ret == SP_OK) {
77f262c4 376 *list_ptr = list;
eac329d2 377 } else {
77f262c4
ML
378 if (list)
379 sp_free_port_list(list);
77f262c4
ML
380 *list_ptr = NULL;
381 }
382
383 return ret;
3b63f34d
ML
384}
385
d54e9004 386void sp_free_port_list(struct sp_port **list)
3b63f34d
ML
387{
388 unsigned int i;
f92f1f0c 389
3b63f34d 390 for (i = 0; list[i]; i++)
e3b2f7a4 391 sp_free_port(list[i]);
3b63f34d
ML
392 free(list);
393}
394
eb6ed20f 395static enum sp_return validate_port(struct sp_port *port)
74510d4b
ML
396{
397 if (port == NULL)
398 return 0;
399#ifdef _WIN32
400 if (port->hdl == INVALID_HANDLE_VALUE)
401 return 0;
402#else
403 if (port->fd < 0)
404 return 0;
405#endif
406 return 1;
407}
408
348e23cc 409#define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0)
74510d4b 410
eb6ed20f 411enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
74510d4b
ML
412{
413 if (!port)
414 return SP_ERR_ARG;
415
74510d4b
ML
416#ifdef _WIN32
417 DWORD desired_access = 0, flags_and_attributes = 0;
99945a1f
ML
418 char *escaped_port_name;
419
420 /* Prefix port name with '\\.\' to work with ports above COM9. */
421 if (!(escaped_port_name = malloc(strlen(port->name + 5))))
422 return SP_ERR_MEM;
423 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
424
74510d4b 425 /* Map 'flags' to the OS-specific settings. */
74510d4b 426 flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
a036341b
ML
427 if (flags & SP_MODE_READ)
428 desired_access |= GENERIC_READ;
429 if (flags & SP_MODE_WRITE)
74510d4b
ML
430 desired_access |= GENERIC_WRITE;
431 if (flags & SP_MODE_NONBLOCK)
432 flags_and_attributes |= FILE_FLAG_OVERLAPPED;
433
99945a1f 434 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
74510d4b 435 OPEN_EXISTING, flags_and_attributes, 0);
99945a1f
ML
436
437 free(escaped_port_name);
438
74510d4b
ML
439 if (port->hdl == INVALID_HANDLE_VALUE)
440 return SP_ERR_FAIL;
441#else
442 int flags_local = 0;
8f189c4c 443 struct port_data data;
e33ab9aa
ML
444 struct sp_port_config config;
445 int ret;
f92f1f0c 446
74510d4b 447 /* Map 'flags' to the OS-specific settings. */
a036341b 448 if (flags & (SP_MODE_READ | SP_MODE_WRITE))
74510d4b 449 flags_local |= O_RDWR;
a036341b 450 else if (flags & SP_MODE_READ)
74510d4b 451 flags_local |= O_RDONLY;
a036341b
ML
452 else if (flags & SP_MODE_WRITE)
453 flags_local |= O_WRONLY;
74510d4b
ML
454 if (flags & SP_MODE_NONBLOCK)
455 flags_local |= O_NONBLOCK;
456
457 if ((port->fd = open(port->name, flags_local)) < 0)
458 return SP_ERR_FAIL;
9cb98459 459
e33ab9aa
ML
460 ret = get_config(port, &data, &config);
461
eac329d2 462 if (ret < 0) {
e33ab9aa
ML
463 sp_close(port);
464 return ret;
465 }
9cb98459
ML
466
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;
472#endif
473 /* Disable canonical mode, and don't echo input characters. */
474 data.term.c_lflag &= ~(ICANON | ECHO);
475
476 /* Ignore modem status lines; enable receiver */
477 data.term.c_cflag |= (CLOCAL | CREAD);
478
e33ab9aa
ML
479 ret = set_config(port, &data, &config);
480
eac329d2 481 if (ret < 0) {
e33ab9aa
ML
482 sp_close(port);
483 return ret;
484 }
74510d4b
ML
485#endif
486
487 return SP_OK;
488}
489
eb6ed20f 490enum sp_return sp_close(struct sp_port *port)
74510d4b
ML
491{
492 CHECK_PORT();
493
494#ifdef _WIN32
495 /* Returns non-zero upon success, 0 upon failure. */
496 if (CloseHandle(port->hdl) == 0)
497 return SP_ERR_FAIL;
8f471c66 498 port->hdl = INVALID_HANDLE_VALUE;
74510d4b
ML
499#else
500 /* Returns 0 upon success, -1 upon failure. */
501 if (close(port->fd) == -1)
502 return SP_ERR_FAIL;
8f471c66 503 port->fd = -1;
74510d4b
ML
504#endif
505
506 return SP_OK;
507}
508
fd8fd11a 509enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
74510d4b
ML
510{
511 CHECK_PORT();
512
513#ifdef _WIN32
fd8fd11a
ML
514 DWORD flags = 0;
515 if (buffers & SP_BUF_INPUT)
516 flags |= PURGE_RXCLEAR;
517 if (buffers & SP_BUF_OUTPUT)
518 flags |= PURGE_TXCLEAR;
519
74510d4b 520 /* Returns non-zero upon success, 0 upon failure. */
fd8fd11a 521 if (PurgeComm(port->hdl, flags) == 0)
74510d4b
ML
522 return SP_ERR_FAIL;
523#else
fd8fd11a
ML
524 int flags = 0;
525 if (buffers & SP_BUF_BOTH)
526 flags = TCIOFLUSH;
527 else if (buffers & SP_BUF_INPUT)
528 flags = TCIFLUSH;
82f424e6 529 else if (buffers & SP_BUF_OUTPUT)
fd8fd11a
ML
530 flags = TCOFLUSH;
531
74510d4b 532 /* Returns 0 upon success, -1 upon failure. */
fd8fd11a 533 if (tcflush(port->fd, flags) < 0)
74510d4b
ML
534 return SP_ERR_FAIL;
535#endif
536 return SP_OK;
537}
538
69a3739c
ML
539enum sp_return sp_drain(struct sp_port *port)
540{
541 CHECK_PORT();
542
543#ifdef _WIN32
544 /* Returns non-zero upon success, 0 upon failure. */
545 if (FlushFileBuffers(port->hdl) == 0)
546 return SP_ERR_FAIL;
547#else
548 /* Returns 0 upon success, -1 upon failure. */
549 if (tcdrain(port->fd) < 0)
550 return SP_ERR_FAIL;
551#endif
552
553 return SP_OK;
554}
555
eb6ed20f 556enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
74510d4b
ML
557{
558 CHECK_PORT();
559
560 if (!buf)
561 return SP_ERR_ARG;
562
563#ifdef _WIN32
564 DWORD written = 0;
f92f1f0c 565
74510d4b
ML
566 /* Returns non-zero upon success, 0 upon failure. */
567 if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
568 return SP_ERR_FAIL;
569 return written;
570#else
571 /* Returns the number of bytes written, or -1 upon failure. */
572 ssize_t written = write(port->fd, buf, count);
f92f1f0c 573
74510d4b
ML
574 if (written < 0)
575 return SP_ERR_FAIL;
576 else
f92f1f0c 577 return written;
74510d4b
ML
578#endif
579}
580
eb6ed20f 581enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
74510d4b
ML
582{
583 CHECK_PORT();
584
585 if (!buf)
586 return SP_ERR_ARG;
587
588#ifdef _WIN32
589 DWORD bytes_read = 0;
f92f1f0c 590
74510d4b
ML
591 /* Returns non-zero upon success, 0 upon failure. */
592 if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
593 return SP_ERR_FAIL;
594 return bytes_read;
595#else
596 ssize_t bytes_read;
f92f1f0c 597
74510d4b
ML
598 /* Returns the number of bytes read, or -1 upon failure. */
599 if ((bytes_read = read(port->fd, buf, count)) < 0)
600 return SP_ERR_FAIL;
601 return bytes_read;
602#endif
603}
604
7a6d2196
ML
605#ifdef __linux__
606static enum sp_return get_baudrate(int fd, int *baudrate)
607{
608 void *data;
609
610 if (!(data = malloc(get_termios_size())))
611 return SP_ERR_MEM;
612
613 if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
614 return SP_ERR_FAIL;
615
616 *baudrate = get_termios_speed(data);
617
618 return SP_OK;
619}
620
621static enum sp_return set_baudrate(int fd, int baudrate)
622{
623 void *data;
624
625 if (!(data = malloc(get_termios_size())))
626 return SP_ERR_MEM;
627
628 if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
629 return SP_ERR_FAIL;
630
631 set_termios_speed(data, baudrate);
632
633 if (ioctl(fd, get_termios_set_ioctl(), data) < 0)
634 return SP_ERR_FAIL;
635
636 return SP_OK;
637}
40978c2b
ML
638
639#ifdef USE_TERMIOX
640static enum sp_return get_flow(int fd, int *flow)
641{
642 void *data;
643
644 if (!(data = malloc(get_termiox_size())))
645 return SP_ERR_MEM;
646
647 if (ioctl(fd, TCGETX, data) < 0)
648 return SP_ERR_FAIL;
649
650 *flow = get_termiox_flow(data);
651
652 return SP_OK;
653}
654
655static enum sp_return set_flow(int fd, int flow)
656{
657 void *data;
658
659 if (!(data = malloc(get_termiox_size())))
660 return SP_ERR_MEM;
661
662 if (ioctl(fd, TCGETX, data) < 0)
663 return SP_ERR_FAIL;
664
665 set_termiox_flow(data, flow);
666
667 if (ioctl(fd, TCSETX, data) < 0)
668 return SP_ERR_FAIL;
669
670 return SP_OK;
671}
672#endif /* USE_TERMIOX */
673#endif /* __linux__ */
7a6d2196 674
eb6ed20f
ML
675static enum sp_return get_config(struct sp_port *port, struct port_data *data,
676 struct sp_port_config *config)
8094e4a0 677{
da2748bf 678 unsigned int i;
cbf628c7 679
8094e4a0 680#ifdef _WIN32
e33ab9aa 681 if (!GetCommState(port->hdl, &data->dcb))
d514a26f 682 return SP_ERR_FAIL;
8094e4a0 683
067417af 684 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
e33ab9aa 685 if (data->dcb.BaudRate == std_baudrates[i].index) {
067417af
ML
686 config->baudrate = std_baudrates[i].value;
687 break;
688 }
689 }
690
691 if (i == NUM_STD_BAUDRATES)
692 /* BaudRate field can be either an index or a custom baud rate. */
e33ab9aa 693 config->baudrate = data->dcb.BaudRate;
067417af 694
e33ab9aa 695 config->bits = data->dcb.ByteSize;
067417af 696
e33ab9aa
ML
697 if (data->dcb.fParity)
698 switch (data->dcb.Parity) {
067417af
ML
699 case NOPARITY:
700 config->parity = SP_PARITY_NONE;
701 break;
702 case EVENPARITY:
703 config->parity = SP_PARITY_EVEN;
704 break;
705 case ODDPARITY:
706 config->parity = SP_PARITY_ODD;
707 break;
708 default:
709 config->parity = -1;
710 }
711 else
712 config->parity = SP_PARITY_NONE;
713
e33ab9aa 714 switch (data->dcb.StopBits) {
067417af
ML
715 case ONESTOPBIT:
716 config->stopbits = 1;
717 break;
718 case TWOSTOPBITS:
719 config->stopbits = 2;
720 break;
721 default:
722 config->stopbits = -1;
723 }
724
e33ab9aa 725 switch (data->dcb.fRtsControl) {
eac329d2
UH
726 case RTS_CONTROL_DISABLE:
727 config->rts = SP_RTS_OFF;
728 break;
729 case RTS_CONTROL_ENABLE:
730 config->rts = SP_RTS_ON;
731 break;
732 case RTS_CONTROL_HANDSHAKE:
733 config->rts = SP_RTS_FLOW_CONTROL;
734 break;
735 default:
736 config->rts = -1;
067417af
ML
737 }
738
e33ab9aa 739 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
067417af 740
e33ab9aa 741 switch (data->dcb.fDtrControl) {
eac329d2
UH
742 case DTR_CONTROL_DISABLE:
743 config->dtr = SP_DTR_OFF;
744 break;
745 case DTR_CONTROL_ENABLE:
746 config->dtr = SP_DTR_ON;
747 break;
748 case DTR_CONTROL_HANDSHAKE:
749 config->dtr = SP_DTR_FLOW_CONTROL;
750 break;
751 default:
752 config->dtr = -1;
067417af
ML
753 }
754
e33ab9aa
ML
755 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
756
c6754b45
ML
757 if (data->dcb.fInX) {
758 if (data->dcb.fOutX)
759 config->xon_xoff = SP_XONXOFF_INOUT;
760 else
761 config->xon_xoff = SP_XONXOFF_IN;
762 } else {
763 if (data->dcb.fOutX)
764 config->xon_xoff = SP_XONXOFF_OUT;
765 else
766 config->xon_xoff = SP_XONXOFF_DISABLED;
767 }
768
e33ab9aa
ML
769#else // !_WIN32
770
771 if (tcgetattr(port->fd, &data->term) < 0)
772 return SP_ERR_FAIL;
773
774 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
775 return SP_ERR_FAIL;
40978c2b
ML
776
777#ifdef USE_TERMIOX
778 TRY(get_flow(port->fd, &data->flow));
779#endif
780
067417af 781 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
e33ab9aa 782 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
067417af
ML
783 config->baudrate = std_baudrates[i].value;
784 break;
785 }
786 }
787
31b3a8f5
MH
788 if (i == NUM_STD_BAUDRATES) {
789#ifdef __APPLE__
790 config->baudrate = (int)data->term.c_ispeed;
7a6d2196
ML
791#elif defined(__linux__)
792 TRY(get_baudrate(port->fd, &config->baudrate));
31b3a8f5 793#else
067417af 794 config->baudrate = -1;
31b3a8f5
MH
795#endif
796 }
067417af 797
e33ab9aa 798 switch (data->term.c_cflag & CSIZE) {
067417af
ML
799 case CS8:
800 config->bits = 8;
801 break;
802 case CS7:
803 config->bits = 7;
804 break;
805 case CS6:
806 config->bits = 6;
807 break;
808 case CS5:
809 config->bits = 5;
810 break;
811 default:
812 config->bits = -1;
813 }
814
e33ab9aa 815 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
067417af 816 config->parity = SP_PARITY_NONE;
e33ab9aa 817 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
067417af
ML
818 config->parity = -1;
819 else
e33ab9aa 820 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
067417af 821
e33ab9aa 822 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
067417af 823
e33ab9aa 824 if (data->term.c_cflag & CRTSCTS) {
067417af
ML
825 config->rts = SP_RTS_FLOW_CONTROL;
826 config->cts = SP_CTS_FLOW_CONTROL;
827 } else {
40978c2b
ML
828#ifdef USE_TERMIOX
829 if (data->flow & RTS_FLOW)
830 config->rts = SP_RTS_FLOW_CONTROL;
831 else
832 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
833
834 config->cts = (data->flow & CTS_FLOW) ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
835#else
e33ab9aa 836 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
067417af 837 config->cts = SP_CTS_IGNORE;
40978c2b 838#endif
067417af
ML
839 }
840
40978c2b
ML
841#ifdef USE_TERMIOX
842 if (data->flow & DTR_FLOW)
843 config->dtr = SP_DTR_FLOW_CONTROL;
844 else
845 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
846
847 config->dsr = (data->flow & DSR_FLOW) ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
848#else
e33ab9aa 849 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
067417af 850 config->dsr = SP_DSR_IGNORE;
40978c2b 851#endif
705bdc69 852
e29b93a5
ML
853 if (data->term.c_iflag & IXOFF) {
854 if (data->term.c_iflag & IXON)
855 config->xon_xoff = SP_XONXOFF_INOUT;
856 else
857 config->xon_xoff = SP_XONXOFF_IN;
858 } else {
859 if (data->term.c_iflag & IXON)
860 config->xon_xoff = SP_XONXOFF_OUT;
861 else
862 config->xon_xoff = SP_XONXOFF_DISABLED;
863 }
067417af
ML
864#endif
865
866 return SP_OK;
867}
868
7a6d2196 869static enum sp_return set_config(struct sp_port *port, struct port_data *data,
eb6ed20f 870 const struct sp_port_config *config)
18fc2dd1 871{
e33ab9aa 872 unsigned int i;
31b3a8f5
MH
873#ifdef __APPLE__
874 BAUD_TYPE baud_nonstd;
875
876 baud_nonstd = B0;
877#endif
7a6d2196
ML
878#ifdef __linux__
879 int baud_nonstd = 0;
880#endif
18fc2dd1 881
e33ab9aa 882#ifdef _WIN32
eac329d2 883 if (config->baudrate >= 0) {
e33ab9aa
ML
884 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
885 if (config->baudrate == std_baudrates[i].value) {
886 data->dcb.BaudRate = std_baudrates[i].index;
887 break;
888 }
889 }
18fc2dd1 890
e33ab9aa
ML
891 if (i == NUM_STD_BAUDRATES)
892 data->dcb.BaudRate = config->baudrate;
893 }
18fc2dd1 894
e33ab9aa
ML
895 if (config->bits >= 0)
896 data->dcb.ByteSize = config->bits;
897
898 if (config->parity >= 0) {
899 switch (config->parity) {
900 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
901 case SP_PARITY_NONE:
902 data->dcb.Parity = NOPARITY;
903 break;
904 case SP_PARITY_EVEN:
905 data->dcb.Parity = EVENPARITY;
906 break;
907 case SP_PARITY_ODD:
908 data->dcb.Parity = ODDPARITY;
909 break;
910 default:
911 return SP_ERR_ARG;
912 }
18fc2dd1
ML
913 }
914
e33ab9aa
ML
915 if (config->stopbits >= 0) {
916 switch (config->stopbits) {
917 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
918 case 1:
919 data->dcb.StopBits = ONESTOPBIT;
920 break;
921 case 2:
922 data->dcb.StopBits = TWOSTOPBITS;
923 break;
924 default:
925 return SP_ERR_ARG;
926 }
927 }
928
929 if (config->rts >= 0) {
930 switch (config->rts) {
931 case SP_RTS_OFF:
932 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
933 break;
934 case SP_RTS_ON:
935 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
936 break;
937 case SP_RTS_FLOW_CONTROL:
938 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
939 break;
940 default:
941 return SP_ERR_ARG;
942 }
943 }
944
945 if (config->cts >= 0) {
946 switch (config->cts) {
947 case SP_CTS_IGNORE:
948 data->dcb.fOutxCtsFlow = FALSE;
949 break;
950 case SP_CTS_FLOW_CONTROL:
951 data->dcb.fOutxCtsFlow = TRUE;
952 break;
953 default:
954 return SP_ERR_ARG;
955 }
956 }
957
958 if (config->dtr >= 0) {
959 switch (config->dtr) {
960 case SP_DTR_OFF:
961 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
962 break;
963 case SP_DTR_ON:
964 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
965 break;
966 case SP_DTR_FLOW_CONTROL:
967 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
968 break;
969 default:
970 return SP_ERR_ARG;
971 }
972 }
973
974 if (config->dsr >= 0) {
975 switch (config->dsr) {
976 case SP_DSR_IGNORE:
977 data->dcb.fOutxDsrFlow = FALSE;
978 break;
979 case SP_DSR_FLOW_CONTROL:
980 data->dcb.fOutxDsrFlow = TRUE;
981 break;
982 default:
983 return SP_ERR_ARG;
984 }
18fc2dd1
ML
985 }
986
e33ab9aa
ML
987 if (config->xon_xoff >= 0) {
988 switch (config->xon_xoff) {
989 case SP_XONXOFF_DISABLED:
990 data->dcb.fInX = FALSE;
991 data->dcb.fOutX = FALSE;
992 break;
993 case SP_XONXOFF_IN:
994 data->dcb.fInX = TRUE;
995 data->dcb.fOutX = FALSE;
996 break;
997 case SP_XONXOFF_OUT:
998 data->dcb.fInX = FALSE;
999 data->dcb.fOutX = TRUE;
1000 break;
1001 case SP_XONXOFF_INOUT:
1002 data->dcb.fInX = TRUE;
1003 data->dcb.fOutX = TRUE;
1004 break;
1005 default:
1006 return SP_ERR_ARG;
1007 }
1008 }
1009
1010 if (!SetCommState(port->hdl, &data->dcb))
1011 return SP_ERR_FAIL;
1012
31b3a8f5 1013#else /* !_WIN32 */
e33ab9aa 1014
7a6d2196
ML
1015 int controlbits;
1016
eac329d2 1017 if (config->baudrate >= 0) {
e33ab9aa
ML
1018 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1019 if (config->baudrate == std_baudrates[i].value) {
1020 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
1021 return SP_ERR_FAIL;
1022
1023 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
1024 return SP_ERR_FAIL;
1025 break;
1026 }
1027 }
1028
31b3a8f5
MH
1029 /* Non-standard baud rate */
1030 if (i == NUM_STD_BAUDRATES) {
1031#ifdef __APPLE__
24abdb68 1032 /* Set "dummy" baud rate. */
31b3a8f5
MH
1033 if (cfsetspeed(&data->term, B9600) < 0)
1034 return SP_ERR_FAIL;
1035 baud_nonstd = config->baudrate;
7a6d2196
ML
1036#elif defined(__linux__)
1037 baud_nonstd = 1;
31b3a8f5 1038#else
e33ab9aa 1039 return SP_ERR_ARG;
31b3a8f5
MH
1040#endif
1041 }
e33ab9aa
ML
1042 }
1043
1044 if (config->bits >= 0) {
1045 data->term.c_cflag &= ~CSIZE;
1046 switch (config->bits) {
1047 case 8:
1048 data->term.c_cflag |= CS8;
1049 break;
1050 case 7:
1051 data->term.c_cflag |= CS7;
1052 break;
1053 case 6:
1054 data->term.c_cflag |= CS6;
1055 break;
23922313
UH
1056 case 5:
1057 data->term.c_cflag |= CS5;
1058 break;
e33ab9aa
ML
1059 default:
1060 return SP_ERR_ARG;
1061 }
1062 }
1063
1064 if (config->parity >= 0) {
1065 data->term.c_iflag &= ~IGNPAR;
1066 data->term.c_cflag &= ~(PARENB | PARODD);
1067 switch (config->parity) {
1068 case SP_PARITY_NONE:
1069 data->term.c_iflag |= IGNPAR;
1070 break;
1071 case SP_PARITY_EVEN:
1072 data->term.c_cflag |= PARENB;
1073 break;
1074 case SP_PARITY_ODD:
1075 data->term.c_cflag |= PARENB | PARODD;
1076 break;
1077 default:
1078 return SP_ERR_ARG;
1079 }
1080 }
1081
1082 if (config->stopbits >= 0) {
1083 data->term.c_cflag &= ~CSTOPB;
1084 switch (config->stopbits) {
1085 case 1:
1086 data->term.c_cflag &= ~CSTOPB;
1087 break;
1088 case 2:
1089 data->term.c_cflag |= CSTOPB;
1090 break;
1091 default:
1092 return SP_ERR_ARG;
1093 }
1094 }
1095
eac329d2 1096 if (config->rts >= 0 || config->cts >= 0) {
40978c2b
ML
1097#ifdef USE_TERMIOX
1098 data->flow &= ~(RTS_FLOW | CTS_FLOW);
1099 switch (config->rts) {
24abdb68
UH
1100 case SP_RTS_OFF:
1101 case SP_RTS_ON:
1102 controlbits = TIOCM_RTS;
1103 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1104 return SP_ERR_FAIL;
1105 break;
1106 case SP_RTS_FLOW_CONTROL:
1107 data->flow |= RTS_FLOW;
1108 break;
1109 default:
1110 break;
40978c2b
ML
1111 }
1112 if (config->cts == SP_CTS_FLOW_CONTROL)
1113 data->flow |= CTS_FLOW;
e33ab9aa 1114
40978c2b
ML
1115 if (data->flow & (RTS_FLOW | CTS_FLOW))
1116 data->term.c_iflag |= CRTSCTS;
1117 else
1118 data->term.c_iflag &= ~CRTSCTS;
1119#else
1120 /* Asymmetric use of RTS/CTS not supported. */
e33ab9aa
ML
1121 if (data->term.c_iflag & CRTSCTS) {
1122 /* Flow control can only be disabled for both RTS & CTS together. */
1123 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
1124 if (config->cts != SP_CTS_IGNORE)
1125 return SP_ERR_ARG;
1126 }
1127 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
1128 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
1129 return SP_ERR_ARG;
1130 }
1131 } else {
1132 /* Flow control can only be enabled for both RTS & CTS together. */
1133 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
1134 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
1135 return SP_ERR_ARG;
1136 }
1137
1138 if (config->rts >= 0) {
1139 if (config->rts == SP_RTS_FLOW_CONTROL) {
1140 data->term.c_iflag |= CRTSCTS;
1141 } else {
7a6d2196 1142 controlbits = TIOCM_RTS;
e33ab9aa
ML
1143 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
1144 &controlbits) < 0)
1145 return SP_ERR_FAIL;
1146 }
1147 }
40978c2b 1148#endif
e33ab9aa
ML
1149 }
1150
eac329d2 1151 if (config->dtr >= 0 || config->dsr >= 0) {
40978c2b
ML
1152#ifdef USE_TERMIOX
1153 data->flow &= ~(DTR_FLOW | DSR_FLOW);
1154 switch (config->dtr) {
24abdb68
UH
1155 case SP_DTR_OFF:
1156 case SP_DTR_ON:
1157 controlbits = TIOCM_DTR;
1158 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
1159 return SP_ERR_FAIL;
1160 break;
1161 case SP_DTR_FLOW_CONTROL:
1162 data->flow |= DTR_FLOW;
1163 break;
1164 default:
1165 break;
40978c2b
ML
1166 }
1167 if (config->dsr == SP_DSR_FLOW_CONTROL)
1168 data->flow |= DSR_FLOW;
1169#else
1170 /* DTR/DSR flow control not supported. */
e33ab9aa
ML
1171 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
1172 return SP_ERR_ARG;
1173
1174 if (config->dtr >= 0) {
7a6d2196 1175 controlbits = TIOCM_DTR;
e33ab9aa
ML
1176 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
1177 &controlbits) < 0)
1178 return SP_ERR_FAIL;
1179 }
40978c2b 1180#endif
e33ab9aa
ML
1181 }
1182
1183 if (config->xon_xoff >= 0) {
1184 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
1185 switch (config->xon_xoff) {
1186 case SP_XONXOFF_DISABLED:
1187 break;
1188 case SP_XONXOFF_IN:
1189 data->term.c_iflag |= IXOFF;
1190 break;
1191 case SP_XONXOFF_OUT:
1192 data->term.c_iflag |= IXON | IXANY;
1193 break;
1194 case SP_XONXOFF_INOUT:
1195 data->term.c_iflag |= IXON | IXOFF | IXANY;
1196 break;
1197 default:
1198 return SP_ERR_ARG;
1199 }
1200 }
1201
1202 if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
1203 return SP_ERR_FAIL;
31b3a8f5
MH
1204
1205#ifdef __APPLE__
1206 if (baud_nonstd != B0) {
1207 if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
1208 return SP_ERR_FAIL;
1209 /* Set baud rates in data->term to correct, but incompatible
1210 * with tcsetattr() value, same as delivered by tcgetattr(). */
1211 if (cfsetspeed(&data->term, baud_nonstd) < 0)
1212 return SP_ERR_FAIL;
1213 }
7a6d2196
ML
1214#elif defined(__linux__)
1215 if (baud_nonstd)
1216 TRY(set_baudrate(port->fd, config->baudrate));
40978c2b
ML
1217#ifdef USE_TERMIOX
1218 TRY(set_flow(port->fd, data->flow));
1219#endif
7a6d2196 1220#endif
31b3a8f5
MH
1221
1222#endif /* !_WIN32 */
e33ab9aa
ML
1223
1224 return SP_OK;
1225}
1226
eb6ed20f 1227enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
e33ab9aa 1228{
8f189c4c 1229 struct port_data data;
e33ab9aa
ML
1230 struct sp_port_config prev_config;
1231
823690ae
ML
1232 CHECK_PORT();
1233
1234 if (!config)
1235 return SP_ERR_ARG;
1236
e33ab9aa
ML
1237 TRY(get_config(port, &data, &prev_config));
1238 TRY(set_config(port, &data, config));
74510d4b
ML
1239
1240 return SP_OK;
1241}
1242
eb6ed20f 1243#define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \
8f189c4c 1244 struct port_data data; \
e33ab9aa 1245 struct sp_port_config config; \
823690ae 1246 CHECK_PORT(); \
e33ab9aa
ML
1247 TRY(get_config(port, &data, &config)); \
1248 config.x = x; \
1249 TRY(set_config(port, &data, &config)); \
9069c2fb
ML
1250 return SP_OK; \
1251}
1252
eb6ed20f
ML
1253CREATE_SETTER(baudrate, int)
1254CREATE_SETTER(bits, int)
1255CREATE_SETTER(parity, enum sp_parity)
1256CREATE_SETTER(stopbits, int)
1257CREATE_SETTER(rts, enum sp_rts)
1258CREATE_SETTER(cts, enum sp_cts)
1259CREATE_SETTER(dtr, enum sp_dtr)
1260CREATE_SETTER(dsr, enum sp_dsr)
1261CREATE_SETTER(xon_xoff, enum sp_xonxoff)
1262
1263enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
e33ab9aa 1264{
8f189c4c 1265 struct port_data data;
e33ab9aa
ML
1266 struct sp_port_config config;
1267
823690ae
ML
1268 CHECK_PORT();
1269
e33ab9aa
ML
1270 TRY(get_config(port, &data, &config));
1271
1272 if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
1273 config.xon_xoff = SP_XONXOFF_INOUT;
1274 else
1275 config.xon_xoff = SP_XONXOFF_DISABLED;
1276
1277 if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
1278 config.rts = SP_RTS_FLOW_CONTROL;
1279 config.cts = SP_CTS_FLOW_CONTROL;
1280 } else {
1281 if (config.rts == SP_RTS_FLOW_CONTROL)
1282 config.rts = SP_RTS_ON;
1283 config.cts = SP_CTS_IGNORE;
1284 }
1285
1286 if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
1287 config.dtr = SP_DTR_FLOW_CONTROL;
1288 config.dsr = SP_DSR_FLOW_CONTROL;
1289 } else {
1290 if (config.dtr == SP_DTR_FLOW_CONTROL)
1291 config.dtr = SP_DTR_ON;
1292 config.dsr = SP_DSR_IGNORE;
1293 }
1294
1295 TRY(set_config(port, &data, &config));
1296
1297 return SP_OK;
1298}
1299
8cf7c697
ML
1300enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals)
1301{
1302 CHECK_PORT();
1303
1304 if (!signals)
1305 return SP_ERR_ARG;
1306
1307 *signals = 0;
1308#ifdef _WIN32
1309 DWORD bits;
1310 if (GetCommModemStatus(port->hdl, &bits) == 0)
1311 return SP_ERR_FAIL;
1312 if (bits & MS_CTS_ON)
1313 *signals |= SP_SIG_CTS;
1314 if (bits & MS_DSR_ON)
1315 *signals |= SP_SIG_DSR;
1316 if (bits & MS_RING_ON)
1317 *signals |= SP_SIG_DCD;
1318 if (bits & MS_RLSD_ON)
1319 *signals |= SP_SIG_RI;
1320#else
1321 int bits;
1322 if (ioctl(port->fd, TIOCMGET, &bits) < 0)
1323 return SP_ERR_FAIL;
1324 if (bits & TIOCM_CTS)
1325 *signals |= SP_SIG_CTS;
1326 if (bits & TIOCM_DSR)
1327 *signals |= SP_SIG_DSR;
1328 if (bits & TIOCM_CAR)
1329 *signals |= SP_SIG_DCD;
1330 if (bits & TIOCM_RNG)
1331 *signals |= SP_SIG_RI;
1332#endif
1333 return SP_OK;
1334}
1335
90cc3ee6
ML
1336enum sp_return sp_start_break(struct sp_port *port)
1337{
1338 CHECK_PORT();
1339#ifdef _WIN32
1340 if (SetCommBreak(port->hdl) == 0)
1341 return SP_ERR_FAIL;
1342#else
1343 if (ioctl(port->fd, TIOCSBRK, 1) < 0)
1344 return SP_ERR_FAIL;
1345#endif
1346
1347 return SP_OK;
1348}
1349
1350enum sp_return sp_end_break(struct sp_port *port)
1351{
1352 CHECK_PORT();
1353#ifdef _WIN32
1354 if (ClearCommBreak(port->hdl) == 0)
1355 return SP_ERR_FAIL;
1356#else
1357 if (ioctl(port->fd, TIOCCBRK, 1) < 0)
1358 return SP_ERR_FAIL;
1359#endif
1360
1361 return SP_OK;
1362}
1363
74510d4b
ML
1364int sp_last_error_code(void)
1365{
1366#ifdef _WIN32
1367 return GetLastError();
1368#else
1369 return errno;
1370#endif
1371}
1372
74510d4b
ML
1373char *sp_last_error_message(void)
1374{
1375#ifdef _WIN32
1376 LPVOID message;
1377 DWORD error = GetLastError();
1378
1379 FormatMessage(
1380 FORMAT_MESSAGE_ALLOCATE_BUFFER |
1381 FORMAT_MESSAGE_FROM_SYSTEM |
1382 FORMAT_MESSAGE_IGNORE_INSERTS,
1383 NULL,
1384 error,
1385 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1386 (LPTSTR) &message,
1387 0, NULL );
1388
1389 return message;
1390#else
1391 return strerror(errno);
1392#endif
1393}
1394
74510d4b
ML
1395void sp_free_error_message(char *message)
1396{
1397#ifdef _WIN32
1398 LocalFree(message);
64eec30d
ML
1399#else
1400 (void)message;
74510d4b
ML
1401#endif
1402}