request-libusb.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2019 Intel Corporation. All Rights Reserved.
3 
4 #include "request-libusb.h"
5 #include "endpoint-libusb.h"
6 #include "device-libusb.h"
7 
8 namespace librealsense
9 {
10  namespace platform
11  {
12  void internal_callback(struct libusb_transfer* transfer)
13  {
14  auto urb = reinterpret_cast<usb_request_libusb*>(transfer->user_data);
15  if(urb)
16  {
17  urb->set_active(false);
18  auto response = urb->get_shared();
19  if(response)
20  {
21  auto cb = response->get_callback();
22  cb->callback(response);
23  }
24  }
25  }
26 
27  usb_request_libusb::usb_request_libusb(libusb_device_handle *dev_handle, rs_usb_endpoint endpoint)
28  {
29  _endpoint = endpoint;
30  _transfer = std::shared_ptr<libusb_transfer>(libusb_alloc_transfer(0), [this](libusb_transfer* req)
31  {
32  if(!_active)
33  libusb_free_transfer(req);
34  else
35  LOG_ERROR("active request didn't return on time");
36  } );
37  if (_endpoint->get_type() == RS2_USB_ENDPOINT_BULK)
38  libusb_fill_bulk_transfer(_transfer.get(), dev_handle, _endpoint->get_address(), NULL, 0, internal_callback, NULL, 0);
39  else if (_endpoint->get_type() == RS2_USB_ENDPOINT_INTERRUPT)
40  libusb_fill_interrupt_transfer(_transfer.get(), dev_handle, _endpoint->get_address(), NULL, 0, internal_callback, NULL, 0);
41  else
42  LOG_ERROR("Unable to fill a usb request for unknown type " << _endpoint->get_type());
43 
44  _transfer->user_data = this;
45  }
46 
48  {
49  if(_active)
50  libusb_cancel_transfer(_transfer.get());
51 
52  int attempts = 10;
53  while(_active && attempts--)
54  std::this_thread::sleep_for(std::chrono::milliseconds(10));
55  }
56 
58  {
59  _active = state;
60  }
61 
63  {
64  return _transfer->length;
65  }
66 
68  {
69  _transfer->length = length;
70  }
71 
73  {
74  return _transfer->actual_length;
75  }
76 
78  {
79  _transfer->buffer = buffer;
80  }
81 
83  {
84  return _transfer->buffer;
85  }
86 
88  {
89  return _transfer.get();
90  }
91 
92  std::shared_ptr<usb_request> usb_request_libusb::get_shared() const
93  {
94  return _shared.lock();
95  }
96 
97  void usb_request_libusb::set_shared(const std::shared_ptr<usb_request>& shared)
98  {
99  _shared = shared;
100  }
101  }
102 }
void internal_callback(struct libusb_transfer *transfer)
std::shared_ptr< usb_request > get_shared() const
usb_request_libusb(libusb_device_handle *dev_handle, rs_usb_endpoint endpoint)
unsigned char uint8_t
Definition: stdint.h:78
GLenum GLfloat * buffer
void set_shared(const std::shared_ptr< usb_request > &shared)
std::weak_ptr< usb_request > _shared
std::shared_ptr< libusb_transfer > _transfer
virtual void * get_native_request() const override
std::shared_ptr< usb_endpoint > rs_usb_endpoint
Definition: usb-endpoint.h:24
virtual int get_actual_length() const override
#define LOG_ERROR(...)
Definition: src/types.h:242
virtual void set_native_buffer(uint8_t *buffer) override
virtual uint8_t * get_native_buffer() const override
virtual void set_native_buffer_length(int length) override
#define NULL
Definition: tinycthread.c:47
GLenum GLuint GLenum GLsizei length


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