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