]> sigrok.org Git - libserialport.git/blob - libserialport.h.in
Add runtime version querying functions.
[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 occured 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 };
113
114 /** Port events. */
115 enum sp_event {
116         /* Data received and ready to read. */
117         SP_EVENT_RX_READY = 1,
118         /* Ready to transmit new data. */
119         SP_EVENT_TX_READY = 2,
120         /* Error occured. */
121         SP_EVENT_ERROR = 4,
122 };
123
124 /** Buffer selection. */
125 enum sp_buffer {
126         /** Input buffer. */
127         SP_BUF_INPUT = 1,
128         /** Output buffer. */
129         SP_BUF_OUTPUT = 2,
130         /** Both buffers. */
131         SP_BUF_BOTH = 3,
132 };
133
134 /** Parity settings. */
135 enum sp_parity {
136         /** Special value to indicate setting should be left alone. */
137         SP_PARITY_INVALID = -1,
138         /** No parity. */
139         SP_PARITY_NONE = 0,
140         /** Odd parity. */
141         SP_PARITY_ODD = 1,
142         /** Even parity. */
143         SP_PARITY_EVEN = 2,
144         /** Mark parity. */
145         SP_PARITY_MARK = 3,
146         /** Space parity. */
147         SP_PARITY_SPACE = 4,
148 };
149
150 /** RTS pin behaviour. */
151 enum sp_rts {
152         /** Special value to indicate setting should be left alone. */
153         SP_RTS_INVALID = -1,
154         /** RTS off. */
155         SP_RTS_OFF = 0,
156         /** RTS on. */
157         SP_RTS_ON = 1,
158         /** RTS used for flow control. */
159         SP_RTS_FLOW_CONTROL = 2,
160 };
161
162 /** CTS pin behaviour. */
163 enum sp_cts {
164         /** Special value to indicate setting should be left alone. */
165         SP_CTS_INVALID = -1,
166         /** CTS ignored. */
167         SP_CTS_IGNORE = 0,
168         /** CTS used for flow control. */
169         SP_CTS_FLOW_CONTROL = 1,
170 };
171
172 /** DTR pin behaviour. */
173 enum sp_dtr {
174         /** Special value to indicate setting should be left alone. */
175         SP_DTR_INVALID = -1,
176         /** DTR off. */
177         SP_DTR_OFF = 0,
178         /** DTR on. */
179         SP_DTR_ON = 1,
180         /** DTR used for flow control. */
181         SP_DTR_FLOW_CONTROL = 2,
182 };
183
184 /** DSR pin behaviour. */
185 enum sp_dsr {
186         /** Special value to indicate setting should be left alone. */
187         SP_DSR_INVALID = -1,
188         /** DSR ignored. */
189         SP_DSR_IGNORE = 0,
190         /** DSR used for flow control. */
191         SP_DSR_FLOW_CONTROL = 1,
192 };
193
194 /** XON/XOFF flow control behaviour. */
195 enum sp_xonxoff {
196         /** Special value to indicate setting should be left alone. */
197         SP_XONXOFF_INVALID = -1,
198         /** XON/XOFF disabled. */
199         SP_XONXOFF_DISABLED = 0,
200         /** XON/XOFF enabled for input only. */
201         SP_XONXOFF_IN = 1,
202         /** XON/XOFF enabled for output only. */
203         SP_XONXOFF_OUT = 2,
204         /** XON/XOFF enabled for input and output. */
205         SP_XONXOFF_INOUT = 3,
206 };
207
208 /** Standard flow control combinations. */
209 enum sp_flowcontrol {
210         /** No flow control. */
211         SP_FLOWCONTROL_NONE = 0,
212         /** Software flow control using XON/XOFF characters. */
213         SP_FLOWCONTROL_XONXOFF = 1,
214         /** Hardware flow control using RTS/CTS signals. */
215         SP_FLOWCONTROL_RTSCTS = 2,
216         /** Hardware flow control using DTR/DSR signals. */
217         SP_FLOWCONTROL_DTRDSR = 3,
218 };
219
220 /** Input signals. */
221 enum sp_signal {
222         /** Clear to send. */
223         SP_SIG_CTS = 1,
224         /** Data set ready. */
225         SP_SIG_DSR = 2,
226         /** Data carrier detect. */
227         SP_SIG_DCD = 4,
228         /** Ring indicator. */
229         SP_SIG_RI = 8,
230 };
231
232 /**
233  * @struct sp_port
234  * An opaque structure representing a serial port.
235  */
236 struct sp_port;
237
238 /**
239  * @struct sp_port_config
240  * An opaque structure representing the configuration for a serial port.
241  */
242 struct sp_port_config;
243
244 /**
245  * @struct sp_event_set
246  * A set of handles to wait on for events.
247  */
248 struct sp_event_set {
249         /** Array of OS-specific handles. */
250         void *handles;
251         /** Array of bitmasks indicating which events apply for each handle. */
252         enum sp_event *masks;
253         /** Number of handles. */
254         unsigned int count;
255 };
256
257 /**
258 @defgroup Enumeration Port enumeration
259 @{
260 */
261
262 /**
263  * Obtain a pointer to a new sp_port structure representing the named port.
264  *
265  * The user should allocate a variable of type "struct sp_port *" and pass a
266  * pointer to this to receive the result.
267  *
268  * The result should be freed after use by calling sp_free_port().
269  *
270  * If any error is returned, the variable pointed to by port_ptr will be set
271  * to NULL. Otherwise, it will be set to point to the newly allocated port.
272  *
273  * @return SP_OK upon success, a negative error code otherwise.
274  */
275 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
276
277 /**
278  * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
279  */
280 void sp_free_port(struct sp_port *port);
281
282 /**
283  * List the serial ports available on the system.
284  *
285  * The result obtained is an array of pointers to sp_port structures,
286  * terminated by a NULL. The user should allocate a variable of type
287  * "struct sp_port **" and pass a pointer to this to receive the result.
288  *
289  * The result should be freed after use by calling sp_free_port_list().
290  * If a port from the list is to be used after freeing the list, it must be
291  * copied first using sp_copy_port().
292  *
293  * If any error is returned, the variable pointed to by list_ptr will be set
294  * to NULL. Otherwise, it will be set to point to the newly allocated array.
295  *
296  * @return SP_OK upon success, a negative error code otherwise.
297  */
298 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
299
300 /**
301  * Make a new copy of a sp_port structure.
302  *
303  * The user should allocate a variable of type "struct sp_port *" and pass a
304  * pointer to this to receive the result.
305  *
306  * The copy should be freed after use by calling sp_free_port().
307  *
308  * If any error is returned, the variable pointed to by copy_ptr will be set
309  * to NULL. Otherwise, it will be set to point to the newly allocated copy.
310  *
311  * @return SP_OK upon success, a negative error code otherwise.
312  */
313 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
314
315 /**
316  * Free a port list obtained from sp_list_ports().
317  *
318  * This will also free all the sp_port structures referred to from the list;
319  * any that are to be retained must be copied first using sp_copy_port().
320  */
321 void sp_free_port_list(struct sp_port **ports);
322
323 /**
324  * @}
325  * @defgroup Ports Opening, closing and querying ports
326  * @{
327  */
328
329 /**
330  * Open the specified serial port.
331  *
332  * @param port Pointer to port structure.
333  * @param flags Flags to use when opening the serial port.
334  *
335  * @return SP_OK upon success, a negative error code otherwise.
336  */
337 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
338
339 /**
340  * Close the specified serial port.
341  *
342  * @return SP_OK upon success, a negative error code otherwise.
343  */
344 enum sp_return sp_close(struct sp_port *port);
345
346 /**
347  * Get the name of a port.
348  *
349  * The name returned is whatever is normally used to refer to a port on the
350  * current operating system; e.g. for Windows it will usually be a "COMn"
351  * device name, and for Unix it will be a device path beginning with "/dev/".
352  *
353  * @param port Pointer to port structure.
354  *
355  * @return The port name, or NULL if an invalid port is passed. The name
356  * string is part of the port structure and may not be used after the
357  * port structure has been freed.
358  */
359 char *sp_get_port_name(const struct sp_port *port);
360
361 /**
362  * Get the operating system handle for a port.
363  *
364  * The type of the handle depends on the operating system. On Unix based
365  * systems, the handle is a file descriptor of type "int". On Windows, the
366  * handle is of type "HANDLE". The user should allocate a variable of the
367  * appropriate type and pass a pointer to this to receive the result.
368  *
369  * To obtain a valid handle, the port must first be opened by calling
370  * sp_open() using the same port structure.
371  *
372  * After the port is closed or the port structure freed, the handle may
373  * no longer be valid.
374  *
375  * @warning This feature is provided so that programs may make use of
376  *          OS-specific functionality where desired. Doing so obviously
377  *          comes at a cost in portability. It also cannot be guaranteed
378  *          that direct usage of the OS handle will not conflict with the
379  *          library's own usage of the port. Be careful.
380  *
381  * @return SP_OK upon success, a negative error code otherwise.
382  */
383 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
384
385 /**
386  * @}
387  * @defgroup Configuration Setting port parameters
388  * @{
389  */
390
391 /**
392  * Allocate a port configuration structure.
393  *
394  * The user should allocate a variable of type "struct sp_config *" and pass a
395  * pointer to this to receive the result. The variable will be updated to
396  * point to the new configuration structure. The structure is opaque and must
397  * be accessed via the functions provided.
398  *
399  * All parameters in the structure will be initialised to special values which
400  * are ignored by sp_set_config().
401  *
402  * The structure should be freed after use by calling sp_free_config().
403  *
404  * @param config_ptr Pointer to variable to receive result.
405  *
406  * @return SP_OK upon success, a negative error code otherwise.
407  */
408 enum sp_return sp_new_config(struct sp_port_config **config_ptr);
409
410 /**
411  * Free a port configuration structure.
412  *
413  * @param config Pointer to configuration structure.
414  */
415 void sp_free_config(struct sp_port_config *config);
416
417 /**
418  * Get the current configuration of the specified serial port.
419  *
420  * The user should allocate a configuration structure using sp_new_config()
421  * and pass this as the config parameter. The configuration structure will
422  * be updated with the port configuration.
423  *
424  * Any parameters that are configured with settings not recognised or
425  * supported by libserialport, will be set to special values that are
426  * ignored by sp_set_config().
427  *
428  * @return SP_OK upon success, a negative error code otherwise.
429  */
430 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
431
432 /**
433  * Set the configuration for the specified serial port.
434  *
435  * For each parameter in the configuration, there is a special value (usually
436  * -1, but see the documentation for each field). These values will be ignored
437  * and the corresponding setting left unchanged on the port.
438  *
439  * @return SP_OK upon success, a negative error code otherwise.
440  */
441 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
442
443 /**
444  * Set the baud rate for the specified serial port.
445  *
446  * @param port Pointer to port structure.
447  * @param baudrate Baud rate in bits per second.
448  *
449  * @return SP_OK upon success, a negative error code otherwise.
450  */
451 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
452
453 /**
454  * Get the baud rate from a port configuration.
455  *
456  * The user should allocate a variable of type int and pass a pointer to this
457  * to receive the result.
458  *
459  * @param config Pointer to configuration structure.
460  * @param baudrate_ptr Pointer to variable to store result.
461  *
462  * @return SP_OK upon success, a negative error code otherwise.
463  */
464 enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
465
466 /**
467  * Set the baud rate in a port configuration.
468  *
469  * @param config Pointer to configuration structure.
470  * @param baudrate Baud rate in bits per second, or -1 to retain current setting.
471  *
472  * @return SP_OK upon success, a negative error code otherwise.
473  */
474 enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
475
476 /**
477  * Set the data bits for the specified serial port.
478  *
479  * @param port Pointer to port structure.
480  * @param bits Number of data bits.
481  *
482  * @return SP_OK upon success, a negative error code otherwise.
483  */
484 enum sp_return sp_set_bits(struct sp_port *port, int bits);
485
486 /**
487  * Get the data bits from a port configuration.
488  *
489  * The user should allocate a variable of type int and pass a pointer to this
490  * to receive the result.
491  *
492  * @param config Pointer to configuration structure.
493  * @param bits_ptr Pointer to variable to store result.
494  *
495  * @return SP_OK upon success, a negative error code otherwise.
496  */
497 enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
498
499 /**
500  * Set the data bits in a port configuration.
501  *
502  * @param config Pointer to configuration structure.
503  * @param bits Number of data bits, or -1 to retain current setting.
504  *
505  * @return SP_OK upon success, a negative error code otherwise.
506  */
507 enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
508
509 /**
510  * Set the parity setting for the specified serial port.
511  *
512  * @param port Pointer to port structure.
513  * @param parity Parity setting.
514  *
515  * @return SP_OK upon success, a negative error code otherwise.
516  */
517 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
518
519 /**
520  * Get the parity setting from a port configuration.
521  *
522  * The user should allocate a variable of type enum sp_parity and pass a pointer to this
523  * to receive the result.
524  *
525  * @param config Pointer to configuration structure.
526  * @param parity_ptr Pointer to variable to store result.
527  *
528  * @return SP_OK upon success, a negative error code otherwise.
529  */
530 enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
531
532 /**
533  * Set the parity setting in a port configuration.
534  *
535  * @param config Pointer to configuration structure.
536  * @param parity Parity setting, or -1 to retain current setting.
537  *
538  * @return SP_OK upon success, a negative error code otherwise.
539  */
540 enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
541
542 /**
543  * Set the stop bits for the specified serial port.
544  *
545  * @param port Pointer to port structure.
546  * @param stopbits Number of stop bits.
547  *
548  * @return SP_OK upon success, a negative error code otherwise.
549  */
550 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
551
552 /**
553  * Get the stop bits from a port configuration.
554  *
555  * The user should allocate a variable of type int and pass a pointer to this
556  * to receive the result.
557  *
558  * @param config Pointer to configuration structure.
559  * @param stopbits_ptr Pointer to variable to store result.
560  *
561  * @return SP_OK upon success, a negative error code otherwise.
562  */
563 enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
564
565 /**
566  * Set the stop bits in a port configuration.
567  *
568  * @param config Pointer to configuration structure.
569  * @param stopbits Number of stop bits, or -1 to retain current setting.
570  *
571  * @return SP_OK upon success, a negative error code otherwise.
572  */
573 enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
574
575 /**
576  * Set the RTS pin behaviour for the specified serial port.
577  *
578  * @param port Pointer to port structure.
579  * @param rts RTS pin mode.
580  *
581  * @return SP_OK upon success, a negative error code otherwise.
582  */
583 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
584
585 /**
586  * Get the RTS pin behaviour from a port configuration.
587  *
588  * The user should allocate a variable of type enum sp_rts and pass a pointer to this
589  * to receive the result.
590  *
591  * @param config Pointer to configuration structure.
592  * @param rts_ptr Pointer to variable to store result.
593  *
594  * @return SP_OK upon success, a negative error code otherwise.
595  */
596 enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
597
598 /**
599  * Set the RTS pin behaviour in a port configuration.
600  *
601  * @param config Pointer to configuration structure.
602  * @param rts RTS pin mode, or -1 to retain current setting.
603  *
604  * @return SP_OK upon success, a negative error code otherwise.
605  */
606 enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
607
608 /**
609  * Set the CTS pin behaviour for the specified serial port.
610  *
611  * @param port Pointer to port structure.
612  * @param cts CTS pin mode.
613  *
614  * @return SP_OK upon success, a negative error code otherwise.
615  */
616 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
617
618 /**
619  * Get the CTS pin behaviour from a port configuration.
620  *
621  * The user should allocate a variable of type enum sp_cts and pass a pointer to this
622  * to receive the result.
623  *
624  * @param config Pointer to configuration structure.
625  * @param cts_ptr Pointer to variable to store result.
626  *
627  * @return SP_OK upon success, a negative error code otherwise.
628  */
629 enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
630
631 /**
632  * Set the CTS pin behaviour in a port configuration.
633  *
634  * @param config Pointer to configuration structure.
635  * @param cts CTS pin mode, or -1 to retain current setting.
636  *
637  * @return SP_OK upon success, a negative error code otherwise.
638  */
639 enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
640
641 /**
642  * Set the DTR pin behaviour for the specified serial port.
643  *
644  * @param port Pointer to port structure.
645  * @param dtr DTR pin mode.
646  *
647  * @return SP_OK upon success, a negative error code otherwise.
648  */
649 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
650
651 /**
652  * Get the DTR pin behaviour from a port configuration.
653  *
654  * The user should allocate a variable of type enum sp_dtr and pass a pointer to this
655  * to receive the result.
656  *
657  * @param config Pointer to configuration structure.
658  * @param dtr_ptr Pointer to variable to store result.
659  *
660  * @return SP_OK upon success, a negative error code otherwise.
661  */
662 enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
663
664 /**
665  * Set the DTR pin behaviour in a port configuration.
666  *
667  * @param config Pointer to configuration structure.
668  * @param dtr DTR pin mode, or -1 to retain current setting.
669  *
670  * @return SP_OK upon success, a negative error code otherwise.
671  */
672 enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
673
674 /**
675  * Set the DSR pin behaviour for the specified serial port.
676  *
677  * @param port Pointer to port structure.
678  * @param dsr DSR pin mode.
679  *
680  * @return SP_OK upon success, a negative error code otherwise.
681  */
682 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
683
684 /**
685  * Get the DSR pin behaviour from a port configuration.
686  *
687  * The user should allocate a variable of type enum sp_dsr and pass a pointer to this
688  * to receive the result.
689  *
690  * @param config Pointer to configuration structure.
691  * @param dsr_ptr Pointer to variable to store result.
692  *
693  * @return SP_OK upon success, a negative error code otherwise.
694  */
695 enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
696
697 /**
698  * Set the DSR pin behaviour in a port configuration.
699  *
700  * @param config Pointer to configuration structure.
701  * @param dsr DSR pin mode, or -1 to retain current setting.
702  *
703  * @return SP_OK upon success, a negative error code otherwise.
704  */
705 enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
706
707 /**
708  * Set the XON/XOFF configuration for the specified serial port.
709  *
710  * @param port Pointer to port structure.
711  * @param xon_xoff XON/XOFF mode.
712  *
713  * @return SP_OK upon success, a negative error code otherwise.
714  */
715 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
716
717 /**
718  * Get the XON/XOFF configuration from a port configuration.
719  *
720  * The user should allocate a variable of type enum sp_xonxoff and pass a pointer to this
721  * to receive the result.
722  *
723  * @param config Pointer to configuration structure.
724  * @param xon_xoff_ptr Pointer to variable to store result.
725  *
726  * @return SP_OK upon success, a negative error code otherwise.
727  */
728 enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
729
730 /**
731  * Set the XON/XOFF configuration in a port configuration.
732  *
733  * @param config Pointer to configuration structure.
734  * @param xon_xoff XON/XOFF mode, or -1 to retain current setting.
735  *
736  * @return SP_OK upon success, a negative error code otherwise.
737  */
738 enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
739
740 /**
741  * Set the flow control type in a port configuration.
742  *
743  * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
744  * XON/XOFF settings as necessary for the specified flow control
745  * type. For more fine-grained control of these settings, use their
746  * individual configuration functions.
747  *
748  * @param config Pointer to configuration structure.
749  * @param flowcontrol Flow control setting to use.
750  *
751  * @return SP_OK upon success, a negative error code otherwise.
752  */
753 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
754
755 /**
756  * Set the flow control type for the specified serial port.
757  *
758  * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
759  * XON/XOFF settings as necessary for the specified flow control
760  * type. For more fine-grained control of these settings, use their
761  * individual configuration functions.
762  *
763  * @param port Pointer to port structure.
764  * @param flowcontrol Flow control setting to use.
765  *
766  * @return SP_OK upon success, a negative error code otherwise.
767  */
768 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
769
770 /**
771  * @}
772  * @defgroup Data Reading, writing, and flushing data
773  * @{
774 */
775
776 /**
777  * Read bytes from the specified serial port, blocking until complete.
778  *
779  * @warning If your program runs on Unix, defines its own signal handlers, and
780  *          needs to abort blocking reads when these are called, then you
781  *          should not use this function. It repeats system calls that return
782  *          with EINTR. To be able to abort a read from a signal handler, you
783  *          should implement your own blocking read using sp_nonblocking_read()
784  *          together with a blocking method that makes sense for your program.
785  *          E.g. you can obtain the file descriptor for an open port using
786  *          sp_get_port_handle() and use this to call select() or pselect(),
787  *          with appropriate arrangements to return if a signal is received.
788  *
789  * @param port Pointer to port structure.
790  * @param buf Buffer in which to store the bytes read.
791  * @param count Requested number of bytes to read.
792  * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
793  *
794  * @return The number of bytes read on success, or a negative error code. If
795  *         the number of bytes returned is less than that requested, the
796  *         timeout was reached before the requested number of bytes was
797  *         available. If timeout is zero, the function will always return
798  *         either the requested number of bytes or a negative error code.
799  */
800 enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout);
801
802 /**
803  * Read bytes from the specified serial port, without blocking.
804  *
805  * @param port Pointer to port structure.
806  * @param buf Buffer in which to store the bytes read.
807  * @param count Maximum number of bytes to read.
808  *
809  * @return The number of bytes read on success, or a negative error code. The
810  *         number of bytes returned may be any number from zero to the maximum
811  *         that was requested.
812  */
813 enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
814
815 /**
816  * Write bytes to the specified serial port, blocking until complete.
817  *
818  * Note that this function only ensures that the accepted bytes have been
819  * written to the OS; they may be held in driver or hardware buffers and not
820  * yet physically transmitted. To check whether all written bytes have actually
821  * been transmitted, use the sp_output_waiting() function. To wait until all
822  * written bytes have actually been transmitted, use the sp_drain() function.
823  *
824  * @warning If your program runs on Unix, defines its own signal handlers, and
825  *          needs to abort blocking writes when these are called, then you
826  *          should not use this function. It repeats system calls that return
827  *          with EINTR. To be able to abort a write from a signal handler, you
828  *          should implement your own blocking write using sp_nonblocking_write()
829  *          together with a blocking method that makes sense for your program.
830  *          E.g. you can obtain the file descriptor for an open port using
831  *          sp_get_port_handle() and use this to call select() or pselect(),
832  *          with appropriate arrangements to return if a signal is received.
833  *
834  * @param port Pointer to port structure.
835  * @param buf Buffer containing the bytes to write.
836  * @param count Requested number of bytes to write.
837  * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
838  *
839  * @return The number of bytes written on success, or a negative error code.
840  *         If the number of bytes returned is less than that requested, the
841  *         timeout was reached before the requested number of bytes was
842  *         written. If timeout is zero, the function will always return
843  *         either the requested number of bytes or a negative error code. In
844  *         the event of an error there is no way to determine how many bytes
845  *         were sent before the error occured.
846  */
847 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout);
848
849 /**
850  * Write bytes to the specified serial port, without blocking.
851  *
852  * Note that this function only ensures that the accepted bytes have been
853  * written to the OS; they may be held in driver or hardware buffers and not
854  * yet physically transmitted. To check whether all written bytes have actually
855  * been transmitted, use the sp_output_waiting() function. To wait until all
856  * written bytes have actually been transmitted, use the sp_drain() function.
857  *
858  * @param port Pointer to port structure.
859  * @param buf Buffer containing the bytes to write.
860  * @param count Maximum number of bytes to write.
861  *
862  * @return The number of bytes written on success, or a negative error code.
863  *         The number of bytes returned may be any number from zero to the
864  *         maximum that was requested.
865  */
866 enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
867
868 /**
869  * Gets the number of bytes waiting in the input buffer.
870  *
871  * @param port Pointer to port structure.
872  *
873  * @return Number of bytes waiting on success, a negative error code otherwise.
874  */
875 enum sp_return sp_input_waiting(struct sp_port *port);
876
877 /**
878  * Gets the number of bytes waiting in the output buffer.
879  *
880  * @param port Pointer to port structure.
881  *
882  * @return Number of bytes waiting on success, a negative error code otherwise.
883  */
884 enum sp_return sp_output_waiting(struct sp_port *port);
885
886 /**
887  * Flush serial port buffers. Data in the selected buffer(s) is discarded.
888  *
889  * @param port Pointer to port structure.
890  * @param buffers Which buffer(s) to flush.
891  *
892  * @return SP_OK upon success, a negative error code otherwise.
893  */
894 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
895
896 /**
897  * Wait for buffered data to be transmitted.
898  *
899  * @warning If your program runs on Unix, defines its own signal handlers, and
900  *          needs to abort draining the output buffer when when these are
901  *          called, then you should not use this function. It repeats system
902  *          calls that return with EINTR. To be able to abort a drain from a
903  *          signal handler, you would need to implement your own blocking
904  *          drain by polling the result of sp_output_waiting().
905  *
906  * @param port Pointer to port structure.
907  *
908  * @return SP_OK upon success, a negative error code otherwise.
909  */
910 enum sp_return sp_drain(struct sp_port *port);
911
912 /**
913  * @}
914  * @defgroup Waiting Waiting for events
915  * @{
916  */
917
918 /**
919  * Allocate storage for a set of events.
920  *
921  * The user should allocate a variable of type struct sp_event_set *,
922  * then pass a pointer to this variable to receive the result.
923  *
924  * The result should be freed after use by calling sp_free_event_set().
925  *
926  * @return SP_OK upon success, a negative error code otherwise.
927  */
928 enum sp_return sp_new_event_set(struct sp_event_set **result_ptr);
929
930 /**
931  * Add events to a struct sp_event_set for a given port.
932  *
933  * The port must first be opened by calling sp_open() using the same port
934  * structure.
935  *
936  * After the port is closed or the port structure freed, the results may
937  * no longer be valid.
938  *
939  * @param event_set Event set to update.
940  * @param port Pointer to port structure.
941  * @param mask Bitmask of events to be waited for.
942  *
943  * @return SP_OK upon success, a negative error code otherwise.
944  */
945 enum sp_return sp_add_port_events(struct sp_event_set *event_set,
946         const struct sp_port *port, enum sp_event mask);
947
948 /**
949  * Wait for any of a set of events to occur.
950  *
951  * @param event_set Event set to wait on.
952  * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
953  *
954  * @return SP_OK upon success, a negative error code otherwise.
955  */
956 enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout);
957
958 /**
959  * Free a structure allocated by sp_new_event_set().
960  */
961 void sp_free_event_set(struct sp_event_set *event_set);
962
963 /**
964  * @}
965  * @defgroup Signals Port signalling operations
966  * @{
967  */
968
969 /**
970  * Gets the status of the control signals for the specified port.
971  *
972  * The user should allocate a variable of type "enum sp_signal" and pass a
973  * pointer to this variable to receive the result. The result is a bitmask
974  * in which individual signals can be checked by bitwise OR with values of
975  * the sp_signal enum.
976  *
977  * @param port Pointer to port structure.
978  * @param signals Pointer to variable to receive result.
979  *
980  * @return SP_OK upon success, a negative error code otherwise.
981  */
982 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals);
983
984 /**
985  * Put the port transmit line into the break state.
986  *
987  * @param port Pointer to port structure.
988  *
989  * @return SP_OK upon success, a negative error code otherwise.
990  */
991 enum sp_return sp_start_break(struct sp_port *port);
992
993 /**
994  * Take the port transmit line out of the break state.
995  *
996  * @param port Pointer to port structure.
997  *
998  * @return SP_OK upon success, a negative error code otherwise.
999  */
1000 enum sp_return sp_end_break(struct sp_port *port);
1001
1002 /**
1003  * @}
1004  * @defgroup Errors Obtaining error information
1005  * @{
1006 */
1007
1008 /**
1009  * Get the error code for a failed operation.
1010  *
1011  * In order to obtain the correct result, this function should be called
1012  * straight after the failure, before executing any other system operations.
1013  *
1014  * @return The system's numeric code for the error that caused the last
1015  *         operation to fail.
1016  */
1017 int sp_last_error_code(void);
1018
1019 /**
1020  * Get the error message for a failed operation.
1021  *
1022  * In order to obtain the correct result, this function should be called
1023  * straight after the failure, before executing other system operations.
1024  *
1025  * @return The system's message for the error that caused the last
1026  *         operation to fail. This string may be allocated by the function,
1027  *         and should be freed after use by calling sp_free_error_message().
1028  */
1029 char *sp_last_error_message(void);
1030
1031 /**
1032  * Free an error message returned by sp_last_error_message().
1033  */
1034 void sp_free_error_message(char *message);
1035
1036 /**
1037  * Set the handler function for library debugging messages.
1038  *
1039  * Debugging messages are generated by the library during each operation,
1040  * to help in diagnosing problems. The handler will be called for each
1041  * message. The handler can be set to NULL to ignore all debug messages.
1042  *
1043  * The handler function should accept a format string and variable length
1044  * argument list, in the same manner as e.g. printf().
1045  *
1046  * The default handler is sp_default_debug_handler().
1047  */
1048 void sp_set_debug_handler(void (*handler)(const char *format, ...));
1049
1050 /**
1051  * Default handler function for library debugging messages.
1052  *
1053  * This function prints debug messages to the standard error stream if the
1054  * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
1055  * ignored.
1056  */
1057 void sp_default_debug_handler(const char *format, ...);
1058
1059 /** @} */
1060
1061 /**
1062  * @defgroup Versions Version number querying functions, definitions, and macros
1063  *
1064  * This set of API calls returns two different version numbers related
1065  * to libserialport. The "package version" is the release version number of the
1066  * libserialport tarball in the usual "major.minor.micro" format, e.g. "0.1.0".
1067  *
1068  * The "library version" is independent of that; it is the libtool version
1069  * number in the "current:revision:age" format, e.g. "2:0:0".
1070  * See http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning for details.
1071  *
1072  * Both version numbers (and/or individual components of them) can be
1073  * retrieved via the API calls at runtime, and/or they can be checked at
1074  * compile/preprocessor time using the respective macros.
1075  *
1076  * @{
1077  */
1078
1079 /*
1080  * Package version macros (can be used for conditional compilation).
1081  */
1082
1083 /** The libserialport package 'major' version number. */
1084 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
1085
1086 /** The libserialport package 'minor' version number. */
1087 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
1088
1089 /** The libserialport package 'micro' version number. */
1090 #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
1091
1092 /** The libserialport package version ("major.minor.micro") as string. */
1093 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
1094
1095 /*
1096  * Library/libtool version macros (can be used for conditional compilation).
1097  */
1098
1099 /** The libserialport libtool 'current' version number. */
1100 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
1101
1102 /** The libserialport libtool 'revision' version number. */
1103 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
1104
1105 /** The libserialport libtool 'age' version number. */
1106 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
1107
1108 /** The libserialport libtool version ("current:revision:age") as string. */
1109 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
1110
1111 /**
1112  * Get the major libserialport package version number.
1113  *
1114  * @return The major package version number.
1115  *
1116  * @since 0.1.0
1117  */
1118 int sp_get_major_package_version(void);
1119
1120 /**
1121  * Get the minor libserialport package version number.
1122  *
1123  * @return The minor package version number.
1124  *
1125  * @since 0.1.0
1126  */
1127 int sp_get_minor_package_version(void);
1128
1129 /**
1130  * Get the micro libserialport package version number.
1131  *
1132  * @return The micro package version number.
1133  *
1134  * @since 0.1.0
1135  */
1136 int sp_get_micro_package_version(void);
1137
1138 /**
1139  * Get the libserialport package version number as a string.
1140  *
1141  * @return The package version number string. The returned string is
1142  *         static and thus should NOT be free'd by the caller.
1143  *
1144  * @since 0.1.0
1145  */
1146 const char *sp_get_package_version_string(void);
1147
1148 /**
1149  * Get the "current" part of the libserialport library version number.
1150  *
1151  * @return The "current" library version number.
1152  *
1153  * @since 0.1.0
1154  */
1155 int sp_get_current_lib_version(void);
1156
1157 /**
1158  * Get the "revision" part of the libserialport library version number.
1159  *
1160  * @return The "revision" library version number.
1161  *
1162  * @since 0.1.0
1163  */
1164 int sp_get_revision_lib_version(void);
1165
1166 /**
1167  * Get the "age" part of the libserialport library version number.
1168  *
1169  * @return The "age" library version number.
1170  *
1171  * @since 0.1.0
1172  */
1173 int sp_get_age_lib_version(void);
1174
1175 /**
1176  * Get the libserialport library version number as a string.
1177  *
1178  * @return The library version number string. The returned string is
1179  *         static and thus should NOT be free'd by the caller.
1180  *
1181  * @since 0.1.0
1182  */
1183 const char *sp_get_lib_version_string(void);
1184
1185 /** @} */
1186
1187 #ifdef __cplusplus
1188 }
1189 #endif
1190
1191 #endif