device-usbhost.cpp
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 #ifdef RS2_USE_ANDROID_BACKEND
5 
6 #include "device-usbhost.h"
7 #include "endpoint-usbhost.h"
8 #include "interface-usbhost.h"
9 #include "usbhost.h"
10 #include "../types.h"
11 
12 #include <string>
13 #include <regex>
14 #include <sstream>
15 #include <mutex>
16 
17 namespace librealsense
18 {
19  namespace platform
20  {
21  usb_device_info generate_info(::usb_device *handle, int mi, usb_spec conn_spec, usb_class cls)
22  {
24  usb_device_info rv{};
25  rv.id = name;
26  rv.unique_id = name;
27  rv.vid = usb_device_get_vendor_id(handle);
28  rv.pid = usb_device_get_product_id(handle);
29  rv.mi = mi;
30  rv.cls = cls;
31  rv.conn_spec = conn_spec;
32  return rv;
33  }
34 
36  _handle(handle)
37  {
39  std::shared_ptr<usb_interface_usbhost> curr_ctrl_intf;
40  ::usb_descriptor_iter_init(handle, &it);
41  usb_descriptor_header *h = usb_descriptor_iter_next(&it);
43  if (h != nullptr && h->bDescriptorType == USB_DT_DEVICE) {
44  usb_device_descriptor *device_descriptor = (usb_device_descriptor *) h;
45  usb_descriptor ud = {h->bLength, h->bDescriptorType, std::vector<uint8_t>(h->bLength)};
46  memcpy(ud.data.data(), h, h->bLength);
47  _descriptors.push_back(ud);
48  conn_spec = librealsense::platform::usb_spec(device_descriptor->bcdUSB);
49  }
50 
51  do {
52  h = usb_descriptor_iter_next(&it);
53  if(h == NULL)
54  break;
55  usb_descriptor ud = {h->bLength, h->bDescriptorType, std::vector<uint8_t>(h->bLength)};
56  memcpy(ud.data.data(), h, h->bLength);
57  _descriptors.push_back(ud);
58  if (h->bDescriptorType == USB_DT_INTERFACE_ASSOCIATION) {
59  auto iad = *(usb_interface_assoc_descriptor *) h;
60  auto info = generate_info(_handle, iad.bFirstInterface, conn_spec,
61  static_cast<usb_class>(iad.bFunctionClass));
62  _infos.push_back(info);
63  }
64  if (h->bDescriptorType == USB_DT_INTERFACE) {
65  auto id = *(::usb_interface_descriptor *) h;
66  auto intf = std::make_shared<usb_interface_usbhost>(id, it);
67  _interfaces.push_back(intf);
68  switch (id.bInterfaceClass)
69  {
71  case RS2_USB_CLASS_HID:
72  {
73  auto info = generate_info(_handle, id.bInterfaceNumber, conn_spec,
74  static_cast<usb_class>(id.bInterfaceClass));
75  _infos.push_back(info);
76  break;
77  }
79  {
80  if(id.bInterfaceSubClass == RS2_USB_SUBCLASS_VIDEO_CONTROL)
81  curr_ctrl_intf = intf;
82  if(id.bInterfaceSubClass == RS2_USB_SUBCLASS_VIDEO_STREAMING)
83  curr_ctrl_intf->add_associated_interface(intf);
84  break;
85  }
86  default:
87  break;
88  }
89  }
90  } while (h != nullptr);
91 
93 
94  for(auto&& i : get_interfaces())
95  {
96  for(auto&& e : i->get_endpoints())
97  {
98  if(e->get_direction() != RS2_USB_ENDPOINT_DIRECTION_READ)
99  continue;
100  auto type = e->get_type();
102  {
103  _dispatchers[e->get_address()] = std::make_shared<dispatcher>(10);
104  auto d = _dispatchers.at(e->get_address());
105  d->start();
106  }
107  }
108  }
109  _dispatcher = std::make_shared<dispatcher>(10);
110  _dispatcher->start();
111  }
112 
114  {
115  for(auto&& d : _dispatchers)
116  {
117  d.second->stop();
118  }
119 
120  if(_dispatcher)
121  {
122  _dispatcher->stop();
123  _dispatcher.reset();
124  }
125  }
126 
127  const rs_usb_interface usb_device_usbhost::get_interface(uint8_t interface_number) const
128  {
129  auto it = std::find_if(_interfaces.begin(), _interfaces.end(),
130  [interface_number](const rs_usb_interface& i) { return interface_number == i->get_number(); });
131  if (it == _interfaces.end())
132  return nullptr;
133  return *it;
134  }
135 
136  const std::shared_ptr<usb_messenger> usb_device_usbhost::open(uint8_t interface_number)
137  {
138  auto i = get_interface(interface_number);
139  if (!i)
140  return nullptr;
141  auto intf = std::dynamic_pointer_cast<usb_interface_usbhost>(i);
142  auto h = std::make_shared<handle_usbhost>(_handle, intf);
143  return std::make_shared<usb_messenger_usbhost>(shared_from_this(), h);
144  }
145 
146  const std::vector<usb_descriptor> usb_device_usbhost::get_descriptors() const
147  {
148  return _descriptors;
149  }
150 
152  {
153  auto nr = reinterpret_cast<::usb_request*>(request->get_native_request());
154  auto req = std::dynamic_pointer_cast<usb_request_usbhost>(request);
155  req->set_active(true);
156  auto sts = usb_request_queue(nr);
157  if(sts < 0)
158  {
159  req->set_active(false);
160  std::string strerr = strerror(errno);
161  LOG_WARNING("usb_request_queue returned error, endpoint: " << (int)request->get_endpoint()->get_address() << " error: " << strerr << ", number: " << (int)errno);
162  return usbhost_status_to_rs(errno);
163  }
164  invoke();
165  return RS2_USB_STATUS_SUCCESS;
166  }
167 
169  {
170  auto nr = reinterpret_cast<::usb_request*>(request->get_native_request());
171  auto sts = usb_request_cancel(nr);
172 
173  if(sts < 0)
174  {
175  std::string strerr = strerror(errno);
176  LOG_WARNING("usb_request_cancel returned error, endpoint: " << (int)request->get_endpoint()->get_address() << ", error: " << strerr << ", number: " << (int)errno);
177  return usbhost_status_to_rs(errno);
178  }
179  return RS2_USB_STATUS_SUCCESS;
180  }
181 
183  {
184  _dispatcher->invoke([&](dispatcher::cancellable_timer c)
185  {
186  auto r = usb_request_wait(get_handle(), -1);
187 
188  if(r)
189  {
190  auto urb = reinterpret_cast<usb_request_usbhost*>(r->client_data);
191 
192  if(urb)
193  {
194  urb->set_active(false);
195 
196  auto response = urb->get_shared();
197  if(response)
198  {
199  auto cb = response->get_callback();
200  auto d = _dispatchers.at(response->get_endpoint()->get_address());
201  d->invoke([cb, response](dispatcher::cancellable_timer t)
202  { cb->callback(response); } );
203  }
204  }
205  }
206  });
207  }
208  }
209 }
210 #endif
std::vector< std::shared_ptr< usb_interface > > _interfaces
Definition: device-libusb.h:29
GLuint const GLchar * name
std::shared_ptr< usb_request > rs_usb_request
Definition: usb-request.h:41
const struct usb_device_descriptor * usb_device_get_device_descriptor(struct usb_device *device)
Definition: usbhost.c:462
std::shared_ptr< usb_interface > rs_usb_interface
Definition: usb-interface.h:31
static usb_status usbhost_status_to_rs(int sts)
const char * usb_device_get_name(struct usb_device *device)
Definition: usbhost.c:422
usb_status cancel_request(const rs_usb_request &request)
#define LOG_WARNING(...)
Definition: src/types.h:241
GLuint64 GLenum void * handle
Definition: glext.h:7785
enum librealsense::platform::_usb_class usb_class
std::shared_ptr< handle_libusb > get_handle(uint8_t interface_number)
#define USB_DT_INTERFACE
Definition: usb-types.h:15
GLsizei const GLchar *const * string
d
Definition: rmse.py:171
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
unsigned char uint8_t
Definition: stdint.h:78
libusb_device_descriptor _usb_device_descriptor
Definition: device-libusb.h:27
e
Definition: rmse.py:177
enum librealsense::platform::_usb_status usb_status
GLenum GLuint id
virtual const rs_usb_interface get_interface(uint8_t interface_number) const override
GLdouble t
def info(name, value, persistent=False)
Definition: test.py:301
virtual const rs_usb_interface get_interface(uint8_t interface_number) const override
void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter)
Definition: usbhost.c:587
const GLubyte * c
Definition: glext.h:12690
uint16_t usb_device_get_vendor_id(struct usb_device *device)
Definition: usbhost.c:450
GLdouble GLdouble r
uint16_t usb_device_get_product_id(struct usb_device *device)
Definition: usbhost.c:456
int usb_request_queue(struct usb_request *req)
Definition: usbhost.c:726
int usb_request_cancel(struct usb_request *req)
Definition: usbhost.c:776
virtual const std::vector< usb_descriptor > get_descriptors() const override
static auto it
GLenum type
virtual const std::vector< rs_usb_interface > get_interfaces() const override
Definition: device-libusb.h:19
struct usb_descriptor_header * usb_descriptor_iter_next(struct usb_descriptor_iter *iter)
Definition: usbhost.c:594
#define NULL
Definition: tinycthread.c:47
int i
#define USB_DT_DEVICE
Definition: usb-types.h:12
#define USB_DT_INTERFACE_ASSOCIATION
Definition: usb-types.h:22
std::vector< usb_descriptor > _descriptors
Definition: device-libusb.h:30
usb_status submit_request(const rs_usb_request &request)
virtual const rs_usb_messenger open(uint8_t interface_number) override
struct usb_request * usb_request_wait(struct usb_device *dev, int timeoutMillis)
Definition: usbhost.c:742


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