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.
65 * To use libserialport functions in your code, you should include the
66 * libserialport.h header, i.e. "#include <libserialport.h>".
71 * All identifiers defined by the public libserialport headers use the prefix
72 * sp_ (for functions and data types) or SP_ (for macros and constants).
77 * The functions provided by the library are documented in detail in
78 * the following sections:
80 * - @ref Enumeration (obtaining a list of serial ports on the system)
81 * - @ref Ports (opening, closing and getting information about ports)
82 * - @ref Configuration (baud rate, parity, etc.)
83 * - @ref Signals (modem control lines, breaks, etc.)
84 * - @ref Data (reading and writing data, and buffer management)
85 * - @ref Waiting (waiting for ports to be ready, integrating with event loops)
86 * - @ref Errors (getting error and debugging information)
91 * The library defines three data structures:
93 * - @ref sp_port, which represents a serial port.
94 * See @ref Enumeration.
95 * - @ref sp_port_config, which represents a port configuration.
96 * See @ref Configuration.
97 * - @ref sp_event_set, which represents a set of events.
100 * All these structures are allocated and freed by library functions. It is
101 * the caller's responsibility to ensure that the correct calls are made to
102 * free allocated structures after use.
104 * Return codes and error handling
105 * -------------------------------
107 * Most functions have return type @ref sp_return and can return only four
108 * possible error values:
110 * - @ref SP_ERR_ARG means that a function was called with invalid
111 * arguments. This implies a bug in the caller. The arguments passed would
112 * be invalid regardless of the underlying OS or serial device involved.
114 * - @ref SP_ERR_FAIL means that the OS reported a failure. The error code or
115 * message provided by the OS can be obtained by calling sp_last_error_code()
116 * or sp_last_error_message().
118 * - @ref SP_ERR_SUPP indicates that there is no support for the requested
119 * operation in the current OS, driver or device. No error message is
120 * available from the OS in this case. There is either no way to request
121 * the operation in the first place, or libserialport does not know how to
122 * do so in the current version.
124 * - @ref SP_ERR_MEM indicates that a memory allocation failed.
126 * All of these error values are negative.
128 * Calls that succeed return @ref SP_OK, which is equal to zero. Some functions
129 * declared @ref sp_return can also return a positive value for a successful
130 * numeric result, e.g. sp_blocking_read() or sp_blocking_write().
132 * An error message is only available via sp_last_error_message() in the case
133 * where SP_ERR_FAIL was returned by the previous function call. The error
134 * message returned is that provided by the OS, using the current language
135 * settings. It is an error to call sp_last_error_code() or
136 * sp_last_error_message() except after a previous function call returned
137 * SP_ERR_FAIL. The library does not define its own error codes or messages
138 * to accompany other return codes.
143 * Certain combinations of calls can be made concurrently, as follows.
145 * - Calls using different ports may always be made concurrently, i.e.
146 * it is safe for separate threads to handle their own ports.
148 * - Calls using the same port may be made concurrently when one call
149 * is a read operation and one call is a write operation, i.e. it is safe
150 * to use separate "reader" and "writer" threads for the same port. See
151 * below for which operations meet these definitions.
155 * - sp_blocking_read()
156 * - sp_blocking_read_next()
157 * - sp_nonblocking_read()
158 * - sp_input_waiting()
159 * - sp_flush() with @ref SP_BUF_INPUT only.
160 * - sp_wait() with @ref SP_EVENT_RX_READY only.
164 * - sp_blocking_write()
165 * - sp_nonblocking_write()
166 * - sp_output_waiting()
168 * - sp_flush() with @ref SP_BUF_OUTPUT only.
169 * - sp_wait() with @ref SP_EVENT_TX_READY only.
171 * If two calls, on the same port, do not fit into one of these categories
172 * each, then they may not be made concurrently.
177 * The library can output extensive tracing and debugging information. The
178 * simplest way to use this is to set the environment variable
179 * LIBSERIALPORT_DEBUG to any value; messages will then be output to the
180 * standard error stream.
182 * This behaviour is implemented by a default debug message handling
183 * callback. An alternative callback can be set using sp_set_debug_handler(),
184 * in order to e.g. redirect the output elsewhere or filter it.
186 * No guarantees are made about the content of the debug output; it is chosen
187 * to suit the needs of the developers and may change between releases.
193 * The following guidelines may help when porting existing OS-specific code
194 * to use libserialport.
196 * ### Porting from Unix-like systems ###
198 * There are two main differences to note when porting code written for Unix.
200 * The first is that Unix traditionally provides a wide range of functionality
201 * for dealing with serial devices at the OS level; this is exposed through the
202 * termios API and dates to the days when serial terminals were common. If your
203 * code relies on many of these facilities you will need to adapt it, because
204 * libserialport provides only a raw binary channel with no special handling.
206 * The second relates to blocking versus non-blocking I/O behaviour. In
207 * Unix-like systems this is normally specified by setting the O_NONBLOCK
208 * flag on the file descriptor, affecting the semantics of subsequent read()
211 * In libserialport, blocking and nonblocking operations are both available at
212 * any time. If your existing code Ń•ets O_NONBLOCK, you should use
213 * sp_nonblocking_read() and sp_nonblocking_write() to get the same behaviour
214 * as your existing read() and write() calls. If it does not, you should use
215 * sp_blocking_read() and sp_blocking_write() instead. You may also find
216 * sp_blocking_read_next() useful, which reproduces the semantics of a blocking
217 * read() with VTIME = 0 and VMIN = 1 set in termios.
219 * Finally, you should take care if your program uses custom signal handlers.
220 * The blocking calls provided by libserialport will restart system calls that
221 * return with EINTR, so you will need to make your own arrangements if you
222 * need to interrupt blocking operations when your signal handlers are called.
223 * This is not an issue if you only use the default handlers.
225 * ### Porting from Windows ###
227 * The main consideration when porting from Windows is that there is no
228 * direct equivalent for overlapped I/O operations.
230 * If your program does not use overlapped I/O, you can simply use
231 * sp_blocking_read() and sp_blocking_write() as direct equivalents for
232 * ReadFile() and WriteFile(). You may also find sp_blocking_read_next()
233 * useful, which reproduces the special semantics of ReadFile() with
234 * ReadIntervalTimeout and ReadTotalTimeoutMultiplier set to MAXDWORD
235 * and 0 < ReadTotalTimeoutConstant < MAXDWORD.
237 * If your program makes use of overlapped I/O to continue work while a serial
238 * operation is in progress, then you can achieve the same results using
239 * sp_nonblocking_read() and sp_nonblocking_write().
241 * Generally, overlapped I/O is combined with either waiting for completion
242 * once there is no more background work to do (using WaitForSingleObject() or
243 * WaitForMultipleObjects()), or periodically checking for completion with
244 * GetOverlappedResult(). If the aim is to start a new operation for further
245 * data once the previous one has completed, you can instead simply call the
246 * nonblocking functions again with the next data. If you need to wait for
247 * completion, use sp_wait() to determine when the port is ready to send or
248 * receive further data.
251 #ifndef LIBSERIALPORT_LIBSERIALPORT_H
252 #define LIBSERIALPORT_LIBSERIALPORT_H
260 /** Return values. */
262 /** Operation completed successfully. */
264 /** Invalid arguments were passed to the function. */
266 /** A system error occurred while executing the operation. */
268 /** A memory allocation failed while executing the operation. */
270 /** The requested operation is not supported by this system or device. */
274 /** Port access modes. */
276 /** Open port for read access. */
278 /** Open port for write access. */
280 /** Open port for read and write access. @since 0.1.1 */
281 SP_MODE_READ_WRITE = 3
286 /** Data received and ready to read. */
287 SP_EVENT_RX_READY = 1,
288 /** Ready to transmit new data. */
289 SP_EVENT_TX_READY = 2,
290 /** Error occurred. */
294 /** Buffer selection. */
298 /** Output buffer. */
304 /** Parity settings. */
306 /** Special value to indicate setting should be left alone. */
307 SP_PARITY_INVALID = -1,
320 /** RTS pin behaviour. */
322 /** Special value to indicate setting should be left alone. */
328 /** RTS used for flow control. */
329 SP_RTS_FLOW_CONTROL = 2
332 /** CTS pin behaviour. */
334 /** Special value to indicate setting should be left alone. */
338 /** CTS used for flow control. */
339 SP_CTS_FLOW_CONTROL = 1
342 /** DTR pin behaviour. */
344 /** Special value to indicate setting should be left alone. */
350 /** DTR used for flow control. */
351 SP_DTR_FLOW_CONTROL = 2
354 /** DSR pin behaviour. */
356 /** Special value to indicate setting should be left alone. */
360 /** DSR used for flow control. */
361 SP_DSR_FLOW_CONTROL = 1
364 /** XON/XOFF flow control behaviour. */
366 /** Special value to indicate setting should be left alone. */
367 SP_XONXOFF_INVALID = -1,
368 /** XON/XOFF disabled. */
369 SP_XONXOFF_DISABLED = 0,
370 /** XON/XOFF enabled for input only. */
372 /** XON/XOFF enabled for output only. */
374 /** XON/XOFF enabled for input and output. */
378 /** Standard flow control combinations. */
379 enum sp_flowcontrol {
380 /** No flow control. */
381 SP_FLOWCONTROL_NONE = 0,
382 /** Software flow control using XON/XOFF characters. */
383 SP_FLOWCONTROL_XONXOFF = 1,
384 /** Hardware flow control using RTS/CTS signals. */
385 SP_FLOWCONTROL_RTSCTS = 2,
386 /** Hardware flow control using DTR/DSR signals. */
387 SP_FLOWCONTROL_DTRDSR = 3
390 /** Input signals. */
392 /** Clear to send. */
394 /** Data set ready. */
396 /** Data carrier detect. */
398 /** Ring indicator. */
408 /** Native platform serial port. @since 0.1.1 */
410 /** USB serial port adapter. @since 0.1.1 */
412 /** Bluetooth serial port adapter. @since 0.1.1 */
413 SP_TRANSPORT_BLUETOOTH
418 * An opaque structure representing a serial port.
423 * @struct sp_port_config
424 * An opaque structure representing the configuration for a serial port.
426 struct sp_port_config;
429 * @struct sp_event_set
430 * A set of handles to wait on for events.
432 struct sp_event_set {
433 /** Array of OS-specific handles. */
435 /** Array of bitmasks indicating which events apply for each handle. */
436 enum sp_event *masks;
437 /** Number of handles. */
442 * @defgroup Enumeration Port enumeration
444 * Enumerating the serial ports of a system.
450 * Obtain a pointer to a new sp_port structure representing the named port.
452 * The user should allocate a variable of type "struct sp_port *" and pass a
453 * pointer to this to receive the result.
455 * The result should be freed after use by calling sp_free_port().
457 * @param[in] portname The OS-specific name of a serial port. Must not be NULL.
458 * @param[out] port_ptr If any error is returned, the variable pointed to by
459 * port_ptr will be set to NULL. Otherwise, it will be set
460 * to point to the newly allocated port. Must not be NULL.
462 * @return SP_OK upon success, a negative error code otherwise.
466 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
469 * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
471 * @param[in] port Pointer to a port structure. Must not be NULL.
475 void sp_free_port(struct sp_port *port);
478 * List the serial ports available on the system.
480 * The result obtained is an array of pointers to sp_port structures,
481 * terminated by a NULL. The user should allocate a variable of type
482 * "struct sp_port **" and pass a pointer to this to receive the result.
484 * The result should be freed after use by calling sp_free_port_list().
485 * If a port from the list is to be used after freeing the list, it must be
486 * copied first using sp_copy_port().
488 * @param[out] list_ptr If any error is returned, the variable pointed to by
489 * list_ptr will be set to NULL. Otherwise, it will be set
490 * to point to the newly allocated array. Must not be NULL.
492 * @return SP_OK upon success, a negative error code otherwise.
496 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
499 * Make a new copy of an sp_port structure.
501 * The user should allocate a variable of type "struct sp_port *" and pass a
502 * pointer to this to receive the result.
504 * The copy should be freed after use by calling sp_free_port().
506 * @param[in] port Pointer to a port structure. Must not be NULL.
507 * @param[out] copy_ptr If any error is returned, the variable pointed to by
508 * copy_ptr will be set to NULL. Otherwise, it will be set
509 * to point to the newly allocated copy. Must not be NULL.
511 * @return SP_OK upon success, a negative error code otherwise.
515 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
518 * Free a port list obtained from sp_list_ports().
520 * This will also free all the sp_port structures referred to from the list;
521 * any that are to be retained must be copied first using sp_copy_port().
523 * @param[in] ports Pointer to a list of port structures. Must not be NULL.
527 void sp_free_port_list(struct sp_port **ports);
531 * @defgroup Ports Port handling
533 * Opening, closing and querying ports.
539 * Open the specified serial port.
541 * @param[in] port Pointer to a port structure. Must not be NULL.
542 * @param[in] flags Flags to use when opening the serial port.
544 * @return SP_OK upon success, a negative error code otherwise.
548 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
551 * Close the specified serial port.
553 * @param[in] port Pointer to a port structure. Must not be NULL.
555 * @return SP_OK upon success, a negative error code otherwise.
559 enum sp_return sp_close(struct sp_port *port);
562 * Get the name of a port.
564 * The name returned is whatever is normally used to refer to a port on the
565 * current operating system; e.g. for Windows it will usually be a "COMn"
566 * device name, and for Unix it will be a device path beginning with "/dev/".
568 * @param[in] port Pointer to a port structure. Must not be NULL.
570 * @return The port name, or NULL if an invalid port is passed. The name
571 * string is part of the port structure and may not be used after
572 * the port structure has been freed.
576 char *sp_get_port_name(const struct sp_port *port);
579 * Get a description for a port, to present to end user.
581 * @param[in] port Pointer to a port structure. Must not be NULL.
583 * @return The port description, or NULL if an invalid port is passed.
584 * The description string is part of the port structure and may not
585 * be used after the port structure has been freed.
589 char *sp_get_port_description(const struct sp_port *port);
592 * Get the transport type used by a port.
594 * @param[in] port Pointer to a port structure. Must not be NULL.
596 * @return The port transport type.
600 enum sp_transport sp_get_port_transport(const struct sp_port *port);
603 * Get the USB bus number and address on bus of a USB serial adapter port.
605 * @param[in] port Pointer to a port structure. Must not be NULL.
606 * @param[out] usb_bus Pointer to a variable to store the USB bus.
607 * Can be NULL (in that case it will be ignored).
608 * @param[out] usb_address Pointer to a variable to store the USB address.
609 * Can be NULL (in that case it will be ignored).
611 * @return SP_OK upon success, a negative error code otherwise.
615 enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port,
616 int *usb_bus, int *usb_address);
619 * Get the USB Vendor ID and Product ID of a USB serial adapter port.
621 * @param[in] port Pointer to a port structure. Must not be NULL.
622 * @param[out] usb_vid Pointer to a variable to store the USB VID.
623 * Can be NULL (in that case it will be ignored).
624 * @param[out] usb_pid Pointer to a variable to store the USB PID.
625 * Can be NULL (in that case it will be ignored).
627 * @return SP_OK upon success, a negative error code otherwise.
631 enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, int *usb_vid, int *usb_pid);
634 * Get the USB manufacturer string of a USB serial adapter port.
636 * @param[in] port Pointer to a port structure. Must not be NULL.
638 * @return The port manufacturer string, or NULL if an invalid port is passed.
639 * The manufacturer string is part of the port structure and may not
640 * be used after the port structure has been freed.
644 char *sp_get_port_usb_manufacturer(const struct sp_port *port);
647 * Get the USB product string of a USB serial adapter port.
649 * @param[in] port Pointer to a port structure. Must not be NULL.
651 * @return The port product string, or NULL if an invalid port is passed.
652 * The product string is part of the port structure and may not be
653 * used after the port structure has been freed.
657 char *sp_get_port_usb_product(const struct sp_port *port);
660 * Get the USB serial number string of a USB serial adapter port.
662 * @param[in] port Pointer to a port structure. Must not be NULL.
664 * @return The port serial number, or NULL if an invalid port is passed.
665 * The serial number string is part of the port structure and may
666 * not be used after the port structure has been freed.
670 char *sp_get_port_usb_serial(const struct sp_port *port);
673 * Get the MAC address of a Bluetooth serial adapter port.
675 * @param[in] port Pointer to a port structure. Must not be NULL.
677 * @return The port MAC address, or NULL if an invalid port is passed.
678 * The MAC address string is part of the port structure and may not
679 * be used after the port structure has been freed.
683 char *sp_get_port_bluetooth_address(const struct sp_port *port);
686 * Get the operating system handle for a port.
688 * The type of the handle depends on the operating system. On Unix based
689 * systems, the handle is a file descriptor of type "int". On Windows, the
690 * handle is of type "HANDLE". The user should allocate a variable of the
691 * appropriate type and pass a pointer to this to receive the result.
693 * To obtain a valid handle, the port must first be opened by calling
694 * sp_open() using the same port structure.
696 * After the port is closed or the port structure freed, the handle may
697 * no longer be valid.
699 * @warning This feature is provided so that programs may make use of
700 * OS-specific functionality where desired. Doing so obviously
701 * comes at a cost in portability. It also cannot be guaranteed
702 * that direct usage of the OS handle will not conflict with the
703 * library's own usage of the port. Be careful.
705 * @param[in] port Pointer to a port structure. Must not be NULL.
706 * @param[out] result_ptr If any error is returned, the variable pointed to by
707 * result_ptr will have unknown contents and should not
708 * be used. Otherwise, it will be set to point to the
709 * OS handle. Must not be NULL.
711 * @return SP_OK upon success, a negative error code otherwise.
715 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
720 * @defgroup Configuration Configuration
722 * Setting and querying serial port parameters.
727 * Allocate a port configuration structure.
729 * The user should allocate a variable of type "struct sp_port_config *" and
730 * pass a pointer to this to receive the result. The variable will be updated
731 * to point to the new configuration structure. The structure is opaque and
732 * must be accessed via the functions provided.
734 * All parameters in the structure will be initialised to special values which
735 * are ignored by sp_set_config().
737 * The structure should be freed after use by calling sp_free_config().
739 * @param[out] config_ptr If any error is returned, the variable pointed to by
740 * config_ptr will be set to NULL. Otherwise, it will
741 * be set to point to the allocated config structure.
744 * @return SP_OK upon success, a negative error code otherwise.
748 enum sp_return sp_new_config(struct sp_port_config **config_ptr);
751 * Free a port configuration structure.
753 * @param[in] config Pointer to a configuration structure. Must not be NULL.
757 void sp_free_config(struct sp_port_config *config);
760 * Get the current configuration of the specified serial port.
762 * The user should allocate a configuration structure using sp_new_config()
763 * and pass this as the config parameter. The configuration structure will
764 * be updated with the port configuration.
766 * Any parameters that are configured with settings not recognised or
767 * supported by libserialport, will be set to special values that are
768 * ignored by sp_set_config().
770 * @param[in] port Pointer to a port structure. Must not be NULL.
771 * @param[out] config Pointer to a configuration structure that will hold
772 * the result. Upon errors the contents of the config
773 * struct will not be changed. Must not be NULL.
775 * @return SP_OK upon success, a negative error code otherwise.
779 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
782 * Set the configuration for the specified serial port.
784 * For each parameter in the configuration, there is a special value (usually
785 * -1, but see the documentation for each field). These values will be ignored
786 * and the corresponding setting left unchanged on the port.
788 * Upon errors, the configuration of the serial port is unknown since
789 * partial/incomplete config updates may have happened.
791 * @param[in] port Pointer to a port structure. Must not be NULL.
792 * @param[in] config Pointer to a configuration structure. Must not be NULL.
794 * @return SP_OK upon success, a negative error code otherwise.
798 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
801 * Set the baud rate for the specified serial port.
803 * @param[in] port Pointer to a port structure. Must not be NULL.
804 * @param[in] baudrate Baud rate in bits per second.
806 * @return SP_OK upon success, a negative error code otherwise.
810 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
813 * Get the baud rate from a port configuration.
815 * The user should allocate a variable of type int and
816 * pass a pointer to this to receive the result.
818 * @param[in] config Pointer to a configuration structure. Must not be NULL.
819 * @param[out] baudrate_ptr Pointer to a variable to store the result. Must not be NULL.
821 * @return SP_OK upon success, a negative error code otherwise.
825 enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
828 * Set the baud rate in a port configuration.
830 * @param[in] config Pointer to a configuration structure. Must not be NULL.
831 * @param[in] baudrate Baud rate in bits per second, or -1 to retain the current setting.
833 * @return SP_OK upon success, a negative error code otherwise.
837 enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
840 * Set the data bits for the specified serial port.
842 * @param[in] port Pointer to a port structure. Must not be NULL.
843 * @param[in] bits Number of data bits.
845 * @return SP_OK upon success, a negative error code otherwise.
849 enum sp_return sp_set_bits(struct sp_port *port, int bits);
852 * Get the data bits from a port configuration.
854 * The user should allocate a variable of type int and
855 * pass a pointer to this to receive the result.
857 * @param[in] config Pointer to a configuration structure. Must not be NULL.
858 * @param[out] bits_ptr Pointer to a variable to store the result. Must not be NULL.
860 * @return SP_OK upon success, a negative error code otherwise.
864 enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
867 * Set the data bits in a port configuration.
869 * @param[in] config Pointer to a configuration structure. Must not be NULL.
870 * @param[in] bits Number of data bits, or -1 to retain the current setting.
872 * @return SP_OK upon success, a negative error code otherwise.
876 enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
879 * Set the parity setting for the specified serial port.
881 * @param[in] port Pointer to a port structure. Must not be NULL.
882 * @param[in] parity Parity setting.
884 * @return SP_OK upon success, a negative error code otherwise.
888 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
891 * Get the parity setting from a port configuration.
893 * The user should allocate a variable of type enum sp_parity and
894 * pass a pointer to this to receive the result.
896 * @param[in] config Pointer to a configuration structure. Must not be NULL.
897 * @param[out] parity_ptr Pointer to a variable to store the result. Must not be NULL.
899 * @return SP_OK upon success, a negative error code otherwise.
903 enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
906 * Set the parity setting in a port configuration.
908 * @param[in] config Pointer to a configuration structure. Must not be NULL.
909 * @param[in] parity Parity setting, or -1 to retain the current setting.
911 * @return SP_OK upon success, a negative error code otherwise.
915 enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
918 * Set the stop bits for the specified serial port.
920 * @param[in] port Pointer to a port structure. Must not be NULL.
921 * @param[in] stopbits Number of stop bits.
923 * @return SP_OK upon success, a negative error code otherwise.
927 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
930 * Get the stop bits from a port configuration.
932 * The user should allocate a variable of type int and
933 * pass a pointer to this to receive the result.
935 * @param[in] config Pointer to a configuration structure. Must not be NULL.
936 * @param[out] stopbits_ptr Pointer to a variable to store the result. Must not be NULL.
938 * @return SP_OK upon success, a negative error code otherwise.
942 enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
945 * Set the stop bits in a port configuration.
947 * @param[in] config Pointer to a configuration structure. Must not be NULL.
948 * @param[in] stopbits Number of stop bits, or -1 to retain the current setting.
950 * @return SP_OK upon success, a negative error code otherwise.
954 enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
957 * Set the RTS pin behaviour for the specified serial port.
959 * @param[in] port Pointer to a port structure. Must not be NULL.
960 * @param[in] rts RTS pin mode.
962 * @return SP_OK upon success, a negative error code otherwise.
966 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
969 * Get the RTS pin behaviour from a port configuration.
971 * The user should allocate a variable of type enum sp_rts and
972 * pass a pointer to this to receive the result.
974 * @param[in] config Pointer to a configuration structure. Must not be NULL.
975 * @param[out] rts_ptr Pointer to a variable to store the result. Must not be NULL.
977 * @return SP_OK upon success, a negative error code otherwise.
981 enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
984 * Set the RTS pin behaviour in a port configuration.
986 * @param[in] config Pointer to a configuration structure. Must not be NULL.
987 * @param[in] rts RTS pin mode, or -1 to retain the current setting.
989 * @return SP_OK upon success, a negative error code otherwise.
993 enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
996 * Set the CTS pin behaviour for the specified serial port.
998 * @param[in] port Pointer to a port structure. Must not be NULL.
999 * @param[in] cts CTS pin mode.
1001 * @return SP_OK upon success, a negative error code otherwise.
1005 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
1008 * Get the CTS pin behaviour from a port configuration.
1010 * The user should allocate a variable of type enum sp_cts and
1011 * pass a pointer to this to receive the result.
1013 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1014 * @param[out] cts_ptr Pointer to a variable to store the result. Must not be NULL.
1016 * @return SP_OK upon success, a negative error code otherwise.
1020 enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
1023 * Set the CTS pin behaviour in a port configuration.
1025 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1026 * @param[in] cts CTS pin mode, or -1 to retain the current setting.
1028 * @return SP_OK upon success, a negative error code otherwise.
1032 enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
1035 * Set the DTR pin behaviour for the specified serial port.
1037 * @param[in] port Pointer to a port structure. Must not be NULL.
1038 * @param[in] dtr DTR pin mode.
1040 * @return SP_OK upon success, a negative error code otherwise.
1044 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
1047 * Get the DTR pin behaviour from a port configuration.
1049 * The user should allocate a variable of type enum sp_dtr and
1050 * pass a pointer to this to receive the result.
1052 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1053 * @param[out] dtr_ptr Pointer to a variable to store the result. Must not be NULL.
1055 * @return SP_OK upon success, a negative error code otherwise.
1059 enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
1062 * Set the DTR pin behaviour in a port configuration.
1064 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1065 * @param[in] dtr DTR pin mode, or -1 to retain the current setting.
1067 * @return SP_OK upon success, a negative error code otherwise.
1071 enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
1074 * Set the DSR pin behaviour for the specified serial port.
1076 * @param[in] port Pointer to a port structure. Must not be NULL.
1077 * @param[in] dsr DSR pin mode.
1079 * @return SP_OK upon success, a negative error code otherwise.
1083 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
1086 * Get the DSR pin behaviour from a port configuration.
1088 * The user should allocate a variable of type enum sp_dsr and
1089 * pass a pointer to this to receive the result.
1091 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1092 * @param[out] dsr_ptr Pointer to a variable to store the result. Must not be NULL.
1094 * @return SP_OK upon success, a negative error code otherwise.
1098 enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
1101 * Set the DSR pin behaviour in a port configuration.
1103 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1104 * @param[in] dsr DSR pin mode, or -1 to retain the current setting.
1106 * @return SP_OK upon success, a negative error code otherwise.
1110 enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
1113 * Set the XON/XOFF configuration for the specified serial port.
1115 * @param[in] port Pointer to a port structure. Must not be NULL.
1116 * @param[in] xon_xoff XON/XOFF mode.
1118 * @return SP_OK upon success, a negative error code otherwise.
1122 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
1125 * Get the XON/XOFF configuration from a port configuration.
1127 * The user should allocate a variable of type enum sp_xonxoff and
1128 * pass a pointer to this to receive the result.
1130 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1131 * @param[out] xon_xoff_ptr Pointer to a variable to store the result. Must not be NULL.
1133 * @return SP_OK upon success, a negative error code otherwise.
1137 enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
1140 * Set the XON/XOFF configuration in a port configuration.
1142 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1143 * @param[in] xon_xoff XON/XOFF mode, or -1 to retain the current setting.
1145 * @return SP_OK upon success, a negative error code otherwise.
1149 enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
1152 * Set the flow control type in a port configuration.
1154 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
1155 * XON/XOFF settings as necessary for the specified flow control
1156 * type. For more fine-grained control of these settings, use their
1157 * individual configuration functions.
1159 * @param[in] config Pointer to a configuration structure. Must not be NULL.
1160 * @param[in] flowcontrol Flow control setting to use.
1162 * @return SP_OK upon success, a negative error code otherwise.
1166 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
1169 * Set the flow control type for the specified serial port.
1171 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
1172 * XON/XOFF settings as necessary for the specified flow control
1173 * type. For more fine-grained control of these settings, use their
1174 * individual configuration functions.
1176 * @param[in] port Pointer to a port structure. Must not be NULL.
1177 * @param[in] flowcontrol Flow control setting to use.
1179 * @return SP_OK upon success, a negative error code otherwise.
1183 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
1188 * @defgroup Data Data handling
1190 * Reading, writing, and flushing data.
1196 * Read bytes from the specified serial port, blocking until complete.
1198 * @warning If your program runs on Unix, defines its own signal handlers, and
1199 * needs to abort blocking reads when these are called, then you
1200 * should not use this function. It repeats system calls that return
1201 * with EINTR. To be able to abort a read from a signal handler, you
1202 * should implement your own blocking read using sp_nonblocking_read()
1203 * together with a blocking method that makes sense for your program.
1204 * E.g. you can obtain the file descriptor for an open port using
1205 * sp_get_port_handle() and use this to call select() or pselect(),
1206 * with appropriate arrangements to return if a signal is received.
1208 * @param[in] port Pointer to a port structure. Must not be NULL.
1209 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1210 * @param[in] count Requested number of bytes to read.
1211 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1213 * @return The number of bytes read on success, or a negative error code. If
1214 * the number of bytes returned is less than that requested, the
1215 * timeout was reached before the requested number of bytes was
1216 * available. If timeout is zero, the function will always return
1217 * either the requested number of bytes or a negative error code.
1221 enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
1224 * Read bytes from the specified serial port, returning as soon as any data is
1227 * @warning If your program runs on Unix, defines its own signal handlers, and
1228 * needs to abort blocking reads when these are called, then you
1229 * should not use this function. It repeats system calls that return
1230 * with EINTR. To be able to abort a read from a signal handler, you
1231 * should implement your own blocking read using sp_nonblocking_read()
1232 * together with a blocking method that makes sense for your program.
1233 * E.g. you can obtain the file descriptor for an open port using
1234 * sp_get_port_handle() and use this to call select() or pselect(),
1235 * with appropriate arrangements to return if a signal is received.
1237 * @param[in] port Pointer to a port structure. Must not be NULL.
1238 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1239 * @param[in] count Maximum number of bytes to read. Must not be zero.
1240 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1242 * @return The number of bytes read on success, or a negative error code. If
1243 * the result is zero, the timeout was reached before any bytes were
1244 * available. If timeout_ms is zero, the function will always return
1245 * either at least one byte, or a negative error code.
1249 enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
1252 * Read bytes from the specified serial port, without blocking.
1254 * @param[in] port Pointer to a port structure. Must not be NULL.
1255 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1256 * @param[in] count Maximum number of bytes to read.
1258 * @return The number of bytes read on success, or a negative error code. The
1259 * number of bytes returned may be any number from zero to the maximum
1260 * that was requested.
1264 enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
1267 * Write bytes to the specified serial port, blocking until complete.
1269 * Note that this function only ensures that the accepted bytes have been
1270 * written to the OS; they may be held in driver or hardware buffers and not
1271 * yet physically transmitted. To check whether all written bytes have actually
1272 * been transmitted, use the sp_output_waiting() function. To wait until all
1273 * written bytes have actually been transmitted, use the sp_drain() function.
1275 * @warning If your program runs on Unix, defines its own signal handlers, and
1276 * needs to abort blocking writes when these are called, then you
1277 * should not use this function. It repeats system calls that return
1278 * with EINTR. To be able to abort a write from a signal handler, you
1279 * should implement your own blocking write using sp_nonblocking_write()
1280 * together with a blocking method that makes sense for your program.
1281 * E.g. you can obtain the file descriptor for an open port using
1282 * sp_get_port_handle() and use this to call select() or pselect(),
1283 * with appropriate arrangements to return if a signal is received.
1285 * @param[in] port Pointer to a port structure. Must not be NULL.
1286 * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1287 * @param[in] count Requested number of bytes to write.
1288 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1290 * @return The number of bytes written on success, or a negative error code.
1291 * If the number of bytes returned is less than that requested, the
1292 * timeout was reached before the requested number of bytes was
1293 * written. If timeout is zero, the function will always return
1294 * either the requested number of bytes or a negative error code. In
1295 * the event of an error there is no way to determine how many bytes
1296 * were sent before the error occurred.
1300 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout_ms);
1303 * Write bytes to the specified serial port, without blocking.
1305 * Note that this function only ensures that the accepted bytes have been
1306 * written to the OS; they may be held in driver or hardware buffers and not
1307 * yet physically transmitted. To check whether all written bytes have actually
1308 * been transmitted, use the sp_output_waiting() function. To wait until all
1309 * written bytes have actually been transmitted, use the sp_drain() function.
1311 * @param[in] port Pointer to a port structure. Must not be NULL.
1312 * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1313 * @param[in] count Maximum number of bytes to write.
1315 * @return The number of bytes written on success, or a negative error code.
1316 * The number of bytes returned may be any number from zero to the
1317 * maximum that was requested.
1321 enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
1324 * Gets the number of bytes waiting in the input buffer.
1326 * @param[in] port Pointer to a port structure. Must not be NULL.
1328 * @return Number of bytes waiting on success, a negative error code otherwise.
1332 enum sp_return sp_input_waiting(struct sp_port *port);
1335 * Gets the number of bytes waiting in the output buffer.
1337 * @param[in] port Pointer to a port structure. Must not be NULL.
1339 * @return Number of bytes waiting on success, a negative error code otherwise.
1343 enum sp_return sp_output_waiting(struct sp_port *port);
1346 * Flush serial port buffers. Data in the selected buffer(s) is discarded.
1348 * @param[in] port Pointer to a port structure. Must not be NULL.
1349 * @param[in] buffers Which buffer(s) to flush.
1351 * @return SP_OK upon success, a negative error code otherwise.
1355 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
1358 * Wait for buffered data to be transmitted.
1360 * @warning If your program runs on Unix, defines its own signal handlers, and
1361 * needs to abort draining the output buffer when when these are
1362 * called, then you should not use this function. It repeats system
1363 * calls that return with EINTR. To be able to abort a drain from a
1364 * signal handler, you would need to implement your own blocking
1365 * drain by polling the result of sp_output_waiting().
1367 * @param[in] port Pointer to a port structure. Must not be NULL.
1369 * @return SP_OK upon success, a negative error code otherwise.
1373 enum sp_return sp_drain(struct sp_port *port);
1378 * @defgroup Waiting Waiting
1380 * Waiting for events and timeout handling.
1386 * Allocate storage for a set of events.
1388 * The user should allocate a variable of type struct sp_event_set *,
1389 * then pass a pointer to this variable to receive the result.
1391 * The result should be freed after use by calling sp_free_event_set().
1393 * @param[out] result_ptr If any error is returned, the variable pointed to by
1394 * result_ptr will be set to NULL. Otherwise, it will
1395 * be set to point to the event set. Must not be NULL.
1397 * @return SP_OK upon success, a negative error code otherwise.
1401 enum sp_return sp_new_event_set(struct sp_event_set **result_ptr);
1404 * Add events to a struct sp_event_set for a given port.
1406 * The port must first be opened by calling sp_open() using the same port
1409 * After the port is closed or the port structure freed, the results may
1410 * no longer be valid.
1412 * @param[in,out] event_set Event set to update. Must not be NULL.
1413 * @param[in] port Pointer to a port structure. Must not be NULL.
1414 * @param[in] mask Bitmask of events to be waited for.
1416 * @return SP_OK upon success, a negative error code otherwise.
1420 enum sp_return sp_add_port_events(struct sp_event_set *event_set,
1421 const struct sp_port *port, enum sp_event mask);
1424 * Wait for any of a set of events to occur.
1426 * @param[in] event_set Event set to wait on. Must not be NULL.
1427 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1429 * @return SP_OK upon success, a negative error code otherwise.
1433 enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout_ms);
1436 * Free a structure allocated by sp_new_event_set().
1438 * @param[in] event_set Event set to free. Must not be NULL.
1442 void sp_free_event_set(struct sp_event_set *event_set);
1447 * @defgroup Signals Signals
1449 * Port signalling operations.
1455 * Gets the status of the control signals for the specified port.
1457 * The user should allocate a variable of type "enum sp_signal" and pass a
1458 * pointer to this variable to receive the result. The result is a bitmask
1459 * in which individual signals can be checked by bitwise OR with values of
1460 * the sp_signal enum.
1462 * @param[in] port Pointer to a port structure. Must not be NULL.
1463 * @param[out] signal_mask Pointer to a variable to receive the result.
1466 * @return SP_OK upon success, a negative error code otherwise.
1470 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signal_mask);
1473 * Put the port transmit line into the break state.
1475 * @param[in] port Pointer to a port structure. Must not be NULL.
1477 * @return SP_OK upon success, a negative error code otherwise.
1481 enum sp_return sp_start_break(struct sp_port *port);
1484 * Take the port transmit line out of the break state.
1486 * @param[in] port Pointer to a port structure. Must not be NULL.
1488 * @return SP_OK upon success, a negative error code otherwise.
1492 enum sp_return sp_end_break(struct sp_port *port);
1497 * @defgroup Errors Errors
1499 * Obtaining error information.
1505 * Get the error code for a failed operation.
1507 * In order to obtain the correct result, this function should be called
1508 * straight after the failure, before executing any other system operations.
1509 * The result is thread-specific, and only valid when called immediately
1510 * after a previous call returning SP_ERR_FAIL.
1512 * @return The system's numeric code for the error that caused the last
1513 * operation to fail.
1517 int sp_last_error_code(void);
1520 * Get the error message for a failed operation.
1522 * In order to obtain the correct result, this function should be called
1523 * straight after the failure, before executing other system operations.
1524 * The result is thread-specific, and only valid when called immediately
1525 * after a previous call returning SP_ERR_FAIL.
1527 * @return The system's message for the error that caused the last
1528 * operation to fail. This string may be allocated by the function,
1529 * and should be freed after use by calling sp_free_error_message().
1533 char *sp_last_error_message(void);
1536 * Free an error message returned by sp_last_error_message().
1538 * @param[in] message The error message string to free. Must not be NULL.
1542 void sp_free_error_message(char *message);
1545 * Set the handler function for library debugging messages.
1547 * Debugging messages are generated by the library during each operation,
1548 * to help in diagnosing problems. The handler will be called for each
1549 * message. The handler can be set to NULL to ignore all debug messages.
1551 * The handler function should accept a format string and variable length
1552 * argument list, in the same manner as e.g. printf().
1554 * The default handler is sp_default_debug_handler().
1556 * @param[in] handler The handler function to use. Can be NULL (in that case
1557 * all debug messages will be ignored).
1561 void sp_set_debug_handler(void (*handler)(const char *format, ...));
1564 * Default handler function for library debugging messages.
1566 * This function prints debug messages to the standard error stream if the
1567 * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
1570 * @param[in] format The format string to use. Must not be NULL.
1571 * @param[in] ... The variable length argument list to use.
1575 void sp_default_debug_handler(const char *format, ...);
1580 * @defgroup Versions Versions
1582 * Version number querying functions, definitions, and macros.
1584 * This set of API calls returns two different version numbers related
1585 * to libserialport. The "package version" is the release version number of the
1586 * libserialport tarball in the usual "major.minor.micro" format, e.g. "0.1.0".
1588 * The "library version" is independent of that; it is the libtool version
1589 * number in the "current:revision:age" format, e.g. "2:0:0".
1590 * See http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning for details.
1592 * Both version numbers (and/or individual components of them) can be
1593 * retrieved via the API calls at runtime, and/or they can be checked at
1594 * compile/preprocessor time using the respective macros.
1600 * Package version macros (can be used for conditional compilation).
1603 /** The libserialport package 'major' version number. */
1604 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
1606 /** The libserialport package 'minor' version number. */
1607 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
1609 /** The libserialport package 'micro' version number. */
1610 #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
1612 /** The libserialport package version ("major.minor.micro") as string. */
1613 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
1616 * Library/libtool version macros (can be used for conditional compilation).
1619 /** The libserialport libtool 'current' version number. */
1620 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
1622 /** The libserialport libtool 'revision' version number. */
1623 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
1625 /** The libserialport libtool 'age' version number. */
1626 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
1628 /** The libserialport libtool version ("current:revision:age") as string. */
1629 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
1632 * Get the major libserialport package version number.
1634 * @return The major package version number.
1638 int sp_get_major_package_version(void);
1641 * Get the minor libserialport package version number.
1643 * @return The minor package version number.
1647 int sp_get_minor_package_version(void);
1650 * Get the micro libserialport package version number.
1652 * @return The micro package version number.
1656 int sp_get_micro_package_version(void);
1659 * Get the libserialport package version number as a string.
1661 * @return The package version number string. The returned string is
1662 * static and thus should NOT be free'd by the caller.
1666 const char *sp_get_package_version_string(void);
1669 * Get the "current" part of the libserialport library version number.
1671 * @return The "current" library version number.
1675 int sp_get_current_lib_version(void);
1678 * Get the "revision" part of the libserialport library version number.
1680 * @return The "revision" library version number.
1684 int sp_get_revision_lib_version(void);
1687 * Get the "age" part of the libserialport library version number.
1689 * @return The "age" library version number.
1693 int sp_get_age_lib_version(void);
1696 * Get the libserialport library version number as a string.
1698 * @return The library version number string. The returned string is
1699 * static and thus should NOT be free'd by the caller.
1703 const char *sp_get_lib_version_string(void);