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