]> sigrok.org Git - libserialport.git/blob - libserialport.h
Add some more narrative docs on the configuration API.
[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  * See @ref port_config.c for a working example of port configuration.
766  *
767  * You should always configure all settings before using a port.
768  * There are no default settings applied by libserialport.
769  * When you open a port it may have default settings from the OS or
770  * driver, or the settings left over by the last program to use it.
771  *
772  * You should always set baud rate, data bits, parity and stop bits.
773  *
774  * You should normally also set one of the preset @ref sp_flowcontrol
775  * flow control modes, which will set up the RTS, CTS, DTR and DSR pin
776  * behaviours and enable or disable XON/XOFF. If you need an unusual
777  * configuration not covered by the preset flow control modes, you
778  * will need to configure these settings individually, and avoid
779  * calling sp_set_flowcontrol() or sp_set_config_flowcontrol() which
780  * will overwrite these settings.
781  *
782  * A port must be opened before you can change its settings.
783  *
784  * There are two ways of accessing port settings:
785  *
786  * Configuration structures
787  * ------------------------
788  *
789  * You can read and write a whole configuration (all settings at once)
790  * using sp_get_config() and sp_set_config(). This is handy if you want
791  * to change between some preset combinations, or save and restore an
792  * existing configuration. It also ensures the changes are made
793  * together, via an efficient set of calls into the OS - in some cases
794  * a single system call can be used.
795  *
796  * Use accessor functions like sp_get_config_baudrate() and
797  * sp_set_config_baudrate() to get and set individual settings
798  * from a configuration.
799  *
800  * For each setting in a port configuration, a special value of -1 can
801  * be used, which will cause that setting to be left alone when the
802  * configuration is applied by sp_set_config().
803  *
804  * This value is also be used by sp_get_config() for any settings
805  * which are unconfigured at the OS level, or in a state that is
806  * not representable within the libserialport API.
807  *
808  * Configurations are allocated using sp_new_config() and freed
809  * with sp_free_config(). You need to manage them yourself. When
810  * a new configuration is allocated by sp_new_config(), all of
811  * its settings are initially set to the special -1 value.
812  *
813  * Direct functions for changing port settings
814  * -------------------------------------------
815  *
816  * As a shortcut, you can set individual settings on a port directly
817  * by calling functions like sp_set_baudrate() and sp_set_parity().
818  * This saves you the work of allocating a temporary config, setting it
819  * up, applying it to a port and then freeing it.
820  *
821  * @{
822  */
823
824 /**
825  * Allocate a port configuration structure.
826  *
827  * The user should allocate a variable of type "struct sp_port_config *" and
828  * pass a pointer to this to receive the result. The variable will be updated
829  * to point to the new configuration structure. The structure is opaque and
830  * must be accessed via the functions provided.
831  *
832  * All parameters in the structure will be initialised to special values which
833  * are ignored by sp_set_config().
834  *
835  * The structure should be freed after use by calling sp_free_config().
836  *
837  * @param[out] config_ptr If any error is returned, the variable pointed to by
838  *                        config_ptr will be set to NULL. Otherwise, it will
839  *                        be set to point to the allocated config structure.
840  *                        Must not be NULL.
841  *
842  * @return SP_OK upon success, a negative error code otherwise.
843  *
844  * @since 0.1.0
845  */
846 SP_API enum sp_return sp_new_config(struct sp_port_config **config_ptr);
847
848 /**
849  * Free a port configuration structure.
850  *
851  * @param[in] config Pointer to a configuration structure. Must not be NULL.
852  *
853  * @since 0.1.0
854  */
855 SP_API void sp_free_config(struct sp_port_config *config);
856
857 /**
858  * Get the current configuration of the specified serial port.
859  *
860  * The user should allocate a configuration structure using sp_new_config()
861  * and pass this as the config parameter. The configuration structure will
862  * be updated with the port configuration.
863  *
864  * Any parameters that are configured with settings not recognised or
865  * supported by libserialport, will be set to special values that are
866  * ignored by sp_set_config().
867  *
868  * @param[in] port Pointer to a port structure. Must not be NULL.
869  * @param[out] config Pointer to a configuration structure that will hold
870  *                    the result. Upon errors the contents of the config
871  *                    struct will not be changed. Must not be NULL.
872  *
873  * @return SP_OK upon success, a negative error code otherwise.
874  *
875  * @since 0.1.0
876  */
877 SP_API enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
878
879 /**
880  * Set the configuration for the specified serial port.
881  *
882  * For each parameter in the configuration, there is a special value (usually
883  * -1, but see the documentation for each field). These values will be ignored
884  * and the corresponding setting left unchanged on the port.
885  *
886  * Upon errors, the configuration of the serial port is unknown since
887  * partial/incomplete config updates may have happened.
888  *
889  * @param[in] port Pointer to a port structure. Must not be NULL.
890  * @param[in] config Pointer to a configuration structure. Must not be NULL.
891  *
892  * @return SP_OK upon success, a negative error code otherwise.
893  *
894  * @since 0.1.0
895  */
896 SP_API enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
897
898 /**
899  * Set the baud rate for the specified serial port.
900  *
901  * @param[in] port Pointer to a port structure. Must not be NULL.
902  * @param[in] baudrate Baud rate in bits per second.
903  *
904  * @return SP_OK upon success, a negative error code otherwise.
905  *
906  * @since 0.1.0
907  */
908 SP_API enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
909
910 /**
911  * Get the baud rate from a port configuration.
912  *
913  * The user should allocate a variable of type int and
914  * pass a pointer to this to receive the result.
915  *
916  * @param[in] config Pointer to a configuration structure. Must not be NULL.
917  * @param[out] baudrate_ptr Pointer to a variable to store the result. Must not be NULL.
918  *
919  * @return SP_OK upon success, a negative error code otherwise.
920  *
921  * @since 0.1.0
922  */
923 SP_API enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
924
925 /**
926  * Set the baud rate in a port configuration.
927  *
928  * @param[in] config Pointer to a configuration structure. Must not be NULL.
929  * @param[in] baudrate Baud rate in bits per second, or -1 to retain the current setting.
930  *
931  * @return SP_OK upon success, a negative error code otherwise.
932  *
933  * @since 0.1.0
934  */
935 SP_API enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
936
937 /**
938  * Set the data bits for the specified serial port.
939  *
940  * @param[in] port Pointer to a port structure. Must not be NULL.
941  * @param[in] bits Number of data bits.
942  *
943  * @return SP_OK upon success, a negative error code otherwise.
944  *
945  * @since 0.1.0
946  */
947 SP_API enum sp_return sp_set_bits(struct sp_port *port, int bits);
948
949 /**
950  * Get the data bits from a port configuration.
951  *
952  * The user should allocate a variable of type int and
953  * pass a pointer to this to receive the result.
954  *
955  * @param[in] config Pointer to a configuration structure. Must not be NULL.
956  * @param[out] bits_ptr Pointer to a variable to store the result. Must not be NULL.
957  *
958  * @return SP_OK upon success, a negative error code otherwise.
959  *
960  * @since 0.1.0
961  */
962 SP_API enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
963
964 /**
965  * Set the data bits in a port configuration.
966  *
967  * @param[in] config Pointer to a configuration structure. Must not be NULL.
968  * @param[in] bits Number of data bits, or -1 to retain the current setting.
969  *
970  * @return SP_OK upon success, a negative error code otherwise.
971  *
972  * @since 0.1.0
973  */
974 SP_API enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
975
976 /**
977  * Set the parity setting for the specified serial port.
978  *
979  * @param[in] port Pointer to a port structure. Must not be NULL.
980  * @param[in] parity Parity setting.
981  *
982  * @return SP_OK upon success, a negative error code otherwise.
983  *
984  * @since 0.1.0
985  */
986 SP_API enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
987
988 /**
989  * Get the parity setting from a port configuration.
990  *
991  * The user should allocate a variable of type enum sp_parity and
992  * pass a pointer to this to receive the result.
993  *
994  * @param[in] config Pointer to a configuration structure. Must not be NULL.
995  * @param[out] parity_ptr Pointer to a variable to store the result. Must not be NULL.
996  *
997  * @return SP_OK upon success, a negative error code otherwise.
998  *
999  * @since 0.1.0
1000  */
1001 SP_API enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
1002
1003 /**
1004  * Set the parity setting in a port configuration.
1005  *
1006  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1007  * @param[in] parity Parity setting, or -1 to retain the current setting.
1008  *
1009  * @return SP_OK upon success, a negative error code otherwise.
1010  *
1011  * @since 0.1.0
1012  */
1013 SP_API enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
1014
1015 /**
1016  * Set the stop bits for the specified serial port.
1017  *
1018  * @param[in] port Pointer to a port structure. Must not be NULL.
1019  * @param[in] stopbits Number of stop bits.
1020  *
1021  * @return SP_OK upon success, a negative error code otherwise.
1022  *
1023  * @since 0.1.0
1024  */
1025 SP_API enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
1026
1027 /**
1028  * Get the stop bits from a port configuration.
1029  *
1030  * The user should allocate a variable of type int and
1031  * pass a pointer to this to receive the result.
1032  *
1033  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1034  * @param[out] stopbits_ptr Pointer to a variable to store the result. Must not be NULL.
1035  *
1036  * @return SP_OK upon success, a negative error code otherwise.
1037  *
1038  * @since 0.1.0
1039  */
1040 SP_API enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
1041
1042 /**
1043  * Set the stop bits in a port configuration.
1044  *
1045  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1046  * @param[in] stopbits Number of stop bits, or -1 to retain the current setting.
1047  *
1048  * @return SP_OK upon success, a negative error code otherwise.
1049  *
1050  * @since 0.1.0
1051  */
1052 SP_API enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
1053
1054 /**
1055  * Set the RTS pin behaviour for the specified serial port.
1056  *
1057  * @param[in] port Pointer to a port structure. Must not be NULL.
1058  * @param[in] rts RTS pin mode.
1059  *
1060  * @return SP_OK upon success, a negative error code otherwise.
1061  *
1062  * @since 0.1.0
1063  */
1064 SP_API enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
1065
1066 /**
1067  * Get the RTS pin behaviour from a port configuration.
1068  *
1069  * The user should allocate a variable of type enum sp_rts and
1070  * pass a pointer to this to receive the result.
1071  *
1072  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1073  * @param[out] rts_ptr Pointer to a variable to store the result. Must not be NULL.
1074  *
1075  * @return SP_OK upon success, a negative error code otherwise.
1076  *
1077  * @since 0.1.0
1078  */
1079 SP_API enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
1080
1081 /**
1082  * Set the RTS pin behaviour in a port configuration.
1083  *
1084  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1085  * @param[in] rts RTS pin mode, or -1 to retain the current setting.
1086  *
1087  * @return SP_OK upon success, a negative error code otherwise.
1088  *
1089  * @since 0.1.0
1090  */
1091 SP_API enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
1092
1093 /**
1094  * Set the CTS pin behaviour for the specified serial port.
1095  *
1096  * @param[in] port Pointer to a port structure. Must not be NULL.
1097  * @param[in] cts CTS pin mode.
1098  *
1099  * @return SP_OK upon success, a negative error code otherwise.
1100  *
1101  * @since 0.1.0
1102  */
1103 SP_API enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
1104
1105 /**
1106  * Get the CTS pin behaviour from a port configuration.
1107  *
1108  * The user should allocate a variable of type enum sp_cts and
1109  * pass a pointer to this to receive the result.
1110  *
1111  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1112  * @param[out] cts_ptr Pointer to a variable to store the result. Must not be NULL.
1113  *
1114  * @return SP_OK upon success, a negative error code otherwise.
1115  *
1116  * @since 0.1.0
1117  */
1118 SP_API enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
1119
1120 /**
1121  * Set the CTS pin behaviour in a port configuration.
1122  *
1123  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1124  * @param[in] cts CTS pin mode, or -1 to retain the current setting.
1125  *
1126  * @return SP_OK upon success, a negative error code otherwise.
1127  *
1128  * @since 0.1.0
1129  */
1130 SP_API enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
1131
1132 /**
1133  * Set the DTR pin behaviour for the specified serial port.
1134  *
1135  * @param[in] port Pointer to a port structure. Must not be NULL.
1136  * @param[in] dtr DTR pin mode.
1137  *
1138  * @return SP_OK upon success, a negative error code otherwise.
1139  *
1140  * @since 0.1.0
1141  */
1142 SP_API enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
1143
1144 /**
1145  * Get the DTR pin behaviour from a port configuration.
1146  *
1147  * The user should allocate a variable of type enum sp_dtr and
1148  * pass a pointer to this to receive the result.
1149  *
1150  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1151  * @param[out] dtr_ptr Pointer to a variable to store the result. Must not be NULL.
1152  *
1153  * @return SP_OK upon success, a negative error code otherwise.
1154  *
1155  * @since 0.1.0
1156  */
1157 SP_API enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
1158
1159 /**
1160  * Set the DTR pin behaviour in a port configuration.
1161  *
1162  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1163  * @param[in] dtr DTR pin mode, or -1 to retain the current setting.
1164  *
1165  * @return SP_OK upon success, a negative error code otherwise.
1166  *
1167  * @since 0.1.0
1168  */
1169 SP_API enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
1170
1171 /**
1172  * Set the DSR pin behaviour for the specified serial port.
1173  *
1174  * @param[in] port Pointer to a port structure. Must not be NULL.
1175  * @param[in] dsr DSR pin mode.
1176  *
1177  * @return SP_OK upon success, a negative error code otherwise.
1178  *
1179  * @since 0.1.0
1180  */
1181 SP_API enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
1182
1183 /**
1184  * Get the DSR pin behaviour from a port configuration.
1185  *
1186  * The user should allocate a variable of type enum sp_dsr and
1187  * pass a pointer to this to receive the result.
1188  *
1189  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1190  * @param[out] dsr_ptr Pointer to a variable to store the result. Must not be NULL.
1191  *
1192  * @return SP_OK upon success, a negative error code otherwise.
1193  *
1194  * @since 0.1.0
1195  */
1196 SP_API enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
1197
1198 /**
1199  * Set the DSR pin behaviour in a port configuration.
1200  *
1201  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1202  * @param[in] dsr DSR pin mode, or -1 to retain the current setting.
1203  *
1204  * @return SP_OK upon success, a negative error code otherwise.
1205  *
1206  * @since 0.1.0
1207  */
1208 SP_API enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
1209
1210 /**
1211  * Set the XON/XOFF configuration for the specified serial port.
1212  *
1213  * @param[in] port Pointer to a port structure. Must not be NULL.
1214  * @param[in] xon_xoff XON/XOFF mode.
1215  *
1216  * @return SP_OK upon success, a negative error code otherwise.
1217  *
1218  * @since 0.1.0
1219  */
1220 SP_API enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
1221
1222 /**
1223  * Get the XON/XOFF configuration from a port configuration.
1224  *
1225  * The user should allocate a variable of type enum sp_xonxoff and
1226  * pass a pointer to this to receive the result.
1227  *
1228  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1229  * @param[out] xon_xoff_ptr Pointer to a variable to store the result. Must not be NULL.
1230  *
1231  * @return SP_OK upon success, a negative error code otherwise.
1232  *
1233  * @since 0.1.0
1234  */
1235 SP_API enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
1236
1237 /**
1238  * Set the XON/XOFF configuration in a port configuration.
1239  *
1240  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1241  * @param[in] xon_xoff XON/XOFF mode, or -1 to retain the current setting.
1242  *
1243  * @return SP_OK upon success, a negative error code otherwise.
1244  *
1245  * @since 0.1.0
1246  */
1247 SP_API enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
1248
1249 /**
1250  * Set the flow control type in a port configuration.
1251  *
1252  * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
1253  * XON/XOFF settings as necessary for the specified flow control
1254  * type. For more fine-grained control of these settings, use their
1255  * individual configuration functions.
1256  *
1257  * @param[in] config Pointer to a configuration structure. Must not be NULL.
1258  * @param[in] flowcontrol Flow control setting to use.
1259  *
1260  * @return SP_OK upon success, a negative error code otherwise.
1261  *
1262  * @since 0.1.0
1263  */
1264 SP_API enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
1265
1266 /**
1267  * Set the flow control type for the specified serial port.
1268  *
1269  * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
1270  * XON/XOFF settings as necessary for the specified flow control
1271  * type. For more fine-grained control of these settings, use their
1272  * individual configuration functions.
1273  *
1274  * @param[in] port Pointer to a port structure. Must not be NULL.
1275  * @param[in] flowcontrol Flow control setting to use.
1276  *
1277  * @return SP_OK upon success, a negative error code otherwise.
1278  *
1279  * @since 0.1.0
1280  */
1281 SP_API enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
1282
1283 /**
1284  * @}
1285  *
1286  * @defgroup Data Data handling
1287  *
1288  * Reading, writing, and flushing data.
1289  *
1290  * @{
1291  */
1292
1293 /**
1294  * Read bytes from the specified serial port, blocking until complete.
1295  *
1296  * @warning If your program runs on Unix, defines its own signal handlers, and
1297  *          needs to abort blocking reads when these are called, then you
1298  *          should not use this function. It repeats system calls that return
1299  *          with EINTR. To be able to abort a read from a signal handler, you
1300  *          should implement your own blocking read using sp_nonblocking_read()
1301  *          together with a blocking method that makes sense for your program.
1302  *          E.g. you can obtain the file descriptor for an open port using
1303  *          sp_get_port_handle() and use this to call select() or pselect(),
1304  *          with appropriate arrangements to return if a signal is received.
1305  *
1306  * @param[in] port Pointer to a port structure. Must not be NULL.
1307  * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1308  * @param[in] count Requested number of bytes to read.
1309  * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1310  *
1311  * @return The number of bytes read on success, or a negative error code. If
1312  *         the number of bytes returned is less than that requested, the
1313  *         timeout was reached before the requested number of bytes was
1314  *         available. If timeout is zero, the function will always return
1315  *         either the requested number of bytes or a negative error code.
1316  *
1317  * @since 0.1.0
1318  */
1319 SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
1320
1321 /**
1322  * Read bytes from the specified serial port, returning as soon as any data is
1323  * available.
1324  *
1325  * @warning If your program runs on Unix, defines its own signal handlers, and
1326  *          needs to abort blocking reads when these are called, then you
1327  *          should not use this function. It repeats system calls that return
1328  *          with EINTR. To be able to abort a read from a signal handler, you
1329  *          should implement your own blocking read using sp_nonblocking_read()
1330  *          together with a blocking method that makes sense for your program.
1331  *          E.g. you can obtain the file descriptor for an open port using
1332  *          sp_get_port_handle() and use this to call select() or pselect(),
1333  *          with appropriate arrangements to return if a signal is received.
1334  *
1335  * @param[in] port Pointer to a port structure. Must not be NULL.
1336  * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1337  * @param[in] count Maximum number of bytes to read. Must not be zero.
1338  * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1339  *
1340  * @return The number of bytes read on success, or a negative error code. If
1341  *         the result is zero, the timeout was reached before any bytes were
1342  *         available. If timeout_ms is zero, the function will always return
1343  *         either at least one byte, or a negative error code.
1344  *
1345  * @since 0.1.1
1346  */
1347 SP_API enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms);
1348
1349 /**
1350  * Read bytes from the specified serial port, without blocking.
1351  *
1352  * @param[in] port Pointer to a port structure. Must not be NULL.
1353  * @param[out] buf Buffer in which to store the bytes read. Must not be NULL.
1354  * @param[in] count Maximum number of bytes to read.
1355  *
1356  * @return The number of bytes read on success, or a negative error code. The
1357  *         number of bytes returned may be any number from zero to the maximum
1358  *         that was requested.
1359  *
1360  * @since 0.1.0
1361  */
1362 SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
1363
1364 /**
1365  * Write bytes to the specified serial port, blocking until complete.
1366  *
1367  * Note that this function only ensures that the accepted bytes have been
1368  * written to the OS; they may be held in driver or hardware buffers and not
1369  * yet physically transmitted. To check whether all written bytes have actually
1370  * been transmitted, use the sp_output_waiting() function. To wait until all
1371  * written bytes have actually been transmitted, use the sp_drain() function.
1372  *
1373  * @warning If your program runs on Unix, defines its own signal handlers, and
1374  *          needs to abort blocking writes when these are called, then you
1375  *          should not use this function. It repeats system calls that return
1376  *          with EINTR. To be able to abort a write from a signal handler, you
1377  *          should implement your own blocking write using sp_nonblocking_write()
1378  *          together with a blocking method that makes sense for your program.
1379  *          E.g. you can obtain the file descriptor for an open port using
1380  *          sp_get_port_handle() and use this to call select() or pselect(),
1381  *          with appropriate arrangements to return if a signal is received.
1382  *
1383  * @param[in] port Pointer to a port structure. Must not be NULL.
1384  * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1385  * @param[in] count Requested number of bytes to write.
1386  * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1387  *
1388  * @return The number of bytes written on success, or a negative error code.
1389  *         If the number of bytes returned is less than that requested, the
1390  *         timeout was reached before the requested number of bytes was
1391  *         written. If timeout is zero, the function will always return
1392  *         either the requested number of bytes or a negative error code. In
1393  *         the event of an error there is no way to determine how many bytes
1394  *         were sent before the error occurred.
1395  *
1396  * @since 0.1.0
1397  */
1398 SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout_ms);
1399
1400 /**
1401  * Write bytes to the specified serial port, without blocking.
1402  *
1403  * Note that this function only ensures that the accepted bytes have been
1404  * written to the OS; they may be held in driver or hardware buffers and not
1405  * yet physically transmitted. To check whether all written bytes have actually
1406  * been transmitted, use the sp_output_waiting() function. To wait until all
1407  * written bytes have actually been transmitted, use the sp_drain() function.
1408  *
1409  * @param[in] port Pointer to a port structure. Must not be NULL.
1410  * @param[in] buf Buffer containing the bytes to write. Must not be NULL.
1411  * @param[in] count Maximum number of bytes to write.
1412  *
1413  * @return The number of bytes written on success, or a negative error code.
1414  *         The number of bytes returned may be any number from zero to the
1415  *         maximum that was requested.
1416  *
1417  * @since 0.1.0
1418  */
1419 SP_API enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
1420
1421 /**
1422  * Gets the number of bytes waiting in the input buffer.
1423  *
1424  * @param[in] port Pointer to a port structure. Must not be NULL.
1425  *
1426  * @return Number of bytes waiting on success, a negative error code otherwise.
1427  *
1428  * @since 0.1.0
1429  */
1430 SP_API enum sp_return sp_input_waiting(struct sp_port *port);
1431
1432 /**
1433  * Gets the number of bytes waiting in the output buffer.
1434  *
1435  * @param[in] port Pointer to a port structure. Must not be NULL.
1436  *
1437  * @return Number of bytes waiting on success, a negative error code otherwise.
1438  *
1439  * @since 0.1.0
1440  */
1441 SP_API enum sp_return sp_output_waiting(struct sp_port *port);
1442
1443 /**
1444  * Flush serial port buffers. Data in the selected buffer(s) is discarded.
1445  *
1446  * @param[in] port Pointer to a port structure. Must not be NULL.
1447  * @param[in] buffers Which buffer(s) to flush.
1448  *
1449  * @return SP_OK upon success, a negative error code otherwise.
1450  *
1451  * @since 0.1.0
1452  */
1453 SP_API enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
1454
1455 /**
1456  * Wait for buffered data to be transmitted.
1457  *
1458  * @warning If your program runs on Unix, defines its own signal handlers, and
1459  *          needs to abort draining the output buffer when when these are
1460  *          called, then you should not use this function. It repeats system
1461  *          calls that return with EINTR. To be able to abort a drain from a
1462  *          signal handler, you would need to implement your own blocking
1463  *          drain by polling the result of sp_output_waiting().
1464  *
1465  * @param[in] port Pointer to a port structure. Must not be NULL.
1466  *
1467  * @return SP_OK upon success, a negative error code otherwise.
1468  *
1469  * @since 0.1.0
1470  */
1471 SP_API enum sp_return sp_drain(struct sp_port *port);
1472
1473 /**
1474  * @}
1475  *
1476  * @defgroup Waiting Waiting
1477  *
1478  * Waiting for events and timeout handling.
1479  *
1480  * @{
1481  */
1482
1483 /**
1484  * Allocate storage for a set of events.
1485  *
1486  * The user should allocate a variable of type struct sp_event_set *,
1487  * then pass a pointer to this variable to receive the result.
1488  *
1489  * The result should be freed after use by calling sp_free_event_set().
1490  *
1491  * @param[out] result_ptr If any error is returned, the variable pointed to by
1492  *                        result_ptr will be set to NULL. Otherwise, it will
1493  *                        be set to point to the event set. Must not be NULL.
1494  *
1495  * @return SP_OK upon success, a negative error code otherwise.
1496  *
1497  * @since 0.1.0
1498  */
1499 SP_API enum sp_return sp_new_event_set(struct sp_event_set **result_ptr);
1500
1501 /**
1502  * Add events to a struct sp_event_set for a given port.
1503  *
1504  * The port must first be opened by calling sp_open() using the same port
1505  * structure.
1506  *
1507  * After the port is closed or the port structure freed, the results may
1508  * no longer be valid.
1509  *
1510  * @param[in,out] event_set Event set to update. Must not be NULL.
1511  * @param[in] port Pointer to a port structure. Must not be NULL.
1512  * @param[in] mask Bitmask of events to be waited for.
1513  *
1514  * @return SP_OK upon success, a negative error code otherwise.
1515  *
1516  * @since 0.1.0
1517  */
1518 SP_API enum sp_return sp_add_port_events(struct sp_event_set *event_set,
1519         const struct sp_port *port, enum sp_event mask);
1520
1521 /**
1522  * Wait for any of a set of events to occur.
1523  *
1524  * @param[in] event_set Event set to wait on. Must not be NULL.
1525  * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely.
1526  *
1527  * @return SP_OK upon success, a negative error code otherwise.
1528  *
1529  * @since 0.1.0
1530  */
1531 SP_API enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout_ms);
1532
1533 /**
1534  * Free a structure allocated by sp_new_event_set().
1535  *
1536  * @param[in] event_set Event set to free. Must not be NULL.
1537  *
1538  * @since 0.1.0
1539  */
1540 SP_API void sp_free_event_set(struct sp_event_set *event_set);
1541
1542 /**
1543  * @}
1544  *
1545  * @defgroup Signals Signals
1546  *
1547  * Port signalling operations.
1548  *
1549  * @{
1550  */
1551
1552 /**
1553  * Gets the status of the control signals for the specified port.
1554  *
1555  * The user should allocate a variable of type "enum sp_signal" and pass a
1556  * pointer to this variable to receive the result. The result is a bitmask
1557  * in which individual signals can be checked by bitwise OR with values of
1558  * the sp_signal enum.
1559  *
1560  * @param[in] port Pointer to a port structure. Must not be NULL.
1561  * @param[out] signal_mask Pointer to a variable to receive the result.
1562  *                         Must not be NULL.
1563  *
1564  * @return SP_OK upon success, a negative error code otherwise.
1565  *
1566  * @since 0.1.0
1567  */
1568 SP_API enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signal_mask);
1569
1570 /**
1571  * Put the port transmit line into the break state.
1572  *
1573  * @param[in] port Pointer to a port structure. Must not be NULL.
1574  *
1575  * @return SP_OK upon success, a negative error code otherwise.
1576  *
1577  * @since 0.1.0
1578  */
1579 SP_API enum sp_return sp_start_break(struct sp_port *port);
1580
1581 /**
1582  * Take the port transmit line out of the break state.
1583  *
1584  * @param[in] port Pointer to a port structure. Must not be NULL.
1585  *
1586  * @return SP_OK upon success, a negative error code otherwise.
1587  *
1588  * @since 0.1.0
1589  */
1590 SP_API enum sp_return sp_end_break(struct sp_port *port);
1591
1592 /**
1593  * @}
1594  *
1595  * @defgroup Errors Errors
1596  *
1597  * Obtaining error information.
1598  *
1599  * @{
1600  */
1601
1602 /**
1603  * Get the error code for a failed operation.
1604  *
1605  * In order to obtain the correct result, this function should be called
1606  * straight after the failure, before executing any other system operations.
1607  * The result is thread-specific, and only valid when called immediately
1608  * after a previous call returning SP_ERR_FAIL.
1609  *
1610  * @return The system's numeric code for the error that caused the last
1611  *         operation to fail.
1612  *
1613  * @since 0.1.0
1614  */
1615 SP_API int sp_last_error_code(void);
1616
1617 /**
1618  * Get the error message for a failed operation.
1619  *
1620  * In order to obtain the correct result, this function should be called
1621  * straight after the failure, before executing other system operations.
1622  * The result is thread-specific, and only valid when called immediately
1623  * after a previous call returning SP_ERR_FAIL.
1624  *
1625  * @return The system's message for the error that caused the last
1626  *         operation to fail. This string may be allocated by the function,
1627  *         and should be freed after use by calling sp_free_error_message().
1628  *
1629  * @since 0.1.0
1630  */
1631 SP_API char *sp_last_error_message(void);
1632
1633 /**
1634  * Free an error message returned by sp_last_error_message().
1635  *
1636  * @param[in] message The error message string to free. Must not be NULL.
1637  *
1638  * @since 0.1.0
1639  */
1640 SP_API void sp_free_error_message(char *message);
1641
1642 /**
1643  * Set the handler function for library debugging messages.
1644  *
1645  * Debugging messages are generated by the library during each operation,
1646  * to help in diagnosing problems. The handler will be called for each
1647  * message. The handler can be set to NULL to ignore all debug messages.
1648  *
1649  * The handler function should accept a format string and variable length
1650  * argument list, in the same manner as e.g. printf().
1651  *
1652  * The default handler is sp_default_debug_handler().
1653  *
1654  * @param[in] handler The handler function to use. Can be NULL (in that case
1655  *                    all debug messages will be ignored).
1656  *
1657  * @since 0.1.0
1658  */
1659 SP_API void sp_set_debug_handler(void (*handler)(const char *format, ...));
1660
1661 /**
1662  * Default handler function for library debugging messages.
1663  *
1664  * This function prints debug messages to the standard error stream if the
1665  * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
1666  * ignored.
1667  *
1668  * @param[in] format The format string to use. Must not be NULL.
1669  * @param[in] ... The variable length argument list to use.
1670  *
1671  * @since 0.1.0
1672  */
1673 SP_API void sp_default_debug_handler(const char *format, ...);
1674
1675 /** @} */
1676
1677 /**
1678  * @defgroup Versions Versions
1679  *
1680  * Version number querying functions, definitions, and macros.
1681  *
1682  * This set of API calls returns two different version numbers related
1683  * to libserialport. The "package version" is the release version number of the
1684  * libserialport tarball in the usual "major.minor.micro" format, e.g. "0.1.0".
1685  *
1686  * The "library version" is independent of that; it is the libtool version
1687  * number in the "current:revision:age" format, e.g. "2:0:0".
1688  * See http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning for details.
1689  *
1690  * Both version numbers (and/or individual components of them) can be
1691  * retrieved via the API calls at runtime, and/or they can be checked at
1692  * compile/preprocessor time using the respective macros.
1693  *
1694  * @{
1695  */
1696
1697 /*
1698  * Package version macros (can be used for conditional compilation).
1699  */
1700
1701 /** The libserialport package 'major' version number. */
1702 #define SP_PACKAGE_VERSION_MAJOR 0
1703
1704 /** The libserialport package 'minor' version number. */
1705 #define SP_PACKAGE_VERSION_MINOR 1
1706
1707 /** The libserialport package 'micro' version number. */
1708 #define SP_PACKAGE_VERSION_MICRO 1
1709
1710 /** The libserialport package version ("major.minor.micro") as string. */
1711 #define SP_PACKAGE_VERSION_STRING "0.1.1"
1712
1713 /*
1714  * Library/libtool version macros (can be used for conditional compilation).
1715  */
1716
1717 /** The libserialport libtool 'current' version number. */
1718 #define SP_LIB_VERSION_CURRENT 1
1719
1720 /** The libserialport libtool 'revision' version number. */
1721 #define SP_LIB_VERSION_REVISION 0
1722
1723 /** The libserialport libtool 'age' version number. */
1724 #define SP_LIB_VERSION_AGE 1
1725
1726 /** The libserialport libtool version ("current:revision:age") as string. */
1727 #define SP_LIB_VERSION_STRING "1:0:1"
1728
1729 /**
1730  * Get the major libserialport package version number.
1731  *
1732  * @return The major package version number.
1733  *
1734  * @since 0.1.0
1735  */
1736 SP_API int sp_get_major_package_version(void);
1737
1738 /**
1739  * Get the minor libserialport package version number.
1740  *
1741  * @return The minor package version number.
1742  *
1743  * @since 0.1.0
1744  */
1745 SP_API int sp_get_minor_package_version(void);
1746
1747 /**
1748  * Get the micro libserialport package version number.
1749  *
1750  * @return The micro package version number.
1751  *
1752  * @since 0.1.0
1753  */
1754 SP_API int sp_get_micro_package_version(void);
1755
1756 /**
1757  * Get the libserialport package version number as a string.
1758  *
1759  * @return The package version number string. The returned string is
1760  *         static and thus should NOT be free'd by the caller.
1761  *
1762  * @since 0.1.0
1763  */
1764 SP_API const char *sp_get_package_version_string(void);
1765
1766 /**
1767  * Get the "current" part of the libserialport library version number.
1768  *
1769  * @return The "current" library version number.
1770  *
1771  * @since 0.1.0
1772  */
1773 SP_API int sp_get_current_lib_version(void);
1774
1775 /**
1776  * Get the "revision" part of the libserialport library version number.
1777  *
1778  * @return The "revision" library version number.
1779  *
1780  * @since 0.1.0
1781  */
1782 SP_API int sp_get_revision_lib_version(void);
1783
1784 /**
1785  * Get the "age" part of the libserialport library version number.
1786  *
1787  * @return The "age" library version number.
1788  *
1789  * @since 0.1.0
1790  */
1791 SP_API int sp_get_age_lib_version(void);
1792
1793 /**
1794  * Get the libserialport library version number as a string.
1795  *
1796  * @return The library version number string. The returned string is
1797  *         static and thus should NOT be free'd by the caller.
1798  *
1799  * @since 0.1.0
1800  */
1801 SP_API const char *sp_get_lib_version_string(void);
1802
1803 /** @} */
1804
1805 /**
1806  * @example list_ports.c Getting a list of ports present on the system.
1807  * @example port_info.c Getting information on a particular serial port.
1808  * @example port_config.c Accessing configuration settings of a port.
1809 */
1810
1811 #ifdef __cplusplus
1812 }
1813 #endif
1814
1815 #endif