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