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