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