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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/bluetooth/bluetooth_pairing_bluez.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_device_bluez.h"
using device::BluetoothDevice;
namespace {
// Histogram enumerations for pairing methods.
enum UMAPairingMethod {
UMA_PAIRING_METHOD_NONE,
UMA_PAIRING_METHOD_REQUEST_PINCODE,
UMA_PAIRING_METHOD_REQUEST_PASSKEY,
UMA_PAIRING_METHOD_DISPLAY_PINCODE,
UMA_PAIRING_METHOD_DISPLAY_PASSKEY,
UMA_PAIRING_METHOD_CONFIRM_PASSKEY,
// NOTE: Add new pairing methods immediately above this line. Make sure to
// update the enum list in tools/histogram/histograms.xml accordingly.
UMA_PAIRING_METHOD_COUNT
};
// Number of keys that will be entered for a passkey, six digits plus the
// final enter.
const uint16 kPasskeyMaxKeysEntered = 7;
} // namespace
namespace bluez {
BluetoothPairingBlueZ::BluetoothPairingBlueZ(
BluetoothDeviceBlueZ* device,
BluetoothDevice::PairingDelegate* pairing_delegate)
: device_(device),
pairing_delegate_(pairing_delegate),
pairing_delegate_used_(false) {
VLOG(1) << "Created BluetoothPairingBlueZ for " << device_->GetAddress();
}
BluetoothPairingBlueZ::~BluetoothPairingBlueZ() {
VLOG(1) << "Destroying BluetoothPairingBlueZ for " << device_->GetAddress();
if (!pairing_delegate_used_) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_NONE,
UMA_PAIRING_METHOD_COUNT);
}
if (!pincode_callback_.is_null()) {
pincode_callback_.Run(
bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED, "");
}
if (!passkey_callback_.is_null()) {
passkey_callback_.Run(
bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED, 0);
}
if (!confirmation_callback_.is_null()) {
confirmation_callback_.Run(
bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED);
}
pairing_delegate_ = NULL;
}
void BluetoothPairingBlueZ::RequestPinCode(
const bluez::BluetoothAgentServiceProvider::Delegate::PinCodeCallback&
callback) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_REQUEST_PINCODE,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
pincode_callback_ = callback;
pairing_delegate_used_ = true;
pairing_delegate_->RequestPinCode(device_);
}
bool BluetoothPairingBlueZ::ExpectingPinCode() const {
return !pincode_callback_.is_null();
}
void BluetoothPairingBlueZ::SetPinCode(const std::string& pincode) {
if (pincode_callback_.is_null())
return;
pincode_callback_.Run(bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS,
pincode);
pincode_callback_.Reset();
// If this is not an outgoing connection to the device, clean up the pairing
// context since the pairing is done. The outgoing connection case is cleaned
// up in the callback for the underlying Pair() call.
if (!device_->IsConnecting())
device_->EndPairing();
}
void BluetoothPairingBlueZ::DisplayPinCode(const std::string& pincode) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_DISPLAY_PINCODE,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
pairing_delegate_used_ = true;
pairing_delegate_->DisplayPinCode(device_, pincode);
// If this is not an outgoing connection to the device, the pairing context
// needs to be cleaned up again as there's no reliable indication of
// completion of incoming pairing.
if (!device_->IsConnecting())
device_->EndPairing();
}
void BluetoothPairingBlueZ::RequestPasskey(
const bluez::BluetoothAgentServiceProvider::Delegate::PasskeyCallback&
callback) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_REQUEST_PASSKEY,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
passkey_callback_ = callback;
pairing_delegate_used_ = true;
pairing_delegate_->RequestPasskey(device_);
}
bool BluetoothPairingBlueZ::ExpectingPasskey() const {
return !passkey_callback_.is_null();
}
void BluetoothPairingBlueZ::SetPasskey(uint32 passkey) {
if (passkey_callback_.is_null())
return;
passkey_callback_.Run(bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS,
passkey);
passkey_callback_.Reset();
// If this is not an outgoing connection to the device, clean up the pairing
// context since the pairing is done. The outgoing connection case is cleaned
// up in the callback for the underlying Pair() call.
if (!device_->IsConnecting())
device_->EndPairing();
}
void BluetoothPairingBlueZ::DisplayPasskey(uint32 passkey) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_DISPLAY_PASSKEY,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
pairing_delegate_used_ = true;
pairing_delegate_->DisplayPasskey(device_, passkey);
}
void BluetoothPairingBlueZ::KeysEntered(uint16 entered) {
pairing_delegate_used_ = true;
pairing_delegate_->KeysEntered(device_, entered);
// If this is not an outgoing connection to the device, the pairing context
// needs to be cleaned up again as there's no reliable indication of
// completion of incoming pairing.
if (entered >= kPasskeyMaxKeysEntered && !device_->IsConnecting())
device_->EndPairing();
}
void BluetoothPairingBlueZ::RequestConfirmation(
uint32 passkey,
const bluez::BluetoothAgentServiceProvider::Delegate::ConfirmationCallback&
callback) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_CONFIRM_PASSKEY,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
confirmation_callback_ = callback;
pairing_delegate_used_ = true;
pairing_delegate_->ConfirmPasskey(device_, passkey);
}
void BluetoothPairingBlueZ::RequestAuthorization(
const bluez::BluetoothAgentServiceProvider::Delegate::ConfirmationCallback&
callback) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", UMA_PAIRING_METHOD_NONE,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
confirmation_callback_ = callback;
pairing_delegate_used_ = true;
pairing_delegate_->AuthorizePairing(device_);
}
bool BluetoothPairingBlueZ::ExpectingConfirmation() const {
return !confirmation_callback_.is_null();
}
void BluetoothPairingBlueZ::ConfirmPairing() {
if (confirmation_callback_.is_null())
return;
confirmation_callback_.Run(
bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS);
confirmation_callback_.Reset();
// If this is not an outgoing connection to the device, clean up the pairing
// context since the pairing is done. The outgoing connection case is cleaned
// up in the callback for the underlying Pair() call.
if (!device_->IsConnecting())
device_->EndPairing();
}
bool BluetoothPairingBlueZ::RejectPairing() {
return RunPairingCallbacks(
bluez::BluetoothAgentServiceProvider::Delegate::REJECTED);
}
bool BluetoothPairingBlueZ::CancelPairing() {
return RunPairingCallbacks(
bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED);
}
BluetoothDevice::PairingDelegate* BluetoothPairingBlueZ::GetPairingDelegate()
const {
return pairing_delegate_;
}
void BluetoothPairingBlueZ::ResetCallbacks() {
pincode_callback_.Reset();
passkey_callback_.Reset();
confirmation_callback_.Reset();
}
bool BluetoothPairingBlueZ::RunPairingCallbacks(
bluez::BluetoothAgentServiceProvider::Delegate::Status status) {
pairing_delegate_used_ = true;
bool callback_run = false;
if (!pincode_callback_.is_null()) {
pincode_callback_.Run(status, "");
pincode_callback_.Reset();
callback_run = true;
}
if (!passkey_callback_.is_null()) {
passkey_callback_.Run(status, 0);
passkey_callback_.Reset();
callback_run = true;
}
if (!confirmation_callback_.is_null()) {
confirmation_callback_.Run(status);
confirmation_callback_.Reset();
callback_run = true;
}
// If this is not an outgoing connection to the device, clean up the pairing
// context since the pairing is done. The outgoing connection case is cleaned
// up in the callback for the underlying Pair() call.
if (!device_->IsConnecting())
device_->EndPairing();
return callback_run;
}
} // namespace bluez
|