windows: Fix restart of RX/error wait operation after reads.
[libserialport/gsi.git] / libserialport.h.in
blobeca21a5cf9a9a9688b227568aca8e42addff3b55
1 /*
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/>.
20 /**
21 * @mainpage libserialport API
23 * Introduction
24 * ============
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)
35 * - @ref Ports
36 * - @ref Configuration (baud rate, parity, etc.)
37 * - @ref Signals (modem control lines, breaks, etc.)
38 * - @ref Data
39 * - @ref Waiting
40 * - @ref Errors
42 * libserialport is an open source project released under the LGPL3+ license.
44 * API principles
45 * ==============
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
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
87 #include <stddef.h>
88 #ifdef _WIN32
89 #include <windows.h>
90 #endif
92 /** Return values. */
93 enum sp_return {
94 /** Operation completed successfully. */
95 SP_OK = 0,
96 /** Invalid arguments were passed to the function. */
97 SP_ERR_ARG = -1,
98 /** A system error occured while executing the operation. */
99 SP_ERR_FAIL = -2,
100 /** A memory allocation failed while executing the operation. */
101 SP_ERR_MEM = -3,
102 /** The requested operation is not supported by this system or device. */
103 SP_ERR_SUPP = -4
106 /** Port access modes. */
107 enum sp_mode {
108 /** Open port for read access. */
109 SP_MODE_READ = 1,
110 /** Open port for write access. */
111 SP_MODE_WRITE = 2,
112 /** Open port for read and write access. */
113 SP_MODE_READ_WRITE = 3
116 /** Port events. */
117 enum sp_event {
118 /* Data received and ready to read. */
119 SP_EVENT_RX_READY = 1,
120 /* Ready to transmit new data. */
121 SP_EVENT_TX_READY = 2,
122 /* Error occured. */
123 SP_EVENT_ERROR = 4
126 /** Buffer selection. */
127 enum sp_buffer {
128 /** Input buffer. */
129 SP_BUF_INPUT = 1,
130 /** Output buffer. */
131 SP_BUF_OUTPUT = 2,
132 /** Both buffers. */
133 SP_BUF_BOTH = 3
136 /** Parity settings. */
137 enum sp_parity {
138 /** Special value to indicate setting should be left alone. */
139 SP_PARITY_INVALID = -1,
140 /** No parity. */
141 SP_PARITY_NONE = 0,
142 /** Odd parity. */
143 SP_PARITY_ODD = 1,
144 /** Even parity. */
145 SP_PARITY_EVEN = 2,
146 /** Mark parity. */
147 SP_PARITY_MARK = 3,
148 /** Space parity. */
149 SP_PARITY_SPACE = 4
152 /** RTS pin behaviour. */
153 enum sp_rts {
154 /** Special value to indicate setting should be left alone. */
155 SP_RTS_INVALID = -1,
156 /** RTS off. */
157 SP_RTS_OFF = 0,
158 /** RTS on. */
159 SP_RTS_ON = 1,
160 /** RTS used for flow control. */
161 SP_RTS_FLOW_CONTROL = 2
164 /** CTS pin behaviour. */
165 enum sp_cts {
166 /** Special value to indicate setting should be left alone. */
167 SP_CTS_INVALID = -1,
168 /** CTS ignored. */
169 SP_CTS_IGNORE = 0,
170 /** CTS used for flow control. */
171 SP_CTS_FLOW_CONTROL = 1
174 /** DTR pin behaviour. */
175 enum sp_dtr {
176 /** Special value to indicate setting should be left alone. */
177 SP_DTR_INVALID = -1,
178 /** DTR off. */
179 SP_DTR_OFF = 0,
180 /** DTR on. */
181 SP_DTR_ON = 1,
182 /** DTR used for flow control. */
183 SP_DTR_FLOW_CONTROL = 2
186 /** DSR pin behaviour. */
187 enum sp_dsr {
188 /** Special value to indicate setting should be left alone. */
189 SP_DSR_INVALID = -1,
190 /** DSR ignored. */
191 SP_DSR_IGNORE = 0,
192 /** DSR used for flow control. */
193 SP_DSR_FLOW_CONTROL = 1
196 /** XON/XOFF flow control behaviour. */
197 enum sp_xonxoff {
198 /** Special value to indicate setting should be left alone. */
199 SP_XONXOFF_INVALID = -1,
200 /** XON/XOFF disabled. */
201 SP_XONXOFF_DISABLED = 0,
202 /** XON/XOFF enabled for input only. */
203 SP_XONXOFF_IN = 1,
204 /** XON/XOFF enabled for output only. */
205 SP_XONXOFF_OUT = 2,
206 /** XON/XOFF enabled for input and output. */
207 SP_XONXOFF_INOUT = 3
210 /** Standard flow control combinations. */
211 enum sp_flowcontrol {
212 /** No flow control. */
213 SP_FLOWCONTROL_NONE = 0,
214 /** Software flow control using XON/XOFF characters. */
215 SP_FLOWCONTROL_XONXOFF = 1,
216 /** Hardware flow control using RTS/CTS signals. */
217 SP_FLOWCONTROL_RTSCTS = 2,
218 /** Hardware flow control using DTR/DSR signals. */
219 SP_FLOWCONTROL_DTRDSR = 3
222 /** Input signals. */
223 enum sp_signal {
224 /** Clear to send. */
225 SP_SIG_CTS = 1,
226 /** Data set ready. */
227 SP_SIG_DSR = 2,
228 /** Data carrier detect. */
229 SP_SIG_DCD = 4,
230 /** Ring indicator. */
231 SP_SIG_RI = 8
234 /** Transport types. */
235 enum sp_transport {
236 /** Native platform serial port. */
237 SP_TRANSPORT_NATIVE,
238 /** USB serial port adapter. */
239 SP_TRANSPORT_USB,
240 /** Bluetooth serial port adapter. */
241 SP_TRANSPORT_BLUETOOTH
245 * @struct sp_port
246 * An opaque structure representing a serial port.
248 struct sp_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. */
262 void *handles;
263 /** Array of bitmasks indicating which events apply for each handle. */
264 enum sp_event *masks;
265 /** Number of handles. */
266 unsigned int count;
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 * @since 0.1.0
289 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
292 * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
294 * @since 0.1.0
296 void sp_free_port(struct sp_port *port);
299 * List the serial ports available on the system.
301 * The result obtained is an array of pointers to sp_port structures,
302 * terminated by a NULL. The user should allocate a variable of type
303 * "struct sp_port **" and pass a pointer to this to receive the result.
305 * The result should be freed after use by calling sp_free_port_list().
306 * If a port from the list is to be used after freeing the list, it must be
307 * copied first using sp_copy_port().
309 * If any error is returned, the variable pointed to by list_ptr will be set
310 * to NULL. Otherwise, it will be set to point to the newly allocated array.
312 * @return SP_OK upon success, a negative error code otherwise.
314 * @since 0.1.0
316 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
319 * Make a new copy of a sp_port structure.
321 * The user should allocate a variable of type "struct sp_port *" and pass a
322 * pointer to this to receive the result.
324 * The copy should be freed after use by calling sp_free_port().
326 * If any error is returned, the variable pointed to by copy_ptr will be set
327 * to NULL. Otherwise, it will be set to point to the newly allocated copy.
329 * @return SP_OK upon success, a negative error code otherwise.
331 * @since 0.1.0
333 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
336 * Free a port list obtained from sp_list_ports().
338 * This will also free all the sp_port structures referred to from the list;
339 * any that are to be retained must be copied first using sp_copy_port().
341 * @since 0.1.0
343 void sp_free_port_list(struct sp_port **ports);
346 * @}
347 * @defgroup Ports Opening, closing and querying ports
348 * @{
352 * Open the specified serial port.
354 * @param port Pointer to port structure.
355 * @param flags Flags to use when opening the serial port.
357 * @return SP_OK upon success, a negative error code otherwise.
359 * @since 0.1.0
361 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
364 * Close the specified serial port.
366 * @return SP_OK upon success, a negative error code otherwise.
368 * @since 0.1.0
370 enum sp_return sp_close(struct sp_port *port);
373 * Get the name of a port.
375 * The name returned is whatever is normally used to refer to a port on the
376 * current operating system; e.g. for Windows it will usually be a "COMn"
377 * device name, and for Unix it will be a device path beginning with "/dev/".
379 * @param port Pointer to port structure.
381 * @return The port name, or NULL if an invalid port is passed. The name
382 * string is part of the port structure and may not be used after the
383 * port structure has been freed.
385 * @since 0.1.0
387 char *sp_get_port_name(const struct sp_port *port);
390 * Get a description for a port, to present to end user.
392 * @param port Pointer to port structure.
394 * @return The port description, or NULL if an invalid port is passed.
395 * The description string is part of the port structure and may not be used
396 * after the port structure has been freed.
398 * @since 0.2.0
400 char *sp_get_port_description(struct sp_port *port);
403 * Get the transport type used by a port.
405 * @param port Pointer to port structure.
407 * @return The port transport type.
409 * @since 0.2.0
411 enum sp_transport sp_get_port_transport(struct sp_port *port);
414 * Get the USB bus number and address on bus of a USB serial adapter port.
416 * @param port Pointer to port structure.
417 * @param usb_bus Pointer to variable to store USB bus.
418 * @param usb_address Pointer to variable to store USB address
420 * @return SP_OK upon success, a negative error code otherwise.
422 * @since 0.2.0
424 enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port,
425 int *usb_bus, int *usb_address);
428 * Get the USB Vendor ID and Product ID of a USB serial adapter port.
430 * @param port Pointer to port structure.
431 * @param usb_vid Pointer to variable to store USB VID.
432 * @param usb_pid Pointer to variable to store USB PID
434 * @return SP_OK upon success, a negative error code otherwise.
436 * @since 0.2.0
438 enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, int *usb_vid, int *usb_pid);
441 * Get the USB manufacturer string of a USB serial adapter port.
443 * @param port Pointer to port structure.
445 * @return The port manufacturer string, or NULL if an invalid port is passed.
446 * The manufacturer string is part of the port structure and may not be used
447 * after the port structure has been freed.
449 * @since 0.2.0
451 char *sp_get_port_usb_manufacturer(const struct sp_port *port);
454 * Get the USB product string of a USB serial adapter port.
456 * @param port Pointer to port structure.
458 * @return The port product string, or NULL if an invalid port is passed.
459 * The product string is part of the port structure and may not be used
460 * after the port structure has been freed.
462 * @since 0.2.0
464 char *sp_get_port_usb_product(const struct sp_port *port);
467 * Get the USB serial number string of a USB serial adapter port.
469 * @param port Pointer to port structure.
471 * @return The port serial number, or NULL if an invalid port is passed.
472 * The serial number string is part of the port structure and may not be used
473 * after the port structure has been freed.
475 * @since 0.2.0
477 char *sp_get_port_usb_serial(const struct sp_port *port);
480 * Get the MAC address of a Bluetooth serial adapter port.
482 * @param port Pointer to port structure.
484 * @return The port MAC address, or NULL if an invalid port is passed.
485 * The MAC address string is part of the port structure and may not be used
486 * after the port structure has been freed.
488 * @since 0.2.0
490 char *sp_get_port_bluetooth_address(const struct sp_port *port);
493 * Get the operating system handle for a port.
495 * The type of the handle depends on the operating system. On Unix based
496 * systems, the handle is a file descriptor of type "int". On Windows, the
497 * handle is of type "HANDLE". The user should allocate a variable of the
498 * appropriate type and pass a pointer to this to receive the result.
500 * To obtain a valid handle, the port must first be opened by calling
501 * sp_open() using the same port structure.
503 * After the port is closed or the port structure freed, the handle may
504 * no longer be valid.
506 * @warning This feature is provided so that programs may make use of
507 * OS-specific functionality where desired. Doing so obviously
508 * comes at a cost in portability. It also cannot be guaranteed
509 * that direct usage of the OS handle will not conflict with the
510 * library's own usage of the port. Be careful.
512 * @return SP_OK upon success, a negative error code otherwise.
514 * @since 0.1.0
516 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
519 * @}
520 * @defgroup Configuration Setting port parameters
521 * @{
525 * Allocate a port configuration structure.
527 * The user should allocate a variable of type "struct sp_config *" and pass a
528 * pointer to this to receive the result. The variable will be updated to
529 * point to the new configuration structure. The structure is opaque and must
530 * be accessed via the functions provided.
532 * All parameters in the structure will be initialised to special values which
533 * are ignored by sp_set_config().
535 * The structure should be freed after use by calling sp_free_config().
537 * @param config_ptr Pointer to variable to receive result.
539 * @return SP_OK upon success, a negative error code otherwise.
541 * @since 0.1.0
543 enum sp_return sp_new_config(struct sp_port_config **config_ptr);
546 * Free a port configuration structure.
548 * @param config Pointer to configuration structure.
550 * @since 0.1.0
552 void sp_free_config(struct sp_port_config *config);
555 * Get the current configuration of the specified serial port.
557 * The user should allocate a configuration structure using sp_new_config()
558 * and pass this as the config parameter. The configuration structure will
559 * be updated with the port configuration.
561 * Any parameters that are configured with settings not recognised or
562 * supported by libserialport, will be set to special values that are
563 * ignored by sp_set_config().
565 * @return SP_OK upon success, a negative error code otherwise.
567 * @since 0.1.0
569 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
572 * Set the configuration for the specified serial port.
574 * For each parameter in the configuration, there is a special value (usually
575 * -1, but see the documentation for each field). These values will be ignored
576 * and the corresponding setting left unchanged on the port.
578 * @return SP_OK upon success, a negative error code otherwise.
580 * @since 0.1.0
582 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
585 * Set the baud rate for the specified serial port.
587 * @param port Pointer to port structure.
588 * @param baudrate Baud rate in bits per second.
590 * @return SP_OK upon success, a negative error code otherwise.
592 * @since 0.1.0
594 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
597 * Get the baud rate from a port configuration.
599 * The user should allocate a variable of type int and pass a pointer to this
600 * to receive the result.
602 * @param config Pointer to configuration structure.
603 * @param baudrate_ptr Pointer to variable to store result.
605 * @return SP_OK upon success, a negative error code otherwise.
607 * @since 0.1.0
609 enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
612 * Set the baud rate in a port configuration.
614 * @param config Pointer to configuration structure.
615 * @param baudrate Baud rate in bits per second, or -1 to retain current setting.
617 * @return SP_OK upon success, a negative error code otherwise.
619 * @since 0.1.0
621 enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
624 * Set the data bits for the specified serial port.
626 * @param port Pointer to port structure.
627 * @param bits Number of data bits.
629 * @return SP_OK upon success, a negative error code otherwise.
631 * @since 0.1.0
633 enum sp_return sp_set_bits(struct sp_port *port, int bits);
636 * Get the data bits from a port configuration.
638 * The user should allocate a variable of type int and pass a pointer to this
639 * to receive the result.
641 * @param config Pointer to configuration structure.
642 * @param bits_ptr Pointer to variable to store result.
644 * @return SP_OK upon success, a negative error code otherwise.
646 * @since 0.1.0
648 enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
651 * Set the data bits in a port configuration.
653 * @param config Pointer to configuration structure.
654 * @param bits Number of data bits, or -1 to retain current setting.
656 * @return SP_OK upon success, a negative error code otherwise.
658 * @since 0.1.0
660 enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
663 * Set the parity setting for the specified serial port.
665 * @param port Pointer to port structure.
666 * @param parity Parity setting.
668 * @return SP_OK upon success, a negative error code otherwise.
670 * @since 0.1.0
672 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
675 * Get the parity setting from a port configuration.
677 * The user should allocate a variable of type enum sp_parity and pass a pointer to this
678 * to receive the result.
680 * @param config Pointer to configuration structure.
681 * @param parity_ptr Pointer to variable to store result.
683 * @return SP_OK upon success, a negative error code otherwise.
685 * @since 0.1.0
687 enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
690 * Set the parity setting in a port configuration.
692 * @param config Pointer to configuration structure.
693 * @param parity Parity setting, or -1 to retain current setting.
695 * @return SP_OK upon success, a negative error code otherwise.
697 * @since 0.1.0
699 enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
702 * Set the stop bits for the specified serial port.
704 * @param port Pointer to port structure.
705 * @param stopbits Number of stop bits.
707 * @return SP_OK upon success, a negative error code otherwise.
709 * @since 0.1.0
711 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
714 * Get the stop bits from a port configuration.
716 * The user should allocate a variable of type int and pass a pointer to this
717 * to receive the result.
719 * @param config Pointer to configuration structure.
720 * @param stopbits_ptr Pointer to variable to store result.
722 * @return SP_OK upon success, a negative error code otherwise.
724 * @since 0.1.0
726 enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
729 * Set the stop bits in a port configuration.
731 * @param config Pointer to configuration structure.
732 * @param stopbits Number of stop bits, or -1 to retain current setting.
734 * @return SP_OK upon success, a negative error code otherwise.
736 * @since 0.1.0
738 enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
741 * Set the RTS pin behaviour for the specified serial port.
743 * @param port Pointer to port structure.
744 * @param rts RTS pin mode.
746 * @return SP_OK upon success, a negative error code otherwise.
748 * @since 0.1.0
750 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
753 * Get the RTS pin behaviour from a port configuration.
755 * The user should allocate a variable of type enum sp_rts and pass a pointer to this
756 * to receive the result.
758 * @param config Pointer to configuration structure.
759 * @param rts_ptr Pointer to variable to store result.
761 * @return SP_OK upon success, a negative error code otherwise.
763 * @since 0.1.0
765 enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
768 * Set the RTS pin behaviour in a port configuration.
770 * @param config Pointer to configuration structure.
771 * @param rts RTS pin mode, or -1 to retain current setting.
773 * @return SP_OK upon success, a negative error code otherwise.
775 * @since 0.1.0
777 enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
780 * Set the CTS pin behaviour for the specified serial port.
782 * @param port Pointer to port structure.
783 * @param cts CTS pin mode.
785 * @return SP_OK upon success, a negative error code otherwise.
787 * @since 0.1.0
789 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
792 * Get the CTS pin behaviour from a port configuration.
794 * The user should allocate a variable of type enum sp_cts and pass a pointer to this
795 * to receive the result.
797 * @param config Pointer to configuration structure.
798 * @param cts_ptr Pointer to variable to store result.
800 * @return SP_OK upon success, a negative error code otherwise.
802 * @since 0.1.0
804 enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
807 * Set the CTS pin behaviour in a port configuration.
809 * @param config Pointer to configuration structure.
810 * @param cts CTS pin mode, or -1 to retain current setting.
812 * @return SP_OK upon success, a negative error code otherwise.
814 * @since 0.1.0
816 enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
819 * Set the DTR pin behaviour for the specified serial port.
821 * @param port Pointer to port structure.
822 * @param dtr DTR pin mode.
824 * @return SP_OK upon success, a negative error code otherwise.
826 * @since 0.1.0
828 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
831 * Get the DTR pin behaviour from a port configuration.
833 * The user should allocate a variable of type enum sp_dtr and pass a pointer to this
834 * to receive the result.
836 * @param config Pointer to configuration structure.
837 * @param dtr_ptr Pointer to variable to store result.
839 * @return SP_OK upon success, a negative error code otherwise.
841 * @since 0.1.0
843 enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
846 * Set the DTR pin behaviour in a port configuration.
848 * @param config Pointer to configuration structure.
849 * @param dtr DTR pin mode, or -1 to retain current setting.
851 * @return SP_OK upon success, a negative error code otherwise.
853 * @since 0.1.0
855 enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
858 * Set the DSR pin behaviour for the specified serial port.
860 * @param port Pointer to port structure.
861 * @param dsr DSR pin mode.
863 * @return SP_OK upon success, a negative error code otherwise.
865 * @since 0.1.0
867 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
870 * Get the DSR pin behaviour from a port configuration.
872 * The user should allocate a variable of type enum sp_dsr and pass a pointer to this
873 * to receive the result.
875 * @param config Pointer to configuration structure.
876 * @param dsr_ptr Pointer to variable to store result.
878 * @return SP_OK upon success, a negative error code otherwise.
880 * @since 0.1.0
882 enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
885 * Set the DSR pin behaviour in a port configuration.
887 * @param config Pointer to configuration structure.
888 * @param dsr DSR pin mode, or -1 to retain current setting.
890 * @return SP_OK upon success, a negative error code otherwise.
892 * @since 0.1.0
894 enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
897 * Set the XON/XOFF configuration for the specified serial port.
899 * @param port Pointer to port structure.
900 * @param xon_xoff XON/XOFF mode.
902 * @return SP_OK upon success, a negative error code otherwise.
904 * @since 0.1.0
906 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
909 * Get the XON/XOFF configuration from a port configuration.
911 * The user should allocate a variable of type enum sp_xonxoff and pass a pointer to this
912 * to receive the result.
914 * @param config Pointer to configuration structure.
915 * @param xon_xoff_ptr Pointer to variable to store result.
917 * @return SP_OK upon success, a negative error code otherwise.
919 * @since 0.1.0
921 enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
924 * Set the XON/XOFF configuration in a port configuration.
926 * @param config Pointer to configuration structure.
927 * @param xon_xoff XON/XOFF mode, or -1 to retain current setting.
929 * @return SP_OK upon success, a negative error code otherwise.
931 * @since 0.1.0
933 enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
936 * Set the flow control type in a port configuration.
938 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
939 * XON/XOFF settings as necessary for the specified flow control
940 * type. For more fine-grained control of these settings, use their
941 * individual configuration functions.
943 * @param config Pointer to configuration structure.
944 * @param flowcontrol Flow control setting to use.
946 * @return SP_OK upon success, a negative error code otherwise.
948 * @since 0.1.0
950 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
953 * Set the flow control type for the specified serial port.
955 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
956 * XON/XOFF settings as necessary for the specified flow control
957 * type. For more fine-grained control of these settings, use their
958 * individual configuration functions.
960 * @param port Pointer to port structure.
961 * @param flowcontrol Flow control setting to use.
963 * @return SP_OK upon success, a negative error code otherwise.
965 * @since 0.1.0
967 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
970 * @}
971 * @defgroup Data Reading, writing, and flushing data
972 * @{
976 * Read bytes from the specified serial port, blocking until complete.
978 * @warning If your program runs on Unix, defines its own signal handlers, and
979 * needs to abort blocking reads when these are called, then you
980 * should not use this function. It repeats system calls that return
981 * with EINTR. To be able to abort a read from a signal handler, you
982 * should implement your own blocking read using sp_nonblocking_read()
983 * together with a blocking method that makes sense for your program.
984 * E.g. you can obtain the file descriptor for an open port using
985 * sp_get_port_handle() and use this to call select() or pselect(),
986 * with appropriate arrangements to return if a signal is received.
988 * @param port Pointer to port structure.
989 * @param buf Buffer in which to store the bytes read.
990 * @param count Requested number of bytes to read.
991 * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
993 * @return The number of bytes read on success, or a negative error code. If
994 * the number of bytes returned is less than that requested, the
995 * timeout was reached before the requested number of bytes was
996 * available. If timeout is zero, the function will always return
997 * either the requested number of bytes or a negative error code.
999 * @since 0.1.0
1001 enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout);
1004 * Read bytes from the specified serial port, without blocking.
1006 * @param port Pointer to port structure.
1007 * @param buf Buffer in which to store the bytes read.
1008 * @param count Maximum number of bytes to read.
1010 * @return The number of bytes read on success, or a negative error code. The
1011 * number of bytes returned may be any number from zero to the maximum
1012 * that was requested.
1014 * @since 0.1.0
1016 enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
1019 * Write bytes to the specified serial port, blocking until complete.
1021 * Note that this function only ensures that the accepted bytes have been
1022 * written to the OS; they may be held in driver or hardware buffers and not
1023 * yet physically transmitted. To check whether all written bytes have actually
1024 * been transmitted, use the sp_output_waiting() function. To wait until all
1025 * written bytes have actually been transmitted, use the sp_drain() function.
1027 * @warning If your program runs on Unix, defines its own signal handlers, and
1028 * needs to abort blocking writes 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 write from a signal handler, you
1031 * should implement your own blocking write using sp_nonblocking_write()
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 port Pointer to port structure.
1038 * @param buf Buffer containing the bytes to write.
1039 * @param count Requested number of bytes to write.
1040 * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
1042 * @return The number of bytes written on success, or a negative error code.
1043 * If the number of bytes returned is less than that requested, the
1044 * timeout was reached before the requested number of bytes was
1045 * written. If timeout is zero, the function will always return
1046 * either the requested number of bytes or a negative error code. In
1047 * the event of an error there is no way to determine how many bytes
1048 * were sent before the error occured.
1050 * @since 0.1.0
1052 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout);
1055 * Write bytes to the specified serial port, without blocking.
1057 * Note that this function only ensures that the accepted bytes have been
1058 * written to the OS; they may be held in driver or hardware buffers and not
1059 * yet physically transmitted. To check whether all written bytes have actually
1060 * been transmitted, use the sp_output_waiting() function. To wait until all
1061 * written bytes have actually been transmitted, use the sp_drain() function.
1063 * @param port Pointer to port structure.
1064 * @param buf Buffer containing the bytes to write.
1065 * @param count Maximum number of bytes to write.
1067 * @return The number of bytes written on success, or a negative error code.
1068 * The number of bytes returned may be any number from zero to the
1069 * maximum that was requested.
1071 * @since 0.1.0
1073 enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
1076 * Gets the number of bytes waiting in the input buffer.
1078 * @param port Pointer to port structure.
1080 * @return Number of bytes waiting on success, a negative error code otherwise.
1082 * @since 0.1.0
1084 enum sp_return sp_input_waiting(struct sp_port *port);
1087 * Gets the number of bytes waiting in the output buffer.
1089 * @param port Pointer to port structure.
1091 * @return Number of bytes waiting on success, a negative error code otherwise.
1093 * @since 0.1.0
1095 enum sp_return sp_output_waiting(struct sp_port *port);
1098 * Flush serial port buffers. Data in the selected buffer(s) is discarded.
1100 * @param port Pointer to port structure.
1101 * @param buffers Which buffer(s) to flush.
1103 * @return SP_OK upon success, a negative error code otherwise.
1105 * @since 0.1.0
1107 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
1110 * Wait for buffered data to be transmitted.
1112 * @warning If your program runs on Unix, defines its own signal handlers, and
1113 * needs to abort draining the output buffer when when these are
1114 * called, then you should not use this function. It repeats system
1115 * calls that return with EINTR. To be able to abort a drain from a
1116 * signal handler, you would need to implement your own blocking
1117 * drain by polling the result of sp_output_waiting().
1119 * @param port Pointer to port structure.
1121 * @return SP_OK upon success, a negative error code otherwise.
1123 * @since 0.1.0
1125 enum sp_return sp_drain(struct sp_port *port);
1128 * @}
1129 * @defgroup Waiting Waiting for events
1130 * @{
1134 * Allocate storage for a set of events.
1136 * The user should allocate a variable of type struct sp_event_set *,
1137 * then pass a pointer to this variable to receive the result.
1139 * The result should be freed after use by calling sp_free_event_set().
1141 * @return SP_OK upon success, a negative error code otherwise.
1143 * @since 0.1.0
1145 enum sp_return sp_new_event_set(struct sp_event_set **result_ptr);
1148 * Add events to a struct sp_event_set for a given port.
1150 * The port must first be opened by calling sp_open() using the same port
1151 * structure.
1153 * After the port is closed or the port structure freed, the results may
1154 * no longer be valid.
1156 * @param event_set Event set to update.
1157 * @param port Pointer to port structure.
1158 * @param mask Bitmask of events to be waited for.
1160 * @return SP_OK upon success, a negative error code otherwise.
1162 * @since 0.1.0
1164 enum sp_return sp_add_port_events(struct sp_event_set *event_set,
1165 const struct sp_port *port, enum sp_event mask);
1168 * Wait for any of a set of events to occur.
1170 * @param event_set Event set to wait on.
1171 * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
1173 * @return SP_OK upon success, a negative error code otherwise.
1175 * @since 0.1.0
1177 enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout);
1180 * Free a structure allocated by sp_new_event_set().
1182 * @since 0.1.0
1184 void sp_free_event_set(struct sp_event_set *event_set);
1187 * @}
1188 * @defgroup Signals Port signalling operations
1189 * @{
1193 * Gets the status of the control signals for the specified port.
1195 * The user should allocate a variable of type "enum sp_signal" and pass a
1196 * pointer to this variable to receive the result. The result is a bitmask
1197 * in which individual signals can be checked by bitwise OR with values of
1198 * the sp_signal enum.
1200 * @param port Pointer to port structure.
1201 * @param signal_mask Pointer to variable to receive result.
1203 * @return SP_OK upon success, a negative error code otherwise.
1205 * @since 0.1.0
1207 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signal_mask);
1210 * Put the port transmit line into the break state.
1212 * @param port Pointer to port structure.
1214 * @return SP_OK upon success, a negative error code otherwise.
1216 * @since 0.1.0
1218 enum sp_return sp_start_break(struct sp_port *port);
1221 * Take the port transmit line out of the break state.
1223 * @param port Pointer to port structure.
1225 * @return SP_OK upon success, a negative error code otherwise.
1227 * @since 0.1.0
1229 enum sp_return sp_end_break(struct sp_port *port);
1232 * @}
1233 * @defgroup Errors Obtaining error information
1234 * @{
1238 * Get the error code for a failed operation.
1240 * In order to obtain the correct result, this function should be called
1241 * straight after the failure, before executing any other system operations.
1243 * @return The system's numeric code for the error that caused the last
1244 * operation to fail.
1246 * @since 0.1.0
1248 int sp_last_error_code(void);
1251 * Get the error message for a failed operation.
1253 * In order to obtain the correct result, this function should be called
1254 * straight after the failure, before executing other system operations.
1256 * @return The system's message for the error that caused the last
1257 * operation to fail. This string may be allocated by the function,
1258 * and should be freed after use by calling sp_free_error_message().
1260 * @since 0.1.0
1262 char *sp_last_error_message(void);
1265 * Free an error message returned by sp_last_error_message().
1267 * @since 0.1.0
1269 void sp_free_error_message(char *message);
1272 * Set the handler function for library debugging messages.
1274 * Debugging messages are generated by the library during each operation,
1275 * to help in diagnosing problems. The handler will be called for each
1276 * message. The handler can be set to NULL to ignore all debug messages.
1278 * The handler function should accept a format string and variable length
1279 * argument list, in the same manner as e.g. printf().
1281 * The default handler is sp_default_debug_handler().
1283 * @since 0.1.0
1285 void sp_set_debug_handler(void (*handler)(const char *format, ...));
1288 * Default handler function for library debugging messages.
1290 * This function prints debug messages to the standard error stream if the
1291 * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
1292 * ignored.
1294 * @since 0.1.0
1296 void sp_default_debug_handler(const char *format, ...);
1298 /** @} */
1301 * @defgroup Versions Version number querying functions, definitions, and macros
1303 * This set of API calls returns two different version numbers related
1304 * to libserialport. The "package version" is the release version number of the
1305 * libserialport tarball in the usual "major.minor.micro" format, e.g. "0.1.0".
1307 * The "library version" is independent of that; it is the libtool version
1308 * number in the "current:revision:age" format, e.g. "2:0:0".
1309 * See http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning for details.
1311 * Both version numbers (and/or individual components of them) can be
1312 * retrieved via the API calls at runtime, and/or they can be checked at
1313 * compile/preprocessor time using the respective macros.
1315 * @{
1319 * Package version macros (can be used for conditional compilation).
1322 /** The libserialport package 'major' version number. */
1323 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
1325 /** The libserialport package 'minor' version number. */
1326 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
1328 /** The libserialport package 'micro' version number. */
1329 #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
1331 /** The libserialport package version ("major.minor.micro") as string. */
1332 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
1335 * Library/libtool version macros (can be used for conditional compilation).
1338 /** The libserialport libtool 'current' version number. */
1339 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
1341 /** The libserialport libtool 'revision' version number. */
1342 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
1344 /** The libserialport libtool 'age' version number. */
1345 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
1347 /** The libserialport libtool version ("current:revision:age") as string. */
1348 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
1351 * Get the major libserialport package version number.
1353 * @return The major package version number.
1355 * @since 0.1.0
1357 int sp_get_major_package_version(void);
1360 * Get the minor libserialport package version number.
1362 * @return The minor package version number.
1364 * @since 0.1.0
1366 int sp_get_minor_package_version(void);
1369 * Get the micro libserialport package version number.
1371 * @return The micro package version number.
1373 * @since 0.1.0
1375 int sp_get_micro_package_version(void);
1378 * Get the libserialport package version number as a string.
1380 * @return The package version number string. The returned string is
1381 * static and thus should NOT be free'd by the caller.
1383 * @since 0.1.0
1385 const char *sp_get_package_version_string(void);
1388 * Get the "current" part of the libserialport library version number.
1390 * @return The "current" library version number.
1392 * @since 0.1.0
1394 int sp_get_current_lib_version(void);
1397 * Get the "revision" part of the libserialport library version number.
1399 * @return The "revision" library version number.
1401 * @since 0.1.0
1403 int sp_get_revision_lib_version(void);
1406 * Get the "age" part of the libserialport library version number.
1408 * @return The "age" library version number.
1410 * @since 0.1.0
1412 int sp_get_age_lib_version(void);
1415 * Get the libserialport library version number as a string.
1417 * @return The library version number string. The returned string is
1418 * static and thus should NOT be free'd by the caller.
1420 * @since 0.1.0
1422 const char *sp_get_lib_version_string(void);
1424 /** @} */
1426 #ifdef __cplusplus
1428 #endif
1430 #endif