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