USBUART
A library for reading/wring data via USB-UART adapters
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules
usbuart.hpp
Go to the documentation of this file.
1 
5 /* This file is part of USBUART Library. http://usbuart.info/
6  *
7  * Copyright (C) 2016 Eugene Hutorny <eugene@hutorny.in.ua>
8  *
9  * The USBUART Library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License v2
11  * as published by the Free Software Foundation;
12  *
13  * The USBUART Library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with the USBUART Library; if not, see
20  * <http://www.gnu.org/licenses/gpl-2.0.html>.
21  */
22 
23 #ifndef USBUART_HPP_
24 #define USBUART_HPP_
25 
26 #include "usbuart.h"
27 
28 #include <cstdint>
29 
30 extern "C" {
31  struct libusb_device_handle;
32  struct libusb_transfer;
33  struct libusb_context;
34 }
35 
36 namespace usbuart {
37 
38 typedef uint32_t time_us_t;
39 typedef uint16_t size_t;
40 struct interface {
41  uint8_t ep_bulk_in;
42  uint8_t ep_bulk_out;
43  uint16_t chunk_size;
44 };
45 
46 static inline bool operator!(const device_id& id) noexcept { return id.vid; }
47 
55 class driver {
56 public:
57  virtual const interface& getifc() const noexcept =0;
61  virtual void setup(const eia_tia_232_info&) const throw(error_t) =0;
65  virtual void setbaudrate(baudrate_t) const throw(error_t) =0;
69  virtual void reset() const throw(error_t) =0;
73  virtual void sendbreak() const throw(error_t) =0;
79  virtual void read_callback(libusb_transfer* xfer, size_t& pos) noexcept =0;
83  virtual void write_callback(libusb_transfer* writexfer) noexcept =0;
88  virtual void prepare_write(libusb_transfer* xfer) throw(error_t) =0;
92  virtual libusb_device_handle * handle() const noexcept =0;
93 
94  virtual ~driver() noexcept {}
95 
104  class factory {
105  public:
106  factory() noexcept;
107  virtual driver* create(libusb_device_handle*, uint8_t =0)
108  const throw(error_t) =0;
109  virtual ~factory() noexcept;
110  // static driver* create(libusb_device_handle*) noexcept;
111  static device_id devid(libusb_device_handle*) noexcept;
112 
113  static inline constexpr uint32_t devid32(uint16_t vid, uint16_t pid) noexcept{
114  return (((uint32_t)vid)<<16)|pid;
115  }
116  static inline constexpr uint32_t devid32(const device_id& dev) noexcept{
117  return (((uint32_t)dev.vid)<<16)|dev.pid;
118  }
119  };
120 
121 };
122 
126 class generic : public driver {
127 public:
128  static constexpr unsigned default_timeout = 5000;
129  ~generic() noexcept;
130  void read_callback(libusb_transfer*, size_t& pos) noexcept { pos = 0; }
131  void write_callback(libusb_transfer*) noexcept { }
132  void prepare_write(libusb_transfer*) throw(error_t) {};
133  const interface& getifc() const noexcept { return ifc; }
134  void sendbreak() const throw(error_t) { throw error_t::not_implemented; }
135  void reset() const throw(error_t) { }
136  libusb_device_handle * handle() const noexcept { return dev; }
137 protected:
138  inline generic(libusb_device_handle* handle, const interface& _ifc,
139  uint8_t num = 0) throw(error_t) : dev(handle), ifc(_ifc), ifcnum(num),
140  timeout(default_timeout) {
141  claim_interface();
142  }
143  void setup(const eia_tia_232_info&) const throw(error_t) {}
144  void control(uint8_t, uint8_t, void*, size_t) const throw(error_t);
145  void write_cv(uint8_t r, uint16_t v, uint16_t i) const throw(error_t);
146  void read_cv(uint8_t, uint16_t, uint8_t&) const throw(error_t);
147  void read_cv(uint8_t, uint16_t, uint16_t&) const throw(error_t);
148  void claim_interface() const throw(error_t);
149  void release_interface() const noexcept;
150 protected:
151  libusb_device_handle* const dev;
152  interface const & ifc;
153  const uint8_t ifcnum;
154  unsigned timeout;
155 };
156 
157 
161 template<class C, typename T, size_t N>
162 static inline constexpr size_t countof(T (C::*)[N]) noexcept { return N; }
163 
167 template<typename T, size_t N>
168 static inline constexpr size_t countof(T (&)[N]) noexcept { return N; }
169 
170 /*****************************************************************************/
171 
172 class Log {
173 public:
174  void e(const char * tag, const char *fmt, ...) noexcept
175  __attribute__ ((format (printf, 3, 4)));
176  void w(const char * tag, const char *fmt, ...) noexcept
177  __attribute__ ((format (printf, 3, 4)));
178  void i(const char * tag, const char *fmt, ...) noexcept
179  __attribute__ ((format (printf, 3, 4)));
180  void d(const char * tag, const char *fmt, ...) noexcept
181  __attribute__ ((format (printf, 3, 4)));
182  loglevel_t level;
183 };
184 
185 extern Log log;
186 
187 }
188 
189 #if defined(__GNUC__) && defined(DEBUG)
190 # define __ __PRETTY_FUNCTION__
191 #else
192 # define __ ("usbuart")
193 #endif
194 
195 #endif /* USBUART_HPP_ */
virtual void reset() const =0
set baud rate only, keep other protocol properties intact
virtual void prepare_write(libusb_transfer *xfer)=0
called before first byte is actually written to xfer buffer so that the driver can place hardware spe...
Driver factory registrar TO register a factory:
Definition: usbuart.hpp:104
uint32_t baudrate_t
Baud rate data type.
Definition: usbuart.h:35
virtual void setbaudrate(baudrate_t) const =0
set baud rate only, keep other protocol properties intact
API header for USBUART Library.
void write_callback(libusb_transfer *) noexcept
called on write transfer completion
Definition: usbuart.hpp:131
uint16_t vid
Vendor ID.
Definition: usbuart.h:92
uint16_t pid
Product ID.
Definition: usbuart.h:93
libusb_device_handle * handle() const noexcept
Returns handle of associated USB device.
Definition: usbuart.hpp:136
void setup(const eia_tia_232_info &) const
setup protocol on the hardware level
Definition: usbuart.hpp:143
USBUART namespace.
Definition: usbuart.h:29
Device ID (Vendor ID/Product ID).
Definition: usbuart.h:91
void sendbreak() const
Send break.
Definition: usbuart.hpp:134
virtual void read_callback(libusb_transfer *xfer, size_t &pos) noexcept=0
called on read transfer completion must fill pos with position of first payload data return true if t...
void prepare_write(libusb_transfer *)
called before first byte is actually written to xfer buffer so that the driver can place hardware spe...
Definition: usbuart.hpp:132
method not implemented in this lib
USB-to-UART driver interface.
Definition: usbuart.hpp:55
error_t
API Error codes.
Definition: usbuart.h:177
virtual libusb_device_handle * handle() const noexcept=0
Returns handle of associated USB device.
void read_callback(libusb_transfer *, size_t &pos) noexcept
called on read transfer completion must fill pos with position of first payload data return true if t...
Definition: usbuart.hpp:130
virtual void write_callback(libusb_transfer *writexfer) noexcept=0
called on write transfer completion
implementation of common driver methods
Definition: usbuart.hpp:126
void reset() const
set baud rate only, keep other protocol properties intact
Definition: usbuart.hpp:135
virtual void sendbreak() const =0
Send break.
virtual void setup(const eia_tia_232_info &) const =0
setup protocol on the hardware level