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