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