| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_SERVER_H_ |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_SERVER_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
| 11 #include <map> |
| 12 #include <memory> |
| 13 #include <vector> |
| 14 |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/task_runner.h" |
| 17 #include "components/proximity_auth/ble/bluetooth_low_energy_advertisement_rotat
or.h" |
| 18 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_channel.h" |
| 19 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_defines.h" |
| 20 #include "components/proximity_auth/connection.h" |
| 21 #include "components/proximity_auth/remote_device.h" |
| 22 #include "device/bluetooth/bluetooth_adapter.h" |
| 23 #include "device/bluetooth/bluetooth_device.h" |
| 24 #include "device/bluetooth/bluetooth_local_gatt_characteristic.h" |
| 25 #include "device/bluetooth/bluetooth_local_gatt_descriptor.h" |
| 26 #include "device/bluetooth/bluetooth_local_gatt_service.h" |
| 27 |
| 28 namespace base { |
| 29 class TaskRunner; |
| 30 } |
| 31 |
| 32 namespace proximity_auth { |
| 33 namespace weave { |
| 34 namespace { |
| 35 |
| 36 using BluetoothAdapter = device::BluetoothAdapter; |
| 37 using BluetoothAdvertisement = device::BluetoothAdvertisement; |
| 38 using BluetoothDevice = device::BluetoothDevice; |
| 39 using BluetoothLocalGattCharacteristic = |
| 40 device::BluetoothLocalGattCharacteristic; |
| 41 using BluetoothLocalGattService = device::BluetoothLocalGattService; |
| 42 using BluetoothLocalGattDescriptor = device::BluetoothLocalGattDescriptor; |
| 43 using ValueCallback = base::Callback<void(const std::vector<uint8_t>&)>; |
| 44 using ErrorCallback = base::Closure; |
| 45 using ConnectionStatus = proximity_auth::Connection::Status; |
| 46 |
| 47 } // namespace |
| 48 |
| 49 // Represent a server for device's connection requests. |
| 50 // Listens for requests from registered devices on the RX and TX |
| 51 // characteristics. |
| 52 // A registered device is either being advertised to or connected with the |
| 53 // server. |
| 54 // If a device is being advertised to, the advertisement could either be |
| 55 // registered on the adapter or waiting on the queue in advertisement rotator. |
| 56 // The device must be in the ad_rotator_ but its corresponding advertisement |
| 57 // may or may not be advertising at one moment. |
| 58 class BluetoothLowEnergyWeaveServer |
| 59 : public device::BluetoothLocalGattService::Delegate, |
| 60 public BluetoothLowEnergyWeaveChannel::Observer { |
| 61 public: |
| 62 class Observer { |
| 63 public: |
| 64 virtual void OnConnectionEstablished( |
| 65 std::shared_ptr<BluetoothLowEnergyWeaveServerConnection> |
| 66 connection) = 0; |
| 67 |
| 68 virtual void OnConnectionClosed() = 0; |
| 69 |
| 70 virtual void OnDeviceAdvertised(RemoteDevice device) = 0; |
| 71 }; |
| 72 |
| 73 class Factory { |
| 74 public: |
| 75 static std::unique_ptr<BluetoothLowEnergyWeaveServer> NewInstance(); |
| 76 |
| 77 // Exposed for testing. |
| 78 static void SetInstanceForTesting(Factory* factory); |
| 79 |
| 80 protected: |
| 81 // Exposed for testing. |
| 82 virtual std::unique_ptr<BluetoothLowEnergyWeaveServer> BuildInstance(); |
| 83 |
| 84 private: |
| 85 static Factory* factory_instance_; |
| 86 }; |
| 87 |
| 88 // TODO(jingxuy): The observer for this class. |
| 89 |
| 90 // device::BluetoothLocalGattService::Delegate: |
| 91 void OnCharacteristicReadRequest( |
| 92 const BluetoothDevice* bluetooth_device, |
| 93 const BluetoothLocalGattCharacteristic* characteristic, |
| 94 int offset, |
| 95 const ValueCallback& callback, |
| 96 const ErrorCallback& error_callback) override; |
| 97 |
| 98 // If the write request is from a known device and the TX characteristic, |
| 99 // then forward |value| to the appropriate ServerConnection of that device. |
| 100 void OnCharacteristicWriteRequest( |
| 101 const BluetoothDevice* bluetooth_device, |
| 102 const BluetoothLocalGattCharacteristic* characteristic, |
| 103 const Packet& value, |
| 104 int offset, |
| 105 const base::Closure& callback, |
| 106 const ErrorCallback& error_callback) override; |
| 107 |
| 108 void OnDescriptorReadRequest(const BluetoothDevice* device, |
| 109 const BluetoothLocalGattDescriptor* descriptor, |
| 110 int offset, |
| 111 const ValueCallback& callback, |
| 112 const ErrorCallback& error_callback) override {} |
| 113 |
| 114 void OnDescriptorWriteRequest(const BluetoothDevice* device, |
| 115 const BluetoothLocalGattDescriptor* descriptor, |
| 116 const std::vector<uint8_t>& value, |
| 117 int offset, |
| 118 const base::Closure& callback, |
| 119 const ErrorCallback& error_callback) override {} |
| 120 |
| 121 void OnNotificationsStart( |
| 122 const BluetoothDevice* bluetooth_device, |
| 123 const BluetoothLocalGattCharacteristic* characteristic) override; |
| 124 |
| 125 void OnNotificationsStop( |
| 126 const BluetoothDevice* bluetooth_device, |
| 127 const BluetoothLocalGattCharacteristic* characteristic) override; |
| 128 |
| 129 // BluetoothLowEnergyWeaveChannel::Observer |
| 130 void OnChannelAuthenticated(std::string device_id, |
| 131 const BluetoothDevice* bluetooth_device) override; |
| 132 |
| 133 void OnChannelDisconnected(std::string device_id, |
| 134 const BluetoothDevice* bluetooth_device) override; |
| 135 |
| 136 ~BluetoothLowEnergyWeaveServer(); |
| 137 |
| 138 void RegisterObserver(std::shared_ptr<Observer> observer); |
| 139 |
| 140 void UnregisterObserver(std::shared_ptr<Observer> observer); |
| 141 |
| 142 // |remote_device| must not share a user_id with any device already |
| 143 // registered. |
| 144 // Registration effectively puts the device in advertisement queue. |
| 145 void RegisterDevice(const RemoteDevice& remote_device); |
| 146 |
| 147 // |remote_device| must already be registered. |
| 148 // If the device is advertising, the advertisement will be taken down. |
| 149 // If the device is connected, it will be disconnected immediately. |
| 150 void UnregisterDevice(const RemoteDevice& remote_device); |
| 151 |
| 152 bool HasDevice(std::string device_address); |
| 153 |
| 154 protected: |
| 155 BluetoothLowEnergyWeaveServer(); |
| 156 |
| 157 // Sets |task_runner_| for testing. |
| 158 void SetTaskRunnerForTesting(scoped_refptr<base::TaskRunner> task_runner); |
| 159 |
| 160 private: |
| 161 // |remote_device| must already be registered and not advertisement enabled. |
| 162 void EnableAdvertising(std::string device_id); |
| 163 |
| 164 // |remote_device| must already be registered and advertisement enabled. |
| 165 void DisableAdvertising(std::string device_id); |
| 166 |
| 167 bool IsAdvertising(std::string bluetooth_address); |
| 168 bool HasChannel(const BluetoothDevice* bluetooth_device); |
| 169 bool IsConnected(const BluetoothDevice* bluetooth_device); |
| 170 |
| 171 // Rotating function for giving other devices a fair share of the advertising |
| 172 // time slot. Will post itself on the task runner upon completion to run in |
| 173 // 5 seconds. |
| 174 void RotateAdvertisement(); |
| 175 |
| 176 // Helper to attempt to register an advertisement. |
| 177 void Advertise(); |
| 178 |
| 179 void UnregisterAdvertisement(std::string bluetooth_address); |
| 180 |
| 181 // Remove the advertisement from map_to_advertisement_ because it's no longer |
| 182 // advertising. |
| 183 void UnregisterAdvertisementSuccess(std::string bluetooth_address); |
| 184 |
| 185 // This should not happen. |
| 186 void UnregisterAdvertisementError(std::string bluetooth_address, |
| 187 BluetoothAdvertisement::ErrorCode error); |
| 188 |
| 189 // If it's a success, then rotate the ad_rotator_ to go to the next |
| 190 // advertisement and attempt to register that one. |
| 191 void RegisterAdvertisementSuccess( |
| 192 std::string bluetooth_address, |
| 193 scoped_refptr<BluetoothAdvertisement> advertisement); |
| 194 |
| 195 // Stop the rolling advertisement process by doing nothing. |
| 196 // TODO(jingxuy): do I need to log anything? |
| 197 void RegisterAdvertisementError(BluetoothAdvertisement::ErrorCode error) {} |
| 198 |
| 199 // TODO(jingxuy): currently this is an empty function, unsure whether if |
| 200 // anything needed to be done when the adapter is created. |
| 201 void CreateAdapterCallback() {} |
| 202 |
| 203 scoped_refptr<base::TaskRunner> task_runner_; |
| 204 |
| 205 std::vector<std::shared_ptr<Observer>> observers_; |
| 206 |
| 207 // All registered devices included here. |
| 208 std::map<std::string, RemoteDevice> map_to_remote_device_; |
| 209 |
| 210 std::unique_ptr<BluetoothLowEnergyAdvertisementRotator> ad_rotator_; |
| 211 |
| 212 // The currently registered advertisements. |
| 213 std::map<std::string, scoped_refptr<BluetoothAdvertisement>> |
| 214 map_to_advertisement_; |
| 215 |
| 216 // The currently active channels. |
| 217 std::map<const BluetoothDevice*, |
| 218 std::unique_ptr<BluetoothLowEnergyWeaveChannel>> |
| 219 map_to_channel_; |
| 220 |
| 221 scoped_refptr<BluetoothAdapter> adapter_; |
| 222 |
| 223 base::WeakPtr<BluetoothLocalGattService> service_; |
| 224 |
| 225 base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic_; |
| 226 base::WeakPtr<BluetoothLocalGattCharacteristic> tx_characteristic_; |
| 227 |
| 228 base::WeakPtrFactory<BluetoothLowEnergyWeaveServer> weak_ptr_factory_; |
| 229 }; |
| 230 |
| 231 } // namespace weave |
| 232 |
| 233 } // namespace proximity_auth |
| 234 |
| 235 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_SERVER_H_ |
| OLD | NEW |