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