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. 4 5 #ifndef CHROMEOS_PAIRING_BLUETOOTH_HOST_PAIRING_CONTROLLER_H_ 6 #define CHROMEOS_PAIRING_BLUETOOTH_HOST_PAIRING_CONTROLLER_H_ 7 8 #include "base/macros.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/weak_ptr.h" 11 #include "base/observer_list.h" 12 #include "base/threading/thread_checker.h" 13 #include "components/pairing/host_pairing_controller.h" 14 #include "components/pairing/proto_decoder.h" 15 #include "device/bluetooth/bluetooth_adapter.h" 16 #include "device/bluetooth/bluetooth_device.h" 17 #include "device/bluetooth/bluetooth_socket.h" 18 19 namespace device { 20 class BluetoothAdapter; 21 } 22 23 namespace net { 24 class IOBuffer; 25 } 26 27 namespace pairing_chromeos { 28 29 class BluetoothHostPairingController 30 : public HostPairingController, 31 public ProtoDecoder::Observer, 32 public device::BluetoothAdapter::Observer, 33 public device::BluetoothDevice::PairingDelegate { 34 public: 35 typedef HostPairingController::Observer Observer; 36 37 BluetoothHostPairingController(); 38 virtual ~BluetoothHostPairingController(); 39 40 private: 41 void ChangeStage(Stage new_stage); 42 void SendHostStatus(); 43 void AbortWithError(int code, const std::string& message); 44 void Reset(); 45 46 void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter); 47 void SetName(); 48 void OnSetName(); 49 void OnSetPowered(); 50 void OnCreateService(scoped_refptr<device::BluetoothSocket> socket); 51 void OnSetDiscoverable(bool change_stage); 52 void OnAccept(const device::BluetoothDevice* device, 53 scoped_refptr<device::BluetoothSocket> socket); 54 void OnSendComplete(int bytes_sent); 55 void OnReceiveComplete(int bytes, scoped_refptr<net::IOBuffer> io_buffer); 56 57 void OnCreateServiceError(const std::string& message); 58 void OnSetError(); 59 void OnAcceptError(const std::string& error_message); 60 void OnSendError(const std::string& error_message); 61 void OnReceiveError(device::BluetoothSocket::ErrorReason reason, 62 const std::string& error_message); 63 64 // HostPairingController: 65 virtual void AddObserver(Observer* observer) OVERRIDE; 66 virtual void RemoveObserver(Observer* observer) OVERRIDE; 67 virtual Stage GetCurrentStage() OVERRIDE; 68 virtual void StartPairing() OVERRIDE; 69 virtual std::string GetDeviceName() OVERRIDE; 70 virtual std::string GetConfirmationCode() OVERRIDE; 71 virtual std::string GetEnrollmentDomain() OVERRIDE; 72 virtual void OnUpdateStatusChanged(UpdateStatus update_status) OVERRIDE; 73 virtual void SetEnrollmentComplete(bool success) OVERRIDE; 74 75 // ProtoDecoder::Observer: 76 virtual void OnHostStatusMessage( 77 const pairing_api::HostStatus& message) OVERRIDE; 78 virtual void OnConfigureHostMessage( 79 const pairing_api::ConfigureHost& message) OVERRIDE; 80 virtual void OnPairDevicesMessage( 81 const pairing_api::PairDevices& message) OVERRIDE; 82 virtual void OnCompleteSetupMessage( 83 const pairing_api::CompleteSetup& message) OVERRIDE; 84 virtual void OnErrorMessage(const pairing_api::Error& message) OVERRIDE; 85 86 // BluetoothAdapter::Observer: 87 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter, 88 bool present) OVERRIDE; 89 90 // device::BluetoothDevice::PairingDelegate: 91 virtual void RequestPinCode(device::BluetoothDevice* device) OVERRIDE; 92 virtual void RequestPasskey(device::BluetoothDevice* device) OVERRIDE; 93 virtual void DisplayPinCode(device::BluetoothDevice* device, 94 const std::string& pincode) OVERRIDE; 95 virtual void DisplayPasskey(device::BluetoothDevice* device, 96 uint32 passkey) OVERRIDE; 97 virtual void KeysEntered(device::BluetoothDevice* device, 98 uint32 entered) OVERRIDE; 99 virtual void ConfirmPasskey(device::BluetoothDevice* device, 100 uint32 passkey) OVERRIDE; 101 virtual void AuthorizePairing(device::BluetoothDevice* device) OVERRIDE; 102 103 Stage current_stage_; 104 std::string device_name_; 105 std::string confirmation_code_; 106 std::string enrollment_domain_; 107 108 scoped_refptr<device::BluetoothAdapter> adapter_; 109 device::BluetoothDevice* device_; 110 scoped_refptr<device::BluetoothSocket> service_socket_; 111 scoped_refptr<device::BluetoothSocket> controller_socket_; 112 scoped_ptr<ProtoDecoder> proto_decoder_; 113 114 base::ThreadChecker thread_checker_; 115 ObserverList<Observer> observers_; 116 base::WeakPtrFactory<BluetoothHostPairingController> ptr_factory_; 117 118 DISALLOW_COPY_AND_ASSIGN(BluetoothHostPairingController); 119 }; 120 121 } // namespace pairing_chromeos 122 123 #endif // CHROMEOS_PAIRING_BLUETOOTH_HOST_PAIRING_CONTROLLER_H_ 124