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