/usr/include/barry/usbwrap.h is in libbarry-dev 0.15-1.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | ///
/// \file usbwrap.h
/// USB API wrapper
///
/*
Copyright (C) 2005-2009, Chris Frey
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License in the COPYING file at the
root directory of this project for more details.
*/
#ifndef __SB_USBWRAP_H__
#define __SB_USBWRAP_H__
#include "dll.h"
#include <usb.h>
#include <vector>
#include <map>
#include "error.h"
#define USBWRAP_DEFAULT_TIMEOUT 30000
namespace Barry { class Data; }
/// Namespace for the libusb-related wrapper classes. This namespace
/// may change in the future.
namespace Usb {
/// \addtogroup exceptions
/// @{
/// Thrown on low level USB errors.
class BXEXPORT Error : public Barry::Error
{
int m_libusb_errcode;
public:
Error(const std::string &str);
Error(int libusb_errcode, const std::string &str);
// can return 0 in some case, if unknown error code
int libusb_errcode() const { return m_libusb_errcode; }
};
class BXEXPORT Timeout : public Error
{
public:
Timeout(const std::string &str) : Error(str) {}
Timeout(int libusb_errcode, const std::string &str)
: Error(libusb_errcode, str) {}
};
/// @}
/// Typedefs used by the wrapper class, in the hope to make it
/// easier to switch from libusb stable to devel and back.
typedef struct usb_device* DeviceIDType;
typedef struct usb_dev_handle* DeviceHandleType;
class BXEXPORT Match
{
private:
struct usb_bus *m_busses;
struct usb_device *m_dev;
int m_vendor, m_product;
int m_lasterror;
const char *m_busname;
const char *m_devname;
protected:
static bool ToNum(const char *str, long &num);
static bool NameCompare(const char *n1, const char *n2);
public:
Match(int vendor, int product,
const char *busname = 0, const char *devname = 0);
~Match();
// searches for next match, and if found, fills devid with
// something you can pass on to DeviceDiscover, etc
// returns true if next is found, false if no more
bool next_device(Usb::DeviceIDType *devid);
};
class BXEXPORT Device
{
private:
Usb::DeviceIDType m_id;
Usb::DeviceHandleType m_handle;
int m_timeout;
int m_lasterror;
public:
Device(Usb::DeviceIDType id, int timeout = USBWRAP_DEFAULT_TIMEOUT);
~Device();
/////////////////////////////
// Data access
Usb::DeviceIDType GetID() const { return m_id; }
Usb::DeviceHandleType GetHandle() const { return m_handle; }
int GetLastError() const { return m_lasterror; } //< not thread safe...
//< use the error code stored in the exceptions to track
//< errors in threaded usage
/////////////////////////////
// Device manipulation
bool SetConfiguration(unsigned char cfg);
bool ClearHalt(int ep);
bool Reset();
/////////////////////////////
// IO functions
bool BulkRead(int ep, Barry::Data &data, int timeout = -1);
bool BulkWrite(int ep, const Barry::Data &data, int timeout = -1);
bool BulkWrite(int ep, const void *data, size_t size, int timeout = -1);
bool InterruptRead(int ep, Barry::Data &data, int timeout = -1);
bool InterruptWrite(int ep, const Barry::Data &data, int timeout = -1);
void BulkDrain(int ep, int timeout = 100);
/////////////////////////////
// Combo functions
bool GetConfiguration(unsigned char &cfg);
};
class BXEXPORT Interface
{
Device &m_dev;
int m_iface;
public:
Interface(Device &dev, int iface);
~Interface();
};
// Map of Endpoint numbers (not indexes) to endpoint descriptors
struct BXEXPORT EndpointPair
{
unsigned char read;
unsigned char write;
unsigned char type;
EndpointPair() : read(0), write(0), type(0xff) {}
bool IsTypeSet() const { return type != 0xff; }
bool IsComplete() const { return read && write && IsTypeSet(); }
};
class BXEXPORT EndpointDiscovery : public std::map<unsigned char, usb_endpoint_descriptor>
{
friend class InterfaceDiscovery;
public:
typedef std::map<unsigned char, usb_endpoint_descriptor>base_type;
typedef std::vector<EndpointPair> endpoint_array_type;
private:
bool m_valid;
endpoint_array_type m_endpoints;
BXLOCAL bool Discover(struct usb_interface_descriptor *interface, int epcount);
public:
EndpointDiscovery() : m_valid(false) {}
bool IsValid() const { return m_valid; }
const endpoint_array_type & GetEndpointPairs() const { return m_endpoints; }
};
// Map of Interface numbers (not indexes) to interface descriptors and endpoint map
struct BXEXPORT InterfaceDesc
{
usb_interface_descriptor desc;
EndpointDiscovery endpoints;
};
class BXEXPORT InterfaceDiscovery : public std::map<int, InterfaceDesc>
{
public:
typedef std::map<int, InterfaceDesc> base_type;
private:
bool m_valid;
BXLOCAL bool DiscoverInterface(struct usb_interface *interface);
public:
InterfaceDiscovery() : m_valid(false) {}
bool Discover(Usb::DeviceIDType devid, int cfgidx, int ifcount);
bool IsValid() const { return m_valid; }
};
// Map of Config numbers (not indexes) to config descriptors and interface map
struct BXEXPORT ConfigDesc
{
usb_config_descriptor desc;
InterfaceDiscovery interfaces;
};
class BXEXPORT ConfigDiscovery : public std::map<unsigned char, ConfigDesc>
{
public:
typedef std::map<unsigned char, ConfigDesc> base_type;
private:
bool m_valid;
public:
ConfigDiscovery() : m_valid(false) {}
bool Discover(Usb::DeviceIDType devid, int cfgcount);
bool IsValid() const { return m_valid; }
};
// Discovers all configurations, interfaces, and endpoints for a given device
class BXEXPORT DeviceDiscovery
{
bool m_valid;
public:
usb_device_descriptor desc;
ConfigDiscovery configs;
public:
DeviceDiscovery(Usb::DeviceIDType devid);
bool Discover(Usb::DeviceIDType devid);
bool IsValid() const { return m_valid; }
};
} // namespace Usb
#endif
|