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