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