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