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