]>
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 |
fa106ef1 | 544 | int flags_local = O_NONBLOCK | O_NOCTTY | O_CLOEXEC; |
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) { | |
abd31fd9 ML |
723 | #if defined(__ANDROID__) && (__ANDROID_API__ < 21) |
724 | /* Android only has tcdrain from platform 21 onwards. | |
725 | * On previous API versions, use the ioctl directly. */ | |
33fd8804 ML |
726 | int arg = 1; |
727 | result = ioctl(port->fd, TCSBRK, &arg); | |
728 | #else | |
2c827b21 | 729 | result = tcdrain(port->fd); |
33fd8804 | 730 | #endif |
2c827b21 ML |
731 | if (result < 0) { |
732 | if (errno == EINTR) { | |
733 | DEBUG("tcdrain() was interrupted"); | |
734 | continue; | |
735 | } else { | |
736 | RETURN_FAIL("tcdrain() failed"); | |
737 | } | |
738 | } else { | |
739 | RETURN_OK(); | |
740 | } | |
741 | } | |
69a3739c | 742 | #endif |
69a3739c ML |
743 | } |
744 | ||
62ed9f80 ML |
745 | #ifdef _WIN32 |
746 | static enum sp_return await_write_completion(struct sp_port *port) | |
747 | { | |
748 | TRACE("%p", port); | |
749 | DWORD bytes_written; | |
750 | BOOL result; | |
751 | ||
752 | /* Wait for previous non-blocking write to complete, if any. */ | |
753 | if (port->writing) { | |
754 | DEBUG("Waiting for previous write to complete"); | |
755 | result = GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE); | |
756 | port->writing = 0; | |
757 | if (!result) | |
758 | RETURN_FAIL("Previous write failed to complete"); | |
759 | DEBUG("Previous write completed"); | |
760 | } | |
761 | ||
762 | RETURN_OK(); | |
763 | } | |
764 | #endif | |
765 | ||
970f279a | 766 | SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, |
1b91c6ea | 767 | size_t count, unsigned int timeout_ms) |
e3dcf906 | 768 | { |
1b91c6ea | 769 | TRACE("%p, %p, %d, %d", port, buf, count, timeout_ms); |
e3dcf906 ML |
770 | |
771 | CHECK_OPEN_PORT(); | |
772 | ||
773 | if (!buf) | |
774 | RETURN_ERROR(SP_ERR_ARG, "Null buffer"); | |
775 | ||
1b91c6ea | 776 | if (timeout_ms) |
7890cef6 | 777 | DEBUG_FMT("Writing %d bytes to port %s, timeout %d ms", |
1b91c6ea | 778 | count, port->name, timeout_ms); |
e3dcf906 | 779 | else |
7890cef6 ML |
780 | DEBUG_FMT("Writing %d bytes to port %s, no timeout", |
781 | count, port->name); | |
e3dcf906 ML |
782 | |
783 | if (count == 0) | |
9caa2e86 | 784 | RETURN_INT(0); |
e3dcf906 ML |
785 | |
786 | #ifdef _WIN32 | |
060d1d8a ML |
787 | DWORD remaining_ms, write_size, bytes_written, total_bytes_written = 0; |
788 | const uint8_t *write_ptr = (uint8_t *) buf; | |
789 | bool result; | |
790 | struct timeout timeout; | |
791 | ||
792 | timeout_start(&timeout, timeout_ms); | |
e3dcf906 | 793 | |
62ed9f80 | 794 | TRY(await_write_completion(port)); |
e3dcf906 | 795 | |
060d1d8a | 796 | while (total_bytes_written < count) { |
e3dcf906 | 797 | |
060d1d8a ML |
798 | if (timeout_check(&timeout)) |
799 | break; | |
800 | ||
801 | remaining_ms = timeout_remaining_ms(&timeout); | |
802 | ||
803 | if (port->timeouts.WriteTotalTimeoutConstant != remaining_ms) { | |
804 | port->timeouts.WriteTotalTimeoutConstant = remaining_ms; | |
805 | if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) | |
806 | RETURN_FAIL("SetCommTimeouts() failed"); | |
807 | } | |
808 | ||
809 | /* Reduce write size if it exceeds the WriteFile limit. */ | |
810 | write_size = count - total_bytes_written; | |
811 | if (write_size > WRITEFILE_MAX_SIZE) | |
812 | write_size = WRITEFILE_MAX_SIZE; | |
813 | ||
814 | /* Start write. */ | |
815 | ||
816 | result = WriteFile(port->hdl, write_ptr, write_size, NULL, &port->write_ovl); | |
817 | ||
818 | timeout_update(&timeout); | |
819 | ||
820 | if (result) { | |
821 | DEBUG("Write completed immediately"); | |
822 | bytes_written = write_size; | |
823 | } else if (GetLastError() == ERROR_IO_PENDING) { | |
824 | DEBUG("Waiting for write to complete"); | |
825 | if (GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE) == 0) { | |
826 | if (GetLastError() == ERROR_SEM_TIMEOUT) { | |
827 | DEBUG("Write timed out"); | |
828 | break; | |
829 | } else { | |
830 | RETURN_FAIL("GetOverlappedResult() failed"); | |
831 | } | |
aee7d691 | 832 | } |
060d1d8a ML |
833 | DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, write_size); |
834 | } else { | |
835 | RETURN_FAIL("WriteFile() failed"); | |
aee7d691 | 836 | } |
060d1d8a ML |
837 | |
838 | write_ptr += bytes_written; | |
839 | total_bytes_written += bytes_written; | |
e3dcf906 | 840 | } |
060d1d8a ML |
841 | |
842 | RETURN_INT(total_bytes_written); | |
e3dcf906 ML |
843 | #else |
844 | size_t bytes_written = 0; | |
845 | unsigned char *ptr = (unsigned char *) buf; | |
32dbe2d2 | 846 | struct timeout timeout; |
e3dcf906 ML |
847 | fd_set fds; |
848 | int result; | |
849 | ||
32dbe2d2 | 850 | timeout_start(&timeout, timeout_ms); |
e3dcf906 | 851 | |
772c5861 ML |
852 | FD_ZERO(&fds); |
853 | FD_SET(port->fd, &fds); | |
854 | ||
e3dcf906 | 855 | /* Loop until we have written the requested number of bytes. */ |
dc422c04 | 856 | while (bytes_written < count) { |
bd72614f ML |
857 | |
858 | if (timeout_check(&timeout)) | |
32dbe2d2 ML |
859 | break; |
860 | ||
861 | result = select(port->fd + 1, NULL, &fds, NULL, timeout_timeval(&timeout)); | |
bd72614f ML |
862 | |
863 | timeout_update(&timeout); | |
864 | ||
63a17c64 ML |
865 | if (result < 0) { |
866 | if (errno == EINTR) { | |
867 | DEBUG("select() call was interrupted, repeating"); | |
868 | continue; | |
869 | } else { | |
870 | RETURN_FAIL("select() failed"); | |
871 | } | |
872 | } else if (result == 0) { | |
c3cee38c ML |
873 | /* Timeout has expired. */ |
874 | break; | |
e3dcf906 ML |
875 | } |
876 | ||
877 | /* Do write. */ | |
878 | result = write(port->fd, ptr, count - bytes_written); | |
879 | ||
880 | if (result < 0) { | |
881 | if (errno == EAGAIN) | |
882 | /* This shouldn't happen because we did a select() first, but handle anyway. */ | |
883 | continue; | |
884 | else | |
885 | /* This is an actual failure. */ | |
886 | RETURN_FAIL("write() failed"); | |
887 | } | |
888 | ||
889 | bytes_written += result; | |
890 | ptr += result; | |
891 | } | |
892 | ||
c3cee38c ML |
893 | if (bytes_written < count) |
894 | DEBUG("Write timed out"); | |
895 | ||
9caa2e86 | 896 | RETURN_INT(bytes_written); |
e3dcf906 ML |
897 | #endif |
898 | } | |
899 | ||
970f279a AJ |
900 | SP_API enum sp_return sp_nonblocking_write(struct sp_port *port, |
901 | const void *buf, size_t count) | |
74510d4b | 902 | { |
c33efc48 ML |
903 | TRACE("%p, %p, %d", port, buf, count); |
904 | ||
dec10e31 | 905 | CHECK_OPEN_PORT(); |
74510d4b ML |
906 | |
907 | if (!buf) | |
c33efc48 | 908 | RETURN_ERROR(SP_ERR_ARG, "Null buffer"); |
74510d4b | 909 | |
7890cef6 | 910 | DEBUG_FMT("Writing up to %d bytes to port %s", count, port->name); |
ea667be7 | 911 | |
a3cb91f5 | 912 | if (count == 0) |
9caa2e86 | 913 | RETURN_INT(0); |
a3cb91f5 | 914 | |
74510d4b | 915 | #ifdef _WIN32 |
39df7833 | 916 | DWORD buf_bytes; |
f92f1f0c | 917 | |
e3dcf906 ML |
918 | /* Check whether previous write is complete. */ |
919 | if (port->writing) { | |
920 | if (HasOverlappedIoCompleted(&port->write_ovl)) { | |
921 | DEBUG("Previous write completed"); | |
922 | port->writing = 0; | |
923 | } else { | |
924 | DEBUG("Previous write not complete"); | |
925 | /* Can't take a new write until the previous one finishes. */ | |
9caa2e86 | 926 | RETURN_INT(0); |
a3cb91f5 | 927 | } |
e3dcf906 | 928 | } |
a3cb91f5 | 929 | |
e3dcf906 | 930 | /* Set timeout. */ |
27a58c05 ML |
931 | if (port->timeouts.WriteTotalTimeoutConstant != 0) { |
932 | port->timeouts.WriteTotalTimeoutConstant = 0; | |
933 | if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) | |
934 | RETURN_FAIL("SetCommTimeouts() failed"); | |
935 | } | |
e3dcf906 | 936 | |
b457865b ML |
937 | /* Reduce count if it exceeds the WriteFile limit. */ |
938 | if (count > WRITEFILE_MAX_SIZE) | |
939 | count = WRITEFILE_MAX_SIZE; | |
940 | ||
39df7833 ML |
941 | /* Copy data to our write buffer. */ |
942 | buf_bytes = min(port->write_buf_size, count); | |
943 | memcpy(port->write_buf, buf, buf_bytes); | |
944 | ||
945 | /* Start asynchronous write. */ | |
946 | if (WriteFile(port->hdl, port->write_buf, buf_bytes, NULL, &port->write_ovl) == 0) { | |
947 | if (GetLastError() == ERROR_IO_PENDING) { | |
948 | if ((port->writing = !HasOverlappedIoCompleted(&port->write_ovl))) | |
949 | DEBUG("Asynchronous write completed immediately"); | |
950 | else | |
951 | DEBUG("Asynchronous write running"); | |
e3dcf906 | 952 | } else { |
39df7833 ML |
953 | /* Actual failure of some kind. */ |
954 | RETURN_FAIL("WriteFile() failed"); | |
a3cb91f5 ML |
955 | } |
956 | } | |
957 | ||
9af8cff3 | 958 | DEBUG("All bytes written immediately"); |
e3dcf906 | 959 | |
39df7833 | 960 | RETURN_INT(buf_bytes); |
74510d4b ML |
961 | #else |
962 | /* Returns the number of bytes written, or -1 upon failure. */ | |
963 | ssize_t written = write(port->fd, buf, count); | |
f92f1f0c | 964 | |
55ab7e0b ML |
965 | if (written < 0) { |
966 | if (errno == EAGAIN) | |
967 | // Buffer is full, no bytes written. | |
968 | RETURN_INT(0); | |
969 | else | |
970 | RETURN_FAIL("write() failed"); | |
971 | } else { | |
9caa2e86 | 972 | RETURN_INT(written); |
55ab7e0b | 973 | } |
74510d4b ML |
974 | #endif |
975 | } | |
976 | ||
94a314a8 ML |
977 | #ifdef _WIN32 |
978 | /* Restart wait operation if buffer was emptied. */ | |
979 | static enum sp_return restart_wait_if_needed(struct sp_port *port, unsigned int bytes_read) | |
980 | { | |
8ebfaf89 ML |
981 | DWORD errors; |
982 | COMSTAT comstat; | |
94a314a8 | 983 | |
21ae5ce1 ML |
984 | if (bytes_read == 0) |
985 | RETURN_OK(); | |
986 | ||
8ebfaf89 ML |
987 | if (ClearCommError(port->hdl, &errors, &comstat) == 0) |
988 | RETURN_FAIL("ClearCommError() failed"); | |
94a314a8 | 989 | |
8ebfaf89 | 990 | if (comstat.cbInQue == 0) |
94a314a8 ML |
991 | TRY(restart_wait(port)); |
992 | ||
993 | RETURN_OK(); | |
994 | } | |
995 | #endif | |
996 | ||
970f279a | 997 | SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, |
1b91c6ea | 998 | size_t count, unsigned int timeout_ms) |
e3dcf906 | 999 | { |
1b91c6ea | 1000 | TRACE("%p, %p, %d, %d", port, buf, count, timeout_ms); |
e3dcf906 ML |
1001 | |
1002 | CHECK_OPEN_PORT(); | |
1003 | ||
1004 | if (!buf) | |
1005 | RETURN_ERROR(SP_ERR_ARG, "Null buffer"); | |
1006 | ||
1b91c6ea | 1007 | if (timeout_ms) |
7890cef6 | 1008 | DEBUG_FMT("Reading %d bytes from port %s, timeout %d ms", |
1b91c6ea | 1009 | count, port->name, timeout_ms); |
e3dcf906 | 1010 | else |
7890cef6 ML |
1011 | DEBUG_FMT("Reading %d bytes from port %s, no timeout", |
1012 | count, port->name); | |
e3dcf906 ML |
1013 | |
1014 | if (count == 0) | |
9caa2e86 | 1015 | RETURN_INT(0); |
e3dcf906 ML |
1016 | |
1017 | #ifdef _WIN32 | |
1018 | DWORD bytes_read = 0; | |
1019 | ||
1020 | /* Set timeout. */ | |
27a58c05 | 1021 | if (port->timeouts.ReadIntervalTimeout != 0 || |
0188a545 | 1022 | port->timeouts.ReadTotalTimeoutMultiplier != 0 || |
27a58c05 ML |
1023 | port->timeouts.ReadTotalTimeoutConstant != timeout_ms) { |
1024 | port->timeouts.ReadIntervalTimeout = 0; | |
0188a545 | 1025 | port->timeouts.ReadTotalTimeoutMultiplier = 0; |
27a58c05 ML |
1026 | port->timeouts.ReadTotalTimeoutConstant = timeout_ms; |
1027 | if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) | |
1028 | RETURN_FAIL("SetCommTimeouts() failed"); | |
1029 | } | |
e3dcf906 ML |
1030 | |
1031 | /* Start read. */ | |
bbe566fe | 1032 | if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl)) { |
e3dcf906 | 1033 | DEBUG("Read completed immediately"); |
6f1186aa | 1034 | bytes_read = count; |
bbe566fe ML |
1035 | } else if (GetLastError() == ERROR_IO_PENDING) { |
1036 | DEBUG("Waiting for read to complete"); | |
27911925 ML |
1037 | if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0) |
1038 | RETURN_FAIL("GetOverlappedResult() failed"); | |
bbe566fe ML |
1039 | DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count); |
1040 | } else { | |
1041 | RETURN_FAIL("ReadFile() failed"); | |
6f1186aa ML |
1042 | } |
1043 | ||
94a314a8 | 1044 | TRY(restart_wait_if_needed(port, bytes_read)); |
6f1186aa | 1045 | |
9caa2e86 | 1046 | RETURN_INT(bytes_read); |
6f1186aa | 1047 | |
e3dcf906 ML |
1048 | #else |
1049 | size_t bytes_read = 0; | |
9a7945af | 1050 | unsigned char *ptr = (unsigned char *) buf; |
32dbe2d2 | 1051 | struct timeout timeout; |
e3dcf906 ML |
1052 | fd_set fds; |
1053 | int result; | |
1054 | ||
32dbe2d2 | 1055 | timeout_start(&timeout, timeout_ms); |
e3dcf906 | 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) { |
bd72614f ML |
1062 | |
1063 | if (timeout_check(&timeout)) | |
32dbe2d2 ML |
1064 | /* Timeout has expired. */ |
1065 | break; | |
1066 | ||
1067 | result = select(port->fd + 1, &fds, NULL, NULL, timeout_timeval(&timeout)); | |
bd72614f ML |
1068 | |
1069 | timeout_update(&timeout); | |
1070 | ||
63a17c64 ML |
1071 | if (result < 0) { |
1072 | if (errno == EINTR) { | |
1073 | DEBUG("select() call was interrupted, repeating"); | |
1074 | continue; | |
1075 | } else { | |
1076 | RETURN_FAIL("select() failed"); | |
1077 | } | |
1078 | } else if (result == 0) { | |
c3cee38c ML |
1079 | /* Timeout has expired. */ |
1080 | break; | |
e3dcf906 ML |
1081 | } |
1082 | ||
1083 | /* Do read. */ | |
1084 | result = read(port->fd, ptr, count - bytes_read); | |
1085 | ||
1086 | if (result < 0) { | |
1087 | if (errno == EAGAIN) | |
76222ee5 ML |
1088 | /* |
1089 | * This shouldn't happen because we did a | |
1090 | * select() first, but handle anyway. | |
1091 | */ | |
e3dcf906 ML |
1092 | continue; |
1093 | else | |
1094 | /* This is an actual failure. */ | |
1095 | RETURN_FAIL("read() failed"); | |
1096 | } | |
1097 | ||
1098 | bytes_read += result; | |
1099 | ptr += result; | |
1100 | } | |
1101 | ||
c3cee38c ML |
1102 | if (bytes_read < count) |
1103 | DEBUG("Read timed out"); | |
1104 | ||
9caa2e86 | 1105 | RETURN_INT(bytes_read); |
e3dcf906 ML |
1106 | #endif |
1107 | } | |
1108 | ||
e5c2630e ML |
1109 | SP_API enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, |
1110 | size_t count, unsigned int timeout_ms) | |
1111 | { | |
1112 | TRACE("%p, %p, %d, %d", port, buf, count, timeout_ms); | |
1113 | ||
1114 | CHECK_OPEN_PORT(); | |
1115 | ||
1116 | if (!buf) | |
1117 | RETURN_ERROR(SP_ERR_ARG, "Null buffer"); | |
1118 | ||
1119 | if (count == 0) | |
1120 | RETURN_ERROR(SP_ERR_ARG, "Zero count"); | |
1121 | ||
1122 | if (timeout_ms) | |
1123 | DEBUG_FMT("Reading next max %d bytes from port %s, timeout %d ms", | |
1124 | count, port->name, timeout_ms); | |
1125 | else | |
1126 | DEBUG_FMT("Reading next max %d bytes from port %s, no timeout", | |
1127 | count, port->name); | |
1128 | ||
1129 | #ifdef _WIN32 | |
1130 | DWORD bytes_read = 0; | |
1131 | ||
1132 | /* If timeout_ms == 0, set maximum timeout. */ | |
1133 | DWORD timeout_val = (timeout_ms == 0 ? MAXDWORD - 1 : timeout_ms); | |
1134 | ||
1135 | /* Set timeout. */ | |
1136 | if (port->timeouts.ReadIntervalTimeout != MAXDWORD || | |
1137 | port->timeouts.ReadTotalTimeoutMultiplier != MAXDWORD || | |
1138 | port->timeouts.ReadTotalTimeoutConstant != timeout_val) { | |
1139 | port->timeouts.ReadIntervalTimeout = MAXDWORD; | |
1140 | port->timeouts.ReadTotalTimeoutMultiplier = MAXDWORD; | |
1141 | port->timeouts.ReadTotalTimeoutConstant = timeout_val; | |
1142 | if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) | |
1143 | RETURN_FAIL("SetCommTimeouts() failed"); | |
1144 | } | |
1145 | ||
1146 | /* Loop until we have at least one byte, or timeout is reached. */ | |
1147 | while (bytes_read == 0) { | |
1148 | /* Start read. */ | |
888fb45d | 1149 | if (ReadFile(port->hdl, buf, count, &bytes_read, &port->read_ovl)) { |
e5c2630e | 1150 | DEBUG("Read completed immediately"); |
e5c2630e ML |
1151 | } else if (GetLastError() == ERROR_IO_PENDING) { |
1152 | DEBUG("Waiting for read to complete"); | |
1153 | if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0) | |
1154 | RETURN_FAIL("GetOverlappedResult() failed"); | |
1155 | if (bytes_read > 0) { | |
1156 | DEBUG("Read completed"); | |
1157 | } else if (timeout_ms > 0) { | |
1158 | DEBUG("Read timed out"); | |
1159 | break; | |
1160 | } else { | |
1161 | DEBUG("Restarting read"); | |
1162 | } | |
1163 | } else { | |
1164 | RETURN_FAIL("ReadFile() failed"); | |
1165 | } | |
1166 | } | |
1167 | ||
1168 | TRY(restart_wait_if_needed(port, bytes_read)); | |
1169 | ||
1170 | RETURN_INT(bytes_read); | |
1171 | ||
1172 | #else | |
1173 | size_t bytes_read = 0; | |
32dbe2d2 | 1174 | struct timeout timeout; |
e5c2630e ML |
1175 | fd_set fds; |
1176 | int result; | |
1177 | ||
32dbe2d2 | 1178 | timeout_start(&timeout, timeout_ms); |
e5c2630e ML |
1179 | |
1180 | FD_ZERO(&fds); | |
1181 | FD_SET(port->fd, &fds); | |
1182 | ||
1183 | /* Loop until we have at least one byte, or timeout is reached. */ | |
1184 | while (bytes_read == 0) { | |
bd72614f ML |
1185 | |
1186 | if (timeout_check(&timeout)) | |
32dbe2d2 ML |
1187 | /* Timeout has expired. */ |
1188 | break; | |
1189 | ||
1190 | result = select(port->fd + 1, &fds, NULL, NULL, timeout_timeval(&timeout)); | |
bd72614f ML |
1191 | |
1192 | timeout_update(&timeout); | |
1193 | ||
e5c2630e ML |
1194 | if (result < 0) { |
1195 | if (errno == EINTR) { | |
1196 | DEBUG("select() call was interrupted, repeating"); | |
1197 | continue; | |
1198 | } else { | |
1199 | RETURN_FAIL("select() failed"); | |
1200 | } | |
1201 | } else if (result == 0) { | |
1202 | /* Timeout has expired. */ | |
1203 | break; | |
1204 | } | |
1205 | ||
1206 | /* Do read. */ | |
1207 | result = read(port->fd, buf, count); | |
1208 | ||
1209 | if (result < 0) { | |
1210 | if (errno == EAGAIN) | |
1211 | /* This shouldn't happen because we did a select() first, but handle anyway. */ | |
1212 | continue; | |
1213 | else | |
1214 | /* This is an actual failure. */ | |
1215 | RETURN_FAIL("read() failed"); | |
1216 | } | |
1217 | ||
1218 | bytes_read = result; | |
1219 | } | |
1220 | ||
1221 | if (bytes_read == 0) | |
1222 | DEBUG("Read timed out"); | |
1223 | ||
1224 | RETURN_INT(bytes_read); | |
1225 | #endif | |
1226 | } | |
1227 | ||
970f279a AJ |
1228 | SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, |
1229 | size_t count) | |
74510d4b | 1230 | { |
c33efc48 ML |
1231 | TRACE("%p, %p, %d", port, buf, count); |
1232 | ||
dec10e31 | 1233 | CHECK_OPEN_PORT(); |
74510d4b ML |
1234 | |
1235 | if (!buf) | |
c33efc48 | 1236 | RETURN_ERROR(SP_ERR_ARG, "Null buffer"); |
74510d4b | 1237 | |
7890cef6 | 1238 | DEBUG_FMT("Reading up to %d bytes from port %s", count, port->name); |
ea667be7 | 1239 | |
74510d4b | 1240 | #ifdef _WIN32 |
e3dcf906 | 1241 | DWORD bytes_read; |
f92f1f0c | 1242 | |
e3dcf906 | 1243 | /* Set timeout. */ |
27a58c05 | 1244 | if (port->timeouts.ReadIntervalTimeout != MAXDWORD || |
0188a545 | 1245 | port->timeouts.ReadTotalTimeoutMultiplier != 0 || |
27a58c05 ML |
1246 | port->timeouts.ReadTotalTimeoutConstant != 0) { |
1247 | port->timeouts.ReadIntervalTimeout = MAXDWORD; | |
0188a545 | 1248 | port->timeouts.ReadTotalTimeoutMultiplier = 0; |
27a58c05 ML |
1249 | port->timeouts.ReadTotalTimeoutConstant = 0; |
1250 | if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) | |
1251 | RETURN_FAIL("SetCommTimeouts() failed"); | |
1252 | } | |
e3dcf906 ML |
1253 | |
1254 | /* Do read. */ | |
1255 | if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0) | |
ae2c5825 ML |
1256 | if (GetLastError() != ERROR_IO_PENDING) |
1257 | RETURN_FAIL("ReadFile() failed"); | |
e3dcf906 ML |
1258 | |
1259 | /* Get number of bytes read. */ | |
ae2c5825 | 1260 | if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, FALSE) == 0) |
1622ef60 | 1261 | RETURN_FAIL("GetOverlappedResult() failed"); |
e3dcf906 | 1262 | |
94a314a8 | 1263 | TRY(restart_wait_if_needed(port, bytes_read)); |
6f1186aa | 1264 | |
9caa2e86 | 1265 | RETURN_INT(bytes_read); |
74510d4b ML |
1266 | #else |
1267 | ssize_t bytes_read; | |
f92f1f0c | 1268 | |
74510d4b | 1269 | /* Returns the number of bytes read, or -1 upon failure. */ |
33d5ff47 | 1270 | if ((bytes_read = read(port->fd, buf, count)) < 0) { |
e3dcf906 ML |
1271 | if (errno == EAGAIN) |
1272 | /* No bytes available. */ | |
33d5ff47 ML |
1273 | bytes_read = 0; |
1274 | else | |
1275 | /* This is an actual failure. */ | |
1276 | RETURN_FAIL("read() failed"); | |
1277 | } | |
9caa2e86 | 1278 | RETURN_INT(bytes_read); |
74510d4b ML |
1279 | #endif |
1280 | } | |
1281 | ||
970f279a | 1282 | SP_API enum sp_return sp_input_waiting(struct sp_port *port) |
3353c22f ML |
1283 | { |
1284 | TRACE("%p", port); | |
1285 | ||
1286 | CHECK_OPEN_PORT(); | |
1287 | ||
7890cef6 | 1288 | DEBUG_FMT("Checking input bytes waiting on port %s", port->name); |
3353c22f ML |
1289 | |
1290 | #ifdef _WIN32 | |
1291 | DWORD errors; | |
1292 | COMSTAT comstat; | |
1293 | ||
1294 | if (ClearCommError(port->hdl, &errors, &comstat) == 0) | |
1a2a1544 | 1295 | RETURN_FAIL("ClearCommError() failed"); |
9caa2e86 | 1296 | RETURN_INT(comstat.cbInQue); |
3353c22f ML |
1297 | #else |
1298 | int bytes_waiting; | |
1299 | if (ioctl(port->fd, TIOCINQ, &bytes_waiting) < 0) | |
1300 | RETURN_FAIL("TIOCINQ ioctl failed"); | |
9caa2e86 | 1301 | RETURN_INT(bytes_waiting); |
3353c22f ML |
1302 | #endif |
1303 | } | |
1304 | ||
970f279a | 1305 | SP_API enum sp_return sp_output_waiting(struct sp_port *port) |
3353c22f ML |
1306 | { |
1307 | TRACE("%p", port); | |
1308 | ||
1309 | CHECK_OPEN_PORT(); | |
1310 | ||
7890cef6 | 1311 | DEBUG_FMT("Checking output bytes waiting on port %s", port->name); |
3353c22f ML |
1312 | |
1313 | #ifdef _WIN32 | |
1314 | DWORD errors; | |
1315 | COMSTAT comstat; | |
1316 | ||
1317 | if (ClearCommError(port->hdl, &errors, &comstat) == 0) | |
1a2a1544 | 1318 | RETURN_FAIL("ClearCommError() failed"); |
9caa2e86 | 1319 | RETURN_INT(comstat.cbOutQue); |
3353c22f ML |
1320 | #else |
1321 | int bytes_waiting; | |
1322 | if (ioctl(port->fd, TIOCOUTQ, &bytes_waiting) < 0) | |
1323 | RETURN_FAIL("TIOCOUTQ ioctl failed"); | |
9caa2e86 | 1324 | RETURN_INT(bytes_waiting); |
3353c22f ML |
1325 | #endif |
1326 | } | |
1327 | ||
970f279a | 1328 | SP_API enum sp_return sp_new_event_set(struct sp_event_set **result_ptr) |
6f1186aa ML |
1329 | { |
1330 | struct sp_event_set *result; | |
1331 | ||
1332 | TRACE("%p", result_ptr); | |
1333 | ||
1334 | if (!result_ptr) | |
1335 | RETURN_ERROR(SP_ERR_ARG, "Null result"); | |
1336 | ||
1337 | *result_ptr = NULL; | |
1338 | ||
1339 | if (!(result = malloc(sizeof(struct sp_event_set)))) | |
1340 | RETURN_ERROR(SP_ERR_MEM, "sp_event_set malloc() failed"); | |
1341 | ||
1342 | memset(result, 0, sizeof(struct sp_event_set)); | |
1343 | ||
1344 | *result_ptr = result; | |
1345 | ||
1346 | RETURN_OK(); | |
1347 | } | |
1348 | ||
1349 | static enum sp_return add_handle(struct sp_event_set *event_set, | |
1350 | event_handle handle, enum sp_event mask) | |
1351 | { | |
1352 | void *new_handles; | |
1353 | enum sp_event *new_masks; | |
1354 | ||
1355 | TRACE("%p, %d, %d", event_set, handle, mask); | |
1356 | ||
1357 | if (!(new_handles = realloc(event_set->handles, | |
1358 | sizeof(event_handle) * (event_set->count + 1)))) | |
dc422c04 | 1359 | RETURN_ERROR(SP_ERR_MEM, "Handle array realloc() failed"); |
6f1186aa | 1360 | |
ac323d80 UH |
1361 | event_set->handles = new_handles; |
1362 | ||
6f1186aa | 1363 | if (!(new_masks = realloc(event_set->masks, |
ac323d80 | 1364 | sizeof(enum sp_event) * (event_set->count + 1)))) |
dc422c04 | 1365 | RETURN_ERROR(SP_ERR_MEM, "Mask array realloc() failed"); |
6f1186aa | 1366 | |
6f1186aa ML |
1367 | event_set->masks = new_masks; |
1368 | ||
1369 | ((event_handle *) event_set->handles)[event_set->count] = handle; | |
1370 | event_set->masks[event_set->count] = mask; | |
1371 | ||
1372 | event_set->count++; | |
1373 | ||
1374 | RETURN_OK(); | |
1375 | } | |
1376 | ||
970f279a | 1377 | SP_API enum sp_return sp_add_port_events(struct sp_event_set *event_set, |
6f1186aa ML |
1378 | const struct sp_port *port, enum sp_event mask) |
1379 | { | |
1380 | TRACE("%p, %p, %d", event_set, port, mask); | |
1381 | ||
1382 | if (!event_set) | |
1383 | RETURN_ERROR(SP_ERR_ARG, "Null event set"); | |
1384 | ||
1385 | if (!port) | |
1386 | RETURN_ERROR(SP_ERR_ARG, "Null port"); | |
1387 | ||
1388 | if (mask > (SP_EVENT_RX_READY | SP_EVENT_TX_READY | SP_EVENT_ERROR)) | |
1389 | RETURN_ERROR(SP_ERR_ARG, "Invalid event mask"); | |
1390 | ||
1391 | if (!mask) | |
1392 | RETURN_OK(); | |
1393 | ||
1394 | #ifdef _WIN32 | |
1395 | enum sp_event handle_mask; | |
1396 | if ((handle_mask = mask & SP_EVENT_TX_READY)) | |
1397 | TRY(add_handle(event_set, port->write_ovl.hEvent, handle_mask)); | |
1398 | if ((handle_mask = mask & (SP_EVENT_RX_READY | SP_EVENT_ERROR))) | |
1399 | TRY(add_handle(event_set, port->wait_ovl.hEvent, handle_mask)); | |
1400 | #else | |
1401 | TRY(add_handle(event_set, port->fd, mask)); | |
1402 | #endif | |
1403 | ||
1404 | RETURN_OK(); | |
1405 | } | |
1406 | ||
970f279a | 1407 | SP_API void sp_free_event_set(struct sp_event_set *event_set) |
6f1186aa ML |
1408 | { |
1409 | TRACE("%p", event_set); | |
1410 | ||
1411 | if (!event_set) { | |
1412 | DEBUG("Null event set"); | |
1413 | RETURN(); | |
1414 | } | |
1415 | ||
1416 | DEBUG("Freeing event set"); | |
1417 | ||
1418 | if (event_set->handles) | |
1419 | free(event_set->handles); | |
1420 | if (event_set->masks) | |
1421 | free(event_set->masks); | |
1422 | ||
1423 | free(event_set); | |
1424 | ||
1425 | RETURN(); | |
1426 | } | |
1427 | ||
970f279a | 1428 | SP_API enum sp_return sp_wait(struct sp_event_set *event_set, |
1b91c6ea | 1429 | unsigned int timeout_ms) |
6f1186aa | 1430 | { |
1b91c6ea | 1431 | TRACE("%p, %d", event_set, timeout_ms); |
6f1186aa ML |
1432 | |
1433 | if (!event_set) | |
1434 | RETURN_ERROR(SP_ERR_ARG, "Null event set"); | |
1435 | ||
1436 | #ifdef _WIN32 | |
1437 | if (WaitForMultipleObjects(event_set->count, event_set->handles, FALSE, | |
1b91c6ea | 1438 | timeout_ms ? timeout_ms : INFINITE) == WAIT_FAILED) |
6f1186aa ML |
1439 | RETURN_FAIL("WaitForMultipleObjects() failed"); |
1440 | ||
1441 | RETURN_OK(); | |
1442 | #else | |
32dbe2d2 | 1443 | struct timeout timeout; |
bd72614f | 1444 | int result; |
6f1186aa ML |
1445 | struct pollfd *pollfds; |
1446 | unsigned int i; | |
1447 | ||
1448 | if (!(pollfds = malloc(sizeof(struct pollfd) * event_set->count))) | |
1449 | RETURN_ERROR(SP_ERR_MEM, "pollfds malloc() failed"); | |
1450 | ||
1451 | for (i = 0; i < event_set->count; i++) { | |
81243567 | 1452 | pollfds[i].fd = ((int *)event_set->handles)[i]; |
6f1186aa ML |
1453 | pollfds[i].events = 0; |
1454 | pollfds[i].revents = 0; | |
1455 | if (event_set->masks[i] & SP_EVENT_RX_READY) | |
1456 | pollfds[i].events |= POLLIN; | |
1457 | if (event_set->masks[i] & SP_EVENT_TX_READY) | |
1458 | pollfds[i].events |= POLLOUT; | |
1459 | if (event_set->masks[i] & SP_EVENT_ERROR) | |
1460 | pollfds[i].events |= POLLERR; | |
1461 | } | |
1462 | ||
32dbe2d2 | 1463 | timeout_start(&timeout, timeout_ms); |
08eb25f5 | 1464 | timeout_limit(&timeout, INT_MAX); |
6f1186aa ML |
1465 | |
1466 | /* Loop until an event occurs. */ | |
dc422c04 | 1467 | while (1) { |
bd72614f ML |
1468 | |
1469 | if (timeout_check(&timeout)) { | |
32dbe2d2 ML |
1470 | DEBUG("Wait timed out"); |
1471 | break; | |
6f1186aa ML |
1472 | } |
1473 | ||
9d1ca7c8 | 1474 | result = poll(pollfds, event_set->count, timeout_remaining_ms(&timeout) || -1); |
bd72614f ML |
1475 | |
1476 | timeout_update(&timeout); | |
6f1186aa ML |
1477 | |
1478 | if (result < 0) { | |
1479 | if (errno == EINTR) { | |
1480 | DEBUG("poll() call was interrupted, repeating"); | |
1481 | continue; | |
1482 | } else { | |
1483 | free(pollfds); | |
1484 | RETURN_FAIL("poll() failed"); | |
1485 | } | |
1486 | } else if (result == 0) { | |
1487 | DEBUG("poll() timed out"); | |
32dbe2d2 | 1488 | if (!timeout.overflow) |
127d8d0c | 1489 | break; |
6f1186aa ML |
1490 | } else { |
1491 | DEBUG("poll() completed"); | |
1492 | break; | |
1493 | } | |
1494 | } | |
1495 | ||
1496 | free(pollfds); | |
1497 | RETURN_OK(); | |
1498 | #endif | |
1499 | } | |
1500 | ||
5cea279a | 1501 | #ifdef USE_TERMIOS_SPEED |
7a6d2196 ML |
1502 | static enum sp_return get_baudrate(int fd, int *baudrate) |
1503 | { | |
1504 | void *data; | |
1505 | ||
c33efc48 ML |
1506 | TRACE("%d, %p", fd, baudrate); |
1507 | ||
ea667be7 ML |
1508 | DEBUG("Getting baud rate"); |
1509 | ||
7a6d2196 | 1510 | if (!(data = malloc(get_termios_size()))) |
c33efc48 | 1511 | RETURN_ERROR(SP_ERR_MEM, "termios malloc failed"); |
7a6d2196 | 1512 | |
8d43110a ML |
1513 | if (ioctl(fd, get_termios_get_ioctl(), data) < 0) { |
1514 | free(data); | |
dc422c04 | 1515 | RETURN_FAIL("Getting termios failed"); |
8d43110a | 1516 | } |
7a6d2196 ML |
1517 | |
1518 | *baudrate = get_termios_speed(data); | |
1519 | ||
8d43110a ML |
1520 | free(data); |
1521 | ||
c33efc48 | 1522 | RETURN_OK(); |
7a6d2196 ML |
1523 | } |
1524 | ||
1525 | static enum sp_return set_baudrate(int fd, int baudrate) | |
1526 | { | |
1527 | void *data; | |
1528 | ||
c33efc48 ML |
1529 | TRACE("%d, %d", fd, baudrate); |
1530 | ||
ea667be7 ML |
1531 | DEBUG("Getting baud rate"); |
1532 | ||
7a6d2196 | 1533 | if (!(data = malloc(get_termios_size()))) |
c33efc48 | 1534 | RETURN_ERROR(SP_ERR_MEM, "termios malloc failed"); |
7a6d2196 | 1535 | |
8d43110a ML |
1536 | if (ioctl(fd, get_termios_get_ioctl(), data) < 0) { |
1537 | free(data); | |
dc422c04 | 1538 | RETURN_FAIL("Getting termios failed"); |
8d43110a | 1539 | } |
7a6d2196 | 1540 | |
ea667be7 ML |
1541 | DEBUG("Setting baud rate"); |
1542 | ||
7a6d2196 ML |
1543 | set_termios_speed(data, baudrate); |
1544 | ||
8d43110a ML |
1545 | if (ioctl(fd, get_termios_set_ioctl(), data) < 0) { |
1546 | free(data); | |
dc422c04 | 1547 | RETURN_FAIL("Setting termios failed"); |
8d43110a ML |
1548 | } |
1549 | ||
1550 | free(data); | |
7a6d2196 | 1551 | |
c33efc48 | 1552 | RETURN_OK(); |
7a6d2196 | 1553 | } |
5cea279a | 1554 | #endif /* USE_TERMIOS_SPEED */ |
40978c2b ML |
1555 | |
1556 | #ifdef USE_TERMIOX | |
bd791fe1 | 1557 | static enum sp_return get_flow(int fd, struct port_data *data) |
40978c2b | 1558 | { |
bd791fe1 | 1559 | void *termx; |
40978c2b | 1560 | |
bd791fe1 | 1561 | TRACE("%d, %p", fd, data); |
c33efc48 | 1562 | |
ea667be7 ML |
1563 | DEBUG("Getting advanced flow control"); |
1564 | ||
bd791fe1 | 1565 | if (!(termx = malloc(get_termiox_size()))) |
c33efc48 | 1566 | RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed"); |
40978c2b | 1567 | |
bd791fe1 ML |
1568 | if (ioctl(fd, TCGETX, termx) < 0) { |
1569 | free(termx); | |
dc422c04 | 1570 | RETURN_FAIL("Getting termiox failed"); |
8d43110a | 1571 | } |
40978c2b | 1572 | |
bd791fe1 ML |
1573 | get_termiox_flow(termx, &data->rts_flow, &data->cts_flow, |
1574 | &data->dtr_flow, &data->dsr_flow); | |
40978c2b | 1575 | |
bd791fe1 | 1576 | free(termx); |
8d43110a | 1577 | |
c33efc48 | 1578 | RETURN_OK(); |
40978c2b ML |
1579 | } |
1580 | ||
bd791fe1 | 1581 | static enum sp_return set_flow(int fd, struct port_data *data) |
40978c2b | 1582 | { |
bd791fe1 | 1583 | void *termx; |
40978c2b | 1584 | |
bd791fe1 | 1585 | TRACE("%d, %p", fd, data); |
c33efc48 | 1586 | |
ea667be7 ML |
1587 | DEBUG("Getting advanced flow control"); |
1588 | ||
bd791fe1 | 1589 | if (!(termx = malloc(get_termiox_size()))) |
c33efc48 | 1590 | RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed"); |
40978c2b | 1591 | |
bd791fe1 ML |
1592 | if (ioctl(fd, TCGETX, termx) < 0) { |
1593 | free(termx); | |
dc422c04 | 1594 | RETURN_FAIL("Getting termiox failed"); |
8d43110a | 1595 | } |
40978c2b | 1596 | |
ea667be7 ML |
1597 | DEBUG("Setting advanced flow control"); |
1598 | ||
bd791fe1 ML |
1599 | set_termiox_flow(termx, data->rts_flow, data->cts_flow, |
1600 | data->dtr_flow, data->dsr_flow); | |
40978c2b | 1601 | |
bd791fe1 ML |
1602 | if (ioctl(fd, TCSETX, termx) < 0) { |
1603 | free(termx); | |
dc422c04 | 1604 | RETURN_FAIL("Setting termiox failed"); |
8d43110a ML |
1605 | } |
1606 | ||
bd791fe1 | 1607 | free(termx); |
40978c2b | 1608 | |
c33efc48 | 1609 | RETURN_OK(); |
40978c2b ML |
1610 | } |
1611 | #endif /* USE_TERMIOX */ | |
7a6d2196 | 1612 | |
eb6ed20f ML |
1613 | static enum sp_return get_config(struct sp_port *port, struct port_data *data, |
1614 | struct sp_port_config *config) | |
8094e4a0 | 1615 | { |
da2748bf | 1616 | unsigned int i; |
cbf628c7 | 1617 | |
c33efc48 ML |
1618 | TRACE("%p, %p, %p", port, data, config); |
1619 | ||
7890cef6 | 1620 | DEBUG_FMT("Getting configuration for port %s", port->name); |
ea667be7 | 1621 | |
8094e4a0 | 1622 | #ifdef _WIN32 |
e33ab9aa | 1623 | if (!GetCommState(port->hdl, &data->dcb)) |
c33efc48 | 1624 | RETURN_FAIL("GetCommState() failed"); |
8094e4a0 | 1625 | |
067417af | 1626 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
e33ab9aa | 1627 | if (data->dcb.BaudRate == std_baudrates[i].index) { |
067417af ML |
1628 | config->baudrate = std_baudrates[i].value; |
1629 | break; | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | if (i == NUM_STD_BAUDRATES) | |
1634 | /* BaudRate field can be either an index or a custom baud rate. */ | |
e33ab9aa | 1635 | config->baudrate = data->dcb.BaudRate; |
067417af | 1636 | |
e33ab9aa | 1637 | config->bits = data->dcb.ByteSize; |
067417af | 1638 | |
e33ab9aa ML |
1639 | if (data->dcb.fParity) |
1640 | switch (data->dcb.Parity) { | |
067417af ML |
1641 | case NOPARITY: |
1642 | config->parity = SP_PARITY_NONE; | |
1643 | break; | |
e432ce60 ML |
1644 | case ODDPARITY: |
1645 | config->parity = SP_PARITY_ODD; | |
1646 | break; | |
067417af ML |
1647 | case EVENPARITY: |
1648 | config->parity = SP_PARITY_EVEN; | |
1649 | break; | |
e432ce60 ML |
1650 | case MARKPARITY: |
1651 | config->parity = SP_PARITY_MARK; | |
1652 | break; | |
1653 | case SPACEPARITY: | |
1654 | config->parity = SP_PARITY_SPACE; | |
067417af ML |
1655 | break; |
1656 | default: | |
1657 | config->parity = -1; | |
1658 | } | |
1659 | else | |
1660 | config->parity = SP_PARITY_NONE; | |
1661 | ||
e33ab9aa | 1662 | switch (data->dcb.StopBits) { |
067417af ML |
1663 | case ONESTOPBIT: |
1664 | config->stopbits = 1; | |
1665 | break; | |
1666 | case TWOSTOPBITS: | |
1667 | config->stopbits = 2; | |
1668 | break; | |
1669 | default: | |
1670 | config->stopbits = -1; | |
1671 | } | |
1672 | ||
e33ab9aa | 1673 | switch (data->dcb.fRtsControl) { |
eac329d2 UH |
1674 | case RTS_CONTROL_DISABLE: |
1675 | config->rts = SP_RTS_OFF; | |
1676 | break; | |
1677 | case RTS_CONTROL_ENABLE: | |
1678 | config->rts = SP_RTS_ON; | |
1679 | break; | |
1680 | case RTS_CONTROL_HANDSHAKE: | |
1681 | config->rts = SP_RTS_FLOW_CONTROL; | |
1682 | break; | |
1683 | default: | |
1684 | config->rts = -1; | |
067417af ML |
1685 | } |
1686 | ||
e33ab9aa | 1687 | config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE; |
067417af | 1688 | |
e33ab9aa | 1689 | switch (data->dcb.fDtrControl) { |
eac329d2 UH |
1690 | case DTR_CONTROL_DISABLE: |
1691 | config->dtr = SP_DTR_OFF; | |
1692 | break; | |
1693 | case DTR_CONTROL_ENABLE: | |
1694 | config->dtr = SP_DTR_ON; | |
1695 | break; | |
1696 | case DTR_CONTROL_HANDSHAKE: | |
1697 | config->dtr = SP_DTR_FLOW_CONTROL; | |
1698 | break; | |
1699 | default: | |
1700 | config->dtr = -1; | |
067417af ML |
1701 | } |
1702 | ||
e33ab9aa ML |
1703 | config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE; |
1704 | ||
c6754b45 ML |
1705 | if (data->dcb.fInX) { |
1706 | if (data->dcb.fOutX) | |
1707 | config->xon_xoff = SP_XONXOFF_INOUT; | |
1708 | else | |
1709 | config->xon_xoff = SP_XONXOFF_IN; | |
1710 | } else { | |
1711 | if (data->dcb.fOutX) | |
1712 | config->xon_xoff = SP_XONXOFF_OUT; | |
1713 | else | |
1714 | config->xon_xoff = SP_XONXOFF_DISABLED; | |
1715 | } | |
1716 | ||
e33ab9aa ML |
1717 | #else // !_WIN32 |
1718 | ||
1719 | if (tcgetattr(port->fd, &data->term) < 0) | |
c33efc48 | 1720 | RETURN_FAIL("tcgetattr() failed"); |
e33ab9aa ML |
1721 | |
1722 | if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0) | |
c33efc48 | 1723 | RETURN_FAIL("TIOCMGET ioctl failed"); |
40978c2b ML |
1724 | |
1725 | #ifdef USE_TERMIOX | |
bd791fe1 | 1726 | int ret = get_flow(port->fd, data); |
68ec29db ML |
1727 | |
1728 | if (ret == SP_ERR_FAIL && errno == EINVAL) | |
1729 | data->termiox_supported = 0; | |
1730 | else if (ret < 0) | |
c33efc48 | 1731 | RETURN_CODEVAL(ret); |
68ec29db ML |
1732 | else |
1733 | data->termiox_supported = 1; | |
1734 | #else | |
1735 | data->termiox_supported = 0; | |
40978c2b ML |
1736 | #endif |
1737 | ||
067417af | 1738 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
e33ab9aa | 1739 | if (cfgetispeed(&data->term) == std_baudrates[i].index) { |
067417af ML |
1740 | config->baudrate = std_baudrates[i].value; |
1741 | break; | |
1742 | } | |
1743 | } | |
1744 | ||
31b3a8f5 MH |
1745 | if (i == NUM_STD_BAUDRATES) { |
1746 | #ifdef __APPLE__ | |
1747 | config->baudrate = (int)data->term.c_ispeed; | |
5cea279a | 1748 | #elif defined(USE_TERMIOS_SPEED) |
7a6d2196 | 1749 | TRY(get_baudrate(port->fd, &config->baudrate)); |
31b3a8f5 | 1750 | #else |
067417af | 1751 | config->baudrate = -1; |
31b3a8f5 MH |
1752 | #endif |
1753 | } | |
067417af | 1754 | |
e33ab9aa | 1755 | switch (data->term.c_cflag & CSIZE) { |
067417af ML |
1756 | case CS8: |
1757 | config->bits = 8; | |
1758 | break; | |
1759 | case CS7: | |
1760 | config->bits = 7; | |
1761 | break; | |
1762 | case CS6: | |
1763 | config->bits = 6; | |
1764 | break; | |
1765 | case CS5: | |
1766 | config->bits = 5; | |
1767 | break; | |
1768 | default: | |
1769 | config->bits = -1; | |
1770 | } | |
1771 | ||
e33ab9aa | 1772 | if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR)) |
067417af | 1773 | config->parity = SP_PARITY_NONE; |
e33ab9aa | 1774 | else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR)) |
067417af | 1775 | config->parity = -1; |
c3e05092 | 1776 | #ifdef CMSPAR |
e432ce60 ML |
1777 | else if (data->term.c_cflag & CMSPAR) |
1778 | config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_MARK : SP_PARITY_SPACE; | |
c3e05092 | 1779 | #endif |
067417af | 1780 | else |
e33ab9aa | 1781 | config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN; |
067417af | 1782 | |
e33ab9aa | 1783 | config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1; |
067417af | 1784 | |
e33ab9aa | 1785 | if (data->term.c_cflag & CRTSCTS) { |
067417af ML |
1786 | config->rts = SP_RTS_FLOW_CONTROL; |
1787 | config->cts = SP_CTS_FLOW_CONTROL; | |
1788 | } else { | |
bd791fe1 | 1789 | if (data->termiox_supported && data->rts_flow) |
40978c2b ML |
1790 | config->rts = SP_RTS_FLOW_CONTROL; |
1791 | else | |
1792 | config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF; | |
1793 | ||
bd791fe1 | 1794 | config->cts = (data->termiox_supported && data->cts_flow) ? |
68ec29db | 1795 | SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE; |
067417af ML |
1796 | } |
1797 | ||
bd791fe1 | 1798 | if (data->termiox_supported && data->dtr_flow) |
40978c2b ML |
1799 | config->dtr = SP_DTR_FLOW_CONTROL; |
1800 | else | |
1801 | config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF; | |
1802 | ||
bd791fe1 | 1803 | config->dsr = (data->termiox_supported && data->dsr_flow) ? |
68ec29db | 1804 | SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE; |
705bdc69 | 1805 | |
e29b93a5 ML |
1806 | if (data->term.c_iflag & IXOFF) { |
1807 | if (data->term.c_iflag & IXON) | |
1808 | config->xon_xoff = SP_XONXOFF_INOUT; | |
1809 | else | |
1810 | config->xon_xoff = SP_XONXOFF_IN; | |
1811 | } else { | |
1812 | if (data->term.c_iflag & IXON) | |
1813 | config->xon_xoff = SP_XONXOFF_OUT; | |
1814 | else | |
1815 | config->xon_xoff = SP_XONXOFF_DISABLED; | |
1816 | } | |
067417af ML |
1817 | #endif |
1818 | ||
c33efc48 | 1819 | RETURN_OK(); |
067417af ML |
1820 | } |
1821 | ||
7a6d2196 | 1822 | static enum sp_return set_config(struct sp_port *port, struct port_data *data, |
eb6ed20f | 1823 | const struct sp_port_config *config) |
18fc2dd1 | 1824 | { |
e33ab9aa | 1825 | unsigned int i; |
31b3a8f5 MH |
1826 | #ifdef __APPLE__ |
1827 | BAUD_TYPE baud_nonstd; | |
1828 | ||
1829 | baud_nonstd = B0; | |
1830 | #endif | |
5cea279a | 1831 | #ifdef USE_TERMIOS_SPEED |
7a6d2196 ML |
1832 | int baud_nonstd = 0; |
1833 | #endif | |
18fc2dd1 | 1834 | |
c33efc48 ML |
1835 | TRACE("%p, %p, %p", port, data, config); |
1836 | ||
7890cef6 | 1837 | DEBUG_FMT("Setting configuration for port %s", port->name); |
ea667be7 | 1838 | |
e33ab9aa | 1839 | #ifdef _WIN32 |
6bd6a8b5 ML |
1840 | |
1841 | TRY(await_write_completion(port)); | |
1842 | ||
eac329d2 | 1843 | if (config->baudrate >= 0) { |
e33ab9aa ML |
1844 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
1845 | if (config->baudrate == std_baudrates[i].value) { | |
1846 | data->dcb.BaudRate = std_baudrates[i].index; | |
1847 | break; | |
1848 | } | |
1849 | } | |
18fc2dd1 | 1850 | |
e33ab9aa ML |
1851 | if (i == NUM_STD_BAUDRATES) |
1852 | data->dcb.BaudRate = config->baudrate; | |
39df7833 ML |
1853 | |
1854 | /* Allocate write buffer for 50ms of data at baud rate. */ | |
1855 | port->write_buf_size = max(config->baudrate / (8 * 20), 1); | |
1856 | port->write_buf = realloc(port->write_buf, | |
1857 | port->write_buf_size); | |
1858 | ||
1859 | if (!port->write_buf) | |
1860 | RETURN_ERROR(SP_ERR_MEM, "Allocating write buffer failed"); | |
e33ab9aa | 1861 | } |
18fc2dd1 | 1862 | |
e33ab9aa ML |
1863 | if (config->bits >= 0) |
1864 | data->dcb.ByteSize = config->bits; | |
1865 | ||
1866 | if (config->parity >= 0) { | |
1867 | switch (config->parity) { | |
e33ab9aa ML |
1868 | case SP_PARITY_NONE: |
1869 | data->dcb.Parity = NOPARITY; | |
1870 | break; | |
e432ce60 ML |
1871 | case SP_PARITY_ODD: |
1872 | data->dcb.Parity = ODDPARITY; | |
1873 | break; | |
e33ab9aa ML |
1874 | case SP_PARITY_EVEN: |
1875 | data->dcb.Parity = EVENPARITY; | |
1876 | break; | |
e432ce60 ML |
1877 | case SP_PARITY_MARK: |
1878 | data->dcb.Parity = MARKPARITY; | |
1879 | break; | |
1880 | case SP_PARITY_SPACE: | |
1881 | data->dcb.Parity = SPACEPARITY; | |
e33ab9aa ML |
1882 | break; |
1883 | default: | |
c33efc48 | 1884 | RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting"); |
e33ab9aa | 1885 | } |
18fc2dd1 ML |
1886 | } |
1887 | ||
e33ab9aa ML |
1888 | if (config->stopbits >= 0) { |
1889 | switch (config->stopbits) { | |
1890 | /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */ | |
1891 | case 1: | |
1892 | data->dcb.StopBits = ONESTOPBIT; | |
1893 | break; | |
1894 | case 2: | |
1895 | data->dcb.StopBits = TWOSTOPBITS; | |
1896 | break; | |
1897 | default: | |
c33efc48 | 1898 | RETURN_ERROR(SP_ERR_ARG, "Invalid stop bit setting"); |
e33ab9aa ML |
1899 | } |
1900 | } | |
1901 | ||
1902 | if (config->rts >= 0) { | |
1903 | switch (config->rts) { | |
1904 | case SP_RTS_OFF: | |
1905 | data->dcb.fRtsControl = RTS_CONTROL_DISABLE; | |
1906 | break; | |
1907 | case SP_RTS_ON: | |
1908 | data->dcb.fRtsControl = RTS_CONTROL_ENABLE; | |
1909 | break; | |
1910 | case SP_RTS_FLOW_CONTROL: | |
1911 | data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; | |
1912 | break; | |
1913 | default: | |
c33efc48 | 1914 | RETURN_ERROR(SP_ERR_ARG, "Invalid RTS setting"); |
e33ab9aa ML |
1915 | } |
1916 | } | |
1917 | ||
1918 | if (config->cts >= 0) { | |
1919 | switch (config->cts) { | |
1920 | case SP_CTS_IGNORE: | |
1921 | data->dcb.fOutxCtsFlow = FALSE; | |
1922 | break; | |
1923 | case SP_CTS_FLOW_CONTROL: | |
1924 | data->dcb.fOutxCtsFlow = TRUE; | |
1925 | break; | |
1926 | default: | |
c33efc48 | 1927 | RETURN_ERROR(SP_ERR_ARG, "Invalid CTS setting"); |
e33ab9aa ML |
1928 | } |
1929 | } | |
1930 | ||
1931 | if (config->dtr >= 0) { | |
1932 | switch (config->dtr) { | |
1933 | case SP_DTR_OFF: | |
1934 | data->dcb.fDtrControl = DTR_CONTROL_DISABLE; | |
1935 | break; | |
1936 | case SP_DTR_ON: | |
1937 | data->dcb.fDtrControl = DTR_CONTROL_ENABLE; | |
1938 | break; | |
1939 | case SP_DTR_FLOW_CONTROL: | |
1940 | data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; | |
1941 | break; | |
1942 | default: | |
c33efc48 | 1943 | RETURN_ERROR(SP_ERR_ARG, "Invalid DTR setting"); |
e33ab9aa ML |
1944 | } |
1945 | } | |
1946 | ||
1947 | if (config->dsr >= 0) { | |
1948 | switch (config->dsr) { | |
1949 | case SP_DSR_IGNORE: | |
1950 | data->dcb.fOutxDsrFlow = FALSE; | |
1951 | break; | |
1952 | case SP_DSR_FLOW_CONTROL: | |
1953 | data->dcb.fOutxDsrFlow = TRUE; | |
1954 | break; | |
1955 | default: | |
c33efc48 | 1956 | RETURN_ERROR(SP_ERR_ARG, "Invalid DSR setting"); |
e33ab9aa | 1957 | } |
18fc2dd1 ML |
1958 | } |
1959 | ||
e33ab9aa ML |
1960 | if (config->xon_xoff >= 0) { |
1961 | switch (config->xon_xoff) { | |
1962 | case SP_XONXOFF_DISABLED: | |
1963 | data->dcb.fInX = FALSE; | |
1964 | data->dcb.fOutX = FALSE; | |
1965 | break; | |
1966 | case SP_XONXOFF_IN: | |
1967 | data->dcb.fInX = TRUE; | |
1968 | data->dcb.fOutX = FALSE; | |
1969 | break; | |
1970 | case SP_XONXOFF_OUT: | |
1971 | data->dcb.fInX = FALSE; | |
1972 | data->dcb.fOutX = TRUE; | |
1973 | break; | |
1974 | case SP_XONXOFF_INOUT: | |
1975 | data->dcb.fInX = TRUE; | |
1976 | data->dcb.fOutX = TRUE; | |
1977 | break; | |
1978 | default: | |
c33efc48 | 1979 | RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting"); |
e33ab9aa ML |
1980 | } |
1981 | } | |
1982 | ||
1983 | if (!SetCommState(port->hdl, &data->dcb)) | |
c33efc48 | 1984 | RETURN_FAIL("SetCommState() failed"); |
e33ab9aa | 1985 | |
31b3a8f5 | 1986 | #else /* !_WIN32 */ |
e33ab9aa | 1987 | |
7a6d2196 ML |
1988 | int controlbits; |
1989 | ||
eac329d2 | 1990 | if (config->baudrate >= 0) { |
e33ab9aa ML |
1991 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
1992 | if (config->baudrate == std_baudrates[i].value) { | |
1993 | if (cfsetospeed(&data->term, std_baudrates[i].index) < 0) | |
c33efc48 | 1994 | RETURN_FAIL("cfsetospeed() failed"); |
e33ab9aa ML |
1995 | |
1996 | if (cfsetispeed(&data->term, std_baudrates[i].index) < 0) | |
c33efc48 | 1997 | RETURN_FAIL("cfsetispeed() failed"); |
e33ab9aa ML |
1998 | break; |
1999 | } | |
2000 | } | |
2001 | ||
31b3a8f5 MH |
2002 | /* Non-standard baud rate */ |
2003 | if (i == NUM_STD_BAUDRATES) { | |
2004 | #ifdef __APPLE__ | |
24abdb68 | 2005 | /* Set "dummy" baud rate. */ |
31b3a8f5 | 2006 | if (cfsetspeed(&data->term, B9600) < 0) |
c33efc48 | 2007 | RETURN_FAIL("cfsetspeed() failed"); |
31b3a8f5 | 2008 | baud_nonstd = config->baudrate; |
5cea279a | 2009 | #elif defined(USE_TERMIOS_SPEED) |
7a6d2196 | 2010 | baud_nonstd = 1; |
31b3a8f5 | 2011 | #else |
c33efc48 | 2012 | RETURN_ERROR(SP_ERR_SUPP, "Non-standard baudrate not supported"); |
31b3a8f5 MH |
2013 | #endif |
2014 | } | |
e33ab9aa ML |
2015 | } |
2016 | ||
2017 | if (config->bits >= 0) { | |
2018 | data->term.c_cflag &= ~CSIZE; | |
2019 | switch (config->bits) { | |
2020 | case 8: | |
2021 | data->term.c_cflag |= CS8; | |
2022 | break; | |
2023 | case 7: | |
2024 | data->term.c_cflag |= CS7; | |
2025 | break; | |
2026 | case 6: | |
2027 | data->term.c_cflag |= CS6; | |
2028 | break; | |
23922313 UH |
2029 | case 5: |
2030 | data->term.c_cflag |= CS5; | |
2031 | break; | |
e33ab9aa | 2032 | default: |
c33efc48 | 2033 | RETURN_ERROR(SP_ERR_ARG, "Invalid data bits setting"); |
e33ab9aa ML |
2034 | } |
2035 | } | |
2036 | ||
2037 | if (config->parity >= 0) { | |
2038 | data->term.c_iflag &= ~IGNPAR; | |
c3e05092 UH |
2039 | data->term.c_cflag &= ~(PARENB | PARODD); |
2040 | #ifdef CMSPAR | |
2041 | data->term.c_cflag &= ~CMSPAR; | |
2042 | #endif | |
e33ab9aa ML |
2043 | switch (config->parity) { |
2044 | case SP_PARITY_NONE: | |
2045 | data->term.c_iflag |= IGNPAR; | |
2046 | break; | |
2047 | case SP_PARITY_EVEN: | |
2048 | data->term.c_cflag |= PARENB; | |
2049 | break; | |
2050 | case SP_PARITY_ODD: | |
2051 | data->term.c_cflag |= PARENB | PARODD; | |
2052 | break; | |
afb518f0 | 2053 | #ifdef CMSPAR |
e432ce60 | 2054 | case SP_PARITY_MARK: |
c3e05092 | 2055 | data->term.c_cflag |= PARENB | PARODD; |
c3e05092 | 2056 | data->term.c_cflag |= CMSPAR; |
e432ce60 ML |
2057 | break; |
2058 | case SP_PARITY_SPACE: | |
c3e05092 | 2059 | data->term.c_cflag |= PARENB; |
c3e05092 | 2060 | data->term.c_cflag |= CMSPAR; |
e432ce60 | 2061 | break; |
afb518f0 ML |
2062 | #else |
2063 | case SP_PARITY_MARK: | |
2064 | case SP_PARITY_SPACE: | |
2065 | RETURN_ERROR(SP_ERR_SUPP, "Mark/space parity not supported"); | |
2066 | #endif | |
e33ab9aa | 2067 | default: |
c33efc48 | 2068 | RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting"); |
e33ab9aa ML |
2069 | } |
2070 | } | |
2071 | ||
2072 | if (config->stopbits >= 0) { | |
2073 | data->term.c_cflag &= ~CSTOPB; | |
2074 | switch (config->stopbits) { | |
2075 | case 1: | |
2076 | data->term.c_cflag &= ~CSTOPB; | |
2077 | break; | |
2078 | case 2: | |
2079 | data->term.c_cflag |= CSTOPB; | |
2080 | break; | |
2081 | default: | |
c33efc48 | 2082 | RETURN_ERROR(SP_ERR_ARG, "Invalid stop bits setting"); |
e33ab9aa ML |
2083 | } |
2084 | } | |
2085 | ||
eac329d2 | 2086 | if (config->rts >= 0 || config->cts >= 0) { |
68ec29db | 2087 | if (data->termiox_supported) { |
bd791fe1 | 2088 | data->rts_flow = data->cts_flow = 0; |
68ec29db ML |
2089 | switch (config->rts) { |
2090 | case SP_RTS_OFF: | |
2091 | case SP_RTS_ON: | |
2092 | controlbits = TIOCM_RTS; | |
2093 | if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0) | |
c33efc48 | 2094 | RETURN_FAIL("Setting RTS signal level failed"); |
68ec29db ML |
2095 | break; |
2096 | case SP_RTS_FLOW_CONTROL: | |
bd791fe1 | 2097 | data->rts_flow = 1; |
68ec29db ML |
2098 | break; |
2099 | default: | |
2100 | break; | |
e33ab9aa | 2101 | } |
68ec29db | 2102 | if (config->cts == SP_CTS_FLOW_CONTROL) |
bd791fe1 | 2103 | data->cts_flow = 1; |
e33ab9aa | 2104 | |
bd791fe1 | 2105 | if (data->rts_flow && data->cts_flow) |
e33ab9aa | 2106 | data->term.c_iflag |= CRTSCTS; |
68ec29db ML |
2107 | else |
2108 | data->term.c_iflag &= ~CRTSCTS; | |
2109 | } else { | |
2110 | /* Asymmetric use of RTS/CTS not supported. */ | |
2111 | if (data->term.c_iflag & CRTSCTS) { | |
2112 | /* Flow control can only be disabled for both RTS & CTS together. */ | |
2113 | if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) { | |
2114 | if (config->cts != SP_CTS_IGNORE) | |
c33efc48 | 2115 | RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together"); |
68ec29db ML |
2116 | } |
2117 | if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) { | |
2118 | if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL) | |
c33efc48 | 2119 | RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together"); |
68ec29db | 2120 | } |
e33ab9aa | 2121 | } else { |
68ec29db ML |
2122 | /* Flow control can only be enabled for both RTS & CTS together. */ |
2123 | if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) || | |
2124 | ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL))) | |
c33efc48 | 2125 | RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be enabled together"); |
68ec29db ML |
2126 | } |
2127 | ||
2128 | if (config->rts >= 0) { | |
2129 | if (config->rts == SP_RTS_FLOW_CONTROL) { | |
2130 | data->term.c_iflag |= CRTSCTS; | |
2131 | } else { | |
2132 | controlbits = TIOCM_RTS; | |
2133 | if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, | |
2134 | &controlbits) < 0) | |
c33efc48 | 2135 | RETURN_FAIL("Setting RTS signal level failed"); |
68ec29db | 2136 | } |
e33ab9aa ML |
2137 | } |
2138 | } | |
2139 | } | |
2140 | ||
eac329d2 | 2141 | if (config->dtr >= 0 || config->dsr >= 0) { |
68ec29db | 2142 | if (data->termiox_supported) { |
bd791fe1 | 2143 | data->dtr_flow = data->dsr_flow = 0; |
68ec29db ML |
2144 | switch (config->dtr) { |
2145 | case SP_DTR_OFF: | |
2146 | case SP_DTR_ON: | |
2147 | controlbits = TIOCM_DTR; | |
2148 | if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0) | |
c33efc48 | 2149 | RETURN_FAIL("Setting DTR signal level failed"); |
68ec29db ML |
2150 | break; |
2151 | case SP_DTR_FLOW_CONTROL: | |
bd791fe1 | 2152 | data->dtr_flow = 1; |
68ec29db ML |
2153 | break; |
2154 | default: | |
2155 | break; | |
2156 | } | |
2157 | if (config->dsr == SP_DSR_FLOW_CONTROL) | |
bd791fe1 | 2158 | data->dsr_flow = 1; |
68ec29db ML |
2159 | } else { |
2160 | /* DTR/DSR flow control not supported. */ | |
2161 | if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL) | |
c33efc48 | 2162 | RETURN_ERROR(SP_ERR_SUPP, "DTR/DSR flow control not supported"); |
e33ab9aa | 2163 | |
68ec29db ML |
2164 | if (config->dtr >= 0) { |
2165 | controlbits = TIOCM_DTR; | |
2166 | if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, | |
2167 | &controlbits) < 0) | |
c33efc48 | 2168 | RETURN_FAIL("Setting DTR signal level failed"); |
68ec29db | 2169 | } |
e33ab9aa ML |
2170 | } |
2171 | } | |
2172 | ||
2173 | if (config->xon_xoff >= 0) { | |
2174 | data->term.c_iflag &= ~(IXON | IXOFF | IXANY); | |
2175 | switch (config->xon_xoff) { | |
2176 | case SP_XONXOFF_DISABLED: | |
2177 | break; | |
2178 | case SP_XONXOFF_IN: | |
2179 | data->term.c_iflag |= IXOFF; | |
2180 | break; | |
2181 | case SP_XONXOFF_OUT: | |
2182 | data->term.c_iflag |= IXON | IXANY; | |
2183 | break; | |
2184 | case SP_XONXOFF_INOUT: | |
2185 | data->term.c_iflag |= IXON | IXOFF | IXANY; | |
2186 | break; | |
2187 | default: | |
c33efc48 | 2188 | RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting"); |
e33ab9aa ML |
2189 | } |
2190 | } | |
2191 | ||
3f5c06d0 | 2192 | if (tcsetattr(port->fd, TCSANOW, &data->term) < 0) |
c33efc48 | 2193 | RETURN_FAIL("tcsetattr() failed"); |
31b3a8f5 MH |
2194 | |
2195 | #ifdef __APPLE__ | |
2196 | if (baud_nonstd != B0) { | |
2197 | if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1) | |
c33efc48 | 2198 | RETURN_FAIL("IOSSIOSPEED ioctl failed"); |
dc422c04 UH |
2199 | /* |
2200 | * Set baud rates in data->term to correct, but incompatible | |
2201 | * with tcsetattr() value, same as delivered by tcgetattr(). | |
2202 | */ | |
31b3a8f5 | 2203 | if (cfsetspeed(&data->term, baud_nonstd) < 0) |
c33efc48 | 2204 | RETURN_FAIL("cfsetspeed() failed"); |
31b3a8f5 | 2205 | } |
7a6d2196 | 2206 | #elif defined(__linux__) |
5cea279a | 2207 | #ifdef USE_TERMIOS_SPEED |
7a6d2196 ML |
2208 | if (baud_nonstd) |
2209 | TRY(set_baudrate(port->fd, config->baudrate)); | |
5cea279a | 2210 | #endif |
40978c2b | 2211 | #ifdef USE_TERMIOX |
68ec29db | 2212 | if (data->termiox_supported) |
bd791fe1 | 2213 | TRY(set_flow(port->fd, data)); |
40978c2b | 2214 | #endif |
7a6d2196 | 2215 | #endif |
31b3a8f5 MH |
2216 | |
2217 | #endif /* !_WIN32 */ | |
e33ab9aa | 2218 | |
c33efc48 | 2219 | RETURN_OK(); |
e33ab9aa ML |
2220 | } |
2221 | ||
970f279a | 2222 | SP_API enum sp_return sp_new_config(struct sp_port_config **config_ptr) |
9b1502ef | 2223 | { |
59131d60 | 2224 | struct sp_port_config *config; |
9b1502ef | 2225 | |
00d8c56d UH |
2226 | TRACE("%p", config_ptr); |
2227 | ||
9b1502ef ML |
2228 | if (!config_ptr) |
2229 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); | |
2230 | ||
59131d60 ML |
2231 | *config_ptr = NULL; |
2232 | ||
2233 | if (!(config = malloc(sizeof(struct sp_port_config)))) | |
dc422c04 | 2234 | RETURN_ERROR(SP_ERR_MEM, "Config malloc failed"); |
9b1502ef | 2235 | |
59131d60 ML |
2236 | config->baudrate = -1; |
2237 | config->bits = -1; | |
2238 | config->parity = -1; | |
2239 | config->stopbits = -1; | |
2240 | config->rts = -1; | |
2241 | config->cts = -1; | |
2242 | config->dtr = -1; | |
2243 | config->dsr = -1; | |
2244 | ||
2245 | *config_ptr = config; | |
2246 | ||
9b1502ef ML |
2247 | RETURN_OK(); |
2248 | } | |
2249 | ||
970f279a | 2250 | SP_API void sp_free_config(struct sp_port_config *config) |
9b1502ef ML |
2251 | { |
2252 | TRACE("%p", config); | |
2253 | ||
2254 | if (!config) | |
2255 | DEBUG("Null config"); | |
2256 | else | |
2257 | free(config); | |
2258 | ||
2259 | RETURN(); | |
2260 | } | |
2261 | ||
970f279a AJ |
2262 | SP_API enum sp_return sp_get_config(struct sp_port *port, |
2263 | struct sp_port_config *config) | |
70cd37de ML |
2264 | { |
2265 | struct port_data data; | |
2266 | ||
2267 | TRACE("%p, %p", port, config); | |
2268 | ||
2269 | CHECK_OPEN_PORT(); | |
2270 | ||
2271 | if (!config) | |
9b1502ef | 2272 | RETURN_ERROR(SP_ERR_ARG, "Null config"); |
70cd37de ML |
2273 | |
2274 | TRY(get_config(port, &data, config)); | |
2275 | ||
2276 | RETURN_OK(); | |
2277 | } | |
2278 | ||
970f279a AJ |
2279 | SP_API enum sp_return sp_set_config(struct sp_port *port, |
2280 | const struct sp_port_config *config) | |
e33ab9aa | 2281 | { |
8f189c4c | 2282 | struct port_data data; |
e33ab9aa ML |
2283 | struct sp_port_config prev_config; |
2284 | ||
c33efc48 ML |
2285 | TRACE("%p, %p", port, config); |
2286 | ||
dec10e31 | 2287 | CHECK_OPEN_PORT(); |
823690ae ML |
2288 | |
2289 | if (!config) | |
c33efc48 | 2290 | RETURN_ERROR(SP_ERR_ARG, "Null config"); |
823690ae | 2291 | |
e33ab9aa ML |
2292 | TRY(get_config(port, &data, &prev_config)); |
2293 | TRY(set_config(port, &data, config)); | |
74510d4b | 2294 | |
c33efc48 | 2295 | RETURN_OK(); |
74510d4b ML |
2296 | } |
2297 | ||
9b1502ef | 2298 | #define CREATE_ACCESSORS(x, type) \ |
970f279a | 2299 | SP_API enum sp_return sp_set_##x(struct sp_port *port, type x) { \ |
8f189c4c | 2300 | struct port_data data; \ |
e33ab9aa | 2301 | struct sp_port_config config; \ |
c33efc48 | 2302 | TRACE("%p, %d", port, x); \ |
dec10e31 | 2303 | CHECK_OPEN_PORT(); \ |
e33ab9aa ML |
2304 | TRY(get_config(port, &data, &config)); \ |
2305 | config.x = x; \ | |
2306 | TRY(set_config(port, &data, &config)); \ | |
c33efc48 | 2307 | RETURN_OK(); \ |
9b1502ef | 2308 | } \ |
970f279a AJ |
2309 | SP_API enum sp_return sp_get_config_##x(const struct sp_port_config *config, \ |
2310 | type *x) { \ | |
00d8c56d | 2311 | TRACE("%p, %p", config, x); \ |
6aad5bdd UH |
2312 | if (!x) \ |
2313 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); \ | |
9b1502ef ML |
2314 | if (!config) \ |
2315 | RETURN_ERROR(SP_ERR_ARG, "Null config"); \ | |
2316 | *x = config->x; \ | |
2317 | RETURN_OK(); \ | |
2318 | } \ | |
970f279a AJ |
2319 | SP_API enum sp_return sp_set_config_##x(struct sp_port_config *config, \ |
2320 | type x) { \ | |
9b1502ef ML |
2321 | TRACE("%p, %d", config, x); \ |
2322 | if (!config) \ | |
2323 | RETURN_ERROR(SP_ERR_ARG, "Null config"); \ | |
2324 | config->x = x; \ | |
2325 | RETURN_OK(); \ | |
9069c2fb ML |
2326 | } |
2327 | ||
9b1502ef ML |
2328 | CREATE_ACCESSORS(baudrate, int) |
2329 | CREATE_ACCESSORS(bits, int) | |
2330 | CREATE_ACCESSORS(parity, enum sp_parity) | |
2331 | CREATE_ACCESSORS(stopbits, int) | |
2332 | CREATE_ACCESSORS(rts, enum sp_rts) | |
2333 | CREATE_ACCESSORS(cts, enum sp_cts) | |
2334 | CREATE_ACCESSORS(dtr, enum sp_dtr) | |
2335 | CREATE_ACCESSORS(dsr, enum sp_dsr) | |
2336 | CREATE_ACCESSORS(xon_xoff, enum sp_xonxoff) | |
2337 | ||
970f279a AJ |
2338 | SP_API enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, |
2339 | enum sp_flowcontrol flowcontrol) | |
e33ab9aa | 2340 | { |
9b1502ef ML |
2341 | if (!config) |
2342 | RETURN_ERROR(SP_ERR_ARG, "Null configuration"); | |
dec10e31 ML |
2343 | |
2344 | if (flowcontrol > SP_FLOWCONTROL_DTRDSR) | |
2345 | RETURN_ERROR(SP_ERR_ARG, "Invalid flow control setting"); | |
823690ae | 2346 | |
e33ab9aa | 2347 | if (flowcontrol == SP_FLOWCONTROL_XONXOFF) |
9b1502ef | 2348 | config->xon_xoff = SP_XONXOFF_INOUT; |
e33ab9aa | 2349 | else |
9b1502ef | 2350 | config->xon_xoff = SP_XONXOFF_DISABLED; |
e33ab9aa ML |
2351 | |
2352 | if (flowcontrol == SP_FLOWCONTROL_RTSCTS) { | |
9b1502ef ML |
2353 | config->rts = SP_RTS_FLOW_CONTROL; |
2354 | config->cts = SP_CTS_FLOW_CONTROL; | |
e33ab9aa | 2355 | } else { |
9b1502ef ML |
2356 | if (config->rts == SP_RTS_FLOW_CONTROL) |
2357 | config->rts = SP_RTS_ON; | |
2358 | config->cts = SP_CTS_IGNORE; | |
e33ab9aa ML |
2359 | } |
2360 | ||
2361 | if (flowcontrol == SP_FLOWCONTROL_DTRDSR) { | |
9b1502ef ML |
2362 | config->dtr = SP_DTR_FLOW_CONTROL; |
2363 | config->dsr = SP_DSR_FLOW_CONTROL; | |
e33ab9aa | 2364 | } else { |
9b1502ef ML |
2365 | if (config->dtr == SP_DTR_FLOW_CONTROL) |
2366 | config->dtr = SP_DTR_ON; | |
2367 | config->dsr = SP_DSR_IGNORE; | |
e33ab9aa ML |
2368 | } |
2369 | ||
9b1502ef ML |
2370 | RETURN_OK(); |
2371 | } | |
2372 | ||
970f279a AJ |
2373 | SP_API enum sp_return sp_set_flowcontrol(struct sp_port *port, |
2374 | enum sp_flowcontrol flowcontrol) | |
9b1502ef ML |
2375 | { |
2376 | struct port_data data; | |
2377 | struct sp_port_config config; | |
2378 | ||
2379 | TRACE("%p, %d", port, flowcontrol); | |
2380 | ||
2381 | CHECK_OPEN_PORT(); | |
2382 | ||
2383 | TRY(get_config(port, &data, &config)); | |
2384 | ||
2385 | TRY(sp_set_config_flowcontrol(&config, flowcontrol)); | |
2386 | ||
e33ab9aa ML |
2387 | TRY(set_config(port, &data, &config)); |
2388 | ||
c33efc48 | 2389 | RETURN_OK(); |
e33ab9aa ML |
2390 | } |
2391 | ||
970f279a AJ |
2392 | SP_API enum sp_return sp_get_signals(struct sp_port *port, |
2393 | enum sp_signal *signals) | |
8cf7c697 | 2394 | { |
c33efc48 ML |
2395 | TRACE("%p, %p", port, signals); |
2396 | ||
dec10e31 | 2397 | CHECK_OPEN_PORT(); |
8cf7c697 ML |
2398 | |
2399 | if (!signals) | |
c33efc48 | 2400 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); |
8cf7c697 | 2401 | |
7890cef6 | 2402 | DEBUG_FMT("Getting control signals for port %s", port->name); |
ea667be7 | 2403 | |
8cf7c697 ML |
2404 | *signals = 0; |
2405 | #ifdef _WIN32 | |
2406 | DWORD bits; | |
2407 | if (GetCommModemStatus(port->hdl, &bits) == 0) | |
c33efc48 | 2408 | RETURN_FAIL("GetCommModemStatus() failed"); |
8cf7c697 ML |
2409 | if (bits & MS_CTS_ON) |
2410 | *signals |= SP_SIG_CTS; | |
2411 | if (bits & MS_DSR_ON) | |
2412 | *signals |= SP_SIG_DSR; | |
8cf7c697 | 2413 | if (bits & MS_RLSD_ON) |
a6cda1e8 ML |
2414 | *signals |= SP_SIG_DCD; |
2415 | if (bits & MS_RING_ON) | |
8cf7c697 ML |
2416 | *signals |= SP_SIG_RI; |
2417 | #else | |
2418 | int bits; | |
2419 | if (ioctl(port->fd, TIOCMGET, &bits) < 0) | |
c33efc48 | 2420 | RETURN_FAIL("TIOCMGET ioctl failed"); |
8cf7c697 ML |
2421 | if (bits & TIOCM_CTS) |
2422 | *signals |= SP_SIG_CTS; | |
2423 | if (bits & TIOCM_DSR) | |
2424 | *signals |= SP_SIG_DSR; | |
2425 | if (bits & TIOCM_CAR) | |
2426 | *signals |= SP_SIG_DCD; | |
2427 | if (bits & TIOCM_RNG) | |
2428 | *signals |= SP_SIG_RI; | |
2429 | #endif | |
c33efc48 | 2430 | RETURN_OK(); |
8cf7c697 ML |
2431 | } |
2432 | ||
970f279a | 2433 | SP_API enum sp_return sp_start_break(struct sp_port *port) |
90cc3ee6 | 2434 | { |
c33efc48 ML |
2435 | TRACE("%p", port); |
2436 | ||
dec10e31 | 2437 | CHECK_OPEN_PORT(); |
90cc3ee6 ML |
2438 | #ifdef _WIN32 |
2439 | if (SetCommBreak(port->hdl) == 0) | |
c33efc48 | 2440 | RETURN_FAIL("SetCommBreak() failed"); |
90cc3ee6 ML |
2441 | #else |
2442 | if (ioctl(port->fd, TIOCSBRK, 1) < 0) | |
c33efc48 | 2443 | RETURN_FAIL("TIOCSBRK ioctl failed"); |
90cc3ee6 ML |
2444 | #endif |
2445 | ||
c33efc48 | 2446 | RETURN_OK(); |
90cc3ee6 ML |
2447 | } |
2448 | ||
970f279a | 2449 | SP_API enum sp_return sp_end_break(struct sp_port *port) |
90cc3ee6 | 2450 | { |
c33efc48 ML |
2451 | TRACE("%p", port); |
2452 | ||
dec10e31 | 2453 | CHECK_OPEN_PORT(); |
90cc3ee6 ML |
2454 | #ifdef _WIN32 |
2455 | if (ClearCommBreak(port->hdl) == 0) | |
c33efc48 | 2456 | RETURN_FAIL("ClearCommBreak() failed"); |
90cc3ee6 ML |
2457 | #else |
2458 | if (ioctl(port->fd, TIOCCBRK, 1) < 0) | |
c33efc48 | 2459 | RETURN_FAIL("TIOCCBRK ioctl failed"); |
90cc3ee6 ML |
2460 | #endif |
2461 | ||
c33efc48 | 2462 | RETURN_OK(); |
90cc3ee6 ML |
2463 | } |
2464 | ||
970f279a | 2465 | SP_API int sp_last_error_code(void) |
74510d4b | 2466 | { |
7890cef6 | 2467 | TRACE_VOID(); |
74510d4b | 2468 | #ifdef _WIN32 |
9caa2e86 | 2469 | RETURN_INT(GetLastError()); |
74510d4b | 2470 | #else |
9caa2e86 | 2471 | RETURN_INT(errno); |
74510d4b ML |
2472 | #endif |
2473 | } | |
2474 | ||
970f279a | 2475 | SP_API char *sp_last_error_message(void) |
74510d4b | 2476 | { |
7890cef6 | 2477 | TRACE_VOID(); |
c33efc48 | 2478 | |
74510d4b | 2479 | #ifdef _WIN32 |
9a0c4bbd | 2480 | TCHAR *message; |
74510d4b ML |
2481 | DWORD error = GetLastError(); |
2482 | ||
9a0c4bbd | 2483 | DWORD length = FormatMessage( |
74510d4b ML |
2484 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
2485 | FORMAT_MESSAGE_FROM_SYSTEM | | |
2486 | FORMAT_MESSAGE_IGNORE_INSERTS, | |
2487 | NULL, | |
2488 | error, | |
2489 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
2490 | (LPTSTR) &message, | |
2491 | 0, NULL ); | |
2492 | ||
9a0c4bbd ML |
2493 | if (length >= 2 && message[length - 2] == '\r') |
2494 | message[length - 2] = '\0'; | |
2495 | ||
9caa2e86 | 2496 | RETURN_STRING(message); |
74510d4b | 2497 | #else |
9caa2e86 | 2498 | RETURN_STRING(strerror(errno)); |
74510d4b ML |
2499 | #endif |
2500 | } | |
2501 | ||
970f279a | 2502 | SP_API void sp_free_error_message(char *message) |
74510d4b | 2503 | { |
c33efc48 ML |
2504 | TRACE("%s", message); |
2505 | ||
74510d4b ML |
2506 | #ifdef _WIN32 |
2507 | LocalFree(message); | |
64eec30d ML |
2508 | #else |
2509 | (void)message; | |
74510d4b | 2510 | #endif |
c33efc48 ML |
2511 | |
2512 | RETURN(); | |
74510d4b | 2513 | } |
863b35e6 | 2514 | |
970f279a | 2515 | SP_API void sp_set_debug_handler(void (*handler)(const char *format, ...)) |
863b35e6 | 2516 | { |
c33efc48 ML |
2517 | TRACE("%p", handler); |
2518 | ||
863b35e6 | 2519 | sp_debug_handler = handler; |
c33efc48 ML |
2520 | |
2521 | RETURN(); | |
863b35e6 ML |
2522 | } |
2523 | ||
970f279a | 2524 | SP_API void sp_default_debug_handler(const char *format, ...) |
863b35e6 ML |
2525 | { |
2526 | va_list args; | |
2527 | va_start(args, format); | |
2528 | if (getenv("LIBSERIALPORT_DEBUG")) { | |
dd7742fb | 2529 | fputs("sp: ", stderr); |
863b35e6 ML |
2530 | vfprintf(stderr, format, args); |
2531 | } | |
2532 | va_end(args); | |
2533 | } | |
524b0e14 | 2534 | |
970f279a | 2535 | SP_API int sp_get_major_package_version(void) |
524b0e14 UH |
2536 | { |
2537 | return SP_PACKAGE_VERSION_MAJOR; | |
2538 | } | |
2539 | ||
970f279a | 2540 | SP_API int sp_get_minor_package_version(void) |
524b0e14 UH |
2541 | { |
2542 | return SP_PACKAGE_VERSION_MINOR; | |
2543 | } | |
2544 | ||
970f279a | 2545 | SP_API int sp_get_micro_package_version(void) |
524b0e14 UH |
2546 | { |
2547 | return SP_PACKAGE_VERSION_MICRO; | |
2548 | } | |
2549 | ||
970f279a | 2550 | SP_API const char *sp_get_package_version_string(void) |
524b0e14 UH |
2551 | { |
2552 | return SP_PACKAGE_VERSION_STRING; | |
2553 | } | |
2554 | ||
970f279a | 2555 | SP_API int sp_get_current_lib_version(void) |
524b0e14 UH |
2556 | { |
2557 | return SP_LIB_VERSION_CURRENT; | |
2558 | } | |
2559 | ||
970f279a | 2560 | SP_API int sp_get_revision_lib_version(void) |
524b0e14 UH |
2561 | { |
2562 | return SP_LIB_VERSION_REVISION; | |
2563 | } | |
2564 | ||
970f279a | 2565 | SP_API int sp_get_age_lib_version(void) |
524b0e14 UH |
2566 | { |
2567 | return SP_LIB_VERSION_AGE; | |
2568 | } | |
2569 | ||
970f279a | 2570 | SP_API const char *sp_get_lib_version_string(void) |
524b0e14 UH |
2571 | { |
2572 | return SP_LIB_VERSION_STRING; | |
2573 | } | |
2574 | ||
2575 | /** @} */ |