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