]>
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> | |
7 | * | |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU Lesser General Public License as | |
10 | * published by the Free Software Foundation, either version 3 of the | |
11 | * License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include <string.h> | |
23 | #include <sys/types.h> | |
24 | #include <sys/stat.h> | |
25 | #include <fcntl.h> | |
26 | #include <unistd.h> | |
3b63f34d ML |
27 | #include <stdlib.h> |
28 | #include <errno.h> | |
74510d4b ML |
29 | #ifdef _WIN32 |
30 | #include <windows.h> | |
3b63f34d | 31 | #include <tchar.h> |
74510d4b | 32 | #else |
74510d4b ML |
33 | #include <termios.h> |
34 | #include <sys/ioctl.h> | |
35 | #endif | |
3b63f34d | 36 | #ifdef __APPLE__ |
1ebf4347 ML |
37 | #include <IOKit/IOKitLib.h> |
38 | #include <IOKit/serial/IOSerialKeys.h> | |
39 | #include <sys/syslimits.h> | |
3b63f34d ML |
40 | #endif |
41 | #ifdef __linux__ | |
42 | #include "libudev.h" | |
4b97c9fc | 43 | #include "linux/serial.h" |
3b63f34d | 44 | #endif |
74510d4b | 45 | |
f6a1fb65 | 46 | #include "libserialport.h" |
74510d4b | 47 | |
77f262c4 | 48 | int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr) |
d54e9004 ML |
49 | { |
50 | struct sp_port *port; | |
5919c913 | 51 | int len; |
d54e9004 | 52 | |
32b5ac05 ML |
53 | if (!port_ptr) |
54 | return SP_ERR_ARG; | |
55 | ||
77f262c4 ML |
56 | *port_ptr = NULL; |
57 | ||
d4babed2 | 58 | if (!portname) |
77f262c4 | 59 | return SP_ERR_ARG; |
d4babed2 | 60 | |
d54e9004 | 61 | if (!(port = malloc(sizeof(struct sp_port)))) |
77f262c4 | 62 | return SP_ERR_MEM; |
d54e9004 | 63 | |
5919c913 ML |
64 | len = strlen(portname) + 1; |
65 | ||
d54e9004 ML |
66 | if (!(port->name = malloc(len))) |
67 | { | |
68 | free(port); | |
77f262c4 | 69 | return SP_ERR_MEM; |
d54e9004 ML |
70 | } |
71 | ||
72 | memcpy(port->name, portname, len); | |
73 | ||
77f262c4 ML |
74 | *port_ptr = port; |
75 | ||
76 | return SP_OK; | |
d54e9004 ML |
77 | } |
78 | ||
32b5ac05 ML |
79 | int sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr) |
80 | { | |
81 | if (!copy_ptr) | |
82 | return SP_ERR_ARG; | |
83 | ||
84 | *copy_ptr = NULL; | |
85 | ||
86 | if (!port || !port->name) | |
87 | return SP_ERR_ARG; | |
88 | ||
89 | return sp_get_port_by_name(port->name, copy_ptr); | |
90 | } | |
91 | ||
e3b2f7a4 ML |
92 | void sp_free_port(struct sp_port *port) |
93 | { | |
94 | if (!port) | |
95 | return; | |
96 | ||
97 | if (port->name) | |
98 | free(port->name); | |
99 | ||
100 | free(port); | |
101 | } | |
102 | ||
5919c913 | 103 | static struct sp_port **sp_list_append(struct sp_port **list, const char *portname) |
3b63f34d ML |
104 | { |
105 | void *tmp; | |
106 | unsigned int count; | |
107 | for (count = 0; list[count]; count++); | |
d54e9004 | 108 | if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2)))) |
3b63f34d ML |
109 | goto fail; |
110 | list = tmp; | |
77f262c4 | 111 | if (sp_get_port_by_name(portname, &list[count]) != SP_OK) |
3b63f34d | 112 | goto fail; |
db2794ce | 113 | list[count + 1] = NULL; |
3b63f34d ML |
114 | return list; |
115 | fail: | |
116 | sp_free_port_list(list); | |
117 | return NULL; | |
118 | } | |
119 | ||
77f262c4 | 120 | int sp_list_ports(struct sp_port ***list_ptr) |
3b63f34d | 121 | { |
d54e9004 | 122 | struct sp_port **list; |
77f262c4 | 123 | int ret = SP_OK; |
24c1a4bb | 124 | |
d54e9004 | 125 | if (!(list = malloc(sizeof(struct sp_port **)))) |
77f262c4 | 126 | return SP_ERR_MEM;; |
24c1a4bb ML |
127 | |
128 | list[0] = NULL; | |
3b63f34d ML |
129 | |
130 | #ifdef _WIN32 | |
131 | HKEY key; | |
bdfb5b8c ML |
132 | TCHAR *value, *data; |
133 | DWORD max_value_len, max_data_size, max_data_len; | |
134 | DWORD value_len, data_size, data_len; | |
3b63f34d | 135 | DWORD type, index = 0; |
8b532d9c ML |
136 | char *name; |
137 | int name_len; | |
3b63f34d ML |
138 | |
139 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), | |
140 | 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) | |
77f262c4 ML |
141 | { |
142 | ret = SP_ERR_FAIL; | |
143 | goto out_done; | |
144 | } | |
3b63f34d | 145 | if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
bdfb5b8c | 146 | &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) |
77f262c4 ML |
147 | { |
148 | ret = SP_ERR_FAIL; | |
3b63f34d | 149 | goto out_close; |
77f262c4 | 150 | } |
3b63f34d | 151 | max_data_len = max_data_size / sizeof(TCHAR); |
bdfb5b8c | 152 | if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) |
77f262c4 ML |
153 | { |
154 | ret = SP_ERR_MEM; | |
3b63f34d | 155 | goto out_close; |
77f262c4 | 156 | } |
3b63f34d | 157 | if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) |
77f262c4 ML |
158 | { |
159 | ret = SP_ERR_MEM; | |
bdfb5b8c | 160 | goto out_free_value; |
77f262c4 | 161 | } |
3b63f34d | 162 | while ( |
d9573bad | 163 | value_len = max_value_len + 1, |
3b63f34d | 164 | data_size = max_data_size, |
bdfb5b8c | 165 | RegEnumValue(key, index, value, &value_len, |
3b63f34d ML |
166 | NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS) |
167 | { | |
168 | data_len = data_size / sizeof(TCHAR); | |
169 | data[data_len] = '\0'; | |
8b532d9c ML |
170 | #ifdef UNICODE |
171 | name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL) | |
172 | #else | |
173 | name_len = data_len + 1; | |
174 | #endif | |
175 | if (!(name = malloc(name_len))) | |
77f262c4 ML |
176 | { |
177 | ret = SP_ERR_MEM; | |
8b532d9c | 178 | goto out; |
77f262c4 | 179 | } |
8b532d9c ML |
180 | #ifdef UNICODE |
181 | WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL); | |
182 | #else | |
183 | strcpy(name, data); | |
184 | #endif | |
77f262c4 ML |
185 | if (type == REG_SZ && !(list = sp_list_append(list, name))) |
186 | { | |
187 | ret = SP_ERR_MEM; | |
188 | goto out; | |
189 | } | |
3b63f34d ML |
190 | index++; |
191 | } | |
192 | out: | |
193 | free(data); | |
bdfb5b8c ML |
194 | out_free_value: |
195 | free(value); | |
3b63f34d ML |
196 | out_close: |
197 | RegCloseKey(key); | |
77f262c4 | 198 | out_done: |
3b63f34d ML |
199 | #endif |
200 | #ifdef __APPLE__ | |
201 | mach_port_t master; | |
202 | CFMutableDictionaryRef classes; | |
203 | io_iterator_t iter; | |
204 | char *path; | |
205 | io_object_t port; | |
206 | CFTypeRef cf_path; | |
207 | Boolean result; | |
208 | ||
209 | if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) | |
77f262c4 ML |
210 | { |
211 | ret = SP_ERR_FAIL; | |
212 | goto out_done; | |
213 | } | |
3b63f34d ML |
214 | |
215 | if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) | |
77f262c4 ML |
216 | { |
217 | ret = SP_ERR_FAIL; | |
218 | goto out_done; | |
219 | } | |
3b63f34d ML |
220 | |
221 | CFDictionarySetValue(classes, | |
222 | CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes)); | |
223 | ||
0d34b451 | 224 | if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) |
77f262c4 ML |
225 | { |
226 | ret = SP_ERR_FAIL; | |
227 | goto out_done; | |
228 | } | |
3b63f34d ML |
229 | |
230 | if (!(path = malloc(PATH_MAX))) | |
77f262c4 ML |
231 | { |
232 | ret = SP_ERR_MEM; | |
3b63f34d | 233 | goto out_release; |
77f262c4 | 234 | } |
3b63f34d | 235 | |
1ebf4347 | 236 | while ((port = IOIteratorNext(iter))) { |
3b63f34d ML |
237 | cf_path = IORegistryEntryCreateCFProperty(port, |
238 | CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0); | |
239 | if (cf_path) { | |
240 | result = CFStringGetCString(cf_path, | |
241 | path, PATH_MAX, kCFStringEncodingASCII); | |
242 | CFRelease(cf_path); | |
77f262c4 ML |
243 | if (result && !(list = sp_list_append(list, path))) |
244 | { | |
245 | ret = SP_ERR_MEM; | |
246 | IOObjectRelease(port); | |
247 | goto out; | |
248 | } | |
3b63f34d ML |
249 | } |
250 | IOObjectRelease(port); | |
251 | } | |
3b63f34d ML |
252 | out: |
253 | free(path); | |
254 | out_release: | |
255 | IOObjectRelease(iter); | |
77f262c4 | 256 | out_done: |
3b63f34d ML |
257 | #endif |
258 | #ifdef __linux__ | |
259 | struct udev *ud; | |
260 | struct udev_enumerate *ud_enumerate; | |
261 | struct udev_list_entry *ud_list; | |
262 | struct udev_list_entry *ud_entry; | |
263 | const char *path; | |
08fe0bdb | 264 | struct udev_device *ud_dev, *ud_parent; |
3b63f34d | 265 | const char *name; |
4b97c9fc ML |
266 | const char *driver; |
267 | int fd, ioctl_result; | |
268 | struct serial_struct serial_info; | |
3b63f34d ML |
269 | |
270 | ud = udev_new(); | |
271 | ud_enumerate = udev_enumerate_new(ud); | |
272 | udev_enumerate_add_match_subsystem(ud_enumerate, "tty"); | |
273 | udev_enumerate_scan_devices(ud_enumerate); | |
274 | ud_list = udev_enumerate_get_list_entry(ud_enumerate); | |
3b63f34d ML |
275 | udev_list_entry_foreach(ud_entry, ud_list) |
276 | { | |
277 | path = udev_list_entry_get_name(ud_entry); | |
278 | ud_dev = udev_device_new_from_syspath(ud, path); | |
08fe0bdb ML |
279 | /* If there is no parent device, this is a virtual tty. */ |
280 | ud_parent = udev_device_get_parent(ud_dev); | |
281 | if (ud_parent == NULL) | |
282 | { | |
283 | udev_device_unref(ud_dev); | |
284 | continue; | |
285 | } | |
3b63f34d | 286 | name = udev_device_get_devnode(ud_dev); |
4b97c9fc ML |
287 | /* The serial8250 driver has a hardcoded number of ports. |
288 | * The only way to tell which actually exist on a given system | |
289 | * is to try to open them and make an ioctl call. */ | |
290 | driver = udev_device_get_driver(ud_parent); | |
291 | if (driver && !strcmp(driver, "serial8250")) | |
292 | { | |
293 | if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) | |
294 | goto skip; | |
295 | ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info); | |
296 | close(fd); | |
297 | if (ioctl_result != 0) | |
298 | goto skip; | |
299 | if (serial_info.type == PORT_UNKNOWN) | |
300 | goto skip; | |
301 | } | |
5919c913 | 302 | list = sp_list_append(list, name); |
4b97c9fc | 303 | skip: |
3b63f34d ML |
304 | udev_device_unref(ud_dev); |
305 | if (!list) | |
77f262c4 ML |
306 | { |
307 | ret = SP_ERR_MEM; | |
3b63f34d | 308 | goto out; |
77f262c4 | 309 | } |
3b63f34d ML |
310 | } |
311 | out: | |
312 | udev_enumerate_unref(ud_enumerate); | |
313 | udev_unref(ud); | |
3b63f34d | 314 | #endif |
77f262c4 ML |
315 | |
316 | if (ret == SP_OK) | |
317 | { | |
318 | *list_ptr = list; | |
319 | } | |
320 | else | |
321 | { | |
322 | if (list) | |
323 | sp_free_port_list(list); | |
324 | ||
325 | *list_ptr = NULL; | |
326 | } | |
327 | ||
328 | return ret; | |
3b63f34d ML |
329 | } |
330 | ||
d54e9004 | 331 | void sp_free_port_list(struct sp_port **list) |
3b63f34d ML |
332 | { |
333 | unsigned int i; | |
334 | for (i = 0; list[i]; i++) | |
e3b2f7a4 | 335 | sp_free_port(list[i]); |
3b63f34d ML |
336 | free(list); |
337 | } | |
338 | ||
74510d4b ML |
339 | static int sp_validate_port(struct sp_port *port) |
340 | { | |
341 | if (port == NULL) | |
342 | return 0; | |
343 | #ifdef _WIN32 | |
344 | if (port->hdl == INVALID_HANDLE_VALUE) | |
345 | return 0; | |
346 | #else | |
347 | if (port->fd < 0) | |
348 | return 0; | |
349 | #endif | |
350 | return 1; | |
351 | } | |
352 | ||
353 | #define CHECK_PORT() do { if (!sp_validate_port(port)) return SP_ERR_ARG; } while (0) | |
354 | ||
d54e9004 | 355 | int sp_open(struct sp_port *port, int flags) |
74510d4b ML |
356 | { |
357 | if (!port) | |
358 | return SP_ERR_ARG; | |
359 | ||
74510d4b ML |
360 | #ifdef _WIN32 |
361 | DWORD desired_access = 0, flags_and_attributes = 0; | |
99945a1f ML |
362 | char *escaped_port_name; |
363 | ||
364 | /* Prefix port name with '\\.\' to work with ports above COM9. */ | |
365 | if (!(escaped_port_name = malloc(strlen(port->name + 5)))) | |
366 | return SP_ERR_MEM; | |
367 | sprintf(escaped_port_name, "\\\\.\\%s", port->name); | |
368 | ||
74510d4b ML |
369 | /* Map 'flags' to the OS-specific settings. */ |
370 | desired_access |= GENERIC_READ; | |
371 | flags_and_attributes = FILE_ATTRIBUTE_NORMAL; | |
372 | if (flags & SP_MODE_RDWR) | |
373 | desired_access |= GENERIC_WRITE; | |
374 | if (flags & SP_MODE_NONBLOCK) | |
375 | flags_and_attributes |= FILE_FLAG_OVERLAPPED; | |
376 | ||
99945a1f | 377 | port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0, |
74510d4b | 378 | OPEN_EXISTING, flags_and_attributes, 0); |
99945a1f ML |
379 | |
380 | free(escaped_port_name); | |
381 | ||
74510d4b ML |
382 | if (port->hdl == INVALID_HANDLE_VALUE) |
383 | return SP_ERR_FAIL; | |
384 | #else | |
385 | int flags_local = 0; | |
386 | /* Map 'flags' to the OS-specific settings. */ | |
387 | if (flags & SP_MODE_RDWR) | |
388 | flags_local |= O_RDWR; | |
389 | if (flags & SP_MODE_RDONLY) | |
390 | flags_local |= O_RDONLY; | |
391 | if (flags & SP_MODE_NONBLOCK) | |
392 | flags_local |= O_NONBLOCK; | |
393 | ||
394 | if ((port->fd = open(port->name, flags_local)) < 0) | |
395 | return SP_ERR_FAIL; | |
396 | #endif | |
397 | ||
398 | return SP_OK; | |
399 | } | |
400 | ||
74510d4b ML |
401 | int sp_close(struct sp_port *port) |
402 | { | |
403 | CHECK_PORT(); | |
404 | ||
405 | #ifdef _WIN32 | |
406 | /* Returns non-zero upon success, 0 upon failure. */ | |
407 | if (CloseHandle(port->hdl) == 0) | |
408 | return SP_ERR_FAIL; | |
409 | #else | |
410 | /* Returns 0 upon success, -1 upon failure. */ | |
411 | if (close(port->fd) == -1) | |
412 | return SP_ERR_FAIL; | |
413 | #endif | |
414 | ||
415 | return SP_OK; | |
416 | } | |
417 | ||
74510d4b ML |
418 | int sp_flush(struct sp_port *port) |
419 | { | |
420 | CHECK_PORT(); | |
421 | ||
422 | #ifdef _WIN32 | |
423 | /* Returns non-zero upon success, 0 upon failure. */ | |
424 | if (PurgeComm(port->hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) | |
425 | return SP_ERR_FAIL; | |
426 | #else | |
427 | /* Returns 0 upon success, -1 upon failure. */ | |
428 | if (tcflush(port->fd, TCIOFLUSH) < 0) | |
429 | return SP_ERR_FAIL; | |
430 | #endif | |
431 | return SP_OK; | |
432 | } | |
433 | ||
74510d4b ML |
434 | int sp_write(struct sp_port *port, const void *buf, size_t count) |
435 | { | |
436 | CHECK_PORT(); | |
437 | ||
438 | if (!buf) | |
439 | return SP_ERR_ARG; | |
440 | ||
441 | #ifdef _WIN32 | |
442 | DWORD written = 0; | |
443 | /* Returns non-zero upon success, 0 upon failure. */ | |
444 | if (WriteFile(port->hdl, buf, count, &written, NULL) == 0) | |
445 | return SP_ERR_FAIL; | |
446 | return written; | |
447 | #else | |
448 | /* Returns the number of bytes written, or -1 upon failure. */ | |
449 | ssize_t written = write(port->fd, buf, count); | |
450 | if (written < 0) | |
451 | return SP_ERR_FAIL; | |
452 | else | |
453 | return written;; | |
454 | #endif | |
455 | } | |
456 | ||
74510d4b ML |
457 | int sp_read(struct sp_port *port, void *buf, size_t count) |
458 | { | |
459 | CHECK_PORT(); | |
460 | ||
461 | if (!buf) | |
462 | return SP_ERR_ARG; | |
463 | ||
464 | #ifdef _WIN32 | |
465 | DWORD bytes_read = 0; | |
466 | /* Returns non-zero upon success, 0 upon failure. */ | |
467 | if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0) | |
468 | return SP_ERR_FAIL; | |
469 | return bytes_read; | |
470 | #else | |
471 | ssize_t bytes_read; | |
472 | /* Returns the number of bytes read, or -1 upon failure. */ | |
473 | if ((bytes_read = read(port->fd, buf, count)) < 0) | |
474 | return SP_ERR_FAIL; | |
475 | return bytes_read; | |
476 | #endif | |
477 | } | |
478 | ||
74510d4b ML |
479 | int sp_set_params(struct sp_port *port, int baudrate, |
480 | int bits, int parity, int stopbits, | |
481 | int flowcontrol, int rts, int dtr) | |
482 | { | |
483 | CHECK_PORT(); | |
484 | ||
485 | #ifdef _WIN32 | |
486 | DCB dcb; | |
487 | ||
488 | if (!GetCommState(port->hdl, &dcb)) | |
489 | return SP_ERR_FAIL; | |
490 | ||
491 | switch (baudrate) { | |
492 | /* | |
493 | * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to | |
494 | * have documented CBR_* macros. | |
495 | */ | |
496 | case 110: | |
497 | dcb.BaudRate = CBR_110; | |
498 | break; | |
499 | case 300: | |
500 | dcb.BaudRate = CBR_300; | |
501 | break; | |
502 | case 600: | |
503 | dcb.BaudRate = CBR_600; | |
504 | break; | |
505 | case 1200: | |
506 | dcb.BaudRate = CBR_1200; | |
507 | break; | |
508 | case 2400: | |
509 | dcb.BaudRate = CBR_2400; | |
510 | break; | |
511 | case 4800: | |
512 | dcb.BaudRate = CBR_4800; | |
513 | break; | |
514 | case 9600: | |
515 | dcb.BaudRate = CBR_9600; | |
516 | break; | |
517 | case 14400: | |
518 | dcb.BaudRate = CBR_14400; /* Not available on Unix? */ | |
519 | break; | |
520 | case 19200: | |
521 | dcb.BaudRate = CBR_19200; | |
522 | break; | |
523 | case 38400: | |
524 | dcb.BaudRate = CBR_38400; | |
525 | break; | |
526 | case 57600: | |
527 | dcb.BaudRate = CBR_57600; | |
528 | break; | |
529 | case 115200: | |
530 | dcb.BaudRate = CBR_115200; | |
531 | break; | |
532 | case 128000: | |
533 | dcb.BaudRate = CBR_128000; /* Not available on Unix? */ | |
534 | break; | |
535 | case 256000: | |
536 | dcb.BaudRate = CBR_256000; /* Not available on Unix? */ | |
537 | break; | |
538 | default: | |
539 | return SP_ERR_ARG; | |
540 | } | |
541 | ||
e4cc1a53 ML |
542 | dcb.ByteSize = bits; |
543 | ||
74510d4b ML |
544 | switch (stopbits) { |
545 | /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */ | |
546 | case 1: | |
547 | dcb.StopBits = ONESTOPBIT; | |
548 | break; | |
549 | case 2: | |
550 | dcb.StopBits = TWOSTOPBITS; | |
551 | break; | |
552 | default: | |
553 | return SP_ERR_ARG; | |
554 | } | |
555 | ||
556 | switch (parity) { | |
557 | /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */ | |
558 | case SP_PARITY_NONE: | |
559 | dcb.Parity = NOPARITY; | |
560 | break; | |
561 | case SP_PARITY_EVEN: | |
562 | dcb.Parity = EVENPARITY; | |
563 | break; | |
564 | case SP_PARITY_ODD: | |
565 | dcb.Parity = ODDPARITY; | |
566 | break; | |
567 | default: | |
568 | return SP_ERR_ARG; | |
569 | } | |
570 | ||
571 | if (rts != -1) { | |
572 | if (rts) | |
573 | dcb.fRtsControl = RTS_CONTROL_ENABLE; | |
574 | else | |
575 | dcb.fRtsControl = RTS_CONTROL_DISABLE; | |
576 | } | |
577 | ||
578 | if (dtr != -1) { | |
579 | if (dtr) | |
580 | dcb.fDtrControl = DTR_CONTROL_ENABLE; | |
581 | else | |
582 | dcb.fDtrControl = DTR_CONTROL_DISABLE; | |
583 | } | |
584 | ||
585 | if (!SetCommState(port->hdl, &dcb)) | |
586 | return SP_ERR_FAIL; | |
587 | #else | |
588 | struct termios term; | |
589 | speed_t baud; | |
590 | int controlbits; | |
591 | ||
592 | if (tcgetattr(port->fd, &term) < 0) | |
593 | return SP_ERR_FAIL; | |
594 | ||
595 | switch (baudrate) { | |
596 | case 50: | |
597 | baud = B50; | |
598 | break; | |
599 | case 75: | |
600 | baud = B75; | |
601 | break; | |
602 | case 110: | |
603 | baud = B110; | |
604 | break; | |
605 | case 134: | |
606 | baud = B134; | |
607 | break; | |
608 | case 150: | |
609 | baud = B150; | |
610 | break; | |
611 | case 200: | |
612 | baud = B200; | |
613 | break; | |
614 | case 300: | |
615 | baud = B300; | |
616 | break; | |
617 | case 600: | |
618 | baud = B600; | |
619 | break; | |
620 | case 1200: | |
621 | baud = B1200; | |
622 | break; | |
623 | case 1800: | |
624 | baud = B1800; | |
625 | break; | |
626 | case 2400: | |
627 | baud = B2400; | |
628 | break; | |
629 | case 4800: | |
630 | baud = B4800; | |
631 | break; | |
632 | case 9600: | |
633 | baud = B9600; | |
634 | break; | |
635 | case 19200: | |
636 | baud = B19200; | |
637 | break; | |
638 | case 38400: | |
639 | baud = B38400; | |
640 | break; | |
641 | case 57600: | |
642 | baud = B57600; | |
643 | break; | |
644 | case 115200: | |
645 | baud = B115200; | |
646 | break; | |
647 | case 230400: | |
648 | baud = B230400; | |
649 | break; | |
650 | #if !defined(__APPLE__) && !defined(__OpenBSD__) | |
651 | case 460800: | |
652 | baud = B460800; | |
653 | break; | |
654 | #endif | |
655 | default: | |
656 | return SP_ERR_ARG; | |
657 | } | |
658 | ||
659 | if (cfsetospeed(&term, baud) < 0) | |
660 | return SP_ERR_FAIL; | |
661 | ||
662 | if (cfsetispeed(&term, baud) < 0) | |
663 | return SP_ERR_FAIL; | |
664 | ||
665 | term.c_cflag &= ~CSIZE; | |
666 | switch (bits) { | |
667 | case 8: | |
668 | term.c_cflag |= CS8; | |
669 | break; | |
670 | case 7: | |
671 | term.c_cflag |= CS7; | |
672 | break; | |
d1d566f2 ML |
673 | case 6: |
674 | term.c_cflag |= CS6; | |
675 | break; | |
74510d4b ML |
676 | default: |
677 | return SP_ERR_ARG; | |
678 | } | |
679 | ||
680 | term.c_cflag &= ~CSTOPB; | |
681 | switch (stopbits) { | |
682 | case 1: | |
683 | term.c_cflag &= ~CSTOPB; | |
684 | break; | |
685 | case 2: | |
686 | term.c_cflag |= CSTOPB; | |
687 | break; | |
688 | default: | |
689 | return SP_ERR_ARG; | |
690 | } | |
691 | ||
8683177b | 692 | term.c_iflag &= ~(IXON | IXOFF | IXANY); |
74510d4b ML |
693 | term.c_cflag &= ~CRTSCTS; |
694 | switch (flowcontrol) { | |
695 | case 0: | |
696 | /* No flow control. */ | |
697 | break; | |
698 | case 1: | |
699 | term.c_cflag |= CRTSCTS; | |
700 | break; | |
701 | case 2: | |
8683177b | 702 | term.c_iflag |= IXON | IXOFF | IXANY; |
74510d4b ML |
703 | break; |
704 | default: | |
705 | return SP_ERR_ARG; | |
706 | } | |
707 | ||
708 | term.c_iflag &= ~IGNPAR; | |
8683177b | 709 | term.c_cflag &= ~(PARENB | PARODD); |
74510d4b ML |
710 | switch (parity) { |
711 | case SP_PARITY_NONE: | |
712 | term.c_iflag |= IGNPAR; | |
713 | break; | |
714 | case SP_PARITY_EVEN: | |
715 | term.c_cflag |= PARENB; | |
716 | break; | |
717 | case SP_PARITY_ODD: | |
718 | term.c_cflag |= PARENB | PARODD; | |
719 | break; | |
720 | default: | |
721 | return SP_ERR_ARG; | |
722 | } | |
723 | ||
724 | /* Turn off all serial port cooking. */ | |
725 | term.c_iflag &= ~(ISTRIP | INLCR | ICRNL); | |
726 | term.c_oflag &= ~(ONLCR | OCRNL | ONOCR); | |
727 | #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) | |
728 | term.c_oflag &= ~OFILL; | |
729 | #endif | |
730 | ||
731 | /* Disable canonical mode, and don't echo input characters. */ | |
732 | term.c_lflag &= ~(ICANON | ECHO); | |
733 | ||
8683177b ML |
734 | /* Ignore modem status lines; enable receiver */ |
735 | term.c_cflag |= (CLOCAL | CREAD); | |
736 | ||
74510d4b ML |
737 | /* Write the configured settings. */ |
738 | if (tcsetattr(port->fd, TCSADRAIN, &term) < 0) | |
739 | return SP_ERR_FAIL; | |
740 | ||
741 | if (rts != -1) { | |
742 | controlbits = TIOCM_RTS; | |
743 | if (ioctl(port->fd, rts ? TIOCMBIS : TIOCMBIC, | |
744 | &controlbits) < 0) | |
745 | return SP_ERR_FAIL; | |
746 | } | |
747 | ||
748 | if (dtr != -1) { | |
749 | controlbits = TIOCM_DTR; | |
750 | if (ioctl(port->fd, dtr ? TIOCMBIS : TIOCMBIC, | |
751 | &controlbits) < 0) | |
752 | return SP_ERR_FAIL; | |
753 | } | |
754 | #endif | |
755 | ||
756 | return SP_OK; | |
757 | } | |
758 | ||
74510d4b ML |
759 | int sp_last_error_code(void) |
760 | { | |
761 | #ifdef _WIN32 | |
762 | return GetLastError(); | |
763 | #else | |
764 | return errno; | |
765 | #endif | |
766 | } | |
767 | ||
74510d4b ML |
768 | char *sp_last_error_message(void) |
769 | { | |
770 | #ifdef _WIN32 | |
771 | LPVOID message; | |
772 | DWORD error = GetLastError(); | |
773 | ||
774 | FormatMessage( | |
775 | FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
776 | FORMAT_MESSAGE_FROM_SYSTEM | | |
777 | FORMAT_MESSAGE_IGNORE_INSERTS, | |
778 | NULL, | |
779 | error, | |
780 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
781 | (LPTSTR) &message, | |
782 | 0, NULL ); | |
783 | ||
784 | return message; | |
785 | #else | |
786 | return strerror(errno); | |
787 | #endif | |
788 | } | |
789 | ||
74510d4b ML |
790 | void sp_free_error_message(char *message) |
791 | { | |
792 | #ifdef _WIN32 | |
793 | LocalFree(message); | |
64eec30d ML |
794 | #else |
795 | (void)message; | |
74510d4b ML |
796 | #endif |
797 | } |