| 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..8fa2f18c21e8bc03c225fbf997f55b73de978fb0
|
| --- /dev/null
|
| +++ b/components/proximity_auth/ble/bluetooth_low_energy_advertisement_rotator.cc
|
| @@ -0,0 +1,146 @@
|
| +// 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 {
|
| +
|
| +const device::BluetoothAdvertisement::AdvertisementType kPeripheralAdType =
|
| + device::BluetoothAdvertisement::AdvertisementType::
|
| + ADVERTISEMENT_TYPE_PERIPHERAL;
|
| +}
|
| +
|
| +// 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 RemoteDevice& device = devices_[waiting_queue_.front()];
|
| +
|
| + 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 = eid_generator_->GetEid(device);
|
| +
|
| + 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::pair<std::string, std::unique_ptr<Advertisement>>(
|
| + device.bluetooth_address, 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));
|
| + 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;
|
| + }
|
| + }
|
| +}
|
| +
|
| +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
|
|
|