Bump libtool and package versions to 1:1:1 and 0.1.2
[libserialport.git] / libserialport_internal.h
blob57346d653ad369636a769e35858ef36138686b8c
1 /*
2 * This file is part of the libserialport project.
4 * Copyright (C) 2014 Martin Ling <martin-libserialport@earth.li>
5 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
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.
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.
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/>.
21 #ifndef LIBSERIALPORT_LIBSERIALPORT_INTERNAL_H
22 #define LIBSERIALPORT_LIBSERIALPORT_INTERNAL_H
24 /* These MSVC-specific defines must appear before other headers.*/
25 #ifdef _MSC_VER
26 #define _CRT_NONSTDC_NO_DEPRECATE
27 #define _CRT_SECURE_NO_WARNINGS
28 #endif
30 /* These feature test macros must appear before other headers.*/
31 #if defined(__linux__) || defined(__CYGWIN__)
32 /* For timeradd, timersub, timercmp, realpath. */
33 #define _BSD_SOURCE 1 /* for glibc < 2.19 */
34 #define _DEFAULT_SOURCE 1 /* for glibc >= 2.20 */
35 /* For clock_gettime and associated types. */
36 #define _POSIX_C_SOURCE 199309L
37 #endif
39 #ifdef LIBSERIALPORT_ATBUILD
40 /* If building with autoconf, include the generated config.h. */
41 #include <config.h>
42 #endif
44 #ifdef LIBSERIALPORT_MSBUILD
45 /* If building with MS tools, define necessary things that
46 would otherwise appear in config.h. */
47 #define SP_PRIV
48 #endif
50 #include "libserialport.h"
52 #include <string.h>
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <fcntl.h>
56 #include <stdlib.h>
57 #include <errno.h>
58 #include <stdio.h>
59 #include <stdarg.h>
60 #include <stdbool.h>
61 #include <stdint.h>
62 #ifdef _WIN32
63 #include <windows.h>
64 #include <tchar.h>
65 #include <setupapi.h>
66 #include <cfgmgr32.h>
67 #undef DEFINE_GUID
68 #define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
69 static const GUID name = { l,w1,w2,{ b1,b2,b3,b4,b5,b6,b7,b8 } }
70 #include <usbioctl.h>
71 #include <usbiodef.h>
72 /* The largest size that can be passed to WriteFile() safely
73 * on any architecture. This arises from the expression:
74 * PAGE_SIZE * (65535 - sizeof(MDL)) / sizeof(ULONG_PTR)
75 * and this worst-case value is found on x64. */
76 #define WRITEFILE_MAX_SIZE 33525760
77 #else
78 #include <limits.h>
79 #include <termios.h>
80 #include <sys/ioctl.h>
81 #include <sys/time.h>
82 #include <time.h>
83 #include <poll.h>
84 #include <unistd.h>
85 #ifdef HAVE_SYS_FILE_H
86 #include <sys/file.h>
87 #endif
88 #endif
89 #ifdef __APPLE__
90 #include <CoreFoundation/CoreFoundation.h>
91 #include <IOKit/IOKitLib.h>
92 #include <IOKit/serial/IOSerialKeys.h>
93 #include <IOKit/serial/ioss.h>
94 #include <sys/syslimits.h>
95 #include <mach/mach_time.h>
96 #if MAC_OS_X_VERSION_MAX_ALLOWED < 120000 /* Before macOS 12 */
97 #define kIOMainPortDefault kIOMasterPortDefault
98 #endif
99 #endif
100 #ifdef __linux__
101 #include <dirent.h>
102 /* Android only has linux/serial.h from platform 21 onwards. */
103 #if !(defined(__ANDROID__) && (__ANDROID_API__ < 21))
104 #include <linux/serial.h>
105 #endif
106 #include "linux_termios.h"
108 /* TCGETX/TCSETX is not available everywhere. */
109 #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_STRUCT_TERMIOX)
110 #define USE_TERMIOX
111 #endif
112 #endif
114 /* TIOCINQ/TIOCOUTQ is not available everywhere. */
115 #if !defined(TIOCINQ) && defined(FIONREAD)
116 #define TIOCINQ FIONREAD
117 #endif
118 #if !defined(TIOCOUTQ) && defined(FIONWRITE)
119 #define TIOCOUTQ FIONWRITE
120 #endif
123 * O_CLOEXEC is not available everywhere, fallback to not setting the
124 * flag on those systems.
126 #ifndef _WIN32
127 #ifndef O_CLOEXEC
128 #define O_CLOEXEC 0
129 #endif
130 #endif
132 /* Non-standard baudrates are not available everywhere. */
133 #if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && HAVE_DECL_BOTHER
134 #define USE_TERMIOS_SPEED
135 #endif
137 struct sp_port {
138 char *name;
139 char *description;
140 enum sp_transport transport;
141 int usb_bus;
142 int usb_address;
143 int usb_vid;
144 int usb_pid;
145 char *usb_manufacturer;
146 char *usb_product;
147 char *usb_serial;
148 char *bluetooth_address;
149 #ifdef _WIN32
150 char *usb_path;
151 HANDLE hdl;
152 COMMTIMEOUTS timeouts;
153 OVERLAPPED write_ovl;
154 OVERLAPPED read_ovl;
155 OVERLAPPED wait_ovl;
156 DWORD events;
157 BYTE *write_buf;
158 DWORD write_buf_size;
159 BOOL writing;
160 BOOL wait_running;
161 BOOL last_wait_thread_exited;
162 #else
163 int fd;
164 #endif
167 struct sp_port_config {
168 int baudrate;
169 int bits;
170 enum sp_parity parity;
171 int stopbits;
172 enum sp_rts rts;
173 enum sp_cts cts;
174 enum sp_dtr dtr;
175 enum sp_dsr dsr;
176 enum sp_xonxoff xon_xoff;
179 struct port_data {
180 #ifdef _WIN32
181 DCB dcb;
182 #else
183 struct termios term;
184 int controlbits;
185 int termiox_supported;
186 int rts_flow;
187 int cts_flow;
188 int dtr_flow;
189 int dsr_flow;
190 #endif
193 #ifdef _WIN32
194 typedef HANDLE event_handle;
195 #else
196 typedef int event_handle;
197 #endif
199 /* Standard baud rates. */
200 #ifdef _WIN32
201 #define BAUD_TYPE DWORD
202 #define BAUD(n) {CBR_##n, n}
203 #else
204 #define BAUD_TYPE speed_t
205 #define BAUD(n) {B##n, n}
206 #endif
208 struct std_baudrate {
209 BAUD_TYPE index;
210 int value;
213 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
215 extern void (*sp_debug_handler)(const char *format, ...);
217 /* Debug output macros. */
218 #define DEBUG_FMT(fmt, ...) do { \
219 if (sp_debug_handler) \
220 sp_debug_handler(fmt ".\n", __VA_ARGS__); \
221 } while (0)
222 #define DEBUG(msg) DEBUG_FMT(msg, NULL)
223 #define DEBUG_ERROR(err, msg) DEBUG_FMT("%s returning " #err ": " msg, __func__)
224 #define DEBUG_FAIL(msg) do { \
225 char *errmsg = sp_last_error_message(); \
226 DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
227 sp_free_error_message(errmsg); \
228 } while (0);
229 #define RETURN() do { \
230 DEBUG_FMT("%s returning", __func__); \
231 return; \
232 } while (0)
233 #define RETURN_CODE(x) do { \
234 DEBUG_FMT("%s returning " #x, __func__); \
235 return x; \
236 } while (0)
237 #define RETURN_CODEVAL(x) do { \
238 switch (x) { \
239 case SP_OK: RETURN_CODE(SP_OK); \
240 case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \
241 case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \
242 case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \
243 case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \
244 default: RETURN_CODE(SP_ERR_FAIL); \
246 } while (0)
247 #define RETURN_OK() RETURN_CODE(SP_OK);
248 #define RETURN_ERROR(err, msg) do { \
249 DEBUG_ERROR(err, msg); \
250 return err; \
251 } while (0)
252 #define RETURN_FAIL(msg) do { \
253 DEBUG_FAIL(msg); \
254 return SP_ERR_FAIL; \
255 } while (0)
256 #define RETURN_INT(x) do { \
257 int _x = x; \
258 DEBUG_FMT("%s returning %d", __func__, _x); \
259 return _x; \
260 } while (0)
261 #define RETURN_STRING(x) do { \
262 char *_x = x; \
263 DEBUG_FMT("%s returning %s", __func__, _x); \
264 return _x; \
265 } while (0)
266 #define RETURN_POINTER(x) do { \
267 void *_x = x; \
268 DEBUG_FMT("%s returning %p", __func__, _x); \
269 return _x; \
270 } while (0)
271 #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
272 #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
273 #define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__)
274 #define TRACE_VOID() DEBUG_FMT("%s() called", __func__)
276 #define TRY(x) do { int retval = x; if (retval != SP_OK) RETURN_CODEVAL(retval); } while (0)
278 SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname);
280 /* OS-specific Helper functions. */
281 SP_PRIV enum sp_return get_port_details(struct sp_port *port);
282 SP_PRIV enum sp_return list_ports(struct sp_port ***list);
284 /* Timing abstraction */
286 struct time {
287 #ifdef _WIN32
288 int64_t ticks;
289 #else
290 struct timeval tv;
291 #endif
294 struct timeout {
295 unsigned int ms, limit_ms;
296 struct time start, now, end, delta, delta_max;
297 struct timeval delta_tv;
298 bool calls_started, overflow;
301 SP_PRIV void time_get(struct time *time);
302 SP_PRIV void time_set_ms(struct time *time, unsigned int ms);
303 SP_PRIV void time_add(const struct time *a, const struct time *b, struct time *result);
304 SP_PRIV void time_sub(const struct time *a, const struct time *b, struct time *result);
305 SP_PRIV bool time_greater(const struct time *a, const struct time *b);
306 SP_PRIV void time_as_timeval(const struct time *time, struct timeval *tv);
307 SP_PRIV unsigned int time_as_ms(const struct time *time);
308 SP_PRIV void timeout_start(struct timeout *timeout, unsigned int timeout_ms);
309 SP_PRIV void timeout_limit(struct timeout *timeout, unsigned int limit_ms);
310 SP_PRIV bool timeout_check(struct timeout *timeout);
311 SP_PRIV void timeout_update(struct timeout *timeout);
312 SP_PRIV struct timeval *timeout_timeval(struct timeout *timeout);
313 SP_PRIV unsigned int timeout_remaining_ms(struct timeout *timeout);
315 #endif