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