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