]> sigrok.org Git - libserialport.git/blob - libserialport.h.in
doc: Expanded introductory text.
[libserialport.git] / libserialport.h.in
1 /*
2  * This file is part of the libserialport project.
3  *
4  * Copyright (C) 2013 Martin Ling <martin-libserialport@earth.li>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @mainpage libserialport API
22  *
23  * Introduction
24  * ============
25  *
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.
28  *
29  * By writing your serial code to use libserialport, you enable it to work
30  * transparently on any platform supported by the library.
31  *
32  * libserialport is an open source project released under the LGPL3+ license.
33  *
34  * The library is maintained by the [sigrok](http://sigrok.org/) project. See
35  * the [libserialport homepage](http://sigrok.org/wiki/Libserialport) for the
36  * latest information.
37  *
38  * Source code is maintained in git at
39  * [git://sigrok.org/libserialport](http://sigrok.org/gitweb/?p=libserialport.git).
40  *
41  * Bugs are tracked at http://sigrok.org/bugzilla/.
42  *
43  * The library was conceived and designed by Martin Ling, is maintained by
44  * Uwe Hermann, and has received contributions from several other developers.
45  * See the git history for full credits.
46  *
47  * API information
48  * ===============
49  *
50  * The API has been designed from scratch. It does not exactly resemble the
51  * serial API of any particular operating system. Instead it aims to provide
52  * a set of functions that can reliably be implemented across all operating
53  * systems. These form a sufficient basis for higher level behaviour to
54  * be implemented in a platform independent manner.
55  *
56  * If you are porting code written for a particular OS, you may find you need
57  * to restructure things somewhat, or do without some specialised features.
58  * For particular notes on porting existing code, see @ref Porting.
59  *
60  * The following subsections will help explain the principles of the API.
61  * To jump directly into the detailed function documentation, see the
62  * <a href="modules.html">categorised function lists</a>.
63  *
64  * Data structures
65  * ---------------
66  *
67  * The library defines three data structures:
68  *
69  * - @ref sp_port, which represents a serial port.
70  *   See @ref Enumeration.
71  * - @ref sp_port_config, which represents a port configuration.
72  *   See @ref Configuration.
73  * - @ref sp_event_set, which represents a set of events.
74  *   See @ref Waiting.
75  *
76  * All these structures are allocated and freed by library functions. It is
77  * the caller's responsibility to ensure that the correct calls are made to
78  * free allocated structures after use.
79  *
80  * Return codes and error handling
81  * -------------------------------
82  *
83  * Most functions have return type @ref sp_return and can return only four
84  * possible error values:
85  *
86  * - @ref SP_ERR_ARG means that a function was called with invalid
87  *   arguments. This implies a bug in the caller. The arguments passed would
88  *   be invalid regardless of the underlying OS or serial device involved.
89  *
90  * - @ref SP_ERR_FAIL means that the OS reported a failure. The error code or
91  *   message provided by the OS can be obtained by calling sp_last_error_code()
92  *   or sp_last_error_message().
93  *
94  * - @ref SP_ERR_SUPP indicates that there is no support for the requested
95  *   operation in the current OS, driver or device. No error message is
96  *   available from the OS in this case. There is either no way to request
97  *   the operation in the first place, or libserialport does not know how to
98  *   do so in the current version.
99  *
100  * - @ref SP_ERR_MEM indicates that a memory allocation failed.
101  *
102  * All of these error values are negative.
103  *
104  * Calls that succeed return @ref SP_OK, which is equal to zero. Some functions
105  * declared @ref sp_return can also return a positive value for a successful
106  * numeric result, e.g. sp_blocking_read() or sp_blocking_write().
107  *
108  * An error message is only available via sp_last_error_message() in the case
109  * where SP_ERR_FAIL was returned by the previous function call. The error
110  * message returned is that provided by the OS, using the current language
111  * settings. It is an error to call sp_last_error_code() or
112  * sp_last_error_message() except after a previous function call returned
113  * SP_ERR_FAIL. The library does not define its own error codes or messages
114  * to accompany other return codes.
115  *
116  * Thread safety
117  * -------------
118  *
119  * Certain combinations of calls can be made concurrently, as follows.
120  *
121  * - Calls using different ports may always be made concurrently, i.e.
122  *   it is safe for separate threads to handle their own ports.
123  *
124  * - Calls using the same port may be made concurrently when one call
125  *   is a read operation and one call is a write operation, i.e. it is safe
126  *   to use separate "reader" and "writer" threads for the same port. See
127  *   below for which operations meet these definitions.
128  *
129  * Read operations:
130  *
131  * - sp_blocking_read()
132  * - sp_blocking_read_next()
133  * - sp_nonblocking_read()
134  * - sp_input_waiting()
135  * - sp_flush() with @ref SP_BUF_INPUT only.
136  * - sp_wait() with @ref SP_EVENT_RX_READY only.
137  *
138  * Write operations:
139  *
140  * - sp_blocking_write()
141  * - sp_nonblocking_write()
142  * - sp_output_waiting()
143  * - sp_drain()
144  * - sp_flush() with @ref SP_BUF_OUTPUT only.
145  * - sp_wait() with @ref SP_EVENT_TX_READY only.
146  *
147  * If two calls, on the same port, do not fit into one of these categories
148  * each, then they may not be made concurrently.
149  *
150  * Debugging
151  * ---------
152  *
153  * The library can output extensive tracing and debugging information. The
154  * simplest way to use this is to set the environment variable
155  * LIBSERIALPORT_DEBUG to any value; messages will then be output to the
156  * standard error stream.
157  *
158  * This behaviour is implemented by a default debug message handling
159  * callback. An alternative callback can be set using sp_set_debug_handler(),
160  * in order to e.g. redirect the output elsewhere or filter it.
161  *
162  * No guarantees are made about the content of the debug output; it is chosen
163  * to suit the needs of the developers and may change between releases.
164  *
165  * @anchor Porting
166  * Porting
167  * -------
168  *
169  * The following guidelines may help when porting existing OS-specific code
170  * to use libserialport.
171  *
172  * ### Porting from Unix-like systems ###
173  *
174  * There are two main differences to note when porting code written for Unix.
175  *
176  * The first is that Unix traditionally provides a wide range of functionality
177  * for dealing with serial devices at the OS level; this is exposed through the
178  * termios API and dates to the days when serial terminals were common. If your
179  * code relies on many of these facilities you will need to adapt it, because
180  * libserialport provides only a raw 8-bit channel with no special handling.
181  *
182  * The second relates to blocking versus non-blocking I/O behaviour. In
183  * Unix-like systems this is normally specified by setting the O_NONBLOCK
184  * flag on the file descriptor, affecting the semantics of subsequent read()
185  * and write() calls.
186  *
187  * In libserialport, blocking and nonblocking operations are both available at
188  * any time. If your existing code Ń•ets O_NONBLOCK, you should use
189  * sp_nonblocking_read() and sp_nonblocking_write() to get the same behaviour
190  * as your existing read() and write() calls. If it does not, you should use
191  * sp_blocking_read() and sp_blocking_write() instead. You may also find
192  * sp_blocking_read_next() useful, which reproduces the semantics of a blocking
193  * read() with VTIME = 0 and VMIN = 1 set in termios.
194  *
195  * Finally, you should take care if your program uses custom signal handlers.
196  * The blocking calls provided by libserialport will restart system calls that
197  * return with EINTR, so you will need to make your own arrangements if you
198  * need to interrupt blocking operations when your signal handlers are called.
199  * This is not an issue if you only use the default handlers.
200  *
201  * ### Porting from Windows ###
202  *
203  * The main consideration when porting from Windows is that there is no
204  * direct equivalent for overlapped I/O operations.
205  *
206  * If your program does not use overlapped I/O, you can simply use
207  * sp_blocking_read() and sp_blocking_write() as direct equivalents for
208  * ReadFile() and WriteFile(). You may also find sp_blocking_read_next()
209  * useful, which reproduces the special semantics of ReadFile() with
210  * ReadIntervalTimeout and ReadTotalTimeoutMultiplier set to MAXDWORD
211  * and 0 < ReadTotalTimeoutConstant < MAXDWORD.
212  *
213  * If your program makes use of overlapped I/O to continue work while a serial
214  * operation is in progress, then you can achieve the same results using
215  * sp_nonblocking_read() and sp_nonblocking_write().
216  *
217  * Generally, overlapped I/O is combined with either waiting for completion
218  * once there is no more background work to do (using WaitForSingleObject() or
219  * WaitForMultipleObjects()), or periodically checking for completion with
220  * GetOverlappedResult(). If the aim is to start a new operation for further
221  * data once the previous one has completed, you can instead simply call the
222  * nonblocking functions again with the next data. If you need to wait for
223  * completion, use sp_wait() to determine when the port is ready to send or
224  * receive further data.
225  */
226
227 #ifndef LIBSERIALPORT_LIBSERIALPORT_H
228 #define LIBSERIALPORT_LIBSERIALPORT_H
229
230 #ifdef __cplusplus
231 extern "C" {
232 #endif
233
234 #include <stddef.h>
235
236 /** Return values. */
237 enum sp_return {
238         /** Operation completed successfully. */
239         SP_OK = 0,
240         /** Invalid arguments were passed to the function. */
241         SP_ERR_ARG = -1,
242         /** A system error occurred while executing the operation. */
243         SP_ERR_FAIL = -2,
244         /** A memory allocation failed while executing the operation. */
245         SP_ERR_MEM = -3,
246         /** The requested operation is not supported by this system or device. */
247         SP_ERR_SUPP = -4
248 };
249
250 /** Port access modes. */
251 enum sp_mode {
252         /** Open port for read access. */
253         SP_MODE_READ = 1,
254         /** Open port for write access. */
255         SP_MODE_WRITE = 2,
256         /** Open port for read and write access. @since 0.1.1 */
257         SP_MODE_READ_WRITE = 3
258 };
259
260 /** Port events. */
261 enum sp_event {
262         /** Data received and ready to read. */
263         SP_EVENT_RX_READY = 1,
264         /** Ready to transmit new data. */
265         SP_EVENT_TX_READY = 2,
266         /** Error occurred. */
267         SP_EVENT_ERROR = 4
268 };
269
270 /** Buffer selection. */
271 enum sp_buffer {
272         /** Input buffer. */
273         SP_BUF_INPUT = 1,
274         /** Output buffer. */
275         SP_BUF_OUTPUT = 2,
276         /** Both buffers. */
277         SP_BUF_BOTH = 3
278 };
279
280 /** Parity settings. */
281 enum sp_parity {
282         /** Special value to indicate setting should be left alone. */
283         SP_PARITY_INVALID = -1,
284         /** No parity. */
285         SP_PARITY_NONE = 0,
286         /** Odd parity. */
287         SP_PARITY_ODD = 1,
288         /** Even parity. */
289         SP_PARITY_EVEN = 2,
290         /** Mark parity. */
291         SP_PARITY_MARK = 3,
292         /** Space parity. */
293         SP_PARITY_SPACE = 4
294 };
295
296 /** RTS pin behaviour. */
297 enum sp_rts {
298         /** Special value to indicate setting should be left alone. */
299         SP_RTS_INVALID = -1,
300         /** RTS off. */
301         SP_RTS_OFF = 0,
302         /** RTS on. */
303         SP_RTS_ON = 1,
304         /** RTS used for flow control. */
305         SP_RTS_FLOW_CONTROL = 2
306 };
307
308 /** CTS pin behaviour. */
309 enum sp_cts {
310         /** Special value to indicate setting should be left alone. */
311         SP_CTS_INVALID = -1,
312         /** CTS ignored. */
313         SP_CTS_IGNORE = 0,
314         /** CTS used for flow control. */
315         SP_CTS_FLOW_CONTROL = 1
316 };
317
318 /** DTR pin behaviour. */
319 enum sp_dtr {
320         /** Special value to indicate setting should be left alone. */
321         SP_DTR_INVALID = -1,
322         /** DTR off. */
323         SP_DTR_OFF = 0,
324         /** DTR on. */
325         SP_DTR_ON = 1,
326         /** DTR used for flow control. */
327         SP_DTR_FLOW_CONTROL = 2
328 };
329
330 /** DSR pin behaviour. */
331 enum sp_dsr {
332         /** Special value to indicate setting should be left alone. */
333         SP_DSR_INVALID = -1,
334         /** DSR ignored. */
335         SP_DSR_IGNORE = 0,
336         /** DSR used for flow control. */
337         SP_DSR_FLOW_CONTROL = 1
338 };
339
340 /** XON/XOFF flow control behaviour. */
341 enum sp_xonxoff {
342         /** Special value to indicate setting should be left alone. */
343         SP_XONXOFF_INVALID = -1,
344         /** XON/XOFF disabled. */
345         SP_XONXOFF_DISABLED = 0,
346         /** XON/XOFF enabled for input only. */
347         SP_XONXOFF_IN = 1,
348         /** XON/XOFF enabled for output only. */
349         SP_XONXOFF_OUT = 2,
350         /** XON/XOFF enabled for input and output. */
351         SP_XONXOFF_INOUT = 3
352 };
353
354 /** Standard flow control combinations. */
355 enum sp_flowcontrol {
356         /** No flow control. */
357         SP_FLOWCONTROL_NONE = 0,
358         /** Software flow control using XON/XOFF characters. */
359         SP_FLOWCONTROL_XONXOFF = 1,
360         /** Hardware flow control using RTS/CTS signals. */
361         SP_FLOWCONTROL_RTSCTS = 2,
362         /** Hardware flow control using DTR/DSR signals. */
363         SP_FLOWCONTROL_DTRDSR = 3
364 };
365
366 /** Input signals. */
367 enum sp_signal {
368         /** Clear to send. */
369         SP_SIG_CTS = 1,
370         /** Data set ready. */
371         SP_SIG_DSR = 2,
372         /** Data carrier detect. */
373         SP_SIG_DCD = 4,
374         /** Ring indicator. */
375         SP_SIG_RI = 8
376 };
377
378 /**
379  * Transport types.
380  *
381  * @since 0.1.1
382  */
383 enum sp_transport {
384         /** Native platform serial port. @since 0.1.1 */
385         SP_TRANSPORT_NATIVE,
386         /** USB serial port adapter. @since 0.1.1 */
387         SP_TRANSPORT_USB,
388         /** Bluetooth serial port adapter. @since 0.1.1 */
389         SP_TRANSPORT_BLUETOOTH
390 };
391
392 /**
393  * @struct sp_port
394  * An opaque structure representing a serial port.
395  */
396 struct sp_port;
397
398 /**
399  * @struct sp_port_config
400  * An opaque structure representing the configuration for a serial port.
401  */
402 struct sp_port_config;
403
404 /**
405  * @struct sp_event_set
406  * A set of handles to wait on for events.
407  */
408 struct sp_event_set {
409         /** Array of OS-specific handles. */
410         void *handles;
411         /** Array of bitmasks indicating which events apply for each handle. */
412         enum sp_event *masks;
413         /** Number of handles. */
414         unsigned int count;
415 };
416
417 /**
418  * @defgroup Enumeration Port enumeration
419  *
420  * Enumerating the serial ports of a system.
421  *
422  * @{
423  */
424
425 /**
426  * Obtain a pointer to a new sp_port structure representing the named port.
427  *
428  * The user should allocate a variable of type "struct sp_port *" and pass a
429  * pointer to this to receive the result.
430  *
431  * The result should be freed after use by calling sp_free_port().
432  *
433  * @param[in] portname The OS-specific name of a serial port. Must not be NULL.
434  * @param[out] port_ptr If any error is returned, the variable pointed to by
435  *                      port_ptr will be set to NULL. Otherwise, it will be set
436  *                      to point to the newly allocated port. Must not be NULL.
437  *
438  * @return SP_OK upon success, a negative error code otherwise.
439  *
440  * @since 0.1.0
441  */
442 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
443
444 /**
445  * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
446  *
447  * @param[in] port Pointer to a port structure. Must not be NULL.
448  *
449  * @since 0.1.0
450  */
451 void sp_free_port(struct sp_port *port);
452
453 /**
454  * List the serial ports available on the system.
455  *
456  * The result obtained is an array of pointers to sp_port structures,
457  * terminated by a NULL. The user should allocate a variable of type
458  * "struct sp_port **" and pass a pointer to this to receive the result.
459  *
460  * The result should be freed after use by calling sp_free_port_list().
461  * If a port from the list is to be used after freeing the list, it must be
462  * copied first using sp_copy_port().
463  *
464  * @param[out] list_ptr If any error is returned, the variable pointed to by
465  *                      list_ptr will be set to NULL. Otherwise, it will be set
466  *                      to point to the newly allocated array. Must not be NULL.
467  *
468  * @return SP_OK upon success, a negative error code otherwise.
469  *
470  * @since 0.1.0
471  */
472 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
473
474 /**
475  * Make a new copy of an sp_port structure.
476  *
477  * The user should allocate a variable of type "struct sp_port *" and pass a
478  * pointer to this to receive the result.
479  *
480  * The copy should be freed after use by calling sp_free_port().
481  *
482  * @param[in] port Pointer to a port structure. Must not be NULL.
483  * @param[out] copy_ptr If any error is returned, the variable pointed to by
484  *                      copy_ptr will be set to NULL. Otherwise, it will be set
485  *                      to point to the newly allocated copy. Must not be NULL.
486  *
487  * @return SP_OK upon success, a negative error code otherwise.
488  *
489  * @since 0.1.0
490  */
491 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
492
493 /**
494  * Free a port list obtained from sp_list_ports().
495  *
496  * This will also free all the sp_port structures referred to from the list;
497  * any that are to be retained must be copied first using sp_copy_port().
498  *
499  * @param[in] ports Pointer to a list of port structures. Must not be NULL.
500  *
501  * @since 0.1.0
502  */
503 void sp_free_port_list(struct sp_port **ports);
504
505 /**
506  * @}
507  * @defgroup Ports Port handling
508  *
509  * Opening, closing and querying ports.
510  *
511  * @{
512  */
513
514 /**
515  * Open the specified serial port.
516  *
517  * @param[in] port Pointer to a port structure. Must not be NULL.
518  * @param[in] flags Flags to use when opening the serial port.
519  *
520  * @return SP_OK upon success, a negative error code otherwise.
521  *
522  * @since 0.1.0
523  */
524 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
525
526 /**
527  * Close the specified serial port.
528  *
529  * @param[in] port Pointer to a port structure. Must not be NULL.
530  *
531  * @return SP_OK upon success, a negative error code otherwise.
532  *
533  * @since 0.1.0
534  */
535 enum sp_return sp_close(struct sp_port *port);
536
537 /**
538  * Get the name of a port.
539  *
540  * The name returned is whatever is normally used to refer to a port on the
541  * current operating system; e.g. for Windows it will usually be a "COMn"
542  * device name, and for Unix it will be a device path beginning with "/dev/".
543  *
544  * @param[in] port Pointer to a port structure. Must not be NULL.
545  *
546  * @return The port name, or NULL if an invalid port is passed. The name
547  *         string is part of the port structure and may not be used after
548  *         the port structure has been freed.
549  *
550  * @since 0.1.0
551  */
552 char *sp_get_port_name(const struct sp_port *port);
553
554 /**
555  * Get a description for a port, to present to end user.
556  *
557  * @param[in] port Pointer to a port structure. Must not be NULL.
558  *
559  * @return The port description, or NULL if an invalid port is passed.
560  *         The description string is part of the port structure and may not
561  *         be used after the port structure has been freed.
562  *
563  * @since 0.1.1
564  */
565 char *sp_get_port_description(const struct sp_port *port);
566
567 /**
568  * Get the transport type used by a port.
569  *
570  * @param[in] port Pointer to a port structure. Must not be NULL.
571  *
572  * @return The port transport type.
573  *
574  * @since 0.1.1
575  */
576 enum sp_transport sp_get_port_transport(const struct sp_port *port);
577
578 /**
579  * Get the USB bus number and address on bus of a USB serial adapter port.
580  *
581  * @param[in] port Pointer to a port structure. Must not be NULL.
582  * @param[out] usb_bus Pointer to a variable to store the USB bus.
583  *                     Can be NULL (in that case it will be ignored).
584  * @param[out] usb_address Pointer to a variable to store the USB address.
585  *                         Can be NULL (in that case it will be ignored).
586  *
587  * @return SP_OK upon success, a negative error code otherwise.
588  *
589  * @since 0.1.1
590  */
591 enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port,
592                                            int *usb_bus, int *usb_address);
593
594 /**
595  * Get the USB Vendor ID and Product ID of a USB serial adapter port.
596  *
597  * @param[in] port Pointer to a port structure. Must not be NULL.
598  * @param[out] usb_vid Pointer to a variable to store the USB VID.
599  *                     Can be NULL (in that case it will be ignored).
600  * @param[out] usb_pid Pointer to a variable to store the USB PID.
601  *                     Can be NULL (in that case it will be ignored).
602  *
603  * @return SP_OK upon success, a negative error code otherwise.
604  *
605  * @since 0.1.1
606  */
607 enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, int *usb_vid, int *usb_pid);
608
609 /**
610  * Get the USB manufacturer string of a USB serial adapter port.
611  *
612  * @param[in] port Pointer to a port structure. Must not be NULL.
613  *
614  * @return The port manufacturer string, or NULL if an invalid port is passed.
615  *         The manufacturer string is part of the port structure and may not
616  *         be used after the port structure has been freed.
617  *
618  * @since 0.1.1
619  */
620 char *sp_get_port_usb_manufacturer(const struct sp_port *port);
621
622 /**
623  * Get the USB product string of a USB serial adapter port.
624  *
625  * @param[in] port Pointer to a port structure. Must not be NULL.
626  *
627  * @return The port product string, or NULL if an invalid port is passed.
628  *         The product string is part of the port structure and may not be
629  *         used after the port structure has been freed.
630  *
631  * @since 0.1.1
632  */
633 char *sp_get_port_usb_product(const struct sp_port *port);
634
635 /**
636  * Get the USB serial number string of a USB serial adapter port.
637  *
638  * @param[in] port Pointer to a port structure. Must not be NULL.
639  *
640  * @return The port serial number, or NULL if an invalid port is passed.
641  *         The serial number string is part of the port structure and may
642  *         not be used after the port structure has been freed.
643  *
644  * @since 0.1.1
645  */
646 char *sp_get_port_usb_serial(const struct sp_port *port);
647
648 /**
649  * Get the MAC address of a Bluetooth serial adapter port.
650  *
651  * @param[in] port Pointer to a port structure. Must not be NULL.
652  *
653  * @return The port MAC address, or NULL if an invalid port is passed.
654  *         The MAC address string is part of the port structure and may not
655  *         be used after the port structure has been freed.
656  *
657  * @since 0.1.1
658  */
659 char *sp_get_port_bluetooth_address(const struct sp_port *port);
660
661 /**
662  * Get the operating system handle for a port.
663  *
664  * The type of the handle depends on the operating system. On Unix based
665  * systems, the handle is a file descriptor of type "int". On Windows, the
666  * handle is of type "HANDLE". The user should allocate a variable of the
667  * appropriate type and pass a pointer to this to receive the result.
668  *
669  * To obtain a valid handle, the port must first be opened by calling
670  * sp_open() using the same port structure.
671  *
672  * After the port is closed or the port structure freed, the handle may
673  * no longer be valid.
674  *
675  * @warning This feature is provided so that programs may make use of
676  *          OS-specific functionality where desired. Doing so obviously
677  *          comes at a cost in portability. It also cannot be guaranteed
678  *          that direct usage of the OS handle will not conflict with the
679  *          library's own usage of the port. Be careful.
680  *
681  * @param[in] port Pointer to a port structure. Must not be NULL.
682  * @param[out] result_ptr If any error is returned, the variable pointed to by
683  *                        result_ptr will have unknown contents and should not
684  *                        be used. Otherwise, it will be set to point to the
685  *                        OS handle. Must not be NULL.
686  *
687  * @return SP_OK upon success, a negative error code otherwise.
688  *
689  * @since 0.1.0
690  */
691 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
692
693 /**
694  * @}
695  *
696  * @defgroup Configuration Configuration
697  *
698  * Setting and querying serial port parameters.
699  * @{
700  */
701
702 /**
703  * Allocate a port configuration structure.
704  *
705  * The user should allocate a variable of type "struct sp_port_config *" and
706  * pass a pointer to this to receive the result. The variable will be updated
707  * to point to the new configuration structure. The structure is opaque and
708  * must be accessed via the functions provided.
709  *
710  * All parameters in the structure will be initialised to special values which
711  * are ignored by sp_set_config().
712  *
713  * The structure should be freed after use by calling sp_free_config().
714  *
715  * @param[out] config_ptr If any error is returned, the variable pointed to by
716  *                        config_ptr will be set to NULL. Otherwise, it will
717  *                        be set to point to the allocated config structure.
718  *                        Must not be NULL.
719  *
720  * @return SP_OK upon success, a negative error code otherwise.
721  *
722  * @since 0.1.0
723  */
724 enum sp_return sp_new_config(struct sp_port_config **config_ptr);
725
726 /**
727  * Free a port configuration structure.
728  *
729  * @param[in] config Pointer to a configuration structure. Must not be NULL.
730  *
731  * @since 0.1.0
732  */
733 void sp_free_config(struct sp_port_config *config);
734
735 /**
736  * Get the current configuration of the specified serial port.
737  *
738  * The user should allocate a configuration structure using sp_new_config()
739  * and pass this as the config parameter. The configuration structure will
740  * be updated with the port configuration.
741  *
742  * Any parameters that are configured with settings not recognised or
743  * supported by libserialport, will be set to special values that are
744  * ignored by sp_set_config().
745  *
746  * @param[in] port Pointer to a port structure. Must not be NULL.
747  * @param[out] config Pointer to a configuration structure that will hold
748  *                    the result. Upon errors the contents of the config
749  *                    struct will not be changed. Must not be NULL.
750  *
751  * @return SP_OK upon success, a negative error code otherwise.
752  *
753  * @since 0.1.0
754  */
755 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
756
757 /**
758  * Set the configuration for the specified serial port.
759  *
760  * For each parameter in the configuration, there is a special value (usually
761  * -1, but see the documentation for each field). These values will be ignored
762  * and the corresponding setting left unchanged on the port.
763  *
764  * Upon errors, the configuration of the serial port is unknown since
765  * partial/incomplete config updates may have happened.
766  *
767  * @param[in] port Pointer to a port structure. Must not be NULL.
768  * @param[in] config Pointer to a configuration structure. Must not be NULL.
769  *
770  * @return SP_OK upon success, a negative error code otherwise.
771  *
772  * @since 0.1.0
773  */
774 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
775
776 /**
777  * Set the baud rate for the specified serial port.
778  *
779  * @param[in] port Pointer to a port structure. Must not be NULL.
780  * @param[in] baudrate Baud rate in bits per second.
781  *
782  * @return SP_OK upon success, a negative error code otherwise.
783  *
784  * @since 0.1.0
785  */
786 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
787
788 /**
789  * Get the baud rate from a port configuration.
790  *
791  * The user should allocate a variable of type int and
792  * pass a pointer to this to receive the result.
793  *
794  * @param[in] config Pointer to a configuration structure. Must not be NULL.
795  * @param[out] baudrate_ptr Pointer to a variable to store the result. Must not be NULL.
796  *
797  * @return SP_OK upon success, a negative error code otherwise.
798  *
799  * @since 0.1.0
800  */
801 enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
802
803 /**
804  * Set the baud rate in a port configuration.
805  *
806  * @param[in] config Pointer to a configuration structure. Must not be NULL.
807  * @param[in] baudrate Baud rate in bits per second, or -1 to retain the current setting.
808  *
809  * @return SP_OK upon success, a negative error code otherwise.
810  *
811  * @since 0.1.0
812  */
813 enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
814
815 /**
816  * Set the data bits for the specified serial port.
817  *
818  * @param[in] port Pointer to a port structure. Must not be NULL.
819  * @param[in] bits Number of data bits.
820  *
821  * @return SP_OK upon success, a negative error code otherwise.
822  *
823  * @since 0.1.0
824  */
825 enum sp_return sp_set_bits(struct sp_port *port, int bits);
826
827 /**
828  * Get the data bits from a port configuration.
829  *
830  * The user should allocate a variable of type int and
831  * pass a pointer to this to receive the result.
832  *
833  * @param[in] config Pointer to a configuration structure. Must not be NULL.
834  * @param[out] bits_ptr Pointer to a variable to store the result. Must not be NULL.
835  *
836  * @return SP_OK upon success, a negative error code otherwise.
837  *
838  * @since 0.1.0
839  */
840 enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
841
842 /**
843  * Set the data bits in a port configuration.
844  *
845  * @param[in] config Pointer to a configuration structure. Must not be NULL.
846  * @param[in] bits Number of data bits, or -1 to retain the current setting.
847  *
848  * @return SP_OK upon success, a negative error code otherwise.
849  *
850  * @since 0.1.0
851  */
852 enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
853
854 /**
855  * Set the parity setting for the specified serial port.
856  *
857  * @param[in] port Pointer to a port structure. Must not be NULL.
858  * @param[in] parity Parity setting.
859  *
860  * @return SP_OK upon success, a negative error code otherwise.
861  *
862  * @since 0.1.0
863  */
864 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
865
866 /**
867  * Get the parity setting from a port configuration.
868  *
869  * The user should allocate a variable of type enum sp_parity and
870  * pass a pointer to this to receive the result.
871  *
872  * @param[in] config Pointer to a configuration structure. Must not be NULL.
873  * @param[out] parity_ptr Pointer to a variable to store the result. Must not be NULL.
874  *
875  * @return SP_OK upon success, a negative error code otherwise.
876  *
877  * @since 0.1.0
878  */
879 enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
880
881 /**
882  * Set the parity setting in a port configuration.
883  *
884  * @param[in] config Pointer to a configuration structure. Must not be NULL.
885  * @param[in] parity Parity setting, or -1 to retain the current setting.
886  *
887  * @return SP_OK upon success, a negative error code otherwise.
888  *
889  * @since 0.1.0
890  */
891 enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
892
893 /**
894  * Set the stop bits for the specified serial port.
895  *
896  * @param[in] port Pointer to a port structure. Must not be NULL.
897  * @param[in] stopbits Number of stop bits.
898  *
899  * @return SP_OK upon success, a negative error code otherwise.
900  *
901  * @since 0.1.0
902  */
903 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
904
905 /**
906  * Get the stop bits from a port configuration.
907  *
908  * The user should allocate a variable of type int and
909  * pass a pointer to this to receive the result.
910  *
911  * @param[in] config Pointer to a configuration structure. Must not be NULL.
912  * @param[out] stopbits_ptr Pointer to a variable to store the result. Must not be NULL.
913  *
914  * @return SP_OK upon success, a negative error code otherwise.
915  *
916  * @since 0.1.0
917  */
918 enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
919
920 /**
921  * Set the stop bits in a port configuration.
922  *
923  * @param[in] config Pointer to a configuration structure. Must not be NULL.
924  * @param[in] stopbits Number of stop bits, or -1 to retain the current setting.
925  *
926  * @return SP_OK upon success, a negative error code otherwise.
927  *
928  * @since 0.1.0
929  */
930 enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
931
932 /**
933  * Set the RTS pin behaviour for the specified serial port.
934  *
935  * @param[in] port Pointer to a port structure. Must not be NULL.
936  * @param[in] rts RTS pin mode.
937  *
938  * @return SP_OK upon success, a negative error code otherwise.
939  *
940  * @since 0.1.0
941  */
942 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
943
944 /**
945  * Get the RTS pin behaviour from a port configuration.
946  *
947  * The user should allocate a variable of type enum sp_rts and
948  * pass a pointer to this to receive the result.
949  *
950  * @param[in] config Pointer to a configuration structure. Must not be NULL.
951  * @param[out] rts_ptr Pointer to a variable to store the result. Must not be NULL.
952  *
953  * @return SP_OK upon success, a negative error code otherwise.
954  *
955  * @since 0.1.0
956  */
957 enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
958
959 /**
960  * Set the RTS pin behaviour in a port configuration.
961  *
962  * @param[in] config Pointer to a configuration structure. Must not be NULL.
963  * @param[in] rts RTS pin mode, or -1 to retain the current setting.
964  *
965  * @return SP_OK upon success, a negative error code otherwise.
966  *
967  * @since 0.1.0
968  */
969 enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
970
971 /**
972  * Set the CTS pin behaviour for the specified serial port.
973  *
974  * @param[in] port Pointer to a port structure. Must not be NULL.
975  * @param[in] cts CTS pin mode.
976  *
977  * @return SP_OK upon success, a negative error code otherwise.
978  *
979  * @since 0.1.0
980  */
981 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
982
983 /**
984  * Get the CTS pin behaviour from a port configuration.
985  *
986  * The user should allocate a variable of type enum sp_cts and
987  * pass a pointer to this to receive the result.
988  *
989  * @param[in] config Pointer to a configuration structure. Must not be NULL.
990  * @param[out] cts_ptr Pointer to a variable to store the result. Must not be NULL.
991  *
992  * @return SP_OK upon success, a negative error code otherwise.
993  *
994  * @since 0.1.0
995  */
996 enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
997
998 /**
999  * Set the CTS pin behaviour in a port configuration.
1000  *
1001  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1002  * @param[in] cts CTS pin mode, or -1 to retain the current setting.
1003  *
1004  * @return SP_OK upon success, a negative error code otherwise.
1005  *
1006  * @since 0.1.0
1007  */
1008 enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
1009
1010 /**
1011  * Set the DTR pin behaviour for the specified serial port.
1012  *
1013  * @param[in] port Pointer to a port structure. Must not be NULL.
1014  * @param[in] dtr DTR pin mode.
1015  *
1016  * @return SP_OK upon success, a negative error code otherwise.
1017  *
1018  * @since 0.1.0
1019  */
1020 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
1021
1022 /**
1023  * Get the DTR pin behaviour from a port configuration.
1024  *
1025  * The user should allocate a variable of type enum sp_dtr and
1026  * pass a pointer to this to receive the result.
1027  *
1028  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1029  * @param[out] dtr_ptr Pointer to a variable to store the result. Must not be NULL.
1030  *
1031  * @return SP_OK upon success, a negative error code otherwise.
1032  *
1033  * @since 0.1.0
1034  */
1035 enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
1036
1037 /**
1038  * Set the DTR pin behaviour in a port configuration.
1039  *
1040  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1041  * @param[in] dtr DTR pin mode, or -1 to retain the current setting.
1042  *
1043  * @return SP_OK upon success, a negative error code otherwise.
1044  *
1045  * @since 0.1.0
1046  */
1047 enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
1048
1049 /**
1050  * Set the DSR pin behaviour for the specified serial port.
1051  *
1052  * @param[in] port Pointer to a port structure. Must not be NULL.
1053  * @param[in] dsr DSR pin mode.
1054  *
1055  * @return SP_OK upon success, a negative error code otherwise.
1056  *
1057  * @since 0.1.0
1058  */
1059 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
1060
1061 /**
1062  * Get the DSR pin behaviour from a port configuration.
1063  *
1064  * The user should allocate a variable of type enum sp_dsr and
1065  * pass a pointer to this to receive the result.
1066  *
1067  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1068  * @param[out] dsr_ptr Pointer to a variable to store the result. Must not be NULL.
1069  *
1070  * @return SP_OK upon success, a negative error code otherwise.
1071  *
1072  * @since 0.1.0
1073  */
1074 enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
1075
1076 /**
1077  * Set the DSR pin behaviour in a port configuration.
1078  *
1079  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1080  * @param[in] dsr DSR pin mode, or -1 to retain the current setting.
1081  *
1082  * @return SP_OK upon success, a negative error code otherwise.
1083  *
1084  * @since 0.1.0
1085  */
1086 enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
1087
1088 /**
1089  * Set the XON/XOFF configuration for the specified serial port.
1090  *
1091  * @param[in] port Pointer to a port structure. Must not be NULL.
1092  * @param[in] xon_xoff XON/XOFF mode.
1093  *
1094  * @return SP_OK upon success, a negative error code otherwise.
1095  *
1096  * @since 0.1.0
1097  */
1098 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
1099
1100 /**
1101  * Get the XON/XOFF configuration from a port configuration.
1102  *
1103  * The user should allocate a variable of type enum sp_xonxoff and
1104  * pass a pointer to this to receive the result.
1105  *
1106  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1107  * @param[out] xon_xoff_ptr Pointer to a variable to store the result. Must not be NULL.
1108  *
1109  * @return SP_OK upon success, a negative error code otherwise.
1110  *
1111  * @since 0.1.0
1112  */
1113 enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
1114
1115 /**
1116  * Set the XON/XOFF configuration in a port configuration.
1117  *
1118  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1119  * @param[in] xon_xoff XON/XOFF mode, or -1 to retain the current setting.
1120  *
1121  * @return SP_OK upon success, a negative error code otherwise.
1122  *
1123  * @since 0.1.0
1124  */
1125 enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
1126
1127 /**
1128  * Set the flow control type in a port configuration.
1129  *
1130  * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
1131  * XON/XOFF settings as necessary for the specified flow control
1132  * type. For more fine-grained control of these settings, use their
1133  * individual configuration functions.
1134  *
1135  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1136  * @param[in] flowcontrol Flow control setting to use.
1137  *
1138  * @return SP_OK upon success, a negative error code otherwise.
1139  *
1140  * @since 0.1.0
1141  */
1142 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
1143
1144 /**
1145  * Set the flow control type for the specified serial port.
1146  *
1147  * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
1148  * XON/XOFF settings as necessary for the specified flow control
1149  * type. For more fine-grained control of these settings, use their
1150  * individual configuration functions.
1151  *
1152  * @param[in] port Pointer to a port structure. Must not be NULL.
1153  * @param[in] flowcontrol Flow control setting to use.
1154  *
1155  * @return SP_OK upon success, a negative error code otherwise.
1156  *
1157  * @since 0.1.0
1158  */
1159 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
1160
1161 /**
1162  * @}
1163  *
1164  * @defgroup Data Data handling
1165  *
1166  * Reading, writing, and flushing data.
1167  *
1168  * @{
1169  */
1170
1171 /**
1172  * Read bytes from the specified serial port, blocking until complete.
1173  *
1174  * @warning If your program runs on Unix, defines its own signal handlers, and
1175  *          needs to abort blocking reads when these are called, then you
1176  *          should not use this function. It repeats system calls that return
1177  *          with EINTR. To be able to abort a read from a signal handler, you
1178  *          should implement your own blocking read using sp_nonblocking_read()
1179  *          together with a blocking method that makes sense for your program.
1180  *          E.g. you can obtain the file descriptor for an open port using
1181  *          sp_get_port_handle() and use this to call select() or pselect(),
1182  *          with appropriate arrangements to return if a signal is received.
1183  *
1184  * @param[in] port Pointer to a port structure. Must not be NULL.
1185  * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1186  * @param[in] count Requested number of bytes to read.
1187  * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1188  *
1189  * @return The number of bytes read on success, or a negative error code. If
1190  *         the number of bytes returned is less than that requested, the
1191  *         timeout was reached before the requested number of bytes was
1192  *         available. If timeout is zero, the function will always return
1193  *         either the requested number of bytes or a negative error code.
1194  *
1195  * @since 0.1.0
1196  */
1197 enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
1198
1199 /**
1200  * Read bytes from the specified serial port, returning as soon as any data is
1201  * available.
1202  *
1203  * @warning If your program runs on Unix, defines its own signal handlers, and
1204  *          needs to abort blocking reads when these are called, then you
1205  *          should not use this function. It repeats system calls that return
1206  *          with EINTR. To be able to abort a read from a signal handler, you
1207  *          should implement your own blocking read using sp_nonblocking_read()
1208  *          together with a blocking method that makes sense for your program.
1209  *          E.g. you can obtain the file descriptor for an open port using
1210  *          sp_get_port_handle() and use this to call select() or pselect(),
1211  *          with appropriate arrangements to return if a signal is received.
1212  *
1213  * @param[in] port Pointer to a port structure. Must not be NULL.
1214  * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1215  * @param[in] count Maximum number of bytes to read. Must not be zero.
1216  * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1217  *
1218  * @return The number of bytes read on success, or a negative error code. If
1219  *         the result is zero, the timeout was reached before any bytes were
1220  *         available. If timeout_ms is zero, the function will always return
1221  *         either at least one byte, or a negative error code.
1222  *
1223  * @since 0.1.1
1224  */
1225 enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
1226
1227 /**
1228  * Read bytes from the specified serial port, without blocking.
1229  *
1230  * @param[in] port Pointer to a port structure. Must not be NULL.
1231  * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1232  * @param[in] count Maximum number of bytes to read.
1233  *
1234  * @return The number of bytes read on success, or a negative error code. The
1235  *         number of bytes returned may be any number from zero to the maximum
1236  *         that was requested.
1237  *
1238  * @since 0.1.0
1239  */
1240 enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
1241
1242 /**
1243  * Write bytes to the specified serial port, blocking until complete.
1244  *
1245  * Note that this function only ensures that the accepted bytes have been
1246  * written to the OS; they may be held in driver or hardware buffers and not
1247  * yet physically transmitted. To check whether all written bytes have actually
1248  * been transmitted, use the sp_output_waiting() function. To wait until all
1249  * written bytes have actually been transmitted, use the sp_drain() function.
1250  *
1251  * @warning If your program runs on Unix, defines its own signal handlers, and
1252  *          needs to abort blocking writes when these are called, then you
1253  *          should not use this function. It repeats system calls that return
1254  *          with EINTR. To be able to abort a write from a signal handler, you
1255  *          should implement your own blocking write using sp_nonblocking_write()
1256  *          together with a blocking method that makes sense for your program.
1257  *          E.g. you can obtain the file descriptor for an open port using
1258  *          sp_get_port_handle() and use this to call select() or pselect(),
1259  *          with appropriate arrangements to return if a signal is received.
1260  *
1261  * @param[in] port Pointer to a port structure. Must not be NULL.
1262  * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1263  * @param[in] count Requested number of bytes to write.
1264  * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1265  *
1266  * @return The number of bytes written on success, or a negative error code.
1267  *         If the number of bytes returned is less than that requested, the
1268  *         timeout was reached before the requested number of bytes was
1269  *         written. If timeout is zero, the function will always return
1270  *         either the requested number of bytes or a negative error code. In
1271  *         the event of an error there is no way to determine how many bytes
1272  *         were sent before the error occurred.
1273  *
1274  * @since 0.1.0
1275  */
1276 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout_ms);
1277
1278 /**
1279  * Write bytes to the specified serial port, without blocking.
1280  *
1281  * Note that this function only ensures that the accepted bytes have been
1282  * written to the OS; they may be held in driver or hardware buffers and not
1283  * yet physically transmitted. To check whether all written bytes have actually
1284  * been transmitted, use the sp_output_waiting() function. To wait until all
1285  * written bytes have actually been transmitted, use the sp_drain() function.
1286  *
1287  * @param[in] port Pointer to a port structure. Must not be NULL.
1288  * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1289  * @param[in] count Maximum number of bytes to write.
1290  *
1291  * @return The number of bytes written on success, or a negative error code.
1292  *         The number of bytes returned may be any number from zero to the
1293  *         maximum that was requested.
1294  *
1295  * @since 0.1.0
1296  */
1297 enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
1298
1299 /**
1300  * Gets the number of bytes waiting in the input buffer.
1301  *
1302  * @param[in] port Pointer to a port structure. Must not be NULL.
1303  *
1304  * @return Number of bytes waiting on success, a negative error code otherwise.
1305  *
1306  * @since 0.1.0
1307  */
1308 enum sp_return sp_input_waiting(struct sp_port *port);
1309
1310 /**
1311  * Gets the number of bytes waiting in the output buffer.
1312  *
1313  * @param[in] port Pointer to a port structure. Must not be NULL.
1314  *
1315  * @return Number of bytes waiting on success, a negative error code otherwise.
1316  *
1317  * @since 0.1.0
1318  */
1319 enum sp_return sp_output_waiting(struct sp_port *port);
1320
1321 /**
1322  * Flush serial port buffers. Data in the selected buffer(s) is discarded.
1323  *
1324  * @param[in] port Pointer to a port structure. Must not be NULL.
1325  * @param[in] buffers Which buffer(s) to flush.
1326  *
1327  * @return SP_OK upon success, a negative error code otherwise.
1328  *
1329  * @since 0.1.0
1330  */
1331 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
1332
1333 /**
1334  * Wait for buffered data to be transmitted.
1335  *
1336  * @warning If your program runs on Unix, defines its own signal handlers, and
1337  *          needs to abort draining the output buffer when when these are
1338  *          called, then you should not use this function. It repeats system
1339  *          calls that return with EINTR. To be able to abort a drain from a
1340  *          signal handler, you would need to implement your own blocking
1341  *          drain by polling the result of sp_output_waiting().
1342  *
1343  * @param[in] port Pointer to a port structure. Must not be NULL.
1344  *
1345  * @return SP_OK upon success, a negative error code otherwise.
1346  *
1347  * @since 0.1.0
1348  */
1349 enum sp_return sp_drain(struct sp_port *port);
1350
1351 /**
1352  * @}
1353  *
1354  * @defgroup Waiting Waiting
1355  *
1356  * Waiting for events and timeout handling.
1357  *
1358  * @{
1359  */
1360
1361 /**
1362  * Allocate storage for a set of events.
1363  *
1364  * The user should allocate a variable of type struct sp_event_set *,
1365  * then pass a pointer to this variable to receive the result.
1366  *
1367  * The result should be freed after use by calling sp_free_event_set().
1368  *
1369  * @param[out] result_ptr If any error is returned, the variable pointed to by
1370  *                        result_ptr will be set to NULL. Otherwise, it will
1371  *                        be set to point to the event set. Must not be NULL.
1372  *
1373  * @return SP_OK upon success, a negative error code otherwise.
1374  *
1375  * @since 0.1.0
1376  */
1377 enum sp_return sp_new_event_set(struct sp_event_set **result_ptr);
1378
1379 /**
1380  * Add events to a struct sp_event_set for a given port.
1381  *
1382  * The port must first be opened by calling sp_open() using the same port
1383  * structure.
1384  *
1385  * After the port is closed or the port structure freed, the results may
1386  * no longer be valid.
1387  *
1388  * @param[in,out] event_set Event set to update. Must not be NULL.
1389  * @param[in] port Pointer to a port structure. Must not be NULL.
1390  * @param[in] mask Bitmask of events to be waited for.
1391  *
1392  * @return SP_OK upon success, a negative error code otherwise.
1393  *
1394  * @since 0.1.0
1395  */
1396 enum sp_return sp_add_port_events(struct sp_event_set *event_set,
1397         const struct sp_port *port, enum sp_event mask);
1398
1399 /**
1400  * Wait for any of a set of events to occur.
1401  *
1402  * @param[in] event_set Event set to wait on. Must not be NULL.
1403  * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1404  *
1405  * @return SP_OK upon success, a negative error code otherwise.
1406  *
1407  * @since 0.1.0
1408  */
1409 enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout_ms);
1410
1411 /**
1412  * Free a structure allocated by sp_new_event_set().
1413  *
1414  * @param[in] event_set Event set to free. Must not be NULL.
1415  *
1416  * @since 0.1.0
1417  */
1418 void sp_free_event_set(struct sp_event_set *event_set);
1419
1420 /**
1421  * @}
1422  *
1423  * @defgroup Signals Signals
1424  *
1425  * Port signalling operations.
1426  *
1427  * @{
1428  */
1429
1430 /**
1431  * Gets the status of the control signals for the specified port.
1432  *
1433  * The user should allocate a variable of type "enum sp_signal" and pass a
1434  * pointer to this variable to receive the result. The result is a bitmask
1435  * in which individual signals can be checked by bitwise OR with values of
1436  * the sp_signal enum.
1437  *
1438  * @param[in] port Pointer to a port structure. Must not be NULL.
1439  * @param[out] signal_mask Pointer to a variable to receive the result.
1440  *                         Must not be NULL.
1441  *
1442  * @return SP_OK upon success, a negative error code otherwise.
1443  *
1444  * @since 0.1.0
1445  */
1446 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signal_mask);
1447
1448 /**
1449  * Put the port transmit line into the break state.
1450  *
1451  * @param[in] port Pointer to a port structure. Must not be NULL.
1452  *
1453  * @return SP_OK upon success, a negative error code otherwise.
1454  *
1455  * @since 0.1.0
1456  */
1457 enum sp_return sp_start_break(struct sp_port *port);
1458
1459 /**
1460  * Take the port transmit line out of the break state.
1461  *
1462  * @param[in] port Pointer to a port structure. Must not be NULL.
1463  *
1464  * @return SP_OK upon success, a negative error code otherwise.
1465  *
1466  * @since 0.1.0
1467  */
1468 enum sp_return sp_end_break(struct sp_port *port);
1469
1470 /**
1471  * @}
1472  *
1473  * @defgroup Errors Errors
1474  *
1475  * Obtaining error information.
1476  *
1477  * @{
1478  */
1479
1480 /**
1481  * Get the error code for a failed operation.
1482  *
1483  * In order to obtain the correct result, this function should be called
1484  * straight after the failure, before executing any other system operations.
1485  * The result is thread-specific, and only valid when called immediately
1486  * after a previous call returning SP_ERR_FAIL.
1487  *
1488  * @return The system's numeric code for the error that caused the last
1489  *         operation to fail.
1490  *
1491  * @since 0.1.0
1492  */
1493 int sp_last_error_code(void);
1494
1495 /**
1496  * Get the error message for a failed operation.
1497  *
1498  * In order to obtain the correct result, this function should be called
1499  * straight after the failure, before executing other system operations.
1500  * The result is thread-specific, and only valid when called immediately
1501  * after a previous call returning SP_ERR_FAIL.
1502  *
1503  * @return The system's message for the error that caused the last
1504  *         operation to fail. This string may be allocated by the function,
1505  *         and should be freed after use by calling sp_free_error_message().
1506  *
1507  * @since 0.1.0
1508  */
1509 char *sp_last_error_message(void);
1510
1511 /**
1512  * Free an error message returned by sp_last_error_message().
1513  *
1514  * @param[in] message The error message string to free. Must not be NULL.
1515  *
1516  * @since 0.1.0
1517  */
1518 void sp_free_error_message(char *message);
1519
1520 /**
1521  * Set the handler function for library debugging messages.
1522  *
1523  * Debugging messages are generated by the library during each operation,
1524  * to help in diagnosing problems. The handler will be called for each
1525  * message. The handler can be set to NULL to ignore all debug messages.
1526  *
1527  * The handler function should accept a format string and variable length
1528  * argument list, in the same manner as e.g. printf().
1529  *
1530  * The default handler is sp_default_debug_handler().
1531  *
1532  * @param[in] handler The handler function to use. Can be NULL (in that case
1533  *                    all debug messages will be ignored).
1534  *
1535  * @since 0.1.0
1536  */
1537 void sp_set_debug_handler(void (*handler)(const char *format, ...));
1538
1539 /**
1540  * Default handler function for library debugging messages.
1541  *
1542  * This function prints debug messages to the standard error stream if the
1543  * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
1544  * ignored.
1545  *
1546  * @param[in] format The format string to use. Must not be NULL.
1547  * @param[in] ... The variable length argument list to use.
1548  *
1549  * @since 0.1.0
1550  */
1551 void sp_default_debug_handler(const char *format, ...);
1552
1553 /** @} */
1554
1555 /**
1556  * @defgroup Versions Versions
1557  *
1558  * Version number querying functions, definitions, and macros.
1559  *
1560  * This set of API calls returns two different version numbers related
1561  * to libserialport. The "package version" is the release version number of the
1562  * libserialport tarball in the usual "major.minor.micro" format, e.g. "0.1.0".
1563  *
1564  * The "library version" is independent of that; it is the libtool version
1565  * number in the "current:revision:age" format, e.g. "2:0:0".
1566  * See http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning for details.
1567  *
1568  * Both version numbers (and/or individual components of them) can be
1569  * retrieved via the API calls at runtime, and/or they can be checked at
1570  * compile/preprocessor time using the respective macros.
1571  *
1572  * @{
1573  */
1574
1575 /*
1576  * Package version macros (can be used for conditional compilation).
1577  */
1578
1579 /** The libserialport package 'major' version number. */
1580 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
1581
1582 /** The libserialport package 'minor' version number. */
1583 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
1584
1585 /** The libserialport package 'micro' version number. */
1586 #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
1587
1588 /** The libserialport package version ("major.minor.micro") as string. */
1589 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
1590
1591 /*
1592  * Library/libtool version macros (can be used for conditional compilation).
1593  */
1594
1595 /** The libserialport libtool 'current' version number. */
1596 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
1597
1598 /** The libserialport libtool 'revision' version number. */
1599 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
1600
1601 /** The libserialport libtool 'age' version number. */
1602 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
1603
1604 /** The libserialport libtool version ("current:revision:age") as string. */
1605 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
1606
1607 /**
1608  * Get the major libserialport package version number.
1609  *
1610  * @return The major package version number.
1611  *
1612  * @since 0.1.0
1613  */
1614 int sp_get_major_package_version(void);
1615
1616 /**
1617  * Get the minor libserialport package version number.
1618  *
1619  * @return The minor package version number.
1620  *
1621  * @since 0.1.0
1622  */
1623 int sp_get_minor_package_version(void);
1624
1625 /**
1626  * Get the micro libserialport package version number.
1627  *
1628  * @return The micro package version number.
1629  *
1630  * @since 0.1.0
1631  */
1632 int sp_get_micro_package_version(void);
1633
1634 /**
1635  * Get the libserialport package version number as a string.
1636  *
1637  * @return The package version number string. The returned string is
1638  *         static and thus should NOT be free'd by the caller.
1639  *
1640  * @since 0.1.0
1641  */
1642 const char *sp_get_package_version_string(void);
1643
1644 /**
1645  * Get the "current" part of the libserialport library version number.
1646  *
1647  * @return The "current" library version number.
1648  *
1649  * @since 0.1.0
1650  */
1651 int sp_get_current_lib_version(void);
1652
1653 /**
1654  * Get the "revision" part of the libserialport library version number.
1655  *
1656  * @return The "revision" library version number.
1657  *
1658  * @since 0.1.0
1659  */
1660 int sp_get_revision_lib_version(void);
1661
1662 /**
1663  * Get the "age" part of the libserialport library version number.
1664  *
1665  * @return The "age" library version number.
1666  *
1667  * @since 0.1.0
1668  */
1669 int sp_get_age_lib_version(void);
1670
1671 /**
1672  * Get the libserialport library version number as a string.
1673  *
1674  * @return The library version number string. The returned string is
1675  *         static and thus should NOT be free'd by the caller.
1676  *
1677  * @since 0.1.0
1678  */
1679 const char *sp_get_lib_version_string(void);
1680
1681 /** @} */
1682
1683 #ifdef __cplusplus
1684 }
1685 #endif
1686
1687 #endif