-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Expand file tree
/
Copy pathcable_scanner_linux.cpp
More file actions
249 lines (217 loc) · 6.47 KB
/
Copy pathcable_scanner_linux.cpp
File metadata and controls
249 lines (217 loc) · 6.47 KB
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
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "webauthn/cable_scanner.h"
#include "webauthn/cable_core.h"
#include "base/algorithm.h"
#include "base/debug_log.h"
#include "base/weak_ptr.h"
#include <crl/crl_on_main.h>
#include <bluez/bluez.hpp>
#include <set>
#include <vector>
namespace Platform::WebAuthn::Cable {
namespace {
using namespace gi::repository;
namespace GObject = gi::repository::GObject;
constexpr auto kBluezService = "org.bluez";
constexpr auto kBluezObjectPath = "/";
constexpr auto kGoogleCableUuid = "0000fde2-0000-1000-8000-00805f9b34fb";
constexpr auto kFidoCableUuid = "0000fff9-0000-1000-8000-00805f9b34fb";
[[nodiscard]] bool IsCableUuid(const gi::cstring_v uuid) {
return !GLib::ascii_strcasecmp(uuid, kGoogleCableUuid)
|| !GLib::ascii_strcasecmp(uuid, kFidoCableUuid);
}
[[nodiscard]] QByteArray FixedArrayBytes(GLib::Variant value) {
return QByteArray(
static_cast<const char*>(value.get_data()),
int(value.get_size()));
}
[[nodiscard]] std::vector<QByteArray> CableServiceData(
GLib::Variant serviceData) {
auto result = std::vector<QByteArray>();
if (!serviceData) {
return result;
}
const auto count = serviceData.n_children();
for (auto i = gsize(0); i != count; ++i) {
auto entry = serviceData.get_child_value(i);
if (!IsCableUuid(entry.get_child_value(0).get_string(nullptr))) {
continue;
}
auto value = entry.get_child_value(1).get_variant();
if (value.is_of_type(GLib::VariantType::new_("ay"))
&& value.get_size() >= kAdvertSize) {
result.push_back(FixedArrayBytes(value));
}
}
return result;
}
[[nodiscard]] GLib::Variant CableDiscoveryFilter() {
return GLib::Variant::new_array({
GLib::Variant::new_dict_entry(
GLib::Variant::new_string("Transport"),
GLib::Variant::new_variant(GLib::Variant::new_string("le"))),
GLib::Variant::new_dict_entry(
GLib::Variant::new_string("DuplicateData"),
GLib::Variant::new_variant(GLib::Variant::new_boolean(true))),
});
}
class LinuxBleScanner final : public BleScanner, public base::has_weak_ptr {
public:
~LinuxBleScanner();
bool start(
std::function<void(QByteArray)> onAdvert,
std::function<void(bool)> onAvailability) override;
void stop() override;
private:
void subscribeToObjects();
void handleObject(Gio::DBusObject object);
void handleDevice(Bluez::Device1 device);
void setDiscoveryFilter();
void startDiscovery();
void handleAvailability(bool available);
Bluez::ObjectManagerClient _manager;
Bluez::Adapter1 _adapter;
Gio::Cancellable _cancellable;
std::set<QByteArray> _seen;
bool _discovering = false;
std::function<void(QByteArray)> _onAdvert;
std::function<void(bool)> _onAvailability;
};
LinuxBleScanner::~LinuxBleScanner() {
stop();
}
bool LinuxBleScanner::start(
std::function<void(QByteArray)> onAdvert,
std::function<void(bool)> onAvailability) {
stop();
_cancellable = Gio::Cancellable::new_();
_onAdvert = std::move(onAdvert);
_onAvailability = std::move(onAvailability);
Bluez::ObjectManagerClient::new_for_bus(
Gio::BusType::SYSTEM_,
Gio::DBusObjectManagerClientFlags::NONE_,
kBluezService,
kBluezObjectPath,
_cancellable,
crl::guard(this, [=](GObject::Object, Gio::AsyncResult result) {
if (!_cancellable) {
return;
}
auto manager = Bluez::ObjectManagerClient::new_for_bus_finish(
result);
if (!manager) {
Gio::DBusErrorNS_::strip_remote_error(manager.error());
LOG(("Passkey Error: BlueZ object manager: %1."
).arg(manager.error().message_().c_str()));
handleAvailability(false);
return;
}
_manager = *manager;
subscribeToObjects();
auto objects = Gio::DBusObjectManager(_manager).get_objects();
for (auto object : objects) {
handleObject(object);
}
if (!_adapter) {
LOG(("Passkey Error: No bluetooth adapter in BlueZ."));
handleAvailability(false);
return;
} else if (!_adapter.get_powered()) {
LOG(("Passkey Error: Bluetooth adapter is powered off."));
handleAvailability(false);
return;
}
setDiscoveryFilter();
}));
return true;
}
void LinuxBleScanner::subscribeToObjects() {
Gio::DBusObjectManager(_manager).signal_object_added().connect(
crl::guard(this, [=](
Gio::DBusObjectManager,
Gio::DBusObject object) {
handleObject(object);
}));
}
void LinuxBleScanner::handleObject(Gio::DBusObject object) {
auto typed = gi::object_cast<Bluez::Object>(object);
if (!typed) {
return;
} else if (auto device = typed.get_device1()) {
device.property_service_data().signal_notify().connect(
crl::guard(this, [=](auto changed, auto) {
if (auto device = gi::object_cast<Bluez::Device1>(changed)) {
handleDevice(device);
}
}));
handleDevice(device);
} else if (!_adapter) {
_adapter = typed.get_adapter1();
}
}
void LinuxBleScanner::handleDevice(Bluez::Device1 device) {
for (auto &blob : CableServiceData(device.get_service_data())) {
if (_seen.emplace(blob).second && _onAdvert) {
_onAdvert(blob);
}
}
}
void LinuxBleScanner::setDiscoveryFilter() {
_adapter.call_set_discovery_filter(
CableDiscoveryFilter(),
_cancellable,
crl::guard(this, [=](GObject::Object, Gio::AsyncResult) {
if (!_cancellable) {
return;
}
startDiscovery();
}));
}
void LinuxBleScanner::startDiscovery() {
_adapter.call_start_discovery(
_cancellable,
crl::guard(this, [=](GObject::Object, Gio::AsyncResult result) {
if (!_cancellable) {
return;
}
auto started = _adapter.call_start_discovery_finish(result);
if (!started) {
Gio::DBusErrorNS_::strip_remote_error(started.error());
LOG(("Passkey Error: BlueZ StartDiscovery: %1."
).arg(started.error().message_().c_str()));
handleAvailability(false);
return;
}
_discovering = true;
handleAvailability(true);
}));
}
void LinuxBleScanner::handleAvailability(bool available) {
if (const auto onAvailability = base::take(_onAvailability)) {
onAvailability(available);
}
}
void LinuxBleScanner::stop() {
if (!_cancellable) {
return;
}
base::take(_cancellable).cancel();
if (base::take(_discovering)) {
_adapter.call_stop_discovery([](GObject::Object, Gio::AsyncResult) {});
}
_manager = nullptr;
_adapter = nullptr;
_onAdvert = nullptr;
_onAvailability = nullptr;
_seen.clear();
}
} // namespace
std::unique_ptr<BleScanner> MakeBleScanner() {
return std::make_unique<LinuxBleScanner>();
}
} // namespace Platform::WebAuthn::Cable