Chromium Code Reviews| 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 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.
| |
| 12 } | |
| 13 | |
| 14 // static. | |
| 15 BluetoothLowEnergyAdvertisementRotator::Factory* | |
| 16 BluetoothLowEnergyAdvertisementRotator::Factory::factory_instance_ = | |
| 17 nullptr; | |
| 18 | |
| 19 // static. | |
| 20 std::unique_ptr<BluetoothLowEnergyAdvertisementRotator> | |
| 21 BluetoothLowEnergyAdvertisementRotator::Factory::NewInstance( | |
| 22 std::string service_uuid) { | |
| 23 if (factory_instance_ == nullptr) { | |
| 24 factory_instance_ = new Factory(); | |
| 25 } | |
| 26 return factory_instance_->BuildInstance(service_uuid); | |
| 27 } | |
| 28 | |
| 29 // static. | |
| 30 void BluetoothLowEnergyAdvertisementRotator::Factory::SetInstanceForTesting( | |
| 31 Factory* factory) { | |
| 32 factory_instance_ = factory; | |
| 33 } | |
| 34 | |
| 35 std::unique_ptr<BluetoothLowEnergyAdvertisementRotator> | |
| 36 BluetoothLowEnergyAdvertisementRotator::Factory::BuildInstance( | |
| 37 std::string service_uuid) { | |
| 38 return std::unique_ptr<BluetoothLowEnergyAdvertisementRotator>( | |
| 39 new BluetoothLowEnergyAdvertisementRotator(service_uuid)); | |
| 40 } | |
| 41 | |
| 42 BluetoothLowEnergyAdvertisementRotator::BluetoothLowEnergyAdvertisementRotator( | |
| 43 std::string service_uuid) | |
| 44 : service_uuid_(service_uuid) {} | |
| 45 | |
| 46 BluetoothLowEnergyAdvertisementRotator:: | |
| 47 ~BluetoothLowEnergyAdvertisementRotator() {} | |
| 48 | |
| 49 std::pair<std::string, std::unique_ptr<Advertisement>> | |
| 50 BluetoothLowEnergyAdvertisementRotator::GetNextAdvertisement() { | |
| 51 DCHECK(!devices_.empty()); | |
| 52 | |
| 53 if (waiting_queue_.empty()) { | |
| 54 for (uint32_t i = 0; i < devices_.size(); ++i) { | |
| 55 waiting_queue_.push_back(i); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 const std::string bluetooth_address = | |
| 60 devices_[waiting_queue_.front()].bluetooth_address; | |
| 61 | |
| 62 return std::pair<std::string, std::unique_ptr<Advertisement>>( | |
| 63 bluetooth_address, GetAddedDeviceAdvertisement(bluetooth_address)); | |
| 64 } | |
| 65 | |
| 66 std::unique_ptr<Advertisement> | |
| 67 BluetoothLowEnergyAdvertisementRotator::GetAddedDeviceAdvertisement( | |
| 68 std::string bluetooth_address) { | |
| 69 DCHECK(HasDeviceWithAddress(bluetooth_address)); | |
| 70 | |
| 71 Advertisement* ad = new Advertisement(kPeripheralAdType); | |
| 72 std::vector<std::string>* service_uuid = | |
| 73 new std::vector<std::string>(1, service_uuid_); | |
| 74 ad->set_service_uuids( | |
| 75 std::unique_ptr<std::vector<std::string>>(service_uuid)); | |
| 76 | |
| 77 // TOOD(tether-dev): set service data to real eid stuff | |
| 78 | |
| 79 uint16_t eid = map_to_eid_generator_[bluetooth_address]->GetEid(); | |
| 80 | |
| 81 std::map<std::string, std::vector<uint8_t>>* service_data = | |
| 82 new std::map<std::string, std::vector<uint8_t>>(); | |
| 83 service_data->at("EID") = std::vector<uint8_t>(1, eid); | |
| 84 | |
| 85 ad->set_service_data( | |
| 86 std::unique_ptr<std::map<std::string, std::vector<uint8_t>>>( | |
| 87 service_data)); | |
| 88 | |
| 89 // TODO(jingxuy): find out what to do with the other sets | |
| 90 | |
| 91 return std::unique_ptr<Advertisement>(ad); | |
| 92 } | |
| 93 | |
| 94 void BluetoothLowEnergyAdvertisementRotator::RotateAdvertisement() { | |
| 95 DCHECK(!waiting_queue_.empty()); | |
| 96 waiting_queue_.erase(waiting_queue_.begin()); | |
| 97 } | |
| 98 | |
| 99 void BluetoothLowEnergyAdvertisementRotator::AddDevice( | |
| 100 const RemoteDevice& device) { | |
| 101 DCHECK(!HasDeviceWithAddress(device.bluetooth_address)); | |
| 102 map_to_eid_generator_.insert( | |
| 103 std::pair<std::string, std::unique_ptr<BluetoothLowEnergyEidGenerator>>( | |
| 104 device.bluetooth_address, | |
| 105 BluetoothLowEnergyEidGenerator::Factory::NewInstance(device))); | |
| 106 | |
| 107 devices_.push_back(device); | |
| 108 } | |
| 109 | |
| 110 void BluetoothLowEnergyAdvertisementRotator::RemoveDevice( | |
| 111 const RemoteDevice& device) { | |
| 112 DCHECK(HasDeviceWithAddress(device.bluetooth_address)); | |
| 113 uint32_t pos = GetPosWithAddress(device.bluetooth_address); | |
| 114 devices_.erase(devices_.begin() + pos); | |
| 115 | |
| 116 // Remove possible multiple instances of the waiting advertisements. | |
| 117 while (true) { | |
| 118 auto waiting_queue_it = | |
| 119 std::find(waiting_queue_.begin(), waiting_queue_.end(), pos); | |
| 120 if (waiting_queue_it != waiting_queue_.end()) { | |
| 121 waiting_queue_.erase(waiting_queue_it); | |
| 122 } else { | |
| 123 break; | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 map_to_eid_generator_.erase(device.bluetooth_address); | |
| 128 } | |
| 129 | |
| 130 void BluetoothLowEnergyAdvertisementRotator::CutAdvertisementLine( | |
| 131 std::string bluetooth_address) { | |
| 132 DCHECK(HasDeviceWithAddress(bluetooth_address)); | |
| 133 waiting_queue_.insert(waiting_queue_.begin(), | |
| 134 GetPosWithAddress(bluetooth_address)); | |
| 135 } | |
| 136 | |
| 137 bool BluetoothLowEnergyAdvertisementRotator::HasDeviceWithAddress( | |
| 138 std::string bluetooth_address) { | |
| 139 for (const RemoteDevice& device : devices_) { | |
| 140 if (device.bluetooth_address == bluetooth_address) { | |
| 141 return true; | |
| 142 } | |
| 143 } | |
| 144 return false; | |
| 145 } | |
| 146 | |
| 147 int BluetoothLowEnergyAdvertisementRotator::GetPosWithAddress( | |
| 148 std::string bluetooth_address) { | |
| 149 for (uint32_t i = 0; i < devices_.size(); ++i) { | |
| 150 if (devices_[i].bluetooth_address == bluetooth_address) { | |
| 151 return i; | |
| 152 } | |
| 153 } | |
| 154 NOTREACHED(); | |
| 155 return -1; | |
| 156 } | |
| 157 | |
| 158 } // namespace weave | |
| 159 | |
| 160 } // namespace proximity_auth | |
| OLD | NEW |