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