Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(585)

Unified Diff: components/proximity_auth/ble/bluetooth_low_energy_weave_server.h

Issue 2183523006: Chrome OS uWeave Characteristics Server (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@migration
Patch Set: added channel Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..daa393188704ed918301216e7f2c682ad75c6b48
--- /dev/null
+++ b/components/proximity_auth/ble/bluetooth_low_energy_weave_server.h
@@ -0,0 +1,235 @@
+// 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_channel.h"
+#include "components/proximity_auth/ble/bluetooth_low_energy_weave_defines.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
+
+// 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 BluetoothLowEnergyWeaveChannel::Observer {
+ public:
+ class Observer {
+ public:
+ virtual void OnConnectionEstablished(
+ std::shared_ptr<BluetoothLowEnergyWeaveServerConnection>
+ connection) = 0;
+
+ virtual void OnConnectionClosed() = 0;
+
+ virtual void OnDeviceAdvertised(RemoteDevice device) = 0;
+ };
+
+ 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;
+
+ // BluetoothLowEnergyWeaveChannel::Observer
+ void OnChannelAuthenticated(std::string device_id,
+ const BluetoothDevice* bluetooth_device) override;
+
+ void OnChannelDisconnected(std::string device_id,
+ const BluetoothDevice* bluetooth_device) override;
+
+ ~BluetoothLowEnergyWeaveServer();
+
+ void RegisterObserver(std::shared_ptr<Observer> observer);
+
+ void UnregisterObserver(std::shared_ptr<Observer> observer);
+
+ // |remote_device| must not share a user_id 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);
+
+ protected:
+ BluetoothLowEnergyWeaveServer();
+
+ // Sets |task_runner_| for testing.
+ void SetTaskRunnerForTesting(scoped_refptr<base::TaskRunner> task_runner);
+
+ private:
+ // |remote_device| must already be registered and not advertisement enabled.
+ void EnableAdvertising(std::string device_id);
+
+ // |remote_device| must already be registered and advertisement enabled.
+ void DisableAdvertising(std::string device_id);
+
+ bool IsAdvertising(std::string bluetooth_address);
+ bool HasChannel(const BluetoothDevice* bluetooth_device);
+ bool IsConnected(const BluetoothDevice* bluetooth_device);
+
+ // 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();
+
+ // 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(jingxuy): do I need to log anything?
+ void RegisterAdvertisementError(BluetoothAdvertisement::ErrorCode error) {}
+
+ // TODO(jingxuy): 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_;
+
+ std::vector<std::shared_ptr<Observer>> observers_;
+
+ // 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 channels.
+ std::map<const BluetoothDevice*,
+ std::unique_ptr<BluetoothLowEnergyWeaveChannel>>
+ map_to_channel_;
+
+ 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_

Powered by Google App Engine
This is Rietveld 408576698