UsbDevice.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2014-2020, Dataspeed Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Dataspeed Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #ifndef USB_DEVICE_H_
36 #define USB_DEVICE_H_
37 
38 #include <stdexcept>
39 #include <string>
40 #include <boost/function.hpp>
41 #include <boost/thread.hpp>
42 
43 struct libusb_device_handle;
44 struct libusb_context;
45 
46 namespace lusb
47 {
48 
49 struct UsbDeviceException : public std::runtime_error
50 {
52  UsbDeviceException(int code, const char* msg) :
53  std::runtime_error(msg), error_code_(code)
54  {
55  }
56 };
57 
58 class UsbDevice
59 {
60 public:
61  UsbDevice(uint16_t vid, uint16_t pid, uint8_t mi);
62  UsbDevice(uint16_t vid, uint16_t pid);
63  UsbDevice();
64  ~UsbDevice();
65 
66  struct UsbIds {
67  UsbIds() : vid(0), pid(0) {}
68  UsbIds(uint16_t _vid, uint16_t _pid) : vid(_vid), pid(_pid) {}
69  uint16_t vid, pid;
70  };
71 
72  class Location {
73  public:
74  Location() : loc(0) {}
75  Location(uint8_t _bus, uint8_t _port = 0, uint8_t _addr = 0, uint16_t _vid = 0, uint16_t _pid = 0) : loc(0) {
76  bus = _bus;
77  addr = _addr;
78  port = _port;
79  vid = _vid;
80  pid = _pid;
81  }
82  static bool match(const Location& a, const Location& b) {
83  // Equal to each other or zero
84  return !((a.bus && b.bus && (a.bus != b.bus )) ||
85  (a.addr && b.addr && (a.addr != b.addr)) ||
86  (a.port && b.port && (a.port != b.port)));
87  }
88  bool match(const Location& other) const {
89  return match(*this, other);
90  }
91  bool operator==(const Location& other) const {
92  return loc == other.loc;
93  }
94  bool operator!=(const Location& other) const {
95  return loc != other.loc;
96  }
97  bool operator<(const Location& other) const {
98  return loc < other.loc;
99  }
100  bool operator>(const Location& other) const {
101  return loc > other.loc;
102  }
103  bool operator<=(const Location& other) const {
104  return loc <= other.loc;
105  }
106  bool operator>=(const Location& other) const {
107  return loc >= other.loc;
108  }
109  union { // Zero for don't care
110  uint32_t loc;
111  struct {
112  uint8_t bus; // Bus number
113  uint8_t addr; // Address, unique for each bus, new on reconnect
114  uint8_t port; // Port on parent hub, not unique for each bus
115  uint8_t :8;
116  };
117  };
118  uint16_t vid; // Vendor Id
119  uint16_t pid; // Product Id
120  };
121  static void listDevices(const std::vector<UsbIds> &ids, std::vector<Location> &list);
122  static void listDevices(uint16_t vid, uint16_t pid, std::vector<Location> &list);
123  void listDevices(std::vector<Location> &list) const;
124 
125  void setDevceIds(uint16_t vid, uint16_t pid, uint8_t mi);
126  bool open(const Location &location = Location());
127  void close();
128 
129  bool isOpen() const { return open_; }
130  Location getLocation() const { return location_; }
131 
132  bool bulkWrite(const void * data, int size, unsigned char endpoint, int timeout);
133  int bulkRead(void * data, int size, unsigned char endpoint, int timeout);
134  bool interruptWrite(const void * data, int size, unsigned char endpoint, int timeout);
135  int interruptRead(void * data, int size, unsigned char endpoint, int timeout);
136 
137  typedef boost::function<void(const void *data, int size)> Callback;
138  void startBulkReadThread(Callback callback, unsigned char endpoint);
139  void stopBulkReadThread(unsigned char endpoint);
140  void startInterruptReadThread(Callback callback, unsigned char endpoint);
141  void stopInterruptReadThread(unsigned char endpoint);
142 
144  int getLastError(std::string &str) const;
145  void setDebugLevel(uint8_t level);
146 
147 private:
148  bool handleError(int err);
149  void throwError(int err);
150  void init();
151  void closeDevice();
152 
153  void bulkReadThread(Callback callback, unsigned char endpoint);
154  void interruptReadThread(Callback callback, unsigned char endpoint);
155 
157  std::string error_str_;
158 
159  uint16_t vid_;
160  uint16_t pid_;
161  uint8_t mi_;
162  bool open_;
164  libusb_device_handle *libusb_handle_;
165  libusb_context *ctx_;
166  boost::thread bulk_threads_[128];
167  bool bulk_threads_enable_[128];
168  boost::thread interrupt_threads_[128];
169  bool interrupt_threads_enable_[128];
170 
171 };
172 } //namespace lusb
173 
174 #endif /* USB_DEVICE_H_ */
bool operator==(const Location &other) const
Definition: UsbDevice.h:91
Location location_
Definition: UsbDevice.h:163
std::string error_str_
Definition: UsbDevice.h:157
bool operator<=(const Location &other) const
Definition: UsbDevice.h:103
Location(uint8_t _bus, uint8_t _port=0, uint8_t _addr=0, uint16_t _vid=0, uint16_t _pid=0)
Definition: UsbDevice.h:75
Definition: UsbDevice.h:46
libusb_device_handle * libusb_handle_
Definition: UsbDevice.h:164
bool operator<(const Location &other) const
Definition: UsbDevice.h:97
static bool match(const Location &a, const Location &b)
Definition: UsbDevice.h:82
bool operator!=(const Location &other) const
Definition: UsbDevice.h:94
bool operator>(const Location &other) const
Definition: UsbDevice.h:100
bool isOpen() const
Definition: UsbDevice.h:129
Location getLocation() const
Definition: UsbDevice.h:130
uint16_t vid_
Definition: UsbDevice.h:159
bool operator>=(const Location &other) const
Definition: UsbDevice.h:106
UsbDeviceException(int code, const char *msg)
Definition: UsbDevice.h:52
boost::function< void(const void *data, int size)> Callback
Definition: UsbDevice.h:137
uint16_t pid_
Definition: UsbDevice.h:160
UsbIds(uint16_t _vid, uint16_t _pid)
Definition: UsbDevice.h:68
bool throw_errors_
Definition: UsbDevice.h:143
bool match(const Location &other) const
Definition: UsbDevice.h:88
libusb_context * ctx_
Definition: UsbDevice.h:165


lusb
Author(s): Kevin Hallenbeck
autogenerated on Fri Dec 11 2020 03:47:26