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

Side by Side 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 unified diff | Download patch
OLDNEW
(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 using AdvertisementContent =
12 BluetoothLowEnergyEidGenerator::AdvertisementContent;
13
14 const device::BluetoothAdvertisement::AdvertisementType kPeripheralAdType =
15 device::BluetoothAdvertisement::AdvertisementType::
16 ADVERTISEMENT_TYPE_PERIPHERAL;
17
18 } // namespace
19
20 // static.
21 BluetoothLowEnergyAdvertisementRotator::Factory*
22 BluetoothLowEnergyAdvertisementRotator::Factory::factory_instance_ =
23 nullptr;
24
25 // static.
26 std::unique_ptr<BluetoothLowEnergyAdvertisementRotator>
27 BluetoothLowEnergyAdvertisementRotator::Factory::NewInstance(
28 std::string service_uuid) {
29 if (factory_instance_ == nullptr) {
30 factory_instance_ = new Factory();
31 }
32 return factory_instance_->BuildInstance(service_uuid);
33 }
34
35 // static.
36 void BluetoothLowEnergyAdvertisementRotator::Factory::SetInstanceForTesting(
37 Factory* factory) {
38 factory_instance_ = factory;
39 }
40
41 std::unique_ptr<BluetoothLowEnergyAdvertisementRotator>
42 BluetoothLowEnergyAdvertisementRotator::Factory::BuildInstance(
43 std::string service_uuid) {
44 return std::unique_ptr<BluetoothLowEnergyAdvertisementRotator>(
45 new BluetoothLowEnergyAdvertisementRotator(service_uuid));
46 }
47
48 BluetoothLowEnergyAdvertisementRotator::BluetoothLowEnergyAdvertisementRotator(
49 std::string service_uuid)
50 : service_uuid_(service_uuid), queue_head_(0) {}
51
52 BluetoothLowEnergyAdvertisementRotator::
53 ~BluetoothLowEnergyAdvertisementRotator() {}
54
55 std::pair<std::string, std::unique_ptr<Advertisement>>
56 BluetoothLowEnergyAdvertisementRotator::GetNextAdvertisement() {
57 DCHECK(!devices_.empty());
58 DCHECK(queue_head_ < devices_.size());
59
60 const RemoteDevice& device = devices_[queue_head_];
61
62 Advertisement* ad = new Advertisement(kPeripheralAdType);
63 std::vector<std::string>* service_uuid =
64 new std::vector<std::string>(1, service_uuid_);
65 ad->set_service_uuids(
66 std::unique_ptr<std::vector<std::string>>(service_uuid));
67
68 // TOOD(tether-dev): set service data to real eid stuff
69
70 AdvertisementContent content =
71 eid_generator_->GetAdvertisementContent(devices_.front(), "");
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("content") = content;
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.user_id, std::unique_ptr<Advertisement>(ad));
85 }
86
87 void BluetoothLowEnergyAdvertisementRotator::RotateAdvertisement() {
88 AdvanceQueueHead();
89 }
90
91 void BluetoothLowEnergyAdvertisementRotator::AddDevice(
92 const RemoteDevice& device) {
93 DCHECK(!HasDeviceWithId(device.user_id));
94 devices_.insert(devices_.begin() + queue_head_, device);
95 AdvanceQueueHead();
96 }
97
98 void BluetoothLowEnergyAdvertisementRotator::RemoveDevice(
99 const RemoteDevice& device) {
100 DCHECK(HasDeviceWithId(device.user_id));
101
102 for (auto it = devices_.begin(); it != devices_.end(); ++it) {
103 if (it->user_id == device.user_id) {
104 devices_.erase(it);
105 uint32_t pos = std::distance(devices_.begin(), it);
106 if (pos < queue_head_) {
107 queue_head_--;
108 }
109 return;
110 }
111 }
112 }
113
114 bool BluetoothLowEnergyAdvertisementRotator::HasDeviceWithId(
115 std::string device_id) {
116 for (const auto& device : devices_) {
117 if (device.user_id == device_id) {
118 return true;
119 }
120 }
121 return false;
122 }
123
124 void BluetoothLowEnergyAdvertisementRotator::AdvanceQueueHead() {
125 queue_head_ = (queue_head_ + 1) % devices_.size();
126 }
127
128 } // namespace weave
129
130 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698