command-transfer.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 "../usb/usb-device.h"
7 
8 #include <vector>
9 #include <algorithm>
10 #include <stdint.h>
11 
12 namespace librealsense
13 {
14  namespace platform
15  {
17  {
18  public:
19  virtual std::vector<uint8_t> send_receive(
20  uint8_t const * pb,
21  size_t cb,
22  int timeout_ms = 5000,
23  bool require_response = true) = 0;
24 
25  virtual ~command_transfer() = default;
26  };
27 
29  {
30  public:
33 
34  std::vector< uint8_t > send_receive( uint8_t const * pb, size_t cb, int timeout_ms, bool ) override
35  {
36  auto intfs = _device->get_interfaces();
37  auto it = std::find_if(intfs.begin(), intfs.end(),
38  [](const rs_usb_interface& i) { return i->get_class() == RS2_USB_CLASS_VENDOR_SPECIFIC; });
39  if (it == intfs.end())
40  throw std::runtime_error("can't find VENDOR_SPECIFIC interface of device: " + _device->get_info().id);
41 
42  auto hwm = *it;
43 
44  std::vector<uint8_t> output;
45  if (const auto& m = _device->open(hwm->get_number()))
46  {
47  uint32_t transfered_count = 0;
48  auto sts = m->bulk_transfer( hwm->first_endpoint( RS2_USB_ENDPOINT_DIRECTION_WRITE ),
49  const_cast< uint8_t * >( pb ),
50  static_cast< uint32_t >( cb ),
51  transfered_count,
52  timeout_ms );
53 
54  if (sts != RS2_USB_STATUS_SUCCESS)
55  throw std::runtime_error("command transfer failed to execute bulk transfer, error: " + usb_status_to_string.at(sts));
56 
57  output.resize(DEFAULT_BUFFER_SIZE);
58  sts = m->bulk_transfer(hwm->first_endpoint(RS2_USB_ENDPOINT_DIRECTION_READ), output.data(), static_cast<uint32_t>(output.size()), transfered_count, timeout_ms);
59 
60  if (sts != RS2_USB_STATUS_SUCCESS)
61  throw std::runtime_error("command transfer failed to execute bulk transfer, error: " + usb_status_to_string.at(sts));
62 
63  output.resize(transfered_count);
64  }
65  else
66  {
67  std::stringstream s;
68  s << "access failed for " << std::hex << _device->get_info().vid << ":"
69  <<_device->get_info().pid << " uid: " << _device->get_info().id << std::dec;
70  throw std::runtime_error(s.str().c_str());
71  }
72 
73  return output;
74  }
75 
76  private:
78  static const uint32_t DEFAULT_BUFFER_SIZE = 1024;
79  };
80  }
81 }
librealsense
Definition: algo.h:18
uint8_t
unsigned char uint8_t
Definition: stdint.h:78
librealsense::platform::rs_usb_device
std::shared_ptr< usb_device > rs_usb_device
Definition: usb-device.h:29
librealsense::platform::RS2_USB_STATUS_SUCCESS
@ RS2_USB_STATUS_SUCCESS
Definition: usb-types.h:49
librealsense::platform::command_transfer::~command_transfer
virtual ~command_transfer()=default
librealsense::platform::command_transfer
Definition: command-transfer.h:16
librealsense::platform::RS2_USB_ENDPOINT_DIRECTION_WRITE
@ RS2_USB_ENDPOINT_DIRECTION_WRITE
Definition: usb-types.h:67
librealsense::platform::command_transfer_usb::send_receive
std::vector< uint8_t > send_receive(uint8_t const *pb, size_t cb, int timeout_ms, bool) override
Definition: command-transfer.h:34
librealsense::platform::usb_status_to_string
static std::map< usb_status, std::string > usb_status_to_string
Definition: usb-types.h:178
m
std::mutex m
Definition: test-waiting-on.cpp:126
uint32_t
unsigned int uint32_t
Definition: stdint.h:80
i
int i
Definition: rs-pcl-color.cpp:54
stdint.h
librealsense::platform::command_transfer::send_receive
virtual std::vector< uint8_t > send_receive(uint8_t const *pb, size_t cb, int timeout_ms=5000, bool require_response=true)=0
librealsense::platform::command_transfer_usb::_device
rs_usb_device _device
Definition: command-transfer.h:77
it
static auto it
Definition: openvino-face-detection.cpp:375
librealsense::platform::command_transfer_usb::command_transfer_usb
command_transfer_usb(const rs_usb_device &device)
Definition: command-transfer.h:31
s
GLdouble s
Definition: glad/glad/glad.h:2441
librealsense::platform::command_transfer_usb::~command_transfer_usb
~command_transfer_usb()
Definition: command-transfer.h:32
test-color_frame_frops.cb
def cb(frame, ts)
Definition: test-color_frame_frops.py:29
librealsense::device
Definition: device.h:35
librealsense::platform::command_transfer_usb
Definition: command-transfer.h:28
librealsense::platform::RS2_USB_ENDPOINT_DIRECTION_READ
@ RS2_USB_ENDPOINT_DIRECTION_READ
Definition: usb-types.h:68
librealsense::platform::command_transfer_usb::DEFAULT_BUFFER_SIZE
static const uint32_t DEFAULT_BUFFER_SIZE
Definition: command-transfer.h:78
librealsense::platform::rs_usb_interface
std::shared_ptr< usb_interface > rs_usb_interface
Definition: usb-interface.h:31


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:01