OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_H |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_H |
| 7 |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/time/time.h" |
| 13 #include "components/proximity_auth/ble/fake_wire_message.h" |
| 14 #include "components/proximity_auth/connection.h" |
| 15 #include "device/bluetooth/bluetooth_adapter.h" |
| 16 #include "device/bluetooth/bluetooth_device.h" |
| 17 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
| 18 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
| 19 #include "device/bluetooth/bluetooth_uuid.h" |
| 20 |
| 21 namespace proximity_auth { |
| 22 |
| 23 // Represents a connection with a remote device over Bluetooth low energy. The |
| 24 // connection is a persistent bidirectional channel for sending and receiving |
| 25 // wire messages. The remote device is the peripheral mode and the service |
| 26 // contains two characteristics: one to send data and another to receive it. |
| 27 class BluetoothLowEnergyConnection : public Connection, |
| 28 public device::BluetoothAdapter::Observer { |
| 29 public: |
| 30 enum class ControlSignal : uint32 { |
| 31 kInviteToConnectSignal = 0, |
| 32 kInvitationResponseSignal = 1, |
| 33 kSendSignal = 2, |
| 34 kDisconnectSignal = 3, |
| 35 }; |
| 36 |
| 37 // Constructs a Bluetooth low energy connection to the service with |
| 38 // |remote_service_| on the |remote_device|. The GATT connection with |
| 39 // |remote_device| must be already established and |adapter| already |
| 40 // initialized. |
| 41 // |
| 42 // The connection flow is described below. |
| 43 // |
| 44 // Discover Reader Characteristic Discover Writer Characteristic |
| 45 // | | |
| 46 // Start Notify Session | |
| 47 // | | |
| 48 // ---------------- && --------------------- |
| 49 // | |
| 50 // | |
| 51 // | |
| 52 // Write kInviteToConnectSignal to Writer Characteristic |
| 53 // | |
| 54 // | |
| 55 // | |
| 56 // Read kInvitationResponseSignal from Reader Characteristic |
| 57 // | |
| 58 // | |
| 59 // | |
| 60 // Proximity Auth Connection Established |
| 61 BluetoothLowEnergyConnection( |
| 62 const RemoteDevice& remote_device, |
| 63 scoped_refptr<device::BluetoothAdapter> adapter, |
| 64 const device::BluetoothUUID remote_service_uuid, |
| 65 const device::BluetoothUUID to_peripheral_char_uuid, |
| 66 const device::BluetoothUUID from_peripheral_char_uuid, |
| 67 scoped_ptr<device::BluetoothGattConnection> gatt_connection); |
| 68 |
| 69 ~BluetoothLowEnergyConnection() override; |
| 70 |
| 71 // proximity_auth::Connection |
| 72 void Connect() override; |
| 73 void Disconnect() override; |
| 74 |
| 75 protected: |
| 76 // proximity_auth::Connection |
| 77 void SendMessageImpl(scoped_ptr<WireMessage> message) override; |
| 78 scoped_ptr<WireMessage> DeserializeWireMessage( |
| 79 bool* is_incomplete_message) override; |
| 80 |
| 81 // device::BluetoothAdapter::Observer |
| 82 void DeviceRemoved(device::BluetoothAdapter* adapter, |
| 83 device::BluetoothDevice* device) override; |
| 84 void GattDiscoveryCompleteForService( |
| 85 device::BluetoothAdapter* adapter, |
| 86 device::BluetoothGattService* service) override; |
| 87 void GattCharacteristicAdded( |
| 88 device::BluetoothAdapter* adapter, |
| 89 device::BluetoothGattCharacteristic* characteristic) override; |
| 90 void GattCharacteristicValueChanged( |
| 91 device::BluetoothAdapter* adapter, |
| 92 device::BluetoothGattCharacteristic* characteristic, |
| 93 const std::vector<uint8>& value) override; |
| 94 |
| 95 private: |
| 96 // Represents a remote characteristic or service. |
| 97 struct RemoteBleObject { |
| 98 device::BluetoothUUID uuid; |
| 99 std::string id; |
| 100 }; |
| 101 |
| 102 // Sends an invite to connect signal to the peripheral if the connection |
| 103 // is ready. That is, satisfying the conditions: |
| 104 // (i) |to_peripheral_char_| and |from_peripheral_char_| were found; |
| 105 // (ii) |notify_session_| was set for |from_peripheral_char_|. |
| 106 // |
| 107 // The asynchronous events that can casue these conditions to be |
| 108 // satisfied are: |
| 109 // (i) a new characteristic is discovered (HandleCharacteristicUpdate); |
| 110 // (ii) a new notify session is started (OnNotifySessionStarted). |
| 111 void SendInviteToConnectSignal(); |
| 112 |
| 113 // Called when there is an error writing to the remote characteristic |
| 114 // |to_peripheral_char_|. |
| 115 void OnWriteRemoteCharacteristicError( |
| 116 device::BluetoothGattService::GattErrorCode error); |
| 117 |
| 118 // Handles the discovery of a new characteristic. |
| 119 void HandleCharacteristicUpdate( |
| 120 device::BluetoothGattCharacteristic* characteristic); |
| 121 |
| 122 // Scans the remote chracteristics of |service| calling |
| 123 // HandleCharacteristicUpdate() for each of them. |
| 124 void ScanRemoteCharacteristics(device::BluetoothGattService* service); |
| 125 |
| 126 // This function verifies if the connection is complete and updates |
| 127 // the status accordingly. The only asynchronous event |
| 128 // that can cause the connection to be completed is receiving a |
| 129 // kInvitationResponseSignal (GattCharateristcValueChanged). |
| 130 void CompleteConnection(); |
| 131 |
| 132 // Starts a notify session for |from_peripheral_char_|. |
| 133 void StartNotifySession(); |
| 134 |
| 135 // Called when there is an error starting a notification session for |
| 136 // |from_peripheral_char_| characteristic. |
| 137 void OnNotifySessionError(device::BluetoothGattService::GattErrorCode); |
| 138 |
| 139 // Called when a notification session is successfully started for |
| 140 // |from_peripheral_char_| characteristic. |
| 141 void OnNotifySessionStarted( |
| 142 scoped_ptr<device::BluetoothGattNotifySession> notify_session); |
| 143 |
| 144 // Stops |notify_session_|. |
| 145 void StopNotifySession(); |
| 146 |
| 147 // Updates the value of |to_peripheral_char_| and |
| 148 // |from_peripheral_char_| |
| 149 // when |characteristic| was found. |
| 150 void UpdateCharacteristicsStatus( |
| 151 device::BluetoothGattCharacteristic* characteristic); |
| 152 |
| 153 // Returns the Bluetooth address of the remote device. |
| 154 const std::string& GetRemoteDeviceAddress(); |
| 155 |
| 156 // Returns the device corresponding to |remote_device_address_|. |
| 157 device::BluetoothDevice* GetRemoteDevice(); |
| 158 |
| 159 // Returns the service corresponding to |remote_service_| in the current |
| 160 // device. |
| 161 device::BluetoothGattService* GetRemoteService(); |
| 162 |
| 163 // Returns the characteristic corresponding to |identifier| in the current |
| 164 // service. |
| 165 device::BluetoothGattCharacteristic* GetGattCharacteristic( |
| 166 const std::string& identifier); |
| 167 |
| 168 // Convert the first 4 bytes from a byte vector to a uint32. |
| 169 uint32 ToUint32(const std::vector<uint8>& bytes); |
| 170 |
| 171 // Convert an uint32 to a byte array. |
| 172 const std::string ToString(const uint32 value); |
| 173 |
| 174 // The Bluetooth adapter over which the Bluetooth connection will be made. |
| 175 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 176 |
| 177 // Remote service the |connection_| was established with. |
| 178 RemoteBleObject remote_service_; |
| 179 |
| 180 // Characteristic used to send data to the remote device. |
| 181 RemoteBleObject to_peripheral_char_; |
| 182 |
| 183 // Characteristic used to receive data from the remote device. |
| 184 RemoteBleObject from_peripheral_char_; |
| 185 |
| 186 // The GATT connection with the remote device. |
| 187 scoped_ptr<device::BluetoothGattConnection> connection_; |
| 188 |
| 189 // Indicates if there is a pending notification session. Used to ensure there |
| 190 // is only one pending notify session. |
| 191 bool notify_session_pending_; |
| 192 |
| 193 // The notify session for |from_peripheral_char|. |
| 194 scoped_ptr<device::BluetoothGattNotifySession> notify_session_; |
| 195 |
| 196 // Indicates if there pending response to a invite to connect signal. |
| 197 bool connect_signal_response_pending_; |
| 198 |
| 199 // Stores when the instace was created. |
| 200 base::TimeTicks start_time_; |
| 201 |
| 202 base::WeakPtrFactory<BluetoothLowEnergyConnection> weak_ptr_factory_; |
| 203 |
| 204 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnection); |
| 205 }; |
| 206 |
| 207 } // namespace proximity_auth |
| 208 |
| 209 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_H |
OLD | NEW |