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