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