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

Side by Side Diff: device/bluetooth/bluetooth_remote_gatt_service_win.cc

Issue 1690133002: Implement BluetoothRemoteGattServiceWin and related unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments and split out of included GATT services Created 4 years, 9 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" 5 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h"
6 6
7 #include "base/bind.h"
8 #include "device/bluetooth/bluetooth_adapter_win.h"
9 #include "device/bluetooth/bluetooth_device_win.h"
10 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h"
11 #include "device/bluetooth/bluetooth_task_manager_win.h"
12
7 namespace device { 13 namespace device {
14
8 BluetoothRemoteGattServiceWin::BluetoothRemoteGattServiceWin( 15 BluetoothRemoteGattServiceWin::BluetoothRemoteGattServiceWin(
9 BluetoothDeviceWin* device, 16 BluetoothDeviceWin* device,
10 base::FilePath service_path, 17 base::FilePath service_path,
11 BluetoothUUID service_uuid, 18 BluetoothUUID service_uuid,
12 uint16_t service_attribute_handle, 19 uint16_t service_attribute_handle,
13 bool is_primary, 20 bool is_primary,
14 BluetoothRemoteGattServiceWin* parent_service, 21 BluetoothRemoteGattServiceWin* parent_service,
15 scoped_refptr<base::SequencedTaskRunner>& ui_task_runner) 22 scoped_refptr<base::SequencedTaskRunner>& ui_task_runner)
16 : service_path_(service_path), 23 : device_(device),
17 device_(device), 24 service_path_(service_path),
18 service_uuid_(service_uuid), 25 service_uuid_(service_uuid),
19 service_attribute_handle_(service_attribute_handle), 26 service_attribute_handle_(service_attribute_handle),
20 is_primary_(is_primary), 27 is_primary_(is_primary),
21 parent_service_(parent_service), 28 parent_service_(parent_service),
22 ui_task_runner_(ui_task_runner) { 29 ui_task_runner_(ui_task_runner),
30 discovery_complete_notified_(false),
31 included_characteristics_discovered_(false),
32 weak_ptr_factory_(this) {
23 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 33 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
24 DCHECK(!service_path_.empty()); 34 DCHECK(!service_path_.empty());
25 DCHECK(service_uuid_.IsValid()); 35 DCHECK(service_uuid_.IsValid());
26 DCHECK(service_attribute_handle_); 36 DCHECK(service_attribute_handle_);
27 DCHECK(device_); 37 DCHECK(device_);
28 if (!is_primary_) 38 if (!is_primary_)
29 DCHECK(parent_service_); 39 DCHECK(parent_service_);
40
41 adapter_ = static_cast<BluetoothAdapterWin*>(device_->GetAdapter());
42 DCHECK(adapter_);
43 task_manager_ = adapter_->GetWinBluetoothTaskManager();
44 DCHECK(task_manager_);
45 service_identifier_ = device_->GetIdentifier() + "/" + service_uuid_.value() +
46 "_" + std::to_string(service_attribute_handle_);
30 Update(); 47 Update();
31 } 48 }
32 49
33 BluetoothRemoteGattServiceWin::~BluetoothRemoteGattServiceWin() {} 50 BluetoothRemoteGattServiceWin::~BluetoothRemoteGattServiceWin() {
51 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
52
53 adapter_->NotifyGattServiceRemoved(this);
54 }
34 55
35 std::string BluetoothRemoteGattServiceWin::GetIdentifier() const { 56 std::string BluetoothRemoteGattServiceWin::GetIdentifier() const {
36 std::string identifier = 57 return service_identifier_;
37 service_uuid_.value() + "_" + std::to_string(service_attribute_handle_);
38 if (is_primary_)
39 return device_->GetIdentifier() + "/" + identifier;
40 else
41 return parent_service_->GetIdentifier() + "/" + identifier;
42 } 58 }
43 59
44 BluetoothUUID BluetoothRemoteGattServiceWin::GetUUID() const { 60 BluetoothUUID BluetoothRemoteGattServiceWin::GetUUID() const {
45 return const_cast<BluetoothUUID&>(service_uuid_); 61 return const_cast<BluetoothUUID&>(service_uuid_);
46 } 62 }
47 63
48 bool BluetoothRemoteGattServiceWin::IsLocal() const { 64 bool BluetoothRemoteGattServiceWin::IsLocal() const {
49 return false; 65 return false;
50 } 66 }
51 67
52 bool BluetoothRemoteGattServiceWin::IsPrimary() const { 68 bool BluetoothRemoteGattServiceWin::IsPrimary() const {
53 return is_primary_; 69 return is_primary_;
54 } 70 }
55 71
56 BluetoothDevice* BluetoothRemoteGattServiceWin::GetDevice() const { 72 BluetoothDevice* BluetoothRemoteGattServiceWin::GetDevice() const {
57 return device_; 73 return device_;
58 } 74 }
59 75
60 std::vector<BluetoothGattCharacteristic*> 76 std::vector<BluetoothGattCharacteristic*>
61 BluetoothRemoteGattServiceWin::GetCharacteristics() const { 77 BluetoothRemoteGattServiceWin::GetCharacteristics() const {
62 NOTIMPLEMENTED(); 78 std::vector<BluetoothGattCharacteristic*> has_characteristics;
63 return std::vector<BluetoothGattCharacteristic*>(); 79 for (const auto& c : included_characteristics_)
80 has_characteristics.push_back(c.second.get());
81 return has_characteristics;
64 } 82 }
65 83
66 std::vector<BluetoothGattService*> 84 std::vector<BluetoothGattService*>
67 BluetoothRemoteGattServiceWin::GetIncludedServices() const { 85 BluetoothRemoteGattServiceWin::GetIncludedServices() const {
68 NOTIMPLEMENTED(); 86 NOTIMPLEMENTED();
87 // TODO(crbug.com/590008): Needs implementation.
scheib 2016/02/26 04:56:17 Thanks for issue. Please always have issues block
69 return std::vector<BluetoothGattService*>(); 88 return std::vector<BluetoothGattService*>();
70 } 89 }
71 90
72 BluetoothGattCharacteristic* BluetoothRemoteGattServiceWin::GetCharacteristic( 91 BluetoothGattCharacteristic* BluetoothRemoteGattServiceWin::GetCharacteristic(
73 const std::string& identifier) const { 92 const std::string& identifier) const {
74 NOTIMPLEMENTED(); 93 GattCharacteristicsMap::const_iterator it =
94 included_characteristics_.find(identifier);
95 if (it != included_characteristics_.end())
96 return it->second.get();
75 return nullptr; 97 return nullptr;
76 } 98 }
77 99
78 bool BluetoothRemoteGattServiceWin::AddCharacteristic( 100 bool BluetoothRemoteGattServiceWin::AddCharacteristic(
79 device::BluetoothGattCharacteristic* characteristic) { 101 device::BluetoothGattCharacteristic* characteristic) {
80 NOTIMPLEMENTED(); 102 NOTIMPLEMENTED();
81 return false; 103 return false;
82 } 104 }
83 105
84 bool BluetoothRemoteGattServiceWin::AddIncludedService( 106 bool BluetoothRemoteGattServiceWin::AddIncludedService(
85 device::BluetoothGattService* service) { 107 device::BluetoothGattService* service) {
86 NOTIMPLEMENTED(); 108 NOTIMPLEMENTED();
87 return false; 109 return false;
88 } 110 }
89 111
90 void BluetoothRemoteGattServiceWin::Register( 112 void BluetoothRemoteGattServiceWin::Register(
91 const base::Closure& callback, 113 const base::Closure& callback,
92 const ErrorCallback& error_callback) { 114 const ErrorCallback& error_callback) {
93 NOTIMPLEMENTED(); 115 NOTIMPLEMENTED();
94 error_callback.Run(); 116 error_callback.Run();
95 } 117 }
96 118
97 void BluetoothRemoteGattServiceWin::Unregister( 119 void BluetoothRemoteGattServiceWin::Unregister(
98 const base::Closure& callback, 120 const base::Closure& callback,
99 const ErrorCallback& error_callback) { 121 const ErrorCallback& error_callback) {
100 NOTIMPLEMENTED(); 122 NOTIMPLEMENTED();
101 error_callback.Run(); 123 error_callback.Run();
102 } 124 }
103 125
126 void BluetoothRemoteGattServiceWin::GattCharacteristicDiscoveryComplete(
127 BluetoothRemoteGattCharacteristicWin* characteristic) {
128 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
129 DCHECK(included_characteristics_.find(characteristic->GetIdentifier()) !=
130 included_characteristics_.end());
131
132 discovery_completed_included_charateristics_.insert(
133 characteristic->GetIdentifier());
134 adapter_->NotifyGattCharacteristicAdded(characteristic);
135 NotifyGattDiscoveryCompleteForServiceIfNecessary();
136 }
137
104 void BluetoothRemoteGattServiceWin::Update() { 138 void BluetoothRemoteGattServiceWin::Update() {
105 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 139 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
106 NOTIMPLEMENTED(); 140
141 task_manager_->PostGetGattIncludedCharacteristics(
142 service_path_, service_uuid_, service_attribute_handle_,
143 base::Bind(&BluetoothRemoteGattServiceWin::OnGetIncludedCharacteristics,
144 weak_ptr_factory_.GetWeakPtr()));
107 } 145 }
108 146
109 uint16_t BluetoothRemoteGattServiceWin::GetAttributeHandle() { 147 void BluetoothRemoteGattServiceWin::OnGetIncludedCharacteristics(
110 return service_attribute_handle_; 148 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC> characteristics,
149 uint16_t num,
150 HRESULT hr) {
151 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
152
153 UpdateIncludedCharacteristics(characteristics.get(), num);
154 included_characteristics_discovered_ = true;
155 NotifyGattDiscoveryCompleteForServiceIfNecessary();
111 } 156 }
112 157
113 } // namespace device. 158 void BluetoothRemoteGattServiceWin::UpdateIncludedCharacteristics(
159 PBTH_LE_GATT_CHARACTERISTIC characteristics,
160 uint16_t num) {
161 if (num == 0) {
162 if (included_characteristics_.size() > 0) {
163 ClearIncludedCharacteristics();
164 adapter_->NotifyGattServiceChanged(this);
165 }
166 return;
167 }
168
169 // First, remove characteristics that no longer exist.
170 std::vector<std::string> to_be_removed;
171 for (const auto& c : included_characteristics_) {
172 if (!DoesCharacteristicExist(characteristics, num, c.second.get()))
173 to_be_removed.push_back(c.second->GetIdentifier());
174 }
175 for (const auto& id : to_be_removed) {
176 RemoveIncludedCharacteristic(id);
177 }
178
179 // Update previously known characteristics.
180 for (auto& c : included_characteristics_)
181 c.second->Update();
182
183 // Return if no new characteristics have been added.
184 if (included_characteristics_.size() == num)
185 return;
186
187 // Add new characteristics.
188 for (uint16_t i = 0; i < num; i++) {
189 if (!IsCharacteristicDiscovered(characteristics[i].CharacteristicUuid,
190 characteristics[i].AttributeHandle)) {
191 PBTH_LE_GATT_CHARACTERISTIC info = new BTH_LE_GATT_CHARACTERISTIC();
192 *info = characteristics[i];
193 BluetoothRemoteGattCharacteristicWin* characteristic_object =
194 new BluetoothRemoteGattCharacteristicWin(this, info, ui_task_runner_);
195 included_characteristics_[characteristic_object->GetIdentifier()] =
196 make_scoped_ptr(characteristic_object);
197 }
198 }
199
200 if (included_characteristics_discovered_)
201 adapter_->NotifyGattServiceChanged(this);
202 }
203
204 void BluetoothRemoteGattServiceWin::
205 NotifyGattDiscoveryCompleteForServiceIfNecessary() {
206 if (discovery_completed_included_charateristics_.size() ==
207 included_characteristics_.size() &&
208 included_characteristics_discovered_ && !discovery_complete_notified_) {
209 adapter_->NotifyGattDiscoveryComplete(this);
210 discovery_complete_notified_ = true;
211 }
212 }
213
214 bool BluetoothRemoteGattServiceWin::IsCharacteristicDiscovered(
215 BTH_LE_UUID& uuid,
216 uint16_t attribute_handle) {
217 BluetoothUUID bt_uuid =
218 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid(uuid);
219 for (const auto& c : included_characteristics_) {
220 if (bt_uuid == c.second->GetUUID() &&
221 attribute_handle == c.second->GetAttributeHandle()) {
222 return true;
223 }
224 }
225 return false;
226 }
227
228 bool BluetoothRemoteGattServiceWin::DoesCharacteristicExist(
229 PBTH_LE_GATT_CHARACTERISTIC characteristics,
230 uint16_t num,
231 BluetoothRemoteGattCharacteristicWin* characteristic) {
232 for (uint16_t i = 0; i < num; i++) {
233 BluetoothUUID uuid =
234 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid(
235 characteristics[i].CharacteristicUuid);
236 if (characteristic->GetUUID() == uuid &&
237 characteristic->GetAttributeHandle() ==
238 characteristics[i].AttributeHandle) {
239 return true;
240 }
241 }
242 return false;
243 }
244
245 void BluetoothRemoteGattServiceWin::RemoveIncludedCharacteristic(
246 std::string identifier) {
247 discovery_completed_included_charateristics_.erase(identifier);
248 included_characteristics_.erase(identifier);
249 }
250
251 void BluetoothRemoteGattServiceWin::ClearIncludedCharacteristics() {
252 discovery_completed_included_charateristics_.clear();
253 included_characteristics_.clear();
254 }
255
256 } // namespace device.
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_remote_gatt_service_win.h ('k') | device/bluetooth/bluetooth_task_manager_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698