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