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 */
32 #include <sys/types.h>
47 #define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
48 static const GUID name = { l,w1,w2,{ b1,b2,b3,b4,b5,b6,b7,b8 } }
54 #include <sys/ioctl.h>
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>
69 #include "linux/serial.h"
71 #include "linux_termios.h"
73 /* TCGETX/TCSETX is not available everywhere. */
74 #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_STRUCT_TERMIOX)
79 /* TIOCINQ/TIOCOUTQ is not available everywhere. */
80 #if !defined(TIOCINQ) && defined(FIONREAD)
81 #define TIOCINQ FIONREAD
83 #if !defined(TIOCOUTQ) && defined(FIONWRITE)
84 #define TIOCOUTQ FIONWRITE
87 /* Non-standard baudrates are not available everywhere. */
88 #if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && HAVE_DECL_BOTHER
89 #define USE_TERMIOS_SPEED
95 enum sp_transport transport;
100 char *usb_manufacturer;
103 char *bluetooth_address;
107 COMMTIMEOUTS timeouts;
108 OVERLAPPED write_ovl;
120 struct sp_port_config {
123 enum sp_parity parity;
129 enum sp_xonxoff xon_xoff;
138 int termiox_supported;
147 typedef HANDLE event_handle;
149 typedef int event_handle;
152 /* Standard baud rates. */
154 #define BAUD_TYPE DWORD
155 #define BAUD(n) {CBR_##n, n}
157 #define BAUD_TYPE speed_t
158 #define BAUD(n) {B##n, n}
161 struct std_baudrate {
166 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
168 extern void (*sp_debug_handler)(const char *format, ...);
170 /* Debug output macros. */
171 #define DEBUG_FMT(fmt, ...) do { \
172 if (sp_debug_handler) \
173 sp_debug_handler(fmt ".\n", __VA_ARGS__); \
175 #define DEBUG(msg) DEBUG_FMT(msg, NULL)
176 #define DEBUG_ERROR(err, msg) DEBUG_FMT("%s returning " #err ": " msg, __func__)
177 #define DEBUG_FAIL(msg) do { \
178 char *errmsg = sp_last_error_message(); \
179 DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
180 sp_free_error_message(errmsg); \
182 #define RETURN() do { \
183 DEBUG_FMT("%s returning", __func__); \
186 #define RETURN_CODE(x) do { \
187 DEBUG_FMT("%s returning " #x, __func__); \
190 #define RETURN_CODEVAL(x) do { \
192 case SP_OK: RETURN_CODE(SP_OK); \
193 case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \
194 case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \
195 case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \
196 case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \
197 default: RETURN_CODE(SP_ERR_FAIL); \
200 #define RETURN_OK() RETURN_CODE(SP_OK);
201 #define RETURN_ERROR(err, msg) do { \
202 DEBUG_ERROR(err, msg); \
205 #define RETURN_FAIL(msg) do { \
207 return SP_ERR_FAIL; \
209 #define RETURN_INT(x) do { \
211 DEBUG_FMT("%s returning %d", __func__, _x); \
214 #define RETURN_STRING(x) do { \
216 DEBUG_FMT("%s returning %s", __func__, _x); \
219 #define RETURN_POINTER(x) do { \
221 DEBUG_FMT("%s returning %p", __func__, _x); \
224 #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
225 #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
226 #define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__)
227 #define TRACE_VOID() DEBUG_FMT("%s() called", __func__)
229 #define TRY(x) do { int retval = x; if (retval != SP_OK) RETURN_CODEVAL(retval); } while (0)
231 SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname);
233 /* OS-specific Helper functions. */
234 SP_PRIV enum sp_return get_port_details(struct sp_port *port);
235 SP_PRIV enum sp_return list_ports(struct sp_port ***list);