handle-libusb.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 #include "types.h"
7 #include "context-libusb.h"
8 
9 #include <chrono>
10 
11 #include <libusb.h>
12 
13 namespace librealsense
14 {
15  namespace platform
16  {
18  {
19  switch (sts)
20  {
21  case LIBUSB_SUCCESS: return RS2_USB_STATUS_SUCCESS;
22  case LIBUSB_ERROR_IO: return RS2_USB_STATUS_IO;
23  case LIBUSB_ERROR_INVALID_PARAM: return RS2_USB_STATUS_INVALID_PARAM;
24  case LIBUSB_ERROR_ACCESS: return RS2_USB_STATUS_ACCESS;
25  case LIBUSB_ERROR_NO_DEVICE: return RS2_USB_STATUS_NO_DEVICE;
26  case LIBUSB_ERROR_NOT_FOUND: return RS2_USB_STATUS_NOT_FOUND;
27  case LIBUSB_ERROR_BUSY: return RS2_USB_STATUS_BUSY;
28  case LIBUSB_ERROR_TIMEOUT: return RS2_USB_STATUS_TIMEOUT;
29  case LIBUSB_ERROR_OVERFLOW: return RS2_USB_STATUS_OVERFLOW;
30  case LIBUSB_ERROR_PIPE: return RS2_USB_STATUS_PIPE;
31  case LIBUSB_ERROR_INTERRUPTED: return RS2_USB_STATUS_INTERRUPTED;
32  case LIBUSB_ERROR_NO_MEM: return RS2_USB_STATUS_NO_MEM;
33  case LIBUSB_ERROR_NOT_SUPPORTED: return RS2_USB_STATUS_NOT_SUPPORTED;
34  case LIBUSB_ERROR_OTHER: return RS2_USB_STATUS_OTHER;
35  default: return RS2_USB_STATUS_OTHER;
36  }
37  }
38 
40  {
41  public:
42  handle_libusb(std::shared_ptr<usb_context> context, libusb_device* device, std::shared_ptr<usb_interface_libusb> interface) :
43  _first_interface(interface), _context(context), _handle(nullptr)
44  {
45  auto sts = libusb_open(device, &_handle);
46  if(sts != LIBUSB_SUCCESS)
47  {
48  auto rs_sts = libusb_status_to_rs(sts);
49  std::stringstream msg;
50  msg << "failed to open usb interface: " << (int)interface->get_number() << ", error: " << usb_status_to_string.at(rs_sts);
51  LOG_ERROR(msg.str());
52  throw std::runtime_error(msg.str());
53  }
54 
55  claim_interface_or_throw(interface->get_number());
56  for(auto&& i : interface->get_associated_interfaces())
57  claim_interface_or_throw(i->get_number());
58 
59  _context->start_event_handler();
60  }
61 
63  {
64  _context->stop_event_handler();
65  for(auto&& i : _first_interface->get_associated_interfaces())
66  libusb_release_interface(_handle, i->get_number());
67  libusb_close(_handle);
68  }
69 
70  libusb_device_handle* get()
71  {
72  return _handle;
73  }
74 
75  private:
77  {
78  auto rs_sts = claim_interface(interface);
79  if(rs_sts != RS2_USB_STATUS_SUCCESS)
80  throw std::runtime_error(to_string() << "Unable to claim interface " << (int)interface << ", error: " << usb_status_to_string.at(rs_sts));
81  }
82 
84  {
85  //libusb_set_auto_detach_kernel_driver(h, true);
86 
87  if (libusb_kernel_driver_active(_handle, interface) == 1)//find out if kernel driver is attached
88  if (libusb_detach_kernel_driver(_handle, interface) == 0)// detach driver from device if attached.
89  LOG_DEBUG("handle_libusb - detach kernel driver");
90 
91  auto sts = libusb_claim_interface(_handle, interface);
92  if(sts != LIBUSB_SUCCESS)
93  {
94  auto rs_sts = libusb_status_to_rs(sts);
95  LOG_ERROR("failed to claim usb interface: " << (int)interface << ", error: " << usb_status_to_string.at(rs_sts));
96  return rs_sts;
97  }
98 
100  }
101 
102  std::shared_ptr<usb_context> _context;
103  std::shared_ptr<usb_interface_libusb> _first_interface;
104  libusb_device_handle* _handle;
105  };
106  }
107 }
static usb_status libusb_status_to_rs(int sts)
Definition: handle-libusb.h:17
void claim_interface_or_throw(uint8_t interface)
Definition: handle-libusb.h:76
std::shared_ptr< usb_interface_libusb > _first_interface
handle_libusb(std::shared_ptr< usb_context > context, libusb_device *device, std::shared_ptr< usb_interface_libusb > interface)
Definition: handle-libusb.h:42
unsigned char uint8_t
Definition: stdint.h:78
enum librealsense::platform::_usb_status usb_status
static std::map< usb_status, std::string > usb_status_to_string
Definition: usb-types.h:162
usb_status claim_interface(uint8_t interface)
Definition: handle-libusb.h:83
#define LOG_ERROR(...)
Definition: src/types.h:242
int i
std::shared_ptr< usb_context > _context
#define LOG_DEBUG(...)
Definition: src/types.h:239
std::string to_string(T value)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:16