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