]> sigrok.org Git - libserialport.git/blame - libserialport.h.in
New API and implementation for blocking and non-blocking I/O.
[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 *
32 * The operations that are supported are:
33 *
34 * - @ref Enumeration (obtaining a list of serial ports on the system)
35 * - @ref Ports
36 * - @ref Configuration (baud rate, parity, etc.)
0151b157 37 * - @ref Signals (modem control lines, breaks, etc.)
cf9d365c
UH
38 * - @ref Data
39 * - @ref Errors
40 *
41 * libserialport is an open source project released under the LGPL3+ license.
42 *
43 * API principles
44 * ==============
45 *
46 * The API is simple, and designed to be a minimal wrapper around the serial
47 * port support in each OS.
48 *
49 * Most functions take a pointer to a struct sp_port, which represents a serial
50 * port. These structures are always allocated and freed by the library, using
51 * the functions in the @ref Enumeration "Enumeration" section.
52 *
0151b157
ML
53 * Most functions have return type @ref sp_return and can return only four
54 * possible error values:
cf9d365c 55 *
6aabf62a
ML
56 * - @ref SP_ERR_ARG means that a function was called with invalid
57 * arguments. This implies a bug in the caller. The arguments passed would
58 * be invalid regardless of the underlying OS or serial device involved.
59 *
60 * - @ref SP_ERR_FAIL means that the OS reported a failure. The error code or
61 * message provided by the OS can be obtained by calling sp_last_error_code()
62 * or sp_last_error_message().
63 *
64 * - @ref SP_ERR_SUPP indicates that there is no support for the requested
65 * operation in the current OS, driver or device. No error message is
66 * available from the OS in this case. There is either no way to request
67 * the operation in the first place, or libserialport does not know how to
68 * do so in the current version.
69 *
70 * - @ref SP_ERR_MEM indicates that a memory allocation failed.
71 *
72 * All of these error values are negative.
cf9d365c 73 *
0151b157
ML
74 * Calls that succeed return @ref SP_OK, which is equal to zero. Some functions
75 * declared @ref sp_return can also return a positive value for a successful
76 * numeric result, e.g. sp_read() and sp_write().
cf9d365c 77 */
cd5f5281 78
8645feda
UH
79#ifndef LIBSERIALPORT_LIBSERIALPORT_H
80#define LIBSERIALPORT_LIBSERIALPORT_H
e8ffaee9 81
5ef8a1ed
UH
82#ifdef __cplusplus
83extern "C" {
84#endif
85
74510d4b
ML
86#include <stddef.h>
87#ifdef _WIN32
88#include <windows.h>
89#endif
90
baba0759
UH
91/* Package version macros (e.g. for conditional compilation). */
92#define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
93#define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
7de20e39 94#define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
baba0759
UH
95#define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
96
97/* Library/libtool version macros (e.g. for conditional compilation). */
98#define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
99#define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
100#define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
101#define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
102
cd5f5281 103/** Return values. */
eb6ed20f 104enum sp_return {
cd5f5281 105 /** Operation completed successfully. */
74510d4b 106 SP_OK = 0,
cd5f5281 107 /** Invalid arguments were passed to the function. */
e9a2f9c9 108 SP_ERR_ARG = -1,
cd5f5281 109 /** A system error occured while executing the operation. */
e9a2f9c9 110 SP_ERR_FAIL = -2,
cd5f5281 111 /** A memory allocation failed while executing the operation. */
f92f1f0c 112 SP_ERR_MEM = -3,
6aabf62a
ML
113 /** The requested operation is not supported by this system or device. */
114 SP_ERR_SUPP = -4,
74510d4b
ML
115};
116
cd5f5281 117/** Port access modes. */
eb6ed20f 118enum sp_mode {
a036341b
ML
119 /** Open port for read access. */
120 SP_MODE_READ = 1,
121 /** Open port for write access. */
122 SP_MODE_WRITE = 2,
74510d4b
ML
123};
124
fd8fd11a
ML
125/** Buffer selection. */
126enum sp_buffer {
127 /** Input buffer. */
128 SP_BUF_INPUT = 1,
129 /** Output buffer. */
130 SP_BUF_OUTPUT = 2,
131 /** Both buffers. */
132 SP_BUF_BOTH = 3,
133};
134
cd5f5281 135/** Parity settings. */
eb6ed20f 136enum sp_parity {
c200f5c1 137 /** Special value to indicate setting should be left alone. */
eb6ed20f 138 SP_PARITY_INVALID = -1,
cd5f5281 139 /** No parity. */
74510d4b 140 SP_PARITY_NONE = 0,
cd5f5281 141 /** Odd parity. */
20e63a77
ML
142 SP_PARITY_ODD = 1,
143 /** Even parity. */
144 SP_PARITY_EVEN = 2,
e432ce60
ML
145 /** Mark parity. */
146 SP_PARITY_MARK = 3,
147 /** Space parity. */
148 SP_PARITY_SPACE = 4,
74510d4b
ML
149};
150
cd5f5281 151/** RTS pin behaviour. */
eb6ed20f 152enum sp_rts {
c200f5c1 153 /** Special value to indicate setting should be left alone. */
eb6ed20f 154 SP_RTS_INVALID = -1,
cd5f5281 155 /** RTS off. */
d514a26f 156 SP_RTS_OFF = 0,
cd5f5281 157 /** RTS on. */
d514a26f 158 SP_RTS_ON = 1,
cd5f5281 159 /** RTS used for flow control. */
cf9d365c 160 SP_RTS_FLOW_CONTROL = 2,
d514a26f
ML
161};
162
cd5f5281 163/** CTS pin behaviour. */
eb6ed20f 164enum sp_cts {
c200f5c1 165 /** Special value to indicate setting should be left alone. */
eb6ed20f 166 SP_CTS_INVALID = -1,
cd5f5281 167 /** CTS ignored. */
d514a26f 168 SP_CTS_IGNORE = 0,
cd5f5281 169 /** CTS used for flow control. */
cf9d365c 170 SP_CTS_FLOW_CONTROL = 1,
d514a26f
ML
171};
172
cd5f5281 173/** DTR pin behaviour. */
eb6ed20f 174enum sp_dtr {
c200f5c1 175 /** Special value to indicate setting should be left alone. */
eb6ed20f 176 SP_DTR_INVALID = -1,
cd5f5281 177 /** DTR off. */
d514a26f 178 SP_DTR_OFF = 0,
cd5f5281 179 /** DTR on. */
d514a26f 180 SP_DTR_ON = 1,
cd5f5281 181 /** DTR used for flow control. */
cf9d365c 182 SP_DTR_FLOW_CONTROL = 2,
d514a26f
ML
183};
184
cd5f5281 185/** DSR pin behaviour. */
eb6ed20f 186enum sp_dsr {
c200f5c1 187 /** Special value to indicate setting should be left alone. */
eb6ed20f 188 SP_DSR_INVALID = -1,
cd5f5281 189 /** DSR ignored. */
d514a26f 190 SP_DSR_IGNORE = 0,
cd5f5281 191 /** DSR used for flow control. */
cf9d365c 192 SP_DSR_FLOW_CONTROL = 1,
d514a26f
ML
193};
194
cd5f5281 195/** XON/XOFF flow control behaviour. */
eb6ed20f 196enum sp_xonxoff {
c200f5c1 197 /** Special value to indicate setting should be left alone. */
eb6ed20f 198 SP_XONXOFF_INVALID = -1,
cd5f5281 199 /** XON/XOFF disabled. */
d514a26f 200 SP_XONXOFF_DISABLED = 0,
cd5f5281 201 /** XON/XOFF enabled for input only. */
d514a26f 202 SP_XONXOFF_IN = 1,
cd5f5281 203 /** XON/XOFF enabled for output only. */
d514a26f 204 SP_XONXOFF_OUT = 2,
cd5f5281 205 /** XON/XOFF enabled for input and output. */
cf9d365c 206 SP_XONXOFF_INOUT = 3,
ac74fdaf
ML
207};
208
cd5f5281 209/** Standard flow control combinations. */
eb6ed20f 210enum sp_flowcontrol {
cd5f5281 211 /** No flow control. */
18fc2dd1 212 SP_FLOWCONTROL_NONE = 0,
cd5f5281 213 /** Software flow control using XON/XOFF characters. */
18fc2dd1 214 SP_FLOWCONTROL_XONXOFF = 1,
cd5f5281 215 /** Hardware flow control using RTS/CTS signals. */
18fc2dd1 216 SP_FLOWCONTROL_RTSCTS = 2,
cd5f5281 217 /** Hardware flow control using DTR/DSR signals. */
cf9d365c 218 SP_FLOWCONTROL_DTRDSR = 3,
18fc2dd1
ML
219};
220
8cf7c697
ML
221/** Input signals. */
222enum sp_signal {
223 /** Clear to send. */
224 SP_SIG_CTS = 1,
225 /** Data set ready. */
226 SP_SIG_DSR = 2,
227 /** Data carrier detect. */
228 SP_SIG_DCD = 4,
229 /** Ring indicator. */
230 SP_SIG_RI = 8,
231};
232
eb6ed20f 233/** A serial port. */
1c5aae9d 234struct sp_port;
eb6ed20f
ML
235
236/** Configuration for a serial port. */
9b1502ef 237struct sp_port_config;
eb6ed20f 238
091e75fe 239/**
626d280f 240@defgroup Enumeration Port enumeration
091e75fe
ML
241@{
242*/
243
cd5f5281 244/**
cf9d365c
UH
245 * Obtain a pointer to a new sp_port structure representing the named port.
246 *
247 * The user should allocate a variable of type "struct sp_port *" and pass a
248 * pointer to this to receive the result.
249 *
250 * The result should be freed after use by calling sp_free_port().
251 *
f36c6395
ML
252 * If any error is returned, the variable pointed to by port_ptr will be set
253 * to NULL. Otherwise, it will be set to point to the newly allocated port.
254 *
255 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 256 */
eb6ed20f 257enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
cd5f5281
ML
258
259/**
cf9d365c
UH
260 * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
261 */
e3b2f7a4 262void sp_free_port(struct sp_port *port);
cd5f5281
ML
263
264/**
cf9d365c
UH
265 * List the serial ports available on the system.
266 *
267 * The result obtained is an array of pointers to sp_port structures,
268 * terminated by a NULL. The user should allocate a variable of type
269 * "struct sp_port **" and pass a pointer to this to receive the result.
270 *
271 * The result should be freed after use by calling sp_free_port_list().
272 * If a port from the list is to be used after freeing the list, it must be
273 * copied first using sp_copy_port().
274 *
f36c6395
ML
275 * If any error is returned, the variable pointed to by list_ptr will be set
276 * to NULL. Otherwise, it will be set to point to the newly allocated array.
277 *
278 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 279 */
eb6ed20f 280enum sp_return sp_list_ports(struct sp_port ***list_ptr);
cd5f5281
ML
281
282/**
cf9d365c
UH
283 * Make a new copy of a sp_port structure.
284 *
285 * The user should allocate a variable of type "struct sp_port *" and pass a
286 * pointer to this to receive the result.
287 *
288 * The copy should be freed after use by calling sp_free_port().
289 *
f36c6395
ML
290 * If any error is returned, the variable pointed to by copy_ptr will be set
291 * to NULL. Otherwise, it will be set to point to the newly allocated copy.
292 *
293 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 294 */
eb6ed20f 295enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
cd5f5281
ML
296
297/**
cf9d365c
UH
298 * Free a port list obtained from sp_list_ports().
299 *
300 * This will also free all the sp_port structures referred to from the list;
301 * any that are to be retained must be copied first using sp_copy_port().
302 */
d54e9004 303void sp_free_port_list(struct sp_port **ports);
e96d8bd2 304
091e75fe 305/**
cf9d365c 306 * @}
0151b157 307 * @defgroup Ports Opening, closing and querying ports
cf9d365c
UH
308 * @{
309 */
091e75fe 310
cd5f5281 311/**
cf9d365c
UH
312 * Open the specified serial port.
313 *
314 * @param port Pointer to port structure.
315 * @param flags Flags to use when opening the serial port.
316 *
f36c6395 317 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 318 */
eb6ed20f 319enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
cd5f5281
ML
320
321/**
cf9d365c
UH
322 * Close the specified serial port.
323 *
f36c6395 324 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 325 */
eb6ed20f 326enum sp_return sp_close(struct sp_port *port);
e96d8bd2 327
0151b157
ML
328/**
329 * Get the name of a port.
330 *
331 * The name returned is whatever is normally used to refer to a port on the
332 * current operating system; e.g. for Windows it will usually be a "COMn"
333 * device name, and for Unix it will be a device path beginning with "/dev/".
334 *
335 * @param port Pointer to port structure.
336 *
337 * @return The port name, or NULL if an invalid port is passed. The name
338 * string is part of the port structure and may not be used after the
339 * port structure has been freed.
340 */
341char *sp_get_port_name(const struct sp_port *port);
342
343/**
344 * Get the operating system handle for a port.
345 *
346 * The type of the handle depends on the operating system. On Unix based
347 * systems, the handle is a file descriptor of type "int". On Windows, the
348 * handle is of type "HANDLE". The user should allocate a variable of the
349 * appropriate type and pass a pointer to this to receive the result.
350 *
351 * To obtain a valid handle, the port must first be opened by calling
352 * sp_open() using the same port structure.
353 *
354 * After the port is closed or the port structure freed, the handle may
355 * no longer be valid.
356 *
357 * @warning This feature is provided so that programs may make use of
358 * OS-specific functionality where desired. Doing so obviously
359 * comes at a cost in portability. It also cannot be guaranteed
360 * that direct usage of the OS handle will not conflict with the
361 * library's own usage of the port. Be careful.
362 *
363 * @return SP_OK upon success, a negative error code otherwise.
364 */
365enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
366
cd5f5281 367/**
cf9d365c
UH
368 * @}
369 * @defgroup Configuration Setting port parameters
370 * @{
371 */
e96d8bd2 372
9b1502ef
ML
373/**
374 * Allocate a port configuration structure.
375 *
376 * The user should allocate a variable of type "struct sp_config *" and pass a
377 * pointer to this to receive the result. The variable will be updated to
378 * point to the new configuration structure. The structure is opaque and must
379 * be accessed via the functions provided.
380 *
381 * All parameters in the structure will be initialised to special values which
382 * are ignored by sp_set_config().
383 *
384 * The structure should be freed after use by calling sp_free_config().
385 *
386 * @param config_ptr Pointer to variable to receive result.
387 *
388 * @return SP_OK upon success, a negative error code otherwise.
389 */
390enum sp_return sp_new_config(struct sp_port_config **config_ptr);
391
392/**
393 * Free a port configuration structure.
394 *
395 * @param config Pointer to configuration structure.
396 */
397void sp_free_config(struct sp_port_config *config);
398
cd5f5281 399/**
cf9d365c
UH
400 * Get the current configuration of the specified serial port.
401 *
9b1502ef
ML
402 * The user should allocate a configuration structure using sp_new_config()
403 * and pass this as the config parameter. The configuration structure will
404 * be updated with the port configuration.
cf9d365c 405 *
9b1502ef
ML
406 * Any parameters that are configured with settings not recognised or
407 * supported by libserialport, will be set to special values that are
408 * ignored by sp_set_config().
cf9d365c 409 *
f36c6395 410 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 411 */
eb6ed20f 412enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
cd5f5281
ML
413
414/**
cf9d365c
UH
415 * Set the configuration for the specified serial port.
416 *
9b1502ef
ML
417 * For each parameter in the configuration, there is a special value (usually
418 * -1, but see the documentation for each field). These values will be ignored
419 * and the corresponding setting left unchanged on the port.
cf9d365c 420 *
f36c6395 421 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 422 */
eb6ed20f 423enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
cd5f5281
ML
424
425/**
cf9d365c
UH
426 * Set the baud rate for the specified serial port.
427 *
428 * @param port Pointer to port structure.
429 * @param baudrate Baud rate in bits per second.
430 *
f36c6395 431 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 432 */
eb6ed20f 433enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
cd5f5281
ML
434
435/**
9b1502ef
ML
436 * Get the baud rate from a port configuration.
437 *
438 * The user should allocate a variable of type int and pass a pointer to this
439 * to receive the result.
440 *
441 * @param config Pointer to configuration structure.
442 * @param baudrate_ptr Pointer to variable to store result.
443 *
444 * @return SP_OK upon success, a negative error code otherwise.
445 */
446enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
447
448/**
449 * Set the baud rate in a port configuration.
450 *
451 * @param config Pointer to configuration structure.
452 * @param baudrate Baud rate in bits per second, or -1 to retain current setting.
453 *
454 * @return SP_OK upon success, a negative error code otherwise.
455 */
456enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
457
458/**
459 * Set the data bits for the specified serial port.
cf9d365c
UH
460 *
461 * @param port Pointer to port structure.
9b1502ef 462 * @param bits Number of data bits.
cf9d365c 463 *
f36c6395 464 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 465 */
eb6ed20f 466enum sp_return sp_set_bits(struct sp_port *port, int bits);
cd5f5281
ML
467
468/**
9b1502ef
ML
469 * Get the data bits from a port configuration.
470 *
471 * The user should allocate a variable of type int and pass a pointer to this
472 * to receive the result.
473 *
474 * @param config Pointer to configuration structure.
475 * @param bits_ptr Pointer to variable to store result.
476 *
477 * @return SP_OK upon success, a negative error code otherwise.
478 */
479enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
480
481/**
482 * Set the data bits in a port configuration.
483 *
484 * @param config Pointer to configuration structure.
485 * @param bits Number of data bits, or -1 to retain current setting.
486 *
487 * @return SP_OK upon success, a negative error code otherwise.
488 */
489enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
490
491/**
492 * Set the parity setting for the specified serial port.
cf9d365c
UH
493 *
494 * @param port Pointer to port structure.
9b1502ef 495 * @param parity Parity setting.
cf9d365c 496 *
f36c6395 497 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 498 */
eb6ed20f 499enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
cd5f5281
ML
500
501/**
9b1502ef
ML
502 * Get the parity setting from a port configuration.
503 *
504 * The user should allocate a variable of type enum sp_parity and pass a pointer to this
505 * to receive the result.
506 *
507 * @param config Pointer to configuration structure.
508 * @param parity_ptr Pointer to variable to store result.
509 *
510 * @return SP_OK upon success, a negative error code otherwise.
511 */
512enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
513
514/**
515 * Set the parity setting in a port configuration.
516 *
517 * @param config Pointer to configuration structure.
518 * @param parity Parity setting, or -1 to retain current setting.
519 *
520 * @return SP_OK upon success, a negative error code otherwise.
521 */
522enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
523
524/**
525 * Set the stop bits for the specified serial port.
cf9d365c
UH
526 *
527 * @param port Pointer to port structure.
9b1502ef 528 * @param stopbits Number of stop bits.
cf9d365c 529 *
f36c6395 530 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 531 */
eb6ed20f 532enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
cd5f5281
ML
533
534/**
9b1502ef 535 * Get the stop bits from a port configuration.
cf9d365c 536 *
9b1502ef
ML
537 * The user should allocate a variable of type int and pass a pointer to this
538 * to receive the result.
cf9d365c 539 *
9b1502ef
ML
540 * @param config Pointer to configuration structure.
541 * @param stopbits_ptr Pointer to variable to store result.
cf9d365c 542 *
f36c6395 543 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 544 */
9b1502ef
ML
545enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
546
547/**
548 * Set the stop bits in a port configuration.
549 *
550 * @param config Pointer to configuration structure.
551 * @param stopbits Number of stop bits, or -1 to retain current setting.
552 *
553 * @return SP_OK upon success, a negative error code otherwise.
554 */
555enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
18fc2dd1 556
cd5f5281 557/**
9b1502ef 558 * Set the RTS pin behaviour for the specified serial port.
cf9d365c
UH
559 *
560 * @param port Pointer to port structure.
561 * @param rts RTS pin mode.
562 *
f36c6395 563 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 564 */
eb6ed20f 565enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
cd5f5281
ML
566
567/**
9b1502ef
ML
568 * Get the RTS pin behaviour from a port configuration.
569 *
570 * The user should allocate a variable of type enum sp_rts and pass a pointer to this
571 * to receive the result.
572 *
573 * @param config Pointer to configuration structure.
574 * @param rts_ptr Pointer to variable to store result.
575 *
576 * @return SP_OK upon success, a negative error code otherwise.
577 */
578enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
579
580/**
581 * Set the RTS pin behaviour in a port configuration.
582 *
583 * @param config Pointer to configuration structure.
584 * @param rts RTS pin mode, or -1 to retain current setting.
585 *
586 * @return SP_OK upon success, a negative error code otherwise.
587 */
588enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
589
590/**
591 * Set the CTS pin behaviour for the specified serial port.
cf9d365c
UH
592 *
593 * @param port Pointer to port structure.
594 * @param cts CTS pin mode.
595 *
f36c6395 596 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 597 */
eb6ed20f 598enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
cd5f5281
ML
599
600/**
9b1502ef
ML
601 * Get the CTS pin behaviour from a port configuration.
602 *
603 * The user should allocate a variable of type enum sp_cts and pass a pointer to this
604 * to receive the result.
605 *
606 * @param config Pointer to configuration structure.
607 * @param cts_ptr Pointer to variable to store result.
608 *
609 * @return SP_OK upon success, a negative error code otherwise.
610 */
611enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
612
613/**
614 * Set the CTS pin behaviour in a port configuration.
615 *
616 * @param config Pointer to configuration structure.
617 * @param cts CTS pin mode, or -1 to retain current setting.
618 *
619 * @return SP_OK upon success, a negative error code otherwise.
620 */
621enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
622
623/**
624 * Set the DTR pin behaviour for the specified serial port.
cf9d365c
UH
625 *
626 * @param port Pointer to port structure.
627 * @param dtr DTR pin mode.
628 *
f36c6395 629 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 630 */
eb6ed20f 631enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
cd5f5281
ML
632
633/**
9b1502ef
ML
634 * Get the DTR pin behaviour from a port configuration.
635 *
636 * The user should allocate a variable of type enum sp_dtr and pass a pointer to this
637 * to receive the result.
638 *
639 * @param config Pointer to configuration structure.
640 * @param dtr_ptr Pointer to variable to store result.
641 *
642 * @return SP_OK upon success, a negative error code otherwise.
643 */
644enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
645
646/**
647 * Set the DTR pin behaviour in a port configuration.
648 *
649 * @param config Pointer to configuration structure.
650 * @param dtr DTR pin mode, or -1 to retain current setting.
651 *
652 * @return SP_OK upon success, a negative error code otherwise.
653 */
654enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
655
656/**
657 * Set the DSR pin behaviour for the specified serial port.
cf9d365c
UH
658 *
659 * @param port Pointer to port structure.
660 * @param dsr DSR pin mode.
661 *
f36c6395 662 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 663 */
eb6ed20f 664enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
cd5f5281
ML
665
666/**
9b1502ef
ML
667 * Get the DSR pin behaviour from a port configuration.
668 *
669 * The user should allocate a variable of type enum sp_dsr and pass a pointer to this
670 * to receive the result.
671 *
672 * @param config Pointer to configuration structure.
673 * @param dsr_ptr Pointer to variable to store result.
674 *
675 * @return SP_OK upon success, a negative error code otherwise.
676 */
677enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
678
679/**
680 * Set the DSR pin behaviour in a port configuration.
681 *
682 * @param config Pointer to configuration structure.
683 * @param dsr DSR pin mode, or -1 to retain current setting.
684 *
685 * @return SP_OK upon success, a negative error code otherwise.
686 */
687enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
688
689/**
690 * Set the XON/XOFF configuration for the specified serial port.
cf9d365c
UH
691 *
692 * @param port Pointer to port structure.
9b1502ef 693 * @param xon_xoff XON/XOFF mode.
cf9d365c 694 *
f36c6395 695 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 696 */
eb6ed20f 697enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
e96d8bd2 698
9b1502ef
ML
699/**
700 * Get the XON/XOFF configuration from a port configuration.
701 *
702 * The user should allocate a variable of type enum sp_xonxoff and pass a pointer to this
703 * to receive the result.
704 *
705 * @param config Pointer to configuration structure.
706 * @param xon_xoff_ptr Pointer to variable to store result.
707 *
708 * @return SP_OK upon success, a negative error code otherwise.
709 */
710enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
711
712/**
713 * Set the XON/XOFF configuration in a port configuration.
714 *
715 * @param config Pointer to configuration structure.
716 * @param xon_xoff XON/XOFF mode, or -1 to retain current setting.
717 *
718 * @return SP_OK upon success, a negative error code otherwise.
719 */
720enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
721
722/**
723 * Set the flow control type in a port configuration.
724 *
725 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
726 * XON/XOFF settings as necessary for the specified flow control
727 * type. For more fine-grained control of these settings, use their
728 * individual configuration functions.
729 *
730 * @param config Pointer to configuration structure.
731 * @param flowcontrol Flow control setting to use.
732 *
733 * @return SP_OK upon success, a negative error code otherwise.
734 */
735enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
736
737/**
738 * Set the flow control type for the specified serial port.
739 *
740 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
741 * XON/XOFF settings as necessary for the specified flow control
742 * type. For more fine-grained control of these settings, use their
743 * individual configuration functions.
744 *
745 * @param port Pointer to port structure.
746 * @param flowcontrol Flow control setting to use.
747 *
748 * @return SP_OK upon success, a negative error code otherwise.
749 */
750enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
751
091e75fe 752/**
cf9d365c
UH
753 * @}
754 * @defgroup Data Reading, writing, and flushing data
755 * @{
091e75fe
ML
756*/
757
758/**
e3dcf906 759 * Read bytes from the specified serial port, blocking until available.
cf9d365c 760 *
e3dcf906
ML
761 * @param port Pointer to port structure.
762 * @param buf Buffer in which to store the bytes read.
763 * @param count Requested number of bytes to read.
764 * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
765 *
766 * @return The number of bytes read on success, or a negative error code. If
767 * the number of bytes returned is less than that requested, the
768 * timeout was reached before the requested number of bytes was
769 * available. If timeout is zero, the function will always return
770 * either the requested number of bytes or a negative error code.
771 */
772enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout);
773
774/**
775 * Read bytes from the specified serial port, without blocking.
cf9d365c
UH
776 *
777 * @param port Pointer to port structure.
778 * @param buf Buffer in which to store the bytes read.
779 * @param count Maximum number of bytes to read.
780 *
e3dcf906
ML
781 * @return The number of bytes read on success, or a negative error code. The
782 * number of bytes returned may be any number from zero to the maximum
783 * that was requested.
784 */
785enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
786
787/**
788 * Write bytes to the specified serial port, blocking until complete.
789 *
790 * Note that this function only ensures that the accepted bytes have been
791 * written to the OS; they may be held in driver or hardware buffers and not
792 * yet physically transmitted. To check whether all written bytes have actually
793 * been transmitted, use the sp_output_waiting() function. To wait until all
794 * written bytes have actually been transmitted, use the sp_drain() function.
795 *
796 * @param port Pointer to port structure.
797 * @param buf Buffer containing the bytes to write.
798 * @param count Requested number of bytes to write.
799 * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
800 *
801 * @return The number of bytes read on success, or a negative error code. If
802 * the number of bytes returned is less than that requested, the
803 * timeout was reached before the requested number of bytes was
804 * sent. If timeout is zero, the function will always return
805 * either the requested number of bytes or a negative error code. In
806 * the event of an error there is no way to determine how many bytes
807 * were sent before the error occured.
cf9d365c 808 */
e3dcf906 809enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout);
091e75fe
ML
810
811/**
e3dcf906 812 * Write bytes to the specified serial port, without blocking.
cf9d365c 813 *
e3dcf906
ML
814 * Note that this function only ensures that the accepted bytes have been
815 * written to the OS; they may be held in driver or hardware buffers and not
816 * yet physically transmitted. To check whether all written bytes have actually
817 * been transmitted, use the sp_output_waiting() function. To wait until all
818 * written bytes have actually been transmitted, use the sp_drain() function.
cf9d365c
UH
819 *
820 * @param port Pointer to port structure.
821 * @param buf Buffer containing the bytes to write.
822 * @param count Maximum number of bytes to write.
823 *
f36c6395 824 * @return The number of bytes written on success, or a negative error code.
e3dcf906
ML
825 * The number of bytes returned may be any number from zero to the
826 * maximum that was requested.
cf9d365c 827 */
e3dcf906 828enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
091e75fe
ML
829
830/**
fd8fd11a
ML
831 * Flush serial port buffers. Data in the selected buffer(s) is discarded.
832 *
ea34fba8 833 * @param port Pointer to port structure.
fd8fd11a 834 * @param buffers Which buffer(s) to flush.
cf9d365c 835 *
f36c6395 836 * @return SP_OK upon success, a negative error code otherwise.
cf9d365c 837 */
fd8fd11a 838enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
091e75fe 839
69a3739c
ML
840/**
841 * Wait for buffered data to be transmitted.
842 *
3f099f4f
ML
843 * @param port Pointer to port structure.
844 *
f36c6395 845 * @return SP_OK upon success, a negative error code otherwise.
69a3739c
ML
846 */
847enum sp_return sp_drain(struct sp_port *port);
848
90cc3ee6
ML
849/**
850 * @}
0151b157 851 * @defgroup Signals Port signalling operations
90cc3ee6
ML
852 * @{
853 */
854
8cf7c697
ML
855/**
856 * Gets the status of the control signals for the specified port.
857 *
858 * The user should allocate a variable of type "enum sp_signal" and pass a
859 * pointer to this variable to receive the result. The result is a bitmask
860 * in which individual signals can be checked by bitwise OR with values of
861 * the sp_signal enum.
862 *
863 * @param port Pointer to port structure.
864 * @param signals Pointer to variable to receive result.
865 *
f36c6395 866 * @return SP_OK upon success, a negative error code otherwise.
8cf7c697
ML
867 */
868enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals);
869
90cc3ee6
ML
870/**
871 * Put the port transmit line into the break state.
872 *
3f099f4f
ML
873 * @param port Pointer to port structure.
874 *
f36c6395 875 * @return SP_OK upon success, a negative error code otherwise.
90cc3ee6
ML
876 */
877enum sp_return sp_start_break(struct sp_port *port);
878
879/**
880 * Take the port transmit line out of the break state.
881 *
3f099f4f
ML
882 * @param port Pointer to port structure.
883 *
f36c6395 884 * @return SP_OK upon success, a negative error code otherwise.
90cc3ee6
ML
885 */
886enum sp_return sp_end_break(struct sp_port *port);
887
091e75fe 888/**
cf9d365c
UH
889 * @}
890 * @defgroup Errors Obtaining error information
891 * @{
091e75fe
ML
892*/
893
cd5f5281 894/**
cf9d365c
UH
895 * Get the error code for a failed operation.
896 *
897 * In order to obtain the correct result, this function should be called
898 * straight after the failure, before executing any other system operations.
899 *
900 * @return The system's numeric code for the error that caused the last
901 * operation to fail.
902 */
74510d4b 903int sp_last_error_code(void);
cd5f5281
ML
904
905/**
cf9d365c
UH
906 * Get the error message for a failed operation.
907 *
908 * In order to obtain the correct result, this function should be called
909 * straight after the failure, before executing other system operations.
910 *
911 * @return The system's message for the error that caused the last
912 * operation to fail. This string may be allocated by the function,
913 * and should be freed after use by calling sp_free_error_message().
914 */
74510d4b 915char *sp_last_error_message(void);
cd5f5281
ML
916
917/**
cf9d365c
UH
918 * Free an error message returned by sp_last_error_message().
919 */
74510d4b 920void sp_free_error_message(char *message);
e8ffaee9 921
863b35e6
ML
922/**
923 * Set the handler function for library debugging messages.
924 *
925 * Debugging messages are generated by the library during each operation,
926 * to help in diagnosing problems. The handler will be called for each
927 * message. The handler can be set to NULL to ignore all debug messages.
928 *
929 * The handler function should accept a format string and variable length
930 * argument list, in the same manner as e.g. printf().
931 *
932 * The default handler is sp_default_debug_handler().
933 */
934void sp_set_debug_handler(void (*handler)(const char *format, ...));
935
936/**
937 * Default handler function for library debugging messages.
938 *
939 * This function prints debug messages to the standard error stream if the
940 * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
941 * ignored.
942 */
943void sp_default_debug_handler(const char *format, ...);
944
cf9d365c 945/** @} */
091e75fe 946
5ef8a1ed
UH
947#ifdef __cplusplus
948}
949#endif
950
8645feda 951#endif