]> sigrok.org Git - libserialport.git/blob - libserialport.h.in
Make configuration structure opaque.
[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 Errors
40  *
41  * libserialport is an open source project released under the LGPL3+ license.
42  *
43  * API principles
44  * ==============
45  *
46  * The API is simple, and designed to be a minimal wrapper around the serial
47  * port support in each OS.
48  *
49  * Most functions take a pointer to a struct sp_port, which represents a serial
50  * port. These structures are always allocated and freed by the library, using
51  * the functions in the @ref Enumeration "Enumeration" section.
52  *
53  * Most functions have return type @ref sp_return and can return only four
54  * possible error values:
55  *
56  * - @ref SP_ERR_ARG means that a function was called with invalid
57  *   arguments. This implies a bug in the caller. The arguments passed would
58  *   be invalid regardless of the underlying OS or serial device involved.
59  *
60  * - @ref SP_ERR_FAIL means that the OS reported a failure. The error code or
61  *   message provided by the OS can be obtained by calling sp_last_error_code()
62  *   or sp_last_error_message().
63  *
64  * - @ref SP_ERR_SUPP indicates that there is no support for the requested
65  *   operation in the current OS, driver or device. No error message is
66  *   available from the OS in this case. There is either no way to request
67  *   the operation in the first place, or libserialport does not know how to
68  *   do so in the current version.
69  *
70  * - @ref SP_ERR_MEM indicates that a memory allocation failed.
71  *
72  * All of these error values are negative.
73  *
74  * Calls that succeed return @ref SP_OK, which is equal to zero. Some functions
75  * declared @ref sp_return can also return a positive value for a successful
76  * numeric result, e.g. sp_read() and sp_write().
77  */
78
79 #ifndef LIBSERIALPORT_LIBSERIALPORT_H
80 #define LIBSERIALPORT_LIBSERIALPORT_H
81
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85
86 #include <stddef.h>
87 #ifdef _WIN32
88 #include <windows.h>
89 #endif
90
91 /* Package version macros (e.g. for conditional compilation). */
92 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
93 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
94 #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
95 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
96
97 /* Library/libtool version macros (e.g. for conditional compilation). */
98 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
99 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
100 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
101 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
102
103 /** Return values. */
104 enum sp_return {
105         /** Operation completed successfully. */
106         SP_OK = 0,
107         /** Invalid arguments were passed to the function. */
108         SP_ERR_ARG = -1,
109         /** A system error occured while executing the operation. */
110         SP_ERR_FAIL = -2,
111         /** A memory allocation failed while executing the operation. */
112         SP_ERR_MEM = -3,
113         /** The requested operation is not supported by this system or device. */
114         SP_ERR_SUPP = -4,
115 };
116
117 /** Port access modes. */
118 enum sp_mode {
119         /** Open port for read access. */
120         SP_MODE_READ = 1,
121         /** Open port for write access. */
122         SP_MODE_WRITE = 2,
123         /** Open port in non-blocking mode. */
124         SP_MODE_NONBLOCK = 4,
125 };
126
127 /** Buffer selection. */
128 enum sp_buffer {
129         /** Input buffer. */
130         SP_BUF_INPUT = 1,
131         /** Output buffer. */
132         SP_BUF_OUTPUT = 2,
133         /** Both buffers. */
134         SP_BUF_BOTH = 3,
135 };
136
137 /** Parity settings. */
138 enum sp_parity {
139         /** Special value to indicate setting should be left alone. */
140         SP_PARITY_INVALID = -1,
141         /** No parity. */
142         SP_PARITY_NONE = 0,
143         /** Odd parity. */
144         SP_PARITY_ODD = 1,
145         /** Even parity. */
146         SP_PARITY_EVEN = 2,
147 };
148
149 /** RTS pin behaviour. */
150 enum sp_rts {
151         /** Special value to indicate setting should be left alone. */
152         SP_RTS_INVALID = -1,
153         /** RTS off. */
154         SP_RTS_OFF = 0,
155         /** RTS on. */
156         SP_RTS_ON = 1,
157         /** RTS used for flow control. */
158         SP_RTS_FLOW_CONTROL = 2,
159 };
160
161 /** CTS pin behaviour. */
162 enum sp_cts {
163         /** Special value to indicate setting should be left alone. */
164         SP_CTS_INVALID = -1,
165         /** CTS ignored. */
166         SP_CTS_IGNORE = 0,
167         /** CTS used for flow control. */
168         SP_CTS_FLOW_CONTROL = 1,
169 };
170
171 /** DTR pin behaviour. */
172 enum sp_dtr {
173         /** Special value to indicate setting should be left alone. */
174         SP_DTR_INVALID = -1,
175         /** DTR off. */
176         SP_DTR_OFF = 0,
177         /** DTR on. */
178         SP_DTR_ON = 1,
179         /** DTR used for flow control. */
180         SP_DTR_FLOW_CONTROL = 2,
181 };
182
183 /** DSR pin behaviour. */
184 enum sp_dsr {
185         /** Special value to indicate setting should be left alone. */
186         SP_DSR_INVALID = -1,
187         /** DSR ignored. */
188         SP_DSR_IGNORE = 0,
189         /** DSR used for flow control. */
190         SP_DSR_FLOW_CONTROL = 1,
191 };
192
193 /** XON/XOFF flow control behaviour. */
194 enum sp_xonxoff {
195         /** Special value to indicate setting should be left alone. */
196         SP_XONXOFF_INVALID = -1,
197         /** XON/XOFF disabled. */
198         SP_XONXOFF_DISABLED = 0,
199         /** XON/XOFF enabled for input only. */
200         SP_XONXOFF_IN = 1,
201         /** XON/XOFF enabled for output only. */
202         SP_XONXOFF_OUT = 2,
203         /** XON/XOFF enabled for input and output. */
204         SP_XONXOFF_INOUT = 3,
205 };
206
207 /** Standard flow control combinations. */
208 enum sp_flowcontrol {
209         /** No flow control. */
210         SP_FLOWCONTROL_NONE = 0,
211         /** Software flow control using XON/XOFF characters. */
212         SP_FLOWCONTROL_XONXOFF = 1,
213         /** Hardware flow control using RTS/CTS signals. */
214         SP_FLOWCONTROL_RTSCTS = 2,
215         /** Hardware flow control using DTR/DSR signals. */
216         SP_FLOWCONTROL_DTRDSR = 3,
217 };
218
219 /** Input signals. */
220 enum sp_signal {
221         /** Clear to send. */
222         SP_SIG_CTS = 1,
223         /** Data set ready. */
224         SP_SIG_DSR = 2,
225         /** Data carrier detect. */
226         SP_SIG_DCD = 4,
227         /** Ring indicator. */
228         SP_SIG_RI = 8,
229 };
230
231 /** A serial port. */
232 struct sp_port;
233
234 /** Configuration for a serial port. */
235 struct sp_port_config;
236
237 /**
238 @defgroup Enumeration Port enumeration
239 @{
240 */
241
242 /**
243  * Obtain a pointer to a new sp_port structure representing the named port.
244  *
245  * The user should allocate a variable of type "struct sp_port *" and pass a
246  * pointer to this to receive the result.
247  *
248  * The result should be freed after use by calling sp_free_port().
249  *
250  * If any error is returned, the variable pointed to by port_ptr will be set
251  * to NULL. Otherwise, it will be set to point to the newly allocated port.
252  *
253  * @return SP_OK upon success, a negative error code otherwise.
254  */
255 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
256
257 /**
258  * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
259  */
260 void sp_free_port(struct sp_port *port);
261
262 /**
263  * List the serial ports available on the system.
264  *
265  * The result obtained is an array of pointers to sp_port structures,
266  * terminated by a NULL. The user should allocate a variable of type
267  * "struct sp_port **" and pass a pointer to this to receive the result.
268  *
269  * The result should be freed after use by calling sp_free_port_list().
270  * If a port from the list is to be used after freeing the list, it must be
271  * copied first using sp_copy_port().
272  *
273  * If any error is returned, the variable pointed to by list_ptr will be set
274  * to NULL. Otherwise, it will be set to point to the newly allocated array.
275  *
276  * @return SP_OK upon success, a negative error code otherwise.
277  */
278 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
279
280 /**
281  * Make a new copy of a sp_port structure.
282  *
283  * The user should allocate a variable of type "struct sp_port *" and pass a
284  * pointer to this to receive the result.
285  *
286  * The copy should be freed after use by calling sp_free_port().
287  *
288  * If any error is returned, the variable pointed to by copy_ptr will be set
289  * to NULL. Otherwise, it will be set to point to the newly allocated copy.
290  *
291  * @return SP_OK upon success, a negative error code otherwise.
292  */
293 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
294
295 /**
296  * Free a port list obtained from sp_list_ports().
297  *
298  * This will also free all the sp_port structures referred to from the list;
299  * any that are to be retained must be copied first using sp_copy_port().
300  */
301 void sp_free_port_list(struct sp_port **ports);
302
303 /**
304  * @}
305  * @defgroup Ports Opening, closing and querying ports
306  * @{
307  */
308
309 /**
310  * Open the specified serial port.
311  *
312  * @param port Pointer to port structure.
313  * @param flags Flags to use when opening the serial port.
314  *
315  * @return SP_OK upon success, a negative error code otherwise.
316  */
317 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
318
319 /**
320  * Close the specified serial port.
321  *
322  * @return SP_OK upon success, a negative error code otherwise.
323  */
324 enum sp_return sp_close(struct sp_port *port);
325
326 /**
327  * Get the name of a port.
328  *
329  * The name returned is whatever is normally used to refer to a port on the
330  * current operating system; e.g. for Windows it will usually be a "COMn"
331  * device name, and for Unix it will be a device path beginning with "/dev/".
332  *
333  * @param port Pointer to port structure.
334  *
335  * @return The port name, or NULL if an invalid port is passed. The name
336  * string is part of the port structure and may not be used after the
337  * port structure has been freed.
338  */
339 char *sp_get_port_name(const struct sp_port *port);
340
341 /**
342  * Get the operating system handle for a port.
343  *
344  * The type of the handle depends on the operating system. On Unix based
345  * systems, the handle is a file descriptor of type "int". On Windows, the
346  * handle is of type "HANDLE". The user should allocate a variable of the
347  * appropriate type and pass a pointer to this to receive the result.
348  *
349  * To obtain a valid handle, the port must first be opened by calling
350  * sp_open() using the same port structure.
351  *
352  * After the port is closed or the port structure freed, the handle may
353  * no longer be valid.
354  *
355  * @warning This feature is provided so that programs may make use of
356  *          OS-specific functionality where desired. Doing so obviously
357  *          comes at a cost in portability. It also cannot be guaranteed
358  *          that direct usage of the OS handle will not conflict with the
359  *          library's own usage of the port. Be careful.
360  *
361  * @return SP_OK upon success, a negative error code otherwise.
362  */
363 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
364
365 /**
366  * @}
367  * @defgroup Configuration Setting port parameters
368  * @{
369  */
370
371 /**
372  * Allocate a port configuration structure.
373  *
374  * The user should allocate a variable of type "struct sp_config *" and pass a
375  * pointer to this to receive the result. The variable will be updated to
376  * point to the new configuration structure. The structure is opaque and must
377  * be accessed via the functions provided.
378  *
379  * All parameters in the structure will be initialised to special values which
380  * are ignored by sp_set_config().
381  *
382  * The structure should be freed after use by calling sp_free_config().
383  *
384  * @param config_ptr Pointer to variable to receive result.
385  *
386  * @return SP_OK upon success, a negative error code otherwise.
387  */
388 enum sp_return sp_new_config(struct sp_port_config **config_ptr);
389
390 /**
391  * Free a port configuration structure.
392  *
393  * @param config Pointer to configuration structure.
394  */
395 void sp_free_config(struct sp_port_config *config);
396
397 /**
398  * Get the current configuration of the specified serial port.
399  *
400  * The user should allocate a configuration structure using sp_new_config()
401  * and pass this as the config parameter. The configuration structure will
402  * be updated with the port configuration.
403  *
404  * Any parameters that are configured with settings not recognised or
405  * supported by libserialport, will be set to special values that are
406  * ignored by sp_set_config().
407  *
408  * @return SP_OK upon success, a negative error code otherwise.
409  */
410 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
411
412 /**
413  * Set the configuration for the specified serial port.
414  *
415  * For each parameter in the configuration, there is a special value (usually
416  * -1, but see the documentation for each field). These values will be ignored
417  * and the corresponding setting left unchanged on the port.
418  *
419  * @return SP_OK upon success, a negative error code otherwise.
420  */
421 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
422
423 /**
424  * Set the baud rate for the specified serial port.
425  *
426  * @param port Pointer to port structure.
427  * @param baudrate Baud rate in bits per second.
428  *
429  * @return SP_OK upon success, a negative error code otherwise.
430  */
431 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
432
433 /**
434  * Get the baud rate from a port configuration.
435  *
436  * The user should allocate a variable of type int and pass a pointer to this
437  * to receive the result.
438  *
439  * @param config Pointer to configuration structure.
440  * @param baudrate_ptr Pointer to variable to store result.
441  *
442  * @return SP_OK upon success, a negative error code otherwise.
443  */
444 enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
445
446 /**
447  * Set the baud rate in a port configuration.
448  *
449  * @param config Pointer to configuration structure.
450  * @param baudrate Baud rate in bits per second, or -1 to retain current setting.
451  *
452  * @return SP_OK upon success, a negative error code otherwise.
453  */
454 enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
455
456 /**
457  * Set the data bits for the specified serial port.
458  *
459  * @param port Pointer to port structure.
460  * @param bits Number of data bits.
461  *
462  * @return SP_OK upon success, a negative error code otherwise.
463  */
464 enum sp_return sp_set_bits(struct sp_port *port, int bits);
465
466 /**
467  * Get the data bits from a port configuration.
468  *
469  * The user should allocate a variable of type int and pass a pointer to this
470  * to receive the result.
471  *
472  * @param config Pointer to configuration structure.
473  * @param bits_ptr Pointer to variable to store result.
474  *
475  * @return SP_OK upon success, a negative error code otherwise.
476  */
477 enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
478
479 /**
480  * Set the data bits in a port configuration.
481  *
482  * @param config Pointer to configuration structure.
483  * @param bits Number of data bits, or -1 to retain current setting.
484  *
485  * @return SP_OK upon success, a negative error code otherwise.
486  */
487 enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
488
489 /**
490  * Set the parity setting for the specified serial port.
491  *
492  * @param port Pointer to port structure.
493  * @param parity Parity setting.
494  *
495  * @return SP_OK upon success, a negative error code otherwise.
496  */
497 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
498
499 /**
500  * Get the parity setting from a port configuration.
501  *
502  * The user should allocate a variable of type enum sp_parity and pass a pointer to this
503  * to receive the result.
504  *
505  * @param config Pointer to configuration structure.
506  * @param parity_ptr Pointer to variable to store result.
507  *
508  * @return SP_OK upon success, a negative error code otherwise.
509  */
510 enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
511
512 /**
513  * Set the parity setting in a port configuration.
514  *
515  * @param config Pointer to configuration structure.
516  * @param parity Parity setting, or -1 to retain current setting.
517  *
518  * @return SP_OK upon success, a negative error code otherwise.
519  */
520 enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
521
522 /**
523  * Set the stop bits for the specified serial port.
524  *
525  * @param port Pointer to port structure.
526  * @param stopbits Number of stop bits.
527  *
528  * @return SP_OK upon success, a negative error code otherwise.
529  */
530 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
531
532 /**
533  * Get the stop bits from a port configuration.
534  *
535  * The user should allocate a variable of type int and pass a pointer to this
536  * to receive the result.
537  *
538  * @param config Pointer to configuration structure.
539  * @param stopbits_ptr Pointer to variable to store result.
540  *
541  * @return SP_OK upon success, a negative error code otherwise.
542  */
543 enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
544
545 /**
546  * Set the stop bits in a port configuration.
547  *
548  * @param config Pointer to configuration structure.
549  * @param stopbits Number of stop bits, or -1 to retain current setting.
550  *
551  * @return SP_OK upon success, a negative error code otherwise.
552  */
553 enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
554
555 /**
556  * Set the RTS pin behaviour for the specified serial port.
557  *
558  * @param port Pointer to port structure.
559  * @param rts RTS pin mode.
560  *
561  * @return SP_OK upon success, a negative error code otherwise.
562  */
563 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
564
565 /**
566  * Get the RTS pin behaviour from a port configuration.
567  *
568  * The user should allocate a variable of type enum sp_rts and pass a pointer to this
569  * to receive the result.
570  *
571  * @param config Pointer to configuration structure.
572  * @param rts_ptr Pointer to variable to store result.
573  *
574  * @return SP_OK upon success, a negative error code otherwise.
575  */
576 enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
577
578 /**
579  * Set the RTS pin behaviour in a port configuration.
580  *
581  * @param config Pointer to configuration structure.
582  * @param rts RTS pin mode, or -1 to retain current setting.
583  *
584  * @return SP_OK upon success, a negative error code otherwise.
585  */
586 enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
587
588 /**
589  * Set the CTS pin behaviour for the specified serial port.
590  *
591  * @param port Pointer to port structure.
592  * @param cts CTS pin mode.
593  *
594  * @return SP_OK upon success, a negative error code otherwise.
595  */
596 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
597
598 /**
599  * Get the CTS pin behaviour from a port configuration.
600  *
601  * The user should allocate a variable of type enum sp_cts and pass a pointer to this
602  * to receive the result.
603  *
604  * @param config Pointer to configuration structure.
605  * @param cts_ptr Pointer to variable to store result.
606  *
607  * @return SP_OK upon success, a negative error code otherwise.
608  */
609 enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
610
611 /**
612  * Set the CTS pin behaviour in a port configuration.
613  *
614  * @param config Pointer to configuration structure.
615  * @param cts CTS pin mode, or -1 to retain current setting.
616  *
617  * @return SP_OK upon success, a negative error code otherwise.
618  */
619 enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
620
621 /**
622  * Set the DTR pin behaviour for the specified serial port.
623  *
624  * @param port Pointer to port structure.
625  * @param dtr DTR pin mode.
626  *
627  * @return SP_OK upon success, a negative error code otherwise.
628  */
629 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
630
631 /**
632  * Get the DTR pin behaviour from a port configuration.
633  *
634  * The user should allocate a variable of type enum sp_dtr and pass a pointer to this
635  * to receive the result.
636  *
637  * @param config Pointer to configuration structure.
638  * @param dtr_ptr Pointer to variable to store result.
639  *
640  * @return SP_OK upon success, a negative error code otherwise.
641  */
642 enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
643
644 /**
645  * Set the DTR pin behaviour in a port configuration.
646  *
647  * @param config Pointer to configuration structure.
648  * @param dtr DTR pin mode, or -1 to retain current setting.
649  *
650  * @return SP_OK upon success, a negative error code otherwise.
651  */
652 enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
653
654 /**
655  * Set the DSR pin behaviour for the specified serial port.
656  *
657  * @param port Pointer to port structure.
658  * @param dsr DSR pin mode.
659  *
660  * @return SP_OK upon success, a negative error code otherwise.
661  */
662 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
663
664 /**
665  * Get the DSR pin behaviour from a port configuration.
666  *
667  * The user should allocate a variable of type enum sp_dsr and pass a pointer to this
668  * to receive the result.
669  *
670  * @param config Pointer to configuration structure.
671  * @param dsr_ptr Pointer to variable to store result.
672  *
673  * @return SP_OK upon success, a negative error code otherwise.
674  */
675 enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
676
677 /**
678  * Set the DSR pin behaviour in a port configuration.
679  *
680  * @param config Pointer to configuration structure.
681  * @param dsr DSR pin mode, or -1 to retain current setting.
682  *
683  * @return SP_OK upon success, a negative error code otherwise.
684  */
685 enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
686
687 /**
688  * Set the XON/XOFF configuration for the specified serial port.
689  *
690  * @param port Pointer to port structure.
691  * @param xon_xoff XON/XOFF mode.
692  *
693  * @return SP_OK upon success, a negative error code otherwise.
694  */
695 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
696
697 /**
698  * Get the XON/XOFF configuration from a port configuration.
699  *
700  * The user should allocate a variable of type enum sp_xonxoff and pass a pointer to this
701  * to receive the result.
702  *
703  * @param config Pointer to configuration structure.
704  * @param xon_xoff_ptr Pointer to variable to store result.
705  *
706  * @return SP_OK upon success, a negative error code otherwise.
707  */
708 enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
709
710 /**
711  * Set the XON/XOFF configuration in a port configuration.
712  *
713  * @param config Pointer to configuration structure.
714  * @param xon_xoff XON/XOFF mode, or -1 to retain current setting.
715  *
716  * @return SP_OK upon success, a negative error code otherwise.
717  */
718 enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
719
720 /**
721  * Set the flow control type in a port configuration.
722  *
723  * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
724  * XON/XOFF settings as necessary for the specified flow control
725  * type. For more fine-grained control of these settings, use their
726  * individual configuration functions.
727  *
728  * @param config Pointer to configuration structure.
729  * @param flowcontrol Flow control setting to use.
730  *
731  * @return SP_OK upon success, a negative error code otherwise.
732  */
733 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
734
735 /**
736  * Set the flow control type for the specified serial port.
737  *
738  * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
739  * XON/XOFF settings as necessary for the specified flow control
740  * type. For more fine-grained control of these settings, use their
741  * individual configuration functions.
742  *
743  * @param port Pointer to port structure.
744  * @param flowcontrol Flow control setting to use.
745  *
746  * @return SP_OK upon success, a negative error code otherwise.
747  */
748 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
749
750 /**
751  * @}
752  * @defgroup Data Reading, writing, and flushing data
753  * @{
754 */
755
756 /**
757  * Read bytes from the specified serial port.
758  *
759  * Note that this function may return after reading less than the specified
760  * number of bytes; it is the user's responsibility to iterate as necessary
761  * in this case.
762  *
763  * @param port Pointer to port structure.
764  * @param buf Buffer in which to store the bytes read.
765  * @param count Maximum number of bytes to read.
766  *
767  * @return The number of bytes read on success, or a negative error code.
768  */
769 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count);
770
771 /**
772  * Write bytes to the specified serial port.
773  *
774  * Note that this function may return after writing less than the specified
775  * number of bytes; it is the user's responsibility to iterate as necessary
776  * in this case.
777  *
778  * @param port Pointer to port structure.
779  * @param buf Buffer containing the bytes to write.
780  * @param count Maximum number of bytes to write.
781  *
782  * @return The number of bytes written on success, or a negative error code.
783  */
784 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count);
785
786 /**
787  * Flush serial port buffers. Data in the selected buffer(s) is discarded.
788  *
789  * @param port Pointer to port structure.
790  * @param buffers Which buffer(s) to flush.
791  *
792  * @return SP_OK upon success, a negative error code otherwise.
793  */
794 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
795
796 /**
797  * Wait for buffered data to be transmitted.
798  *
799  * @param port Pointer to port structure.
800  *
801  * @return SP_OK upon success, a negative error code otherwise.
802  */
803 enum sp_return sp_drain(struct sp_port *port);
804
805 /**
806  * @}
807  * @defgroup Signals Port signalling operations
808  * @{
809  */
810
811 /**
812  * Gets the status of the control signals for the specified port.
813  *
814  * The user should allocate a variable of type "enum sp_signal" and pass a
815  * pointer to this variable to receive the result. The result is a bitmask
816  * in which individual signals can be checked by bitwise OR with values of
817  * the sp_signal enum.
818  *
819  * @param port Pointer to port structure.
820  * @param signals Pointer to variable to receive result.
821  *
822  * @return SP_OK upon success, a negative error code otherwise.
823  */
824 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals);
825
826 /**
827  * Put the port transmit line into the break state.
828  *
829  * @param port Pointer to port structure.
830  *
831  * @return SP_OK upon success, a negative error code otherwise.
832  */
833 enum sp_return sp_start_break(struct sp_port *port);
834
835 /**
836  * Take the port transmit line out of the break state.
837  *
838  * @param port Pointer to port structure.
839  *
840  * @return SP_OK upon success, a negative error code otherwise.
841  */
842 enum sp_return sp_end_break(struct sp_port *port);
843
844 /**
845  * @}
846  * @defgroup Errors Obtaining error information
847  * @{
848 */
849
850 /**
851  * Get the error code for a failed operation.
852  *
853  * In order to obtain the correct result, this function should be called
854  * straight after the failure, before executing any other system operations.
855  *
856  * @return The system's numeric code for the error that caused the last
857  *         operation to fail.
858  */
859 int sp_last_error_code(void);
860
861 /**
862  * Get the error message for a failed operation.
863  *
864  * In order to obtain the correct result, this function should be called
865  * straight after the failure, before executing other system operations.
866  *
867  * @return The system's message for the error that caused the last
868  *         operation to fail. This string may be allocated by the function,
869  *         and should be freed after use by calling sp_free_error_message().
870  */
871 char *sp_last_error_message(void);
872
873 /**
874  * Free an error message returned by sp_last_error_message().
875  */
876 void sp_free_error_message(char *message);
877
878 /**
879  * Set the handler function for library debugging messages.
880  *
881  * Debugging messages are generated by the library during each operation,
882  * to help in diagnosing problems. The handler will be called for each
883  * message. The handler can be set to NULL to ignore all debug messages.
884  *
885  * The handler function should accept a format string and variable length
886  * argument list, in the same manner as e.g. printf().
887  *
888  * The default handler is sp_default_debug_handler().
889  */
890 void sp_set_debug_handler(void (*handler)(const char *format, ...));
891
892 /**
893  * Default handler function for library debugging messages.
894  *
895  * This function prints debug messages to the standard error stream if the
896  * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
897  * ignored.
898  */
899 void sp_default_debug_handler(const char *format, ...);
900
901 /** @} */
902
903 #ifdef __cplusplus
904 }
905 #endif
906
907 #endif