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