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