]> sigrok.org Git - libserialport.git/blob - libserialport_internal.h
78b727b7a2dbbb7a506786092c77e9a8e0e372f9
[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, ...) 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); \
164 } while (0);
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 { \
168         switch (x) { \
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); \
174         } \
175 } while (0)
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_INT(x) do { \
180         int _x = x; \
181         DEBUG("%s returning %d", __func__, _x); \
182         return _x; \
183 } while (0)
184 #define RETURN_STRING(x) do { \
185         char *_x = x; \
186         DEBUG("%s returning %s", __func__, _x); \
187         return _x; \
188 } while (0)
189 #define RETURN_POINTER(x) do { \
190         void *_x = x; \
191         DEBUG("%s returning %p", __func__, _x); \
192         return _x; \
193 } while (0)
194 #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
195 #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
196 #define TRACE(fmt, ...) DEBUG("%s(" fmt ") called", __func__, ##__VA_ARGS__)
197
198 #define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0)
199
200 SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname);
201
202 /* OS-specific Helper functions. */
203 SP_PRIV enum sp_return get_port_details(struct sp_port *port);
204 SP_PRIV enum sp_return list_ports(struct sp_port ***list);