Intel® RealSense™ Cross Platform API  2.13.0
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  protected:
138  friend class rs2::context;
139  friend class rs2::device_list;
140  friend class rs2::pipeline_profile;
141  friend class rs2::device_hub;
142 
143  std::shared_ptr<rs2_device> _dev;
144  explicit device(std::shared_ptr<rs2_device> dev) : _dev(dev)
145  {
146  }
147  };
148 
149  class debug_protocol : public device
150  {
151  public:
153  : device(d.get())
154  {
155  rs2_error* e = nullptr;
156  if(rs2_is_device_extendable_to(_dev.get(), RS2_EXTENSION_DEBUG, &e) == 0 && !e)
157  {
158  _dev.reset();
159  }
160  error::handle(e);
161  }
162 
163  std::vector<uint8_t> send_and_receive_raw_data(const std::vector<uint8_t>& input) const
164  {
165  std::vector<uint8_t> results;
166 
167  rs2_error* e = nullptr;
168  std::shared_ptr<const rs2_raw_data_buffer> list(
169  rs2_send_and_receive_raw_data(_dev.get(), (void*)input.data(), (uint32_t)input.size(), &e),
171  error::handle(e);
172 
173  auto size = rs2_get_raw_data_size(list.get(), &e);
174  error::handle(e);
175 
176  auto start = rs2_get_raw_data(list.get(), &e);
177 
178  results.insert(results.begin(), start, start + size);
179 
180  return results;
181  }
182  };
183 
185  {
186  public:
187  explicit device_list(std::shared_ptr<rs2_device_list> list)
188  : _list(move(list)) {}
189 
191  : _list(nullptr) {}
192 
193  operator std::vector<device>() const
194  {
195  std::vector<device> res;
196  for (auto&& dev : *this) res.push_back(dev);
197  return res;
198  }
199 
200  bool contains(const device& dev) const
201  {
202  rs2_error* e = nullptr;
203  auto res = !!(rs2_device_list_contains(_list.get(), dev.get().get(), &e));
204  error::handle(e);
205  return res;
206  }
207 
208  device_list& operator=(std::shared_ptr<rs2_device_list> list)
209  {
210  _list = move(list);
211  return *this;
212  }
213 
214  device operator[](uint32_t index) const
215  {
216  rs2_error* e = nullptr;
217  std::shared_ptr<rs2_device> dev(
218  rs2_create_device(_list.get(), index, &e),
220  error::handle(e);
221 
222  return device(dev);
223  }
224 
225  uint32_t size() const
226  {
227  rs2_error* e = nullptr;
228  auto size = rs2_get_device_count(_list.get(), &e);
229  error::handle(e);
230  return size;
231  }
232 
233  device front() const { return std::move((*this)[0]); }
234  device back() const
235  {
236  return std::move((*this)[size() - 1]);
237  }
238 
240  {
242  const device_list& device_list,
243  uint32_t uint32_t)
244  : _list(device_list),
245  _index(uint32_t)
246  {
247  }
248 
249  public:
251  {
252  return _list[_index];
253  }
254  bool operator!=(const device_list_iterator& other) const
255  {
256  return other._index != _index || &other._list != &_list;
257  }
258  bool operator==(const device_list_iterator& other) const
259  {
260  return !(*this != other);
261  }
263  {
264  _index++;
265  return *this;
266  }
267  private:
268  friend device_list;
269  const device_list& _list;
270  uint32_t _index;
271  };
272 
274  {
275  return device_list_iterator(*this, 0);
276  }
278  {
279  return device_list_iterator(*this, size());
280  }
281  const rs2_device_list* get_list() const
282  {
283  return _list.get();
284  }
285 
286  private:
287  std::shared_ptr<rs2_device_list> _list;
288  };
289 
290  class tm2 : public device //TODO: add to wrappers
291  {
292  public:
294  : device(d.get())
295  {
296  rs2_error* e = nullptr;
297  if (rs2_is_device_extendable_to(_dev.get(), RS2_EXTENSION_TM2, &e) == 0 && !e)
298  {
299  _dev.reset();
300  }
301  error::handle(e);
302  }
303 
308  void enable_loopback(const std::string& from_file)
309  {
310  rs2_error* e = nullptr;
311  rs2_loopback_enable(_dev.get(), from_file.c_str(), &e);
312  error::handle(e);
313  }
314 
319  {
320  rs2_error* e = nullptr;
321  rs2_loopback_disable(_dev.get(), &e);
322  error::handle(e);
323  }
324 
329  bool is_loopback_enabled() const
330  {
331  rs2_error* e = nullptr;
332  int is_enabled = rs2_loopback_is_enabled(_dev.get(), &e);
333  error::handle(e);
334  return is_enabled != 0;
335  }
336 
341  void connect_controller(const std::array<uint8_t, 6>& mac_addr)
342  {
343  rs2_error* e = nullptr;
344  rs2_connect_tm2_controller(_dev.get(), mac_addr.data(), &e);
345  error::handle(e);
346  }
347 
353  {
354  rs2_error* e = nullptr;
355  rs2_disconnect_tm2_controller(_dev.get(), id, &e);
356  error::handle(e);
357  }
358  };
359 }
360 #endif // LIBREALSENSE_RS2_DEVICE_HPP
Definition: rs_types.hpp:69
device operator*() const
Definition: rs_device.hpp:250
device back() const
Definition: rs_device.hpp:234
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:232
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:187
std::vector< uint8_t > send_and_receive_raw_data(const std::vector< uint8_t > &input) const
Definition: rs_device.hpp:163
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:258
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:341
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:352
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:208
Definition: rs_context.hpp:11
void rs2_delete_raw_data(const rs2_raw_data_buffer *buffer)
Definition: rs_context.hpp:78
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:273
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:149
bool is_loopback_enabled() const
Definition: rs_device.hpp:329
device operator[](uint32_t index) const
Definition: rs_device.hpp:214
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:233
uint32_t size() const
Definition: rs_device.hpp:225
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:143
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:118
bool contains(const device &dev) const
Definition: rs_device.hpp:200
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:97
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:262
device_list()
Definition: rs_device.hpp:190
Definition: rs_device.hpp:184
Definition: rs_context.hpp:182
bool operator!=(const device_list_iterator &other) const
Definition: rs_device.hpp:254
void rs2_delete_sensor_list(rs2_sensor_list *info_list)
void enable_loopback(const std::string &from_file)
Definition: rs_device.hpp:308
struct rs2_device_list rs2_device_list
Definition: rs_types.h:156
debug_protocol(device d)
Definition: rs_device.hpp:152
void disable_loopback()
Definition: rs_device.hpp:318
tm2(device d)
Definition: rs_device.hpp:293
device_list_iterator end() const
Definition: rs_device.hpp:277
Definition: rs_device.hpp:290
struct rs2_error rs2_error
Definition: rs_types.h:149
Definition: rs_device.hpp:239
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:281
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:144
const char * rs2_get_device_info(const rs2_device *device, rs2_camera_info info, rs2_error **error)