]>
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> | |
42 | #ifdef _WIN32 | |
43 | #include <windows.h> | |
44 | #include <tchar.h> | |
45 | #include <setupapi.h> | |
46 | #include <cfgmgr32.h> | |
78940e69 ML |
47 | #undef DEFINE_GUID |
48 | #define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \ | |
49 | static const GUID name = { l,w1,w2,{ b1,b2,b3,b4,b5,b6,b7,b8 } } | |
e33dcf90 | 50 | #include <usbioctl.h> |
d179da05 | 51 | #include <usbiodef.h> |
b457865b | 52 | #include "windows_ddk.h" |
e33dcf90 ML |
53 | #else |
54 | #include <limits.h> | |
55 | #include <termios.h> | |
56 | #include <sys/ioctl.h> | |
57 | #include <sys/time.h> | |
f40ea9d4 | 58 | #include <time.h> |
e33dcf90 ML |
59 | #include <poll.h> |
60 | #endif | |
61 | #ifdef __APPLE__ | |
62 | #include <CoreFoundation/CoreFoundation.h> | |
63 | #include <IOKit/IOKitLib.h> | |
64 | #include <IOKit/serial/IOSerialKeys.h> | |
65 | #include <IOKit/serial/ioss.h> | |
66 | #include <sys/syslimits.h> | |
192e7749 | 67 | #include <mach/mach_time.h> |
e33dcf90 ML |
68 | #endif |
69 | #ifdef __linux__ | |
70 | #include <dirent.h> | |
abd31fd9 ML |
71 | /* Android only has linux/serial.h from platform 21 onwards. */ |
72 | #if !(defined(__ANDROID__) && (__ANDROID_API__ < 21)) | |
73 | #include <linux/serial.h> | |
e33dcf90 ML |
74 | #endif |
75 | #include "linux_termios.h" | |
76 | ||
77 | /* TCGETX/TCSETX is not available everywhere. */ | |
f1c916ed | 78 | #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_STRUCT_TERMIOX) |
e33dcf90 ML |
79 | #define USE_TERMIOX |
80 | #endif | |
81 | #endif | |
82 | ||
83 | /* TIOCINQ/TIOCOUTQ is not available everywhere. */ | |
84 | #if !defined(TIOCINQ) && defined(FIONREAD) | |
85 | #define TIOCINQ FIONREAD | |
86 | #endif | |
87 | #if !defined(TIOCOUTQ) && defined(FIONWRITE) | |
88 | #define TIOCOUTQ FIONWRITE | |
89 | #endif | |
90 | ||
fa106ef1 CS |
91 | /* |
92 | * O_CLOEXEC is not available everywhere, fallback to not setting the | |
93 | * flag on those systems. | |
94 | */ | |
95 | #ifndef _WIN32 | |
96 | #ifndef O_CLOEXEC | |
97 | #define O_CLOEXEC 0 | |
98 | #endif | |
99 | #endif | |
100 | ||
e33dcf90 | 101 | /* Non-standard baudrates are not available everywhere. */ |
6c811582 | 102 | #if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && HAVE_DECL_BOTHER |
e33dcf90 ML |
103 | #define USE_TERMIOS_SPEED |
104 | #endif | |
105 | ||
106 | struct sp_port { | |
107 | char *name; | |
108 | char *description; | |
109 | enum sp_transport transport; | |
110 | int usb_bus; | |
111 | int usb_address; | |
112 | int usb_vid; | |
113 | int usb_pid; | |
114 | char *usb_manufacturer; | |
115 | char *usb_product; | |
116 | char *usb_serial; | |
117 | char *bluetooth_address; | |
118 | #ifdef _WIN32 | |
119 | char *usb_path; | |
120 | HANDLE hdl; | |
121 | COMMTIMEOUTS timeouts; | |
122 | OVERLAPPED write_ovl; | |
123 | OVERLAPPED read_ovl; | |
124 | OVERLAPPED wait_ovl; | |
125 | DWORD events; | |
39df7833 ML |
126 | BYTE *write_buf; |
127 | DWORD write_buf_size; | |
e33dcf90 | 128 | BOOL writing; |
47fcf8ec | 129 | BOOL wait_running; |
e33dcf90 ML |
130 | #else |
131 | int fd; | |
132 | #endif | |
133 | }; | |
134 | ||
135 | struct sp_port_config { | |
136 | int baudrate; | |
137 | int bits; | |
138 | enum sp_parity parity; | |
139 | int stopbits; | |
140 | enum sp_rts rts; | |
141 | enum sp_cts cts; | |
142 | enum sp_dtr dtr; | |
143 | enum sp_dsr dsr; | |
144 | enum sp_xonxoff xon_xoff; | |
145 | }; | |
146 | ||
147 | struct port_data { | |
148 | #ifdef _WIN32 | |
149 | DCB dcb; | |
150 | #else | |
151 | struct termios term; | |
152 | int controlbits; | |
153 | int termiox_supported; | |
154 | int rts_flow; | |
155 | int cts_flow; | |
156 | int dtr_flow; | |
157 | int dsr_flow; | |
158 | #endif | |
159 | }; | |
160 | ||
161 | #ifdef _WIN32 | |
162 | typedef HANDLE event_handle; | |
163 | #else | |
164 | typedef int event_handle; | |
165 | #endif | |
166 | ||
167 | /* Standard baud rates. */ | |
168 | #ifdef _WIN32 | |
169 | #define BAUD_TYPE DWORD | |
170 | #define BAUD(n) {CBR_##n, n} | |
171 | #else | |
172 | #define BAUD_TYPE speed_t | |
173 | #define BAUD(n) {B##n, n} | |
174 | #endif | |
175 | ||
176 | struct std_baudrate { | |
177 | BAUD_TYPE index; | |
178 | int value; | |
179 | }; | |
180 | ||
e33dcf90 | 181 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
e33dcf90 ML |
182 | |
183 | extern void (*sp_debug_handler)(const char *format, ...); | |
184 | ||
185 | /* Debug output macros. */ | |
7890cef6 ML |
186 | #define DEBUG_FMT(fmt, ...) do { \ |
187 | if (sp_debug_handler) \ | |
188 | sp_debug_handler(fmt ".\n", __VA_ARGS__); \ | |
189 | } while (0) | |
190 | #define DEBUG(msg) DEBUG_FMT(msg, NULL) | |
191 | #define DEBUG_ERROR(err, msg) DEBUG_FMT("%s returning " #err ": " msg, __func__) | |
192 | #define DEBUG_FAIL(msg) do { \ | |
e33dcf90 | 193 | char *errmsg = sp_last_error_message(); \ |
7890cef6 | 194 | DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \ |
e33dcf90 ML |
195 | sp_free_error_message(errmsg); \ |
196 | } while (0); | |
7890cef6 ML |
197 | #define RETURN() do { \ |
198 | DEBUG_FMT("%s returning", __func__); \ | |
199 | return; \ | |
dc422c04 | 200 | } while (0) |
7890cef6 ML |
201 | #define RETURN_CODE(x) do { \ |
202 | DEBUG_FMT("%s returning " #x, __func__); \ | |
203 | return x; \ | |
204 | } while (0) | |
e33dcf90 ML |
205 | #define RETURN_CODEVAL(x) do { \ |
206 | switch (x) { \ | |
b344a40b UH |
207 | case SP_OK: RETURN_CODE(SP_OK); \ |
208 | case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \ | |
209 | case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \ | |
210 | case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \ | |
211 | case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \ | |
e019e72e | 212 | default: RETURN_CODE(SP_ERR_FAIL); \ |
e33dcf90 ML |
213 | } \ |
214 | } while (0) | |
215 | #define RETURN_OK() RETURN_CODE(SP_OK); | |
7890cef6 ML |
216 | #define RETURN_ERROR(err, msg) do { \ |
217 | DEBUG_ERROR(err, msg); \ | |
218 | return err; \ | |
219 | } while (0) | |
220 | #define RETURN_FAIL(msg) do { \ | |
221 | DEBUG_FAIL(msg); \ | |
222 | return SP_ERR_FAIL; \ | |
223 | } while (0) | |
9caa2e86 ML |
224 | #define RETURN_INT(x) do { \ |
225 | int _x = x; \ | |
7890cef6 | 226 | DEBUG_FMT("%s returning %d", __func__, _x); \ |
9caa2e86 ML |
227 | return _x; \ |
228 | } while (0) | |
229 | #define RETURN_STRING(x) do { \ | |
230 | char *_x = x; \ | |
7890cef6 | 231 | DEBUG_FMT("%s returning %s", __func__, _x); \ |
9caa2e86 ML |
232 | return _x; \ |
233 | } while (0) | |
234 | #define RETURN_POINTER(x) do { \ | |
235 | void *_x = x; \ | |
7890cef6 | 236 | DEBUG_FMT("%s returning %p", __func__, _x); \ |
e33dcf90 ML |
237 | return _x; \ |
238 | } while (0) | |
239 | #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0) | |
240 | #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0) | |
7890cef6 ML |
241 | #define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__) |
242 | #define TRACE_VOID() DEBUG_FMT("%s() called", __func__) | |
e33dcf90 | 243 | |
613c48f1 | 244 | #define TRY(x) do { int retval = x; if (retval != SP_OK) RETURN_CODEVAL(retval); } while (0) |
e33dcf90 | 245 | |
970f279a | 246 | SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname); |
48a4076f | 247 | |
e33dcf90 | 248 | /* OS-specific Helper functions. */ |
970f279a AJ |
249 | SP_PRIV enum sp_return get_port_details(struct sp_port *port); |
250 | SP_PRIV enum sp_return list_ports(struct sp_port ***list); | |
2b40f814 UH |
251 | |
252 | #endif |