]> sigrok.org Git - libserialport.git/blame - libserialport.h
Add an example of proper error handling.
[libserialport.git] / libserialport.h
CommitLineData
74510d4b
ML
1/*
2 * This file is part of the libserialport project.
3 *
f77bb46d
ML
4 * Copyright (C) 2013, 2015 Martin Ling <martin-libserialport@earth.li>
5 * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
6 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
74510d4b
ML
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
cd5f5281 22/**
cf9d365c
UH
23 * @mainpage libserialport API
24 *
25 * Introduction
26 * ============
27 *
28 * libserialport is a minimal library written in C that is intended to take
29 * care of the OS-specific details when writing software that uses serial ports.
30 *
31 * By writing your serial code to use libserialport, you enable it to work
32 * transparently on any platform supported by the library.
33 *
deef6e52 34 * libserialport is an open source project released under the LGPL3+ license.
cf9d365c 35 *
deef6e52
ML
36 * The library is maintained by the [sigrok](http://sigrok.org/) project. See
37 * the [libserialport homepage](http://sigrok.org/wiki/Libserialport) for the
38 * latest information.
cf9d365c 39 *
deef6e52
ML
40 * Source code is maintained in git at
41 * [git://sigrok.org/libserialport](http://sigrok.org/gitweb/?p=libserialport.git).
42 *
43 * Bugs are tracked at http://sigrok.org/bugzilla/.
44 *
45 * The library was conceived and designed by Martin Ling, is maintained by
46 * Uwe Hermann, and has received contributions from several other developers.
47 * See the git history for full credits.
48 *
49 * API information
50 * ===============
51 *
52 * The API has been designed from scratch. It does not exactly resemble the
53 * serial API of any particular operating system. Instead it aims to provide
54 * a set of functions that can reliably be implemented across all operating
55 * systems. These form a sufficient basis for higher level behaviour to
56 * be implemented in a platform independent manner.
57 *
58 * If you are porting code written for a particular OS, you may find you need
59 * to restructure things somewhat, or do without some specialised features.
60 * For particular notes on porting existing code, see @ref Porting.
61 *
7c8d67ef
ML
62 * Examples
63 * --------
64 *
65 * Some simple example programs using libserialport are included in the
66 * @c examples directory in the source package:
67 *
68 * - @ref list_ports.c - Getting a list of ports present on the system.
69 * - @ref port_info.c - Getting information on a particular serial port.
9ddf0858 70 * - @ref port_config.c - Accessing configuration settings of a port.
47200531 71 * - @ref handle_errors.c - Handling errors returned from the library.
7c8d67ef
ML
72 *
73 * These examples are linked with the API documentation. Each function
74 * in the API reference includes links to where it is used in an example
75 * program, and each appearance of a function in the examples links
76 * to that function's entry in the API reference.
49fd7b1b
ML
77 *
78 * Headers
79 * -------
80 *
81 * To use libserialport functions in your code, you should include the
44df4154
UH
82 * libserialport.h header, i.e.
83 * @code
84 * #include <libserialport.h>
85 * @endcode
49fd7b1b
ML
86 *
87 * Namespace
88 * ---------
89 *
90 * All identifiers defined by the public libserialport headers use the prefix
ad19d604 91 * @c sp_ (for functions and data types) or @c SP_ (for macros and constants).
49fd7b1b
ML
92 *
93 * Functions
94 * ---------
95 *
96 * The functions provided by the library are documented in detail in
97 * the following sections:
98 *
99 * - @ref Enumeration (obtaining a list of serial ports on the system)
100 * - @ref Ports (opening, closing and getting information about ports)
101 * - @ref Configuration (baud rate, parity, etc.)
102 * - @ref Signals (modem control lines, breaks, etc.)
103 * - @ref Data (reading and writing data, and buffer management)
104 * - @ref Waiting (waiting for ports to be ready, integrating with event loops)
105 * - @ref Errors (getting error and debugging information)
deef6e52
ML
106 *
107 * Data structures
108 * ---------------
109 *
110 * The library defines three data structures:
cf9d365c 111 *
deef6e52
ML
112 * - @ref sp_port, which represents a serial port.
113 * See @ref Enumeration.
114 * - @ref sp_port_config, which represents a port configuration.
115 * See @ref Configuration.
116 * - @ref sp_event_set, which represents a set of events.
117 * See @ref Waiting.
cf9d365c 118 *
deef6e52
ML
119 * All these structures are allocated and freed by library functions. It is
120 * the caller's responsibility to ensure that the correct calls are made to
121 * free allocated structures after use.
cf9d365c 122 *
deef6e52
ML
123 * Return codes and error handling
124 * -------------------------------
cf9d365c 125 *
0151b157
ML
126 * Most functions have return type @ref sp_return and can return only four
127 * possible error values:
cf9d365c 128 *
6aabf62a
ML
129 * - @ref SP_ERR_ARG means that a function was called with invalid
130 * arguments. This implies a bug in the caller. The arguments passed would
131 * be invalid regardless of the underlying OS or serial device involved.
132 *
133 * - @ref SP_ERR_FAIL means that the OS reported a failure. The error code or
134 * message provided by the OS can be obtained by calling sp_last_error_code()
135 * or sp_last_error_message().
136 *
137 * - @ref SP_ERR_SUPP indicates that there is no support for the requested
138 * operation in the current OS, driver or device. No error message is
139 * available from the OS in this case. There is either no way to request
140 * the operation in the first place, or libserialport does not know how to
141 * do so in the current version.
142 *
143 * - @ref SP_ERR_MEM indicates that a memory allocation failed.
144 *
145 * All of these error values are negative.
cf9d365c 146 *
0151b157
ML
147 * Calls that succeed return @ref SP_OK, which is equal to zero. Some functions
148 * declared @ref sp_return can also return a positive value for a successful
0a1ab8bf 149 * numeric result, e.g. sp_blocking_read() or sp_blocking_write().
deef6e52
ML
150 *
151 * An error message is only available via sp_last_error_message() in the case
ad19d604 152 * where @ref SP_ERR_FAIL was returned by the previous function call. The error
deef6e52
ML
153 * message returned is that provided by the OS, using the current language
154 * settings. It is an error to call sp_last_error_code() or
155 * sp_last_error_message() except after a previous function call returned
ad19d604
ML
156 * @ref SP_ERR_FAIL. The library does not define its own error codes or
157 * messages to accompany other return codes.
deef6e52
ML
158 *
159 * Thread safety
160 * -------------
161 *
162 * Certain combinations of calls can be made concurrently, as follows.
163 *
164 * - Calls using different ports may always be made concurrently, i.e.
165 * it is safe for separate threads to handle their own ports.
166 *
167 * - Calls using the same port may be made concurrently when one call
168 * is a read operation and one call is a write operation, i.e. it is safe
169 * to use separate "reader" and "writer" threads for the same port. See
170 * below for which operations meet these definitions.
171 *
172 * Read operations:
173 *
174 * - sp_blocking_read()
175 * - sp_blocking_read_next()
176 * - sp_nonblocking_read()
177 * - sp_input_waiting()
178 * - sp_flush() with @ref SP_BUF_INPUT only.
179 * - sp_wait() with @ref SP_EVENT_RX_READY only.
180 *
181 * Write operations:
182 *
183 * - sp_blocking_write()
184 * - sp_nonblocking_write()
185 * - sp_output_waiting()
186 * - sp_drain()
187 * - sp_flush() with @ref SP_BUF_OUTPUT only.
188 * - sp_wait() with @ref SP_EVENT_TX_READY only.
189 *
190 * If two calls, on the same port, do not fit into one of these categories
191 * each, then they may not be made concurrently.
192 *
193 * Debugging
194 * ---------
195 *
196 * The library can output extensive tracing and debugging information. The
197 * simplest way to use this is to set the environment variable
ad19d604 198 * @c LIBSERIALPORT_DEBUG to any value; messages will then be output to the
deef6e52
ML
199 * standard error stream.
200 *
201 * This behaviour is implemented by a default debug message handling
202 * callback. An alternative callback can be set using sp_set_debug_handler(),
203 * in order to e.g. redirect the output elsewhere or filter it.
204 *
205 * No guarantees are made about the content of the debug output; it is chosen
206 * to suit the needs of the developers and may change between releases.
207 *
208 * @anchor Porting
209 * Porting
210 * -------
211 *
212 * The following guidelines may help when porting existing OS-specific code
213 * to use libserialport.
214 *
215 * ### Porting from Unix-like systems ###
216 *
217 * There are two main differences to note when porting code written for Unix.
218 *
219 * The first is that Unix traditionally provides a wide range of functionality
220 * for dealing with serial devices at the OS level; this is exposed through the
221 * termios API and dates to the days when serial terminals were common. If your
222 * code relies on many of these facilities you will need to adapt it, because
56fe6e32 223 * libserialport provides only a raw binary channel with no special handling.
deef6e52
ML
224 *
225 * The second relates to blocking versus non-blocking I/O behaviour. In
ad19d604
ML
226 * Unix-like systems this is normally specified by setting the @c O_NONBLOCK
227 * flag on the file descriptor, affecting the semantics of subsequent @c read()
228 * and @c write() calls.
deef6e52
ML
229 *
230 * In libserialport, blocking and nonblocking operations are both available at
ad19d604 231 * any time. If your existing code Ñ•ets @c O_NONBLOCK, you should use
deef6e52 232 * sp_nonblocking_read() and sp_nonblocking_write() to get the same behaviour
ad19d604
ML
233 * as your existing @c read() and @c write() calls. If it does not, you should
234 * use sp_blocking_read() and sp_blocking_write() instead. You may also find
deef6e52 235 * sp_blocking_read_next() useful, which reproduces the semantics of a blocking
ad19d604 236 * read() with @c VTIME=0 and @c VMIN=1 set in termios.
deef6e52
ML
237 *
238 * Finally, you should take care if your program uses custom signal handlers.
239 * The blocking calls provided by libserialport will restart system calls that
ad19d604 240 * return with @c EINTR, so you will need to make your own arrangements if you
deef6e52
ML
241 * need to interrupt blocking operations when your signal handlers are called.
242 * This is not an issue if you only use the default handlers.
243 *
244 * ### Porting from Windows ###
245 *
246 * The main consideration when porting from Windows is that there is no
247 * direct equivalent for overlapped I/O operations.
248 *
249 * If your program does not use overlapped I/O, you can simply use
250 * sp_blocking_read() and sp_blocking_write() as direct equivalents for
ad19d604
ML
251 * @c ReadFile() and @c WriteFile(). You may also find sp_blocking_read_next()
252 * useful, which reproduces the special semantics of @c ReadFile() with
253 * @c ReadIntervalTimeout and @c ReadTotalTimeoutMultiplier set to @c MAXDWORD
254 * and @c ReadTotalTimeoutConstant set to between @c 1 and @c MAXDWORD-1 .
deef6e52
ML
255 *
256 * If your program makes use of overlapped I/O to continue work while a serial
257 * operation is in progress, then you can achieve the same results using
258 * sp_nonblocking_read() and sp_nonblocking_write().
259 *
260 * Generally, overlapped I/O is combined with either waiting for completion
ad19d604
ML
261 * once there is no more background work to do (using @c WaitForSingleObject()
262 * or @c WaitForMultipleObjects()), or periodically checking for completion
263 * with @c GetOverlappedResult(). If the aim is to start a new operation for
264 * further data once the previous one has completed, you can instead simply
265 * call the nonblocking functions again with the next data. If you need to
266 * wait for completion, use sp_wait() to determine when the port is ready to
267 * send or receive further data.
cf9d365c 268 */
cd5f5281 269
8645feda
UH
270#ifndef LIBSERIALPORT_LIBSERIALPORT_H
271#define LIBSERIALPORT_LIBSERIALPORT_H
e8ffaee9 272
5ef8a1ed
UH
273#ifdef __cplusplus
274extern "C" {
275#endif
276
74510d4b 277#include <stddef.h>
74510d4b 278
0838c979
ML
279/** @cond */
280#ifdef _MSC_VER
281/* Microsoft Visual C/C++ compiler in use */
282#ifdef LIBSERIALPORT_MSBUILD
283/* Building the library - need to export DLL symbols */
284#define SP_API __declspec(dllexport)
285#else
286/* Using the library - need to import DLL symbols */
287#define SP_API __declspec(dllimport)
288#endif
289#else
290/* Some other compiler in use */
291#ifndef LIBSERIALPORT_ATBUILD
292/* Not building the library itself - don't need any special prefixes. */
293#define SP_API
294#endif
295#endif
296/** @endcond */
297
cd5f5281 298/** Return values. */
eb6ed20f 299enum sp_return {
cd5f5281 300 /** Operation completed successfully. */
74510d4b 301 SP_OK = 0,
cd5f5281 302 /** Invalid arguments were passed to the function. */
e9a2f9c9 303 SP_ERR_ARG = -1,
cfa5af67 304 /** A system error occurred while executing the operation. */
e9a2f9c9 305 SP_ERR_FAIL = -2,
cd5f5281 306 /** A memory allocation failed while executing the operation. */
f92f1f0c 307 SP_ERR_MEM = -3,
6aabf62a 308 /** The requested operation is not supported by this system or device. */
79a80046 309 SP_ERR_SUPP = -4
74510d4b
ML
310};
311
cd5f5281 312/** Port access modes. */
eb6ed20f 313enum sp_mode {
a036341b
ML
314 /** Open port for read access. */
315 SP_MODE_READ = 1,
316 /** Open port for write access. */
276ef1b9 317 SP_MODE_WRITE = 2,
13efecf8 318 /** Open port for read and write access. @since 0.1.1 */
276ef1b9 319 SP_MODE_READ_WRITE = 3
74510d4b
ML
320};
321
6f1186aa
ML
322/** Port events. */
323enum sp_event {
144a598f 324 /** Data received and ready to read. */
6f1186aa 325 SP_EVENT_RX_READY = 1,
144a598f 326 /** Ready to transmit new data. */
6f1186aa 327 SP_EVENT_TX_READY = 2,
144a598f 328 /** Error occurred. */
79a80046 329 SP_EVENT_ERROR = 4
6f1186aa
ML
330};
331
fd8fd11a
ML
332/** Buffer selection. */
333enum sp_buffer {
334 /** Input buffer. */
335 SP_BUF_INPUT = 1,
336 /** Output buffer. */
337 SP_BUF_OUTPUT = 2,
338 /** Both buffers. */
79a80046 339 SP_BUF_BOTH = 3
fd8fd11a
ML
340};
341
cd5f5281 342/** Parity settings. */
eb6ed20f 343enum sp_parity {
c200f5c1 344 /** Special value to indicate setting should be left alone. */
eb6ed20f 345 SP_PARITY_INVALID = -1,
cd5f5281 346 /** No parity. */
74510d4b 347 SP_PARITY_NONE = 0,
cd5f5281 348 /** Odd parity. */
20e63a77
ML
349 SP_PARITY_ODD = 1,
350 /** Even parity. */
351 SP_PARITY_EVEN = 2,
e432ce60
ML
352 /** Mark parity. */
353 SP_PARITY_MARK = 3,
354 /** Space parity. */
79a80046 355 SP_PARITY_SPACE = 4
74510d4b
ML
356};
357
cd5f5281 358/** RTS pin behaviour. */
eb6ed20f 359enum sp_rts {
c200f5c1 360 /** Special value to indicate setting should be left alone. */
eb6ed20f 361 SP_RTS_INVALID = -1,
cd5f5281 362 /** RTS off. */
d514a26f 363 SP_RTS_OFF = 0,
cd5f5281 364 /** RTS on. */
d514a26f 365 SP_RTS_ON = 1,
cd5f5281 366 /** RTS used for flow control. */
79a80046 367 SP_RTS_FLOW_CONTROL = 2
d514a26f
ML
368};
369
cd5f5281 370/** CTS pin behaviour. */
eb6ed20f 371enum sp_cts {
c200f5c1 372 /** Special value to indicate setting should be left alone. */
eb6ed20f 373 SP_CTS_INVALID = -1,
cd5f5281 374 /** CTS ignored. */
d514a26f 375 SP_CTS_IGNORE = 0,
cd5f5281 376 /** CTS used for flow control. */
79a80046 377 SP_CTS_FLOW_CONTROL = 1
d514a26f
ML
378};
379
cd5f5281 380/** DTR pin behaviour. */
eb6ed20f 381enum sp_dtr {
c200f5c1 382 /** Special value to indicate setting should be left alone. */
eb6ed20f 383 SP_DTR_INVALID = -1,
cd5f5281 384 /** DTR off. */
d514a26f 385 SP_DTR_OFF = 0,
cd5f5281 386 /** DTR on. */
d514a26f 387 SP_DTR_ON = 1,
cd5f5281 388 /** DTR used for flow control. */
79a80046 389 SP_DTR_FLOW_CONTROL = 2
d514a26f
ML
390};
391
cd5f5281 392/** DSR pin behaviour. */
eb6ed20f 393enum sp_dsr {
c200f5c1 394 /** Special value to indicate setting should be left alone. */
eb6ed20f 395 SP_DSR_INVALID = -1,
cd5f5281 396 /** DSR ignored. */
d514a26f 397 SP_DSR_IGNORE = 0,
cd5f5281 398 /** DSR used for flow control. */
79a80046 399 SP_DSR_FLOW_CONTROL = 1
d514a26f
ML
400};
401
cd5f5281 402/** XON/XOFF flow control behaviour. */
eb6ed20f 403enum sp_xonxoff {
c200f5c1 404 /** Special value to indicate setting should be left alone. */
eb6ed20f 405 SP_XONXOFF_INVALID = -1,
cd5f5281 406 /** XON/XOFF disabled. */
d514a26f 407 SP_XONXOFF_DISABLED = 0,
cd5f5281 408 /** XON/XOFF enabled for input only. */
d514a26f 409 SP_XONXOFF_IN = 1,
cd5f5281 410 /** XON/XOFF enabled for output only. */
d514a26f 411 SP_XONXOFF_OUT = 2,
cd5f5281 412 /** XON/XOFF enabled for input and output. */
79a80046 413 SP_XONXOFF_INOUT = 3
ac74fdaf
ML
414};
415
cd5f5281 416/** Standard flow control combinations. */
eb6ed20f 417enum sp_flowcontrol {
cd5f5281 418 /** No flow control. */
18fc2dd1 419 SP_FLOWCONTROL_NONE = 0,
cd5f5281 420 /** Software flow control using XON/XOFF characters. */
18fc2dd1 421 SP_FLOWCONTROL_XONXOFF = 1,
cd5f5281 422 /** Hardware flow control using RTS/CTS signals. */
18fc2dd1 423 SP_FLOWCONTROL_RTSCTS = 2,
cd5f5281 424 /** Hardware flow control using DTR/DSR signals. */
79a80046 425 SP_FLOWCONTROL_DTRDSR = 3
18fc2dd1
ML
426};
427
8cf7c697
ML
428/** Input signals. */
429enum sp_signal {
430 /** Clear to send. */
431 SP_SIG_CTS = 1,
432 /** Data set ready. */
433 SP_SIG_DSR = 2,
434 /** Data carrier detect. */
435 SP_SIG_DCD = 4,
436 /** Ring indicator. */
79a80046 437 SP_SIG_RI = 8
8cf7c697
ML
438};
439
13efecf8
UH
440/**
441 * Transport types.
442 *
443 * @since 0.1.1
444 */
a93fb468 445enum sp_transport {
13efecf8 446 /** Native platform serial port. @since 0.1.1 */
a93fb468 447 SP_TRANSPORT_NATIVE,
13efecf8 448 /** USB serial port adapter. @since 0.1.1 */
a93fb468 449 SP_TRANSPORT_USB,
13efecf8 450 /** Bluetooth serial port adapter. @since 0.1.1 */
79a80046 451 SP_TRANSPORT_BLUETOOTH
a93fb468
AJ
452};
453
0a1ab8bf
UH
454/**
455 * @struct sp_port
456 * An opaque structure representing a serial port.
457 */
1c5aae9d 458struct sp_port;
eb6ed20f 459
0a1ab8bf
UH
460/**
461 * @struct sp_port_config
462 * An opaque structure representing the configuration for a serial port.
463 */
9b1502ef 464struct sp_port_config;
eb6ed20f 465
6f1186aa
ML
466/**
467 * @struct sp_event_set
468 * A set of handles to wait on for events.
469 */
470struct sp_event_set {
471 /** Array of OS-specific handles. */
472 void *handles;
473 /** Array of bitmasks indicating which events apply for each handle. */
474 enum sp_event *masks;
475 /** Number of handles. */
476 unsigned int count;
477};
478
091e75fe 479/**
144a598f
UH
480 * @defgroup Enumeration Port enumeration
481 *
482 * Enumerating the serial ports of a system.
483 *
7c8d67ef
ML
484 * See @ref list_ports.c for a working example of port enumeration.
485 *
144a598f
UH
486 * @{
487 */
091e75fe 488
cd5f5281 489/**
cf9d365c
UH
490 * Obtain a pointer to a new sp_port structure representing the named port.
491 *
492 * The user should allocate a variable of type "struct sp_port *" and pass a
493 * pointer to this to receive the result.
494 *
495 * The result should be freed after use by calling sp_free_port().
496 *
35578b06
UH
497 * @param[in] portname The OS-specific name of a serial port. Must not be NULL.
498 * @param[out] port_ptr If any error is returned, the variable pointed to by
499 * port_ptr will be set to NULL. Otherwise, it will be set
500 * to point to the newly allocated port. Must not be NULL.
f36c6395
ML
501 *
502 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
503 *
504 * @since 0.1.0
cf9d365c 505 */
0838c979 506SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
cd5f5281
ML
507
508/**
cf9d365c 509 * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
1652aa86 510 *
35578b06
UH
511 * @param[in] port Pointer to a port structure. Must not be NULL.
512 *
1652aa86 513 * @since 0.1.0
cf9d365c 514 */
0838c979 515SP_API void sp_free_port(struct sp_port *port);
cd5f5281
ML
516
517/**
cf9d365c
UH
518 * List the serial ports available on the system.
519 *
520 * The result obtained is an array of pointers to sp_port structures,
521 * terminated by a NULL. The user should allocate a variable of type
522 * "struct sp_port **" and pass a pointer to this to receive the result.
523 *
524 * The result should be freed after use by calling sp_free_port_list().
525 * If a port from the list is to be used after freeing the list, it must be
526 * copied first using sp_copy_port().
527 *
35578b06
UH
528 * @param[out] list_ptr If any error is returned, the variable pointed to by
529 * list_ptr will be set to NULL. Otherwise, it will be set
530 * to point to the newly allocated array. Must not be NULL.
f36c6395
ML
531 *
532 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
533 *
534 * @since 0.1.0
cf9d365c 535 */
0838c979 536SP_API enum sp_return sp_list_ports(struct sp_port ***list_ptr);
cd5f5281
ML
537
538/**
35578b06 539 * Make a new copy of an sp_port structure.
cf9d365c
UH
540 *
541 * The user should allocate a variable of type "struct sp_port *" and pass a
542 * pointer to this to receive the result.
543 *
544 * The copy should be freed after use by calling sp_free_port().
545 *
35578b06
UH
546 * @param[in] port Pointer to a port structure. Must not be NULL.
547 * @param[out] copy_ptr If any error is returned, the variable pointed to by
548 * copy_ptr will be set to NULL. Otherwise, it will be set
549 * to point to the newly allocated copy. Must not be NULL.
f36c6395
ML
550 *
551 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
552 *
553 * @since 0.1.0
cf9d365c 554 */
0838c979 555SP_API enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
cd5f5281
ML
556
557/**
cf9d365c
UH
558 * Free a port list obtained from sp_list_ports().
559 *
560 * This will also free all the sp_port structures referred to from the list;
561 * any that are to be retained must be copied first using sp_copy_port().
1652aa86 562 *
35578b06
UH
563 * @param[in] ports Pointer to a list of port structures. Must not be NULL.
564 *
1652aa86 565 * @since 0.1.0
cf9d365c 566 */
0838c979 567SP_API void sp_free_port_list(struct sp_port **ports);
e96d8bd2 568
091e75fe 569/**
cf9d365c 570 * @}
144a598f
UH
571 * @defgroup Ports Port handling
572 *
573 * Opening, closing and querying ports.
574 *
7c8d67ef
ML
575 * See @ref port_info.c for a working example of getting port information.
576 *
cf9d365c
UH
577 * @{
578 */
091e75fe 579
cd5f5281 580/**
cf9d365c
UH
581 * Open the specified serial port.
582 *
35578b06
UH
583 * @param[in] port Pointer to a port structure. Must not be NULL.
584 * @param[in] flags Flags to use when opening the serial port.
cf9d365c 585 *
f36c6395 586 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
587 *
588 * @since 0.1.0
cf9d365c 589 */
0838c979 590SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
cd5f5281
ML
591
592/**
cf9d365c
UH
593 * Close the specified serial port.
594 *
35578b06
UH
595 * @param[in] port Pointer to a port structure. Must not be NULL.
596 *
f36c6395 597 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
598 *
599 * @since 0.1.0
cf9d365c 600 */
0838c979 601SP_API enum sp_return sp_close(struct sp_port *port);
e96d8bd2 602
0151b157
ML
603/**
604 * Get the name of a port.
605 *
606 * The name returned is whatever is normally used to refer to a port on the
607 * current operating system; e.g. for Windows it will usually be a "COMn"
608 * device name, and for Unix it will be a device path beginning with "/dev/".
609 *
35578b06 610 * @param[in] port Pointer to a port structure. Must not be NULL.
0151b157
ML
611 *
612 * @return The port name, or NULL if an invalid port is passed. The name
35578b06
UH
613 * string is part of the port structure and may not be used after
614 * the port structure has been freed.
1652aa86
UH
615 *
616 * @since 0.1.0
0151b157 617 */
0838c979 618SP_API char *sp_get_port_name(const struct sp_port *port);
0151b157 619
a93fb468
AJ
620/**
621 * Get a description for a port, to present to end user.
622 *
35578b06 623 * @param[in] port Pointer to a port structure. Must not be NULL.
a93fb468
AJ
624 *
625 * @return The port description, or NULL if an invalid port is passed.
35578b06
UH
626 * The description string is part of the port structure and may not
627 * be used after the port structure has been freed.
a93fb468 628 *
3f2f48fc 629 * @since 0.1.1
a93fb468 630 */
0838c979 631SP_API char *sp_get_port_description(const struct sp_port *port);
a93fb468
AJ
632
633/**
634 * Get the transport type used by a port.
635 *
35578b06 636 * @param[in] port Pointer to a port structure. Must not be NULL.
a93fb468
AJ
637 *
638 * @return The port transport type.
639 *
3f2f48fc 640 * @since 0.1.1
a93fb468 641 */
0838c979 642SP_API enum sp_transport sp_get_port_transport(const struct sp_port *port);
a93fb468
AJ
643
644/**
645 * Get the USB bus number and address on bus of a USB serial adapter port.
646 *
35578b06 647 * @param[in] port Pointer to a port structure. Must not be NULL.
ff6da776
UH
648 * @param[out] usb_bus Pointer to a variable to store the USB bus.
649 * Can be NULL (in that case it will be ignored).
650 * @param[out] usb_address Pointer to a variable to store the USB address.
651 * Can be NULL (in that case it will be ignored).
a93fb468
AJ
652 *
653 * @return SP_OK upon success, a negative error code otherwise.
654 *
3f2f48fc 655 * @since 0.1.1
a93fb468 656 */
0838c979 657SP_API enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port,
a93fb468
AJ
658 int *usb_bus, int *usb_address);
659
660/**
661 * Get the USB Vendor ID and Product ID of a USB serial adapter port.
662 *
35578b06 663 * @param[in] port Pointer to a port structure. Must not be NULL.
9eb9f071
UH
664 * @param[out] usb_vid Pointer to a variable to store the USB VID.
665 * Can be NULL (in that case it will be ignored).
666 * @param[out] usb_pid Pointer to a variable to store the USB PID.
667 * Can be NULL (in that case it will be ignored).
a93fb468
AJ
668 *
669 * @return SP_OK upon success, a negative error code otherwise.
670 *
3f2f48fc 671 * @since 0.1.1
a93fb468 672 */
0838c979 673SP_API enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, int *usb_vid, int *usb_pid);
a93fb468
AJ
674
675/**
676 * Get the USB manufacturer string of a USB serial adapter port.
677 *
35578b06 678 * @param[in] port Pointer to a port structure. Must not be NULL.
a93fb468
AJ
679 *
680 * @return The port manufacturer string, or NULL if an invalid port is passed.
35578b06
UH
681 * The manufacturer string is part of the port structure and may not
682 * be used after the port structure has been freed.
a93fb468 683 *
3f2f48fc 684 * @since 0.1.1
a93fb468 685 */
0838c979 686SP_API char *sp_get_port_usb_manufacturer(const struct sp_port *port);
a93fb468
AJ
687
688/**
689 * Get the USB product string of a USB serial adapter port.
690 *
35578b06 691 * @param[in] port Pointer to a port structure. Must not be NULL.
a93fb468
AJ
692 *
693 * @return The port product string, or NULL if an invalid port is passed.
35578b06
UH
694 * The product string is part of the port structure and may not be
695 * used after the port structure has been freed.
a93fb468 696 *
3f2f48fc 697 * @since 0.1.1
a93fb468 698 */
0838c979 699SP_API char *sp_get_port_usb_product(const struct sp_port *port);
a93fb468
AJ
700
701/**
702 * Get the USB serial number string of a USB serial adapter port.
703 *
35578b06 704 * @param[in] port Pointer to a port structure. Must not be NULL.
a93fb468
AJ
705 *
706 * @return The port serial number, or NULL if an invalid port is passed.
35578b06
UH
707 * The serial number string is part of the port structure and may
708 * not be used after the port structure has been freed.
a93fb468 709 *
3f2f48fc 710 * @since 0.1.1
a93fb468 711 */
0838c979 712SP_API char *sp_get_port_usb_serial(const struct sp_port *port);
a93fb468
AJ
713
714/**
e33dcf90 715 * Get the MAC address of a Bluetooth serial adapter port.
a93fb468 716 *
35578b06 717 * @param[in] port Pointer to a port structure. Must not be NULL.
a93fb468 718 *
e33dcf90 719 * @return The port MAC address, or NULL if an invalid port is passed.
35578b06
UH
720 * The MAC address string is part of the port structure and may not
721 * be used after the port structure has been freed.
a93fb468 722 *
3f2f48fc 723 * @since 0.1.1
a93fb468 724 */
0838c979 725SP_API char *sp_get_port_bluetooth_address(const struct sp_port *port);
a93fb468 726
0151b157
ML
727/**
728 * Get the operating system handle for a port.
729 *
730 * The type of the handle depends on the operating system. On Unix based
731 * systems, the handle is a file descriptor of type "int". On Windows, the
732 * handle is of type "HANDLE". The user should allocate a variable of the
733 * appropriate type and pass a pointer to this to receive the result.
734 *
735 * To obtain a valid handle, the port must first be opened by calling
736 * sp_open() using the same port structure.
737 *
738 * After the port is closed or the port structure freed, the handle may
739 * no longer be valid.
740 *
741 * @warning This feature is provided so that programs may make use of
742 * OS-specific functionality where desired. Doing so obviously
743 * comes at a cost in portability. It also cannot be guaranteed
744 * that direct usage of the OS handle will not conflict with the
745 * library's own usage of the port. Be careful.
746 *
35578b06
UH
747 * @param[in] port Pointer to a port structure. Must not be NULL.
748 * @param[out] result_ptr If any error is returned, the variable pointed to by
ff6da776
UH
749 * result_ptr will have unknown contents and should not
750 * be used. Otherwise, it will be set to point to the
751 * OS handle. Must not be NULL.
35578b06 752 *
0151b157 753 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
754 *
755 * @since 0.1.0
0151b157 756 */
0838c979 757SP_API enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
0151b157 758
cd5f5281 759/**
cf9d365c 760 * @}
144a598f
UH
761 *
762 * @defgroup Configuration Configuration
763 *
764 * Setting and querying serial port parameters.
6dba8447
ML
765 *
766 * See @ref port_config.c for a working example of port configuration.
767 *
768 * You should always configure all settings before using a port.
769 * There are no default settings applied by libserialport.
770 * When you open a port it may have default settings from the OS or
771 * driver, or the settings left over by the last program to use it.
772 *
773 * You should always set baud rate, data bits, parity and stop bits.
774 *
775 * You should normally also set one of the preset @ref sp_flowcontrol
776 * flow control modes, which will set up the RTS, CTS, DTR and DSR pin
777 * behaviours and enable or disable XON/XOFF. If you need an unusual
778 * configuration not covered by the preset flow control modes, you
779 * will need to configure these settings individually, and avoid
780 * calling sp_set_flowcontrol() or sp_set_config_flowcontrol() which
781 * will overwrite these settings.
782 *
783 * A port must be opened before you can change its settings.
784 *
785 * There are two ways of accessing port settings:
786 *
787 * Configuration structures
788 * ------------------------
789 *
790 * You can read and write a whole configuration (all settings at once)
791 * using sp_get_config() and sp_set_config(). This is handy if you want
792 * to change between some preset combinations, or save and restore an
793 * existing configuration. It also ensures the changes are made
794 * together, via an efficient set of calls into the OS - in some cases
795 * a single system call can be used.
796 *
797 * Use accessor functions like sp_get_config_baudrate() and
798 * sp_set_config_baudrate() to get and set individual settings
799 * from a configuration.
800 *
801 * For each setting in a port configuration, a special value of -1 can
802 * be used, which will cause that setting to be left alone when the
803 * configuration is applied by sp_set_config().
804 *
805 * This value is also be used by sp_get_config() for any settings
806 * which are unconfigured at the OS level, or in a state that is
807 * not representable within the libserialport API.
808 *
809 * Configurations are allocated using sp_new_config() and freed
810 * with sp_free_config(). You need to manage them yourself. When
811 * a new configuration is allocated by sp_new_config(), all of
812 * its settings are initially set to the special -1 value.
813 *
814 * Direct functions for changing port settings
815 * -------------------------------------------
816 *
817 * As a shortcut, you can set individual settings on a port directly
818 * by calling functions like sp_set_baudrate() and sp_set_parity().
819 * This saves you the work of allocating a temporary config, setting it
820 * up, applying it to a port and then freeing it.
821 *
cf9d365c
UH
822 * @{
823 */
e96d8bd2 824
9b1502ef
ML
825/**
826 * Allocate a port configuration structure.
827 *
480d750c
ML
828 * The user should allocate a variable of type "struct sp_port_config *" and
829 * pass a pointer to this to receive the result. The variable will be updated
830 * to point to the new configuration structure. The structure is opaque and
831 * must be accessed via the functions provided.
9b1502ef
ML
832 *
833 * All parameters in the structure will be initialised to special values which
834 * are ignored by sp_set_config().
835 *
836 * The structure should be freed after use by calling sp_free_config().
837 *
ff6da776
UH
838 * @param[out] config_ptr If any error is returned, the variable pointed to by
839 * config_ptr will be set to NULL. Otherwise, it will
840 * be set to point to the allocated config structure.
35578b06 841 * Must not be NULL.
9b1502ef
ML
842 *
843 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
844 *
845 * @since 0.1.0
9b1502ef 846 */
0838c979 847SP_API enum sp_return sp_new_config(struct sp_port_config **config_ptr);
9b1502ef
ML
848
849/**
850 * Free a port configuration structure.
851 *
35578b06 852 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1652aa86
UH
853 *
854 * @since 0.1.0
9b1502ef 855 */
0838c979 856SP_API void sp_free_config(struct sp_port_config *config);
9b1502ef 857
cd5f5281 858/**
cf9d365c
UH
859 * Get the current configuration of the specified serial port.
860 *
9b1502ef
ML
861 * The user should allocate a configuration structure using sp_new_config()
862 * and pass this as the config parameter. The configuration structure will
863 * be updated with the port configuration.
cf9d365c 864 *
9b1502ef
ML
865 * Any parameters that are configured with settings not recognised or
866 * supported by libserialport, will be set to special values that are
867 * ignored by sp_set_config().
cf9d365c 868 *
35578b06
UH
869 * @param[in] port Pointer to a port structure. Must not be NULL.
870 * @param[out] config Pointer to a configuration structure that will hold
ff6da776
UH
871 * the result. Upon errors the contents of the config
872 * struct will not be changed. Must not be NULL.
35578b06 873 *
f36c6395 874 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
875 *
876 * @since 0.1.0
cf9d365c 877 */
0838c979 878SP_API enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
cd5f5281
ML
879
880/**
cf9d365c
UH
881 * Set the configuration for the specified serial port.
882 *
9b1502ef
ML
883 * For each parameter in the configuration, there is a special value (usually
884 * -1, but see the documentation for each field). These values will be ignored
885 * and the corresponding setting left unchanged on the port.
cf9d365c 886 *
ff6da776
UH
887 * Upon errors, the configuration of the serial port is unknown since
888 * partial/incomplete config updates may have happened.
889 *
35578b06
UH
890 * @param[in] port Pointer to a port structure. Must not be NULL.
891 * @param[in] config Pointer to a configuration structure. Must not be NULL.
892 *
f36c6395 893 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
894 *
895 * @since 0.1.0
cf9d365c 896 */
0838c979 897SP_API enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
cd5f5281
ML
898
899/**
cf9d365c
UH
900 * Set the baud rate for the specified serial port.
901 *
35578b06
UH
902 * @param[in] port Pointer to a port structure. Must not be NULL.
903 * @param[in] baudrate Baud rate in bits per second.
cf9d365c 904 *
f36c6395 905 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
906 *
907 * @since 0.1.0
cf9d365c 908 */
0838c979 909SP_API enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
cd5f5281
ML
910
911/**
9b1502ef
ML
912 * Get the baud rate from a port configuration.
913 *
35578b06
UH
914 * The user should allocate a variable of type int and
915 * pass a pointer to this to receive the result.
9b1502ef 916 *
35578b06
UH
917 * @param[in] config Pointer to a configuration structure. Must not be NULL.
918 * @param[out] baudrate_ptr Pointer to a variable to store the result. Must not be NULL.
9b1502ef
ML
919 *
920 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
921 *
922 * @since 0.1.0
9b1502ef 923 */
0838c979 924SP_API enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
9b1502ef
ML
925
926/**
927 * Set the baud rate in a port configuration.
928 *
35578b06
UH
929 * @param[in] config Pointer to a configuration structure. Must not be NULL.
930 * @param[in] baudrate Baud rate in bits per second, or -1 to retain the current setting.
9b1502ef
ML
931 *
932 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
933 *
934 * @since 0.1.0
9b1502ef 935 */
0838c979 936SP_API enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
9b1502ef
ML
937
938/**
939 * Set the data bits for the specified serial port.
cf9d365c 940 *
35578b06
UH
941 * @param[in] port Pointer to a port structure. Must not be NULL.
942 * @param[in] bits Number of data bits.
cf9d365c 943 *
f36c6395 944 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
945 *
946 * @since 0.1.0
cf9d365c 947 */
0838c979 948SP_API enum sp_return sp_set_bits(struct sp_port *port, int bits);
cd5f5281
ML
949
950/**
9b1502ef
ML
951 * Get the data bits from a port configuration.
952 *
35578b06
UH
953 * The user should allocate a variable of type int and
954 * pass a pointer to this to receive the result.
9b1502ef 955 *
35578b06
UH
956 * @param[in] config Pointer to a configuration structure. Must not be NULL.
957 * @param[out] bits_ptr Pointer to a variable to store the result. Must not be NULL.
9b1502ef
ML
958 *
959 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
960 *
961 * @since 0.1.0
9b1502ef 962 */
0838c979 963SP_API enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
9b1502ef
ML
964
965/**
966 * Set the data bits in a port configuration.
967 *
35578b06
UH
968 * @param[in] config Pointer to a configuration structure. Must not be NULL.
969 * @param[in] bits Number of data bits, or -1 to retain the current setting.
9b1502ef
ML
970 *
971 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
972 *
973 * @since 0.1.0
9b1502ef 974 */
0838c979 975SP_API enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
9b1502ef
ML
976
977/**
978 * Set the parity setting for the specified serial port.
cf9d365c 979 *
35578b06
UH
980 * @param[in] port Pointer to a port structure. Must not be NULL.
981 * @param[in] parity Parity setting.
cf9d365c 982 *
f36c6395 983 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
984 *
985 * @since 0.1.0
cf9d365c 986 */
0838c979 987SP_API enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
cd5f5281
ML
988
989/**
9b1502ef
ML
990 * Get the parity setting from a port configuration.
991 *
35578b06
UH
992 * The user should allocate a variable of type enum sp_parity and
993 * pass a pointer to this to receive the result.
9b1502ef 994 *
35578b06
UH
995 * @param[in] config Pointer to a configuration structure. Must not be NULL.
996 * @param[out] parity_ptr Pointer to a variable to store the result. Must not be NULL.
9b1502ef
ML
997 *
998 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
999 *
1000 * @since 0.1.0
9b1502ef 1001 */
0838c979 1002SP_API enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
9b1502ef
ML
1003
1004/**
1005 * Set the parity setting in a port configuration.
1006 *
35578b06
UH
1007 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1008 * @param[in] parity Parity setting, or -1 to retain the current setting.
9b1502ef
ML
1009 *
1010 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1011 *
1012 * @since 0.1.0
9b1502ef 1013 */
0838c979 1014SP_API enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
9b1502ef
ML
1015
1016/**
1017 * Set the stop bits for the specified serial port.
cf9d365c 1018 *
35578b06
UH
1019 * @param[in] port Pointer to a port structure. Must not be NULL.
1020 * @param[in] stopbits Number of stop bits.
cf9d365c 1021 *
f36c6395 1022 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1023 *
1024 * @since 0.1.0
cf9d365c 1025 */
0838c979 1026SP_API enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
cd5f5281
ML
1027
1028/**
9b1502ef 1029 * Get the stop bits from a port configuration.
cf9d365c 1030 *
35578b06
UH
1031 * The user should allocate a variable of type int and
1032 * pass a pointer to this to receive the result.
cf9d365c 1033 *
35578b06
UH
1034 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1035 * @param[out] stopbits_ptr Pointer to a variable to store the result. Must not be NULL.
cf9d365c 1036 *
f36c6395 1037 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1038 *
1039 * @since 0.1.0
cf9d365c 1040 */
0838c979 1041SP_API enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
9b1502ef
ML
1042
1043/**
1044 * Set the stop bits in a port configuration.
1045 *
35578b06
UH
1046 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1047 * @param[in] stopbits Number of stop bits, or -1 to retain the current setting.
9b1502ef
ML
1048 *
1049 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1050 *
1051 * @since 0.1.0
9b1502ef 1052 */
0838c979 1053SP_API enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
18fc2dd1 1054
cd5f5281 1055/**
9b1502ef 1056 * Set the RTS pin behaviour for the specified serial port.
cf9d365c 1057 *
35578b06
UH
1058 * @param[in] port Pointer to a port structure. Must not be NULL.
1059 * @param[in] rts RTS pin mode.
cf9d365c 1060 *
f36c6395 1061 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1062 *
1063 * @since 0.1.0
cf9d365c 1064 */
0838c979 1065SP_API enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
cd5f5281
ML
1066
1067/**
9b1502ef
ML
1068 * Get the RTS pin behaviour from a port configuration.
1069 *
35578b06
UH
1070 * The user should allocate a variable of type enum sp_rts and
1071 * pass a pointer to this to receive the result.
9b1502ef 1072 *
35578b06
UH
1073 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1074 * @param[out] rts_ptr Pointer to a variable to store the result. Must not be NULL.
9b1502ef
ML
1075 *
1076 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1077 *
1078 * @since 0.1.0
9b1502ef 1079 */
0838c979 1080SP_API enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
9b1502ef
ML
1081
1082/**
1083 * Set the RTS pin behaviour in a port configuration.
1084 *
35578b06
UH
1085 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1086 * @param[in] rts RTS pin mode, or -1 to retain the current setting.
9b1502ef
ML
1087 *
1088 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1089 *
1090 * @since 0.1.0
9b1502ef 1091 */
0838c979 1092SP_API enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
9b1502ef
ML
1093
1094/**
1095 * Set the CTS pin behaviour for the specified serial port.
cf9d365c 1096 *
35578b06
UH
1097 * @param[in] port Pointer to a port structure. Must not be NULL.
1098 * @param[in] cts CTS pin mode.
cf9d365c 1099 *
f36c6395 1100 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1101 *
1102 * @since 0.1.0
cf9d365c 1103 */
0838c979 1104SP_API enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
cd5f5281
ML
1105
1106/**
9b1502ef
ML
1107 * Get the CTS pin behaviour from a port configuration.
1108 *
35578b06
UH
1109 * The user should allocate a variable of type enum sp_cts and
1110 * pass a pointer to this to receive the result.
9b1502ef 1111 *
35578b06
UH
1112 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1113 * @param[out] cts_ptr Pointer to a variable to store the result. Must not be NULL.
9b1502ef
ML
1114 *
1115 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1116 *
1117 * @since 0.1.0
9b1502ef 1118 */
0838c979 1119SP_API enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
9b1502ef
ML
1120
1121/**
1122 * Set the CTS pin behaviour in a port configuration.
1123 *
35578b06
UH
1124 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1125 * @param[in] cts CTS pin mode, or -1 to retain the current setting.
9b1502ef
ML
1126 *
1127 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1128 *
1129 * @since 0.1.0
9b1502ef 1130 */
0838c979 1131SP_API enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
9b1502ef
ML
1132
1133/**
1134 * Set the DTR pin behaviour for the specified serial port.
cf9d365c 1135 *
35578b06
UH
1136 * @param[in] port Pointer to a port structure. Must not be NULL.
1137 * @param[in] dtr DTR pin mode.
cf9d365c 1138 *
f36c6395 1139 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1140 *
1141 * @since 0.1.0
cf9d365c 1142 */
0838c979 1143SP_API enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
cd5f5281
ML
1144
1145/**
9b1502ef
ML
1146 * Get the DTR pin behaviour from a port configuration.
1147 *
35578b06
UH
1148 * The user should allocate a variable of type enum sp_dtr and
1149 * pass a pointer to this to receive the result.
9b1502ef 1150 *
35578b06
UH
1151 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1152 * @param[out] dtr_ptr Pointer to a variable to store the result. Must not be NULL.
9b1502ef
ML
1153 *
1154 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1155 *
1156 * @since 0.1.0
9b1502ef 1157 */
0838c979 1158SP_API enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
9b1502ef
ML
1159
1160/**
1161 * Set the DTR pin behaviour in a port configuration.
1162 *
35578b06
UH
1163 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1164 * @param[in] dtr DTR pin mode, or -1 to retain the current setting.
9b1502ef
ML
1165 *
1166 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1167 *
1168 * @since 0.1.0
9b1502ef 1169 */
0838c979 1170SP_API enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
9b1502ef
ML
1171
1172/**
1173 * Set the DSR pin behaviour for the specified serial port.
cf9d365c 1174 *
35578b06
UH
1175 * @param[in] port Pointer to a port structure. Must not be NULL.
1176 * @param[in] dsr DSR pin mode.
cf9d365c 1177 *
f36c6395 1178 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1179 *
1180 * @since 0.1.0
cf9d365c 1181 */
0838c979 1182SP_API enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
cd5f5281
ML
1183
1184/**
9b1502ef
ML
1185 * Get the DSR pin behaviour from a port configuration.
1186 *
35578b06
UH
1187 * The user should allocate a variable of type enum sp_dsr and
1188 * pass a pointer to this to receive the result.
9b1502ef 1189 *
35578b06
UH
1190 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1191 * @param[out] dsr_ptr Pointer to a variable to store the result. Must not be NULL.
9b1502ef
ML
1192 *
1193 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1194 *
1195 * @since 0.1.0
9b1502ef 1196 */
0838c979 1197SP_API enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
9b1502ef
ML
1198
1199/**
1200 * Set the DSR pin behaviour in a port configuration.
1201 *
35578b06
UH
1202 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1203 * @param[in] dsr DSR pin mode, or -1 to retain the current setting.
9b1502ef
ML
1204 *
1205 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1206 *
1207 * @since 0.1.0
9b1502ef 1208 */
0838c979 1209SP_API enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
9b1502ef
ML
1210
1211/**
1212 * Set the XON/XOFF configuration for the specified serial port.
cf9d365c 1213 *
35578b06
UH
1214 * @param[in] port Pointer to a port structure. Must not be NULL.
1215 * @param[in] xon_xoff XON/XOFF mode.
cf9d365c 1216 *
f36c6395 1217 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1218 *
1219 * @since 0.1.0
cf9d365c 1220 */
0838c979 1221SP_API enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
e96d8bd2 1222
9b1502ef
ML
1223/**
1224 * Get the XON/XOFF configuration from a port configuration.
1225 *
35578b06
UH
1226 * The user should allocate a variable of type enum sp_xonxoff and
1227 * pass a pointer to this to receive the result.
9b1502ef 1228 *
35578b06
UH
1229 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1230 * @param[out] xon_xoff_ptr Pointer to a variable to store the result. Must not be NULL.
9b1502ef
ML
1231 *
1232 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1233 *
1234 * @since 0.1.0
9b1502ef 1235 */
0838c979 1236SP_API enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
9b1502ef
ML
1237
1238/**
1239 * Set the XON/XOFF configuration in a port configuration.
1240 *
35578b06
UH
1241 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1242 * @param[in] xon_xoff XON/XOFF mode, or -1 to retain the current setting.
9b1502ef
ML
1243 *
1244 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1245 *
1246 * @since 0.1.0
9b1502ef 1247 */
0838c979 1248SP_API enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
9b1502ef
ML
1249
1250/**
1251 * Set the flow control type in a port configuration.
1252 *
1253 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
1254 * XON/XOFF settings as necessary for the specified flow control
1255 * type. For more fine-grained control of these settings, use their
1256 * individual configuration functions.
1257 *
35578b06
UH
1258 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1259 * @param[in] flowcontrol Flow control setting to use.
9b1502ef
ML
1260 *
1261 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1262 *
1263 * @since 0.1.0
9b1502ef 1264 */
0838c979 1265SP_API enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
9b1502ef
ML
1266
1267/**
1268 * Set the flow control type for the specified serial port.
1269 *
1270 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
1271 * XON/XOFF settings as necessary for the specified flow control
1272 * type. For more fine-grained control of these settings, use their
1273 * individual configuration functions.
1274 *
35578b06
UH
1275 * @param[in] port Pointer to a port structure. Must not be NULL.
1276 * @param[in] flowcontrol Flow control setting to use.
9b1502ef
ML
1277 *
1278 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1279 *
1280 * @since 0.1.0
9b1502ef 1281 */
0838c979 1282SP_API enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
9b1502ef 1283
091e75fe 1284/**
cf9d365c 1285 * @}
144a598f
UH
1286 *
1287 * @defgroup Data Data handling
1288 *
1289 * Reading, writing, and flushing data.
1290 *
cf9d365c 1291 * @{
144a598f 1292 */
091e75fe
ML
1293
1294/**
2007ce5e
ML
1295 * Read bytes from the specified serial port, blocking until complete.
1296 *
b87deb7c
ML
1297 * @warning If your program runs on Unix, defines its own signal handlers, and
1298 * needs to abort blocking reads when these are called, then you
1299 * should not use this function. It repeats system calls that return
1300 * with EINTR. To be able to abort a read from a signal handler, you
1301 * should implement your own blocking read using sp_nonblocking_read()
2007ce5e
ML
1302 * together with a blocking method that makes sense for your program.
1303 * E.g. you can obtain the file descriptor for an open port using
1304 * sp_get_port_handle() and use this to call select() or pselect(),
1305 * with appropriate arrangements to return if a signal is received.
cf9d365c 1306 *
35578b06
UH
1307 * @param[in] port Pointer to a port structure. Must not be NULL.
1308 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1309 * @param[in] count Requested number of bytes to read.
1b91c6ea 1310 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
e3dcf906
ML
1311 *
1312 * @return The number of bytes read on success, or a negative error code. If
1313 * the number of bytes returned is less than that requested, the
1314 * timeout was reached before the requested number of bytes was
1315 * available. If timeout is zero, the function will always return
1316 * either the requested number of bytes or a negative error code.
1652aa86
UH
1317 *
1318 * @since 0.1.0
e3dcf906 1319 */
0838c979 1320SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
e3dcf906 1321
e5c2630e
ML
1322/**
1323 * Read bytes from the specified serial port, returning as soon as any data is
1324 * available.
1325 *
1326 * @warning If your program runs on Unix, defines its own signal handlers, and
1327 * needs to abort blocking reads when these are called, then you
1328 * should not use this function. It repeats system calls that return
1329 * with EINTR. To be able to abort a read from a signal handler, you
1330 * should implement your own blocking read using sp_nonblocking_read()
1331 * together with a blocking method that makes sense for your program.
1332 * E.g. you can obtain the file descriptor for an open port using
1333 * sp_get_port_handle() and use this to call select() or pselect(),
1334 * with appropriate arrangements to return if a signal is received.
1335 *
1336 * @param[in] port Pointer to a port structure. Must not be NULL.
1337 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1338 * @param[in] count Maximum number of bytes to read. Must not be zero.
1339 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1340 *
1341 * @return The number of bytes read on success, or a negative error code. If
1342 * the result is zero, the timeout was reached before any bytes were
1343 * available. If timeout_ms is zero, the function will always return
1344 * either at least one byte, or a negative error code.
1345 *
1346 * @since 0.1.1
1347 */
0838c979 1348SP_API enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
e5c2630e 1349
e3dcf906
ML
1350/**
1351 * Read bytes from the specified serial port, without blocking.
cf9d365c 1352 *
35578b06
UH
1353 * @param[in] port Pointer to a port structure. Must not be NULL.
1354 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1355 * @param[in] count Maximum number of bytes to read.
cf9d365c 1356 *
e3dcf906
ML
1357 * @return The number of bytes read on success, or a negative error code. The
1358 * number of bytes returned may be any number from zero to the maximum
1359 * that was requested.
1652aa86
UH
1360 *
1361 * @since 0.1.0
e3dcf906 1362 */
0838c979 1363SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
e3dcf906
ML
1364
1365/**
1366 * Write bytes to the specified serial port, blocking until complete.
1367 *
1368 * Note that this function only ensures that the accepted bytes have been
1369 * written to the OS; they may be held in driver or hardware buffers and not
1370 * yet physically transmitted. To check whether all written bytes have actually
1371 * been transmitted, use the sp_output_waiting() function. To wait until all
1372 * written bytes have actually been transmitted, use the sp_drain() function.
1373 *
b87deb7c
ML
1374 * @warning If your program runs on Unix, defines its own signal handlers, and
1375 * needs to abort blocking writes when these are called, then you
1376 * should not use this function. It repeats system calls that return
1377 * with EINTR. To be able to abort a write from a signal handler, you
1378 * should implement your own blocking write using sp_nonblocking_write()
2007ce5e
ML
1379 * together with a blocking method that makes sense for your program.
1380 * E.g. you can obtain the file descriptor for an open port using
1381 * sp_get_port_handle() and use this to call select() or pselect(),
1382 * with appropriate arrangements to return if a signal is received.
1383 *
35578b06
UH
1384 * @param[in] port Pointer to a port structure. Must not be NULL.
1385 * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1386 * @param[in] count Requested number of bytes to write.
1b91c6ea 1387 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
e3dcf906 1388 *
85987464
ML
1389 * @return The number of bytes written on success, or a negative error code.
1390 * If the number of bytes returned is less than that requested, the
e3dcf906 1391 * timeout was reached before the requested number of bytes was
85987464 1392 * written. If timeout is zero, the function will always return
e3dcf906
ML
1393 * either the requested number of bytes or a negative error code. In
1394 * the event of an error there is no way to determine how many bytes
cfa5af67 1395 * were sent before the error occurred.
1652aa86
UH
1396 *
1397 * @since 0.1.0
cf9d365c 1398 */
0838c979 1399SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout_ms);
091e75fe
ML
1400
1401/**
e3dcf906 1402 * Write bytes to the specified serial port, without blocking.
cf9d365c 1403 *
e3dcf906
ML
1404 * Note that this function only ensures that the accepted bytes have been
1405 * written to the OS; they may be held in driver or hardware buffers and not
1406 * yet physically transmitted. To check whether all written bytes have actually
1407 * been transmitted, use the sp_output_waiting() function. To wait until all
1408 * written bytes have actually been transmitted, use the sp_drain() function.
cf9d365c 1409 *
35578b06
UH
1410 * @param[in] port Pointer to a port structure. Must not be NULL.
1411 * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1412 * @param[in] count Maximum number of bytes to write.
cf9d365c 1413 *
f36c6395 1414 * @return The number of bytes written on success, or a negative error code.
e3dcf906
ML
1415 * The number of bytes returned may be any number from zero to the
1416 * maximum that was requested.
1652aa86
UH
1417 *
1418 * @since 0.1.0
cf9d365c 1419 */
0838c979 1420SP_API enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
091e75fe 1421
3353c22f
ML
1422/**
1423 * Gets the number of bytes waiting in the input buffer.
1424 *
35578b06 1425 * @param[in] port Pointer to a port structure. Must not be NULL.
3353c22f
ML
1426 *
1427 * @return Number of bytes waiting on success, a negative error code otherwise.
1652aa86
UH
1428 *
1429 * @since 0.1.0
3353c22f 1430 */
0838c979 1431SP_API enum sp_return sp_input_waiting(struct sp_port *port);
3353c22f
ML
1432
1433/**
1434 * Gets the number of bytes waiting in the output buffer.
1435 *
35578b06 1436 * @param[in] port Pointer to a port structure. Must not be NULL.
3353c22f
ML
1437 *
1438 * @return Number of bytes waiting on success, a negative error code otherwise.
1652aa86
UH
1439 *
1440 * @since 0.1.0
3353c22f 1441 */
0838c979 1442SP_API enum sp_return sp_output_waiting(struct sp_port *port);
3353c22f 1443
091e75fe 1444/**
fd8fd11a
ML
1445 * Flush serial port buffers. Data in the selected buffer(s) is discarded.
1446 *
35578b06
UH
1447 * @param[in] port Pointer to a port structure. Must not be NULL.
1448 * @param[in] buffers Which buffer(s) to flush.
cf9d365c 1449 *
f36c6395 1450 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1451 *
1452 * @since 0.1.0
cf9d365c 1453 */
0838c979 1454SP_API enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
091e75fe 1455
69a3739c
ML
1456/**
1457 * Wait for buffered data to be transmitted.
1458 *
2c827b21
ML
1459 * @warning If your program runs on Unix, defines its own signal handlers, and
1460 * needs to abort draining the output buffer when when these are
1461 * called, then you should not use this function. It repeats system
1462 * calls that return with EINTR. To be able to abort a drain from a
1463 * signal handler, you would need to implement your own blocking
1464 * drain by polling the result of sp_output_waiting().
1465 *
35578b06 1466 * @param[in] port Pointer to a port structure. Must not be NULL.
3f099f4f 1467 *
f36c6395 1468 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1469 *
1470 * @since 0.1.0
69a3739c 1471 */
0838c979 1472SP_API enum sp_return sp_drain(struct sp_port *port);
69a3739c 1473
6f1186aa
ML
1474/**
1475 * @}
144a598f
UH
1476 *
1477 * @defgroup Waiting Waiting
1478 *
1479 * Waiting for events and timeout handling.
1480 *
6f1186aa
ML
1481 * @{
1482 */
1483
1484/**
1485 * Allocate storage for a set of events.
1486 *
1487 * The user should allocate a variable of type struct sp_event_set *,
1488 * then pass a pointer to this variable to receive the result.
1489 *
1490 * The result should be freed after use by calling sp_free_event_set().
1491 *
35578b06
UH
1492 * @param[out] result_ptr If any error is returned, the variable pointed to by
1493 * result_ptr will be set to NULL. Otherwise, it will
1494 * be set to point to the event set. Must not be NULL.
1495 *
6f1186aa 1496 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1497 *
1498 * @since 0.1.0
6f1186aa 1499 */
0838c979 1500SP_API enum sp_return sp_new_event_set(struct sp_event_set **result_ptr);
6f1186aa
ML
1501
1502/**
1503 * Add events to a struct sp_event_set for a given port.
1504 *
1505 * The port must first be opened by calling sp_open() using the same port
1506 * structure.
1507 *
1508 * After the port is closed or the port structure freed, the results may
1509 * no longer be valid.
1510 *
35578b06
UH
1511 * @param[in,out] event_set Event set to update. Must not be NULL.
1512 * @param[in] port Pointer to a port structure. Must not be NULL.
1513 * @param[in] mask Bitmask of events to be waited for.
6f1186aa
ML
1514 *
1515 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1516 *
1517 * @since 0.1.0
6f1186aa 1518 */
0838c979 1519SP_API enum sp_return sp_add_port_events(struct sp_event_set *event_set,
6f1186aa
ML
1520 const struct sp_port *port, enum sp_event mask);
1521
1522/**
1523 * Wait for any of a set of events to occur.
1524 *
35578b06 1525 * @param[in] event_set Event set to wait on. Must not be NULL.
1b91c6ea 1526 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
6f1186aa
ML
1527 *
1528 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1529 *
1530 * @since 0.1.0
6f1186aa 1531 */
0838c979 1532SP_API enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout_ms);
6f1186aa
ML
1533
1534/**
1535 * Free a structure allocated by sp_new_event_set().
1652aa86 1536 *
35578b06
UH
1537 * @param[in] event_set Event set to free. Must not be NULL.
1538 *
1652aa86 1539 * @since 0.1.0
6f1186aa 1540 */
0838c979 1541SP_API void sp_free_event_set(struct sp_event_set *event_set);
6f1186aa 1542
90cc3ee6
ML
1543/**
1544 * @}
144a598f
UH
1545 *
1546 * @defgroup Signals Signals
1547 *
1548 * Port signalling operations.
1549 *
90cc3ee6
ML
1550 * @{
1551 */
1552
8cf7c697
ML
1553/**
1554 * Gets the status of the control signals for the specified port.
1555 *
1556 * The user should allocate a variable of type "enum sp_signal" and pass a
1557 * pointer to this variable to receive the result. The result is a bitmask
1558 * in which individual signals can be checked by bitwise OR with values of
1559 * the sp_signal enum.
1560 *
35578b06
UH
1561 * @param[in] port Pointer to a port structure. Must not be NULL.
1562 * @param[out] signal_mask Pointer to a variable to receive the result.
1563 * Must not be NULL.
8cf7c697 1564 *
f36c6395 1565 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1566 *
1567 * @since 0.1.0
8cf7c697 1568 */
0838c979 1569SP_API enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signal_mask);
8cf7c697 1570
90cc3ee6
ML
1571/**
1572 * Put the port transmit line into the break state.
1573 *
35578b06 1574 * @param[in] port Pointer to a port structure. Must not be NULL.
3f099f4f 1575 *
f36c6395 1576 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1577 *
1578 * @since 0.1.0
90cc3ee6 1579 */
0838c979 1580SP_API enum sp_return sp_start_break(struct sp_port *port);
90cc3ee6
ML
1581
1582/**
1583 * Take the port transmit line out of the break state.
1584 *
35578b06 1585 * @param[in] port Pointer to a port structure. Must not be NULL.
3f099f4f 1586 *
f36c6395 1587 * @return SP_OK upon success, a negative error code otherwise.
1652aa86
UH
1588 *
1589 * @since 0.1.0
90cc3ee6 1590 */
0838c979 1591SP_API enum sp_return sp_end_break(struct sp_port *port);
90cc3ee6 1592
091e75fe 1593/**
cf9d365c 1594 * @}
144a598f
UH
1595 *
1596 * @defgroup Errors Errors
1597 *
1598 * Obtaining error information.
1599 *
47200531
ML
1600 * See @ref handle_errors.c for an example of error handling.
1601 *
cf9d365c 1602 * @{
144a598f 1603 */
091e75fe 1604
cd5f5281 1605/**
cf9d365c
UH
1606 * Get the error code for a failed operation.
1607 *
1608 * In order to obtain the correct result, this function should be called
1609 * straight after the failure, before executing any other system operations.
ec4b55ae
ML
1610 * The result is thread-specific, and only valid when called immediately
1611 * after a previous call returning SP_ERR_FAIL.
cf9d365c
UH
1612 *
1613 * @return The system's numeric code for the error that caused the last
1614 * operation to fail.
1652aa86
UH
1615 *
1616 * @since 0.1.0
cf9d365c 1617 */
0838c979 1618SP_API int sp_last_error_code(void);
cd5f5281
ML
1619
1620/**
cf9d365c
UH
1621 * Get the error message for a failed operation.
1622 *
1623 * In order to obtain the correct result, this function should be called
1624 * straight after the failure, before executing other system operations.
ec4b55ae
ML
1625 * The result is thread-specific, and only valid when called immediately
1626 * after a previous call returning SP_ERR_FAIL.
cf9d365c
UH
1627 *
1628 * @return The system's message for the error that caused the last
1629 * operation to fail. This string may be allocated by the function,
1630 * and should be freed after use by calling sp_free_error_message().
1652aa86
UH
1631 *
1632 * @since 0.1.0
cf9d365c 1633 */
0838c979 1634SP_API char *sp_last_error_message(void);
cd5f5281
ML
1635
1636/**
cf9d365c 1637 * Free an error message returned by sp_last_error_message().
1652aa86 1638 *
35578b06
UH
1639 * @param[in] message The error message string to free. Must not be NULL.
1640 *
1652aa86 1641 * @since 0.1.0
cf9d365c 1642 */
0838c979 1643SP_API void sp_free_error_message(char *message);
e8ffaee9 1644
863b35e6
ML
1645/**
1646 * Set the handler function for library debugging messages.
1647 *
1648 * Debugging messages are generated by the library during each operation,
1649 * to help in diagnosing problems. The handler will be called for each
1650 * message. The handler can be set to NULL to ignore all debug messages.
1651 *
1652 * The handler function should accept a format string and variable length
1653 * argument list, in the same manner as e.g. printf().
1654 *
1655 * The default handler is sp_default_debug_handler().
1652aa86 1656 *
35578b06
UH
1657 * @param[in] handler The handler function to use. Can be NULL (in that case
1658 * all debug messages will be ignored).
1659 *
1652aa86 1660 * @since 0.1.0
863b35e6 1661 */
0838c979 1662SP_API void sp_set_debug_handler(void (*handler)(const char *format, ...));
863b35e6
ML
1663
1664/**
1665 * Default handler function for library debugging messages.
1666 *
1667 * This function prints debug messages to the standard error stream if the
1668 * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
1669 * ignored.
1652aa86 1670 *
35578b06
UH
1671 * @param[in] format The format string to use. Must not be NULL.
1672 * @param[in] ... The variable length argument list to use.
1673 *
1652aa86 1674 * @since 0.1.0
863b35e6 1675 */
0838c979 1676SP_API void sp_default_debug_handler(const char *format, ...);
863b35e6 1677
cf9d365c 1678/** @} */
091e75fe 1679
524b0e14 1680/**
144a598f
UH
1681 * @defgroup Versions Versions
1682 *
1683 * Version number querying functions, definitions, and macros.
524b0e14
UH
1684 *
1685 * This set of API calls returns two different version numbers related
1686 * to libserialport. The "package version" is the release version number of the
1687 * libserialport tarball in the usual "major.minor.micro" format, e.g. "0.1.0".
1688 *
1689 * The "library version" is independent of that; it is the libtool version
1690 * number in the "current:revision:age" format, e.g. "2:0:0".
1691 * See http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning for details.
1692 *
1693 * Both version numbers (and/or individual components of them) can be
1694 * retrieved via the API calls at runtime, and/or they can be checked at
1695 * compile/preprocessor time using the respective macros.
1696 *
1697 * @{
1698 */
1699
1700/*
1701 * Package version macros (can be used for conditional compilation).
1702 */
1703
1704/** The libserialport package 'major' version number. */
f6e32b2d 1705#define SP_PACKAGE_VERSION_MAJOR 0
524b0e14
UH
1706
1707/** The libserialport package 'minor' version number. */
f6e32b2d 1708#define SP_PACKAGE_VERSION_MINOR 1
524b0e14
UH
1709
1710/** The libserialport package 'micro' version number. */
f6e32b2d 1711#define SP_PACKAGE_VERSION_MICRO 1
524b0e14
UH
1712
1713/** The libserialport package version ("major.minor.micro") as string. */
f6e32b2d 1714#define SP_PACKAGE_VERSION_STRING "0.1.1"
524b0e14
UH
1715
1716/*
1717 * Library/libtool version macros (can be used for conditional compilation).
1718 */
1719
1720/** The libserialport libtool 'current' version number. */
f6e32b2d 1721#define SP_LIB_VERSION_CURRENT 1
524b0e14
UH
1722
1723/** The libserialport libtool 'revision' version number. */
f6e32b2d 1724#define SP_LIB_VERSION_REVISION 0
524b0e14
UH
1725
1726/** The libserialport libtool 'age' version number. */
f6e32b2d 1727#define SP_LIB_VERSION_AGE 1
524b0e14
UH
1728
1729/** The libserialport libtool version ("current:revision:age") as string. */
f6e32b2d 1730#define SP_LIB_VERSION_STRING "1:0:1"
524b0e14
UH
1731
1732/**
1733 * Get the major libserialport package version number.
1734 *
1735 * @return The major package version number.
1736 *
1737 * @since 0.1.0
1738 */
0838c979 1739SP_API int sp_get_major_package_version(void);
524b0e14
UH
1740
1741/**
1742 * Get the minor libserialport package version number.
1743 *
1744 * @return The minor package version number.
1745 *
1746 * @since 0.1.0
1747 */
0838c979 1748SP_API int sp_get_minor_package_version(void);
524b0e14
UH
1749
1750/**
1751 * Get the micro libserialport package version number.
1752 *
1753 * @return The micro package version number.
1754 *
1755 * @since 0.1.0
1756 */
0838c979 1757SP_API int sp_get_micro_package_version(void);
524b0e14
UH
1758
1759/**
1760 * Get the libserialport package version number as a string.
1761 *
1762 * @return The package version number string. The returned string is
1763 * static and thus should NOT be free'd by the caller.
1764 *
1765 * @since 0.1.0
1766 */
0838c979 1767SP_API const char *sp_get_package_version_string(void);
524b0e14
UH
1768
1769/**
1770 * Get the "current" part of the libserialport library version number.
1771 *
1772 * @return The "current" library version number.
1773 *
1774 * @since 0.1.0
1775 */
0838c979 1776SP_API int sp_get_current_lib_version(void);
524b0e14
UH
1777
1778/**
1779 * Get the "revision" part of the libserialport library version number.
1780 *
1781 * @return The "revision" library version number.
1782 *
1783 * @since 0.1.0
1784 */
0838c979 1785SP_API int sp_get_revision_lib_version(void);
524b0e14
UH
1786
1787/**
1788 * Get the "age" part of the libserialport library version number.
1789 *
1790 * @return The "age" library version number.
1791 *
1792 * @since 0.1.0
1793 */
0838c979 1794SP_API int sp_get_age_lib_version(void);
524b0e14
UH
1795
1796/**
1797 * Get the libserialport library version number as a string.
1798 *
1799 * @return The library version number string. The returned string is
1800 * static and thus should NOT be free'd by the caller.
1801 *
1802 * @since 0.1.0
1803 */
0838c979 1804SP_API const char *sp_get_lib_version_string(void);
524b0e14
UH
1805
1806/** @} */
1807
7c8d67ef
ML
1808/**
1809 * @example list_ports.c Getting a list of ports present on the system.
1810 * @example port_info.c Getting information on a particular serial port.
9ddf0858 1811 * @example port_config.c Accessing configuration settings of a port.
47200531 1812 * @example handle_errors.c - Handling errors returned from the library.
7c8d67ef
ML
1813*/
1814
5ef8a1ed
UH
1815#ifdef __cplusplus
1816}
1817#endif
1818
8645feda 1819#endif