]> sigrok.org Git - libserialport.git/blob - libserialport.h.in
Define SP_PARITY_* more logically.
[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  * 
22  * @mainpage libserialport API
23  *
24  * Introduction
25  * ============
26  *
27  * libserialport is a minimal library written in C that is intended to take
28  * care of the OS-specific details when writing software that uses serial ports.
29  *
30  * By writing your serial code to use libserialport, you enable it to work
31  * transparently on any platform supported by the library.
32  *
33  * The operations that are supported are:
34  *
35  * - @ref Enumeration (obtaining a list of serial ports on the system)
36  * - @ref Ports
37  * - @ref Configuration (baud rate, parity, 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  * All functions can return only three possible error values. @ref SP_ERR_ARG
54  * indicates the function was called with invalid arguments. @ref SP_ERR_FAIL
55  * indicates that the OS reported a failure. @ref SP_ERR_MEM indicates that a
56  * memory allocation failed. All of these error values are negative.
57  *
58  * When @ref SP_ERR_FAIL is returned, an error code or string description of
59  * the error can be obtained by calling sp_last_error_code() or
60  * sp_last_error_message(). The error code or message is that provided by the
61  * OS; libserialport does not define any error codes or messages of its own.
62  *
63  * Function calls that succeed return @ref SP_OK, which is equal to zero,
64  * or where otherwise documented a positive value.
65  */
66
67 #ifndef LIBSERIALPORT_H
68 #define LIBSERIALPORT_H
69
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73
74 #include <stddef.h>
75 #ifdef _WIN32
76 #include <windows.h>
77 #endif
78
79 /* Package version macros (e.g. for conditional compilation). */
80 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
81 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
82 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
83
84 /* Library/libtool version macros (e.g. for conditional compilation). */
85 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
86 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
87 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
88 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
89
90 /** Return values. */
91 enum sp_return {
92         /** Operation completed successfully. */
93         SP_OK = 0,
94         /** Invalid arguments were passed to the function. */
95         SP_ERR_ARG = -1,
96         /** A system error occured while executing the operation. */
97         SP_ERR_FAIL = -2,
98         /** A memory allocation failed while executing the operation. */
99         SP_ERR_MEM = -3,
100 };
101
102 /** Port access modes. */
103 enum sp_mode {
104         /** Open port for read/write access. */
105         SP_MODE_RDWR = 1,
106         /** Open port for read access only. */
107         SP_MODE_RDONLY = 2,
108         /** Open port in non-blocking mode. */
109         SP_MODE_NONBLOCK = 4,
110 };
111
112 /** Parity settings. */
113 enum sp_parity {
114         /** Special value to indicate setting should be left alone. */
115         SP_PARITY_INVALID = -1,
116         /** No parity. */
117         SP_PARITY_NONE = 0,
118         /** Odd parity. */
119         SP_PARITY_ODD = 1,
120         /** Even parity. */
121         SP_PARITY_EVEN = 2,
122 };
123
124 /** RTS pin behaviour. */
125 enum sp_rts {
126         /** Special value to indicate setting should be left alone. */
127         SP_RTS_INVALID = -1,
128         /** RTS off. */
129         SP_RTS_OFF = 0,
130         /** RTS on. */
131         SP_RTS_ON = 1,
132         /** RTS used for flow control. */
133         SP_RTS_FLOW_CONTROL = 2,
134 };
135
136 /** CTS pin behaviour. */
137 enum sp_cts {
138         /** Special value to indicate setting should be left alone. */
139         SP_CTS_INVALID = -1,
140         /** CTS ignored. */
141         SP_CTS_IGNORE = 0,
142         /** CTS used for flow control. */
143         SP_CTS_FLOW_CONTROL = 1,
144 };
145
146 /** DTR pin behaviour. */
147 enum sp_dtr {
148         /** Special value to indicate setting should be left alone. */
149         SP_DTR_INVALID = -1,
150         /** DTR off. */
151         SP_DTR_OFF = 0,
152         /** DTR on. */
153         SP_DTR_ON = 1,
154         /** DTR used for flow control. */
155         SP_DTR_FLOW_CONTROL = 2,
156 };
157
158 /** DSR pin behaviour. */
159 enum sp_dsr {
160         /** Special value to indicate setting should be left alone. */
161         SP_DSR_INVALID = -1,
162         /** DSR ignored. */
163         SP_DSR_IGNORE = 0,
164         /** DSR used for flow control. */
165         SP_DSR_FLOW_CONTROL = 1,
166 };
167
168 /** XON/XOFF flow control behaviour. */
169 enum sp_xonxoff {
170         /** Special value to indicate setting should be left alone. */
171         SP_XONXOFF_INVALID = -1,
172         /** XON/XOFF disabled. */
173         SP_XONXOFF_DISABLED = 0,
174         /** XON/XOFF enabled for input only. */
175         SP_XONXOFF_IN = 1,
176         /** XON/XOFF enabled for output only. */
177         SP_XONXOFF_OUT = 2,
178         /** XON/XOFF enabled for input and output. */
179         SP_XONXOFF_INOUT = 3,
180 };
181
182 /** Standard flow control combinations. */
183 enum sp_flowcontrol {
184         /** No flow control. */
185         SP_FLOWCONTROL_NONE = 0,
186         /** Software flow control using XON/XOFF characters. */
187         SP_FLOWCONTROL_XONXOFF = 1,
188         /** Hardware flow control using RTS/CTS signals. */
189         SP_FLOWCONTROL_RTSCTS = 2,
190         /** Hardware flow control using DTR/DSR signals. */
191         SP_FLOWCONTROL_DTRDSR = 3,
192 };
193
194 /** A serial port. */
195 struct sp_port {
196         /** Name used to open the port. */
197         char *name;
198 /** @cond 0 */
199         /** OS-specific port handle. */
200 #ifdef _WIN32
201         HANDLE hdl;
202 #else
203         int fd;
204 #endif
205 /** @endcond */
206 };
207
208 /** Configuration for a serial port. */
209 struct sp_port_config {
210         /** Baud rate in bits per second. */
211         int baudrate;
212         /** Number of data bits to use. Valid values are 5 to 8. */
213         int bits;
214         /** Parity setting to use. */
215         enum sp_parity parity;
216         /** Number of stop bits to use (1 or 2). */
217         int stopbits;
218         /** RTS pin mode. */
219         enum sp_rts rts;
220         /** CTS pin mode. */
221         enum sp_cts cts;
222         /** DTR pin mode. */
223         enum sp_dtr dtr;
224         /** DSR pin mode. */
225         enum sp_dsr dsr;
226         /** XON/XOFF flow control mode. */
227         enum sp_xonxoff xon_xoff;
228 };
229
230 /**
231 @defgroup Enumeration Port enumeration
232 @{
233 */
234
235 /**
236  * Obtain a pointer to a new sp_port structure representing the named port.
237  *
238  * The user should allocate a variable of type "struct sp_port *" and pass a
239  * pointer to this to receive the result.
240  *
241  * The result should be freed after use by calling sp_free_port().
242  *
243  * @return SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
244  *         failure, or SP_ERR_ARG if an invalid pointer is passed. If any error
245  *         is returned, the variable pointed to by port_ptr will be set to NULL.
246  *         Otherwise, it will be set to point to the newly allocated port.
247  */
248 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
249
250 /**
251  * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
252  */
253 void sp_free_port(struct sp_port *port);
254
255 /**
256  * List the serial ports available on the system.
257  *
258  * The result obtained is an array of pointers to sp_port structures,
259  * terminated by a NULL. The user should allocate a variable of type
260  * "struct sp_port **" and pass a pointer to this to receive the result.
261  *
262  * The result should be freed after use by calling sp_free_port_list().
263  * If a port from the list is to be used after freeing the list, it must be
264  * copied first using sp_copy_port().
265  *
266  * @return SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
267  *         failure, or SP_ERR_ARG if an invalid pointer is passed. If any error
268  *         is returned, the variable pointed to by list_ptr will be set to NULL.
269  *         Otherwise, it will be set to point to the newly allocated array.
270  */
271 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
272
273 /**
274  * Make a new copy of a sp_port structure.
275  *
276  * The user should allocate a variable of type "struct sp_port *" and pass a
277  * pointer to this to receive the result.
278  *
279  * The copy should be freed after use by calling sp_free_port().
280  *
281  * @return SP_OK on success, SP_ERR_MEM on allocation failure, or SP_ERR_ARG
282  *         if an invalid port or pointer is passed. If any error is returned,
283  *         the variable pointed to by copy_ptr will be set to NULL. Otherwise,
284  *         it will be set to point to the newly allocated copy.
285  */
286 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
287
288 /**
289  * Free a port list obtained from sp_list_ports().
290  *
291  * This will also free all the sp_port structures referred to from the list;
292  * any that are to be retained must be copied first using sp_copy_port().
293  */
294 void sp_free_port_list(struct sp_port **ports);
295
296 /**
297  * @}
298  * @defgroup Ports Opening and closing ports
299  * @{
300  */
301
302 /**
303  * Open the specified serial port.
304  *
305  * @param port Pointer to port structure.
306  * @param flags Flags to use when opening the serial port.
307  *
308  * @return SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
309  *         failure, or SP_ERR_ARG if an invalid port is passed.
310  */
311 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
312
313 /**
314  * Close the specified serial port.
315  *
316  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
317  *         if an invalid port is passed.
318  */
319 enum sp_return sp_close(struct sp_port *port);
320
321 /**
322  * @}
323  * @defgroup Configuration Setting port parameters
324  * @{
325  */
326
327 /**
328  * Get the current configuration of the specified serial port.
329  *
330  * The user should allocate a struct sp_port_config, then pass a pointer to it
331  * as the config parameter. The struct will be populated with the port
332  * configuration.
333  *
334  * Any setting that is in a state not recognised or supported by
335  * libserialport will have its value set to -1 in the struct.
336  *
337  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
338  *         for invalid arguments.
339  */
340 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
341
342 /**
343  * Set the configuration for the specified serial port.
344  *
345  * The user should populate a struct sp_port_config, then pass a pointer to it
346  * as the config parameter.
347  *
348  * To retain the current value of any setting, set that field to -1.
349  *
350  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
351  *         for invalid arguments.
352  */
353 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
354
355 /**
356  * Set the baud rate for the specified serial port.
357  *
358  * @param port Pointer to port structure.
359  * @param baudrate Baud rate in bits per second.
360  *
361  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
362  *         for invalid arguments.
363  */
364 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
365
366 /**
367  * Set the number of data bits for the specified serial port.
368  *
369  * @param port Pointer to port structure.
370  * @param bits Number of data bits to use. Valid values are 5 to 8.
371  *
372  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
373  *         for invalid arguments.
374  */
375 enum sp_return sp_set_bits(struct sp_port *port, int bits);
376
377 /**
378  * Set the parity for the specified serial port.
379  *
380  * @param port Pointer to port structure.
381  * @param parity Parity setting to use.
382  *
383  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
384  *         for invalid arguments.
385  */
386 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
387
388 /**
389  * Set the number of stop bits for the specified port.
390  *
391  * @param port Pointer to port structure.
392  * @param stopbits Number of stop bits to use (1 or 2).
393  *
394  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
395  *         for invalid arguments.
396  */
397 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
398
399 /**
400  * Set the flow control type for the specified serial port.
401  *
402  * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
403  * XON/XOFF settings as necessary for the specified flow control
404  * type. For more fine-grained control of these settings, use their
405  * individual configuration functions or the sp_set_config() function.
406  *
407  * @param port Pointer to port structure.
408  * @param flowcontrol Flow control setting to use.
409  *
410  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
411  *         for invalid arguments.
412  */
413 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
414
415 /**
416  * Set the RTS pin behaviour for the specified port.
417  *
418  * @param port Pointer to port structure.
419  * @param rts RTS pin mode.
420  *
421  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
422  *         for invalid arguments.
423  */
424 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
425
426 /**
427  * Set the CTS pin behaviour for the specified port.
428  *
429  * @param port Pointer to port structure.
430  * @param cts CTS pin mode.
431  *
432  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
433  *         for invalid arguments.
434  */
435 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
436
437 /**
438  * Set the DTR pin behaviour for the specified port.
439  *
440  * @param port Pointer to port structure.
441  * @param dtr DTR pin mode.
442  *
443  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
444  *         for invalid arguments.
445  */
446 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
447
448 /**
449  * Set the RTS pin behaviour for the specified port.
450  *
451  * @param port Pointer to port structure.
452  * @param dsr DSR pin mode.
453  *
454  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
455  *         for invalid arguments.
456  */
457 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
458
459 /**
460  * Configure XON/XOFF flow control for the specified port.
461  *
462  * @param port Pointer to port structure.
463  * @param xon_xoff XON/XOFF flow control mode.
464  *
465  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
466  *         for invalid arguments.
467  */
468 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
469
470 /**
471  * @}
472  * @defgroup Data Reading, writing, and flushing data
473  * @{
474 */
475
476 /**
477  * Read bytes from the specified serial port.
478  *
479  * Note that this function may return after reading less than the specified
480  * number of bytes; it is the user's responsibility to iterate as necessary
481  * in this case.
482  *
483  * @param port Pointer to port structure.
484  * @param buf Buffer in which to store the bytes read.
485  * @param count Maximum number of bytes to read.
486  *
487  * @return The number of bytes read, SP_ERR_FAIL on failure,
488  *         or SP_ERR_ARG for invalid arguments.
489  */
490 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count);
491
492 /**
493  * Write bytes to the specified serial port.
494  *
495  * Note that this function may return after writing less than the specified
496  * number of bytes; it is the user's responsibility to iterate as necessary
497  * in this case.
498  *
499  * @param port Pointer to port structure.
500  * @param buf Buffer containing the bytes to write.
501  * @param count Maximum number of bytes to write.
502  *
503  * @return The number of bytes written, SP_ERR_FAIL on failure,
504  *         or SP_ERR_ARG for invalid arguments.
505  */
506 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count);
507
508 /**
509  * Flush serial port buffers.
510  *
511  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
512  *         if an invalid port is passed.
513  */
514 enum sp_return sp_flush(struct sp_port *port);
515
516 /**
517  * @}
518  * @defgroup Errors Obtaining error information
519  * @{
520 */
521
522 /**
523  * Get the error code for a failed operation.
524  *
525  * In order to obtain the correct result, this function should be called
526  * straight after the failure, before executing any other system operations.
527  *
528  * @return The system's numeric code for the error that caused the last
529  *         operation to fail.
530  */
531 int sp_last_error_code(void);
532
533 /**
534  * Get the error message for a failed operation.
535  *
536  * In order to obtain the correct result, this function should be called
537  * straight after the failure, before executing other system operations.
538  *
539  * @return The system's message for the error that caused the last
540  *         operation to fail. This string may be allocated by the function,
541  *         and should be freed after use by calling sp_free_error_message().
542  */
543 char *sp_last_error_message(void);
544
545 /**
546  * Free an error message returned by sp_last_error_message().
547  */
548 void sp_free_error_message(char *message);
549
550 /** @} */
551
552 #ifdef __cplusplus
553 }
554 #endif
555
556 #endif /* LIBSERIALPORT_H */