Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
rs_device.hpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #ifndef LIBREALSENSE_RS2_DEVICE_HPP
5 #define LIBREALSENSE_RS2_DEVICE_HPP
6 
7 #include "rs_types.hpp"
8 #include "rs_sensor.hpp"
9 #include <array>
10 
11 namespace rs2
12 {
13  class context;
14  class device_list;
15  class pipeline_profile;
16  class device_hub;
17 
18  class device
19  {
20  public:
25  std::vector<sensor> query_sensors() const
26  {
27  rs2_error* e = nullptr;
28  std::shared_ptr<rs2_sensor_list> list(
29  rs2_query_sensors(_dev.get(), &e),
31  error::handle(e);
32 
33  auto size = rs2_get_sensors_count(list.get(), &e);
34  error::handle(e);
35 
36  std::vector<sensor> results;
37  for (auto i = 0; i < size; i++)
38  {
39  std::shared_ptr<rs2_sensor> dev(
40  rs2_create_sensor(list.get(), i, &e),
42  error::handle(e);
43 
44  sensor rs2_dev(dev);
45  results.push_back(rs2_dev);
46  }
47 
48  return results;
49  }
50 
51  template<class T>
52  T first() const
53  {
54  for (auto&& s : query_sensors())
55  {
56  if (auto t = s.as<T>()) return t;
57  }
58  throw rs2::error("Could not find requested sensor type!");
59  }
60 
66  bool supports(rs2_camera_info info) const
67  {
68  rs2_error* e = nullptr;
69  auto is_supported = rs2_supports_device_info(_dev.get(), info, &e);
70  error::handle(e);
71  return is_supported > 0;
72  }
73 
79  const char* get_info(rs2_camera_info info) const
80  {
81  rs2_error* e = nullptr;
82  auto result = rs2_get_device_info(_dev.get(), info, &e);
83  error::handle(e);
84  return result;
85  }
86 
91  {
92  rs2_error* e = nullptr;
93 
94  rs2_hardware_reset(_dev.get(), &e);
95  error::handle(e);
96  }
97 
98  device& operator=(const std::shared_ptr<rs2_device> dev)
99  {
100  _dev.reset();
101  _dev = dev;
102  return *this;
103  }
104  device& operator=(const device& dev)
105  {
106  *this = nullptr;
107  _dev = dev._dev;
108  return *this;
109  }
110  device() : _dev(nullptr) {}
111 
112  operator bool() const
113  {
114  return _dev != nullptr;
115  }
116  const std::shared_ptr<rs2_device>& get() const
117  {
118  return _dev;
119  }
120 
121  template<class T>
122  bool is() const
123  {
124  T extension(*this);
125  return extension;
126  }
127 
128  template<class T>
129  T as() const
130  {
131  T extension(*this);
132  return extension;
133  }
134  virtual ~device()
135  {
136  }
137 
138  explicit operator std::shared_ptr<rs2_device>() { return _dev; };
139  explicit device(std::shared_ptr<rs2_device> dev) : _dev(dev) {}
140  protected:
141  friend class rs2::context;
142  friend class rs2::device_list;
143  friend class rs2::pipeline_profile;
144  friend class rs2::device_hub;
145 
146  std::shared_ptr<rs2_device> _dev;
147 
148  };
149 
150  class debug_protocol : public device
151  {
152  public:
154  : device(d.get())
155  {
156  rs2_error* e = nullptr;
157  if(rs2_is_device_extendable_to(_dev.get(), RS2_EXTENSION_DEBUG, &e) == 0 && !e)
158  {
159  _dev.reset();
160  }
161  error::handle(e);
162  }
163 
164  std::vector<uint8_t> send_and_receive_raw_data(const std::vector<uint8_t>& input) const
165  {
166  std::vector<uint8_t> results;
167 
168  rs2_error* e = nullptr;
169  std::shared_ptr<const rs2_raw_data_buffer> list(
170  rs2_send_and_receive_raw_data(_dev.get(), (void*)input.data(), (uint32_t)input.size(), &e),
172  error::handle(e);
173 
174  auto size = rs2_get_raw_data_size(list.get(), &e);
175  error::handle(e);
176 
177  auto start = rs2_get_raw_data(list.get(), &e);
178 
179  results.insert(results.begin(), start, start + size);
180 
181  return results;
182  }
183  };
184 
186  {
187  public:
188  explicit device_list(std::shared_ptr<rs2_device_list> list)
189  : _list(move(list)) {}
190 
192  : _list(nullptr) {}
193 
194  operator std::vector<device>() const
195  {
196  std::vector<device> res;
197  for (auto&& dev : *this) res.push_back(dev);
198  return res;
199  }
200 
201  bool contains(const device& dev) const
202  {
203  rs2_error* e = nullptr;
204  auto res = !!(rs2_device_list_contains(_list.get(), dev.get().get(), &e));
205  error::handle(e);
206  return res;
207  }
208 
209  device_list& operator=(std::shared_ptr<rs2_device_list> list)
210  {
211  _list = move(list);
212  return *this;
213  }
214 
215  device operator[](uint32_t index) const
216  {
217  rs2_error* e = nullptr;
218  std::shared_ptr<rs2_device> dev(
219  rs2_create_device(_list.get(), index, &e),
221  error::handle(e);
222 
223  return device(dev);
224  }
225 
226  uint32_t size() const
227  {
228  rs2_error* e = nullptr;
229  auto size = rs2_get_device_count(_list.get(), &e);
230  error::handle(e);
231  return size;
232  }
233 
234  device front() const { return std::move((*this)[0]); }
235  device back() const
236  {
237  return std::move((*this)[size() - 1]);
238  }
239 
241  {
243  const device_list& device_list,
244  uint32_t uint32_t)
245  : _list(device_list),
246  _index(uint32_t)
247  {
248  }
249 
250  public:
252  {
253  return _list[_index];
254  }
255  bool operator!=(const device_list_iterator& other) const
256  {
257  return other._index != _index || &other._list != &_list;
258  }
259  bool operator==(const device_list_iterator& other) const
260  {
261  return !(*this != other);
262  }
264  {
265  _index++;
266  return *this;
267  }
268  private:
269  friend device_list;
270  const device_list& _list;
271  uint32_t _index;
272  };
273 
275  {
276  return device_list_iterator(*this, 0);
277  }
279  {
280  return device_list_iterator(*this, size());
281  }
282  const rs2_device_list* get_list() const
283  {
284  return _list.get();
285  }
286 
287  operator std::shared_ptr<rs2_device_list>() { return _list; };
288 
289  private:
290  std::shared_ptr<rs2_device_list> _list;
291  };
292 
293  class tm2 : public device //TODO: add to wrappers
294  {
295  public:
297  : device(d.get())
298  {
299  rs2_error* e = nullptr;
300  if (rs2_is_device_extendable_to(_dev.get(), RS2_EXTENSION_TM2, &e) == 0 && !e)
301  {
302  _dev.reset();
303  }
304  error::handle(e);
305  }
306 
311  void enable_loopback(const std::string& from_file)
312  {
313  rs2_error* e = nullptr;
314  rs2_loopback_enable(_dev.get(), from_file.c_str(), &e);
315  error::handle(e);
316  }
317 
322  {
323  rs2_error* e = nullptr;
324  rs2_loopback_disable(_dev.get(), &e);
325  error::handle(e);
326  }
327 
332  bool is_loopback_enabled() const
333  {
334  rs2_error* e = nullptr;
335  int is_enabled = rs2_loopback_is_enabled(_dev.get(), &e);
336  error::handle(e);
337  return is_enabled != 0;
338  }
339 
344  void connect_controller(const std::array<uint8_t, 6>& mac_addr)
345  {
346  rs2_error* e = nullptr;
347  rs2_connect_tm2_controller(_dev.get(), mac_addr.data(), &e);
348  error::handle(e);
349  }
350 
356  {
357  rs2_error* e = nullptr;
358  rs2_disconnect_tm2_controller(_dev.get(), id, &e);
359  error::handle(e);
360  }
361  };
362 }
363 #endif // LIBREALSENSE_RS2_DEVICE_HPP
Definition: rs_types.hpp:69
device operator*() const
Definition: rs_device.hpp:251
device back() const
Definition: rs_device.hpp:235
int rs2_get_sensors_count(const rs2_sensor_list *info_list, rs2_error **error)
rs2_camera_info
Read-only strings that can be queried from the device. Not all information attributes are available o...
Definition: rs_sensor.h:22
Definition: rs_sensor.hpp:103
bool is() const
Definition: rs_device.hpp:122
int rs2_device_list_contains(const rs2_device_list *info_list, const rs2_device *device, rs2_error **error)
int rs2_loopback_is_enabled(const rs2_device *device, rs2_error **error)
device_list(std::shared_ptr< rs2_device_list > list)
Definition: rs_device.hpp:188
std::vector< uint8_t > send_and_receive_raw_data(const std::vector< uint8_t > &input) const
Definition: rs_device.hpp:164
std::vector< sensor > query_sensors() const
Definition: rs_device.hpp:25
device()
Definition: rs_device.hpp:110
const char * get_info(rs2_camera_info info) const
Definition: rs_device.hpp:79
device & operator=(const device &dev)
Definition: rs_device.hpp:104
Definition: rs_pipeline.hpp:18
bool operator==(const device_list_iterator &other) const
Definition: rs_device.hpp:259
rs2_sensor * rs2_create_sensor(const rs2_sensor_list *list, int index, rs2_error **error)
void rs2_delete_device(rs2_device *device)
void connect_controller(const std::array< uint8_t, 6 > &mac_addr)
Definition: rs_device.hpp:344
const unsigned char * rs2_get_raw_data(const rs2_raw_data_buffer *buffer, rs2_error **error)
void disconnect_controller(int id)
Definition: rs_device.hpp:355
rs2_device * rs2_create_device(const rs2_device_list *info_list, int index, rs2_error **error)
device_list & operator=(std::shared_ptr< rs2_device_list > list)
Definition: rs_device.hpp:209
Definition: rs_context.hpp:11
void rs2_delete_raw_data(const rs2_raw_data_buffer *buffer)
Definition: rs_context.hpp:96
int rs2_get_raw_data_size(const rs2_raw_data_buffer *buffer, rs2_error **error)
T as() const
Definition: rs_device.hpp:129
void rs2_connect_tm2_controller(const rs2_device *device, const unsigned char *mac_addr, rs2_error **error)
device_list_iterator begin() const
Definition: rs_device.hpp:274
void rs2_loopback_enable(const rs2_device *device, const char *from_file, rs2_error **error)
void rs2_disconnect_tm2_controller(const rs2_device *device, int id, rs2_error **error)
Definition: rs_device.hpp:150
bool is_loopback_enabled() const
Definition: rs_device.hpp:332
device operator[](uint32_t index) const
Definition: rs_device.hpp:215
int rs2_supports_device_info(const rs2_device *device, rs2_camera_info info, rs2_error **error)
void rs2_delete_sensor(rs2_sensor *sensor)
int rs2_is_device_extendable_to(const rs2_device *device, rs2_extension extension, rs2_error **error)
device front() const
Definition: rs_device.hpp:234
uint32_t size() const
Definition: rs_device.hpp:226
void hardware_reset()
Definition: rs_device.hpp:90
const std::shared_ptr< rs2_device > & get() const
Definition: rs_device.hpp:116
std::shared_ptr< rs2_device > _dev
Definition: rs_device.hpp:146
void rs2_hardware_reset(const rs2_device *device, rs2_error **error)
rs2_sensor_list * rs2_query_sensors(const rs2_device *device, rs2_error **error)
static void handle(rs2_error *e)
Definition: rs_types.hpp:121
Definition: rs_types.h:155
bool contains(const device &dev) const
Definition: rs_device.hpp:201
const rs2_raw_data_buffer * rs2_send_and_receive_raw_data(rs2_device *device, void *raw_data_to_send, unsigned size_of_raw_data_to_send, rs2_error **error)
Definition: rs_types.h:134
bool supports(rs2_camera_info info) const
Definition: rs_device.hpp:66
device & operator=(const std::shared_ptr< rs2_device > dev)
Definition: rs_device.hpp:98
device_list_iterator & operator++()
Definition: rs_device.hpp:263
device_list()
Definition: rs_device.hpp:191
Definition: rs_device.hpp:185
Definition: rs_context.hpp:224
bool operator!=(const device_list_iterator &other) const
Definition: rs_device.hpp:255
void rs2_delete_sensor_list(rs2_sensor_list *info_list)
void enable_loopback(const std::string &from_file)
Definition: rs_device.hpp:311
struct rs2_device_list rs2_device_list
Definition: rs_types.h:204
debug_protocol(device d)
Definition: rs_device.hpp:153
void disable_loopback()
Definition: rs_device.hpp:321
tm2(device d)
Definition: rs_device.hpp:296
device_list_iterator end() const
Definition: rs_device.hpp:278
Definition: rs_device.hpp:293
struct rs2_error rs2_error
Definition: rs_types.h:197
Definition: rs_device.hpp:240
void rs2_loopback_disable(const rs2_device *device, rs2_error **error)
Definition: rs_device.hpp:18
int rs2_get_device_count(const rs2_device_list *info_list, rs2_error **error)
const rs2_device_list * get_list() const
Definition: rs_device.hpp:282
T first() const
Definition: rs_device.hpp:52
virtual ~device()
Definition: rs_device.hpp:134
device(std::shared_ptr< rs2_device > dev)
Definition: rs_device.hpp:139
const char * rs2_get_device_info(const rs2_device *device, rs2_camera_info info, rs2_error **error)