]>
Commit | Line | Data |
---|---|---|
e33dcf90 ML |
1 | /* |
2 | * This file is part of the libserialport project. | |
3 | * | |
4 | * Copyright (C) 2014 Martin Ling <martin-libserialport@earth.li> | |
5 | * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU Lesser General Public License as | |
9 | * published by the Free Software Foundation, either version 3 of the | |
10 | * License, or (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
c51b846e ML |
21 | #ifdef __linux__ |
22 | #define _BSD_SOURCE // for timeradd, timersub, timercmp | |
c51b846e ML |
23 | #endif |
24 | ||
e33dcf90 ML |
25 | #include <string.h> |
26 | #include <sys/types.h> | |
27 | #include <sys/stat.h> | |
28 | #include <fcntl.h> | |
29 | #include <unistd.h> | |
30 | #include <stdlib.h> | |
31 | #include <errno.h> | |
32 | #include <stdio.h> | |
33 | #include <stdarg.h> | |
34 | #ifdef _WIN32 | |
d179da05 MT |
35 | #include <initguid.h> |
36 | ||
e33dcf90 ML |
37 | #include <windows.h> |
38 | #include <tchar.h> | |
39 | #include <setupapi.h> | |
40 | #include <cfgmgr32.h> | |
41 | #include <usbioctl.h> | |
d179da05 | 42 | #include <usbiodef.h> |
e33dcf90 ML |
43 | #else |
44 | #include <limits.h> | |
45 | #include <termios.h> | |
46 | #include <sys/ioctl.h> | |
47 | #include <sys/time.h> | |
48 | #include <limits.h> | |
49 | #include <poll.h> | |
50 | #endif | |
51 | #ifdef __APPLE__ | |
52 | #include <CoreFoundation/CoreFoundation.h> | |
53 | #include <IOKit/IOKitLib.h> | |
54 | #include <IOKit/serial/IOSerialKeys.h> | |
55 | #include <IOKit/serial/ioss.h> | |
56 | #include <sys/syslimits.h> | |
57 | #endif | |
58 | #ifdef __linux__ | |
59 | #include <dirent.h> | |
60 | #ifndef __ANDROID__ | |
61 | #include "linux/serial.h" | |
62 | #endif | |
63 | #include "linux_termios.h" | |
64 | ||
65 | /* TCGETX/TCSETX is not available everywhere. */ | |
66 | #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX) | |
67 | #define USE_TERMIOX | |
68 | #endif | |
69 | #endif | |
70 | ||
71 | /* TIOCINQ/TIOCOUTQ is not available everywhere. */ | |
72 | #if !defined(TIOCINQ) && defined(FIONREAD) | |
73 | #define TIOCINQ FIONREAD | |
74 | #endif | |
75 | #if !defined(TIOCOUTQ) && defined(FIONWRITE) | |
76 | #define TIOCOUTQ FIONWRITE | |
77 | #endif | |
78 | ||
79 | /* Non-standard baudrates are not available everywhere. */ | |
80 | #if defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED) | |
81 | #define USE_TERMIOS_SPEED | |
82 | #endif | |
83 | ||
84 | struct sp_port { | |
85 | char *name; | |
86 | char *description; | |
87 | enum sp_transport transport; | |
88 | int usb_bus; | |
89 | int usb_address; | |
90 | int usb_vid; | |
91 | int usb_pid; | |
92 | char *usb_manufacturer; | |
93 | char *usb_product; | |
94 | char *usb_serial; | |
95 | char *bluetooth_address; | |
96 | #ifdef _WIN32 | |
97 | char *usb_path; | |
98 | HANDLE hdl; | |
99 | COMMTIMEOUTS timeouts; | |
100 | OVERLAPPED write_ovl; | |
101 | OVERLAPPED read_ovl; | |
102 | OVERLAPPED wait_ovl; | |
103 | DWORD events; | |
104 | BYTE pending_byte; | |
105 | BOOL writing; | |
106 | #else | |
107 | int fd; | |
108 | #endif | |
109 | }; | |
110 | ||
111 | struct sp_port_config { | |
112 | int baudrate; | |
113 | int bits; | |
114 | enum sp_parity parity; | |
115 | int stopbits; | |
116 | enum sp_rts rts; | |
117 | enum sp_cts cts; | |
118 | enum sp_dtr dtr; | |
119 | enum sp_dsr dsr; | |
120 | enum sp_xonxoff xon_xoff; | |
121 | }; | |
122 | ||
123 | struct port_data { | |
124 | #ifdef _WIN32 | |
125 | DCB dcb; | |
126 | #else | |
127 | struct termios term; | |
128 | int controlbits; | |
129 | int termiox_supported; | |
130 | int rts_flow; | |
131 | int cts_flow; | |
132 | int dtr_flow; | |
133 | int dsr_flow; | |
134 | #endif | |
135 | }; | |
136 | ||
137 | #ifdef _WIN32 | |
138 | typedef HANDLE event_handle; | |
139 | #else | |
140 | typedef int event_handle; | |
141 | #endif | |
142 | ||
143 | /* Standard baud rates. */ | |
144 | #ifdef _WIN32 | |
145 | #define BAUD_TYPE DWORD | |
146 | #define BAUD(n) {CBR_##n, n} | |
147 | #else | |
148 | #define BAUD_TYPE speed_t | |
149 | #define BAUD(n) {B##n, n} | |
150 | #endif | |
151 | ||
152 | struct std_baudrate { | |
153 | BAUD_TYPE index; | |
154 | int value; | |
155 | }; | |
156 | ||
157 | extern const struct std_baudrate std_baudrates[]; | |
158 | ||
159 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) | |
160 | #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates) | |
161 | ||
162 | extern void (*sp_debug_handler)(const char *format, ...); | |
163 | ||
164 | /* Debug output macros. */ | |
7890cef6 ML |
165 | #define DEBUG_FMT(fmt, ...) do { \ |
166 | if (sp_debug_handler) \ | |
167 | sp_debug_handler(fmt ".\n", __VA_ARGS__); \ | |
168 | } while (0) | |
169 | #define DEBUG(msg) DEBUG_FMT(msg, NULL) | |
170 | #define DEBUG_ERROR(err, msg) DEBUG_FMT("%s returning " #err ": " msg, __func__) | |
171 | #define DEBUG_FAIL(msg) do { \ | |
e33dcf90 | 172 | char *errmsg = sp_last_error_message(); \ |
7890cef6 | 173 | DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \ |
e33dcf90 ML |
174 | sp_free_error_message(errmsg); \ |
175 | } while (0); | |
7890cef6 ML |
176 | #define RETURN() do { \ |
177 | DEBUG_FMT("%s returning", __func__); \ | |
178 | return; \ | |
179 | } while(0) | |
180 | #define RETURN_CODE(x) do { \ | |
181 | DEBUG_FMT("%s returning " #x, __func__); \ | |
182 | return x; \ | |
183 | } while (0) | |
e33dcf90 ML |
184 | #define RETURN_CODEVAL(x) do { \ |
185 | switch (x) { \ | |
186 | case SP_OK: RETURN_CODE(SP_OK); \ | |
187 | case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \ | |
188 | case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \ | |
189 | case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \ | |
190 | case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \ | |
191 | } \ | |
192 | } while (0) | |
193 | #define RETURN_OK() RETURN_CODE(SP_OK); | |
7890cef6 ML |
194 | #define RETURN_ERROR(err, msg) do { \ |
195 | DEBUG_ERROR(err, msg); \ | |
196 | return err; \ | |
197 | } while (0) | |
198 | #define RETURN_FAIL(msg) do { \ | |
199 | DEBUG_FAIL(msg); \ | |
200 | return SP_ERR_FAIL; \ | |
201 | } while (0) | |
9caa2e86 ML |
202 | #define RETURN_INT(x) do { \ |
203 | int _x = x; \ | |
7890cef6 | 204 | DEBUG_FMT("%s returning %d", __func__, _x); \ |
9caa2e86 ML |
205 | return _x; \ |
206 | } while (0) | |
207 | #define RETURN_STRING(x) do { \ | |
208 | char *_x = x; \ | |
7890cef6 | 209 | DEBUG_FMT("%s returning %s", __func__, _x); \ |
9caa2e86 ML |
210 | return _x; \ |
211 | } while (0) | |
212 | #define RETURN_POINTER(x) do { \ | |
213 | void *_x = x; \ | |
7890cef6 | 214 | DEBUG_FMT("%s returning %p", __func__, _x); \ |
e33dcf90 ML |
215 | return _x; \ |
216 | } while (0) | |
217 | #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0) | |
218 | #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0) | |
7890cef6 ML |
219 | #define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__) |
220 | #define TRACE_VOID() DEBUG_FMT("%s() called", __func__) | |
e33dcf90 ML |
221 | |
222 | #define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0) | |
223 | ||
970f279a | 224 | SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname); |
48a4076f | 225 | |
e33dcf90 | 226 | /* OS-specific Helper functions. */ |
970f279a AJ |
227 | SP_PRIV enum sp_return get_port_details(struct sp_port *port); |
228 | SP_PRIV enum sp_return list_ports(struct sp_port ***list); |