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