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