]>
Commit | Line | Data |
---|---|---|
74510d4b ML |
1 | /* |
2 | * This file is part of the libserialport project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> | |
f77bb46d ML |
5 | * Copyright (C) 2010-2015 Uwe Hermann <uwe@hermann-uwe.de> |
6 | * Copyright (C) 2013-2015 Martin Ling <martin-libserialport@earth.li> | |
31b3a8f5 | 7 | * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz> |
a93fb468 | 8 | * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org> |
74510d4b ML |
9 | * |
10 | * This program is free software: you can redistribute it and/or modify | |
11 | * it under the terms of the GNU Lesser General Public License as | |
12 | * published by the Free Software Foundation, either version 3 of the | |
13 | * License, or (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU Lesser General Public License | |
21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 | */ | |
23 | ||
1a584c45 | 24 | #include <config.h> |
f6a1fb65 | 25 | #include "libserialport.h" |
e33dcf90 | 26 | #include "libserialport_internal.h" |
da2748bf | 27 | |
6c6aebe7 | 28 | static const struct std_baudrate std_baudrates[] = { |
da2748bf ML |
29 | #ifdef _WIN32 |
30 | /* | |
31 | * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to | |
32 | * have documented CBR_* macros. | |
33 | */ | |
34 | BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800), | |
35 | BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600), | |
eac329d2 | 36 | BAUD(115200), BAUD(128000), BAUD(256000), |
da2748bf | 37 | #else |
eac329d2 UH |
38 | BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200), |
39 | BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800), | |
40 | BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200), | |
41 | BAUD(230400), | |
da2748bf | 42 | #if !defined(__APPLE__) && !defined(__OpenBSD__) |
eac329d2 | 43 | BAUD(460800), |
da2748bf ML |
44 | #endif |
45 | #endif | |
46 | }; | |
47 | ||
6c6aebe7 UH |
48 | #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates) |
49 | ||
863b35e6 ML |
50 | void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler; |
51 | ||
eb6ed20f ML |
52 | static enum sp_return get_config(struct sp_port *port, struct port_data *data, |
53 | struct sp_port_config *config); | |
e33dcf90 | 54 | |
eb6ed20f ML |
55 | static enum sp_return set_config(struct sp_port *port, struct port_data *data, |
56 | const struct sp_port_config *config); | |
80186526 | 57 | |
970f279a | 58 | SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr) |
d54e9004 ML |
59 | { |
60 | struct sp_port *port; | |
5a8810e2 | 61 | #ifndef NO_PORT_METADATA |
a93fb468 | 62 | enum sp_return ret; |
5a8810e2 | 63 | #endif |
5919c913 | 64 | int len; |
d54e9004 | 65 | |
c33efc48 ML |
66 | TRACE("%s, %p", portname, port_ptr); |
67 | ||
32b5ac05 | 68 | if (!port_ptr) |
c33efc48 | 69 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); |
32b5ac05 | 70 | |
77f262c4 ML |
71 | *port_ptr = NULL; |
72 | ||
d4babed2 | 73 | if (!portname) |
c33efc48 | 74 | RETURN_ERROR(SP_ERR_ARG, "Null port name"); |
d4babed2 | 75 | |
7890cef6 | 76 | DEBUG_FMT("Building structure for port %s", portname); |
ea667be7 | 77 | |
95bad38c ST |
78 | #if !defined(_WIN32) && defined(HAVE_REALPATH) |
79 | /* | |
80 | * get_port_details() below tries to be too smart and figure out | |
81 | * some transport properties from the port name which breaks with | |
82 | * symlinks. Therefore we canonicalize the portname first. | |
83 | */ | |
84 | char pathbuf[PATH_MAX + 1]; | |
85 | char *res = realpath(portname, pathbuf); | |
86 | if (!res) | |
87 | RETURN_ERROR(SP_ERR_ARG, "Could not retrieve realpath behind port name"); | |
88 | ||
89 | portname = pathbuf; | |
90 | #endif | |
91 | ||
d54e9004 | 92 | if (!(port = malloc(sizeof(struct sp_port)))) |
c33efc48 | 93 | RETURN_ERROR(SP_ERR_MEM, "Port structure malloc failed"); |
d54e9004 | 94 | |
5919c913 ML |
95 | len = strlen(portname) + 1; |
96 | ||
eac329d2 | 97 | if (!(port->name = malloc(len))) { |
d54e9004 | 98 | free(port); |
c33efc48 | 99 | RETURN_ERROR(SP_ERR_MEM, "Port name malloc failed"); |
d54e9004 ML |
100 | } |
101 | ||
102 | memcpy(port->name, portname, len); | |
103 | ||
8f471c66 | 104 | #ifdef _WIN32 |
e31f2c6b | 105 | port->usb_path = NULL; |
8f471c66 ML |
106 | port->hdl = INVALID_HANDLE_VALUE; |
107 | #else | |
108 | port->fd = -1; | |
109 | #endif | |
110 | ||
e33dcf90 ML |
111 | port->description = NULL; |
112 | port->transport = SP_TRANSPORT_NATIVE; | |
113 | port->usb_bus = -1; | |
114 | port->usb_address = -1; | |
115 | port->usb_vid = -1; | |
116 | port->usb_pid = -1; | |
117 | port->usb_manufacturer = NULL; | |
118 | port->usb_product = NULL; | |
119 | port->usb_serial = NULL; | |
120 | port->bluetooth_address = NULL; | |
121 | ||
e4ce975a | 122 | #ifndef NO_PORT_METADATA |
e33dcf90 | 123 | if ((ret = get_port_details(port)) != SP_OK) { |
a93fb468 AJ |
124 | sp_free_port(port); |
125 | return ret; | |
126 | } | |
e4ce975a | 127 | #endif |
a93fb468 | 128 | |
77f262c4 ML |
129 | *port_ptr = port; |
130 | ||
c33efc48 | 131 | RETURN_OK(); |
d54e9004 ML |
132 | } |
133 | ||
970f279a | 134 | SP_API char *sp_get_port_name(const struct sp_port *port) |
1c5aae9d ML |
135 | { |
136 | TRACE("%p", port); | |
137 | ||
138 | if (!port) | |
139 | return NULL; | |
140 | ||
9caa2e86 | 141 | RETURN_STRING(port->name); |
1c5aae9d ML |
142 | } |
143 | ||
f1480939 | 144 | SP_API char *sp_get_port_description(const struct sp_port *port) |
a93fb468 AJ |
145 | { |
146 | TRACE("%p", port); | |
147 | ||
148 | if (!port || !port->description) | |
149 | return NULL; | |
150 | ||
9caa2e86 | 151 | RETURN_STRING(port->description); |
a93fb468 AJ |
152 | } |
153 | ||
ad036cc8 | 154 | SP_API enum sp_transport sp_get_port_transport(const struct sp_port *port) |
a93fb468 AJ |
155 | { |
156 | TRACE("%p", port); | |
157 | ||
158 | if (!port) | |
159 | RETURN_ERROR(SP_ERR_ARG, "Null port"); | |
160 | ||
9caa2e86 | 161 | RETURN_INT(port->transport); |
a93fb468 AJ |
162 | } |
163 | ||
970f279a AJ |
164 | SP_API enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port, |
165 | int *usb_bus,int *usb_address) | |
a93fb468 AJ |
166 | { |
167 | TRACE("%p", port); | |
168 | ||
169 | if (!port) | |
170 | RETURN_ERROR(SP_ERR_ARG, "Null port"); | |
171 | if (port->transport != SP_TRANSPORT_USB) | |
172 | RETURN_ERROR(SP_ERR_ARG, "Port does not use USB transport"); | |
65172bef AJ |
173 | if (port->usb_bus < 0 || port->usb_address < 0) |
174 | RETURN_ERROR(SP_ERR_SUPP, "Bus and address values are not available"); | |
a93fb468 | 175 | |
dc422c04 UH |
176 | if (usb_bus) |
177 | *usb_bus = port->usb_bus; | |
178 | if (usb_address) | |
179 | *usb_address = port->usb_address; | |
a93fb468 AJ |
180 | |
181 | RETURN_OK(); | |
182 | } | |
183 | ||
970f279a AJ |
184 | SP_API enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, |
185 | int *usb_vid, int *usb_pid) | |
a93fb468 AJ |
186 | { |
187 | TRACE("%p", port); | |
188 | ||
189 | if (!port) | |
190 | RETURN_ERROR(SP_ERR_ARG, "Null port"); | |
191 | if (port->transport != SP_TRANSPORT_USB) | |
192 | RETURN_ERROR(SP_ERR_ARG, "Port does not use USB transport"); | |
65172bef AJ |
193 | if (port->usb_vid < 0 || port->usb_pid < 0) |
194 | RETURN_ERROR(SP_ERR_SUPP, "VID:PID values are not available"); | |
a93fb468 | 195 | |
dc422c04 UH |
196 | if (usb_vid) |
197 | *usb_vid = port->usb_vid; | |
198 | if (usb_pid) | |
199 | *usb_pid = port->usb_pid; | |
a93fb468 AJ |
200 | |
201 | RETURN_OK(); | |
202 | } | |
203 | ||
970f279a | 204 | SP_API char *sp_get_port_usb_manufacturer(const struct sp_port *port) |
a93fb468 AJ |
205 | { |
206 | TRACE("%p", port); | |
207 | ||
208 | if (!port || port->transport != SP_TRANSPORT_USB || !port->usb_manufacturer) | |
209 | return NULL; | |
210 | ||
9caa2e86 | 211 | RETURN_STRING(port->usb_manufacturer); |
a93fb468 AJ |
212 | } |
213 | ||
970f279a | 214 | SP_API char *sp_get_port_usb_product(const struct sp_port *port) |
a93fb468 AJ |
215 | { |
216 | TRACE("%p", port); | |
217 | ||
218 | if (!port || port->transport != SP_TRANSPORT_USB || !port->usb_product) | |
219 | return NULL; | |
220 | ||
9caa2e86 | 221 | RETURN_STRING(port->usb_product); |
a93fb468 AJ |
222 | } |
223 | ||
970f279a | 224 | SP_API char *sp_get_port_usb_serial(const struct sp_port *port) |
a93fb468 AJ |
225 | { |
226 | TRACE("%p", port); | |
227 | ||
228 | if (!port || port->transport != SP_TRANSPORT_USB || !port->usb_serial) | |
229 | return NULL; | |
230 | ||
9caa2e86 | 231 | RETURN_STRING(port->usb_serial); |
a93fb468 AJ |
232 | } |
233 | ||
970f279a | 234 | SP_API char *sp_get_port_bluetooth_address(const struct sp_port *port) |
a93fb468 AJ |
235 | { |
236 | TRACE("%p", port); | |
237 | ||
238 | if (!port || port->transport != SP_TRANSPORT_BLUETOOTH | |
239 | || !port->bluetooth_address) | |
240 | return NULL; | |
241 | ||
9caa2e86 | 242 | RETURN_STRING(port->bluetooth_address); |
a93fb468 AJ |
243 | } |
244 | ||
970f279a AJ |
245 | SP_API enum sp_return sp_get_port_handle(const struct sp_port *port, |
246 | void *result_ptr) | |
3c126654 | 247 | { |
00d8c56d | 248 | TRACE("%p, %p", port, result_ptr); |
3c126654 ML |
249 | |
250 | if (!port) | |
251 | RETURN_ERROR(SP_ERR_ARG, "Null port"); | |
5f64c285 UH |
252 | if (!result_ptr) |
253 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); | |
3c126654 ML |
254 | |
255 | #ifdef _WIN32 | |
256 | HANDLE *handle_ptr = result_ptr; | |
257 | *handle_ptr = port->hdl; | |
258 | #else | |
259 | int *fd_ptr = result_ptr; | |
260 | *fd_ptr = port->fd; | |
261 | #endif | |
262 | ||
263 | RETURN_OK(); | |
264 | } | |
265 | ||
970f279a AJ |
266 | SP_API enum sp_return sp_copy_port(const struct sp_port *port, |
267 | struct sp_port **copy_ptr) | |
32b5ac05 | 268 | { |
c33efc48 ML |
269 | TRACE("%p, %p", port, copy_ptr); |
270 | ||
32b5ac05 | 271 | if (!copy_ptr) |
c33efc48 | 272 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); |
32b5ac05 ML |
273 | |
274 | *copy_ptr = NULL; | |
275 | ||
c33efc48 ML |
276 | if (!port) |
277 | RETURN_ERROR(SP_ERR_ARG, "Null port"); | |
278 | ||
279 | if (!port->name) | |
280 | RETURN_ERROR(SP_ERR_ARG, "Null port name"); | |
32b5ac05 | 281 | |
ea667be7 ML |
282 | DEBUG("Copying port structure"); |
283 | ||
9caa2e86 | 284 | RETURN_INT(sp_get_port_by_name(port->name, copy_ptr)); |
32b5ac05 ML |
285 | } |
286 | ||
970f279a | 287 | SP_API void sp_free_port(struct sp_port *port) |
e3b2f7a4 | 288 | { |
c33efc48 ML |
289 | TRACE("%p", port); |
290 | ||
00d8c56d | 291 | if (!port) { |
c33efc48 ML |
292 | DEBUG("Null port"); |
293 | RETURN(); | |
294 | } | |
e3b2f7a4 | 295 | |
ea667be7 ML |
296 | DEBUG("Freeing port structure"); |
297 | ||
e3b2f7a4 ML |
298 | if (port->name) |
299 | free(port->name); | |
a93fb468 AJ |
300 | if (port->description) |
301 | free(port->description); | |
302 | if (port->usb_manufacturer) | |
303 | free(port->usb_manufacturer); | |
304 | if (port->usb_product) | |
305 | free(port->usb_product); | |
306 | if (port->usb_serial) | |
307 | free(port->usb_serial); | |
308 | if (port->bluetooth_address) | |
309 | free(port->bluetooth_address); | |
310 | #ifdef _WIN32 | |
311 | if (port->usb_path) | |
312 | free(port->usb_path); | |
313 | #endif | |
e3b2f7a4 ML |
314 | |
315 | free(port); | |
c33efc48 ML |
316 | |
317 | RETURN(); | |
e3b2f7a4 ML |
318 | } |
319 | ||
970f279a AJ |
320 | SP_PRIV struct sp_port **list_append(struct sp_port **list, |
321 | const char *portname) | |
3b63f34d ML |
322 | { |
323 | void *tmp; | |
324 | unsigned int count; | |
f92f1f0c | 325 | |
81243567 UH |
326 | for (count = 0; list[count]; count++) |
327 | ; | |
d54e9004 | 328 | if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2)))) |
3b63f34d ML |
329 | goto fail; |
330 | list = tmp; | |
77f262c4 | 331 | if (sp_get_port_by_name(portname, &list[count]) != SP_OK) |
3b63f34d | 332 | goto fail; |
db2794ce | 333 | list[count + 1] = NULL; |
3b63f34d | 334 | return list; |
f92f1f0c | 335 | |
3b63f34d ML |
336 | fail: |
337 | sp_free_port_list(list); | |
338 | return NULL; | |
339 | } | |
340 | ||
970f279a | 341 | SP_API enum sp_return sp_list_ports(struct sp_port ***list_ptr) |
3b63f34d | 342 | { |
23e7fa37 | 343 | #ifndef NO_ENUMERATION |
d54e9004 | 344 | struct sp_port **list; |
48a4076f | 345 | int ret; |
23e7fa37 | 346 | #endif |
24c1a4bb | 347 | |
c33efc48 ML |
348 | TRACE("%p", list_ptr); |
349 | ||
dec10e31 ML |
350 | if (!list_ptr) |
351 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); | |
352 | ||
63926415 UH |
353 | *list_ptr = NULL; |
354 | ||
8d12e620 ML |
355 | #ifdef NO_ENUMERATION |
356 | RETURN_ERROR(SP_ERR_SUPP, "Enumeration not supported on this platform"); | |
357 | #else | |
ea667be7 ML |
358 | DEBUG("Enumerating ports"); |
359 | ||
8d12e620 | 360 | if (!(list = malloc(sizeof(struct sp_port *)))) |
c33efc48 | 361 | RETURN_ERROR(SP_ERR_MEM, "Port list malloc failed"); |
24c1a4bb ML |
362 | |
363 | list[0] = NULL; | |
3b63f34d | 364 | |
48a4076f | 365 | ret = list_ports(&list); |
77f262c4 | 366 | |
8d12e620 | 367 | if (ret == SP_OK) { |
77f262c4 | 368 | *list_ptr = list; |
8d12e620 ML |
369 | } else { |
370 | sp_free_port_list(list); | |
77f262c4 ML |
371 | *list_ptr = NULL; |
372 | } | |
8d12e620 ML |
373 | |
374 | RETURN_CODEVAL(ret); | |
375 | #endif | |
3b63f34d ML |
376 | } |
377 | ||
970f279a | 378 | SP_API void sp_free_port_list(struct sp_port **list) |
3b63f34d ML |
379 | { |
380 | unsigned int i; | |
f92f1f0c | 381 | |
c33efc48 ML |
382 | TRACE("%p", list); |
383 | ||
dec10e31 ML |
384 | if (!list) { |
385 | DEBUG("Null list"); | |
386 | RETURN(); | |
387 | } | |
388 | ||
ea667be7 ML |
389 | DEBUG("Freeing port list"); |
390 | ||
3b63f34d | 391 | for (i = 0; list[i]; i++) |
e3b2f7a4 | 392 | sp_free_port(list[i]); |
3b63f34d | 393 | free(list); |
c33efc48 ML |
394 | |
395 | RETURN(); | |
3b63f34d ML |
396 | } |
397 | ||
c33efc48 | 398 | #define CHECK_PORT() do { \ |
dc422c04 | 399 | if (!port) \ |
c33efc48 | 400 | RETURN_ERROR(SP_ERR_ARG, "Null port"); \ |
dc422c04 | 401 | if (!port->name) \ |
dec10e31 ML |
402 | RETURN_ERROR(SP_ERR_ARG, "Null port name"); \ |
403 | } while (0) | |
404 | #ifdef _WIN32 | |
405 | #define CHECK_PORT_HANDLE() do { \ | |
c33efc48 | 406 | if (port->hdl == INVALID_HANDLE_VALUE) \ |
8062087a | 407 | RETURN_ERROR(SP_ERR_ARG, "Port not open"); \ |
dec10e31 | 408 | } while (0) |
74510d4b | 409 | #else |
dec10e31 | 410 | #define CHECK_PORT_HANDLE() do { \ |
c33efc48 | 411 | if (port->fd < 0) \ |
8062087a | 412 | RETURN_ERROR(SP_ERR_ARG, "Port not open"); \ |
dec10e31 | 413 | } while (0) |
74510d4b | 414 | #endif |
dec10e31 ML |
415 | #define CHECK_OPEN_PORT() do { \ |
416 | CHECK_PORT(); \ | |
417 | CHECK_PORT_HANDLE(); \ | |
418 | } while (0) | |
74510d4b | 419 | |
47fcf8ec ML |
420 | #ifdef WIN32 |
421 | /** To be called after port receive buffer is emptied. */ | |
422 | static enum sp_return restart_wait(struct sp_port *port) | |
423 | { | |
424 | DWORD wait_result; | |
425 | ||
426 | if (port->wait_running) { | |
427 | /* Check status of running wait operation. */ | |
428 | if (GetOverlappedResult(port->hdl, &port->wait_ovl, | |
429 | &wait_result, FALSE)) { | |
430 | DEBUG("Previous wait completed"); | |
431 | port->wait_running = FALSE; | |
432 | } else if (GetLastError() == ERROR_IO_INCOMPLETE) { | |
433 | DEBUG("Previous wait still running"); | |
434 | RETURN_OK(); | |
435 | } else { | |
436 | RETURN_FAIL("GetOverlappedResult() failed"); | |
437 | } | |
438 | } | |
439 | ||
440 | if (!port->wait_running) { | |
441 | /* Start new wait operation. */ | |
442 | if (WaitCommEvent(port->hdl, &port->events, | |
443 | &port->wait_ovl)) { | |
444 | DEBUG("New wait returned, events already pending"); | |
445 | } else if (GetLastError() == ERROR_IO_PENDING) { | |
446 | DEBUG("New wait running in background"); | |
447 | port->wait_running = TRUE; | |
448 | } else { | |
449 | RETURN_FAIL("WaitCommEvent() failed"); | |
450 | } | |
451 | } | |
452 | ||
453 | RETURN_OK(); | |
454 | } | |
455 | #endif | |
456 | ||
970f279a | 457 | SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) |
74510d4b | 458 | { |
bccc7c9f ML |
459 | struct port_data data; |
460 | struct sp_port_config config; | |
461 | enum sp_return ret; | |
462 | ||
00d8c56d | 463 | TRACE("%p, 0x%x", port, flags); |
c33efc48 | 464 | |
dec10e31 ML |
465 | CHECK_PORT(); |
466 | ||
276ef1b9 | 467 | if (flags > SP_MODE_READ_WRITE) |
dec10e31 | 468 | RETURN_ERROR(SP_ERR_ARG, "Invalid flags"); |
74510d4b | 469 | |
7890cef6 | 470 | DEBUG_FMT("Opening port %s", port->name); |
ea667be7 | 471 | |
74510d4b | 472 | #ifdef _WIN32 |
537942c9 | 473 | DWORD desired_access = 0, flags_and_attributes = 0, errors; |
99945a1f | 474 | char *escaped_port_name; |
537942c9 | 475 | COMSTAT status; |
99945a1f ML |
476 | |
477 | /* Prefix port name with '\\.\' to work with ports above COM9. */ | |
e48f0ece | 478 | if (!(escaped_port_name = malloc(strlen(port->name) + 5))) |
c33efc48 | 479 | RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed"); |
99945a1f ML |
480 | sprintf(escaped_port_name, "\\\\.\\%s", port->name); |
481 | ||
74510d4b | 482 | /* Map 'flags' to the OS-specific settings. */ |
e3dcf906 | 483 | flags_and_attributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED; |
a036341b ML |
484 | if (flags & SP_MODE_READ) |
485 | desired_access |= GENERIC_READ; | |
486 | if (flags & SP_MODE_WRITE) | |
74510d4b | 487 | desired_access |= GENERIC_WRITE; |
74510d4b | 488 | |
99945a1f | 489 | port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0, |
74510d4b | 490 | OPEN_EXISTING, flags_and_attributes, 0); |
99945a1f ML |
491 | |
492 | free(escaped_port_name); | |
493 | ||
74510d4b | 494 | if (port->hdl == INVALID_HANDLE_VALUE) |
dc422c04 | 495 | RETURN_FAIL("Port CreateFile() failed"); |
e3dcf906 ML |
496 | |
497 | /* All timeouts initially disabled. */ | |
498 | port->timeouts.ReadIntervalTimeout = 0; | |
499 | port->timeouts.ReadTotalTimeoutMultiplier = 0; | |
500 | port->timeouts.ReadTotalTimeoutConstant = 0; | |
501 | port->timeouts.WriteTotalTimeoutMultiplier = 0; | |
502 | port->timeouts.WriteTotalTimeoutConstant = 0; | |
a3cb91f5 | 503 | |
e3dcf906 | 504 | if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) { |
a3cb91f5 ML |
505 | sp_close(port); |
506 | RETURN_FAIL("SetCommTimeouts() failed"); | |
507 | } | |
e3dcf906 ML |
508 | |
509 | /* Prepare OVERLAPPED structures. */ | |
6f1186aa ML |
510 | #define INIT_OVERLAPPED(ovl) do { \ |
511 | memset(&port->ovl, 0, sizeof(port->ovl)); \ | |
512 | port->ovl.hEvent = INVALID_HANDLE_VALUE; \ | |
513 | if ((port->ovl.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL)) \ | |
514 | == INVALID_HANDLE_VALUE) { \ | |
515 | sp_close(port); \ | |
516 | RETURN_FAIL(#ovl "CreateEvent() failed"); \ | |
517 | } \ | |
518 | } while (0) | |
519 | ||
520 | INIT_OVERLAPPED(read_ovl); | |
521 | INIT_OVERLAPPED(write_ovl); | |
522 | INIT_OVERLAPPED(wait_ovl); | |
523 | ||
524 | /* Set event mask for RX and error events. */ | |
525 | if (SetCommMask(port->hdl, EV_RXCHAR | EV_ERR) == 0) { | |
e3dcf906 | 526 | sp_close(port); |
6f1186aa | 527 | RETURN_FAIL("SetCommMask() failed"); |
e3dcf906 | 528 | } |
6f1186aa | 529 | |
e3dcf906 | 530 | port->writing = FALSE; |
47fcf8ec | 531 | port->wait_running = FALSE; |
e3dcf906 | 532 | |
47fcf8ec ML |
533 | ret = restart_wait(port); |
534 | ||
535 | if (ret < 0) { | |
536 | sp_close(port); | |
537 | RETURN_CODEVAL(ret); | |
538 | } | |
74510d4b | 539 | #else |
e3dcf906 | 540 | int flags_local = O_NONBLOCK | O_NOCTTY; |
f92f1f0c | 541 | |
74510d4b | 542 | /* Map 'flags' to the OS-specific settings. */ |
276ef1b9 | 543 | if ((flags & SP_MODE_READ_WRITE) == SP_MODE_READ_WRITE) |
74510d4b | 544 | flags_local |= O_RDWR; |
a036341b | 545 | else if (flags & SP_MODE_READ) |
74510d4b | 546 | flags_local |= O_RDONLY; |
a036341b ML |
547 | else if (flags & SP_MODE_WRITE) |
548 | flags_local |= O_WRONLY; | |
74510d4b ML |
549 | |
550 | if ((port->fd = open(port->name, flags_local)) < 0) | |
c33efc48 | 551 | RETURN_FAIL("open() failed"); |
bccc7c9f | 552 | #endif |
9cb98459 | 553 | |
e33ab9aa ML |
554 | ret = get_config(port, &data, &config); |
555 | ||
eac329d2 | 556 | if (ret < 0) { |
e33ab9aa | 557 | sp_close(port); |
c33efc48 | 558 | RETURN_CODEVAL(ret); |
e33ab9aa | 559 | } |
9cb98459 | 560 | |
bccc7c9f ML |
561 | /* Set sane port settings. */ |
562 | #ifdef _WIN32 | |
563 | data.dcb.fBinary = TRUE; | |
564 | data.dcb.fDsrSensitivity = FALSE; | |
565 | data.dcb.fErrorChar = FALSE; | |
566 | data.dcb.fNull = FALSE; | |
34442106 | 567 | data.dcb.fAbortOnError = FALSE; |
bccc7c9f | 568 | #else |
b251be4b | 569 | /* Turn off all fancy termios tricks, give us a raw channel. */ |
c3e05092 UH |
570 | data.term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IMAXBEL); |
571 | #ifdef IUCLC | |
572 | data.term.c_iflag &= ~IUCLC; | |
573 | #endif | |
574 | data.term.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET); | |
575 | #ifdef OLCUC | |
576 | data.term.c_oflag &= ~OLCUC; | |
577 | #endif | |
578 | #ifdef NLDLY | |
579 | data.term.c_oflag &= ~NLDLY; | |
580 | #endif | |
581 | #ifdef CRDLY | |
582 | data.term.c_oflag &= ~CRDLY; | |
583 | #endif | |
584 | #ifdef TABDLY | |
585 | data.term.c_oflag &= ~TABDLY; | |
586 | #endif | |
587 | #ifdef BSDLY | |
588 | data.term.c_oflag &= ~BSDLY; | |
589 | #endif | |
590 | #ifdef VTDLY | |
591 | data.term.c_oflag &= ~VTDLY; | |
592 | #endif | |
593 | #ifdef FFDLY | |
594 | data.term.c_oflag &= ~FFDLY; | |
595 | #endif | |
b251be4b | 596 | #ifdef OFILL |
9cb98459 ML |
597 | data.term.c_oflag &= ~OFILL; |
598 | #endif | |
c3e05092 | 599 | data.term.c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN); |
e3dcf906 | 600 | data.term.c_cc[VMIN] = 0; |
b251be4b | 601 | data.term.c_cc[VTIME] = 0; |
9cb98459 | 602 | |
b251be4b ML |
603 | /* Ignore modem status lines; enable receiver; leave control lines alone on close. */ |
604 | data.term.c_cflag |= (CLOCAL | CREAD | HUPCL); | |
bccc7c9f | 605 | #endif |
9cb98459 | 606 | |
537942c9 | 607 | #ifdef _WIN32 |
1a2a1544 UH |
608 | if (ClearCommError(port->hdl, &errors, &status) == 0) |
609 | RETURN_FAIL("ClearCommError() failed"); | |
537942c9 ML |
610 | #endif |
611 | ||
e33ab9aa ML |
612 | ret = set_config(port, &data, &config); |
613 | ||
eac329d2 | 614 | if (ret < 0) { |
e33ab9aa | 615 | sp_close(port); |
c33efc48 | 616 | RETURN_CODEVAL(ret); |
e33ab9aa | 617 | } |
74510d4b | 618 | |
c33efc48 | 619 | RETURN_OK(); |
74510d4b ML |
620 | } |
621 | ||
970f279a | 622 | SP_API enum sp_return sp_close(struct sp_port *port) |
74510d4b | 623 | { |
c33efc48 ML |
624 | TRACE("%p", port); |
625 | ||
dec10e31 | 626 | CHECK_OPEN_PORT(); |
74510d4b | 627 | |
7890cef6 | 628 | DEBUG_FMT("Closing port %s", port->name); |
ea667be7 | 629 | |
74510d4b ML |
630 | #ifdef _WIN32 |
631 | /* Returns non-zero upon success, 0 upon failure. */ | |
632 | if (CloseHandle(port->hdl) == 0) | |
dc422c04 | 633 | RETURN_FAIL("Port CloseHandle() failed"); |
8f471c66 | 634 | port->hdl = INVALID_HANDLE_VALUE; |
6f1186aa ML |
635 | |
636 | /* Close event handles for overlapped structures. */ | |
637 | #define CLOSE_OVERLAPPED(ovl) do { \ | |
638 | if (port->ovl.hEvent != INVALID_HANDLE_VALUE && \ | |
639 | CloseHandle(port->ovl.hEvent) == 0) \ | |
640 | RETURN_FAIL(# ovl "event CloseHandle() failed"); \ | |
641 | } while (0) | |
642 | CLOSE_OVERLAPPED(read_ovl); | |
643 | CLOSE_OVERLAPPED(write_ovl); | |
644 | CLOSE_OVERLAPPED(wait_ovl); | |
645 | ||
74510d4b ML |
646 | #else |
647 | /* Returns 0 upon success, -1 upon failure. */ | |
648 | if (close(port->fd) == -1) | |
c33efc48 | 649 | RETURN_FAIL("close() failed"); |
8f471c66 | 650 | port->fd = -1; |
74510d4b ML |
651 | #endif |
652 | ||
c33efc48 | 653 | RETURN_OK(); |
74510d4b ML |
654 | } |
655 | ||
970f279a | 656 | SP_API enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers) |
74510d4b | 657 | { |
00d8c56d | 658 | TRACE("%p, 0x%x", port, buffers); |
c33efc48 | 659 | |
dec10e31 ML |
660 | CHECK_OPEN_PORT(); |
661 | ||
662 | if (buffers > SP_BUF_BOTH) | |
663 | RETURN_ERROR(SP_ERR_ARG, "Invalid buffer selection"); | |
74510d4b | 664 | |
0ba3e49b | 665 | const char *buffer_names[] = {"no", "input", "output", "both"}; |
ea667be7 | 666 | |
7890cef6 ML |
667 | DEBUG_FMT("Flushing %s buffers on port %s", |
668 | buffer_names[buffers], port->name); | |
ea667be7 | 669 | |
74510d4b | 670 | #ifdef _WIN32 |
fd8fd11a ML |
671 | DWORD flags = 0; |
672 | if (buffers & SP_BUF_INPUT) | |
673 | flags |= PURGE_RXCLEAR; | |
674 | if (buffers & SP_BUF_OUTPUT) | |
675 | flags |= PURGE_TXCLEAR; | |
676 | ||
74510d4b | 677 | /* Returns non-zero upon success, 0 upon failure. */ |
fd8fd11a | 678 | if (PurgeComm(port->hdl, flags) == 0) |
c33efc48 | 679 | RETURN_FAIL("PurgeComm() failed"); |
47fcf8ec ML |
680 | |
681 | if (buffers & SP_BUF_INPUT) | |
682 | TRY(restart_wait(port)); | |
74510d4b | 683 | #else |
fd8fd11a | 684 | int flags = 0; |
ad749855 | 685 | if (buffers == SP_BUF_BOTH) |
fd8fd11a | 686 | flags = TCIOFLUSH; |
ad749855 | 687 | else if (buffers == SP_BUF_INPUT) |
fd8fd11a | 688 | flags = TCIFLUSH; |
ad749855 | 689 | else if (buffers == SP_BUF_OUTPUT) |
fd8fd11a ML |
690 | flags = TCOFLUSH; |
691 | ||
74510d4b | 692 | /* Returns 0 upon success, -1 upon failure. */ |
fd8fd11a | 693 | if (tcflush(port->fd, flags) < 0) |
c33efc48 | 694 | RETURN_FAIL("tcflush() failed"); |
74510d4b | 695 | #endif |
c33efc48 | 696 | RETURN_OK(); |
74510d4b ML |
697 | } |
698 | ||
970f279a | 699 | SP_API enum sp_return sp_drain(struct sp_port *port) |
69a3739c | 700 | { |
c33efc48 ML |
701 | TRACE("%p", port); |
702 | ||
dec10e31 | 703 | CHECK_OPEN_PORT(); |
69a3739c | 704 | |
7890cef6 | 705 | DEBUG_FMT("Draining port %s", port->name); |
ea667be7 | 706 | |
69a3739c ML |
707 | #ifdef _WIN32 |
708 | /* Returns non-zero upon success, 0 upon failure. */ | |
709 | if (FlushFileBuffers(port->hdl) == 0) | |
c33efc48 | 710 | RETURN_FAIL("FlushFileBuffers() failed"); |
2c827b21 | 711 | RETURN_OK(); |
69a3739c | 712 | #else |
2c827b21 ML |
713 | int result; |
714 | while (1) { | |
33fd8804 ML |
715 | #ifdef __ANDROID__ |
716 | int arg = 1; | |
717 | result = ioctl(port->fd, TCSBRK, &arg); | |
718 | #else | |
2c827b21 | 719 | result = tcdrain(port->fd); |
33fd8804 | 720 | #endif |
2c827b21 ML |
721 | if (result < 0) { |
722 | if (errno == EINTR) { | |
723 | DEBUG("tcdrain() was interrupted"); | |
724 | continue; | |
725 | } else { | |
726 | RETURN_FAIL("tcdrain() failed"); | |
727 | } | |
728 | } else { | |
729 | RETURN_OK(); | |
730 | } | |
731 | } | |
69a3739c | 732 | #endif |
69a3739c ML |
733 | } |
734 | ||
62ed9f80 ML |
735 | #ifdef _WIN32 |
736 | static enum sp_return await_write_completion(struct sp_port *port) | |
737 | { | |
738 | TRACE("%p", port); | |
739 | DWORD bytes_written; | |
740 | BOOL result; | |
741 | ||
742 | /* Wait for previous non-blocking write to complete, if any. */ | |
743 | if (port->writing) { | |
744 | DEBUG("Waiting for previous write to complete"); | |
745 | result = GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE); | |
746 | port->writing = 0; | |
747 | if (!result) | |
748 | RETURN_FAIL("Previous write failed to complete"); | |
749 | DEBUG("Previous write completed"); | |
750 | } | |
751 | ||
752 | RETURN_OK(); | |
753 | } | |
754 | #endif | |
755 | ||
970f279a | 756 | SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, |
1b91c6ea | 757 | size_t count, unsigned int timeout_ms) |
e3dcf906 | 758 | { |
1b91c6ea | 759 | TRACE("%p, %p, %d, %d", port, buf, count, timeout_ms); |
e3dcf906 ML |
760 | |
761 | CHECK_OPEN_PORT(); | |
762 | ||
763 | if (!buf) | |
764 | RETURN_ERROR(SP_ERR_ARG, "Null buffer"); | |
765 | ||
1b91c6ea | 766 | if (timeout_ms) |
7890cef6 | 767 | DEBUG_FMT("Writing %d bytes to port %s, timeout %d ms", |
1b91c6ea | 768 | count, port->name, timeout_ms); |
e3dcf906 | 769 | else |
7890cef6 ML |
770 | DEBUG_FMT("Writing %d bytes to port %s, no timeout", |
771 | count, port->name); | |
e3dcf906 ML |
772 | |
773 | if (count == 0) | |
9caa2e86 | 774 | RETURN_INT(0); |
e3dcf906 ML |
775 | |
776 | #ifdef _WIN32 | |
777 | DWORD bytes_written = 0; | |
e3dcf906 | 778 | |
62ed9f80 | 779 | TRY(await_write_completion(port)); |
e3dcf906 ML |
780 | |
781 | /* Set timeout. */ | |
27a58c05 ML |
782 | if (port->timeouts.WriteTotalTimeoutConstant != timeout_ms) { |
783 | port->timeouts.WriteTotalTimeoutConstant = timeout_ms; | |
784 | if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) | |
785 | RETURN_FAIL("SetCommTimeouts() failed"); | |
786 | } | |
e3dcf906 ML |
787 | |
788 | /* Start write. */ | |
bbe566fe | 789 | if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl)) { |
e3dcf906 | 790 | DEBUG("Write completed immediately"); |
9caa2e86 | 791 | RETURN_INT(count); |
bbe566fe ML |
792 | } else if (GetLastError() == ERROR_IO_PENDING) { |
793 | DEBUG("Waiting for write to complete"); | |
aee7d691 UH |
794 | if (GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE) == 0) { |
795 | if (GetLastError() == ERROR_SEM_TIMEOUT) { | |
796 | DEBUG("Write timed out"); | |
797 | RETURN_INT(0); | |
798 | } else { | |
799 | RETURN_FAIL("GetOverlappedResult() failed"); | |
800 | } | |
801 | } | |
bbe566fe ML |
802 | DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count); |
803 | RETURN_INT(bytes_written); | |
804 | } else { | |
805 | RETURN_FAIL("WriteFile() failed"); | |
e3dcf906 ML |
806 | } |
807 | #else | |
808 | size_t bytes_written = 0; | |
809 | unsigned char *ptr = (unsigned char *) buf; | |
810 | struct timeval start, delta, now, end = {0, 0}; | |
76222ee5 | 811 | int started = 0; |
e3dcf906 ML |
812 | fd_set fds; |
813 | int result; | |
814 | ||
1b91c6ea | 815 | if (timeout_ms) { |
e3dcf906 ML |
816 | /* Get time at start of operation. */ |
817 | gettimeofday(&start, NULL); | |
818 | /* Define duration of timeout. */ | |
1b91c6ea UH |
819 | delta.tv_sec = timeout_ms / 1000; |
820 | delta.tv_usec = (timeout_ms % 1000) * 1000; | |
e3dcf906 ML |
821 | /* Calculate time at which we should give up. */ |
822 | timeradd(&start, &delta, &end); | |
823 | } | |
824 | ||
772c5861 ML |
825 | FD_ZERO(&fds); |
826 | FD_SET(port->fd, &fds); | |
827 | ||
e3dcf906 | 828 | /* Loop until we have written the requested number of bytes. */ |
dc422c04 | 829 | while (bytes_written < count) { |
76222ee5 ML |
830 | /* |
831 | * Check timeout only if we have run select() at least once, | |
832 | * to avoid any issues if a short timeout is reached before | |
833 | * select() is even run. | |
834 | */ | |
835 | if (timeout_ms && started) { | |
e3dcf906 | 836 | gettimeofday(&now, NULL); |
c3cee38c ML |
837 | if (timercmp(&now, &end, >)) |
838 | /* Timeout has expired. */ | |
839 | break; | |
e3dcf906 ML |
840 | timersub(&end, &now, &delta); |
841 | } | |
1b91c6ea | 842 | result = select(port->fd + 1, NULL, &fds, NULL, timeout_ms ? &delta : NULL); |
76222ee5 | 843 | started = 1; |
63a17c64 ML |
844 | if (result < 0) { |
845 | if (errno == EINTR) { | |
846 | DEBUG("select() call was interrupted, repeating"); | |
847 | continue; | |
848 | } else { | |
849 | RETURN_FAIL("select() failed"); | |
850 | } | |
851 | } else if (result == 0) { | |
c3cee38c ML |
852 | /* Timeout has expired. */ |
853 | break; | |
e3dcf906 ML |
854 | } |
855 | ||
856 | /* Do write. */ | |
857 | result = write(port->fd, ptr, count - bytes_written); | |
858 | ||
859 | if (result < 0) { | |
860 | if (errno == EAGAIN) | |
861 | /* This shouldn't happen because we did a select() first, but handle anyway. */ | |
862 | continue; | |
863 | else | |
864 | /* This is an actual failure. */ | |
865 | RETURN_FAIL("write() failed"); | |
866 | } | |
867 | ||
868 | bytes_written += result; | |
869 | ptr += result; | |
870 | } | |
871 | ||
c3cee38c ML |
872 | if (bytes_written < count) |
873 | DEBUG("Write timed out"); | |
874 | ||
9caa2e86 | 875 | RETURN_INT(bytes_written); |
e3dcf906 ML |
876 | #endif |
877 | } | |
878 | ||
970f279a AJ |
879 | SP_API enum sp_return sp_nonblocking_write(struct sp_port *port, |
880 | const void *buf, size_t count) | |
74510d4b | 881 | { |
c33efc48 ML |
882 | TRACE("%p, %p, %d", port, buf, count); |
883 | ||
dec10e31 | 884 | CHECK_OPEN_PORT(); |
74510d4b ML |
885 | |
886 | if (!buf) | |
c33efc48 | 887 | RETURN_ERROR(SP_ERR_ARG, "Null buffer"); |
74510d4b | 888 | |
7890cef6 | 889 | DEBUG_FMT("Writing up to %d bytes to port %s", count, port->name); |
ea667be7 | 890 | |
a3cb91f5 | 891 | if (count == 0) |
9caa2e86 | 892 | RETURN_INT(0); |
a3cb91f5 | 893 | |
74510d4b ML |
894 | #ifdef _WIN32 |
895 | DWORD written = 0; | |
0765af56 | 896 | BYTE *ptr = (BYTE *) buf; |
f92f1f0c | 897 | |
e3dcf906 ML |
898 | /* Check whether previous write is complete. */ |
899 | if (port->writing) { | |
900 | if (HasOverlappedIoCompleted(&port->write_ovl)) { | |
901 | DEBUG("Previous write completed"); | |
902 | port->writing = 0; | |
903 | } else { | |
904 | DEBUG("Previous write not complete"); | |
905 | /* Can't take a new write until the previous one finishes. */ | |
9caa2e86 | 906 | RETURN_INT(0); |
a3cb91f5 | 907 | } |
e3dcf906 | 908 | } |
a3cb91f5 | 909 | |
e3dcf906 | 910 | /* Set timeout. */ |
27a58c05 ML |
911 | if (port->timeouts.WriteTotalTimeoutConstant != 0) { |
912 | port->timeouts.WriteTotalTimeoutConstant = 0; | |
913 | if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) | |
914 | RETURN_FAIL("SetCommTimeouts() failed"); | |
915 | } | |
e3dcf906 | 916 | |
dc422c04 UH |
917 | /* |
918 | * Keep writing data until the OS has to actually start an async IO | |
919 | * for it. At that point we know the buffer is full. | |
920 | */ | |
921 | while (written < count) { | |
e3dcf906 ML |
922 | /* Copy first byte of user buffer. */ |
923 | port->pending_byte = *ptr++; | |
924 | ||
925 | /* Start asynchronous write. */ | |
926 | if (WriteFile(port->hdl, &port->pending_byte, 1, NULL, &port->write_ovl) == 0) { | |
927 | if (GetLastError() == ERROR_IO_PENDING) { | |
64d99621 ML |
928 | if (HasOverlappedIoCompleted(&port->write_ovl)) { |
929 | DEBUG("Asynchronous write completed immediately"); | |
930 | port->writing = 0; | |
931 | written++; | |
932 | continue; | |
933 | } else { | |
934 | DEBUG("Asynchronous write running"); | |
935 | port->writing = 1; | |
9caa2e86 | 936 | RETURN_INT(++written); |
64d99621 | 937 | } |
a3cb91f5 | 938 | } else { |
e3dcf906 ML |
939 | /* Actual failure of some kind. */ |
940 | RETURN_FAIL("WriteFile() failed"); | |
a3cb91f5 | 941 | } |
e3dcf906 | 942 | } else { |
9af8cff3 | 943 | DEBUG("Single byte written immediately"); |
e3dcf906 | 944 | written++; |
a3cb91f5 ML |
945 | } |
946 | } | |
947 | ||
9af8cff3 | 948 | DEBUG("All bytes written immediately"); |
e3dcf906 | 949 | |
9caa2e86 | 950 | RETURN_INT(written); |
74510d4b ML |
951 | #else |
952 | /* Returns the number of bytes written, or -1 upon failure. */ | |
953 | ssize_t written = write(port->fd, buf, count); | |
f92f1f0c | 954 | |
74510d4b | 955 | if (written < 0) |
c33efc48 | 956 | RETURN_FAIL("write() failed"); |
74510d4b | 957 | else |
9caa2e86 | 958 | RETURN_INT(written); |
74510d4b ML |
959 | #endif |
960 | } | |
961 | ||
94a314a8 ML |
962 | #ifdef _WIN32 |
963 | /* Restart wait operation if buffer was emptied. */ | |
964 | static enum sp_return restart_wait_if_needed(struct sp_port *port, unsigned int bytes_read) | |
965 | { | |
8ebfaf89 ML |
966 | DWORD errors; |
967 | COMSTAT comstat; | |
94a314a8 | 968 | |
21ae5ce1 ML |
969 | if (bytes_read == 0) |
970 | RETURN_OK(); | |
971 | ||
8ebfaf89 ML |
972 | if (ClearCommError(port->hdl, &errors, &comstat) == 0) |
973 | RETURN_FAIL("ClearCommError() failed"); | |
94a314a8 | 974 | |
8ebfaf89 | 975 | if (comstat.cbInQue == 0) |
94a314a8 ML |
976 | TRY(restart_wait(port)); |
977 | ||
978 | RETURN_OK(); | |
979 | } | |
980 | #endif | |
981 | ||
970f279a | 982 | SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, |
1b91c6ea | 983 | size_t count, unsigned int timeout_ms) |
e3dcf906 | 984 | { |
1b91c6ea | 985 | TRACE("%p, %p, %d, %d", port, buf, count, timeout_ms); |
e3dcf906 ML |
986 | |
987 | CHECK_OPEN_PORT(); | |
988 | ||
989 | if (!buf) | |
990 | RETURN_ERROR(SP_ERR_ARG, "Null buffer"); | |
991 | ||
1b91c6ea | 992 | if (timeout_ms) |
7890cef6 | 993 | DEBUG_FMT("Reading %d bytes from port %s, timeout %d ms", |
1b91c6ea | 994 | count, port->name, timeout_ms); |
e3dcf906 | 995 | else |
7890cef6 ML |
996 | DEBUG_FMT("Reading %d bytes from port %s, no timeout", |
997 | count, port->name); | |
e3dcf906 ML |
998 | |
999 | if (count == 0) | |
9caa2e86 | 1000 | RETURN_INT(0); |
e3dcf906 ML |
1001 | |
1002 | #ifdef _WIN32 | |
1003 | DWORD bytes_read = 0; | |
1004 | ||
1005 | /* Set timeout. */ | |
27a58c05 | 1006 | if (port->timeouts.ReadIntervalTimeout != 0 || |
0188a545 | 1007 | port->timeouts.ReadTotalTimeoutMultiplier != 0 || |
27a58c05 ML |
1008 | port->timeouts.ReadTotalTimeoutConstant != timeout_ms) { |
1009 | port->timeouts.ReadIntervalTimeout = 0; | |
0188a545 | 1010 | port->timeouts.ReadTotalTimeoutMultiplier = 0; |
27a58c05 ML |
1011 | port->timeouts.ReadTotalTimeoutConstant = timeout_ms; |
1012 | if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) | |
1013 | RETURN_FAIL("SetCommTimeouts() failed"); | |
1014 | } | |
e3dcf906 ML |
1015 | |
1016 | /* Start read. */ | |
bbe566fe | 1017 | if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl)) { |
e3dcf906 | 1018 | DEBUG("Read completed immediately"); |
6f1186aa | 1019 | bytes_read = count; |
bbe566fe ML |
1020 | } else if (GetLastError() == ERROR_IO_PENDING) { |
1021 | DEBUG("Waiting for read to complete"); | |
27911925 ML |
1022 | if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0) |
1023 | RETURN_FAIL("GetOverlappedResult() failed"); | |
bbe566fe ML |
1024 | DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count); |
1025 | } else { | |
1026 | RETURN_FAIL("ReadFile() failed"); | |
6f1186aa ML |
1027 | } |
1028 | ||
94a314a8 | 1029 | TRY(restart_wait_if_needed(port, bytes_read)); |
6f1186aa | 1030 | |
9caa2e86 | 1031 | RETURN_INT(bytes_read); |
6f1186aa | 1032 | |
e3dcf906 ML |
1033 | #else |
1034 | size_t bytes_read = 0; | |
81243567 | 1035 | unsigned char *ptr = (unsigned char *)buf; |
e3dcf906 | 1036 | struct timeval start, delta, now, end = {0, 0}; |
76222ee5 | 1037 | int started = 0; |
e3dcf906 ML |
1038 | fd_set fds; |
1039 | int result; | |
1040 | ||
1b91c6ea | 1041 | if (timeout_ms) { |
e3dcf906 ML |
1042 | /* Get time at start of operation. */ |
1043 | gettimeofday(&start, NULL); | |
1044 | /* Define duration of timeout. */ | |
1b91c6ea UH |
1045 | delta.tv_sec = timeout_ms / 1000; |
1046 | delta.tv_usec = (timeout_ms % 1000) * 1000; | |
e3dcf906 ML |
1047 | /* Calculate time at which we should give up. */ |
1048 | timeradd(&start, &delta, &end); | |
1049 | } | |
1050 | ||
772c5861 ML |
1051 | FD_ZERO(&fds); |
1052 | FD_SET(port->fd, &fds); | |
1053 | ||
e3dcf906 | 1054 | /* Loop until we have the requested number of bytes. */ |
dc422c04 | 1055 | while (bytes_read < count) { |
76222ee5 ML |
1056 | /* |
1057 | * Check timeout only if we have run select() at least once, | |
1058 | * to avoid any issues if a short timeout is reached before | |
1059 | * select() is even run. | |
1060 | */ | |
1061 | if (timeout_ms && started) { | |
e3dcf906 ML |
1062 | gettimeofday(&now, NULL); |
1063 | if (timercmp(&now, &end, >)) | |
1064 | /* Timeout has expired. */ | |
c3cee38c | 1065 | break; |
e3dcf906 ML |
1066 | timersub(&end, &now, &delta); |
1067 | } | |
1b91c6ea | 1068 | result = select(port->fd + 1, &fds, NULL, NULL, timeout_ms ? &delta : NULL); |
76222ee5 | 1069 | started = 1; |
63a17c64 ML |
1070 | if (result < 0) { |
1071 | if (errno == EINTR) { | |
1072 | DEBUG("select() call was interrupted, repeating"); | |
1073 | continue; | |
1074 | } else { | |
1075 | RETURN_FAIL("select() failed"); | |
1076 | } | |
1077 | } else if (result == 0) { | |
c3cee38c ML |
1078 | /* Timeout has expired. */ |
1079 | break; | |
e3dcf906 ML |
1080 | } |
1081 | ||
1082 | /* Do read. */ | |
1083 | result = read(port->fd, ptr, count - bytes_read); | |
1084 | ||
1085 | if (result < 0) { | |
1086 | if (errno == EAGAIN) | |
76222ee5 ML |
1087 | /* |
1088 | * This shouldn't happen because we did a | |
1089 | * select() first, but handle anyway. | |
1090 | */ | |
e3dcf906 ML |
1091 | continue; |
1092 | else | |
1093 | /* This is an actual failure. */ | |
1094 | RETURN_FAIL("read() failed"); | |
1095 | } | |
1096 | ||
1097 | bytes_read += result; | |
1098 | ptr += result; | |
1099 | } | |
1100 | ||
c3cee38c ML |
1101 | if (bytes_read < count) |
1102 | DEBUG("Read timed out"); | |
1103 | ||
9caa2e86 | 1104 | RETURN_INT(bytes_read); |
e3dcf906 ML |
1105 | #endif |
1106 | } | |
1107 | ||
e5c2630e ML |
1108 | SP_API enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, |
1109 | size_t count, unsigned int timeout_ms) | |
1110 | { | |
1111 | TRACE("%p, %p, %d, %d", port, buf, count, timeout_ms); | |
1112 | ||
1113 | CHECK_OPEN_PORT(); | |
1114 | ||
1115 | if (!buf) | |
1116 | RETURN_ERROR(SP_ERR_ARG, "Null buffer"); | |
1117 | ||
1118 | if (count == 0) | |
1119 | RETURN_ERROR(SP_ERR_ARG, "Zero count"); | |
1120 | ||
1121 | if (timeout_ms) | |
1122 | DEBUG_FMT("Reading next max %d bytes from port %s, timeout %d ms", | |
1123 | count, port->name, timeout_ms); | |
1124 | else | |
1125 | DEBUG_FMT("Reading next max %d bytes from port %s, no timeout", | |
1126 | count, port->name); | |
1127 | ||
1128 | #ifdef _WIN32 | |
1129 | DWORD bytes_read = 0; | |
1130 | ||
1131 | /* If timeout_ms == 0, set maximum timeout. */ | |
1132 | DWORD timeout_val = (timeout_ms == 0 ? MAXDWORD - 1 : timeout_ms); | |
1133 | ||
1134 | /* Set timeout. */ | |
1135 | if (port->timeouts.ReadIntervalTimeout != MAXDWORD || | |
1136 | port->timeouts.ReadTotalTimeoutMultiplier != MAXDWORD || | |
1137 | port->timeouts.ReadTotalTimeoutConstant != timeout_val) { | |
1138 | port->timeouts.ReadIntervalTimeout = MAXDWORD; | |
1139 | port->timeouts.ReadTotalTimeoutMultiplier = MAXDWORD; | |
1140 | port->timeouts.ReadTotalTimeoutConstant = timeout_val; | |
1141 | if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) | |
1142 | RETURN_FAIL("SetCommTimeouts() failed"); | |
1143 | } | |
1144 | ||
1145 | /* Loop until we have at least one byte, or timeout is reached. */ | |
1146 | while (bytes_read == 0) { | |
1147 | /* Start read. */ | |
888fb45d | 1148 | if (ReadFile(port->hdl, buf, count, &bytes_read, &port->read_ovl)) { |
e5c2630e | 1149 | DEBUG("Read completed immediately"); |
e5c2630e ML |
1150 | } else if (GetLastError() == ERROR_IO_PENDING) { |
1151 | DEBUG("Waiting for read to complete"); | |
1152 | if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0) | |
1153 | RETURN_FAIL("GetOverlappedResult() failed"); | |
1154 | if (bytes_read > 0) { | |
1155 | DEBUG("Read completed"); | |
1156 | } else if (timeout_ms > 0) { | |
1157 | DEBUG("Read timed out"); | |
1158 | break; | |
1159 | } else { | |
1160 | DEBUG("Restarting read"); | |
1161 | } | |
1162 | } else { | |
1163 | RETURN_FAIL("ReadFile() failed"); | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | TRY(restart_wait_if_needed(port, bytes_read)); | |
1168 | ||
1169 | RETURN_INT(bytes_read); | |
1170 | ||
1171 | #else | |
1172 | size_t bytes_read = 0; | |
1173 | struct timeval start, delta, now, end = {0, 0}; | |
1174 | int started = 0; | |
1175 | fd_set fds; | |
1176 | int result; | |
1177 | ||
1178 | if (timeout_ms) { | |
1179 | /* Get time at start of operation. */ | |
1180 | gettimeofday(&start, NULL); | |
1181 | /* Define duration of timeout. */ | |
1182 | delta.tv_sec = timeout_ms / 1000; | |
1183 | delta.tv_usec = (timeout_ms % 1000) * 1000; | |
1184 | /* Calculate time at which we should give up. */ | |
1185 | timeradd(&start, &delta, &end); | |
1186 | } | |
1187 | ||
1188 | FD_ZERO(&fds); | |
1189 | FD_SET(port->fd, &fds); | |
1190 | ||
1191 | /* Loop until we have at least one byte, or timeout is reached. */ | |
1192 | while (bytes_read == 0) { | |
1193 | /* | |
1194 | * Check timeout only if we have run select() at least once, | |
1195 | * to avoid any issues if a short timeout is reached before | |
1196 | * select() is even run. | |
1197 | */ | |
1198 | if (timeout_ms && started) { | |
1199 | gettimeofday(&now, NULL); | |
1200 | if (timercmp(&now, &end, >)) | |
1201 | /* Timeout has expired. */ | |
1202 | break; | |
1203 | timersub(&end, &now, &delta); | |
1204 | } | |
1205 | result = select(port->fd + 1, &fds, NULL, NULL, timeout_ms ? &delta : NULL); | |
1206 | started = 1; | |
1207 | if (result < 0) { | |
1208 | if (errno == EINTR) { | |
1209 | DEBUG("select() call was interrupted, repeating"); | |
1210 | continue; | |
1211 | } else { | |
1212 | RETURN_FAIL("select() failed"); | |
1213 | } | |
1214 | } else if (result == 0) { | |
1215 | /* Timeout has expired. */ | |
1216 | break; | |
1217 | } | |
1218 | ||
1219 | /* Do read. */ | |
1220 | result = read(port->fd, buf, count); | |
1221 | ||
1222 | if (result < 0) { | |
1223 | if (errno == EAGAIN) | |
1224 | /* This shouldn't happen because we did a select() first, but handle anyway. */ | |
1225 | continue; | |
1226 | else | |
1227 | /* This is an actual failure. */ | |
1228 | RETURN_FAIL("read() failed"); | |
1229 | } | |
1230 | ||
1231 | bytes_read = result; | |
1232 | } | |
1233 | ||
1234 | if (bytes_read == 0) | |
1235 | DEBUG("Read timed out"); | |
1236 | ||
1237 | RETURN_INT(bytes_read); | |
1238 | #endif | |
1239 | } | |
1240 | ||
970f279a AJ |
1241 | SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, |
1242 | size_t count) | |
74510d4b | 1243 | { |
c33efc48 ML |
1244 | TRACE("%p, %p, %d", port, buf, count); |
1245 | ||
dec10e31 | 1246 | CHECK_OPEN_PORT(); |
74510d4b ML |
1247 | |
1248 | if (!buf) | |
c33efc48 | 1249 | RETURN_ERROR(SP_ERR_ARG, "Null buffer"); |
74510d4b | 1250 | |
7890cef6 | 1251 | DEBUG_FMT("Reading up to %d bytes from port %s", count, port->name); |
ea667be7 | 1252 | |
74510d4b | 1253 | #ifdef _WIN32 |
e3dcf906 | 1254 | DWORD bytes_read; |
f92f1f0c | 1255 | |
e3dcf906 | 1256 | /* Set timeout. */ |
27a58c05 | 1257 | if (port->timeouts.ReadIntervalTimeout != MAXDWORD || |
0188a545 | 1258 | port->timeouts.ReadTotalTimeoutMultiplier != 0 || |
27a58c05 ML |
1259 | port->timeouts.ReadTotalTimeoutConstant != 0) { |
1260 | port->timeouts.ReadIntervalTimeout = MAXDWORD; | |
0188a545 | 1261 | port->timeouts.ReadTotalTimeoutMultiplier = 0; |
27a58c05 ML |
1262 | port->timeouts.ReadTotalTimeoutConstant = 0; |
1263 | if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) | |
1264 | RETURN_FAIL("SetCommTimeouts() failed"); | |
1265 | } | |
e3dcf906 ML |
1266 | |
1267 | /* Do read. */ | |
1268 | if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0) | |
ae2c5825 ML |
1269 | if (GetLastError() != ERROR_IO_PENDING) |
1270 | RETURN_FAIL("ReadFile() failed"); | |
e3dcf906 ML |
1271 | |
1272 | /* Get number of bytes read. */ | |
ae2c5825 | 1273 | if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, FALSE) == 0) |
1622ef60 | 1274 | RETURN_FAIL("GetOverlappedResult() failed"); |
e3dcf906 | 1275 | |
94a314a8 | 1276 | TRY(restart_wait_if_needed(port, bytes_read)); |
6f1186aa | 1277 | |
9caa2e86 | 1278 | RETURN_INT(bytes_read); |
74510d4b ML |
1279 | #else |
1280 | ssize_t bytes_read; | |
f92f1f0c | 1281 | |
74510d4b | 1282 | /* Returns the number of bytes read, or -1 upon failure. */ |
33d5ff47 | 1283 | if ((bytes_read = read(port->fd, buf, count)) < 0) { |
e3dcf906 ML |
1284 | if (errno == EAGAIN) |
1285 | /* No bytes available. */ | |
33d5ff47 ML |
1286 | bytes_read = 0; |
1287 | else | |
1288 | /* This is an actual failure. */ | |
1289 | RETURN_FAIL("read() failed"); | |
1290 | } | |
9caa2e86 | 1291 | RETURN_INT(bytes_read); |
74510d4b ML |
1292 | #endif |
1293 | } | |
1294 | ||
970f279a | 1295 | SP_API enum sp_return sp_input_waiting(struct sp_port *port) |
3353c22f ML |
1296 | { |
1297 | TRACE("%p", port); | |
1298 | ||
1299 | CHECK_OPEN_PORT(); | |
1300 | ||
7890cef6 | 1301 | DEBUG_FMT("Checking input bytes waiting on port %s", port->name); |
3353c22f ML |
1302 | |
1303 | #ifdef _WIN32 | |
1304 | DWORD errors; | |
1305 | COMSTAT comstat; | |
1306 | ||
1307 | if (ClearCommError(port->hdl, &errors, &comstat) == 0) | |
1a2a1544 | 1308 | RETURN_FAIL("ClearCommError() failed"); |
9caa2e86 | 1309 | RETURN_INT(comstat.cbInQue); |
3353c22f ML |
1310 | #else |
1311 | int bytes_waiting; | |
1312 | if (ioctl(port->fd, TIOCINQ, &bytes_waiting) < 0) | |
1313 | RETURN_FAIL("TIOCINQ ioctl failed"); | |
9caa2e86 | 1314 | RETURN_INT(bytes_waiting); |
3353c22f ML |
1315 | #endif |
1316 | } | |
1317 | ||
970f279a | 1318 | SP_API enum sp_return sp_output_waiting(struct sp_port *port) |
3353c22f ML |
1319 | { |
1320 | TRACE("%p", port); | |
1321 | ||
1322 | CHECK_OPEN_PORT(); | |
1323 | ||
7890cef6 | 1324 | DEBUG_FMT("Checking output bytes waiting on port %s", port->name); |
3353c22f ML |
1325 | |
1326 | #ifdef _WIN32 | |
1327 | DWORD errors; | |
1328 | COMSTAT comstat; | |
1329 | ||
1330 | if (ClearCommError(port->hdl, &errors, &comstat) == 0) | |
1a2a1544 | 1331 | RETURN_FAIL("ClearCommError() failed"); |
9caa2e86 | 1332 | RETURN_INT(comstat.cbOutQue); |
3353c22f ML |
1333 | #else |
1334 | int bytes_waiting; | |
1335 | if (ioctl(port->fd, TIOCOUTQ, &bytes_waiting) < 0) | |
1336 | RETURN_FAIL("TIOCOUTQ ioctl failed"); | |
9caa2e86 | 1337 | RETURN_INT(bytes_waiting); |
3353c22f ML |
1338 | #endif |
1339 | } | |
1340 | ||
970f279a | 1341 | SP_API enum sp_return sp_new_event_set(struct sp_event_set **result_ptr) |
6f1186aa ML |
1342 | { |
1343 | struct sp_event_set *result; | |
1344 | ||
1345 | TRACE("%p", result_ptr); | |
1346 | ||
1347 | if (!result_ptr) | |
1348 | RETURN_ERROR(SP_ERR_ARG, "Null result"); | |
1349 | ||
1350 | *result_ptr = NULL; | |
1351 | ||
1352 | if (!(result = malloc(sizeof(struct sp_event_set)))) | |
1353 | RETURN_ERROR(SP_ERR_MEM, "sp_event_set malloc() failed"); | |
1354 | ||
1355 | memset(result, 0, sizeof(struct sp_event_set)); | |
1356 | ||
1357 | *result_ptr = result; | |
1358 | ||
1359 | RETURN_OK(); | |
1360 | } | |
1361 | ||
1362 | static enum sp_return add_handle(struct sp_event_set *event_set, | |
1363 | event_handle handle, enum sp_event mask) | |
1364 | { | |
1365 | void *new_handles; | |
1366 | enum sp_event *new_masks; | |
1367 | ||
1368 | TRACE("%p, %d, %d", event_set, handle, mask); | |
1369 | ||
1370 | if (!(new_handles = realloc(event_set->handles, | |
1371 | sizeof(event_handle) * (event_set->count + 1)))) | |
dc422c04 | 1372 | RETURN_ERROR(SP_ERR_MEM, "Handle array realloc() failed"); |
6f1186aa | 1373 | |
ac323d80 UH |
1374 | event_set->handles = new_handles; |
1375 | ||
6f1186aa | 1376 | if (!(new_masks = realloc(event_set->masks, |
ac323d80 | 1377 | sizeof(enum sp_event) * (event_set->count + 1)))) |
dc422c04 | 1378 | RETURN_ERROR(SP_ERR_MEM, "Mask array realloc() failed"); |
6f1186aa | 1379 | |
6f1186aa ML |
1380 | event_set->masks = new_masks; |
1381 | ||
1382 | ((event_handle *) event_set->handles)[event_set->count] = handle; | |
1383 | event_set->masks[event_set->count] = mask; | |
1384 | ||
1385 | event_set->count++; | |
1386 | ||
1387 | RETURN_OK(); | |
1388 | } | |
1389 | ||
970f279a | 1390 | SP_API enum sp_return sp_add_port_events(struct sp_event_set *event_set, |
6f1186aa ML |
1391 | const struct sp_port *port, enum sp_event mask) |
1392 | { | |
1393 | TRACE("%p, %p, %d", event_set, port, mask); | |
1394 | ||
1395 | if (!event_set) | |
1396 | RETURN_ERROR(SP_ERR_ARG, "Null event set"); | |
1397 | ||
1398 | if (!port) | |
1399 | RETURN_ERROR(SP_ERR_ARG, "Null port"); | |
1400 | ||
1401 | if (mask > (SP_EVENT_RX_READY | SP_EVENT_TX_READY | SP_EVENT_ERROR)) | |
1402 | RETURN_ERROR(SP_ERR_ARG, "Invalid event mask"); | |
1403 | ||
1404 | if (!mask) | |
1405 | RETURN_OK(); | |
1406 | ||
1407 | #ifdef _WIN32 | |
1408 | enum sp_event handle_mask; | |
1409 | if ((handle_mask = mask & SP_EVENT_TX_READY)) | |
1410 | TRY(add_handle(event_set, port->write_ovl.hEvent, handle_mask)); | |
1411 | if ((handle_mask = mask & (SP_EVENT_RX_READY | SP_EVENT_ERROR))) | |
1412 | TRY(add_handle(event_set, port->wait_ovl.hEvent, handle_mask)); | |
1413 | #else | |
1414 | TRY(add_handle(event_set, port->fd, mask)); | |
1415 | #endif | |
1416 | ||
1417 | RETURN_OK(); | |
1418 | } | |
1419 | ||
970f279a | 1420 | SP_API void sp_free_event_set(struct sp_event_set *event_set) |
6f1186aa ML |
1421 | { |
1422 | TRACE("%p", event_set); | |
1423 | ||
1424 | if (!event_set) { | |
1425 | DEBUG("Null event set"); | |
1426 | RETURN(); | |
1427 | } | |
1428 | ||
1429 | DEBUG("Freeing event set"); | |
1430 | ||
1431 | if (event_set->handles) | |
1432 | free(event_set->handles); | |
1433 | if (event_set->masks) | |
1434 | free(event_set->masks); | |
1435 | ||
1436 | free(event_set); | |
1437 | ||
1438 | RETURN(); | |
1439 | } | |
1440 | ||
970f279a | 1441 | SP_API enum sp_return sp_wait(struct sp_event_set *event_set, |
1b91c6ea | 1442 | unsigned int timeout_ms) |
6f1186aa | 1443 | { |
1b91c6ea | 1444 | TRACE("%p, %d", event_set, timeout_ms); |
6f1186aa ML |
1445 | |
1446 | if (!event_set) | |
1447 | RETURN_ERROR(SP_ERR_ARG, "Null event set"); | |
1448 | ||
1449 | #ifdef _WIN32 | |
1450 | if (WaitForMultipleObjects(event_set->count, event_set->handles, FALSE, | |
1b91c6ea | 1451 | timeout_ms ? timeout_ms : INFINITE) == WAIT_FAILED) |
6f1186aa ML |
1452 | RETURN_FAIL("WaitForMultipleObjects() failed"); |
1453 | ||
1454 | RETURN_OK(); | |
1455 | #else | |
1456 | struct timeval start, delta, now, end = {0, 0}; | |
127d8d0c | 1457 | const struct timeval max_delta = { |
81243567 UH |
1458 | (INT_MAX / 1000), (INT_MAX % 1000) * 1000 |
1459 | }; | |
127d8d0c | 1460 | int started = 0, timeout_overflow = 0; |
1b91c6ea | 1461 | int result, timeout_remaining_ms; |
6f1186aa ML |
1462 | struct pollfd *pollfds; |
1463 | unsigned int i; | |
1464 | ||
1465 | if (!(pollfds = malloc(sizeof(struct pollfd) * event_set->count))) | |
1466 | RETURN_ERROR(SP_ERR_MEM, "pollfds malloc() failed"); | |
1467 | ||
1468 | for (i = 0; i < event_set->count; i++) { | |
81243567 | 1469 | pollfds[i].fd = ((int *)event_set->handles)[i]; |
6f1186aa ML |
1470 | pollfds[i].events = 0; |
1471 | pollfds[i].revents = 0; | |
1472 | if (event_set->masks[i] & SP_EVENT_RX_READY) | |
1473 | pollfds[i].events |= POLLIN; | |
1474 | if (event_set->masks[i] & SP_EVENT_TX_READY) | |
1475 | pollfds[i].events |= POLLOUT; | |
1476 | if (event_set->masks[i] & SP_EVENT_ERROR) | |
1477 | pollfds[i].events |= POLLERR; | |
1478 | } | |
1479 | ||
1b91c6ea | 1480 | if (timeout_ms) { |
6f1186aa ML |
1481 | /* Get time at start of operation. */ |
1482 | gettimeofday(&start, NULL); | |
1483 | /* Define duration of timeout. */ | |
1b91c6ea UH |
1484 | delta.tv_sec = timeout_ms / 1000; |
1485 | delta.tv_usec = (timeout_ms % 1000) * 1000; | |
6f1186aa ML |
1486 | /* Calculate time at which we should give up. */ |
1487 | timeradd(&start, &delta, &end); | |
1488 | } | |
1489 | ||
1490 | /* Loop until an event occurs. */ | |
dc422c04 | 1491 | while (1) { |
76222ee5 ML |
1492 | /* |
1493 | * Check timeout only if we have run poll() at least once, | |
1494 | * to avoid any issues if a short timeout is reached before | |
1495 | * poll() is even run. | |
1496 | */ | |
6b8eee06 ML |
1497 | if (!timeout_ms) { |
1498 | timeout_remaining_ms = -1; | |
1499 | } else if (!started) { | |
127d8d0c ML |
1500 | timeout_overflow = (timeout_ms > INT_MAX); |
1501 | timeout_remaining_ms = timeout_overflow ? INT_MAX : timeout_ms; | |
6b8eee06 | 1502 | } else { |
6f1186aa ML |
1503 | gettimeofday(&now, NULL); |
1504 | if (timercmp(&now, &end, >)) { | |
dc422c04 | 1505 | DEBUG("Wait timed out"); |
6f1186aa ML |
1506 | break; |
1507 | } | |
1508 | timersub(&end, &now, &delta); | |
127d8d0c ML |
1509 | if ((timeout_overflow = timercmp(&delta, &max_delta, >))) |
1510 | delta = max_delta; | |
1b91c6ea | 1511 | timeout_remaining_ms = delta.tv_sec * 1000 + delta.tv_usec / 1000; |
6f1186aa ML |
1512 | } |
1513 | ||
6b8eee06 | 1514 | result = poll(pollfds, event_set->count, timeout_remaining_ms); |
76222ee5 | 1515 | started = 1; |
6f1186aa ML |
1516 | |
1517 | if (result < 0) { | |
1518 | if (errno == EINTR) { | |
1519 | DEBUG("poll() call was interrupted, repeating"); | |
1520 | continue; | |
1521 | } else { | |
1522 | free(pollfds); | |
1523 | RETURN_FAIL("poll() failed"); | |
1524 | } | |
1525 | } else if (result == 0) { | |
1526 | DEBUG("poll() timed out"); | |
127d8d0c ML |
1527 | if (!timeout_overflow) |
1528 | break; | |
6f1186aa ML |
1529 | } else { |
1530 | DEBUG("poll() completed"); | |
1531 | break; | |
1532 | } | |
1533 | } | |
1534 | ||
1535 | free(pollfds); | |
1536 | RETURN_OK(); | |
1537 | #endif | |
1538 | } | |
1539 | ||
5cea279a | 1540 | #ifdef USE_TERMIOS_SPEED |
7a6d2196 ML |
1541 | static enum sp_return get_baudrate(int fd, int *baudrate) |
1542 | { | |
1543 | void *data; | |
1544 | ||
c33efc48 ML |
1545 | TRACE("%d, %p", fd, baudrate); |
1546 | ||
ea667be7 ML |
1547 | DEBUG("Getting baud rate"); |
1548 | ||
7a6d2196 | 1549 | if (!(data = malloc(get_termios_size()))) |
c33efc48 | 1550 | RETURN_ERROR(SP_ERR_MEM, "termios malloc failed"); |
7a6d2196 | 1551 | |
8d43110a ML |
1552 | if (ioctl(fd, get_termios_get_ioctl(), data) < 0) { |
1553 | free(data); | |
dc422c04 | 1554 | RETURN_FAIL("Getting termios failed"); |
8d43110a | 1555 | } |
7a6d2196 ML |
1556 | |
1557 | *baudrate = get_termios_speed(data); | |
1558 | ||
8d43110a ML |
1559 | free(data); |
1560 | ||
c33efc48 | 1561 | RETURN_OK(); |
7a6d2196 ML |
1562 | } |
1563 | ||
1564 | static enum sp_return set_baudrate(int fd, int baudrate) | |
1565 | { | |
1566 | void *data; | |
1567 | ||
c33efc48 ML |
1568 | TRACE("%d, %d", fd, baudrate); |
1569 | ||
ea667be7 ML |
1570 | DEBUG("Getting baud rate"); |
1571 | ||
7a6d2196 | 1572 | if (!(data = malloc(get_termios_size()))) |
c33efc48 | 1573 | RETURN_ERROR(SP_ERR_MEM, "termios malloc failed"); |
7a6d2196 | 1574 | |
8d43110a ML |
1575 | if (ioctl(fd, get_termios_get_ioctl(), data) < 0) { |
1576 | free(data); | |
dc422c04 | 1577 | RETURN_FAIL("Getting termios failed"); |
8d43110a | 1578 | } |
7a6d2196 | 1579 | |
ea667be7 ML |
1580 | DEBUG("Setting baud rate"); |
1581 | ||
7a6d2196 ML |
1582 | set_termios_speed(data, baudrate); |
1583 | ||
8d43110a ML |
1584 | if (ioctl(fd, get_termios_set_ioctl(), data) < 0) { |
1585 | free(data); | |
dc422c04 | 1586 | RETURN_FAIL("Setting termios failed"); |
8d43110a ML |
1587 | } |
1588 | ||
1589 | free(data); | |
7a6d2196 | 1590 | |
c33efc48 | 1591 | RETURN_OK(); |
7a6d2196 | 1592 | } |
5cea279a | 1593 | #endif /* USE_TERMIOS_SPEED */ |
40978c2b ML |
1594 | |
1595 | #ifdef USE_TERMIOX | |
bd791fe1 | 1596 | static enum sp_return get_flow(int fd, struct port_data *data) |
40978c2b | 1597 | { |
bd791fe1 | 1598 | void *termx; |
40978c2b | 1599 | |
bd791fe1 | 1600 | TRACE("%d, %p", fd, data); |
c33efc48 | 1601 | |
ea667be7 ML |
1602 | DEBUG("Getting advanced flow control"); |
1603 | ||
bd791fe1 | 1604 | if (!(termx = malloc(get_termiox_size()))) |
c33efc48 | 1605 | RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed"); |
40978c2b | 1606 | |
bd791fe1 ML |
1607 | if (ioctl(fd, TCGETX, termx) < 0) { |
1608 | free(termx); | |
dc422c04 | 1609 | RETURN_FAIL("Getting termiox failed"); |
8d43110a | 1610 | } |
40978c2b | 1611 | |
bd791fe1 ML |
1612 | get_termiox_flow(termx, &data->rts_flow, &data->cts_flow, |
1613 | &data->dtr_flow, &data->dsr_flow); | |
40978c2b | 1614 | |
bd791fe1 | 1615 | free(termx); |
8d43110a | 1616 | |
c33efc48 | 1617 | RETURN_OK(); |
40978c2b ML |
1618 | } |
1619 | ||
bd791fe1 | 1620 | static enum sp_return set_flow(int fd, struct port_data *data) |
40978c2b | 1621 | { |
bd791fe1 | 1622 | void *termx; |
40978c2b | 1623 | |
bd791fe1 | 1624 | TRACE("%d, %p", fd, data); |
c33efc48 | 1625 | |
ea667be7 ML |
1626 | DEBUG("Getting advanced flow control"); |
1627 | ||
bd791fe1 | 1628 | if (!(termx = malloc(get_termiox_size()))) |
c33efc48 | 1629 | RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed"); |
40978c2b | 1630 | |
bd791fe1 ML |
1631 | if (ioctl(fd, TCGETX, termx) < 0) { |
1632 | free(termx); | |
dc422c04 | 1633 | RETURN_FAIL("Getting termiox failed"); |
8d43110a | 1634 | } |
40978c2b | 1635 | |
ea667be7 ML |
1636 | DEBUG("Setting advanced flow control"); |
1637 | ||
bd791fe1 ML |
1638 | set_termiox_flow(termx, data->rts_flow, data->cts_flow, |
1639 | data->dtr_flow, data->dsr_flow); | |
40978c2b | 1640 | |
bd791fe1 ML |
1641 | if (ioctl(fd, TCSETX, termx) < 0) { |
1642 | free(termx); | |
dc422c04 | 1643 | RETURN_FAIL("Setting termiox failed"); |
8d43110a ML |
1644 | } |
1645 | ||
bd791fe1 | 1646 | free(termx); |
40978c2b | 1647 | |
c33efc48 | 1648 | RETURN_OK(); |
40978c2b ML |
1649 | } |
1650 | #endif /* USE_TERMIOX */ | |
7a6d2196 | 1651 | |
eb6ed20f ML |
1652 | static enum sp_return get_config(struct sp_port *port, struct port_data *data, |
1653 | struct sp_port_config *config) | |
8094e4a0 | 1654 | { |
da2748bf | 1655 | unsigned int i; |
cbf628c7 | 1656 | |
c33efc48 ML |
1657 | TRACE("%p, %p, %p", port, data, config); |
1658 | ||
7890cef6 | 1659 | DEBUG_FMT("Getting configuration for port %s", port->name); |
ea667be7 | 1660 | |
8094e4a0 | 1661 | #ifdef _WIN32 |
e33ab9aa | 1662 | if (!GetCommState(port->hdl, &data->dcb)) |
c33efc48 | 1663 | RETURN_FAIL("GetCommState() failed"); |
8094e4a0 | 1664 | |
067417af | 1665 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
e33ab9aa | 1666 | if (data->dcb.BaudRate == std_baudrates[i].index) { |
067417af ML |
1667 | config->baudrate = std_baudrates[i].value; |
1668 | break; | |
1669 | } | |
1670 | } | |
1671 | ||
1672 | if (i == NUM_STD_BAUDRATES) | |
1673 | /* BaudRate field can be either an index or a custom baud rate. */ | |
e33ab9aa | 1674 | config->baudrate = data->dcb.BaudRate; |
067417af | 1675 | |
e33ab9aa | 1676 | config->bits = data->dcb.ByteSize; |
067417af | 1677 | |
e33ab9aa ML |
1678 | if (data->dcb.fParity) |
1679 | switch (data->dcb.Parity) { | |
067417af ML |
1680 | case NOPARITY: |
1681 | config->parity = SP_PARITY_NONE; | |
1682 | break; | |
e432ce60 ML |
1683 | case ODDPARITY: |
1684 | config->parity = SP_PARITY_ODD; | |
1685 | break; | |
067417af ML |
1686 | case EVENPARITY: |
1687 | config->parity = SP_PARITY_EVEN; | |
1688 | break; | |
e432ce60 ML |
1689 | case MARKPARITY: |
1690 | config->parity = SP_PARITY_MARK; | |
1691 | break; | |
1692 | case SPACEPARITY: | |
1693 | config->parity = SP_PARITY_SPACE; | |
067417af ML |
1694 | break; |
1695 | default: | |
1696 | config->parity = -1; | |
1697 | } | |
1698 | else | |
1699 | config->parity = SP_PARITY_NONE; | |
1700 | ||
e33ab9aa | 1701 | switch (data->dcb.StopBits) { |
067417af ML |
1702 | case ONESTOPBIT: |
1703 | config->stopbits = 1; | |
1704 | break; | |
1705 | case TWOSTOPBITS: | |
1706 | config->stopbits = 2; | |
1707 | break; | |
1708 | default: | |
1709 | config->stopbits = -1; | |
1710 | } | |
1711 | ||
e33ab9aa | 1712 | switch (data->dcb.fRtsControl) { |
eac329d2 UH |
1713 | case RTS_CONTROL_DISABLE: |
1714 | config->rts = SP_RTS_OFF; | |
1715 | break; | |
1716 | case RTS_CONTROL_ENABLE: | |
1717 | config->rts = SP_RTS_ON; | |
1718 | break; | |
1719 | case RTS_CONTROL_HANDSHAKE: | |
1720 | config->rts = SP_RTS_FLOW_CONTROL; | |
1721 | break; | |
1722 | default: | |
1723 | config->rts = -1; | |
067417af ML |
1724 | } |
1725 | ||
e33ab9aa | 1726 | config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE; |
067417af | 1727 | |
e33ab9aa | 1728 | switch (data->dcb.fDtrControl) { |
eac329d2 UH |
1729 | case DTR_CONTROL_DISABLE: |
1730 | config->dtr = SP_DTR_OFF; | |
1731 | break; | |
1732 | case DTR_CONTROL_ENABLE: | |
1733 | config->dtr = SP_DTR_ON; | |
1734 | break; | |
1735 | case DTR_CONTROL_HANDSHAKE: | |
1736 | config->dtr = SP_DTR_FLOW_CONTROL; | |
1737 | break; | |
1738 | default: | |
1739 | config->dtr = -1; | |
067417af ML |
1740 | } |
1741 | ||
e33ab9aa ML |
1742 | config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE; |
1743 | ||
c6754b45 ML |
1744 | if (data->dcb.fInX) { |
1745 | if (data->dcb.fOutX) | |
1746 | config->xon_xoff = SP_XONXOFF_INOUT; | |
1747 | else | |
1748 | config->xon_xoff = SP_XONXOFF_IN; | |
1749 | } else { | |
1750 | if (data->dcb.fOutX) | |
1751 | config->xon_xoff = SP_XONXOFF_OUT; | |
1752 | else | |
1753 | config->xon_xoff = SP_XONXOFF_DISABLED; | |
1754 | } | |
1755 | ||
e33ab9aa ML |
1756 | #else // !_WIN32 |
1757 | ||
1758 | if (tcgetattr(port->fd, &data->term) < 0) | |
c33efc48 | 1759 | RETURN_FAIL("tcgetattr() failed"); |
e33ab9aa ML |
1760 | |
1761 | if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0) | |
c33efc48 | 1762 | RETURN_FAIL("TIOCMGET ioctl failed"); |
40978c2b ML |
1763 | |
1764 | #ifdef USE_TERMIOX | |
bd791fe1 | 1765 | int ret = get_flow(port->fd, data); |
68ec29db ML |
1766 | |
1767 | if (ret == SP_ERR_FAIL && errno == EINVAL) | |
1768 | data->termiox_supported = 0; | |
1769 | else if (ret < 0) | |
c33efc48 | 1770 | RETURN_CODEVAL(ret); |
68ec29db ML |
1771 | else |
1772 | data->termiox_supported = 1; | |
1773 | #else | |
1774 | data->termiox_supported = 0; | |
40978c2b ML |
1775 | #endif |
1776 | ||
067417af | 1777 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
e33ab9aa | 1778 | if (cfgetispeed(&data->term) == std_baudrates[i].index) { |
067417af ML |
1779 | config->baudrate = std_baudrates[i].value; |
1780 | break; | |
1781 | } | |
1782 | } | |
1783 | ||
31b3a8f5 MH |
1784 | if (i == NUM_STD_BAUDRATES) { |
1785 | #ifdef __APPLE__ | |
1786 | config->baudrate = (int)data->term.c_ispeed; | |
5cea279a | 1787 | #elif defined(USE_TERMIOS_SPEED) |
7a6d2196 | 1788 | TRY(get_baudrate(port->fd, &config->baudrate)); |
31b3a8f5 | 1789 | #else |
067417af | 1790 | config->baudrate = -1; |
31b3a8f5 MH |
1791 | #endif |
1792 | } | |
067417af | 1793 | |
e33ab9aa | 1794 | switch (data->term.c_cflag & CSIZE) { |
067417af ML |
1795 | case CS8: |
1796 | config->bits = 8; | |
1797 | break; | |
1798 | case CS7: | |
1799 | config->bits = 7; | |
1800 | break; | |
1801 | case CS6: | |
1802 | config->bits = 6; | |
1803 | break; | |
1804 | case CS5: | |
1805 | config->bits = 5; | |
1806 | break; | |
1807 | default: | |
1808 | config->bits = -1; | |
1809 | } | |
1810 | ||
e33ab9aa | 1811 | if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR)) |
067417af | 1812 | config->parity = SP_PARITY_NONE; |
e33ab9aa | 1813 | else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR)) |
067417af | 1814 | config->parity = -1; |
c3e05092 | 1815 | #ifdef CMSPAR |
e432ce60 ML |
1816 | else if (data->term.c_cflag & CMSPAR) |
1817 | config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_MARK : SP_PARITY_SPACE; | |
c3e05092 | 1818 | #endif |
067417af | 1819 | else |
e33ab9aa | 1820 | config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN; |
067417af | 1821 | |
e33ab9aa | 1822 | config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1; |
067417af | 1823 | |
e33ab9aa | 1824 | if (data->term.c_cflag & CRTSCTS) { |
067417af ML |
1825 | config->rts = SP_RTS_FLOW_CONTROL; |
1826 | config->cts = SP_CTS_FLOW_CONTROL; | |
1827 | } else { | |
bd791fe1 | 1828 | if (data->termiox_supported && data->rts_flow) |
40978c2b ML |
1829 | config->rts = SP_RTS_FLOW_CONTROL; |
1830 | else | |
1831 | config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF; | |
1832 | ||
bd791fe1 | 1833 | config->cts = (data->termiox_supported && data->cts_flow) ? |
68ec29db | 1834 | SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE; |
067417af ML |
1835 | } |
1836 | ||
bd791fe1 | 1837 | if (data->termiox_supported && data->dtr_flow) |
40978c2b ML |
1838 | config->dtr = SP_DTR_FLOW_CONTROL; |
1839 | else | |
1840 | config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF; | |
1841 | ||
bd791fe1 | 1842 | config->dsr = (data->termiox_supported && data->dsr_flow) ? |
68ec29db | 1843 | SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE; |
705bdc69 | 1844 | |
e29b93a5 ML |
1845 | if (data->term.c_iflag & IXOFF) { |
1846 | if (data->term.c_iflag & IXON) | |
1847 | config->xon_xoff = SP_XONXOFF_INOUT; | |
1848 | else | |
1849 | config->xon_xoff = SP_XONXOFF_IN; | |
1850 | } else { | |
1851 | if (data->term.c_iflag & IXON) | |
1852 | config->xon_xoff = SP_XONXOFF_OUT; | |
1853 | else | |
1854 | config->xon_xoff = SP_XONXOFF_DISABLED; | |
1855 | } | |
067417af ML |
1856 | #endif |
1857 | ||
c33efc48 | 1858 | RETURN_OK(); |
067417af ML |
1859 | } |
1860 | ||
7a6d2196 | 1861 | static enum sp_return set_config(struct sp_port *port, struct port_data *data, |
eb6ed20f | 1862 | const struct sp_port_config *config) |
18fc2dd1 | 1863 | { |
e33ab9aa | 1864 | unsigned int i; |
31b3a8f5 MH |
1865 | #ifdef __APPLE__ |
1866 | BAUD_TYPE baud_nonstd; | |
1867 | ||
1868 | baud_nonstd = B0; | |
1869 | #endif | |
5cea279a | 1870 | #ifdef USE_TERMIOS_SPEED |
7a6d2196 ML |
1871 | int baud_nonstd = 0; |
1872 | #endif | |
18fc2dd1 | 1873 | |
c33efc48 ML |
1874 | TRACE("%p, %p, %p", port, data, config); |
1875 | ||
7890cef6 | 1876 | DEBUG_FMT("Setting configuration for port %s", port->name); |
ea667be7 | 1877 | |
e33ab9aa | 1878 | #ifdef _WIN32 |
eac329d2 | 1879 | if (config->baudrate >= 0) { |
e33ab9aa ML |
1880 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
1881 | if (config->baudrate == std_baudrates[i].value) { | |
1882 | data->dcb.BaudRate = std_baudrates[i].index; | |
1883 | break; | |
1884 | } | |
1885 | } | |
18fc2dd1 | 1886 | |
e33ab9aa ML |
1887 | if (i == NUM_STD_BAUDRATES) |
1888 | data->dcb.BaudRate = config->baudrate; | |
1889 | } | |
18fc2dd1 | 1890 | |
e33ab9aa ML |
1891 | if (config->bits >= 0) |
1892 | data->dcb.ByteSize = config->bits; | |
1893 | ||
1894 | if (config->parity >= 0) { | |
1895 | switch (config->parity) { | |
e33ab9aa ML |
1896 | case SP_PARITY_NONE: |
1897 | data->dcb.Parity = NOPARITY; | |
1898 | break; | |
e432ce60 ML |
1899 | case SP_PARITY_ODD: |
1900 | data->dcb.Parity = ODDPARITY; | |
1901 | break; | |
e33ab9aa ML |
1902 | case SP_PARITY_EVEN: |
1903 | data->dcb.Parity = EVENPARITY; | |
1904 | break; | |
e432ce60 ML |
1905 | case SP_PARITY_MARK: |
1906 | data->dcb.Parity = MARKPARITY; | |
1907 | break; | |
1908 | case SP_PARITY_SPACE: | |
1909 | data->dcb.Parity = SPACEPARITY; | |
e33ab9aa ML |
1910 | break; |
1911 | default: | |
c33efc48 | 1912 | RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting"); |
e33ab9aa | 1913 | } |
18fc2dd1 ML |
1914 | } |
1915 | ||
e33ab9aa ML |
1916 | if (config->stopbits >= 0) { |
1917 | switch (config->stopbits) { | |
1918 | /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */ | |
1919 | case 1: | |
1920 | data->dcb.StopBits = ONESTOPBIT; | |
1921 | break; | |
1922 | case 2: | |
1923 | data->dcb.StopBits = TWOSTOPBITS; | |
1924 | break; | |
1925 | default: | |
c33efc48 | 1926 | RETURN_ERROR(SP_ERR_ARG, "Invalid stop bit setting"); |
e33ab9aa ML |
1927 | } |
1928 | } | |
1929 | ||
1930 | if (config->rts >= 0) { | |
1931 | switch (config->rts) { | |
1932 | case SP_RTS_OFF: | |
1933 | data->dcb.fRtsControl = RTS_CONTROL_DISABLE; | |
1934 | break; | |
1935 | case SP_RTS_ON: | |
1936 | data->dcb.fRtsControl = RTS_CONTROL_ENABLE; | |
1937 | break; | |
1938 | case SP_RTS_FLOW_CONTROL: | |
1939 | data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; | |
1940 | break; | |
1941 | default: | |
c33efc48 | 1942 | RETURN_ERROR(SP_ERR_ARG, "Invalid RTS setting"); |
e33ab9aa ML |
1943 | } |
1944 | } | |
1945 | ||
1946 | if (config->cts >= 0) { | |
1947 | switch (config->cts) { | |
1948 | case SP_CTS_IGNORE: | |
1949 | data->dcb.fOutxCtsFlow = FALSE; | |
1950 | break; | |
1951 | case SP_CTS_FLOW_CONTROL: | |
1952 | data->dcb.fOutxCtsFlow = TRUE; | |
1953 | break; | |
1954 | default: | |
c33efc48 | 1955 | RETURN_ERROR(SP_ERR_ARG, "Invalid CTS setting"); |
e33ab9aa ML |
1956 | } |
1957 | } | |
1958 | ||
1959 | if (config->dtr >= 0) { | |
1960 | switch (config->dtr) { | |
1961 | case SP_DTR_OFF: | |
1962 | data->dcb.fDtrControl = DTR_CONTROL_DISABLE; | |
1963 | break; | |
1964 | case SP_DTR_ON: | |
1965 | data->dcb.fDtrControl = DTR_CONTROL_ENABLE; | |
1966 | break; | |
1967 | case SP_DTR_FLOW_CONTROL: | |
1968 | data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; | |
1969 | break; | |
1970 | default: | |
c33efc48 | 1971 | RETURN_ERROR(SP_ERR_ARG, "Invalid DTR setting"); |
e33ab9aa ML |
1972 | } |
1973 | } | |
1974 | ||
1975 | if (config->dsr >= 0) { | |
1976 | switch (config->dsr) { | |
1977 | case SP_DSR_IGNORE: | |
1978 | data->dcb.fOutxDsrFlow = FALSE; | |
1979 | break; | |
1980 | case SP_DSR_FLOW_CONTROL: | |
1981 | data->dcb.fOutxDsrFlow = TRUE; | |
1982 | break; | |
1983 | default: | |
c33efc48 | 1984 | RETURN_ERROR(SP_ERR_ARG, "Invalid DSR setting"); |
e33ab9aa | 1985 | } |
18fc2dd1 ML |
1986 | } |
1987 | ||
e33ab9aa ML |
1988 | if (config->xon_xoff >= 0) { |
1989 | switch (config->xon_xoff) { | |
1990 | case SP_XONXOFF_DISABLED: | |
1991 | data->dcb.fInX = FALSE; | |
1992 | data->dcb.fOutX = FALSE; | |
1993 | break; | |
1994 | case SP_XONXOFF_IN: | |
1995 | data->dcb.fInX = TRUE; | |
1996 | data->dcb.fOutX = FALSE; | |
1997 | break; | |
1998 | case SP_XONXOFF_OUT: | |
1999 | data->dcb.fInX = FALSE; | |
2000 | data->dcb.fOutX = TRUE; | |
2001 | break; | |
2002 | case SP_XONXOFF_INOUT: | |
2003 | data->dcb.fInX = TRUE; | |
2004 | data->dcb.fOutX = TRUE; | |
2005 | break; | |
2006 | default: | |
c33efc48 | 2007 | RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting"); |
e33ab9aa ML |
2008 | } |
2009 | } | |
2010 | ||
2011 | if (!SetCommState(port->hdl, &data->dcb)) | |
c33efc48 | 2012 | RETURN_FAIL("SetCommState() failed"); |
e33ab9aa | 2013 | |
31b3a8f5 | 2014 | #else /* !_WIN32 */ |
e33ab9aa | 2015 | |
7a6d2196 ML |
2016 | int controlbits; |
2017 | ||
eac329d2 | 2018 | if (config->baudrate >= 0) { |
e33ab9aa ML |
2019 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
2020 | if (config->baudrate == std_baudrates[i].value) { | |
2021 | if (cfsetospeed(&data->term, std_baudrates[i].index) < 0) | |
c33efc48 | 2022 | RETURN_FAIL("cfsetospeed() failed"); |
e33ab9aa ML |
2023 | |
2024 | if (cfsetispeed(&data->term, std_baudrates[i].index) < 0) | |
c33efc48 | 2025 | RETURN_FAIL("cfsetispeed() failed"); |
e33ab9aa ML |
2026 | break; |
2027 | } | |
2028 | } | |
2029 | ||
31b3a8f5 MH |
2030 | /* Non-standard baud rate */ |
2031 | if (i == NUM_STD_BAUDRATES) { | |
2032 | #ifdef __APPLE__ | |
24abdb68 | 2033 | /* Set "dummy" baud rate. */ |
31b3a8f5 | 2034 | if (cfsetspeed(&data->term, B9600) < 0) |
c33efc48 | 2035 | RETURN_FAIL("cfsetspeed() failed"); |
31b3a8f5 | 2036 | baud_nonstd = config->baudrate; |
5cea279a | 2037 | #elif defined(USE_TERMIOS_SPEED) |
7a6d2196 | 2038 | baud_nonstd = 1; |
31b3a8f5 | 2039 | #else |
c33efc48 | 2040 | RETURN_ERROR(SP_ERR_SUPP, "Non-standard baudrate not supported"); |
31b3a8f5 MH |
2041 | #endif |
2042 | } | |
e33ab9aa ML |
2043 | } |
2044 | ||
2045 | if (config->bits >= 0) { | |
2046 | data->term.c_cflag &= ~CSIZE; | |
2047 | switch (config->bits) { | |
2048 | case 8: | |
2049 | data->term.c_cflag |= CS8; | |
2050 | break; | |
2051 | case 7: | |
2052 | data->term.c_cflag |= CS7; | |
2053 | break; | |
2054 | case 6: | |
2055 | data->term.c_cflag |= CS6; | |
2056 | break; | |
23922313 UH |
2057 | case 5: |
2058 | data->term.c_cflag |= CS5; | |
2059 | break; | |
e33ab9aa | 2060 | default: |
c33efc48 | 2061 | RETURN_ERROR(SP_ERR_ARG, "Invalid data bits setting"); |
e33ab9aa ML |
2062 | } |
2063 | } | |
2064 | ||
2065 | if (config->parity >= 0) { | |
2066 | data->term.c_iflag &= ~IGNPAR; | |
c3e05092 UH |
2067 | data->term.c_cflag &= ~(PARENB | PARODD); |
2068 | #ifdef CMSPAR | |
2069 | data->term.c_cflag &= ~CMSPAR; | |
2070 | #endif | |
e33ab9aa ML |
2071 | switch (config->parity) { |
2072 | case SP_PARITY_NONE: | |
2073 | data->term.c_iflag |= IGNPAR; | |
2074 | break; | |
2075 | case SP_PARITY_EVEN: | |
2076 | data->term.c_cflag |= PARENB; | |
2077 | break; | |
2078 | case SP_PARITY_ODD: | |
2079 | data->term.c_cflag |= PARENB | PARODD; | |
2080 | break; | |
afb518f0 | 2081 | #ifdef CMSPAR |
e432ce60 | 2082 | case SP_PARITY_MARK: |
c3e05092 | 2083 | data->term.c_cflag |= PARENB | PARODD; |
c3e05092 | 2084 | data->term.c_cflag |= CMSPAR; |
e432ce60 ML |
2085 | break; |
2086 | case SP_PARITY_SPACE: | |
c3e05092 | 2087 | data->term.c_cflag |= PARENB; |
c3e05092 | 2088 | data->term.c_cflag |= CMSPAR; |
e432ce60 | 2089 | break; |
afb518f0 ML |
2090 | #else |
2091 | case SP_PARITY_MARK: | |
2092 | case SP_PARITY_SPACE: | |
2093 | RETURN_ERROR(SP_ERR_SUPP, "Mark/space parity not supported"); | |
2094 | #endif | |
e33ab9aa | 2095 | default: |
c33efc48 | 2096 | RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting"); |
e33ab9aa ML |
2097 | } |
2098 | } | |
2099 | ||
2100 | if (config->stopbits >= 0) { | |
2101 | data->term.c_cflag &= ~CSTOPB; | |
2102 | switch (config->stopbits) { | |
2103 | case 1: | |
2104 | data->term.c_cflag &= ~CSTOPB; | |
2105 | break; | |
2106 | case 2: | |
2107 | data->term.c_cflag |= CSTOPB; | |
2108 | break; | |
2109 | default: | |
c33efc48 | 2110 | RETURN_ERROR(SP_ERR_ARG, "Invalid stop bits setting"); |
e33ab9aa ML |
2111 | } |
2112 | } | |
2113 | ||
eac329d2 | 2114 | if (config->rts >= 0 || config->cts >= 0) { |
68ec29db | 2115 | if (data->termiox_supported) { |
bd791fe1 | 2116 | data->rts_flow = data->cts_flow = 0; |
68ec29db ML |
2117 | switch (config->rts) { |
2118 | case SP_RTS_OFF: | |
2119 | case SP_RTS_ON: | |
2120 | controlbits = TIOCM_RTS; | |
2121 | if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0) | |
c33efc48 | 2122 | RETURN_FAIL("Setting RTS signal level failed"); |
68ec29db ML |
2123 | break; |
2124 | case SP_RTS_FLOW_CONTROL: | |
bd791fe1 | 2125 | data->rts_flow = 1; |
68ec29db ML |
2126 | break; |
2127 | default: | |
2128 | break; | |
e33ab9aa | 2129 | } |
68ec29db | 2130 | if (config->cts == SP_CTS_FLOW_CONTROL) |
bd791fe1 | 2131 | data->cts_flow = 1; |
e33ab9aa | 2132 | |
bd791fe1 | 2133 | if (data->rts_flow && data->cts_flow) |
e33ab9aa | 2134 | data->term.c_iflag |= CRTSCTS; |
68ec29db ML |
2135 | else |
2136 | data->term.c_iflag &= ~CRTSCTS; | |
2137 | } else { | |
2138 | /* Asymmetric use of RTS/CTS not supported. */ | |
2139 | if (data->term.c_iflag & CRTSCTS) { | |
2140 | /* Flow control can only be disabled for both RTS & CTS together. */ | |
2141 | if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) { | |
2142 | if (config->cts != SP_CTS_IGNORE) | |
c33efc48 | 2143 | RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together"); |
68ec29db ML |
2144 | } |
2145 | if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) { | |
2146 | if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL) | |
c33efc48 | 2147 | RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together"); |
68ec29db | 2148 | } |
e33ab9aa | 2149 | } else { |
68ec29db ML |
2150 | /* Flow control can only be enabled for both RTS & CTS together. */ |
2151 | if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) || | |
2152 | ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL))) | |
c33efc48 | 2153 | RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be enabled together"); |
68ec29db ML |
2154 | } |
2155 | ||
2156 | if (config->rts >= 0) { | |
2157 | if (config->rts == SP_RTS_FLOW_CONTROL) { | |
2158 | data->term.c_iflag |= CRTSCTS; | |
2159 | } else { | |
2160 | controlbits = TIOCM_RTS; | |
2161 | if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, | |
2162 | &controlbits) < 0) | |
c33efc48 | 2163 | RETURN_FAIL("Setting RTS signal level failed"); |
68ec29db | 2164 | } |
e33ab9aa ML |
2165 | } |
2166 | } | |
2167 | } | |
2168 | ||
eac329d2 | 2169 | if (config->dtr >= 0 || config->dsr >= 0) { |
68ec29db | 2170 | if (data->termiox_supported) { |
bd791fe1 | 2171 | data->dtr_flow = data->dsr_flow = 0; |
68ec29db ML |
2172 | switch (config->dtr) { |
2173 | case SP_DTR_OFF: | |
2174 | case SP_DTR_ON: | |
2175 | controlbits = TIOCM_DTR; | |
2176 | if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0) | |
c33efc48 | 2177 | RETURN_FAIL("Setting DTR signal level failed"); |
68ec29db ML |
2178 | break; |
2179 | case SP_DTR_FLOW_CONTROL: | |
bd791fe1 | 2180 | data->dtr_flow = 1; |
68ec29db ML |
2181 | break; |
2182 | default: | |
2183 | break; | |
2184 | } | |
2185 | if (config->dsr == SP_DSR_FLOW_CONTROL) | |
bd791fe1 | 2186 | data->dsr_flow = 1; |
68ec29db ML |
2187 | } else { |
2188 | /* DTR/DSR flow control not supported. */ | |
2189 | if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL) | |
c33efc48 | 2190 | RETURN_ERROR(SP_ERR_SUPP, "DTR/DSR flow control not supported"); |
e33ab9aa | 2191 | |
68ec29db ML |
2192 | if (config->dtr >= 0) { |
2193 | controlbits = TIOCM_DTR; | |
2194 | if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, | |
2195 | &controlbits) < 0) | |
c33efc48 | 2196 | RETURN_FAIL("Setting DTR signal level failed"); |
68ec29db | 2197 | } |
e33ab9aa ML |
2198 | } |
2199 | } | |
2200 | ||
2201 | if (config->xon_xoff >= 0) { | |
2202 | data->term.c_iflag &= ~(IXON | IXOFF | IXANY); | |
2203 | switch (config->xon_xoff) { | |
2204 | case SP_XONXOFF_DISABLED: | |
2205 | break; | |
2206 | case SP_XONXOFF_IN: | |
2207 | data->term.c_iflag |= IXOFF; | |
2208 | break; | |
2209 | case SP_XONXOFF_OUT: | |
2210 | data->term.c_iflag |= IXON | IXANY; | |
2211 | break; | |
2212 | case SP_XONXOFF_INOUT: | |
2213 | data->term.c_iflag |= IXON | IXOFF | IXANY; | |
2214 | break; | |
2215 | default: | |
c33efc48 | 2216 | RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting"); |
e33ab9aa ML |
2217 | } |
2218 | } | |
2219 | ||
3f5c06d0 | 2220 | if (tcsetattr(port->fd, TCSANOW, &data->term) < 0) |
c33efc48 | 2221 | RETURN_FAIL("tcsetattr() failed"); |
31b3a8f5 MH |
2222 | |
2223 | #ifdef __APPLE__ | |
2224 | if (baud_nonstd != B0) { | |
2225 | if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1) | |
c33efc48 | 2226 | RETURN_FAIL("IOSSIOSPEED ioctl failed"); |
dc422c04 UH |
2227 | /* |
2228 | * Set baud rates in data->term to correct, but incompatible | |
2229 | * with tcsetattr() value, same as delivered by tcgetattr(). | |
2230 | */ | |
31b3a8f5 | 2231 | if (cfsetspeed(&data->term, baud_nonstd) < 0) |
c33efc48 | 2232 | RETURN_FAIL("cfsetspeed() failed"); |
31b3a8f5 | 2233 | } |
7a6d2196 | 2234 | #elif defined(__linux__) |
5cea279a | 2235 | #ifdef USE_TERMIOS_SPEED |
7a6d2196 ML |
2236 | if (baud_nonstd) |
2237 | TRY(set_baudrate(port->fd, config->baudrate)); | |
5cea279a | 2238 | #endif |
40978c2b | 2239 | #ifdef USE_TERMIOX |
68ec29db | 2240 | if (data->termiox_supported) |
bd791fe1 | 2241 | TRY(set_flow(port->fd, data)); |
40978c2b | 2242 | #endif |
7a6d2196 | 2243 | #endif |
31b3a8f5 MH |
2244 | |
2245 | #endif /* !_WIN32 */ | |
e33ab9aa | 2246 | |
c33efc48 | 2247 | RETURN_OK(); |
e33ab9aa ML |
2248 | } |
2249 | ||
970f279a | 2250 | SP_API enum sp_return sp_new_config(struct sp_port_config **config_ptr) |
9b1502ef | 2251 | { |
59131d60 | 2252 | struct sp_port_config *config; |
9b1502ef | 2253 | |
00d8c56d UH |
2254 | TRACE("%p", config_ptr); |
2255 | ||
9b1502ef ML |
2256 | if (!config_ptr) |
2257 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); | |
2258 | ||
59131d60 ML |
2259 | *config_ptr = NULL; |
2260 | ||
2261 | if (!(config = malloc(sizeof(struct sp_port_config)))) | |
dc422c04 | 2262 | RETURN_ERROR(SP_ERR_MEM, "Config malloc failed"); |
9b1502ef | 2263 | |
59131d60 ML |
2264 | config->baudrate = -1; |
2265 | config->bits = -1; | |
2266 | config->parity = -1; | |
2267 | config->stopbits = -1; | |
2268 | config->rts = -1; | |
2269 | config->cts = -1; | |
2270 | config->dtr = -1; | |
2271 | config->dsr = -1; | |
2272 | ||
2273 | *config_ptr = config; | |
2274 | ||
9b1502ef ML |
2275 | RETURN_OK(); |
2276 | } | |
2277 | ||
970f279a | 2278 | SP_API void sp_free_config(struct sp_port_config *config) |
9b1502ef ML |
2279 | { |
2280 | TRACE("%p", config); | |
2281 | ||
2282 | if (!config) | |
2283 | DEBUG("Null config"); | |
2284 | else | |
2285 | free(config); | |
2286 | ||
2287 | RETURN(); | |
2288 | } | |
2289 | ||
970f279a AJ |
2290 | SP_API enum sp_return sp_get_config(struct sp_port *port, |
2291 | struct sp_port_config *config) | |
70cd37de ML |
2292 | { |
2293 | struct port_data data; | |
2294 | ||
2295 | TRACE("%p, %p", port, config); | |
2296 | ||
2297 | CHECK_OPEN_PORT(); | |
2298 | ||
2299 | if (!config) | |
9b1502ef | 2300 | RETURN_ERROR(SP_ERR_ARG, "Null config"); |
70cd37de ML |
2301 | |
2302 | TRY(get_config(port, &data, config)); | |
2303 | ||
2304 | RETURN_OK(); | |
2305 | } | |
2306 | ||
970f279a AJ |
2307 | SP_API enum sp_return sp_set_config(struct sp_port *port, |
2308 | const struct sp_port_config *config) | |
e33ab9aa | 2309 | { |
8f189c4c | 2310 | struct port_data data; |
e33ab9aa ML |
2311 | struct sp_port_config prev_config; |
2312 | ||
c33efc48 ML |
2313 | TRACE("%p, %p", port, config); |
2314 | ||
dec10e31 | 2315 | CHECK_OPEN_PORT(); |
823690ae ML |
2316 | |
2317 | if (!config) | |
c33efc48 | 2318 | RETURN_ERROR(SP_ERR_ARG, "Null config"); |
823690ae | 2319 | |
e33ab9aa ML |
2320 | TRY(get_config(port, &data, &prev_config)); |
2321 | TRY(set_config(port, &data, config)); | |
74510d4b | 2322 | |
c33efc48 | 2323 | RETURN_OK(); |
74510d4b ML |
2324 | } |
2325 | ||
9b1502ef | 2326 | #define CREATE_ACCESSORS(x, type) \ |
970f279a | 2327 | SP_API enum sp_return sp_set_##x(struct sp_port *port, type x) { \ |
8f189c4c | 2328 | struct port_data data; \ |
e33ab9aa | 2329 | struct sp_port_config config; \ |
c33efc48 | 2330 | TRACE("%p, %d", port, x); \ |
dec10e31 | 2331 | CHECK_OPEN_PORT(); \ |
e33ab9aa ML |
2332 | TRY(get_config(port, &data, &config)); \ |
2333 | config.x = x; \ | |
2334 | TRY(set_config(port, &data, &config)); \ | |
c33efc48 | 2335 | RETURN_OK(); \ |
9b1502ef | 2336 | } \ |
970f279a AJ |
2337 | SP_API enum sp_return sp_get_config_##x(const struct sp_port_config *config, \ |
2338 | type *x) { \ | |
00d8c56d | 2339 | TRACE("%p, %p", config, x); \ |
6aad5bdd UH |
2340 | if (!x) \ |
2341 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); \ | |
9b1502ef ML |
2342 | if (!config) \ |
2343 | RETURN_ERROR(SP_ERR_ARG, "Null config"); \ | |
2344 | *x = config->x; \ | |
2345 | RETURN_OK(); \ | |
2346 | } \ | |
970f279a AJ |
2347 | SP_API enum sp_return sp_set_config_##x(struct sp_port_config *config, \ |
2348 | type x) { \ | |
9b1502ef ML |
2349 | TRACE("%p, %d", config, x); \ |
2350 | if (!config) \ | |
2351 | RETURN_ERROR(SP_ERR_ARG, "Null config"); \ | |
2352 | config->x = x; \ | |
2353 | RETURN_OK(); \ | |
9069c2fb ML |
2354 | } |
2355 | ||
9b1502ef ML |
2356 | CREATE_ACCESSORS(baudrate, int) |
2357 | CREATE_ACCESSORS(bits, int) | |
2358 | CREATE_ACCESSORS(parity, enum sp_parity) | |
2359 | CREATE_ACCESSORS(stopbits, int) | |
2360 | CREATE_ACCESSORS(rts, enum sp_rts) | |
2361 | CREATE_ACCESSORS(cts, enum sp_cts) | |
2362 | CREATE_ACCESSORS(dtr, enum sp_dtr) | |
2363 | CREATE_ACCESSORS(dsr, enum sp_dsr) | |
2364 | CREATE_ACCESSORS(xon_xoff, enum sp_xonxoff) | |
2365 | ||
970f279a AJ |
2366 | SP_API enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, |
2367 | enum sp_flowcontrol flowcontrol) | |
e33ab9aa | 2368 | { |
9b1502ef ML |
2369 | if (!config) |
2370 | RETURN_ERROR(SP_ERR_ARG, "Null configuration"); | |
dec10e31 ML |
2371 | |
2372 | if (flowcontrol > SP_FLOWCONTROL_DTRDSR) | |
2373 | RETURN_ERROR(SP_ERR_ARG, "Invalid flow control setting"); | |
823690ae | 2374 | |
e33ab9aa | 2375 | if (flowcontrol == SP_FLOWCONTROL_XONXOFF) |
9b1502ef | 2376 | config->xon_xoff = SP_XONXOFF_INOUT; |
e33ab9aa | 2377 | else |
9b1502ef | 2378 | config->xon_xoff = SP_XONXOFF_DISABLED; |
e33ab9aa ML |
2379 | |
2380 | if (flowcontrol == SP_FLOWCONTROL_RTSCTS) { | |
9b1502ef ML |
2381 | config->rts = SP_RTS_FLOW_CONTROL; |
2382 | config->cts = SP_CTS_FLOW_CONTROL; | |
e33ab9aa | 2383 | } else { |
9b1502ef ML |
2384 | if (config->rts == SP_RTS_FLOW_CONTROL) |
2385 | config->rts = SP_RTS_ON; | |
2386 | config->cts = SP_CTS_IGNORE; | |
e33ab9aa ML |
2387 | } |
2388 | ||
2389 | if (flowcontrol == SP_FLOWCONTROL_DTRDSR) { | |
9b1502ef ML |
2390 | config->dtr = SP_DTR_FLOW_CONTROL; |
2391 | config->dsr = SP_DSR_FLOW_CONTROL; | |
e33ab9aa | 2392 | } else { |
9b1502ef ML |
2393 | if (config->dtr == SP_DTR_FLOW_CONTROL) |
2394 | config->dtr = SP_DTR_ON; | |
2395 | config->dsr = SP_DSR_IGNORE; | |
e33ab9aa ML |
2396 | } |
2397 | ||
9b1502ef ML |
2398 | RETURN_OK(); |
2399 | } | |
2400 | ||
970f279a AJ |
2401 | SP_API enum sp_return sp_set_flowcontrol(struct sp_port *port, |
2402 | enum sp_flowcontrol flowcontrol) | |
9b1502ef ML |
2403 | { |
2404 | struct port_data data; | |
2405 | struct sp_port_config config; | |
2406 | ||
2407 | TRACE("%p, %d", port, flowcontrol); | |
2408 | ||
2409 | CHECK_OPEN_PORT(); | |
2410 | ||
2411 | TRY(get_config(port, &data, &config)); | |
2412 | ||
2413 | TRY(sp_set_config_flowcontrol(&config, flowcontrol)); | |
2414 | ||
e33ab9aa ML |
2415 | TRY(set_config(port, &data, &config)); |
2416 | ||
c33efc48 | 2417 | RETURN_OK(); |
e33ab9aa ML |
2418 | } |
2419 | ||
970f279a AJ |
2420 | SP_API enum sp_return sp_get_signals(struct sp_port *port, |
2421 | enum sp_signal *signals) | |
8cf7c697 | 2422 | { |
c33efc48 ML |
2423 | TRACE("%p, %p", port, signals); |
2424 | ||
dec10e31 | 2425 | CHECK_OPEN_PORT(); |
8cf7c697 ML |
2426 | |
2427 | if (!signals) | |
c33efc48 | 2428 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); |
8cf7c697 | 2429 | |
7890cef6 | 2430 | DEBUG_FMT("Getting control signals for port %s", port->name); |
ea667be7 | 2431 | |
8cf7c697 ML |
2432 | *signals = 0; |
2433 | #ifdef _WIN32 | |
2434 | DWORD bits; | |
2435 | if (GetCommModemStatus(port->hdl, &bits) == 0) | |
c33efc48 | 2436 | RETURN_FAIL("GetCommModemStatus() failed"); |
8cf7c697 ML |
2437 | if (bits & MS_CTS_ON) |
2438 | *signals |= SP_SIG_CTS; | |
2439 | if (bits & MS_DSR_ON) | |
2440 | *signals |= SP_SIG_DSR; | |
8cf7c697 | 2441 | if (bits & MS_RLSD_ON) |
a6cda1e8 ML |
2442 | *signals |= SP_SIG_DCD; |
2443 | if (bits & MS_RING_ON) | |
8cf7c697 ML |
2444 | *signals |= SP_SIG_RI; |
2445 | #else | |
2446 | int bits; | |
2447 | if (ioctl(port->fd, TIOCMGET, &bits) < 0) | |
c33efc48 | 2448 | RETURN_FAIL("TIOCMGET ioctl failed"); |
8cf7c697 ML |
2449 | if (bits & TIOCM_CTS) |
2450 | *signals |= SP_SIG_CTS; | |
2451 | if (bits & TIOCM_DSR) | |
2452 | *signals |= SP_SIG_DSR; | |
2453 | if (bits & TIOCM_CAR) | |
2454 | *signals |= SP_SIG_DCD; | |
2455 | if (bits & TIOCM_RNG) | |
2456 | *signals |= SP_SIG_RI; | |
2457 | #endif | |
c33efc48 | 2458 | RETURN_OK(); |
8cf7c697 ML |
2459 | } |
2460 | ||
970f279a | 2461 | SP_API enum sp_return sp_start_break(struct sp_port *port) |
90cc3ee6 | 2462 | { |
c33efc48 ML |
2463 | TRACE("%p", port); |
2464 | ||
dec10e31 | 2465 | CHECK_OPEN_PORT(); |
90cc3ee6 ML |
2466 | #ifdef _WIN32 |
2467 | if (SetCommBreak(port->hdl) == 0) | |
c33efc48 | 2468 | RETURN_FAIL("SetCommBreak() failed"); |
90cc3ee6 ML |
2469 | #else |
2470 | if (ioctl(port->fd, TIOCSBRK, 1) < 0) | |
c33efc48 | 2471 | RETURN_FAIL("TIOCSBRK ioctl failed"); |
90cc3ee6 ML |
2472 | #endif |
2473 | ||
c33efc48 | 2474 | RETURN_OK(); |
90cc3ee6 ML |
2475 | } |
2476 | ||
970f279a | 2477 | SP_API enum sp_return sp_end_break(struct sp_port *port) |
90cc3ee6 | 2478 | { |
c33efc48 ML |
2479 | TRACE("%p", port); |
2480 | ||
dec10e31 | 2481 | CHECK_OPEN_PORT(); |
90cc3ee6 ML |
2482 | #ifdef _WIN32 |
2483 | if (ClearCommBreak(port->hdl) == 0) | |
c33efc48 | 2484 | RETURN_FAIL("ClearCommBreak() failed"); |
90cc3ee6 ML |
2485 | #else |
2486 | if (ioctl(port->fd, TIOCCBRK, 1) < 0) | |
c33efc48 | 2487 | RETURN_FAIL("TIOCCBRK ioctl failed"); |
90cc3ee6 ML |
2488 | #endif |
2489 | ||
c33efc48 | 2490 | RETURN_OK(); |
90cc3ee6 ML |
2491 | } |
2492 | ||
970f279a | 2493 | SP_API int sp_last_error_code(void) |
74510d4b | 2494 | { |
7890cef6 | 2495 | TRACE_VOID(); |
74510d4b | 2496 | #ifdef _WIN32 |
9caa2e86 | 2497 | RETURN_INT(GetLastError()); |
74510d4b | 2498 | #else |
9caa2e86 | 2499 | RETURN_INT(errno); |
74510d4b ML |
2500 | #endif |
2501 | } | |
2502 | ||
970f279a | 2503 | SP_API char *sp_last_error_message(void) |
74510d4b | 2504 | { |
7890cef6 | 2505 | TRACE_VOID(); |
c33efc48 | 2506 | |
74510d4b | 2507 | #ifdef _WIN32 |
9a0c4bbd | 2508 | TCHAR *message; |
74510d4b ML |
2509 | DWORD error = GetLastError(); |
2510 | ||
9a0c4bbd | 2511 | DWORD length = FormatMessage( |
74510d4b ML |
2512 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
2513 | FORMAT_MESSAGE_FROM_SYSTEM | | |
2514 | FORMAT_MESSAGE_IGNORE_INSERTS, | |
2515 | NULL, | |
2516 | error, | |
2517 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
2518 | (LPTSTR) &message, | |
2519 | 0, NULL ); | |
2520 | ||
9a0c4bbd ML |
2521 | if (length >= 2 && message[length - 2] == '\r') |
2522 | message[length - 2] = '\0'; | |
2523 | ||
9caa2e86 | 2524 | RETURN_STRING(message); |
74510d4b | 2525 | #else |
9caa2e86 | 2526 | RETURN_STRING(strerror(errno)); |
74510d4b ML |
2527 | #endif |
2528 | } | |
2529 | ||
970f279a | 2530 | SP_API void sp_free_error_message(char *message) |
74510d4b | 2531 | { |
c33efc48 ML |
2532 | TRACE("%s", message); |
2533 | ||
74510d4b ML |
2534 | #ifdef _WIN32 |
2535 | LocalFree(message); | |
64eec30d ML |
2536 | #else |
2537 | (void)message; | |
74510d4b | 2538 | #endif |
c33efc48 ML |
2539 | |
2540 | RETURN(); | |
74510d4b | 2541 | } |
863b35e6 | 2542 | |
970f279a | 2543 | SP_API void sp_set_debug_handler(void (*handler)(const char *format, ...)) |
863b35e6 | 2544 | { |
c33efc48 ML |
2545 | TRACE("%p", handler); |
2546 | ||
863b35e6 | 2547 | sp_debug_handler = handler; |
c33efc48 ML |
2548 | |
2549 | RETURN(); | |
863b35e6 ML |
2550 | } |
2551 | ||
970f279a | 2552 | SP_API void sp_default_debug_handler(const char *format, ...) |
863b35e6 ML |
2553 | { |
2554 | va_list args; | |
2555 | va_start(args, format); | |
2556 | if (getenv("LIBSERIALPORT_DEBUG")) { | |
dd7742fb | 2557 | fputs("sp: ", stderr); |
863b35e6 ML |
2558 | vfprintf(stderr, format, args); |
2559 | } | |
2560 | va_end(args); | |
2561 | } | |
524b0e14 | 2562 | |
970f279a | 2563 | SP_API int sp_get_major_package_version(void) |
524b0e14 UH |
2564 | { |
2565 | return SP_PACKAGE_VERSION_MAJOR; | |
2566 | } | |
2567 | ||
970f279a | 2568 | SP_API int sp_get_minor_package_version(void) |
524b0e14 UH |
2569 | { |
2570 | return SP_PACKAGE_VERSION_MINOR; | |
2571 | } | |
2572 | ||
970f279a | 2573 | SP_API int sp_get_micro_package_version(void) |
524b0e14 UH |
2574 | { |
2575 | return SP_PACKAGE_VERSION_MICRO; | |
2576 | } | |
2577 | ||
970f279a | 2578 | SP_API const char *sp_get_package_version_string(void) |
524b0e14 UH |
2579 | { |
2580 | return SP_PACKAGE_VERSION_STRING; | |
2581 | } | |
2582 | ||
970f279a | 2583 | SP_API int sp_get_current_lib_version(void) |
524b0e14 UH |
2584 | { |
2585 | return SP_LIB_VERSION_CURRENT; | |
2586 | } | |
2587 | ||
970f279a | 2588 | SP_API int sp_get_revision_lib_version(void) |
524b0e14 UH |
2589 | { |
2590 | return SP_LIB_VERSION_REVISION; | |
2591 | } | |
2592 | ||
970f279a | 2593 | SP_API int sp_get_age_lib_version(void) |
524b0e14 UH |
2594 | { |
2595 | return SP_LIB_VERSION_AGE; | |
2596 | } | |
2597 | ||
970f279a | 2598 | SP_API const char *sp_get_lib_version_string(void) |
524b0e14 UH |
2599 | { |
2600 | return SP_LIB_VERSION_STRING; | |
2601 | } | |
2602 | ||
2603 | /** @} */ |