messenger-libusb.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 #include "messenger-libusb.h"
5 #include "device-libusb.h"
6 #include "handle-libusb.h"
7 namespace librealsense
8 {
9  namespace platform
10  {
11  usb_messenger_libusb::usb_messenger_libusb(const std::shared_ptr<usb_device_libusb>& device,
12  std::shared_ptr<handle_libusb> handle)
13  : _device(device), _handle(handle)
14  {
15 
16  }
17 
19  {
20 
21  }
22 
24  {
25  int ep = endpoint->get_address();
26  auto sts = libusb_clear_halt(_handle->get(), ep);
27  if(sts < 0)
28  {
29  std::string strerr = strerror(errno);
30  LOG_WARNING("reset_endpoint returned error, index: " << ep << ", error: " << strerr << ", number: " << int(errno));
31  return libusb_status_to_rs(sts);
32  }
34  }
35 
36  usb_status usb_messenger_libusb::control_transfer(int request_type, int request, int value, int index, uint8_t* buffer, uint32_t length, uint32_t& transferred, uint32_t timeout_ms)
37  {
38  auto sts = libusb_control_transfer(_handle->get(), request_type, request, value, index, buffer, length, timeout_ms);
39  if(sts < 0)
40  {
41  std::string strerr = strerror(errno);
42  LOG_WARNING("control_transfer returned error, index: " << index << ", error: " << strerr << ", number: " << int(errno));
43  return libusb_status_to_rs(sts);
44  }
45  transferred = uint32_t(sts);
47  }
48 
49  usb_status usb_messenger_libusb::bulk_transfer(const std::shared_ptr<usb_endpoint>& endpoint, uint8_t* buffer, uint32_t length, uint32_t& transferred, uint32_t timeout_ms)
50  {
51  int actual_length = 0;
52  int sts;
53  if (endpoint->get_type() == RS2_USB_ENDPOINT_BULK)
54  sts = libusb_bulk_transfer(_handle->get(), endpoint->get_address(), buffer, length, &actual_length, timeout_ms);
55  else if (endpoint->get_type() == RS2_USB_ENDPOINT_INTERRUPT)
56  sts = libusb_interrupt_transfer(_handle->get(), endpoint->get_address(), buffer, length, &actual_length, timeout_ms);
57  else {
58  LOG_ERROR("Invalid transfer type " << endpoint->get_type() << " on endpoint " << endpoint->get_address());
59  return RS2_USB_STATUS_OTHER;
60  }
61 
62  if(sts < 0)
63  {
64  std::string strerr = strerror(errno);
65  LOG_WARNING("bulk_transfer returned error, endpoint: 0x" << std::hex << int(endpoint->get_address()) << std::dec
66  << ", error: " << strerr << ", err. num: " << int(errno));
67  return libusb_status_to_rs(sts);
68  }
69  transferred = actual_length;
71  }
72 
74  {
75  auto rv = std::make_shared<usb_request_libusb>(_handle->get(), endpoint);
76  rv->set_shared(rv);
77  return rv;
78  }
79 
81  {
82  auto nr = reinterpret_cast<libusb_transfer*>(request->get_native_request());
83  if(nr->dev_handle == nullptr)
85  auto req = std::dynamic_pointer_cast<usb_request_libusb>(request);
86  req->set_active(true);
87  auto sts = libusb_submit_transfer(nr);
88  if (sts < 0)
89  {
90  req->set_active(false);
91  std::string strerr = strerror(errno);
92  LOG_WARNING("usb_request_queue returned error, endpoint: " << (int)request->get_endpoint()->get_address() << " error: " << strerr << ", number: " << (int)errno);
93  return libusb_status_to_rs(errno);
94  }
96  }
97 
99  {
100  auto nr = reinterpret_cast<libusb_transfer*>(request->get_native_request());
101  auto sts = libusb_cancel_transfer(nr);
102  if (sts < 0 && sts != LIBUSB_ERROR_NOT_FOUND)
103  {
104  std::string strerr = strerror(errno);
105  LOG_WARNING("usb_request_cancel returned error, endpoint: " << (int)request->get_endpoint()->get_address() << " error: " << strerr << ", number: " << (int)errno);
106  return libusb_status_to_rs(errno);
107  }
108  return RS2_USB_STATUS_SUCCESS;
109  }
110  }
111 }
usb_messenger_libusb(const std::shared_ptr< usb_device_libusb > &device, std::shared_ptr< handle_libusb > handle)
static usb_status libusb_status_to_rs(int sts)
Definition: handle-libusb.h:17
std::shared_ptr< usb_request > rs_usb_request
Definition: usb-request.h:41
#define LOG_WARNING(...)
Definition: src/types.h:241
GLuint64 GLenum void * handle
Definition: glext.h:7785
GLfloat value
std::shared_ptr< handle_libusb > _handle
GLsizei const GLchar *const * string
unsigned char uint8_t
Definition: stdint.h:78
GLenum GLfloat * buffer
enum librealsense::platform::_usb_status usb_status
GLuint index
unsigned int uint32_t
Definition: stdint.h:80
std::shared_ptr< usb_endpoint > rs_usb_endpoint
Definition: usb-endpoint.h:24
virtual usb_status cancel_request(const rs_usb_request &request) override
virtual usb_status control_transfer(int request_type, int request, int value, int index, uint8_t *buffer, uint32_t length, uint32_t &transferred, uint32_t timeout_ms) override
#define LOG_ERROR(...)
Definition: src/types.h:242
GLenum GLuint GLenum GLsizei length
virtual usb_status reset_endpoint(const rs_usb_endpoint &endpoint, uint32_t timeout_ms) override
virtual usb_status submit_request(const rs_usb_request &request) override
virtual usb_status bulk_transfer(const rs_usb_endpoint &endpoint, uint8_t *buffer, uint32_t length, uint32_t &transferred, uint32_t timeout_ms) override
virtual rs_usb_request create_request(rs_usb_endpoint endpoint) override


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