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