]>
Commit | Line | Data |
---|---|---|
74510d4b ML |
1 | /* |
2 | * This file is part of the libserialport project. | |
3 | * | |
f77bb46d ML |
4 | * Copyright (C) 2013, 2015 Martin Ling <martin-libserialport@earth.li> |
5 | * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de> | |
6 | * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org> | |
74510d4b ML |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU Lesser General Public License as | |
10 | * published by the Free Software Foundation, either version 3 of the | |
11 | * License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
cd5f5281 | 22 | /** |
cf9d365c UH |
23 | * @mainpage libserialport API |
24 | * | |
25 | * Introduction | |
26 | * ============ | |
27 | * | |
28 | * libserialport is a minimal library written in C that is intended to take | |
29 | * care of the OS-specific details when writing software that uses serial ports. | |
30 | * | |
31 | * By writing your serial code to use libserialport, you enable it to work | |
32 | * transparently on any platform supported by the library. | |
33 | * | |
deef6e52 | 34 | * libserialport is an open source project released under the LGPL3+ license. |
cf9d365c | 35 | * |
deef6e52 ML |
36 | * The library is maintained by the [sigrok](http://sigrok.org/) project. See |
37 | * the [libserialport homepage](http://sigrok.org/wiki/Libserialport) for the | |
38 | * latest information. | |
cf9d365c | 39 | * |
deef6e52 ML |
40 | * Source code is maintained in git at |
41 | * [git://sigrok.org/libserialport](http://sigrok.org/gitweb/?p=libserialport.git). | |
42 | * | |
43 | * Bugs are tracked at http://sigrok.org/bugzilla/. | |
44 | * | |
45 | * The library was conceived and designed by Martin Ling, is maintained by | |
46 | * Uwe Hermann, and has received contributions from several other developers. | |
47 | * See the git history for full credits. | |
48 | * | |
49 | * API information | |
50 | * =============== | |
51 | * | |
52 | * The API has been designed from scratch. It does not exactly resemble the | |
53 | * serial API of any particular operating system. Instead it aims to provide | |
54 | * a set of functions that can reliably be implemented across all operating | |
55 | * systems. These form a sufficient basis for higher level behaviour to | |
56 | * be implemented in a platform independent manner. | |
57 | * | |
58 | * If you are porting code written for a particular OS, you may find you need | |
59 | * to restructure things somewhat, or do without some specialised features. | |
60 | * For particular notes on porting existing code, see @ref Porting. | |
61 | * | |
7c8d67ef ML |
62 | * Examples |
63 | * -------- | |
64 | * | |
65 | * Some simple example programs using libserialport are included in the | |
66 | * @c examples directory in the source package: | |
67 | * | |
68 | * - @ref list_ports.c - Getting a list of ports present on the system. | |
69 | * - @ref port_info.c - Getting information on a particular serial port. | |
70 | * | |
71 | * These examples are linked with the API documentation. Each function | |
72 | * in the API reference includes links to where it is used in an example | |
73 | * program, and each appearance of a function in the examples links | |
74 | * to that function's entry in the API reference. | |
49fd7b1b ML |
75 | * |
76 | * Headers | |
77 | * ------- | |
78 | * | |
79 | * To use libserialport functions in your code, you should include the | |
44df4154 UH |
80 | * libserialport.h header, i.e. |
81 | * @code | |
82 | * #include <libserialport.h> | |
83 | * @endcode | |
49fd7b1b ML |
84 | * |
85 | * Namespace | |
86 | * --------- | |
87 | * | |
88 | * All identifiers defined by the public libserialport headers use the prefix | |
ad19d604 | 89 | * @c sp_ (for functions and data types) or @c SP_ (for macros and constants). |
49fd7b1b ML |
90 | * |
91 | * Functions | |
92 | * --------- | |
93 | * | |
94 | * The functions provided by the library are documented in detail in | |
95 | * the following sections: | |
96 | * | |
97 | * - @ref Enumeration (obtaining a list of serial ports on the system) | |
98 | * - @ref Ports (opening, closing and getting information about ports) | |
99 | * - @ref Configuration (baud rate, parity, etc.) | |
100 | * - @ref Signals (modem control lines, breaks, etc.) | |
101 | * - @ref Data (reading and writing data, and buffer management) | |
102 | * - @ref Waiting (waiting for ports to be ready, integrating with event loops) | |
103 | * - @ref Errors (getting error and debugging information) | |
deef6e52 ML |
104 | * |
105 | * Data structures | |
106 | * --------------- | |
107 | * | |
108 | * The library defines three data structures: | |
cf9d365c | 109 | * |
deef6e52 ML |
110 | * - @ref sp_port, which represents a serial port. |
111 | * See @ref Enumeration. | |
112 | * - @ref sp_port_config, which represents a port configuration. | |
113 | * See @ref Configuration. | |
114 | * - @ref sp_event_set, which represents a set of events. | |
115 | * See @ref Waiting. | |
cf9d365c | 116 | * |
deef6e52 ML |
117 | * All these structures are allocated and freed by library functions. It is |
118 | * the caller's responsibility to ensure that the correct calls are made to | |
119 | * free allocated structures after use. | |
cf9d365c | 120 | * |
deef6e52 ML |
121 | * Return codes and error handling |
122 | * ------------------------------- | |
cf9d365c | 123 | * |
0151b157 ML |
124 | * Most functions have return type @ref sp_return and can return only four |
125 | * possible error values: | |
cf9d365c | 126 | * |
6aabf62a ML |
127 | * - @ref SP_ERR_ARG means that a function was called with invalid |
128 | * arguments. This implies a bug in the caller. The arguments passed would | |
129 | * be invalid regardless of the underlying OS or serial device involved. | |
130 | * | |
131 | * - @ref SP_ERR_FAIL means that the OS reported a failure. The error code or | |
132 | * message provided by the OS can be obtained by calling sp_last_error_code() | |
133 | * or sp_last_error_message(). | |
134 | * | |
135 | * - @ref SP_ERR_SUPP indicates that there is no support for the requested | |
136 | * operation in the current OS, driver or device. No error message is | |
137 | * available from the OS in this case. There is either no way to request | |
138 | * the operation in the first place, or libserialport does not know how to | |
139 | * do so in the current version. | |
140 | * | |
141 | * - @ref SP_ERR_MEM indicates that a memory allocation failed. | |
142 | * | |
143 | * All of these error values are negative. | |
cf9d365c | 144 | * |
0151b157 ML |
145 | * Calls that succeed return @ref SP_OK, which is equal to zero. Some functions |
146 | * declared @ref sp_return can also return a positive value for a successful | |
0a1ab8bf | 147 | * numeric result, e.g. sp_blocking_read() or sp_blocking_write(). |
deef6e52 ML |
148 | * |
149 | * An error message is only available via sp_last_error_message() in the case | |
ad19d604 | 150 | * where @ref SP_ERR_FAIL was returned by the previous function call. The error |
deef6e52 ML |
151 | * message returned is that provided by the OS, using the current language |
152 | * settings. It is an error to call sp_last_error_code() or | |
153 | * sp_last_error_message() except after a previous function call returned | |
ad19d604 ML |
154 | * @ref SP_ERR_FAIL. The library does not define its own error codes or |
155 | * messages to accompany other return codes. | |
deef6e52 ML |
156 | * |
157 | * Thread safety | |
158 | * ------------- | |
159 | * | |
160 | * Certain combinations of calls can be made concurrently, as follows. | |
161 | * | |
162 | * - Calls using different ports may always be made concurrently, i.e. | |
163 | * it is safe for separate threads to handle their own ports. | |
164 | * | |
165 | * - Calls using the same port may be made concurrently when one call | |
166 | * is a read operation and one call is a write operation, i.e. it is safe | |
167 | * to use separate "reader" and "writer" threads for the same port. See | |
168 | * below for which operations meet these definitions. | |
169 | * | |
170 | * Read operations: | |
171 | * | |
172 | * - sp_blocking_read() | |
173 | * - sp_blocking_read_next() | |
174 | * - sp_nonblocking_read() | |
175 | * - sp_input_waiting() | |
176 | * - sp_flush() with @ref SP_BUF_INPUT only. | |
177 | * - sp_wait() with @ref SP_EVENT_RX_READY only. | |
178 | * | |
179 | * Write operations: | |
180 | * | |
181 | * - sp_blocking_write() | |
182 | * - sp_nonblocking_write() | |
183 | * - sp_output_waiting() | |
184 | * - sp_drain() | |
185 | * - sp_flush() with @ref SP_BUF_OUTPUT only. | |
186 | * - sp_wait() with @ref SP_EVENT_TX_READY only. | |
187 | * | |
188 | * If two calls, on the same port, do not fit into one of these categories | |
189 | * each, then they may not be made concurrently. | |
190 | * | |
191 | * Debugging | |
192 | * --------- | |
193 | * | |
194 | * The library can output extensive tracing and debugging information. The | |
195 | * simplest way to use this is to set the environment variable | |
ad19d604 | 196 | * @c LIBSERIALPORT_DEBUG to any value; messages will then be output to the |
deef6e52 ML |
197 | * standard error stream. |
198 | * | |
199 | * This behaviour is implemented by a default debug message handling | |
200 | * callback. An alternative callback can be set using sp_set_debug_handler(), | |
201 | * in order to e.g. redirect the output elsewhere or filter it. | |
202 | * | |
203 | * No guarantees are made about the content of the debug output; it is chosen | |
204 | * to suit the needs of the developers and may change between releases. | |
205 | * | |
206 | * @anchor Porting | |
207 | * Porting | |
208 | * ------- | |
209 | * | |
210 | * The following guidelines may help when porting existing OS-specific code | |
211 | * to use libserialport. | |
212 | * | |
213 | * ### Porting from Unix-like systems ### | |
214 | * | |
215 | * There are two main differences to note when porting code written for Unix. | |
216 | * | |
217 | * The first is that Unix traditionally provides a wide range of functionality | |
218 | * for dealing with serial devices at the OS level; this is exposed through the | |
219 | * termios API and dates to the days when serial terminals were common. If your | |
220 | * code relies on many of these facilities you will need to adapt it, because | |
56fe6e32 | 221 | * libserialport provides only a raw binary channel with no special handling. |
deef6e52 ML |
222 | * |
223 | * The second relates to blocking versus non-blocking I/O behaviour. In | |
ad19d604 ML |
224 | * Unix-like systems this is normally specified by setting the @c O_NONBLOCK |
225 | * flag on the file descriptor, affecting the semantics of subsequent @c read() | |
226 | * and @c write() calls. | |
deef6e52 ML |
227 | * |
228 | * In libserialport, blocking and nonblocking operations are both available at | |
ad19d604 | 229 | * any time. If your existing code Ñ•ets @c O_NONBLOCK, you should use |
deef6e52 | 230 | * sp_nonblocking_read() and sp_nonblocking_write() to get the same behaviour |
ad19d604 ML |
231 | * as your existing @c read() and @c write() calls. If it does not, you should |
232 | * use sp_blocking_read() and sp_blocking_write() instead. You may also find | |
deef6e52 | 233 | * sp_blocking_read_next() useful, which reproduces the semantics of a blocking |
ad19d604 | 234 | * read() with @c VTIME=0 and @c VMIN=1 set in termios. |
deef6e52 ML |
235 | * |
236 | * Finally, you should take care if your program uses custom signal handlers. | |
237 | * The blocking calls provided by libserialport will restart system calls that | |
ad19d604 | 238 | * return with @c EINTR, so you will need to make your own arrangements if you |
deef6e52 ML |
239 | * need to interrupt blocking operations when your signal handlers are called. |
240 | * This is not an issue if you only use the default handlers. | |
241 | * | |
242 | * ### Porting from Windows ### | |
243 | * | |
244 | * The main consideration when porting from Windows is that there is no | |
245 | * direct equivalent for overlapped I/O operations. | |
246 | * | |
247 | * If your program does not use overlapped I/O, you can simply use | |
248 | * sp_blocking_read() and sp_blocking_write() as direct equivalents for | |
ad19d604 ML |
249 | * @c ReadFile() and @c WriteFile(). You may also find sp_blocking_read_next() |
250 | * useful, which reproduces the special semantics of @c ReadFile() with | |
251 | * @c ReadIntervalTimeout and @c ReadTotalTimeoutMultiplier set to @c MAXDWORD | |
252 | * and @c ReadTotalTimeoutConstant set to between @c 1 and @c MAXDWORD-1 . | |
deef6e52 ML |
253 | * |
254 | * If your program makes use of overlapped I/O to continue work while a serial | |
255 | * operation is in progress, then you can achieve the same results using | |
256 | * sp_nonblocking_read() and sp_nonblocking_write(). | |
257 | * | |
258 | * Generally, overlapped I/O is combined with either waiting for completion | |
ad19d604 ML |
259 | * once there is no more background work to do (using @c WaitForSingleObject() |
260 | * or @c WaitForMultipleObjects()), or periodically checking for completion | |
261 | * with @c GetOverlappedResult(). If the aim is to start a new operation for | |
262 | * further data once the previous one has completed, you can instead simply | |
263 | * call the nonblocking functions again with the next data. If you need to | |
264 | * wait for completion, use sp_wait() to determine when the port is ready to | |
265 | * send or receive further data. | |
cf9d365c | 266 | */ |
cd5f5281 | 267 | |
8645feda UH |
268 | #ifndef LIBSERIALPORT_LIBSERIALPORT_H |
269 | #define LIBSERIALPORT_LIBSERIALPORT_H | |
e8ffaee9 | 270 | |
5ef8a1ed UH |
271 | #ifdef __cplusplus |
272 | extern "C" { | |
273 | #endif | |
274 | ||
74510d4b | 275 | #include <stddef.h> |
74510d4b | 276 | |
cd5f5281 | 277 | /** Return values. */ |
eb6ed20f | 278 | enum sp_return { |
cd5f5281 | 279 | /** Operation completed successfully. */ |
74510d4b | 280 | SP_OK = 0, |
cd5f5281 | 281 | /** Invalid arguments were passed to the function. */ |
e9a2f9c9 | 282 | SP_ERR_ARG = -1, |
cfa5af67 | 283 | /** A system error occurred while executing the operation. */ |
e9a2f9c9 | 284 | SP_ERR_FAIL = -2, |
cd5f5281 | 285 | /** A memory allocation failed while executing the operation. */ |
f92f1f0c | 286 | SP_ERR_MEM = -3, |
6aabf62a | 287 | /** The requested operation is not supported by this system or device. */ |
79a80046 | 288 | SP_ERR_SUPP = -4 |
74510d4b ML |
289 | }; |
290 | ||
cd5f5281 | 291 | /** Port access modes. */ |
eb6ed20f | 292 | enum sp_mode { |
a036341b ML |
293 | /** Open port for read access. */ |
294 | SP_MODE_READ = 1, | |
295 | /** Open port for write access. */ | |
276ef1b9 | 296 | SP_MODE_WRITE = 2, |
13efecf8 | 297 | /** Open port for read and write access. @since 0.1.1 */ |
276ef1b9 | 298 | SP_MODE_READ_WRITE = 3 |
74510d4b ML |
299 | }; |
300 | ||
6f1186aa ML |
301 | /** Port events. */ |
302 | enum sp_event { | |
144a598f | 303 | /** Data received and ready to read. */ |
6f1186aa | 304 | SP_EVENT_RX_READY = 1, |
144a598f | 305 | /** Ready to transmit new data. */ |
6f1186aa | 306 | SP_EVENT_TX_READY = 2, |
144a598f | 307 | /** Error occurred. */ |
79a80046 | 308 | SP_EVENT_ERROR = 4 |
6f1186aa ML |
309 | }; |
310 | ||
fd8fd11a ML |
311 | /** Buffer selection. */ |
312 | enum sp_buffer { | |
313 | /** Input buffer. */ | |
314 | SP_BUF_INPUT = 1, | |
315 | /** Output buffer. */ | |
316 | SP_BUF_OUTPUT = 2, | |
317 | /** Both buffers. */ | |
79a80046 | 318 | SP_BUF_BOTH = 3 |
fd8fd11a ML |
319 | }; |
320 | ||
cd5f5281 | 321 | /** Parity settings. */ |
eb6ed20f | 322 | enum sp_parity { |
c200f5c1 | 323 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 324 | SP_PARITY_INVALID = -1, |
cd5f5281 | 325 | /** No parity. */ |
74510d4b | 326 | SP_PARITY_NONE = 0, |
cd5f5281 | 327 | /** Odd parity. */ |
20e63a77 ML |
328 | SP_PARITY_ODD = 1, |
329 | /** Even parity. */ | |
330 | SP_PARITY_EVEN = 2, | |
e432ce60 ML |
331 | /** Mark parity. */ |
332 | SP_PARITY_MARK = 3, | |
333 | /** Space parity. */ | |
79a80046 | 334 | SP_PARITY_SPACE = 4 |
74510d4b ML |
335 | }; |
336 | ||
cd5f5281 | 337 | /** RTS pin behaviour. */ |
eb6ed20f | 338 | enum sp_rts { |
c200f5c1 | 339 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 340 | SP_RTS_INVALID = -1, |
cd5f5281 | 341 | /** RTS off. */ |
d514a26f | 342 | SP_RTS_OFF = 0, |
cd5f5281 | 343 | /** RTS on. */ |
d514a26f | 344 | SP_RTS_ON = 1, |
cd5f5281 | 345 | /** RTS used for flow control. */ |
79a80046 | 346 | SP_RTS_FLOW_CONTROL = 2 |
d514a26f ML |
347 | }; |
348 | ||
cd5f5281 | 349 | /** CTS pin behaviour. */ |
eb6ed20f | 350 | enum sp_cts { |
c200f5c1 | 351 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 352 | SP_CTS_INVALID = -1, |
cd5f5281 | 353 | /** CTS ignored. */ |
d514a26f | 354 | SP_CTS_IGNORE = 0, |
cd5f5281 | 355 | /** CTS used for flow control. */ |
79a80046 | 356 | SP_CTS_FLOW_CONTROL = 1 |
d514a26f ML |
357 | }; |
358 | ||
cd5f5281 | 359 | /** DTR pin behaviour. */ |
eb6ed20f | 360 | enum sp_dtr { |
c200f5c1 | 361 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 362 | SP_DTR_INVALID = -1, |
cd5f5281 | 363 | /** DTR off. */ |
d514a26f | 364 | SP_DTR_OFF = 0, |
cd5f5281 | 365 | /** DTR on. */ |
d514a26f | 366 | SP_DTR_ON = 1, |
cd5f5281 | 367 | /** DTR used for flow control. */ |
79a80046 | 368 | SP_DTR_FLOW_CONTROL = 2 |
d514a26f ML |
369 | }; |
370 | ||
cd5f5281 | 371 | /** DSR pin behaviour. */ |
eb6ed20f | 372 | enum sp_dsr { |
c200f5c1 | 373 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 374 | SP_DSR_INVALID = -1, |
cd5f5281 | 375 | /** DSR ignored. */ |
d514a26f | 376 | SP_DSR_IGNORE = 0, |
cd5f5281 | 377 | /** DSR used for flow control. */ |
79a80046 | 378 | SP_DSR_FLOW_CONTROL = 1 |
d514a26f ML |
379 | }; |
380 | ||
cd5f5281 | 381 | /** XON/XOFF flow control behaviour. */ |
eb6ed20f | 382 | enum sp_xonxoff { |
c200f5c1 | 383 | /** Special value to indicate setting should be left alone. */ |
eb6ed20f | 384 | SP_XONXOFF_INVALID = -1, |
cd5f5281 | 385 | /** XON/XOFF disabled. */ |
d514a26f | 386 | SP_XONXOFF_DISABLED = 0, |
cd5f5281 | 387 | /** XON/XOFF enabled for input only. */ |
d514a26f | 388 | SP_XONXOFF_IN = 1, |
cd5f5281 | 389 | /** XON/XOFF enabled for output only. */ |
d514a26f | 390 | SP_XONXOFF_OUT = 2, |
cd5f5281 | 391 | /** XON/XOFF enabled for input and output. */ |
79a80046 | 392 | SP_XONXOFF_INOUT = 3 |
ac74fdaf ML |
393 | }; |
394 | ||
cd5f5281 | 395 | /** Standard flow control combinations. */ |
eb6ed20f | 396 | enum sp_flowcontrol { |
cd5f5281 | 397 | /** No flow control. */ |
18fc2dd1 | 398 | SP_FLOWCONTROL_NONE = 0, |
cd5f5281 | 399 | /** Software flow control using XON/XOFF characters. */ |
18fc2dd1 | 400 | SP_FLOWCONTROL_XONXOFF = 1, |
cd5f5281 | 401 | /** Hardware flow control using RTS/CTS signals. */ |
18fc2dd1 | 402 | SP_FLOWCONTROL_RTSCTS = 2, |
cd5f5281 | 403 | /** Hardware flow control using DTR/DSR signals. */ |
79a80046 | 404 | SP_FLOWCONTROL_DTRDSR = 3 |
18fc2dd1 ML |
405 | }; |
406 | ||
8cf7c697 ML |
407 | /** Input signals. */ |
408 | enum sp_signal { | |
409 | /** Clear to send. */ | |
410 | SP_SIG_CTS = 1, | |
411 | /** Data set ready. */ | |
412 | SP_SIG_DSR = 2, | |
413 | /** Data carrier detect. */ | |
414 | SP_SIG_DCD = 4, | |
415 | /** Ring indicator. */ | |
79a80046 | 416 | SP_SIG_RI = 8 |
8cf7c697 ML |
417 | }; |
418 | ||
13efecf8 UH |
419 | /** |
420 | * Transport types. | |
421 | * | |
422 | * @since 0.1.1 | |
423 | */ | |
a93fb468 | 424 | enum sp_transport { |
13efecf8 | 425 | /** Native platform serial port. @since 0.1.1 */ |
a93fb468 | 426 | SP_TRANSPORT_NATIVE, |
13efecf8 | 427 | /** USB serial port adapter. @since 0.1.1 */ |
a93fb468 | 428 | SP_TRANSPORT_USB, |
13efecf8 | 429 | /** Bluetooth serial port adapter. @since 0.1.1 */ |
79a80046 | 430 | SP_TRANSPORT_BLUETOOTH |
a93fb468 AJ |
431 | }; |
432 | ||
0a1ab8bf UH |
433 | /** |
434 | * @struct sp_port | |
435 | * An opaque structure representing a serial port. | |
436 | */ | |
1c5aae9d | 437 | struct sp_port; |
eb6ed20f | 438 | |
0a1ab8bf UH |
439 | /** |
440 | * @struct sp_port_config | |
441 | * An opaque structure representing the configuration for a serial port. | |
442 | */ | |
9b1502ef | 443 | struct sp_port_config; |
eb6ed20f | 444 | |
6f1186aa ML |
445 | /** |
446 | * @struct sp_event_set | |
447 | * A set of handles to wait on for events. | |
448 | */ | |
449 | struct sp_event_set { | |
450 | /** Array of OS-specific handles. */ | |
451 | void *handles; | |
452 | /** Array of bitmasks indicating which events apply for each handle. */ | |
453 | enum sp_event *masks; | |
454 | /** Number of handles. */ | |
455 | unsigned int count; | |
456 | }; | |
457 | ||
091e75fe | 458 | /** |
144a598f UH |
459 | * @defgroup Enumeration Port enumeration |
460 | * | |
461 | * Enumerating the serial ports of a system. | |
462 | * | |
7c8d67ef ML |
463 | * See @ref list_ports.c for a working example of port enumeration. |
464 | * | |
144a598f UH |
465 | * @{ |
466 | */ | |
091e75fe | 467 | |
cd5f5281 | 468 | /** |
cf9d365c UH |
469 | * Obtain a pointer to a new sp_port structure representing the named port. |
470 | * | |
471 | * The user should allocate a variable of type "struct sp_port *" and pass a | |
472 | * pointer to this to receive the result. | |
473 | * | |
474 | * The result should be freed after use by calling sp_free_port(). | |
475 | * | |
35578b06 UH |
476 | * @param[in] portname The OS-specific name of a serial port. Must not be NULL. |
477 | * @param[out] port_ptr If any error is returned, the variable pointed to by | |
478 | * port_ptr will be set to NULL. Otherwise, it will be set | |
479 | * to point to the newly allocated port. Must not be NULL. | |
f36c6395 ML |
480 | * |
481 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
482 | * |
483 | * @since 0.1.0 | |
cf9d365c | 484 | */ |
eb6ed20f | 485 | enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr); |
cd5f5281 ML |
486 | |
487 | /** | |
cf9d365c | 488 | * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port(). |
1652aa86 | 489 | * |
35578b06 UH |
490 | * @param[in] port Pointer to a port structure. Must not be NULL. |
491 | * | |
1652aa86 | 492 | * @since 0.1.0 |
cf9d365c | 493 | */ |
e3b2f7a4 | 494 | void sp_free_port(struct sp_port *port); |
cd5f5281 ML |
495 | |
496 | /** | |
cf9d365c UH |
497 | * List the serial ports available on the system. |
498 | * | |
499 | * The result obtained is an array of pointers to sp_port structures, | |
500 | * terminated by a NULL. The user should allocate a variable of type | |
501 | * "struct sp_port **" and pass a pointer to this to receive the result. | |
502 | * | |
503 | * The result should be freed after use by calling sp_free_port_list(). | |
504 | * If a port from the list is to be used after freeing the list, it must be | |
505 | * copied first using sp_copy_port(). | |
506 | * | |
35578b06 UH |
507 | * @param[out] list_ptr If any error is returned, the variable pointed to by |
508 | * list_ptr will be set to NULL. Otherwise, it will be set | |
509 | * to point to the newly allocated array. Must not be NULL. | |
f36c6395 ML |
510 | * |
511 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
512 | * |
513 | * @since 0.1.0 | |
cf9d365c | 514 | */ |
eb6ed20f | 515 | enum sp_return sp_list_ports(struct sp_port ***list_ptr); |
cd5f5281 ML |
516 | |
517 | /** | |
35578b06 | 518 | * Make a new copy of an sp_port structure. |
cf9d365c UH |
519 | * |
520 | * The user should allocate a variable of type "struct sp_port *" and pass a | |
521 | * pointer to this to receive the result. | |
522 | * | |
523 | * The copy should be freed after use by calling sp_free_port(). | |
524 | * | |
35578b06 UH |
525 | * @param[in] port Pointer to a port structure. Must not be NULL. |
526 | * @param[out] copy_ptr If any error is returned, the variable pointed to by | |
527 | * copy_ptr will be set to NULL. Otherwise, it will be set | |
528 | * to point to the newly allocated copy. Must not be NULL. | |
f36c6395 ML |
529 | * |
530 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
531 | * |
532 | * @since 0.1.0 | |
cf9d365c | 533 | */ |
eb6ed20f | 534 | enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr); |
cd5f5281 ML |
535 | |
536 | /** | |
cf9d365c UH |
537 | * Free a port list obtained from sp_list_ports(). |
538 | * | |
539 | * This will also free all the sp_port structures referred to from the list; | |
540 | * any that are to be retained must be copied first using sp_copy_port(). | |
1652aa86 | 541 | * |
35578b06 UH |
542 | * @param[in] ports Pointer to a list of port structures. Must not be NULL. |
543 | * | |
1652aa86 | 544 | * @since 0.1.0 |
cf9d365c | 545 | */ |
d54e9004 | 546 | void sp_free_port_list(struct sp_port **ports); |
e96d8bd2 | 547 | |
091e75fe | 548 | /** |
cf9d365c | 549 | * @} |
144a598f UH |
550 | * @defgroup Ports Port handling |
551 | * | |
552 | * Opening, closing and querying ports. | |
553 | * | |
7c8d67ef ML |
554 | * See @ref port_info.c for a working example of getting port information. |
555 | * | |
cf9d365c UH |
556 | * @{ |
557 | */ | |
091e75fe | 558 | |
cd5f5281 | 559 | /** |
cf9d365c UH |
560 | * Open the specified serial port. |
561 | * | |
35578b06 UH |
562 | * @param[in] port Pointer to a port structure. Must not be NULL. |
563 | * @param[in] flags Flags to use when opening the serial port. | |
cf9d365c | 564 | * |
f36c6395 | 565 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
566 | * |
567 | * @since 0.1.0 | |
cf9d365c | 568 | */ |
eb6ed20f | 569 | enum sp_return sp_open(struct sp_port *port, enum sp_mode flags); |
cd5f5281 ML |
570 | |
571 | /** | |
cf9d365c UH |
572 | * Close the specified serial port. |
573 | * | |
35578b06 UH |
574 | * @param[in] port Pointer to a port structure. Must not be NULL. |
575 | * | |
f36c6395 | 576 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
577 | * |
578 | * @since 0.1.0 | |
cf9d365c | 579 | */ |
eb6ed20f | 580 | enum sp_return sp_close(struct sp_port *port); |
e96d8bd2 | 581 | |
0151b157 ML |
582 | /** |
583 | * Get the name of a port. | |
584 | * | |
585 | * The name returned is whatever is normally used to refer to a port on the | |
586 | * current operating system; e.g. for Windows it will usually be a "COMn" | |
587 | * device name, and for Unix it will be a device path beginning with "/dev/". | |
588 | * | |
35578b06 | 589 | * @param[in] port Pointer to a port structure. Must not be NULL. |
0151b157 ML |
590 | * |
591 | * @return The port name, or NULL if an invalid port is passed. The name | |
35578b06 UH |
592 | * string is part of the port structure and may not be used after |
593 | * the port structure has been freed. | |
1652aa86 UH |
594 | * |
595 | * @since 0.1.0 | |
0151b157 ML |
596 | */ |
597 | char *sp_get_port_name(const struct sp_port *port); | |
598 | ||
a93fb468 AJ |
599 | /** |
600 | * Get a description for a port, to present to end user. | |
601 | * | |
35578b06 | 602 | * @param[in] port Pointer to a port structure. Must not be NULL. |
a93fb468 AJ |
603 | * |
604 | * @return The port description, or NULL if an invalid port is passed. | |
35578b06 UH |
605 | * The description string is part of the port structure and may not |
606 | * be used after the port structure has been freed. | |
a93fb468 | 607 | * |
3f2f48fc | 608 | * @since 0.1.1 |
a93fb468 | 609 | */ |
f1480939 | 610 | char *sp_get_port_description(const struct sp_port *port); |
a93fb468 AJ |
611 | |
612 | /** | |
613 | * Get the transport type used by a port. | |
614 | * | |
35578b06 | 615 | * @param[in] port Pointer to a port structure. Must not be NULL. |
a93fb468 AJ |
616 | * |
617 | * @return The port transport type. | |
618 | * | |
3f2f48fc | 619 | * @since 0.1.1 |
a93fb468 | 620 | */ |
ad036cc8 | 621 | enum sp_transport sp_get_port_transport(const struct sp_port *port); |
a93fb468 AJ |
622 | |
623 | /** | |
624 | * Get the USB bus number and address on bus of a USB serial adapter port. | |
625 | * | |
35578b06 | 626 | * @param[in] port Pointer to a port structure. Must not be NULL. |
ff6da776 UH |
627 | * @param[out] usb_bus Pointer to a variable to store the USB bus. |
628 | * Can be NULL (in that case it will be ignored). | |
629 | * @param[out] usb_address Pointer to a variable to store the USB address. | |
630 | * Can be NULL (in that case it will be ignored). | |
a93fb468 AJ |
631 | * |
632 | * @return SP_OK upon success, a negative error code otherwise. | |
633 | * | |
3f2f48fc | 634 | * @since 0.1.1 |
a93fb468 AJ |
635 | */ |
636 | enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port, | |
637 | int *usb_bus, int *usb_address); | |
638 | ||
639 | /** | |
640 | * Get the USB Vendor ID and Product ID of a USB serial adapter port. | |
641 | * | |
35578b06 | 642 | * @param[in] port Pointer to a port structure. Must not be NULL. |
9eb9f071 UH |
643 | * @param[out] usb_vid Pointer to a variable to store the USB VID. |
644 | * Can be NULL (in that case it will be ignored). | |
645 | * @param[out] usb_pid Pointer to a variable to store the USB PID. | |
646 | * Can be NULL (in that case it will be ignored). | |
a93fb468 AJ |
647 | * |
648 | * @return SP_OK upon success, a negative error code otherwise. | |
649 | * | |
3f2f48fc | 650 | * @since 0.1.1 |
a93fb468 AJ |
651 | */ |
652 | enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, int *usb_vid, int *usb_pid); | |
653 | ||
654 | /** | |
655 | * Get the USB manufacturer string of a USB serial adapter port. | |
656 | * | |
35578b06 | 657 | * @param[in] port Pointer to a port structure. Must not be NULL. |
a93fb468 AJ |
658 | * |
659 | * @return The port manufacturer string, or NULL if an invalid port is passed. | |
35578b06 UH |
660 | * The manufacturer string is part of the port structure and may not |
661 | * be used after the port structure has been freed. | |
a93fb468 | 662 | * |
3f2f48fc | 663 | * @since 0.1.1 |
a93fb468 AJ |
664 | */ |
665 | char *sp_get_port_usb_manufacturer(const struct sp_port *port); | |
666 | ||
667 | /** | |
668 | * Get the USB product string of a USB serial adapter port. | |
669 | * | |
35578b06 | 670 | * @param[in] port Pointer to a port structure. Must not be NULL. |
a93fb468 AJ |
671 | * |
672 | * @return The port product string, or NULL if an invalid port is passed. | |
35578b06 UH |
673 | * The product string is part of the port structure and may not be |
674 | * used after the port structure has been freed. | |
a93fb468 | 675 | * |
3f2f48fc | 676 | * @since 0.1.1 |
a93fb468 AJ |
677 | */ |
678 | char *sp_get_port_usb_product(const struct sp_port *port); | |
679 | ||
680 | /** | |
681 | * Get the USB serial number string of a USB serial adapter port. | |
682 | * | |
35578b06 | 683 | * @param[in] port Pointer to a port structure. Must not be NULL. |
a93fb468 AJ |
684 | * |
685 | * @return The port serial number, or NULL if an invalid port is passed. | |
35578b06 UH |
686 | * The serial number string is part of the port structure and may |
687 | * not be used after the port structure has been freed. | |
a93fb468 | 688 | * |
3f2f48fc | 689 | * @since 0.1.1 |
a93fb468 AJ |
690 | */ |
691 | char *sp_get_port_usb_serial(const struct sp_port *port); | |
692 | ||
693 | /** | |
e33dcf90 | 694 | * Get the MAC address of a Bluetooth serial adapter port. |
a93fb468 | 695 | * |
35578b06 | 696 | * @param[in] port Pointer to a port structure. Must not be NULL. |
a93fb468 | 697 | * |
e33dcf90 | 698 | * @return The port MAC address, or NULL if an invalid port is passed. |
35578b06 UH |
699 | * The MAC address string is part of the port structure and may not |
700 | * be used after the port structure has been freed. | |
a93fb468 | 701 | * |
3f2f48fc | 702 | * @since 0.1.1 |
a93fb468 AJ |
703 | */ |
704 | char *sp_get_port_bluetooth_address(const struct sp_port *port); | |
705 | ||
0151b157 ML |
706 | /** |
707 | * Get the operating system handle for a port. | |
708 | * | |
709 | * The type of the handle depends on the operating system. On Unix based | |
710 | * systems, the handle is a file descriptor of type "int". On Windows, the | |
711 | * handle is of type "HANDLE". The user should allocate a variable of the | |
712 | * appropriate type and pass a pointer to this to receive the result. | |
713 | * | |
714 | * To obtain a valid handle, the port must first be opened by calling | |
715 | * sp_open() using the same port structure. | |
716 | * | |
717 | * After the port is closed or the port structure freed, the handle may | |
718 | * no longer be valid. | |
719 | * | |
720 | * @warning This feature is provided so that programs may make use of | |
721 | * OS-specific functionality where desired. Doing so obviously | |
722 | * comes at a cost in portability. It also cannot be guaranteed | |
723 | * that direct usage of the OS handle will not conflict with the | |
724 | * library's own usage of the port. Be careful. | |
725 | * | |
35578b06 UH |
726 | * @param[in] port Pointer to a port structure. Must not be NULL. |
727 | * @param[out] result_ptr If any error is returned, the variable pointed to by | |
ff6da776 UH |
728 | * result_ptr will have unknown contents and should not |
729 | * be used. Otherwise, it will be set to point to the | |
730 | * OS handle. Must not be NULL. | |
35578b06 | 731 | * |
0151b157 | 732 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
733 | * |
734 | * @since 0.1.0 | |
0151b157 ML |
735 | */ |
736 | enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr); | |
737 | ||
cd5f5281 | 738 | /** |
cf9d365c | 739 | * @} |
144a598f UH |
740 | * |
741 | * @defgroup Configuration Configuration | |
742 | * | |
743 | * Setting and querying serial port parameters. | |
cf9d365c UH |
744 | * @{ |
745 | */ | |
e96d8bd2 | 746 | |
9b1502ef ML |
747 | /** |
748 | * Allocate a port configuration structure. | |
749 | * | |
480d750c ML |
750 | * The user should allocate a variable of type "struct sp_port_config *" and |
751 | * pass a pointer to this to receive the result. The variable will be updated | |
752 | * to point to the new configuration structure. The structure is opaque and | |
753 | * must be accessed via the functions provided. | |
9b1502ef ML |
754 | * |
755 | * All parameters in the structure will be initialised to special values which | |
756 | * are ignored by sp_set_config(). | |
757 | * | |
758 | * The structure should be freed after use by calling sp_free_config(). | |
759 | * | |
ff6da776 UH |
760 | * @param[out] config_ptr If any error is returned, the variable pointed to by |
761 | * config_ptr will be set to NULL. Otherwise, it will | |
762 | * be set to point to the allocated config structure. | |
35578b06 | 763 | * Must not be NULL. |
9b1502ef ML |
764 | * |
765 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
766 | * |
767 | * @since 0.1.0 | |
9b1502ef ML |
768 | */ |
769 | enum sp_return sp_new_config(struct sp_port_config **config_ptr); | |
770 | ||
771 | /** | |
772 | * Free a port configuration structure. | |
773 | * | |
35578b06 | 774 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
1652aa86 UH |
775 | * |
776 | * @since 0.1.0 | |
9b1502ef ML |
777 | */ |
778 | void sp_free_config(struct sp_port_config *config); | |
779 | ||
cd5f5281 | 780 | /** |
cf9d365c UH |
781 | * Get the current configuration of the specified serial port. |
782 | * | |
9b1502ef ML |
783 | * The user should allocate a configuration structure using sp_new_config() |
784 | * and pass this as the config parameter. The configuration structure will | |
785 | * be updated with the port configuration. | |
cf9d365c | 786 | * |
9b1502ef ML |
787 | * Any parameters that are configured with settings not recognised or |
788 | * supported by libserialport, will be set to special values that are | |
789 | * ignored by sp_set_config(). | |
cf9d365c | 790 | * |
35578b06 UH |
791 | * @param[in] port Pointer to a port structure. Must not be NULL. |
792 | * @param[out] config Pointer to a configuration structure that will hold | |
ff6da776 UH |
793 | * the result. Upon errors the contents of the config |
794 | * struct will not be changed. Must not be NULL. | |
35578b06 | 795 | * |
f36c6395 | 796 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
797 | * |
798 | * @since 0.1.0 | |
cf9d365c | 799 | */ |
eb6ed20f | 800 | enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config); |
cd5f5281 ML |
801 | |
802 | /** | |
cf9d365c UH |
803 | * Set the configuration for the specified serial port. |
804 | * | |
9b1502ef ML |
805 | * For each parameter in the configuration, there is a special value (usually |
806 | * -1, but see the documentation for each field). These values will be ignored | |
807 | * and the corresponding setting left unchanged on the port. | |
cf9d365c | 808 | * |
ff6da776 UH |
809 | * Upon errors, the configuration of the serial port is unknown since |
810 | * partial/incomplete config updates may have happened. | |
811 | * | |
35578b06 UH |
812 | * @param[in] port Pointer to a port structure. Must not be NULL. |
813 | * @param[in] config Pointer to a configuration structure. Must not be NULL. | |
814 | * | |
f36c6395 | 815 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
816 | * |
817 | * @since 0.1.0 | |
cf9d365c | 818 | */ |
eb6ed20f | 819 | enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config); |
cd5f5281 ML |
820 | |
821 | /** | |
cf9d365c UH |
822 | * Set the baud rate for the specified serial port. |
823 | * | |
35578b06 UH |
824 | * @param[in] port Pointer to a port structure. Must not be NULL. |
825 | * @param[in] baudrate Baud rate in bits per second. | |
cf9d365c | 826 | * |
f36c6395 | 827 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
828 | * |
829 | * @since 0.1.0 | |
cf9d365c | 830 | */ |
eb6ed20f | 831 | enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate); |
cd5f5281 ML |
832 | |
833 | /** | |
9b1502ef ML |
834 | * Get the baud rate from a port configuration. |
835 | * | |
35578b06 UH |
836 | * The user should allocate a variable of type int and |
837 | * pass a pointer to this to receive the result. | |
9b1502ef | 838 | * |
35578b06 UH |
839 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
840 | * @param[out] baudrate_ptr Pointer to a variable to store the result. Must not be NULL. | |
9b1502ef ML |
841 | * |
842 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
843 | * |
844 | * @since 0.1.0 | |
9b1502ef ML |
845 | */ |
846 | enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr); | |
847 | ||
848 | /** | |
849 | * Set the baud rate in a port configuration. | |
850 | * | |
35578b06 UH |
851 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
852 | * @param[in] baudrate Baud rate in bits per second, or -1 to retain the current setting. | |
9b1502ef ML |
853 | * |
854 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
855 | * |
856 | * @since 0.1.0 | |
9b1502ef ML |
857 | */ |
858 | enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate); | |
859 | ||
860 | /** | |
861 | * Set the data bits for the specified serial port. | |
cf9d365c | 862 | * |
35578b06 UH |
863 | * @param[in] port Pointer to a port structure. Must not be NULL. |
864 | * @param[in] bits Number of data bits. | |
cf9d365c | 865 | * |
f36c6395 | 866 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
867 | * |
868 | * @since 0.1.0 | |
cf9d365c | 869 | */ |
eb6ed20f | 870 | enum sp_return sp_set_bits(struct sp_port *port, int bits); |
cd5f5281 ML |
871 | |
872 | /** | |
9b1502ef ML |
873 | * Get the data bits from a port configuration. |
874 | * | |
35578b06 UH |
875 | * The user should allocate a variable of type int and |
876 | * pass a pointer to this to receive the result. | |
9b1502ef | 877 | * |
35578b06 UH |
878 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
879 | * @param[out] bits_ptr Pointer to a variable to store the result. Must not be NULL. | |
9b1502ef ML |
880 | * |
881 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
882 | * |
883 | * @since 0.1.0 | |
9b1502ef ML |
884 | */ |
885 | enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr); | |
886 | ||
887 | /** | |
888 | * Set the data bits in a port configuration. | |
889 | * | |
35578b06 UH |
890 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
891 | * @param[in] bits Number of data bits, or -1 to retain the current setting. | |
9b1502ef ML |
892 | * |
893 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
894 | * |
895 | * @since 0.1.0 | |
9b1502ef ML |
896 | */ |
897 | enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits); | |
898 | ||
899 | /** | |
900 | * Set the parity setting for the specified serial port. | |
cf9d365c | 901 | * |
35578b06 UH |
902 | * @param[in] port Pointer to a port structure. Must not be NULL. |
903 | * @param[in] parity Parity setting. | |
cf9d365c | 904 | * |
f36c6395 | 905 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
906 | * |
907 | * @since 0.1.0 | |
cf9d365c | 908 | */ |
eb6ed20f | 909 | enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity); |
cd5f5281 ML |
910 | |
911 | /** | |
9b1502ef ML |
912 | * Get the parity setting from a port configuration. |
913 | * | |
35578b06 UH |
914 | * The user should allocate a variable of type enum sp_parity and |
915 | * pass a pointer to this to receive the result. | |
9b1502ef | 916 | * |
35578b06 UH |
917 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
918 | * @param[out] parity_ptr Pointer to a variable to store the result. Must not be NULL. | |
9b1502ef ML |
919 | * |
920 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
921 | * |
922 | * @since 0.1.0 | |
9b1502ef ML |
923 | */ |
924 | enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr); | |
925 | ||
926 | /** | |
927 | * Set the parity setting in a port configuration. | |
928 | * | |
35578b06 UH |
929 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
930 | * @param[in] parity Parity setting, or -1 to retain the current setting. | |
9b1502ef ML |
931 | * |
932 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
933 | * |
934 | * @since 0.1.0 | |
9b1502ef ML |
935 | */ |
936 | enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity); | |
937 | ||
938 | /** | |
939 | * Set the stop bits for the specified serial port. | |
cf9d365c | 940 | * |
35578b06 UH |
941 | * @param[in] port Pointer to a port structure. Must not be NULL. |
942 | * @param[in] stopbits Number of stop bits. | |
cf9d365c | 943 | * |
f36c6395 | 944 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
945 | * |
946 | * @since 0.1.0 | |
cf9d365c | 947 | */ |
eb6ed20f | 948 | enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits); |
cd5f5281 ML |
949 | |
950 | /** | |
9b1502ef | 951 | * Get the stop bits from a port configuration. |
cf9d365c | 952 | * |
35578b06 UH |
953 | * The user should allocate a variable of type int and |
954 | * pass a pointer to this to receive the result. | |
cf9d365c | 955 | * |
35578b06 UH |
956 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
957 | * @param[out] stopbits_ptr Pointer to a variable to store the result. Must not be NULL. | |
cf9d365c | 958 | * |
f36c6395 | 959 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
960 | * |
961 | * @since 0.1.0 | |
cf9d365c | 962 | */ |
9b1502ef ML |
963 | enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr); |
964 | ||
965 | /** | |
966 | * Set the stop bits in a port configuration. | |
967 | * | |
35578b06 UH |
968 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
969 | * @param[in] stopbits Number of stop bits, or -1 to retain the current setting. | |
9b1502ef ML |
970 | * |
971 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
972 | * |
973 | * @since 0.1.0 | |
9b1502ef ML |
974 | */ |
975 | enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits); | |
18fc2dd1 | 976 | |
cd5f5281 | 977 | /** |
9b1502ef | 978 | * Set the RTS pin behaviour for the specified serial port. |
cf9d365c | 979 | * |
35578b06 UH |
980 | * @param[in] port Pointer to a port structure. Must not be NULL. |
981 | * @param[in] rts RTS pin mode. | |
cf9d365c | 982 | * |
f36c6395 | 983 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
984 | * |
985 | * @since 0.1.0 | |
cf9d365c | 986 | */ |
eb6ed20f | 987 | enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts); |
cd5f5281 ML |
988 | |
989 | /** | |
9b1502ef ML |
990 | * Get the RTS pin behaviour from a port configuration. |
991 | * | |
35578b06 UH |
992 | * The user should allocate a variable of type enum sp_rts and |
993 | * pass a pointer to this to receive the result. | |
9b1502ef | 994 | * |
35578b06 UH |
995 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
996 | * @param[out] rts_ptr Pointer to a variable to store the result. Must not be NULL. | |
9b1502ef ML |
997 | * |
998 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
999 | * |
1000 | * @since 0.1.0 | |
9b1502ef ML |
1001 | */ |
1002 | enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr); | |
1003 | ||
1004 | /** | |
1005 | * Set the RTS pin behaviour in a port configuration. | |
1006 | * | |
35578b06 UH |
1007 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
1008 | * @param[in] rts RTS pin mode, or -1 to retain the current setting. | |
9b1502ef ML |
1009 | * |
1010 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1011 | * |
1012 | * @since 0.1.0 | |
9b1502ef ML |
1013 | */ |
1014 | enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts); | |
1015 | ||
1016 | /** | |
1017 | * Set the CTS pin behaviour for the specified serial port. | |
cf9d365c | 1018 | * |
35578b06 UH |
1019 | * @param[in] port Pointer to a port structure. Must not be NULL. |
1020 | * @param[in] cts CTS pin mode. | |
cf9d365c | 1021 | * |
f36c6395 | 1022 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1023 | * |
1024 | * @since 0.1.0 | |
cf9d365c | 1025 | */ |
eb6ed20f | 1026 | enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts); |
cd5f5281 ML |
1027 | |
1028 | /** | |
9b1502ef ML |
1029 | * Get the CTS pin behaviour from a port configuration. |
1030 | * | |
35578b06 UH |
1031 | * The user should allocate a variable of type enum sp_cts and |
1032 | * pass a pointer to this to receive the result. | |
9b1502ef | 1033 | * |
35578b06 UH |
1034 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
1035 | * @param[out] cts_ptr Pointer to a variable to store the result. Must not be NULL. | |
9b1502ef ML |
1036 | * |
1037 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1038 | * |
1039 | * @since 0.1.0 | |
9b1502ef ML |
1040 | */ |
1041 | enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr); | |
1042 | ||
1043 | /** | |
1044 | * Set the CTS pin behaviour in a port configuration. | |
1045 | * | |
35578b06 UH |
1046 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
1047 | * @param[in] cts CTS pin mode, or -1 to retain the current setting. | |
9b1502ef ML |
1048 | * |
1049 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1050 | * |
1051 | * @since 0.1.0 | |
9b1502ef ML |
1052 | */ |
1053 | enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts); | |
1054 | ||
1055 | /** | |
1056 | * Set the DTR pin behaviour for the specified serial port. | |
cf9d365c | 1057 | * |
35578b06 UH |
1058 | * @param[in] port Pointer to a port structure. Must not be NULL. |
1059 | * @param[in] dtr DTR pin mode. | |
cf9d365c | 1060 | * |
f36c6395 | 1061 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1062 | * |
1063 | * @since 0.1.0 | |
cf9d365c | 1064 | */ |
eb6ed20f | 1065 | enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr); |
cd5f5281 ML |
1066 | |
1067 | /** | |
9b1502ef ML |
1068 | * Get the DTR pin behaviour from a port configuration. |
1069 | * | |
35578b06 UH |
1070 | * The user should allocate a variable of type enum sp_dtr and |
1071 | * pass a pointer to this to receive the result. | |
9b1502ef | 1072 | * |
35578b06 UH |
1073 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
1074 | * @param[out] dtr_ptr Pointer to a variable to store the result. Must not be NULL. | |
9b1502ef ML |
1075 | * |
1076 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1077 | * |
1078 | * @since 0.1.0 | |
9b1502ef ML |
1079 | */ |
1080 | enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr); | |
1081 | ||
1082 | /** | |
1083 | * Set the DTR pin behaviour in a port configuration. | |
1084 | * | |
35578b06 UH |
1085 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
1086 | * @param[in] dtr DTR pin mode, or -1 to retain the current setting. | |
9b1502ef ML |
1087 | * |
1088 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1089 | * |
1090 | * @since 0.1.0 | |
9b1502ef ML |
1091 | */ |
1092 | enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr); | |
1093 | ||
1094 | /** | |
1095 | * Set the DSR pin behaviour for the specified serial port. | |
cf9d365c | 1096 | * |
35578b06 UH |
1097 | * @param[in] port Pointer to a port structure. Must not be NULL. |
1098 | * @param[in] dsr DSR pin mode. | |
cf9d365c | 1099 | * |
f36c6395 | 1100 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1101 | * |
1102 | * @since 0.1.0 | |
cf9d365c | 1103 | */ |
eb6ed20f | 1104 | enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr); |
cd5f5281 ML |
1105 | |
1106 | /** | |
9b1502ef ML |
1107 | * Get the DSR pin behaviour from a port configuration. |
1108 | * | |
35578b06 UH |
1109 | * The user should allocate a variable of type enum sp_dsr and |
1110 | * pass a pointer to this to receive the result. | |
9b1502ef | 1111 | * |
35578b06 UH |
1112 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
1113 | * @param[out] dsr_ptr Pointer to a variable to store the result. Must not be NULL. | |
9b1502ef ML |
1114 | * |
1115 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1116 | * |
1117 | * @since 0.1.0 | |
9b1502ef ML |
1118 | */ |
1119 | enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr); | |
1120 | ||
1121 | /** | |
1122 | * Set the DSR pin behaviour in a port configuration. | |
1123 | * | |
35578b06 UH |
1124 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
1125 | * @param[in] dsr DSR pin mode, or -1 to retain the current setting. | |
9b1502ef ML |
1126 | * |
1127 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1128 | * |
1129 | * @since 0.1.0 | |
9b1502ef ML |
1130 | */ |
1131 | enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr); | |
1132 | ||
1133 | /** | |
1134 | * Set the XON/XOFF configuration for the specified serial port. | |
cf9d365c | 1135 | * |
35578b06 UH |
1136 | * @param[in] port Pointer to a port structure. Must not be NULL. |
1137 | * @param[in] xon_xoff XON/XOFF mode. | |
cf9d365c | 1138 | * |
f36c6395 | 1139 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1140 | * |
1141 | * @since 0.1.0 | |
cf9d365c | 1142 | */ |
eb6ed20f | 1143 | enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff); |
e96d8bd2 | 1144 | |
9b1502ef ML |
1145 | /** |
1146 | * Get the XON/XOFF configuration from a port configuration. | |
1147 | * | |
35578b06 UH |
1148 | * The user should allocate a variable of type enum sp_xonxoff and |
1149 | * pass a pointer to this to receive the result. | |
9b1502ef | 1150 | * |
35578b06 UH |
1151 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
1152 | * @param[out] xon_xoff_ptr Pointer to a variable to store the result. Must not be NULL. | |
9b1502ef ML |
1153 | * |
1154 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1155 | * |
1156 | * @since 0.1.0 | |
9b1502ef ML |
1157 | */ |
1158 | enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr); | |
1159 | ||
1160 | /** | |
1161 | * Set the XON/XOFF configuration in a port configuration. | |
1162 | * | |
35578b06 UH |
1163 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
1164 | * @param[in] xon_xoff XON/XOFF mode, or -1 to retain the current setting. | |
9b1502ef ML |
1165 | * |
1166 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1167 | * |
1168 | * @since 0.1.0 | |
9b1502ef ML |
1169 | */ |
1170 | enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff); | |
1171 | ||
1172 | /** | |
1173 | * Set the flow control type in a port configuration. | |
1174 | * | |
1175 | * This function is a wrapper that sets the RTS, CTS, DTR, DSR and | |
1176 | * XON/XOFF settings as necessary for the specified flow control | |
1177 | * type. For more fine-grained control of these settings, use their | |
1178 | * individual configuration functions. | |
1179 | * | |
35578b06 UH |
1180 | * @param[in] config Pointer to a configuration structure. Must not be NULL. |
1181 | * @param[in] flowcontrol Flow control setting to use. | |
9b1502ef ML |
1182 | * |
1183 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1184 | * |
1185 | * @since 0.1.0 | |
9b1502ef ML |
1186 | */ |
1187 | enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol); | |
1188 | ||
1189 | /** | |
1190 | * Set the flow control type for the specified serial port. | |
1191 | * | |
1192 | * This function is a wrapper that sets the RTS, CTS, DTR, DSR and | |
1193 | * XON/XOFF settings as necessary for the specified flow control | |
1194 | * type. For more fine-grained control of these settings, use their | |
1195 | * individual configuration functions. | |
1196 | * | |
35578b06 UH |
1197 | * @param[in] port Pointer to a port structure. Must not be NULL. |
1198 | * @param[in] flowcontrol Flow control setting to use. | |
9b1502ef ML |
1199 | * |
1200 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1201 | * |
1202 | * @since 0.1.0 | |
9b1502ef ML |
1203 | */ |
1204 | enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol); | |
1205 | ||
091e75fe | 1206 | /** |
cf9d365c | 1207 | * @} |
144a598f UH |
1208 | * |
1209 | * @defgroup Data Data handling | |
1210 | * | |
1211 | * Reading, writing, and flushing data. | |
1212 | * | |
cf9d365c | 1213 | * @{ |
144a598f | 1214 | */ |
091e75fe ML |
1215 | |
1216 | /** | |
2007ce5e ML |
1217 | * Read bytes from the specified serial port, blocking until complete. |
1218 | * | |
b87deb7c ML |
1219 | * @warning If your program runs on Unix, defines its own signal handlers, and |
1220 | * needs to abort blocking reads when these are called, then you | |
1221 | * should not use this function. It repeats system calls that return | |
1222 | * with EINTR. To be able to abort a read from a signal handler, you | |
1223 | * should implement your own blocking read using sp_nonblocking_read() | |
2007ce5e ML |
1224 | * together with a blocking method that makes sense for your program. |
1225 | * E.g. you can obtain the file descriptor for an open port using | |
1226 | * sp_get_port_handle() and use this to call select() or pselect(), | |
1227 | * with appropriate arrangements to return if a signal is received. | |
cf9d365c | 1228 | * |
35578b06 UH |
1229 | * @param[in] port Pointer to a port structure. Must not be NULL. |
1230 | * @param[out] buf Buffer in which to store the bytes read. Must not be NULL. | |
1231 | * @param[in] count Requested number of bytes to read. | |
1b91c6ea | 1232 | * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely. |
e3dcf906 ML |
1233 | * |
1234 | * @return The number of bytes read on success, or a negative error code. If | |
1235 | * the number of bytes returned is less than that requested, the | |
1236 | * timeout was reached before the requested number of bytes was | |
1237 | * available. If timeout is zero, the function will always return | |
1238 | * either the requested number of bytes or a negative error code. | |
1652aa86 UH |
1239 | * |
1240 | * @since 0.1.0 | |
e3dcf906 | 1241 | */ |
1b91c6ea | 1242 | enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms); |
e3dcf906 | 1243 | |
e5c2630e ML |
1244 | /** |
1245 | * Read bytes from the specified serial port, returning as soon as any data is | |
1246 | * available. | |
1247 | * | |
1248 | * @warning If your program runs on Unix, defines its own signal handlers, and | |
1249 | * needs to abort blocking reads when these are called, then you | |
1250 | * should not use this function. It repeats system calls that return | |
1251 | * with EINTR. To be able to abort a read from a signal handler, you | |
1252 | * should implement your own blocking read using sp_nonblocking_read() | |
1253 | * together with a blocking method that makes sense for your program. | |
1254 | * E.g. you can obtain the file descriptor for an open port using | |
1255 | * sp_get_port_handle() and use this to call select() or pselect(), | |
1256 | * with appropriate arrangements to return if a signal is received. | |
1257 | * | |
1258 | * @param[in] port Pointer to a port structure. Must not be NULL. | |
1259 | * @param[out] buf Buffer in which to store the bytes read. Must not be NULL. | |
1260 | * @param[in] count Maximum number of bytes to read. Must not be zero. | |
1261 | * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely. | |
1262 | * | |
1263 | * @return The number of bytes read on success, or a negative error code. If | |
1264 | * the result is zero, the timeout was reached before any bytes were | |
1265 | * available. If timeout_ms is zero, the function will always return | |
1266 | * either at least one byte, or a negative error code. | |
1267 | * | |
1268 | * @since 0.1.1 | |
1269 | */ | |
1270 | enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms); | |
1271 | ||
e3dcf906 ML |
1272 | /** |
1273 | * Read bytes from the specified serial port, without blocking. | |
cf9d365c | 1274 | * |
35578b06 UH |
1275 | * @param[in] port Pointer to a port structure. Must not be NULL. |
1276 | * @param[out] buf Buffer in which to store the bytes read. Must not be NULL. | |
1277 | * @param[in] count Maximum number of bytes to read. | |
cf9d365c | 1278 | * |
e3dcf906 ML |
1279 | * @return The number of bytes read on success, or a negative error code. The |
1280 | * number of bytes returned may be any number from zero to the maximum | |
1281 | * that was requested. | |
1652aa86 UH |
1282 | * |
1283 | * @since 0.1.0 | |
e3dcf906 ML |
1284 | */ |
1285 | enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count); | |
1286 | ||
1287 | /** | |
1288 | * Write bytes to the specified serial port, blocking until complete. | |
1289 | * | |
1290 | * Note that this function only ensures that the accepted bytes have been | |
1291 | * written to the OS; they may be held in driver or hardware buffers and not | |
1292 | * yet physically transmitted. To check whether all written bytes have actually | |
1293 | * been transmitted, use the sp_output_waiting() function. To wait until all | |
1294 | * written bytes have actually been transmitted, use the sp_drain() function. | |
1295 | * | |
b87deb7c ML |
1296 | * @warning If your program runs on Unix, defines its own signal handlers, and |
1297 | * needs to abort blocking writes when these are called, then you | |
1298 | * should not use this function. It repeats system calls that return | |
1299 | * with EINTR. To be able to abort a write from a signal handler, you | |
1300 | * should implement your own blocking write using sp_nonblocking_write() | |
2007ce5e ML |
1301 | * together with a blocking method that makes sense for your program. |
1302 | * E.g. you can obtain the file descriptor for an open port using | |
1303 | * sp_get_port_handle() and use this to call select() or pselect(), | |
1304 | * with appropriate arrangements to return if a signal is received. | |
1305 | * | |
35578b06 UH |
1306 | * @param[in] port Pointer to a port structure. Must not be NULL. |
1307 | * @param[in] buf Buffer containing the bytes to write. Must not be NULL. | |
1308 | * @param[in] count Requested number of bytes to write. | |
1b91c6ea | 1309 | * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely. |
e3dcf906 | 1310 | * |
85987464 ML |
1311 | * @return The number of bytes written on success, or a negative error code. |
1312 | * If the number of bytes returned is less than that requested, the | |
e3dcf906 | 1313 | * timeout was reached before the requested number of bytes was |
85987464 | 1314 | * written. If timeout is zero, the function will always return |
e3dcf906 ML |
1315 | * either the requested number of bytes or a negative error code. In |
1316 | * the event of an error there is no way to determine how many bytes | |
cfa5af67 | 1317 | * were sent before the error occurred. |
1652aa86 UH |
1318 | * |
1319 | * @since 0.1.0 | |
cf9d365c | 1320 | */ |
1b91c6ea | 1321 | enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout_ms); |
091e75fe ML |
1322 | |
1323 | /** | |
e3dcf906 | 1324 | * Write bytes to the specified serial port, without blocking. |
cf9d365c | 1325 | * |
e3dcf906 ML |
1326 | * Note that this function only ensures that the accepted bytes have been |
1327 | * written to the OS; they may be held in driver or hardware buffers and not | |
1328 | * yet physically transmitted. To check whether all written bytes have actually | |
1329 | * been transmitted, use the sp_output_waiting() function. To wait until all | |
1330 | * written bytes have actually been transmitted, use the sp_drain() function. | |
cf9d365c | 1331 | * |
35578b06 UH |
1332 | * @param[in] port Pointer to a port structure. Must not be NULL. |
1333 | * @param[in] buf Buffer containing the bytes to write. Must not be NULL. | |
1334 | * @param[in] count Maximum number of bytes to write. | |
cf9d365c | 1335 | * |
f36c6395 | 1336 | * @return The number of bytes written on success, or a negative error code. |
e3dcf906 ML |
1337 | * The number of bytes returned may be any number from zero to the |
1338 | * maximum that was requested. | |
1652aa86 UH |
1339 | * |
1340 | * @since 0.1.0 | |
cf9d365c | 1341 | */ |
e3dcf906 | 1342 | enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count); |
091e75fe | 1343 | |
3353c22f ML |
1344 | /** |
1345 | * Gets the number of bytes waiting in the input buffer. | |
1346 | * | |
35578b06 | 1347 | * @param[in] port Pointer to a port structure. Must not be NULL. |
3353c22f ML |
1348 | * |
1349 | * @return Number of bytes waiting on success, a negative error code otherwise. | |
1652aa86 UH |
1350 | * |
1351 | * @since 0.1.0 | |
3353c22f ML |
1352 | */ |
1353 | enum sp_return sp_input_waiting(struct sp_port *port); | |
1354 | ||
1355 | /** | |
1356 | * Gets the number of bytes waiting in the output buffer. | |
1357 | * | |
35578b06 | 1358 | * @param[in] port Pointer to a port structure. Must not be NULL. |
3353c22f ML |
1359 | * |
1360 | * @return Number of bytes waiting on success, a negative error code otherwise. | |
1652aa86 UH |
1361 | * |
1362 | * @since 0.1.0 | |
3353c22f ML |
1363 | */ |
1364 | enum sp_return sp_output_waiting(struct sp_port *port); | |
1365 | ||
091e75fe | 1366 | /** |
fd8fd11a ML |
1367 | * Flush serial port buffers. Data in the selected buffer(s) is discarded. |
1368 | * | |
35578b06 UH |
1369 | * @param[in] port Pointer to a port structure. Must not be NULL. |
1370 | * @param[in] buffers Which buffer(s) to flush. | |
cf9d365c | 1371 | * |
f36c6395 | 1372 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1373 | * |
1374 | * @since 0.1.0 | |
cf9d365c | 1375 | */ |
fd8fd11a | 1376 | enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers); |
091e75fe | 1377 | |
69a3739c ML |
1378 | /** |
1379 | * Wait for buffered data to be transmitted. | |
1380 | * | |
2c827b21 ML |
1381 | * @warning If your program runs on Unix, defines its own signal handlers, and |
1382 | * needs to abort draining the output buffer when when these are | |
1383 | * called, then you should not use this function. It repeats system | |
1384 | * calls that return with EINTR. To be able to abort a drain from a | |
1385 | * signal handler, you would need to implement your own blocking | |
1386 | * drain by polling the result of sp_output_waiting(). | |
1387 | * | |
35578b06 | 1388 | * @param[in] port Pointer to a port structure. Must not be NULL. |
3f099f4f | 1389 | * |
f36c6395 | 1390 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1391 | * |
1392 | * @since 0.1.0 | |
69a3739c ML |
1393 | */ |
1394 | enum sp_return sp_drain(struct sp_port *port); | |
1395 | ||
6f1186aa ML |
1396 | /** |
1397 | * @} | |
144a598f UH |
1398 | * |
1399 | * @defgroup Waiting Waiting | |
1400 | * | |
1401 | * Waiting for events and timeout handling. | |
1402 | * | |
6f1186aa ML |
1403 | * @{ |
1404 | */ | |
1405 | ||
1406 | /** | |
1407 | * Allocate storage for a set of events. | |
1408 | * | |
1409 | * The user should allocate a variable of type struct sp_event_set *, | |
1410 | * then pass a pointer to this variable to receive the result. | |
1411 | * | |
1412 | * The result should be freed after use by calling sp_free_event_set(). | |
1413 | * | |
35578b06 UH |
1414 | * @param[out] result_ptr If any error is returned, the variable pointed to by |
1415 | * result_ptr will be set to NULL. Otherwise, it will | |
1416 | * be set to point to the event set. Must not be NULL. | |
1417 | * | |
6f1186aa | 1418 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1419 | * |
1420 | * @since 0.1.0 | |
6f1186aa ML |
1421 | */ |
1422 | enum sp_return sp_new_event_set(struct sp_event_set **result_ptr); | |
1423 | ||
1424 | /** | |
1425 | * Add events to a struct sp_event_set for a given port. | |
1426 | * | |
1427 | * The port must first be opened by calling sp_open() using the same port | |
1428 | * structure. | |
1429 | * | |
1430 | * After the port is closed or the port structure freed, the results may | |
1431 | * no longer be valid. | |
1432 | * | |
35578b06 UH |
1433 | * @param[in,out] event_set Event set to update. Must not be NULL. |
1434 | * @param[in] port Pointer to a port structure. Must not be NULL. | |
1435 | * @param[in] mask Bitmask of events to be waited for. | |
6f1186aa ML |
1436 | * |
1437 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1438 | * |
1439 | * @since 0.1.0 | |
6f1186aa ML |
1440 | */ |
1441 | enum sp_return sp_add_port_events(struct sp_event_set *event_set, | |
1442 | const struct sp_port *port, enum sp_event mask); | |
1443 | ||
1444 | /** | |
1445 | * Wait for any of a set of events to occur. | |
1446 | * | |
35578b06 | 1447 | * @param[in] event_set Event set to wait on. Must not be NULL. |
1b91c6ea | 1448 | * @param[in] timeout_ms Timeout in milliseconds, or zero to wait indefinitely. |
6f1186aa ML |
1449 | * |
1450 | * @return SP_OK upon success, a negative error code otherwise. | |
1652aa86 UH |
1451 | * |
1452 | * @since 0.1.0 | |
6f1186aa | 1453 | */ |
1b91c6ea | 1454 | enum sp_return sp_wait(struct sp_event_set *event_set, unsigned int timeout_ms); |
6f1186aa ML |
1455 | |
1456 | /** | |
1457 | * Free a structure allocated by sp_new_event_set(). | |
1652aa86 | 1458 | * |
35578b06 UH |
1459 | * @param[in] event_set Event set to free. Must not be NULL. |
1460 | * | |
1652aa86 | 1461 | * @since 0.1.0 |
6f1186aa ML |
1462 | */ |
1463 | void sp_free_event_set(struct sp_event_set *event_set); | |
1464 | ||
90cc3ee6 ML |
1465 | /** |
1466 | * @} | |
144a598f UH |
1467 | * |
1468 | * @defgroup Signals Signals | |
1469 | * | |
1470 | * Port signalling operations. | |
1471 | * | |
90cc3ee6 ML |
1472 | * @{ |
1473 | */ | |
1474 | ||
8cf7c697 ML |
1475 | /** |
1476 | * Gets the status of the control signals for the specified port. | |
1477 | * | |
1478 | * The user should allocate a variable of type "enum sp_signal" and pass a | |
1479 | * pointer to this variable to receive the result. The result is a bitmask | |
1480 | * in which individual signals can be checked by bitwise OR with values of | |
1481 | * the sp_signal enum. | |
1482 | * | |
35578b06 UH |
1483 | * @param[in] port Pointer to a port structure. Must not be NULL. |
1484 | * @param[out] signal_mask Pointer to a variable to receive the result. | |
1485 | * Must not be NULL. | |
8cf7c697 | 1486 | * |
f36c6395 | 1487 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1488 | * |
1489 | * @since 0.1.0 | |
8cf7c697 | 1490 | */ |
f3ccf157 | 1491 | enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signal_mask); |
8cf7c697 | 1492 | |
90cc3ee6 ML |
1493 | /** |
1494 | * Put the port transmit line into the break state. | |
1495 | * | |
35578b06 | 1496 | * @param[in] port Pointer to a port structure. Must not be NULL. |
3f099f4f | 1497 | * |
f36c6395 | 1498 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1499 | * |
1500 | * @since 0.1.0 | |
90cc3ee6 ML |
1501 | */ |
1502 | enum sp_return sp_start_break(struct sp_port *port); | |
1503 | ||
1504 | /** | |
1505 | * Take the port transmit line out of the break state. | |
1506 | * | |
35578b06 | 1507 | * @param[in] port Pointer to a port structure. Must not be NULL. |
3f099f4f | 1508 | * |
f36c6395 | 1509 | * @return SP_OK upon success, a negative error code otherwise. |
1652aa86 UH |
1510 | * |
1511 | * @since 0.1.0 | |
90cc3ee6 ML |
1512 | */ |
1513 | enum sp_return sp_end_break(struct sp_port *port); | |
1514 | ||
091e75fe | 1515 | /** |
cf9d365c | 1516 | * @} |
144a598f UH |
1517 | * |
1518 | * @defgroup Errors Errors | |
1519 | * | |
1520 | * Obtaining error information. | |
1521 | * | |
cf9d365c | 1522 | * @{ |
144a598f | 1523 | */ |
091e75fe | 1524 | |
cd5f5281 | 1525 | /** |
cf9d365c UH |
1526 | * Get the error code for a failed operation. |
1527 | * | |
1528 | * In order to obtain the correct result, this function should be called | |
1529 | * straight after the failure, before executing any other system operations. | |
ec4b55ae ML |
1530 | * The result is thread-specific, and only valid when called immediately |
1531 | * after a previous call returning SP_ERR_FAIL. | |
cf9d365c UH |
1532 | * |
1533 | * @return The system's numeric code for the error that caused the last | |
1534 | * operation to fail. | |
1652aa86 UH |
1535 | * |
1536 | * @since 0.1.0 | |
cf9d365c | 1537 | */ |
74510d4b | 1538 | int sp_last_error_code(void); |
cd5f5281 ML |
1539 | |
1540 | /** | |
cf9d365c UH |
1541 | * Get the error message for a failed operation. |
1542 | * | |
1543 | * In order to obtain the correct result, this function should be called | |
1544 | * straight after the failure, before executing other system operations. | |
ec4b55ae ML |
1545 | * The result is thread-specific, and only valid when called immediately |
1546 | * after a previous call returning SP_ERR_FAIL. | |
cf9d365c UH |
1547 | * |
1548 | * @return The system's message for the error that caused the last | |
1549 | * operation to fail. This string may be allocated by the function, | |
1550 | * and should be freed after use by calling sp_free_error_message(). | |
1652aa86 UH |
1551 | * |
1552 | * @since 0.1.0 | |
cf9d365c | 1553 | */ |
74510d4b | 1554 | char *sp_last_error_message(void); |
cd5f5281 ML |
1555 | |
1556 | /** | |
cf9d365c | 1557 | * Free an error message returned by sp_last_error_message(). |
1652aa86 | 1558 | * |
35578b06 UH |
1559 | * @param[in] message The error message string to free. Must not be NULL. |
1560 | * | |
1652aa86 | 1561 | * @since 0.1.0 |
cf9d365c | 1562 | */ |
74510d4b | 1563 | void sp_free_error_message(char *message); |
e8ffaee9 | 1564 | |
863b35e6 ML |
1565 | /** |
1566 | * Set the handler function for library debugging messages. | |
1567 | * | |
1568 | * Debugging messages are generated by the library during each operation, | |
1569 | * to help in diagnosing problems. The handler will be called for each | |
1570 | * message. The handler can be set to NULL to ignore all debug messages. | |
1571 | * | |
1572 | * The handler function should accept a format string and variable length | |
1573 | * argument list, in the same manner as e.g. printf(). | |
1574 | * | |
1575 | * The default handler is sp_default_debug_handler(). | |
1652aa86 | 1576 | * |
35578b06 UH |
1577 | * @param[in] handler The handler function to use. Can be NULL (in that case |
1578 | * all debug messages will be ignored). | |
1579 | * | |
1652aa86 | 1580 | * @since 0.1.0 |
863b35e6 ML |
1581 | */ |
1582 | void sp_set_debug_handler(void (*handler)(const char *format, ...)); | |
1583 | ||
1584 | /** | |
1585 | * Default handler function for library debugging messages. | |
1586 | * | |
1587 | * This function prints debug messages to the standard error stream if the | |
1588 | * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are | |
1589 | * ignored. | |
1652aa86 | 1590 | * |
35578b06 UH |
1591 | * @param[in] format The format string to use. Must not be NULL. |
1592 | * @param[in] ... The variable length argument list to use. | |
1593 | * | |
1652aa86 | 1594 | * @since 0.1.0 |
863b35e6 ML |
1595 | */ |
1596 | void sp_default_debug_handler(const char *format, ...); | |
1597 | ||
cf9d365c | 1598 | /** @} */ |
091e75fe | 1599 | |
524b0e14 | 1600 | /** |
144a598f UH |
1601 | * @defgroup Versions Versions |
1602 | * | |
1603 | * Version number querying functions, definitions, and macros. | |
524b0e14 UH |
1604 | * |
1605 | * This set of API calls returns two different version numbers related | |
1606 | * to libserialport. The "package version" is the release version number of the | |
1607 | * libserialport tarball in the usual "major.minor.micro" format, e.g. "0.1.0". | |
1608 | * | |
1609 | * The "library version" is independent of that; it is the libtool version | |
1610 | * number in the "current:revision:age" format, e.g. "2:0:0". | |
1611 | * See http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning for details. | |
1612 | * | |
1613 | * Both version numbers (and/or individual components of them) can be | |
1614 | * retrieved via the API calls at runtime, and/or they can be checked at | |
1615 | * compile/preprocessor time using the respective macros. | |
1616 | * | |
1617 | * @{ | |
1618 | */ | |
1619 | ||
1620 | /* | |
1621 | * Package version macros (can be used for conditional compilation). | |
1622 | */ | |
1623 | ||
1624 | /** The libserialport package 'major' version number. */ | |
f1c916ed | 1625 | #undef SP_PACKAGE_VERSION_MAJOR |
524b0e14 UH |
1626 | |
1627 | /** The libserialport package 'minor' version number. */ | |
f1c916ed | 1628 | #undef SP_PACKAGE_VERSION_MINOR |
524b0e14 UH |
1629 | |
1630 | /** The libserialport package 'micro' version number. */ | |
f1c916ed | 1631 | #undef SP_PACKAGE_VERSION_MICRO |
524b0e14 UH |
1632 | |
1633 | /** The libserialport package version ("major.minor.micro") as string. */ | |
f1c916ed | 1634 | #undef SP_PACKAGE_VERSION_STRING |
524b0e14 UH |
1635 | |
1636 | /* | |
1637 | * Library/libtool version macros (can be used for conditional compilation). | |
1638 | */ | |
1639 | ||
1640 | /** The libserialport libtool 'current' version number. */ | |
f1c916ed | 1641 | #undef SP_LIB_VERSION_CURRENT |
524b0e14 UH |
1642 | |
1643 | /** The libserialport libtool 'revision' version number. */ | |
f1c916ed | 1644 | #undef SP_LIB_VERSION_REVISION |
524b0e14 UH |
1645 | |
1646 | /** The libserialport libtool 'age' version number. */ | |
f1c916ed | 1647 | #undef SP_LIB_VERSION_AGE |
524b0e14 UH |
1648 | |
1649 | /** The libserialport libtool version ("current:revision:age") as string. */ | |
f1c916ed | 1650 | #undef SP_LIB_VERSION_STRING |
524b0e14 UH |
1651 | |
1652 | /** | |
1653 | * Get the major libserialport package version number. | |
1654 | * | |
1655 | * @return The major package version number. | |
1656 | * | |
1657 | * @since 0.1.0 | |
1658 | */ | |
1659 | int sp_get_major_package_version(void); | |
1660 | ||
1661 | /** | |
1662 | * Get the minor libserialport package version number. | |
1663 | * | |
1664 | * @return The minor package version number. | |
1665 | * | |
1666 | * @since 0.1.0 | |
1667 | */ | |
1668 | int sp_get_minor_package_version(void); | |
1669 | ||
1670 | /** | |
1671 | * Get the micro libserialport package version number. | |
1672 | * | |
1673 | * @return The micro package version number. | |
1674 | * | |
1675 | * @since 0.1.0 | |
1676 | */ | |
1677 | int sp_get_micro_package_version(void); | |
1678 | ||
1679 | /** | |
1680 | * Get the libserialport package version number as a string. | |
1681 | * | |
1682 | * @return The package version number string. The returned string is | |
1683 | * static and thus should NOT be free'd by the caller. | |
1684 | * | |
1685 | * @since 0.1.0 | |
1686 | */ | |
1687 | const char *sp_get_package_version_string(void); | |
1688 | ||
1689 | /** | |
1690 | * Get the "current" part of the libserialport library version number. | |
1691 | * | |
1692 | * @return The "current" library version number. | |
1693 | * | |
1694 | * @since 0.1.0 | |
1695 | */ | |
1696 | int sp_get_current_lib_version(void); | |
1697 | ||
1698 | /** | |
1699 | * Get the "revision" part of the libserialport library version number. | |
1700 | * | |
1701 | * @return The "revision" library version number. | |
1702 | * | |
1703 | * @since 0.1.0 | |
1704 | */ | |
1705 | int sp_get_revision_lib_version(void); | |
1706 | ||
1707 | /** | |
1708 | * Get the "age" part of the libserialport library version number. | |
1709 | * | |
1710 | * @return The "age" library version number. | |
1711 | * | |
1712 | * @since 0.1.0 | |
1713 | */ | |
1714 | int sp_get_age_lib_version(void); | |
1715 | ||
1716 | /** | |
1717 | * Get the libserialport library version number as a string. | |
1718 | * | |
1719 | * @return The library version number string. The returned string is | |
1720 | * static and thus should NOT be free'd by the caller. | |
1721 | * | |
1722 | * @since 0.1.0 | |
1723 | */ | |
1724 | const char *sp_get_lib_version_string(void); | |
1725 | ||
1726 | /** @} */ | |
1727 | ||
7c8d67ef ML |
1728 | /** |
1729 | * @example list_ports.c Getting a list of ports present on the system. | |
1730 | * @example port_info.c Getting information on a particular serial port. | |
1731 | */ | |
1732 | ||
5ef8a1ed UH |
1733 | #ifdef __cplusplus |
1734 | } | |
1735 | #endif | |
1736 | ||
8645feda | 1737 | #endif |