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
content / browser / devtools / protocol / bluetooth_emulation_handler.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/devtools/protocol/bluetooth_emulation_handler.h"
#include <map>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/containers/span.h"
#include "content/browser/bluetooth/bluetooth_adapter_factory_wrapper.h"
#include "content/browser/devtools/protocol/bluetooth_emulation.h"
#include "device/bluetooth/emulation/fake_central.h"
using device::BluetoothAdapterFactory;
namespace content::protocol {
namespace {
base::flat_map<uint16_t, std::vector<uint8_t>> ToManufacturerData(
protocol::Array<protocol::BluetoothEmulation::ManufacturerData>*
in_manufacturer_data) {
base::flat_map<uint16_t, std::vector<uint8_t>> out_manufacturer_data;
for (auto& data : *in_manufacturer_data) {
const auto& binary_data = data->GetData();
auto span = base::as_byte_span(binary_data);
out_manufacturer_data[data->GetKey()] =
std::vector<uint8_t>(span.begin(), span.end());
}
return out_manufacturer_data;
}
std::vector<device::BluetoothUUID> ToBluetoothUUIDs(
protocol::Array<protocol::String>* in_bluetooth_ids) {
std::vector<device::BluetoothUUID> out_bluetooth_ids;
for (auto& uuid : *in_bluetooth_ids) {
out_bluetooth_ids.emplace_back(uuid);
}
return out_bluetooth_ids;
}
mojo::StructPtr<bluetooth::mojom::ScanRecord> ToScanRecord(
BluetoothEmulation::ScanRecord* in_record) {
mojo::StructPtr<bluetooth::mojom::ScanRecord> out_record =
bluetooth::mojom::ScanRecord::New();
if (in_record->HasUuids()) {
out_record->uuids = ToBluetoothUUIDs(in_record->GetUuids(nullptr));
}
// Convert Appearance
if (in_record->HasAppearance()) {
out_record->appearance = bluetooth::mojom::Appearance::New(
in_record->HasAppearance(), in_record->GetAppearance(0));
}
// Convert TX Power
if (in_record->HasTxPower()) {
out_record->tx_power = bluetooth::mojom::Power::New(
in_record->HasTxPower(), in_record->GetTxPower(0));
}
if (in_record->HasManufacturerData()) {
out_record->manufacturer_data =
ToManufacturerData(in_record->GetManufacturerData(nullptr));
}
return out_record;
}
bluetooth::mojom::CentralState ToCentralState(const String& state_string) {
if (state_string ==
protocol::BluetoothEmulation::CentralStateEnum::PoweredOff) {
return bluetooth::mojom::CentralState::POWERED_OFF;
} else if (state_string ==
protocol::BluetoothEmulation::CentralStateEnum::PoweredOn) {
return bluetooth::mojom::CentralState::POWERED_ON;
}
return bluetooth::mojom::CentralState::ABSENT;
}
} // namespace
BluetoothEmulationHandler::BluetoothEmulationHandler()
: DevToolsDomainHandler(BluetoothEmulation::Metainfo::domainName) {}
BluetoothEmulationHandler::~BluetoothEmulationHandler() = default;
void BluetoothEmulationHandler::Wire(UberDispatcher* dispatcher) {
BluetoothEmulation::Dispatcher::wire(dispatcher, this);
}
Response BluetoothEmulationHandler::Enable(const String& in_state) {
if (emulation_enabled_ || fake_central_.is_bound()) {
return Response::ServerError("BluetoothEmulation already enabled");
}
emulation_enabled_ = true;
content::BluetoothAdapterFactoryWrapper::Get().SetBluetoothAdapterOverride(
base::MakeRefCounted<bluetooth::FakeCentral>(
ToCentralState(in_state),
fake_central_.BindNewPipeAndPassReceiver()));
return Response::Success();
}
Response BluetoothEmulationHandler::Disable() {
if (fake_central_.is_bound()) {
CHECK(emulation_enabled_);
fake_central_.reset();
// reset this only if this is the instance holding the bound central.
content::BluetoothAdapterFactoryWrapper::Get().SetBluetoothAdapterOverride(
nullptr);
emulation_enabled_ = false;
}
return Response::Success();
}
void BluetoothEmulationHandler::SimulatePreconnectedPeripheral(
const String& in_address,
const String& in_name,
std::unique_ptr<
protocol::Array<protocol::BluetoothEmulation::ManufacturerData>>
in_manufacturer_data,
std::unique_ptr<protocol::Array<String>> in_known_service_uuids,
std::unique_ptr<SimulatePreconnectedPeripheralCallback> callback) {
if (!fake_central_.is_bound()) {
std::move(callback)->sendFailure(
Response::ServerError("BluetoothEmulation not enabled"));
return;
}
fake_central_->SimulatePreconnectedPeripheral(
in_address, in_name, ToManufacturerData(in_manufacturer_data.get()),
ToBluetoothUUIDs(in_known_service_uuids.get()),
base::BindOnce(&SimulatePreconnectedPeripheralCallback::sendSuccess,
std::move(callback)));
}
void BluetoothEmulationHandler::SimulateAdvertisement(
std::unique_ptr<protocol::BluetoothEmulation::ScanEntry> in_entry,
std::unique_ptr<SimulateAdvertisementCallback> callback) {
if (!fake_central_.is_bound()) {
std::move(callback)->sendFailure(
Response::ServerError("BluetoothEmulation not enabled"));
return;
}
bluetooth::mojom::ScanResultPtr payload = bluetooth::mojom::ScanResult::New(
in_entry->GetDeviceAddress(), in_entry->GetRssi(),
ToScanRecord(in_entry->GetScanRecord()));
fake_central_->SimulateAdvertisementReceived(
std::move(payload),
base::BindOnce(&SimulateAdvertisementCallback::sendSuccess,
std::move(callback)));
}
} // namespace content::protocol