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