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