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