2 * This file is part of the libserialport project.
4 * Copyright (C) 2014 Martin Ling <martin-libserialport@earth.li>
5 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
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.
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.
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/>.
21 #ifndef LIBSERIALPORT_LIBSERIALPORT_INTERNAL_H
22 #define LIBSERIALPORT_LIBSERIALPORT_INTERNAL_H
26 /* For timeradd, timersub, timercmp, realpath. */
27 #define _BSD_SOURCE 1 /* for glibc < 2.19 */
28 #define _DEFAULT_SOURCE 1 /* for glibc >= 2.20 */
29 /* For clock_gettime and associated types. */
30 #define _POSIX_C_SOURCE 199309L
34 #include <sys/types.h>
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 } }
52 #include "windows_ddk.h"
56 #include <sys/ioctl.h>
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>
67 #include <mach/mach_time.h>
72 #include "linux/serial.h"
74 #include "linux_termios.h"
76 /* TCGETX/TCSETX is not available everywhere. */
77 #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_STRUCT_TERMIOX)
82 /* TIOCINQ/TIOCOUTQ is not available everywhere. */
83 #if !defined(TIOCINQ) && defined(FIONREAD)
84 #define TIOCINQ FIONREAD
86 #if !defined(TIOCOUTQ) && defined(FIONWRITE)
87 #define TIOCOUTQ FIONWRITE
91 * O_CLOEXEC is not available everywhere, fallback to not setting the
92 * flag on those systems.
100 /* Non-standard baudrates are not available everywhere. */
101 #if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && HAVE_DECL_BOTHER
102 #define USE_TERMIOS_SPEED
108 enum sp_transport transport;
113 char *usb_manufacturer;
116 char *bluetooth_address;
120 COMMTIMEOUTS timeouts;
121 OVERLAPPED write_ovl;
126 DWORD write_buf_size;
134 struct sp_port_config {
137 enum sp_parity parity;
143 enum sp_xonxoff xon_xoff;
152 int termiox_supported;
161 typedef HANDLE event_handle;
163 typedef int event_handle;
166 /* Standard baud rates. */
168 #define BAUD_TYPE DWORD
169 #define BAUD(n) {CBR_##n, n}
171 #define BAUD_TYPE speed_t
172 #define BAUD(n) {B##n, n}
175 struct std_baudrate {
180 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
182 extern void (*sp_debug_handler)(const char *format, ...);
184 /* Debug output macros. */
185 #define DEBUG_FMT(fmt, ...) do { \
186 if (sp_debug_handler) \
187 sp_debug_handler(fmt ".\n", __VA_ARGS__); \
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 { \
192 char *errmsg = sp_last_error_message(); \
193 DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
194 sp_free_error_message(errmsg); \
196 #define RETURN() do { \
197 DEBUG_FMT("%s returning", __func__); \
200 #define RETURN_CODE(x) do { \
201 DEBUG_FMT("%s returning " #x, __func__); \
204 #define RETURN_CODEVAL(x) do { \
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); \
211 default: RETURN_CODE(SP_ERR_FAIL); \
214 #define RETURN_OK() RETURN_CODE(SP_OK);
215 #define RETURN_ERROR(err, msg) do { \
216 DEBUG_ERROR(err, msg); \
219 #define RETURN_FAIL(msg) do { \
221 return SP_ERR_FAIL; \
223 #define RETURN_INT(x) do { \
225 DEBUG_FMT("%s returning %d", __func__, _x); \
228 #define RETURN_STRING(x) do { \
230 DEBUG_FMT("%s returning %s", __func__, _x); \
233 #define RETURN_POINTER(x) do { \
235 DEBUG_FMT("%s returning %p", __func__, _x); \
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)
240 #define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__)
241 #define TRACE_VOID() DEBUG_FMT("%s() called", __func__)
243 #define TRY(x) do { int retval = x; if (retval != SP_OK) RETURN_CODEVAL(retval); } while (0)
245 SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname);
247 /* OS-specific Helper functions. */
248 SP_PRIV enum sp_return get_port_details(struct sp_port *port);
249 SP_PRIV enum sp_return list_ports(struct sp_port ***list);