]> sigrok.org Git - libserialport.git/blob - libserialport.h.in
Add sp_start_break() and sp_end_break() 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  * 
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 access. */
105         SP_MODE_READ = 1,
106         /** Open port for write access. */
107         SP_MODE_WRITE = 2,
108         /** Open port in non-blocking mode. */
109         SP_MODE_NONBLOCK = 4,
110 };
111
112 /** Buffer selection. */
113 enum sp_buffer {
114         /** Input buffer. */
115         SP_BUF_INPUT = 1,
116         /** Output buffer. */
117         SP_BUF_OUTPUT = 2,
118         /** Both buffers. */
119         SP_BUF_BOTH = 3,
120 };
121
122 /** Parity settings. */
123 enum sp_parity {
124         /** Special value to indicate setting should be left alone. */
125         SP_PARITY_INVALID = -1,
126         /** No parity. */
127         SP_PARITY_NONE = 0,
128         /** Odd parity. */
129         SP_PARITY_ODD = 1,
130         /** Even parity. */
131         SP_PARITY_EVEN = 2,
132 };
133
134 /** RTS pin behaviour. */
135 enum sp_rts {
136         /** Special value to indicate setting should be left alone. */
137         SP_RTS_INVALID = -1,
138         /** RTS off. */
139         SP_RTS_OFF = 0,
140         /** RTS on. */
141         SP_RTS_ON = 1,
142         /** RTS used for flow control. */
143         SP_RTS_FLOW_CONTROL = 2,
144 };
145
146 /** CTS pin behaviour. */
147 enum sp_cts {
148         /** Special value to indicate setting should be left alone. */
149         SP_CTS_INVALID = -1,
150         /** CTS ignored. */
151         SP_CTS_IGNORE = 0,
152         /** CTS used for flow control. */
153         SP_CTS_FLOW_CONTROL = 1,
154 };
155
156 /** DTR pin behaviour. */
157 enum sp_dtr {
158         /** Special value to indicate setting should be left alone. */
159         SP_DTR_INVALID = -1,
160         /** DTR off. */
161         SP_DTR_OFF = 0,
162         /** DTR on. */
163         SP_DTR_ON = 1,
164         /** DTR used for flow control. */
165         SP_DTR_FLOW_CONTROL = 2,
166 };
167
168 /** DSR pin behaviour. */
169 enum sp_dsr {
170         /** Special value to indicate setting should be left alone. */
171         SP_DSR_INVALID = -1,
172         /** DSR ignored. */
173         SP_DSR_IGNORE = 0,
174         /** DSR used for flow control. */
175         SP_DSR_FLOW_CONTROL = 1,
176 };
177
178 /** XON/XOFF flow control behaviour. */
179 enum sp_xonxoff {
180         /** Special value to indicate setting should be left alone. */
181         SP_XONXOFF_INVALID = -1,
182         /** XON/XOFF disabled. */
183         SP_XONXOFF_DISABLED = 0,
184         /** XON/XOFF enabled for input only. */
185         SP_XONXOFF_IN = 1,
186         /** XON/XOFF enabled for output only. */
187         SP_XONXOFF_OUT = 2,
188         /** XON/XOFF enabled for input and output. */
189         SP_XONXOFF_INOUT = 3,
190 };
191
192 /** Standard flow control combinations. */
193 enum sp_flowcontrol {
194         /** No flow control. */
195         SP_FLOWCONTROL_NONE = 0,
196         /** Software flow control using XON/XOFF characters. */
197         SP_FLOWCONTROL_XONXOFF = 1,
198         /** Hardware flow control using RTS/CTS signals. */
199         SP_FLOWCONTROL_RTSCTS = 2,
200         /** Hardware flow control using DTR/DSR signals. */
201         SP_FLOWCONTROL_DTRDSR = 3,
202 };
203
204 /** A serial port. */
205 struct sp_port {
206         /** Name used to open the port. */
207         char *name;
208 /** @cond 0 */
209         /** OS-specific port handle. */
210 #ifdef _WIN32
211         HANDLE hdl;
212 #else
213         int fd;
214 #endif
215 /** @endcond */
216 };
217
218 /** Configuration for a serial port. */
219 struct sp_port_config {
220         /** Baud rate in bits per second. */
221         int baudrate;
222         /** Number of data bits to use. Valid values are 5 to 8. */
223         int bits;
224         /** Parity setting to use. */
225         enum sp_parity parity;
226         /** Number of stop bits to use (1 or 2). */
227         int stopbits;
228         /** RTS pin mode. */
229         enum sp_rts rts;
230         /** CTS pin mode. */
231         enum sp_cts cts;
232         /** DTR pin mode. */
233         enum sp_dtr dtr;
234         /** DSR pin mode. */
235         enum sp_dsr dsr;
236         /** XON/XOFF flow control mode. */
237         enum sp_xonxoff xon_xoff;
238 };
239
240 /**
241 @defgroup Enumeration Port enumeration
242 @{
243 */
244
245 /**
246  * Obtain a pointer to a new sp_port structure representing the named port.
247  *
248  * The user should allocate a variable of type "struct sp_port *" and pass a
249  * pointer to this to receive the result.
250  *
251  * The result should be freed after use by calling sp_free_port().
252  *
253  * @return SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
254  *         failure, or SP_ERR_ARG if an invalid pointer is passed. If any error
255  *         is returned, the variable pointed to by port_ptr will be set to NULL.
256  *         Otherwise, it will be set to point to the newly allocated port.
257  */
258 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
259
260 /**
261  * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
262  */
263 void sp_free_port(struct sp_port *port);
264
265 /**
266  * List the serial ports available on the system.
267  *
268  * The result obtained is an array of pointers to sp_port structures,
269  * terminated by a NULL. The user should allocate a variable of type
270  * "struct sp_port **" and pass a pointer to this to receive the result.
271  *
272  * The result should be freed after use by calling sp_free_port_list().
273  * If a port from the list is to be used after freeing the list, it must be
274  * copied first using sp_copy_port().
275  *
276  * @return SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
277  *         failure, or SP_ERR_ARG if an invalid pointer is passed. If any error
278  *         is returned, the variable pointed to by list_ptr will be set to NULL.
279  *         Otherwise, it will be set to point to the newly allocated array.
280  */
281 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
282
283 /**
284  * Make a new copy of a sp_port structure.
285  *
286  * The user should allocate a variable of type "struct sp_port *" and pass a
287  * pointer to this to receive the result.
288  *
289  * The copy should be freed after use by calling sp_free_port().
290  *
291  * @return SP_OK on success, SP_ERR_MEM on allocation failure, or SP_ERR_ARG
292  *         if an invalid port or pointer is passed. If any error is returned,
293  *         the variable pointed to by copy_ptr will be set to NULL. Otherwise,
294  *         it will be set to point to the newly allocated copy.
295  */
296 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
297
298 /**
299  * Free a port list obtained from sp_list_ports().
300  *
301  * This will also free all the sp_port structures referred to from the list;
302  * any that are to be retained must be copied first using sp_copy_port().
303  */
304 void sp_free_port_list(struct sp_port **ports);
305
306 /**
307  * @}
308  * @defgroup Ports Opening and closing ports
309  * @{
310  */
311
312 /**
313  * Open the specified serial port.
314  *
315  * @param port Pointer to port structure.
316  * @param flags Flags to use when opening the serial port.
317  *
318  * @return SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
319  *         failure, or SP_ERR_ARG if an invalid port is passed.
320  */
321 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
322
323 /**
324  * Close the specified serial port.
325  *
326  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
327  *         if an invalid port is passed.
328  */
329 enum sp_return sp_close(struct sp_port *port);
330
331 /**
332  * @}
333  * @defgroup Configuration Setting port parameters
334  * @{
335  */
336
337 /**
338  * Get the current configuration of the specified serial port.
339  *
340  * The user should allocate a struct sp_port_config, then pass a pointer to it
341  * as the config parameter. The struct will be populated with the port
342  * configuration.
343  *
344  * Any setting that is in a state not recognised or supported by
345  * libserialport will have its value set to -1 in the struct.
346  *
347  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
348  *         for invalid arguments.
349  */
350 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
351
352 /**
353  * Set the configuration for the specified serial port.
354  *
355  * The user should populate a struct sp_port_config, then pass a pointer to it
356  * as the config parameter.
357  *
358  * To retain the current value of any setting, set that field to -1.
359  *
360  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
361  *         for invalid arguments.
362  */
363 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
364
365 /**
366  * Set the baud rate for the specified serial port.
367  *
368  * @param port Pointer to port structure.
369  * @param baudrate Baud rate in bits per second.
370  *
371  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
372  *         for invalid arguments.
373  */
374 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
375
376 /**
377  * Set the number of data bits for the specified serial port.
378  *
379  * @param port Pointer to port structure.
380  * @param bits Number of data bits to use. Valid values are 5 to 8.
381  *
382  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
383  *         for invalid arguments.
384  */
385 enum sp_return sp_set_bits(struct sp_port *port, int bits);
386
387 /**
388  * Set the parity for the specified serial port.
389  *
390  * @param port Pointer to port structure.
391  * @param parity Parity setting to use.
392  *
393  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
394  *         for invalid arguments.
395  */
396 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
397
398 /**
399  * Set the number of stop bits for the specified port.
400  *
401  * @param port Pointer to port structure.
402  * @param stopbits Number of stop bits to use (1 or 2).
403  *
404  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
405  *         for invalid arguments.
406  */
407 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
408
409 /**
410  * Set the flow control type for the specified serial port.
411  *
412  * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
413  * XON/XOFF settings as necessary for the specified flow control
414  * type. For more fine-grained control of these settings, use their
415  * individual configuration functions or the sp_set_config() function.
416  *
417  * @param port Pointer to port structure.
418  * @param flowcontrol Flow control setting to use.
419  *
420  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
421  *         for invalid arguments.
422  */
423 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
424
425 /**
426  * Set the RTS pin behaviour for the specified port.
427  *
428  * @param port Pointer to port structure.
429  * @param rts RTS pin mode.
430  *
431  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
432  *         for invalid arguments.
433  */
434 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
435
436 /**
437  * Set the CTS pin behaviour for the specified port.
438  *
439  * @param port Pointer to port structure.
440  * @param cts CTS pin mode.
441  *
442  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
443  *         for invalid arguments.
444  */
445 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
446
447 /**
448  * Set the DTR pin behaviour for the specified port.
449  *
450  * @param port Pointer to port structure.
451  * @param dtr DTR pin mode.
452  *
453  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
454  *         for invalid arguments.
455  */
456 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
457
458 /**
459  * Set the RTS pin behaviour for the specified port.
460  *
461  * @param port Pointer to port structure.
462  * @param dsr DSR pin mode.
463  *
464  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
465  *         for invalid arguments.
466  */
467 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
468
469 /**
470  * Configure XON/XOFF flow control for the specified port.
471  *
472  * @param port Pointer to port structure.
473  * @param xon_xoff XON/XOFF flow control mode.
474  *
475  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
476  *         for invalid arguments.
477  */
478 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
479
480 /**
481  * @}
482  * @defgroup Data Reading, writing, and flushing data
483  * @{
484 */
485
486 /**
487  * Read bytes from the specified serial port.
488  *
489  * Note that this function may return after reading less than the specified
490  * number of bytes; it is the user's responsibility to iterate as necessary
491  * in this case.
492  *
493  * @param port Pointer to port structure.
494  * @param buf Buffer in which to store the bytes read.
495  * @param count Maximum number of bytes to read.
496  *
497  * @return The number of bytes read, SP_ERR_FAIL on failure,
498  *         or SP_ERR_ARG for invalid arguments.
499  */
500 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count);
501
502 /**
503  * Write bytes to the specified serial port.
504  *
505  * Note that this function may return after writing less than the specified
506  * number of bytes; it is the user's responsibility to iterate as necessary
507  * in this case.
508  *
509  * @param port Pointer to port structure.
510  * @param buf Buffer containing the bytes to write.
511  * @param count Maximum number of bytes to write.
512  *
513  * @return The number of bytes written, SP_ERR_FAIL on failure,
514  *         or SP_ERR_ARG for invalid arguments.
515  */
516 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count);
517
518 /**
519  * Flush serial port buffers. Data in the selected buffer(s) is discarded.
520  *
521  * @param port Pointer to port structure.
522  * @param buffers Which buffer(s) to flush.
523  *
524  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
525  *         if an invalid port is passed.
526  */
527 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
528
529 /**
530  * Wait for buffered data to be transmitted.
531  *
532  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
533  *         if an invalid port is passed.
534  */
535 enum sp_return sp_drain(struct sp_port *port);
536
537 /**
538  * @}
539  * @defgroup Signal Port signalling operations
540  * @{
541  */
542
543 /**
544  * Put the port transmit line into the break state.
545  *
546  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
547  *         if an invalid port is passed.
548  */
549 enum sp_return sp_start_break(struct sp_port *port);
550
551 /**
552  * Take the port transmit line out of the break state.
553  *
554  * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
555  *         if an invalid port is passed.
556  */
557 enum sp_return sp_end_break(struct sp_port *port);
558
559 /**
560  * @}
561  * @defgroup Errors Obtaining error information
562  * @{
563 */
564
565 /**
566  * Get the error code for a failed operation.
567  *
568  * In order to obtain the correct result, this function should be called
569  * straight after the failure, before executing any other system operations.
570  *
571  * @return The system's numeric code for the error that caused the last
572  *         operation to fail.
573  */
574 int sp_last_error_code(void);
575
576 /**
577  * Get the error message for a failed operation.
578  *
579  * In order to obtain the correct result, this function should be called
580  * straight after the failure, before executing other system operations.
581  *
582  * @return The system's message for the error that caused the last
583  *         operation to fail. This string may be allocated by the function,
584  *         and should be freed after use by calling sp_free_error_message().
585  */
586 char *sp_last_error_message(void);
587
588 /**
589  * Free an error message returned by sp_last_error_message().
590  */
591 void sp_free_error_message(char *message);
592
593 /** @} */
594
595 #ifdef __cplusplus
596 }
597 #endif
598
599 #endif /* LIBSERIALPORT_H */