]>
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, | |
1169 | sizeof(enum sp_event) * (event_set->count + 1)))) | |
dc422c04 | 1170 | RETURN_ERROR(SP_ERR_MEM, "Mask array realloc() failed"); |
6f1186aa ML |
1171 | |
1172 | event_set->handles = new_handles; | |
1173 | event_set->masks = new_masks; | |
1174 | ||
1175 | ((event_handle *) event_set->handles)[event_set->count] = handle; | |
1176 | event_set->masks[event_set->count] = mask; | |
1177 | ||
1178 | event_set->count++; | |
1179 | ||
1180 | RETURN_OK(); | |
1181 | } | |
1182 | ||
970f279a | 1183 | SP_API enum sp_return sp_add_port_events(struct sp_event_set *event_set, |
6f1186aa ML |
1184 | const struct sp_port *port, enum sp_event mask) |
1185 | { | |
1186 | TRACE("%p, %p, %d", event_set, port, mask); | |
1187 | ||
1188 | if (!event_set) | |
1189 | RETURN_ERROR(SP_ERR_ARG, "Null event set"); | |
1190 | ||
1191 | if (!port) | |
1192 | RETURN_ERROR(SP_ERR_ARG, "Null port"); | |
1193 | ||
1194 | if (mask > (SP_EVENT_RX_READY | SP_EVENT_TX_READY | SP_EVENT_ERROR)) | |
1195 | RETURN_ERROR(SP_ERR_ARG, "Invalid event mask"); | |
1196 | ||
1197 | if (!mask) | |
1198 | RETURN_OK(); | |
1199 | ||
1200 | #ifdef _WIN32 | |
1201 | enum sp_event handle_mask; | |
1202 | if ((handle_mask = mask & SP_EVENT_TX_READY)) | |
1203 | TRY(add_handle(event_set, port->write_ovl.hEvent, handle_mask)); | |
1204 | if ((handle_mask = mask & (SP_EVENT_RX_READY | SP_EVENT_ERROR))) | |
1205 | TRY(add_handle(event_set, port->wait_ovl.hEvent, handle_mask)); | |
1206 | #else | |
1207 | TRY(add_handle(event_set, port->fd, mask)); | |
1208 | #endif | |
1209 | ||
1210 | RETURN_OK(); | |
1211 | } | |
1212 | ||
970f279a | 1213 | SP_API void sp_free_event_set(struct sp_event_set *event_set) |
6f1186aa ML |
1214 | { |
1215 | TRACE("%p", event_set); | |
1216 | ||
1217 | if (!event_set) { | |
1218 | DEBUG("Null event set"); | |
1219 | RETURN(); | |
1220 | } | |
1221 | ||
1222 | DEBUG("Freeing event set"); | |
1223 | ||
1224 | if (event_set->handles) | |
1225 | free(event_set->handles); | |
1226 | if (event_set->masks) | |
1227 | free(event_set->masks); | |
1228 | ||
1229 | free(event_set); | |
1230 | ||
1231 | RETURN(); | |
1232 | } | |
1233 | ||
970f279a AJ |
1234 | SP_API enum sp_return sp_wait(struct sp_event_set *event_set, |
1235 | unsigned int timeout) | |
6f1186aa ML |
1236 | { |
1237 | TRACE("%p, %d", event_set, timeout); | |
1238 | ||
1239 | if (!event_set) | |
1240 | RETURN_ERROR(SP_ERR_ARG, "Null event set"); | |
1241 | ||
1242 | #ifdef _WIN32 | |
1243 | if (WaitForMultipleObjects(event_set->count, event_set->handles, FALSE, | |
1244 | timeout ? timeout : INFINITE) == WAIT_FAILED) | |
1245 | RETURN_FAIL("WaitForMultipleObjects() failed"); | |
1246 | ||
1247 | RETURN_OK(); | |
1248 | #else | |
1249 | struct timeval start, delta, now, end = {0, 0}; | |
1250 | int result, timeout_remaining; | |
1251 | struct pollfd *pollfds; | |
1252 | unsigned int i; | |
1253 | ||
1254 | if (!(pollfds = malloc(sizeof(struct pollfd) * event_set->count))) | |
1255 | RETURN_ERROR(SP_ERR_MEM, "pollfds malloc() failed"); | |
1256 | ||
1257 | for (i = 0; i < event_set->count; i++) { | |
1258 | pollfds[i].fd = ((int *) event_set->handles)[i]; | |
1259 | pollfds[i].events = 0; | |
1260 | pollfds[i].revents = 0; | |
1261 | if (event_set->masks[i] & SP_EVENT_RX_READY) | |
1262 | pollfds[i].events |= POLLIN; | |
1263 | if (event_set->masks[i] & SP_EVENT_TX_READY) | |
1264 | pollfds[i].events |= POLLOUT; | |
1265 | if (event_set->masks[i] & SP_EVENT_ERROR) | |
1266 | pollfds[i].events |= POLLERR; | |
1267 | } | |
1268 | ||
1269 | if (timeout) { | |
1270 | /* Get time at start of operation. */ | |
1271 | gettimeofday(&start, NULL); | |
1272 | /* Define duration of timeout. */ | |
1273 | delta.tv_sec = timeout / 1000; | |
1274 | delta.tv_usec = (timeout % 1000) * 1000; | |
1275 | /* Calculate time at which we should give up. */ | |
1276 | timeradd(&start, &delta, &end); | |
1277 | } | |
1278 | ||
1279 | /* Loop until an event occurs. */ | |
dc422c04 | 1280 | while (1) { |
6f1186aa ML |
1281 | if (timeout) { |
1282 | gettimeofday(&now, NULL); | |
1283 | if (timercmp(&now, &end, >)) { | |
dc422c04 | 1284 | DEBUG("Wait timed out"); |
6f1186aa ML |
1285 | break; |
1286 | } | |
1287 | timersub(&end, &now, &delta); | |
1288 | timeout_remaining = delta.tv_sec * 1000 + delta.tv_usec / 1000; | |
1289 | } | |
1290 | ||
1291 | result = poll(pollfds, event_set->count, timeout ? timeout_remaining : -1); | |
1292 | ||
1293 | if (result < 0) { | |
1294 | if (errno == EINTR) { | |
1295 | DEBUG("poll() call was interrupted, repeating"); | |
1296 | continue; | |
1297 | } else { | |
1298 | free(pollfds); | |
1299 | RETURN_FAIL("poll() failed"); | |
1300 | } | |
1301 | } else if (result == 0) { | |
1302 | DEBUG("poll() timed out"); | |
1303 | break; | |
1304 | } else { | |
1305 | DEBUG("poll() completed"); | |
1306 | break; | |
1307 | } | |
1308 | } | |
1309 | ||
1310 | free(pollfds); | |
1311 | RETURN_OK(); | |
1312 | #endif | |
1313 | } | |
1314 | ||
5cea279a | 1315 | #ifdef USE_TERMIOS_SPEED |
7a6d2196 ML |
1316 | static enum sp_return get_baudrate(int fd, int *baudrate) |
1317 | { | |
1318 | void *data; | |
1319 | ||
c33efc48 ML |
1320 | TRACE("%d, %p", fd, baudrate); |
1321 | ||
ea667be7 ML |
1322 | DEBUG("Getting baud rate"); |
1323 | ||
7a6d2196 | 1324 | if (!(data = malloc(get_termios_size()))) |
c33efc48 | 1325 | RETURN_ERROR(SP_ERR_MEM, "termios malloc failed"); |
7a6d2196 | 1326 | |
8d43110a ML |
1327 | if (ioctl(fd, get_termios_get_ioctl(), data) < 0) { |
1328 | free(data); | |
dc422c04 | 1329 | RETURN_FAIL("Getting termios failed"); |
8d43110a | 1330 | } |
7a6d2196 ML |
1331 | |
1332 | *baudrate = get_termios_speed(data); | |
1333 | ||
8d43110a ML |
1334 | free(data); |
1335 | ||
c33efc48 | 1336 | RETURN_OK(); |
7a6d2196 ML |
1337 | } |
1338 | ||
1339 | static enum sp_return set_baudrate(int fd, int baudrate) | |
1340 | { | |
1341 | void *data; | |
1342 | ||
c33efc48 ML |
1343 | TRACE("%d, %d", fd, baudrate); |
1344 | ||
ea667be7 ML |
1345 | DEBUG("Getting baud rate"); |
1346 | ||
7a6d2196 | 1347 | if (!(data = malloc(get_termios_size()))) |
c33efc48 | 1348 | RETURN_ERROR(SP_ERR_MEM, "termios malloc failed"); |
7a6d2196 | 1349 | |
8d43110a ML |
1350 | if (ioctl(fd, get_termios_get_ioctl(), data) < 0) { |
1351 | free(data); | |
dc422c04 | 1352 | RETURN_FAIL("Getting termios failed"); |
8d43110a | 1353 | } |
7a6d2196 | 1354 | |
ea667be7 ML |
1355 | DEBUG("Setting baud rate"); |
1356 | ||
7a6d2196 ML |
1357 | set_termios_speed(data, baudrate); |
1358 | ||
8d43110a ML |
1359 | if (ioctl(fd, get_termios_set_ioctl(), data) < 0) { |
1360 | free(data); | |
dc422c04 | 1361 | RETURN_FAIL("Setting termios failed"); |
8d43110a ML |
1362 | } |
1363 | ||
1364 | free(data); | |
7a6d2196 | 1365 | |
c33efc48 | 1366 | RETURN_OK(); |
7a6d2196 | 1367 | } |
5cea279a | 1368 | #endif /* USE_TERMIOS_SPEED */ |
40978c2b ML |
1369 | |
1370 | #ifdef USE_TERMIOX | |
bd791fe1 | 1371 | static enum sp_return get_flow(int fd, struct port_data *data) |
40978c2b | 1372 | { |
bd791fe1 | 1373 | void *termx; |
40978c2b | 1374 | |
bd791fe1 | 1375 | TRACE("%d, %p", fd, data); |
c33efc48 | 1376 | |
ea667be7 ML |
1377 | DEBUG("Getting advanced flow control"); |
1378 | ||
bd791fe1 | 1379 | if (!(termx = malloc(get_termiox_size()))) |
c33efc48 | 1380 | RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed"); |
40978c2b | 1381 | |
bd791fe1 ML |
1382 | if (ioctl(fd, TCGETX, termx) < 0) { |
1383 | free(termx); | |
dc422c04 | 1384 | RETURN_FAIL("Getting termiox failed"); |
8d43110a | 1385 | } |
40978c2b | 1386 | |
bd791fe1 ML |
1387 | get_termiox_flow(termx, &data->rts_flow, &data->cts_flow, |
1388 | &data->dtr_flow, &data->dsr_flow); | |
40978c2b | 1389 | |
bd791fe1 | 1390 | free(termx); |
8d43110a | 1391 | |
c33efc48 | 1392 | RETURN_OK(); |
40978c2b ML |
1393 | } |
1394 | ||
bd791fe1 | 1395 | static enum sp_return set_flow(int fd, struct port_data *data) |
40978c2b | 1396 | { |
bd791fe1 | 1397 | void *termx; |
40978c2b | 1398 | |
bd791fe1 | 1399 | TRACE("%d, %p", fd, data); |
c33efc48 | 1400 | |
ea667be7 ML |
1401 | DEBUG("Getting advanced flow control"); |
1402 | ||
bd791fe1 | 1403 | if (!(termx = malloc(get_termiox_size()))) |
c33efc48 | 1404 | RETURN_ERROR(SP_ERR_MEM, "termiox malloc failed"); |
40978c2b | 1405 | |
bd791fe1 ML |
1406 | if (ioctl(fd, TCGETX, termx) < 0) { |
1407 | free(termx); | |
dc422c04 | 1408 | RETURN_FAIL("Getting termiox failed"); |
8d43110a | 1409 | } |
40978c2b | 1410 | |
ea667be7 ML |
1411 | DEBUG("Setting advanced flow control"); |
1412 | ||
bd791fe1 ML |
1413 | set_termiox_flow(termx, data->rts_flow, data->cts_flow, |
1414 | data->dtr_flow, data->dsr_flow); | |
40978c2b | 1415 | |
bd791fe1 ML |
1416 | if (ioctl(fd, TCSETX, termx) < 0) { |
1417 | free(termx); | |
dc422c04 | 1418 | RETURN_FAIL("Setting termiox failed"); |
8d43110a ML |
1419 | } |
1420 | ||
bd791fe1 | 1421 | free(termx); |
40978c2b | 1422 | |
c33efc48 | 1423 | RETURN_OK(); |
40978c2b ML |
1424 | } |
1425 | #endif /* USE_TERMIOX */ | |
7a6d2196 | 1426 | |
eb6ed20f ML |
1427 | static enum sp_return get_config(struct sp_port *port, struct port_data *data, |
1428 | struct sp_port_config *config) | |
8094e4a0 | 1429 | { |
da2748bf | 1430 | unsigned int i; |
cbf628c7 | 1431 | |
c33efc48 ML |
1432 | TRACE("%p, %p, %p", port, data, config); |
1433 | ||
7890cef6 | 1434 | DEBUG_FMT("Getting configuration for port %s", port->name); |
ea667be7 | 1435 | |
8094e4a0 | 1436 | #ifdef _WIN32 |
e33ab9aa | 1437 | if (!GetCommState(port->hdl, &data->dcb)) |
c33efc48 | 1438 | RETURN_FAIL("GetCommState() failed"); |
8094e4a0 | 1439 | |
067417af | 1440 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
e33ab9aa | 1441 | if (data->dcb.BaudRate == std_baudrates[i].index) { |
067417af ML |
1442 | config->baudrate = std_baudrates[i].value; |
1443 | break; | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | if (i == NUM_STD_BAUDRATES) | |
1448 | /* BaudRate field can be either an index or a custom baud rate. */ | |
e33ab9aa | 1449 | config->baudrate = data->dcb.BaudRate; |
067417af | 1450 | |
e33ab9aa | 1451 | config->bits = data->dcb.ByteSize; |
067417af | 1452 | |
e33ab9aa ML |
1453 | if (data->dcb.fParity) |
1454 | switch (data->dcb.Parity) { | |
067417af ML |
1455 | case NOPARITY: |
1456 | config->parity = SP_PARITY_NONE; | |
1457 | break; | |
e432ce60 ML |
1458 | case ODDPARITY: |
1459 | config->parity = SP_PARITY_ODD; | |
1460 | break; | |
067417af ML |
1461 | case EVENPARITY: |
1462 | config->parity = SP_PARITY_EVEN; | |
1463 | break; | |
e432ce60 ML |
1464 | case MARKPARITY: |
1465 | config->parity = SP_PARITY_MARK; | |
1466 | break; | |
1467 | case SPACEPARITY: | |
1468 | config->parity = SP_PARITY_SPACE; | |
067417af ML |
1469 | break; |
1470 | default: | |
1471 | config->parity = -1; | |
1472 | } | |
1473 | else | |
1474 | config->parity = SP_PARITY_NONE; | |
1475 | ||
e33ab9aa | 1476 | switch (data->dcb.StopBits) { |
067417af ML |
1477 | case ONESTOPBIT: |
1478 | config->stopbits = 1; | |
1479 | break; | |
1480 | case TWOSTOPBITS: | |
1481 | config->stopbits = 2; | |
1482 | break; | |
1483 | default: | |
1484 | config->stopbits = -1; | |
1485 | } | |
1486 | ||
e33ab9aa | 1487 | switch (data->dcb.fRtsControl) { |
eac329d2 UH |
1488 | case RTS_CONTROL_DISABLE: |
1489 | config->rts = SP_RTS_OFF; | |
1490 | break; | |
1491 | case RTS_CONTROL_ENABLE: | |
1492 | config->rts = SP_RTS_ON; | |
1493 | break; | |
1494 | case RTS_CONTROL_HANDSHAKE: | |
1495 | config->rts = SP_RTS_FLOW_CONTROL; | |
1496 | break; | |
1497 | default: | |
1498 | config->rts = -1; | |
067417af ML |
1499 | } |
1500 | ||
e33ab9aa | 1501 | config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE; |
067417af | 1502 | |
e33ab9aa | 1503 | switch (data->dcb.fDtrControl) { |
eac329d2 UH |
1504 | case DTR_CONTROL_DISABLE: |
1505 | config->dtr = SP_DTR_OFF; | |
1506 | break; | |
1507 | case DTR_CONTROL_ENABLE: | |
1508 | config->dtr = SP_DTR_ON; | |
1509 | break; | |
1510 | case DTR_CONTROL_HANDSHAKE: | |
1511 | config->dtr = SP_DTR_FLOW_CONTROL; | |
1512 | break; | |
1513 | default: | |
1514 | config->dtr = -1; | |
067417af ML |
1515 | } |
1516 | ||
e33ab9aa ML |
1517 | config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE; |
1518 | ||
c6754b45 ML |
1519 | if (data->dcb.fInX) { |
1520 | if (data->dcb.fOutX) | |
1521 | config->xon_xoff = SP_XONXOFF_INOUT; | |
1522 | else | |
1523 | config->xon_xoff = SP_XONXOFF_IN; | |
1524 | } else { | |
1525 | if (data->dcb.fOutX) | |
1526 | config->xon_xoff = SP_XONXOFF_OUT; | |
1527 | else | |
1528 | config->xon_xoff = SP_XONXOFF_DISABLED; | |
1529 | } | |
1530 | ||
e33ab9aa ML |
1531 | #else // !_WIN32 |
1532 | ||
1533 | if (tcgetattr(port->fd, &data->term) < 0) | |
c33efc48 | 1534 | RETURN_FAIL("tcgetattr() failed"); |
e33ab9aa ML |
1535 | |
1536 | if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0) | |
c33efc48 | 1537 | RETURN_FAIL("TIOCMGET ioctl failed"); |
40978c2b ML |
1538 | |
1539 | #ifdef USE_TERMIOX | |
bd791fe1 | 1540 | int ret = get_flow(port->fd, data); |
68ec29db ML |
1541 | |
1542 | if (ret == SP_ERR_FAIL && errno == EINVAL) | |
1543 | data->termiox_supported = 0; | |
1544 | else if (ret < 0) | |
c33efc48 | 1545 | RETURN_CODEVAL(ret); |
68ec29db ML |
1546 | else |
1547 | data->termiox_supported = 1; | |
1548 | #else | |
1549 | data->termiox_supported = 0; | |
40978c2b ML |
1550 | #endif |
1551 | ||
067417af | 1552 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
e33ab9aa | 1553 | if (cfgetispeed(&data->term) == std_baudrates[i].index) { |
067417af ML |
1554 | config->baudrate = std_baudrates[i].value; |
1555 | break; | |
1556 | } | |
1557 | } | |
1558 | ||
31b3a8f5 MH |
1559 | if (i == NUM_STD_BAUDRATES) { |
1560 | #ifdef __APPLE__ | |
1561 | config->baudrate = (int)data->term.c_ispeed; | |
5cea279a | 1562 | #elif defined(USE_TERMIOS_SPEED) |
7a6d2196 | 1563 | TRY(get_baudrate(port->fd, &config->baudrate)); |
31b3a8f5 | 1564 | #else |
067417af | 1565 | config->baudrate = -1; |
31b3a8f5 MH |
1566 | #endif |
1567 | } | |
067417af | 1568 | |
e33ab9aa | 1569 | switch (data->term.c_cflag & CSIZE) { |
067417af ML |
1570 | case CS8: |
1571 | config->bits = 8; | |
1572 | break; | |
1573 | case CS7: | |
1574 | config->bits = 7; | |
1575 | break; | |
1576 | case CS6: | |
1577 | config->bits = 6; | |
1578 | break; | |
1579 | case CS5: | |
1580 | config->bits = 5; | |
1581 | break; | |
1582 | default: | |
1583 | config->bits = -1; | |
1584 | } | |
1585 | ||
e33ab9aa | 1586 | if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR)) |
067417af | 1587 | config->parity = SP_PARITY_NONE; |
e33ab9aa | 1588 | else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR)) |
067417af | 1589 | config->parity = -1; |
c3e05092 | 1590 | #ifdef CMSPAR |
e432ce60 ML |
1591 | else if (data->term.c_cflag & CMSPAR) |
1592 | config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_MARK : SP_PARITY_SPACE; | |
c3e05092 | 1593 | #endif |
067417af | 1594 | else |
e33ab9aa | 1595 | config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN; |
067417af | 1596 | |
e33ab9aa | 1597 | config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1; |
067417af | 1598 | |
e33ab9aa | 1599 | if (data->term.c_cflag & CRTSCTS) { |
067417af ML |
1600 | config->rts = SP_RTS_FLOW_CONTROL; |
1601 | config->cts = SP_CTS_FLOW_CONTROL; | |
1602 | } else { | |
bd791fe1 | 1603 | if (data->termiox_supported && data->rts_flow) |
40978c2b ML |
1604 | config->rts = SP_RTS_FLOW_CONTROL; |
1605 | else | |
1606 | config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF; | |
1607 | ||
bd791fe1 | 1608 | config->cts = (data->termiox_supported && data->cts_flow) ? |
68ec29db | 1609 | SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE; |
067417af ML |
1610 | } |
1611 | ||
bd791fe1 | 1612 | if (data->termiox_supported && data->dtr_flow) |
40978c2b ML |
1613 | config->dtr = SP_DTR_FLOW_CONTROL; |
1614 | else | |
1615 | config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF; | |
1616 | ||
bd791fe1 | 1617 | config->dsr = (data->termiox_supported && data->dsr_flow) ? |
68ec29db | 1618 | SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE; |
705bdc69 | 1619 | |
e29b93a5 ML |
1620 | if (data->term.c_iflag & IXOFF) { |
1621 | if (data->term.c_iflag & IXON) | |
1622 | config->xon_xoff = SP_XONXOFF_INOUT; | |
1623 | else | |
1624 | config->xon_xoff = SP_XONXOFF_IN; | |
1625 | } else { | |
1626 | if (data->term.c_iflag & IXON) | |
1627 | config->xon_xoff = SP_XONXOFF_OUT; | |
1628 | else | |
1629 | config->xon_xoff = SP_XONXOFF_DISABLED; | |
1630 | } | |
067417af ML |
1631 | #endif |
1632 | ||
c33efc48 | 1633 | RETURN_OK(); |
067417af ML |
1634 | } |
1635 | ||
7a6d2196 | 1636 | static enum sp_return set_config(struct sp_port *port, struct port_data *data, |
eb6ed20f | 1637 | const struct sp_port_config *config) |
18fc2dd1 | 1638 | { |
e33ab9aa | 1639 | unsigned int i; |
31b3a8f5 MH |
1640 | #ifdef __APPLE__ |
1641 | BAUD_TYPE baud_nonstd; | |
1642 | ||
1643 | baud_nonstd = B0; | |
1644 | #endif | |
5cea279a | 1645 | #ifdef USE_TERMIOS_SPEED |
7a6d2196 ML |
1646 | int baud_nonstd = 0; |
1647 | #endif | |
18fc2dd1 | 1648 | |
c33efc48 ML |
1649 | TRACE("%p, %p, %p", port, data, config); |
1650 | ||
7890cef6 | 1651 | DEBUG_FMT("Setting configuration for port %s", port->name); |
ea667be7 | 1652 | |
e33ab9aa | 1653 | #ifdef _WIN32 |
eac329d2 | 1654 | if (config->baudrate >= 0) { |
e33ab9aa ML |
1655 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
1656 | if (config->baudrate == std_baudrates[i].value) { | |
1657 | data->dcb.BaudRate = std_baudrates[i].index; | |
1658 | break; | |
1659 | } | |
1660 | } | |
18fc2dd1 | 1661 | |
e33ab9aa ML |
1662 | if (i == NUM_STD_BAUDRATES) |
1663 | data->dcb.BaudRate = config->baudrate; | |
1664 | } | |
18fc2dd1 | 1665 | |
e33ab9aa ML |
1666 | if (config->bits >= 0) |
1667 | data->dcb.ByteSize = config->bits; | |
1668 | ||
1669 | if (config->parity >= 0) { | |
1670 | switch (config->parity) { | |
e33ab9aa ML |
1671 | case SP_PARITY_NONE: |
1672 | data->dcb.Parity = NOPARITY; | |
1673 | break; | |
e432ce60 ML |
1674 | case SP_PARITY_ODD: |
1675 | data->dcb.Parity = ODDPARITY; | |
1676 | break; | |
e33ab9aa ML |
1677 | case SP_PARITY_EVEN: |
1678 | data->dcb.Parity = EVENPARITY; | |
1679 | break; | |
e432ce60 ML |
1680 | case SP_PARITY_MARK: |
1681 | data->dcb.Parity = MARKPARITY; | |
1682 | break; | |
1683 | case SP_PARITY_SPACE: | |
1684 | data->dcb.Parity = SPACEPARITY; | |
e33ab9aa ML |
1685 | break; |
1686 | default: | |
c33efc48 | 1687 | RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting"); |
e33ab9aa | 1688 | } |
18fc2dd1 ML |
1689 | } |
1690 | ||
e33ab9aa ML |
1691 | if (config->stopbits >= 0) { |
1692 | switch (config->stopbits) { | |
1693 | /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */ | |
1694 | case 1: | |
1695 | data->dcb.StopBits = ONESTOPBIT; | |
1696 | break; | |
1697 | case 2: | |
1698 | data->dcb.StopBits = TWOSTOPBITS; | |
1699 | break; | |
1700 | default: | |
c33efc48 | 1701 | RETURN_ERROR(SP_ERR_ARG, "Invalid stop bit setting"); |
e33ab9aa ML |
1702 | } |
1703 | } | |
1704 | ||
1705 | if (config->rts >= 0) { | |
1706 | switch (config->rts) { | |
1707 | case SP_RTS_OFF: | |
1708 | data->dcb.fRtsControl = RTS_CONTROL_DISABLE; | |
1709 | break; | |
1710 | case SP_RTS_ON: | |
1711 | data->dcb.fRtsControl = RTS_CONTROL_ENABLE; | |
1712 | break; | |
1713 | case SP_RTS_FLOW_CONTROL: | |
1714 | data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; | |
1715 | break; | |
1716 | default: | |
c33efc48 | 1717 | RETURN_ERROR(SP_ERR_ARG, "Invalid RTS setting"); |
e33ab9aa ML |
1718 | } |
1719 | } | |
1720 | ||
1721 | if (config->cts >= 0) { | |
1722 | switch (config->cts) { | |
1723 | case SP_CTS_IGNORE: | |
1724 | data->dcb.fOutxCtsFlow = FALSE; | |
1725 | break; | |
1726 | case SP_CTS_FLOW_CONTROL: | |
1727 | data->dcb.fOutxCtsFlow = TRUE; | |
1728 | break; | |
1729 | default: | |
c33efc48 | 1730 | RETURN_ERROR(SP_ERR_ARG, "Invalid CTS setting"); |
e33ab9aa ML |
1731 | } |
1732 | } | |
1733 | ||
1734 | if (config->dtr >= 0) { | |
1735 | switch (config->dtr) { | |
1736 | case SP_DTR_OFF: | |
1737 | data->dcb.fDtrControl = DTR_CONTROL_DISABLE; | |
1738 | break; | |
1739 | case SP_DTR_ON: | |
1740 | data->dcb.fDtrControl = DTR_CONTROL_ENABLE; | |
1741 | break; | |
1742 | case SP_DTR_FLOW_CONTROL: | |
1743 | data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; | |
1744 | break; | |
1745 | default: | |
c33efc48 | 1746 | RETURN_ERROR(SP_ERR_ARG, "Invalid DTR setting"); |
e33ab9aa ML |
1747 | } |
1748 | } | |
1749 | ||
1750 | if (config->dsr >= 0) { | |
1751 | switch (config->dsr) { | |
1752 | case SP_DSR_IGNORE: | |
1753 | data->dcb.fOutxDsrFlow = FALSE; | |
1754 | break; | |
1755 | case SP_DSR_FLOW_CONTROL: | |
1756 | data->dcb.fOutxDsrFlow = TRUE; | |
1757 | break; | |
1758 | default: | |
c33efc48 | 1759 | RETURN_ERROR(SP_ERR_ARG, "Invalid DSR setting"); |
e33ab9aa | 1760 | } |
18fc2dd1 ML |
1761 | } |
1762 | ||
e33ab9aa ML |
1763 | if (config->xon_xoff >= 0) { |
1764 | switch (config->xon_xoff) { | |
1765 | case SP_XONXOFF_DISABLED: | |
1766 | data->dcb.fInX = FALSE; | |
1767 | data->dcb.fOutX = FALSE; | |
1768 | break; | |
1769 | case SP_XONXOFF_IN: | |
1770 | data->dcb.fInX = TRUE; | |
1771 | data->dcb.fOutX = FALSE; | |
1772 | break; | |
1773 | case SP_XONXOFF_OUT: | |
1774 | data->dcb.fInX = FALSE; | |
1775 | data->dcb.fOutX = TRUE; | |
1776 | break; | |
1777 | case SP_XONXOFF_INOUT: | |
1778 | data->dcb.fInX = TRUE; | |
1779 | data->dcb.fOutX = TRUE; | |
1780 | break; | |
1781 | default: | |
c33efc48 | 1782 | RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting"); |
e33ab9aa ML |
1783 | } |
1784 | } | |
1785 | ||
1786 | if (!SetCommState(port->hdl, &data->dcb)) | |
c33efc48 | 1787 | RETURN_FAIL("SetCommState() failed"); |
e33ab9aa | 1788 | |
31b3a8f5 | 1789 | #else /* !_WIN32 */ |
e33ab9aa | 1790 | |
7a6d2196 ML |
1791 | int controlbits; |
1792 | ||
eac329d2 | 1793 | if (config->baudrate >= 0) { |
e33ab9aa ML |
1794 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
1795 | if (config->baudrate == std_baudrates[i].value) { | |
1796 | if (cfsetospeed(&data->term, std_baudrates[i].index) < 0) | |
c33efc48 | 1797 | RETURN_FAIL("cfsetospeed() failed"); |
e33ab9aa ML |
1798 | |
1799 | if (cfsetispeed(&data->term, std_baudrates[i].index) < 0) | |
c33efc48 | 1800 | RETURN_FAIL("cfsetispeed() failed"); |
e33ab9aa ML |
1801 | break; |
1802 | } | |
1803 | } | |
1804 | ||
31b3a8f5 MH |
1805 | /* Non-standard baud rate */ |
1806 | if (i == NUM_STD_BAUDRATES) { | |
1807 | #ifdef __APPLE__ | |
24abdb68 | 1808 | /* Set "dummy" baud rate. */ |
31b3a8f5 | 1809 | if (cfsetspeed(&data->term, B9600) < 0) |
c33efc48 | 1810 | RETURN_FAIL("cfsetspeed() failed"); |
31b3a8f5 | 1811 | baud_nonstd = config->baudrate; |
5cea279a | 1812 | #elif defined(USE_TERMIOS_SPEED) |
7a6d2196 | 1813 | baud_nonstd = 1; |
31b3a8f5 | 1814 | #else |
c33efc48 | 1815 | RETURN_ERROR(SP_ERR_SUPP, "Non-standard baudrate not supported"); |
31b3a8f5 MH |
1816 | #endif |
1817 | } | |
e33ab9aa ML |
1818 | } |
1819 | ||
1820 | if (config->bits >= 0) { | |
1821 | data->term.c_cflag &= ~CSIZE; | |
1822 | switch (config->bits) { | |
1823 | case 8: | |
1824 | data->term.c_cflag |= CS8; | |
1825 | break; | |
1826 | case 7: | |
1827 | data->term.c_cflag |= CS7; | |
1828 | break; | |
1829 | case 6: | |
1830 | data->term.c_cflag |= CS6; | |
1831 | break; | |
23922313 UH |
1832 | case 5: |
1833 | data->term.c_cflag |= CS5; | |
1834 | break; | |
e33ab9aa | 1835 | default: |
c33efc48 | 1836 | RETURN_ERROR(SP_ERR_ARG, "Invalid data bits setting"); |
e33ab9aa ML |
1837 | } |
1838 | } | |
1839 | ||
1840 | if (config->parity >= 0) { | |
1841 | data->term.c_iflag &= ~IGNPAR; | |
c3e05092 UH |
1842 | data->term.c_cflag &= ~(PARENB | PARODD); |
1843 | #ifdef CMSPAR | |
1844 | data->term.c_cflag &= ~CMSPAR; | |
1845 | #endif | |
e33ab9aa ML |
1846 | switch (config->parity) { |
1847 | case SP_PARITY_NONE: | |
1848 | data->term.c_iflag |= IGNPAR; | |
1849 | break; | |
1850 | case SP_PARITY_EVEN: | |
1851 | data->term.c_cflag |= PARENB; | |
1852 | break; | |
1853 | case SP_PARITY_ODD: | |
1854 | data->term.c_cflag |= PARENB | PARODD; | |
1855 | break; | |
afb518f0 | 1856 | #ifdef CMSPAR |
e432ce60 | 1857 | case SP_PARITY_MARK: |
c3e05092 | 1858 | data->term.c_cflag |= PARENB | PARODD; |
c3e05092 | 1859 | data->term.c_cflag |= CMSPAR; |
e432ce60 ML |
1860 | break; |
1861 | case SP_PARITY_SPACE: | |
c3e05092 | 1862 | data->term.c_cflag |= PARENB; |
c3e05092 | 1863 | data->term.c_cflag |= CMSPAR; |
e432ce60 | 1864 | break; |
afb518f0 ML |
1865 | #else |
1866 | case SP_PARITY_MARK: | |
1867 | case SP_PARITY_SPACE: | |
1868 | RETURN_ERROR(SP_ERR_SUPP, "Mark/space parity not supported"); | |
1869 | #endif | |
e33ab9aa | 1870 | default: |
c33efc48 | 1871 | RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting"); |
e33ab9aa ML |
1872 | } |
1873 | } | |
1874 | ||
1875 | if (config->stopbits >= 0) { | |
1876 | data->term.c_cflag &= ~CSTOPB; | |
1877 | switch (config->stopbits) { | |
1878 | case 1: | |
1879 | data->term.c_cflag &= ~CSTOPB; | |
1880 | break; | |
1881 | case 2: | |
1882 | data->term.c_cflag |= CSTOPB; | |
1883 | break; | |
1884 | default: | |
c33efc48 | 1885 | RETURN_ERROR(SP_ERR_ARG, "Invalid stop bits setting"); |
e33ab9aa ML |
1886 | } |
1887 | } | |
1888 | ||
eac329d2 | 1889 | if (config->rts >= 0 || config->cts >= 0) { |
68ec29db | 1890 | if (data->termiox_supported) { |
bd791fe1 | 1891 | data->rts_flow = data->cts_flow = 0; |
68ec29db ML |
1892 | switch (config->rts) { |
1893 | case SP_RTS_OFF: | |
1894 | case SP_RTS_ON: | |
1895 | controlbits = TIOCM_RTS; | |
1896 | if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0) | |
c33efc48 | 1897 | RETURN_FAIL("Setting RTS signal level failed"); |
68ec29db ML |
1898 | break; |
1899 | case SP_RTS_FLOW_CONTROL: | |
bd791fe1 | 1900 | data->rts_flow = 1; |
68ec29db ML |
1901 | break; |
1902 | default: | |
1903 | break; | |
e33ab9aa | 1904 | } |
68ec29db | 1905 | if (config->cts == SP_CTS_FLOW_CONTROL) |
bd791fe1 | 1906 | data->cts_flow = 1; |
e33ab9aa | 1907 | |
bd791fe1 | 1908 | if (data->rts_flow && data->cts_flow) |
e33ab9aa | 1909 | data->term.c_iflag |= CRTSCTS; |
68ec29db ML |
1910 | else |
1911 | data->term.c_iflag &= ~CRTSCTS; | |
1912 | } else { | |
1913 | /* Asymmetric use of RTS/CTS not supported. */ | |
1914 | if (data->term.c_iflag & CRTSCTS) { | |
1915 | /* Flow control can only be disabled for both RTS & CTS together. */ | |
1916 | if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) { | |
1917 | if (config->cts != SP_CTS_IGNORE) | |
c33efc48 | 1918 | RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together"); |
68ec29db ML |
1919 | } |
1920 | if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) { | |
1921 | if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL) | |
c33efc48 | 1922 | RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together"); |
68ec29db | 1923 | } |
e33ab9aa | 1924 | } else { |
68ec29db ML |
1925 | /* Flow control can only be enabled for both RTS & CTS together. */ |
1926 | if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) || | |
1927 | ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL))) | |
c33efc48 | 1928 | RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be enabled together"); |
68ec29db ML |
1929 | } |
1930 | ||
1931 | if (config->rts >= 0) { | |
1932 | if (config->rts == SP_RTS_FLOW_CONTROL) { | |
1933 | data->term.c_iflag |= CRTSCTS; | |
1934 | } else { | |
1935 | controlbits = TIOCM_RTS; | |
1936 | if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, | |
1937 | &controlbits) < 0) | |
c33efc48 | 1938 | RETURN_FAIL("Setting RTS signal level failed"); |
68ec29db | 1939 | } |
e33ab9aa ML |
1940 | } |
1941 | } | |
1942 | } | |
1943 | ||
eac329d2 | 1944 | if (config->dtr >= 0 || config->dsr >= 0) { |
68ec29db | 1945 | if (data->termiox_supported) { |
bd791fe1 | 1946 | data->dtr_flow = data->dsr_flow = 0; |
68ec29db ML |
1947 | switch (config->dtr) { |
1948 | case SP_DTR_OFF: | |
1949 | case SP_DTR_ON: | |
1950 | controlbits = TIOCM_DTR; | |
1951 | if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0) | |
c33efc48 | 1952 | RETURN_FAIL("Setting DTR signal level failed"); |
68ec29db ML |
1953 | break; |
1954 | case SP_DTR_FLOW_CONTROL: | |
bd791fe1 | 1955 | data->dtr_flow = 1; |
68ec29db ML |
1956 | break; |
1957 | default: | |
1958 | break; | |
1959 | } | |
1960 | if (config->dsr == SP_DSR_FLOW_CONTROL) | |
bd791fe1 | 1961 | data->dsr_flow = 1; |
68ec29db ML |
1962 | } else { |
1963 | /* DTR/DSR flow control not supported. */ | |
1964 | if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL) | |
c33efc48 | 1965 | RETURN_ERROR(SP_ERR_SUPP, "DTR/DSR flow control not supported"); |
e33ab9aa | 1966 | |
68ec29db ML |
1967 | if (config->dtr >= 0) { |
1968 | controlbits = TIOCM_DTR; | |
1969 | if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, | |
1970 | &controlbits) < 0) | |
c33efc48 | 1971 | RETURN_FAIL("Setting DTR signal level failed"); |
68ec29db | 1972 | } |
e33ab9aa ML |
1973 | } |
1974 | } | |
1975 | ||
1976 | if (config->xon_xoff >= 0) { | |
1977 | data->term.c_iflag &= ~(IXON | IXOFF | IXANY); | |
1978 | switch (config->xon_xoff) { | |
1979 | case SP_XONXOFF_DISABLED: | |
1980 | break; | |
1981 | case SP_XONXOFF_IN: | |
1982 | data->term.c_iflag |= IXOFF; | |
1983 | break; | |
1984 | case SP_XONXOFF_OUT: | |
1985 | data->term.c_iflag |= IXON | IXANY; | |
1986 | break; | |
1987 | case SP_XONXOFF_INOUT: | |
1988 | data->term.c_iflag |= IXON | IXOFF | IXANY; | |
1989 | break; | |
1990 | default: | |
c33efc48 | 1991 | RETURN_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting"); |
e33ab9aa ML |
1992 | } |
1993 | } | |
1994 | ||
3f5c06d0 | 1995 | if (tcsetattr(port->fd, TCSANOW, &data->term) < 0) |
c33efc48 | 1996 | RETURN_FAIL("tcsetattr() failed"); |
31b3a8f5 MH |
1997 | |
1998 | #ifdef __APPLE__ | |
1999 | if (baud_nonstd != B0) { | |
2000 | if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1) | |
c33efc48 | 2001 | RETURN_FAIL("IOSSIOSPEED ioctl failed"); |
dc422c04 UH |
2002 | /* |
2003 | * Set baud rates in data->term to correct, but incompatible | |
2004 | * with tcsetattr() value, same as delivered by tcgetattr(). | |
2005 | */ | |
31b3a8f5 | 2006 | if (cfsetspeed(&data->term, baud_nonstd) < 0) |
c33efc48 | 2007 | RETURN_FAIL("cfsetspeed() failed"); |
31b3a8f5 | 2008 | } |
7a6d2196 | 2009 | #elif defined(__linux__) |
5cea279a | 2010 | #ifdef USE_TERMIOS_SPEED |
7a6d2196 ML |
2011 | if (baud_nonstd) |
2012 | TRY(set_baudrate(port->fd, config->baudrate)); | |
5cea279a | 2013 | #endif |
40978c2b | 2014 | #ifdef USE_TERMIOX |
68ec29db | 2015 | if (data->termiox_supported) |
bd791fe1 | 2016 | TRY(set_flow(port->fd, data)); |
40978c2b | 2017 | #endif |
7a6d2196 | 2018 | #endif |
31b3a8f5 MH |
2019 | |
2020 | #endif /* !_WIN32 */ | |
e33ab9aa | 2021 | |
c33efc48 | 2022 | RETURN_OK(); |
e33ab9aa ML |
2023 | } |
2024 | ||
970f279a | 2025 | SP_API enum sp_return sp_new_config(struct sp_port_config **config_ptr) |
9b1502ef | 2026 | { |
59131d60 | 2027 | struct sp_port_config *config; |
9b1502ef | 2028 | |
00d8c56d UH |
2029 | TRACE("%p", config_ptr); |
2030 | ||
9b1502ef ML |
2031 | if (!config_ptr) |
2032 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); | |
2033 | ||
59131d60 ML |
2034 | *config_ptr = NULL; |
2035 | ||
2036 | if (!(config = malloc(sizeof(struct sp_port_config)))) | |
dc422c04 | 2037 | RETURN_ERROR(SP_ERR_MEM, "Config malloc failed"); |
9b1502ef | 2038 | |
59131d60 ML |
2039 | config->baudrate = -1; |
2040 | config->bits = -1; | |
2041 | config->parity = -1; | |
2042 | config->stopbits = -1; | |
2043 | config->rts = -1; | |
2044 | config->cts = -1; | |
2045 | config->dtr = -1; | |
2046 | config->dsr = -1; | |
2047 | ||
2048 | *config_ptr = config; | |
2049 | ||
9b1502ef ML |
2050 | RETURN_OK(); |
2051 | } | |
2052 | ||
970f279a | 2053 | SP_API void sp_free_config(struct sp_port_config *config) |
9b1502ef ML |
2054 | { |
2055 | TRACE("%p", config); | |
2056 | ||
2057 | if (!config) | |
2058 | DEBUG("Null config"); | |
2059 | else | |
2060 | free(config); | |
2061 | ||
2062 | RETURN(); | |
2063 | } | |
2064 | ||
970f279a AJ |
2065 | SP_API enum sp_return sp_get_config(struct sp_port *port, |
2066 | struct sp_port_config *config) | |
70cd37de ML |
2067 | { |
2068 | struct port_data data; | |
2069 | ||
2070 | TRACE("%p, %p", port, config); | |
2071 | ||
2072 | CHECK_OPEN_PORT(); | |
2073 | ||
2074 | if (!config) | |
9b1502ef | 2075 | RETURN_ERROR(SP_ERR_ARG, "Null config"); |
70cd37de ML |
2076 | |
2077 | TRY(get_config(port, &data, config)); | |
2078 | ||
2079 | RETURN_OK(); | |
2080 | } | |
2081 | ||
970f279a AJ |
2082 | SP_API enum sp_return sp_set_config(struct sp_port *port, |
2083 | const struct sp_port_config *config) | |
e33ab9aa | 2084 | { |
8f189c4c | 2085 | struct port_data data; |
e33ab9aa ML |
2086 | struct sp_port_config prev_config; |
2087 | ||
c33efc48 ML |
2088 | TRACE("%p, %p", port, config); |
2089 | ||
dec10e31 | 2090 | CHECK_OPEN_PORT(); |
823690ae ML |
2091 | |
2092 | if (!config) | |
c33efc48 | 2093 | RETURN_ERROR(SP_ERR_ARG, "Null config"); |
823690ae | 2094 | |
e33ab9aa ML |
2095 | TRY(get_config(port, &data, &prev_config)); |
2096 | TRY(set_config(port, &data, config)); | |
74510d4b | 2097 | |
c33efc48 | 2098 | RETURN_OK(); |
74510d4b ML |
2099 | } |
2100 | ||
9b1502ef | 2101 | #define CREATE_ACCESSORS(x, type) \ |
970f279a | 2102 | SP_API enum sp_return sp_set_##x(struct sp_port *port, type x) { \ |
8f189c4c | 2103 | struct port_data data; \ |
e33ab9aa | 2104 | struct sp_port_config config; \ |
c33efc48 | 2105 | TRACE("%p, %d", port, x); \ |
dec10e31 | 2106 | CHECK_OPEN_PORT(); \ |
e33ab9aa ML |
2107 | TRY(get_config(port, &data, &config)); \ |
2108 | config.x = x; \ | |
2109 | TRY(set_config(port, &data, &config)); \ | |
c33efc48 | 2110 | RETURN_OK(); \ |
9b1502ef | 2111 | } \ |
970f279a AJ |
2112 | SP_API enum sp_return sp_get_config_##x(const struct sp_port_config *config, \ |
2113 | type *x) { \ | |
00d8c56d | 2114 | TRACE("%p, %p", config, x); \ |
9b1502ef ML |
2115 | if (!config) \ |
2116 | RETURN_ERROR(SP_ERR_ARG, "Null config"); \ | |
2117 | *x = config->x; \ | |
2118 | RETURN_OK(); \ | |
2119 | } \ | |
970f279a AJ |
2120 | SP_API enum sp_return sp_set_config_##x(struct sp_port_config *config, \ |
2121 | type x) { \ | |
9b1502ef ML |
2122 | TRACE("%p, %d", config, x); \ |
2123 | if (!config) \ | |
2124 | RETURN_ERROR(SP_ERR_ARG, "Null config"); \ | |
2125 | config->x = x; \ | |
2126 | RETURN_OK(); \ | |
9069c2fb ML |
2127 | } |
2128 | ||
9b1502ef ML |
2129 | CREATE_ACCESSORS(baudrate, int) |
2130 | CREATE_ACCESSORS(bits, int) | |
2131 | CREATE_ACCESSORS(parity, enum sp_parity) | |
2132 | CREATE_ACCESSORS(stopbits, int) | |
2133 | CREATE_ACCESSORS(rts, enum sp_rts) | |
2134 | CREATE_ACCESSORS(cts, enum sp_cts) | |
2135 | CREATE_ACCESSORS(dtr, enum sp_dtr) | |
2136 | CREATE_ACCESSORS(dsr, enum sp_dsr) | |
2137 | CREATE_ACCESSORS(xon_xoff, enum sp_xonxoff) | |
2138 | ||
970f279a AJ |
2139 | SP_API enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, |
2140 | enum sp_flowcontrol flowcontrol) | |
e33ab9aa | 2141 | { |
9b1502ef ML |
2142 | if (!config) |
2143 | RETURN_ERROR(SP_ERR_ARG, "Null configuration"); | |
dec10e31 ML |
2144 | |
2145 | if (flowcontrol > SP_FLOWCONTROL_DTRDSR) | |
2146 | RETURN_ERROR(SP_ERR_ARG, "Invalid flow control setting"); | |
823690ae | 2147 | |
e33ab9aa | 2148 | if (flowcontrol == SP_FLOWCONTROL_XONXOFF) |
9b1502ef | 2149 | config->xon_xoff = SP_XONXOFF_INOUT; |
e33ab9aa | 2150 | else |
9b1502ef | 2151 | config->xon_xoff = SP_XONXOFF_DISABLED; |
e33ab9aa ML |
2152 | |
2153 | if (flowcontrol == SP_FLOWCONTROL_RTSCTS) { | |
9b1502ef ML |
2154 | config->rts = SP_RTS_FLOW_CONTROL; |
2155 | config->cts = SP_CTS_FLOW_CONTROL; | |
e33ab9aa | 2156 | } else { |
9b1502ef ML |
2157 | if (config->rts == SP_RTS_FLOW_CONTROL) |
2158 | config->rts = SP_RTS_ON; | |
2159 | config->cts = SP_CTS_IGNORE; | |
e33ab9aa ML |
2160 | } |
2161 | ||
2162 | if (flowcontrol == SP_FLOWCONTROL_DTRDSR) { | |
9b1502ef ML |
2163 | config->dtr = SP_DTR_FLOW_CONTROL; |
2164 | config->dsr = SP_DSR_FLOW_CONTROL; | |
e33ab9aa | 2165 | } else { |
9b1502ef ML |
2166 | if (config->dtr == SP_DTR_FLOW_CONTROL) |
2167 | config->dtr = SP_DTR_ON; | |
2168 | config->dsr = SP_DSR_IGNORE; | |
e33ab9aa ML |
2169 | } |
2170 | ||
9b1502ef ML |
2171 | RETURN_OK(); |
2172 | } | |
2173 | ||
970f279a AJ |
2174 | SP_API enum sp_return sp_set_flowcontrol(struct sp_port *port, |
2175 | enum sp_flowcontrol flowcontrol) | |
9b1502ef ML |
2176 | { |
2177 | struct port_data data; | |
2178 | struct sp_port_config config; | |
2179 | ||
2180 | TRACE("%p, %d", port, flowcontrol); | |
2181 | ||
2182 | CHECK_OPEN_PORT(); | |
2183 | ||
2184 | TRY(get_config(port, &data, &config)); | |
2185 | ||
2186 | TRY(sp_set_config_flowcontrol(&config, flowcontrol)); | |
2187 | ||
e33ab9aa ML |
2188 | TRY(set_config(port, &data, &config)); |
2189 | ||
c33efc48 | 2190 | RETURN_OK(); |
e33ab9aa ML |
2191 | } |
2192 | ||
970f279a AJ |
2193 | SP_API enum sp_return sp_get_signals(struct sp_port *port, |
2194 | enum sp_signal *signals) | |
8cf7c697 | 2195 | { |
c33efc48 ML |
2196 | TRACE("%p, %p", port, signals); |
2197 | ||
dec10e31 | 2198 | CHECK_OPEN_PORT(); |
8cf7c697 ML |
2199 | |
2200 | if (!signals) | |
c33efc48 | 2201 | RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); |
8cf7c697 | 2202 | |
7890cef6 | 2203 | DEBUG_FMT("Getting control signals for port %s", port->name); |
ea667be7 | 2204 | |
8cf7c697 ML |
2205 | *signals = 0; |
2206 | #ifdef _WIN32 | |
2207 | DWORD bits; | |
2208 | if (GetCommModemStatus(port->hdl, &bits) == 0) | |
c33efc48 | 2209 | RETURN_FAIL("GetCommModemStatus() failed"); |
8cf7c697 ML |
2210 | if (bits & MS_CTS_ON) |
2211 | *signals |= SP_SIG_CTS; | |
2212 | if (bits & MS_DSR_ON) | |
2213 | *signals |= SP_SIG_DSR; | |
8cf7c697 | 2214 | if (bits & MS_RLSD_ON) |
a6cda1e8 ML |
2215 | *signals |= SP_SIG_DCD; |
2216 | if (bits & MS_RING_ON) | |
8cf7c697 ML |
2217 | *signals |= SP_SIG_RI; |
2218 | #else | |
2219 | int bits; | |
2220 | if (ioctl(port->fd, TIOCMGET, &bits) < 0) | |
c33efc48 | 2221 | RETURN_FAIL("TIOCMGET ioctl failed"); |
8cf7c697 ML |
2222 | if (bits & TIOCM_CTS) |
2223 | *signals |= SP_SIG_CTS; | |
2224 | if (bits & TIOCM_DSR) | |
2225 | *signals |= SP_SIG_DSR; | |
2226 | if (bits & TIOCM_CAR) | |
2227 | *signals |= SP_SIG_DCD; | |
2228 | if (bits & TIOCM_RNG) | |
2229 | *signals |= SP_SIG_RI; | |
2230 | #endif | |
c33efc48 | 2231 | RETURN_OK(); |
8cf7c697 ML |
2232 | } |
2233 | ||
970f279a | 2234 | SP_API enum sp_return sp_start_break(struct sp_port *port) |
90cc3ee6 | 2235 | { |
c33efc48 ML |
2236 | TRACE("%p", port); |
2237 | ||
dec10e31 | 2238 | CHECK_OPEN_PORT(); |
90cc3ee6 ML |
2239 | #ifdef _WIN32 |
2240 | if (SetCommBreak(port->hdl) == 0) | |
c33efc48 | 2241 | RETURN_FAIL("SetCommBreak() failed"); |
90cc3ee6 ML |
2242 | #else |
2243 | if (ioctl(port->fd, TIOCSBRK, 1) < 0) | |
c33efc48 | 2244 | RETURN_FAIL("TIOCSBRK ioctl failed"); |
90cc3ee6 ML |
2245 | #endif |
2246 | ||
c33efc48 | 2247 | RETURN_OK(); |
90cc3ee6 ML |
2248 | } |
2249 | ||
970f279a | 2250 | SP_API enum sp_return sp_end_break(struct sp_port *port) |
90cc3ee6 | 2251 | { |
c33efc48 ML |
2252 | TRACE("%p", port); |
2253 | ||
dec10e31 | 2254 | CHECK_OPEN_PORT(); |
90cc3ee6 ML |
2255 | #ifdef _WIN32 |
2256 | if (ClearCommBreak(port->hdl) == 0) | |
c33efc48 | 2257 | RETURN_FAIL("ClearCommBreak() failed"); |
90cc3ee6 ML |
2258 | #else |
2259 | if (ioctl(port->fd, TIOCCBRK, 1) < 0) | |
c33efc48 | 2260 | RETURN_FAIL("TIOCCBRK ioctl failed"); |
90cc3ee6 ML |
2261 | #endif |
2262 | ||
c33efc48 | 2263 | RETURN_OK(); |
90cc3ee6 ML |
2264 | } |
2265 | ||
970f279a | 2266 | SP_API int sp_last_error_code(void) |
74510d4b | 2267 | { |
7890cef6 | 2268 | TRACE_VOID(); |
74510d4b | 2269 | #ifdef _WIN32 |
9caa2e86 | 2270 | RETURN_INT(GetLastError()); |
74510d4b | 2271 | #else |
9caa2e86 | 2272 | RETURN_INT(errno); |
74510d4b ML |
2273 | #endif |
2274 | } | |
2275 | ||
970f279a | 2276 | SP_API char *sp_last_error_message(void) |
74510d4b | 2277 | { |
7890cef6 | 2278 | TRACE_VOID(); |
c33efc48 | 2279 | |
74510d4b ML |
2280 | #ifdef _WIN32 |
2281 | LPVOID message; | |
2282 | DWORD error = GetLastError(); | |
2283 | ||
2284 | FormatMessage( | |
2285 | FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
2286 | FORMAT_MESSAGE_FROM_SYSTEM | | |
2287 | FORMAT_MESSAGE_IGNORE_INSERTS, | |
2288 | NULL, | |
2289 | error, | |
2290 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
2291 | (LPTSTR) &message, | |
2292 | 0, NULL ); | |
2293 | ||
9caa2e86 | 2294 | RETURN_STRING(message); |
74510d4b | 2295 | #else |
9caa2e86 | 2296 | RETURN_STRING(strerror(errno)); |
74510d4b ML |
2297 | #endif |
2298 | } | |
2299 | ||
970f279a | 2300 | SP_API void sp_free_error_message(char *message) |
74510d4b | 2301 | { |
c33efc48 ML |
2302 | TRACE("%s", message); |
2303 | ||
74510d4b ML |
2304 | #ifdef _WIN32 |
2305 | LocalFree(message); | |
64eec30d ML |
2306 | #else |
2307 | (void)message; | |
74510d4b | 2308 | #endif |
c33efc48 ML |
2309 | |
2310 | RETURN(); | |
74510d4b | 2311 | } |
863b35e6 | 2312 | |
970f279a | 2313 | SP_API void sp_set_debug_handler(void (*handler)(const char *format, ...)) |
863b35e6 | 2314 | { |
c33efc48 ML |
2315 | TRACE("%p", handler); |
2316 | ||
863b35e6 | 2317 | sp_debug_handler = handler; |
c33efc48 ML |
2318 | |
2319 | RETURN(); | |
863b35e6 ML |
2320 | } |
2321 | ||
970f279a | 2322 | SP_API void sp_default_debug_handler(const char *format, ...) |
863b35e6 ML |
2323 | { |
2324 | va_list args; | |
2325 | va_start(args, format); | |
2326 | if (getenv("LIBSERIALPORT_DEBUG")) { | |
dd7742fb | 2327 | fputs("sp: ", stderr); |
863b35e6 ML |
2328 | vfprintf(stderr, format, args); |
2329 | } | |
2330 | va_end(args); | |
2331 | } | |
524b0e14 | 2332 | |
970f279a | 2333 | SP_API int sp_get_major_package_version(void) |
524b0e14 UH |
2334 | { |
2335 | return SP_PACKAGE_VERSION_MAJOR; | |
2336 | } | |
2337 | ||
970f279a | 2338 | SP_API int sp_get_minor_package_version(void) |
524b0e14 UH |
2339 | { |
2340 | return SP_PACKAGE_VERSION_MINOR; | |
2341 | } | |
2342 | ||
970f279a | 2343 | SP_API int sp_get_micro_package_version(void) |
524b0e14 UH |
2344 | { |
2345 | return SP_PACKAGE_VERSION_MICRO; | |
2346 | } | |
2347 | ||
970f279a | 2348 | SP_API const char *sp_get_package_version_string(void) |
524b0e14 UH |
2349 | { |
2350 | return SP_PACKAGE_VERSION_STRING; | |
2351 | } | |
2352 | ||
970f279a | 2353 | SP_API int sp_get_current_lib_version(void) |
524b0e14 UH |
2354 | { |
2355 | return SP_LIB_VERSION_CURRENT; | |
2356 | } | |
2357 | ||
970f279a | 2358 | SP_API int sp_get_revision_lib_version(void) |
524b0e14 UH |
2359 | { |
2360 | return SP_LIB_VERSION_REVISION; | |
2361 | } | |
2362 | ||
970f279a | 2363 | SP_API int sp_get_age_lib_version(void) |
524b0e14 UH |
2364 | { |
2365 | return SP_LIB_VERSION_AGE; | |
2366 | } | |
2367 | ||
970f279a | 2368 | SP_API const char *sp_get_lib_version_string(void) |
524b0e14 UH |
2369 | { |
2370 | return SP_LIB_VERSION_STRING; | |
2371 | } | |
2372 | ||
2373 | /** @} */ |