uvc-device.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 "uvc-device.h"
5 
6 #define UVC_AE_MODE_D0_MANUAL ( 1 << 0 )
7 #define UVC_AE_MODE_D1_AUTO ( 1 << 1 )
8 #define UVC_AE_MODE_D2_SP ( 1 << 2 )
9 #define UVC_AE_MODE_D3_AP ( 1 << 3 )
10 
11 #define SWAP_UINT32(x) (((x) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | ((x) << 24))
12 
13 const int CONTROL_TRANSFER_TIMEOUT = 100;
14 const int INTERRUPT_BUFFER_SIZE = 1024;
16 
18 {
19 public:
21  {
22  static lock_singleton inst;
23  return inst;
24  }
25  static void lock();
26  static void unlock();
27 
28 private:
29  static std::recursive_mutex m;
30 };
31 std::recursive_mutex lock_singleton::m;
32 void lock_singleton::lock() { m.lock(); }
33 void lock_singleton::unlock() { m.unlock(); }
34 
35 
36 namespace librealsense
37 {
38  namespace platform
39  {
40  std::vector<uvc_device_info> query_uvc_devices_info()
41  {
42  std::vector<platform::uvc_device_info> rv;
43  auto usb_devices = platform::usb_enumerator::query_devices_info();
44  for (auto&& info : usb_devices)
45  {
46  if (info.cls != RS2_USB_CLASS_VIDEO)
47  continue;
49  device_info.id = info.id;
50  device_info.vid = info.vid;
51  device_info.pid = info.pid;
52  device_info.mi = info.mi;
53  device_info.unique_id = info.unique_id;
54  device_info.device_path = info.id;
55  device_info.conn_spec = info.conn_spec;
56  //LOG_INFO("Found UVC device: " << std::string(device_info).c_str());
57  rv.push_back(device_info);
58  }
59  return rv;
60  }
61 
62  std::shared_ptr<uvc_device> create_rsuvc_device(uvc_device_info info)
63  {
64  auto devices = usb_enumerator::query_devices_info();
65  for (auto&& usb_info : devices)
66  {
67  if(usb_info.id != info.id)
68  continue;
69 
70  auto dev = usb_enumerator::create_usb_device(usb_info);
71  if(dev)
72  return std::make_shared<rs_uvc_device>(dev, info);
73  }
74 
75  return nullptr;
76  }
77 
78  rs_uvc_device::rs_uvc_device(const rs_usb_device& usb_device, const uvc_device_info &info, uint8_t usb_request_count) :
79  _usb_device(usb_device),
80  _info(info),
81  _action_dispatcher(10),
82  _usb_request_count(usb_request_count)
83  {
84  _parser = std::make_shared<uvc_parser>(usb_device, info);
86  }
87 
89  {
90  try {
92  }
93  catch (...) {
94  LOG_WARNING("rs_uvc_device failed to switch power state on destructor");
95  }
97  }
98 
100  {
101  if (!_streamers.empty())
102  throw std::runtime_error("Device is already streaming!");
103 
104  _profiles.push_back(profile);
105  _frame_callbacks.push_back(callback);
106  }
107 
108  void rs_uvc_device::stream_on(std::function<void(const notification& n)> error_handler)
109  {
110  if (_profiles.empty())
111  throw std::runtime_error("Stream not configured");
112 
113  if (!_streamers.empty())
114  throw std::runtime_error("Device is already streaming!");
115 
117 
118  try {
119  for (uint32_t i = 0; i < _profiles.size(); ++i) {
121  }
122  }
123  catch (...) {
124  for (auto &elem : _streams)
125  if (elem.callback)
126  close(elem.profile);
127 
128  _profiles.clear();
129  _frame_callbacks.clear();
130 
131  throw;
132  }
133  }
134 
136  {
137  for(auto&& s : _streamers)
138  s->enable_user_callbacks();
139  }
140 
142  {
143  for(auto&& s : _streamers)
144  s->disable_user_callbacks();
145  }
146 
148  {
150 
151  for(auto&& s : _streamers)
152  s->stop();
153 
154  auto elem = std::find_if(_streams.begin(), _streams.end(), [&](const profile_and_callback &pac) {
155  return (pac.profile == profile && (pac.callback));
156  });
157 
158  if (elem == _streams.end() && _frame_callbacks.empty())
159  throw std::runtime_error("Camera is not streaming!");
160 
161  stop_stream_cleanup(profile, elem);
162 
163  if (!_profiles.empty())
164  _streamers.clear();
165  }
166 
168  {
170  {
171  if(state != _power_state)
172  {
173  switch(state)
174  {
175  case D0:
176  _messenger = _usb_device->open(_info.mi);
177  if (_messenger)
178  {
179  try{
181  } catch(const std::exception& exception) {
182  // this exception catching avoids crash when disconnecting 2 devices at once - bug seen in android os
183  LOG_WARNING("rs_uvc_device exception in listen_to_interrupts method: " << exception.what());
184  }
185  _power_state = D0;
186  }
187  break;
188  case D3:
189  if(_messenger)
190  {
191  close_uvc_device();
192  _messenger.reset();
193  }
194  _power_state = D3;
195  break;
196  }
197  }
198  }, [this, state](){ return state == _power_state; });
199 
200  if(state != _power_state)
201  throw std::runtime_error("failed to set power state");
202  }
203 
205  {
206  return _power_state;
207  }
208 
210  {
211  // not supported
212  return;
213  }
214 
215  bool rs_uvc_device::set_xu(const extension_unit& xu, uint8_t ctrl, const uint8_t* data, int len)
216  {
217  return uvc_set_ctrl(xu.unit, ctrl, (void *) data, len);
218  }
219 
220  bool rs_uvc_device::get_xu(const extension_unit& xu, uint8_t ctrl, uint8_t* data, int len) const
221  {
222  return uvc_get_ctrl(xu.unit, ctrl, (void *) data, len, UVC_GET_CUR);
223  }
224 
226  {
227  bool status;
228  int max;
229  int min;
230  int step;
231  int def;
232  status = uvc_get_ctrl(xu.unit, control, &max, sizeof(int32_t),
233  UVC_GET_MAX);
234  if (!status)
235  throw std::runtime_error("uvc_get_ctrl(UVC_GET_MAX) failed");
236 
237  status = uvc_get_ctrl(xu.unit, control, &min, sizeof(int32_t),
238  UVC_GET_MIN);
239  if (!status)
240  throw std::runtime_error("uvc_get_ctrl(UVC_GET_MIN) failed");
241 
242  status = uvc_get_ctrl(xu.unit, control, &step, sizeof(int32_t),
243  UVC_GET_RES);
244  if (!status)
245  throw std::runtime_error("uvc_get_ctrl(UVC_GET_RES) failed");
246 
247  status = uvc_get_ctrl(xu.unit, control, &def, sizeof(int32_t),
248  UVC_GET_DEF);
249  if (!status)
250  throw std::runtime_error("uvc_get_ctrl(UVC_GET_DEF) failed");
251 
252  control_range result(min, max, step, def);
253  return result;
254  }
255 
257  {
258  int unit;
259  int control = rs2_option_to_ctrl_selector(option, unit);
260  unsigned int length = get_data_usb(UVC_GET_LEN, control, unit);
261 
262  value = get_data_usb(UVC_GET_CUR, control, unit, length);
263  value = rs2_value_translate(UVC_GET_CUR, option, value);
264 
265  return true;
266  }
267 
269  {
270  int unit;
271  int control = rs2_option_to_ctrl_selector(option, unit);
272 
273  value = rs2_value_translate(UVC_SET_CUR, option, value);
274  set_data_usb(UVC_SET_CUR, control, unit, value);
275 
276  return true;
277  }
278 
280  {
281  int unit = 0;
282  int min, max, step, def;
283  int control = rs2_option_to_ctrl_selector(option, unit);
284 
285  min = get_data_usb(UVC_GET_MIN, control, unit);
286  min = rs2_value_translate(UVC_GET_MIN, option, min);
287 
288  max = get_data_usb(UVC_GET_MAX, control, unit);
289  max = rs2_value_translate(UVC_GET_MAX, option, max);
290 
291  step = get_data_usb(UVC_GET_RES, control, unit);
292  step = rs2_value_translate(UVC_GET_RES, option, step);
293 
294  def = get_data_usb(UVC_GET_DEF, control, unit);
295  def = rs2_value_translate(UVC_GET_DEF, option, def);
296 
297  control_range result(min, max, step, def);
298 
299  return result;
300  }
301 
302  std::vector<stream_profile> rs_uvc_device::get_profiles() const
303  {
304  std::vector<stream_profile> results;
305  int i = 0;
306 
308 
309  if (get_power_state() != D0) {
310  throw std::runtime_error("Device must be powered to query supported profiles!");
311  }
312 
313  auto formats = get_available_formats_all();
314 
315  for(auto&& f : formats)
316  {
317  stream_profile sp;
318  sp.width = f.width;
319  sp.height = f.height;
320  sp.fps = f.fps;
321  sp.format = f.fourcc;
322  results.push_back(sp);
323  }
324 
325  return results;
326  }
327 
328 
329  void rs_uvc_device::lock() const
330  {
332  }
333 
335  {
337  }
338 
340  {
341  return _location;
342  }
343 
345  {
346  // On Win7, USB type is determined only when the USB device is created, _info.conn_spec holds wrong information
347  return _usb_device->get_info().conn_spec;
348  }
349 
350  // Translate between UVC 1.5 Spec and RS
352  int32_t value) const {
353  // Value may be translated according to action/option value
354  int32_t translated_value = value;
355 
356  switch (action) {
357  case UVC_GET_CUR: // Translating from UVC 1.5 Spec up to RS
358  if (option == RS2_OPTION_ENABLE_AUTO_EXPOSURE) {
359  switch (value) {
360  case UVC_AE_MODE_D3_AP:
361  translated_value = 1;
362  break;
364  translated_value = 0;
365  break;
366  default:
367  throw std::runtime_error(
368  "Unsupported GET value for RS2_OPTION_ENABLE_AUTO_EXPOSURE");
369  }
370  }
371  break;
372 
373  case UVC_SET_CUR: // Translating from RS down to UVC 1.5 Spec
374  if (option == RS2_OPTION_ENABLE_AUTO_EXPOSURE) {
375  switch (value) {
376  case 1:
377  // Enabling auto exposure
378  translated_value = UVC_AE_MODE_D3_AP;
379  break;
380  case 0:
381  // Disabling auto exposure
382  translated_value = UVC_AE_MODE_D0_MANUAL;
383  break;
384  default:
385  throw std::runtime_error(
386  "Unsupported SET value for RS2_OPTION_ENABLE_AUTO_EXPOSURE");
387  }
388  }
389  break;
390 
391  case UVC_GET_MIN:
392  if (option == RS2_OPTION_ENABLE_AUTO_EXPOSURE) {
393  translated_value = 0; // Hardcoded MIN value
394  }
395  break;
396 
397  case UVC_GET_MAX:
398  if (option == RS2_OPTION_ENABLE_AUTO_EXPOSURE) {
399  translated_value = 1; // Hardcoded MAX value
400  }
401  break;
402 
403  case UVC_GET_RES:
404  if (option == RS2_OPTION_ENABLE_AUTO_EXPOSURE) {
405  translated_value = 1; // Hardcoded RES (step) value
406  }
407  break;
408 
409  case UVC_GET_DEF:
410  if (option == RS2_OPTION_ENABLE_AUTO_EXPOSURE) {
411  translated_value = 1; // Hardcoded DEF value
412  }
413  break;
414 
415  default:
416  throw std::runtime_error("Unsupported action translation");
417  }
418  return translated_value;
419  }
420 
422  bool foundFormat = false;
423 
424  uvc_format_t selected_format{};
425  // Return list of all available formats inside devices[0]
426  auto formats = get_available_formats_all();
427 
428  for(auto&& f : formats)
429  {
430  if ((profile.format == f.fourcc) &&
431  (profile.fps == f.fps) &&
432  (profile.height == f.height) &&
433  (profile.width == f.width)) {
434  foundFormat = true;
435  selected_format = f;
436  break;
437  }
438  }
439 
440  if (foundFormat == false) {
441  throw std::runtime_error("Failed to find supported format!");
442  }
443 
444  auto ctrl = std::make_shared<uvc_stream_ctrl_t>();
445  auto ret = get_stream_ctrl_format_size(selected_format, ctrl);
446  if (ret != RS2_USB_STATUS_SUCCESS) {
447  throw std::runtime_error("Failed to get control format size!");
448  }
449 
450  auto sts = query_stream_ctrl(ctrl, 0, UVC_SET_CUR);
451  if(sts != RS2_USB_STATUS_SUCCESS)
452  throw std::runtime_error("Failed to start streaming!");
453 
455 
456  auto streamer = std::make_shared<uvc_streamer>(usc);
457  _streamers.push_back(streamer);
458 
459  if(_streamers.size() == _profiles.size())
460  {
461  for(auto&& s : _streamers)
462  s->start();
463  }
464  }
465 
467  std::vector<profile_and_callback>::iterator &elem) {
468  if (elem != _streams.end()) {
469  elem->callback = nullptr;
470  elem->profile.format = 0;
471  elem->profile.fps = 0;
472  elem->profile.width = 0;
473  elem->profile.height = 0;
474  }
475 
476  auto pos = std::find(_profiles.begin(), _profiles.end(), profile) - _profiles.begin();
477  if (pos != _profiles.size()) {
478  _profiles.erase(_profiles.begin() + pos);
479  _frame_callbacks.erase(_frame_callbacks.begin() + pos);
480  }
481  }
482 
483  // Retreive USB data, sign bit is calculated according to length
485  unsigned int length) const {
486  unsigned char buffer[4] = {0};
487  int32_t ret = 0;
488 
489  usb_status sts;
490  uint32_t transferred;
492  {
493  if (_messenger)
494  {
495  sts = _messenger->control_transfer(
497  action,
498  control << 8,
499  unit << 8 | (_info.mi),
500  buffer,
501  sizeof(int32_t),
502  transferred,
503  0);
504  }
505 
506  }, [this](){ return !_messenger; });
507 
508  if (sts != RS2_USB_STATUS_SUCCESS)
509  throw std::runtime_error("get_data_usb failed, error: " + usb_status_to_string.at(sts));
510 
511  if (transferred != sizeof(int32_t))
512  throw std::runtime_error("insufficient data read from USB");
513 
514  // Converting byte array buffer (with length 8/16/32) to int32
515  switch (length) {
516  case sizeof(uint8_t):
517  ret = B_TO_BYTE(buffer);
518  break;
519  case sizeof(uint16_t) :
520  ret = SW_TO_SHORT(buffer);
521  break;
522  case sizeof(uint32_t) :
523  ret = DW_TO_INT(buffer);
524  break;
525  default:
526  throw std::runtime_error("unsupported length");
527  }
528 
529  return ret;
530  }
531 
533  // chances are that we will need to adjust some of these operation to the
534  // processing unit and some to the camera terminal.
535  unit = _parser->get_processing_unit().bUnitID;
536  switch (option) {
541  case RS2_OPTION_CONTRAST:
543  case RS2_OPTION_EXPOSURE:
544  unit = _parser->get_input_terminal().bTerminalID;
546  case RS2_OPTION_GAIN:
547  return UVC_PU_GAIN_CONTROL;
548  case RS2_OPTION_GAMMA:
549  return UVC_PU_GAMMA_CONTROL;
550  case RS2_OPTION_HUE:
551  return UVC_PU_HUE_CONTROL;
559  unit = _parser->get_input_terminal().bTerminalID;
560  return UVC_CT_AE_MODE_CONTROL; // Automatic gain/exposure control
566  unit = _parser->get_input_terminal().bTerminalID;
568  default:
569  throw linux_backend_exception(to_string() << "invalid option : " << option);
570  }
571  }
572 
574  int value) const {
575  unsigned char buffer[4];
576  INT_TO_DW(value, buffer);
577 
578  usb_status sts;
579  uint32_t transferred;
580 
582  {
583  if (_messenger)
584  {
585  sts = _messenger->control_transfer(
587  action,
588  control << 8,
589  unit << 8 | (_info.mi),
590  buffer,
591  sizeof(int32_t),
592  transferred,
593  0);
594  }
595 
596  }, [this](){ return !_messenger; });
597 
598  if (sts != RS2_USB_STATUS_SUCCESS)
599  throw std::runtime_error("set_data_usb failed, error: " + usb_status_to_string.at(sts));
600 
601  if (transferred != sizeof(int32_t))
602  throw std::runtime_error("insufficient data writen to USB");
603  }
604 
605  bool rs_uvc_device::uvc_get_ctrl(uint8_t unit, uint8_t ctrl, void *data, int len, uvc_req_code req_code) const
606  {
607  usb_status sts;
609  {
610  if (_messenger)
611  {
612  uint32_t transferred;
613  sts = _messenger->control_transfer(
614  UVC_REQ_TYPE_GET, req_code,
615  ctrl << 8,
616  unit << 8 | _info.mi,
617  static_cast<unsigned char *>(data),
618  len, transferred, CONTROL_TRANSFER_TIMEOUT);
619  }
620  }, [this](){ return !_messenger; });
621 
622  if (sts == RS2_USB_STATUS_NO_DEVICE)
623  throw std::runtime_error("usb device disconnected");
624 
625  return sts == RS2_USB_STATUS_SUCCESS;
626  }
627 
629  {
630  usb_status sts;
632  {
633  if (_messenger)
634  {
635  uint32_t transferred;
636  sts = _messenger->control_transfer(
638  ctrl << 8,
639  unit << 8 | _info.mi,
640  static_cast<unsigned char *>(data),
641  len, transferred, CONTROL_TRANSFER_TIMEOUT);
642  }
643  }, [this](){ return !_messenger; });
644 
645  if (sts == RS2_USB_STATUS_NO_DEVICE)
646  throw std::runtime_error("usb device disconnected");
647 
648  return sts == RS2_USB_STATUS_SUCCESS;
649  }
650 
652  {
653  auto ctrl_interface = _usb_device->get_interface(_info.mi);
654  if (!ctrl_interface)
655  return;
656  auto iep = ctrl_interface->first_endpoint(RS2_USB_ENDPOINT_DIRECTION_READ, RS2_USB_ENDPOINT_INTERRUPT);
657  if(!iep)
658  return;
659 
660  _interrupt_callback = std::make_shared<usb_request_callback>
661  ([&](rs_usb_request response)
662  {
663  //TODO:MK Should call the sensor to handle via callback
664  if (response->get_actual_length() > 0)
665  {
666  std::string buff = "";
667  for (int i = 0; i < response->get_actual_length(); i++)
668  buff += std::to_string(response->get_buffer()[i]) + ", ";
669  LOG_DEBUG("interrupt event received: " << buff.c_str());
670  }
671 
673  {
674  if (!_messenger)
675  return;
676  _messenger->submit_request(_interrupt_request);
677  });
678  });
679 
680  _interrupt_request = _messenger->create_request(iep);
681  _interrupt_request->set_buffer(std::vector<uint8_t>(INTERRUPT_BUFFER_SIZE));
682  _interrupt_request->set_callback(_interrupt_callback);
683  auto sts = _messenger->submit_request(_interrupt_request);
684  if (sts != RS2_USB_STATUS_SUCCESS)
685  throw std::runtime_error("failed to submit interrupt request, error: " + usb_status_to_string.at(sts));
686  }
687 
689  {
690  _streamers.clear();
691 
693  {
694  _interrupt_callback->cancel();
695  _messenger->cancel_request(_interrupt_request);
696  _interrupt_request.reset();
697  }
698  }
699 
700  // Probe (Set and Get) streaming control block
701  usb_status rs_uvc_device::probe_stream_ctrl(const std::shared_ptr<uvc_stream_ctrl_t>& control)
702  {
703  // Sending probe SET request - UVC_SET_CUR request in a probe/commit structure containing desired values for VS Format index, VS Frame index, and VS Frame Interval
704  // UVC device will check the VS Format index, VS Frame index, and Frame interval properties to verify if possible and update the probe/commit structure if feasible
705  auto sts = query_stream_ctrl(control, 1, UVC_SET_CUR);
706  if(sts != RS2_USB_STATUS_SUCCESS)
707  return sts;
708 
709  // Sending probe GET request - UVC_GET_CUR request to read the updated values from UVC device
710  sts = query_stream_ctrl(control, 1, UVC_GET_CUR);
711  if(sts != RS2_USB_STATUS_SUCCESS)
712  return sts;
713 
714  return RS2_USB_STATUS_SUCCESS;
715  }
716 
717  std::vector<uvc_format_t> rs_uvc_device::get_available_formats_all() const
718  {
719  std::vector<uvc_format_t> rv;
720  for(auto&& s : _parser->get_formats())
721  {
722  for(auto&& format : s.second)
723  {
724  for(auto&& fd : format.frame_descs)
725  {
726  uvc_format_t cur_format{};
727  cur_format.height = fd.wHeight;
728  cur_format.width = fd.wWidth;
729  auto temp = SWAP_UINT32(*(const uint32_t *) format.guidFormat);
730  cur_format.fourcc = fourcc_map.count(temp) ? fourcc_map.at(temp) : temp;
731  cur_format.interfaceNumber = s.first;
732 
733  for(auto&& i : fd.intervals)
734  {
735  if(i == 0)
736  continue;
737  cur_format.fps = 10000000 / i;
738  rv.push_back(cur_format);
739  }
740  }
741  }
742  }
743  return rv;
744  }
745 
746  usb_status rs_uvc_device::get_stream_ctrl_format_size(uvc_format_t format, const std::shared_ptr<uvc_stream_ctrl_t>& control)
747  {
748  for(auto&& s : _parser->get_formats())
749  {
750  for(auto&& curr_format : s.second)
751  {
752  auto val = SWAP_UINT32(*(const uint32_t *) curr_format.guidFormat);
753  if(fourcc_map.count(val))
754  val = fourcc_map.at(val);
755 
756  if (format.fourcc != val)
757  continue;
758 
759  for(auto&& fd : curr_format.frame_descs)
760  {
761  if (fd.wWidth != format.width || fd.wHeight != format.height)
762  continue;
763 
764  for(auto&& i : fd.intervals)
765  {
766  if (10000000 / i == (unsigned int) format.fps || format.fps == 0) {
767 
768  /* get the max values -- we need the interface number to be able
769  to do this */
770  control->bInterfaceNumber = s.first;
771  auto sts = query_stream_ctrl(control, 1, UVC_GET_MAX);
772  if(sts != RS2_USB_STATUS_SUCCESS)
773  return sts;
774 
775  control->bmHint = (1 << 0); /* don't negotiate interval */
776  control->bFormatIndex = curr_format.bFormatIndex;
777  control->bFrameIndex = fd.bFrameIndex;
778  control->dwFrameInterval = i;
779  return probe_stream_ctrl(control);
780  }
781  }
782  }
783  }
784  }
786  }
787 
788  usb_status rs_uvc_device::query_stream_ctrl(const std::shared_ptr<uvc_stream_ctrl_t>& ctrl, uint8_t probe, int req)
789  {
790  uint8_t buf[48];
791  size_t len;
792  int err = 0;
793 
794  memset(buf, 0, sizeof(buf));
795  switch (_parser->get_bcd_uvc()) {
796  case 0x0110:
797  len = 34;
798  break;
799  case 0x0150:
800  len = 48;
801  break;
802  default:
803  len = 26;
804  break;
805  }
806 
807  /* prepare for a SET transfer */
808  if (req == UVC_SET_CUR) {
809  SHORT_TO_SW(ctrl->bmHint, buf);
810  buf[2] = ctrl->bFormatIndex;
811  buf[3] = ctrl->bFrameIndex;
812  INT_TO_DW(ctrl->dwFrameInterval, buf + 4);
813  SHORT_TO_SW(ctrl->wKeyFrameRate, buf + 8);
814  SHORT_TO_SW(ctrl->wPFrameRate, buf + 10);
815  SHORT_TO_SW(ctrl->wCompQuality, buf + 12);
816  SHORT_TO_SW(ctrl->wCompWindowSize, buf + 14);
817  SHORT_TO_SW(ctrl->wDelay, buf + 16);
818  INT_TO_DW(ctrl->dwMaxVideoFrameSize, buf + 18);
819  INT_TO_DW(ctrl->dwMaxPayloadTransferSize, buf + 22);
820  INT_TO_DW(0, buf + 18);
821  INT_TO_DW(0, buf + 22);
822 
823  if (len >= 34) {
824  INT_TO_DW(ctrl->dwClockFrequency, buf + 26);
825  buf[30] = ctrl->bmFramingInfo;
826  buf[31] = ctrl->bPreferredVersion;
827  buf[32] = ctrl->bMinVersion;
828  buf[33] = ctrl->bMaxVersion;
830  }
831 
832  if (len == 48) {
833  buf[34] = ctrl->bUsage;
834  buf[35] = ctrl->bBitDepthLuma;
835  buf[36] = ctrl->bmSettings;
836  buf[37] = ctrl->bMaxNumberOfRefFramesPlus1;
837  SHORT_TO_SW(ctrl->bmRateControlModes, buf + 38);
838  QUAD_TO_QW(ctrl->bmLayoutPerStream, buf + 40);
839  }
840  }
841 
842  usb_status sts;
844  {
845  if (_messenger)
846  {
847  int retries = 0;
848  do {
849  uint32_t transferred;
850  sts = _messenger->control_transfer(
852  req,
853  probe ? (UVC_VS_PROBE_CONTROL << 8) : (UVC_VS_COMMIT_CONTROL << 8),
854  ctrl->bInterfaceNumber, // When requestType is directed to an interface, the driver automatically passes the interface number in the low byte of index
855  buf, len, transferred, 0);
856  } while (sts != RS2_USB_STATUS_SUCCESS && retries++ < 5);
857  }
858  }, [this](){ return !_messenger; });
859 
860  if (sts != RS2_USB_STATUS_SUCCESS)
861  {
862  LOG_ERROR("Probe-commit control transfer failed with error: " << usb_status_to_string.at(sts));
863  return sts;
864  }
865 
866  /* now decode following a GET transfer */
867  if (req != UVC_SET_CUR) {
868  ctrl->bmHint = SW_TO_SHORT(buf);
869  ctrl->bFormatIndex = buf[2];
870  ctrl->bFrameIndex = buf[3];
871  ctrl->dwFrameInterval = DW_TO_INT(buf + 4);
872  ctrl->wKeyFrameRate = SW_TO_SHORT(buf + 8);
873  ctrl->wPFrameRate = SW_TO_SHORT(buf + 10);
874  ctrl->wCompQuality = SW_TO_SHORT(buf + 12);
875  ctrl->wCompWindowSize = SW_TO_SHORT(buf + 14);
876  ctrl->wDelay = SW_TO_SHORT(buf + 16);
877  ctrl->dwMaxVideoFrameSize = DW_TO_INT(buf + 18);
878  ctrl->dwMaxPayloadTransferSize = DW_TO_INT(buf + 22);
879 
880  if (len >= 34) {
881  ctrl->dwClockFrequency = DW_TO_INT(buf + 26);
882  ctrl->bmFramingInfo = buf[30];
883  ctrl->bPreferredVersion = buf[31];
884  ctrl->bMinVersion = buf[32];
885  ctrl->bMaxVersion = buf[33];
887  }
888 
889  if (len == 48) {
890  ctrl->bUsage = buf[34];
891  ctrl->bBitDepthLuma = buf[35];
892  ctrl->bmSettings = buf[36];
893  ctrl->bMaxNumberOfRefFramesPlus1 = buf[37];
894  ctrl->bmRateControlModes = DW_TO_INT(buf + 38);
895  ctrl->bmLayoutPerStream = QW_TO_QUAD(buf + 40);
896  }
897  else
898  ctrl->dwClockFrequency = _parser->get_clock_frequency();
899  }
900  return RS2_USB_STATUS_SUCCESS;
901  }
902 
904  {
906  for (auto&& usb_info : devices)
907  {
908  if(usb_info.unique_id == _info.unique_id)
909  return;
910  }
911  throw std::runtime_error("Camera is no longer connected!");
912  }
913  }
914 }
int rs2_option_to_ctrl_selector(rs2_option option, int &unit) const
Definition: uvc-device.cpp:532
#define INT_TO_DW(i, p)
Definition: uvc-types.h:41
virtual void unlock() const override
Definition: uvc-device.cpp:334
bool uvc_set_ctrl(uint8_t unit, uint8_t ctrl, void *data, int len)
Definition: uvc-device.cpp:628
std::shared_ptr< uvc_device > create_rsuvc_device(uvc_device_info info)
Definition: uvc-device.cpp:62
std::vector< uvc_device_info > query_uvc_devices_info()
Definition: uvc-device.cpp:40
std::shared_ptr< usb_request > rs_usb_request
Definition: usb-request.h:41
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
GLdouble s
static std::vector< usb_device_info > query_devices_info()
static std::recursive_mutex m
Definition: uvc-device.cpp:29
#define UVC_AE_MODE_D0_MANUAL
Definition: uvc-device.cpp:6
void start()
Definition: concurrency.h:286
const GLfloat * m
Definition: glext.h:6814
const int FIRST_FRAME_MILLISECONDS_TIMEOUT
Definition: uvc-device.cpp:15
virtual bool set_xu(const extension_unit &xu, uint8_t ctrl, const uint8_t *data, int len) override
Definition: uvc-device.cpp:215
usb_status probe_stream_ctrl(const std::shared_ptr< uvc_stream_ctrl_t > &control)
Definition: uvc-device.cpp:701
virtual std::vector< stream_profile > get_profiles() const override
Definition: uvc-device.cpp:302
static void unlock()
Definition: uvc-device.cpp:33
#define LOG_WARNING(...)
Definition: src/types.h:241
#define SW_TO_SHORT(p)
Definition: uvc-types.h:15
virtual bool set_pu(rs2_option opt, int32_t value) override
Definition: uvc-device.cpp:268
virtual void set_power_state(power_state state) override
Definition: uvc-device.cpp:167
GLfloat value
virtual void start_callbacks() override
Definition: uvc-device.cpp:135
virtual void stop_callbacks() override
Definition: uvc-device.cpp:141
uint32_t fourcc
Definition: uvc-types.h:333
virtual power_state get_power_state() const override
Definition: uvc-device.cpp:204
unsigned short uint16_t
Definition: stdint.h:79
virtual bool get_pu(rs2_option opt, int32_t &value) const override
Definition: uvc-device.cpp:256
virtual void probe_and_commit(stream_profile profile, frame_callback callback, int buffers=DEFAULT_V4L2_FRAME_BUFFERS) override
Definition: uvc-device.cpp:99
GLsizei const GLchar *const * string
void set_data_usb(uvc_req_code action, int control, int unit, int value) const
Definition: uvc-device.cpp:573
GLdouble n
Definition: glext.h:1966
unsigned char uint8_t
Definition: stdint.h:78
virtual bool get_xu(const extension_unit &xu, uint8_t ctrl, uint8_t *data, int len) const override
Definition: uvc-device.cpp:220
uint32_t width
Definition: uvc-types.h:329
enum librealsense::platform::_usb_status usb_status
virtual void stream_on(std::function< void(const notification &n)> error_handler=[](const notification &n){}) override
Definition: uvc-device.cpp:108
static std::map< usb_status, std::string > usb_status_to_string
Definition: usb-types.h:162
status
Defines return codes that SDK interfaces use. Negative values indicate errors, a zero value indicates...
std::vector< stream_profile > _profiles
Definition: uvc-device.h:98
#define UVC_AE_MODE_D3_AP
Definition: uvc-device.cpp:9
GLenum GLuint GLenum GLsizei const GLchar * buf
GLenum GLsizei len
Definition: glext.h:3285
GLuint GLfloat * val
def info(name, value, persistent=False)
Definition: test.py:301
int32_t rs2_value_translate(uvc_req_code action, rs2_option option, int32_t value) const
Definition: uvc-device.cpp:351
GLdouble f
bool uvc_get_ctrl(uint8_t unit, uint8_t ctrl, void *data, int len, uvc_req_code req_code) const
Definition: uvc-device.cpp:605
std::vector< profile_and_callback > _streams
Definition: uvc-device.h:95
uvc_req_code
Definition: uvc-types.h:123
#define QUAD_TO_QW(i, p)
Definition: uvc-types.h:48
void stop()
Definition: concurrency.h:294
const GLubyte * c
Definition: glext.h:12690
virtual void init_xu(const extension_unit &xu) override
Definition: uvc-device.cpp:209
rs_usb_request_callback _interrupt_callback
Definition: uvc-device.h:104
unsigned int uint32_t
Definition: stdint.h:80
std::vector< uvc_format_t > get_available_formats_all() const
Definition: uvc-device.cpp:717
const std::unordered_map< uint32_t, uint32_t > fourcc_map
Definition: uvc-types.h:59
devices
Definition: test-fg.py:9
const int INTERRUPT_BUFFER_SIZE
Definition: uvc-device.cpp:14
#define SWAP_UINT32(x)
Definition: uvc-device.cpp:11
static void lock()
Definition: uvc-device.cpp:32
GLint GLint GLsizei GLint GLenum format
def find(dir, mask)
Definition: file.py:25
def callback(frame)
Definition: t265_stereo.py:91
#define QW_TO_QUAD(p)
Definition: uvc-types.h:25
virtual void lock() const override
Definition: uvc-device.cpp:329
#define LOG_ERROR(...)
Definition: src/types.h:242
const base::type::char_t * unit
void stop_stream_cleanup(const stream_profile &profile, std::vector< profile_and_callback >::iterator &elem)
Definition: uvc-device.cpp:466
#define SHORT_TO_SW(s, p)
Definition: uvc-types.h:36
const GLuint * buffers
action
Definition: enums.py:62
std::vector< std::shared_ptr< uvc_streamer > > _streamers
Definition: uvc-device.h:110
std::function< void(stream_profile, frame_object, std::function< void()>)> frame_callback
Definition: backend.h:177
std::vector< frame_callback > _frame_callbacks
Definition: uvc-device.h:99
const int CONTROL_TRANSFER_TIMEOUT
Definition: uvc-device.cpp:13
virtual usb_spec get_usb_specification() const override
Definition: uvc-device.cpp:344
virtual control_range get_xu_range(const extension_unit &xu, uint8_t ctrl, int len) const override
Definition: uvc-device.cpp:225
int min(int a, int b)
Definition: lz4s.c:73
virtual void close(stream_profile profile) override
Definition: uvc-device.cpp:147
void invoke(T item, bool is_blocking=false)
Definition: concurrency.h:254
float rs2_vector::* pos
uint32_t fps
Definition: uvc-types.h:335
std::shared_ptr< uvc_parser > _parser
Definition: uvc-device.h:109
usb_status get_stream_ctrl_format_size(uvc_format_t format, const std::shared_ptr< uvc_stream_ctrl_t > &control)
Definition: uvc-device.cpp:746
int i
GLenum GLuint GLenum GLsizei length
signed int int32_t
Definition: stdint.h:77
#define LOG_DEBUG(...)
Definition: src/types.h:239
usb_status query_stream_ctrl(const std::shared_ptr< uvc_stream_ctrl_t > &control, uint8_t probe, int req)
Definition: uvc-device.cpp:788
#define DW_TO_INT(p)
Definition: uvc-types.h:19
GLuint64 GLenum GLint fd
Definition: glext.h:7768
static lock_singleton & instance()
Definition: uvc-device.cpp:20
void play_profile(stream_profile profile, frame_callback callback)
Definition: uvc-device.cpp:421
#define B_TO_BYTE(p)
Definition: uvc-types.h:12
uint32_t height
Definition: uvc-types.h:331
GLuint64EXT * result
Definition: glext.h:10921
int32_t get_data_usb(uvc_req_code action, int control, int unit, unsigned int length=sizeof(uint32_t)) const
Definition: uvc-device.cpp:484
Definition: parser.hpp:150
std::shared_ptr< usb_device > rs_usb_device
Definition: usb-device.h:29
void invoke_and_wait(T item, std::function< bool()> exit_condition, bool is_blocking=false)
Definition: concurrency.h:266
virtual control_range get_pu_range(rs2_option opt) const override
Definition: uvc-device.cpp:279
std::string to_string(T value)
virtual std::string get_device_location() const override
Definition: uvc-device.cpp:339


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