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