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