OLD | NEW |
---|---|
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_characteristic_win.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" |
6 | 6 |
7 #include "base/bind.h" | |
7 #include "device/bluetooth/bluetooth_adapter_win.h" | 8 #include "device/bluetooth/bluetooth_adapter_win.h" |
9 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_win.h" | |
8 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" | 10 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
9 #include "device/bluetooth/bluetooth_task_manager_win.h" | 11 #include "device/bluetooth/bluetooth_task_manager_win.h" |
10 | 12 |
11 namespace device { | 13 namespace device { |
12 | 14 |
13 BluetoothRemoteGattCharacteristicWin::BluetoothRemoteGattCharacteristicWin( | 15 BluetoothRemoteGattCharacteristicWin::BluetoothRemoteGattCharacteristicWin( |
14 BluetoothRemoteGattServiceWin* parent_service, | 16 BluetoothRemoteGattServiceWin* parent_service, |
15 BTH_LE_GATT_CHARACTERISTIC* characteristic_info, | 17 BTH_LE_GATT_CHARACTERISTIC* characteristic_info, |
16 scoped_refptr<base::SequencedTaskRunner>& ui_task_runner) | 18 scoped_refptr<base::SequencedTaskRunner>& ui_task_runner) |
17 : parent_service_(parent_service), | 19 : parent_service_(parent_service), |
18 characteristic_info_(characteristic_info), | 20 characteristic_info_(characteristic_info), |
19 ui_task_runner_(ui_task_runner), | 21 ui_task_runner_(ui_task_runner), |
22 characteristic_added_notified_(false), | |
20 weak_ptr_factory_(this) { | 23 weak_ptr_factory_(this) { |
21 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 24 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
22 DCHECK(parent_service_); | 25 DCHECK(parent_service_); |
23 DCHECK(characteristic_info_); | 26 DCHECK(characteristic_info_); |
24 | 27 |
25 adapter_ = static_cast<BluetoothAdapterWin*>( | 28 adapter_ = static_cast<BluetoothAdapterWin*>( |
26 parent_service_->GetDevice()->GetAdapter()); | 29 parent_service_->GetDevice()->GetAdapter()); |
27 DCHECK(adapter_); | 30 DCHECK(adapter_); |
28 task_manager_ = adapter_->GetWinBluetoothTaskManager(); | 31 task_manager_ = adapter_->GetWinBluetoothTaskManager(); |
29 DCHECK(task_manager_); | 32 DCHECK(task_manager_); |
30 | 33 characteristic_uuid_ = |
31 characteristic_uuid_ = task_manager_->BluetoothLowEnergyUuidToBluetoothUuid( | 34 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid( |
32 characteristic_info_->CharacteristicUuid); | 35 characteristic_info_->CharacteristicUuid); |
33 characteristic_identifier_ = | 36 characteristic_identifier_ = |
34 parent_service_->GetIdentifier() + | 37 parent_service_->GetIdentifier() + "_" + |
35 std::to_string(characteristic_info_->AttributeHandle); | 38 std::to_string(characteristic_info_->AttributeHandle); |
36 Update(); | 39 Update(); |
37 } | 40 } |
38 | 41 |
39 BluetoothRemoteGattCharacteristicWin::~BluetoothRemoteGattCharacteristicWin() { | 42 BluetoothRemoteGattCharacteristicWin::~BluetoothRemoteGattCharacteristicWin() { |
40 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 43 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
41 | 44 |
42 adapter_->NotifyGattCharacteristicRemoved(this); | 45 adapter_->NotifyGattCharacteristicRemoved(this); |
43 } | 46 } |
44 | 47 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 return permissions; | 106 return permissions; |
104 } | 107 } |
105 | 108 |
106 bool BluetoothRemoteGattCharacteristicWin::IsNotifying() const { | 109 bool BluetoothRemoteGattCharacteristicWin::IsNotifying() const { |
107 NOTIMPLEMENTED(); | 110 NOTIMPLEMENTED(); |
108 return false; | 111 return false; |
109 } | 112 } |
110 | 113 |
111 std::vector<BluetoothGattDescriptor*> | 114 std::vector<BluetoothGattDescriptor*> |
112 BluetoothRemoteGattCharacteristicWin::GetDescriptors() const { | 115 BluetoothRemoteGattCharacteristicWin::GetDescriptors() const { |
113 NOTIMPLEMENTED(); | 116 std::vector<BluetoothGattDescriptor*> has_descriptors; |
scheib
2016/03/01 00:06:22
has_descriptors -> descriptors.
Has implies a Bool
gogerald1
2016/03/01 17:00:13
Done.
| |
114 return std::vector<BluetoothGattDescriptor*>(); | 117 for (const auto& descriptor : included_descriptors_) |
118 has_descriptors.push_back(descriptor.second.get()); | |
119 return has_descriptors; | |
115 } | 120 } |
116 | 121 |
117 BluetoothGattDescriptor* BluetoothRemoteGattCharacteristicWin::GetDescriptor( | 122 BluetoothGattDescriptor* BluetoothRemoteGattCharacteristicWin::GetDescriptor( |
118 const std::string& identifier) const { | 123 const std::string& identifier) const { |
119 NOTIMPLEMENTED(); | 124 GattDescriptorMap::const_iterator it = included_descriptors_.find(identifier); |
125 if (it != included_descriptors_.end()) | |
126 return it->second.get(); | |
120 return nullptr; | 127 return nullptr; |
121 } | 128 } |
122 | 129 |
123 bool BluetoothRemoteGattCharacteristicWin::AddDescriptor( | 130 bool BluetoothRemoteGattCharacteristicWin::AddDescriptor( |
124 BluetoothGattDescriptor* descriptor) { | 131 BluetoothGattDescriptor* descriptor) { |
125 NOTIMPLEMENTED(); | 132 NOTIMPLEMENTED(); |
126 return false; | 133 return false; |
127 } | 134 } |
128 | 135 |
129 bool BluetoothRemoteGattCharacteristicWin::UpdateValue( | 136 bool BluetoothRemoteGattCharacteristicWin::UpdateValue( |
(...skipping 19 matching lines...) Expand all Loading... | |
149 void BluetoothRemoteGattCharacteristicWin::WriteRemoteCharacteristic( | 156 void BluetoothRemoteGattCharacteristicWin::WriteRemoteCharacteristic( |
150 const std::vector<uint8_t>& new_value, | 157 const std::vector<uint8_t>& new_value, |
151 const base::Closure& callback, | 158 const base::Closure& callback, |
152 const ErrorCallback& error_callback) { | 159 const ErrorCallback& error_callback) { |
153 NOTIMPLEMENTED(); | 160 NOTIMPLEMENTED(); |
154 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED); | 161 error_callback.Run(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED); |
155 } | 162 } |
156 | 163 |
157 void BluetoothRemoteGattCharacteristicWin::Update() { | 164 void BluetoothRemoteGattCharacteristicWin::Update() { |
158 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 165 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
159 NOTIMPLEMENTED(); | 166 |
167 task_manager_->PostGetGattIncludedDescriptors( | |
168 parent_service_->GetServicePath(), characteristic_info_.get(), | |
169 base::Bind(&BluetoothRemoteGattCharacteristicWin:: | |
170 OnGetIncludedDescriptorsCallback, | |
171 weak_ptr_factory_.GetWeakPtr())); | |
160 } | 172 } |
161 | 173 |
162 uint16_t BluetoothRemoteGattCharacteristicWin::GetAttributeHandle() const { | 174 uint16_t BluetoothRemoteGattCharacteristicWin::GetAttributeHandle() const { |
163 return characteristic_info_->AttributeHandle; | 175 return characteristic_info_->AttributeHandle; |
164 } | 176 } |
165 | 177 |
178 void BluetoothRemoteGattCharacteristicWin::OnGetIncludedDescriptorsCallback( | |
179 scoped_ptr<BTH_LE_GATT_DESCRIPTOR> descriptors, | |
180 uint16_t num, | |
181 HRESULT hr) { | |
182 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
183 | |
184 UpdateIncludedDescriptors(descriptors.get(), num); | |
185 if (!characteristic_added_notified_) { | |
186 characteristic_added_notified_ = true; | |
187 adapter_->NotifyGattCharacteristicAdded(this); | |
188 } | |
189 } | |
190 | |
191 void BluetoothRemoteGattCharacteristicWin::UpdateIncludedDescriptors( | |
192 PBTH_LE_GATT_DESCRIPTOR descriptors, | |
193 uint16_t num) { | |
194 if (num == 0) { | |
195 included_descriptors_.clear(); | |
196 return; | |
197 } | |
198 | |
199 // First, remove descriptors that no longer exist. | |
200 std::vector<std::string> to_be_removed; | |
201 for (const auto& d : included_descriptors_) { | |
202 if (!DoesDescriptorExist(descriptors, num, d.second.get())) | |
203 to_be_removed.push_back(d.second->GetIdentifier()); | |
204 } | |
205 for (auto id : to_be_removed) | |
206 included_descriptors_.erase(id); | |
207 | |
208 // Return if no new descriptors have been added. | |
209 if (included_descriptors_.size() == num) | |
210 return; | |
211 | |
212 // Add new descriptors. | |
213 for (uint16_t i = 0; i < num; i++) { | |
214 if (!IsDescriptorDiscovered(descriptors[i].DescriptorUuid, | |
215 descriptors[i].AttributeHandle)) { | |
216 PBTH_LE_GATT_DESCRIPTOR win_descriptor_info = | |
217 new BTH_LE_GATT_DESCRIPTOR(); | |
218 *win_descriptor_info = descriptors[i]; | |
219 BluetoothRemoteGattDescriptorWin* descriptor = | |
220 new BluetoothRemoteGattDescriptorWin(this, win_descriptor_info, | |
221 ui_task_runner_); | |
222 included_descriptors_[descriptor->GetIdentifier()] = | |
223 make_scoped_ptr(descriptor); | |
224 } | |
225 } | |
226 } | |
227 | |
228 bool BluetoothRemoteGattCharacteristicWin::IsDescriptorDiscovered( | |
229 BTH_LE_UUID& uuid, | |
230 uint16_t attribute_handle) { | |
231 BluetoothUUID bt_uuid = | |
232 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid(uuid); | |
233 for (const auto& d : included_descriptors_) { | |
234 if (bt_uuid == d.second->GetUUID() && | |
235 attribute_handle == d.second->GetAttributeHandle()) { | |
236 return true; | |
237 } | |
238 } | |
239 return false; | |
240 } | |
241 | |
242 bool BluetoothRemoteGattCharacteristicWin::DoesDescriptorExist( | |
243 PBTH_LE_GATT_DESCRIPTOR descriptors, | |
244 uint16_t num, | |
245 BluetoothRemoteGattDescriptorWin* descriptor) { | |
246 for (uint16_t i = 0; i < num; i++) { | |
247 BluetoothUUID uuid = | |
248 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid( | |
249 descriptors[i].DescriptorUuid); | |
250 if (descriptor->GetUUID() == uuid && | |
251 descriptor->GetAttributeHandle() == descriptors[i].AttributeHandle) { | |
252 return true; | |
253 } | |
254 } | |
255 return false; | |
256 } | |
257 | |
166 } // namespace device. | 258 } // namespace device. |
OLD | NEW |