]> sigrok.org Git - libserialport.git/blame - libserialport_internal.h
Open the file descriptor of a serial port on POSIX systems exclusively
[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
e919e2ef
ML
24/* These MSVC-specific defines must appear before other headers.*/
25#ifdef _MSC_VER
26#define _CRT_NONSTDC_NO_DEPRECATE
27#define _CRT_SECURE_NO_WARNINGS
28#endif
7c081505 29
2be41b12
ML
30/* These feature test macros must appear before other headers.*/
31#if defined(__linux__) || defined(__CYGWIN__)
95bad38c 32/* For timeradd, timersub, timercmp, realpath. */
4d8195fe
WS
33#define _BSD_SOURCE 1 /* for glibc < 2.19 */
34#define _DEFAULT_SOURCE 1 /* for glibc >= 2.20 */
277f832a
ML
35/* For clock_gettime and associated types. */
36#define _POSIX_C_SOURCE 199309L
c51b846e
ML
37#endif
38
e919e2ef
ML
39#ifdef LIBSERIALPORT_ATBUILD
40/* If building with autoconf, include the generated config.h. */
41#include <config.h>
42#endif
43
44#ifdef LIBSERIALPORT_MSBUILD
45/* If building with MS tools, define necessary things that
46 would otherwise appear in config.h. */
47#define SP_PRIV
48#endif
49
50#include "libserialport.h"
51
e33dcf90
ML
52#include <string.h>
53#include <sys/types.h>
54#include <sys/stat.h>
55#include <fcntl.h>
e33dcf90
ML
56#include <stdlib.h>
57#include <errno.h>
58#include <stdio.h>
59#include <stdarg.h>
9a7945af 60#include <stdbool.h>
3317d678 61#include <stdint.h>
e33dcf90
ML
62#ifdef _WIN32
63#include <windows.h>
64#include <tchar.h>
65#include <setupapi.h>
66#include <cfgmgr32.h>
78940e69
ML
67#undef DEFINE_GUID
68#define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
69 static const GUID name = { l,w1,w2,{ b1,b2,b3,b4,b5,b6,b7,b8 } }
e33dcf90 70#include <usbioctl.h>
d179da05 71#include <usbiodef.h>
e9d78d82
ML
72/* The largest size that can be passed to WriteFile() safely
73 * on any architecture. This arises from the expression:
74 * PAGE_SIZE * (65535 - sizeof(MDL)) / sizeof(ULONG_PTR)
75 * and this worst-case value is found on x64. */
76#define WRITEFILE_MAX_SIZE 33525760
e33dcf90
ML
77#else
78#include <limits.h>
79#include <termios.h>
80#include <sys/ioctl.h>
81#include <sys/time.h>
f40ea9d4 82#include <time.h>
e33dcf90 83#include <poll.h>
fdbb55ae 84#include <unistd.h>
ffbfc5c7
LV
85#ifdef HAVE_SYS_FILE_H
86#include <sys/file.h>
87#endif
e33dcf90
ML
88#endif
89#ifdef __APPLE__
90#include <CoreFoundation/CoreFoundation.h>
91#include <IOKit/IOKitLib.h>
92#include <IOKit/serial/IOSerialKeys.h>
93#include <IOKit/serial/ioss.h>
94#include <sys/syslimits.h>
192e7749 95#include <mach/mach_time.h>
e33dcf90
ML
96#endif
97#ifdef __linux__
98#include <dirent.h>
abd31fd9
ML
99/* Android only has linux/serial.h from platform 21 onwards. */
100#if !(defined(__ANDROID__) && (__ANDROID_API__ < 21))
101#include <linux/serial.h>
e33dcf90
ML
102#endif
103#include "linux_termios.h"
104
105/* TCGETX/TCSETX is not available everywhere. */
f1c916ed 106#if defined(TCGETX) && defined(TCSETX) && defined(HAVE_STRUCT_TERMIOX)
e33dcf90
ML
107#define USE_TERMIOX
108#endif
109#endif
110
111/* TIOCINQ/TIOCOUTQ is not available everywhere. */
112#if !defined(TIOCINQ) && defined(FIONREAD)
113#define TIOCINQ FIONREAD
114#endif
115#if !defined(TIOCOUTQ) && defined(FIONWRITE)
116#define TIOCOUTQ FIONWRITE
117#endif
118
fa106ef1
CS
119/*
120 * O_CLOEXEC is not available everywhere, fallback to not setting the
121 * flag on those systems.
122 */
123#ifndef _WIN32
124#ifndef O_CLOEXEC
125#define O_CLOEXEC 0
126#endif
127#endif
128
e33dcf90 129/* Non-standard baudrates are not available everywhere. */
6c811582 130#if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && HAVE_DECL_BOTHER
e33dcf90
ML
131#define USE_TERMIOS_SPEED
132#endif
133
134struct sp_port {
135 char *name;
136 char *description;
137 enum sp_transport transport;
138 int usb_bus;
139 int usb_address;
140 int usb_vid;
141 int usb_pid;
142 char *usb_manufacturer;
143 char *usb_product;
144 char *usb_serial;
145 char *bluetooth_address;
146#ifdef _WIN32
147 char *usb_path;
148 HANDLE hdl;
149 COMMTIMEOUTS timeouts;
150 OVERLAPPED write_ovl;
151 OVERLAPPED read_ovl;
152 OVERLAPPED wait_ovl;
153 DWORD events;
39df7833
ML
154 BYTE *write_buf;
155 DWORD write_buf_size;
e33dcf90 156 BOOL writing;
47fcf8ec 157 BOOL wait_running;
e33dcf90
ML
158#else
159 int fd;
160#endif
161};
162
163struct sp_port_config {
164 int baudrate;
165 int bits;
166 enum sp_parity parity;
167 int stopbits;
168 enum sp_rts rts;
169 enum sp_cts cts;
170 enum sp_dtr dtr;
171 enum sp_dsr dsr;
172 enum sp_xonxoff xon_xoff;
173};
174
175struct port_data {
176#ifdef _WIN32
177 DCB dcb;
178#else
179 struct termios term;
180 int controlbits;
181 int termiox_supported;
182 int rts_flow;
183 int cts_flow;
184 int dtr_flow;
185 int dsr_flow;
186#endif
187};
188
189#ifdef _WIN32
190typedef HANDLE event_handle;
191#else
192typedef int event_handle;
193#endif
194
195/* Standard baud rates. */
196#ifdef _WIN32
197#define BAUD_TYPE DWORD
198#define BAUD(n) {CBR_##n, n}
199#else
200#define BAUD_TYPE speed_t
201#define BAUD(n) {B##n, n}
202#endif
203
204struct std_baudrate {
205 BAUD_TYPE index;
206 int value;
207};
208
e33dcf90 209#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
e33dcf90
ML
210
211extern void (*sp_debug_handler)(const char *format, ...);
212
213/* Debug output macros. */
7890cef6
ML
214#define DEBUG_FMT(fmt, ...) do { \
215 if (sp_debug_handler) \
216 sp_debug_handler(fmt ".\n", __VA_ARGS__); \
217} while (0)
218#define DEBUG(msg) DEBUG_FMT(msg, NULL)
219#define DEBUG_ERROR(err, msg) DEBUG_FMT("%s returning " #err ": " msg, __func__)
220#define DEBUG_FAIL(msg) do { \
e33dcf90 221 char *errmsg = sp_last_error_message(); \
7890cef6 222 DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
e33dcf90
ML
223 sp_free_error_message(errmsg); \
224} while (0);
7890cef6
ML
225#define RETURN() do { \
226 DEBUG_FMT("%s returning", __func__); \
227 return; \
dc422c04 228} while (0)
7890cef6
ML
229#define RETURN_CODE(x) do { \
230 DEBUG_FMT("%s returning " #x, __func__); \
231 return x; \
232} while (0)
e33dcf90
ML
233#define RETURN_CODEVAL(x) do { \
234 switch (x) { \
b344a40b
UH
235 case SP_OK: RETURN_CODE(SP_OK); \
236 case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \
237 case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \
238 case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \
239 case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \
e019e72e 240 default: RETURN_CODE(SP_ERR_FAIL); \
e33dcf90
ML
241 } \
242} while (0)
243#define RETURN_OK() RETURN_CODE(SP_OK);
7890cef6
ML
244#define RETURN_ERROR(err, msg) do { \
245 DEBUG_ERROR(err, msg); \
246 return err; \
247} while (0)
248#define RETURN_FAIL(msg) do { \
249 DEBUG_FAIL(msg); \
250 return SP_ERR_FAIL; \
251} while (0)
9caa2e86
ML
252#define RETURN_INT(x) do { \
253 int _x = x; \
7890cef6 254 DEBUG_FMT("%s returning %d", __func__, _x); \
9caa2e86
ML
255 return _x; \
256} while (0)
257#define RETURN_STRING(x) do { \
258 char *_x = x; \
7890cef6 259 DEBUG_FMT("%s returning %s", __func__, _x); \
9caa2e86
ML
260 return _x; \
261} while (0)
262#define RETURN_POINTER(x) do { \
263 void *_x = x; \
7890cef6 264 DEBUG_FMT("%s returning %p", __func__, _x); \
e33dcf90
ML
265 return _x; \
266} while (0)
267#define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
268#define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
7890cef6
ML
269#define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__)
270#define TRACE_VOID() DEBUG_FMT("%s() called", __func__)
e33dcf90 271
613c48f1 272#define TRY(x) do { int retval = x; if (retval != SP_OK) RETURN_CODEVAL(retval); } while (0)
e33dcf90 273
970f279a 274SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname);
48a4076f 275
e33dcf90 276/* OS-specific Helper functions. */
970f279a
AJ
277SP_PRIV enum sp_return get_port_details(struct sp_port *port);
278SP_PRIV enum sp_return list_ports(struct sp_port ***list);
2b40f814 279
39acdc47
ML
280/* Timing abstraction */
281
282struct time {
283#ifdef _WIN32
284 int64_t ticks;
285#else
286 struct timeval tv;
287#endif
288};
289
290struct timeout {
291 unsigned int ms, limit_ms;
292 struct time start, now, end, delta, delta_max;
293 struct timeval delta_tv;
294 bool calls_started, overflow;
295};
296
297SP_PRIV void time_get(struct time *time);
298SP_PRIV void time_set_ms(struct time *time, unsigned int ms);
299SP_PRIV void time_add(const struct time *a, const struct time *b, struct time *result);
300SP_PRIV void time_sub(const struct time *a, const struct time *b, struct time *result);
301SP_PRIV bool time_greater(const struct time *a, const struct time *b);
302SP_PRIV void time_as_timeval(const struct time *time, struct timeval *tv);
303SP_PRIV unsigned int time_as_ms(const struct time *time);
304SP_PRIV void timeout_start(struct timeout *timeout, unsigned int timeout_ms);
305SP_PRIV void timeout_limit(struct timeout *timeout, unsigned int limit_ms);
306SP_PRIV bool timeout_check(struct timeout *timeout);
307SP_PRIV void timeout_update(struct timeout *timeout);
308SP_PRIV struct timeval *timeout_timeval(struct timeout *timeout);
309SP_PRIV unsigned int timeout_remaining_ms(struct timeout *timeout);
310
2b40f814 311#endif