internal-tests-usb.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 "catch.h"
5 #include "usb/usb-enumerator.h"
6 #include "usb/usb-device.h"
7 #include "hw-monitor.h"
9 #include <map>
10 
11 using namespace librealsense::platform;
12 
13 #define USB_SUBCLASS_CONTROL 1
14 
15 static std::map< std::string, int> uvc_req_code = {
16  {"UVC_RC_UNDEFINED", 0x00},
17  {"UVC_SET_CUR", 0x01},
18  {"UVC_GET_CUR", 0x81},
19  {"UVC_GET_MIN", 0x82},
20  {"UVC_GET_MAX", 0x83},
21  {"UVC_GET_RES", 0x84},
22  {"UVC_GET_LEN", 0x85},
23  {"UVC_GET_INFO", 0x86},
24  {"UVC_GET_DEF", 0x87},
25  {"UVC_REQ_TYPE_GET", 0xa1},
26  {"UVC_REQ_TYPE_SET", 0x21}
27 };
28 
30 {
31  auto res = std::find_if(std::begin(uvc_req_code), std::end(uvc_req_code), [&](const std::pair<std::string, int> &p)
32  {
33  return p.second == req;
34  });
35  if (res != std::end(uvc_req_code))
36  return res->first;
37  return "";
38 }
39 
40 static std::map< std::string, int> pu_controls = {
41  {"PU_CONTROL_UNDEFINED", 0x00},
42  {"PU_BACKLIGHT_COMPENSATION_CONTROL", 0x01},
43  {"PU_BRIGHTNESS_CONTROL", 0x02},
44  {"PU_CONTRAST_CONTROL", 0x03 },
45  {"PU_GAIN_CONTROL", 0x04 },
46  {"PU_POWER_LINE_FREQUENCY_CONTROL", 0x05 },
47  {"PU_HUE_CONTROL", 0x06 },
48  {"PU_SATURATION_CONTROL", 0x07 },
49  {"PU_SHARPNESS_CONTROL", 0x08 },
50  {"PU_GAMMA_CONTROL", 0x09 },
51  {"PU_WHITE_BALANCE_TEMPERATURE_CONTROL", 0x0A },
52  {"PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL", 0x0B },
53  {"PU_WHITE_BALANCE_COMPONENT_CONTROL", 0x0C },
54  {"PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL", 0x0D },
55  {"PU_DIGITAL_MULTIPLIER_CONTROL", 0x0E },
56  {"PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL", 0x0F },
57  {"PU_HUE_AUTO_CONTROL", 0x10 },
58  {"PU_ANALOG_VIDEO_STANDARD_CONTROL", 0x11 },
59  {"PU_ANALOG_LOCK_STATUS_CONTROL", 0x12 },
60  {"PU_CONTRAST_AUTO_CONTROL", 0x13 },
61 
62 };
63 
64 std::vector<usb_device_info> get_devices_info()
65 {
66  std::vector<usb_device_info> rv;
68  for (auto&& info : devices_info)
69  {
70  if (std::find_if(rv.begin(), rv.end(), [&](const usb_device_info& i) { return i.id == info.id; }) == rv.end())
71  rv.push_back(info);
72  }
73  return rv;
74 }
75 
76 TEST_CASE("query_devices", "[live][usb]")
77 {
79 
80  REQUIRE(devices_info.size());
81 
82  if (devices_info.size() == 1)
83  printf("\n1 USB device found:\n\n");
84  else
85  ("\n%d USB devices found:\n\n", devices_info.size());
86 
87  int device_counter = 0;
88  for (auto&& device_info : devices_info)
89  {
90  printf("%d)uid: %s\tmi: %d\tpath: %s\n", ++device_counter, device_info.unique_id.c_str(), device_info.mi, device_info.id.c_str());
91  }
92  printf("===============================================================================\n");
93 }
94 
95 TEST_CASE("first_endpoints_direction", "[live][usb]")
96 {
98  int device_counter = 0;
99  for (auto&& info : devices_info)
100  {
101  if(info.vid != 0x8086)
102  continue;
104  if (!dev)
105  continue;
106  auto interfaces = dev->get_interfaces();
107  auto it = std::find_if(interfaces.begin(), interfaces.end(),
108  [](const rs_usb_interface& i) { return i->get_class() == RS2_USB_CLASS_VENDOR_SPECIFIC; });
109 
110  REQUIRE(it != interfaces.end());
111 
112  auto hwm = *it;
113  auto w = hwm->first_endpoint(RS2_USB_ENDPOINT_DIRECTION_WRITE);
114  if (w)
115  REQUIRE(RS2_USB_ENDPOINT_DIRECTION_WRITE == w->get_direction());
116  auto r = hwm->first_endpoint(RS2_USB_ENDPOINT_DIRECTION_READ);
117  if (r)
118  REQUIRE(RS2_USB_ENDPOINT_DIRECTION_READ == r->get_direction());
119  }
120 }
121 
122 //control transfer test
123 TEST_CASE("query_controls", "[live][usb]")
124 {
126 
127  const int REQ_TYPE_GET = 0xa1;
128 
129  std::vector<int> requests =
130  {
131  uvc_req_code.at("UVC_GET_CUR"),
132  uvc_req_code.at("UVC_GET_MIN"),
133  uvc_req_code.at("UVC_GET_MAX"),
134  uvc_req_code.at("UVC_GET_DEF")
135  };
136 
137  bool controls_found = false;
138 
139  for (auto&& info : devices_info)
140  {
141  if(info.vid != 0x8086)
142  continue;
144  if (!dev)
145  continue;
146  if (0 != info.mi) // Lookup for controls interface only
147  continue;
148  auto m = dev->open(info.mi);
149 
150  std::vector<rs_usb_interface> interfaces = dev->get_interfaces();
151  REQUIRE(interfaces.size() > 0);
152 
153  //uvc units, rs uvc api is not implemented yet so we use hard coded mapping for reading the PUs
154  std::map<int,int> processing_units =
155  {
156  {0,2},
157  {2,2},
158  {3,7}
159  };
160 
161  int timeout = 1000;
162 
163  for (auto&& intf : interfaces)
164  {
165  if (intf->get_class() != RS2_USB_CLASS_VIDEO || intf->get_subclass() != USB_SUBCLASS_CONTROL)
166  continue;
167 
168  controls_found = true;
169  if (processing_units.find(intf->get_number()) == processing_units.end())
170  continue;
171 
172  auto unit = processing_units.at(intf->get_number());
173 
174  printf("interface: %d, processing unit: %d\n", intf->get_number(), unit);
175  int index = unit << 8 | intf->get_number();
176 
177 
178  for(auto&& ctrl : pu_controls)
179  {
180  int value = ctrl.second << 8;
181  std::map<int,int> values;
182  for(auto&& req : requests)
183  {
184  int val = 0;
185  uint32_t transferred = 0;
186  auto sts = m->control_transfer(REQ_TYPE_GET, req, value, index, reinterpret_cast<uint8_t*>(&val), sizeof(val), transferred, timeout);
187  if (sts != RS2_USB_STATUS_SUCCESS)
188  continue;
189  REQUIRE(transferred == sizeof(val));
190  values[req] = val;
191  }
192 
193  if(values.size() > 0)
194  printf("%s:\n", ctrl.first.c_str());
195 
196  for(auto&& req : values)
197  {
198  printf("%s:\t%d\t", req_to_string(req.first).c_str(), req.second);
199  }
200 
201  if(values.size() > 0)
202  printf("\n");
203  }
204  }
205  }
206  if (!controls_found)
207  printf("There are no control interfaces available, force winusb to probe controls\n");
208  printf("===============================================================================\n");
209 }
210 
211 std::vector<uint8_t> create_gvd_request_buffer(bool is_sr300)
212 {
213  uint16_t header_size = 4;
214  uint16_t data_size = 20;
215  std::vector<uint8_t> rv(header_size + data_size);
216 
217  uint32_t GVD = is_sr300 ? 0x3B : 0x10;
218 
220 
221  memcpy(rv.data(), &data_size, sizeof(uint16_t));
222  memcpy(rv.data() + 2, &IVCAM_MONITOR_MAGIC_NUMBER, sizeof(uint16_t));
223  memcpy(rv.data() + 4, &GVD, sizeof(uint32_t));
224 
225  return rv;
226 }
227 
229 {
230  command_transfer_usb ctu(dev);
231 
232  bool is_sr300 = ((dev->get_info().pid == 0x0AA5) || (dev->get_info().pid == 0x0B48)) ? true : false;
233 
234  int timeout = 100;
235  bool require_response = true;
236  auto data = create_gvd_request_buffer(is_sr300);
237 
238  auto res = ctu.send_receive(data, timeout, require_response);
239  REQUIRE(res.size());
240 
241  uint8_t fws[8];
242  uint16_t header_size = 4;
243  int fw_version_offset = is_sr300 ? 0 : 12;
244 
245  librealsense::copy(fws, res.data() + header_size + fw_version_offset, 8);
246  std::string fw = librealsense::to_string() << static_cast<int>(fws[3]) << "." << static_cast<int>(fws[2])
247  << "." << static_cast<int>(fws[1]) << "." << static_cast<int>(fws[0]);
248 
249  printf("device: %s, fw: %s\n", dev->get_info().unique_id.c_str(), fw.c_str());
250 }
251 
252 //bulk transfer test
253 TEST_CASE("read_gvd", "[live][usb]")
254 {
256  int device_counter = 0;
257 
258  printf("Devices FW version:\n");
259  for (auto&& info : devices_info)
260  {
261 
262  if(info.vid != 0x8086)
263  continue;
265  if (!dev)
266  continue;
267  for(int i = 0; i < 3; i++)
268  read_gvd(dev);
269  }
270  printf("===============================================================================\n");
271 }
272 
273 TEST_CASE("query_devices_info_duration", "[live][usb]")
274 {
278  auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
279  REQUIRE(diff < 500);
280  printf("===============================================================================\n");
281 }
282 
283 TEST_CASE("create_device_duration", "[live][usb]")
284 {
286  int device_counter = 0;
287  for (auto&& info : devices_info)
288  {
289  if (info.vid != 0x8086)
290  continue;
294  auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
295  REQUIRE(diff < 500);
296  if (!dev)
297  continue;
298  }
299  printf("===============================================================================\n");
300 }
std::string req_to_string(int req)
#define USB_SUBCLASS_CONTROL
GLuint GLuint end
static rs_usb_device create_usb_device(const usb_device_info &info)
void read_gvd(const rs_usb_device &dev)
static std::vector< usb_device_info > query_devices_info()
std::shared_ptr< usb_interface > rs_usb_interface
Definition: usb-interface.h:31
GLfloat GLfloat p
Definition: glext.h:12687
const GLfloat * m
Definition: glext.h:6814
std::vector< std::shared_ptr< device_info > > devices_info
Definition: context.h:104
GLfloat value
std::vector< usb_device_info > get_devices_info()
unsigned short uint16_t
Definition: stdint.h:79
GLdouble GLdouble GLdouble w
GLsizei const GLchar *const * string
unsigned char uint8_t
Definition: stdint.h:78
static const pu_control pu_controls[]
Definition: mf-uvc.cpp:464
GLuint index
GLuint GLfloat * val
def info(name, value, persistent=False)
Definition: test.py:301
not_this_one begin(...)
TEST_CASE("query_devices","[live][usb]")
uvc_req_code
Definition: uvc-types.h:123
const uint16_t IVCAM_MONITOR_MAGIC_NUMBER
Definition: hw-monitor.h:37
GLdouble GLdouble r
REQUIRE(n_callbacks==1)
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
const base::type::char_t * unit
Exposes sensor options functionality for C compilers.
GLsizei const GLfloat * values
GLbitfield GLuint64 timeout
static auto it
GLint GLsizei count
int i
GLuint res
Definition: glext.h:8856
std::vector< uint8_t > create_gvd_request_buffer(bool is_sr300)
Definition: parser.hpp:150
void copy(void *dst, void const *src, size_t size)
Definition: types.cpp:836
std::shared_ptr< usb_device > rs_usb_device
Definition: usb-device.h:29
std::string to_string(T value)


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