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