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