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