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_CONTROLLER_PAIRING_FLOW_H_
6#define CHROMEOS_PAIRING_BLUETOOTH_CONTROLLER_PAIRING_FLOW_H_
7
8#include <set>
9
10#include "base/macros.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/observer_list.h"
13#include "base/threading/thread_checker.h"
14#include "components/pairing/controller_pairing_controller.h"
15#include "components/pairing/proto_decoder.h"
16#include "device/bluetooth/bluetooth_adapter.h"
17#include "device/bluetooth/bluetooth_device.h"
18#include "device/bluetooth/bluetooth_socket.h"
19
20namespace net {
21class IOBuffer;
22}
23
24namespace pairing_chromeos {
25
26class BluetoothControllerPairingController
27    : public ControllerPairingController,
28      public ProtoDecoder::Observer,
29      public device::BluetoothAdapter::Observer,
30      public device::BluetoothDevice::PairingDelegate  {
31 public:
32  BluetoothControllerPairingController();
33  virtual ~BluetoothControllerPairingController();
34
35 private:
36  void ChangeStage(Stage new_stage);
37  device::BluetoothDevice* GetController();
38  void Reset();
39  void DeviceFound(device::BluetoothDevice* device);
40  void DeviceLost(device::BluetoothDevice* device);
41  void SendBuffer(scoped_refptr<net::IOBuffer> io_buffer, int size);
42
43  void OnSetPowered();
44  void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
45  void OnStartDiscoverySession(
46      scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
47  void OnConnect();
48  void OnConnectToService(scoped_refptr<device::BluetoothSocket> socket);
49  void OnSendComplete(int bytes_sent);
50  void OnReceiveComplete(int bytes, scoped_refptr<net::IOBuffer> io_buffer);
51
52  void OnError();
53  void OnErrorWithMessage(const std::string& message);
54  void OnConnectError(device::BluetoothDevice::ConnectErrorCode error_code);
55  void OnReceiveError(device::BluetoothSocket::ErrorReason reason,
56                      const std::string& error_message);
57
58  // ControllerPairingController:
59  virtual void AddObserver(
60      ControllerPairingController::Observer* observer) OVERRIDE;
61  virtual void RemoveObserver(
62      ControllerPairingController::Observer* observer) OVERRIDE;
63  virtual Stage GetCurrentStage() OVERRIDE;
64  virtual void StartPairing() OVERRIDE;
65  virtual DeviceIdList GetDiscoveredDevices() OVERRIDE;
66  virtual void ChooseDeviceForPairing(const std::string& device_id) OVERRIDE;
67  virtual void RepeatDiscovery() OVERRIDE;
68  virtual std::string GetConfirmationCode() OVERRIDE;
69  virtual void SetConfirmationCodeIsCorrect(bool correct) OVERRIDE;
70  virtual void SetHostConfiguration(
71      bool accepted_eula,
72      const std::string& lang,
73      const std::string& timezone,
74      bool send_reports,
75      const std::string& keyboard_layout) OVERRIDE;
76  virtual void OnAuthenticationDone(const std::string& domain,
77                                    const std::string& auth_token) OVERRIDE;
78  virtual void StartSession() OVERRIDE;
79
80  // ProtoDecoder::Observer:
81  virtual void OnHostStatusMessage(
82      const pairing_api::HostStatus& message) OVERRIDE;
83  virtual void OnConfigureHostMessage(
84      const pairing_api::ConfigureHost& message) OVERRIDE;
85  virtual void OnPairDevicesMessage(
86      const pairing_api::PairDevices& message) OVERRIDE;
87  virtual void OnCompleteSetupMessage(
88      const pairing_api::CompleteSetup& message) OVERRIDE;
89  virtual void OnErrorMessage(const pairing_api::Error& message) OVERRIDE;
90
91  // BluetoothAdapter::Observer:
92  virtual void DeviceAdded(device::BluetoothAdapter* adapter,
93                           device::BluetoothDevice* device) OVERRIDE;
94  virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
95                             device::BluetoothDevice* device) OVERRIDE;
96
97  // device::BluetoothDevice::PairingDelegate:
98  virtual void RequestPinCode(device::BluetoothDevice* device) OVERRIDE;
99  virtual void RequestPasskey(device::BluetoothDevice* device) OVERRIDE;
100  virtual void DisplayPinCode(device::BluetoothDevice* device,
101                              const std::string& pincode) OVERRIDE;
102  virtual void DisplayPasskey(device::BluetoothDevice* device,
103                              uint32 passkey) OVERRIDE;
104  virtual void KeysEntered(device::BluetoothDevice* device,
105                           uint32 entered) OVERRIDE;
106  virtual void ConfirmPasskey(device::BluetoothDevice* device,
107                              uint32 passkey) OVERRIDE;
108  virtual void AuthorizePairing(device::BluetoothDevice* device) OVERRIDE;
109
110  Stage current_stage_;
111  bool got_initial_status_;
112  scoped_refptr<device::BluetoothAdapter> adapter_;
113  scoped_ptr<device::BluetoothDiscoverySession> discovery_session_;
114  scoped_refptr<device::BluetoothSocket> socket_;
115  std::string controller_device_id_;
116
117  std::string confirmation_code_;
118  std::set<std::string> discovered_devices_;
119
120  scoped_ptr<ProtoDecoder> proto_decoder_;
121
122  base::ThreadChecker thread_checker_;
123  ObserverList<ControllerPairingController::Observer> observers_;
124  base::WeakPtrFactory<BluetoothControllerPairingController> ptr_factory_;
125
126  DISALLOW_COPY_AND_ASSIGN(BluetoothControllerPairingController);
127};
128
129}  // namespace pairing_chromeos
130
131#endif  // CHROMEOS_PAIRING_BLUETOOTH_CONTROLLER_PAIRING_FLOW_H_
132