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