device_hub.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 <librealsense2/rs.hpp>
5 #include "source.h"
6 #include "core/processing.h"
8 #include "device_hub.h"
9 
10 namespace librealsense
11 {
13 
14  std::vector<std::shared_ptr<device_info>> filter_by_vid(std::vector<std::shared_ptr<device_info>> devices , int vid)
15  {
16  std::vector<std::shared_ptr<device_info>> result;
17  for (auto dev : devices)
18  {
19  bool filtered = false;
20  auto data = dev->get_device_data();
21  for (const auto& usb : data.usb_devices)
22  {
23  if (usb.vid == vid || vid == 0)
24  {
25  result.push_back(dev);
26  filtered = true;
27  break;
28  }
29  }
30  for (const auto& uvc : data.uvc_devices)
31  {
32  if (uvc.vid == vid || vid == 0)
33  {
34  result.push_back(dev);
35  filtered = true;
36  break;
37  }
38  }
39  }
40  return result;
41  }
42 
43  device_hub::device_hub(std::shared_ptr<librealsense::context> ctx, int mask, int vid,
44  bool register_device_notifications)
45  : _ctx(ctx), _vid(vid),
46  _device_changes_callback_id(0),
47  _register_device_notifications(register_device_notifications)
48  {
49  _device_list = filter_by_vid(_ctx->query_devices(mask), _vid);
50 
52  {
53  std::unique_lock<std::mutex> lock(_mutex);
54 
55  _device_list = filter_by_vid(_ctx->query_devices(mask), _vid);
56 
57  // Current device will point to the first available device
58  _camera_index = 0;
59  if (_device_list.size() > 0)
60  {
61  _cv.notify_all();
62  }
63  });
64 
65  _device_changes_callback_id = _ctx->register_internal_device_callback({ cb, [](rs2_devices_changed_callback* p) { p->release(); } });
66  }
67 
69  {
71  _ctx->unregister_internal_device_callback(_device_changes_callback_id);
72 
73  _ctx->stop();
74  }
75 
76  std::shared_ptr<device_interface> device_hub::create_device(const std::string& serial, bool cycle_devices)
77  {
78  std::shared_ptr<device_interface> res = nullptr;
79  for(size_t i = 0; ((i< _device_list.size()) && (nullptr == res)); i++)
80  {
81  // _camera_index is the curr device that the hub will expose
82  auto d = _device_list[ (_camera_index + i) % _device_list.size()];
83  try
84  {
85  auto dev = d->create_device(_register_device_notifications);
86 
87  if(serial.size() > 0 )
88  {
89  auto new_serial = dev->get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
90 
91  if(serial == new_serial)
92  {
93  res = dev;
94  cycle_devices = false; // Requesting a device by its serial shall not invoke internal cycling
95  }
96  }
97  else // Use the first selected if "any device" pattern was used
98  {
99  res = dev;
100  }
101  }
102  catch (const std::exception& ex)
103  {
104  LOG_WARNING("Could not open device " << ex.what());
105  }
106  }
107 
108  // Advance the internal selection when appropriate
109  if (res && cycle_devices)
111 
112  return res;
113  }
114 
115 
120  std::shared_ptr<device_interface> device_hub::wait_for_device(const std::chrono::milliseconds& timeout, bool loop_through_devices, const std::string& serial)
121  {
122  std::unique_lock<std::mutex> lock(_mutex);
123 
124  std::shared_ptr<device_interface> res = nullptr;
125 
126  // check if there is at least one device connected
128  if (_device_list.size() > 0)
129  {
130  res = create_device(serial, loop_through_devices);
131  }
132 
133  if (res) return res;
134 
135  // block for the requested device to be connected, or till the timeout occurs
136  if (!_cv.wait_for(lock, timeout, [&]()
137  {
138  if (_device_list.size() > 0)
139  {
140  res = create_device(serial, loop_through_devices);
141  }
142  return res != nullptr;
143  }))
144  {
145  throw std::runtime_error("No device connected");
146  }
147  return res;
148  }
149 
154  {
155  std::unique_lock<std::mutex> lock(_mutex);
156  return dev.is_valid();
157  }
158 
159  std::shared_ptr<librealsense::context> device_hub::get_context()
160  {
161  return _ctx;
162  }
163 }
164 
static const textual_icon lock
Definition: model-views.h:218
device_hub(std::shared_ptr< librealsense::context > ctx, int mask=RS2_PRODUCT_LINE_ANY, int vid=0, bool register_device_notifications=true)
Definition: device_hub.cpp:43
bool is_connected(const device_interface &dev)
Definition: device_hub.cpp:153
GLfloat GLfloat p
Definition: glext.h:12687
#define LOG_WARNING(...)
Definition: src/types.h:241
GLint GLuint mask
GLsizei const GLchar *const * string
d
Definition: rmse.py:171
std::condition_variable _cv
Definition: device_hub.h:52
virtual bool is_valid() const =0
devices
Definition: test-fg.py:9
std::shared_ptr< device_interface > create_device(const std::string &serial, bool cycle_devices=true)
Definition: device_hub.cpp:76
std::shared_ptr< librealsense::context > _ctx
Definition: device_hub.h:50
std::shared_ptr< device_interface > wait_for_device(const std::chrono::milliseconds &timeout=std::chrono::milliseconds(std::chrono::hours(1)), bool loop_through_devices=true, const std::string &serial="")
Definition: device_hub.cpp:120
#define RS2_PRODUCT_LINE_ANY
Definition: rs_context.h:91
uint64_t _device_changes_callback_id
Definition: device_hub.h:56
GLbitfield GLuint64 timeout
std::vector< std::shared_ptr< device_info > > filter_by_vid(std::vector< std::shared_ptr< device_info >> devices, int vid)
Definition: device_hub.cpp:14
std::vector< std::shared_ptr< device_info > > _device_list
Definition: device_hub.h:53
rs2::devices_changed_callback< std::function< void(rs2::event_information &info)> > hub_devices_changed_callback
Definition: device_hub.cpp:12
int i
GLuint res
Definition: glext.h:8856
std::shared_ptr< librealsense::context > get_context()
Definition: device_hub.cpp:159
GLuint64EXT * result
Definition: glext.h:10921
Definition: parser.hpp:150


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