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