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