Chromium Code Reviews| Index: components/proximity_auth/ble/bluetooth_low_energy_weave_server.h |
| diff --git a/components/proximity_auth/ble/bluetooth_low_energy_weave_server.h b/components/proximity_auth/ble/bluetooth_low_energy_weave_server.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c47b626d61240e7563a0feeced3a44dc29795183 |
| --- /dev/null |
| +++ b/components/proximity_auth/ble/bluetooth_low_energy_weave_server.h |
| @@ -0,0 +1,227 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_SERVER_H_ |
| +#define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_SERVER_H_ |
| + |
| +#include <stddef.h> |
| +#include <stdint.h> |
| + |
| +#include <map> |
| +#include <memory> |
| +#include <vector> |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "base/task_runner.h" |
| +#include "components/proximity_auth/ble/bluetooth_low_energy_advertisement_rotator.h" |
| +#include "components/proximity_auth/ble/bluetooth_low_energy_weave_defines.h" |
| +#include "components/proximity_auth/ble/bluetooth_low_energy_weave_server_connection.h" |
| +#include "components/proximity_auth/connection.h" |
| +#include "components/proximity_auth/remote_device.h" |
| +#include "device/bluetooth/bluetooth_adapter.h" |
| +#include "device/bluetooth/bluetooth_device.h" |
| +#include "device/bluetooth/bluetooth_local_gatt_characteristic.h" |
| +#include "device/bluetooth/bluetooth_local_gatt_descriptor.h" |
| +#include "device/bluetooth/bluetooth_local_gatt_service.h" |
| + |
| +namespace base { |
| +class TaskRunner; |
| +} |
| + |
| +namespace proximity_auth { |
| +namespace weave { |
| +namespace { |
| + |
| +using BluetoothAdapter = device::BluetoothAdapter; |
| +using BluetoothAdvertisement = device::BluetoothAdvertisement; |
| +using BluetoothDevice = device::BluetoothDevice; |
| +using BluetoothLocalGattCharacteristic = |
| + device::BluetoothLocalGattCharacteristic; |
| +using BluetoothLocalGattService = device::BluetoothLocalGattService; |
| +using BluetoothLocalGattDescriptor = device::BluetoothLocalGattDescriptor; |
| +using ValueCallback = base::Callback<void(const std::vector<uint8_t>&)>; |
| +using ErrorCallback = base::Closure; |
| +using ConnectionStatus = proximity_auth::Connection::Status; |
| + |
| +} // namespace |
| + |
| +class BluetoothLowEnergyWeaveServerConnection; |
| + |
| +// Represent a server for device's connection requests. |
| +// Listens for requests from registered devices on the RX and TX |
| +// characteristics. |
| +// A registered device is either being advertised to or connected with the |
| +// server. |
| +// If a device is being advertised to, the advertisement could either be |
| +// registered on the adapter or waiting on the queue in advertisement rotator. |
| +// The device must be in the ad_rotator_ but its corresponding advertisement |
| +// may or may not be advertising at one moment. |
| +class BluetoothLowEnergyWeaveServer |
| + : public device::BluetoothLocalGattService::Delegate { |
| + public: |
| + class Factory { |
| + public: |
| + static std::unique_ptr<BluetoothLowEnergyWeaveServer> NewInstance(); |
| + |
| + // Exposed for testing. |
| + static void SetInstanceForTesting(Factory* factory); |
| + |
| + protected: |
| + // Exposed for testing. |
| + virtual std::unique_ptr<BluetoothLowEnergyWeaveServer> BuildInstance(); |
| + |
| + private: |
| + static Factory* factory_instance_; |
| + }; |
| + |
| + // TODO(jingxuy): The observer for this class. |
| + |
| + // device::BluetoothLocalGattService::Delegate: |
| + void OnCharacteristicReadRequest( |
| + const BluetoothDevice* bluetooth_device, |
| + const BluetoothLocalGattCharacteristic* characteristic, |
| + int offset, |
| + const ValueCallback& callback, |
| + const ErrorCallback& error_callback) override; |
| + |
| + // If the write request is from a known device and the TX characteristic, |
| + // then forward |value| to the appropriate ServerConnection of that device. |
| + void OnCharacteristicWriteRequest( |
| + const BluetoothDevice* bluetooth_device, |
| + const BluetoothLocalGattCharacteristic* characteristic, |
| + const Packet& value, |
| + int offset, |
| + const base::Closure& callback, |
| + const ErrorCallback& error_callback) override; |
| + |
| + void OnDescriptorReadRequest(const BluetoothDevice* device, |
| + const BluetoothLocalGattDescriptor* descriptor, |
| + int offset, |
| + const ValueCallback& callback, |
| + const ErrorCallback& error_callback) override {} |
| + |
| + void OnDescriptorWriteRequest(const BluetoothDevice* device, |
| + const BluetoothLocalGattDescriptor* descriptor, |
| + const std::vector<uint8_t>& value, |
| + int offset, |
| + const base::Closure& callback, |
| + const ErrorCallback& error_callback) override {} |
| + |
| + void OnNotificationsStart( |
| + const BluetoothDevice* bluetooth_device, |
| + const BluetoothLocalGattCharacteristic* characteristic) override; |
| + |
| + void OnNotificationsStop( |
| + const BluetoothDevice* bluetooth_device, |
| + const BluetoothLocalGattCharacteristic* characteristic) override; |
| + |
| + ~BluetoothLowEnergyWeaveServer(); |
| + |
| + // |remote_device| must not share a bluetooth_address with any device already |
| + // registered. |
| + // Registration effectively puts the device in advertisement queue. |
| + void RegisterDevice(const RemoteDevice& remote_device); |
| + |
| + // |remote_device| must already be registered. |
| + // If the device is advertising, the advertisement will be taken down. |
| + // If the device is connected, it will be disconnected immediately. |
| + void UnregisterDevice(const RemoteDevice& remote_device); |
| + |
| + bool HasDevice(std::string device_address); |
|
Kyle Horimoto
2016/07/29 03:15:39
rkc@: Is this a reasonable function to use? I thou
jingxuy
2016/07/29 18:45:43
I had the same question sort of. But from the exte
Kyle Horimoto
2016/07/29 19:04:18
In that case, it is the Bluetooth MAC address from
jingxuy
2016/07/29 20:26:01
hmmmm, I think this actually post a pretty large p
Kyle Horimoto
2016/07/29 21:07:46
Interesting - I thought that the RemoteDevice obje
Kyle Horimoto
2016/07/29 21:11:34
An approach we've used on other platforms is just
|
| + |
| + const RemoteDevice& GetRemoteDevice(const BluetoothDevice* bluetooth_device); |
| + |
| + // |remote_device| must already be registered and not advertisement enabled. |
| + void EnableAdvertising(const RemoteDevice& remote_device); |
|
Kyle Horimoto
2016/07/29 03:15:39
This class should rotate advertisements between th
jingxuy
2016/07/29 18:45:43
I put it here so that the serverConnection can sta
Kyle Horimoto
2016/07/29 19:04:18
The server connection should not know anything abo
jingxuy
2016/07/29 20:26:01
so you mean this class should be an observer of th
Kyle Horimoto
2016/07/29 21:07:45
Yeah, but I think it would be better to create a w
|
| + |
| + // |remote_device| must already be registered and advertisement enabled. |
| + void DisableAdvertising(const RemoteDevice& remote_device); |
| + |
| + protected: |
| + BluetoothLowEnergyWeaveServer(); |
| + |
| + // Sets |task_runner_| for testing. |
| + void SetTaskRunnerForTesting(scoped_refptr<base::TaskRunner> task_runner); |
| + |
| + private: |
| + bool IsAdvertising(std::string bluetooth_address); |
| + bool IsConnected(std::string bluetooth_address); |
| + |
| + // Rotating function for refreshing the EID seed. Will post itself on the |
| + // task runner upon completion to run at the next 14 day refresh boundary. |
| + void UpdateEidSeeds(); |
|
Kyle Horimoto
2016/07/29 03:15:39
We should try to rotate which devices we advertise
jingxuy
2016/07/29 18:45:43
You misunderstood. This function is for refreshing
Kyle Horimoto
2016/07/29 19:04:18
EidGenerator does this for you already; this class
jingxuy
2016/07/29 20:26:01
This does not do the refreshing. It calls EIDGener
Kyle Horimoto
2016/07/29 21:07:46
Yeah, but this should not happen every 14 days. Ea
|
| + |
| + // Rotating function for refreshing the EID. Will post itself on the task |
| + // runner upon completion to run at the next 8 hour refresh boundary. |
| + void UpdateEids(); |
|
Kyle Horimoto
2016/07/29 03:15:39
Same.
jingxuy
2016/07/29 18:45:43
This function is for refreshing advertisement alre
Kyle Horimoto
2016/07/29 19:04:18
Same - EidGenerator does this for you already.
jingxuy
2016/07/29 20:26:01
same as above.
Kyle Horimoto
2016/07/29 21:07:46
Same as above.
|
| + |
| + // Refresh the currently registered functions. Since the currently registered |
| + // advertisements could not be modified, unregister them and tries to |
| + // re-register them. If they are not re-registered, they are guarnateed to be |
| + // on the front of the advertisement queue. |
| + void RefreshAdvertisement(); |
| + |
| + // Rotating function for giving other devices a fair share of the advertising |
| + // time slot. Will post itself on the task runner upon completion to run in |
| + // 5 seconds. |
| + void RotateAdvertisement(); |
|
jingxuy
2016/07/29 18:45:43
This function is for actually rotating 5 seconds.
jingxuy
2016/07/29 20:26:01
@Kyle
Kyle Horimoto
2016/07/29 21:07:46
Yep - is there a question I'm supposed to answer?
|
| + |
| + // Helper to attempt to register an advertisement. |
| + void Advertise(); |
| + |
| + void UnregisterAdvertisement(std::string bluetooth_address); |
| + |
| + // Remove the advertisement from map_to_advertisement_ because it's no longer |
| + // advertising. |
| + void UnregisterAdvertisementSuccess(std::string bluetooth_address); |
| + |
| + // This should not happen. |
| + void UnregisterAdvertisementError(std::string bluetooth_address, |
| + BluetoothAdvertisement::ErrorCode error); |
| + |
| + // If it's a success, then rotate the ad_rotator_ to go to the next |
| + // advertisement and attempt to register that one. |
| + void RegisterAdvertisementSuccess( |
| + std::string bluetooth_address, |
| + scoped_refptr<BluetoothAdvertisement> advertisement); |
| + |
| + // Stop the rolling advertisement process by doing nothing. |
| + // TODO(jingxuu): do I need to log anything? |
| + void RegisterAdvertisementError(BluetoothAdvertisement::ErrorCode error) {} |
| + |
| + // TODO(jingxuu): currently this is an empty function, unsure whether if |
| + // anything needed to be done when the adapter is created. |
| + void CreateAdapterCallback() {} |
| + |
| + scoped_refptr<base::TaskRunner> task_runner_; |
| + |
| + // All registered devices included here. |
| + std::map<std::string, RemoteDevice> map_to_remote_device_; |
| + |
| + std::unique_ptr<BluetoothLowEnergyAdvertisementRotator> ad_rotator_; |
| + |
| + // The currently registered advertisements. |
| + std::map<std::string, scoped_refptr<BluetoothAdvertisement>> |
| + map_to_advertisement_; |
| + |
| + // The currently active connections. One connection per device allowed. |
| + std::map<std::string, |
| + std::unique_ptr<BluetoothLowEnergyWeaveServerConnection>> |
| + map_to_server_connection_; |
| + |
| + scoped_refptr<BluetoothAdapter> adapter_; |
| + |
| + base::WeakPtr<BluetoothLocalGattService> service_; |
| + |
| + base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic_; |
| + base::WeakPtr<BluetoothLocalGattCharacteristic> tx_characteristic_; |
| + |
| + base::WeakPtrFactory<BluetoothLowEnergyWeaveServer> weak_ptr_factory_; |
| +}; |
| + |
| +} // namespace weave |
| + |
| +} // namespace proximity_auth |
| + |
| +#endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_SERVER_H_ |