]>
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 | ||
2b40f814 UH |
21 | #ifndef LIBSERIALPORT_LIBSERIALPORT_INTERNAL_H |
22 | #define LIBSERIALPORT_LIBSERIALPORT_INTERNAL_H | |
23 | ||
7c081505 | 24 | |
c51b846e | 25 | #ifdef __linux__ |
95bad38c | 26 | /* For timeradd, timersub, timercmp, realpath. */ |
4d8195fe WS |
27 | #define _BSD_SOURCE 1 /* for glibc < 2.19 */ |
28 | #define _DEFAULT_SOURCE 1 /* for glibc >= 2.20 */ | |
277f832a ML |
29 | /* For clock_gettime and associated types. */ |
30 | #define _POSIX_C_SOURCE 199309L | |
c51b846e ML |
31 | #endif |
32 | ||
e33dcf90 ML |
33 | #include <string.h> |
34 | #include <sys/types.h> | |
35 | #include <sys/stat.h> | |
36 | #include <fcntl.h> | |
37 | #include <unistd.h> | |
38 | #include <stdlib.h> | |
39 | #include <errno.h> | |
40 | #include <stdio.h> | |
41 | #include <stdarg.h> | |
9a7945af | 42 | #include <stdbool.h> |
3317d678 | 43 | #include <stdint.h> |
e33dcf90 ML |
44 | #ifdef _WIN32 |
45 | #include <windows.h> | |
46 | #include <tchar.h> | |
47 | #include <setupapi.h> | |
48 | #include <cfgmgr32.h> | |
78940e69 ML |
49 | #undef DEFINE_GUID |
50 | #define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \ | |
51 | static const GUID name = { l,w1,w2,{ b1,b2,b3,b4,b5,b6,b7,b8 } } | |
e33dcf90 | 52 | #include <usbioctl.h> |
d179da05 | 53 | #include <usbiodef.h> |
e9d78d82 ML |
54 | /* The largest size that can be passed to WriteFile() safely |
55 | * on any architecture. This arises from the expression: | |
56 | * PAGE_SIZE * (65535 - sizeof(MDL)) / sizeof(ULONG_PTR) | |
57 | * and this worst-case value is found on x64. */ | |
58 | #define WRITEFILE_MAX_SIZE 33525760 | |
e33dcf90 ML |
59 | #else |
60 | #include <limits.h> | |
61 | #include <termios.h> | |
62 | #include <sys/ioctl.h> | |
63 | #include <sys/time.h> | |
f40ea9d4 | 64 | #include <time.h> |
e33dcf90 ML |
65 | #include <poll.h> |
66 | #endif | |
67 | #ifdef __APPLE__ | |
68 | #include <CoreFoundation/CoreFoundation.h> | |
69 | #include <IOKit/IOKitLib.h> | |
70 | #include <IOKit/serial/IOSerialKeys.h> | |
71 | #include <IOKit/serial/ioss.h> | |
72 | #include <sys/syslimits.h> | |
192e7749 | 73 | #include <mach/mach_time.h> |
e33dcf90 ML |
74 | #endif |
75 | #ifdef __linux__ | |
76 | #include <dirent.h> | |
abd31fd9 ML |
77 | /* Android only has linux/serial.h from platform 21 onwards. */ |
78 | #if !(defined(__ANDROID__) && (__ANDROID_API__ < 21)) | |
79 | #include <linux/serial.h> | |
e33dcf90 ML |
80 | #endif |
81 | #include "linux_termios.h" | |
82 | ||
83 | /* TCGETX/TCSETX is not available everywhere. */ | |
f1c916ed | 84 | #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_STRUCT_TERMIOX) |
e33dcf90 ML |
85 | #define USE_TERMIOX |
86 | #endif | |
87 | #endif | |
88 | ||
89 | /* TIOCINQ/TIOCOUTQ is not available everywhere. */ | |
90 | #if !defined(TIOCINQ) && defined(FIONREAD) | |
91 | #define TIOCINQ FIONREAD | |
92 | #endif | |
93 | #if !defined(TIOCOUTQ) && defined(FIONWRITE) | |
94 | #define TIOCOUTQ FIONWRITE | |
95 | #endif | |
96 | ||
fa106ef1 CS |
97 | /* |
98 | * O_CLOEXEC is not available everywhere, fallback to not setting the | |
99 | * flag on those systems. | |
100 | */ | |
101 | #ifndef _WIN32 | |
102 | #ifndef O_CLOEXEC | |
103 | #define O_CLOEXEC 0 | |
104 | #endif | |
105 | #endif | |
106 | ||
e33dcf90 | 107 | /* Non-standard baudrates are not available everywhere. */ |
6c811582 | 108 | #if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && HAVE_DECL_BOTHER |
e33dcf90 ML |
109 | #define USE_TERMIOS_SPEED |
110 | #endif | |
111 | ||
112 | struct sp_port { | |
113 | char *name; | |
114 | char *description; | |
115 | enum sp_transport transport; | |
116 | int usb_bus; | |
117 | int usb_address; | |
118 | int usb_vid; | |
119 | int usb_pid; | |
120 | char *usb_manufacturer; | |
121 | char *usb_product; | |
122 | char *usb_serial; | |
123 | char *bluetooth_address; | |
124 | #ifdef _WIN32 | |
125 | char *usb_path; | |
126 | HANDLE hdl; | |
127 | COMMTIMEOUTS timeouts; | |
128 | OVERLAPPED write_ovl; | |
129 | OVERLAPPED read_ovl; | |
130 | OVERLAPPED wait_ovl; | |
131 | DWORD events; | |
39df7833 ML |
132 | BYTE *write_buf; |
133 | DWORD write_buf_size; | |
e33dcf90 | 134 | BOOL writing; |
47fcf8ec | 135 | BOOL wait_running; |
e33dcf90 ML |
136 | #else |
137 | int fd; | |
138 | #endif | |
139 | }; | |
140 | ||
141 | struct sp_port_config { | |
142 | int baudrate; | |
143 | int bits; | |
144 | enum sp_parity parity; | |
145 | int stopbits; | |
146 | enum sp_rts rts; | |
147 | enum sp_cts cts; | |
148 | enum sp_dtr dtr; | |
149 | enum sp_dsr dsr; | |
150 | enum sp_xonxoff xon_xoff; | |
151 | }; | |
152 | ||
153 | struct port_data { | |
154 | #ifdef _WIN32 | |
155 | DCB dcb; | |
156 | #else | |
157 | struct termios term; | |
158 | int controlbits; | |
159 | int termiox_supported; | |
160 | int rts_flow; | |
161 | int cts_flow; | |
162 | int dtr_flow; | |
163 | int dsr_flow; | |
164 | #endif | |
165 | }; | |
166 | ||
167 | #ifdef _WIN32 | |
168 | typedef HANDLE event_handle; | |
169 | #else | |
170 | typedef int event_handle; | |
171 | #endif | |
172 | ||
173 | /* Standard baud rates. */ | |
174 | #ifdef _WIN32 | |
175 | #define BAUD_TYPE DWORD | |
176 | #define BAUD(n) {CBR_##n, n} | |
177 | #else | |
178 | #define BAUD_TYPE speed_t | |
179 | #define BAUD(n) {B##n, n} | |
180 | #endif | |
181 | ||
182 | struct std_baudrate { | |
183 | BAUD_TYPE index; | |
184 | int value; | |
185 | }; | |
186 | ||
e33dcf90 | 187 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
e33dcf90 ML |
188 | |
189 | extern void (*sp_debug_handler)(const char *format, ...); | |
190 | ||
191 | /* Debug output macros. */ | |
7890cef6 ML |
192 | #define DEBUG_FMT(fmt, ...) do { \ |
193 | if (sp_debug_handler) \ | |
194 | sp_debug_handler(fmt ".\n", __VA_ARGS__); \ | |
195 | } while (0) | |
196 | #define DEBUG(msg) DEBUG_FMT(msg, NULL) | |
197 | #define DEBUG_ERROR(err, msg) DEBUG_FMT("%s returning " #err ": " msg, __func__) | |
198 | #define DEBUG_FAIL(msg) do { \ | |
e33dcf90 | 199 | char *errmsg = sp_last_error_message(); \ |
7890cef6 | 200 | DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \ |
e33dcf90 ML |
201 | sp_free_error_message(errmsg); \ |
202 | } while (0); | |
7890cef6 ML |
203 | #define RETURN() do { \ |
204 | DEBUG_FMT("%s returning", __func__); \ | |
205 | return; \ | |
dc422c04 | 206 | } while (0) |
7890cef6 ML |
207 | #define RETURN_CODE(x) do { \ |
208 | DEBUG_FMT("%s returning " #x, __func__); \ | |
209 | return x; \ | |
210 | } while (0) | |
e33dcf90 ML |
211 | #define RETURN_CODEVAL(x) do { \ |
212 | switch (x) { \ | |
b344a40b UH |
213 | case SP_OK: RETURN_CODE(SP_OK); \ |
214 | case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \ | |
215 | case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \ | |
216 | case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \ | |
217 | case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \ | |
e019e72e | 218 | default: RETURN_CODE(SP_ERR_FAIL); \ |
e33dcf90 ML |
219 | } \ |
220 | } while (0) | |
221 | #define RETURN_OK() RETURN_CODE(SP_OK); | |
7890cef6 ML |
222 | #define RETURN_ERROR(err, msg) do { \ |
223 | DEBUG_ERROR(err, msg); \ | |
224 | return err; \ | |
225 | } while (0) | |
226 | #define RETURN_FAIL(msg) do { \ | |
227 | DEBUG_FAIL(msg); \ | |
228 | return SP_ERR_FAIL; \ | |
229 | } while (0) | |
9caa2e86 ML |
230 | #define RETURN_INT(x) do { \ |
231 | int _x = x; \ | |
7890cef6 | 232 | DEBUG_FMT("%s returning %d", __func__, _x); \ |
9caa2e86 ML |
233 | return _x; \ |
234 | } while (0) | |
235 | #define RETURN_STRING(x) do { \ | |
236 | char *_x = x; \ | |
7890cef6 | 237 | DEBUG_FMT("%s returning %s", __func__, _x); \ |
9caa2e86 ML |
238 | return _x; \ |
239 | } while (0) | |
240 | #define RETURN_POINTER(x) do { \ | |
241 | void *_x = x; \ | |
7890cef6 | 242 | DEBUG_FMT("%s returning %p", __func__, _x); \ |
e33dcf90 ML |
243 | return _x; \ |
244 | } while (0) | |
245 | #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0) | |
246 | #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0) | |
7890cef6 ML |
247 | #define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__) |
248 | #define TRACE_VOID() DEBUG_FMT("%s() called", __func__) | |
e33dcf90 | 249 | |
613c48f1 | 250 | #define TRY(x) do { int retval = x; if (retval != SP_OK) RETURN_CODEVAL(retval); } while (0) |
e33dcf90 | 251 | |
970f279a | 252 | SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname); |
48a4076f | 253 | |
e33dcf90 | 254 | /* OS-specific Helper functions. */ |
970f279a AJ |
255 | SP_PRIV enum sp_return get_port_details(struct sp_port *port); |
256 | SP_PRIV enum sp_return list_ports(struct sp_port ***list); | |
2b40f814 | 257 | |
39acdc47 ML |
258 | /* Timing abstraction */ |
259 | ||
260 | struct time { | |
261 | #ifdef _WIN32 | |
262 | int64_t ticks; | |
263 | #else | |
264 | struct timeval tv; | |
265 | #endif | |
266 | }; | |
267 | ||
268 | struct timeout { | |
269 | unsigned int ms, limit_ms; | |
270 | struct time start, now, end, delta, delta_max; | |
271 | struct timeval delta_tv; | |
272 | bool calls_started, overflow; | |
273 | }; | |
274 | ||
275 | SP_PRIV void time_get(struct time *time); | |
276 | SP_PRIV void time_set_ms(struct time *time, unsigned int ms); | |
277 | SP_PRIV void time_add(const struct time *a, const struct time *b, struct time *result); | |
278 | SP_PRIV void time_sub(const struct time *a, const struct time *b, struct time *result); | |
279 | SP_PRIV bool time_greater(const struct time *a, const struct time *b); | |
280 | SP_PRIV void time_as_timeval(const struct time *time, struct timeval *tv); | |
281 | SP_PRIV unsigned int time_as_ms(const struct time *time); | |
282 | SP_PRIV void timeout_start(struct timeout *timeout, unsigned int timeout_ms); | |
283 | SP_PRIV void timeout_limit(struct timeout *timeout, unsigned int limit_ms); | |
284 | SP_PRIV bool timeout_check(struct timeout *timeout); | |
285 | SP_PRIV void timeout_update(struct timeout *timeout); | |
286 | SP_PRIV struct timeval *timeout_timeval(struct timeout *timeout); | |
287 | SP_PRIV unsigned int timeout_remaining_ms(struct timeout *timeout); | |
288 | ||
2b40f814 | 289 | #endif |