forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs_device.hpp
479 lines (411 loc) · 13.6 KB
/
rs_device.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
#ifndef LIBREALSENSE_RS2_DEVICE_HPP
#define LIBREALSENSE_RS2_DEVICE_HPP
#include "rs_types.hpp"
#include "rs_sensor.hpp"
#include <array>
namespace rs2
{
class context;
class device_list;
class pipeline_profile;
class device_hub;
class device
{
public:
/**
* returns the list of adjacent devices, sharing the same physical parent composite device
* \return the list of adjacent devices
*/
std::vector<sensor> query_sensors() const
{
rs2_error* e = nullptr;
std::shared_ptr<rs2_sensor_list> list(
rs2_query_sensors(_dev.get(), &e),
rs2_delete_sensor_list);
error::handle(e);
auto size = rs2_get_sensors_count(list.get(), &e);
error::handle(e);
std::vector<sensor> results;
for (auto i = 0; i < size; i++)
{
std::shared_ptr<rs2_sensor> dev(
rs2_create_sensor(list.get(), i, &e),
rs2_delete_sensor);
error::handle(e);
sensor rs2_dev(dev);
results.push_back(rs2_dev);
}
return results;
}
template<class T>
T first() const
{
for (auto&& s : query_sensors())
{
if (auto t = s.as<T>()) return t;
}
throw rs2::error("Could not find requested sensor type!");
}
/**
* check if specific camera info is supported
* \param[in] info the parameter to check for support
* \return true if the parameter both exist and well-defined for the specific device
*/
bool supports(rs2_camera_info info) const
{
rs2_error* e = nullptr;
auto is_supported = rs2_supports_device_info(_dev.get(), info, &e);
error::handle(e);
return is_supported > 0;
}
/**
* retrieve camera specific information, like versions of various internal components
* \param[in] info camera info type to retrieve
* \return the requested camera info string, in a format specific to the device model
*/
const char* get_info(rs2_camera_info info) const
{
rs2_error* e = nullptr;
auto result = rs2_get_device_info(_dev.get(), info, &e);
error::handle(e);
return result;
}
/**
* send hardware reset request to the device
*/
void hardware_reset()
{
rs2_error* e = nullptr;
rs2_hardware_reset(_dev.get(), &e);
error::handle(e);
}
device& operator=(const std::shared_ptr<rs2_device> dev)
{
_dev.reset();
_dev = dev;
return *this;
}
device& operator=(const device& dev)
{
*this = nullptr;
_dev = dev._dev;
return *this;
}
device() : _dev(nullptr) {}
operator bool() const
{
return _dev != nullptr;
}
const std::shared_ptr<rs2_device>& get() const
{
return _dev;
}
template<class T>
bool is() const
{
T extension(*this);
return extension;
}
template<class T>
T as() const
{
T extension(*this);
return extension;
}
virtual ~device()
{
}
explicit operator std::shared_ptr<rs2_device>() { return _dev; };
explicit device(std::shared_ptr<rs2_device> dev) : _dev(dev) {}
protected:
friend class rs2::context;
friend class rs2::device_list;
friend class rs2::pipeline_profile;
friend class rs2::device_hub;
std::shared_ptr<rs2_device> _dev;
};
template<class T>
class update_progress_callback : public rs2_update_progress_callback
{
T _callback;
public:
explicit update_progress_callback(T callback) : _callback(callback) {}
void on_update_progress(const float progress) override
{
_callback(progress);
}
void release() override { delete this; }
};
class updatable : public device
{
public:
updatable() : device() {}
updatable(device d)
: device(d.get())
{
rs2_error* e = nullptr;
if (rs2_is_device_extendable_to(_dev.get(), RS2_EXTENSION_UPDATABLE, &e) == 0 && !e)
{
_dev.reset();
}
error::handle(e);
}
// Enter the device to update state, this will cause the updatable device to disconnect and reconnect as update device.
void enter_update_state() const
{
rs2_error* e = nullptr;
rs2_enter_update_state(_dev.get(), &e);
error::handle(e);
}
std::vector<uint8_t> create_flash_backup() const
{
std::vector<uint8_t> results;
rs2_error* e = nullptr;
std::shared_ptr<const rs2_raw_data_buffer> list(
rs2_create_flash_backup_cpp(_dev.get(), nullptr, &e),
rs2_delete_raw_data);
error::handle(e);
auto size = rs2_get_raw_data_size(list.get(), &e);
error::handle(e);
auto start = rs2_get_raw_data(list.get(), &e);
results.insert(results.begin(), start, start + size);
return results;
}
template<class T>
std::vector<uint8_t> create_flash_backup(T callback) const
{
std::vector<uint8_t> results;
rs2_error* e = nullptr;
std::shared_ptr<const rs2_raw_data_buffer> list(
rs2_create_flash_backup_cpp(_dev.get(), new update_progress_callback<T>(std::move(callback)), &e),
rs2_delete_raw_data);
error::handle(e);
auto size = rs2_get_raw_data_size(list.get(), &e);
error::handle(e);
auto start = rs2_get_raw_data(list.get(), &e);
results.insert(results.begin(), start, start + size);
return results;
}
};
class update_device : public device
{
public:
update_device() : device() {}
update_device(device d)
: device(d.get())
{
rs2_error* e = nullptr;
if (rs2_is_device_extendable_to(_dev.get(), RS2_EXTENSION_UPDATE_DEVICE, &e) == 0 && !e)
{
_dev.reset();
}
error::handle(e);
}
// Update an updatable device to the provided firmware.
// This call is executed on the caller's thread.
void update(const std::vector<uint8_t>& fw_image) const
{
rs2_error* e = nullptr;
rs2_update_firmware_cpp(_dev.get(), fw_image.data(), fw_image.size(), NULL, &e);
error::handle(e);
}
// Update an updatable device to the provided firmware.
// This call is executed on the caller's thread and it supports progress notifications via the callback.
template<class T>
void update(const std::vector<uint8_t>& fw_image, T callback) const
{
rs2_error* e = nullptr;
rs2_update_firmware_cpp(_dev.get(), fw_image.data(), fw_image.size(), new update_progress_callback<T>(std::move(callback)), &e);
error::handle(e);
}
};
class debug_protocol : public device
{
public:
debug_protocol(device d)
: device(d.get())
{
rs2_error* e = nullptr;
if(rs2_is_device_extendable_to(_dev.get(), RS2_EXTENSION_DEBUG, &e) == 0 && !e)
{
_dev.reset();
}
error::handle(e);
}
std::vector<uint8_t> send_and_receive_raw_data(const std::vector<uint8_t>& input) const
{
std::vector<uint8_t> results;
rs2_error* e = nullptr;
std::shared_ptr<const rs2_raw_data_buffer> list(
rs2_send_and_receive_raw_data(_dev.get(), (void*)input.data(), (uint32_t)input.size(), &e),
rs2_delete_raw_data);
error::handle(e);
auto size = rs2_get_raw_data_size(list.get(), &e);
error::handle(e);
auto start = rs2_get_raw_data(list.get(), &e);
results.insert(results.begin(), start, start + size);
return results;
}
};
class device_list
{
public:
explicit device_list(std::shared_ptr<rs2_device_list> list)
: _list(move(list)) {}
device_list()
: _list(nullptr) {}
operator std::vector<device>() const
{
std::vector<device> res;
for (auto&& dev : *this) res.push_back(dev);
return res;
}
bool contains(const device& dev) const
{
rs2_error* e = nullptr;
auto res = !!(rs2_device_list_contains(_list.get(), dev.get().get(), &e));
error::handle(e);
return res;
}
device_list& operator=(std::shared_ptr<rs2_device_list> list)
{
_list = move(list);
return *this;
}
device operator[](uint32_t index) const
{
rs2_error* e = nullptr;
std::shared_ptr<rs2_device> dev(
rs2_create_device(_list.get(), index, &e),
rs2_delete_device);
error::handle(e);
return device(dev);
}
uint32_t size() const
{
rs2_error* e = nullptr;
auto size = rs2_get_device_count(_list.get(), &e);
error::handle(e);
return size;
}
device front() const { return std::move((*this)[0]); }
device back() const
{
return std::move((*this)[size() - 1]);
}
class device_list_iterator
{
device_list_iterator(
const device_list& device_list,
uint32_t uint32_t)
: _list(device_list),
_index(uint32_t)
{
}
public:
device operator*() const
{
return _list[_index];
}
bool operator!=(const device_list_iterator& other) const
{
return other._index != _index || &other._list != &_list;
}
bool operator==(const device_list_iterator& other) const
{
return !(*this != other);
}
device_list_iterator& operator++()
{
_index++;
return *this;
}
private:
friend device_list;
const device_list& _list;
uint32_t _index;
};
device_list_iterator begin() const
{
return device_list_iterator(*this, 0);
}
device_list_iterator end() const
{
return device_list_iterator(*this, size());
}
const rs2_device_list* get_list() const
{
return _list.get();
}
operator std::shared_ptr<rs2_device_list>() { return _list; };
private:
std::shared_ptr<rs2_device_list> _list;
};
class tm2 : public device //TODO: add to wrappers
{
public:
tm2(device d)
: device(d.get())
{
rs2_error* e = nullptr;
if (rs2_is_device_extendable_to(_dev.get(), RS2_EXTENSION_TM2, &e) == 0 && !e)
{
_dev.reset();
}
error::handle(e);
}
/**
* Enter the given device into loopback operation mode that uses the given file as input for raw data
* \param[in] from_file Path to bag file with raw data for loopback
*/
void enable_loopback(const std::string& from_file)
{
rs2_error* e = nullptr;
rs2_loopback_enable(_dev.get(), from_file.c_str(), &e);
error::handle(e);
}
/**
* Restores the given device into normal operation mode
*/
void disable_loopback()
{
rs2_error* e = nullptr;
rs2_loopback_disable(_dev.get(), &e);
error::handle(e);
}
/**
* Checks if the device is in loopback mode or not
* \return true if the device is in loopback operation mode
*/
bool is_loopback_enabled() const
{
rs2_error* e = nullptr;
int is_enabled = rs2_loopback_is_enabled(_dev.get(), &e);
error::handle(e);
return is_enabled != 0;
}
/**
* Connects to a given tm2 controller
* \param[in] mac_addr The MAC address of the desired controller
*/
void connect_controller(const std::array<uint8_t, 6>& mac_addr)
{
rs2_error* e = nullptr;
rs2_connect_tm2_controller(_dev.get(), mac_addr.data(), &e);
error::handle(e);
}
/**
* Disconnects a given tm2 controller
* \param[in] id The ID of the desired controller
*/
void disconnect_controller(int id)
{
rs2_error* e = nullptr;
rs2_disconnect_tm2_controller(_dev.get(), id, &e);
error::handle(e);
}
};
}
#endif // LIBREALSENSE_RS2_DEVICE_HPP