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