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