| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/proximity_auth/ble/bluetooth_low_energy_advertisement_rotat
or.h" |
| 6 |
| 7 namespace proximity_auth { |
| 8 namespace weave { |
| 9 namespace { |
| 10 |
| 11 const device::BluetoothAdvertisement::AdvertisementType kPeripheralAdType = |
| 12 device::BluetoothAdvertisement::AdvertisementType:: |
| 13 ADVERTISEMENT_TYPE_PERIPHERAL; |
| 14 } |
| 15 |
| 16 // static. |
| 17 BluetoothLowEnergyAdvertisementRotator::Factory* |
| 18 BluetoothLowEnergyAdvertisementRotator::Factory::factory_instance_ = |
| 19 nullptr; |
| 20 |
| 21 // static. |
| 22 std::unique_ptr<BluetoothLowEnergyAdvertisementRotator> |
| 23 BluetoothLowEnergyAdvertisementRotator::Factory::NewInstance( |
| 24 std::string service_uuid) { |
| 25 if (factory_instance_ == nullptr) { |
| 26 factory_instance_ = new Factory(); |
| 27 } |
| 28 return factory_instance_->BuildInstance(service_uuid); |
| 29 } |
| 30 |
| 31 // static. |
| 32 void BluetoothLowEnergyAdvertisementRotator::Factory::SetInstanceForTesting( |
| 33 Factory* factory) { |
| 34 factory_instance_ = factory; |
| 35 } |
| 36 |
| 37 std::unique_ptr<BluetoothLowEnergyAdvertisementRotator> |
| 38 BluetoothLowEnergyAdvertisementRotator::Factory::BuildInstance( |
| 39 std::string service_uuid) { |
| 40 return std::unique_ptr<BluetoothLowEnergyAdvertisementRotator>( |
| 41 new BluetoothLowEnergyAdvertisementRotator(service_uuid)); |
| 42 } |
| 43 |
| 44 BluetoothLowEnergyAdvertisementRotator::BluetoothLowEnergyAdvertisementRotator( |
| 45 std::string service_uuid) |
| 46 : service_uuid_(service_uuid) {} |
| 47 |
| 48 BluetoothLowEnergyAdvertisementRotator:: |
| 49 ~BluetoothLowEnergyAdvertisementRotator() {} |
| 50 |
| 51 std::pair<std::string, std::unique_ptr<Advertisement>> |
| 52 BluetoothLowEnergyAdvertisementRotator::GetNextAdvertisement() { |
| 53 DCHECK(!devices_.empty()); |
| 54 |
| 55 if (waiting_queue_.empty()) { |
| 56 for (uint32_t i = 0; i < devices_.size(); ++i) { |
| 57 waiting_queue_.push_back(i); |
| 58 } |
| 59 } |
| 60 |
| 61 const RemoteDevice& device = devices_[waiting_queue_.front()]; |
| 62 |
| 63 Advertisement* ad = new Advertisement(kPeripheralAdType); |
| 64 std::vector<std::string>* service_uuid = |
| 65 new std::vector<std::string>(1, service_uuid_); |
| 66 ad->set_service_uuids( |
| 67 std::unique_ptr<std::vector<std::string>>(service_uuid)); |
| 68 |
| 69 // TOOD(tether-dev): set service data to real eid stuff |
| 70 |
| 71 uint16_t eid = eid_generator_->GetEid(device); |
| 72 |
| 73 std::map<std::string, std::vector<uint8_t>>* service_data = |
| 74 new std::map<std::string, std::vector<uint8_t>>(); |
| 75 service_data->at("EID") = std::vector<uint8_t>(1, eid); |
| 76 |
| 77 ad->set_service_data( |
| 78 std::unique_ptr<std::map<std::string, std::vector<uint8_t>>>( |
| 79 service_data)); |
| 80 |
| 81 // TODO(jingxuy): find out what to do with the other sets |
| 82 |
| 83 return std::pair<std::string, std::unique_ptr<Advertisement>>( |
| 84 device.bluetooth_address, std::unique_ptr<Advertisement>(ad)); |
| 85 } |
| 86 |
| 87 void BluetoothLowEnergyAdvertisementRotator::RotateAdvertisement() { |
| 88 DCHECK(!waiting_queue_.empty()); |
| 89 waiting_queue_.erase(waiting_queue_.begin()); |
| 90 } |
| 91 |
| 92 void BluetoothLowEnergyAdvertisementRotator::AddDevice( |
| 93 const RemoteDevice& device) { |
| 94 DCHECK(!HasDeviceWithAddress(device.bluetooth_address)); |
| 95 devices_.push_back(device); |
| 96 } |
| 97 |
| 98 void BluetoothLowEnergyAdvertisementRotator::RemoveDevice( |
| 99 const RemoteDevice& device) { |
| 100 DCHECK(HasDeviceWithAddress(device.bluetooth_address)); |
| 101 uint32_t pos = GetPosWithAddress(device.bluetooth_address); |
| 102 devices_.erase(devices_.begin() + pos); |
| 103 |
| 104 // Remove possible multiple instances of the waiting advertisements. |
| 105 while (true) { |
| 106 auto waiting_queue_it = |
| 107 std::find(waiting_queue_.begin(), waiting_queue_.end(), pos); |
| 108 if (waiting_queue_it != waiting_queue_.end()) { |
| 109 waiting_queue_.erase(waiting_queue_it); |
| 110 } else { |
| 111 break; |
| 112 } |
| 113 } |
| 114 } |
| 115 |
| 116 void BluetoothLowEnergyAdvertisementRotator::CutAdvertisementLine( |
| 117 std::string bluetooth_address) { |
| 118 DCHECK(HasDeviceWithAddress(bluetooth_address)); |
| 119 waiting_queue_.insert(waiting_queue_.begin(), |
| 120 GetPosWithAddress(bluetooth_address)); |
| 121 } |
| 122 |
| 123 bool BluetoothLowEnergyAdvertisementRotator::HasDeviceWithAddress( |
| 124 std::string bluetooth_address) { |
| 125 for (const RemoteDevice& device : devices_) { |
| 126 if (device.bluetooth_address == bluetooth_address) { |
| 127 return true; |
| 128 } |
| 129 } |
| 130 return false; |
| 131 } |
| 132 |
| 133 int BluetoothLowEnergyAdvertisementRotator::GetPosWithAddress( |
| 134 std::string bluetooth_address) { |
| 135 for (uint32_t i = 0; i < devices_.size(); ++i) { |
| 136 if (devices_[i].bluetooth_address == bluetooth_address) { |
| 137 return i; |
| 138 } |
| 139 } |
| 140 NOTREACHED(); |
| 141 return -1; |
| 142 } |
| 143 |
| 144 } // namespace weave |
| 145 |
| 146 } // namespace proximity_auth |
| OLD | NEW |