1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/pairing/bluetooth_controller_pairing_controller.h"
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "components/pairing/bluetooth_pairing_constants.h"
13 #include "components/pairing/pairing_api.pb.h"
14 #include "device/bluetooth/bluetooth_adapter_factory.h"
15 #include "device/bluetooth/bluetooth_discovery_session.h"
16 #include "net/base/io_buffer.h"
19 const int kReceiveSize
= 16384;
22 namespace pairing_chromeos
{
24 BluetoothControllerPairingController::BluetoothControllerPairingController()
25 : current_stage_(STAGE_NONE
),
26 got_initial_status_(false),
27 proto_decoder_(new ProtoDecoder(this)),
31 BluetoothControllerPairingController::~BluetoothControllerPairingController() {
35 device::BluetoothDevice
* BluetoothControllerPairingController::GetController() {
36 DCHECK(!controller_device_id_
.empty());
37 device::BluetoothDevice
* device
= adapter_
->GetDevice(controller_device_id_
);
39 LOG(ERROR
) << "Lost connection to controller.";
40 ChangeStage(STAGE_ESTABLISHING_CONNECTION_ERROR
);
46 void BluetoothControllerPairingController::ChangeStage(Stage new_stage
) {
47 if (current_stage_
== new_stage
)
49 current_stage_
= new_stage
;
50 FOR_EACH_OBSERVER(ControllerPairingController::Observer
, observers_
,
51 PairingStageChanged(new_stage
));
54 void BluetoothControllerPairingController::Reset() {
55 got_initial_status_
= false;
56 controller_device_id_
.clear();
57 discovery_session_
.reset();
65 adapter_
->RemoveObserver(this);
70 void BluetoothControllerPairingController::DeviceFound(
71 device::BluetoothDevice
* device
) {
72 DCHECK_EQ(current_stage_
, STAGE_DEVICES_DISCOVERY
);
73 DCHECK(thread_checker_
.CalledOnValidThread());
74 if (StartsWith(device
->GetName(), base::ASCIIToUTF16(kDeviceNamePrefix
),
76 discovered_devices_
.insert(device
->GetAddress());
77 FOR_EACH_OBSERVER(ControllerPairingController::Observer
, observers_
,
78 DiscoveredDevicesListChanged());
82 void BluetoothControllerPairingController::DeviceLost(
83 device::BluetoothDevice
* device
) {
84 DCHECK_EQ(current_stage_
, STAGE_DEVICES_DISCOVERY
);
85 DCHECK(thread_checker_
.CalledOnValidThread());
86 std::set
<std::string
>::iterator ix
=
87 discovered_devices_
.find(device
->GetAddress());
88 if (ix
!= discovered_devices_
.end()) {
89 discovered_devices_
.erase(ix
);
90 FOR_EACH_OBSERVER(ControllerPairingController::Observer
, observers_
,
91 DiscoveredDevicesListChanged());
95 void BluetoothControllerPairingController::SendBuffer(
96 scoped_refptr
<net::IOBuffer
> io_buffer
, int size
) {
99 base::Bind(&BluetoothControllerPairingController::OnSendComplete
,
100 ptr_factory_
.GetWeakPtr()),
101 base::Bind(&BluetoothControllerPairingController::OnErrorWithMessage
,
102 ptr_factory_
.GetWeakPtr()));
105 void BluetoothControllerPairingController::OnSetPowered() {
106 DCHECK(thread_checker_
.CalledOnValidThread());
107 adapter_
->StartDiscoverySession(
108 base::Bind(&BluetoothControllerPairingController::OnStartDiscoverySession
,
109 ptr_factory_
.GetWeakPtr()),
110 base::Bind(&BluetoothControllerPairingController::OnError
,
111 ptr_factory_
.GetWeakPtr()));
114 void BluetoothControllerPairingController::OnGetAdapter(
115 scoped_refptr
<device::BluetoothAdapter
> adapter
) {
116 DCHECK(thread_checker_
.CalledOnValidThread());
117 DCHECK(!adapter_
.get());
119 adapter_
->AddObserver(this);
121 if (adapter_
->IsPowered()) {
124 adapter_
->SetPowered(
126 base::Bind(&BluetoothControllerPairingController::OnSetPowered
,
127 ptr_factory_
.GetWeakPtr()),
128 base::Bind(&BluetoothControllerPairingController::OnError
,
129 ptr_factory_
.GetWeakPtr()));
133 void BluetoothControllerPairingController::OnStartDiscoverySession(
134 scoped_ptr
<device::BluetoothDiscoverySession
> discovery_session
) {
135 DCHECK(thread_checker_
.CalledOnValidThread());
136 discovery_session_
= discovery_session
.Pass();
137 ChangeStage(STAGE_DEVICES_DISCOVERY
);
139 device::BluetoothAdapter::DeviceList device_list
= adapter_
->GetDevices();
140 for (device::BluetoothAdapter::DeviceList::iterator ix
= device_list
.begin();
141 ix
!= device_list
.end(); ++ix
) {
146 void BluetoothControllerPairingController::OnConnect() {
147 DCHECK(thread_checker_
.CalledOnValidThread());
148 device::BluetoothDevice
* device
= GetController();
150 device
->ConnectToService(
151 device::BluetoothUUID(kPairingServiceUUID
),
152 base::Bind(&BluetoothControllerPairingController::OnConnectToService
,
153 ptr_factory_
.GetWeakPtr()),
154 base::Bind(&BluetoothControllerPairingController::OnErrorWithMessage
,
155 ptr_factory_
.GetWeakPtr()));
159 void BluetoothControllerPairingController::OnConnectToService(
160 scoped_refptr
<device::BluetoothSocket
> socket
) {
161 DCHECK(thread_checker_
.CalledOnValidThread());
166 base::Bind(&BluetoothControllerPairingController::OnReceiveComplete
,
167 ptr_factory_
.GetWeakPtr()),
168 base::Bind(&BluetoothControllerPairingController::OnReceiveError
,
169 ptr_factory_
.GetWeakPtr()));
172 void BluetoothControllerPairingController::OnSendComplete(int bytes_sent
) {}
174 void BluetoothControllerPairingController::OnReceiveComplete(
175 int bytes
, scoped_refptr
<net::IOBuffer
> io_buffer
) {
176 DCHECK(thread_checker_
.CalledOnValidThread());
177 proto_decoder_
->DecodeIOBuffer(bytes
, io_buffer
);
181 base::Bind(&BluetoothControllerPairingController::OnReceiveComplete
,
182 ptr_factory_
.GetWeakPtr()),
183 base::Bind(&BluetoothControllerPairingController::OnReceiveError
,
184 ptr_factory_
.GetWeakPtr()));
187 void BluetoothControllerPairingController::OnError() {
188 LOG(ERROR
) << "Pairing initialization failed";
189 ChangeStage(STAGE_INITIALIZATION_ERROR
);
193 void BluetoothControllerPairingController::OnErrorWithMessage(
194 const std::string
& message
) {
195 LOG(ERROR
) << message
;
196 ChangeStage(STAGE_INITIALIZATION_ERROR
);
200 void BluetoothControllerPairingController::OnConnectError(
201 device::BluetoothDevice::ConnectErrorCode error_code
) {
202 DCHECK(thread_checker_
.CalledOnValidThread());
203 device::BluetoothDevice
* device
= GetController();
205 if (device
&& device
->IsPaired()) {
206 // The connection attempt is only used to start the pairing between the
207 // devices. If the connection fails, it's not a problem as long as pairing
213 void BluetoothControllerPairingController::OnReceiveError(
214 device::BluetoothSocket::ErrorReason reason
,
215 const std::string
& error_message
) {
216 LOG(ERROR
) << reason
<< ", " << error_message
;
220 void BluetoothControllerPairingController::AddObserver(
221 ControllerPairingController::Observer
* observer
) {
222 observers_
.AddObserver(observer
);
225 void BluetoothControllerPairingController::RemoveObserver(
226 ControllerPairingController::Observer
* observer
) {
227 observers_
.RemoveObserver(observer
);
230 ControllerPairingController::Stage
231 BluetoothControllerPairingController::GetCurrentStage() {
232 return current_stage_
;
235 void BluetoothControllerPairingController::StartPairing() {
236 DCHECK(thread_checker_
.CalledOnValidThread());
237 DCHECK(current_stage_
== STAGE_NONE
||
238 current_stage_
== STAGE_DEVICE_NOT_FOUND
||
239 current_stage_
== STAGE_ESTABLISHING_CONNECTION_ERROR
||
240 current_stage_
== STAGE_HOST_ENROLLMENT_ERROR
);
241 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
242 ChangeStage(STAGE_INITIALIZATION_ERROR
);
246 device::BluetoothAdapterFactory::GetAdapter(
247 base::Bind(&BluetoothControllerPairingController::OnGetAdapter
,
248 ptr_factory_
.GetWeakPtr()));
252 ControllerPairingController::DeviceIdList
253 BluetoothControllerPairingController::GetDiscoveredDevices() {
254 DCHECK_EQ(current_stage_
, STAGE_DEVICES_DISCOVERY
);
255 return DeviceIdList(discovered_devices_
.begin(), discovered_devices_
.end());
258 void BluetoothControllerPairingController::ChooseDeviceForPairing(
259 const std::string
& device_id
) {
260 DCHECK_EQ(current_stage_
, STAGE_DEVICES_DISCOVERY
);
261 DCHECK(discovered_devices_
.count(device_id
));
262 discovery_session_
.reset();
263 controller_device_id_
= device_id
;
265 device::BluetoothDevice
* device
= GetController();
268 ChangeStage(STAGE_ESTABLISHING_CONNECTION
);
269 if (device
->IsPaired()) {
274 base::Bind(&BluetoothControllerPairingController::OnConnect
,
275 ptr_factory_
.GetWeakPtr()),
276 base::Bind(&BluetoothControllerPairingController::OnConnectError
,
277 ptr_factory_
.GetWeakPtr()));
282 void BluetoothControllerPairingController::RepeatDiscovery() {
283 DCHECK(current_stage_
== STAGE_DEVICE_NOT_FOUND
||
284 current_stage_
== STAGE_ESTABLISHING_CONNECTION_ERROR
||
285 current_stage_
== STAGE_HOST_ENROLLMENT_ERROR
);
290 std::string
BluetoothControllerPairingController::GetConfirmationCode() {
291 DCHECK_EQ(current_stage_
, STAGE_WAITING_FOR_CODE_CONFIRMATION
);
292 DCHECK(!confirmation_code_
.empty());
293 return confirmation_code_
;
296 void BluetoothControllerPairingController::SetConfirmationCodeIsCorrect(
298 DCHECK_EQ(current_stage_
, STAGE_WAITING_FOR_CODE_CONFIRMATION
);
300 device::BluetoothDevice
* device
= GetController();
305 device
->ConfirmPairing();
306 // Once pairing is confirmed, the connection will either be successful, or
307 // fail. Either case is acceptable as long as the devices are paired.
309 device
->RejectPairing();
310 controller_device_id_
.clear();
315 void BluetoothControllerPairingController::SetHostConfiguration(
317 const std::string
& lang
,
318 const std::string
& timezone
,
320 const std::string
& keyboard_layout
) {
322 pairing_api::ConfigureHost host_config
;
323 host_config
.set_api_version(kPairingAPIVersion
);
324 host_config
.mutable_parameters()->set_accepted_eula(accepted_eula
);
325 host_config
.mutable_parameters()->set_lang(lang
);
326 host_config
.mutable_parameters()->set_timezone(timezone
);
327 host_config
.mutable_parameters()->set_send_reports(send_reports
);
328 host_config
.mutable_parameters()->set_keyboard_layout(keyboard_layout
);
331 scoped_refptr
<net::IOBuffer
> io_buffer(
332 ProtoDecoder::SendConfigureHost(host_config
, &size
));
334 SendBuffer(io_buffer
, size
);
337 void BluetoothControllerPairingController::OnAuthenticationDone(
338 const std::string
& domain
,
339 const std::string
& auth_token
) {
340 DCHECK_EQ(current_stage_
, STAGE_WAITING_FOR_CREDENTIALS
);
342 pairing_api::PairDevices pair_devices
;
343 pair_devices
.set_api_version(kPairingAPIVersion
);
344 pair_devices
.mutable_parameters()->set_admin_access_token(auth_token
);
347 scoped_refptr
<net::IOBuffer
> io_buffer(
348 ProtoDecoder::SendPairDevices(pair_devices
, &size
));
350 SendBuffer(io_buffer
, size
);
351 ChangeStage(STAGE_HOST_ENROLLMENT_IN_PROGRESS
);
354 void BluetoothControllerPairingController::StartSession() {
355 DCHECK_EQ(current_stage_
, STAGE_PAIRING_DONE
);
356 ChangeStage(STAGE_FINISHED
);
359 // ProtoDecoder::Observer:
360 void BluetoothControllerPairingController::OnHostStatusMessage(
361 const pairing_api::HostStatus
& message
) {
362 if (got_initial_status_
) {
363 // TODO(zork): Check that the domain matches. (http://crbug.com/405761)
364 // TODO(zork): Handling updating stages (http://crbug.com/405754).
365 pairing_api::CompleteSetup complete_setup
;
366 complete_setup
.set_api_version(kPairingAPIVersion
);
367 // TODO(zork): Get AddAnother from UI (http://crbug.com/405757)
368 complete_setup
.mutable_parameters()->set_add_another(false);
371 scoped_refptr
<net::IOBuffer
> io_buffer(
372 ProtoDecoder::SendCompleteSetup(complete_setup
, &size
));
374 SendBuffer(io_buffer
, size
);
375 ChangeStage(STAGE_PAIRING_DONE
);
377 got_initial_status_
= true;
379 // TODO(zork): Check domain. (http://crbug.com/405761)
380 ChangeStage(STAGE_WAITING_FOR_CREDENTIALS
);
384 void BluetoothControllerPairingController::OnConfigureHostMessage(
385 const pairing_api::ConfigureHost
& message
) {
389 void BluetoothControllerPairingController::OnPairDevicesMessage(
390 const pairing_api::PairDevices
& message
) {
394 void BluetoothControllerPairingController::OnCompleteSetupMessage(
395 const pairing_api::CompleteSetup
& message
) {
399 void BluetoothControllerPairingController::OnErrorMessage(
400 const pairing_api::Error
& message
) {
401 LOG(ERROR
) << message
.parameters().code() << ", " <<
402 message
.parameters().description();
403 ChangeStage(STAGE_HOST_ENROLLMENT_ERROR
);
406 void BluetoothControllerPairingController::DeviceAdded(
407 device::BluetoothAdapter
* adapter
,
408 device::BluetoothDevice
* device
) {
409 DCHECK_EQ(adapter
, adapter_
.get());
413 void BluetoothControllerPairingController::DeviceRemoved(
414 device::BluetoothAdapter
* adapter
,
415 device::BluetoothDevice
* device
) {
416 DCHECK_EQ(adapter
, adapter_
.get());
420 void BluetoothControllerPairingController::RequestPinCode(
421 device::BluetoothDevice
* device
) {
422 // Disallow unknown device.
423 device
->RejectPairing();
426 void BluetoothControllerPairingController::RequestPasskey(
427 device::BluetoothDevice
* device
) {
428 // Disallow unknown device.
429 device
->RejectPairing();
432 void BluetoothControllerPairingController::DisplayPinCode(
433 device::BluetoothDevice
* device
,
434 const std::string
& pincode
) {
435 // Disallow unknown device.
436 device
->RejectPairing();
439 void BluetoothControllerPairingController::DisplayPasskey(
440 device::BluetoothDevice
* device
,
442 // Disallow unknown device.
443 device
->RejectPairing();
446 void BluetoothControllerPairingController::KeysEntered(
447 device::BluetoothDevice
* device
,
449 // Disallow unknown device.
450 device
->RejectPairing();
453 void BluetoothControllerPairingController::ConfirmPasskey(
454 device::BluetoothDevice
* device
,
456 confirmation_code_
= base::StringPrintf("%06d", passkey
);
457 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION
);
460 void BluetoothControllerPairingController::AuthorizePairing(
461 device::BluetoothDevice
* device
) {
462 // Disallow unknown device.
463 device
->RejectPairing();
466 } // namespace pairing_chromeos