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