]>
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 | #ifdef _WIN32 | |
89 | #include <windows.h> | |
90 | #endif | |
91 | ||
92 | /* Package version macros (e.g. for conditional compilation). */ | |
93 | #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@ | |
94 | #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@ | |
95 | #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@ | |
96 | #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@" | |
97 | ||
98 | /* Library/libtool version macros (e.g. for conditional compilation). */ | |
99 | #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@ | |
100 | #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@ | |
101 | #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@ | |
102 | #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@" | |
103 | ||
104 | /** Return values. */ | |
105 | enum sp_return { | |
106 | /** Operation completed successfully. */ | |
107 | SP_OK = 0, | |
108 | /** Invalid arguments were passed to the function. */ | |
109 | SP_ERR_ARG = -1, | |
110 | /** A system error occured while executing the operation. */ | |
111 | SP_ERR_FAIL = -2, | |
112 | /** A memory allocation failed while executing the operation. */ | |
113 | SP_ERR_MEM = -3, | |
114 | /** The requested operation is not supported by this system or device. */ | |
115 | SP_ERR_SUPP = -4, | |
116 | }; | |
117 | ||
118 | /** Port access modes. */ | |
119 | enum sp_mode { | |
120 | /** Open port for read access. */ | |
121 | SP_MODE_READ = 1, | |
122 | /** Open port for write access. */ | |
123 | SP_MODE_WRITE = 2, | |
124 | }; | |
125 | ||
126 | /** Port events. */ | |
127 | enum sp_event { | |
128 | /* Data received and ready to read. */ | |
129 | SP_EVENT_RX_READY = 1, | |
130 | /* Ready to transmit new data. */ | |
131 | SP_EVENT_TX_READY = 2, | |
132 | /* Error occured. */ | |
133 | SP_EVENT_ERROR = 4, | |
134 | }; | |
135 | ||
136 | /** Buffer selection. */ | |
137 | enum sp_buffer { | |
138 | /** Input buffer. */ | |
139 | SP_BUF_INPUT = 1, | |
140 | /** Output buffer. */ | |
141 | SP_BUF_OUTPUT = 2, | |
142 | /** Both buffers. */ | |
143 | SP_BUF_BOTH = 3, | |
144 | }; | |
145 | ||
146 | /** Parity settings. */ | |
147 | enum sp_parity { | |
148 | /** Special value to indicate setting should be left alone. */ | |
149 | SP_PARITY_INVALID = -1, | |
150 | /** No parity. */ | |
151 | SP_PARITY_NONE = 0, | |
152 | /** Odd parity. */ | |
153 | SP_PARITY_ODD = 1, | |
154 | /** Even parity. */ | |
155 | SP_PARITY_EVEN = 2, | |
156 | /** Mark parity. */ | |
157 | SP_PARITY_MARK = 3, | |
158 | /** Space parity. */ | |
159 | SP_PARITY_SPACE = 4, | |
160 | }; | |
161 | ||
162 | /** RTS pin behaviour. */ | |
163 | enum sp_rts { | |
164 | /** Special value to indicate setting should be left alone. */ | |
165 | SP_RTS_INVALID = -1, | |
166 | /** RTS off. */ | |
167 | SP_RTS_OFF = 0, | |
168 | /** RTS on. */ | |
169 | SP_RTS_ON = 1, | |
170 | /** RTS used for flow control. */ | |
171 | SP_RTS_FLOW_CONTROL = 2, | |
172 | }; | |
173 | ||
174 | /** CTS pin behaviour. */ | |
175 | enum sp_cts { | |
176 | /** Special value to indicate setting should be left alone. */ | |
177 | SP_CTS_INVALID = -1, | |
178 | /** CTS ignored. */ | |
179 | SP_CTS_IGNORE = 0, | |
180 | /** CTS used for flow control. */ | |
181 | SP_CTS_FLOW_CONTROL = 1, | |
182 | }; | |
183 | ||
184 | /** DTR pin behaviour. */ | |
185 | enum sp_dtr { | |
186 | /** Special value to indicate setting should be left alone. */ | |
187 | SP_DTR_INVALID = -1, | |
188 | /** DTR off. */ | |
189 | SP_DTR_OFF = 0, | |
190 | /** DTR on. */ | |
191 | SP_DTR_ON = 1, | |
192 | /** DTR used for flow control. */ | |
193 | SP_DTR_FLOW_CONTROL = 2, | |
194 | }; | |
195 | ||
196 | /** DSR pin behaviour. */ | |
197 | enum sp_dsr { | |
198 | /** Special value to indicate setting should be left alone. */ | |
199 | SP_DSR_INVALID = -1, | |
200 | /** DSR ignored. */ | |
201 | SP_DSR_IGNORE = 0, | |
202 | /** DSR used for flow control. */ | |
203 | SP_DSR_FLOW_CONTROL = 1, | |
204 | }; | |
205 | ||
206 | /** XON/XOFF flow control behaviour. */ | |
207 | enum sp_xonxoff { | |
208 | /** Special value to indicate setting should be left alone. */ | |
209 | SP_XONXOFF_INVALID = -1, | |
210 | /** XON/XOFF disabled. */ | |
211 | SP_XONXOFF_DISABLED = 0, | |
212 | /** XON/XOFF enabled for input only. */ | |
213 | SP_XONXOFF_IN = 1, | |
214 | /** XON/XOFF enabled for output only. */ | |
215 | SP_XONXOFF_OUT = 2, | |
216 | /** XON/XOFF enabled for input and output. */ | |
217 | SP_XONXOFF_INOUT = 3, | |
218 | }; | |
219 | ||
220 | /** Standard flow control combinations. */ | |
221 | enum sp_flowcontrol { | |
222 | /** No flow control. */ | |
223 | SP_FLOWCONTROL_NONE = 0, | |
224 | /** Software flow control using XON/XOFF characters. */ | |
225 | SP_FLOWCONTROL_XONXOFF = 1, | |
226 | /** Hardware flow control using RTS/CTS signals. */ | |
227 | SP_FLOWCONTROL_RTSCTS = 2, | |
228 | /** Hardware flow control using DTR/DSR signals. */ | |
229 | SP_FLOWCONTROL_DTRDSR = 3, | |
230 | }; | |
231 | ||
232 | /** Input signals. */ | |
233 | enum sp_signal { | |
234 | /** Clear to send. */ | |
235 | SP_SIG_CTS = 1, | |
236 | /** Data set ready. */ | |
237 | SP_SIG_DSR = 2, | |
238 | /** Data carrier detect. */ | |
239 | SP_SIG_DCD = 4, | |
240 | /** Ring indicator. */ | |
241 | SP_SIG_RI = 8, | |
242 | }; | |
243 | ||
244 | /** | |
245 | * @struct sp_port | |
246 | * An opaque structure representing a serial port. | |
247 | */ | |
248 | struct sp_port; | |
249 | ||
250 | /** | |
251 | * @struct sp_port_config | |
252 | * An opaque structure representing the configuration for a serial port. | |
253 | */ | |
254 | struct sp_port_config; | |
255 | ||
256 | /** | |
257 | * @struct sp_event_set | |
258 | * A set of handles to wait on for events. | |
259 | */ | |
260 | struct sp_event_set { | |
261 | /** Array of OS-specific handles. */ | |
262 | void *handles; | |
263 | /** Array of bitmasks indicating which events apply for each handle. */ | |
264 | enum sp_event *masks; | |
265 | /** Number of handles. */ | |
266 | unsigned int count; | |
267 | }; | |
268 | ||
269 | /** | |
270 | @defgroup Enumeration Port enumeration | |
271 | @{ | |
272 | */ | |
273 | ||
274 | /** | |
275 | * Obtain a pointer to a new sp_port structure representing the named port. | |
276 | * | |
277 | * The user should allocate a variable of type "struct sp_port *" and pass a | |
278 | * pointer to this to receive the result. | |
279 | * | |
280 | * The result should be freed after use by calling sp_free_port(). | |
281 | * | |
282 | * If any error is returned, the variable pointed to by port_ptr will be set | |
283 | * to NULL. Otherwise, it will be set to point to the newly allocated port. | |
284 | * | |
285 | * @return SP_OK upon success, a negative error code otherwise. | |
286 | */ | |
287 | enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr); | |
288 | ||
289 | /** | |
290 | * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port(). | |
291 | */ | |
292 | void sp_free_port(struct sp_port *port); | |
293 | ||
294 | /** | |
295 | * List the serial ports available on the system. | |
296 | * | |
297 | * The result obtained is an array of pointers to sp_port structures, | |
298 | * terminated by a NULL. The user should allocate a variable of type | |
299 | * "struct sp_port **" and pass a pointer to this to receive the result. | |
300 | * | |
301 | * The result should be freed after use by calling sp_free_port_list(). | |
302 | * If a port from the list is to be used after freeing the list, it must be | |
303 | * copied first using sp_copy_port(). | |
304 | * | |
305 | * If any error is returned, the variable pointed to by list_ptr will be set | |
306 | * to NULL. Otherwise, it will be set to point to the newly allocated array. | |
307 | * | |
308 | * @return SP_OK upon success, a negative error code otherwise. | |
309 | */ | |
310 | enum sp_return sp_list_ports(struct sp_port ***list_ptr); | |
311 | ||
312 | /** | |
313 | * Make a new copy of a sp_port structure. | |
314 | * | |
315 | * The user should allocate a variable of type "struct sp_port *" and pass a | |
316 | * pointer to this to receive the result. | |
317 | * | |
318 | * The copy should be freed after use by calling sp_free_port(). | |
319 | * | |
320 | * If any error is returned, the variable pointed to by copy_ptr will be set | |
321 | * to NULL. Otherwise, it will be set to point to the newly allocated copy. | |
322 | * | |
323 | * @return SP_OK upon success, a negative error code otherwise. | |
324 | */ | |
325 | enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr); | |
326 | ||
327 | /** | |
328 | * Free a port list obtained from sp_list_ports(). | |
329 | * | |
330 | * This will also free all the sp_port structures referred to from the list; | |
331 | * any that are to be retained must be copied first using sp_copy_port(). | |
332 | */ | |
333 | void sp_free_port_list(struct sp_port **ports); | |
334 | ||
335 | /** | |
336 | * @} | |
337 | * @defgroup Ports Opening, closing and querying ports | |
338 | * @{ | |
339 | */ | |
340 | ||
341 | /** | |
342 | * Open the specified serial port. | |
343 | * | |
344 | * @param port Pointer to port structure. | |
345 | * @param flags Flags to use when opening the serial port. | |
346 | * | |
347 | * @return SP_OK upon success, a negative error code otherwise. | |
348 | */ | |
349 | enum sp_return sp_open(struct sp_port *port, enum sp_mode flags); | |
350 | ||
351 | /** | |
352 | * Close the specified serial port. | |
353 | * | |
354 | * @return SP_OK upon success, a negative error code otherwise. | |
355 | */ | |
356 | enum sp_return sp_close(struct sp_port *port); | |
357 | ||
358 | /** | |
359 | * Get the name of a port. | |
360 | * | |
361 | * The name returned is whatever is normally used to refer to a port on the | |
362 | * current operating system; e.g. for Windows it will usually be a "COMn" | |
363 | * device name, and for Unix it will be a device path beginning with "/dev/". | |
364 | * | |
365 | * @param port Pointer to port structure. | |
366 | * | |
367 | * @return The port name, or NULL if an invalid port is passed. The name | |
368 | * string is part of the port structure and may not be used after the | |
369 | * port structure has been freed. | |
370 | */ | |
371 | char *sp_get_port_name(const struct sp_port *port); | |
372 | ||
373 | /** | |
374 | * Get the operating system handle for a port. | |
375 | * | |
376 | * The type of the handle depends on the operating system. On Unix based | |
377 | * systems, the handle is a file descriptor of type "int". On Windows, the | |
378 | * handle is of type "HANDLE". The user should allocate a variable of the | |
379 | * appropriate type and pass a pointer to this to receive the result. | |
380 | * | |
381 | * To obtain a valid handle, the port must first be opened by calling | |
382 | * sp_open() using the same port structure. | |
383 | * | |
384 | * After the port is closed or the port structure freed, the handle may | |
385 | * no longer be valid. | |
386 | * | |
387 | * @warning This feature is provided so that programs may make use of | |
388 | * OS-specific functionality where desired. Doing so obviously | |
389 | * comes at a cost in portability. It also cannot be guaranteed | |
390 | * that direct usage of the OS handle will not conflict with the | |
391 | * library's own usage of the port. Be careful. | |
392 | * | |
393 | * @return SP_OK upon success, a negative error code otherwise. | |
394 | */ | |
395 | enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr); | |
396 | ||
397 | /** | |
398 | * @} | |
399 | * @defgroup Configuration Setting port parameters | |
400 | * @{ | |
401 | */ | |
402 | ||
403 | /** | |
404 | * Allocate a port configuration structure. | |
405 | * | |
406 | * The user should allocate a variable of type "struct sp_config *" and pass a | |
407 | * pointer to this to receive the result. The variable will be updated to | |
408 | * point to the new configuration structure. The structure is opaque and must | |
409 | * be accessed via the functions provided. | |
410 | * | |
411 | * All parameters in the structure will be initialised to special values which | |
412 | * are ignored by sp_set_config(). | |
413 | * | |
414 | * The structure should be freed after use by calling sp_free_config(). | |
415 | * | |
416 | * @param config_ptr Pointer to variable to receive result. | |
417 | * | |
418 | * @return SP_OK upon success, a negative error code otherwise. | |
419 | */ | |
420 | enum sp_return sp_new_config(struct sp_port_config **config_ptr); | |
421 | ||
422 | /** | |
423 | * Free a port configuration structure. | |
424 | * | |
425 | * @param config Pointer to configuration structure. | |
426 | */ | |
427 | void sp_free_config(struct sp_port_config *config); | |
428 | ||
429 | /** | |
430 | * Get the current configuration of the specified serial port. | |
431 | * | |
432 | * The user should allocate a configuration structure using sp_new_config() | |
433 | * and pass this as the config parameter. The configuration structure will | |
434 | * be updated with the port configuration. | |
435 | * | |
436 | * Any parameters that are configured with settings not recognised or | |
437 | * supported by libserialport, will be set to special values that are | |
438 | * ignored by sp_set_config(). | |
439 | * | |
440 | * @return SP_OK upon success, a negative error code otherwise. | |
441 | */ | |
442 | enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config); | |
443 | ||
444 | /** | |
445 | * Set the configuration for the specified serial port. | |
446 | * | |
447 | * For each parameter in the configuration, there is a special value (usually | |
448 | * -1, but see the documentation for each field). These values will be ignored | |
449 | * and the corresponding setting left unchanged on the port. | |
450 | * | |
451 | * @return SP_OK upon success, a negative error code otherwise. | |
452 | */ | |
453 | enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config); | |
454 | ||
455 | /** | |
456 | * Set the baud rate for the specified serial port. | |
457 | * | |
458 | * @param port Pointer to port structure. | |
459 | * @param baudrate Baud rate in bits per second. | |
460 | * | |
461 | * @return SP_OK upon success, a negative error code otherwise. | |
462 | */ | |
463 | enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate); | |
464 | ||
465 | /** | |
466 | * Get the baud rate from a port configuration. | |
467 | * | |
468 | * The user should allocate a variable of type int and pass a pointer to this | |
469 | * to receive the result. | |
470 | * | |
471 | * @param config Pointer to configuration structure. | |
472 | * @param baudrate_ptr Pointer to variable to store result. | |
473 | * | |
474 | * @return SP_OK upon success, a negative error code otherwise. | |
475 | */ | |
476 | enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr); | |
477 | ||
478 | /** | |
479 | * Set the baud rate in a port configuration. | |
480 | * | |
481 | * @param config Pointer to configuration structure. | |
482 | * @param baudrate Baud rate in bits per second, or -1 to retain current setting. | |
483 | * | |
484 | * @return SP_OK upon success, a negative error code otherwise. | |
485 | */ | |
486 | enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate); | |
487 | ||
488 | /** | |
489 | * Set the data bits for the specified serial port. | |
490 | * | |
491 | * @param port Pointer to port structure. | |
492 | * @param bits Number of data bits. | |
493 | * | |
494 | * @return SP_OK upon success, a negative error code otherwise. | |
495 | */ | |
496 | enum sp_return sp_set_bits(struct sp_port *port, int bits); | |
497 | ||
498 | /** | |
499 | * Get the data bits from a port configuration. | |
500 | * | |
501 | * The user should allocate a variable of type int and pass a pointer to this | |
502 | * to receive the result. | |
503 | * | |
504 | * @param config Pointer to configuration structure. | |
505 | * @param bits_ptr Pointer to variable to store result. | |
506 | * | |
507 | * @return SP_OK upon success, a negative error code otherwise. | |
508 | */ | |
509 | enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr); | |
510 | ||
511 | /** | |
512 | * Set the data bits in a port configuration. | |
513 | * | |
514 | * @param config Pointer to configuration structure. | |
515 | * @param bits Number of data bits, or -1 to retain current setting. | |
516 | * | |
517 | * @return SP_OK upon success, a negative error code otherwise. | |
518 | */ | |
519 | enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits); | |
520 | ||
521 | /** | |
522 | * Set the parity setting for the specified serial port. | |
523 | * | |
524 | * @param port Pointer to port structure. | |
525 | * @param parity Parity setting. | |
526 | * | |
527 | * @return SP_OK upon success, a negative error code otherwise. | |
528 | */ | |
529 | enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity); | |
530 | ||
531 | /** | |
532 | * Get the parity setting from a port configuration. | |
533 | * | |
534 | * The user should allocate a variable of type enum sp_parity and pass a pointer to this | |
535 | * to receive the result. | |
536 | * | |
537 | * @param config Pointer to configuration structure. | |
538 | * @param parity_ptr Pointer to variable to store result. | |
539 | * | |
540 | * @return SP_OK upon success, a negative error code otherwise. | |
541 | */ | |
542 | enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr); | |
543 | ||
544 | /** | |
545 | * Set the parity setting in a port configuration. | |
546 | * | |
547 | * @param config Pointer to configuration structure. | |
548 | * @param parity Parity setting, or -1 to retain current setting. | |
549 | * | |
550 | * @return SP_OK upon success, a negative error code otherwise. | |
551 | */ | |
552 | enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity); | |
553 | ||
554 | /** | |
555 | * Set the stop bits for the specified serial port. | |
556 | * | |
557 | * @param port Pointer to port structure. | |
558 | * @param stopbits Number of stop bits. | |
559 | * | |
560 | * @return SP_OK upon success, a negative error code otherwise. | |
561 | */ | |
562 | enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits); | |
563 | ||
564 | /** | |
565 | * Get the stop bits from a port configuration. | |
566 | * | |
567 | * The user should allocate a variable of type int and pass a pointer to this | |
568 | * to receive the result. | |
569 | * | |
570 | * @param config Pointer to configuration structure. | |
571 | * @param stopbits_ptr Pointer to variable to store result. | |
572 | * | |
573 | * @return SP_OK upon success, a negative error code otherwise. | |
574 | */ | |
575 | enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr); | |
576 | ||
577 | /** | |
578 | * Set the stop bits in a port configuration. | |
579 | * | |
580 | * @param config Pointer to configuration structure. | |
581 | * @param stopbits Number of stop bits, or -1 to retain current setting. | |
582 | * | |
583 | * @return SP_OK upon success, a negative error code otherwise. | |
584 | */ | |
585 | enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits); | |
586 | ||
587 | /** | |
588 | * Set the RTS pin behaviour for the specified serial port. | |
589 | * | |
590 | * @param port Pointer to port structure. | |
591 | * @param rts RTS pin mode. | |
592 | * | |
593 | * @return SP_OK upon success, a negative error code otherwise. | |
594 | */ | |
595 | enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts); | |
596 | ||
597 | /** | |
598 | * Get the RTS pin behaviour from a port configuration. | |
599 | * | |
600 | * The user should allocate a variable of type enum sp_rts and pass a pointer to this | |
601 | * to receive the result. | |
602 | * | |
603 | * @param config Pointer to configuration structure. | |
604 | * @param rts_ptr Pointer to variable to store result. | |
605 | * | |
606 | * @return SP_OK upon success, a negative error code otherwise. | |
607 | */ | |
608 | enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr); | |
609 | ||
610 | /** | |
611 | * Set the RTS pin behaviour in a port configuration. | |
612 | * | |
613 | * @param config Pointer to configuration structure. | |
614 | * @param rts RTS pin mode, or -1 to retain current setting. | |
615 | * | |
616 | * @return SP_OK upon success, a negative error code otherwise. | |
617 | */ | |
618 | enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts); | |
619 | ||
620 | /** | |
621 | * Set the CTS pin behaviour for the specified serial port. | |
622 | * | |
623 | * @param port Pointer to port structure. | |
624 | * @param cts CTS pin mode. | |
625 | * | |
626 | * @return SP_OK upon success, a negative error code otherwise. | |
627 | */ | |
628 | enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts); | |
629 | ||
630 | /** | |
631 | * Get the CTS pin behaviour from a port configuration. | |
632 | * | |
633 | * The user should allocate a variable of type enum sp_cts and pass a pointer to this | |
634 | * to receive the result. | |
635 | * | |
636 | * @param config Pointer to configuration structure. | |
637 | * @param cts_ptr Pointer to variable to store result. | |
638 | * | |
639 | * @return SP_OK upon success, a negative error code otherwise. | |
640 | */ | |
641 | enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr); | |
642 | ||
643 | /** | |
644 | * Set the CTS pin behaviour in a port configuration. | |
645 | * | |
646 | * @param config Pointer to configuration structure. | |
647 | * @param cts CTS pin mode, or -1 to retain current setting. | |
648 | * | |
649 | * @return SP_OK upon success, a negative error code otherwise. | |
650 | */ | |
651 | enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts); | |
652 | ||
653 | /** | |
654 | * Set the DTR pin behaviour for the specified serial port. | |
655 | * | |
656 | * @param port Pointer to port structure. | |
657 | * @param dtr DTR pin mode. | |
658 | * | |
659 | * @return SP_OK upon success, a negative error code otherwise. | |
660 | */ | |
661 | enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr); | |
662 | ||
663 | /** | |
664 | * Get the DTR pin behaviour from a port configuration. | |
665 | * | |
666 | * The user should allocate a variable of type enum sp_dtr and pass a pointer to this | |
667 | * to receive the result. | |
668 | * | |
669 | * @param config Pointer to configuration structure. | |
670 | * @param dtr_ptr Pointer to variable to store result. | |
671 | * | |
672 | * @return SP_OK upon success, a negative error code otherwise. | |
673 | */ | |
674 | enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr); | |
675 | ||
676 | /** | |
677 | * Set the DTR pin behaviour in a port configuration. | |
678 | * | |
679 | * @param config Pointer to configuration structure. | |
680 | * @param dtr DTR pin mode, or -1 to retain current setting. | |
681 | * | |
682 | * @return SP_OK upon success, a negative error code otherwise. | |
683 | */ | |
684 | enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr); | |
685 | ||
686 | /** | |
687 | * Set the DSR pin behaviour for the specified serial port. | |
688 | * | |
689 | * @param port Pointer to port structure. | |
690 | * @param dsr DSR pin mode. | |
691 | * | |
692 | * @return SP_OK upon success, a negative error code otherwise. | |
693 | */ | |
694 | enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr); | |
695 | ||
696 | /** | |
697 | * Get the DSR pin behaviour from a port configuration. | |
698 | * | |
699 | * The user should allocate a variable of type enum sp_dsr and pass a pointer to this | |
700 | * to receive the result. | |
701 | * | |
702 | * @param config Pointer to configuration structure. | |
703 | * @param dsr_ptr Pointer to variable to store result. | |
704 | * | |
705 | * @return SP_OK upon success, a negative error code otherwise. | |
706 | */ | |
707 | enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr); | |
708 | ||
709 | /** | |
710 | * Set the DSR pin behaviour in a port configuration. | |
711 | * | |
712 | * @param config Pointer to configuration structure. | |
713 | * @param dsr DSR pin mode, or -1 to retain current setting. | |
714 | * | |
715 | * @return SP_OK upon success, a negative error code otherwise. | |
716 | */ | |
717 | enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr); | |
718 | ||
719 | /** | |
720 | * Set the XON/XOFF configuration for the specified serial port. | |
721 | * | |
722 | * @param port Pointer to port structure. | |
723 | * @param xon_xoff XON/XOFF mode. | |
724 | * | |
725 | * @return SP_OK upon success, a negative error code otherwise. | |
726 | */ | |
727 | enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff); | |
728 | ||
729 | /** | |
730 | * Get the XON/XOFF configuration from a port configuration. | |
731 | * | |
732 | * The user should allocate a variable of type enum sp_xonxoff and pass a pointer to this | |
733 | * to receive the result. | |
734 | * | |
735 | * @param config Pointer to configuration structure. | |
736 | * @param xon_xoff_ptr Pointer to variable to store result. | |
737 | * | |
738 | * @return SP_OK upon success, a negative error code otherwise. | |
739 | */ | |
740 | enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr); | |
741 | ||
742 | /** | |
743 | * Set the XON/XOFF configuration in a port configuration. | |
744 | * | |
745 | * @param config Pointer to configuration structure. | |
746 | * @param xon_xoff XON/XOFF mode, or -1 to retain current setting. | |
747 | * | |
748 | * @return SP_OK upon success, a negative error code otherwise. | |
749 | */ | |
750 | enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff); | |
751 | ||
752 | /** | |
753 | * Set the flow control type in a port configuration. | |
754 | * | |
755 | * This function is a wrapper that sets the RTS, CTS, DTR, DSR and | |
756 | * XON/XOFF settings as necessary for the specified flow control | |
757 | * type. For more fine-grained control of these settings, use their | |
758 | * individual configuration functions. | |
759 | * | |
760 | * @param config Pointer to configuration structure. | |
761 | * @param flowcontrol Flow control setting to use. | |
762 | * | |
763 | * @return SP_OK upon success, a negative error code otherwise. | |
764 | */ | |
765 | enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol); | |
766 | ||
767 | /** | |
768 | * Set the flow control type for the specified serial port. | |
769 | * | |
770 | * This function is a wrapper that sets the RTS, CTS, DTR, DSR and | |
771 | * XON/XOFF settings as necessary for the specified flow control | |
772 | * type. For more fine-grained control of these settings, use their | |
773 | * individual configuration functions. | |
774 | * | |
775 | * @param port Pointer to port structure. | |
776 | * @param flowcontrol Flow control setting to use. | |
777 | * | |
778 | * @return SP_OK upon success, a negative error code otherwise. | |
779 | */ | |
780 | enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol); | |
781 | ||
782 | /** | |
783 | * @} | |
784 | * @defgroup Data Reading, writing, and flushing data | |
785 | * @{ | |
786 | */ | |
787 | ||
788 | /** | |
789 | * Read bytes from the specified serial port, blocking until complete. | |
790 | * | |
791 | * @warning If your program runs on Unix, defines its own signal handlers, and | |
792 | * needs to abort blocking reads when these are called, then you | |
793 | * should not use this function. It repeats system calls that return | |
794 | * with EINTR. To be able to abort a read from a signal handler, you | |
795 | * should implement your own blocking read using sp_nonblocking_read() | |
796 | * together with a blocking method that makes sense for your program. | |
797 | * E.g. you can obtain the file descriptor for an open port using | |
798 | * sp_get_port_handle() and use this to call select() or pselect(), | |
799 | * with appropriate arrangements to return if a signal is received. | |
800 | * | |
801 | * @param port Pointer to port structure. | |
802 | * @param buf Buffer in which to store the bytes read. | |
803 | * @param count Requested number of bytes to read. | |
804 | * @param timeout Timeout in milliseconds, or zero to wait indefinitely. | |
805 | * | |
806 | * @return The number of bytes read on success, or a negative error code. If | |
807 | * the number of bytes returned is less than that requested, the | |
808 | * timeout was reached before the requested number of bytes was | |
809 | * available. If timeout is zero, the function will always return | |
810 | * either the requested number of bytes or a negative error code. | |
811 | */ | |
812 | enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout); | |
813 | ||
814 | /** | |
815 | * Read bytes from the specified serial port, without blocking. | |
816 | * | |
817 | * @param port Pointer to port structure. | |
818 | * @param buf Buffer in which to store the bytes read. | |
819 | * @param count Maximum number of bytes to read. | |
820 | * | |
821 | * @return The number of bytes read on success, or a negative error code. The | |
822 | * number of bytes returned may be any number from zero to the maximum | |
823 | * that was requested. | |
824 | */ | |
825 | enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count); | |
826 | ||
827 | /** | |
828 | * Write bytes to the specified serial port, blocking until complete. | |
829 | * | |
830 | * Note that this function only ensures that the accepted bytes have been | |
831 | * written to the OS; they may be held in driver or hardware buffers and not | |
832 | * yet physically transmitted. To check whether all written bytes have actually | |
833 | * been transmitted, use the sp_output_waiting() function. To wait until all | |
834 | * written bytes have actually been transmitted, use the sp_drain() function. | |
835 | * | |
836 | * @warning If your program runs on Unix, defines its own signal handlers, and | |
837 | * needs to abort blocking writes when these are called, then you | |
838 | * should not use this function. It repeats system calls that return | |
839 | * with EINTR. To be able to abort a write from a signal handler, you | |
840 | * should implement your own blocking write using sp_nonblocking_write() | |
841 | * together with a blocking method that makes sense for your program. | |
842 | * E.g. you can obtain the file descriptor for an open port using | |
843 | * sp_get_port_handle() and use this to call select() or pselect(), | |
844 | * with appropriate arrangements to return if a signal is received. | |
845 | * | |
846 | * @param port Pointer to port structure. | |
847 | * @param buf Buffer containing the bytes to write. | |
848 | * @param count Requested number of bytes to write. | |
849 | * @param timeout Timeout in milliseconds, or zero to wait indefinitely. | |
850 | * | |
851 | * @return The number of bytes written on success, or a negative error code. | |
852 | * If the number of bytes returned is less than that requested, the | |
853 | * timeout was reached before the requested number of bytes was | |
854 | * written. If timeout is zero, the function will always return | |
855 | * either the requested number of bytes or a negative error code. In | |
856 | * the event of an error there is no way to determine how many bytes | |
857 | * were sent before the error occured. | |
858 | */ | |
859 | enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout); | |
860 | ||
861 | /** | |
862 | * Write bytes to the specified serial port, without blocking. | |
863 | * | |
864 | * Note that this function only ensures that the accepted bytes have been | |
865 | * written to the OS; they may be held in driver or hardware buffers and not | |
866 | * yet physically transmitted. To check whether all written bytes have actually | |
867 | * been transmitted, use the sp_output_waiting() function. To wait until all | |
868 | * written bytes have actually been transmitted, use the sp_drain() function. | |
869 | * | |
870 | * @param port Pointer to port structure. | |
871 | * @param buf Buffer containing the bytes to write. | |
872 | * @param count Maximum number of bytes to write. | |
873 | * | |
874 | * @return The number of bytes written on success, or a negative error code. | |
875 | * The number of bytes returned may be any number from zero to the | |
876 | * maximum that was requested. | |
877 | */ | |
878 | enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count); | |
879 | ||
880 | /** | |
881 | * Gets the number of bytes waiting in the input buffer. | |
882 | * | |
883 | * @param port Pointer to port structure. | |
884 | * | |
885 | * @return Number of bytes waiting on success, a negative error code otherwise. | |
886 | */ | |
887 | enum sp_return sp_input_waiting(struct sp_port *port); | |
888 | ||
889 | /** | |
890 | * Gets the number of bytes waiting in the output buffer. | |
891 | * | |
892 | * @param port Pointer to port structure. | |
893 | * | |
894 | * @return Number of bytes waiting on success, a negative error code otherwise. | |
895 | */ | |
896 | enum sp_return sp_output_waiting(struct sp_port *port); | |
897 | ||
898 | /** | |
899 | * Flush serial port buffers. Data in the selected buffer(s) is discarded. | |
900 | * | |
901 | * @param port Pointer to port structure. | |
902 | * @param buffers Which buffer(s) to flush. | |
903 | * | |
904 | * @return SP_OK upon success, a negative error code otherwise. | |
905 | */ | |
906 | enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers); | |
907 | ||
908 | /** | |
909 | * Wait for buffered data to be transmitted. | |
910 | * | |
911 | * @warning If your program runs on Unix, defines its own signal handlers, and | |
912 | * needs to abort draining the output buffer when when these are | |
913 | * called, then you should not use this function. It repeats system | |
914 | * calls that return with EINTR. To be able to abort a drain from a | |
915 | * signal handler, you would need to implement your own blocking | |
916 | * drain by polling the result of sp_output_waiting(). | |
917 | * | |
918 | * @param port Pointer to port structure. | |
919 | * | |
920 | * @return SP_OK upon success, a negative error code otherwise. | |
921 | */ | |
922 | enum sp_return sp_drain(struct sp_port *port); | |
923 | ||
924 | /** | |
925 | * @} | |
926 | * @defgroup Waiting Waiting for events | |
927 | * @{ | |
928 | */ | |
929 | ||
930 | /** | |
931 | * Allocate storage for a set of events. | |
932 | * | |
933 | * The user should allocate a variable of type struct sp_event_set *, | |
934 | * then pass a pointer to this variable to receive the result. | |
935 | * | |
936 | * The result should be freed after use by calling sp_free_event_set(). | |
937 | * | |
938 | * @return SP_OK upon success, a negative error code otherwise. | |
939 | */ | |
940 | enum sp_return sp_new_event_set(struct sp_event_set **result_ptr); | |
941 | ||
942 | /** | |
943 | * Add events to a struct sp_event_set for a given port. | |
944 | * | |
945 | * The port must first be opened by calling sp_open() using the same port | |
946 | * structure. | |
947 | * | |
948 | * After the port is closed or the port structure freed, the results may | |
949 | * no longer be valid. | |
950 | * | |
951 | * @param event_set Event set to update. | |
952 | * @param port Pointer to port structure. | |
953 | * @param mask Bitmask of events to be waited for. | |
954 | * | |
955 | * @return SP_OK upon success, a negative error code otherwise. | |
956 | */ | |
957 | enum sp_return sp_add_port_events(struct sp_event_set *event_set, | |
958 | const struct sp_port *port, enum sp_event mask); | |
959 | ||
960 | /** | |
961 | * Wait for any of a set of events to occur. | |
962 | * | |
963 | * @param event_set Event set to wait on. | |
964 | * @param timeout Timeout in milliseconds, or zero to wait indefinitely. | |
965 | * | |
966 | * @return SP_OK upon success, a negative error code otherwise. | |
967 | */ | |
968 | enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout); | |
969 | ||
970 | /** | |
971 | * Free a structure allocated by sp_new_event_set(). | |
972 | */ | |
973 | void sp_free_event_set(struct sp_event_set *event_set); | |
974 | ||
975 | /** | |
976 | * @} | |
977 | * @defgroup Signals Port signalling operations | |
978 | * @{ | |
979 | */ | |
980 | ||
981 | /** | |
982 | * Gets the status of the control signals for the specified port. | |
983 | * | |
984 | * The user should allocate a variable of type "enum sp_signal" and pass a | |
985 | * pointer to this variable to receive the result. The result is a bitmask | |
986 | * in which individual signals can be checked by bitwise OR with values of | |
987 | * the sp_signal enum. | |
988 | * | |
989 | * @param port Pointer to port structure. | |
990 | * @param signals Pointer to variable to receive result. | |
991 | * | |
992 | * @return SP_OK upon success, a negative error code otherwise. | |
993 | */ | |
994 | enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals); | |
995 | ||
996 | /** | |
997 | * Put the port transmit line into the break state. | |
998 | * | |
999 | * @param port Pointer to port structure. | |
1000 | * | |
1001 | * @return SP_OK upon success, a negative error code otherwise. | |
1002 | */ | |
1003 | enum sp_return sp_start_break(struct sp_port *port); | |
1004 | ||
1005 | /** | |
1006 | * Take the port transmit line out of the break state. | |
1007 | * | |
1008 | * @param port Pointer to port structure. | |
1009 | * | |
1010 | * @return SP_OK upon success, a negative error code otherwise. | |
1011 | */ | |
1012 | enum sp_return sp_end_break(struct sp_port *port); | |
1013 | ||
1014 | /** | |
1015 | * @} | |
1016 | * @defgroup Errors Obtaining error information | |
1017 | * @{ | |
1018 | */ | |
1019 | ||
1020 | /** | |
1021 | * Get the error code for a failed operation. | |
1022 | * | |
1023 | * In order to obtain the correct result, this function should be called | |
1024 | * straight after the failure, before executing any other system operations. | |
1025 | * | |
1026 | * @return The system's numeric code for the error that caused the last | |
1027 | * operation to fail. | |
1028 | */ | |
1029 | int sp_last_error_code(void); | |
1030 | ||
1031 | /** | |
1032 | * Get the error message for a failed operation. | |
1033 | * | |
1034 | * In order to obtain the correct result, this function should be called | |
1035 | * straight after the failure, before executing other system operations. | |
1036 | * | |
1037 | * @return The system's message for the error that caused the last | |
1038 | * operation to fail. This string may be allocated by the function, | |
1039 | * and should be freed after use by calling sp_free_error_message(). | |
1040 | */ | |
1041 | char *sp_last_error_message(void); | |
1042 | ||
1043 | /** | |
1044 | * Free an error message returned by sp_last_error_message(). | |
1045 | */ | |
1046 | void sp_free_error_message(char *message); | |
1047 | ||
1048 | /** | |
1049 | * Set the handler function for library debugging messages. | |
1050 | * | |
1051 | * Debugging messages are generated by the library during each operation, | |
1052 | * to help in diagnosing problems. The handler will be called for each | |
1053 | * message. The handler can be set to NULL to ignore all debug messages. | |
1054 | * | |
1055 | * The handler function should accept a format string and variable length | |
1056 | * argument list, in the same manner as e.g. printf(). | |
1057 | * | |
1058 | * The default handler is sp_default_debug_handler(). | |
1059 | */ | |
1060 | void sp_set_debug_handler(void (*handler)(const char *format, ...)); | |
1061 | ||
1062 | /** | |
1063 | * Default handler function for library debugging messages. | |
1064 | * | |
1065 | * This function prints debug messages to the standard error stream if the | |
1066 | * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are | |
1067 | * ignored. | |
1068 | */ | |
1069 | void sp_default_debug_handler(const char *format, ...); | |
1070 | ||
1071 | /** @} */ | |
1072 | ||
1073 | #ifdef __cplusplus | |
1074 | } | |
1075 | #endif | |
1076 | ||
1077 | #endif |