]> sigrok.org Git - libserialport.git/blame_incremental - libserialport_internal.h
examples/send_receive: Fix receive check.
[libserialport.git] / libserialport_internal.h
... / ...
CommitLineData
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/* 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
29
30/* These feature test macros must appear before other headers.*/
31#if defined(__linux__) || defined(__CYGWIN__)
32/* For timeradd, timersub, timercmp, realpath. */
33#define _BSD_SOURCE 1 /* for glibc < 2.19 */
34#define _DEFAULT_SOURCE 1 /* for glibc >= 2.20 */
35/* For clock_gettime and associated types. */
36#define _POSIX_C_SOURCE 199309L
37#endif
38
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
52#include <string.h>
53#include <sys/types.h>
54#include <sys/stat.h>
55#include <fcntl.h>
56#include <stdlib.h>
57#include <errno.h>
58#include <stdio.h>
59#include <stdarg.h>
60#include <stdbool.h>
61#include <stdint.h>
62#ifdef _WIN32
63#include <windows.h>
64#include <tchar.h>
65#include <setupapi.h>
66#include <cfgmgr32.h>
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 } }
70#include <usbioctl.h>
71#include <usbiodef.h>
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
77#else
78#include <limits.h>
79#include <termios.h>
80#include <sys/ioctl.h>
81#include <sys/time.h>
82#include <time.h>
83#include <poll.h>
84#include <unistd.h>
85#endif
86#ifdef __APPLE__
87#include <CoreFoundation/CoreFoundation.h>
88#include <IOKit/IOKitLib.h>
89#include <IOKit/serial/IOSerialKeys.h>
90#include <IOKit/serial/ioss.h>
91#include <sys/syslimits.h>
92#include <mach/mach_time.h>
93#endif
94#ifdef __linux__
95#include <dirent.h>
96/* Android only has linux/serial.h from platform 21 onwards. */
97#if !(defined(__ANDROID__) && (__ANDROID_API__ < 21))
98#include <linux/serial.h>
99#endif
100#include "linux_termios.h"
101
102/* TCGETX/TCSETX is not available everywhere. */
103#if defined(TCGETX) && defined(TCSETX) && defined(HAVE_STRUCT_TERMIOX)
104#define USE_TERMIOX
105#endif
106#endif
107
108/* TIOCINQ/TIOCOUTQ is not available everywhere. */
109#if !defined(TIOCINQ) && defined(FIONREAD)
110#define TIOCINQ FIONREAD
111#endif
112#if !defined(TIOCOUTQ) && defined(FIONWRITE)
113#define TIOCOUTQ FIONWRITE
114#endif
115
116/*
117 * O_CLOEXEC is not available everywhere, fallback to not setting the
118 * flag on those systems.
119 */
120#ifndef _WIN32
121#ifndef O_CLOEXEC
122#define O_CLOEXEC 0
123#endif
124#endif
125
126/* Non-standard baudrates are not available everywhere. */
127#if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && HAVE_DECL_BOTHER
128#define USE_TERMIOS_SPEED
129#endif
130
131struct sp_port {
132 char *name;
133 char *description;
134 enum sp_transport transport;
135 int usb_bus;
136 int usb_address;
137 int usb_vid;
138 int usb_pid;
139 char *usb_manufacturer;
140 char *usb_product;
141 char *usb_serial;
142 char *bluetooth_address;
143#ifdef _WIN32
144 char *usb_path;
145 HANDLE hdl;
146 COMMTIMEOUTS timeouts;
147 OVERLAPPED write_ovl;
148 OVERLAPPED read_ovl;
149 OVERLAPPED wait_ovl;
150 DWORD events;
151 BYTE *write_buf;
152 DWORD write_buf_size;
153 BOOL writing;
154 BOOL wait_running;
155#else
156 int fd;
157#endif
158};
159
160struct sp_port_config {
161 int baudrate;
162 int bits;
163 enum sp_parity parity;
164 int stopbits;
165 enum sp_rts rts;
166 enum sp_cts cts;
167 enum sp_dtr dtr;
168 enum sp_dsr dsr;
169 enum sp_xonxoff xon_xoff;
170};
171
172struct port_data {
173#ifdef _WIN32
174 DCB dcb;
175#else
176 struct termios term;
177 int controlbits;
178 int termiox_supported;
179 int rts_flow;
180 int cts_flow;
181 int dtr_flow;
182 int dsr_flow;
183#endif
184};
185
186#ifdef _WIN32
187typedef HANDLE event_handle;
188#else
189typedef int event_handle;
190#endif
191
192/* Standard baud rates. */
193#ifdef _WIN32
194#define BAUD_TYPE DWORD
195#define BAUD(n) {CBR_##n, n}
196#else
197#define BAUD_TYPE speed_t
198#define BAUD(n) {B##n, n}
199#endif
200
201struct std_baudrate {
202 BAUD_TYPE index;
203 int value;
204};
205
206#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
207
208extern void (*sp_debug_handler)(const char *format, ...);
209
210/* Debug output macros. */
211#define DEBUG_FMT(fmt, ...) do { \
212 if (sp_debug_handler) \
213 sp_debug_handler(fmt ".\n", __VA_ARGS__); \
214} while (0)
215#define DEBUG(msg) DEBUG_FMT(msg, NULL)
216#define DEBUG_ERROR(err, msg) DEBUG_FMT("%s returning " #err ": " msg, __func__)
217#define DEBUG_FAIL(msg) do { \
218 char *errmsg = sp_last_error_message(); \
219 DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
220 sp_free_error_message(errmsg); \
221} while (0);
222#define RETURN() do { \
223 DEBUG_FMT("%s returning", __func__); \
224 return; \
225} while (0)
226#define RETURN_CODE(x) do { \
227 DEBUG_FMT("%s returning " #x, __func__); \
228 return x; \
229} while (0)
230#define RETURN_CODEVAL(x) do { \
231 switch (x) { \
232 case SP_OK: RETURN_CODE(SP_OK); \
233 case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \
234 case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \
235 case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \
236 case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \
237 default: RETURN_CODE(SP_ERR_FAIL); \
238 } \
239} while (0)
240#define RETURN_OK() RETURN_CODE(SP_OK);
241#define RETURN_ERROR(err, msg) do { \
242 DEBUG_ERROR(err, msg); \
243 return err; \
244} while (0)
245#define RETURN_FAIL(msg) do { \
246 DEBUG_FAIL(msg); \
247 return SP_ERR_FAIL; \
248} while (0)
249#define RETURN_INT(x) do { \
250 int _x = x; \
251 DEBUG_FMT("%s returning %d", __func__, _x); \
252 return _x; \
253} while (0)
254#define RETURN_STRING(x) do { \
255 char *_x = x; \
256 DEBUG_FMT("%s returning %s", __func__, _x); \
257 return _x; \
258} while (0)
259#define RETURN_POINTER(x) do { \
260 void *_x = x; \
261 DEBUG_FMT("%s returning %p", __func__, _x); \
262 return _x; \
263} while (0)
264#define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
265#define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
266#define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__)
267#define TRACE_VOID() DEBUG_FMT("%s() called", __func__)
268
269#define TRY(x) do { int retval = x; if (retval != SP_OK) RETURN_CODEVAL(retval); } while (0)
270
271SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname);
272
273/* OS-specific Helper functions. */
274SP_PRIV enum sp_return get_port_details(struct sp_port *port);
275SP_PRIV enum sp_return list_ports(struct sp_port ***list);
276
277/* Timing abstraction */
278
279struct time {
280#ifdef _WIN32
281 int64_t ticks;
282#else
283 struct timeval tv;
284#endif
285};
286
287struct timeout {
288 unsigned int ms, limit_ms;
289 struct time start, now, end, delta, delta_max;
290 struct timeval delta_tv;
291 bool calls_started, overflow;
292};
293
294SP_PRIV void time_get(struct time *time);
295SP_PRIV void time_set_ms(struct time *time, unsigned int ms);
296SP_PRIV void time_add(const struct time *a, const struct time *b, struct time *result);
297SP_PRIV void time_sub(const struct time *a, const struct time *b, struct time *result);
298SP_PRIV bool time_greater(const struct time *a, const struct time *b);
299SP_PRIV void time_as_timeval(const struct time *time, struct timeval *tv);
300SP_PRIV unsigned int time_as_ms(const struct time *time);
301SP_PRIV void timeout_start(struct timeout *timeout, unsigned int timeout_ms);
302SP_PRIV void timeout_limit(struct timeout *timeout, unsigned int limit_ms);
303SP_PRIV bool timeout_check(struct timeout *timeout);
304SP_PRIV void timeout_update(struct timeout *timeout);
305SP_PRIV struct timeval *timeout_timeval(struct timeout *timeout);
306SP_PRIV unsigned int timeout_remaining_ms(struct timeout *timeout);
307
308#endif