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

Unified Diff: components/proximity_auth/ble/bluetooth_low_energy_advertisement_rotator.cc

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_advertisement_rotator.cc
diff --git a/components/proximity_auth/ble/bluetooth_low_energy_advertisement_rotator.cc b/components/proximity_auth/ble/bluetooth_low_energy_advertisement_rotator.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0c4b637f5fba567d11c094cd4379189afb7dd99a
--- /dev/null
+++ b/components/proximity_auth/ble/bluetooth_low_energy_advertisement_rotator.cc
@@ -0,0 +1,130 @@
+// 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.
+
+#include "components/proximity_auth/ble/bluetooth_low_energy_advertisement_rotator.h"
+
+namespace proximity_auth {
+namespace weave {
+namespace {
+
+using AdvertisementContent =
+ BluetoothLowEnergyEidGenerator::AdvertisementContent;
+
+const device::BluetoothAdvertisement::AdvertisementType kPeripheralAdType =
+ device::BluetoothAdvertisement::AdvertisementType::
+ ADVERTISEMENT_TYPE_PERIPHERAL;
+
+} // namespace
+
+// static.
+BluetoothLowEnergyAdvertisementRotator::Factory*
+ BluetoothLowEnergyAdvertisementRotator::Factory::factory_instance_ =
+ nullptr;
+
+// static.
+std::unique_ptr<BluetoothLowEnergyAdvertisementRotator>
+BluetoothLowEnergyAdvertisementRotator::Factory::NewInstance(
+ std::string service_uuid) {
+ if (factory_instance_ == nullptr) {
+ factory_instance_ = new Factory();
+ }
+ return factory_instance_->BuildInstance(service_uuid);
+}
+
+// static.
+void BluetoothLowEnergyAdvertisementRotator::Factory::SetInstanceForTesting(
+ Factory* factory) {
+ factory_instance_ = factory;
+}
+
+std::unique_ptr<BluetoothLowEnergyAdvertisementRotator>
+BluetoothLowEnergyAdvertisementRotator::Factory::BuildInstance(
+ std::string service_uuid) {
+ return std::unique_ptr<BluetoothLowEnergyAdvertisementRotator>(
+ new BluetoothLowEnergyAdvertisementRotator(service_uuid));
+}
+
+BluetoothLowEnergyAdvertisementRotator::BluetoothLowEnergyAdvertisementRotator(
+ std::string service_uuid)
+ : service_uuid_(service_uuid), queue_head_(0) {}
+
+BluetoothLowEnergyAdvertisementRotator::
+ ~BluetoothLowEnergyAdvertisementRotator() {}
+
+std::pair<std::string, std::unique_ptr<Advertisement>>
+BluetoothLowEnergyAdvertisementRotator::GetNextAdvertisement() {
+ DCHECK(!devices_.empty());
+ DCHECK(queue_head_ < devices_.size());
+
+ const RemoteDevice& device = devices_[queue_head_];
+
+ Advertisement* ad = new Advertisement(kPeripheralAdType);
+ std::vector<std::string>* service_uuid =
+ new std::vector<std::string>(1, service_uuid_);
+ ad->set_service_uuids(
+ std::unique_ptr<std::vector<std::string>>(service_uuid));
+
+ // TOOD(tether-dev): set service data to real eid stuff
+
+ AdvertisementContent content =
+ eid_generator_->GetAdvertisementContent(devices_.front(), "");
+
+ std::map<std::string, std::vector<uint8_t>>* service_data =
+ new std::map<std::string, std::vector<uint8_t>>();
+ service_data->at("content") = content;
+
+ ad->set_service_data(
+ std::unique_ptr<std::map<std::string, std::vector<uint8_t>>>(
+ service_data));
+
+ // TODO(jingxuy): find out what to do with the other sets
+
+ return std::pair<std::string, std::unique_ptr<Advertisement>>(
+ device.user_id, std::unique_ptr<Advertisement>(ad));
+}
+
+void BluetoothLowEnergyAdvertisementRotator::RotateAdvertisement() {
+ AdvanceQueueHead();
+}
+
+void BluetoothLowEnergyAdvertisementRotator::AddDevice(
+ const RemoteDevice& device) {
+ DCHECK(!HasDeviceWithId(device.user_id));
+ devices_.insert(devices_.begin() + queue_head_, device);
+ AdvanceQueueHead();
+}
+
+void BluetoothLowEnergyAdvertisementRotator::RemoveDevice(
+ const RemoteDevice& device) {
+ DCHECK(HasDeviceWithId(device.user_id));
+
+ for (auto it = devices_.begin(); it != devices_.end(); ++it) {
+ if (it->user_id == device.user_id) {
+ devices_.erase(it);
+ uint32_t pos = std::distance(devices_.begin(), it);
+ if (pos < queue_head_) {
+ queue_head_--;
+ }
+ return;
+ }
+ }
+}
+
+bool BluetoothLowEnergyAdvertisementRotator::HasDeviceWithId(
+ std::string device_id) {
+ for (const auto& device : devices_) {
+ if (device.user_id == device_id) {
+ return true;
+ }
+ }
+ return false;
+}
+
+void BluetoothLowEnergyAdvertisementRotator::AdvanceQueueHead() {
+ queue_head_ = (queue_head_ + 1) % devices_.size();
+}
+
+} // namespace weave
+
+} // namespace proximity_auth

Powered by Google App Engine
This is Rietveld 408576698