2 * This file is part of the libserialport project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2010-2015 Uwe Hermann <uwe@hermann-uwe.de>
6 * Copyright (C) 2013-2015 Martin Ling <martin-libserialport@earth.li>
7 * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
8 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
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.
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.
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/>.
24 #include "libserialport_internal.h"
26 static const struct std_baudrate std_baudrates
[] = {
29 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
30 * have documented CBR_* macros.
32 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
33 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
34 BAUD(115200), BAUD(128000), BAUD(256000),
36 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
37 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
38 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
40 #if !defined(__APPLE__) && !defined(__OpenBSD__)
46 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
48 void (*sp_debug_handler
)(const char *format
, ...) = sp_default_debug_handler
;
50 static enum sp_return
get_config(struct sp_port
*port
, struct port_data
*data
,
51 struct sp_port_config
*config
);
53 static enum sp_return
set_config(struct sp_port
*port
, struct port_data
*data
,
54 const struct sp_port_config
*config
);
56 SP_API
enum sp_return
sp_get_port_by_name(const char *portname
, struct sp_port
**port_ptr
)
59 #ifndef NO_PORT_METADATA
64 TRACE("%s, %p", portname
, port_ptr
);
67 RETURN_ERROR(SP_ERR_ARG
, "Null result pointer");
72 RETURN_ERROR(SP_ERR_ARG
, "Null port name");
74 DEBUG_FMT("Building structure for port %s", portname
);
76 #if !defined(_WIN32) && defined(HAVE_REALPATH)
78 * get_port_details() below tries to be too smart and figure out
79 * some transport properties from the port name which breaks with
80 * symlinks. Therefore we canonicalize the portname first.
82 char pathbuf
[PATH_MAX
+ 1];
83 char *res
= realpath(portname
, pathbuf
);
85 RETURN_ERROR(SP_ERR_ARG
, "Could not retrieve realpath behind port name");
90 if (!(port
= malloc(sizeof(struct sp_port
))))
91 RETURN_ERROR(SP_ERR_MEM
, "Port structure malloc failed");
93 len
= strlen(portname
) + 1;
95 if (!(port
->name
= malloc(len
))) {
97 RETURN_ERROR(SP_ERR_MEM
, "Port name malloc failed");
100 memcpy(port
->name
, portname
, len
);
103 port
->usb_path
= NULL
;
104 port
->hdl
= INVALID_HANDLE_VALUE
;
105 port
->write_buf
= NULL
;
106 port
->write_buf_size
= 0;
111 port
->description
= NULL
;
112 port
->transport
= SP_TRANSPORT_NATIVE
;
114 port
->usb_address
= -1;
117 port
->usb_manufacturer
= NULL
;
118 port
->usb_product
= NULL
;
119 port
->usb_serial
= NULL
;
120 port
->bluetooth_address
= NULL
;
122 #ifndef NO_PORT_METADATA
123 if ((ret
= get_port_details(port
)) != SP_OK
) {
134 SP_API
char *sp_get_port_name(const struct sp_port
*port
)
141 RETURN_STRING(port
->name
);
144 SP_API
char *sp_get_port_description(const struct sp_port
*port
)
148 if (!port
|| !port
->description
)
151 RETURN_STRING(port
->description
);
154 SP_API
enum sp_transport
sp_get_port_transport(const struct sp_port
*port
)
159 RETURN_ERROR(SP_ERR_ARG
, "Null port");
161 RETURN_INT(port
->transport
);
164 SP_API
enum sp_return
sp_get_port_usb_bus_address(const struct sp_port
*port
,
165 int *usb_bus
,int *usb_address
)
170 RETURN_ERROR(SP_ERR_ARG
, "Null port");
171 if (port
->transport
!= SP_TRANSPORT_USB
)
172 RETURN_ERROR(SP_ERR_ARG
, "Port does not use USB transport");
173 if (port
->usb_bus
< 0 || port
->usb_address
< 0)
174 RETURN_ERROR(SP_ERR_SUPP
, "Bus and address values are not available");
177 *usb_bus
= port
->usb_bus
;
179 *usb_address
= port
->usb_address
;
184 SP_API
enum sp_return
sp_get_port_usb_vid_pid(const struct sp_port
*port
,
185 int *usb_vid
, int *usb_pid
)
190 RETURN_ERROR(SP_ERR_ARG
, "Null port");
191 if (port
->transport
!= SP_TRANSPORT_USB
)
192 RETURN_ERROR(SP_ERR_ARG
, "Port does not use USB transport");
193 if (port
->usb_vid
< 0 || port
->usb_pid
< 0)
194 RETURN_ERROR(SP_ERR_SUPP
, "VID:PID values are not available");
197 *usb_vid
= port
->usb_vid
;
199 *usb_pid
= port
->usb_pid
;
204 SP_API
char *sp_get_port_usb_manufacturer(const struct sp_port
*port
)
208 if (!port
|| port
->transport
!= SP_TRANSPORT_USB
|| !port
->usb_manufacturer
)
211 RETURN_STRING(port
->usb_manufacturer
);
214 SP_API
char *sp_get_port_usb_product(const struct sp_port
*port
)
218 if (!port
|| port
->transport
!= SP_TRANSPORT_USB
|| !port
->usb_product
)
221 RETURN_STRING(port
->usb_product
);
224 SP_API
char *sp_get_port_usb_serial(const struct sp_port
*port
)
228 if (!port
|| port
->transport
!= SP_TRANSPORT_USB
|| !port
->usb_serial
)
231 RETURN_STRING(port
->usb_serial
);
234 SP_API
char *sp_get_port_bluetooth_address(const struct sp_port
*port
)
238 if (!port
|| port
->transport
!= SP_TRANSPORT_BLUETOOTH
239 || !port
->bluetooth_address
)
242 RETURN_STRING(port
->bluetooth_address
);
245 SP_API
enum sp_return
sp_get_port_handle(const struct sp_port
*port
,
248 TRACE("%p, %p", port
, result_ptr
);
251 RETURN_ERROR(SP_ERR_ARG
, "Null port");
253 RETURN_ERROR(SP_ERR_ARG
, "Null result pointer");
256 HANDLE
*handle_ptr
= result_ptr
;
257 *handle_ptr
= port
->hdl
;
259 int *fd_ptr
= result_ptr
;
266 SP_API
enum sp_return
sp_copy_port(const struct sp_port
*port
,
267 struct sp_port
**copy_ptr
)
269 TRACE("%p, %p", port
, copy_ptr
);
272 RETURN_ERROR(SP_ERR_ARG
, "Null result pointer");
277 RETURN_ERROR(SP_ERR_ARG
, "Null port");
280 RETURN_ERROR(SP_ERR_ARG
, "Null port name");
282 DEBUG("Copying port structure");
284 RETURN_INT(sp_get_port_by_name(port
->name
, copy_ptr
));
287 SP_API
void sp_free_port(struct sp_port
*port
)
296 DEBUG("Freeing port structure");
300 if (port
->description
)
301 free(port
->description
);
302 if (port
->usb_manufacturer
)
303 free(port
->usb_manufacturer
);
304 if (port
->usb_product
)
305 free(port
->usb_product
);
306 if (port
->usb_serial
)
307 free(port
->usb_serial
);
308 if (port
->bluetooth_address
)
309 free(port
->bluetooth_address
);
312 free(port
->usb_path
);
314 free(port
->write_buf
);
322 SP_PRIV
struct sp_port
**list_append(struct sp_port
**list
,
323 const char *portname
)
328 for (count
= 0; list
[count
]; count
++)
330 if (!(tmp
= realloc(list
, sizeof(struct sp_port
*) * (count
+ 2))))
333 if (sp_get_port_by_name(portname
, &list
[count
]) != SP_OK
)
335 list
[count
+ 1] = NULL
;
339 sp_free_port_list(list
);
343 SP_API
enum sp_return
sp_list_ports(struct sp_port
***list_ptr
)
345 #ifndef NO_ENUMERATION
346 struct sp_port
**list
;
350 TRACE("%p", list_ptr
);
353 RETURN_ERROR(SP_ERR_ARG
, "Null result pointer");
357 #ifdef NO_ENUMERATION
358 RETURN_ERROR(SP_ERR_SUPP
, "Enumeration not supported on this platform");
360 DEBUG("Enumerating ports");
362 if (!(list
= malloc(sizeof(struct sp_port
*))))
363 RETURN_ERROR(SP_ERR_MEM
, "Port list malloc failed");
367 ret
= list_ports(&list
);
372 sp_free_port_list(list
);
380 SP_API
void sp_free_port_list(struct sp_port
**list
)
391 DEBUG("Freeing port list");
393 for (i
= 0; list
[i
]; i
++)
394 sp_free_port(list
[i
]);
400 #define CHECK_PORT() do { \
402 RETURN_ERROR(SP_ERR_ARG, "Null port"); \
404 RETURN_ERROR(SP_ERR_ARG, "Null port name"); \
407 #define CHECK_PORT_HANDLE() do { \
408 if (port->hdl == INVALID_HANDLE_VALUE) \
409 RETURN_ERROR(SP_ERR_ARG, "Port not open"); \
412 #define CHECK_PORT_HANDLE() do { \
414 RETURN_ERROR(SP_ERR_ARG, "Port not open"); \
417 #define CHECK_OPEN_PORT() do { \
419 CHECK_PORT_HANDLE(); \
423 /** To be called after port receive buffer is emptied. */
424 static enum sp_return
restart_wait(struct sp_port
*port
)
428 if (port
->wait_running
) {
429 /* Check status of running wait operation. */
430 if (GetOverlappedResult(port
->hdl
, &port
->wait_ovl
,
431 &wait_result
, FALSE
)) {
432 DEBUG("Previous wait completed");
433 port
->wait_running
= FALSE
;
434 } else if (GetLastError() == ERROR_IO_INCOMPLETE
) {
435 DEBUG("Previous wait still running");
438 RETURN_FAIL("GetOverlappedResult() failed");
442 if (!port
->wait_running
) {
443 /* Start new wait operation. */
444 if (WaitCommEvent(port
->hdl
, &port
->events
,
446 DEBUG("New wait returned, events already pending");
447 } else if (GetLastError() == ERROR_IO_PENDING
) {
448 DEBUG("New wait running in background");
449 port
->wait_running
= TRUE
;
451 RETURN_FAIL("WaitCommEvent() failed");
459 SP_API
enum sp_return
sp_open(struct sp_port
*port
, enum sp_mode flags
)
461 struct port_data data
;
462 struct sp_port_config config
;
465 TRACE("%p, 0x%x", port
, flags
);
469 if (flags
> SP_MODE_READ_WRITE
)
470 RETURN_ERROR(SP_ERR_ARG
, "Invalid flags");
472 DEBUG_FMT("Opening port %s", port
->name
);
475 DWORD desired_access
= 0, flags_and_attributes
= 0, errors
;
476 char *escaped_port_name
;
479 /* Prefix port name with '\\.\' to work with ports above COM9. */
480 if (!(escaped_port_name
= malloc(strlen(port
->name
) + 5)))
481 RETURN_ERROR(SP_ERR_MEM
, "Escaped port name malloc failed");
482 sprintf(escaped_port_name
, "\\\\.\\%s", port
->name
);
484 /* Map 'flags' to the OS-specific settings. */
485 flags_and_attributes
= FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_OVERLAPPED
;
486 if (flags
& SP_MODE_READ
)
487 desired_access
|= GENERIC_READ
;
488 if (flags
& SP_MODE_WRITE
)
489 desired_access
|= GENERIC_WRITE
;
491 port
->hdl
= CreateFileA(escaped_port_name
, desired_access
, 0, 0,
492 OPEN_EXISTING
, flags_and_attributes
, 0);
494 free(escaped_port_name
);
496 if (port
->hdl
== INVALID_HANDLE_VALUE
)
497 RETURN_FAIL("Port CreateFile() failed");
499 /* All timeouts initially disabled. */
500 port
->timeouts
.ReadIntervalTimeout
= 0;
501 port
->timeouts
.ReadTotalTimeoutMultiplier
= 0;
502 port
->timeouts
.ReadTotalTimeoutConstant
= 0;
503 port
->timeouts
.WriteTotalTimeoutMultiplier
= 0;
504 port
->timeouts
.WriteTotalTimeoutConstant
= 0;
506 if (SetCommTimeouts(port
->hdl
, &port
->timeouts
) == 0) {
508 RETURN_FAIL("SetCommTimeouts() failed");
511 /* Prepare OVERLAPPED structures. */
512 #define INIT_OVERLAPPED(ovl) do { \
513 memset(&port->ovl, 0, sizeof(port->ovl)); \
514 port->ovl.hEvent = INVALID_HANDLE_VALUE; \
515 if ((port->ovl.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL)) \
516 == INVALID_HANDLE_VALUE) { \
518 RETURN_FAIL(#ovl "CreateEvent() failed"); \
522 INIT_OVERLAPPED(read_ovl
);
523 INIT_OVERLAPPED(write_ovl
);
524 INIT_OVERLAPPED(wait_ovl
);
526 /* Set event mask for RX and error events. */
527 if (SetCommMask(port
->hdl
, EV_RXCHAR
| EV_ERR
) == 0) {
529 RETURN_FAIL("SetCommMask() failed");
532 port
->writing
= FALSE
;
533 port
->wait_running
= FALSE
;
535 ret
= restart_wait(port
);
542 int flags_local
= O_NONBLOCK
| O_NOCTTY
| O_CLOEXEC
;
544 /* Map 'flags' to the OS-specific settings. */
545 if ((flags
& SP_MODE_READ_WRITE
) == SP_MODE_READ_WRITE
)
546 flags_local
|= O_RDWR
;
547 else if (flags
& SP_MODE_READ
)
548 flags_local
|= O_RDONLY
;
549 else if (flags
& SP_MODE_WRITE
)
550 flags_local
|= O_WRONLY
;
552 if ((port
->fd
= open(port
->name
, flags_local
)) < 0)
553 RETURN_FAIL("open() failed");
556 ret
= get_config(port
, &data
, &config
);
563 /* Set sane port settings. */
565 data
.dcb
.fBinary
= TRUE
;
566 data
.dcb
.fDsrSensitivity
= FALSE
;
567 data
.dcb
.fErrorChar
= FALSE
;
568 data
.dcb
.fNull
= FALSE
;
569 data
.dcb
.fAbortOnError
= FALSE
;
571 /* Turn off all fancy termios tricks, give us a raw channel. */
572 data
.term
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
| IGNCR
| ICRNL
| IMAXBEL
);
574 data
.term
.c_iflag
&= ~IUCLC
;
576 data
.term
.c_oflag
&= ~(OPOST
| ONLCR
| OCRNL
| ONOCR
| ONLRET
);
578 data
.term
.c_oflag
&= ~OLCUC
;
581 data
.term
.c_oflag
&= ~NLDLY
;
584 data
.term
.c_oflag
&= ~CRDLY
;
587 data
.term
.c_oflag
&= ~TABDLY
;
590 data
.term
.c_oflag
&= ~BSDLY
;
593 data
.term
.c_oflag
&= ~VTDLY
;
596 data
.term
.c_oflag
&= ~FFDLY
;
599 data
.term
.c_oflag
&= ~OFILL
;
601 data
.term
.c_lflag
&= ~(ISIG
| ICANON
| ECHO
| IEXTEN
);
602 data
.term
.c_cc
[VMIN
] = 0;
603 data
.term
.c_cc
[VTIME
] = 0;
605 /* Ignore modem status lines; enable receiver; leave control lines alone on close. */
606 data
.term
.c_cflag
|= (CLOCAL
| CREAD
| HUPCL
);
610 if (ClearCommError(port
->hdl
, &errors
, &status
) == 0)
611 RETURN_FAIL("ClearCommError() failed");
614 ret
= set_config(port
, &data
, &config
);
624 SP_API
enum sp_return
sp_close(struct sp_port
*port
)
630 DEBUG_FMT("Closing port %s", port
->name
);
633 /* Returns non-zero upon success, 0 upon failure. */
634 if (CloseHandle(port
->hdl
) == 0)
635 RETURN_FAIL("Port CloseHandle() failed");
636 port
->hdl
= INVALID_HANDLE_VALUE
;
638 /* Close event handles for overlapped structures. */
639 #define CLOSE_OVERLAPPED(ovl) do { \
640 if (port->ovl.hEvent != INVALID_HANDLE_VALUE && \
641 CloseHandle(port->ovl.hEvent) == 0) \
642 RETURN_FAIL(# ovl "event CloseHandle() failed"); \
644 CLOSE_OVERLAPPED(read_ovl
);
645 CLOSE_OVERLAPPED(write_ovl
);
646 CLOSE_OVERLAPPED(wait_ovl
);
648 if (port
->write_buf
) {
649 free(port
->write_buf
);
650 port
->write_buf
= NULL
;
653 /* Returns 0 upon success, -1 upon failure. */
654 if (close(port
->fd
) == -1)
655 RETURN_FAIL("close() failed");
662 SP_API
enum sp_return
sp_flush(struct sp_port
*port
, enum sp_buffer buffers
)
664 TRACE("%p, 0x%x", port
, buffers
);
668 if (buffers
> SP_BUF_BOTH
)
669 RETURN_ERROR(SP_ERR_ARG
, "Invalid buffer selection");
671 const char *buffer_names
[] = {"no", "input", "output", "both"};
673 DEBUG_FMT("Flushing %s buffers on port %s",
674 buffer_names
[buffers
], port
->name
);
678 if (buffers
& SP_BUF_INPUT
)
679 flags
|= PURGE_RXCLEAR
;
680 if (buffers
& SP_BUF_OUTPUT
)
681 flags
|= PURGE_TXCLEAR
;
683 /* Returns non-zero upon success, 0 upon failure. */
684 if (PurgeComm(port
->hdl
, flags
) == 0)
685 RETURN_FAIL("PurgeComm() failed");
687 if (buffers
& SP_BUF_INPUT
)
688 TRY(restart_wait(port
));
691 if (buffers
== SP_BUF_BOTH
)
693 else if (buffers
== SP_BUF_INPUT
)
695 else if (buffers
== SP_BUF_OUTPUT
)
698 /* Returns 0 upon success, -1 upon failure. */
699 if (tcflush(port
->fd
, flags
) < 0)
700 RETURN_FAIL("tcflush() failed");
705 SP_API
enum sp_return
sp_drain(struct sp_port
*port
)
711 DEBUG_FMT("Draining port %s", port
->name
);
714 /* Returns non-zero upon success, 0 upon failure. */
715 if (FlushFileBuffers(port
->hdl
) == 0)
716 RETURN_FAIL("FlushFileBuffers() failed");
721 #if defined(__ANDROID__) && (__ANDROID_API__ < 21)
722 /* Android only has tcdrain from platform 21 onwards.
723 * On previous API versions, use the ioctl directly. */
725 result
= ioctl(port
->fd
, TCSBRK
, &arg
);
727 result
= tcdrain(port
->fd
);
730 if (errno
== EINTR
) {
731 DEBUG("tcdrain() was interrupted");
734 RETURN_FAIL("tcdrain() failed");
744 static enum sp_return
await_write_completion(struct sp_port
*port
)
750 /* Wait for previous non-blocking write to complete, if any. */
752 DEBUG("Waiting for previous write to complete");
753 result
= GetOverlappedResult(port
->hdl
, &port
->write_ovl
, &bytes_written
, TRUE
);
756 RETURN_FAIL("Previous write failed to complete");
757 DEBUG("Previous write completed");
764 SP_API
enum sp_return
sp_blocking_write(struct sp_port
*port
, const void *buf
,
765 size_t count
, unsigned int timeout_ms
)
767 TRACE("%p, %p, %d, %d", port
, buf
, count
, timeout_ms
);
772 RETURN_ERROR(SP_ERR_ARG
, "Null buffer");
775 DEBUG_FMT("Writing %d bytes to port %s, timeout %d ms",
776 count
, port
->name
, timeout_ms
);
778 DEBUG_FMT("Writing %d bytes to port %s, no timeout",
785 DWORD remaining_ms
, write_size
, bytes_written
;
786 size_t remaining_bytes
, total_bytes_written
= 0;
787 const uint8_t *write_ptr
= (uint8_t *) buf
;
789 struct timeout timeout
;
791 timeout_start(&timeout
, timeout_ms
);
793 TRY(await_write_completion(port
));
795 while (total_bytes_written
< count
) {
797 if (timeout_check(&timeout
))
800 remaining_ms
= timeout_remaining_ms(&timeout
);
802 if (port
->timeouts
.WriteTotalTimeoutConstant
!= remaining_ms
) {
803 port
->timeouts
.WriteTotalTimeoutConstant
= remaining_ms
;
804 if (SetCommTimeouts(port
->hdl
, &port
->timeouts
) == 0)
805 RETURN_FAIL("SetCommTimeouts() failed");
808 /* Reduce write size if it exceeds the WriteFile limit. */
809 remaining_bytes
= count
- total_bytes_written
;
810 if (remaining_bytes
> WRITEFILE_MAX_SIZE
)
811 write_size
= WRITEFILE_MAX_SIZE
;
813 write_size
= (DWORD
) remaining_bytes
;
817 result
= WriteFile(port
->hdl
, write_ptr
, write_size
, NULL
, &port
->write_ovl
);
819 timeout_update(&timeout
);
822 DEBUG("Write completed immediately");
823 bytes_written
= write_size
;
824 } else if (GetLastError() == ERROR_IO_PENDING
) {
825 DEBUG("Waiting for write to complete");
826 if (GetOverlappedResult(port
->hdl
, &port
->write_ovl
, &bytes_written
, TRUE
) == 0) {
827 if (GetLastError() == ERROR_SEM_TIMEOUT
) {
828 DEBUG("Write timed out");
831 RETURN_FAIL("GetOverlappedResult() failed");
834 DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written
, write_size
);
836 RETURN_FAIL("WriteFile() failed");
839 write_ptr
+= bytes_written
;
840 total_bytes_written
+= bytes_written
;
843 RETURN_INT((int) total_bytes_written
);
845 size_t bytes_written
= 0;
846 unsigned char *ptr
= (unsigned char *) buf
;
847 struct timeout timeout
;
851 timeout_start(&timeout
, timeout_ms
);
854 FD_SET(port
->fd
, &fds
);
856 /* Loop until we have written the requested number of bytes. */
857 while (bytes_written
< count
) {
859 if (timeout_check(&timeout
))
862 result
= select(port
->fd
+ 1, NULL
, &fds
, NULL
, timeout_timeval(&timeout
));
864 timeout_update(&timeout
);
867 if (errno
== EINTR
) {
868 DEBUG("select() call was interrupted, repeating");
871 RETURN_FAIL("select() failed");
873 } else if (result
== 0) {
874 /* Timeout has expired. */
879 result
= write(port
->fd
, ptr
, count
- bytes_written
);
883 /* This shouldn't happen because we did a select() first, but handle anyway. */
886 /* This is an actual failure. */
887 RETURN_FAIL("write() failed");
890 bytes_written
+= result
;
894 if (bytes_written
< count
)
895 DEBUG("Write timed out");
897 RETURN_INT(bytes_written
);
901 SP_API
enum sp_return
sp_nonblocking_write(struct sp_port
*port
,
902 const void *buf
, size_t count
)
904 TRACE("%p, %p, %d", port
, buf
, count
);
909 RETURN_ERROR(SP_ERR_ARG
, "Null buffer");
911 DEBUG_FMT("Writing up to %d bytes to port %s", count
, port
->name
);
919 /* Check whether previous write is complete. */
921 if (HasOverlappedIoCompleted(&port
->write_ovl
)) {
922 DEBUG("Previous write completed");
925 DEBUG("Previous write not complete");
926 /* Can't take a new write until the previous one finishes. */
932 if (port
->timeouts
.WriteTotalTimeoutConstant
!= 0) {
933 port
->timeouts
.WriteTotalTimeoutConstant
= 0;
934 if (SetCommTimeouts(port
->hdl
, &port
->timeouts
) == 0)
935 RETURN_FAIL("SetCommTimeouts() failed");
938 /* Reduce count if it exceeds the WriteFile limit. */
939 if (count
> WRITEFILE_MAX_SIZE
)
940 count
= WRITEFILE_MAX_SIZE
;
942 /* Copy data to our write buffer. */
943 buf_bytes
= min(port
->write_buf_size
, count
);
944 memcpy(port
->write_buf
, buf
, buf_bytes
);
946 /* Start asynchronous write. */
947 if (WriteFile(port
->hdl
, port
->write_buf
, (DWORD
) buf_bytes
, NULL
, &port
->write_ovl
) == 0) {
948 if (GetLastError() == ERROR_IO_PENDING
) {
949 if ((port
->writing
= !HasOverlappedIoCompleted(&port
->write_ovl
)))
950 DEBUG("Asynchronous write completed immediately");
952 DEBUG("Asynchronous write running");
954 /* Actual failure of some kind. */
955 RETURN_FAIL("WriteFile() failed");
959 DEBUG("All bytes written immediately");
961 RETURN_INT((int) buf_bytes
);
963 /* Returns the number of bytes written, or -1 upon failure. */
964 ssize_t written
= write(port
->fd
, buf
, count
);
968 // Buffer is full, no bytes written.
971 RETURN_FAIL("write() failed");
979 /* Restart wait operation if buffer was emptied. */
980 static enum sp_return
restart_wait_if_needed(struct sp_port
*port
, unsigned int bytes_read
)
988 if (ClearCommError(port
->hdl
, &errors
, &comstat
) == 0)
989 RETURN_FAIL("ClearCommError() failed");
991 if (comstat
.cbInQue
== 0)
992 TRY(restart_wait(port
));
998 SP_API
enum sp_return
sp_blocking_read(struct sp_port
*port
, void *buf
,
999 size_t count
, unsigned int timeout_ms
)
1001 TRACE("%p, %p, %d, %d", port
, buf
, count
, timeout_ms
);
1006 RETURN_ERROR(SP_ERR_ARG
, "Null buffer");
1009 DEBUG_FMT("Reading %d bytes from port %s, timeout %d ms",
1010 count
, port
->name
, timeout_ms
);
1012 DEBUG_FMT("Reading %d bytes from port %s, no timeout",
1022 if (port
->timeouts
.ReadIntervalTimeout
!= 0 ||
1023 port
->timeouts
.ReadTotalTimeoutMultiplier
!= 0 ||
1024 port
->timeouts
.ReadTotalTimeoutConstant
!= timeout_ms
) {
1025 port
->timeouts
.ReadIntervalTimeout
= 0;
1026 port
->timeouts
.ReadTotalTimeoutMultiplier
= 0;
1027 port
->timeouts
.ReadTotalTimeoutConstant
= timeout_ms
;
1028 if (SetCommTimeouts(port
->hdl
, &port
->timeouts
) == 0)
1029 RETURN_FAIL("SetCommTimeouts() failed");
1033 if (ReadFile(port
->hdl
, buf
, (DWORD
) count
, NULL
, &port
->read_ovl
)) {
1034 DEBUG("Read completed immediately");
1035 bytes_read
= (DWORD
) count
;
1036 } else if (GetLastError() == ERROR_IO_PENDING
) {
1037 DEBUG("Waiting for read to complete");
1038 if (GetOverlappedResult(port
->hdl
, &port
->read_ovl
, &bytes_read
, TRUE
) == 0)
1039 RETURN_FAIL("GetOverlappedResult() failed");
1040 DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read
, count
);
1042 RETURN_FAIL("ReadFile() failed");
1045 TRY(restart_wait_if_needed(port
, bytes_read
));
1047 RETURN_INT((int) bytes_read
);
1050 size_t bytes_read
= 0;
1051 unsigned char *ptr
= (unsigned char *) buf
;
1052 struct timeout timeout
;
1056 timeout_start(&timeout
, timeout_ms
);
1059 FD_SET(port
->fd
, &fds
);
1061 /* Loop until we have the requested number of bytes. */
1062 while (bytes_read
< count
) {
1064 if (timeout_check(&timeout
))
1065 /* Timeout has expired. */
1068 result
= select(port
->fd
+ 1, &fds
, NULL
, NULL
, timeout_timeval(&timeout
));
1070 timeout_update(&timeout
);
1073 if (errno
== EINTR
) {
1074 DEBUG("select() call was interrupted, repeating");
1077 RETURN_FAIL("select() failed");
1079 } else if (result
== 0) {
1080 /* Timeout has expired. */
1085 result
= read(port
->fd
, ptr
, count
- bytes_read
);
1088 if (errno
== EAGAIN
)
1090 * This shouldn't happen because we did a
1091 * select() first, but handle anyway.
1095 /* This is an actual failure. */
1096 RETURN_FAIL("read() failed");
1099 bytes_read
+= result
;
1103 if (bytes_read
< count
)
1104 DEBUG("Read timed out");
1106 RETURN_INT(bytes_read
);
1110 SP_API
enum sp_return
sp_blocking_read_next(struct sp_port
*port
, void *buf
,
1111 size_t count
, unsigned int timeout_ms
)
1113 TRACE("%p, %p, %d, %d", port
, buf
, count
, timeout_ms
);
1118 RETURN_ERROR(SP_ERR_ARG
, "Null buffer");
1121 RETURN_ERROR(SP_ERR_ARG
, "Zero count");
1124 DEBUG_FMT("Reading next max %d bytes from port %s, timeout %d ms",
1125 count
, port
->name
, timeout_ms
);
1127 DEBUG_FMT("Reading next max %d bytes from port %s, no timeout",
1131 DWORD bytes_read
= 0;
1133 /* If timeout_ms == 0, set maximum timeout. */
1134 DWORD timeout_val
= (timeout_ms
== 0 ? MAXDWORD
- 1 : timeout_ms
);
1137 if (port
->timeouts
.ReadIntervalTimeout
!= MAXDWORD
||
1138 port
->timeouts
.ReadTotalTimeoutMultiplier
!= MAXDWORD
||
1139 port
->timeouts
.ReadTotalTimeoutConstant
!= timeout_val
) {
1140 port
->timeouts
.ReadIntervalTimeout
= MAXDWORD
;
1141 port
->timeouts
.ReadTotalTimeoutMultiplier
= MAXDWORD
;
1142 port
->timeouts
.ReadTotalTimeoutConstant
= timeout_val
;
1143 if (SetCommTimeouts(port
->hdl
, &port
->timeouts
) == 0)
1144 RETURN_FAIL("SetCommTimeouts() failed");
1147 /* Loop until we have at least one byte, or timeout is reached. */
1148 while (bytes_read
== 0) {
1150 if (ReadFile(port
->hdl
, buf
, (DWORD
) count
, &bytes_read
, &port
->read_ovl
)) {
1151 DEBUG("Read completed immediately");
1152 } else if (GetLastError() == ERROR_IO_PENDING
) {
1153 DEBUG("Waiting for read to complete");
1154 if (GetOverlappedResult(port
->hdl
, &port
->read_ovl
, &bytes_read
, TRUE
) == 0)
1155 RETURN_FAIL("GetOverlappedResult() failed");
1156 if (bytes_read
> 0) {
1157 DEBUG("Read completed");
1158 } else if (timeout_ms
> 0) {
1159 DEBUG("Read timed out");
1162 DEBUG("Restarting read");
1165 RETURN_FAIL("ReadFile() failed");
1169 TRY(restart_wait_if_needed(port
, bytes_read
));
1171 RETURN_INT(bytes_read
);
1174 size_t bytes_read
= 0;
1175 struct timeout timeout
;
1179 timeout_start(&timeout
, timeout_ms
);
1182 FD_SET(port
->fd
, &fds
);
1184 /* Loop until we have at least one byte, or timeout is reached. */
1185 while (bytes_read
== 0) {
1187 if (timeout_check(&timeout
))
1188 /* Timeout has expired. */
1191 result
= select(port
->fd
+ 1, &fds
, NULL
, NULL
, timeout_timeval(&timeout
));
1193 timeout_update(&timeout
);
1196 if (errno
== EINTR
) {
1197 DEBUG("select() call was interrupted, repeating");
1200 RETURN_FAIL("select() failed");
1202 } else if (result
== 0) {
1203 /* Timeout has expired. */
1208 result
= read(port
->fd
, buf
, count
);
1211 if (errno
== EAGAIN
)
1212 /* This shouldn't happen because we did a select() first, but handle anyway. */
1215 /* This is an actual failure. */
1216 RETURN_FAIL("read() failed");
1219 bytes_read
= result
;
1222 if (bytes_read
== 0)
1223 DEBUG("Read timed out");
1225 RETURN_INT(bytes_read
);
1229 SP_API
enum sp_return
sp_nonblocking_read(struct sp_port
*port
, void *buf
,
1232 TRACE("%p, %p, %d", port
, buf
, count
);
1237 RETURN_ERROR(SP_ERR_ARG
, "Null buffer");
1239 DEBUG_FMT("Reading up to %d bytes from port %s", count
, port
->name
);
1245 if (port
->timeouts
.ReadIntervalTimeout
!= MAXDWORD
||
1246 port
->timeouts
.ReadTotalTimeoutMultiplier
!= 0 ||
1247 port
->timeouts
.ReadTotalTimeoutConstant
!= 0) {
1248 port
->timeouts
.ReadIntervalTimeout
= MAXDWORD
;
1249 port
->timeouts
.ReadTotalTimeoutMultiplier
= 0;
1250 port
->timeouts
.ReadTotalTimeoutConstant
= 0;
1251 if (SetCommTimeouts(port
->hdl
, &port
->timeouts
) == 0)
1252 RETURN_FAIL("SetCommTimeouts() failed");
1256 if (ReadFile(port
->hdl
, buf
, (DWORD
) count
, NULL
, &port
->read_ovl
) == 0)
1257 if (GetLastError() != ERROR_IO_PENDING
)
1258 RETURN_FAIL("ReadFile() failed");
1260 /* Get number of bytes read. */
1261 if (GetOverlappedResult(port
->hdl
, &port
->read_ovl
, &bytes_read
, FALSE
) == 0)
1262 RETURN_FAIL("GetOverlappedResult() failed");
1264 TRY(restart_wait_if_needed(port
, bytes_read
));
1266 RETURN_INT(bytes_read
);
1270 /* Returns the number of bytes read, or -1 upon failure. */
1271 if ((bytes_read
= read(port
->fd
, buf
, count
)) < 0) {
1272 if (errno
== EAGAIN
)
1273 /* No bytes available. */
1276 /* This is an actual failure. */
1277 RETURN_FAIL("read() failed");
1279 RETURN_INT(bytes_read
);
1283 SP_API
enum sp_return
sp_input_waiting(struct sp_port
*port
)
1289 DEBUG_FMT("Checking input bytes waiting on port %s", port
->name
);
1295 if (ClearCommError(port
->hdl
, &errors
, &comstat
) == 0)
1296 RETURN_FAIL("ClearCommError() failed");
1297 RETURN_INT(comstat
.cbInQue
);
1300 if (ioctl(port
->fd
, TIOCINQ
, &bytes_waiting
) < 0)
1301 RETURN_FAIL("TIOCINQ ioctl failed");
1302 RETURN_INT(bytes_waiting
);
1306 SP_API
enum sp_return
sp_output_waiting(struct sp_port
*port
)
1312 DEBUG_FMT("Checking output bytes waiting on port %s", port
->name
);
1318 if (ClearCommError(port
->hdl
, &errors
, &comstat
) == 0)
1319 RETURN_FAIL("ClearCommError() failed");
1320 RETURN_INT(comstat
.cbOutQue
);
1323 if (ioctl(port
->fd
, TIOCOUTQ
, &bytes_waiting
) < 0)
1324 RETURN_FAIL("TIOCOUTQ ioctl failed");
1325 RETURN_INT(bytes_waiting
);
1329 SP_API
enum sp_return
sp_new_event_set(struct sp_event_set
**result_ptr
)
1331 struct sp_event_set
*result
;
1333 TRACE("%p", result_ptr
);
1336 RETURN_ERROR(SP_ERR_ARG
, "Null result");
1340 if (!(result
= malloc(sizeof(struct sp_event_set
))))
1341 RETURN_ERROR(SP_ERR_MEM
, "sp_event_set malloc() failed");
1343 memset(result
, 0, sizeof(struct sp_event_set
));
1345 *result_ptr
= result
;
1350 static enum sp_return
add_handle(struct sp_event_set
*event_set
,
1351 event_handle handle
, enum sp_event mask
)
1354 enum sp_event
*new_masks
;
1356 TRACE("%p, %d, %d", event_set
, handle
, mask
);
1358 if (!(new_handles
= realloc(event_set
->handles
,
1359 sizeof(event_handle
) * (event_set
->count
+ 1))))
1360 RETURN_ERROR(SP_ERR_MEM
, "Handle array realloc() failed");
1362 event_set
->handles
= new_handles
;
1364 if (!(new_masks
= realloc(event_set
->masks
,
1365 sizeof(enum sp_event
) * (event_set
->count
+ 1))))
1366 RETURN_ERROR(SP_ERR_MEM
, "Mask array realloc() failed");
1368 event_set
->masks
= new_masks
;
1370 ((event_handle
*) event_set
->handles
)[event_set
->count
] = handle
;
1371 event_set
->masks
[event_set
->count
] = mask
;
1378 SP_API
enum sp_return
sp_add_port_events(struct sp_event_set
*event_set
,
1379 const struct sp_port
*port
, enum sp_event mask
)
1381 TRACE("%p, %p, %d", event_set
, port
, mask
);
1384 RETURN_ERROR(SP_ERR_ARG
, "Null event set");
1387 RETURN_ERROR(SP_ERR_ARG
, "Null port");
1389 if (mask
> (SP_EVENT_RX_READY
| SP_EVENT_TX_READY
| SP_EVENT_ERROR
))
1390 RETURN_ERROR(SP_ERR_ARG
, "Invalid event mask");
1396 enum sp_event handle_mask
;
1397 if ((handle_mask
= mask
& SP_EVENT_TX_READY
))
1398 TRY(add_handle(event_set
, port
->write_ovl
.hEvent
, handle_mask
));
1399 if ((handle_mask
= mask
& (SP_EVENT_RX_READY
| SP_EVENT_ERROR
)))
1400 TRY(add_handle(event_set
, port
->wait_ovl
.hEvent
, handle_mask
));
1402 TRY(add_handle(event_set
, port
->fd
, mask
));
1408 SP_API
void sp_free_event_set(struct sp_event_set
*event_set
)
1410 TRACE("%p", event_set
);
1413 DEBUG("Null event set");
1417 DEBUG("Freeing event set");
1419 if (event_set
->handles
)
1420 free(event_set
->handles
);
1421 if (event_set
->masks
)
1422 free(event_set
->masks
);
1429 SP_API
enum sp_return
sp_wait(struct sp_event_set
*event_set
,
1430 unsigned int timeout_ms
)
1432 TRACE("%p, %d", event_set
, timeout_ms
);
1435 RETURN_ERROR(SP_ERR_ARG
, "Null event set");
1438 if (WaitForMultipleObjects(event_set
->count
, event_set
->handles
, FALSE
,
1439 timeout_ms
? timeout_ms
: INFINITE
) == WAIT_FAILED
)
1440 RETURN_FAIL("WaitForMultipleObjects() failed");
1444 struct timeout timeout
;
1446 struct pollfd
*pollfds
;
1449 if (!(pollfds
= malloc(sizeof(struct pollfd
) * event_set
->count
)))
1450 RETURN_ERROR(SP_ERR_MEM
, "pollfds malloc() failed");
1452 for (i
= 0; i
< event_set
->count
; i
++) {
1453 pollfds
[i
].fd
= ((int *)event_set
->handles
)[i
];
1454 pollfds
[i
].events
= 0;
1455 pollfds
[i
].revents
= 0;
1456 if (event_set
->masks
[i
] & SP_EVENT_RX_READY
)
1457 pollfds
[i
].events
|= POLLIN
;
1458 if (event_set
->masks
[i
] & SP_EVENT_TX_READY
)
1459 pollfds
[i
].events
|= POLLOUT
;
1460 if (event_set
->masks
[i
] & SP_EVENT_ERROR
)
1461 pollfds
[i
].events
|= POLLERR
;
1464 timeout_start(&timeout
, timeout_ms
);
1465 timeout_limit(&timeout
, INT_MAX
);
1467 /* Loop until an event occurs. */
1470 if (timeout_check(&timeout
)) {
1471 DEBUG("Wait timed out");
1475 result
= poll(pollfds
, event_set
->count
, timeout_remaining_ms(&timeout
) || -1);
1477 timeout_update(&timeout
);
1480 if (errno
== EINTR
) {
1481 DEBUG("poll() call was interrupted, repeating");
1485 RETURN_FAIL("poll() failed");
1487 } else if (result
== 0) {
1488 DEBUG("poll() timed out");
1489 if (!timeout
.overflow
)
1492 DEBUG("poll() completed");
1502 #ifdef USE_TERMIOS_SPEED
1503 static enum sp_return
get_baudrate(int fd
, int *baudrate
)
1507 TRACE("%d, %p", fd
, baudrate
);
1509 DEBUG("Getting baud rate");
1511 if (!(data
= malloc(get_termios_size())))
1512 RETURN_ERROR(SP_ERR_MEM
, "termios malloc failed");
1514 if (ioctl(fd
, get_termios_get_ioctl(), data
) < 0) {
1516 RETURN_FAIL("Getting termios failed");
1519 *baudrate
= get_termios_speed(data
);
1526 static enum sp_return
set_baudrate(int fd
, int baudrate
)
1530 TRACE("%d, %d", fd
, baudrate
);
1532 DEBUG("Getting baud rate");
1534 if (!(data
= malloc(get_termios_size())))
1535 RETURN_ERROR(SP_ERR_MEM
, "termios malloc failed");
1537 if (ioctl(fd
, get_termios_get_ioctl(), data
) < 0) {
1539 RETURN_FAIL("Getting termios failed");
1542 DEBUG("Setting baud rate");
1544 set_termios_speed(data
, baudrate
);
1546 if (ioctl(fd
, get_termios_set_ioctl(), data
) < 0) {
1548 RETURN_FAIL("Setting termios failed");
1555 #endif /* USE_TERMIOS_SPEED */
1558 static enum sp_return
get_flow(int fd
, struct port_data
*data
)
1562 TRACE("%d, %p", fd
, data
);
1564 DEBUG("Getting advanced flow control");
1566 if (!(termx
= malloc(get_termiox_size())))
1567 RETURN_ERROR(SP_ERR_MEM
, "termiox malloc failed");
1569 if (ioctl(fd
, TCGETX
, termx
) < 0) {
1571 RETURN_FAIL("Getting termiox failed");
1574 get_termiox_flow(termx
, &data
->rts_flow
, &data
->cts_flow
,
1575 &data
->dtr_flow
, &data
->dsr_flow
);
1582 static enum sp_return
set_flow(int fd
, struct port_data
*data
)
1586 TRACE("%d, %p", fd
, data
);
1588 DEBUG("Getting advanced flow control");
1590 if (!(termx
= malloc(get_termiox_size())))
1591 RETURN_ERROR(SP_ERR_MEM
, "termiox malloc failed");
1593 if (ioctl(fd
, TCGETX
, termx
) < 0) {
1595 RETURN_FAIL("Getting termiox failed");
1598 DEBUG("Setting advanced flow control");
1600 set_termiox_flow(termx
, data
->rts_flow
, data
->cts_flow
,
1601 data
->dtr_flow
, data
->dsr_flow
);
1603 if (ioctl(fd
, TCSETX
, termx
) < 0) {
1605 RETURN_FAIL("Setting termiox failed");
1612 #endif /* USE_TERMIOX */
1614 static enum sp_return
get_config(struct sp_port
*port
, struct port_data
*data
,
1615 struct sp_port_config
*config
)
1619 TRACE("%p, %p, %p", port
, data
, config
);
1621 DEBUG_FMT("Getting configuration for port %s", port
->name
);
1624 if (!GetCommState(port
->hdl
, &data
->dcb
))
1625 RETURN_FAIL("GetCommState() failed");
1627 for (i
= 0; i
< NUM_STD_BAUDRATES
; i
++) {
1628 if (data
->dcb
.BaudRate
== std_baudrates
[i
].index
) {
1629 config
->baudrate
= std_baudrates
[i
].value
;
1634 if (i
== NUM_STD_BAUDRATES
)
1635 /* BaudRate field can be either an index or a custom baud rate. */
1636 config
->baudrate
= data
->dcb
.BaudRate
;
1638 config
->bits
= data
->dcb
.ByteSize
;
1640 if (data
->dcb
.fParity
)
1641 switch (data
->dcb
.Parity
) {
1643 config
->parity
= SP_PARITY_NONE
;
1646 config
->parity
= SP_PARITY_ODD
;
1649 config
->parity
= SP_PARITY_EVEN
;
1652 config
->parity
= SP_PARITY_MARK
;
1655 config
->parity
= SP_PARITY_SPACE
;
1658 config
->parity
= -1;
1661 config
->parity
= SP_PARITY_NONE
;
1663 switch (data
->dcb
.StopBits
) {
1665 config
->stopbits
= 1;
1668 config
->stopbits
= 2;
1671 config
->stopbits
= -1;
1674 switch (data
->dcb
.fRtsControl
) {
1675 case RTS_CONTROL_DISABLE
:
1676 config
->rts
= SP_RTS_OFF
;
1678 case RTS_CONTROL_ENABLE
:
1679 config
->rts
= SP_RTS_ON
;
1681 case RTS_CONTROL_HANDSHAKE
:
1682 config
->rts
= SP_RTS_FLOW_CONTROL
;
1688 config
->cts
= data
->dcb
.fOutxCtsFlow
? SP_CTS_FLOW_CONTROL
: SP_CTS_IGNORE
;
1690 switch (data
->dcb
.fDtrControl
) {
1691 case DTR_CONTROL_DISABLE
:
1692 config
->dtr
= SP_DTR_OFF
;
1694 case DTR_CONTROL_ENABLE
:
1695 config
->dtr
= SP_DTR_ON
;
1697 case DTR_CONTROL_HANDSHAKE
:
1698 config
->dtr
= SP_DTR_FLOW_CONTROL
;
1704 config
->dsr
= data
->dcb
.fOutxDsrFlow
? SP_DSR_FLOW_CONTROL
: SP_DSR_IGNORE
;
1706 if (data
->dcb
.fInX
) {
1707 if (data
->dcb
.fOutX
)
1708 config
->xon_xoff
= SP_XONXOFF_INOUT
;
1710 config
->xon_xoff
= SP_XONXOFF_IN
;
1712 if (data
->dcb
.fOutX
)
1713 config
->xon_xoff
= SP_XONXOFF_OUT
;
1715 config
->xon_xoff
= SP_XONXOFF_DISABLED
;
1720 if (tcgetattr(port
->fd
, &data
->term
) < 0)
1721 RETURN_FAIL("tcgetattr() failed");
1723 if (ioctl(port
->fd
, TIOCMGET
, &data
->controlbits
) < 0)
1724 RETURN_FAIL("TIOCMGET ioctl failed");
1727 int ret
= get_flow(port
->fd
, data
);
1729 if (ret
== SP_ERR_FAIL
&& errno
== EINVAL
)
1730 data
->termiox_supported
= 0;
1732 RETURN_CODEVAL(ret
);
1734 data
->termiox_supported
= 1;
1736 data
->termiox_supported
= 0;
1739 for (i
= 0; i
< NUM_STD_BAUDRATES
; i
++) {
1740 if (cfgetispeed(&data
->term
) == std_baudrates
[i
].index
) {
1741 config
->baudrate
= std_baudrates
[i
].value
;
1746 if (i
== NUM_STD_BAUDRATES
) {
1748 config
->baudrate
= (int)data
->term
.c_ispeed
;
1749 #elif defined(USE_TERMIOS_SPEED)
1750 TRY(get_baudrate(port
->fd
, &config
->baudrate
));
1752 config
->baudrate
= -1;
1756 switch (data
->term
.c_cflag
& CSIZE
) {
1773 if (!(data
->term
.c_cflag
& PARENB
) && (data
->term
.c_iflag
& IGNPAR
))
1774 config
->parity
= SP_PARITY_NONE
;
1775 else if (!(data
->term
.c_cflag
& PARENB
) || (data
->term
.c_iflag
& IGNPAR
))
1776 config
->parity
= -1;
1778 else if (data
->term
.c_cflag
& CMSPAR
)
1779 config
->parity
= (data
->term
.c_cflag
& PARODD
) ? SP_PARITY_MARK
: SP_PARITY_SPACE
;
1782 config
->parity
= (data
->term
.c_cflag
& PARODD
) ? SP_PARITY_ODD
: SP_PARITY_EVEN
;
1784 config
->stopbits
= (data
->term
.c_cflag
& CSTOPB
) ? 2 : 1;
1786 if (data
->term
.c_cflag
& CRTSCTS
) {
1787 config
->rts
= SP_RTS_FLOW_CONTROL
;
1788 config
->cts
= SP_CTS_FLOW_CONTROL
;
1790 if (data
->termiox_supported
&& data
->rts_flow
)
1791 config
->rts
= SP_RTS_FLOW_CONTROL
;
1793 config
->rts
= (data
->controlbits
& TIOCM_RTS
) ? SP_RTS_ON
: SP_RTS_OFF
;
1795 config
->cts
= (data
->termiox_supported
&& data
->cts_flow
) ?
1796 SP_CTS_FLOW_CONTROL
: SP_CTS_IGNORE
;
1799 if (data
->termiox_supported
&& data
->dtr_flow
)
1800 config
->dtr
= SP_DTR_FLOW_CONTROL
;
1802 config
->dtr
= (data
->controlbits
& TIOCM_DTR
) ? SP_DTR_ON
: SP_DTR_OFF
;
1804 config
->dsr
= (data
->termiox_supported
&& data
->dsr_flow
) ?
1805 SP_DSR_FLOW_CONTROL
: SP_DSR_IGNORE
;
1807 if (data
->term
.c_iflag
& IXOFF
) {
1808 if (data
->term
.c_iflag
& IXON
)
1809 config
->xon_xoff
= SP_XONXOFF_INOUT
;
1811 config
->xon_xoff
= SP_XONXOFF_IN
;
1813 if (data
->term
.c_iflag
& IXON
)
1814 config
->xon_xoff
= SP_XONXOFF_OUT
;
1816 config
->xon_xoff
= SP_XONXOFF_DISABLED
;
1823 static enum sp_return
set_config(struct sp_port
*port
, struct port_data
*data
,
1824 const struct sp_port_config
*config
)
1828 BAUD_TYPE baud_nonstd
;
1832 #ifdef USE_TERMIOS_SPEED
1833 int baud_nonstd
= 0;
1836 TRACE("%p, %p, %p", port
, data
, config
);
1838 DEBUG_FMT("Setting configuration for port %s", port
->name
);
1843 TRY(await_write_completion(port
));
1845 if (config
->baudrate
>= 0) {
1846 for (i
= 0; i
< NUM_STD_BAUDRATES
; i
++) {
1847 if (config
->baudrate
== std_baudrates
[i
].value
) {
1848 data
->dcb
.BaudRate
= std_baudrates
[i
].index
;
1853 if (i
== NUM_STD_BAUDRATES
)
1854 data
->dcb
.BaudRate
= config
->baudrate
;
1856 /* Allocate write buffer for 50ms of data at baud rate. */
1857 port
->write_buf_size
= max(config
->baudrate
/ (8 * 20), 1);
1858 new_buf
= realloc(port
->write_buf
, port
->write_buf_size
);
1860 RETURN_ERROR(SP_ERR_MEM
, "Allocating write buffer failed");
1861 port
->write_buf
= new_buf
;
1864 if (config
->bits
>= 0)
1865 data
->dcb
.ByteSize
= config
->bits
;
1867 if (config
->parity
>= 0) {
1868 switch (config
->parity
) {
1869 case SP_PARITY_NONE
:
1870 data
->dcb
.Parity
= NOPARITY
;
1873 data
->dcb
.Parity
= ODDPARITY
;
1875 case SP_PARITY_EVEN
:
1876 data
->dcb
.Parity
= EVENPARITY
;
1878 case SP_PARITY_MARK
:
1879 data
->dcb
.Parity
= MARKPARITY
;
1881 case SP_PARITY_SPACE
:
1882 data
->dcb
.Parity
= SPACEPARITY
;
1885 RETURN_ERROR(SP_ERR_ARG
, "Invalid parity setting");
1889 if (config
->stopbits
>= 0) {
1890 switch (config
->stopbits
) {
1891 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
1893 data
->dcb
.StopBits
= ONESTOPBIT
;
1896 data
->dcb
.StopBits
= TWOSTOPBITS
;
1899 RETURN_ERROR(SP_ERR_ARG
, "Invalid stop bit setting");
1903 if (config
->rts
>= 0) {
1904 switch (config
->rts
) {
1906 data
->dcb
.fRtsControl
= RTS_CONTROL_DISABLE
;
1909 data
->dcb
.fRtsControl
= RTS_CONTROL_ENABLE
;
1911 case SP_RTS_FLOW_CONTROL
:
1912 data
->dcb
.fRtsControl
= RTS_CONTROL_HANDSHAKE
;
1915 RETURN_ERROR(SP_ERR_ARG
, "Invalid RTS setting");
1919 if (config
->cts
>= 0) {
1920 switch (config
->cts
) {
1922 data
->dcb
.fOutxCtsFlow
= FALSE
;
1924 case SP_CTS_FLOW_CONTROL
:
1925 data
->dcb
.fOutxCtsFlow
= TRUE
;
1928 RETURN_ERROR(SP_ERR_ARG
, "Invalid CTS setting");
1932 if (config
->dtr
>= 0) {
1933 switch (config
->dtr
) {
1935 data
->dcb
.fDtrControl
= DTR_CONTROL_DISABLE
;
1938 data
->dcb
.fDtrControl
= DTR_CONTROL_ENABLE
;
1940 case SP_DTR_FLOW_CONTROL
:
1941 data
->dcb
.fDtrControl
= DTR_CONTROL_HANDSHAKE
;
1944 RETURN_ERROR(SP_ERR_ARG
, "Invalid DTR setting");
1948 if (config
->dsr
>= 0) {
1949 switch (config
->dsr
) {
1951 data
->dcb
.fOutxDsrFlow
= FALSE
;
1953 case SP_DSR_FLOW_CONTROL
:
1954 data
->dcb
.fOutxDsrFlow
= TRUE
;
1957 RETURN_ERROR(SP_ERR_ARG
, "Invalid DSR setting");
1961 if (config
->xon_xoff
>= 0) {
1962 switch (config
->xon_xoff
) {
1963 case SP_XONXOFF_DISABLED
:
1964 data
->dcb
.fInX
= FALSE
;
1965 data
->dcb
.fOutX
= FALSE
;
1968 data
->dcb
.fInX
= TRUE
;
1969 data
->dcb
.fOutX
= FALSE
;
1971 case SP_XONXOFF_OUT
:
1972 data
->dcb
.fInX
= FALSE
;
1973 data
->dcb
.fOutX
= TRUE
;
1975 case SP_XONXOFF_INOUT
:
1976 data
->dcb
.fInX
= TRUE
;
1977 data
->dcb
.fOutX
= TRUE
;
1980 RETURN_ERROR(SP_ERR_ARG
, "Invalid XON/XOFF setting");
1984 if (!SetCommState(port
->hdl
, &data
->dcb
))
1985 RETURN_FAIL("SetCommState() failed");
1991 if (config
->baudrate
>= 0) {
1992 for (i
= 0; i
< NUM_STD_BAUDRATES
; i
++) {
1993 if (config
->baudrate
== std_baudrates
[i
].value
) {
1994 if (cfsetospeed(&data
->term
, std_baudrates
[i
].index
) < 0)
1995 RETURN_FAIL("cfsetospeed() failed");
1997 if (cfsetispeed(&data
->term
, std_baudrates
[i
].index
) < 0)
1998 RETURN_FAIL("cfsetispeed() failed");
2003 /* Non-standard baud rate */
2004 if (i
== NUM_STD_BAUDRATES
) {
2006 /* Set "dummy" baud rate. */
2007 if (cfsetspeed(&data
->term
, B9600
) < 0)
2008 RETURN_FAIL("cfsetspeed() failed");
2009 baud_nonstd
= config
->baudrate
;
2010 #elif defined(USE_TERMIOS_SPEED)
2013 RETURN_ERROR(SP_ERR_SUPP
, "Non-standard baudrate not supported");
2018 if (config
->bits
>= 0) {
2019 data
->term
.c_cflag
&= ~CSIZE
;
2020 switch (config
->bits
) {
2022 data
->term
.c_cflag
|= CS8
;
2025 data
->term
.c_cflag
|= CS7
;
2028 data
->term
.c_cflag
|= CS6
;
2031 data
->term
.c_cflag
|= CS5
;
2034 RETURN_ERROR(SP_ERR_ARG
, "Invalid data bits setting");
2038 if (config
->parity
>= 0) {
2039 data
->term
.c_iflag
&= ~IGNPAR
;
2040 data
->term
.c_cflag
&= ~(PARENB
| PARODD
);
2042 data
->term
.c_cflag
&= ~CMSPAR
;
2044 switch (config
->parity
) {
2045 case SP_PARITY_NONE
:
2046 data
->term
.c_iflag
|= IGNPAR
;
2048 case SP_PARITY_EVEN
:
2049 data
->term
.c_cflag
|= PARENB
;
2052 data
->term
.c_cflag
|= PARENB
| PARODD
;
2055 case SP_PARITY_MARK
:
2056 data
->term
.c_cflag
|= PARENB
| PARODD
;
2057 data
->term
.c_cflag
|= CMSPAR
;
2059 case SP_PARITY_SPACE
:
2060 data
->term
.c_cflag
|= PARENB
;
2061 data
->term
.c_cflag
|= CMSPAR
;
2064 case SP_PARITY_MARK
:
2065 case SP_PARITY_SPACE
:
2066 RETURN_ERROR(SP_ERR_SUPP
, "Mark/space parity not supported");
2069 RETURN_ERROR(SP_ERR_ARG
, "Invalid parity setting");
2073 if (config
->stopbits
>= 0) {
2074 data
->term
.c_cflag
&= ~CSTOPB
;
2075 switch (config
->stopbits
) {
2077 data
->term
.c_cflag
&= ~CSTOPB
;
2080 data
->term
.c_cflag
|= CSTOPB
;
2083 RETURN_ERROR(SP_ERR_ARG
, "Invalid stop bits setting");
2087 if (config
->rts
>= 0 || config
->cts
>= 0) {
2088 if (data
->termiox_supported
) {
2089 data
->rts_flow
= data
->cts_flow
= 0;
2090 switch (config
->rts
) {
2093 controlbits
= TIOCM_RTS
;
2094 if (ioctl(port
->fd
, config
->rts
== SP_RTS_ON
? TIOCMBIS
: TIOCMBIC
, &controlbits
) < 0)
2095 RETURN_FAIL("Setting RTS signal level failed");
2097 case SP_RTS_FLOW_CONTROL
:
2103 if (config
->cts
== SP_CTS_FLOW_CONTROL
)
2106 if (data
->rts_flow
&& data
->cts_flow
)
2107 data
->term
.c_iflag
|= CRTSCTS
;
2109 data
->term
.c_iflag
&= ~CRTSCTS
;
2111 /* Asymmetric use of RTS/CTS not supported. */
2112 if (data
->term
.c_iflag
& CRTSCTS
) {
2113 /* Flow control can only be disabled for both RTS & CTS together. */
2114 if (config
->rts
>= 0 && config
->rts
!= SP_RTS_FLOW_CONTROL
) {
2115 if (config
->cts
!= SP_CTS_IGNORE
)
2116 RETURN_ERROR(SP_ERR_SUPP
, "RTS & CTS flow control must be disabled together");
2118 if (config
->cts
>= 0 && config
->cts
!= SP_CTS_FLOW_CONTROL
) {
2119 if (config
->rts
<= 0 || config
->rts
== SP_RTS_FLOW_CONTROL
)
2120 RETURN_ERROR(SP_ERR_SUPP
, "RTS & CTS flow control must be disabled together");
2123 /* Flow control can only be enabled for both RTS & CTS together. */
2124 if (((config
->rts
== SP_RTS_FLOW_CONTROL
) && (config
->cts
!= SP_CTS_FLOW_CONTROL
)) ||
2125 ((config
->cts
== SP_CTS_FLOW_CONTROL
) && (config
->rts
!= SP_RTS_FLOW_CONTROL
)))
2126 RETURN_ERROR(SP_ERR_SUPP
, "RTS & CTS flow control must be enabled together");
2129 if (config
->rts
>= 0) {
2130 if (config
->rts
== SP_RTS_FLOW_CONTROL
) {
2131 data
->term
.c_iflag
|= CRTSCTS
;
2133 controlbits
= TIOCM_RTS
;
2134 if (ioctl(port
->fd
, config
->rts
== SP_RTS_ON
? TIOCMBIS
: TIOCMBIC
,
2136 RETURN_FAIL("Setting RTS signal level failed");
2142 if (config
->dtr
>= 0 || config
->dsr
>= 0) {
2143 if (data
->termiox_supported
) {
2144 data
->dtr_flow
= data
->dsr_flow
= 0;
2145 switch (config
->dtr
) {
2148 controlbits
= TIOCM_DTR
;
2149 if (ioctl(port
->fd
, config
->dtr
== SP_DTR_ON
? TIOCMBIS
: TIOCMBIC
, &controlbits
) < 0)
2150 RETURN_FAIL("Setting DTR signal level failed");
2152 case SP_DTR_FLOW_CONTROL
:
2158 if (config
->dsr
== SP_DSR_FLOW_CONTROL
)
2161 /* DTR/DSR flow control not supported. */
2162 if (config
->dtr
== SP_DTR_FLOW_CONTROL
|| config
->dsr
== SP_DSR_FLOW_CONTROL
)
2163 RETURN_ERROR(SP_ERR_SUPP
, "DTR/DSR flow control not supported");
2165 if (config
->dtr
>= 0) {
2166 controlbits
= TIOCM_DTR
;
2167 if (ioctl(port
->fd
, config
->dtr
== SP_DTR_ON
? TIOCMBIS
: TIOCMBIC
,
2169 RETURN_FAIL("Setting DTR signal level failed");
2174 if (config
->xon_xoff
>= 0) {
2175 data
->term
.c_iflag
&= ~(IXON
| IXOFF
| IXANY
);
2176 switch (config
->xon_xoff
) {
2177 case SP_XONXOFF_DISABLED
:
2180 data
->term
.c_iflag
|= IXOFF
;
2182 case SP_XONXOFF_OUT
:
2183 data
->term
.c_iflag
|= IXON
| IXANY
;
2185 case SP_XONXOFF_INOUT
:
2186 data
->term
.c_iflag
|= IXON
| IXOFF
| IXANY
;
2189 RETURN_ERROR(SP_ERR_ARG
, "Invalid XON/XOFF setting");
2193 if (tcsetattr(port
->fd
, TCSANOW
, &data
->term
) < 0)
2194 RETURN_FAIL("tcsetattr() failed");
2197 if (baud_nonstd
!= B0
) {
2198 if (ioctl(port
->fd
, IOSSIOSPEED
, &baud_nonstd
) == -1)
2199 RETURN_FAIL("IOSSIOSPEED ioctl failed");
2201 * Set baud rates in data->term to correct, but incompatible
2202 * with tcsetattr() value, same as delivered by tcgetattr().
2204 if (cfsetspeed(&data
->term
, baud_nonstd
) < 0)
2205 RETURN_FAIL("cfsetspeed() failed");
2207 #elif defined(__linux__)
2208 #ifdef USE_TERMIOS_SPEED
2210 TRY(set_baudrate(port
->fd
, config
->baudrate
));
2213 if (data
->termiox_supported
)
2214 TRY(set_flow(port
->fd
, data
));
2218 #endif /* !_WIN32 */
2223 SP_API
enum sp_return
sp_new_config(struct sp_port_config
**config_ptr
)
2225 struct sp_port_config
*config
;
2227 TRACE("%p", config_ptr
);
2230 RETURN_ERROR(SP_ERR_ARG
, "Null result pointer");
2234 if (!(config
= malloc(sizeof(struct sp_port_config
))))
2235 RETURN_ERROR(SP_ERR_MEM
, "Config malloc failed");
2237 config
->baudrate
= -1;
2239 config
->parity
= -1;
2240 config
->stopbits
= -1;
2246 *config_ptr
= config
;
2251 SP_API
void sp_free_config(struct sp_port_config
*config
)
2253 TRACE("%p", config
);
2256 DEBUG("Null config");
2263 SP_API
enum sp_return
sp_get_config(struct sp_port
*port
,
2264 struct sp_port_config
*config
)
2266 struct port_data data
;
2268 TRACE("%p, %p", port
, config
);
2273 RETURN_ERROR(SP_ERR_ARG
, "Null config");
2275 TRY(get_config(port
, &data
, config
));
2280 SP_API
enum sp_return
sp_set_config(struct sp_port
*port
,
2281 const struct sp_port_config
*config
)
2283 struct port_data data
;
2284 struct sp_port_config prev_config
;
2286 TRACE("%p, %p", port
, config
);
2291 RETURN_ERROR(SP_ERR_ARG
, "Null config");
2293 TRY(get_config(port
, &data
, &prev_config
));
2294 TRY(set_config(port
, &data
, config
));
2299 #define CREATE_ACCESSORS(x, type) \
2300 SP_API enum sp_return sp_set_##x(struct sp_port *port, type x) { \
2301 struct port_data data; \
2302 struct sp_port_config config; \
2303 TRACE("%p, %d", port, x); \
2304 CHECK_OPEN_PORT(); \
2305 TRY(get_config(port, &data, &config)); \
2307 TRY(set_config(port, &data, &config)); \
2310 SP_API enum sp_return sp_get_config_##x(const struct sp_port_config *config, \
2312 TRACE("%p, %p", config, x); \
2314 RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); \
2316 RETURN_ERROR(SP_ERR_ARG, "Null config"); \
2320 SP_API enum sp_return sp_set_config_##x(struct sp_port_config *config, \
2322 TRACE("%p, %d", config, x); \
2324 RETURN_ERROR(SP_ERR_ARG, "Null config"); \
2329 CREATE_ACCESSORS(baudrate
, int)
2330 CREATE_ACCESSORS(bits
, int)
2331 CREATE_ACCESSORS(parity
, enum sp_parity
)
2332 CREATE_ACCESSORS(stopbits
, int)
2333 CREATE_ACCESSORS(rts
, enum sp_rts
)
2334 CREATE_ACCESSORS(cts
, enum sp_cts
)
2335 CREATE_ACCESSORS(dtr
, enum sp_dtr
)
2336 CREATE_ACCESSORS(dsr
, enum sp_dsr
)
2337 CREATE_ACCESSORS(xon_xoff
, enum sp_xonxoff
)
2339 SP_API
enum sp_return
sp_set_config_flowcontrol(struct sp_port_config
*config
,
2340 enum sp_flowcontrol flowcontrol
)
2343 RETURN_ERROR(SP_ERR_ARG
, "Null configuration");
2345 if (flowcontrol
> SP_FLOWCONTROL_DTRDSR
)
2346 RETURN_ERROR(SP_ERR_ARG
, "Invalid flow control setting");
2348 if (flowcontrol
== SP_FLOWCONTROL_XONXOFF
)
2349 config
->xon_xoff
= SP_XONXOFF_INOUT
;
2351 config
->xon_xoff
= SP_XONXOFF_DISABLED
;
2353 if (flowcontrol
== SP_FLOWCONTROL_RTSCTS
) {
2354 config
->rts
= SP_RTS_FLOW_CONTROL
;
2355 config
->cts
= SP_CTS_FLOW_CONTROL
;
2357 if (config
->rts
== SP_RTS_FLOW_CONTROL
)
2358 config
->rts
= SP_RTS_ON
;
2359 config
->cts
= SP_CTS_IGNORE
;
2362 if (flowcontrol
== SP_FLOWCONTROL_DTRDSR
) {
2363 config
->dtr
= SP_DTR_FLOW_CONTROL
;
2364 config
->dsr
= SP_DSR_FLOW_CONTROL
;
2366 if (config
->dtr
== SP_DTR_FLOW_CONTROL
)
2367 config
->dtr
= SP_DTR_ON
;
2368 config
->dsr
= SP_DSR_IGNORE
;
2374 SP_API
enum sp_return
sp_set_flowcontrol(struct sp_port
*port
,
2375 enum sp_flowcontrol flowcontrol
)
2377 struct port_data data
;
2378 struct sp_port_config config
;
2380 TRACE("%p, %d", port
, flowcontrol
);
2384 TRY(get_config(port
, &data
, &config
));
2386 TRY(sp_set_config_flowcontrol(&config
, flowcontrol
));
2388 TRY(set_config(port
, &data
, &config
));
2393 SP_API
enum sp_return
sp_get_signals(struct sp_port
*port
,
2394 enum sp_signal
*signals
)
2396 TRACE("%p, %p", port
, signals
);
2401 RETURN_ERROR(SP_ERR_ARG
, "Null result pointer");
2403 DEBUG_FMT("Getting control signals for port %s", port
->name
);
2408 if (GetCommModemStatus(port
->hdl
, &bits
) == 0)
2409 RETURN_FAIL("GetCommModemStatus() failed");
2410 if (bits
& MS_CTS_ON
)
2411 *signals
|= SP_SIG_CTS
;
2412 if (bits
& MS_DSR_ON
)
2413 *signals
|= SP_SIG_DSR
;
2414 if (bits
& MS_RLSD_ON
)
2415 *signals
|= SP_SIG_DCD
;
2416 if (bits
& MS_RING_ON
)
2417 *signals
|= SP_SIG_RI
;
2420 if (ioctl(port
->fd
, TIOCMGET
, &bits
) < 0)
2421 RETURN_FAIL("TIOCMGET ioctl failed");
2422 if (bits
& TIOCM_CTS
)
2423 *signals
|= SP_SIG_CTS
;
2424 if (bits
& TIOCM_DSR
)
2425 *signals
|= SP_SIG_DSR
;
2426 if (bits
& TIOCM_CAR
)
2427 *signals
|= SP_SIG_DCD
;
2428 if (bits
& TIOCM_RNG
)
2429 *signals
|= SP_SIG_RI
;
2434 SP_API
enum sp_return
sp_start_break(struct sp_port
*port
)
2440 if (SetCommBreak(port
->hdl
) == 0)
2441 RETURN_FAIL("SetCommBreak() failed");
2443 if (ioctl(port
->fd
, TIOCSBRK
, 1) < 0)
2444 RETURN_FAIL("TIOCSBRK ioctl failed");
2450 SP_API
enum sp_return
sp_end_break(struct sp_port
*port
)
2456 if (ClearCommBreak(port
->hdl
) == 0)
2457 RETURN_FAIL("ClearCommBreak() failed");
2459 if (ioctl(port
->fd
, TIOCCBRK
, 1) < 0)
2460 RETURN_FAIL("TIOCCBRK ioctl failed");
2466 SP_API
int sp_last_error_code(void)
2470 RETURN_INT(GetLastError());
2476 SP_API
char *sp_last_error_message(void)
2482 DWORD error
= GetLastError();
2484 DWORD length
= FormatMessageA(
2485 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
2486 FORMAT_MESSAGE_FROM_SYSTEM
|
2487 FORMAT_MESSAGE_IGNORE_INSERTS
,
2490 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
2494 if (length
>= 2 && message
[length
- 2] == '\r')
2495 message
[length
- 2] = '\0';
2497 RETURN_STRING(message
);
2499 RETURN_STRING(strerror(errno
));
2503 SP_API
void sp_free_error_message(char *message
)
2505 TRACE("%s", message
);
2516 SP_API
void sp_set_debug_handler(void (*handler
)(const char *format
, ...))
2518 TRACE("%p", handler
);
2520 sp_debug_handler
= handler
;
2525 SP_API
void sp_default_debug_handler(const char *format
, ...)
2528 va_start(args
, format
);
2529 if (getenv("LIBSERIALPORT_DEBUG")) {
2530 fputs("sp: ", stderr
);
2531 vfprintf(stderr
, format
, args
);
2536 SP_API
int sp_get_major_package_version(void)
2538 return SP_PACKAGE_VERSION_MAJOR
;
2541 SP_API
int sp_get_minor_package_version(void)
2543 return SP_PACKAGE_VERSION_MINOR
;
2546 SP_API
int sp_get_micro_package_version(void)
2548 return SP_PACKAGE_VERSION_MICRO
;
2551 SP_API
const char *sp_get_package_version_string(void)
2553 return SP_PACKAGE_VERSION_STRING
;
2556 SP_API
int sp_get_current_lib_version(void)
2558 return SP_LIB_VERSION_CURRENT
;
2561 SP_API
int sp_get_revision_lib_version(void)
2563 return SP_LIB_VERSION_REVISION
;
2566 SP_API
int sp_get_age_lib_version(void)
2568 return SP_LIB_VERSION_AGE
;
2571 SP_API
const char *sp_get_lib_version_string(void)
2573 return SP_LIB_VERSION_STRING
;