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 * The operations that are supported are:
34 * - @ref Enumeration (obtaining a list of serial ports on the system)
36 * - @ref Configuration (baud rate, parity, etc.)
37 * - @ref Signals (modem control lines, breaks, etc.)
42 * libserialport is an open source project released under the LGPL3+ license.
47 * The API is simple, and designed to be a minimal wrapper around the serial
48 * port support in each OS.
50 * Most functions take a pointer to a struct sp_port, which represents a serial
51 * port. These structures are always allocated and freed by the library, using
52 * the functions in the @ref Enumeration "Enumeration" section.
54 * Most functions have return type @ref sp_return and can return only four
55 * possible error values:
57 * - @ref SP_ERR_ARG means that a function was called with invalid
58 * arguments. This implies a bug in the caller. The arguments passed would
59 * be invalid regardless of the underlying OS or serial device involved.
61 * - @ref SP_ERR_FAIL means that the OS reported a failure. The error code or
62 * message provided by the OS can be obtained by calling sp_last_error_code()
63 * or sp_last_error_message().
65 * - @ref SP_ERR_SUPP indicates that there is no support for the requested
66 * operation in the current OS, driver or device. No error message is
67 * available from the OS in this case. There is either no way to request
68 * the operation in the first place, or libserialport does not know how to
69 * do so in the current version.
71 * - @ref SP_ERR_MEM indicates that a memory allocation failed.
73 * All of these error values are negative.
75 * Calls that succeed return @ref SP_OK, which is equal to zero. Some functions
76 * declared @ref sp_return can also return a positive value for a successful
77 * numeric result, e.g. sp_blocking_read() or sp_blocking_write().
80 #ifndef LIBSERIALPORT_LIBSERIALPORT_H
81 #define LIBSERIALPORT_LIBSERIALPORT_H
91 /** Operation completed successfully. */
93 /** Invalid arguments were passed to the function. */
95 /** A system error occurred while executing the operation. */
97 /** A memory allocation failed while executing the operation. */
99 /** The requested operation is not supported by this system or device. */
103 /** Port access modes. */
105 /** Open port for read access. */
107 /** Open port for write access. */
109 /** Open port for read and write access. @since 0.1.1 */
110 SP_MODE_READ_WRITE = 3
115 /** Data received and ready to read. */
116 SP_EVENT_RX_READY = 1,
117 /** Ready to transmit new data. */
118 SP_EVENT_TX_READY = 2,
119 /** Error occurred. */
123 /** Buffer selection. */
127 /** Output buffer. */
133 /** Parity settings. */
135 /** Special value to indicate setting should be left alone. */
136 SP_PARITY_INVALID = -1,
149 /** RTS pin behaviour. */
151 /** Special value to indicate setting should be left alone. */
157 /** RTS used for flow control. */
158 SP_RTS_FLOW_CONTROL = 2
161 /** CTS pin behaviour. */
163 /** Special value to indicate setting should be left alone. */
167 /** CTS used for flow control. */
168 SP_CTS_FLOW_CONTROL = 1
171 /** DTR pin behaviour. */
173 /** Special value to indicate setting should be left alone. */
179 /** DTR used for flow control. */
180 SP_DTR_FLOW_CONTROL = 2
183 /** DSR pin behaviour. */
185 /** Special value to indicate setting should be left alone. */
189 /** DSR used for flow control. */
190 SP_DSR_FLOW_CONTROL = 1
193 /** XON/XOFF flow control behaviour. */
195 /** Special value to indicate setting should be left alone. */
196 SP_XONXOFF_INVALID = -1,
197 /** XON/XOFF disabled. */
198 SP_XONXOFF_DISABLED = 0,
199 /** XON/XOFF enabled for input only. */
201 /** XON/XOFF enabled for output only. */
203 /** XON/XOFF enabled for input and output. */
207 /** Standard flow control combinations. */
208 enum sp_flowcontrol {
209 /** No flow control. */
210 SP_FLOWCONTROL_NONE = 0,
211 /** Software flow control using XON/XOFF characters. */
212 SP_FLOWCONTROL_XONXOFF = 1,
213 /** Hardware flow control using RTS/CTS signals. */
214 SP_FLOWCONTROL_RTSCTS = 2,
215 /** Hardware flow control using DTR/DSR signals. */
216 SP_FLOWCONTROL_DTRDSR = 3
219 /** Input signals. */
221 /** Clear to send. */
223 /** Data set ready. */
225 /** Data carrier detect. */
227 /** Ring indicator. */
237 /** Native platform serial port. @since 0.1.1 */
239 /** USB serial port adapter. @since 0.1.1 */
241 /** Bluetooth serial port adapter. @since 0.1.1 */
242 SP_TRANSPORT_BLUETOOTH
247 * An opaque structure representing a serial port.
252 * @struct sp_port_config
253 * An opaque structure representing the configuration for a serial port.
255 struct sp_port_config;
258 * @struct sp_event_set
259 * A set of handles to wait on for events.
261 struct sp_event_set {
262 /** Array of OS-specific handles. */
264 /** Array of bitmasks indicating which events apply for each handle. */
265 enum sp_event *masks;
266 /** Number of handles. */
271 * @defgroup Enumeration Port enumeration
273 * Enumerating the serial ports of a system.
279 * Obtain a pointer to a new sp_port structure representing the named port.
281 * The user should allocate a variable of type "struct sp_port *" and pass a
282 * pointer to this to receive the result.
284 * The result should be freed after use by calling sp_free_port().
286 * @param[in] portname The OS-specific name of a serial port. Must not be NULL.
287 * @param[out] port_ptr If any error is returned, the variable pointed to by
288 * port_ptr will be set to NULL. Otherwise, it will be set
289 * to point to the newly allocated port. Must not be NULL.
291 * @return SP_OK upon success, a negative error code otherwise.
295 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
298 * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
300 * @param[in] port Pointer to a port structure. Must not be NULL.
304 void sp_free_port(struct sp_port *port);
307 * List the serial ports available on the system.
309 * The result obtained is an array of pointers to sp_port structures,
310 * terminated by a NULL. The user should allocate a variable of type
311 * "struct sp_port **" and pass a pointer to this to receive the result.
313 * The result should be freed after use by calling sp_free_port_list().
314 * If a port from the list is to be used after freeing the list, it must be
315 * copied first using sp_copy_port().
317 * @param[out] list_ptr If any error is returned, the variable pointed to by
318 * list_ptr will be set to NULL. Otherwise, it will be set
319 * to point to the newly allocated array. Must not be NULL.
321 * @return SP_OK upon success, a negative error code otherwise.
325 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
328 * Make a new copy of an sp_port structure.
330 * The user should allocate a variable of type "struct sp_port *" and pass a
331 * pointer to this to receive the result.
333 * The copy should be freed after use by calling sp_free_port().
335 * @param[in] port Pointer to a port structure. Must not be NULL.
336 * @param[out] copy_ptr If any error is returned, the variable pointed to by
337 * copy_ptr will be set to NULL. Otherwise, it will be set
338 * to point to the newly allocated copy. Must not be NULL.
340 * @return SP_OK upon success, a negative error code otherwise.
344 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
347 * Free a port list obtained from sp_list_ports().
349 * This will also free all the sp_port structures referred to from the list;
350 * any that are to be retained must be copied first using sp_copy_port().
352 * @param[in] ports Pointer to a list of port structures. Must not be NULL.
356 void sp_free_port_list(struct sp_port **ports);
360 * @defgroup Ports Port handling
362 * Opening, closing and querying ports.
368 * Open the specified serial port.
370 * @param[in] port Pointer to a port structure. Must not be NULL.
371 * @param[in] flags Flags to use when opening the serial port.
373 * @return SP_OK upon success, a negative error code otherwise.
377 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
380 * Close the specified serial port.
382 * @param[in] port Pointer to a port structure. Must not be NULL.
384 * @return SP_OK upon success, a negative error code otherwise.
388 enum sp_return sp_close(struct sp_port *port);
391 * Get the name of a port.
393 * The name returned is whatever is normally used to refer to a port on the
394 * current operating system; e.g. for Windows it will usually be a "COMn"
395 * device name, and for Unix it will be a device path beginning with "/dev/".
397 * @param[in] port Pointer to a port structure. Must not be NULL.
399 * @return The port name, or NULL if an invalid port is passed. The name
400 * string is part of the port structure and may not be used after
401 * the port structure has been freed.
405 char *sp_get_port_name(const struct sp_port *port);
408 * Get a description for a port, to present to end user.
410 * @param[in] port Pointer to a port structure. Must not be NULL.
412 * @return The port description, or NULL if an invalid port is passed.
413 * The description string is part of the port structure and may not
414 * be used after the port structure has been freed.
418 char *sp_get_port_description(const struct sp_port *port);
421 * Get the transport type used by a port.
423 * @param[in] port Pointer to a port structure. Must not be NULL.
425 * @return The port transport type.
429 enum sp_transport sp_get_port_transport(const struct sp_port *port);
432 * Get the USB bus number and address on bus of a USB serial adapter port.
434 * @param[in] port Pointer to a port structure. Must not be NULL.
435 * @param[out] usb_bus Pointer to a variable to store the USB bus.
436 * Can be NULL (in that case it will be ignored).
437 * @param[out] usb_address Pointer to a variable to store the USB address.
438 * Can be NULL (in that case it will be ignored).
440 * @return SP_OK upon success, a negative error code otherwise.
444 enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port,
445 int *usb_bus, int *usb_address);
448 * Get the USB Vendor ID and Product ID of a USB serial adapter port.
450 * @param[in] port Pointer to a port structure. Must not be NULL.
451 * @param[out] usb_vid Pointer to a variable to store the USB VID.
452 * Can be NULL (in that case it will be ignored).
453 * @param[out] usb_pid Pointer to a variable to store the USB PID.
454 * Can be NULL (in that case it will be ignored).
456 * @return SP_OK upon success, a negative error code otherwise.
460 enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, int *usb_vid, int *usb_pid);
463 * Get the USB manufacturer string of a USB serial adapter port.
465 * @param[in] port Pointer to a port structure. Must not be NULL.
467 * @return The port manufacturer string, or NULL if an invalid port is passed.
468 * The manufacturer string is part of the port structure and may not
469 * be used after the port structure has been freed.
473 char *sp_get_port_usb_manufacturer(const struct sp_port *port);
476 * Get the USB product string of a USB serial adapter port.
478 * @param[in] port Pointer to a port structure. Must not be NULL.
480 * @return The port product string, or NULL if an invalid port is passed.
481 * The product string is part of the port structure and may not be
482 * used after the port structure has been freed.
486 char *sp_get_port_usb_product(const struct sp_port *port);
489 * Get the USB serial number string of a USB serial adapter port.
491 * @param[in] port Pointer to a port structure. Must not be NULL.
493 * @return The port serial number, or NULL if an invalid port is passed.
494 * The serial number string is part of the port structure and may
495 * not be used after the port structure has been freed.
499 char *sp_get_port_usb_serial(const struct sp_port *port);
502 * Get the MAC address of a Bluetooth serial adapter port.
504 * @param[in] port Pointer to a port structure. Must not be NULL.
506 * @return The port MAC address, or NULL if an invalid port is passed.
507 * The MAC address string is part of the port structure and may not
508 * be used after the port structure has been freed.
512 char *sp_get_port_bluetooth_address(const struct sp_port *port);
515 * Get the operating system handle for a port.
517 * The type of the handle depends on the operating system. On Unix based
518 * systems, the handle is a file descriptor of type "int". On Windows, the
519 * handle is of type "HANDLE". The user should allocate a variable of the
520 * appropriate type and pass a pointer to this to receive the result.
522 * To obtain a valid handle, the port must first be opened by calling
523 * sp_open() using the same port structure.
525 * After the port is closed or the port structure freed, the handle may
526 * no longer be valid.
528 * @warning This feature is provided so that programs may make use of
529 * OS-specific functionality where desired. Doing so obviously
530 * comes at a cost in portability. It also cannot be guaranteed
531 * that direct usage of the OS handle will not conflict with the
532 * library's own usage of the port. Be careful.
534 * @param[in] port Pointer to a port structure. Must not be NULL.
535 * @param[out] result_ptr If any error is returned, the variable pointed to by
536 * result_ptr will have unknown contents and should not
537 * be used. Otherwise, it will be set to point to the
538 * OS handle. Must not be NULL.
540 * @return SP_OK upon success, a negative error code otherwise.
544 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
549 * @defgroup Configuration Configuration
551 * Setting and querying serial port parameters.
556 * Allocate a port configuration structure.
558 * The user should allocate a variable of type "struct sp_port_config *" and
559 * pass a pointer to this to receive the result. The variable will be updated
560 * to point to the new configuration structure. The structure is opaque and
561 * must be accessed via the functions provided.
563 * All parameters in the structure will be initialised to special values which
564 * are ignored by sp_set_config().
566 * The structure should be freed after use by calling sp_free_config().
568 * @param[out] config_ptr If any error is returned, the variable pointed to by
569 * config_ptr will be set to NULL. Otherwise, it will
570 * be set to point to the allocated config structure.
573 * @return SP_OK upon success, a negative error code otherwise.
577 enum sp_return sp_new_config(struct sp_port_config **config_ptr);
580 * Free a port configuration structure.
582 * @param[in] config Pointer to a configuration structure. Must not be NULL.
586 void sp_free_config(struct sp_port_config *config);
589 * Get the current configuration of the specified serial port.
591 * The user should allocate a configuration structure using sp_new_config()
592 * and pass this as the config parameter. The configuration structure will
593 * be updated with the port configuration.
595 * Any parameters that are configured with settings not recognised or
596 * supported by libserialport, will be set to special values that are
597 * ignored by sp_set_config().
599 * @param[in] port Pointer to a port structure. Must not be NULL.
600 * @param[out] config Pointer to a configuration structure that will hold
601 * the result. Upon errors the contents of the config
602 * struct will not be changed. Must not be NULL.
604 * @return SP_OK upon success, a negative error code otherwise.
608 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
611 * Set the configuration for the specified serial port.
613 * For each parameter in the configuration, there is a special value (usually
614 * -1, but see the documentation for each field). These values will be ignored
615 * and the corresponding setting left unchanged on the port.
617 * Upon errors, the configuration of the serial port is unknown since
618 * partial/incomplete config updates may have happened.
620 * @param[in] port Pointer to a port structure. Must not be NULL.
621 * @param[in] config Pointer to a configuration structure. Must not be NULL.
623 * @return SP_OK upon success, a negative error code otherwise.
627 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
630 * Set the baud rate for the specified serial port.
632 * @param[in] port Pointer to a port structure. Must not be NULL.
633 * @param[in] baudrate Baud rate in bits per second.
635 * @return SP_OK upon success, a negative error code otherwise.
639 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
642 * Get the baud rate from a port configuration.
644 * The user should allocate a variable of type int and
645 * pass a pointer to this to receive the result.
647 * @param[in] config Pointer to a configuration structure. Must not be NULL.
648 * @param[out] baudrate_ptr Pointer to a variable to store the result. Must not be NULL.
650 * @return SP_OK upon success, a negative error code otherwise.
654 enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
657 * Set the baud rate in a port configuration.
659 * @param[in] config Pointer to a configuration structure. Must not be NULL.
660 * @param[in] baudrate Baud rate in bits per second, or -1 to retain the current setting.
662 * @return SP_OK upon success, a negative error code otherwise.
666 enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
669 * Set the data bits for the specified serial port.
671 * @param[in] port Pointer to a port structure. Must not be NULL.
672 * @param[in] bits Number of data bits.
674 * @return SP_OK upon success, a negative error code otherwise.
678 enum sp_return sp_set_bits(struct sp_port *port, int bits);
681 * Get the data bits from a port configuration.
683 * The user should allocate a variable of type int and
684 * pass a pointer to this to receive the result.
686 * @param[in] config Pointer to a configuration structure. Must not be NULL.
687 * @param[out] bits_ptr Pointer to a variable to store the result. Must not be NULL.
689 * @return SP_OK upon success, a negative error code otherwise.
693 enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
696 * Set the data bits in a port configuration.
698 * @param[in] config Pointer to a configuration structure. Must not be NULL.
699 * @param[in] bits Number of data bits, or -1 to retain the current setting.
701 * @return SP_OK upon success, a negative error code otherwise.
705 enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
708 * Set the parity setting for the specified serial port.
710 * @param[in] port Pointer to a port structure. Must not be NULL.
711 * @param[in] parity Parity setting.
713 * @return SP_OK upon success, a negative error code otherwise.
717 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
720 * Get the parity setting from a port configuration.
722 * The user should allocate a variable of type enum sp_parity and
723 * pass a pointer to this to receive the result.
725 * @param[in] config Pointer to a configuration structure. Must not be NULL.
726 * @param[out] parity_ptr Pointer to a variable to store the result. Must not be NULL.
728 * @return SP_OK upon success, a negative error code otherwise.
732 enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
735 * Set the parity setting in a port configuration.
737 * @param[in] config Pointer to a configuration structure. Must not be NULL.
738 * @param[in] parity Parity setting, or -1 to retain the current setting.
740 * @return SP_OK upon success, a negative error code otherwise.
744 enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
747 * Set the stop bits for the specified serial port.
749 * @param[in] port Pointer to a port structure. Must not be NULL.
750 * @param[in] stopbits Number of stop bits.
752 * @return SP_OK upon success, a negative error code otherwise.
756 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
759 * Get the stop bits from a port configuration.
761 * The user should allocate a variable of type int and
762 * pass a pointer to this to receive the result.
764 * @param[in] config Pointer to a configuration structure. Must not be NULL.
765 * @param[out] stopbits_ptr Pointer to a variable to store the result. Must not be NULL.
767 * @return SP_OK upon success, a negative error code otherwise.
771 enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
774 * Set the stop bits in a port configuration.
776 * @param[in] config Pointer to a configuration structure. Must not be NULL.
777 * @param[in] stopbits Number of stop bits, or -1 to retain the current setting.
779 * @return SP_OK upon success, a negative error code otherwise.
783 enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
786 * Set the RTS pin behaviour for the specified serial port.
788 * @param[in] port Pointer to a port structure. Must not be NULL.
789 * @param[in] rts RTS pin mode.
791 * @return SP_OK upon success, a negative error code otherwise.
795 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
798 * Get the RTS pin behaviour from a port configuration.
800 * The user should allocate a variable of type enum sp_rts and
801 * pass a pointer to this to receive the result.
803 * @param[in] config Pointer to a configuration structure. Must not be NULL.
804 * @param[out] rts_ptr Pointer to a variable to store the result. Must not be NULL.
806 * @return SP_OK upon success, a negative error code otherwise.
810 enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
813 * Set the RTS pin behaviour in a port configuration.
815 * @param[in] config Pointer to a configuration structure. Must not be NULL.
816 * @param[in] rts RTS pin mode, or -1 to retain the current setting.
818 * @return SP_OK upon success, a negative error code otherwise.
822 enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
825 * Set the CTS pin behaviour for the specified serial port.
827 * @param[in] port Pointer to a port structure. Must not be NULL.
828 * @param[in] cts CTS pin mode.
830 * @return SP_OK upon success, a negative error code otherwise.
834 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
837 * Get the CTS pin behaviour from a port configuration.
839 * The user should allocate a variable of type enum sp_cts and
840 * pass a pointer to this to receive the result.
842 * @param[in] config Pointer to a configuration structure. Must not be NULL.
843 * @param[out] cts_ptr Pointer to a variable to store the result. Must not be NULL.
845 * @return SP_OK upon success, a negative error code otherwise.
849 enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
852 * Set the CTS pin behaviour in a port configuration.
854 * @param[in] config Pointer to a configuration structure. Must not be NULL.
855 * @param[in] cts CTS pin mode, or -1 to retain the current setting.
857 * @return SP_OK upon success, a negative error code otherwise.
861 enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
864 * Set the DTR pin behaviour for the specified serial port.
866 * @param[in] port Pointer to a port structure. Must not be NULL.
867 * @param[in] dtr DTR pin mode.
869 * @return SP_OK upon success, a negative error code otherwise.
873 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
876 * Get the DTR pin behaviour from a port configuration.
878 * The user should allocate a variable of type enum sp_dtr and
879 * pass a pointer to this to receive the result.
881 * @param[in] config Pointer to a configuration structure. Must not be NULL.
882 * @param[out] dtr_ptr Pointer to a variable to store the result. Must not be NULL.
884 * @return SP_OK upon success, a negative error code otherwise.
888 enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
891 * Set the DTR pin behaviour in a port configuration.
893 * @param[in] config Pointer to a configuration structure. Must not be NULL.
894 * @param[in] dtr DTR pin mode, or -1 to retain the current setting.
896 * @return SP_OK upon success, a negative error code otherwise.
900 enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
903 * Set the DSR pin behaviour for the specified serial port.
905 * @param[in] port Pointer to a port structure. Must not be NULL.
906 * @param[in] dsr DSR pin mode.
908 * @return SP_OK upon success, a negative error code otherwise.
912 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
915 * Get the DSR pin behaviour from a port configuration.
917 * The user should allocate a variable of type enum sp_dsr and
918 * pass a pointer to this to receive the result.
920 * @param[in] config Pointer to a configuration structure. Must not be NULL.
921 * @param[out] dsr_ptr Pointer to a variable to store the result. Must not be NULL.
923 * @return SP_OK upon success, a negative error code otherwise.
927 enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
930 * Set the DSR pin behaviour in a port configuration.
932 * @param[in] config Pointer to a configuration structure. Must not be NULL.
933 * @param[in] dsr DSR pin mode, or -1 to retain the current setting.
935 * @return SP_OK upon success, a negative error code otherwise.
939 enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
942 * Set the XON/XOFF configuration for the specified serial port.
944 * @param[in] port Pointer to a port structure. Must not be NULL.
945 * @param[in] xon_xoff XON/XOFF mode.
947 * @return SP_OK upon success, a negative error code otherwise.
951 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
954 * Get the XON/XOFF configuration from a port configuration.
956 * The user should allocate a variable of type enum sp_xonxoff and
957 * pass a pointer to this to receive the result.
959 * @param[in] config Pointer to a configuration structure. Must not be NULL.
960 * @param[out] xon_xoff_ptr Pointer to a variable to store the result. Must not be NULL.
962 * @return SP_OK upon success, a negative error code otherwise.
966 enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
969 * Set the XON/XOFF configuration in a port configuration.
971 * @param[in] config Pointer to a configuration structure. Must not be NULL.
972 * @param[in] xon_xoff XON/XOFF mode, or -1 to retain the current setting.
974 * @return SP_OK upon success, a negative error code otherwise.
978 enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
981 * Set the flow control type in a port configuration.
983 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
984 * XON/XOFF settings as necessary for the specified flow control
985 * type. For more fine-grained control of these settings, use their
986 * individual configuration functions.
988 * @param[in] config Pointer to a configuration structure. Must not be NULL.
989 * @param[in] flowcontrol Flow control setting to use.
991 * @return SP_OK upon success, a negative error code otherwise.
995 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
998 * Set the flow control type for the specified serial port.
1000 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
1001 * XON/XOFF settings as necessary for the specified flow control
1002 * type. For more fine-grained control of these settings, use their
1003 * individual configuration functions.
1005 * @param[in] port Pointer to a port structure. Must not be NULL.
1006 * @param[in] flowcontrol Flow control setting to use.
1008 * @return SP_OK upon success, a negative error code otherwise.
1012 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
1017 * @defgroup Data Data handling
1019 * Reading, writing, and flushing data.
1025 * Read bytes from the specified serial port, blocking until complete.
1027 * @warning If your program runs on Unix, defines its own signal handlers, and
1028 * needs to abort blocking reads when these are called, then you
1029 * should not use this function. It repeats system calls that return
1030 * with EINTR. To be able to abort a read from a signal handler, you
1031 * should implement your own blocking read using sp_nonblocking_read()
1032 * together with a blocking method that makes sense for your program.
1033 * E.g. you can obtain the file descriptor for an open port using
1034 * sp_get_port_handle() and use this to call select() or pselect(),
1035 * with appropriate arrangements to return if a signal is received.
1037 * @param[in] port Pointer to a port structure. Must not be NULL.
1038 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1039 * @param[in] count Requested number of bytes to read.
1040 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1042 * @return The number of bytes read on success, or a negative error code. If
1043 * the number of bytes returned is less than that requested, the
1044 * timeout was reached before the requested number of bytes was
1045 * available. If timeout is zero, the function will always return
1046 * either the requested number of bytes or a negative error code.
1050 enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
1053 * Read bytes from the specified serial port, returning as soon as any data is
1056 * @warning If your program runs on Unix, defines its own signal handlers, and
1057 * needs to abort blocking reads when these are called, then you
1058 * should not use this function. It repeats system calls that return
1059 * with EINTR. To be able to abort a read from a signal handler, you
1060 * should implement your own blocking read using sp_nonblocking_read()
1061 * together with a blocking method that makes sense for your program.
1062 * E.g. you can obtain the file descriptor for an open port using
1063 * sp_get_port_handle() and use this to call select() or pselect(),
1064 * with appropriate arrangements to return if a signal is received.
1066 * @param[in] port Pointer to a port structure. Must not be NULL.
1067 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1068 * @param[in] count Maximum number of bytes to read. Must not be zero.
1069 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1071 * @return The number of bytes read on success, or a negative error code. If
1072 * the result is zero, the timeout was reached before any bytes were
1073 * available. If timeout_ms is zero, the function will always return
1074 * either at least one byte, or a negative error code.
1078 enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
1081 * Read bytes from the specified serial port, without blocking.
1083 * @param[in] port Pointer to a port structure. Must not be NULL.
1084 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1085 * @param[in] count Maximum number of bytes to read.
1087 * @return The number of bytes read on success, or a negative error code. The
1088 * number of bytes returned may be any number from zero to the maximum
1089 * that was requested.
1093 enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
1096 * Write bytes to the specified serial port, blocking until complete.
1098 * Note that this function only ensures that the accepted bytes have been
1099 * written to the OS; they may be held in driver or hardware buffers and not
1100 * yet physically transmitted. To check whether all written bytes have actually
1101 * been transmitted, use the sp_output_waiting() function. To wait until all
1102 * written bytes have actually been transmitted, use the sp_drain() function.
1104 * @warning If your program runs on Unix, defines its own signal handlers, and
1105 * needs to abort blocking writes when these are called, then you
1106 * should not use this function. It repeats system calls that return
1107 * with EINTR. To be able to abort a write from a signal handler, you
1108 * should implement your own blocking write using sp_nonblocking_write()
1109 * together with a blocking method that makes sense for your program.
1110 * E.g. you can obtain the file descriptor for an open port using
1111 * sp_get_port_handle() and use this to call select() or pselect(),
1112 * with appropriate arrangements to return if a signal is received.
1114 * @param[in] port Pointer to a port structure. Must not be NULL.
1115 * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1116 * @param[in] count Requested number of bytes to write.
1117 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1119 * @return The number of bytes written on success, or a negative error code.
1120 * If the number of bytes returned is less than that requested, the
1121 * timeout was reached before the requested number of bytes was
1122 * written. If timeout is zero, the function will always return
1123 * either the requested number of bytes or a negative error code. In
1124 * the event of an error there is no way to determine how many bytes
1125 * were sent before the error occurred.
1129 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout_ms);
1132 * Write bytes to the specified serial port, without blocking.
1134 * Note that this function only ensures that the accepted bytes have been
1135 * written to the OS; they may be held in driver or hardware buffers and not
1136 * yet physically transmitted. To check whether all written bytes have actually
1137 * been transmitted, use the sp_output_waiting() function. To wait until all
1138 * written bytes have actually been transmitted, use the sp_drain() function.
1140 * @param[in] port Pointer to a port structure. Must not be NULL.
1141 * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1142 * @param[in] count Maximum number of bytes to write.
1144 * @return The number of bytes written on success, or a negative error code.
1145 * The number of bytes returned may be any number from zero to the
1146 * maximum that was requested.
1150 enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
1153 * Gets the number of bytes waiting in the input buffer.
1155 * @param[in] port Pointer to a port structure. Must not be NULL.
1157 * @return Number of bytes waiting on success, a negative error code otherwise.
1161 enum sp_return sp_input_waiting(struct sp_port *port);
1164 * Gets the number of bytes waiting in the output buffer.
1166 * @param[in] port Pointer to a port structure. Must not be NULL.
1168 * @return Number of bytes waiting on success, a negative error code otherwise.
1172 enum sp_return sp_output_waiting(struct sp_port *port);
1175 * Flush serial port buffers. Data in the selected buffer(s) is discarded.
1177 * @param[in] port Pointer to a port structure. Must not be NULL.
1178 * @param[in] buffers Which buffer(s) to flush.
1180 * @return SP_OK upon success, a negative error code otherwise.
1184 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
1187 * Wait for buffered data to be transmitted.
1189 * @warning If your program runs on Unix, defines its own signal handlers, and
1190 * needs to abort draining the output buffer when when these are
1191 * called, then you should not use this function. It repeats system
1192 * calls that return with EINTR. To be able to abort a drain from a
1193 * signal handler, you would need to implement your own blocking
1194 * drain by polling the result of sp_output_waiting().
1196 * @param[in] port Pointer to a port structure. Must not be NULL.
1198 * @return SP_OK upon success, a negative error code otherwise.
1202 enum sp_return sp_drain(struct sp_port *port);
1207 * @defgroup Waiting Waiting
1209 * Waiting for events and timeout handling.
1215 * Allocate storage for a set of events.
1217 * The user should allocate a variable of type struct sp_event_set *,
1218 * then pass a pointer to this variable to receive the result.
1220 * The result should be freed after use by calling sp_free_event_set().
1222 * @param[out] result_ptr If any error is returned, the variable pointed to by
1223 * result_ptr will be set to NULL. Otherwise, it will
1224 * be set to point to the event set. Must not be NULL.
1226 * @return SP_OK upon success, a negative error code otherwise.
1230 enum sp_return sp_new_event_set(struct sp_event_set **result_ptr);
1233 * Add events to a struct sp_event_set for a given port.
1235 * The port must first be opened by calling sp_open() using the same port
1238 * After the port is closed or the port structure freed, the results may
1239 * no longer be valid.
1241 * @param[in,out] event_set Event set to update. Must not be NULL.
1242 * @param[in] port Pointer to a port structure. Must not be NULL.
1243 * @param[in] mask Bitmask of events to be waited for.
1245 * @return SP_OK upon success, a negative error code otherwise.
1249 enum sp_return sp_add_port_events(struct sp_event_set *event_set,
1250 const struct sp_port *port, enum sp_event mask);
1253 * Wait for any of a set of events to occur.
1255 * @param[in] event_set Event set to wait on. Must not be NULL.
1256 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1258 * @return SP_OK upon success, a negative error code otherwise.
1262 enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout_ms);
1265 * Free a structure allocated by sp_new_event_set().
1267 * @param[in] event_set Event set to free. Must not be NULL.
1271 void sp_free_event_set(struct sp_event_set *event_set);
1276 * @defgroup Signals Signals
1278 * Port signalling operations.
1284 * Gets the status of the control signals for the specified port.
1286 * The user should allocate a variable of type "enum sp_signal" and pass a
1287 * pointer to this variable to receive the result. The result is a bitmask
1288 * in which individual signals can be checked by bitwise OR with values of
1289 * the sp_signal enum.
1291 * @param[in] port Pointer to a port structure. Must not be NULL.
1292 * @param[out] signal_mask Pointer to a variable to receive the result.
1295 * @return SP_OK upon success, a negative error code otherwise.
1299 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signal_mask);
1302 * Put the port transmit line into the break state.
1304 * @param[in] port Pointer to a port structure. Must not be NULL.
1306 * @return SP_OK upon success, a negative error code otherwise.
1310 enum sp_return sp_start_break(struct sp_port *port);
1313 * Take the port transmit line out of the break state.
1315 * @param[in] port Pointer to a port structure. Must not be NULL.
1317 * @return SP_OK upon success, a negative error code otherwise.
1321 enum sp_return sp_end_break(struct sp_port *port);
1326 * @defgroup Errors Errors
1328 * Obtaining error information.
1334 * Get the error code for a failed operation.
1336 * In order to obtain the correct result, this function should be called
1337 * straight after the failure, before executing any other system operations.
1338 * The result is thread-specific, and only valid when called immediately
1339 * after a previous call returning SP_ERR_FAIL.
1341 * @return The system's numeric code for the error that caused the last
1342 * operation to fail.
1346 int sp_last_error_code(void);
1349 * Get the error message for a failed operation.
1351 * In order to obtain the correct result, this function should be called
1352 * straight after the failure, before executing other system operations.
1353 * The result is thread-specific, and only valid when called immediately
1354 * after a previous call returning SP_ERR_FAIL.
1356 * @return The system's message for the error that caused the last
1357 * operation to fail. This string may be allocated by the function,
1358 * and should be freed after use by calling sp_free_error_message().
1362 char *sp_last_error_message(void);
1365 * Free an error message returned by sp_last_error_message().
1367 * @param[in] message The error message string to free. Must not be NULL.
1371 void sp_free_error_message(char *message);
1374 * Set the handler function for library debugging messages.
1376 * Debugging messages are generated by the library during each operation,
1377 * to help in diagnosing problems. The handler will be called for each
1378 * message. The handler can be set to NULL to ignore all debug messages.
1380 * The handler function should accept a format string and variable length
1381 * argument list, in the same manner as e.g. printf().
1383 * The default handler is sp_default_debug_handler().
1385 * @param[in] handler The handler function to use. Can be NULL (in that case
1386 * all debug messages will be ignored).
1390 void sp_set_debug_handler(void (*handler)(const char *format, ...));
1393 * Default handler function for library debugging messages.
1395 * This function prints debug messages to the standard error stream if the
1396 * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
1399 * @param[in] format The format string to use. Must not be NULL.
1400 * @param[in] ... The variable length argument list to use.
1404 void sp_default_debug_handler(const char *format, ...);
1409 * @defgroup Versions Versions
1411 * Version number querying functions, definitions, and macros.
1413 * This set of API calls returns two different version numbers related
1414 * to libserialport. The "package version" is the release version number of the
1415 * libserialport tarball in the usual "major.minor.micro" format, e.g. "0.1.0".
1417 * The "library version" is independent of that; it is the libtool version
1418 * number in the "current:revision:age" format, e.g. "2:0:0".
1419 * See http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning for details.
1421 * Both version numbers (and/or individual components of them) can be
1422 * retrieved via the API calls at runtime, and/or they can be checked at
1423 * compile/preprocessor time using the respective macros.
1429 * Package version macros (can be used for conditional compilation).
1432 /** The libserialport package 'major' version number. */
1433 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
1435 /** The libserialport package 'minor' version number. */
1436 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
1438 /** The libserialport package 'micro' version number. */
1439 #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
1441 /** The libserialport package version ("major.minor.micro") as string. */
1442 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
1445 * Library/libtool version macros (can be used for conditional compilation).
1448 /** The libserialport libtool 'current' version number. */
1449 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
1451 /** The libserialport libtool 'revision' version number. */
1452 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
1454 /** The libserialport libtool 'age' version number. */
1455 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
1457 /** The libserialport libtool version ("current:revision:age") as string. */
1458 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
1461 * Get the major libserialport package version number.
1463 * @return The major package version number.
1467 int sp_get_major_package_version(void);
1470 * Get the minor libserialport package version number.
1472 * @return The minor package version number.
1476 int sp_get_minor_package_version(void);
1479 * Get the micro libserialport package version number.
1481 * @return The micro package version number.
1485 int sp_get_micro_package_version(void);
1488 * Get the libserialport package version number as a string.
1490 * @return The package version number string. The returned string is
1491 * static and thus should NOT be free'd by the caller.
1495 const char *sp_get_package_version_string(void);
1498 * Get the "current" part of the libserialport library version number.
1500 * @return The "current" library version number.
1504 int sp_get_current_lib_version(void);
1507 * Get the "revision" part of the libserialport library version number.
1509 * @return The "revision" library version number.
1513 int sp_get_revision_lib_version(void);
1516 * Get the "age" part of the libserialport library version number.
1518 * @return The "age" library version number.
1522 int sp_get_age_lib_version(void);
1525 * Get the libserialport library version number as a string.
1527 * @return The library version number string. The returned string is
1528 * static and thus should NOT be free'd by the caller.
1532 const char *sp_get_lib_version_string(void);