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

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: Created 4 years, 5 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..db85c50dab70bb48ce6a169b1ddccdac88a9ee2a
--- /dev/null
+++ b/components/proximity_auth/ble/bluetooth_low_energy_advertisement_rotator.cc
@@ -0,0 +1,160 @@
+// 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 {
+
+typedef BluetoothLowEnergyAdvertisementRotator::Advertisement Advertisement;
rkc 2016/07/27 00:56:33 So we're typedefing another typedef? This seems, o
jingxuy 2016/07/27 23:31:47 Done.
+}
+
+// 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) {}
+
+BluetoothLowEnergyAdvertisementRotator::
+ ~BluetoothLowEnergyAdvertisementRotator() {}
+
+std::pair<std::string, std::unique_ptr<Advertisement>>
+BluetoothLowEnergyAdvertisementRotator::GetNextAdvertisement() {
+ DCHECK(!devices_.empty());
+
+ if (waiting_queue_.empty()) {
+ for (uint32_t i = 0; i < devices_.size(); ++i) {
+ waiting_queue_.push_back(i);
+ }
+ }
+
+ const std::string bluetooth_address =
+ devices_[waiting_queue_.front()].bluetooth_address;
+
+ return std::pair<std::string, std::unique_ptr<Advertisement>>(
+ bluetooth_address, GetAddedDeviceAdvertisement(bluetooth_address));
+}
+
+std::unique_ptr<Advertisement>
+BluetoothLowEnergyAdvertisementRotator::GetAddedDeviceAdvertisement(
+ std::string bluetooth_address) {
+ DCHECK(HasDeviceWithAddress(bluetooth_address));
+
+ 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
+
+ uint16_t eid = map_to_eid_generator_[bluetooth_address]->GetEid();
+
+ std::map<std::string, std::vector<uint8_t>>* service_data =
+ new std::map<std::string, std::vector<uint8_t>>();
+ service_data->at("EID") = std::vector<uint8_t>(1, eid);
+
+ 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::unique_ptr<Advertisement>(ad);
+}
+
+void BluetoothLowEnergyAdvertisementRotator::RotateAdvertisement() {
+ DCHECK(!waiting_queue_.empty());
+ waiting_queue_.erase(waiting_queue_.begin());
+}
+
+void BluetoothLowEnergyAdvertisementRotator::AddDevice(
+ const RemoteDevice& device) {
+ DCHECK(!HasDeviceWithAddress(device.bluetooth_address));
+ map_to_eid_generator_.insert(
+ std::pair<std::string, std::unique_ptr<BluetoothLowEnergyEidGenerator>>(
+ device.bluetooth_address,
+ BluetoothLowEnergyEidGenerator::Factory::NewInstance(device)));
+
+ devices_.push_back(device);
+}
+
+void BluetoothLowEnergyAdvertisementRotator::RemoveDevice(
+ const RemoteDevice& device) {
+ DCHECK(HasDeviceWithAddress(device.bluetooth_address));
+ uint32_t pos = GetPosWithAddress(device.bluetooth_address);
+ devices_.erase(devices_.begin() + pos);
+
+ // Remove possible multiple instances of the waiting advertisements.
+ while (true) {
+ auto waiting_queue_it =
+ std::find(waiting_queue_.begin(), waiting_queue_.end(), pos);
+ if (waiting_queue_it != waiting_queue_.end()) {
+ waiting_queue_.erase(waiting_queue_it);
+ } else {
+ break;
+ }
+ }
+
+ map_to_eid_generator_.erase(device.bluetooth_address);
+}
+
+void BluetoothLowEnergyAdvertisementRotator::CutAdvertisementLine(
+ std::string bluetooth_address) {
+ DCHECK(HasDeviceWithAddress(bluetooth_address));
+ waiting_queue_.insert(waiting_queue_.begin(),
+ GetPosWithAddress(bluetooth_address));
+}
+
+bool BluetoothLowEnergyAdvertisementRotator::HasDeviceWithAddress(
+ std::string bluetooth_address) {
+ for (const RemoteDevice& device : devices_) {
+ if (device.bluetooth_address == bluetooth_address) {
+ return true;
+ }
+ }
+ return false;
+}
+
+int BluetoothLowEnergyAdvertisementRotator::GetPosWithAddress(
+ std::string bluetooth_address) {
+ for (uint32_t i = 0; i < devices_.size(); ++i) {
+ if (devices_[i].bluetooth_address == bluetooth_address) {
+ return i;
+ }
+ }
+ NOTREACHED();
+ return -1;
+}
+
+} // namespace weave
+
+} // namespace proximity_auth

Powered by Google App Engine
This is Rietveld 408576698