Chromium Code Reviews| 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_defines.h" | |
| 19 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_server_connec tion.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 typedef device::BluetoothAdapter BluetoothAdapter; | |
| 37 typedef device::BluetoothAdvertisement BluetoothAdvertisement; | |
| 38 typedef device::BluetoothDevice BluetoothDevice; | |
| 39 typedef device::BluetoothLocalGattCharacteristic | |
| 40 BluetoothLocalGattCharacteristic; | |
| 41 typedef device::BluetoothLocalGattService BluetoothLocalGattService; | |
| 42 typedef device::BluetoothLocalGattDescriptor BluetoothLocalGattDescriptor; | |
| 43 typedef base::Callback<void(const std::vector<uint8_t>&)> ValueCallback; | |
| 44 typedef base::Closure ErrorCallback; | |
| 45 typedef proximity_auth::Connection::Status ConnectionStatus; | |
| 46 typedef BluetoothLowEnergyAdvertisementRotator::Advertisement Advertisement; | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 class BluetoothLowEnergyWeaveServerConnection; | |
| 51 | |
| 52 class BluetoothLowEnergyWeaveServer | |
|
rkc
2016/07/27 00:56:34
Class comment? Method comments?
jingxuy
2016/07/27 23:31:48
Done.
| |
| 53 : public device::BluetoothLocalGattService::Delegate { | |
| 54 public: | |
| 55 class Factory { | |
| 56 public: | |
| 57 static std::unique_ptr<BluetoothLowEnergyWeaveServer> NewInstance(); | |
| 58 | |
| 59 // Exposed for testing. | |
| 60 static void SetInstanceForTesting(Factory* factory); | |
| 61 | |
| 62 protected: | |
| 63 // Exposed for testing. | |
| 64 virtual std::unique_ptr<BluetoothLowEnergyWeaveServer> BuildInstance(); | |
| 65 | |
| 66 private: | |
| 67 static Factory* factory_instance_; | |
| 68 }; | |
| 69 | |
| 70 // device::BluetoothLocalGattService::Delegate: | |
| 71 void OnCharacteristicReadRequest( | |
| 72 const BluetoothDevice* bluetooth_device, | |
| 73 const BluetoothLocalGattCharacteristic* characteristic, | |
| 74 int offset, | |
| 75 const ValueCallback& callback, | |
| 76 const ErrorCallback& error_callback) override; | |
| 77 | |
| 78 void OnCharacteristicWriteRequest( | |
| 79 const BluetoothDevice* bluetooth_device, | |
| 80 const BluetoothLocalGattCharacteristic* characteristic, | |
| 81 const Packet& value, | |
| 82 int offset, | |
| 83 const base::Closure& callback, | |
| 84 const ErrorCallback& error_callback) override; | |
| 85 | |
| 86 void OnDescriptorReadRequest(const BluetoothDevice* device, | |
| 87 const BluetoothLocalGattDescriptor* descriptor, | |
| 88 int offset, | |
| 89 const ValueCallback& callback, | |
| 90 const ErrorCallback& error_callback) override{}; | |
| 91 | |
| 92 void OnDescriptorWriteRequest(const BluetoothDevice* device, | |
| 93 const BluetoothLocalGattDescriptor* descriptor, | |
| 94 const std::vector<uint8_t>& value, | |
| 95 int offset, | |
| 96 const base::Closure& callback, | |
| 97 const ErrorCallback& error_callback) override{}; | |
| 98 | |
| 99 void OnNotificationsStart( | |
| 100 const BluetoothDevice* bluetooth_device, | |
| 101 const BluetoothLocalGattCharacteristic* characteristic) override; | |
| 102 | |
| 103 void OnNotificationsStop( | |
| 104 const BluetoothDevice* bluetooth_device, | |
| 105 const BluetoothLocalGattCharacteristic* characteristic) override; | |
| 106 | |
| 107 ~BluetoothLowEnergyWeaveServer(); | |
| 108 | |
| 109 // Duplicate registeration has no effect. | |
| 110 void RegisterDevice(const RemoteDevice& remote_device); | |
| 111 | |
| 112 void UnregisterDevice(const RemoteDevice& remote_device); | |
| 113 | |
| 114 bool HasDevice(std::string device_address); | |
| 115 | |
| 116 const RemoteDevice& GetRemoteDevice(const BluetoothDevice* bluetooth_device); | |
| 117 | |
| 118 void StartAdvertising(const RemoteDevice& remote_device); | |
| 119 | |
| 120 void StopAdvertising(const RemoteDevice& remote_device); | |
| 121 | |
| 122 protected: | |
| 123 BluetoothLowEnergyWeaveServer(); | |
| 124 | |
| 125 // Sets |task_runner_| for testing. | |
| 126 void SetTaskRunnerForTesting(scoped_refptr<base::TaskRunner> task_runner); | |
| 127 | |
| 128 private: | |
| 129 void UpdateEidSeeds(); | |
| 130 void UpdateEids(); | |
| 131 void RefreshAdvertisement(); | |
| 132 | |
| 133 void RotateAdvertisement(); | |
| 134 void Advertise(); | |
| 135 | |
| 136 void CreateAdapterCallback(); | |
| 137 | |
| 138 void UnregisterAdvertisement(std::string bluetooth_address); | |
| 139 void UnregisterAdvertisementSuccess(std::string bluetooth_address); | |
| 140 void UnregisterAdvertisementError(std::string bluetooth_address, | |
| 141 BluetoothAdvertisement::ErrorCode error); | |
| 142 | |
| 143 void RegisterAdvertisementSuccess( | |
| 144 std::string bluetooth_address, | |
| 145 scoped_refptr<BluetoothAdvertisement> advertisement); | |
| 146 void RegisterAdvertisementError(BluetoothAdvertisement::ErrorCode error); | |
| 147 | |
| 148 scoped_refptr<base::TaskRunner> task_runner_; | |
| 149 | |
| 150 std::map<std::string, RemoteDevice> map_to_remote_device_; | |
| 151 | |
| 152 std::unique_ptr<BluetoothLowEnergyAdvertisementRotator> ad_rotator_; | |
| 153 | |
| 154 std::map<std::string, scoped_refptr<BluetoothAdvertisement>> | |
| 155 map_to_advertisement_; | |
| 156 | |
| 157 std::map<std::string, | |
| 158 std::unique_ptr<BluetoothLowEnergyWeaveServerConnection>> | |
| 159 map_to_server_connection_; | |
| 160 | |
| 161 scoped_refptr<BluetoothAdapter> adapter_; | |
| 162 | |
| 163 base::WeakPtr<BluetoothLocalGattService> service_; | |
| 164 | |
| 165 base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic_; | |
| 166 base::WeakPtr<BluetoothLocalGattCharacteristic> tx_characteristic_; | |
| 167 | |
| 168 base::WeakPtrFactory<BluetoothLowEnergyWeaveServer> weak_ptr_factory_; | |
| 169 }; | |
| 170 | |
| 171 } // namespace weave | |
| 172 | |
| 173 } // namespace proximity_auth | |
| 174 | |
| 175 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_SERVER_H_ | |
| OLD | NEW |