USBUART
A library for reading/wring data via USB-UART adapters
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules
usbuart.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2016 Eugene Hutorny <eugene@hutorny.in.ua>
5  *
6  * This file is part of USBUART Library. http://hutorny.in.ua/projects/usbuart
7  *
8  * The USBUART Library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License v2
10  * as published by the Free Software Foundation;
11  *
12  * The USBUART Library 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.
15  * See the GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with the USBUART Library; if not, see
19  * <http://www.gnu.org/licenses/gpl-2.0.html>.
20  */
21 
22 
23 #ifndef USBUART_H_
24 #define USBUART_H_
25 #ifdef __cplusplus
26 #include <cstdint>
27 struct libusb_context;
29 namespace usbuart {
30 #else
31 #include <stdint.h>
32 #endif
33 
35 typedef uint32_t baudrate_t;
36 
38 typedef enum parity_enum {
39  none,
40  odd,
41  even,
42  mark,
43  space
44 } parity_t;
45 
47 typedef enum stop_bits_enum {
48  one,
49  _1_5,
50  two
51 } stop_bits_t;
52 
54 typedef enum flow_control_enum {
55  none_,
56  rts_cts,
57  dtr_dsr,
58  xon_xoff
60 
62  baudrate_t baudrate;
63  uint8_t databits;
67 };
68 
70 struct channel {
71  int fd_read;
72  int fd_write;
73 };
74 
76 typedef enum status_enum {
77  read_pipe_ok = 1,
78  write_pipe_ok = 2,
79  usb_dev_ok = 4,
80  alles_gute = read_pipe_ok | write_pipe_ok | usb_dev_ok
81 } status_t;
82 
84 struct device_addr {
85  uint8_t busid;
86  uint8_t devid;
87  uint8_t ifc;
88 };
89 
91 struct device_id {
92  uint16_t vid;
93  uint16_t pid;
94  uint8_t ifc;
95 };
96 
97 #ifdef __cplusplus
98 extern "C" {
99 #endif
100 
107 extern int usbuart_pipe_byaddr(struct device_addr ba,
108  struct channel* ch, const struct eia_tia_232_info* pi);
109 
116 extern struct channel usbuart_pipe_bydevid(struct device_id,
117  struct channel* ch, const struct eia_tia_232_info*);
118 
125 extern int usbuart_attach_byaddr(struct device_addr, struct channel,
126  const struct eia_tia_232_info*);
127 
134 extern int usbuart_attach_bydevid(struct device_id id, struct channel ch,
135  const struct eia_tia_232_info* pi);
136 
138 extern int usbuart_status(struct channel);
139 
141 extern void usbuart_close(struct channel);
142 
144 extern void usbuart_reset(struct channel);
145 
147 extern void usbuart_break(struct channel);
149 extern int usbuart_loop(int timeout);
150 
151 #ifdef __cplusplus
152 } /* extern "C" */
153 
154 
155 enum class loglevel_t {
156  silent,
157  error,
158  warning,
159  info,
160  debug,
161 };
162 
163 
164 /*
165  * Error codes encapsulated in enum class to avoid name clashing
166  * Values of this enum are also used as exception codes.
167  * Rationale for using type exception vs. class exceptions:
168  * - avoid construction of exception object
169  * Accepted compromises of this approach - no generalization based on
170  * polymorfism, no ability to introduce a new exception
171  * Exceptions are purely internal, they do not leave API boundaries,
172  * instead exception is converted to the result code it contains
173  */
177 enum class error_t {
178  success,
179  no_channels,
181  invalid_param,
182  no_channel,
183  no_access,
184  not_supported,
185  no_device,
186  no_interface,
188  libusb_error,
189  usb_error,
190  device_error,
191  bad_baudrate,
193  control_error,
194  io_error,
195  fcntl_error,
196  poll_error,
197  pipe_error,
198  out_of_memory,
199  jni_error,
201 };
202 
206 static inline constexpr int operator-(error_t v) noexcept {
207  return - static_cast<int>(v);
208 }
209 
213 static inline constexpr int operator+(error_t v) noexcept {
214  return + static_cast<int>(v);
215 }
216 
217 static constexpr channel bad_channel { -1, -1 };
218 static constexpr eia_tia_232_info _115200_8N1n {115200,8,none,one,none_};
219 static constexpr eia_tia_232_info _115200_8N1r {115200,8,none,one,rts_cts};
220 static constexpr eia_tia_232_info _19200_8N1n { 19200,8,none,one,none_};
221 static constexpr eia_tia_232_info _19200_8N1r { 19200,8,none,one,rts_cts};
222 
226 class context {
227 public:
229  context() throw(error_t);
230  ~context() noexcept;
231 
238  int attach(device_id id, channel ch, const eia_tia_232_info& pi) noexcept;
239 
246  int attach(device_addr ba, channel ch, const eia_tia_232_info& pi) noexcept;
247 
254  int pipe(device_id id, channel& ch, const eia_tia_232_info& pi) noexcept;
255 
262  int pipe(device_addr ba,channel& ch, const eia_tia_232_info& pi) noexcept;
263 
265  void close(channel) noexcept;
266 
268  int reset(channel) noexcept;
269 
271  int status(channel) noexcept;
272 
274  int sendbreak(channel) noexcept;
275 
279  int loop(int timeout) noexcept;
280 
282  libusb_context* native() noexcept;
284  static context& instance() noexcept;
286  static loglevel_t setloglevel(loglevel_t lvl) noexcept;
287  class backend;
288 private:
289  backend * const priv;
290 };
291 
292 }
293 #else
294 static const struct eia_tia_232_info _115200_8N1n = {115200,8,none,one,none_};
295 static const struct eia_tia_232_info _115200_8N1r = {115200,8,none,one,rts_cts};
296 static const struct eia_tia_232_info _19200_8N1n = { 19200,8,none,one,none_};
297 static const struct eia_tia_232_info _19200_8N1r = { 19200,8,none,one,rts_cts};
298 #endif
299 
300 #endif /* USBUART_H_ */
requested interface busy
parity_t parity
parity
Definition: usbuart.h:64
device is not supported
I/O error on an attached file.
poll returned EINVAL
static loglevel_t setloglevel(loglevel_t lvl) noexcept
Set logging level.
Definition: core.cpp:1026
int sendbreak(channel) noexcept
Send RS232 break signal to the USB device.
Definition: core.cpp:985
context()
context constructor allocates a libusb context
Definition: core.cpp:928
void close(channel) noexcept
Close channel, detaches files from USB device.
Definition: core.cpp:955
uint8_t ifc
Interface number.
Definition: usbuart.h:87
hardware level error
fcntl failed on an attached file
uint32_t baudrate_t
Baud rate data type.
Definition: usbuart.h:35
context has nor more live channels
enum usbuart::flow_control_enum flow_control_t
Flow control enum.
Device address in terms bus ID, device number.
Definition: usbuart.h:84
uint16_t vid
Vendor ID.
Definition: usbuart.h:92
stop_bits_enum
Stop bits enum.
Definition: usbuart.h:47
uint16_t pid
Product ID.
Definition: usbuart.h:93
I/O channel, represented by a pair of file descriptors.
Definition: usbuart.h:70
enum usbuart::status_enum status_t
Channel status flags.
int loop(int timeout) noexcept
Run libusb and async I/O message loops.
Definition: core.cpp:996
device does not exist
void usbuart_reset(struct channel)
Resets USB device.
Definition: capi.cpp:57
uint8_t devid
Device Number.
Definition: usbuart.h:86
USBUART namespace.
Definition: usbuart.h:29
void usbuart_close(struct channel)
Close pipes and detach USB device.
Definition: capi.cpp:52
Device ID (Vendor ID/Product ID).
Definition: usbuart.h:91
control transfer error
parity_enum
Parity enum.
Definition: usbuart.h:38
enum usbuart::stop_bits_enum stop_bits_t
Stop bits enum.
uint8_t databits
number of data bits
Definition: usbuart.h:63
stop_bits_t stopbits
stop bits
Definition: usbuart.h:65
a JNI error occurred
baudrate_t baudrate
UART baud rate.
Definition: usbuart.h:62
int usbuart_attach_bydevid(struct device_id id, struct channel ch, const struct eia_tia_232_info *pi)
Attach pair of file descriptors to the USB device using VID/PID.
int fd_read
file descriptor to read from
Definition: usbuart.h:71
enum usbuart::parity_enum parity_t
Parity enum.
claim interface failed
invalid param passed to the API
libusb_context * native() noexcept
Returns native LIBUSB context.
Definition: core.cpp:1015
USBUART API facade class.
Definition: usbuart.h:226
memory allocation failed
requested channel does not exist
int reset(channel) noexcept
Reset USB device.
Definition: core.cpp:964
int usbuart_loop(int timeout)
Run libusb and async I/O message loops.
Definition: capi.cpp:66
device returned unexpected value while probing
method not implemented in this lib
int attach(device_id id, channel ch, const eia_tia_232_info &pi) noexcept
Attach pair of file descriptors to the USB device using VID/PID.
Definition: core.cpp:935
uint8_t ifc
Interface number.
Definition: usbuart.h:94
error_t
API Error codes.
Definition: usbuart.h:177
status_enum
Channel status flags.
Definition: usbuart.h:76
access permission denied
uint8_t busid
USB Bus ID.
Definition: usbuart.h:85
flow_control_enum
Flow control enum.
Definition: usbuart.h:54
int usbuart_status(struct channel)
Returns channel status as combination of status_t bits.
int status(channel) noexcept
Returns combination of status_t bit or negative on error.
Definition: core.cpp:975
flow_control_t flowcontrol
flow control
Definition: usbuart.h:66
failed to create a pipe
static context & instance() noexcept
Returns a singleton context instance.
Definition: core.cpp:1021
int fd_write
file descriptor to write to
Definition: usbuart.h:72
int usbuart_pipe_byaddr(struct device_addr ba, struct channel *ch, const struct eia_tia_232_info *pi)
Create two pipes and attach their ends to the USB device using BUS/ADDR.
Definition: capi.cpp:31
int pipe(device_id id, channel &ch, const eia_tia_232_info &pi) noexcept
Create two pipes and attach their ends to the USB device using VID/PID.
Definition: core.cpp:944
unsupported baud rate
void usbuart_break(struct channel)
Send RS232 break signal to the USB device.
Definition: capi.cpp:62
int usbuart_attach_byaddr(struct device_addr, struct channel, const struct eia_tia_232_info *)
Attach pair of file descriptors to the USB device using BUS/ADDR.
Definition: capi.cpp:41