windows.c: Constify a few function parameters.
[libserialport/gsi.git] / libserialport.h.in
blobc37c1cacc24d8e8f9735367b12c7da7665cf7f8e
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>
89 /** Return values. */
90 enum sp_return {
91 /** Operation completed successfully. */
92 SP_OK = 0,
93 /** Invalid arguments were passed to the function. */
94 SP_ERR_ARG = -1,
95 /** A system error occurred while executing the operation. */
96 SP_ERR_FAIL = -2,
97 /** A memory allocation failed while executing the operation. */
98 SP_ERR_MEM = -3,
99 /** The requested operation is not supported by this system or device. */
100 SP_ERR_SUPP = -4
103 /** Port access modes. */
104 enum sp_mode {
105 /** Open port for read access. */
106 SP_MODE_READ = 1,
107 /** Open port for write access. */
108 SP_MODE_WRITE = 2,
109 /** Open port for read and write access. @since 0.1.1 */
110 SP_MODE_READ_WRITE = 3
113 /** Port events. */
114 enum sp_event {
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. */
120 SP_EVENT_ERROR = 4
123 /** Buffer selection. */
124 enum sp_buffer {
125 /** Input buffer. */
126 SP_BUF_INPUT = 1,
127 /** Output buffer. */
128 SP_BUF_OUTPUT = 2,
129 /** Both buffers. */
130 SP_BUF_BOTH = 3
133 /** Parity settings. */
134 enum sp_parity {
135 /** Special value to indicate setting should be left alone. */
136 SP_PARITY_INVALID = -1,
137 /** No parity. */
138 SP_PARITY_NONE = 0,
139 /** Odd parity. */
140 SP_PARITY_ODD = 1,
141 /** Even parity. */
142 SP_PARITY_EVEN = 2,
143 /** Mark parity. */
144 SP_PARITY_MARK = 3,
145 /** Space parity. */
146 SP_PARITY_SPACE = 4
149 /** RTS pin behaviour. */
150 enum sp_rts {
151 /** Special value to indicate setting should be left alone. */
152 SP_RTS_INVALID = -1,
153 /** RTS off. */
154 SP_RTS_OFF = 0,
155 /** RTS on. */
156 SP_RTS_ON = 1,
157 /** RTS used for flow control. */
158 SP_RTS_FLOW_CONTROL = 2
161 /** CTS pin behaviour. */
162 enum sp_cts {
163 /** Special value to indicate setting should be left alone. */
164 SP_CTS_INVALID = -1,
165 /** CTS ignored. */
166 SP_CTS_IGNORE = 0,
167 /** CTS used for flow control. */
168 SP_CTS_FLOW_CONTROL = 1
171 /** DTR pin behaviour. */
172 enum sp_dtr {
173 /** Special value to indicate setting should be left alone. */
174 SP_DTR_INVALID = -1,
175 /** DTR off. */
176 SP_DTR_OFF = 0,
177 /** DTR on. */
178 SP_DTR_ON = 1,
179 /** DTR used for flow control. */
180 SP_DTR_FLOW_CONTROL = 2
183 /** DSR pin behaviour. */
184 enum sp_dsr {
185 /** Special value to indicate setting should be left alone. */
186 SP_DSR_INVALID = -1,
187 /** DSR ignored. */
188 SP_DSR_IGNORE = 0,
189 /** DSR used for flow control. */
190 SP_DSR_FLOW_CONTROL = 1
193 /** XON/XOFF flow control behaviour. */
194 enum sp_xonxoff {
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. */
200 SP_XONXOFF_IN = 1,
201 /** XON/XOFF enabled for output only. */
202 SP_XONXOFF_OUT = 2,
203 /** XON/XOFF enabled for input and output. */
204 SP_XONXOFF_INOUT = 3
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. */
220 enum sp_signal {
221 /** Clear to send. */
222 SP_SIG_CTS = 1,
223 /** Data set ready. */
224 SP_SIG_DSR = 2,
225 /** Data carrier detect. */
226 SP_SIG_DCD = 4,
227 /** Ring indicator. */
228 SP_SIG_RI = 8
232 * Transport types.
234 * @since 0.1.1
236 enum sp_transport {
237 /** Native platform serial port. @since 0.1.1 */
238 SP_TRANSPORT_NATIVE,
239 /** USB serial port adapter. @since 0.1.1 */
240 SP_TRANSPORT_USB,
241 /** Bluetooth serial port adapter. @since 0.1.1 */
242 SP_TRANSPORT_BLUETOOTH
246 * @struct sp_port
247 * An opaque structure representing a serial port.
249 struct sp_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. */
263 void *handles;
264 /** Array of bitmasks indicating which events apply for each handle. */
265 enum sp_event *masks;
266 /** Number of handles. */
267 unsigned int count;
271 * @defgroup Enumeration Port enumeration
273 * Enumerating the serial ports of a system.
275 * @{
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.
293 * @since 0.1.0
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.
302 * @since 0.1.0
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.
323 * @since 0.1.0
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.
342 * @since 0.1.0
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.
354 * @since 0.1.0
356 void sp_free_port_list(struct sp_port **ports);
359 * @}
360 * @defgroup Ports Port handling
362 * Opening, closing and querying ports.
364 * @{
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.
375 * @since 0.1.0
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.
386 * @since 0.1.0
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.
403 * @since 0.1.0
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.
416 * @since 0.1.1
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.
427 * @since 0.1.1
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. Must not be NULL.
436 * @param[out] usb_address Pointer to a variable to store the USB address. Must not be NULL.
438 * @return SP_OK upon success, a negative error code otherwise.
440 * @since 0.1.1
442 enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port,
443 int *usb_bus, int *usb_address);
446 * Get the USB Vendor ID and Product ID of a USB serial adapter port.
448 * @param[in] port Pointer to a port structure. Must not be NULL.
449 * @param[out] usb_vid Pointer to a variable to store the USB VID.
450 * Can be NULL (in that case it will be ignored).
451 * @param[out] usb_pid Pointer to a variable to store the USB PID.
452 * Can be NULL (in that case it will be ignored).
454 * @return SP_OK upon success, a negative error code otherwise.
456 * @since 0.1.1
458 enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, int *usb_vid, int *usb_pid);
461 * Get the USB manufacturer string of a USB serial adapter port.
463 * @param[in] port Pointer to a port structure. Must not be NULL.
465 * @return The port manufacturer string, or NULL if an invalid port is passed.
466 * The manufacturer string is part of the port structure and may not
467 * be used after the port structure has been freed.
469 * @since 0.1.1
471 char *sp_get_port_usb_manufacturer(const struct sp_port *port);
474 * Get the USB product string of a USB serial adapter port.
476 * @param[in] port Pointer to a port structure. Must not be NULL.
478 * @return The port product string, or NULL if an invalid port is passed.
479 * The product string is part of the port structure and may not be
480 * used after the port structure has been freed.
482 * @since 0.1.1
484 char *sp_get_port_usb_product(const struct sp_port *port);
487 * Get the USB serial number string of a USB serial adapter port.
489 * @param[in] port Pointer to a port structure. Must not be NULL.
491 * @return The port serial number, or NULL if an invalid port is passed.
492 * The serial number string is part of the port structure and may
493 * not be used after the port structure has been freed.
495 * @since 0.1.1
497 char *sp_get_port_usb_serial(const struct sp_port *port);
500 * Get the MAC address of a Bluetooth serial adapter port.
502 * @param[in] port Pointer to a port structure. Must not be NULL.
504 * @return The port MAC address, or NULL if an invalid port is passed.
505 * The MAC address string is part of the port structure and may not
506 * be used after the port structure has been freed.
508 * @since 0.1.1
510 char *sp_get_port_bluetooth_address(const struct sp_port *port);
513 * Get the operating system handle for a port.
515 * The type of the handle depends on the operating system. On Unix based
516 * systems, the handle is a file descriptor of type "int". On Windows, the
517 * handle is of type "HANDLE". The user should allocate a variable of the
518 * appropriate type and pass a pointer to this to receive the result.
520 * To obtain a valid handle, the port must first be opened by calling
521 * sp_open() using the same port structure.
523 * After the port is closed or the port structure freed, the handle may
524 * no longer be valid.
526 * @warning This feature is provided so that programs may make use of
527 * OS-specific functionality where desired. Doing so obviously
528 * comes at a cost in portability. It also cannot be guaranteed
529 * that direct usage of the OS handle will not conflict with the
530 * library's own usage of the port. Be careful.
532 * @param[in] port Pointer to a port structure. Must not be NULL.
533 * @param[out] result_ptr If any error is returned, the variable pointed to by
534 * result_ptr will be set to NULL. Otherwise, it will
535 * be set to point to the OS handle. Must not be NULL.
537 * @return SP_OK upon success, a negative error code otherwise.
539 * @since 0.1.0
541 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
544 * @}
546 * @defgroup Configuration Configuration
548 * Setting and querying serial port parameters.
549 * @{
553 * Allocate a port configuration structure.
555 * The user should allocate a variable of type "struct sp_config *" and pass a
556 * pointer to this to receive the result. The variable will be updated to
557 * point to the new configuration structure. The structure is opaque and must
558 * be accessed via the functions provided.
560 * All parameters in the structure will be initialised to special values which
561 * are ignored by sp_set_config().
563 * The structure should be freed after use by calling sp_free_config().
565 * @param[out] config_ptr Pointer to a variable to receive the result.
566 * Must not be NULL.
568 * @return SP_OK upon success, a negative error code otherwise.
570 * @since 0.1.0
572 enum sp_return sp_new_config(struct sp_port_config **config_ptr);
575 * Free a port configuration structure.
577 * @param[in] config Pointer to a configuration structure. Must not be NULL.
579 * @since 0.1.0
581 void sp_free_config(struct sp_port_config *config);
584 * Get the current configuration of the specified serial port.
586 * The user should allocate a configuration structure using sp_new_config()
587 * and pass this as the config parameter. The configuration structure will
588 * be updated with the port configuration.
590 * Any parameters that are configured with settings not recognised or
591 * supported by libserialport, will be set to special values that are
592 * ignored by sp_set_config().
594 * @param[in] port Pointer to a port structure. Must not be NULL.
595 * @param[out] config Pointer to a configuration structure that will hold
596 * the result. Must not be NULL.
598 * @return SP_OK upon success, a negative error code otherwise.
600 * @since 0.1.0
602 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
605 * Set the configuration for the specified serial port.
607 * For each parameter in the configuration, there is a special value (usually
608 * -1, but see the documentation for each field). These values will be ignored
609 * and the corresponding setting left unchanged on the port.
611 * @param[in] port Pointer to a port structure. Must not be NULL.
612 * @param[in] config Pointer to a configuration structure. Must not be NULL.
614 * @return SP_OK upon success, a negative error code otherwise.
616 * @since 0.1.0
618 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
621 * Set the baud rate for the specified serial port.
623 * @param[in] port Pointer to a port structure. Must not be NULL.
624 * @param[in] baudrate Baud rate in bits per second.
626 * @return SP_OK upon success, a negative error code otherwise.
628 * @since 0.1.0
630 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
633 * Get the baud rate from a port configuration.
635 * The user should allocate a variable of type int and
636 * pass a pointer to this to receive the result.
638 * @param[in] config Pointer to a configuration structure. Must not be NULL.
639 * @param[out] baudrate_ptr Pointer to a variable to store the result. Must not be NULL.
641 * @return SP_OK upon success, a negative error code otherwise.
643 * @since 0.1.0
645 enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
648 * Set the baud rate in a port configuration.
650 * @param[in] config Pointer to a configuration structure. Must not be NULL.
651 * @param[in] baudrate Baud rate in bits per second, or -1 to retain the current setting.
653 * @return SP_OK upon success, a negative error code otherwise.
655 * @since 0.1.0
657 enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
660 * Set the data bits for the specified serial port.
662 * @param[in] port Pointer to a port structure. Must not be NULL.
663 * @param[in] bits Number of data bits.
665 * @return SP_OK upon success, a negative error code otherwise.
667 * @since 0.1.0
669 enum sp_return sp_set_bits(struct sp_port *port, int bits);
672 * Get the data bits from a port configuration.
674 * The user should allocate a variable of type int and
675 * pass a pointer to this to receive the result.
677 * @param[in] config Pointer to a configuration structure. Must not be NULL.
678 * @param[out] bits_ptr Pointer to a variable to store the result. Must not be NULL.
680 * @return SP_OK upon success, a negative error code otherwise.
682 * @since 0.1.0
684 enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
687 * Set the data bits in a port configuration.
689 * @param[in] config Pointer to a configuration structure. Must not be NULL.
690 * @param[in] bits Number of data bits, or -1 to retain the current setting.
692 * @return SP_OK upon success, a negative error code otherwise.
694 * @since 0.1.0
696 enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
699 * Set the parity setting for the specified serial port.
701 * @param[in] port Pointer to a port structure. Must not be NULL.
702 * @param[in] parity Parity setting.
704 * @return SP_OK upon success, a negative error code otherwise.
706 * @since 0.1.0
708 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
711 * Get the parity setting from a port configuration.
713 * The user should allocate a variable of type enum sp_parity and
714 * pass a pointer to this to receive the result.
716 * @param[in] config Pointer to a configuration structure. Must not be NULL.
717 * @param[out] parity_ptr Pointer to a variable to store the result. Must not be NULL.
719 * @return SP_OK upon success, a negative error code otherwise.
721 * @since 0.1.0
723 enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
726 * Set the parity setting in a port configuration.
728 * @param[in] config Pointer to a configuration structure. Must not be NULL.
729 * @param[in] parity Parity setting, or -1 to retain the current setting.
731 * @return SP_OK upon success, a negative error code otherwise.
733 * @since 0.1.0
735 enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
738 * Set the stop bits for the specified serial port.
740 * @param[in] port Pointer to a port structure. Must not be NULL.
741 * @param[in] stopbits Number of stop bits.
743 * @return SP_OK upon success, a negative error code otherwise.
745 * @since 0.1.0
747 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
750 * Get the stop bits from a port configuration.
752 * The user should allocate a variable of type int and
753 * pass a pointer to this to receive the result.
755 * @param[in] config Pointer to a configuration structure. Must not be NULL.
756 * @param[out] stopbits_ptr Pointer to a variable to store the result. Must not be NULL.
758 * @return SP_OK upon success, a negative error code otherwise.
760 * @since 0.1.0
762 enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
765 * Set the stop bits in a port configuration.
767 * @param[in] config Pointer to a configuration structure. Must not be NULL.
768 * @param[in] stopbits Number of stop bits, or -1 to retain the current setting.
770 * @return SP_OK upon success, a negative error code otherwise.
772 * @since 0.1.0
774 enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
777 * Set the RTS pin behaviour for the specified serial port.
779 * @param[in] port Pointer to a port structure. Must not be NULL.
780 * @param[in] rts RTS pin mode.
782 * @return SP_OK upon success, a negative error code otherwise.
784 * @since 0.1.0
786 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
789 * Get the RTS pin behaviour from a port configuration.
791 * The user should allocate a variable of type enum sp_rts and
792 * pass a pointer to this to receive the result.
794 * @param[in] config Pointer to a configuration structure. Must not be NULL.
795 * @param[out] rts_ptr Pointer to a variable to store the result. Must not be NULL.
797 * @return SP_OK upon success, a negative error code otherwise.
799 * @since 0.1.0
801 enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
804 * Set the RTS pin behaviour in a port configuration.
806 * @param[in] config Pointer to a configuration structure. Must not be NULL.
807 * @param[in] rts RTS pin mode, or -1 to retain the current setting.
809 * @return SP_OK upon success, a negative error code otherwise.
811 * @since 0.1.0
813 enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
816 * Set the CTS pin behaviour for the specified serial port.
818 * @param[in] port Pointer to a port structure. Must not be NULL.
819 * @param[in] cts CTS pin mode.
821 * @return SP_OK upon success, a negative error code otherwise.
823 * @since 0.1.0
825 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
828 * Get the CTS pin behaviour from a port configuration.
830 * The user should allocate a variable of type enum sp_cts and
831 * pass a pointer to this to receive the result.
833 * @param[in] config Pointer to a configuration structure. Must not be NULL.
834 * @param[out] cts_ptr Pointer to a variable to store the result. Must not be NULL.
836 * @return SP_OK upon success, a negative error code otherwise.
838 * @since 0.1.0
840 enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
843 * Set the CTS pin behaviour in a port configuration.
845 * @param[in] config Pointer to a configuration structure. Must not be NULL.
846 * @param[in] cts CTS pin mode, or -1 to retain the current setting.
848 * @return SP_OK upon success, a negative error code otherwise.
850 * @since 0.1.0
852 enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
855 * Set the DTR pin behaviour for the specified serial port.
857 * @param[in] port Pointer to a port structure. Must not be NULL.
858 * @param[in] dtr DTR pin mode.
860 * @return SP_OK upon success, a negative error code otherwise.
862 * @since 0.1.0
864 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
867 * Get the DTR pin behaviour from a port configuration.
869 * The user should allocate a variable of type enum sp_dtr and
870 * pass a pointer to this to receive the result.
872 * @param[in] config Pointer to a configuration structure. Must not be NULL.
873 * @param[out] dtr_ptr Pointer to a variable to store the result. Must not be NULL.
875 * @return SP_OK upon success, a negative error code otherwise.
877 * @since 0.1.0
879 enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
882 * Set the DTR pin behaviour in a port configuration.
884 * @param[in] config Pointer to a configuration structure. Must not be NULL.
885 * @param[in] dtr DTR pin mode, or -1 to retain the current setting.
887 * @return SP_OK upon success, a negative error code otherwise.
889 * @since 0.1.0
891 enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
894 * Set the DSR pin behaviour for the specified serial port.
896 * @param[in] port Pointer to a port structure. Must not be NULL.
897 * @param[in] dsr DSR pin mode.
899 * @return SP_OK upon success, a negative error code otherwise.
901 * @since 0.1.0
903 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
906 * Get the DSR pin behaviour from a port configuration.
908 * The user should allocate a variable of type enum sp_dsr and
909 * pass a pointer to this to receive the result.
911 * @param[in] config Pointer to a configuration structure. Must not be NULL.
912 * @param[out] dsr_ptr Pointer to a variable to store the result. Must not be NULL.
914 * @return SP_OK upon success, a negative error code otherwise.
916 * @since 0.1.0
918 enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
921 * Set the DSR pin behaviour in a port configuration.
923 * @param[in] config Pointer to a configuration structure. Must not be NULL.
924 * @param[in] dsr DSR pin mode, or -1 to retain the current setting.
926 * @return SP_OK upon success, a negative error code otherwise.
928 * @since 0.1.0
930 enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
933 * Set the XON/XOFF configuration for the specified serial port.
935 * @param[in] port Pointer to a port structure. Must not be NULL.
936 * @param[in] xon_xoff XON/XOFF mode.
938 * @return SP_OK upon success, a negative error code otherwise.
940 * @since 0.1.0
942 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
945 * Get the XON/XOFF configuration from a port configuration.
947 * The user should allocate a variable of type enum sp_xonxoff and
948 * pass a pointer to this to receive the result.
950 * @param[in] config Pointer to a configuration structure. Must not be NULL.
951 * @param[out] xon_xoff_ptr Pointer to a variable to store the result. Must not be NULL.
953 * @return SP_OK upon success, a negative error code otherwise.
955 * @since 0.1.0
957 enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
960 * Set the XON/XOFF configuration in a port configuration.
962 * @param[in] config Pointer to a configuration structure. Must not be NULL.
963 * @param[in] xon_xoff XON/XOFF mode, or -1 to retain the current setting.
965 * @return SP_OK upon success, a negative error code otherwise.
967 * @since 0.1.0
969 enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
972 * Set the flow control type in a port configuration.
974 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
975 * XON/XOFF settings as necessary for the specified flow control
976 * type. For more fine-grained control of these settings, use their
977 * individual configuration functions.
979 * @param[in] config Pointer to a configuration structure. Must not be NULL.
980 * @param[in] flowcontrol Flow control setting to use.
982 * @return SP_OK upon success, a negative error code otherwise.
984 * @since 0.1.0
986 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
989 * Set the flow control type for the specified serial port.
991 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
992 * XON/XOFF settings as necessary for the specified flow control
993 * type. For more fine-grained control of these settings, use their
994 * individual configuration functions.
996 * @param[in] port Pointer to a port structure. Must not be NULL.
997 * @param[in] flowcontrol Flow control setting to use.
999 * @return SP_OK upon success, a negative error code otherwise.
1001 * @since 0.1.0
1003 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
1006 * @}
1008 * @defgroup Data Data handling
1010 * Reading, writing, and flushing data.
1012 * @{
1016 * Read bytes from the specified serial port, blocking until complete.
1018 * @warning If your program runs on Unix, defines its own signal handlers, and
1019 * needs to abort blocking reads when these are called, then you
1020 * should not use this function. It repeats system calls that return
1021 * with EINTR. To be able to abort a read from a signal handler, you
1022 * should implement your own blocking read using sp_nonblocking_read()
1023 * together with a blocking method that makes sense for your program.
1024 * E.g. you can obtain the file descriptor for an open port using
1025 * sp_get_port_handle() and use this to call select() or pselect(),
1026 * with appropriate arrangements to return if a signal is received.
1028 * @param[in] port Pointer to a port structure. Must not be NULL.
1029 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1030 * @param[in] count Requested number of bytes to read.
1031 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1033 * @return The number of bytes read on success, or a negative error code. If
1034 * the number of bytes returned is less than that requested, the
1035 * timeout was reached before the requested number of bytes was
1036 * available. If timeout is zero, the function will always return
1037 * either the requested number of bytes or a negative error code.
1039 * @since 0.1.0
1041 enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
1044 * Read bytes from the specified serial port, without blocking.
1046 * @param[in] port Pointer to a port structure. Must not be NULL.
1047 * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1048 * @param[in] count Maximum number of bytes to read.
1050 * @return The number of bytes read on success, or a negative error code. The
1051 * number of bytes returned may be any number from zero to the maximum
1052 * that was requested.
1054 * @since 0.1.0
1056 enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
1059 * Write bytes to the specified serial port, blocking until complete.
1061 * Note that this function only ensures that the accepted bytes have been
1062 * written to the OS; they may be held in driver or hardware buffers and not
1063 * yet physically transmitted. To check whether all written bytes have actually
1064 * been transmitted, use the sp_output_waiting() function. To wait until all
1065 * written bytes have actually been transmitted, use the sp_drain() function.
1067 * @warning If your program runs on Unix, defines its own signal handlers, and
1068 * needs to abort blocking writes when these are called, then you
1069 * should not use this function. It repeats system calls that return
1070 * with EINTR. To be able to abort a write from a signal handler, you
1071 * should implement your own blocking write using sp_nonblocking_write()
1072 * together with a blocking method that makes sense for your program.
1073 * E.g. you can obtain the file descriptor for an open port using
1074 * sp_get_port_handle() and use this to call select() or pselect(),
1075 * with appropriate arrangements to return if a signal is received.
1077 * @param[in] port Pointer to a port structure. Must not be NULL.
1078 * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1079 * @param[in] count Requested number of bytes to write.
1080 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1082 * @return The number of bytes written on success, or a negative error code.
1083 * If the number of bytes returned is less than that requested, the
1084 * timeout was reached before the requested number of bytes was
1085 * written. If timeout is zero, the function will always return
1086 * either the requested number of bytes or a negative error code. In
1087 * the event of an error there is no way to determine how many bytes
1088 * were sent before the error occurred.
1090 * @since 0.1.0
1092 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout_ms);
1095 * Write bytes to the specified serial port, without blocking.
1097 * Note that this function only ensures that the accepted bytes have been
1098 * written to the OS; they may be held in driver or hardware buffers and not
1099 * yet physically transmitted. To check whether all written bytes have actually
1100 * been transmitted, use the sp_output_waiting() function. To wait until all
1101 * written bytes have actually been transmitted, use the sp_drain() function.
1103 * @param[in] port Pointer to a port structure. Must not be NULL.
1104 * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1105 * @param[in] count Maximum number of bytes to write.
1107 * @return The number of bytes written on success, or a negative error code.
1108 * The number of bytes returned may be any number from zero to the
1109 * maximum that was requested.
1111 * @since 0.1.0
1113 enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
1116 * Gets the number of bytes waiting in the input buffer.
1118 * @param[in] port Pointer to a port structure. Must not be NULL.
1120 * @return Number of bytes waiting on success, a negative error code otherwise.
1122 * @since 0.1.0
1124 enum sp_return sp_input_waiting(struct sp_port *port);
1127 * Gets the number of bytes waiting in the output buffer.
1129 * @param[in] port Pointer to a port structure. Must not be NULL.
1131 * @return Number of bytes waiting on success, a negative error code otherwise.
1133 * @since 0.1.0
1135 enum sp_return sp_output_waiting(struct sp_port *port);
1138 * Flush serial port buffers. Data in the selected buffer(s) is discarded.
1140 * @param[in] port Pointer to a port structure. Must not be NULL.
1141 * @param[in] buffers Which buffer(s) to flush.
1143 * @return SP_OK upon success, a negative error code otherwise.
1145 * @since 0.1.0
1147 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
1150 * Wait for buffered data to be transmitted.
1152 * @warning If your program runs on Unix, defines its own signal handlers, and
1153 * needs to abort draining the output buffer when when these are
1154 * called, then you should not use this function. It repeats system
1155 * calls that return with EINTR. To be able to abort a drain from a
1156 * signal handler, you would need to implement your own blocking
1157 * drain by polling the result of sp_output_waiting().
1159 * @param[in] port Pointer to a port structure. Must not be NULL.
1161 * @return SP_OK upon success, a negative error code otherwise.
1163 * @since 0.1.0
1165 enum sp_return sp_drain(struct sp_port *port);
1168 * @}
1170 * @defgroup Waiting Waiting
1172 * Waiting for events and timeout handling.
1174 * @{
1178 * Allocate storage for a set of events.
1180 * The user should allocate a variable of type struct sp_event_set *,
1181 * then pass a pointer to this variable to receive the result.
1183 * The result should be freed after use by calling sp_free_event_set().
1185 * @param[out] result_ptr If any error is returned, the variable pointed to by
1186 * result_ptr will be set to NULL. Otherwise, it will
1187 * be set to point to the event set. Must not be NULL.
1189 * @return SP_OK upon success, a negative error code otherwise.
1191 * @since 0.1.0
1193 enum sp_return sp_new_event_set(struct sp_event_set **result_ptr);
1196 * Add events to a struct sp_event_set for a given port.
1198 * The port must first be opened by calling sp_open() using the same port
1199 * structure.
1201 * After the port is closed or the port structure freed, the results may
1202 * no longer be valid.
1204 * @param[in,out] event_set Event set to update. Must not be NULL.
1205 * @param[in] port Pointer to a port structure. Must not be NULL.
1206 * @param[in] mask Bitmask of events to be waited for.
1208 * @return SP_OK upon success, a negative error code otherwise.
1210 * @since 0.1.0
1212 enum sp_return sp_add_port_events(struct sp_event_set *event_set,
1213 const struct sp_port *port, enum sp_event mask);
1216 * Wait for any of a set of events to occur.
1218 * @param[in] event_set Event set to wait on. Must not be NULL.
1219 * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1221 * @return SP_OK upon success, a negative error code otherwise.
1223 * @since 0.1.0
1225 enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout_ms);
1228 * Free a structure allocated by sp_new_event_set().
1230 * @param[in] event_set Event set to free. Must not be NULL.
1232 * @since 0.1.0
1234 void sp_free_event_set(struct sp_event_set *event_set);
1237 * @}
1239 * @defgroup Signals Signals
1241 * Port signalling operations.
1243 * @{
1247 * Gets the status of the control signals for the specified port.
1249 * The user should allocate a variable of type "enum sp_signal" and pass a
1250 * pointer to this variable to receive the result. The result is a bitmask
1251 * in which individual signals can be checked by bitwise OR with values of
1252 * the sp_signal enum.
1254 * @param[in] port Pointer to a port structure. Must not be NULL.
1255 * @param[out] signal_mask Pointer to a variable to receive the result.
1256 * Must not be NULL.
1258 * @return SP_OK upon success, a negative error code otherwise.
1260 * @since 0.1.0
1262 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signal_mask);
1265 * Put the port transmit line into the break state.
1267 * @param[in] port Pointer to a port structure. Must not be NULL.
1269 * @return SP_OK upon success, a negative error code otherwise.
1271 * @since 0.1.0
1273 enum sp_return sp_start_break(struct sp_port *port);
1276 * Take the port transmit line out of the break state.
1278 * @param[in] port Pointer to a port structure. Must not be NULL.
1280 * @return SP_OK upon success, a negative error code otherwise.
1282 * @since 0.1.0
1284 enum sp_return sp_end_break(struct sp_port *port);
1287 * @}
1289 * @defgroup Errors Errors
1291 * Obtaining error information.
1293 * @{
1297 * Get the error code for a failed operation.
1299 * In order to obtain the correct result, this function should be called
1300 * straight after the failure, before executing any other system operations.
1302 * @return The system's numeric code for the error that caused the last
1303 * operation to fail.
1305 * @since 0.1.0
1307 int sp_last_error_code(void);
1310 * Get the error message for a failed operation.
1312 * In order to obtain the correct result, this function should be called
1313 * straight after the failure, before executing other system operations.
1315 * @return The system's message for the error that caused the last
1316 * operation to fail. This string may be allocated by the function,
1317 * and should be freed after use by calling sp_free_error_message().
1319 * @since 0.1.0
1321 char *sp_last_error_message(void);
1324 * Free an error message returned by sp_last_error_message().
1326 * @param[in] message The error message string to free. Must not be NULL.
1328 * @since 0.1.0
1330 void sp_free_error_message(char *message);
1333 * Set the handler function for library debugging messages.
1335 * Debugging messages are generated by the library during each operation,
1336 * to help in diagnosing problems. The handler will be called for each
1337 * message. The handler can be set to NULL to ignore all debug messages.
1339 * The handler function should accept a format string and variable length
1340 * argument list, in the same manner as e.g. printf().
1342 * The default handler is sp_default_debug_handler().
1344 * @param[in] handler The handler function to use. Can be NULL (in that case
1345 * all debug messages will be ignored).
1347 * @since 0.1.0
1349 void sp_set_debug_handler(void (*handler)(const char *format, ...));
1352 * Default handler function for library debugging messages.
1354 * This function prints debug messages to the standard error stream if the
1355 * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
1356 * ignored.
1358 * @param[in] format The format string to use. Must not be NULL.
1359 * @param[in] ... The variable length argument list to use.
1361 * @since 0.1.0
1363 void sp_default_debug_handler(const char *format, ...);
1365 /** @} */
1368 * @defgroup Versions Versions
1370 * Version number querying functions, definitions, and macros.
1372 * This set of API calls returns two different version numbers related
1373 * to libserialport. The "package version" is the release version number of the
1374 * libserialport tarball in the usual "major.minor.micro" format, e.g. "0.1.0".
1376 * The "library version" is independent of that; it is the libtool version
1377 * number in the "current:revision:age" format, e.g. "2:0:0".
1378 * See http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning for details.
1380 * Both version numbers (and/or individual components of them) can be
1381 * retrieved via the API calls at runtime, and/or they can be checked at
1382 * compile/preprocessor time using the respective macros.
1384 * @{
1388 * Package version macros (can be used for conditional compilation).
1391 /** The libserialport package 'major' version number. */
1392 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
1394 /** The libserialport package 'minor' version number. */
1395 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
1397 /** The libserialport package 'micro' version number. */
1398 #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
1400 /** The libserialport package version ("major.minor.micro") as string. */
1401 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
1404 * Library/libtool version macros (can be used for conditional compilation).
1407 /** The libserialport libtool 'current' version number. */
1408 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
1410 /** The libserialport libtool 'revision' version number. */
1411 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
1413 /** The libserialport libtool 'age' version number. */
1414 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
1416 /** The libserialport libtool version ("current:revision:age") as string. */
1417 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
1420 * Get the major libserialport package version number.
1422 * @return The major package version number.
1424 * @since 0.1.0
1426 int sp_get_major_package_version(void);
1429 * Get the minor libserialport package version number.
1431 * @return The minor package version number.
1433 * @since 0.1.0
1435 int sp_get_minor_package_version(void);
1438 * Get the micro libserialport package version number.
1440 * @return The micro package version number.
1442 * @since 0.1.0
1444 int sp_get_micro_package_version(void);
1447 * Get the libserialport package version number as a string.
1449 * @return The package version number string. The returned string is
1450 * static and thus should NOT be free'd by the caller.
1452 * @since 0.1.0
1454 const char *sp_get_package_version_string(void);
1457 * Get the "current" part of the libserialport library version number.
1459 * @return The "current" library version number.
1461 * @since 0.1.0
1463 int sp_get_current_lib_version(void);
1466 * Get the "revision" part of the libserialport library version number.
1468 * @return The "revision" library version number.
1470 * @since 0.1.0
1472 int sp_get_revision_lib_version(void);
1475 * Get the "age" part of the libserialport library version number.
1477 * @return The "age" library version number.
1479 * @since 0.1.0
1481 int sp_get_age_lib_version(void);
1484 * Get the libserialport library version number as a string.
1486 * @return The library version number string. The returned string is
1487 * static and thus should NOT be free'd by the caller.
1489 * @since 0.1.0
1491 const char *sp_get_lib_version_string(void);
1493 /** @} */
1495 #ifdef __cplusplus
1497 #endif
1499 #endif