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