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