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