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