]>
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> | |
74510d4b ML |
30 | #ifdef _WIN32 |
31 | #include <windows.h> | |
3b63f34d | 32 | #include <tchar.h> |
767c5ba8 | 33 | #include <stdio.h> |
74510d4b | 34 | #else |
74510d4b ML |
35 | #include <termios.h> |
36 | #include <sys/ioctl.h> | |
37 | #endif | |
3b63f34d | 38 | #ifdef __APPLE__ |
1ebf4347 ML |
39 | #include <IOKit/IOKitLib.h> |
40 | #include <IOKit/serial/IOSerialKeys.h> | |
31b3a8f5 | 41 | #include <IOKit/serial/ioss.h> |
1ebf4347 | 42 | #include <sys/syslimits.h> |
3b63f34d ML |
43 | #endif |
44 | #ifdef __linux__ | |
45 | #include "libudev.h" | |
4b97c9fc | 46 | #include "linux/serial.h" |
7a6d2196 | 47 | #include "linux_termios.h" |
40978c2b ML |
48 | #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX) |
49 | #define USE_TERMIOX | |
50 | #endif | |
3b63f34d | 51 | #endif |
74510d4b | 52 | |
f6a1fb65 | 53 | #include "libserialport.h" |
74510d4b | 54 | |
8f189c4c | 55 | struct port_data { |
8094e4a0 ML |
56 | #ifdef _WIN32 |
57 | DCB dcb; | |
58 | #else | |
59 | struct termios term; | |
824dcb45 | 60 | int controlbits; |
40978c2b ML |
61 | #ifdef USE_TERMIOX |
62 | int flow; | |
63 | #endif | |
8094e4a0 ML |
64 | #endif |
65 | }; | |
66 | ||
da2748bf ML |
67 | /* Standard baud rates. */ |
68 | #ifdef _WIN32 | |
69 | #define BAUD_TYPE DWORD | |
70 | #define BAUD(n) {CBR_##n, n} | |
71 | #else | |
72 | #define BAUD_TYPE speed_t | |
73 | #define BAUD(n) {B##n, n} | |
74 | #endif | |
75 | ||
76 | struct std_baudrate { | |
77 | BAUD_TYPE index; | |
78 | int value; | |
79 | }; | |
80 | ||
81 | const struct std_baudrate std_baudrates[] = { | |
82 | #ifdef _WIN32 | |
83 | /* | |
84 | * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to | |
85 | * have documented CBR_* macros. | |
86 | */ | |
87 | BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800), | |
88 | BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600), | |
eac329d2 | 89 | BAUD(115200), BAUD(128000), BAUD(256000), |
da2748bf | 90 | #else |
eac329d2 UH |
91 | BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200), |
92 | BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800), | |
93 | BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200), | |
94 | BAUD(230400), | |
da2748bf | 95 | #if !defined(__APPLE__) && !defined(__OpenBSD__) |
eac329d2 | 96 | BAUD(460800), |
da2748bf ML |
97 | #endif |
98 | #endif | |
99 | }; | |
100 | ||
101 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) | |
102 | #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates) | |
103 | ||
7a6d2196 ML |
104 | #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0) |
105 | ||
348e23cc | 106 | /* Helper functions. */ |
eb6ed20f | 107 | static enum sp_return validate_port(struct sp_port *port); |
348e23cc | 108 | static struct sp_port **list_append(struct sp_port **list, const char *portname); |
eb6ed20f ML |
109 | static enum sp_return get_config(struct sp_port *port, struct port_data *data, |
110 | struct sp_port_config *config); | |
111 | static enum sp_return set_config(struct sp_port *port, struct port_data *data, | |
112 | const struct sp_port_config *config); | |
80186526 | 113 | |
eb6ed20f | 114 | enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr) |
d54e9004 ML |
115 | { |
116 | struct sp_port *port; | |
5919c913 | 117 | int len; |
d54e9004 | 118 | |
32b5ac05 ML |
119 | if (!port_ptr) |
120 | return SP_ERR_ARG; | |
121 | ||
77f262c4 ML |
122 | *port_ptr = NULL; |
123 | ||
d4babed2 | 124 | if (!portname) |
77f262c4 | 125 | return SP_ERR_ARG; |
d4babed2 | 126 | |
d54e9004 | 127 | if (!(port = malloc(sizeof(struct sp_port)))) |
77f262c4 | 128 | return SP_ERR_MEM; |
d54e9004 | 129 | |
5919c913 ML |
130 | len = strlen(portname) + 1; |
131 | ||
eac329d2 | 132 | if (!(port->name = malloc(len))) { |
d54e9004 | 133 | free(port); |
77f262c4 | 134 | return SP_ERR_MEM; |
d54e9004 ML |
135 | } |
136 | ||
137 | memcpy(port->name, portname, len); | |
138 | ||
8f471c66 ML |
139 | #ifdef _WIN32 |
140 | port->hdl = INVALID_HANDLE_VALUE; | |
141 | #else | |
142 | port->fd = -1; | |
143 | #endif | |
144 | ||
77f262c4 ML |
145 | *port_ptr = port; |
146 | ||
147 | return SP_OK; | |
d54e9004 ML |
148 | } |
149 | ||
eb6ed20f | 150 | enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr) |
32b5ac05 ML |
151 | { |
152 | if (!copy_ptr) | |
153 | return SP_ERR_ARG; | |
154 | ||
155 | *copy_ptr = NULL; | |
156 | ||
157 | if (!port || !port->name) | |
158 | return SP_ERR_ARG; | |
159 | ||
160 | return sp_get_port_by_name(port->name, copy_ptr); | |
161 | } | |
162 | ||
e3b2f7a4 ML |
163 | void sp_free_port(struct sp_port *port) |
164 | { | |
165 | if (!port) | |
166 | return; | |
167 | ||
168 | if (port->name) | |
169 | free(port->name); | |
170 | ||
171 | free(port); | |
172 | } | |
173 | ||
348e23cc | 174 | static struct sp_port **list_append(struct sp_port **list, const char *portname) |
3b63f34d ML |
175 | { |
176 | void *tmp; | |
177 | unsigned int count; | |
f92f1f0c | 178 | |
3b63f34d | 179 | for (count = 0; list[count]; count++); |
d54e9004 | 180 | if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2)))) |
3b63f34d ML |
181 | goto fail; |
182 | list = tmp; | |
77f262c4 | 183 | if (sp_get_port_by_name(portname, &list[count]) != SP_OK) |
3b63f34d | 184 | goto fail; |
db2794ce | 185 | list[count + 1] = NULL; |
3b63f34d | 186 | return list; |
f92f1f0c | 187 | |
3b63f34d ML |
188 | fail: |
189 | sp_free_port_list(list); | |
190 | return NULL; | |
191 | } | |
192 | ||
eb6ed20f | 193 | enum sp_return sp_list_ports(struct sp_port ***list_ptr) |
3b63f34d | 194 | { |
d54e9004 | 195 | struct sp_port **list; |
77f262c4 | 196 | int ret = SP_OK; |
24c1a4bb | 197 | |
d54e9004 | 198 | if (!(list = malloc(sizeof(struct sp_port **)))) |
f92f1f0c | 199 | return SP_ERR_MEM; |
24c1a4bb ML |
200 | |
201 | list[0] = NULL; | |
3b63f34d ML |
202 | |
203 | #ifdef _WIN32 | |
204 | HKEY key; | |
bdfb5b8c ML |
205 | TCHAR *value, *data; |
206 | DWORD max_value_len, max_data_size, max_data_len; | |
207 | DWORD value_len, data_size, data_len; | |
3b63f34d | 208 | DWORD type, index = 0; |
8b532d9c ML |
209 | char *name; |
210 | int name_len; | |
3b63f34d ML |
211 | |
212 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), | |
eac329d2 | 213 | 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) { |
77f262c4 ML |
214 | ret = SP_ERR_FAIL; |
215 | goto out_done; | |
216 | } | |
3b63f34d | 217 | if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
eac329d2 | 218 | &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) { |
77f262c4 | 219 | ret = SP_ERR_FAIL; |
3b63f34d | 220 | goto out_close; |
77f262c4 | 221 | } |
3b63f34d | 222 | max_data_len = max_data_size / sizeof(TCHAR); |
eac329d2 | 223 | if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) { |
77f262c4 | 224 | ret = SP_ERR_MEM; |
3b63f34d | 225 | goto out_close; |
77f262c4 | 226 | } |
eac329d2 | 227 | if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) { |
77f262c4 | 228 | ret = SP_ERR_MEM; |
bdfb5b8c | 229 | goto out_free_value; |
77f262c4 | 230 | } |
3b63f34d | 231 | while ( |
d9573bad | 232 | value_len = max_value_len + 1, |
3b63f34d | 233 | data_size = max_data_size, |
bdfb5b8c | 234 | RegEnumValue(key, index, value, &value_len, |
3b63f34d ML |
235 | NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS) |
236 | { | |
237 | data_len = data_size / sizeof(TCHAR); | |
238 | data[data_len] = '\0'; | |
8b532d9c ML |
239 | #ifdef UNICODE |
240 | name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL) | |
241 | #else | |
242 | name_len = data_len + 1; | |
243 | #endif | |
eac329d2 | 244 | if (!(name = malloc(name_len))) { |
77f262c4 | 245 | ret = SP_ERR_MEM; |
8b532d9c | 246 | goto out; |
77f262c4 | 247 | } |
8b532d9c ML |
248 | #ifdef UNICODE |
249 | WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL); | |
250 | #else | |
251 | strcpy(name, data); | |
252 | #endif | |
eac329d2 | 253 | if (type == REG_SZ && !(list = list_append(list, name))) { |
77f262c4 ML |
254 | ret = SP_ERR_MEM; |
255 | goto out; | |
256 | } | |
3b63f34d ML |
257 | index++; |
258 | } | |
259 | out: | |
260 | free(data); | |
bdfb5b8c ML |
261 | out_free_value: |
262 | free(value); | |
3b63f34d ML |
263 | out_close: |
264 | RegCloseKey(key); | |
77f262c4 | 265 | out_done: |
3b63f34d ML |
266 | #endif |
267 | #ifdef __APPLE__ | |
268 | mach_port_t master; | |
269 | CFMutableDictionaryRef classes; | |
270 | io_iterator_t iter; | |
271 | char *path; | |
272 | io_object_t port; | |
273 | CFTypeRef cf_path; | |
274 | Boolean result; | |
275 | ||
eac329d2 | 276 | if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) { |
77f262c4 ML |
277 | ret = SP_ERR_FAIL; |
278 | goto out_done; | |
279 | } | |
3b63f34d | 280 | |
eac329d2 | 281 | if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) { |
77f262c4 ML |
282 | ret = SP_ERR_FAIL; |
283 | goto out_done; | |
284 | } | |
3b63f34d ML |
285 | |
286 | CFDictionarySetValue(classes, | |
287 | CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes)); | |
288 | ||
eac329d2 | 289 | if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) { |
77f262c4 ML |
290 | ret = SP_ERR_FAIL; |
291 | goto out_done; | |
292 | } | |
3b63f34d | 293 | |
eac329d2 | 294 | if (!(path = malloc(PATH_MAX))) { |
77f262c4 | 295 | ret = SP_ERR_MEM; |
3b63f34d | 296 | goto out_release; |
77f262c4 | 297 | } |
3b63f34d | 298 | |
1ebf4347 | 299 | while ((port = IOIteratorNext(iter))) { |
3b63f34d ML |
300 | cf_path = IORegistryEntryCreateCFProperty(port, |
301 | CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0); | |
302 | if (cf_path) { | |
303 | result = CFStringGetCString(cf_path, | |
304 | path, PATH_MAX, kCFStringEncodingASCII); | |
305 | CFRelease(cf_path); | |
eac329d2 | 306 | if (result && !(list = list_append(list, path))) { |
77f262c4 ML |
307 | ret = SP_ERR_MEM; |
308 | IOObjectRelease(port); | |
309 | goto out; | |
310 | } | |
3b63f34d ML |
311 | } |
312 | IOObjectRelease(port); | |
313 | } | |
3b63f34d ML |
314 | out: |
315 | free(path); | |
316 | out_release: | |
317 | IOObjectRelease(iter); | |
77f262c4 | 318 | out_done: |
3b63f34d ML |
319 | #endif |
320 | #ifdef __linux__ | |
321 | struct udev *ud; | |
322 | struct udev_enumerate *ud_enumerate; | |
323 | struct udev_list_entry *ud_list; | |
324 | struct udev_list_entry *ud_entry; | |
325 | const char *path; | |
08fe0bdb | 326 | struct udev_device *ud_dev, *ud_parent; |
3b63f34d | 327 | const char *name; |
4b97c9fc ML |
328 | const char *driver; |
329 | int fd, ioctl_result; | |
330 | struct serial_struct serial_info; | |
3b63f34d ML |
331 | |
332 | ud = udev_new(); | |
333 | ud_enumerate = udev_enumerate_new(ud); | |
334 | udev_enumerate_add_match_subsystem(ud_enumerate, "tty"); | |
335 | udev_enumerate_scan_devices(ud_enumerate); | |
336 | ud_list = udev_enumerate_get_list_entry(ud_enumerate); | |
eac329d2 | 337 | udev_list_entry_foreach(ud_entry, ud_list) { |
3b63f34d ML |
338 | path = udev_list_entry_get_name(ud_entry); |
339 | ud_dev = udev_device_new_from_syspath(ud, path); | |
08fe0bdb ML |
340 | /* If there is no parent device, this is a virtual tty. */ |
341 | ud_parent = udev_device_get_parent(ud_dev); | |
eac329d2 | 342 | if (ud_parent == NULL) { |
08fe0bdb ML |
343 | udev_device_unref(ud_dev); |
344 | continue; | |
345 | } | |
3b63f34d | 346 | name = udev_device_get_devnode(ud_dev); |
4b97c9fc ML |
347 | /* The serial8250 driver has a hardcoded number of ports. |
348 | * The only way to tell which actually exist on a given system | |
349 | * is to try to open them and make an ioctl call. */ | |
350 | driver = udev_device_get_driver(ud_parent); | |
eac329d2 | 351 | if (driver && !strcmp(driver, "serial8250")) { |
4b97c9fc ML |
352 | if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) |
353 | goto skip; | |
354 | ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info); | |
355 | close(fd); | |
356 | if (ioctl_result != 0) | |
357 | goto skip; | |
358 | if (serial_info.type == PORT_UNKNOWN) | |
359 | goto skip; | |
360 | } | |
348e23cc | 361 | list = list_append(list, name); |
4b97c9fc | 362 | skip: |
3b63f34d | 363 | udev_device_unref(ud_dev); |
eac329d2 | 364 | if (!list) { |
77f262c4 | 365 | ret = SP_ERR_MEM; |
3b63f34d | 366 | goto out; |
77f262c4 | 367 | } |
3b63f34d ML |
368 | } |
369 | out: | |
370 | udev_enumerate_unref(ud_enumerate); | |
371 | udev_unref(ud); | |
3b63f34d | 372 | #endif |
77f262c4 | 373 | |
eac329d2 | 374 | if (ret == SP_OK) { |
77f262c4 | 375 | *list_ptr = list; |
eac329d2 | 376 | } else { |
77f262c4 ML |
377 | if (list) |
378 | sp_free_port_list(list); | |
77f262c4 ML |
379 | *list_ptr = NULL; |
380 | } | |
381 | ||
382 | return ret; | |
3b63f34d ML |
383 | } |
384 | ||
d54e9004 | 385 | void sp_free_port_list(struct sp_port **list) |
3b63f34d ML |
386 | { |
387 | unsigned int i; | |
f92f1f0c | 388 | |
3b63f34d | 389 | for (i = 0; list[i]; i++) |
e3b2f7a4 | 390 | sp_free_port(list[i]); |
3b63f34d ML |
391 | free(list); |
392 | } | |
393 | ||
eb6ed20f | 394 | static enum sp_return validate_port(struct sp_port *port) |
74510d4b ML |
395 | { |
396 | if (port == NULL) | |
397 | return 0; | |
398 | #ifdef _WIN32 | |
399 | if (port->hdl == INVALID_HANDLE_VALUE) | |
400 | return 0; | |
401 | #else | |
402 | if (port->fd < 0) | |
403 | return 0; | |
404 | #endif | |
405 | return 1; | |
406 | } | |
407 | ||
348e23cc | 408 | #define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0) |
74510d4b | 409 | |
eb6ed20f | 410 | enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) |
74510d4b ML |
411 | { |
412 | if (!port) | |
413 | return SP_ERR_ARG; | |
414 | ||
74510d4b ML |
415 | #ifdef _WIN32 |
416 | DWORD desired_access = 0, flags_and_attributes = 0; | |
99945a1f ML |
417 | char *escaped_port_name; |
418 | ||
419 | /* Prefix port name with '\\.\' to work with ports above COM9. */ | |
420 | if (!(escaped_port_name = malloc(strlen(port->name + 5)))) | |
421 | return SP_ERR_MEM; | |
422 | sprintf(escaped_port_name, "\\\\.\\%s", port->name); | |
423 | ||
74510d4b ML |
424 | /* Map 'flags' to the OS-specific settings. */ |
425 | desired_access |= GENERIC_READ; | |
426 | flags_and_attributes = FILE_ATTRIBUTE_NORMAL; | |
427 | if (flags & SP_MODE_RDWR) | |
428 | desired_access |= GENERIC_WRITE; | |
429 | if (flags & SP_MODE_NONBLOCK) | |
430 | flags_and_attributes |= FILE_FLAG_OVERLAPPED; | |
431 | ||
99945a1f | 432 | port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0, |
74510d4b | 433 | OPEN_EXISTING, flags_and_attributes, 0); |
99945a1f ML |
434 | |
435 | free(escaped_port_name); | |
436 | ||
74510d4b ML |
437 | if (port->hdl == INVALID_HANDLE_VALUE) |
438 | return SP_ERR_FAIL; | |
439 | #else | |
440 | int flags_local = 0; | |
8f189c4c | 441 | struct port_data data; |
e33ab9aa ML |
442 | struct sp_port_config config; |
443 | int ret; | |
f92f1f0c | 444 | |
74510d4b ML |
445 | /* Map 'flags' to the OS-specific settings. */ |
446 | if (flags & SP_MODE_RDWR) | |
447 | flags_local |= O_RDWR; | |
448 | if (flags & SP_MODE_RDONLY) | |
449 | flags_local |= O_RDONLY; | |
450 | if (flags & SP_MODE_NONBLOCK) | |
451 | flags_local |= O_NONBLOCK; | |
452 | ||
453 | if ((port->fd = open(port->name, flags_local)) < 0) | |
454 | return SP_ERR_FAIL; | |
9cb98459 | 455 | |
e33ab9aa ML |
456 | ret = get_config(port, &data, &config); |
457 | ||
eac329d2 | 458 | if (ret < 0) { |
e33ab9aa ML |
459 | sp_close(port); |
460 | return ret; | |
461 | } | |
9cb98459 ML |
462 | |
463 | /* Turn off all serial port cooking. */ | |
464 | data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL); | |
465 | data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR); | |
466 | #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) | |
467 | data.term.c_oflag &= ~OFILL; | |
468 | #endif | |
469 | /* Disable canonical mode, and don't echo input characters. */ | |
470 | data.term.c_lflag &= ~(ICANON | ECHO); | |
471 | ||
472 | /* Ignore modem status lines; enable receiver */ | |
473 | data.term.c_cflag |= (CLOCAL | CREAD); | |
474 | ||
e33ab9aa ML |
475 | ret = set_config(port, &data, &config); |
476 | ||
eac329d2 | 477 | if (ret < 0) { |
e33ab9aa ML |
478 | sp_close(port); |
479 | return ret; | |
480 | } | |
74510d4b ML |
481 | #endif |
482 | ||
483 | return SP_OK; | |
484 | } | |
485 | ||
eb6ed20f | 486 | enum sp_return sp_close(struct sp_port *port) |
74510d4b ML |
487 | { |
488 | CHECK_PORT(); | |
489 | ||
490 | #ifdef _WIN32 | |
491 | /* Returns non-zero upon success, 0 upon failure. */ | |
492 | if (CloseHandle(port->hdl) == 0) | |
493 | return SP_ERR_FAIL; | |
8f471c66 | 494 | port->hdl = INVALID_HANDLE_VALUE; |
74510d4b ML |
495 | #else |
496 | /* Returns 0 upon success, -1 upon failure. */ | |
497 | if (close(port->fd) == -1) | |
498 | return SP_ERR_FAIL; | |
8f471c66 | 499 | port->fd = -1; |
74510d4b ML |
500 | #endif |
501 | ||
502 | return SP_OK; | |
503 | } | |
504 | ||
eb6ed20f | 505 | enum sp_return sp_flush(struct sp_port *port) |
74510d4b ML |
506 | { |
507 | CHECK_PORT(); | |
508 | ||
509 | #ifdef _WIN32 | |
510 | /* Returns non-zero upon success, 0 upon failure. */ | |
511 | if (PurgeComm(port->hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) | |
512 | return SP_ERR_FAIL; | |
513 | #else | |
514 | /* Returns 0 upon success, -1 upon failure. */ | |
515 | if (tcflush(port->fd, TCIOFLUSH) < 0) | |
516 | return SP_ERR_FAIL; | |
517 | #endif | |
518 | return SP_OK; | |
519 | } | |
520 | ||
eb6ed20f | 521 | enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count) |
74510d4b ML |
522 | { |
523 | CHECK_PORT(); | |
524 | ||
525 | if (!buf) | |
526 | return SP_ERR_ARG; | |
527 | ||
528 | #ifdef _WIN32 | |
529 | DWORD written = 0; | |
f92f1f0c | 530 | |
74510d4b ML |
531 | /* Returns non-zero upon success, 0 upon failure. */ |
532 | if (WriteFile(port->hdl, buf, count, &written, NULL) == 0) | |
533 | return SP_ERR_FAIL; | |
534 | return written; | |
535 | #else | |
536 | /* Returns the number of bytes written, or -1 upon failure. */ | |
537 | ssize_t written = write(port->fd, buf, count); | |
f92f1f0c | 538 | |
74510d4b ML |
539 | if (written < 0) |
540 | return SP_ERR_FAIL; | |
541 | else | |
f92f1f0c | 542 | return written; |
74510d4b ML |
543 | #endif |
544 | } | |
545 | ||
eb6ed20f | 546 | enum sp_return sp_read(struct sp_port *port, void *buf, size_t count) |
74510d4b ML |
547 | { |
548 | CHECK_PORT(); | |
549 | ||
550 | if (!buf) | |
551 | return SP_ERR_ARG; | |
552 | ||
553 | #ifdef _WIN32 | |
554 | DWORD bytes_read = 0; | |
f92f1f0c | 555 | |
74510d4b ML |
556 | /* Returns non-zero upon success, 0 upon failure. */ |
557 | if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0) | |
558 | return SP_ERR_FAIL; | |
559 | return bytes_read; | |
560 | #else | |
561 | ssize_t bytes_read; | |
f92f1f0c | 562 | |
74510d4b ML |
563 | /* Returns the number of bytes read, or -1 upon failure. */ |
564 | if ((bytes_read = read(port->fd, buf, count)) < 0) | |
565 | return SP_ERR_FAIL; | |
566 | return bytes_read; | |
567 | #endif | |
568 | } | |
569 | ||
7a6d2196 ML |
570 | #ifdef __linux__ |
571 | static enum sp_return get_baudrate(int fd, int *baudrate) | |
572 | { | |
573 | void *data; | |
574 | ||
575 | if (!(data = malloc(get_termios_size()))) | |
576 | return SP_ERR_MEM; | |
577 | ||
578 | if (ioctl(fd, get_termios_get_ioctl(), data) < 0) | |
579 | return SP_ERR_FAIL; | |
580 | ||
581 | *baudrate = get_termios_speed(data); | |
582 | ||
583 | return SP_OK; | |
584 | } | |
585 | ||
586 | static enum sp_return set_baudrate(int fd, int baudrate) | |
587 | { | |
588 | void *data; | |
589 | ||
590 | if (!(data = malloc(get_termios_size()))) | |
591 | return SP_ERR_MEM; | |
592 | ||
593 | if (ioctl(fd, get_termios_get_ioctl(), data) < 0) | |
594 | return SP_ERR_FAIL; | |
595 | ||
596 | set_termios_speed(data, baudrate); | |
597 | ||
598 | if (ioctl(fd, get_termios_set_ioctl(), data) < 0) | |
599 | return SP_ERR_FAIL; | |
600 | ||
601 | return SP_OK; | |
602 | } | |
40978c2b ML |
603 | |
604 | #ifdef USE_TERMIOX | |
605 | static enum sp_return get_flow(int fd, int *flow) | |
606 | { | |
607 | void *data; | |
608 | ||
609 | if (!(data = malloc(get_termiox_size()))) | |
610 | return SP_ERR_MEM; | |
611 | ||
612 | if (ioctl(fd, TCGETX, data) < 0) | |
613 | return SP_ERR_FAIL; | |
614 | ||
615 | *flow = get_termiox_flow(data); | |
616 | ||
617 | return SP_OK; | |
618 | } | |
619 | ||
620 | static enum sp_return set_flow(int fd, int flow) | |
621 | { | |
622 | void *data; | |
623 | ||
624 | if (!(data = malloc(get_termiox_size()))) | |
625 | return SP_ERR_MEM; | |
626 | ||
627 | if (ioctl(fd, TCGETX, data) < 0) | |
628 | return SP_ERR_FAIL; | |
629 | ||
630 | set_termiox_flow(data, flow); | |
631 | ||
632 | if (ioctl(fd, TCSETX, data) < 0) | |
633 | return SP_ERR_FAIL; | |
634 | ||
635 | return SP_OK; | |
636 | } | |
637 | #endif /* USE_TERMIOX */ | |
638 | #endif /* __linux__ */ | |
7a6d2196 | 639 | |
eb6ed20f ML |
640 | static enum sp_return get_config(struct sp_port *port, struct port_data *data, |
641 | struct sp_port_config *config) | |
8094e4a0 | 642 | { |
da2748bf | 643 | unsigned int i; |
cbf628c7 | 644 | |
8094e4a0 | 645 | #ifdef _WIN32 |
e33ab9aa | 646 | if (!GetCommState(port->hdl, &data->dcb)) |
d514a26f | 647 | return SP_ERR_FAIL; |
8094e4a0 | 648 | |
067417af | 649 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
e33ab9aa | 650 | if (data->dcb.BaudRate == std_baudrates[i].index) { |
067417af ML |
651 | config->baudrate = std_baudrates[i].value; |
652 | break; | |
653 | } | |
654 | } | |
655 | ||
656 | if (i == NUM_STD_BAUDRATES) | |
657 | /* BaudRate field can be either an index or a custom baud rate. */ | |
e33ab9aa | 658 | config->baudrate = data->dcb.BaudRate; |
067417af | 659 | |
e33ab9aa | 660 | config->bits = data->dcb.ByteSize; |
067417af | 661 | |
e33ab9aa ML |
662 | if (data->dcb.fParity) |
663 | switch (data->dcb.Parity) { | |
067417af ML |
664 | case NOPARITY: |
665 | config->parity = SP_PARITY_NONE; | |
666 | break; | |
667 | case EVENPARITY: | |
668 | config->parity = SP_PARITY_EVEN; | |
669 | break; | |
670 | case ODDPARITY: | |
671 | config->parity = SP_PARITY_ODD; | |
672 | break; | |
673 | default: | |
674 | config->parity = -1; | |
675 | } | |
676 | else | |
677 | config->parity = SP_PARITY_NONE; | |
678 | ||
e33ab9aa | 679 | switch (data->dcb.StopBits) { |
067417af ML |
680 | case ONESTOPBIT: |
681 | config->stopbits = 1; | |
682 | break; | |
683 | case TWOSTOPBITS: | |
684 | config->stopbits = 2; | |
685 | break; | |
686 | default: | |
687 | config->stopbits = -1; | |
688 | } | |
689 | ||
e33ab9aa | 690 | switch (data->dcb.fRtsControl) { |
eac329d2 UH |
691 | case RTS_CONTROL_DISABLE: |
692 | config->rts = SP_RTS_OFF; | |
693 | break; | |
694 | case RTS_CONTROL_ENABLE: | |
695 | config->rts = SP_RTS_ON; | |
696 | break; | |
697 | case RTS_CONTROL_HANDSHAKE: | |
698 | config->rts = SP_RTS_FLOW_CONTROL; | |
699 | break; | |
700 | default: | |
701 | config->rts = -1; | |
067417af ML |
702 | } |
703 | ||
e33ab9aa | 704 | config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE; |
067417af | 705 | |
e33ab9aa | 706 | switch (data->dcb.fDtrControl) { |
eac329d2 UH |
707 | case DTR_CONTROL_DISABLE: |
708 | config->dtr = SP_DTR_OFF; | |
709 | break; | |
710 | case DTR_CONTROL_ENABLE: | |
711 | config->dtr = SP_DTR_ON; | |
712 | break; | |
713 | case DTR_CONTROL_HANDSHAKE: | |
714 | config->dtr = SP_DTR_FLOW_CONTROL; | |
715 | break; | |
716 | default: | |
717 | config->dtr = -1; | |
067417af ML |
718 | } |
719 | ||
e33ab9aa ML |
720 | config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE; |
721 | ||
c6754b45 ML |
722 | if (data->dcb.fInX) { |
723 | if (data->dcb.fOutX) | |
724 | config->xon_xoff = SP_XONXOFF_INOUT; | |
725 | else | |
726 | config->xon_xoff = SP_XONXOFF_IN; | |
727 | } else { | |
728 | if (data->dcb.fOutX) | |
729 | config->xon_xoff = SP_XONXOFF_OUT; | |
730 | else | |
731 | config->xon_xoff = SP_XONXOFF_DISABLED; | |
732 | } | |
733 | ||
e33ab9aa ML |
734 | #else // !_WIN32 |
735 | ||
736 | if (tcgetattr(port->fd, &data->term) < 0) | |
737 | return SP_ERR_FAIL; | |
738 | ||
739 | if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0) | |
740 | return SP_ERR_FAIL; | |
40978c2b ML |
741 | |
742 | #ifdef USE_TERMIOX | |
743 | TRY(get_flow(port->fd, &data->flow)); | |
744 | #endif | |
745 | ||
067417af | 746 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
e33ab9aa | 747 | if (cfgetispeed(&data->term) == std_baudrates[i].index) { |
067417af ML |
748 | config->baudrate = std_baudrates[i].value; |
749 | break; | |
750 | } | |
751 | } | |
752 | ||
31b3a8f5 MH |
753 | if (i == NUM_STD_BAUDRATES) { |
754 | #ifdef __APPLE__ | |
755 | config->baudrate = (int)data->term.c_ispeed; | |
7a6d2196 ML |
756 | #elif defined(__linux__) |
757 | TRY(get_baudrate(port->fd, &config->baudrate)); | |
31b3a8f5 | 758 | #else |
067417af | 759 | config->baudrate = -1; |
31b3a8f5 MH |
760 | #endif |
761 | } | |
067417af | 762 | |
e33ab9aa | 763 | switch (data->term.c_cflag & CSIZE) { |
067417af ML |
764 | case CS8: |
765 | config->bits = 8; | |
766 | break; | |
767 | case CS7: | |
768 | config->bits = 7; | |
769 | break; | |
770 | case CS6: | |
771 | config->bits = 6; | |
772 | break; | |
773 | case CS5: | |
774 | config->bits = 5; | |
775 | break; | |
776 | default: | |
777 | config->bits = -1; | |
778 | } | |
779 | ||
e33ab9aa | 780 | if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR)) |
067417af | 781 | config->parity = SP_PARITY_NONE; |
e33ab9aa | 782 | else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR)) |
067417af ML |
783 | config->parity = -1; |
784 | else | |
e33ab9aa | 785 | config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN; |
067417af | 786 | |
e33ab9aa | 787 | config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1; |
067417af | 788 | |
e33ab9aa | 789 | if (data->term.c_cflag & CRTSCTS) { |
067417af ML |
790 | config->rts = SP_RTS_FLOW_CONTROL; |
791 | config->cts = SP_CTS_FLOW_CONTROL; | |
792 | } else { | |
40978c2b ML |
793 | #ifdef USE_TERMIOX |
794 | if (data->flow & RTS_FLOW) | |
795 | config->rts = SP_RTS_FLOW_CONTROL; | |
796 | else | |
797 | config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF; | |
798 | ||
799 | config->cts = (data->flow & CTS_FLOW) ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE; | |
800 | #else | |
e33ab9aa | 801 | config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF; |
067417af | 802 | config->cts = SP_CTS_IGNORE; |
40978c2b | 803 | #endif |
067417af ML |
804 | } |
805 | ||
40978c2b ML |
806 | #ifdef USE_TERMIOX |
807 | if (data->flow & DTR_FLOW) | |
808 | config->dtr = SP_DTR_FLOW_CONTROL; | |
809 | else | |
810 | config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF; | |
811 | ||
812 | config->dsr = (data->flow & DSR_FLOW) ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE; | |
813 | #else | |
e33ab9aa | 814 | config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF; |
067417af | 815 | config->dsr = SP_DSR_IGNORE; |
40978c2b | 816 | #endif |
705bdc69 | 817 | |
e29b93a5 ML |
818 | if (data->term.c_iflag & IXOFF) { |
819 | if (data->term.c_iflag & IXON) | |
820 | config->xon_xoff = SP_XONXOFF_INOUT; | |
821 | else | |
822 | config->xon_xoff = SP_XONXOFF_IN; | |
823 | } else { | |
824 | if (data->term.c_iflag & IXON) | |
825 | config->xon_xoff = SP_XONXOFF_OUT; | |
826 | else | |
827 | config->xon_xoff = SP_XONXOFF_DISABLED; | |
828 | } | |
067417af ML |
829 | #endif |
830 | ||
831 | return SP_OK; | |
832 | } | |
833 | ||
7a6d2196 | 834 | static enum sp_return set_config(struct sp_port *port, struct port_data *data, |
eb6ed20f | 835 | const struct sp_port_config *config) |
18fc2dd1 | 836 | { |
e33ab9aa | 837 | unsigned int i; |
31b3a8f5 MH |
838 | #ifdef __APPLE__ |
839 | BAUD_TYPE baud_nonstd; | |
840 | ||
841 | baud_nonstd = B0; | |
842 | #endif | |
7a6d2196 ML |
843 | #ifdef __linux__ |
844 | int baud_nonstd = 0; | |
845 | #endif | |
18fc2dd1 | 846 | |
e33ab9aa | 847 | #ifdef _WIN32 |
eac329d2 | 848 | if (config->baudrate >= 0) { |
e33ab9aa ML |
849 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
850 | if (config->baudrate == std_baudrates[i].value) { | |
851 | data->dcb.BaudRate = std_baudrates[i].index; | |
852 | break; | |
853 | } | |
854 | } | |
18fc2dd1 | 855 | |
e33ab9aa ML |
856 | if (i == NUM_STD_BAUDRATES) |
857 | data->dcb.BaudRate = config->baudrate; | |
858 | } | |
18fc2dd1 | 859 | |
e33ab9aa ML |
860 | if (config->bits >= 0) |
861 | data->dcb.ByteSize = config->bits; | |
862 | ||
863 | if (config->parity >= 0) { | |
864 | switch (config->parity) { | |
865 | /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */ | |
866 | case SP_PARITY_NONE: | |
867 | data->dcb.Parity = NOPARITY; | |
868 | break; | |
869 | case SP_PARITY_EVEN: | |
870 | data->dcb.Parity = EVENPARITY; | |
871 | break; | |
872 | case SP_PARITY_ODD: | |
873 | data->dcb.Parity = ODDPARITY; | |
874 | break; | |
875 | default: | |
876 | return SP_ERR_ARG; | |
877 | } | |
18fc2dd1 ML |
878 | } |
879 | ||
e33ab9aa ML |
880 | if (config->stopbits >= 0) { |
881 | switch (config->stopbits) { | |
882 | /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */ | |
883 | case 1: | |
884 | data->dcb.StopBits = ONESTOPBIT; | |
885 | break; | |
886 | case 2: | |
887 | data->dcb.StopBits = TWOSTOPBITS; | |
888 | break; | |
889 | default: | |
890 | return SP_ERR_ARG; | |
891 | } | |
892 | } | |
893 | ||
894 | if (config->rts >= 0) { | |
895 | switch (config->rts) { | |
896 | case SP_RTS_OFF: | |
897 | data->dcb.fRtsControl = RTS_CONTROL_DISABLE; | |
898 | break; | |
899 | case SP_RTS_ON: | |
900 | data->dcb.fRtsControl = RTS_CONTROL_ENABLE; | |
901 | break; | |
902 | case SP_RTS_FLOW_CONTROL: | |
903 | data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; | |
904 | break; | |
905 | default: | |
906 | return SP_ERR_ARG; | |
907 | } | |
908 | } | |
909 | ||
910 | if (config->cts >= 0) { | |
911 | switch (config->cts) { | |
912 | case SP_CTS_IGNORE: | |
913 | data->dcb.fOutxCtsFlow = FALSE; | |
914 | break; | |
915 | case SP_CTS_FLOW_CONTROL: | |
916 | data->dcb.fOutxCtsFlow = TRUE; | |
917 | break; | |
918 | default: | |
919 | return SP_ERR_ARG; | |
920 | } | |
921 | } | |
922 | ||
923 | if (config->dtr >= 0) { | |
924 | switch (config->dtr) { | |
925 | case SP_DTR_OFF: | |
926 | data->dcb.fDtrControl = DTR_CONTROL_DISABLE; | |
927 | break; | |
928 | case SP_DTR_ON: | |
929 | data->dcb.fDtrControl = DTR_CONTROL_ENABLE; | |
930 | break; | |
931 | case SP_DTR_FLOW_CONTROL: | |
932 | data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; | |
933 | break; | |
934 | default: | |
935 | return SP_ERR_ARG; | |
936 | } | |
937 | } | |
938 | ||
939 | if (config->dsr >= 0) { | |
940 | switch (config->dsr) { | |
941 | case SP_DSR_IGNORE: | |
942 | data->dcb.fOutxDsrFlow = FALSE; | |
943 | break; | |
944 | case SP_DSR_FLOW_CONTROL: | |
945 | data->dcb.fOutxDsrFlow = TRUE; | |
946 | break; | |
947 | default: | |
948 | return SP_ERR_ARG; | |
949 | } | |
18fc2dd1 ML |
950 | } |
951 | ||
e33ab9aa ML |
952 | if (config->xon_xoff >= 0) { |
953 | switch (config->xon_xoff) { | |
954 | case SP_XONXOFF_DISABLED: | |
955 | data->dcb.fInX = FALSE; | |
956 | data->dcb.fOutX = FALSE; | |
957 | break; | |
958 | case SP_XONXOFF_IN: | |
959 | data->dcb.fInX = TRUE; | |
960 | data->dcb.fOutX = FALSE; | |
961 | break; | |
962 | case SP_XONXOFF_OUT: | |
963 | data->dcb.fInX = FALSE; | |
964 | data->dcb.fOutX = TRUE; | |
965 | break; | |
966 | case SP_XONXOFF_INOUT: | |
967 | data->dcb.fInX = TRUE; | |
968 | data->dcb.fOutX = TRUE; | |
969 | break; | |
970 | default: | |
971 | return SP_ERR_ARG; | |
972 | } | |
973 | } | |
974 | ||
975 | if (!SetCommState(port->hdl, &data->dcb)) | |
976 | return SP_ERR_FAIL; | |
977 | ||
31b3a8f5 | 978 | #else /* !_WIN32 */ |
e33ab9aa | 979 | |
7a6d2196 ML |
980 | int controlbits; |
981 | ||
eac329d2 | 982 | if (config->baudrate >= 0) { |
e33ab9aa ML |
983 | for (i = 0; i < NUM_STD_BAUDRATES; i++) { |
984 | if (config->baudrate == std_baudrates[i].value) { | |
985 | if (cfsetospeed(&data->term, std_baudrates[i].index) < 0) | |
986 | return SP_ERR_FAIL; | |
987 | ||
988 | if (cfsetispeed(&data->term, std_baudrates[i].index) < 0) | |
989 | return SP_ERR_FAIL; | |
990 | break; | |
991 | } | |
992 | } | |
993 | ||
31b3a8f5 MH |
994 | /* Non-standard baud rate */ |
995 | if (i == NUM_STD_BAUDRATES) { | |
996 | #ifdef __APPLE__ | |
997 | /* Set "dummy" baud rate */ | |
998 | if (cfsetspeed(&data->term, B9600) < 0) | |
999 | return SP_ERR_FAIL; | |
1000 | baud_nonstd = config->baudrate; | |
7a6d2196 ML |
1001 | #elif defined(__linux__) |
1002 | baud_nonstd = 1; | |
31b3a8f5 | 1003 | #else |
e33ab9aa | 1004 | return SP_ERR_ARG; |
31b3a8f5 MH |
1005 | #endif |
1006 | } | |
e33ab9aa ML |
1007 | } |
1008 | ||
1009 | if (config->bits >= 0) { | |
1010 | data->term.c_cflag &= ~CSIZE; | |
1011 | switch (config->bits) { | |
1012 | case 8: | |
1013 | data->term.c_cflag |= CS8; | |
1014 | break; | |
1015 | case 7: | |
1016 | data->term.c_cflag |= CS7; | |
1017 | break; | |
1018 | case 6: | |
1019 | data->term.c_cflag |= CS6; | |
1020 | break; | |
23922313 UH |
1021 | case 5: |
1022 | data->term.c_cflag |= CS5; | |
1023 | break; | |
e33ab9aa ML |
1024 | default: |
1025 | return SP_ERR_ARG; | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | if (config->parity >= 0) { | |
1030 | data->term.c_iflag &= ~IGNPAR; | |
1031 | data->term.c_cflag &= ~(PARENB | PARODD); | |
1032 | switch (config->parity) { | |
1033 | case SP_PARITY_NONE: | |
1034 | data->term.c_iflag |= IGNPAR; | |
1035 | break; | |
1036 | case SP_PARITY_EVEN: | |
1037 | data->term.c_cflag |= PARENB; | |
1038 | break; | |
1039 | case SP_PARITY_ODD: | |
1040 | data->term.c_cflag |= PARENB | PARODD; | |
1041 | break; | |
1042 | default: | |
1043 | return SP_ERR_ARG; | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | if (config->stopbits >= 0) { | |
1048 | data->term.c_cflag &= ~CSTOPB; | |
1049 | switch (config->stopbits) { | |
1050 | case 1: | |
1051 | data->term.c_cflag &= ~CSTOPB; | |
1052 | break; | |
1053 | case 2: | |
1054 | data->term.c_cflag |= CSTOPB; | |
1055 | break; | |
1056 | default: | |
1057 | return SP_ERR_ARG; | |
1058 | } | |
1059 | } | |
1060 | ||
eac329d2 | 1061 | if (config->rts >= 0 || config->cts >= 0) { |
40978c2b ML |
1062 | #ifdef USE_TERMIOX |
1063 | data->flow &= ~(RTS_FLOW | CTS_FLOW); | |
1064 | switch (config->rts) { | |
1065 | case SP_RTS_OFF: | |
1066 | case SP_RTS_ON: | |
1067 | controlbits = TIOCM_RTS; | |
1068 | if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, | |
1069 | &controlbits) < 0) | |
1070 | return SP_ERR_FAIL; | |
1071 | break; | |
1072 | case SP_RTS_FLOW_CONTROL: | |
1073 | data->flow |= RTS_FLOW; | |
1074 | break; | |
1075 | default: | |
1076 | break; | |
1077 | } | |
1078 | if (config->cts == SP_CTS_FLOW_CONTROL) | |
1079 | data->flow |= CTS_FLOW; | |
e33ab9aa | 1080 | |
40978c2b ML |
1081 | if (data->flow & (RTS_FLOW | CTS_FLOW)) |
1082 | data->term.c_iflag |= CRTSCTS; | |
1083 | else | |
1084 | data->term.c_iflag &= ~CRTSCTS; | |
1085 | #else | |
1086 | /* Asymmetric use of RTS/CTS not supported. */ | |
e33ab9aa ML |
1087 | if (data->term.c_iflag & CRTSCTS) { |
1088 | /* Flow control can only be disabled for both RTS & CTS together. */ | |
1089 | if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) { | |
1090 | if (config->cts != SP_CTS_IGNORE) | |
1091 | return SP_ERR_ARG; | |
1092 | } | |
1093 | if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) { | |
1094 | if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL) | |
1095 | return SP_ERR_ARG; | |
1096 | } | |
1097 | } else { | |
1098 | /* Flow control can only be enabled for both RTS & CTS together. */ | |
1099 | if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) || | |
1100 | ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL))) | |
1101 | return SP_ERR_ARG; | |
1102 | } | |
1103 | ||
1104 | if (config->rts >= 0) { | |
1105 | if (config->rts == SP_RTS_FLOW_CONTROL) { | |
1106 | data->term.c_iflag |= CRTSCTS; | |
1107 | } else { | |
7a6d2196 | 1108 | controlbits = TIOCM_RTS; |
e33ab9aa ML |
1109 | if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, |
1110 | &controlbits) < 0) | |
1111 | return SP_ERR_FAIL; | |
1112 | } | |
1113 | } | |
40978c2b | 1114 | #endif |
e33ab9aa ML |
1115 | } |
1116 | ||
eac329d2 | 1117 | if (config->dtr >= 0 || config->dsr >= 0) { |
40978c2b ML |
1118 | #ifdef USE_TERMIOX |
1119 | data->flow &= ~(DTR_FLOW | DSR_FLOW); | |
1120 | switch (config->dtr) { | |
1121 | case SP_DTR_OFF: | |
1122 | case SP_DTR_ON: | |
1123 | controlbits = TIOCM_DTR; | |
1124 | if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, | |
1125 | &controlbits) < 0) | |
1126 | return SP_ERR_FAIL; | |
1127 | break; | |
1128 | case SP_DTR_FLOW_CONTROL: | |
1129 | data->flow |= DTR_FLOW; | |
1130 | break; | |
1131 | default: | |
1132 | break; | |
1133 | } | |
1134 | if (config->dsr == SP_DSR_FLOW_CONTROL) | |
1135 | data->flow |= DSR_FLOW; | |
1136 | #else | |
1137 | /* DTR/DSR flow control not supported. */ | |
e33ab9aa ML |
1138 | if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL) |
1139 | return SP_ERR_ARG; | |
1140 | ||
1141 | if (config->dtr >= 0) { | |
7a6d2196 | 1142 | controlbits = TIOCM_DTR; |
e33ab9aa ML |
1143 | if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, |
1144 | &controlbits) < 0) | |
1145 | return SP_ERR_FAIL; | |
1146 | } | |
40978c2b | 1147 | #endif |
e33ab9aa ML |
1148 | } |
1149 | ||
1150 | if (config->xon_xoff >= 0) { | |
1151 | data->term.c_iflag &= ~(IXON | IXOFF | IXANY); | |
1152 | switch (config->xon_xoff) { | |
1153 | case SP_XONXOFF_DISABLED: | |
1154 | break; | |
1155 | case SP_XONXOFF_IN: | |
1156 | data->term.c_iflag |= IXOFF; | |
1157 | break; | |
1158 | case SP_XONXOFF_OUT: | |
1159 | data->term.c_iflag |= IXON | IXANY; | |
1160 | break; | |
1161 | case SP_XONXOFF_INOUT: | |
1162 | data->term.c_iflag |= IXON | IXOFF | IXANY; | |
1163 | break; | |
1164 | default: | |
1165 | return SP_ERR_ARG; | |
1166 | } | |
1167 | } | |
1168 | ||
1169 | if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0) | |
1170 | return SP_ERR_FAIL; | |
31b3a8f5 MH |
1171 | |
1172 | #ifdef __APPLE__ | |
1173 | if (baud_nonstd != B0) { | |
1174 | if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1) | |
1175 | return SP_ERR_FAIL; | |
1176 | /* Set baud rates in data->term to correct, but incompatible | |
1177 | * with tcsetattr() value, same as delivered by tcgetattr(). */ | |
1178 | if (cfsetspeed(&data->term, baud_nonstd) < 0) | |
1179 | return SP_ERR_FAIL; | |
1180 | } | |
7a6d2196 ML |
1181 | #elif defined(__linux__) |
1182 | if (baud_nonstd) | |
1183 | TRY(set_baudrate(port->fd, config->baudrate)); | |
40978c2b ML |
1184 | #ifdef USE_TERMIOX |
1185 | TRY(set_flow(port->fd, data->flow)); | |
1186 | #endif | |
7a6d2196 | 1187 | #endif |
31b3a8f5 MH |
1188 | |
1189 | #endif /* !_WIN32 */ | |
e33ab9aa ML |
1190 | |
1191 | return SP_OK; | |
1192 | } | |
1193 | ||
eb6ed20f | 1194 | enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config) |
e33ab9aa | 1195 | { |
8f189c4c | 1196 | struct port_data data; |
e33ab9aa ML |
1197 | struct sp_port_config prev_config; |
1198 | ||
823690ae ML |
1199 | CHECK_PORT(); |
1200 | ||
1201 | if (!config) | |
1202 | return SP_ERR_ARG; | |
1203 | ||
e33ab9aa ML |
1204 | TRY(get_config(port, &data, &prev_config)); |
1205 | TRY(set_config(port, &data, config)); | |
74510d4b ML |
1206 | |
1207 | return SP_OK; | |
1208 | } | |
1209 | ||
eb6ed20f | 1210 | #define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \ |
8f189c4c | 1211 | struct port_data data; \ |
e33ab9aa | 1212 | struct sp_port_config config; \ |
823690ae | 1213 | CHECK_PORT(); \ |
e33ab9aa ML |
1214 | TRY(get_config(port, &data, &config)); \ |
1215 | config.x = x; \ | |
1216 | TRY(set_config(port, &data, &config)); \ | |
9069c2fb ML |
1217 | return SP_OK; \ |
1218 | } | |
1219 | ||
eb6ed20f ML |
1220 | CREATE_SETTER(baudrate, int) |
1221 | CREATE_SETTER(bits, int) | |
1222 | CREATE_SETTER(parity, enum sp_parity) | |
1223 | CREATE_SETTER(stopbits, int) | |
1224 | CREATE_SETTER(rts, enum sp_rts) | |
1225 | CREATE_SETTER(cts, enum sp_cts) | |
1226 | CREATE_SETTER(dtr, enum sp_dtr) | |
1227 | CREATE_SETTER(dsr, enum sp_dsr) | |
1228 | CREATE_SETTER(xon_xoff, enum sp_xonxoff) | |
1229 | ||
1230 | enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol) | |
e33ab9aa | 1231 | { |
8f189c4c | 1232 | struct port_data data; |
e33ab9aa ML |
1233 | struct sp_port_config config; |
1234 | ||
823690ae ML |
1235 | CHECK_PORT(); |
1236 | ||
e33ab9aa ML |
1237 | TRY(get_config(port, &data, &config)); |
1238 | ||
1239 | if (flowcontrol == SP_FLOWCONTROL_XONXOFF) | |
1240 | config.xon_xoff = SP_XONXOFF_INOUT; | |
1241 | else | |
1242 | config.xon_xoff = SP_XONXOFF_DISABLED; | |
1243 | ||
1244 | if (flowcontrol == SP_FLOWCONTROL_RTSCTS) { | |
1245 | config.rts = SP_RTS_FLOW_CONTROL; | |
1246 | config.cts = SP_CTS_FLOW_CONTROL; | |
1247 | } else { | |
1248 | if (config.rts == SP_RTS_FLOW_CONTROL) | |
1249 | config.rts = SP_RTS_ON; | |
1250 | config.cts = SP_CTS_IGNORE; | |
1251 | } | |
1252 | ||
1253 | if (flowcontrol == SP_FLOWCONTROL_DTRDSR) { | |
1254 | config.dtr = SP_DTR_FLOW_CONTROL; | |
1255 | config.dsr = SP_DSR_FLOW_CONTROL; | |
1256 | } else { | |
1257 | if (config.dtr == SP_DTR_FLOW_CONTROL) | |
1258 | config.dtr = SP_DTR_ON; | |
1259 | config.dsr = SP_DSR_IGNORE; | |
1260 | } | |
1261 | ||
1262 | TRY(set_config(port, &data, &config)); | |
1263 | ||
1264 | return SP_OK; | |
1265 | } | |
1266 | ||
74510d4b ML |
1267 | int sp_last_error_code(void) |
1268 | { | |
1269 | #ifdef _WIN32 | |
1270 | return GetLastError(); | |
1271 | #else | |
1272 | return errno; | |
1273 | #endif | |
1274 | } | |
1275 | ||
74510d4b ML |
1276 | char *sp_last_error_message(void) |
1277 | { | |
1278 | #ifdef _WIN32 | |
1279 | LPVOID message; | |
1280 | DWORD error = GetLastError(); | |
1281 | ||
1282 | FormatMessage( | |
1283 | FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
1284 | FORMAT_MESSAGE_FROM_SYSTEM | | |
1285 | FORMAT_MESSAGE_IGNORE_INSERTS, | |
1286 | NULL, | |
1287 | error, | |
1288 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
1289 | (LPTSTR) &message, | |
1290 | 0, NULL ); | |
1291 | ||
1292 | return message; | |
1293 | #else | |
1294 | return strerror(errno); | |
1295 | #endif | |
1296 | } | |
1297 | ||
74510d4b ML |
1298 | void sp_free_error_message(char *message) |
1299 | { | |
1300 | #ifdef _WIN32 | |
1301 | LocalFree(message); | |
64eec30d ML |
1302 | #else |
1303 | (void)message; | |
74510d4b ML |
1304 | #endif |
1305 | } |