]>
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 | 38 | * - @ref Data |
6f1186aa | 39 | * - @ref Waiting |
cf9d365c UH |
40 | * - @ref Errors |
41 | * | |
42 | * libserialport is an open source project released under the LGPL3+ license. | |
43 | * | |
44 | * API principles | |
45 | * ============== | |
46 | * | |
47 | * The API is simple, and designed to be a minimal wrapper around the serial | |
48 | * port support in each OS. | |
49 | * | |
50 | * Most functions take a pointer to a struct sp_port, which represents a serial | |
51 | * port. These structures are always allocated and freed by the library, using | |
52 | * the functions in the @ref Enumeration "Enumeration" section. | |
53 | * | |
0151b157 ML |
54 | * Most functions have return type @ref sp_return and can return only four |
55 | * possible error values: | |
cf9d365c | 56 | * |
6aabf62a ML |
57 | * - @ref SP_ERR_ARG means that a function was called with invalid |
58 | * arguments. This implies a bug in the caller. The arguments passed would | |
59 | * be invalid regardless of the underlying OS or serial device involved. | |
60 | * | |
61 | * - @ref SP_ERR_FAIL means that the OS reported a failure. The error code or | |
62 | * message provided by the OS can be obtained by calling sp_last_error_code() | |
63 | * or sp_last_error_message(). | |
64 | * | |
65 | * - @ref SP_ERR_SUPP indicates that there is no support for the requested | |
66 | * operation in the current OS, driver or device. No error message is | |
67 | * available from the OS in this case. There is either no way to request | |
68 | * the operation in the first place, or libserialport does not know how to | |
69 | * do so in the current version. | |
70 | * | |
71 | * - @ref SP_ERR_MEM indicates that a memory allocation failed. | |
72 | * | |
73 | * All of these error values are negative. | |
cf9d365c | 74 | * |
0151b157 ML |
75 | * Calls that succeed return @ref SP_OK, which is equal to zero. Some functions |
76 | * declared @ref sp_return can also return a positive value for a successful | |
0a1ab8bf | 77 | * numeric result, e.g. sp_blocking_read() or sp_blocking_write(). |
cf9d365c | 78 | */ |
cd5f5281 | 79 | |
8645feda UH |
80 | #ifndef LIBSERIALPORT_LIBSERIALPORT_H |
81 | #define LIBSERIALPORT_LIBSERIALPORT_H | |
e8ffaee9 | 82 | |
5ef8a1ed UH |
83 | #ifdef __cplusplus |
84 | extern "C" { | |
85 | #endif | |
86 | ||
74510d4b ML |
87 | #include <stddef.h> |
88 | #ifdef _WIN32 | |
89 | #include <windows.h> | |
90 | #endif | |
91 | ||
cd5f5281 | 92 | /** Return values. */ |
eb6ed20f | 93 | enum sp_return { |
cd5f5281 | 94 | /** Operation completed successfully. */ |
74510d4b | 95 | SP_OK = 0, |
cd5f5281 | 96 | /** Invalid arguments were passed to the function. */ |
e9a2f9c9 | 97 | SP_ERR_ARG = -1, |
cfa5af67 | 98 | /** A system error occurred while executing the operation. */ |
e9a2f9c9 | 99 | SP_ERR_FAIL = -2, |
cd5f5281 | 100 | /** A memory allocation failed while executing the operation. */ |
f92f1f0c | 101 | SP_ERR_MEM = -3, |
6aabf62a | 102 | /** The requested operation is not supported by this system or device. */ |
79a80046 | 103 | SP_ERR_SUPP = -4 |
74510d4b ML |
104 | }; |
105 | ||
cd5f5281 | 106 | /** Port access modes. */ |
eb6ed20f | 107 | enum sp_mode { |
a036341b ML |
108 | /** Open port for read access. */ |
109 | SP_MODE_READ = 1, | |
110 | /** Open port for write access. */ | |
276ef1b9 | 111 | SP_MODE_WRITE = 2, |
13efecf8 | 112 | /** Open port for read and write access. @since 0.1.1 */ |
276ef1b9 | 113 | SP_MODE_READ_WRITE = 3 |
74510d4b ML |
114 | }; |
115 | ||
6f1186aa ML |
116 | /** Port events. */ |
117 | enum sp_event { | |
144a598f | 118 | /** Data received and ready to read. */ |
6f1186aa | 119 | SP_EVENT_RX_READY = 1, |
144a598f | 120 | /** Ready to transmit new data. */ |
6f1186aa | 121 | SP_EVENT_TX_READY = 2, |
144a598f | 122 | /** Error occurred. */ |
79a80046 | 123 | SP_EVENT_ERROR = 4 |
6f1186aa ML |
124 | }; |
125 | ||
fd8fd11a ML |
126 | /** Buffer selection. */ |
127 | enum sp_buffer { | |
128 | /** Input buffer. */ | |
129 | SP_BUF_INPUT = 1, | |
130 | /** Output buffer. */ | |
131 | SP_BUF_OUTPUT = 2, | |
132 | /** Both buffers. */ | |
79a80046 | 133 | SP_BUF_BOTH = 3 |
fd8fd11a ML |
134 | }; |
135 | ||
cd5f5281 | 136 | /** Parity settings. */ |
eb6ed20f | 137 | enum sp_parity { |
c200f5c1 | 138 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 139 | SP_PARITY_INVALID = -1, |
cd5f5281 | 140 | /** No parity. */ |
74510d4b | 141 | SP_PARITY_NONE = 0, |
cd5f5281 | 142 | /** Odd parity. */ |
20e63a77 ML |
143 | SP_PARITY_ODD = 1, |
144 | /** Even parity. */ | |
145 | SP_PARITY_EVEN = 2, | |
e432ce60 ML |
146 | /** Mark parity. */ |
147 | SP_PARITY_MARK = 3, | |
148 | /** Space parity. */ | |
79a80046 | 149 | SP_PARITY_SPACE = 4 |
74510d4b ML |
150 | }; |
151 | ||
cd5f5281 | 152 | /** RTS pin behaviour. */ |
eb6ed20f | 153 | enum sp_rts { |
c200f5c1 | 154 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 155 | SP_RTS_INVALID = -1, |
cd5f5281 | 156 | /** RTS off. */ |
d514a26f | 157 | SP_RTS_OFF = 0, |
cd5f5281 | 158 | /** RTS on. */ |
d514a26f | 159 | SP_RTS_ON = 1, |
cd5f5281 | 160 | /** RTS used for flow control. */ |
79a80046 | 161 | SP_RTS_FLOW_CONTROL = 2 |
d514a26f ML |
162 | }; |
163 | ||
cd5f5281 | 164 | /** CTS pin behaviour. */ |
eb6ed20f | 165 | enum sp_cts { |
c200f5c1 | 166 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 167 | SP_CTS_INVALID = -1, |
cd5f5281 | 168 | /** CTS ignored. */ |
d514a26f | 169 | SP_CTS_IGNORE = 0, |
cd5f5281 | 170 | /** CTS used for flow control. */ |
79a80046 | 171 | SP_CTS_FLOW_CONTROL = 1 |
d514a26f ML |
172 | }; |
173 | ||
cd5f5281 | 174 | /** DTR pin behaviour. */ |
eb6ed20f | 175 | enum sp_dtr { |
c200f5c1 | 176 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 177 | SP_DTR_INVALID = -1, |
cd5f5281 | 178 | /** DTR off. */ |
d514a26f | 179 | SP_DTR_OFF = 0, |
cd5f5281 | 180 | /** DTR on. */ |
d514a26f | 181 | SP_DTR_ON = 1, |
cd5f5281 | 182 | /** DTR used for flow control. */ |
79a80046 | 183 | SP_DTR_FLOW_CONTROL = 2 |
d514a26f ML |
184 | }; |
185 | ||
cd5f5281 | 186 | /** DSR pin behaviour. */ |
eb6ed20f | 187 | enum sp_dsr { |
c200f5c1 | 188 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 189 | SP_DSR_INVALID = -1, |
cd5f5281 | 190 | /** DSR ignored. */ |
d514a26f | 191 | SP_DSR_IGNORE = 0, |
cd5f5281 | 192 | /** DSR used for flow control. */ |
79a80046 | 193 | SP_DSR_FLOW_CONTROL = 1 |
d514a26f ML |
194 | }; |
195 | ||
cd5f5281 | 196 | /** XON/XOFF flow control behaviour. */ |
eb6ed20f | 197 | enum sp_xonxoff { |
c200f5c1 | 198 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 199 | SP_XONXOFF_INVALID = -1, |
cd5f5281 | 200 | /** XON/XOFF disabled. */ |
d514a26f | 201 | SP_XONXOFF_DISABLED = 0, |
cd5f5281 | 202 | /** XON/XOFF enabled for input only. */ |
d514a26f | 203 | SP_XONXOFF_IN = 1, |
cd5f5281 | 204 | /** XON/XOFF enabled for output only. */ |
d514a26f | 205 | SP_XONXOFF_OUT = 2, |
cd5f5281 | 206 | /** XON/XOFF enabled for input and output. */ |
79a80046 | 207 | SP_XONXOFF_INOUT = 3 |
ac74fdaf ML |
208 | }; |
209 | ||
cd5f5281 | 210 | /** Standard flow control combinations. */ |
eb6ed20f | 211 | enum sp_flowcontrol { |
cd5f5281 | 212 | /** No flow control. */ |
18fc2dd1 | 213 | SP_FLOWCONTROL_NONE = 0, |
cd5f5281 | 214 | /** Software flow control using XON/XOFF characters. */ |
18fc2dd1 | 215 | SP_FLOWCONTROL_XONXOFF = 1, |
cd5f5281 | 216 | /** Hardware flow control using RTS/CTS signals. */ |
18fc2dd1 | 217 | SP_FLOWCONTROL_RTSCTS = 2, |
cd5f5281 | 218 | /** Hardware flow control using DTR/DSR signals. */ |
79a80046 | 219 | SP_FLOWCONTROL_DTRDSR = 3 |
18fc2dd1 ML |
220 | }; |
221 | ||
8cf7c697 ML |
222 | /** Input signals. */ |
223 | enum sp_signal { | |
224 | /** Clear to send. */ | |
225 | SP_SIG_CTS = 1, | |
226 | /** Data set ready. */ | |
227 | SP_SIG_DSR = 2, | |
228 | /** Data carrier detect. */ | |
229 | SP_SIG_DCD = 4, | |
230 | /** Ring indicator. */ | |
79a80046 | 231 | SP_SIG_RI = 8 |
8cf7c697 ML |
232 | }; |
233 | ||
13efecf8 UH |
234 | /** |
235 | * Transport types. | |
236 | * | |
237 | * @since 0.1.1 | |
238 | */ | |
a93fb468 | 239 | enum sp_transport { |
13efecf8 | 240 | /** Native platform serial port. @since 0.1.1 */ |
a93fb468 | 241 | SP_TRANSPORT_NATIVE, |
13efecf8 | 242 | /** USB serial port adapter. @since 0.1.1 */ |
a93fb468 | 243 | SP_TRANSPORT_USB, |
13efecf8 | 244 | /** Bluetooth serial port adapter. @since 0.1.1 */ |
79a80046 | 245 | SP_TRANSPORT_BLUETOOTH |
a93fb468 AJ |
246 | }; |
247 | ||
0a1ab8bf UH |
248 | /** |
249 | * @struct sp_port | |
250 | * An opaque structure representing a serial port. | |
251 | */ | |
1c5aae9d | 252 | struct sp_port; |
eb6ed20f | 253 | |
0a1ab8bf UH |
254 | /** |
255 | * @struct sp_port_config | |
256 | * An opaque structure representing the configuration for a serial port. | |
257 | */ | |
9b1502ef | 258 | struct sp_port_config; |
eb6ed20f | 259 | |
6f1186aa ML |
260 | /** |
261 | * @struct sp_event_set | |
262 | * A set of handles to wait on for events. | |
263 | */ | |
264 | struct sp_event_set { | |
265 | /** Array of OS-specific handles. */ | |
266 | void *handles; | |
267 | /** Array of bitmasks indicating which events apply for each handle. */ | |
268 | enum sp_event *masks; | |
269 | /** Number of handles. */ | |
270 | unsigned int count; | |
271 | }; | |
272 | ||
091e75fe | 273 | /** |
144a598f UH |
274 | * @defgroup Enumeration Port enumeration |
275 | * | |
276 | * Enumerating the serial ports of a system. | |
277 | * | |
278 | * @{ | |
279 | */ | |
091e75fe | 280 | |
cd5f5281 | 281 | /** |
cf9d365c UH |
282 | * Obtain a pointer to a new sp_port structure representing the named port. |
283 | * | |
284 | * The user should allocate a variable of type "struct sp_port *" and pass a | |
285 | * pointer to this to receive the result. | |
286 | * | |
287 | * The result should be freed after use by calling sp_free_port(). | |
288 | * | |
f36c6395 ML |
289 | * If any error is returned, the variable pointed to by port_ptr will be set |
290 | * to NULL. Otherwise, it will be set to point to the newly allocated port. | |
291 | * | |
292 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
293 | * |
294 | * @since 0.1.0 | |
cf9d365c | 295 | */ |
eb6ed20f | 296 | enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr); |
cd5f5281 ML |
297 | |
298 | /** | |
cf9d365c | 299 | * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port(). |
1652aa86 UH |
300 | * |
301 | * @since 0.1.0 | |
cf9d365c | 302 | */ |
e3b2f7a4 | 303 | void sp_free_port(struct sp_port *port); |
cd5f5281 ML |
304 | |
305 | /** | |
cf9d365c UH |
306 | * List the serial ports available on the system. |
307 | * | |
308 | * The result obtained is an array of pointers to sp_port structures, | |
309 | * terminated by a NULL. The user should allocate a variable of type | |
310 | * "struct sp_port **" and pass a pointer to this to receive the result. | |
311 | * | |
312 | * The result should be freed after use by calling sp_free_port_list(). | |
313 | * If a port from the list is to be used after freeing the list, it must be | |
314 | * copied first using sp_copy_port(). | |
315 | * | |
f36c6395 ML |
316 | * If any error is returned, the variable pointed to by list_ptr will be set |
317 | * to NULL. Otherwise, it will be set to point to the newly allocated array. | |
318 | * | |
319 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
320 | * |
321 | * @since 0.1.0 | |
cf9d365c | 322 | */ |
eb6ed20f | 323 | enum sp_return sp_list_ports(struct sp_port ***list_ptr); |
cd5f5281 ML |
324 | |
325 | /** | |
cf9d365c UH |
326 | * Make a new copy of a sp_port structure. |
327 | * | |
328 | * The user should allocate a variable of type "struct sp_port *" and pass a | |
329 | * pointer to this to receive the result. | |
330 | * | |
331 | * The copy should be freed after use by calling sp_free_port(). | |
332 | * | |
f36c6395 ML |
333 | * If any error is returned, the variable pointed to by copy_ptr will be set |
334 | * to NULL. Otherwise, it will be set to point to the newly allocated copy. | |
335 | * | |
336 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
337 | * |
338 | * @since 0.1.0 | |
cf9d365c | 339 | */ |
eb6ed20f | 340 | enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr); |
cd5f5281 ML |
341 | |
342 | /** | |
cf9d365c UH |
343 | * Free a port list obtained from sp_list_ports(). |
344 | * | |
345 | * This will also free all the sp_port structures referred to from the list; | |
346 | * any that are to be retained must be copied first using sp_copy_port(). | |
1652aa86 UH |
347 | * |
348 | * @since 0.1.0 | |
cf9d365c | 349 | */ |
d54e9004 | 350 | void sp_free_port_list(struct sp_port **ports); |
e96d8bd2 | 351 | |
091e75fe | 352 | /** |
cf9d365c | 353 | * @} |
144a598f UH |
354 | * @defgroup Ports Port handling |
355 | * | |
356 | * Opening, closing and querying ports. | |
357 | * | |
cf9d365c UH |
358 | * @{ |
359 | */ | |
091e75fe | 360 | |
cd5f5281 | 361 | /** |
cf9d365c UH |
362 | * Open the specified serial port. |
363 | * | |
364 | * @param port Pointer to port structure. | |
365 | * @param flags Flags to use when opening the serial port. | |
366 | * | |
f36c6395 | 367 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
368 | * |
369 | * @since 0.1.0 | |
cf9d365c | 370 | */ |
eb6ed20f | 371 | enum sp_return sp_open(struct sp_port *port, enum sp_mode flags); |
cd5f5281 ML |
372 | |
373 | /** | |
cf9d365c UH |
374 | * Close the specified serial port. |
375 | * | |
f36c6395 | 376 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
377 | * |
378 | * @since 0.1.0 | |
cf9d365c | 379 | */ |
eb6ed20f | 380 | enum sp_return sp_close(struct sp_port *port); |
e96d8bd2 | 381 | |
0151b157 ML |
382 | /** |
383 | * Get the name of a port. | |
384 | * | |
385 | * The name returned is whatever is normally used to refer to a port on the | |
386 | * current operating system; e.g. for Windows it will usually be a "COMn" | |
387 | * device name, and for Unix it will be a device path beginning with "/dev/". | |
388 | * | |
389 | * @param port Pointer to port structure. | |
390 | * | |
391 | * @return The port name, or NULL if an invalid port is passed. The name | |
392 | * string is part of the port structure and may not be used after the | |
393 | * port structure has been freed. | |
1652aa86 UH |
394 | * |
395 | * @since 0.1.0 | |
0151b157 ML |
396 | */ |
397 | char *sp_get_port_name(const struct sp_port *port); | |
398 | ||
a93fb468 AJ |
399 | /** |
400 | * Get a description for a port, to present to end user. | |
401 | * | |
402 | * @param port Pointer to port structure. | |
403 | * | |
404 | * @return The port description, or NULL if an invalid port is passed. | |
405 | * The description string is part of the port structure and may not be used | |
406 | * after the port structure has been freed. | |
407 | * | |
3f2f48fc | 408 | * @since 0.1.1 |
a93fb468 AJ |
409 | */ |
410 | char *sp_get_port_description(struct sp_port *port); | |
411 | ||
412 | /** | |
413 | * Get the transport type used by a port. | |
414 | * | |
415 | * @param port Pointer to port structure. | |
416 | * | |
417 | * @return The port transport type. | |
418 | * | |
3f2f48fc | 419 | * @since 0.1.1 |
a93fb468 AJ |
420 | */ |
421 | enum sp_transport sp_get_port_transport(struct sp_port *port); | |
422 | ||
423 | /** | |
424 | * Get the USB bus number and address on bus of a USB serial adapter port. | |
425 | * | |
426 | * @param port Pointer to port structure. | |
427 | * @param usb_bus Pointer to variable to store USB bus. | |
428 | * @param usb_address Pointer to variable to store USB address | |
429 | * | |
430 | * @return SP_OK upon success, a negative error code otherwise. | |
431 | * | |
3f2f48fc | 432 | * @since 0.1.1 |
a93fb468 AJ |
433 | */ |
434 | enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port, | |
435 | int *usb_bus, int *usb_address); | |
436 | ||
437 | /** | |
438 | * Get the USB Vendor ID and Product ID of a USB serial adapter port. | |
439 | * | |
440 | * @param port Pointer to port structure. | |
441 | * @param usb_vid Pointer to variable to store USB VID. | |
442 | * @param usb_pid Pointer to variable to store USB PID | |
443 | * | |
444 | * @return SP_OK upon success, a negative error code otherwise. | |
445 | * | |
3f2f48fc | 446 | * @since 0.1.1 |
a93fb468 AJ |
447 | */ |
448 | enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, int *usb_vid, int *usb_pid); | |
449 | ||
450 | /** | |
451 | * Get the USB manufacturer string of a USB serial adapter port. | |
452 | * | |
453 | * @param port Pointer to port structure. | |
454 | * | |
455 | * @return The port manufacturer string, or NULL if an invalid port is passed. | |
456 | * The manufacturer string is part of the port structure and may not be used | |
457 | * after the port structure has been freed. | |
458 | * | |
3f2f48fc | 459 | * @since 0.1.1 |
a93fb468 AJ |
460 | */ |
461 | char *sp_get_port_usb_manufacturer(const struct sp_port *port); | |
462 | ||
463 | /** | |
464 | * Get the USB product string of a USB serial adapter port. | |
465 | * | |
466 | * @param port Pointer to port structure. | |
467 | * | |
468 | * @return The port product string, or NULL if an invalid port is passed. | |
469 | * The product string is part of the port structure and may not be used | |
470 | * after the port structure has been freed. | |
471 | * | |
3f2f48fc | 472 | * @since 0.1.1 |
a93fb468 AJ |
473 | */ |
474 | char *sp_get_port_usb_product(const struct sp_port *port); | |
475 | ||
476 | /** | |
477 | * Get the USB serial number string of a USB serial adapter port. | |
478 | * | |
479 | * @param port Pointer to port structure. | |
480 | * | |
481 | * @return The port serial number, or NULL if an invalid port is passed. | |
482 | * The serial number string is part of the port structure and may not be used | |
483 | * after the port structure has been freed. | |
484 | * | |
3f2f48fc | 485 | * @since 0.1.1 |
a93fb468 AJ |
486 | */ |
487 | char *sp_get_port_usb_serial(const struct sp_port *port); | |
488 | ||
489 | /** | |
e33dcf90 | 490 | * Get the MAC address of a Bluetooth serial adapter port. |
a93fb468 AJ |
491 | * |
492 | * @param port Pointer to port structure. | |
493 | * | |
e33dcf90 ML |
494 | * @return The port MAC address, or NULL if an invalid port is passed. |
495 | * The MAC address string is part of the port structure and may not be used | |
a93fb468 AJ |
496 | * after the port structure has been freed. |
497 | * | |
3f2f48fc | 498 | * @since 0.1.1 |
a93fb468 AJ |
499 | */ |
500 | char *sp_get_port_bluetooth_address(const struct sp_port *port); | |
501 | ||
0151b157 ML |
502 | /** |
503 | * Get the operating system handle for a port. | |
504 | * | |
505 | * The type of the handle depends on the operating system. On Unix based | |
506 | * systems, the handle is a file descriptor of type "int". On Windows, the | |
507 | * handle is of type "HANDLE". The user should allocate a variable of the | |
508 | * appropriate type and pass a pointer to this to receive the result. | |
509 | * | |
510 | * To obtain a valid handle, the port must first be opened by calling | |
511 | * sp_open() using the same port structure. | |
512 | * | |
513 | * After the port is closed or the port structure freed, the handle may | |
514 | * no longer be valid. | |
515 | * | |
516 | * @warning This feature is provided so that programs may make use of | |
517 | * OS-specific functionality where desired. Doing so obviously | |
518 | * comes at a cost in portability. It also cannot be guaranteed | |
519 | * that direct usage of the OS handle will not conflict with the | |
520 | * library's own usage of the port. Be careful. | |
521 | * | |
522 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
523 | * |
524 | * @since 0.1.0 | |
0151b157 ML |
525 | */ |
526 | enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr); | |
527 | ||
cd5f5281 | 528 | /** |
cf9d365c | 529 | * @} |
144a598f UH |
530 | * |
531 | * @defgroup Configuration Configuration | |
532 | * | |
533 | * Setting and querying serial port parameters. | |
cf9d365c UH |
534 | * @{ |
535 | */ | |
e96d8bd2 | 536 | |
9b1502ef ML |
537 | /** |
538 | * Allocate a port configuration structure. | |
539 | * | |
540 | * The user should allocate a variable of type "struct sp_config *" and pass a | |
541 | * pointer to this to receive the result. The variable will be updated to | |
542 | * point to the new configuration structure. The structure is opaque and must | |
543 | * be accessed via the functions provided. | |
544 | * | |
545 | * All parameters in the structure will be initialised to special values which | |
546 | * are ignored by sp_set_config(). | |
547 | * | |
548 | * The structure should be freed after use by calling sp_free_config(). | |
549 | * | |
550 | * @param config_ptr Pointer to variable to receive result. | |
551 | * | |
552 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
553 | * |
554 | * @since 0.1.0 | |
9b1502ef ML |
555 | */ |
556 | enum sp_return sp_new_config(struct sp_port_config **config_ptr); | |
557 | ||
558 | /** | |
559 | * Free a port configuration structure. | |
560 | * | |
561 | * @param config Pointer to configuration structure. | |
1652aa86 UH |
562 | * |
563 | * @since 0.1.0 | |
9b1502ef ML |
564 | */ |
565 | void sp_free_config(struct sp_port_config *config); | |
566 | ||
cd5f5281 | 567 | /** |
cf9d365c UH |
568 | * Get the current configuration of the specified serial port. |
569 | * | |
9b1502ef ML |
570 | * The user should allocate a configuration structure using sp_new_config() |
571 | * and pass this as the config parameter. The configuration structure will | |
572 | * be updated with the port configuration. | |
cf9d365c | 573 | * |
9b1502ef ML |
574 | * Any parameters that are configured with settings not recognised or |
575 | * supported by libserialport, will be set to special values that are | |
576 | * ignored by sp_set_config(). | |
cf9d365c | 577 | * |
f36c6395 | 578 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
579 | * |
580 | * @since 0.1.0 | |
cf9d365c | 581 | */ |
eb6ed20f | 582 | enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config); |
cd5f5281 ML |
583 | |
584 | /** | |
cf9d365c UH |
585 | * Set the configuration for the specified serial port. |
586 | * | |
9b1502ef ML |
587 | * For each parameter in the configuration, there is a special value (usually |
588 | * -1, but see the documentation for each field). These values will be ignored | |
589 | * and the corresponding setting left unchanged on the port. | |
cf9d365c | 590 | * |
f36c6395 | 591 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
592 | * |
593 | * @since 0.1.0 | |
cf9d365c | 594 | */ |
eb6ed20f | 595 | enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config); |
cd5f5281 ML |
596 | |
597 | /** | |
cf9d365c UH |
598 | * Set the baud rate for the specified serial port. |
599 | * | |
600 | * @param port Pointer to port structure. | |
601 | * @param baudrate Baud rate in bits per second. | |
602 | * | |
f36c6395 | 603 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
604 | * |
605 | * @since 0.1.0 | |
cf9d365c | 606 | */ |
eb6ed20f | 607 | enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate); |
cd5f5281 ML |
608 | |
609 | /** | |
9b1502ef ML |
610 | * Get the baud rate from a port configuration. |
611 | * | |
612 | * The user should allocate a variable of type int and pass a pointer to this | |
613 | * to receive the result. | |
614 | * | |
615 | * @param config Pointer to configuration structure. | |
616 | * @param baudrate_ptr Pointer to variable to store result. | |
617 | * | |
618 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
619 | * |
620 | * @since 0.1.0 | |
9b1502ef ML |
621 | */ |
622 | enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr); | |
623 | ||
624 | /** | |
625 | * Set the baud rate in a port configuration. | |
626 | * | |
627 | * @param config Pointer to configuration structure. | |
628 | * @param baudrate Baud rate in bits per second, or -1 to retain current setting. | |
629 | * | |
630 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
631 | * |
632 | * @since 0.1.0 | |
9b1502ef ML |
633 | */ |
634 | enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate); | |
635 | ||
636 | /** | |
637 | * Set the data bits for the specified serial port. | |
cf9d365c UH |
638 | * |
639 | * @param port Pointer to port structure. | |
9b1502ef | 640 | * @param bits Number of data bits. |
cf9d365c | 641 | * |
f36c6395 | 642 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
643 | * |
644 | * @since 0.1.0 | |
cf9d365c | 645 | */ |
eb6ed20f | 646 | enum sp_return sp_set_bits(struct sp_port *port, int bits); |
cd5f5281 ML |
647 | |
648 | /** | |
9b1502ef ML |
649 | * Get the data bits from a port configuration. |
650 | * | |
651 | * The user should allocate a variable of type int and pass a pointer to this | |
652 | * to receive the result. | |
653 | * | |
654 | * @param config Pointer to configuration structure. | |
655 | * @param bits_ptr Pointer to variable to store result. | |
656 | * | |
657 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
658 | * |
659 | * @since 0.1.0 | |
9b1502ef ML |
660 | */ |
661 | enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr); | |
662 | ||
663 | /** | |
664 | * Set the data bits in a port configuration. | |
665 | * | |
666 | * @param config Pointer to configuration structure. | |
667 | * @param bits Number of data bits, or -1 to retain current setting. | |
668 | * | |
669 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
670 | * |
671 | * @since 0.1.0 | |
9b1502ef ML |
672 | */ |
673 | enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits); | |
674 | ||
675 | /** | |
676 | * Set the parity setting for the specified serial port. | |
cf9d365c UH |
677 | * |
678 | * @param port Pointer to port structure. | |
9b1502ef | 679 | * @param parity Parity setting. |
cf9d365c | 680 | * |
f36c6395 | 681 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
682 | * |
683 | * @since 0.1.0 | |
cf9d365c | 684 | */ |
eb6ed20f | 685 | enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity); |
cd5f5281 ML |
686 | |
687 | /** | |
9b1502ef ML |
688 | * Get the parity setting from a port configuration. |
689 | * | |
690 | * The user should allocate a variable of type enum sp_parity and pass a pointer to this | |
691 | * to receive the result. | |
692 | * | |
693 | * @param config Pointer to configuration structure. | |
694 | * @param parity_ptr Pointer to variable to store result. | |
695 | * | |
696 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
697 | * |
698 | * @since 0.1.0 | |
9b1502ef ML |
699 | */ |
700 | enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr); | |
701 | ||
702 | /** | |
703 | * Set the parity setting in a port configuration. | |
704 | * | |
705 | * @param config Pointer to configuration structure. | |
706 | * @param parity Parity setting, or -1 to retain current setting. | |
707 | * | |
708 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
709 | * |
710 | * @since 0.1.0 | |
9b1502ef ML |
711 | */ |
712 | enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity); | |
713 | ||
714 | /** | |
715 | * Set the stop bits for the specified serial port. | |
cf9d365c UH |
716 | * |
717 | * @param port Pointer to port structure. | |
9b1502ef | 718 | * @param stopbits Number of stop bits. |
cf9d365c | 719 | * |
f36c6395 | 720 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
721 | * |
722 | * @since 0.1.0 | |
cf9d365c | 723 | */ |
eb6ed20f | 724 | enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits); |
cd5f5281 ML |
725 | |
726 | /** | |
9b1502ef | 727 | * Get the stop bits from a port configuration. |
cf9d365c | 728 | * |
9b1502ef ML |
729 | * The user should allocate a variable of type int and pass a pointer to this |
730 | * to receive the result. | |
cf9d365c | 731 | * |
9b1502ef ML |
732 | * @param config Pointer to configuration structure. |
733 | * @param stopbits_ptr Pointer to variable to store result. | |
cf9d365c | 734 | * |
f36c6395 | 735 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
736 | * |
737 | * @since 0.1.0 | |
cf9d365c | 738 | */ |
9b1502ef ML |
739 | enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr); |
740 | ||
741 | /** | |
742 | * Set the stop bits in a port configuration. | |
743 | * | |
744 | * @param config Pointer to configuration structure. | |
745 | * @param stopbits Number of stop bits, or -1 to retain current setting. | |
746 | * | |
747 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
748 | * |
749 | * @since 0.1.0 | |
9b1502ef ML |
750 | */ |
751 | enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits); | |
18fc2dd1 | 752 | |
cd5f5281 | 753 | /** |
9b1502ef | 754 | * Set the RTS pin behaviour for the specified serial port. |
cf9d365c UH |
755 | * |
756 | * @param port Pointer to port structure. | |
757 | * @param rts RTS pin mode. | |
758 | * | |
f36c6395 | 759 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
760 | * |
761 | * @since 0.1.0 | |
cf9d365c | 762 | */ |
eb6ed20f | 763 | enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts); |
cd5f5281 ML |
764 | |
765 | /** | |
9b1502ef ML |
766 | * Get the RTS pin behaviour from a port configuration. |
767 | * | |
768 | * The user should allocate a variable of type enum sp_rts and pass a pointer to this | |
769 | * to receive the result. | |
770 | * | |
771 | * @param config Pointer to configuration structure. | |
772 | * @param rts_ptr Pointer to variable to store result. | |
773 | * | |
774 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
775 | * |
776 | * @since 0.1.0 | |
9b1502ef ML |
777 | */ |
778 | enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr); | |
779 | ||
780 | /** | |
781 | * Set the RTS pin behaviour in a port configuration. | |
782 | * | |
783 | * @param config Pointer to configuration structure. | |
784 | * @param rts RTS pin mode, or -1 to retain current setting. | |
785 | * | |
786 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
787 | * |
788 | * @since 0.1.0 | |
9b1502ef ML |
789 | */ |
790 | enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts); | |
791 | ||
792 | /** | |
793 | * Set the CTS pin behaviour for the specified serial port. | |
cf9d365c UH |
794 | * |
795 | * @param port Pointer to port structure. | |
796 | * @param cts CTS pin mode. | |
797 | * | |
f36c6395 | 798 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
799 | * |
800 | * @since 0.1.0 | |
cf9d365c | 801 | */ |
eb6ed20f | 802 | enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts); |
cd5f5281 ML |
803 | |
804 | /** | |
9b1502ef ML |
805 | * Get the CTS pin behaviour from a port configuration. |
806 | * | |
807 | * The user should allocate a variable of type enum sp_cts and pass a pointer to this | |
808 | * to receive the result. | |
809 | * | |
810 | * @param config Pointer to configuration structure. | |
811 | * @param cts_ptr Pointer to variable to store result. | |
812 | * | |
813 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
814 | * |
815 | * @since 0.1.0 | |
9b1502ef ML |
816 | */ |
817 | enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr); | |
818 | ||
819 | /** | |
820 | * Set the CTS pin behaviour in a port configuration. | |
821 | * | |
822 | * @param config Pointer to configuration structure. | |
823 | * @param cts CTS pin mode, or -1 to retain current setting. | |
824 | * | |
825 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
826 | * |
827 | * @since 0.1.0 | |
9b1502ef ML |
828 | */ |
829 | enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts); | |
830 | ||
831 | /** | |
832 | * Set the DTR pin behaviour for the specified serial port. | |
cf9d365c UH |
833 | * |
834 | * @param port Pointer to port structure. | |
835 | * @param dtr DTR pin mode. | |
836 | * | |
f36c6395 | 837 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
838 | * |
839 | * @since 0.1.0 | |
cf9d365c | 840 | */ |
eb6ed20f | 841 | enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr); |
cd5f5281 ML |
842 | |
843 | /** | |
9b1502ef ML |
844 | * Get the DTR pin behaviour from a port configuration. |
845 | * | |
846 | * The user should allocate a variable of type enum sp_dtr and pass a pointer to this | |
847 | * to receive the result. | |
848 | * | |
849 | * @param config Pointer to configuration structure. | |
850 | * @param dtr_ptr Pointer to variable to store result. | |
851 | * | |
852 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
853 | * |
854 | * @since 0.1.0 | |
9b1502ef ML |
855 | */ |
856 | enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr); | |
857 | ||
858 | /** | |
859 | * Set the DTR pin behaviour in a port configuration. | |
860 | * | |
861 | * @param config Pointer to configuration structure. | |
862 | * @param dtr DTR pin mode, or -1 to retain current setting. | |
863 | * | |
864 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
865 | * |
866 | * @since 0.1.0 | |
9b1502ef ML |
867 | */ |
868 | enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr); | |
869 | ||
870 | /** | |
871 | * Set the DSR pin behaviour for the specified serial port. | |
cf9d365c UH |
872 | * |
873 | * @param port Pointer to port structure. | |
874 | * @param dsr DSR pin mode. | |
875 | * | |
f36c6395 | 876 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
877 | * |
878 | * @since 0.1.0 | |
cf9d365c | 879 | */ |
eb6ed20f | 880 | enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr); |
cd5f5281 ML |
881 | |
882 | /** | |
9b1502ef ML |
883 | * Get the DSR pin behaviour from a port configuration. |
884 | * | |
885 | * The user should allocate a variable of type enum sp_dsr and pass a pointer to this | |
886 | * to receive the result. | |
887 | * | |
888 | * @param config Pointer to configuration structure. | |
889 | * @param dsr_ptr Pointer to variable to store result. | |
890 | * | |
891 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
892 | * |
893 | * @since 0.1.0 | |
9b1502ef ML |
894 | */ |
895 | enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr); | |
896 | ||
897 | /** | |
898 | * Set the DSR pin behaviour in a port configuration. | |
899 | * | |
900 | * @param config Pointer to configuration structure. | |
901 | * @param dsr DSR pin mode, or -1 to retain current setting. | |
902 | * | |
903 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
904 | * |
905 | * @since 0.1.0 | |
9b1502ef ML |
906 | */ |
907 | enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr); | |
908 | ||
909 | /** | |
910 | * Set the XON/XOFF configuration for the specified serial port. | |
cf9d365c UH |
911 | * |
912 | * @param port Pointer to port structure. | |
9b1502ef | 913 | * @param xon_xoff XON/XOFF mode. |
cf9d365c | 914 | * |
f36c6395 | 915 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
916 | * |
917 | * @since 0.1.0 | |
cf9d365c | 918 | */ |
eb6ed20f | 919 | enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff); |
e96d8bd2 | 920 | |
9b1502ef ML |
921 | /** |
922 | * Get the XON/XOFF configuration from a port configuration. | |
923 | * | |
924 | * The user should allocate a variable of type enum sp_xonxoff and pass a pointer to this | |
925 | * to receive the result. | |
926 | * | |
927 | * @param config Pointer to configuration structure. | |
928 | * @param xon_xoff_ptr Pointer to variable to store result. | |
929 | * | |
930 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
931 | * |
932 | * @since 0.1.0 | |
9b1502ef ML |
933 | */ |
934 | enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr); | |
935 | ||
936 | /** | |
937 | * Set the XON/XOFF configuration in a port configuration. | |
938 | * | |
939 | * @param config Pointer to configuration structure. | |
940 | * @param xon_xoff XON/XOFF mode, or -1 to retain current setting. | |
941 | * | |
942 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
943 | * |
944 | * @since 0.1.0 | |
9b1502ef ML |
945 | */ |
946 | enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff); | |
947 | ||
948 | /** | |
949 | * Set the flow control type in a port configuration. | |
950 | * | |
951 | * This function is a wrapper that sets the RTS, CTS, DTR, DSR and | |
952 | * XON/XOFF settings as necessary for the specified flow control | |
953 | * type. For more fine-grained control of these settings, use their | |
954 | * individual configuration functions. | |
955 | * | |
956 | * @param config Pointer to configuration structure. | |
957 | * @param flowcontrol Flow control setting to use. | |
958 | * | |
959 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
960 | * |
961 | * @since 0.1.0 | |
9b1502ef ML |
962 | */ |
963 | enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol); | |
964 | ||
965 | /** | |
966 | * Set the flow control type for the specified serial port. | |
967 | * | |
968 | * This function is a wrapper that sets the RTS, CTS, DTR, DSR and | |
969 | * XON/XOFF settings as necessary for the specified flow control | |
970 | * type. For more fine-grained control of these settings, use their | |
971 | * individual configuration functions. | |
972 | * | |
973 | * @param port Pointer to port structure. | |
974 | * @param flowcontrol Flow control setting to use. | |
975 | * | |
976 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
977 | * |
978 | * @since 0.1.0 | |
9b1502ef ML |
979 | */ |
980 | enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol); | |
981 | ||
091e75fe | 982 | /** |
cf9d365c | 983 | * @} |
144a598f UH |
984 | * |
985 | * @defgroup Data Data handling | |
986 | * | |
987 | * Reading, writing, and flushing data. | |
988 | * | |
cf9d365c | 989 | * @{ |
144a598f | 990 | */ |
091e75fe ML |
991 | |
992 | /** | |
2007ce5e ML |
993 | * Read bytes from the specified serial port, blocking until complete. |
994 | * | |
b87deb7c ML |
995 | * @warning If your program runs on Unix, defines its own signal handlers, and |
996 | * needs to abort blocking reads when these are called, then you | |
997 | * should not use this function. It repeats system calls that return | |
998 | * with EINTR. To be able to abort a read from a signal handler, you | |
999 | * should implement your own blocking read using sp_nonblocking_read() | |
2007ce5e ML |
1000 | * together with a blocking method that makes sense for your program. |
1001 | * E.g. you can obtain the file descriptor for an open port using | |
1002 | * sp_get_port_handle() and use this to call select() or pselect(), | |
1003 | * with appropriate arrangements to return if a signal is received. | |
cf9d365c | 1004 | * |
e3dcf906 ML |
1005 | * @param port Pointer to port structure. |
1006 | * @param buf Buffer in which to store the bytes read. | |
1007 | * @param count Requested number of bytes to read. | |
1008 | * @param timeout Timeout in milliseconds, or zero to wait indefinitely. | |
1009 | * | |
1010 | * @return The number of bytes read on success, or a negative error code. If | |
1011 | * the number of bytes returned is less than that requested, the | |
1012 | * timeout was reached before the requested number of bytes was | |
1013 | * available. If timeout is zero, the function will always return | |
1014 | * either the requested number of bytes or a negative error code. | |
1652aa86 UH |
1015 | * |
1016 | * @since 0.1.0 | |
e3dcf906 ML |
1017 | */ |
1018 | enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout); | |
1019 | ||
1020 | /** | |
1021 | * Read bytes from the specified serial port, without blocking. | |
cf9d365c UH |
1022 | * |
1023 | * @param port Pointer to port structure. | |
1024 | * @param buf Buffer in which to store the bytes read. | |
1025 | * @param count Maximum number of bytes to read. | |
1026 | * | |
e3dcf906 ML |
1027 | * @return The number of bytes read on success, or a negative error code. The |
1028 | * number of bytes returned may be any number from zero to the maximum | |
1029 | * that was requested. | |
1652aa86 UH |
1030 | * |
1031 | * @since 0.1.0 | |
e3dcf906 ML |
1032 | */ |
1033 | enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count); | |
1034 | ||
1035 | /** | |
1036 | * Write bytes to the specified serial port, blocking until complete. | |
1037 | * | |
1038 | * Note that this function only ensures that the accepted bytes have been | |
1039 | * written to the OS; they may be held in driver or hardware buffers and not | |
1040 | * yet physically transmitted. To check whether all written bytes have actually | |
1041 | * been transmitted, use the sp_output_waiting() function. To wait until all | |
1042 | * written bytes have actually been transmitted, use the sp_drain() function. | |
1043 | * | |
b87deb7c ML |
1044 | * @warning If your program runs on Unix, defines its own signal handlers, and |
1045 | * needs to abort blocking writes when these are called, then you | |
1046 | * should not use this function. It repeats system calls that return | |
1047 | * with EINTR. To be able to abort a write from a signal handler, you | |
1048 | * should implement your own blocking write using sp_nonblocking_write() | |
2007ce5e ML |
1049 | * together with a blocking method that makes sense for your program. |
1050 | * E.g. you can obtain the file descriptor for an open port using | |
1051 | * sp_get_port_handle() and use this to call select() or pselect(), | |
1052 | * with appropriate arrangements to return if a signal is received. | |
1053 | * | |
e3dcf906 ML |
1054 | * @param port Pointer to port structure. |
1055 | * @param buf Buffer containing the bytes to write. | |
1056 | * @param count Requested number of bytes to write. | |
1057 | * @param timeout Timeout in milliseconds, or zero to wait indefinitely. | |
1058 | * | |
85987464 ML |
1059 | * @return The number of bytes written on success, or a negative error code. |
1060 | * If the number of bytes returned is less than that requested, the | |
e3dcf906 | 1061 | * timeout was reached before the requested number of bytes was |
85987464 | 1062 | * written. If timeout is zero, the function will always return |
e3dcf906 ML |
1063 | * either the requested number of bytes or a negative error code. In |
1064 | * the event of an error there is no way to determine how many bytes | |
cfa5af67 | 1065 | * were sent before the error occurred. |
1652aa86 UH |
1066 | * |
1067 | * @since 0.1.0 | |
cf9d365c | 1068 | */ |
e3dcf906 | 1069 | enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout); |
091e75fe ML |
1070 | |
1071 | /** | |
e3dcf906 | 1072 | * Write bytes to the specified serial port, without blocking. |
cf9d365c | 1073 | * |
e3dcf906 ML |
1074 | * Note that this function only ensures that the accepted bytes have been |
1075 | * written to the OS; they may be held in driver or hardware buffers and not | |
1076 | * yet physically transmitted. To check whether all written bytes have actually | |
1077 | * been transmitted, use the sp_output_waiting() function. To wait until all | |
1078 | * written bytes have actually been transmitted, use the sp_drain() function. | |
cf9d365c UH |
1079 | * |
1080 | * @param port Pointer to port structure. | |
1081 | * @param buf Buffer containing the bytes to write. | |
1082 | * @param count Maximum number of bytes to write. | |
1083 | * | |
f36c6395 | 1084 | * @return The number of bytes written on success, or a negative error code. |
e3dcf906 ML |
1085 | * The number of bytes returned may be any number from zero to the |
1086 | * maximum that was requested. | |
1652aa86 UH |
1087 | * |
1088 | * @since 0.1.0 | |
cf9d365c | 1089 | */ |
e3dcf906 | 1090 | enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count); |
091e75fe | 1091 | |
3353c22f ML |
1092 | /** |
1093 | * Gets the number of bytes waiting in the input buffer. | |
1094 | * | |
1095 | * @param port Pointer to port structure. | |
1096 | * | |
1097 | * @return Number of bytes waiting on success, a negative error code otherwise. | |
1652aa86 UH |
1098 | * |
1099 | * @since 0.1.0 | |
3353c22f ML |
1100 | */ |
1101 | enum sp_return sp_input_waiting(struct sp_port *port); | |
1102 | ||
1103 | /** | |
1104 | * Gets the number of bytes waiting in the output buffer. | |
1105 | * | |
1106 | * @param port Pointer to port structure. | |
1107 | * | |
1108 | * @return Number of bytes waiting on success, a negative error code otherwise. | |
1652aa86 UH |
1109 | * |
1110 | * @since 0.1.0 | |
3353c22f ML |
1111 | */ |
1112 | enum sp_return sp_output_waiting(struct sp_port *port); | |
1113 | ||
091e75fe | 1114 | /** |
fd8fd11a ML |
1115 | * Flush serial port buffers. Data in the selected buffer(s) is discarded. |
1116 | * | |
ea34fba8 | 1117 | * @param port Pointer to port structure. |
fd8fd11a | 1118 | * @param buffers Which buffer(s) to flush. |
cf9d365c | 1119 | * |
f36c6395 | 1120 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1121 | * |
1122 | * @since 0.1.0 | |
cf9d365c | 1123 | */ |
fd8fd11a | 1124 | enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers); |
091e75fe | 1125 | |
69a3739c ML |
1126 | /** |
1127 | * Wait for buffered data to be transmitted. | |
1128 | * | |
2c827b21 ML |
1129 | * @warning If your program runs on Unix, defines its own signal handlers, and |
1130 | * needs to abort draining the output buffer when when these are | |
1131 | * called, then you should not use this function. It repeats system | |
1132 | * calls that return with EINTR. To be able to abort a drain from a | |
1133 | * signal handler, you would need to implement your own blocking | |
1134 | * drain by polling the result of sp_output_waiting(). | |
1135 | * | |
3f099f4f ML |
1136 | * @param port Pointer to port structure. |
1137 | * | |
f36c6395 | 1138 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1139 | * |
1140 | * @since 0.1.0 | |
69a3739c ML |
1141 | */ |
1142 | enum sp_return sp_drain(struct sp_port *port); | |
1143 | ||
6f1186aa ML |
1144 | /** |
1145 | * @} | |
144a598f UH |
1146 | * |
1147 | * @defgroup Waiting Waiting | |
1148 | * | |
1149 | * Waiting for events and timeout handling. | |
1150 | * | |
6f1186aa ML |
1151 | * @{ |
1152 | */ | |
1153 | ||
1154 | /** | |
1155 | * Allocate storage for a set of events. | |
1156 | * | |
1157 | * The user should allocate a variable of type struct sp_event_set *, | |
1158 | * then pass a pointer to this variable to receive the result. | |
1159 | * | |
1160 | * The result should be freed after use by calling sp_free_event_set(). | |
1161 | * | |
1162 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1163 | * |
1164 | * @since 0.1.0 | |
6f1186aa ML |
1165 | */ |
1166 | enum sp_return sp_new_event_set(struct sp_event_set **result_ptr); | |
1167 | ||
1168 | /** | |
1169 | * Add events to a struct sp_event_set for a given port. | |
1170 | * | |
1171 | * The port must first be opened by calling sp_open() using the same port | |
1172 | * structure. | |
1173 | * | |
1174 | * After the port is closed or the port structure freed, the results may | |
1175 | * no longer be valid. | |
1176 | * | |
1177 | * @param event_set Event set to update. | |
1178 | * @param port Pointer to port structure. | |
1179 | * @param mask Bitmask of events to be waited for. | |
1180 | * | |
1181 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1182 | * |
1183 | * @since 0.1.0 | |
6f1186aa ML |
1184 | */ |
1185 | enum sp_return sp_add_port_events(struct sp_event_set *event_set, | |
1186 | const struct sp_port *port, enum sp_event mask); | |
1187 | ||
1188 | /** | |
1189 | * Wait for any of a set of events to occur. | |
1190 | * | |
deaf0a63 | 1191 | * @param event_set Event set to wait on. |
6f1186aa ML |
1192 | * @param timeout Timeout in milliseconds, or zero to wait indefinitely. |
1193 | * | |
1194 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1195 | * |
1196 | * @since 0.1.0 | |
6f1186aa ML |
1197 | */ |
1198 | enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout); | |
1199 | ||
1200 | /** | |
1201 | * Free a structure allocated by sp_new_event_set(). | |
1652aa86 UH |
1202 | * |
1203 | * @since 0.1.0 | |
6f1186aa ML |
1204 | */ |
1205 | void sp_free_event_set(struct sp_event_set *event_set); | |
1206 | ||
90cc3ee6 ML |
1207 | /** |
1208 | * @} | |
144a598f UH |
1209 | * |
1210 | * @defgroup Signals Signals | |
1211 | * | |
1212 | * Port signalling operations. | |
1213 | * | |
90cc3ee6 ML |
1214 | * @{ |
1215 | */ | |
1216 | ||
8cf7c697 ML |
1217 | /** |
1218 | * Gets the status of the control signals for the specified port. | |
1219 | * | |
1220 | * The user should allocate a variable of type "enum sp_signal" and pass a | |
1221 | * pointer to this variable to receive the result. The result is a bitmask | |
1222 | * in which individual signals can be checked by bitwise OR with values of | |
1223 | * the sp_signal enum. | |
1224 | * | |
1225 | * @param port Pointer to port structure. | |
f3ccf157 | 1226 | * @param signal_mask Pointer to variable to receive result. |
8cf7c697 | 1227 | * |
f36c6395 | 1228 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1229 | * |
1230 | * @since 0.1.0 | |
8cf7c697 | 1231 | */ |
f3ccf157 | 1232 | enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signal_mask); |
8cf7c697 | 1233 | |
90cc3ee6 ML |
1234 | /** |
1235 | * Put the port transmit line into the break state. | |
1236 | * | |
3f099f4f ML |
1237 | * @param port Pointer to port structure. |
1238 | * | |
f36c6395 | 1239 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1240 | * |
1241 | * @since 0.1.0 | |
90cc3ee6 ML |
1242 | */ |
1243 | enum sp_return sp_start_break(struct sp_port *port); | |
1244 | ||
1245 | /** | |
1246 | * Take the port transmit line out of the break state. | |
1247 | * | |
3f099f4f ML |
1248 | * @param port Pointer to port structure. |
1249 | * | |
f36c6395 | 1250 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1251 | * |
1252 | * @since 0.1.0 | |
90cc3ee6 ML |
1253 | */ |
1254 | enum sp_return sp_end_break(struct sp_port *port); | |
1255 | ||
091e75fe | 1256 | /** |
cf9d365c | 1257 | * @} |
144a598f UH |
1258 | * |
1259 | * @defgroup Errors Errors | |
1260 | * | |
1261 | * Obtaining error information. | |
1262 | * | |
cf9d365c | 1263 | * @{ |
144a598f | 1264 | */ |
091e75fe | 1265 | |
cd5f5281 | 1266 | /** |
cf9d365c UH |
1267 | * Get the error code for a failed operation. |
1268 | * | |
1269 | * In order to obtain the correct result, this function should be called | |
1270 | * straight after the failure, before executing any other system operations. | |
1271 | * | |
1272 | * @return The system's numeric code for the error that caused the last | |
1273 | * operation to fail. | |
1652aa86 UH |
1274 | * |
1275 | * @since 0.1.0 | |
cf9d365c | 1276 | */ |
74510d4b | 1277 | int sp_last_error_code(void); |
cd5f5281 ML |
1278 | |
1279 | /** | |
cf9d365c UH |
1280 | * Get the error message for a failed operation. |
1281 | * | |
1282 | * In order to obtain the correct result, this function should be called | |
1283 | * straight after the failure, before executing other system operations. | |
1284 | * | |
1285 | * @return The system's message for the error that caused the last | |
1286 | * operation to fail. This string may be allocated by the function, | |
1287 | * and should be freed after use by calling sp_free_error_message(). | |
1652aa86 UH |
1288 | * |
1289 | * @since 0.1.0 | |
cf9d365c | 1290 | */ |
74510d4b | 1291 | char *sp_last_error_message(void); |
cd5f5281 ML |
1292 | |
1293 | /** | |
cf9d365c | 1294 | * Free an error message returned by sp_last_error_message(). |
1652aa86 UH |
1295 | * |
1296 | * @since 0.1.0 | |
cf9d365c | 1297 | */ |
74510d4b | 1298 | void sp_free_error_message(char *message); |
e8ffaee9 | 1299 | |
863b35e6 ML |
1300 | /** |
1301 | * Set the handler function for library debugging messages. | |
1302 | * | |
1303 | * Debugging messages are generated by the library during each operation, | |
1304 | * to help in diagnosing problems. The handler will be called for each | |
1305 | * message. The handler can be set to NULL to ignore all debug messages. | |
1306 | * | |
1307 | * The handler function should accept a format string and variable length | |
1308 | * argument list, in the same manner as e.g. printf(). | |
1309 | * | |
1310 | * The default handler is sp_default_debug_handler(). | |
1652aa86 UH |
1311 | * |
1312 | * @since 0.1.0 | |
863b35e6 ML |
1313 | */ |
1314 | void sp_set_debug_handler(void (*handler)(const char *format, ...)); | |
1315 | ||
1316 | /** | |
1317 | * Default handler function for library debugging messages. | |
1318 | * | |
1319 | * This function prints debug messages to the standard error stream if the | |
1320 | * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are | |
1321 | * ignored. | |
1652aa86 UH |
1322 | * |
1323 | * @since 0.1.0 | |
863b35e6 ML |
1324 | */ |
1325 | void sp_default_debug_handler(const char *format, ...); | |
1326 | ||
cf9d365c | 1327 | /** @} */ |
091e75fe | 1328 | |
524b0e14 | 1329 | /** |
144a598f UH |
1330 | * @defgroup Versions Versions |
1331 | * | |
1332 | * Version number querying functions, definitions, and macros. | |
524b0e14 UH |
1333 | * |
1334 | * This set of API calls returns two different version numbers related | |
1335 | * to libserialport. The "package version" is the release version number of the | |
1336 | * libserialport tarball in the usual "major.minor.micro" format, e.g. "0.1.0". | |
1337 | * | |
1338 | * The "library version" is independent of that; it is the libtool version | |
1339 | * number in the "current:revision:age" format, e.g. "2:0:0". | |
1340 | * See http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning for details. | |
1341 | * | |
1342 | * Both version numbers (and/or individual components of them) can be | |
1343 | * retrieved via the API calls at runtime, and/or they can be checked at | |
1344 | * compile/preprocessor time using the respective macros. | |
1345 | * | |
1346 | * @{ | |
1347 | */ | |
1348 | ||
1349 | /* | |
1350 | * Package version macros (can be used for conditional compilation). | |
1351 | */ | |
1352 | ||
1353 | /** The libserialport package 'major' version number. */ | |
1354 | #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@ | |
1355 | ||
1356 | /** The libserialport package 'minor' version number. */ | |
1357 | #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@ | |
1358 | ||
1359 | /** The libserialport package 'micro' version number. */ | |
1360 | #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@ | |
1361 | ||
1362 | /** The libserialport package version ("major.minor.micro") as string. */ | |
1363 | #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@" | |
1364 | ||
1365 | /* | |
1366 | * Library/libtool version macros (can be used for conditional compilation). | |
1367 | */ | |
1368 | ||
1369 | /** The libserialport libtool 'current' version number. */ | |
1370 | #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@ | |
1371 | ||
1372 | /** The libserialport libtool 'revision' version number. */ | |
1373 | #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@ | |
1374 | ||
1375 | /** The libserialport libtool 'age' version number. */ | |
1376 | #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@ | |
1377 | ||
1378 | /** The libserialport libtool version ("current:revision:age") as string. */ | |
1379 | #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@" | |
1380 | ||
1381 | /** | |
1382 | * Get the major libserialport package version number. | |
1383 | * | |
1384 | * @return The major package version number. | |
1385 | * | |
1386 | * @since 0.1.0 | |
1387 | */ | |
1388 | int sp_get_major_package_version(void); | |
1389 | ||
1390 | /** | |
1391 | * Get the minor libserialport package version number. | |
1392 | * | |
1393 | * @return The minor package version number. | |
1394 | * | |
1395 | * @since 0.1.0 | |
1396 | */ | |
1397 | int sp_get_minor_package_version(void); | |
1398 | ||
1399 | /** | |
1400 | * Get the micro libserialport package version number. | |
1401 | * | |
1402 | * @return The micro package version number. | |
1403 | * | |
1404 | * @since 0.1.0 | |
1405 | */ | |
1406 | int sp_get_micro_package_version(void); | |
1407 | ||
1408 | /** | |
1409 | * Get the libserialport package version number as a string. | |
1410 | * | |
1411 | * @return The package version number string. The returned string is | |
1412 | * static and thus should NOT be free'd by the caller. | |
1413 | * | |
1414 | * @since 0.1.0 | |
1415 | */ | |
1416 | const char *sp_get_package_version_string(void); | |
1417 | ||
1418 | /** | |
1419 | * Get the "current" part of the libserialport library version number. | |
1420 | * | |
1421 | * @return The "current" library version number. | |
1422 | * | |
1423 | * @since 0.1.0 | |
1424 | */ | |
1425 | int sp_get_current_lib_version(void); | |
1426 | ||
1427 | /** | |
1428 | * Get the "revision" part of the libserialport library version number. | |
1429 | * | |
1430 | * @return The "revision" library version number. | |
1431 | * | |
1432 | * @since 0.1.0 | |
1433 | */ | |
1434 | int sp_get_revision_lib_version(void); | |
1435 | ||
1436 | /** | |
1437 | * Get the "age" part of the libserialport library version number. | |
1438 | * | |
1439 | * @return The "age" library version number. | |
1440 | * | |
1441 | * @since 0.1.0 | |
1442 | */ | |
1443 | int sp_get_age_lib_version(void); | |
1444 | ||
1445 | /** | |
1446 | * Get the libserialport library version number as a string. | |
1447 | * | |
1448 | * @return The library version number string. The returned string is | |
1449 | * static and thus should NOT be free'd by the caller. | |
1450 | * | |
1451 | * @since 0.1.0 | |
1452 | */ | |
1453 | const char *sp_get_lib_version_string(void); | |
1454 | ||
1455 | /** @} */ | |
1456 | ||
5ef8a1ed UH |
1457 | #ifdef __cplusplus |
1458 | } | |
1459 | #endif | |
1460 | ||
8645feda | 1461 | #endif |