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