Fall back to CLOCK_REALTIME if CLOCK_MONOTONIC not usable.
[libserialport/gsi.git] / libserialport_internal.h
blobdd69733e9f38a0b3b76764d61890fd7dec3c513f
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
25 #ifdef __linux__
26 /* For timeradd, timersub, timercmp, realpath. */
27 #define _BSD_SOURCE 1 /* for glibc < 2.19 */
28 #define _DEFAULT_SOURCE 1 /* for glibc >= 2.20 */
29 #endif
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40 #ifdef _WIN32
41 #include <windows.h>
42 #include <tchar.h>
43 #include <setupapi.h>
44 #include <cfgmgr32.h>
45 #undef DEFINE_GUID
46 #define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
47 static const GUID name = { l,w1,w2,{ b1,b2,b3,b4,b5,b6,b7,b8 } }
48 #include <usbioctl.h>
49 #include <usbiodef.h>
50 #include "windows_ddk.h"
51 #else
52 #include <limits.h>
53 #include <termios.h>
54 #include <sys/ioctl.h>
55 #include <sys/time.h>
56 #include <time.h>
57 #include <poll.h>
58 #endif
59 #ifdef __APPLE__
60 #include <CoreFoundation/CoreFoundation.h>
61 #include <IOKit/IOKitLib.h>
62 #include <IOKit/serial/IOSerialKeys.h>
63 #include <IOKit/serial/ioss.h>
64 #include <sys/syslimits.h>
65 #include <mach/mach_time.h>
66 #endif
67 #ifdef __linux__
68 #include <dirent.h>
69 #ifndef __ANDROID__
70 #include "linux/serial.h"
71 #endif
72 #include "linux_termios.h"
74 /* TCGETX/TCSETX is not available everywhere. */
75 #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_STRUCT_TERMIOX)
76 #define USE_TERMIOX
77 #endif
78 #endif
80 /* TIOCINQ/TIOCOUTQ is not available everywhere. */
81 #if !defined(TIOCINQ) && defined(FIONREAD)
82 #define TIOCINQ FIONREAD
83 #endif
84 #if !defined(TIOCOUTQ) && defined(FIONWRITE)
85 #define TIOCOUTQ FIONWRITE
86 #endif
88 /* Non-standard baudrates are not available everywhere. */
89 #if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && HAVE_DECL_BOTHER
90 #define USE_TERMIOS_SPEED
91 #endif
93 struct sp_port {
94 char *name;
95 char *description;
96 enum sp_transport transport;
97 int usb_bus;
98 int usb_address;
99 int usb_vid;
100 int usb_pid;
101 char *usb_manufacturer;
102 char *usb_product;
103 char *usb_serial;
104 char *bluetooth_address;
105 #ifdef _WIN32
106 char *usb_path;
107 HANDLE hdl;
108 COMMTIMEOUTS timeouts;
109 OVERLAPPED write_ovl;
110 OVERLAPPED read_ovl;
111 OVERLAPPED wait_ovl;
112 DWORD events;
113 BYTE *write_buf;
114 DWORD write_buf_size;
115 BOOL writing;
116 BOOL wait_running;
117 #else
118 int fd;
119 #endif
122 struct sp_port_config {
123 int baudrate;
124 int bits;
125 enum sp_parity parity;
126 int stopbits;
127 enum sp_rts rts;
128 enum sp_cts cts;
129 enum sp_dtr dtr;
130 enum sp_dsr dsr;
131 enum sp_xonxoff xon_xoff;
134 struct port_data {
135 #ifdef _WIN32
136 DCB dcb;
137 #else
138 struct termios term;
139 int controlbits;
140 int termiox_supported;
141 int rts_flow;
142 int cts_flow;
143 int dtr_flow;
144 int dsr_flow;
145 #endif
148 #ifdef _WIN32
149 typedef HANDLE event_handle;
150 #else
151 typedef int event_handle;
152 #endif
154 /* Standard baud rates. */
155 #ifdef _WIN32
156 #define BAUD_TYPE DWORD
157 #define BAUD(n) {CBR_##n, n}
158 #else
159 #define BAUD_TYPE speed_t
160 #define BAUD(n) {B##n, n}
161 #endif
163 struct std_baudrate {
164 BAUD_TYPE index;
165 int value;
168 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
170 extern void (*sp_debug_handler)(const char *format, ...);
172 /* Debug output macros. */
173 #define DEBUG_FMT(fmt, ...) do { \
174 if (sp_debug_handler) \
175 sp_debug_handler(fmt ".\n", __VA_ARGS__); \
176 } while (0)
177 #define DEBUG(msg) DEBUG_FMT(msg, NULL)
178 #define DEBUG_ERROR(err, msg) DEBUG_FMT("%s returning " #err ": " msg, __func__)
179 #define DEBUG_FAIL(msg) do { \
180 char *errmsg = sp_last_error_message(); \
181 DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
182 sp_free_error_message(errmsg); \
183 } while (0);
184 #define RETURN() do { \
185 DEBUG_FMT("%s returning", __func__); \
186 return; \
187 } while (0)
188 #define RETURN_CODE(x) do { \
189 DEBUG_FMT("%s returning " #x, __func__); \
190 return x; \
191 } while (0)
192 #define RETURN_CODEVAL(x) do { \
193 switch (x) { \
194 case SP_OK: RETURN_CODE(SP_OK); \
195 case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \
196 case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \
197 case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \
198 case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \
199 default: RETURN_CODE(SP_ERR_FAIL); \
201 } while (0)
202 #define RETURN_OK() RETURN_CODE(SP_OK);
203 #define RETURN_ERROR(err, msg) do { \
204 DEBUG_ERROR(err, msg); \
205 return err; \
206 } while (0)
207 #define RETURN_FAIL(msg) do { \
208 DEBUG_FAIL(msg); \
209 return SP_ERR_FAIL; \
210 } while (0)
211 #define RETURN_INT(x) do { \
212 int _x = x; \
213 DEBUG_FMT("%s returning %d", __func__, _x); \
214 return _x; \
215 } while (0)
216 #define RETURN_STRING(x) do { \
217 char *_x = x; \
218 DEBUG_FMT("%s returning %s", __func__, _x); \
219 return _x; \
220 } while (0)
221 #define RETURN_POINTER(x) do { \
222 void *_x = x; \
223 DEBUG_FMT("%s returning %p", __func__, _x); \
224 return _x; \
225 } while (0)
226 #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
227 #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
228 #define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__)
229 #define TRACE_VOID() DEBUG_FMT("%s() called", __func__)
231 #define TRY(x) do { int retval = x; if (retval != SP_OK) RETURN_CODEVAL(retval); } while (0)
233 SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname);
235 /* OS-specific Helper functions. */
236 SP_PRIV enum sp_return get_port_details(struct sp_port *port);
237 SP_PRIV enum sp_return list_ports(struct sp_port ***list);
239 #endif