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
92 /* Package version macros (e.g. for conditional compilation). */
93 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
94 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
95 #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
96 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
98 /* Library/libtool version macros (e.g. for conditional compilation). */
99 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
100 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
101 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
102 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
104 /** Return values. */
106 /** Operation completed successfully. */
108 /** Invalid arguments were passed to the function. */
110 /** A system error occured while executing the operation. */
112 /** A memory allocation failed while executing the operation. */
114 /** The requested operation is not supported by this system or device. */
118 /** Port access modes. */
120 /** Open port for read access. */
122 /** Open port for write access. */
128 /* Data received and ready to read. */
129 SP_EVENT_RX_READY = 1,
130 /* Ready to transmit new data. */
131 SP_EVENT_TX_READY = 2,
136 /** Buffer selection. */
140 /** Output buffer. */
146 /** Parity settings. */
148 /** Special value to indicate setting should be left alone. */
149 SP_PARITY_INVALID = -1,
162 /** RTS pin behaviour. */
164 /** Special value to indicate setting should be left alone. */
170 /** RTS used for flow control. */
171 SP_RTS_FLOW_CONTROL = 2,
174 /** CTS pin behaviour. */
176 /** Special value to indicate setting should be left alone. */
180 /** CTS used for flow control. */
181 SP_CTS_FLOW_CONTROL = 1,
184 /** DTR pin behaviour. */
186 /** Special value to indicate setting should be left alone. */
192 /** DTR used for flow control. */
193 SP_DTR_FLOW_CONTROL = 2,
196 /** DSR pin behaviour. */
198 /** Special value to indicate setting should be left alone. */
202 /** DSR used for flow control. */
203 SP_DSR_FLOW_CONTROL = 1,
206 /** XON/XOFF flow control behaviour. */
208 /** Special value to indicate setting should be left alone. */
209 SP_XONXOFF_INVALID = -1,
210 /** XON/XOFF disabled. */
211 SP_XONXOFF_DISABLED = 0,
212 /** XON/XOFF enabled for input only. */
214 /** XON/XOFF enabled for output only. */
216 /** XON/XOFF enabled for input and output. */
217 SP_XONXOFF_INOUT = 3,
220 /** Standard flow control combinations. */
221 enum sp_flowcontrol {
222 /** No flow control. */
223 SP_FLOWCONTROL_NONE = 0,
224 /** Software flow control using XON/XOFF characters. */
225 SP_FLOWCONTROL_XONXOFF = 1,
226 /** Hardware flow control using RTS/CTS signals. */
227 SP_FLOWCONTROL_RTSCTS = 2,
228 /** Hardware flow control using DTR/DSR signals. */
229 SP_FLOWCONTROL_DTRDSR = 3,
232 /** Input signals. */
234 /** Clear to send. */
236 /** Data set ready. */
238 /** Data carrier detect. */
240 /** Ring indicator. */
246 * An opaque structure representing a serial port.
251 * @struct sp_port_config
252 * An opaque structure representing the configuration for a serial port.
254 struct sp_port_config;
257 * @struct sp_event_set
258 * A set of handles to wait on for events.
260 struct sp_event_set {
261 /** Array of OS-specific handles. */
263 /** Array of bitmasks indicating which events apply for each handle. */
264 enum sp_event *masks;
265 /** Number of handles. */
270 @defgroup Enumeration Port enumeration
275 * Obtain a pointer to a new sp_port structure representing the named port.
277 * The user should allocate a variable of type "struct sp_port *" and pass a
278 * pointer to this to receive the result.
280 * The result should be freed after use by calling sp_free_port().
282 * If any error is returned, the variable pointed to by port_ptr will be set
283 * to NULL. Otherwise, it will be set to point to the newly allocated port.
285 * @return SP_OK upon success, a negative error code otherwise.
287 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
290 * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
292 void sp_free_port(struct sp_port *port);
295 * List the serial ports available on the system.
297 * The result obtained is an array of pointers to sp_port structures,
298 * terminated by a NULL. The user should allocate a variable of type
299 * "struct sp_port **" and pass a pointer to this to receive the result.
301 * The result should be freed after use by calling sp_free_port_list().
302 * If a port from the list is to be used after freeing the list, it must be
303 * copied first using sp_copy_port().
305 * If any error is returned, the variable pointed to by list_ptr will be set
306 * to NULL. Otherwise, it will be set to point to the newly allocated array.
308 * @return SP_OK upon success, a negative error code otherwise.
310 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
313 * Make a new copy of a sp_port structure.
315 * The user should allocate a variable of type "struct sp_port *" and pass a
316 * pointer to this to receive the result.
318 * The copy should be freed after use by calling sp_free_port().
320 * If any error is returned, the variable pointed to by copy_ptr will be set
321 * to NULL. Otherwise, it will be set to point to the newly allocated copy.
323 * @return SP_OK upon success, a negative error code otherwise.
325 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
328 * Free a port list obtained from sp_list_ports().
330 * This will also free all the sp_port structures referred to from the list;
331 * any that are to be retained must be copied first using sp_copy_port().
333 void sp_free_port_list(struct sp_port **ports);
337 * @defgroup Ports Opening, closing and querying ports
342 * Open the specified serial port.
344 * @param port Pointer to port structure.
345 * @param flags Flags to use when opening the serial port.
347 * @return SP_OK upon success, a negative error code otherwise.
349 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
352 * Close the specified serial port.
354 * @return SP_OK upon success, a negative error code otherwise.
356 enum sp_return sp_close(struct sp_port *port);
359 * Get the name of a port.
361 * The name returned is whatever is normally used to refer to a port on the
362 * current operating system; e.g. for Windows it will usually be a "COMn"
363 * device name, and for Unix it will be a device path beginning with "/dev/".
365 * @param port Pointer to port structure.
367 * @return The port name, or NULL if an invalid port is passed. The name
368 * string is part of the port structure and may not be used after the
369 * port structure has been freed.
371 char *sp_get_port_name(const struct sp_port *port);
374 * Get the operating system handle for a port.
376 * The type of the handle depends on the operating system. On Unix based
377 * systems, the handle is a file descriptor of type "int". On Windows, the
378 * handle is of type "HANDLE". The user should allocate a variable of the
379 * appropriate type and pass a pointer to this to receive the result.
381 * To obtain a valid handle, the port must first be opened by calling
382 * sp_open() using the same port structure.
384 * After the port is closed or the port structure freed, the handle may
385 * no longer be valid.
387 * @warning This feature is provided so that programs may make use of
388 * OS-specific functionality where desired. Doing so obviously
389 * comes at a cost in portability. It also cannot be guaranteed
390 * that direct usage of the OS handle will not conflict with the
391 * library's own usage of the port. Be careful.
393 * @return SP_OK upon success, a negative error code otherwise.
395 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
399 * @defgroup Configuration Setting port parameters
404 * Allocate a port configuration structure.
406 * The user should allocate a variable of type "struct sp_config *" and pass a
407 * pointer to this to receive the result. The variable will be updated to
408 * point to the new configuration structure. The structure is opaque and must
409 * be accessed via the functions provided.
411 * All parameters in the structure will be initialised to special values which
412 * are ignored by sp_set_config().
414 * The structure should be freed after use by calling sp_free_config().
416 * @param config_ptr Pointer to variable to receive result.
418 * @return SP_OK upon success, a negative error code otherwise.
420 enum sp_return sp_new_config(struct sp_port_config **config_ptr);
423 * Free a port configuration structure.
425 * @param config Pointer to configuration structure.
427 void sp_free_config(struct sp_port_config *config);
430 * Get the current configuration of the specified serial port.
432 * The user should allocate a configuration structure using sp_new_config()
433 * and pass this as the config parameter. The configuration structure will
434 * be updated with the port configuration.
436 * Any parameters that are configured with settings not recognised or
437 * supported by libserialport, will be set to special values that are
438 * ignored by sp_set_config().
440 * @return SP_OK upon success, a negative error code otherwise.
442 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
445 * Set the configuration for the specified serial port.
447 * For each parameter in the configuration, there is a special value (usually
448 * -1, but see the documentation for each field). These values will be ignored
449 * and the corresponding setting left unchanged on the port.
451 * @return SP_OK upon success, a negative error code otherwise.
453 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
456 * Set the baud rate for the specified serial port.
458 * @param port Pointer to port structure.
459 * @param baudrate Baud rate in bits per second.
461 * @return SP_OK upon success, a negative error code otherwise.
463 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
466 * Get the baud rate from a port configuration.
468 * The user should allocate a variable of type int and pass a pointer to this
469 * to receive the result.
471 * @param config Pointer to configuration structure.
472 * @param baudrate_ptr Pointer to variable to store result.
474 * @return SP_OK upon success, a negative error code otherwise.
476 enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
479 * Set the baud rate in a port configuration.
481 * @param config Pointer to configuration structure.
482 * @param baudrate Baud rate in bits per second, or -1 to retain current setting.
484 * @return SP_OK upon success, a negative error code otherwise.
486 enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
489 * Set the data bits for the specified serial port.
491 * @param port Pointer to port structure.
492 * @param bits Number of data bits.
494 * @return SP_OK upon success, a negative error code otherwise.
496 enum sp_return sp_set_bits(struct sp_port *port, int bits);
499 * Get the data bits from a port configuration.
501 * The user should allocate a variable of type int and pass a pointer to this
502 * to receive the result.
504 * @param config Pointer to configuration structure.
505 * @param bits_ptr Pointer to variable to store result.
507 * @return SP_OK upon success, a negative error code otherwise.
509 enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
512 * Set the data bits in a port configuration.
514 * @param config Pointer to configuration structure.
515 * @param bits Number of data bits, or -1 to retain current setting.
517 * @return SP_OK upon success, a negative error code otherwise.
519 enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
522 * Set the parity setting for the specified serial port.
524 * @param port Pointer to port structure.
525 * @param parity Parity setting.
527 * @return SP_OK upon success, a negative error code otherwise.
529 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
532 * Get the parity setting from a port configuration.
534 * The user should allocate a variable of type enum sp_parity and pass a pointer to this
535 * to receive the result.
537 * @param config Pointer to configuration structure.
538 * @param parity_ptr Pointer to variable to store result.
540 * @return SP_OK upon success, a negative error code otherwise.
542 enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
545 * Set the parity setting in a port configuration.
547 * @param config Pointer to configuration structure.
548 * @param parity Parity setting, or -1 to retain current setting.
550 * @return SP_OK upon success, a negative error code otherwise.
552 enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
555 * Set the stop bits for the specified serial port.
557 * @param port Pointer to port structure.
558 * @param stopbits Number of stop bits.
560 * @return SP_OK upon success, a negative error code otherwise.
562 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
565 * Get the stop bits from a port configuration.
567 * The user should allocate a variable of type int and pass a pointer to this
568 * to receive the result.
570 * @param config Pointer to configuration structure.
571 * @param stopbits_ptr Pointer to variable to store result.
573 * @return SP_OK upon success, a negative error code otherwise.
575 enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
578 * Set the stop bits in a port configuration.
580 * @param config Pointer to configuration structure.
581 * @param stopbits Number of stop bits, or -1 to retain current setting.
583 * @return SP_OK upon success, a negative error code otherwise.
585 enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
588 * Set the RTS pin behaviour for the specified serial port.
590 * @param port Pointer to port structure.
591 * @param rts RTS pin mode.
593 * @return SP_OK upon success, a negative error code otherwise.
595 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
598 * Get the RTS pin behaviour from a port configuration.
600 * The user should allocate a variable of type enum sp_rts and pass a pointer to this
601 * to receive the result.
603 * @param config Pointer to configuration structure.
604 * @param rts_ptr Pointer to variable to store result.
606 * @return SP_OK upon success, a negative error code otherwise.
608 enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
611 * Set the RTS pin behaviour in a port configuration.
613 * @param config Pointer to configuration structure.
614 * @param rts RTS pin mode, or -1 to retain current setting.
616 * @return SP_OK upon success, a negative error code otherwise.
618 enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
621 * Set the CTS pin behaviour for the specified serial port.
623 * @param port Pointer to port structure.
624 * @param cts CTS pin mode.
626 * @return SP_OK upon success, a negative error code otherwise.
628 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
631 * Get the CTS pin behaviour from a port configuration.
633 * The user should allocate a variable of type enum sp_cts and pass a pointer to this
634 * to receive the result.
636 * @param config Pointer to configuration structure.
637 * @param cts_ptr Pointer to variable to store result.
639 * @return SP_OK upon success, a negative error code otherwise.
641 enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
644 * Set the CTS pin behaviour in a port configuration.
646 * @param config Pointer to configuration structure.
647 * @param cts CTS pin mode, or -1 to retain current setting.
649 * @return SP_OK upon success, a negative error code otherwise.
651 enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
654 * Set the DTR pin behaviour for the specified serial port.
656 * @param port Pointer to port structure.
657 * @param dtr DTR pin mode.
659 * @return SP_OK upon success, a negative error code otherwise.
661 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
664 * Get the DTR pin behaviour from a port configuration.
666 * The user should allocate a variable of type enum sp_dtr and pass a pointer to this
667 * to receive the result.
669 * @param config Pointer to configuration structure.
670 * @param dtr_ptr Pointer to variable to store result.
672 * @return SP_OK upon success, a negative error code otherwise.
674 enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
677 * Set the DTR pin behaviour in a port configuration.
679 * @param config Pointer to configuration structure.
680 * @param dtr DTR pin mode, or -1 to retain current setting.
682 * @return SP_OK upon success, a negative error code otherwise.
684 enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
687 * Set the DSR pin behaviour for the specified serial port.
689 * @param port Pointer to port structure.
690 * @param dsr DSR pin mode.
692 * @return SP_OK upon success, a negative error code otherwise.
694 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
697 * Get the DSR pin behaviour from a port configuration.
699 * The user should allocate a variable of type enum sp_dsr and pass a pointer to this
700 * to receive the result.
702 * @param config Pointer to configuration structure.
703 * @param dsr_ptr Pointer to variable to store result.
705 * @return SP_OK upon success, a negative error code otherwise.
707 enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
710 * Set the DSR pin behaviour in a port configuration.
712 * @param config Pointer to configuration structure.
713 * @param dsr DSR pin mode, or -1 to retain current setting.
715 * @return SP_OK upon success, a negative error code otherwise.
717 enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
720 * Set the XON/XOFF configuration for the specified serial port.
722 * @param port Pointer to port structure.
723 * @param xon_xoff XON/XOFF mode.
725 * @return SP_OK upon success, a negative error code otherwise.
727 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
730 * Get the XON/XOFF configuration from a port configuration.
732 * The user should allocate a variable of type enum sp_xonxoff and pass a pointer to this
733 * to receive the result.
735 * @param config Pointer to configuration structure.
736 * @param xon_xoff_ptr Pointer to variable to store result.
738 * @return SP_OK upon success, a negative error code otherwise.
740 enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
743 * Set the XON/XOFF configuration in a port configuration.
745 * @param config Pointer to configuration structure.
746 * @param xon_xoff XON/XOFF mode, or -1 to retain current setting.
748 * @return SP_OK upon success, a negative error code otherwise.
750 enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
753 * Set the flow control type in a port configuration.
755 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
756 * XON/XOFF settings as necessary for the specified flow control
757 * type. For more fine-grained control of these settings, use their
758 * individual configuration functions.
760 * @param config Pointer to configuration structure.
761 * @param flowcontrol Flow control setting to use.
763 * @return SP_OK upon success, a negative error code otherwise.
765 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
768 * Set the flow control type for the specified serial port.
770 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
771 * XON/XOFF settings as necessary for the specified flow control
772 * type. For more fine-grained control of these settings, use their
773 * individual configuration functions.
775 * @param port Pointer to port structure.
776 * @param flowcontrol Flow control setting to use.
778 * @return SP_OK upon success, a negative error code otherwise.
780 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
784 * @defgroup Data Reading, writing, and flushing data
789 * Read bytes from the specified serial port, blocking until complete.
791 * @warning If your program runs on Unix, defines its own signal handlers, and
792 * needs to abort blocking reads when these are called, then you
793 * should not use this function. It repeats system calls that return
794 * with EINTR. To be able to abort a read from a signal handler, you
795 * should implement your own blocking read using sp_nonblocking_read()
796 * together with a blocking method that makes sense for your program.
797 * E.g. you can obtain the file descriptor for an open port using
798 * sp_get_port_handle() and use this to call select() or pselect(),
799 * with appropriate arrangements to return if a signal is received.
801 * @param port Pointer to port structure.
802 * @param buf Buffer in which to store the bytes read.
803 * @param count Requested number of bytes to read.
804 * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
806 * @return The number of bytes read on success, or a negative error code. If
807 * the number of bytes returned is less than that requested, the
808 * timeout was reached before the requested number of bytes was
809 * available. If timeout is zero, the function will always return
810 * either the requested number of bytes or a negative error code.
812 enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout);
815 * Read bytes from the specified serial port, without blocking.
817 * @param port Pointer to port structure.
818 * @param buf Buffer in which to store the bytes read.
819 * @param count Maximum number of bytes to read.
821 * @return The number of bytes read on success, or a negative error code. The
822 * number of bytes returned may be any number from zero to the maximum
823 * that was requested.
825 enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
828 * Write bytes to the specified serial port, blocking until complete.
830 * Note that this function only ensures that the accepted bytes have been
831 * written to the OS; they may be held in driver or hardware buffers and not
832 * yet physically transmitted. To check whether all written bytes have actually
833 * been transmitted, use the sp_output_waiting() function. To wait until all
834 * written bytes have actually been transmitted, use the sp_drain() function.
836 * @warning If your program runs on Unix, defines its own signal handlers, and
837 * needs to abort blocking writes when these are called, then you
838 * should not use this function. It repeats system calls that return
839 * with EINTR. To be able to abort a write from a signal handler, you
840 * should implement your own blocking write using sp_nonblocking_write()
841 * together with a blocking method that makes sense for your program.
842 * E.g. you can obtain the file descriptor for an open port using
843 * sp_get_port_handle() and use this to call select() or pselect(),
844 * with appropriate arrangements to return if a signal is received.
846 * @param port Pointer to port structure.
847 * @param buf Buffer containing the bytes to write.
848 * @param count Requested number of bytes to write.
849 * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
851 * @return The number of bytes written on success, or a negative error code.
852 * If the number of bytes returned is less than that requested, the
853 * timeout was reached before the requested number of bytes was
854 * written. If timeout is zero, the function will always return
855 * either the requested number of bytes or a negative error code. In
856 * the event of an error there is no way to determine how many bytes
857 * were sent before the error occured.
859 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout);
862 * Write bytes to the specified serial port, without blocking.
864 * Note that this function only ensures that the accepted bytes have been
865 * written to the OS; they may be held in driver or hardware buffers and not
866 * yet physically transmitted. To check whether all written bytes have actually
867 * been transmitted, use the sp_output_waiting() function. To wait until all
868 * written bytes have actually been transmitted, use the sp_drain() function.
870 * @param port Pointer to port structure.
871 * @param buf Buffer containing the bytes to write.
872 * @param count Maximum number of bytes to write.
874 * @return The number of bytes written on success, or a negative error code.
875 * The number of bytes returned may be any number from zero to the
876 * maximum that was requested.
878 enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
881 * Gets the number of bytes waiting in the input buffer.
883 * @param port Pointer to port structure.
885 * @return Number of bytes waiting on success, a negative error code otherwise.
887 enum sp_return sp_input_waiting(struct sp_port *port);
890 * Gets the number of bytes waiting in the output buffer.
892 * @param port Pointer to port structure.
894 * @return Number of bytes waiting on success, a negative error code otherwise.
896 enum sp_return sp_output_waiting(struct sp_port *port);
899 * Flush serial port buffers. Data in the selected buffer(s) is discarded.
901 * @param port Pointer to port structure.
902 * @param buffers Which buffer(s) to flush.
904 * @return SP_OK upon success, a negative error code otherwise.
906 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
909 * Wait for buffered data to be transmitted.
911 * @warning If your program runs on Unix, defines its own signal handlers, and
912 * needs to abort draining the output buffer when when these are
913 * called, then you should not use this function. It repeats system
914 * calls that return with EINTR. To be able to abort a drain from a
915 * signal handler, you would need to implement your own blocking
916 * drain by polling the result of sp_output_waiting().
918 * @param port Pointer to port structure.
920 * @return SP_OK upon success, a negative error code otherwise.
922 enum sp_return sp_drain(struct sp_port *port);
926 * @defgroup Waiting Waiting for events
931 * Allocate storage for a set of events.
933 * The user should allocate a variable of type struct sp_event_set *,
934 * then pass a pointer to this variable to receive the result.
936 * The result should be freed after use by calling sp_free_event_set().
938 * @return SP_OK upon success, a negative error code otherwise.
940 enum sp_return sp_new_event_set(struct sp_event_set **result_ptr);
943 * Add events to a struct sp_event_set for a given port.
945 * The port must first be opened by calling sp_open() using the same port
948 * After the port is closed or the port structure freed, the results may
949 * no longer be valid.
951 * @param event_set Event set to update.
952 * @param port Pointer to port structure.
953 * @param mask Bitmask of events to be waited for.
955 * @return SP_OK upon success, a negative error code otherwise.
957 enum sp_return sp_add_port_events(struct sp_event_set *event_set,
958 const struct sp_port *port, enum sp_event mask);
961 * Wait for any of a set of events to occur.
963 * @param event_set Event set to wait on.
964 * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
966 * @return SP_OK upon success, a negative error code otherwise.
968 enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout);
971 * Free a structure allocated by sp_new_event_set().
973 void sp_free_event_set(struct sp_event_set *event_set);
977 * @defgroup Signals Port signalling operations
982 * Gets the status of the control signals for the specified port.
984 * The user should allocate a variable of type "enum sp_signal" and pass a
985 * pointer to this variable to receive the result. The result is a bitmask
986 * in which individual signals can be checked by bitwise OR with values of
987 * the sp_signal enum.
989 * @param port Pointer to port structure.
990 * @param signals Pointer to variable to receive result.
992 * @return SP_OK upon success, a negative error code otherwise.
994 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals);
997 * Put the port transmit line into the break state.
999 * @param port Pointer to port structure.
1001 * @return SP_OK upon success, a negative error code otherwise.
1003 enum sp_return sp_start_break(struct sp_port *port);
1006 * Take the port transmit line out of the break state.
1008 * @param port Pointer to port structure.
1010 * @return SP_OK upon success, a negative error code otherwise.
1012 enum sp_return sp_end_break(struct sp_port *port);
1016 * @defgroup Errors Obtaining error information
1021 * Get the error code for a failed operation.
1023 * In order to obtain the correct result, this function should be called
1024 * straight after the failure, before executing any other system operations.
1026 * @return The system's numeric code for the error that caused the last
1027 * operation to fail.
1029 int sp_last_error_code(void);
1032 * Get the error message for a failed operation.
1034 * In order to obtain the correct result, this function should be called
1035 * straight after the failure, before executing other system operations.
1037 * @return The system's message for the error that caused the last
1038 * operation to fail. This string may be allocated by the function,
1039 * and should be freed after use by calling sp_free_error_message().
1041 char *sp_last_error_message(void);
1044 * Free an error message returned by sp_last_error_message().
1046 void sp_free_error_message(char *message);
1049 * Set the handler function for library debugging messages.
1051 * Debugging messages are generated by the library during each operation,
1052 * to help in diagnosing problems. The handler will be called for each
1053 * message. The handler can be set to NULL to ignore all debug messages.
1055 * The handler function should accept a format string and variable length
1056 * argument list, in the same manner as e.g. printf().
1058 * The default handler is sp_default_debug_handler().
1060 void sp_set_debug_handler(void (*handler)(const char *format, ...));
1063 * Default handler function for library debugging messages.
1065 * This function prints debug messages to the standard error stream if the
1066 * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
1069 void sp_default_debug_handler(const char *format, ...);