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