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