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