]> sigrok.org Git - libserialport.git/blame - serialport.c
TIOCINQ/TIOCOUTQ is not available everywhere.
[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>
863b35e6
ML
30#include <stdio.h>
31#include <stdarg.h>
74510d4b
ML
32#ifdef _WIN32
33#include <windows.h>
3b63f34d 34#include <tchar.h>
767c5ba8 35#include <stdio.h>
74510d4b 36#else
8fbf876d 37#include <limits.h>
74510d4b
ML
38#include <termios.h>
39#include <sys/ioctl.h>
e3dcf906
ML
40#include <sys/time.h>
41#include <limits.h>
74510d4b 42#endif
3b63f34d 43#ifdef __APPLE__
1ebf4347
ML
44#include <IOKit/IOKitLib.h>
45#include <IOKit/serial/IOSerialKeys.h>
31b3a8f5 46#include <IOKit/serial/ioss.h>
1ebf4347 47#include <sys/syslimits.h>
3b63f34d
ML
48#endif
49#ifdef __linux__
50#include "libudev.h"
4b97c9fc 51#include "linux/serial.h"
7a6d2196 52#include "linux_termios.h"
6c8716e9
UH
53
54/* TCGETX/TCSETX is not available everywhere. */
40978c2b 55#if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX)
68ec29db 56#define USE_TERMIOX
40978c2b 57#endif
3b63f34d 58#endif
74510d4b 59
6c8716e9
UH
60/* TIOCINQ/TIOCOUTQ is not available everywhere. */
61#if !defined(TIOCINQ) && defined(FIONREAD)
62#define TIOCINQ FIONREAD
63#endif
64#if !defined(TIOCOUTQ) && defined(FIONWRITE)
65#define TIOCOUTQ FIONWRITE
66#endif
67
71c8a9b9
UH
68#ifndef _WIN32
69#include "linux_termios.h"
70#endif
71
f6a1fb65 72#include "libserialport.h"
74510d4b 73
1c5aae9d
ML
74struct sp_port {
75 char *name;
76#ifdef _WIN32
77 HANDLE hdl;
e3dcf906 78 COMMTIMEOUTS timeouts;
a3cb91f5 79 OVERLAPPED write_ovl;
e3dcf906 80 OVERLAPPED read_ovl;
0765af56 81 BYTE pending_byte;
a3cb91f5 82 BOOL writing;
1c5aae9d
ML
83#else
84 int fd;
85#endif
86};
87
9b1502ef
ML
88struct sp_port_config {
89 int baudrate;
90 int bits;
91 enum sp_parity parity;
92 int stopbits;
93 enum sp_rts rts;
94 enum sp_cts cts;
95 enum sp_dtr dtr;
96 enum sp_dsr dsr;
97 enum sp_xonxoff xon_xoff;
98};
99
8f189c4c 100struct port_data {
8094e4a0
ML
101#ifdef _WIN32
102 DCB dcb;
103#else
104 struct termios term;
824dcb45 105 int controlbits;
68ec29db 106 int termiox_supported;
40978c2b
ML
107 int flow;
108#endif
8094e4a0
ML
109};
110
da2748bf
ML
111/* Standard baud rates. */
112#ifdef _WIN32
113#define BAUD_TYPE DWORD
114#define BAUD(n) {CBR_##n, n}
115#else
116#define BAUD_TYPE speed_t
117#define BAUD(n) {B##n, n}
118#endif
119
120struct std_baudrate {
121 BAUD_TYPE index;
122 int value;
123};
124
125const struct std_baudrate std_baudrates[] = {
126#ifdef _WIN32
127 /*
128 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
129 * have documented CBR_* macros.
130 */
131 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
132 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
eac329d2 133 BAUD(115200), BAUD(128000), BAUD(256000),
da2748bf 134#else
eac329d2
UH
135 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
136 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
137 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
138 BAUD(230400),
da2748bf 139#if !defined(__APPLE__) && !defined(__OpenBSD__)
eac329d2 140 BAUD(460800),
da2748bf
ML
141#endif
142#endif
143};
144
863b35e6
ML
145void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
146
da2748bf
ML
147#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
148#define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
149
92f756f8
ML
150/* Debug output macros. */
151#define DEBUG(fmt, ...) do { if (sp_debug_handler) sp_debug_handler(fmt ".\n", ##__VA_ARGS__); } while (0)
152#define DEBUG_ERROR(err, msg) DEBUG("%s returning " #err ": " msg, __func__)
153#define DEBUG_FAIL(msg) do { \
154 char *errmsg = sp_last_error_message(); \
155 DEBUG("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
156 sp_free_error_message(errmsg); \
157} while (0);
158#define RETURN() do { DEBUG("%s returning", __func__); return; } while(0)
159#define RETURN_CODE(x) do { DEBUG("%s returning " #x, __func__); return x; } while (0)
160#define RETURN_CODEVAL(x) do { \
161 switch (x) { \
162 case SP_OK: RETURN_CODE(SP_OK); \
163 case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \
164 case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \
165 case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \
166 case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \
167 } \
168} while (0)
169#define RETURN_OK() RETURN_CODE(SP_OK);
170#define RETURN_ERROR(err, msg) do { DEBUG_ERROR(err, msg); return err; } while (0)
171#define RETURN_FAIL(msg) do { DEBUG_FAIL(msg); return SP_ERR_FAIL; } while (0)
172#define RETURN_VALUE(fmt, x) do { DEBUG("%s returning " fmt, __func__, x); return x; } while (0)
173#define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
aac4d7f2 174#define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
92f756f8
ML
175#define TRACE(fmt, ...) DEBUG("%s(" fmt ") called", __func__, ##__VA_ARGS__)
176
64690702
ML
177#define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0)
178
348e23cc 179/* Helper functions. */
348e23cc 180static struct sp_port **list_append(struct sp_port **list, const char *portname);
eb6ed20f
ML
181static enum sp_return get_config(struct sp_port *port, struct port_data *data,
182 struct sp_port_config *config);
183static enum sp_return set_config(struct sp_port *port, struct port_data *data,
184 const struct sp_port_config *config);
80186526 185
eb6ed20f 186enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
d54e9004
ML
187{
188 struct sp_port *port;
5919c913 189 int len;
d54e9004 190
c33efc48
ML
191 TRACE("%s, %p", portname, port_ptr);
192
32b5ac05 193 if (!port_ptr)
c33efc48 194 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
32b5ac05 195
77f262c4
ML
196 *port_ptr = NULL;
197
d4babed2 198 if (!portname)
c33efc48 199 RETURN_ERROR(SP_ERR_ARG, "Null port name");
d4babed2 200
ea667be7
ML
201 DEBUG("Building structure for port %s", portname);
202
d54e9004 203 if (!(port = malloc(sizeof(struct sp_port))))
c33efc48 204 RETURN_ERROR(SP_ERR_MEM, "Port structure malloc failed");
d54e9004 205
5919c913
ML
206 len = strlen(portname) + 1;
207
eac329d2 208 if (!(port->name = malloc(len))) {
d54e9004 209 free(port);
c33efc48 210 RETURN_ERROR(SP_ERR_MEM, "Port name malloc failed");
d54e9004
ML
211 }
212
213 memcpy(port->name, portname, len);
214
8f471c66
ML
215#ifdef _WIN32
216 port->hdl = INVALID_HANDLE_VALUE;
217#else
218 port->fd = -1;
219#endif
220
77f262c4
ML
221 *port_ptr = port;
222
c33efc48 223 RETURN_OK();
d54e9004
ML
224}
225
1c5aae9d
ML
226char *sp_get_port_name(const struct sp_port *port)
227{
228 TRACE("%p", port);
229
230 if (!port)
231 return NULL;
232
233 RETURN_VALUE("%s", port->name);
234}
235
3c126654
ML
236enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr)
237{
00d8c56d 238 TRACE("%p, %p", port, result_ptr);
3c126654
ML
239
240 if (!port)
241 RETURN_ERROR(SP_ERR_ARG, "Null port");
242
243#ifdef _WIN32
244 HANDLE *handle_ptr = result_ptr;
245 *handle_ptr = port->hdl;
246#else
247 int *fd_ptr = result_ptr;
248 *fd_ptr = port->fd;
249#endif
250
251 RETURN_OK();
252}
253
eb6ed20f 254enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
32b5ac05 255{
c33efc48
ML
256 TRACE("%p, %p", port, copy_ptr);
257
32b5ac05 258 if (!copy_ptr)
c33efc48 259 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
32b5ac05
ML
260
261 *copy_ptr = NULL;
262
c33efc48
ML
263 if (!port)
264 RETURN_ERROR(SP_ERR_ARG, "Null port");
265
266 if (!port->name)
267 RETURN_ERROR(SP_ERR_ARG, "Null port name");
32b5ac05 268
ea667be7
ML
269 DEBUG("Copying port structure");
270
c33efc48 271 RETURN_VALUE("%p", sp_get_port_by_name(port->name, copy_ptr));
32b5ac05
ML
272}
273
e3b2f7a4
ML
274void sp_free_port(struct sp_port *port)
275{
c33efc48
ML
276 TRACE("%p", port);
277
00d8c56d 278 if (!port) {
c33efc48
ML
279 DEBUG("Null port");
280 RETURN();
281 }
e3b2f7a4 282
ea667be7
ML
283 DEBUG("Freeing port structure");
284
e3b2f7a4
ML
285 if (port->name)
286 free(port->name);
287
288 free(port);
c33efc48
ML
289
290 RETURN();
e3b2f7a4
ML
291}
292
348e23cc 293static struct sp_port **list_append(struct sp_port **list, const char *portname)
3b63f34d
ML
294{
295 void *tmp;
296 unsigned int count;
f92f1f0c 297
3b63f34d 298 for (count = 0; list[count]; count++);
d54e9004 299 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
3b63f34d
ML
300 goto fail;
301 list = tmp;
77f262c4 302 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
3b63f34d 303 goto fail;
db2794ce 304 list[count + 1] = NULL;
3b63f34d 305 return list;
f92f1f0c 306
3b63f34d
ML
307fail:
308 sp_free_port_list(list);
309 return NULL;
310}
311
eb6ed20f 312enum sp_return sp_list_ports(struct sp_port ***list_ptr)
3b63f34d 313{
d54e9004 314 struct sp_port **list;
6b93ede4 315 int ret = SP_ERR_SUPP;
24c1a4bb 316
c33efc48
ML
317 TRACE("%p", list_ptr);
318
dec10e31
ML
319 if (!list_ptr)
320 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
321
ea667be7
ML
322 DEBUG("Enumerating ports");
323
d54e9004 324 if (!(list = malloc(sizeof(struct sp_port **))))
c33efc48 325 RETURN_ERROR(SP_ERR_MEM, "Port list malloc failed");
24c1a4bb
ML
326
327 list[0] = NULL;
3b63f34d
ML
328
329#ifdef _WIN32
330 HKEY key;
bdfb5b8c
ML
331 TCHAR *value, *data;
332 DWORD max_value_len, max_data_size, max_data_len;
333 DWORD value_len, data_size, data_len;
3b63f34d 334 DWORD type, index = 0;
8b532d9c
ML
335 char *name;
336 int name_len;
3b63f34d 337
6b93ede4
ML
338 ret = SP_OK;
339
ea667be7 340 DEBUG("Opening registry key");
3b63f34d 341 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
eac329d2 342 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
c33efc48 343 SET_FAIL(ret, "RegOpenKeyEx() failed");
77f262c4
ML
344 goto out_done;
345 }
ea667be7 346 DEBUG("Querying registry key value and data sizes");
3b63f34d 347 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
eac329d2 348 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
c33efc48 349 SET_FAIL(ret, "RegQueryInfoKey() failed");
3b63f34d 350 goto out_close;
77f262c4 351 }
3b63f34d 352 max_data_len = max_data_size / sizeof(TCHAR);
eac329d2 353 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
c33efc48 354 SET_ERROR(ret, SP_ERR_MEM, "registry value malloc failed");
3b63f34d 355 goto out_close;
77f262c4 356 }
eac329d2 357 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
c33efc48 358 SET_ERROR(ret, SP_ERR_MEM, "registry data malloc failed");
bdfb5b8c 359 goto out_free_value;
77f262c4 360 }
ea667be7 361 DEBUG("Iterating over values");
3b63f34d 362 while (
d9573bad 363 value_len = max_value_len + 1,
3b63f34d 364 data_size = max_data_size,
bdfb5b8c 365 RegEnumValue(key, index, value, &value_len,
3b63f34d
ML
366 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
367 {
368 data_len = data_size / sizeof(TCHAR);
369 data[data_len] = '\0';
8b532d9c
ML
370#ifdef UNICODE
371 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
372#else
373 name_len = data_len + 1;
374#endif
eac329d2 375 if (!(name = malloc(name_len))) {
c33efc48 376 SET_ERROR(ret, SP_ERR_MEM, "registry port name malloc failed");
8b532d9c 377 goto out;
77f262c4 378 }
8b532d9c
ML
379#ifdef UNICODE
380 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
381#else
382 strcpy(name, data);
383#endif
c33efc48
ML
384 if (type == REG_SZ) {
385 DEBUG("Found port %s", name);
386 if (!(list = list_append(list, name))) {
387 SET_ERROR(ret, SP_ERR_MEM, "list append failed");
388 goto out;
389 }
77f262c4 390 }
3b63f34d
ML
391 index++;
392 }
393out:
394 free(data);
bdfb5b8c
ML
395out_free_value:
396 free(value);
3b63f34d
ML
397out_close:
398 RegCloseKey(key);
77f262c4 399out_done:
3b63f34d
ML
400#endif
401#ifdef __APPLE__
402 mach_port_t master;
403 CFMutableDictionaryRef classes;
404 io_iterator_t iter;
405 char *path;
406 io_object_t port;
407 CFTypeRef cf_path;
408 Boolean result;
409
6b93ede4
ML
410 ret = SP_OK;
411
ea667be7 412 DEBUG("Getting IOKit master port");
eac329d2 413 if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
c33efc48 414 SET_FAIL(ret, "IOMasterPort() failed");
77f262c4
ML
415 goto out_done;
416 }
3b63f34d 417
ea667be7 418 DEBUG("Creating matching dictionary");
eac329d2 419 if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
c33efc48 420 SET_FAIL(ret, "IOServiceMatching() failed");
77f262c4
ML
421 goto out_done;
422 }
3b63f34d
ML
423
424 CFDictionarySetValue(classes,
425 CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
426
ea667be7 427 DEBUG("Getting matching services");
eac329d2 428 if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
c33efc48 429 SET_FAIL(ret, "IOServiceGetMatchingServices() failed");
77f262c4
ML
430 goto out_done;
431 }
3b63f34d 432
eac329d2 433 if (!(path = malloc(PATH_MAX))) {
c33efc48 434 SET_ERROR(ret, SP_ERR_MEM, "device path malloc failed");
3b63f34d 435 goto out_release;
77f262c4 436 }
3b63f34d 437
ea667be7 438 DEBUG("Iterating over results");
1ebf4347 439 while ((port = IOIteratorNext(iter))) {
3b63f34d
ML
440 cf_path = IORegistryEntryCreateCFProperty(port,
441 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
442 if (cf_path) {
443 result = CFStringGetCString(cf_path,
444 path, PATH_MAX, kCFStringEncodingASCII);
445 CFRelease(cf_path);
c33efc48
ML
446 if (result) {
447 DEBUG("Found port %s", path);
448 if (!(list = list_append(list, path))) {
449 SET_ERROR(ret, SP_ERR_MEM, "list append failed");
450 IOObjectRelease(port);
451 goto out;
452 }
77f262c4 453 }
3b63f34d
ML
454 }
455 IOObjectRelease(port);
456 }
3b63f34d
ML
457out:
458 free(path);
459out_release:
460 IOObjectRelease(iter);
77f262c4 461out_done:
3b63f34d
ML
462#endif
463#ifdef __linux__
464 struct udev *ud;
465 struct udev_enumerate *ud_enumerate;
466 struct udev_list_entry *ud_list;
467 struct udev_list_entry *ud_entry;
468 const char *path;
08fe0bdb 469 struct udev_device *ud_dev, *ud_parent;
3b63f34d 470 const char *name;
4b97c9fc
ML
471 const char *driver;
472 int fd, ioctl_result;
473 struct serial_struct serial_info;
3b63f34d 474
6b93ede4
ML
475 ret = SP_OK;
476
ea667be7 477 DEBUG("Enumerating tty devices");
3b63f34d
ML
478 ud = udev_new();
479 ud_enumerate = udev_enumerate_new(ud);
480 udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
481 udev_enumerate_scan_devices(ud_enumerate);
482 ud_list = udev_enumerate_get_list_entry(ud_enumerate);
ea667be7 483 DEBUG("Iterating over results");
eac329d2 484 udev_list_entry_foreach(ud_entry, ud_list) {
3b63f34d 485 path = udev_list_entry_get_name(ud_entry);
ea667be7 486 DEBUG("Found device %s", path);
3b63f34d 487 ud_dev = udev_device_new_from_syspath(ud, path);
08fe0bdb
ML
488 /* If there is no parent device, this is a virtual tty. */
489 ud_parent = udev_device_get_parent(ud_dev);
eac329d2 490 if (ud_parent == NULL) {
ea667be7 491 DEBUG("No parent device, assuming virtual tty");
08fe0bdb
ML
492 udev_device_unref(ud_dev);
493 continue;
494 }
3b63f34d 495 name = udev_device_get_devnode(ud_dev);
4b97c9fc
ML
496 /* The serial8250 driver has a hardcoded number of ports.
497 * The only way to tell which actually exist on a given system
498 * is to try to open them and make an ioctl call. */
499 driver = udev_device_get_driver(ud_parent);
eac329d2 500 if (driver && !strcmp(driver, "serial8250")) {
ea667be7
ML
501 DEBUG("serial8250 device, attempting to open");
502 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
503 DEBUG("open failed, skipping");
4b97c9fc 504 goto skip;
ea667be7 505 }
4b97c9fc
ML
506 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
507 close(fd);
ea667be7
ML
508 if (ioctl_result != 0) {
509 DEBUG("ioctl failed, skipping");
4b97c9fc 510 goto skip;
ea667be7
ML
511 }
512 if (serial_info.type == PORT_UNKNOWN) {
513 DEBUG("port type is unknown, skipping");
4b97c9fc 514 goto skip;
ea667be7 515 }
4b97c9fc 516 }
c33efc48 517 DEBUG("Found port %s", name);
348e23cc 518 list = list_append(list, name);
4b97c9fc 519skip:
3b63f34d 520 udev_device_unref(ud_dev);
eac329d2 521 if (!list) {
c33efc48 522 SET_ERROR(ret, SP_ERR_MEM, "list append failed");
3b63f34d 523 goto out;
77f262c4 524 }
3b63f34d
ML
525 }
526out:
527 udev_enumerate_unref(ud_enumerate);
528 udev_unref(ud);
3b63f34d 529#endif
77f262c4 530
6b93ede4
ML
531 switch (ret) {
532 case SP_OK:
77f262c4 533 *list_ptr = list;
c33efc48 534 RETURN_OK();
6b93ede4
ML
535 case SP_ERR_SUPP:
536 DEBUG_ERROR(SP_ERR_SUPP, "Enumeration not supported on this platform.");
537 default:
77f262c4
ML
538 if (list)
539 sp_free_port_list(list);
77f262c4 540 *list_ptr = NULL;
c33efc48 541 return ret;
77f262c4 542 }
3b63f34d
ML
543}
544
d54e9004 545void sp_free_port_list(struct sp_port **list)
3b63f34d
ML
546{
547 unsigned int i;
f92f1f0c 548
c33efc48
ML
549 TRACE("%p", list);
550
dec10e31
ML
551 if (!list) {
552 DEBUG("Null list");
553 RETURN();
554 }
555
ea667be7
ML
556 DEBUG("Freeing port list");
557
3b63f34d 558 for (i = 0; list[i]; i++)
e3b2f7a4 559 sp_free_port(list[i]);
3b63f34d 560 free(list);
c33efc48
ML
561
562 RETURN();
3b63f34d
ML
563}
564
c33efc48
ML
565#define CHECK_PORT() do { \
566 if (port == NULL) \
567 RETURN_ERROR(SP_ERR_ARG, "Null port"); \
dec10e31
ML
568 if (port->name == NULL) \
569 RETURN_ERROR(SP_ERR_ARG, "Null port name"); \
570} while (0)
571#ifdef _WIN32
572#define CHECK_PORT_HANDLE() do { \
c33efc48
ML
573 if (port->hdl == INVALID_HANDLE_VALUE) \
574 RETURN_ERROR(SP_ERR_ARG, "Invalid port handle"); \
dec10e31 575} while (0)
74510d4b 576#else
dec10e31 577#define CHECK_PORT_HANDLE() do { \
c33efc48
ML
578 if (port->fd < 0) \
579 RETURN_ERROR(SP_ERR_ARG, "Invalid port fd"); \
dec10e31 580} while (0)
74510d4b 581#endif
dec10e31
ML
582#define CHECK_OPEN_PORT() do { \
583 CHECK_PORT(); \
584 CHECK_PORT_HANDLE(); \
585} while (0)
74510d4b 586
eb6ed20f 587enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
74510d4b 588{
bccc7c9f
ML
589 struct port_data data;
590 struct sp_port_config config;
591 enum sp_return ret;
592
00d8c56d 593 TRACE("%p, 0x%x", port, flags);
c33efc48 594
dec10e31
ML
595 CHECK_PORT();
596
e3dcf906 597 if (flags > (SP_MODE_READ | SP_MODE_WRITE))
dec10e31 598 RETURN_ERROR(SP_ERR_ARG, "Invalid flags");
74510d4b 599
ea667be7
ML
600 DEBUG("Opening port %s", port->name);
601
74510d4b
ML
602#ifdef _WIN32
603 DWORD desired_access = 0, flags_and_attributes = 0;
99945a1f
ML
604 char *escaped_port_name;
605
606 /* Prefix port name with '\\.\' to work with ports above COM9. */
607 if (!(escaped_port_name = malloc(strlen(port->name + 5))))
c33efc48 608 RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed");
99945a1f
ML
609 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
610
74510d4b 611 /* Map 'flags' to the OS-specific settings. */
e3dcf906 612 flags_and_attributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED;
a036341b
ML
613 if (flags & SP_MODE_READ)
614 desired_access |= GENERIC_READ;
615 if (flags & SP_MODE_WRITE)
74510d4b 616 desired_access |= GENERIC_WRITE;
74510d4b 617
99945a1f 618 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
74510d4b 619 OPEN_EXISTING, flags_and_attributes, 0);
99945a1f
ML
620
621 free(escaped_port_name);
622
74510d4b 623 if (port->hdl == INVALID_HANDLE_VALUE)
e3dcf906
ML
624 RETURN_FAIL("port CreateFile() failed");
625
626 /* All timeouts initially disabled. */
627 port->timeouts.ReadIntervalTimeout = 0;
628 port->timeouts.ReadTotalTimeoutMultiplier = 0;
629 port->timeouts.ReadTotalTimeoutConstant = 0;
630 port->timeouts.WriteTotalTimeoutMultiplier = 0;
631 port->timeouts.WriteTotalTimeoutConstant = 0;
a3cb91f5 632
e3dcf906 633 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) {
a3cb91f5
ML
634 sp_close(port);
635 RETURN_FAIL("SetCommTimeouts() failed");
636 }
e3dcf906
ML
637
638 /* Prepare OVERLAPPED structures. */
639 memset(&port->read_ovl, 0, sizeof(port->read_ovl));
640 memset(&port->write_ovl, 0, sizeof(port->write_ovl));
641 port->read_ovl.hEvent = INVALID_HANDLE_VALUE;
642 port->write_ovl.hEvent = INVALID_HANDLE_VALUE;
643 if ((port->read_ovl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == INVALID_HANDLE_VALUE) {
644 sp_close(port);
645 RETURN_FAIL("read event CreateEvent() failed");
646 }
647 if ((port->write_ovl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == INVALID_HANDLE_VALUE) {
648 sp_close(port);
649 RETURN_FAIL("write event CreateEvent() failed");
650 }
651
652 port->writing = FALSE;
653
74510d4b 654#else
e3dcf906 655 int flags_local = O_NONBLOCK | O_NOCTTY;
f92f1f0c 656
74510d4b 657 /* Map 'flags' to the OS-specific settings. */
a036341b 658 if (flags & (SP_MODE_READ | SP_MODE_WRITE))
74510d4b 659 flags_local |= O_RDWR;
a036341b 660 else if (flags & SP_MODE_READ)
74510d4b 661 flags_local |= O_RDONLY;
a036341b
ML
662 else if (flags & SP_MODE_WRITE)
663 flags_local |= O_WRONLY;
74510d4b
ML
664
665 if ((port->fd = open(port->name, flags_local)) < 0)
c33efc48 666 RETURN_FAIL("open() failed");
bccc7c9f 667#endif
9cb98459 668
e33ab9aa
ML
669 ret = get_config(port, &data, &config);
670
eac329d2 671 if (ret < 0) {
e33ab9aa 672 sp_close(port);
c33efc48 673 RETURN_CODEVAL(ret);
e33ab9aa 674 }
9cb98459 675
bccc7c9f
ML
676 /* Set sane port settings. */
677#ifdef _WIN32
678 data.dcb.fBinary = TRUE;
679 data.dcb.fDsrSensitivity = FALSE;
680 data.dcb.fErrorChar = FALSE;
681 data.dcb.fNull = FALSE;
682 data.dcb.fAbortOnError = TRUE;
683#else
b251be4b 684 /* Turn off all fancy termios tricks, give us a raw channel. */
c3e05092
UH
685 data.term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IMAXBEL);
686#ifdef IUCLC
687 data.term.c_iflag &= ~IUCLC;
688#endif
689 data.term.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
690#ifdef OLCUC
691 data.term.c_oflag &= ~OLCUC;
692#endif
693#ifdef NLDLY
694 data.term.c_oflag &= ~NLDLY;
695#endif
696#ifdef CRDLY
697 data.term.c_oflag &= ~CRDLY;
698#endif
699#ifdef TABDLY
700 data.term.c_oflag &= ~TABDLY;
701#endif
702#ifdef BSDLY
703 data.term.c_oflag &= ~BSDLY;
704#endif
705#ifdef VTDLY
706 data.term.c_oflag &= ~VTDLY;
707#endif
708#ifdef FFDLY
709 data.term.c_oflag &= ~FFDLY;
710#endif
b251be4b 711#ifdef OFILL
9cb98459
ML
712 data.term.c_oflag &= ~OFILL;
713#endif
c3e05092 714 data.term.c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN);
e3dcf906 715 data.term.c_cc[VMIN] = 0;
b251be4b 716 data.term.c_cc[VTIME] = 0;
9cb98459 717
b251be4b
ML
718 /* Ignore modem status lines; enable receiver; leave control lines alone on close. */
719 data.term.c_cflag |= (CLOCAL | CREAD | HUPCL);
bccc7c9f 720#endif
9cb98459 721
e33ab9aa
ML
722 ret = set_config(port, &data, &config);
723
eac329d2 724 if (ret < 0) {
e33ab9aa 725 sp_close(port);
c33efc48 726 RETURN_CODEVAL(ret);
e33ab9aa 727 }
74510d4b 728
c33efc48 729 RETURN_OK();
74510d4b
ML
730}
731
eb6ed20f 732enum sp_return sp_close(struct sp_port *port)
74510d4b 733{
c33efc48
ML
734 TRACE("%p", port);
735
dec10e31 736 CHECK_OPEN_PORT();
74510d4b 737
ea667be7
ML
738 DEBUG("Closing port %s", port->name);
739
74510d4b
ML
740#ifdef _WIN32
741 /* Returns non-zero upon success, 0 upon failure. */
742 if (CloseHandle(port->hdl) == 0)
e3dcf906 743 RETURN_FAIL("port CloseHandle() failed");
8f471c66 744 port->hdl = INVALID_HANDLE_VALUE;
e3dcf906
ML
745 /* Close event handle created for overlapped reads. */
746 if (port->read_ovl.hEvent != INVALID_HANDLE_VALUE && CloseHandle(port->read_ovl.hEvent) == 0)
747 RETURN_FAIL("read event CloseHandle() failed");
748 /* Close event handle created for overlapped writes. */
749 if (port->write_ovl.hEvent != INVALID_HANDLE_VALUE && CloseHandle(port->write_ovl.hEvent) == 0)
750 RETURN_FAIL("write event CloseHandle() failed");
74510d4b
ML
751#else
752 /* Returns 0 upon success, -1 upon failure. */
753 if (close(port->fd) == -1)
c33efc48 754 RETURN_FAIL("close() failed");
8f471c66 755 port->fd = -1;
74510d4b
ML
756#endif
757
c33efc48 758 RETURN_OK();
74510d4b
ML
759}
760
fd8fd11a 761enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
74510d4b 762{
00d8c56d 763 TRACE("%p, 0x%x", port, buffers);
c33efc48 764
dec10e31
ML
765 CHECK_OPEN_PORT();
766
767 if (buffers > SP_BUF_BOTH)
768 RETURN_ERROR(SP_ERR_ARG, "Invalid buffer selection");
74510d4b 769
0ba3e49b 770 const char *buffer_names[] = {"no", "input", "output", "both"};
ea667be7
ML
771
772 DEBUG("Flushing %s buffers on port %s", buffer_names[buffers], port->name);
773
74510d4b 774#ifdef _WIN32
fd8fd11a
ML
775 DWORD flags = 0;
776 if (buffers & SP_BUF_INPUT)
777 flags |= PURGE_RXCLEAR;
778 if (buffers & SP_BUF_OUTPUT)
779 flags |= PURGE_TXCLEAR;
780
74510d4b 781 /* Returns non-zero upon success, 0 upon failure. */
fd8fd11a 782 if (PurgeComm(port->hdl, flags) == 0)
c33efc48 783 RETURN_FAIL("PurgeComm() failed");
74510d4b 784#else
fd8fd11a
ML
785 int flags = 0;
786 if (buffers & SP_BUF_BOTH)
787 flags = TCIOFLUSH;
788 else if (buffers & SP_BUF_INPUT)
789 flags = TCIFLUSH;
82f424e6 790 else if (buffers & SP_BUF_OUTPUT)
fd8fd11a
ML
791 flags = TCOFLUSH;
792
74510d4b 793 /* Returns 0 upon success, -1 upon failure. */
fd8fd11a 794 if (tcflush(port->fd, flags) < 0)
c33efc48 795 RETURN_FAIL("tcflush() failed");
74510d4b 796#endif
c33efc48 797 RETURN_OK();
74510d4b
ML
798}
799
69a3739c
ML
800enum sp_return sp_drain(struct sp_port *port)
801{
c33efc48
ML
802 TRACE("%p", port);
803
dec10e31 804 CHECK_OPEN_PORT();
69a3739c 805
ea667be7
ML
806 DEBUG("Draining port %s", port->name);
807
69a3739c
ML
808#ifdef _WIN32
809 /* Returns non-zero upon success, 0 upon failure. */
810 if (FlushFileBuffers(port->hdl) == 0)
c33efc48 811 RETURN_FAIL("FlushFileBuffers() failed");
2c827b21 812 RETURN_OK();
69a3739c 813#else
2c827b21
ML
814 int result;
815 while (1) {
816 result = tcdrain(port->fd);
817 if (result < 0) {
818 if (errno == EINTR) {
819 DEBUG("tcdrain() was interrupted");
820 continue;
821 } else {
822 RETURN_FAIL("tcdrain() failed");
823 }
824 } else {
825 RETURN_OK();
826 }
827 }
69a3739c 828#endif
69a3739c
ML
829}
830
e3dcf906
ML
831enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout)
832{
833 TRACE("%p, %p, %d, %d", port, buf, count, timeout);
834
835 CHECK_OPEN_PORT();
836
837 if (!buf)
838 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
839
840 if (timeout)
841 DEBUG("Writing %d bytes to port %s, timeout %d ms", count, port->name, timeout);
842 else
843 DEBUG("Writing %d bytes to port %s, no timeout", count, port->name);
844
845 if (count == 0)
846 RETURN_VALUE("0", 0);
847
848#ifdef _WIN32
849 DWORD bytes_written = 0;
850 BOOL result;
851
852 /* Wait for previous non-blocking write to complete, if any. */
853 if (port->writing) {
854 DEBUG("Waiting for previous write to complete");
855 result = GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
856 port->writing = 0;
857 if (!result)
858 RETURN_FAIL("Previous write failed to complete");
859 DEBUG("Previous write completed");
860 }
861
862 /* Set timeout. */
863 port->timeouts.WriteTotalTimeoutConstant = timeout;
864 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
865 RETURN_FAIL("SetCommTimeouts() failed");
866
867 /* Start write. */
868 if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl) == 0) {
869 if (GetLastError() == ERROR_IO_PENDING) {
870 DEBUG("Waiting for write to complete");
871 GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
872 DEBUG("Write completed, %d/%d bytes written", bytes_written, count);
873 RETURN_VALUE("%d", bytes_written);
874 } else {
875 RETURN_FAIL("WriteFile() failed");
876 }
877 } else {
878 DEBUG("Write completed immediately");
879 RETURN_VALUE("%d", count);
880 }
881#else
882 size_t bytes_written = 0;
883 unsigned char *ptr = (unsigned char *) buf;
884 struct timeval start, delta, now, end = {0, 0};
885 fd_set fds;
886 int result;
887
888 if (timeout) {
889 /* Get time at start of operation. */
890 gettimeofday(&start, NULL);
891 /* Define duration of timeout. */
892 delta.tv_sec = timeout / 1000;
1b342042 893 delta.tv_usec = (timeout % 1000) * 1000;
e3dcf906
ML
894 /* Calculate time at which we should give up. */
895 timeradd(&start, &delta, &end);
896 }
897
898 /* Loop until we have written the requested number of bytes. */
899 while (bytes_written < count)
900 {
901 /* Wait until space is available. */
902 FD_ZERO(&fds);
903 FD_SET(port->fd, &fds);
904 if (timeout) {
905 gettimeofday(&now, NULL);
906 if (timercmp(&now, &end, >)) {
907 DEBUG("write timed out");
908 RETURN_VALUE("%d", bytes_written);
909 }
910 timersub(&end, &now, &delta);
911 }
912 result = select(port->fd + 1, NULL, &fds, NULL, timeout ? &delta : NULL);
63a17c64
ML
913 if (result < 0) {
914 if (errno == EINTR) {
915 DEBUG("select() call was interrupted, repeating");
916 continue;
917 } else {
918 RETURN_FAIL("select() failed");
919 }
920 } else if (result == 0) {
e3dcf906
ML
921 DEBUG("write timed out");
922 RETURN_VALUE("%d", bytes_written);
923 }
924
925 /* Do write. */
926 result = write(port->fd, ptr, count - bytes_written);
927
928 if (result < 0) {
929 if (errno == EAGAIN)
930 /* This shouldn't happen because we did a select() first, but handle anyway. */
931 continue;
932 else
933 /* This is an actual failure. */
934 RETURN_FAIL("write() failed");
935 }
936
937 bytes_written += result;
938 ptr += result;
939 }
940
941 RETURN_VALUE("%d", bytes_written);
942#endif
943}
944
945enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count)
74510d4b 946{
c33efc48
ML
947 TRACE("%p, %p, %d", port, buf, count);
948
dec10e31 949 CHECK_OPEN_PORT();
74510d4b
ML
950
951 if (!buf)
c33efc48 952 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
74510d4b 953
ea667be7
ML
954 DEBUG("Writing up to %d bytes to port %s", count, port->name);
955
a3cb91f5
ML
956 if (count == 0)
957 RETURN_VALUE("0", 0);
958
74510d4b
ML
959#ifdef _WIN32
960 DWORD written = 0;
0765af56 961 BYTE *ptr = (BYTE *) buf;
f92f1f0c 962
e3dcf906
ML
963 /* Check whether previous write is complete. */
964 if (port->writing) {
965 if (HasOverlappedIoCompleted(&port->write_ovl)) {
966 DEBUG("Previous write completed");
967 port->writing = 0;
968 } else {
969 DEBUG("Previous write not complete");
970 /* Can't take a new write until the previous one finishes. */
971 RETURN_VALUE("0", 0);
a3cb91f5 972 }
e3dcf906 973 }
a3cb91f5 974
e3dcf906
ML
975 /* Set timeout. */
976 port->timeouts.WriteTotalTimeoutConstant = 0;
977 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
978 RETURN_FAIL("SetCommTimeouts() failed");
979
980 /* Keep writing data until the OS has to actually start an async IO for it.
981 * At that point we know the buffer is full. */
982 while (written < count)
983 {
984 /* Copy first byte of user buffer. */
985 port->pending_byte = *ptr++;
986
987 /* Start asynchronous write. */
988 if (WriteFile(port->hdl, &port->pending_byte, 1, NULL, &port->write_ovl) == 0) {
989 if (GetLastError() == ERROR_IO_PENDING) {
990 DEBUG("Asynchronous write started");
991 port->writing = 1;
992 RETURN_VALUE("%d", ++written);
a3cb91f5 993 } else {
e3dcf906
ML
994 /* Actual failure of some kind. */
995 RETURN_FAIL("WriteFile() failed");
a3cb91f5 996 }
e3dcf906
ML
997 } else {
998 DEBUG("Single byte written immediately.");
999 written++;
a3cb91f5
ML
1000 }
1001 }
1002
e3dcf906
ML
1003 DEBUG("All bytes written immediately.");
1004
c33efc48 1005 RETURN_VALUE("%d", written);
74510d4b
ML
1006#else
1007 /* Returns the number of bytes written, or -1 upon failure. */
1008 ssize_t written = write(port->fd, buf, count);
f92f1f0c 1009
74510d4b 1010 if (written < 0)
c33efc48 1011 RETURN_FAIL("write() failed");
74510d4b 1012 else
c33efc48 1013 RETURN_VALUE("%d", written);
74510d4b
ML
1014#endif
1015}
1016
e3dcf906
ML
1017enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout)
1018{
1019 TRACE("%p, %p, %d, %d", port, buf, count, timeout);
1020
1021 CHECK_OPEN_PORT();
1022
1023 if (!buf)
1024 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
1025
1026 if (timeout)
1027 DEBUG("Reading %d bytes from port %s, timeout %d ms", count, port->name, timeout);
1028 else
1029 DEBUG("Reading %d bytes from port %s, no timeout", count, port->name);
1030
1031 if (count == 0)
1032 RETURN_VALUE("0", 0);
1033
1034#ifdef _WIN32
1035 DWORD bytes_read = 0;
1036
1037 /* Set timeout. */
1038 port->timeouts.ReadIntervalTimeout = 0;
1039 port->timeouts.ReadTotalTimeoutConstant = timeout;
1040 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
1041 RETURN_FAIL("SetCommTimeouts() failed");
1042
1043 /* Start read. */
1044 if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0) {
1045 if (GetLastError() == ERROR_IO_PENDING) {
1046 DEBUG("Waiting for read to complete");
1047 GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE);
1048 DEBUG("Read completed, %d/%d bytes read", bytes_read, count);
1049 RETURN_VALUE("%d", bytes_read);
1050 } else {
1051 RETURN_FAIL("ReadFile() failed");
1052 }
1053 } else {
1054 DEBUG("Read completed immediately");
1055 RETURN_VALUE("%d", count);
1056 }
1057#else
1058 size_t bytes_read = 0;
1059 unsigned char *ptr = (unsigned char *) buf;
1060 struct timeval start, delta, now, end = {0, 0};
1061 fd_set fds;
1062 int result;
1063
1064 if (timeout) {
1065 /* Get time at start of operation. */
1066 gettimeofday(&start, NULL);
1067 /* Define duration of timeout. */
1068 delta.tv_sec = timeout / 1000;
1b342042 1069 delta.tv_usec = (timeout % 1000) * 1000;
e3dcf906
ML
1070 /* Calculate time at which we should give up. */
1071 timeradd(&start, &delta, &end);
1072 }
1073
1074 /* Loop until we have the requested number of bytes. */
1075 while (bytes_read < count)
1076 {
1077 /* Wait until data is available. */
1078 FD_ZERO(&fds);
1079 FD_SET(port->fd, &fds);
1080 if (timeout) {
1081 gettimeofday(&now, NULL);
1082 if (timercmp(&now, &end, >))
1083 /* Timeout has expired. */
1084 RETURN_VALUE("%d", bytes_read);
1085 timersub(&end, &now, &delta);
1086 }
1087 result = select(port->fd + 1, &fds, NULL, NULL, timeout ? &delta : NULL);
63a17c64
ML
1088 if (result < 0) {
1089 if (errno == EINTR) {
1090 DEBUG("select() call was interrupted, repeating");
1091 continue;
1092 } else {
1093 RETURN_FAIL("select() failed");
1094 }
1095 } else if (result == 0) {
e3dcf906
ML
1096 DEBUG("read timed out");
1097 RETURN_VALUE("%d", bytes_read);
1098 }
1099
1100 /* Do read. */
1101 result = read(port->fd, ptr, count - bytes_read);
1102
1103 if (result < 0) {
1104 if (errno == EAGAIN)
1105 /* This shouldn't happen because we did a select() first, but handle anyway. */
1106 continue;
1107 else
1108 /* This is an actual failure. */
1109 RETURN_FAIL("read() failed");
1110 }
1111
1112 bytes_read += result;
1113 ptr += result;
1114 }
1115
1116 RETURN_VALUE("%d", bytes_read);
1117#endif
1118}
1119
1120enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count)
74510d4b 1121{
c33efc48
ML
1122 TRACE("%p, %p, %d", port, buf, count);
1123
dec10e31 1124 CHECK_OPEN_PORT();
74510d4b
ML
1125
1126 if (!buf)
c33efc48 1127 RETURN_ERROR(SP_ERR_ARG, "Null buffer");
74510d4b 1128
ea667be7
ML
1129 DEBUG("Reading up to %d bytes from port %s", count, port->name);
1130
74510d4b 1131#ifdef _WIN32
e3dcf906 1132 DWORD bytes_read;
f92f1f0c 1133
e3dcf906
ML
1134 /* Set timeout. */
1135 port->timeouts.ReadIntervalTimeout = MAXDWORD;
1136 port->timeouts.ReadTotalTimeoutConstant = 0;
1137 if (SetCommTimeouts(port->hdl, &port->timeouts) == 0)
1138 RETURN_FAIL("SetCommTimeouts() failed");
1139
1140 /* Do read. */
1141 if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0)
c33efc48 1142 RETURN_FAIL("ReadFile() failed");
e3dcf906
ML
1143
1144 /* Get number of bytes read. */
1145 GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE);
1146
c33efc48 1147 RETURN_VALUE("%d", bytes_read);
74510d4b
ML
1148#else
1149 ssize_t bytes_read;
f92f1f0c 1150
74510d4b 1151 /* Returns the number of bytes read, or -1 upon failure. */
33d5ff47 1152 if ((bytes_read = read(port->fd, buf, count)) < 0) {
e3dcf906
ML
1153 if (errno == EAGAIN)
1154 /* No bytes available. */
33d5ff47
ML
1155 bytes_read = 0;
1156 else
1157 /* This is an actual failure. */
1158 RETURN_FAIL("read() failed");
1159 }
c33efc48 1160 RETURN_VALUE("%d", bytes_read);
74510d4b
ML
1161#endif
1162}
1163
3353c22f
ML
1164enum sp_return sp_input_waiting(struct sp_port *port)
1165{
1166 TRACE("%p", port);
1167
1168 CHECK_OPEN_PORT();
1169
1170 DEBUG("Checking input bytes waiting on port %s", port->name);
1171
1172#ifdef _WIN32
1173 DWORD errors;
1174 COMSTAT comstat;
1175
1176 if (ClearCommError(port->hdl, &errors, &comstat) == 0)
1177 RETURN_FAIL("ClearComError() failed");
1178 RETURN_VALUE("%d", comstat.cbInQue);
1179#else
1180 int bytes_waiting;
1181 if (ioctl(port->fd, TIOCINQ, &bytes_waiting) < 0)
1182 RETURN_FAIL("TIOCINQ ioctl failed");
1183 RETURN_VALUE("%d", bytes_waiting);
1184#endif
1185}
1186
1187enum sp_return sp_output_waiting(struct sp_port *port)
1188{
1189 TRACE("%p", port);
1190
1191 CHECK_OPEN_PORT();
1192
1193 DEBUG("Checking output bytes waiting on port %s", port->name);
1194
1195#ifdef _WIN32
1196 DWORD errors;
1197 COMSTAT comstat;
1198
1199 if (ClearCommError(port->hdl, &errors, &comstat) == 0)
1200 RETURN_FAIL("ClearComError() failed");
1201 RETURN_VALUE("%d", comstat.cbOutQue);
1202#else
1203 int bytes_waiting;
1204 if (ioctl(port->fd, TIOCOUTQ, &bytes_waiting) < 0)
1205 RETURN_FAIL("TIOCOUTQ ioctl failed");
1206 RETURN_VALUE("%d", bytes_waiting);
1207#endif
1208}
1209
7a6d2196
ML
1210#ifdef __linux__
1211static enum sp_return get_baudrate(int fd, int *baudrate)
1212{
1213 void *data;
1214
c33efc48
ML
1215 TRACE("%d, %p", fd, baudrate);
1216
ea667be7
ML
1217 DEBUG("Getting baud rate");
1218
7a6d2196 1219 if (!(data = malloc(get_termios_size())))
c33efc48 1220 RETURN_ERROR(SP_ERR_MEM, "termios malloc failed");
7a6d2196 1221
8d43110a
ML
1222 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
1223 free(data);
c33efc48 1224 RETURN_FAIL("getting termios failed");
8d43110a 1225 }
7a6d2196
ML
1226
1227 *baudrate = get_termios_speed(data);
1228
8d43110a
ML
1229 free(data);
1230
c33efc48 1231 RETURN_OK();
7a6d2196
ML
1232}
1233
1234static enum sp_return set_baudrate(int fd, int baudrate)
1235{
1236 void *data;
1237
c33efc48
ML
1238 TRACE("%d, %d", fd, baudrate);
1239
ea667be7
ML
1240 DEBUG("Getting baud rate");
1241
7a6d2196 1242 if (!(data = malloc(get_termios_size())))
c33efc48 1243 RETURN_ERROR(SP_ERR_MEM, "termios malloc failed");
7a6d2196 1244
8d43110a
ML
1245 if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
1246 free(data);
c33efc48 1247 RETURN_FAIL("getting termios failed");
8d43110a 1248 }
7a6d2196 1249
ea667be7
ML
1250 DEBUG("Setting baud rate");
1251
7a6d2196
ML
1252 set_termios_speed(data, baudrate);
1253
8d43110a
ML
1254 if (ioctl(fd, get_termios_set_ioctl(), data) < 0) {
1255 free(data);
c33efc48 1256 RETURN_FAIL("setting termios failed");
8d43110a
ML
1257 }
1258
1259 free(data);
7a6d2196 1260
c33efc48 1261 RETURN_OK();
7a6d2196 1262}
40978c2b
ML
1263
1264#ifdef USE_TERMIOX
1265static enum sp_return get_flow(int fd, int *flow)
1266{
1267 void *data;
1268
c33efc48
ML
1269 TRACE("%d, %p", fd, flow);
1270
ea667be7
ML
1271 DEBUG("Getting advanced flow control");
1272
40978c2b 1273 if (!(data = malloc(get_termiox_size())))
c33efc48 1274 RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed");
40978c2b 1275
8d43110a
ML
1276 if (ioctl(fd, TCGETX, data) < 0) {
1277 free(data);
c33efc48 1278 RETURN_FAIL("getting termiox failed");
8d43110a 1279 }
40978c2b
ML
1280
1281 *flow = get_termiox_flow(data);
1282
8d43110a
ML
1283 free(data);
1284
c33efc48 1285 RETURN_OK();
40978c2b
ML
1286}
1287
1288static enum sp_return set_flow(int fd, int flow)
1289{
1290 void *data;
1291
c33efc48
ML
1292 TRACE("%d, %d", fd, flow);
1293
ea667be7
ML
1294 DEBUG("Getting advanced flow control");
1295
40978c2b 1296 if (!(data = malloc(get_termiox_size())))
c33efc48 1297 RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed");
40978c2b 1298
8d43110a
ML
1299 if (ioctl(fd, TCGETX, data) < 0) {
1300 free(data);
c33efc48 1301 RETURN_FAIL("getting termiox failed");
8d43110a 1302 }
40978c2b 1303
ea667be7
ML
1304 DEBUG("Setting advanced flow control");
1305
40978c2b
ML
1306 set_termiox_flow(data, flow);
1307
8d43110a
ML
1308 if (ioctl(fd, TCSETX, data) < 0) {
1309 free(data);
c33efc48 1310 RETURN_FAIL("setting termiox failed");
8d43110a
ML
1311 }
1312
1313 free(data);
40978c2b 1314
c33efc48 1315 RETURN_OK();
40978c2b
ML
1316}
1317#endif /* USE_TERMIOX */
1318#endif /* __linux__ */
7a6d2196 1319
eb6ed20f
ML
1320static enum sp_return get_config(struct sp_port *port, struct port_data *data,
1321 struct sp_port_config *config)
8094e4a0 1322{
da2748bf 1323 unsigned int i;
cbf628c7 1324
c33efc48
ML
1325 TRACE("%p, %p, %p", port, data, config);
1326
ea667be7
ML
1327 DEBUG("Getting configuration for port %s", port->name);
1328
8094e4a0 1329#ifdef _WIN32
e33ab9aa 1330 if (!GetCommState(port->hdl, &data->dcb))
c33efc48 1331 RETURN_FAIL("GetCommState() failed");
8094e4a0 1332
067417af 1333 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
e33ab9aa 1334 if (data->dcb.BaudRate == std_baudrates[i].index) {
067417af
ML
1335 config->baudrate = std_baudrates[i].value;
1336 break;
1337 }
1338 }
1339
1340 if (i == NUM_STD_BAUDRATES)
1341 /* BaudRate field can be either an index or a custom baud rate. */
e33ab9aa 1342 config->baudrate = data->dcb.BaudRate;
067417af 1343
e33ab9aa 1344 config->bits = data->dcb.ByteSize;
067417af 1345
e33ab9aa
ML
1346 if (data->dcb.fParity)
1347 switch (data->dcb.Parity) {
067417af
ML
1348 case NOPARITY:
1349 config->parity = SP_PARITY_NONE;
1350 break;
e432ce60
ML
1351 case ODDPARITY:
1352 config->parity = SP_PARITY_ODD;
1353 break;
067417af
ML
1354 case EVENPARITY:
1355 config->parity = SP_PARITY_EVEN;
1356 break;
e432ce60
ML
1357 case MARKPARITY:
1358 config->parity = SP_PARITY_MARK;
1359 break;
1360 case SPACEPARITY:
1361 config->parity = SP_PARITY_SPACE;
067417af
ML
1362 break;
1363 default:
1364 config->parity = -1;
1365 }
1366 else
1367 config->parity = SP_PARITY_NONE;
1368
e33ab9aa 1369 switch (data->dcb.StopBits) {
067417af
ML
1370 case ONESTOPBIT:
1371 config->stopbits = 1;
1372 break;
1373 case TWOSTOPBITS:
1374 config->stopbits = 2;
1375 break;
1376 default:
1377 config->stopbits = -1;
1378 }
1379
e33ab9aa 1380 switch (data->dcb.fRtsControl) {
eac329d2
UH
1381 case RTS_CONTROL_DISABLE:
1382 config->rts = SP_RTS_OFF;
1383 break;
1384 case RTS_CONTROL_ENABLE:
1385 config->rts = SP_RTS_ON;
1386 break;
1387 case RTS_CONTROL_HANDSHAKE:
1388 config->rts = SP_RTS_FLOW_CONTROL;
1389 break;
1390 default:
1391 config->rts = -1;
067417af
ML
1392 }
1393
e33ab9aa 1394 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
067417af 1395
e33ab9aa 1396 switch (data->dcb.fDtrControl) {
eac329d2
UH
1397 case DTR_CONTROL_DISABLE:
1398 config->dtr = SP_DTR_OFF;
1399 break;
1400 case DTR_CONTROL_ENABLE:
1401 config->dtr = SP_DTR_ON;
1402 break;
1403 case DTR_CONTROL_HANDSHAKE:
1404 config->dtr = SP_DTR_FLOW_CONTROL;
1405 break;
1406 default:
1407 config->dtr = -1;
067417af
ML
1408 }
1409
e33ab9aa
ML
1410 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
1411
c6754b45
ML
1412 if (data->dcb.fInX) {
1413 if (data->dcb.fOutX)
1414 config->xon_xoff = SP_XONXOFF_INOUT;
1415 else
1416 config->xon_xoff = SP_XONXOFF_IN;
1417 } else {
1418 if (data->dcb.fOutX)
1419 config->xon_xoff = SP_XONXOFF_OUT;
1420 else
1421 config->xon_xoff = SP_XONXOFF_DISABLED;
1422 }
1423
e33ab9aa
ML
1424#else // !_WIN32
1425
1426 if (tcgetattr(port->fd, &data->term) < 0)
c33efc48 1427 RETURN_FAIL("tcgetattr() failed");
e33ab9aa
ML
1428
1429 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
c33efc48 1430 RETURN_FAIL("TIOCMGET ioctl failed");
40978c2b
ML
1431
1432#ifdef USE_TERMIOX
68ec29db
ML
1433 int ret = get_flow(port->fd, &data->flow);
1434
1435 if (ret == SP_ERR_FAIL && errno == EINVAL)
1436 data->termiox_supported = 0;
1437 else if (ret < 0)
c33efc48 1438 RETURN_CODEVAL(ret);
68ec29db
ML
1439 else
1440 data->termiox_supported = 1;
1441#else
1442 data->termiox_supported = 0;
40978c2b
ML
1443#endif
1444
067417af 1445 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
e33ab9aa 1446 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
067417af
ML
1447 config->baudrate = std_baudrates[i].value;
1448 break;
1449 }
1450 }
1451
31b3a8f5
MH
1452 if (i == NUM_STD_BAUDRATES) {
1453#ifdef __APPLE__
1454 config->baudrate = (int)data->term.c_ispeed;
7a6d2196
ML
1455#elif defined(__linux__)
1456 TRY(get_baudrate(port->fd, &config->baudrate));
31b3a8f5 1457#else
067417af 1458 config->baudrate = -1;
31b3a8f5
MH
1459#endif
1460 }
067417af 1461
e33ab9aa 1462 switch (data->term.c_cflag & CSIZE) {
067417af
ML
1463 case CS8:
1464 config->bits = 8;
1465 break;
1466 case CS7:
1467 config->bits = 7;
1468 break;
1469 case CS6:
1470 config->bits = 6;
1471 break;
1472 case CS5:
1473 config->bits = 5;
1474 break;
1475 default:
1476 config->bits = -1;
1477 }
1478
e33ab9aa 1479 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
067417af 1480 config->parity = SP_PARITY_NONE;
e33ab9aa 1481 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
067417af 1482 config->parity = -1;
c3e05092 1483#ifdef CMSPAR
e432ce60
ML
1484 else if (data->term.c_cflag & CMSPAR)
1485 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_MARK : SP_PARITY_SPACE;
c3e05092 1486#endif
067417af 1487 else
e33ab9aa 1488 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
067417af 1489
e33ab9aa 1490 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
067417af 1491
e33ab9aa 1492 if (data->term.c_cflag & CRTSCTS) {
067417af
ML
1493 config->rts = SP_RTS_FLOW_CONTROL;
1494 config->cts = SP_CTS_FLOW_CONTROL;
1495 } else {
68ec29db 1496 if (data->termiox_supported && data->flow & RTS_FLOW)
40978c2b
ML
1497 config->rts = SP_RTS_FLOW_CONTROL;
1498 else
1499 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
1500
68ec29db
ML
1501 config->cts = (data->termiox_supported && data->flow & CTS_FLOW) ?
1502 SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
067417af
ML
1503 }
1504
68ec29db 1505 if (data->termiox_supported && data->flow & DTR_FLOW)
40978c2b
ML
1506 config->dtr = SP_DTR_FLOW_CONTROL;
1507 else
1508 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
1509
68ec29db
ML
1510 config->dsr = (data->termiox_supported && data->flow & DSR_FLOW) ?
1511 SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
705bdc69 1512
e29b93a5
ML
1513 if (data->term.c_iflag & IXOFF) {
1514 if (data->term.c_iflag & IXON)
1515 config->xon_xoff = SP_XONXOFF_INOUT;
1516 else
1517 config->xon_xoff = SP_XONXOFF_IN;
1518 } else {
1519 if (data->term.c_iflag & IXON)
1520 config->xon_xoff = SP_XONXOFF_OUT;
1521 else
1522 config->xon_xoff = SP_XONXOFF_DISABLED;
1523 }
067417af
ML
1524#endif
1525
c33efc48 1526 RETURN_OK();
067417af
ML
1527}
1528
7a6d2196 1529static enum sp_return set_config(struct sp_port *port, struct port_data *data,
eb6ed20f 1530 const struct sp_port_config *config)
18fc2dd1 1531{
e33ab9aa 1532 unsigned int i;
31b3a8f5
MH
1533#ifdef __APPLE__
1534 BAUD_TYPE baud_nonstd;
1535
1536 baud_nonstd = B0;
1537#endif
7a6d2196
ML
1538#ifdef __linux__
1539 int baud_nonstd = 0;
1540#endif
18fc2dd1 1541
c33efc48
ML
1542 TRACE("%p, %p, %p", port, data, config);
1543
ea667be7
ML
1544 DEBUG("Setting configuration for port %s", port->name);
1545
e33ab9aa 1546#ifdef _WIN32
eac329d2 1547 if (config->baudrate >= 0) {
e33ab9aa
ML
1548 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1549 if (config->baudrate == std_baudrates[i].value) {
1550 data->dcb.BaudRate = std_baudrates[i].index;
1551 break;
1552 }
1553 }
18fc2dd1 1554
e33ab9aa
ML
1555 if (i == NUM_STD_BAUDRATES)
1556 data->dcb.BaudRate = config->baudrate;
1557 }
18fc2dd1 1558
e33ab9aa
ML
1559 if (config->bits >= 0)
1560 data->dcb.ByteSize = config->bits;
1561
1562 if (config->parity >= 0) {
1563 switch (config->parity) {
1564 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
1565 case SP_PARITY_NONE:
1566 data->dcb.Parity = NOPARITY;
1567 break;
e432ce60
ML
1568 case SP_PARITY_ODD:
1569 data->dcb.Parity = ODDPARITY;
1570 break;
e33ab9aa
ML
1571 case SP_PARITY_EVEN:
1572 data->dcb.Parity = EVENPARITY;
1573 break;
e432ce60
ML
1574 case SP_PARITY_MARK:
1575 data->dcb.Parity = MARKPARITY;
1576 break;
1577 case SP_PARITY_SPACE:
1578 data->dcb.Parity = SPACEPARITY;
e33ab9aa
ML
1579 break;
1580 default:
c33efc48 1581 RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting");
e33ab9aa 1582 }
18fc2dd1
ML
1583 }
1584
e33ab9aa
ML
1585 if (config->stopbits >= 0) {
1586 switch (config->stopbits) {
1587 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
1588 case 1:
1589 data->dcb.StopBits = ONESTOPBIT;
1590 break;
1591 case 2:
1592 data->dcb.StopBits = TWOSTOPBITS;
1593 break;
1594 default:
c33efc48 1595 RETURN_ERROR(SP_ERR_ARG, "Invalid stop bit setting");
e33ab9aa
ML
1596 }
1597 }
1598
1599 if (config->rts >= 0) {
1600 switch (config->rts) {
1601 case SP_RTS_OFF:
1602 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
1603 break;
1604 case SP_RTS_ON:
1605 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
1606 break;
1607 case SP_RTS_FLOW_CONTROL:
1608 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
1609 break;
1610 default:
c33efc48 1611 RETURN_ERROR(SP_ERR_ARG, "Invalid RTS setting");
e33ab9aa
ML
1612 }
1613 }
1614
1615 if (config->cts >= 0) {
1616 switch (config->cts) {
1617 case SP_CTS_IGNORE:
1618 data->dcb.fOutxCtsFlow = FALSE;
1619 break;
1620 case SP_CTS_FLOW_CONTROL:
1621 data->dcb.fOutxCtsFlow = TRUE;
1622 break;
1623 default:
c33efc48 1624 RETURN_ERROR(SP_ERR_ARG, "Invalid CTS setting");
e33ab9aa
ML
1625 }
1626 }
1627
1628 if (config->dtr >= 0) {
1629 switch (config->dtr) {
1630 case SP_DTR_OFF:
1631 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
1632 break;
1633 case SP_DTR_ON:
1634 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
1635 break;
1636 case SP_DTR_FLOW_CONTROL:
1637 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
1638 break;
1639 default:
c33efc48 1640 RETURN_ERROR(SP_ERR_ARG, "Invalid DTR setting");
e33ab9aa
ML
1641 }
1642 }
1643
1644 if (config->dsr >= 0) {
1645 switch (config->dsr) {
1646 case SP_DSR_IGNORE:
1647 data->dcb.fOutxDsrFlow = FALSE;
1648 break;
1649 case SP_DSR_FLOW_CONTROL:
1650 data->dcb.fOutxDsrFlow = TRUE;
1651 break;
1652 default:
c33efc48 1653 RETURN_ERROR(SP_ERR_ARG, "Invalid DSR setting");
e33ab9aa 1654 }
18fc2dd1
ML
1655 }
1656
e33ab9aa
ML
1657 if (config->xon_xoff >= 0) {
1658 switch (config->xon_xoff) {
1659 case SP_XONXOFF_DISABLED:
1660 data->dcb.fInX = FALSE;
1661 data->dcb.fOutX = FALSE;
1662 break;
1663 case SP_XONXOFF_IN:
1664 data->dcb.fInX = TRUE;
1665 data->dcb.fOutX = FALSE;
1666 break;
1667 case SP_XONXOFF_OUT:
1668 data->dcb.fInX = FALSE;
1669 data->dcb.fOutX = TRUE;
1670 break;
1671 case SP_XONXOFF_INOUT:
1672 data->dcb.fInX = TRUE;
1673 data->dcb.fOutX = TRUE;
1674 break;
1675 default:
c33efc48 1676 RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting");
e33ab9aa
ML
1677 }
1678 }
1679
1680 if (!SetCommState(port->hdl, &data->dcb))
c33efc48 1681 RETURN_FAIL("SetCommState() failed");
e33ab9aa 1682
31b3a8f5 1683#else /* !_WIN32 */
e33ab9aa 1684
7a6d2196
ML
1685 int controlbits;
1686
eac329d2 1687 if (config->baudrate >= 0) {
e33ab9aa
ML
1688 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
1689 if (config->baudrate == std_baudrates[i].value) {
1690 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
c33efc48 1691 RETURN_FAIL("cfsetospeed() failed");
e33ab9aa
ML
1692
1693 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
c33efc48 1694 RETURN_FAIL("cfsetispeed() failed");
e33ab9aa
ML
1695 break;
1696 }
1697 }
1698
31b3a8f5
MH
1699 /* Non-standard baud rate */
1700 if (i == NUM_STD_BAUDRATES) {
1701#ifdef __APPLE__
24abdb68 1702 /* Set "dummy" baud rate. */
31b3a8f5 1703 if (cfsetspeed(&data->term, B9600) < 0)
c33efc48 1704 RETURN_FAIL("cfsetspeed() failed");
31b3a8f5 1705 baud_nonstd = config->baudrate;
7a6d2196
ML
1706#elif defined(__linux__)
1707 baud_nonstd = 1;
31b3a8f5 1708#else
c33efc48 1709 RETURN_ERROR(SP_ERR_SUPP, "Non-standard baudrate not supported");
31b3a8f5
MH
1710#endif
1711 }
e33ab9aa
ML
1712 }
1713
1714 if (config->bits >= 0) {
1715 data->term.c_cflag &= ~CSIZE;
1716 switch (config->bits) {
1717 case 8:
1718 data->term.c_cflag |= CS8;
1719 break;
1720 case 7:
1721 data->term.c_cflag |= CS7;
1722 break;
1723 case 6:
1724 data->term.c_cflag |= CS6;
1725 break;
23922313
UH
1726 case 5:
1727 data->term.c_cflag |= CS5;
1728 break;
e33ab9aa 1729 default:
c33efc48 1730 RETURN_ERROR(SP_ERR_ARG, "Invalid data bits setting");
e33ab9aa
ML
1731 }
1732 }
1733
1734 if (config->parity >= 0) {
1735 data->term.c_iflag &= ~IGNPAR;
c3e05092
UH
1736 data->term.c_cflag &= ~(PARENB | PARODD);
1737#ifdef CMSPAR
1738 data->term.c_cflag &= ~CMSPAR;
1739#endif
e33ab9aa
ML
1740 switch (config->parity) {
1741 case SP_PARITY_NONE:
1742 data->term.c_iflag |= IGNPAR;
1743 break;
1744 case SP_PARITY_EVEN:
1745 data->term.c_cflag |= PARENB;
1746 break;
1747 case SP_PARITY_ODD:
1748 data->term.c_cflag |= PARENB | PARODD;
1749 break;
afb518f0 1750#ifdef CMSPAR
e432ce60 1751 case SP_PARITY_MARK:
c3e05092 1752 data->term.c_cflag |= PARENB | PARODD;
c3e05092 1753 data->term.c_cflag |= CMSPAR;
e432ce60
ML
1754 break;
1755 case SP_PARITY_SPACE:
c3e05092 1756 data->term.c_cflag |= PARENB;
c3e05092 1757 data->term.c_cflag |= CMSPAR;
e432ce60 1758 break;
afb518f0
ML
1759#else
1760 case SP_PARITY_MARK:
1761 case SP_PARITY_SPACE:
1762 RETURN_ERROR(SP_ERR_SUPP, "Mark/space parity not supported");
1763#endif
e33ab9aa 1764 default:
c33efc48 1765 RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting");
e33ab9aa
ML
1766 }
1767 }
1768
1769 if (config->stopbits >= 0) {
1770 data->term.c_cflag &= ~CSTOPB;
1771 switch (config->stopbits) {
1772 case 1:
1773 data->term.c_cflag &= ~CSTOPB;
1774 break;
1775 case 2:
1776 data->term.c_cflag |= CSTOPB;
1777 break;
1778 default:
c33efc48 1779 RETURN_ERROR(SP_ERR_ARG, "Invalid stop bits setting");
e33ab9aa
ML
1780 }
1781 }
1782
eac329d2 1783 if (config->rts >= 0 || config->cts >= 0) {
68ec29db
ML
1784 if (data->termiox_supported) {
1785 data->flow &= ~(RTS_FLOW | CTS_FLOW);
1786 switch (config->rts) {
1787 case SP_RTS_OFF:
1788 case SP_RTS_ON:
1789 controlbits = TIOCM_RTS;
1790 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
c33efc48 1791 RETURN_FAIL("Setting RTS signal level failed");
68ec29db
ML
1792 break;
1793 case SP_RTS_FLOW_CONTROL:
1794 data->flow |= RTS_FLOW;
1795 break;
1796 default:
1797 break;
e33ab9aa 1798 }
68ec29db
ML
1799 if (config->cts == SP_CTS_FLOW_CONTROL)
1800 data->flow |= CTS_FLOW;
e33ab9aa 1801
68ec29db 1802 if (data->flow & (RTS_FLOW | CTS_FLOW))
e33ab9aa 1803 data->term.c_iflag |= CRTSCTS;
68ec29db
ML
1804 else
1805 data->term.c_iflag &= ~CRTSCTS;
1806 } else {
1807 /* Asymmetric use of RTS/CTS not supported. */
1808 if (data->term.c_iflag & CRTSCTS) {
1809 /* Flow control can only be disabled for both RTS & CTS together. */
1810 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
1811 if (config->cts != SP_CTS_IGNORE)
c33efc48 1812 RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together");
68ec29db
ML
1813 }
1814 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
1815 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
c33efc48 1816 RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together");
68ec29db 1817 }
e33ab9aa 1818 } else {
68ec29db
ML
1819 /* Flow control can only be enabled for both RTS & CTS together. */
1820 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
1821 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
c33efc48 1822 RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be enabled together");
68ec29db
ML
1823 }
1824
1825 if (config->rts >= 0) {
1826 if (config->rts == SP_RTS_FLOW_CONTROL) {
1827 data->term.c_iflag |= CRTSCTS;
1828 } else {
1829 controlbits = TIOCM_RTS;
1830 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
1831 &controlbits) < 0)
c33efc48 1832 RETURN_FAIL("Setting RTS signal level failed");
68ec29db 1833 }
e33ab9aa
ML
1834 }
1835 }
1836 }
1837
eac329d2 1838 if (config->dtr >= 0 || config->dsr >= 0) {
68ec29db
ML
1839 if (data->termiox_supported) {
1840 data->flow &= ~(DTR_FLOW | DSR_FLOW);
1841 switch (config->dtr) {
1842 case SP_DTR_OFF:
1843 case SP_DTR_ON:
1844 controlbits = TIOCM_DTR;
1845 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
c33efc48 1846 RETURN_FAIL("Setting DTR signal level failed");
68ec29db
ML
1847 break;
1848 case SP_DTR_FLOW_CONTROL:
1849 data->flow |= DTR_FLOW;
1850 break;
1851 default:
1852 break;
1853 }
1854 if (config->dsr == SP_DSR_FLOW_CONTROL)
1855 data->flow |= DSR_FLOW;
1856 } else {
1857 /* DTR/DSR flow control not supported. */
1858 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
c33efc48 1859 RETURN_ERROR(SP_ERR_SUPP, "DTR/DSR flow control not supported");
e33ab9aa 1860
68ec29db
ML
1861 if (config->dtr >= 0) {
1862 controlbits = TIOCM_DTR;
1863 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
1864 &controlbits) < 0)
c33efc48 1865 RETURN_FAIL("Setting DTR signal level failed");
68ec29db 1866 }
e33ab9aa
ML
1867 }
1868 }
1869
1870 if (config->xon_xoff >= 0) {
1871 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
1872 switch (config->xon_xoff) {
1873 case SP_XONXOFF_DISABLED:
1874 break;
1875 case SP_XONXOFF_IN:
1876 data->term.c_iflag |= IXOFF;
1877 break;
1878 case SP_XONXOFF_OUT:
1879 data->term.c_iflag |= IXON | IXANY;
1880 break;
1881 case SP_XONXOFF_INOUT:
1882 data->term.c_iflag |= IXON | IXOFF | IXANY;
1883 break;
1884 default:
c33efc48 1885 RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting");
e33ab9aa
ML
1886 }
1887 }
1888
3f5c06d0 1889 if (tcsetattr(port->fd, TCSANOW, &data->term) < 0)
c33efc48 1890 RETURN_FAIL("tcsetattr() failed");
31b3a8f5
MH
1891
1892#ifdef __APPLE__
1893 if (baud_nonstd != B0) {
1894 if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
c33efc48 1895 RETURN_FAIL("IOSSIOSPEED ioctl failed");
31b3a8f5
MH
1896 /* Set baud rates in data->term to correct, but incompatible
1897 * with tcsetattr() value, same as delivered by tcgetattr(). */
1898 if (cfsetspeed(&data->term, baud_nonstd) < 0)
c33efc48 1899 RETURN_FAIL("cfsetspeed() failed");
31b3a8f5 1900 }
7a6d2196
ML
1901#elif defined(__linux__)
1902 if (baud_nonstd)
1903 TRY(set_baudrate(port->fd, config->baudrate));
40978c2b 1904#ifdef USE_TERMIOX
68ec29db
ML
1905 if (data->termiox_supported)
1906 TRY(set_flow(port->fd, data->flow));
40978c2b 1907#endif
7a6d2196 1908#endif
31b3a8f5
MH
1909
1910#endif /* !_WIN32 */
e33ab9aa 1911
c33efc48 1912 RETURN_OK();
e33ab9aa
ML
1913}
1914
9b1502ef
ML
1915enum sp_return sp_new_config(struct sp_port_config **config_ptr)
1916{
59131d60 1917 struct sp_port_config *config;
9b1502ef 1918
00d8c56d
UH
1919 TRACE("%p", config_ptr);
1920
9b1502ef
ML
1921 if (!config_ptr)
1922 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
1923
59131d60
ML
1924 *config_ptr = NULL;
1925
1926 if (!(config = malloc(sizeof(struct sp_port_config))))
9b1502ef
ML
1927 RETURN_ERROR(SP_ERR_MEM, "config malloc failed");
1928
59131d60
ML
1929 config->baudrate = -1;
1930 config->bits = -1;
1931 config->parity = -1;
1932 config->stopbits = -1;
1933 config->rts = -1;
1934 config->cts = -1;
1935 config->dtr = -1;
1936 config->dsr = -1;
1937
1938 *config_ptr = config;
1939
9b1502ef
ML
1940 RETURN_OK();
1941}
1942
1943void sp_free_config(struct sp_port_config *config)
1944{
1945 TRACE("%p", config);
1946
1947 if (!config)
1948 DEBUG("Null config");
1949 else
1950 free(config);
1951
1952 RETURN();
1953}
1954
70cd37de
ML
1955enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config)
1956{
1957 struct port_data data;
1958
1959 TRACE("%p, %p", port, config);
1960
1961 CHECK_OPEN_PORT();
1962
1963 if (!config)
9b1502ef 1964 RETURN_ERROR(SP_ERR_ARG, "Null config");
70cd37de
ML
1965
1966 TRY(get_config(port, &data, config));
1967
1968 RETURN_OK();
1969}
1970
eb6ed20f 1971enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
e33ab9aa 1972{
8f189c4c 1973 struct port_data data;
e33ab9aa
ML
1974 struct sp_port_config prev_config;
1975
c33efc48
ML
1976 TRACE("%p, %p", port, config);
1977
dec10e31 1978 CHECK_OPEN_PORT();
823690ae
ML
1979
1980 if (!config)
c33efc48 1981 RETURN_ERROR(SP_ERR_ARG, "Null config");
823690ae 1982
e33ab9aa
ML
1983 TRY(get_config(port, &data, &prev_config));
1984 TRY(set_config(port, &data, config));
74510d4b 1985
c33efc48 1986 RETURN_OK();
74510d4b
ML
1987}
1988
9b1502ef
ML
1989#define CREATE_ACCESSORS(x, type) \
1990enum sp_return sp_set_##x(struct sp_port *port, type x) { \
8f189c4c 1991 struct port_data data; \
e33ab9aa 1992 struct sp_port_config config; \
c33efc48 1993 TRACE("%p, %d", port, x); \
dec10e31 1994 CHECK_OPEN_PORT(); \
e33ab9aa
ML
1995 TRY(get_config(port, &data, &config)); \
1996 config.x = x; \
1997 TRY(set_config(port, &data, &config)); \
c33efc48 1998 RETURN_OK(); \
9b1502ef
ML
1999} \
2000enum sp_return sp_get_config_##x(const struct sp_port_config *config, type *x) { \
00d8c56d 2001 TRACE("%p, %p", config, x); \
9b1502ef
ML
2002 if (!config) \
2003 RETURN_ERROR(SP_ERR_ARG, "Null config"); \
2004 *x = config->x; \
2005 RETURN_OK(); \
2006} \
2007enum sp_return sp_set_config_##x(struct sp_port_config *config, type x) { \
2008 TRACE("%p, %d", config, x); \
2009 if (!config) \
2010 RETURN_ERROR(SP_ERR_ARG, "Null config"); \
2011 config->x = x; \
2012 RETURN_OK(); \
9069c2fb
ML
2013}
2014
9b1502ef
ML
2015CREATE_ACCESSORS(baudrate, int)
2016CREATE_ACCESSORS(bits, int)
2017CREATE_ACCESSORS(parity, enum sp_parity)
2018CREATE_ACCESSORS(stopbits, int)
2019CREATE_ACCESSORS(rts, enum sp_rts)
2020CREATE_ACCESSORS(cts, enum sp_cts)
2021CREATE_ACCESSORS(dtr, enum sp_dtr)
2022CREATE_ACCESSORS(dsr, enum sp_dsr)
2023CREATE_ACCESSORS(xon_xoff, enum sp_xonxoff)
2024
2025enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol)
e33ab9aa 2026{
9b1502ef
ML
2027 if (!config)
2028 RETURN_ERROR(SP_ERR_ARG, "Null configuration");
dec10e31
ML
2029
2030 if (flowcontrol > SP_FLOWCONTROL_DTRDSR)
2031 RETURN_ERROR(SP_ERR_ARG, "Invalid flow control setting");
823690ae 2032
e33ab9aa 2033 if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
9b1502ef 2034 config->xon_xoff = SP_XONXOFF_INOUT;
e33ab9aa 2035 else
9b1502ef 2036 config->xon_xoff = SP_XONXOFF_DISABLED;
e33ab9aa
ML
2037
2038 if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
9b1502ef
ML
2039 config->rts = SP_RTS_FLOW_CONTROL;
2040 config->cts = SP_CTS_FLOW_CONTROL;
e33ab9aa 2041 } else {
9b1502ef
ML
2042 if (config->rts == SP_RTS_FLOW_CONTROL)
2043 config->rts = SP_RTS_ON;
2044 config->cts = SP_CTS_IGNORE;
e33ab9aa
ML
2045 }
2046
2047 if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
9b1502ef
ML
2048 config->dtr = SP_DTR_FLOW_CONTROL;
2049 config->dsr = SP_DSR_FLOW_CONTROL;
e33ab9aa 2050 } else {
9b1502ef
ML
2051 if (config->dtr == SP_DTR_FLOW_CONTROL)
2052 config->dtr = SP_DTR_ON;
2053 config->dsr = SP_DSR_IGNORE;
e33ab9aa
ML
2054 }
2055
9b1502ef
ML
2056 RETURN_OK();
2057}
2058
2059enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
2060{
2061 struct port_data data;
2062 struct sp_port_config config;
2063
2064 TRACE("%p, %d", port, flowcontrol);
2065
2066 CHECK_OPEN_PORT();
2067
2068 TRY(get_config(port, &data, &config));
2069
2070 TRY(sp_set_config_flowcontrol(&config, flowcontrol));
2071
e33ab9aa
ML
2072 TRY(set_config(port, &data, &config));
2073
c33efc48 2074 RETURN_OK();
e33ab9aa
ML
2075}
2076
8cf7c697
ML
2077enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals)
2078{
c33efc48
ML
2079 TRACE("%p, %p", port, signals);
2080
dec10e31 2081 CHECK_OPEN_PORT();
8cf7c697
ML
2082
2083 if (!signals)
c33efc48 2084 RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
8cf7c697 2085
ea667be7
ML
2086 DEBUG("Getting control signals for port %s", port->name);
2087
8cf7c697
ML
2088 *signals = 0;
2089#ifdef _WIN32
2090 DWORD bits;
2091 if (GetCommModemStatus(port->hdl, &bits) == 0)
c33efc48 2092 RETURN_FAIL("GetCommModemStatus() failed");
8cf7c697
ML
2093 if (bits & MS_CTS_ON)
2094 *signals |= SP_SIG_CTS;
2095 if (bits & MS_DSR_ON)
2096 *signals |= SP_SIG_DSR;
8cf7c697 2097 if (bits & MS_RLSD_ON)
a6cda1e8
ML
2098 *signals |= SP_SIG_DCD;
2099 if (bits & MS_RING_ON)
8cf7c697
ML
2100 *signals |= SP_SIG_RI;
2101#else
2102 int bits;
2103 if (ioctl(port->fd, TIOCMGET, &bits) < 0)
c33efc48 2104 RETURN_FAIL("TIOCMGET ioctl failed");
8cf7c697
ML
2105 if (bits & TIOCM_CTS)
2106 *signals |= SP_SIG_CTS;
2107 if (bits & TIOCM_DSR)
2108 *signals |= SP_SIG_DSR;
2109 if (bits & TIOCM_CAR)
2110 *signals |= SP_SIG_DCD;
2111 if (bits & TIOCM_RNG)
2112 *signals |= SP_SIG_RI;
2113#endif
c33efc48 2114 RETURN_OK();
8cf7c697
ML
2115}
2116
90cc3ee6
ML
2117enum sp_return sp_start_break(struct sp_port *port)
2118{
c33efc48
ML
2119 TRACE("%p", port);
2120
dec10e31 2121 CHECK_OPEN_PORT();
90cc3ee6
ML
2122#ifdef _WIN32
2123 if (SetCommBreak(port->hdl) == 0)
c33efc48 2124 RETURN_FAIL("SetCommBreak() failed");
90cc3ee6
ML
2125#else
2126 if (ioctl(port->fd, TIOCSBRK, 1) < 0)
c33efc48 2127 RETURN_FAIL("TIOCSBRK ioctl failed");
90cc3ee6
ML
2128#endif
2129
c33efc48 2130 RETURN_OK();
90cc3ee6
ML
2131}
2132
2133enum sp_return sp_end_break(struct sp_port *port)
2134{
c33efc48
ML
2135 TRACE("%p", port);
2136
dec10e31 2137 CHECK_OPEN_PORT();
90cc3ee6
ML
2138#ifdef _WIN32
2139 if (ClearCommBreak(port->hdl) == 0)
c33efc48 2140 RETURN_FAIL("ClearCommBreak() failed");
90cc3ee6
ML
2141#else
2142 if (ioctl(port->fd, TIOCCBRK, 1) < 0)
c33efc48 2143 RETURN_FAIL("TIOCCBRK ioctl failed");
90cc3ee6
ML
2144#endif
2145
c33efc48 2146 RETURN_OK();
90cc3ee6
ML
2147}
2148
74510d4b
ML
2149int sp_last_error_code(void)
2150{
c33efc48 2151 TRACE("");
74510d4b 2152#ifdef _WIN32
c33efc48 2153 RETURN_VALUE("%d", GetLastError());
74510d4b 2154#else
c33efc48 2155 RETURN_VALUE("%d", errno);
74510d4b
ML
2156#endif
2157}
2158
74510d4b
ML
2159char *sp_last_error_message(void)
2160{
c33efc48
ML
2161 TRACE("");
2162
74510d4b
ML
2163#ifdef _WIN32
2164 LPVOID message;
2165 DWORD error = GetLastError();
2166
2167 FormatMessage(
2168 FORMAT_MESSAGE_ALLOCATE_BUFFER |
2169 FORMAT_MESSAGE_FROM_SYSTEM |
2170 FORMAT_MESSAGE_IGNORE_INSERTS,
2171 NULL,
2172 error,
2173 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
2174 (LPTSTR) &message,
2175 0, NULL );
2176
c33efc48 2177 RETURN_VALUE("%s", message);
74510d4b 2178#else
c33efc48 2179 RETURN_VALUE("%s", strerror(errno));
74510d4b
ML
2180#endif
2181}
2182
74510d4b
ML
2183void sp_free_error_message(char *message)
2184{
c33efc48
ML
2185 TRACE("%s", message);
2186
74510d4b
ML
2187#ifdef _WIN32
2188 LocalFree(message);
64eec30d
ML
2189#else
2190 (void)message;
74510d4b 2191#endif
c33efc48
ML
2192
2193 RETURN();
74510d4b 2194}
863b35e6
ML
2195
2196void sp_set_debug_handler(void (*handler)(const char *format, ...))
2197{
c33efc48
ML
2198 TRACE("%p", handler);
2199
863b35e6 2200 sp_debug_handler = handler;
c33efc48
ML
2201
2202 RETURN();
863b35e6
ML
2203}
2204
2205void sp_default_debug_handler(const char *format, ...)
2206{
2207 va_list args;
2208 va_start(args, format);
2209 if (getenv("LIBSERIALPORT_DEBUG")) {
dd7742fb 2210 fputs("sp: ", stderr);
863b35e6
ML
2211 vfprintf(stderr, format, args);
2212 }
2213 va_end(args);
2214}