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