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