2 * This file is part of the libserialport project.
4 * Copyright (C) 2013 Martin Ling <martin-libserialport@earth.li>
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.
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.
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/>.
21 * @mainpage libserialport API
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.
29 * By writing your serial code to use libserialport, you enable it to work
30 * transparently on any platform supported by the library.
32 * libserialport is an open source project released under the LGPL3+ license.
34 * The library is maintained by the [sigrok](http://sigrok.org/) project. See
35 * the [libserialport homepage](http://sigrok.org/wiki/Libserialport) for the
38 * Source code is maintained in git at
39 * [git://sigrok.org/libserialport](http://sigrok.org/gitweb/?p=libserialport.git).
41 * Bugs are tracked at http://sigrok.org/bugzilla/.
43 * The library was conceived and designed by Martin Ling, is maintained by
44 * Uwe Hermann, and has received contributions from several other developers.
45 * See the git history for full credits.
50 * The API has been designed from scratch. It does not exactly resemble the
51 * serial API of any particular operating system. Instead it aims to provide
52 * a set of functions that can reliably be implemented across all operating
53 * systems. These form a sufficient basis for higher level behaviour to
54 * be implemented in a platform independent manner.
56 * If you are porting code written for a particular OS, you may find you need
57 * to restructure things somewhat, or do without some specialised features.
58 * For particular notes on porting existing code, see @ref Porting.
60 * The following subsections will help explain the principles of the API.
61 * To jump directly into the detailed function documentation, see the
62 * <a href="modules.html">categorised function lists</a>.
67 * The library defines three data structures:
69 * - @ref sp_port, which represents a serial port.
70 * See @ref Enumeration.
71 * - @ref sp_port_config, which represents a port configuration.
72 * See @ref Configuration.
73 * - @ref sp_event_set, which represents a set of events.
76 * All these structures are allocated and freed by library functions. It is
77 * the caller's responsibility to ensure that the correct calls are made to
78 * free allocated structures after use.
80 * Return codes and error handling
81 * -------------------------------
83 * Most functions have return type @ref sp_return and can return only four
84 * possible error values:
86 * - @ref SP_ERR_ARG means that a function was called with invalid
87 * arguments. This implies a bug in the caller. The arguments passed would
88 * be invalid regardless of the underlying OS or serial device involved.
90 * - @ref SP_ERR_FAIL means that the OS reported a failure. The error code or
91 * message provided by the OS can be obtained by calling sp_last_error_code()
92 * or sp_last_error_message().
94 * - @ref SP_ERR_SUPP indicates that there is no support for the requested
95 * operation in the current OS, driver or device. No error message is
96 * available from the OS in this case. There is either no way to request
97 * the operation in the first place, or libserialport does not know how to
98 * do so in the current version.
100 * - @ref SP_ERR_MEM indicates that a memory allocation failed.
102 * All of these error values are negative.
104 * Calls that succeed return @ref SP_OK, which is equal to zero. Some functions
105 * declared @ref sp_return can also return a positive value for a successful
106 * numeric result, e.g. sp_blocking_read() or sp_blocking_write().
108 * An error message is only available via sp_last_error_message() in the case
109 * where SP_ERR_FAIL was returned by the previous function call. The error
110 * message returned is that provided by the OS, using the current language
111 * settings. It is an error to call sp_last_error_code() or
112 * sp_last_error_message() except after a previous function call returned
113 * SP_ERR_FAIL. The library does not define its own error codes or messages
114 * to accompany other return codes.
119 * Certain combinations of calls can be made concurrently, as follows.
121 * - Calls using different ports may always be made concurrently, i.e.
122 * it is safe for separate threads to handle their own ports.
124 * - Calls using the same port may be made concurrently when one call
125 * is a read operation and one call is a write operation, i.e. it is safe
126 * to use separate "reader" and "writer" threads for the same port. See
127 * below for which operations meet these definitions.
131 * - sp_blocking_read()
132 * - sp_blocking_read_next()
133 * - sp_nonblocking_read()
134 * - sp_input_waiting()
135 * - sp_flush() with @ref SP_BUF_INPUT only.
136 * - sp_wait() with @ref SP_EVENT_RX_READY only.
140 * - sp_blocking_write()
141 * - sp_nonblocking_write()
142 * - sp_output_waiting()
144 * - sp_flush() with @ref SP_BUF_OUTPUT only.
145 * - sp_wait() with @ref SP_EVENT_TX_READY only.
147 * If two calls, on the same port, do not fit into one of these categories
148 * each, then they may not be made concurrently.
153 * The library can output extensive tracing and debugging information. The
154 * simplest way to use this is to set the environment variable
155 * LIBSERIALPORT_DEBUG to any value; messages will then be output to the
156 * standard error stream.
158 * This behaviour is implemented by a default debug message handling
159 * callback. An alternative callback can be set using sp_set_debug_handler(),
160 * in order to e.g. redirect the output elsewhere or filter it.
162 * No guarantees are made about the content of the debug output; it is chosen
163 * to suit the needs of the developers and may change between releases.
169 * The following guidelines may help when porting existing OS-specific code
170 * to use libserialport.
172 * ### Porting from Unix-like systems ###
174 * There are two main differences to note when porting code written for Unix.
176 * The first is that Unix traditionally provides a wide range of functionality
177 * for dealing with serial devices at the OS level; this is exposed through the
178 * termios API and dates to the days when serial terminals were common. If your
179 * code relies on many of these facilities you will need to adapt it, because
180 * libserialport provides only a raw 8-bit channel with no special handling.
182 * The second relates to blocking versus non-blocking I/O behaviour. In
183 * Unix-like systems this is normally specified by setting the O_NONBLOCK
184 * flag on the file descriptor, affecting the semantics of subsequent read()
187 * In libserialport, blocking and nonblocking operations are both available at
188 * any time. If your existing code Ń•ets O_NONBLOCK, you should use
189 * sp_nonblocking_read() and sp_nonblocking_write() to get the same behaviour
190 * as your existing read() and write() calls. If it does not, you should use
191 * sp_blocking_read() and sp_blocking_write() instead. You may also find
192 * sp_blocking_read_next() useful, which reproduces the semantics of a blocking
193 * read() with VTIME = 0 and VMIN = 1 set in termios.
195 * Finally, you should take care if your program uses custom signal handlers.
196 * The blocking calls provided by libserialport will restart system calls that
197 * return with EINTR, so you will need to make your own arrangements if you
198 * need to interrupt blocking operations when your signal handlers are called.
199 * This is not an issue if you only use the default handlers.
201 * ### Porting from Windows ###
203 * The main consideration when porting from Windows is that there is no
204 * direct equivalent for overlapped I/O operations.
206 * If your program does not use overlapped I/O, you can simply use
207 * sp_blocking_read() and sp_blocking_write() as direct equivalents for
208 * ReadFile() and WriteFile(). You may also find sp_blocking_read_next()
209 * useful, which reproduces the special semantics of ReadFile() with
210 * ReadIntervalTimeout and ReadTotalTimeoutMultiplier set to MAXDWORD
211 * and 0 < ReadTotalTimeoutConstant < MAXDWORD.
213 * If your program makes use of overlapped I/O to continue work while a serial
214 * operation is in progress, then you can achieve the same results using
215 * sp_nonblocking_read() and sp_nonblocking_write().
217 * Generally, overlapped I/O is combined with either waiting for completion
218 * once there is no more background work to do (using WaitForSingleObject() or
219 * WaitForMultipleObjects()), or periodically checking for completion with
220 * GetOverlappedResult(). If the aim is to start a new operation for further
221 * data once the previous one has completed, you can instead simply call the
222 * nonblocking functions again with the next data. If you need to wait for
223 * completion, use sp_wait() to determine when the port is ready to send or
224 * receive further data.
227 #ifndef LIBSERIALPORT_LIBSERIALPORT_H
228 #define LIBSERIALPORT_LIBSERIALPORT_H
236 /** Return values. */
238 /** Operation completed successfully. */
240 /** Invalid arguments were passed to the function. */
242 /** A system error occurred while executing the operation. */
244 /** A memory allocation failed while executing the operation. */
246 /** The requested operation is not supported by this system or device. */
250 /** Port access modes. */
252 /** Open port for read access. */
254 /** Open port for write access. */
256 /** Open port for read and write access. @since 0.1.1 */
257 SP_MODE_READ_WRITE = 3
262 /** Data received and ready to read. */
263 SP_EVENT_RX_READY = 1,
264 /** Ready to transmit new data. */
265 SP_EVENT_TX_READY = 2,
266 /** Error occurred. */
270 /** Buffer selection. */
274 /** Output buffer. */
280 /** Parity settings. */
282 /** Special value to indicate setting should be left alone. */
283 SP_PARITY_INVALID = -1,
296 /** RTS pin behaviour. */
298 /** Special value to indicate setting should be left alone. */
304 /** RTS used for flow control. */
305 SP_RTS_FLOW_CONTROL = 2
308 /** CTS pin behaviour. */
310 /** Special value to indicate setting should be left alone. */
314 /** CTS used for flow control. */
315 SP_CTS_FLOW_CONTROL = 1
318 /** DTR pin behaviour. */
320 /** Special value to indicate setting should be left alone. */
326 /** DTR used for flow control. */
327 SP_DTR_FLOW_CONTROL = 2
330 /** DSR pin behaviour. */
332 /** Special value to indicate setting should be left alone. */
336 /** DSR used for flow control. */
337 SP_DSR_FLOW_CONTROL = 1
340 /** XON/XOFF flow control behaviour. */
342 /** Special value to indicate setting should be left alone. */
343 SP_XONXOFF_INVALID = -1,
344 /** XON/XOFF disabled. */
345 SP_XONXOFF_DISABLED = 0,
346 /** XON/XOFF enabled for input only. */
348 /** XON/XOFF enabled for output only. */
350 /** XON/XOFF enabled for input and output. */
354 /** Standard flow control combinations. */
355 enum sp_flowcontrol {
356 /** No flow control. */
357 SP_FLOWCONTROL_NONE = 0,
358 /** Software flow control using XON/XOFF characters. */
359 SP_FLOWCONTROL_XONXOFF = 1,
360 /** Hardware flow control using RTS/CTS signals. */
361 SP_FLOWCONTROL_RTSCTS = 2,
362 /** Hardware flow control using DTR/DSR signals. */
363 SP_FLOWCONTROL_DTRDSR = 3
366 /** Input signals. */
368 /** Clear to send. */
370 /** Data set ready. */
372 /** Data carrier detect. */
374 /** Ring indicator. */
384 /** Native platform serial port. @since 0.1.1 */
386 /** USB serial port adapter. @since 0.1.1 */
388 /** Bluetooth serial port adapter. @since 0.1.1 */
389 SP_TRANSPORT_BLUETOOTH
394 * An opaque structure representing a serial port.
399 * @struct sp_port_config
400 * An opaque structure representing the configuration for a serial port.
402 struct sp_port_config;
405 * @struct sp_event_set
406 * A set of handles to wait on for events.
408 struct sp_event_set {
409 /** Array of OS-specific handles. */
411 /** Array of bitmasks indicating which events apply for each handle. */
412 enum sp_event *masks;
413 /** Number of handles. */
418 * @defgroup Enumeration Port enumeration
420 * Enumerating the serial ports of a system.
426 * Obtain a pointer to a new sp_port structure representing the named port.
428 * The user should allocate a variable of type "struct sp_port *" and pass a
429 * pointer to this to receive the result.
431 * The result should be freed after use by calling sp_free_port().
433 * @param[in] portname The OS-specific name of a serial port. Must not be NULL.
434 * @param[out] port_ptr If any error is returned, the variable pointed to by
435 * port_ptr will be set to NULL. Otherwise, it will be set
436 * to point to the newly allocated port. Must not be NULL.
438 * @return SP_OK upon success, a negative error code otherwise.
442 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
445 * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
447 * @param[in] port Pointer to a port structure. Must not be NULL.
451 void sp_free_port(struct sp_port *port);
454 * List the serial ports available on the system.
456 * The result obtained is an array of pointers to sp_port structures,
457 * terminated by a NULL. The user should allocate a variable of type
458 * "struct sp_port **" and pass a pointer to this to receive the result.
460 * The result should be freed after use by calling sp_free_port_list().
461 * If a port from the list is to be used after freeing the list, it must be
462 * copied first using sp_copy_port().
464 * @param[out] list_ptr If any error is returned, the variable pointed to by
465 * list_ptr will be set to NULL. Otherwise, it will be set
466 * to point to the newly allocated array. Must not be NULL.
468 * @return SP_OK upon success, a negative error code otherwise.
472 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
475 * Make a new copy of an sp_port structure.
477 * The user should allocate a variable of type "struct sp_port *" and pass a
478 * pointer to this to receive the result.
480 * The copy should be freed after use by calling sp_free_port().
482 * @param[in] port Pointer to a port structure. Must not be NULL.
483 * @param[out] copy_ptr If any error is returned, the variable pointed to by
484 * copy_ptr will be set to NULL. Otherwise, it will be set
485 * to point to the newly allocated copy. Must not be NULL.
487 * @return SP_OK upon success, a negative error code otherwise.
491 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
494 * Free a port list obtained from sp_list_ports().
496 * This will also free all the sp_port structures referred to from the list;
497 * any that are to be retained must be copied first using sp_copy_port().
499 * @param[in] ports Pointer to a list of port structures. Must not be NULL.
503 void sp_free_port_list(struct sp_port **ports);
507 * @defgroup Ports Port handling
509 * Opening, closing and querying ports.
515 * Open the specified serial port.
517 * @param[in] port Pointer to a port structure. Must not be NULL.
518 * @param[in] flags Flags to use when opening the serial port.
520 * @return SP_OK upon success, a negative error code otherwise.
524 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
527 * Close the specified serial port.
529 * @param[in] port Pointer to a port structure. Must not be NULL.
531 * @return SP_OK upon success, a negative error code otherwise.
535 enum sp_return sp_close(struct sp_port *port);
538 * Get the name of a port.
540 * The name returned is whatever is normally used to refer to a port on the
541 * current operating system; e.g. for Windows it will usually be a "COMn"
542 * device name, and for Unix it will be a device path beginning with "/dev/".
544 * @param[in] port Pointer to a port structure. Must not be NULL.
546 * @return The port name, or NULL if an invalid port is passed. The name
547 * string is part of the port structure and may not be used after
548 * the port structure has been freed.
552 char *sp_get_port_name(const struct sp_port *port);
555 * Get a description for a port, to present to end user.
557 * @param[in] port Pointer to a port structure. Must not be NULL.
559 * @return The port description, or NULL if an invalid port is passed.
560 * The description string is part of the port structure and may not
561 * be used after the port structure has been freed.
565 char *sp_get_port_description(const struct sp_port *port);
568 * Get the transport type used by a port.
570 * @param[in] port Pointer to a port structure. Must not be NULL.
572 * @return The port transport type.
576 enum sp_transport sp_get_port_transport(const struct sp_port *port);
579 * Get the USB bus number and address on bus of a USB serial adapter port.
581 * @param[in] port Pointer to a port structure. Must not be NULL.
582 * @param[out] usb_bus Pointer to a variable to store the USB bus.
583 * Can be NULL (in that case it will be ignored).
584 * @param[out] usb_address Pointer to a variable to store the USB address.
585 * Can be NULL (in that case it will be ignored).
587 * @return SP_OK upon success, a negative error code otherwise.
591 enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port,
592 int *usb_bus, int *usb_address);
595 * Get the USB Vendor ID and Product ID of a USB serial adapter port.
597 * @param[in] port Pointer to a port structure. Must not be NULL.
598 * @param[out] usb_vid Pointer to a variable to store the USB VID.
599 * Can be NULL (in that case it will be ignored).
600 * @param[out] usb_pid Pointer to a variable to store the USB PID.
601 * Can be NULL (in that case it will be ignored).
603 * @return SP_OK upon success, a negative error code otherwise.
607 enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, int *usb_vid, int *usb_pid);
610 * Get the USB manufacturer string of a USB serial adapter port.
612 * @param[in] port Pointer to a port structure. Must not be NULL.
614 * @return The port manufacturer string, or NULL if an invalid port is passed.
615 * The manufacturer string is part of the port structure and may not
616 * be used after the port structure has been freed.
620 char *sp_get_port_usb_manufacturer(const struct sp_port *port);
623 * Get the USB product string of a USB serial adapter port.
625 * @param[in] port Pointer to a port structure. Must not be NULL.
627 * @return The port product string, or NULL if an invalid port is passed.
628 * The product string is part of the port structure and may not be
629 * used after the port structure has been freed.
633 char *sp_get_port_usb_product(const struct sp_port *port);
636 * Get the USB serial number string of a USB serial adapter port.
638 * @param[in] port Pointer to a port structure. Must not be NULL.
640 * @return The port serial number, or NULL if an invalid port is passed.
641 * The serial number string is part of the port structure and may
642 * not be used after the port structure has been freed.
646 char *sp_get_port_usb_serial(const struct sp_port *port);
649 * Get the MAC address of a Bluetooth serial adapter port.
651 * @param[in] port Pointer to a port structure. Must not be NULL.
653 * @return The port MAC address, or NULL if an invalid port is passed.
654 * The MAC address string is part of the port structure and may not
655 * be used after the port structure has been freed.
659 char *sp_get_port_bluetooth_address(const struct sp_port *port);
662 * Get the operating system handle for a port.
664 * The type of the handle depends on the operating system. On Unix based
665 * systems, the handle is a file descriptor of type "int". On Windows, the
666 * handle is of type "HANDLE". The user should allocate a variable of the
667 * appropriate type and pass a pointer to this to receive the result.
669 * To obtain a valid handle, the port must first be opened by calling
670 * sp_open() using the same port structure.
672 * After the port is closed or the port structure freed, the handle may
673 * no longer be valid.
675 * @warning This feature is provided so that programs may make use of
676 * OS-specific functionality where desired. Doing so obviously
677 * comes at a cost in portability. It also cannot be guaranteed
678 * that direct usage of the OS handle will not conflict with the
679 * library's own usage of the port. Be careful.
681 * @param[in] port Pointer to a port structure. Must not be NULL.
682 * @param[out] result_ptr If any error is returned, the variable pointed to by
683 * result_ptr will have unknown contents and should not
684 * be used. Otherwise, it will be set to point to the
685 * OS handle. Must not be NULL.
687 * @return SP_OK upon success, a negative error code otherwise.
691 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
696 * @defgroup Configuration Configuration
698 * Setting and querying serial port parameters.
703 * Allocate a port configuration structure.
705 * The user should allocate a variable of type "struct sp_port_config *" and
706 * pass a pointer to this to receive the result. The variable will be updated
707 * to point to the new configuration structure. The structure is opaque and
708 * must be accessed via the functions provided.
710 * All parameters in the structure will be initialised to special values which
711 * are ignored by sp_set_config().
713 * The structure should be freed after use by calling sp_free_config().
715 * @param[out] config_ptr If any error is returned, the variable pointed to by
716 * config_ptr will be set to NULL. Otherwise, it will
717 * be set to point to the allocated config structure.
720 * @return SP_OK upon success, a negative error code otherwise.
724 enum sp_return sp_new_config(struct sp_port_config **config_ptr);
727 * Free a port configuration structure.
729 * @param[in] config Pointer to a configuration structure. Must not be NULL.
733 void sp_free_config(struct sp_port_config *config);
736 * Get the current configuration of the specified serial port.
738 * The user should allocate a configuration structure using sp_new_config()
739 * and pass this as the config parameter. The configuration structure will
740 * be updated with the port configuration.
742 * Any parameters that are configured with settings not recognised or
743 * supported by libserialport, will be set to special values that are
744 * ignored by sp_set_config().
746 * @param[in] port Pointer to a port structure. Must not be NULL.
747 * @param[out] config Pointer to a configuration structure that will hold
748 * the result. Upon errors the contents of the config
749 * struct will not be changed. Must not be NULL.
751 * @return SP_OK upon success, a negative error code otherwise.
755 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
758 * Set the configuration for the specified serial port.
760 * For each parameter in the configuration, there is a special value (usually
761 * -1, but see the documentation for each field). These values will be ignored
762 * and the corresponding setting left unchanged on the port.
764 * Upon errors, the configuration of the serial port is unknown since
765 * partial/incomplete config updates may have happened.
767 * @param[in] port Pointer to a port structure. Must not be NULL.
768 * @param[in] config Pointer to a configuration structure. Must not be NULL.
770 * @return SP_OK upon success, a negative error code otherwise.
774 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
777 * Set the baud rate for the specified serial port.
779 * @param[in] port Pointer to a port structure. Must not be NULL.
780 * @param[in] baudrate Baud rate in bits per second.
782 * @return SP_OK upon success, a negative error code otherwise.
786 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
789 * Get the baud rate from a port configuration.
791 * The user should allocate a variable of type int and
792 * pass a pointer to this to receive the result.
794 * @param[in] config Pointer to a configuration structure. Must not be NULL.
795 * @param[out] baudrate_ptr Pointer to a variable to store the result. Must not be NULL.
797 * @return SP_OK upon success, a negative error code otherwise.
801 enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
804 * Set the baud rate in a port configuration.
806 * @param[in] config Pointer to a configuration structure. Must not be NULL.
807 * @param[in] baudrate Baud rate in bits per second, or -1 to retain the current setting.
809 * @return SP_OK upon success, a negative error code otherwise.
813 enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
816 * Set the data bits for the specified serial port.
818 * @param[in] port Pointer to a port structure. Must not be NULL.
819 * @param[in] bits Number of data bits.
821 * @return SP_OK upon success, a negative error code otherwise.
825 enum sp_return sp_set_bits(struct sp_port *port, int bits);
828 * Get the data bits from a port configuration.
830 * The user should allocate a variable of type int and
831 * pass a pointer to this to receive the result.
833 * @param[in] config Pointer to a configuration structure. Must not be NULL.
834 * @param[out] bits_ptr Pointer to a variable to store the result. Must not be NULL.
836 * @return SP_OK upon success, a negative error code otherwise.
840 enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
843 * Set the data bits in a port configuration.
845 * @param[in] config Pointer to a configuration structure. Must not be NULL.
846 * @param[in] bits Number of data bits, or -1 to retain the current setting.
848 * @return SP_OK upon success, a negative error code otherwise.
852 enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
855 * Set the parity setting for the specified serial port.
857 * @param[in] port Pointer to a port structure. Must not be NULL.
858 * @param[in] parity Parity setting.
860 * @return SP_OK upon success, a negative error code otherwise.
864 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
867 * Get the parity setting from a port configuration.
869 * The user should allocate a variable of type enum sp_parity and
870 * pass a pointer to this to receive the result.
872 * @param[in] config Pointer to a configuration structure. Must not be NULL.
873 * @param[out] parity_ptr Pointer to a variable to store the result. Must not be NULL.
875 * @return SP_OK upon success, a negative error code otherwise.
879 enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
882 * Set the parity setting in a port configuration.
884 * @param[in] config Pointer to a configuration structure. Must not be NULL.
885 * @param[in] parity Parity setting, or -1 to retain the current setting.
887 * @return SP_OK upon success, a negative error code otherwise.
891 enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
894 * Set the stop bits for the specified serial port.
896 * @param[in] port Pointer to a port structure. Must not be NULL.
897 * @param[in] stopbits Number of stop bits.
899 * @return SP_OK upon success, a negative error code otherwise.
903 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
906 * Get the stop bits from a port configuration.
908 * The user should allocate a variable of type int and
909 * pass a pointer to this to receive the result.
911 * @param[in] config Pointer to a configuration structure. Must not be NULL.
912 * @param[out] stopbits_ptr Pointer to a variable to store the result. Must not be NULL.
914 * @return SP_OK upon success, a negative error code otherwise.
918 enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
921 * Set the stop bits in a port configuration.
923 * @param[in] config Pointer to a configuration structure. Must not be NULL.
924 * @param[in] stopbits Number of stop bits, or -1 to retain the current setting.
926 * @return SP_OK upon success, a negative error code otherwise.
930 enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
933 * Set the RTS pin behaviour for the specified serial port.
935 * @param[in] port Pointer to a port structure. Must not be NULL.
936 * @param[in] rts RTS pin mode.
938 * @return SP_OK upon success, a negative error code otherwise.
942 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
945 * Get the RTS pin behaviour from a port configuration.
947 * The user should allocate a variable of type enum sp_rts and
948 * pass a pointer to this to receive the result.
950 * @param[in] config Pointer to a configuration structure. Must not be NULL.
951 * @param[out] rts_ptr Pointer to a variable to store the result. Must not be NULL.
953 * @return SP_OK upon success, a negative error code otherwise.
957 enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
960 * Set the RTS pin behaviour in a port configuration.
962 * @param[in] config Pointer to a configuration structure. Must not be NULL.
963 * @param[in] rts RTS pin mode, or -1 to retain the current setting.
965 * @return SP_OK upon success, a negative error code otherwise.
969 enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
972 * Set the CTS pin behaviour for the specified serial port.
974 * @param[in] port Pointer to a port structure. Must not be NULL.
975 * @param[in] cts CTS pin mode.
977 * @return SP_OK upon success, a negative error code otherwise.
981 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
984 * Get the CTS pin behaviour from a port configuration.
986 * The user should allocate a variable of type enum sp_cts and
987 * pass a pointer to this to receive the result.
989 * @param[in] config Pointer to a configuration structure. Must not be NULL.
990 * @param[out] cts_ptr Pointer to a variable to store the result. Must not be NULL.
992 * @return SP_OK upon success, a negative error code otherwise.
996 enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
999 * Set the CTS pin behaviour in a port configuration.
1001 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1002 * @param[in] cts CTS pin mode, or -1 to retain the current setting.
1004 * @return SP_OK upon success, a negative error code otherwise.
1008 enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
1011 * Set the DTR pin behaviour for the specified serial port.
1013 * @param[in] port Pointer to a port structure. Must not be NULL.
1014 * @param[in] dtr DTR pin mode.
1016 * @return SP_OK upon success, a negative error code otherwise.
1020 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
1023 * Get the DTR pin behaviour from a port configuration.
1025 * The user should allocate a variable of type enum sp_dtr and
1026 * pass a pointer to this to receive the result.
1028 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1029 * @param[out] dtr_ptr Pointer to a variable to store the result. Must not be NULL.
1031 * @return SP_OK upon success, a negative error code otherwise.
1035 enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
1038 * Set the DTR pin behaviour in a port configuration.
1040 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1041 * @param[in] dtr DTR pin mode, or -1 to retain the current setting.
1043 * @return SP_OK upon success, a negative error code otherwise.
1047 enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
1050 * Set the DSR pin behaviour for the specified serial port.
1052 * @param[in] port Pointer to a port structure. Must not be NULL.
1053 * @param[in] dsr DSR pin mode.
1055 * @return SP_OK upon success, a negative error code otherwise.
1059 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
1062 * Get the DSR pin behaviour from a port configuration.
1064 * The user should allocate a variable of type enum sp_dsr and
1065 * pass a pointer to this to receive the result.
1067 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1068 * @param[out] dsr_ptr Pointer to a variable to store the result. Must not be NULL.
1070 * @return SP_OK upon success, a negative error code otherwise.
1074 enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
1077 * Set the DSR pin behaviour in a port configuration.
1079 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1080 * @param[in] dsr DSR pin mode, or -1 to retain the current setting.
1082 * @return SP_OK upon success, a negative error code otherwise.
1086 enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
1089 * Set the XON/XOFF configuration for the specified serial port.
1091 * @param[in] port Pointer to a port structure. Must not be NULL.
1092 * @param[in] xon_xoff XON/XOFF mode.
1094 * @return SP_OK upon success, a negative error code otherwise.
1098 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
1101 * Get the XON/XOFF configuration from a port configuration.
1103 * The user should allocate a variable of type enum sp_xonxoff and
1104 * pass a pointer to this to receive the result.
1106 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1107 * @param[out] xon_xoff_ptr Pointer to a variable to store the result. Must not be NULL.
1109 * @return SP_OK upon success, a negative error code otherwise.
1113 enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
1116 * Set the XON/XOFF configuration in a port configuration.
1118 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1119 * @param[in] xon_xoff XON/XOFF mode, or -1 to retain the current setting.
1121 * @return SP_OK upon success, a negative error code otherwise.
1125 enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
1128 * Set the flow control type in a port configuration.
1130 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
1131 * XON/XOFF settings as necessary for the specified flow control
1132 * type. For more fine-grained control of these settings, use their
1133 * individual configuration functions.
1135 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1136 * @param[in] flowcontrol Flow control setting to use.
1138 * @return SP_OK upon success, a negative error code otherwise.
1142 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
1145 * Set the flow control type for the specified serial port.
1147 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
1148 * XON/XOFF settings as necessary for the specified flow control
1149 * type. For more fine-grained control of these settings, use their
1150 * individual configuration functions.
1152 * @param[in] port Pointer to a port structure. Must not be NULL.
1153 * @param[in] flowcontrol Flow control setting to use.
1155 * @return SP_OK upon success, a negative error code otherwise.
1159 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
1164 * @defgroup Data Data handling
1166 * Reading, writing, and flushing data.
1172 * Read bytes from the specified serial port, blocking until complete.
1174 * @warning If your program runs on Unix, defines its own signal handlers, and
1175 * needs to abort blocking reads when these are called, then you
1176 * should not use this function. It repeats system calls that return
1177 * with EINTR. To be able to abort a read from a signal handler, you
1178 * should implement your own blocking read using sp_nonblocking_read()
1179 * together with a blocking method that makes sense for your program.
1180 * E.g. you can obtain the file descriptor for an open port using
1181 * sp_get_port_handle() and use this to call select() or pselect(),
1182 * with appropriate arrangements to return if a signal is received.
1184 * @param[in] port Pointer to a port structure. Must not be NULL.
1185 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1186 * @param[in] count Requested number of bytes to read.
1187 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1189 * @return The number of bytes read on success, or a negative error code. If
1190 * the number of bytes returned is less than that requested, the
1191 * timeout was reached before the requested number of bytes was
1192 * available. If timeout is zero, the function will always return
1193 * either the requested number of bytes or a negative error code.
1197 enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
1200 * Read bytes from the specified serial port, returning as soon as any data is
1203 * @warning If your program runs on Unix, defines its own signal handlers, and
1204 * needs to abort blocking reads when these are called, then you
1205 * should not use this function. It repeats system calls that return
1206 * with EINTR. To be able to abort a read from a signal handler, you
1207 * should implement your own blocking read using sp_nonblocking_read()
1208 * together with a blocking method that makes sense for your program.
1209 * E.g. you can obtain the file descriptor for an open port using
1210 * sp_get_port_handle() and use this to call select() or pselect(),
1211 * with appropriate arrangements to return if a signal is received.
1213 * @param[in] port Pointer to a port structure. Must not be NULL.
1214 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1215 * @param[in] count Maximum number of bytes to read. Must not be zero.
1216 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1218 * @return The number of bytes read on success, or a negative error code. If
1219 * the result is zero, the timeout was reached before any bytes were
1220 * available. If timeout_ms is zero, the function will always return
1221 * either at least one byte, or a negative error code.
1225 enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
1228 * Read bytes from the specified serial port, without blocking.
1230 * @param[in] port Pointer to a port structure. Must not be NULL.
1231 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1232 * @param[in] count Maximum number of bytes to read.
1234 * @return The number of bytes read on success, or a negative error code. The
1235 * number of bytes returned may be any number from zero to the maximum
1236 * that was requested.
1240 enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
1243 * Write bytes to the specified serial port, blocking until complete.
1245 * Note that this function only ensures that the accepted bytes have been
1246 * written to the OS; they may be held in driver or hardware buffers and not
1247 * yet physically transmitted. To check whether all written bytes have actually
1248 * been transmitted, use the sp_output_waiting() function. To wait until all
1249 * written bytes have actually been transmitted, use the sp_drain() function.
1251 * @warning If your program runs on Unix, defines its own signal handlers, and
1252 * needs to abort blocking writes when these are called, then you
1253 * should not use this function. It repeats system calls that return
1254 * with EINTR. To be able to abort a write from a signal handler, you
1255 * should implement your own blocking write using sp_nonblocking_write()
1256 * together with a blocking method that makes sense for your program.
1257 * E.g. you can obtain the file descriptor for an open port using
1258 * sp_get_port_handle() and use this to call select() or pselect(),
1259 * with appropriate arrangements to return if a signal is received.
1261 * @param[in] port Pointer to a port structure. Must not be NULL.
1262 * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1263 * @param[in] count Requested number of bytes to write.
1264 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1266 * @return The number of bytes written on success, or a negative error code.
1267 * If the number of bytes returned is less than that requested, the
1268 * timeout was reached before the requested number of bytes was
1269 * written. If timeout is zero, the function will always return
1270 * either the requested number of bytes or a negative error code. In
1271 * the event of an error there is no way to determine how many bytes
1272 * were sent before the error occurred.
1276 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout_ms);
1279 * Write bytes to the specified serial port, without blocking.
1281 * Note that this function only ensures that the accepted bytes have been
1282 * written to the OS; they may be held in driver or hardware buffers and not
1283 * yet physically transmitted. To check whether all written bytes have actually
1284 * been transmitted, use the sp_output_waiting() function. To wait until all
1285 * written bytes have actually been transmitted, use the sp_drain() function.
1287 * @param[in] port Pointer to a port structure. Must not be NULL.
1288 * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1289 * @param[in] count Maximum number of bytes to write.
1291 * @return The number of bytes written on success, or a negative error code.
1292 * The number of bytes returned may be any number from zero to the
1293 * maximum that was requested.
1297 enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
1300 * Gets the number of bytes waiting in the input buffer.
1302 * @param[in] port Pointer to a port structure. Must not be NULL.
1304 * @return Number of bytes waiting on success, a negative error code otherwise.
1308 enum sp_return sp_input_waiting(struct sp_port *port);
1311 * Gets the number of bytes waiting in the output buffer.
1313 * @param[in] port Pointer to a port structure. Must not be NULL.
1315 * @return Number of bytes waiting on success, a negative error code otherwise.
1319 enum sp_return sp_output_waiting(struct sp_port *port);
1322 * Flush serial port buffers. Data in the selected buffer(s) is discarded.
1324 * @param[in] port Pointer to a port structure. Must not be NULL.
1325 * @param[in] buffers Which buffer(s) to flush.
1327 * @return SP_OK upon success, a negative error code otherwise.
1331 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
1334 * Wait for buffered data to be transmitted.
1336 * @warning If your program runs on Unix, defines its own signal handlers, and
1337 * needs to abort draining the output buffer when when these are
1338 * called, then you should not use this function. It repeats system
1339 * calls that return with EINTR. To be able to abort a drain from a
1340 * signal handler, you would need to implement your own blocking
1341 * drain by polling the result of sp_output_waiting().
1343 * @param[in] port Pointer to a port structure. Must not be NULL.
1345 * @return SP_OK upon success, a negative error code otherwise.
1349 enum sp_return sp_drain(struct sp_port *port);
1354 * @defgroup Waiting Waiting
1356 * Waiting for events and timeout handling.
1362 * Allocate storage for a set of events.
1364 * The user should allocate a variable of type struct sp_event_set *,
1365 * then pass a pointer to this variable to receive the result.
1367 * The result should be freed after use by calling sp_free_event_set().
1369 * @param[out] result_ptr If any error is returned, the variable pointed to by
1370 * result_ptr will be set to NULL. Otherwise, it will
1371 * be set to point to the event set. Must not be NULL.
1373 * @return SP_OK upon success, a negative error code otherwise.
1377 enum sp_return sp_new_event_set(struct sp_event_set **result_ptr);
1380 * Add events to a struct sp_event_set for a given port.
1382 * The port must first be opened by calling sp_open() using the same port
1385 * After the port is closed or the port structure freed, the results may
1386 * no longer be valid.
1388 * @param[in,out] event_set Event set to update. Must not be NULL.
1389 * @param[in] port Pointer to a port structure. Must not be NULL.
1390 * @param[in] mask Bitmask of events to be waited for.
1392 * @return SP_OK upon success, a negative error code otherwise.
1396 enum sp_return sp_add_port_events(struct sp_event_set *event_set,
1397 const struct sp_port *port, enum sp_event mask);
1400 * Wait for any of a set of events to occur.
1402 * @param[in] event_set Event set to wait on. Must not be NULL.
1403 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1405 * @return SP_OK upon success, a negative error code otherwise.
1409 enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout_ms);
1412 * Free a structure allocated by sp_new_event_set().
1414 * @param[in] event_set Event set to free. Must not be NULL.
1418 void sp_free_event_set(struct sp_event_set *event_set);
1423 * @defgroup Signals Signals
1425 * Port signalling operations.
1431 * Gets the status of the control signals for the specified port.
1433 * The user should allocate a variable of type "enum sp_signal" and pass a
1434 * pointer to this variable to receive the result. The result is a bitmask
1435 * in which individual signals can be checked by bitwise OR with values of
1436 * the sp_signal enum.
1438 * @param[in] port Pointer to a port structure. Must not be NULL.
1439 * @param[out] signal_mask Pointer to a variable to receive the result.
1442 * @return SP_OK upon success, a negative error code otherwise.
1446 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signal_mask);
1449 * Put the port transmit line into the break state.
1451 * @param[in] port Pointer to a port structure. Must not be NULL.
1453 * @return SP_OK upon success, a negative error code otherwise.
1457 enum sp_return sp_start_break(struct sp_port *port);
1460 * Take the port transmit line out of the break state.
1462 * @param[in] port Pointer to a port structure. Must not be NULL.
1464 * @return SP_OK upon success, a negative error code otherwise.
1468 enum sp_return sp_end_break(struct sp_port *port);
1473 * @defgroup Errors Errors
1475 * Obtaining error information.
1481 * Get the error code for a failed operation.
1483 * In order to obtain the correct result, this function should be called
1484 * straight after the failure, before executing any other system operations.
1485 * The result is thread-specific, and only valid when called immediately
1486 * after a previous call returning SP_ERR_FAIL.
1488 * @return The system's numeric code for the error that caused the last
1489 * operation to fail.
1493 int sp_last_error_code(void);
1496 * Get the error message for a failed operation.
1498 * In order to obtain the correct result, this function should be called
1499 * straight after the failure, before executing other system operations.
1500 * The result is thread-specific, and only valid when called immediately
1501 * after a previous call returning SP_ERR_FAIL.
1503 * @return The system's message for the error that caused the last
1504 * operation to fail. This string may be allocated by the function,
1505 * and should be freed after use by calling sp_free_error_message().
1509 char *sp_last_error_message(void);
1512 * Free an error message returned by sp_last_error_message().
1514 * @param[in] message The error message string to free. Must not be NULL.
1518 void sp_free_error_message(char *message);
1521 * Set the handler function for library debugging messages.
1523 * Debugging messages are generated by the library during each operation,
1524 * to help in diagnosing problems. The handler will be called for each
1525 * message. The handler can be set to NULL to ignore all debug messages.
1527 * The handler function should accept a format string and variable length
1528 * argument list, in the same manner as e.g. printf().
1530 * The default handler is sp_default_debug_handler().
1532 * @param[in] handler The handler function to use. Can be NULL (in that case
1533 * all debug messages will be ignored).
1537 void sp_set_debug_handler(void (*handler)(const char *format, ...));
1540 * Default handler function for library debugging messages.
1542 * This function prints debug messages to the standard error stream if the
1543 * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
1546 * @param[in] format The format string to use. Must not be NULL.
1547 * @param[in] ... The variable length argument list to use.
1551 void sp_default_debug_handler(const char *format, ...);
1556 * @defgroup Versions Versions
1558 * Version number querying functions, definitions, and macros.
1560 * This set of API calls returns two different version numbers related
1561 * to libserialport. The "package version" is the release version number of the
1562 * libserialport tarball in the usual "major.minor.micro" format, e.g. "0.1.0".
1564 * The "library version" is independent of that; it is the libtool version
1565 * number in the "current:revision:age" format, e.g. "2:0:0".
1566 * See http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning for details.
1568 * Both version numbers (and/or individual components of them) can be
1569 * retrieved via the API calls at runtime, and/or they can be checked at
1570 * compile/preprocessor time using the respective macros.
1576 * Package version macros (can be used for conditional compilation).
1579 /** The libserialport package 'major' version number. */
1580 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
1582 /** The libserialport package 'minor' version number. */
1583 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
1585 /** The libserialport package 'micro' version number. */
1586 #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
1588 /** The libserialport package version ("major.minor.micro") as string. */
1589 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
1592 * Library/libtool version macros (can be used for conditional compilation).
1595 /** The libserialport libtool 'current' version number. */
1596 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
1598 /** The libserialport libtool 'revision' version number. */
1599 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
1601 /** The libserialport libtool 'age' version number. */
1602 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
1604 /** The libserialport libtool version ("current:revision:age") as string. */
1605 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
1608 * Get the major libserialport package version number.
1610 * @return The major package version number.
1614 int sp_get_major_package_version(void);
1617 * Get the minor libserialport package version number.
1619 * @return The minor package version number.
1623 int sp_get_minor_package_version(void);
1626 * Get the micro libserialport package version number.
1628 * @return The micro package version number.
1632 int sp_get_micro_package_version(void);
1635 * Get the libserialport package version number as a string.
1637 * @return The package version number string. The returned string is
1638 * static and thus should NOT be free'd by the caller.
1642 const char *sp_get_package_version_string(void);
1645 * Get the "current" part of the libserialport library version number.
1647 * @return The "current" library version number.
1651 int sp_get_current_lib_version(void);
1654 * Get the "revision" part of the libserialport library version number.
1656 * @return The "revision" library version number.
1660 int sp_get_revision_lib_version(void);
1663 * Get the "age" part of the libserialport library version number.
1665 * @return The "age" library version number.
1669 int sp_get_age_lib_version(void);
1672 * Get the libserialport library version number as a string.
1674 * @return The library version number string. The returned string is
1675 * static and thus should NOT be free'd by the caller.
1679 const char *sp_get_lib_version_string(void);