OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/pairing/bluetooth_controller_pairing_controller.h" | 5 #include "components/pairing/bluetooth_controller_pairing_controller.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "components/pairing/bluetooth_pairing_constants.h" | 12 #include "components/pairing/bluetooth_pairing_constants.h" |
13 #include "components/pairing/pairing_api.pb.h" | 13 #include "components/pairing/pairing_api.pb.h" |
14 #include "device/bluetooth/bluetooth_adapter_factory.h" | 14 #include "device/bluetooth/bluetooth_adapter_factory.h" |
15 #include "device/bluetooth/bluetooth_discovery_session.h" | 15 #include "device/bluetooth/bluetooth_discovery_session.h" |
16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 const int kReceiveSize = 16384; | 19 const int kReceiveSize = 16384; |
20 } | 20 } |
21 | 21 |
22 namespace pairing_chromeos { | 22 namespace pairing_chromeos { |
23 | 23 |
24 BluetoothControllerPairingController::BluetoothControllerPairingController() | 24 BluetoothControllerPairingController::BluetoothControllerPairingController() |
25 : current_stage_(STAGE_NONE), | 25 : current_stage_(STAGE_NONE), |
26 got_initial_status_(false), | |
27 proto_decoder_(new ProtoDecoder(this)), | 26 proto_decoder_(new ProtoDecoder(this)), |
28 ptr_factory_(this) { | 27 ptr_factory_(this) { |
29 } | 28 } |
30 | 29 |
31 BluetoothControllerPairingController::~BluetoothControllerPairingController() { | 30 BluetoothControllerPairingController::~BluetoothControllerPairingController() { |
32 Reset(); | 31 Reset(); |
33 } | 32 } |
34 | 33 |
35 device::BluetoothDevice* BluetoothControllerPairingController::GetController() { | 34 device::BluetoothDevice* BluetoothControllerPairingController::GetController() { |
36 DCHECK(!controller_device_id_.empty()); | 35 DCHECK(!controller_device_id_.empty()); |
37 device::BluetoothDevice* device = adapter_->GetDevice(controller_device_id_); | 36 device::BluetoothDevice* device = adapter_->GetDevice(controller_device_id_); |
38 if (!device) { | 37 if (!device) { |
39 LOG(ERROR) << "Lost connection to controller."; | 38 LOG(ERROR) << "Lost connection to controller."; |
40 ChangeStage(STAGE_ESTABLISHING_CONNECTION_ERROR); | 39 ChangeStage(STAGE_ESTABLISHING_CONNECTION_ERROR); |
41 } | 40 } |
42 | 41 |
43 return device; | 42 return device; |
44 } | 43 } |
45 | 44 |
46 void BluetoothControllerPairingController::ChangeStage(Stage new_stage) { | 45 void BluetoothControllerPairingController::ChangeStage(Stage new_stage) { |
47 if (current_stage_ == new_stage) | 46 if (current_stage_ == new_stage) |
48 return; | 47 return; |
49 VLOG(1) << "ChangeStage " << new_stage; | 48 VLOG(1) << "ChangeStage " << new_stage; |
50 current_stage_ = new_stage; | 49 current_stage_ = new_stage; |
51 FOR_EACH_OBSERVER(ControllerPairingController::Observer, observers_, | 50 FOR_EACH_OBSERVER(ControllerPairingController::Observer, observers_, |
52 PairingStageChanged(new_stage)); | 51 PairingStageChanged(new_stage)); |
53 } | 52 } |
54 | 53 |
55 void BluetoothControllerPairingController::Reset() { | 54 void BluetoothControllerPairingController::Reset() { |
56 got_initial_status_ = false; | |
57 controller_device_id_.clear(); | 55 controller_device_id_.clear(); |
58 discovery_session_.reset(); | 56 discovery_session_.reset(); |
59 | 57 |
60 if (socket_.get()) { | 58 if (socket_.get()) { |
61 socket_->Close(); | 59 socket_->Close(); |
62 socket_ = NULL; | 60 socket_ = NULL; |
63 } | 61 } |
64 | 62 |
65 if (adapter_.get()) { | 63 if (adapter_.get()) { |
66 adapter_->RemoveObserver(this); | 64 adapter_->RemoveObserver(this); |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 ChangeStage(STAGE_HOST_ENROLLMENT_IN_PROGRESS); | 347 ChangeStage(STAGE_HOST_ENROLLMENT_IN_PROGRESS); |
350 } | 348 } |
351 | 349 |
352 void BluetoothControllerPairingController::StartSession() { | 350 void BluetoothControllerPairingController::StartSession() { |
353 DCHECK_EQ(current_stage_, STAGE_PAIRING_DONE); | 351 DCHECK_EQ(current_stage_, STAGE_PAIRING_DONE); |
354 ChangeStage(STAGE_FINISHED); | 352 ChangeStage(STAGE_FINISHED); |
355 } | 353 } |
356 | 354 |
357 void BluetoothControllerPairingController::OnHostStatusMessage( | 355 void BluetoothControllerPairingController::OnHostStatusMessage( |
358 const pairing_api::HostStatus& message) { | 356 const pairing_api::HostStatus& message) { |
359 if (got_initial_status_) { | 357 pairing_api::HostStatusParameters::UpdateStatus update_status = |
360 // TODO(zork): Check that the domain matches. (http://crbug.com/405761) | 358 message.parameters().update_status(); |
361 // TODO(zork): Handling updating stages (http://crbug.com/405754). | 359 pairing_api::HostStatusParameters::EnrollmentStatus enrollment_status = |
362 pairing_api::CompleteSetup complete_setup; | 360 message.parameters().enrollment_status(); |
363 complete_setup.set_api_version(kPairingAPIVersion); | 361 VLOG(1) << "OnHostStatusMessage, update_status=" << update_status; |
364 // TODO(zork): Get AddAnother from UI (http://crbug.com/405757) | 362 // TODO(zork): Check domain. (http://crbug.com/405761) |
365 complete_setup.mutable_parameters()->set_add_another(false); | 363 if (enrollment_status == |
366 | 364 pairing_api::HostStatusParameters::ENROLLMENT_STATUS_SUCCESS) { |
367 int size = 0; | 365 // TODO(achuith, zork): Need to ensure that controller has also successfully |
368 scoped_refptr<net::IOBuffer> io_buffer( | 366 // enrolled. |
369 ProtoDecoder::SendCompleteSetup(complete_setup, &size)); | 367 CompleteSetup(); |
370 | 368 } else if (update_status == |
371 SendBuffer(io_buffer, size); | 369 pairing_api::HostStatusParameters::UPDATE_STATUS_UPDATING) { |
372 ChangeStage(STAGE_PAIRING_DONE); | 370 ChangeStage(STAGE_HOST_UPDATE_IN_PROGRESS); |
373 } else { | 371 } else if (update_status == |
374 got_initial_status_ = true; | 372 pairing_api::HostStatusParameters::UPDATE_STATUS_UPDATED) { |
375 | |
376 // TODO(zork): Check domain. (http://crbug.com/405761) | |
377 // TODO(achuith): Need STAGE_HOST_UPDATE_IN_PROGRESS here. | |
378 ChangeStage(STAGE_WAITING_FOR_CREDENTIALS); | 373 ChangeStage(STAGE_WAITING_FOR_CREDENTIALS); |
379 } | 374 } |
380 } | 375 } |
381 | 376 |
| 377 void BluetoothControllerPairingController::CompleteSetup() { |
| 378 pairing_api::CompleteSetup complete_setup; |
| 379 complete_setup.set_api_version(kPairingAPIVersion); |
| 380 // TODO(zork): Get AddAnother from UI (http://crbug.com/405757) |
| 381 complete_setup.mutable_parameters()->set_add_another(false); |
| 382 |
| 383 int size = 0; |
| 384 scoped_refptr<net::IOBuffer> io_buffer( |
| 385 ProtoDecoder::SendCompleteSetup(complete_setup, &size)); |
| 386 |
| 387 SendBuffer(io_buffer, size); |
| 388 ChangeStage(STAGE_PAIRING_DONE); |
| 389 } |
| 390 |
382 void BluetoothControllerPairingController::OnConfigureHostMessage( | 391 void BluetoothControllerPairingController::OnConfigureHostMessage( |
383 const pairing_api::ConfigureHost& message) { | 392 const pairing_api::ConfigureHost& message) { |
384 NOTREACHED(); | 393 NOTREACHED(); |
385 } | 394 } |
386 | 395 |
387 void BluetoothControllerPairingController::OnPairDevicesMessage( | 396 void BluetoothControllerPairingController::OnPairDevicesMessage( |
388 const pairing_api::PairDevices& message) { | 397 const pairing_api::PairDevices& message) { |
389 NOTREACHED(); | 398 NOTREACHED(); |
390 } | 399 } |
391 | 400 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); | 464 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); |
456 } | 465 } |
457 | 466 |
458 void BluetoothControllerPairingController::AuthorizePairing( | 467 void BluetoothControllerPairingController::AuthorizePairing( |
459 device::BluetoothDevice* device) { | 468 device::BluetoothDevice* device) { |
460 // Disallow unknown device. | 469 // Disallow unknown device. |
461 device->RejectPairing(); | 470 device->RejectPairing(); |
462 } | 471 } |
463 | 472 |
464 } // namespace pairing_chromeos | 473 } // namespace pairing_chromeos |
OLD | NEW |