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