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