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/>.
22 #define _BSD_SOURCE // for timeradd, timersub, timercmp
26 #include <sys/types.h>
46 #include <sys/ioctl.h>
52 #include <CoreFoundation/CoreFoundation.h>
53 #include <IOKit/IOKitLib.h>
54 #include <IOKit/serial/IOSerialKeys.h>
55 #include <IOKit/serial/ioss.h>
56 #include <sys/syslimits.h>
61 #include "linux/serial.h"
63 #include "linux_termios.h"
65 /* TCGETX/TCSETX is not available everywhere. */
66 #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX)
71 /* TIOCINQ/TIOCOUTQ is not available everywhere. */
72 #if !defined(TIOCINQ) && defined(FIONREAD)
73 #define TIOCINQ FIONREAD
75 #if !defined(TIOCOUTQ) && defined(FIONWRITE)
76 #define TIOCOUTQ FIONWRITE
79 /* Non-standard baudrates are not available everywhere. */
80 #if defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)
81 #define USE_TERMIOS_SPEED
87 enum sp_transport transport;
92 char *usb_manufacturer;
95 char *bluetooth_address;
99 COMMTIMEOUTS timeouts;
100 OVERLAPPED write_ovl;
111 struct sp_port_config {
114 enum sp_parity parity;
120 enum sp_xonxoff xon_xoff;
129 int termiox_supported;
138 typedef HANDLE event_handle;
140 typedef int event_handle;
143 /* Standard baud rates. */
145 #define BAUD_TYPE DWORD
146 #define BAUD(n) {CBR_##n, n}
148 #define BAUD_TYPE speed_t
149 #define BAUD(n) {B##n, n}
152 struct std_baudrate {
157 extern const struct std_baudrate std_baudrates[];
159 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
160 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
162 extern void (*sp_debug_handler)(const char *format, ...);
164 /* Debug output macros. */
165 #define DEBUG_FMT(fmt, ...) do { \
166 if (sp_debug_handler) \
167 sp_debug_handler(fmt ".\n", __VA_ARGS__); \
169 #define DEBUG(msg) DEBUG_FMT(msg, NULL)
170 #define DEBUG_ERROR(err, msg) DEBUG_FMT("%s returning " #err ": " msg, __func__)
171 #define DEBUG_FAIL(msg) do { \
172 char *errmsg = sp_last_error_message(); \
173 DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
174 sp_free_error_message(errmsg); \
176 #define RETURN() do { \
177 DEBUG_FMT("%s returning", __func__); \
180 #define RETURN_CODE(x) do { \
181 DEBUG_FMT("%s returning " #x, __func__); \
184 #define RETURN_CODEVAL(x) do { \
186 case SP_OK: RETURN_CODE(SP_OK); \
187 case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \
188 case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \
189 case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \
190 case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \
193 #define RETURN_OK() RETURN_CODE(SP_OK);
194 #define RETURN_ERROR(err, msg) do { \
195 DEBUG_ERROR(err, msg); \
198 #define RETURN_FAIL(msg) do { \
200 return SP_ERR_FAIL; \
202 #define RETURN_INT(x) do { \
204 DEBUG_FMT("%s returning %d", __func__, _x); \
207 #define RETURN_STRING(x) do { \
209 DEBUG_FMT("%s returning %s", __func__, _x); \
212 #define RETURN_POINTER(x) do { \
214 DEBUG_FMT("%s returning %p", __func__, _x); \
217 #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
218 #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
219 #define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__)
220 #define TRACE_VOID() DEBUG_FMT("%s() called", __func__)
222 #define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0)
224 SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname);
226 /* OS-specific Helper functions. */
227 SP_PRIV enum sp_return get_port_details(struct sp_port *port);
228 SP_PRIV enum sp_return list_ports(struct sp_port ***list);