Chromium Code Reviews| 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 "components/proximity_auth/ble/fake_wire_message.h" | |
| 13 #include "components/proximity_auth/connection.h" | |
| 14 #include "device/bluetooth/bluetooth_adapter.h" | |
| 15 #include "device/bluetooth/bluetooth_device.h" | |
| 16 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | |
| 17 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | |
| 18 #include "device/bluetooth/bluetooth_uuid.h" | |
| 19 | |
| 20 namespace proximity_auth { | |
| 21 | |
| 22 // Represents a connection with a remote device over Bluetooth low energy. The | |
| 23 // connection is a persistent bidirectional channel for sending and receiving | |
| 24 // wire messages. The remote device is the peripheral mode and the service | |
| 25 // contains two characteristics: one to send data and another to receive it. | |
| 26 class BluetoothLowEnergyConnection : public Connection, | |
| 27 public device::BluetoothAdapter::Observer { | |
| 28 public: | |
| 29 enum ControlSignal : uint32 { | |
|
Tim Song
2015/05/11 08:02:39
nit: use an enum class instead.
sacomoto
2015/05/11 12:04:04
Done.
| |
| 30 kInviteToConnectSignal = 0, | |
| 31 kInvitationResponseSignal = 1, | |
| 32 kSendSignal = 2, | |
| 33 kDisconnectSignal = 3, | |
| 34 }; | |
| 35 | |
| 36 // Constructs a Bluetooth low energy connection to the service with | |
| 37 // |remote_service_uuid| on the |remote_device|. The GATT connection with | |
| 38 // |remote_device| must be already established and |adapter| already | |
|
Tim Song
2015/05/11 08:02:39
Can you document the flow for establishing the log
sacomoto
2015/05/11 12:04:04
Done.
| |
| 39 // initialized. | |
| 40 BluetoothLowEnergyConnection( | |
| 41 const RemoteDevice& remote_device, | |
| 42 scoped_refptr<device::BluetoothAdapter> adapter, | |
| 43 device::BluetoothUUID remote_service_uuid, | |
| 44 const std::string& to_peripheral_char_uuid, | |
| 45 const std::string& from_peripheral_char_uuid, | |
| 46 scoped_ptr<device::BluetoothGattConnection> gatt_connection); | |
| 47 | |
| 48 ~BluetoothLowEnergyConnection(); | |
| 49 | |
| 50 // proximity_auth::Connection | |
| 51 void Connect() override; | |
| 52 void Disconnect() override; | |
| 53 | |
| 54 protected: | |
| 55 // proximity_auth::Connection | |
| 56 void SendMessageImpl(scoped_ptr<WireMessage> message) override; | |
| 57 scoped_ptr<WireMessage> DeserializeWireMessage( | |
| 58 bool* is_incomplete_message) override; | |
| 59 | |
| 60 // device::BluetoothAdaptor::Observer | |
| 61 void DeviceRemoved(device::BluetoothAdapter* adapter, | |
| 62 device::BluetoothDevice* device) override; | |
| 63 void GattDiscoveryCompleteForService( | |
| 64 device::BluetoothAdapter* adapter, | |
| 65 device::BluetoothGattService* service) override; | |
| 66 void GattCharacteristicAdded( | |
| 67 device::BluetoothAdapter* adapter, | |
| 68 device::BluetoothGattCharacteristic* characteristic) override; | |
| 69 void GattCharacteristicValueChanged( | |
| 70 device::BluetoothAdapter* adapter, | |
| 71 device::BluetoothGattCharacteristic* characteristic, | |
| 72 const std::vector<uint8>& value) override; | |
| 73 | |
| 74 private: | |
| 75 // Sends an invite to connect signal to the peripheral if the connection | |
| 76 // is ready. That is, satisfying the conditions: | |
| 77 // (i) |to_peripheral_char_uuid_| and |from_peripheral_char_uuid_| were found; | |
| 78 // (ii) |notify_session_| was set for |from_peripheral_char_uuid_|. | |
| 79 // | |
| 80 // The asynchronous events that can casue these conditions to be | |
| 81 // satisfied are: | |
| 82 // (i) a new characteristic is discovered (HandleCharacteristicUpdate); | |
| 83 // (ii) a new notify session is started (OnNotifySessionStarted). | |
| 84 void SendInviteToConnectSignal(); | |
| 85 | |
| 86 // Called when there is an error writing to the remote characteristic | |
| 87 // |to_peripheral_char_uuid_|. | |
| 88 void OnWriteRemoteCharacteristicError( | |
| 89 device::BluetoothGattService::GattErrorCode error); | |
| 90 | |
| 91 // Handles the discovery of a new characteristic. | |
| 92 void HandleCharacteristicUpdate( | |
| 93 device::BluetoothGattCharacteristic* characteristic); | |
| 94 | |
| 95 // The connection is complete when: | |
| 96 // (i) |to_peripheral_char_uuid_| and |from_peripheral_char_uuid_| were found; | |
| 97 // (ii) |notify_session_| was set for |from_peripheral_char_uuid_|; | |
|
Tim Song
2015/05/11 08:02:39
I would also add that you write an initial hello s
sacomoto
2015/05/11 12:04:04
Done.
| |
| 98 // (iii) kInvitationResponseSignal was received. | |
| 99 // | |
| 100 // The last condition necessariy happens after the first two. | |
| 101 // This function verifies if the connection is complete and updates | |
| 102 // the status accordingly. The only asynchronous event | |
| 103 // that can cause the connection to be completed is receiving a | |
| 104 // kInvitationResponseSignal (GattCharateristcValueChanged). | |
| 105 void CompleteConnection(); | |
| 106 | |
| 107 // Starts a notify session for |from_peripheral_char_uuid_|. | |
| 108 void StartNotifySession(); | |
| 109 | |
| 110 // Called when there is an error starting a notification session for | |
| 111 // |from_peripheral_char_uuid_| characteristic. | |
| 112 void OnNotifySessionError(device::BluetoothGattService::GattErrorCode); | |
| 113 | |
| 114 // Called when a notification session is successfully started for | |
| 115 // |from_peripheral_char_uuid_| characteristic. | |
| 116 void OnNotifySessionStarted( | |
| 117 scoped_ptr<device::BluetoothGattNotifySession> notify_session); | |
| 118 | |
| 119 // Stops |notify_session_|. | |
| 120 void StopNotifySession(); | |
| 121 | |
| 122 // Updates the value of |to_peripheral_char_id_| and | |
| 123 // |from_peripheral_char_id_| | |
| 124 // when |characteristic| was found. | |
| 125 void UpdateCharacteristicsStatus( | |
| 126 device::BluetoothGattCharacteristic* characteristic); | |
| 127 | |
| 128 // Returns the Bluetooth address of the remote device. | |
| 129 const std::string& GetRemoteDeviceAddress(); | |
| 130 | |
| 131 // Returns the device corresponding to |remote_device_address_|. | |
| 132 device::BluetoothDevice* GetRemoteDevice(); | |
| 133 | |
| 134 // Returns the service corresponding to |remote_service_uuid_| in the current | |
| 135 // device. | |
| 136 device::BluetoothGattService* GetRemoteService(); | |
| 137 | |
| 138 // Returns the characteristic corresponding to |identifier| in the current | |
| 139 // service. | |
| 140 device::BluetoothGattCharacteristic* GetGattCharacteristic( | |
| 141 const std::string& identifier); | |
| 142 | |
| 143 // Extract the signal (first 4 bytes) from a byte vector. | |
| 144 uint32 ExtractUint32(const std::vector<uint8>& bytes); | |
| 145 | |
| 146 // The Bluetooth adapter over which the Bluetooth connection will be made. | |
| 147 scoped_refptr<device::BluetoothAdapter> adapter_; | |
| 148 | |
| 149 // The uuid of the service it looks for to establish a GattConnection. | |
| 150 const device::BluetoothUUID remote_service_uuid_; | |
|
Tim Song
2015/05/11 08:02:39
nit: Can you create a private struct that encapsul
sacomoto
2015/05/11 12:04:04
Done.
| |
| 151 | |
| 152 // The identifier of the service corresponding to |remote_service_uuid_|. | |
| 153 std::string remote_service_id_; | |
| 154 | |
| 155 // Characteristic used to send data to the remote device. | |
| 156 const std::string to_peripheral_char_uuid_; | |
| 157 | |
| 158 // Internal unique identifier for |to_peripheral_char_|. | |
| 159 std::string to_peripheral_char_id_; | |
| 160 | |
| 161 // Characteristic used to receive data from the remote device. | |
| 162 const std::string from_peripheral_char_uuid_; | |
| 163 | |
| 164 // Internal unique identifier for |from_peripheral_char_|. | |
| 165 std::string from_peripheral_char_id_; | |
| 166 | |
| 167 // The GATT connection with the remote device. | |
| 168 scoped_ptr<device::BluetoothGattConnection> connection_; | |
| 169 | |
| 170 // Indicates if there is a pending notification session. Used to ensure there | |
| 171 // is only one pending notify session. | |
| 172 bool notify_session_pending_; | |
|
Tim Song
2015/05/11 08:02:39
Instead of having a separate bool for each state t
sacomoto
2015/05/11 12:04:04
We could to this way, but I don't think it would m
Tim Song
2015/05/12 03:02:46
Thanks, I understand the flow a lot better now. Id
sacomoto
2015/05/12 07:23:19
Ok.
| |
| 173 | |
| 174 // The notify session for |from_peripheral_char|. | |
| 175 scoped_ptr<device::BluetoothGattNotifySession> notify_session_; | |
| 176 | |
| 177 // Indicates if there pending response to a invite to connect signal. | |
| 178 bool connect_signal_response_pending_; | |
| 179 | |
| 180 base::WeakPtrFactory<BluetoothLowEnergyConnection> weak_ptr_factory_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnection); | |
| 183 }; | |
| 184 | |
| 185 } // namespace proximity_auth | |
| 186 | |
| 187 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_H | |
| OLD | NEW |