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 #include <sys/types.h>
39 #include <sys/ioctl.h>
45 #include <CoreFoundation/CoreFoundation.h>
46 #include <IOKit/IOKitLib.h>
47 #include <IOKit/serial/IOSerialKeys.h>
48 #include <IOKit/serial/ioss.h>
49 #include <sys/syslimits.h>
54 #include "linux/serial.h"
56 #include "linux_termios.h"
58 /* TCGETX/TCSETX is not available everywhere. */
59 #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX)
64 /* TIOCINQ/TIOCOUTQ is not available everywhere. */
65 #if !defined(TIOCINQ) && defined(FIONREAD)
66 #define TIOCINQ FIONREAD
68 #if !defined(TIOCOUTQ) && defined(FIONWRITE)
69 #define TIOCOUTQ FIONWRITE
72 /* Non-standard baudrates are not available everywhere. */
73 #if defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)
74 #define USE_TERMIOS_SPEED
80 enum sp_transport transport;
85 char *usb_manufacturer;
88 char *bluetooth_address;
92 COMMTIMEOUTS timeouts;
104 struct sp_port_config {
107 enum sp_parity parity;
113 enum sp_xonxoff xon_xoff;
122 int termiox_supported;
131 typedef HANDLE event_handle;
133 typedef int event_handle;
136 /* Standard baud rates. */
138 #define BAUD_TYPE DWORD
139 #define BAUD(n) {CBR_##n, n}
141 #define BAUD_TYPE speed_t
142 #define BAUD(n) {B##n, n}
145 struct std_baudrate {
150 extern const struct std_baudrate std_baudrates[];
152 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
153 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
155 extern void (*sp_debug_handler)(const char *format, ...);
157 /* Debug output macros. */
158 #define DEBUG(fmt, ...) do { if (sp_debug_handler) sp_debug_handler(fmt ".\n", ##__VA_ARGS__); } while (0)
159 #define DEBUG_ERROR(err, fmt, ...) DEBUG("%s returning " #err ": " fmt, __func__, ##__VA_ARGS__)
160 #define DEBUG_FAIL(fmt, ...) do { \
161 char *errmsg = sp_last_error_message(); \
162 DEBUG("%s returning SP_ERR_FAIL: "fmt": %s", __func__,##__VA_ARGS__,errmsg); \
163 sp_free_error_message(errmsg); \
165 #define RETURN() do { DEBUG("%s returning", __func__); return; } while(0)
166 #define RETURN_CODE(x) do { DEBUG("%s returning " #x, __func__); return x; } while (0)
167 #define RETURN_CODEVAL(x) do { \
169 case SP_OK: RETURN_CODE(SP_OK); \
170 case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \
171 case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \
172 case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \
173 case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \
176 #define RETURN_OK() RETURN_CODE(SP_OK);
177 #define RETURN_ERROR(err, ...) do { DEBUG_ERROR(err, __VA_ARGS__); return err; } while (0)
178 #define RETURN_FAIL(...) do { DEBUG_FAIL(__VA_ARGS__); return SP_ERR_FAIL; } while (0)
179 #define RETURN_VALUE(fmt, x) do { \
181 DEBUG("%s returning " fmt, __func__, _x); \
184 #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
185 #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
186 #define TRACE(fmt, ...) DEBUG("%s(" fmt ") called", __func__, ##__VA_ARGS__)
188 #define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0)
190 /* OS-specific Helper functions. */
191 enum sp_return get_port_details(struct sp_port *port);