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  const std::vector<uint8_t>& data,
21  int timeout_ms = 5000,
22  bool require_response = true) = 0;
23 
24  virtual ~command_transfer() = default;
25  };
26 
28  {
29  public:
30  command_transfer_usb(const rs_usb_device& device) : _device(device) {}
32 
33  std::vector<uint8_t> send_receive(
34  const std::vector<uint8_t>& data,
35  int timeout_ms,
36  bool) override
37  {
38  auto intfs = _device->get_interfaces();
39  auto it = std::find_if(intfs.begin(), intfs.end(),
40  [](const rs_usb_interface& i) { return i->get_class() == RS2_USB_CLASS_VENDOR_SPECIFIC; });
41  if (it == intfs.end())
42  throw std::runtime_error("can't find VENDOR_SPECIFIC interface of device: " + _device->get_info().id);
43 
44  auto hwm = *it;
45 
46  std::vector<uint8_t> output;
47  if (const auto& m = _device->open(hwm->get_number()))
48  {
49  uint32_t transfered_count = 0;
50  auto sts = m->bulk_transfer(hwm->first_endpoint(RS2_USB_ENDPOINT_DIRECTION_WRITE), const_cast<uint8_t*>(data.data()), static_cast<uint32_t>(data.size()), transfered_count, timeout_ms);
51 
52  if (sts != RS2_USB_STATUS_SUCCESS)
53  throw std::runtime_error("command transfer failed to execute bulk transfer, error: " + usb_status_to_string.at(sts));
54 
55  output.resize(DEFAULT_BUFFER_SIZE);
56  sts = m->bulk_transfer(hwm->first_endpoint(RS2_USB_ENDPOINT_DIRECTION_READ), output.data(), static_cast<uint32_t>(output.size()), transfered_count, timeout_ms);
57 
58  if (sts != RS2_USB_STATUS_SUCCESS)
59  throw std::runtime_error("command transfer failed to execute bulk transfer, error: " + usb_status_to_string.at(sts));
60 
61  output.resize(transfered_count);
62  }
63  else
64  {
65  std::stringstream s;
66  s << "access failed for " << std::hex << _device->get_info().vid << ":"
67  <<_device->get_info().pid << " uid: " << _device->get_info().id << std::dec;
68  throw std::runtime_error(s.str().c_str());
69  }
70 
71  return output;
72  }
73 
74  private:
76  static const uint32_t DEFAULT_BUFFER_SIZE = 1024;
77  };
78  }
79 }
GLdouble s
std::shared_ptr< usb_interface > rs_usb_interface
Definition: usb-interface.h:31
std::mutex m
command_transfer_usb(const rs_usb_device &device)
unsigned char uint8_t
Definition: stdint.h:78
static std::map< usb_status, std::string > usb_status_to_string
Definition: usb-types.h:162
std::vector< uint8_t > send_receive(const std::vector< uint8_t > &data, int timeout_ms, bool) override
unsigned int uint32_t
Definition: stdint.h:80
virtual std::vector< uint8_t > send_receive(const std::vector< uint8_t > &data, int timeout_ms=5000, bool require_response=true)=0
static auto it
int i
Definition: parser.hpp:153
std::shared_ptr< usb_device > rs_usb_device
Definition: usb-device.h:29


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Thu Dec 22 2022 03:43:16