-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathpolling-device-watcher.h
78 lines (61 loc) · 2.47 KB
/
polling-device-watcher.h
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
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.
#pragma once
#include "backend.h"
#include "platform/device-watcher.h"
#include <rsutils/concurrency/concurrency.h>
#include "callback-invocation.h"
namespace librealsense {
// This device_watcher enumerates all devices every set amount of time (POLLING_DEVICES_INTERVAL_MS)
//
class polling_device_watcher : public librealsense::platform::device_watcher
{
public:
polling_device_watcher( const platform::backend * backend_ref )
: _backend( backend_ref )
, _active_object( [this]( dispatcher::cancellable_timer cancellable_timer ) { polling( cancellable_timer ); } )
, _devices_data()
{
_devices_data = { _backend->query_uvc_devices(), _backend->query_usb_devices(), _backend->query_hid_devices() };
}
~polling_device_watcher() { stop(); }
void polling( dispatcher::cancellable_timer cancellable_timer )
{
if( cancellable_timer.try_sleep( std::chrono::milliseconds( POLLING_DEVICES_INTERVAL_MS ) ) )
{
platform::backend_device_group curr( _backend->query_uvc_devices(),
_backend->query_usb_devices(),
_backend->query_hid_devices() );
if( list_changed( _devices_data.uvc_devices, curr.uvc_devices )
|| list_changed( _devices_data.usb_devices, curr.usb_devices )
|| list_changed( _devices_data.hid_devices, curr.hid_devices ) )
{
callback_invocation_holder callback = { _callback_inflight.allocate(), &_callback_inflight };
if( callback )
{
_callback( _devices_data, curr );
_devices_data = curr;
}
}
}
}
void start( platform::device_changed_callback callback ) override
{
stop();
_callback = std::move( callback );
_active_object.start();
}
void stop() override
{
_active_object.stop();
_callback_inflight.wait_until_empty();
}
bool is_stopped() const override { return ! _active_object.is_active(); }
private:
active_object<> _active_object;
callbacks_heap _callback_inflight;
const platform::backend * _backend;
platform::backend_device_group _devices_data;
platform::device_changed_callback _callback;
};
} // namespace librealsense