| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_chromeos.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "chromeos/dbus/bluetooth_gatt_service_client.h" | 9 #include "chromeos/dbus/bluetooth_gatt_service_client.h" |
| 10 #include "chromeos/dbus/dbus_thread_manager.h" | 10 #include "chromeos/dbus/dbus_thread_manager.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | 47 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 48 RemoveObserver(this); | 48 RemoveObserver(this); |
| 49 | 49 |
| 50 // Clean up all the characteristics. Copy the characteristics list here and | 50 // Clean up all the characteristics. Copy the characteristics list here and |
| 51 // clear the original so that when we send GattCharacteristicRemoved(), | 51 // clear the original so that when we send GattCharacteristicRemoved(), |
| 52 // GetCharacteristics() returns no characteristics. | 52 // GetCharacteristics() returns no characteristics. |
| 53 CharacteristicMap characteristics = characteristics_; | 53 CharacteristicMap characteristics = characteristics_; |
| 54 characteristics_.clear(); | 54 characteristics_.clear(); |
| 55 for (CharacteristicMap::iterator iter = characteristics.begin(); | 55 for (CharacteristicMap::iterator iter = characteristics.begin(); |
| 56 iter != characteristics.end(); ++iter) { | 56 iter != characteristics.end(); ++iter) { |
| 57 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, | 57 DCHECK(adapter_); |
| 58 GattCharacteristicRemoved(this, iter->second)); | 58 adapter_->NotifyGattCharacteristicRemoved(iter->second); |
| 59 |
| 59 delete iter->second; | 60 delete iter->second; |
| 60 } | 61 } |
| 61 } | 62 } |
| 62 | 63 |
| 63 void BluetoothRemoteGattServiceChromeOS::AddObserver( | |
| 64 device::BluetoothGattService::Observer* observer) { | |
| 65 DCHECK(observer); | |
| 66 observers_.AddObserver(observer); | |
| 67 } | |
| 68 | |
| 69 void BluetoothRemoteGattServiceChromeOS::RemoveObserver( | |
| 70 device::BluetoothGattService::Observer* observer) { | |
| 71 DCHECK(observer); | |
| 72 observers_.RemoveObserver(observer); | |
| 73 } | |
| 74 | |
| 75 std::string BluetoothRemoteGattServiceChromeOS::GetIdentifier() const { | 64 std::string BluetoothRemoteGattServiceChromeOS::GetIdentifier() const { |
| 76 return object_path_.value(); | 65 return object_path_.value(); |
| 77 } | 66 } |
| 78 | 67 |
| 79 device::BluetoothUUID BluetoothRemoteGattServiceChromeOS::GetUUID() const { | 68 device::BluetoothUUID BluetoothRemoteGattServiceChromeOS::GetUUID() const { |
| 80 BluetoothGattServiceClient::Properties* properties = | 69 BluetoothGattServiceClient::Properties* properties = |
| 81 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> | 70 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> |
| 82 GetProperties(object_path_); | 71 GetProperties(object_path_); |
| 83 DCHECK(properties); | 72 DCHECK(properties); |
| 84 return device::BluetoothUUID(properties->uuid.value()); | 73 return device::BluetoothUUID(properties->uuid.value()); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 error_callback.Run(); | 134 error_callback.Run(); |
| 146 } | 135 } |
| 147 | 136 |
| 148 void BluetoothRemoteGattServiceChromeOS::Unregister( | 137 void BluetoothRemoteGattServiceChromeOS::Unregister( |
| 149 const base::Closure& callback, | 138 const base::Closure& callback, |
| 150 const ErrorCallback& error_callback) { | 139 const ErrorCallback& error_callback) { |
| 151 VLOG(1) << "A remote GATT service cannot be unregistered."; | 140 VLOG(1) << "A remote GATT service cannot be unregistered."; |
| 152 error_callback.Run(); | 141 error_callback.Run(); |
| 153 } | 142 } |
| 154 | 143 |
| 155 scoped_refptr<device::BluetoothAdapter> | 144 BluetoothAdapterChromeOS* |
| 156 BluetoothRemoteGattServiceChromeOS::GetAdapter() const { | 145 BluetoothRemoteGattServiceChromeOS::GetAdapter() const { |
| 157 return adapter_; | 146 return adapter_; |
| 158 } | 147 } |
| 159 | 148 |
| 160 void BluetoothRemoteGattServiceChromeOS::NotifyServiceChanged() { | 149 void BluetoothRemoteGattServiceChromeOS::NotifyServiceChanged() { |
| 161 // Don't send service changed unless we know that all characteristics have | 150 // Don't send service changed unless we know that all characteristics have |
| 162 // already been discovered. This is to prevent spammy events before sending | 151 // already been discovered. This is to prevent spammy events before sending |
| 163 // out the first Gatt | 152 // out the first Gatt |
| 164 if (!discovery_complete_) | 153 if (!discovery_complete_) |
| 165 return; | 154 return; |
| 166 | 155 |
| 167 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, | 156 DCHECK(adapter_); |
| 168 GattServiceChanged(this)); | 157 adapter_->NotifyGattServiceChanged(this); |
| 169 } | 158 } |
| 170 | 159 |
| 171 void BluetoothRemoteGattServiceChromeOS::NotifyCharacteristicValueChanged( | 160 void BluetoothRemoteGattServiceChromeOS::NotifyCharacteristicValueChanged( |
| 172 BluetoothRemoteGattCharacteristicChromeOS* characteristic, | 161 BluetoothRemoteGattCharacteristicChromeOS* characteristic, |
| 173 const std::vector<uint8>& value) { | 162 const std::vector<uint8>& value) { |
| 174 DCHECK(characteristic->GetService() == this); | 163 DCHECK(characteristic->GetService() == this); |
| 175 FOR_EACH_OBSERVER( | 164 DCHECK(adapter_); |
| 176 device::BluetoothGattService::Observer, | 165 adapter_->NotifyGattCharacteristicValueChanged(characteristic, value); |
| 177 observers_, | |
| 178 GattCharacteristicValueChanged(this, characteristic, value)); | |
| 179 } | 166 } |
| 180 | 167 |
| 181 void BluetoothRemoteGattServiceChromeOS::NotifyDescriptorAddedOrRemoved( | 168 void BluetoothRemoteGattServiceChromeOS::NotifyDescriptorAddedOrRemoved( |
| 182 BluetoothRemoteGattCharacteristicChromeOS* characteristic, | 169 BluetoothRemoteGattCharacteristicChromeOS* characteristic, |
| 183 BluetoothRemoteGattDescriptorChromeOS* descriptor, | 170 BluetoothRemoteGattDescriptorChromeOS* descriptor, |
| 184 bool added) { | 171 bool added) { |
| 185 DCHECK(characteristic->GetService() == this); | 172 DCHECK(characteristic->GetService() == this); |
| 186 DCHECK(descriptor->GetCharacteristic() == characteristic); | 173 DCHECK(descriptor->GetCharacteristic() == characteristic); |
| 174 DCHECK(adapter_); |
| 175 |
| 187 if (added) { | 176 if (added) { |
| 188 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, | 177 adapter_->NotifyGattDescriptorAdded(descriptor); |
| 189 GattDescriptorAdded(characteristic, descriptor)); | |
| 190 return; | 178 return; |
| 191 } | 179 } |
| 192 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, | 180 |
| 193 GattDescriptorRemoved(characteristic, descriptor)); | 181 adapter_->NotifyGattDescriptorRemoved(descriptor); |
| 194 } | 182 } |
| 195 | 183 |
| 196 void BluetoothRemoteGattServiceChromeOS::NotifyDescriptorValueChanged( | 184 void BluetoothRemoteGattServiceChromeOS::NotifyDescriptorValueChanged( |
| 197 BluetoothRemoteGattCharacteristicChromeOS* characteristic, | 185 BluetoothRemoteGattCharacteristicChromeOS* characteristic, |
| 198 BluetoothRemoteGattDescriptorChromeOS* descriptor, | 186 BluetoothRemoteGattDescriptorChromeOS* descriptor, |
| 199 const std::vector<uint8>& value) { | 187 const std::vector<uint8>& value) { |
| 200 DCHECK(characteristic->GetService() == this); | 188 DCHECK(characteristic->GetService() == this); |
| 201 DCHECK(descriptor->GetCharacteristic() == characteristic); | 189 DCHECK(descriptor->GetCharacteristic() == characteristic); |
| 202 FOR_EACH_OBSERVER( | 190 DCHECK(adapter_); |
| 203 device::BluetoothGattService::Observer, observers_, | 191 adapter_->NotifyGattDescriptorValueChanged(descriptor, value); |
| 204 GattDescriptorValueChanged(characteristic, descriptor, value)); | |
| 205 } | 192 } |
| 206 | 193 |
| 207 void BluetoothRemoteGattServiceChromeOS::GattServicePropertyChanged( | 194 void BluetoothRemoteGattServiceChromeOS::GattServicePropertyChanged( |
| 208 const dbus::ObjectPath& object_path, | 195 const dbus::ObjectPath& object_path, |
| 209 const std::string& property_name){ | 196 const std::string& property_name){ |
| 210 if (object_path != object_path_) | 197 if (object_path != object_path_) |
| 211 return; | 198 return; |
| 212 | 199 |
| 213 VLOG(1) << "Service property changed: \"" << property_name << "\", " | 200 VLOG(1) << "Service property changed: \"" << property_name << "\", " |
| 214 << object_path.value(); | 201 << object_path.value(); |
| 215 BluetoothGattServiceClient::Properties* properties = | 202 BluetoothGattServiceClient::Properties* properties = |
| 216 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->GetProperties( | 203 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->GetProperties( |
| 217 object_path); | 204 object_path); |
| 218 DCHECK(properties); | 205 DCHECK(properties); |
| 219 | 206 |
| 220 if (property_name != properties->characteristics.name()) { | 207 if (property_name != properties->characteristics.name()) { |
| 221 NotifyServiceChanged(); | 208 NotifyServiceChanged(); |
| 222 return; | 209 return; |
| 223 } | 210 } |
| 224 | 211 |
| 225 if (discovery_complete_) | 212 if (discovery_complete_) |
| 226 return; | 213 return; |
| 227 | 214 |
| 228 VLOG(1) << "All characteristics were discovered for service: " | 215 VLOG(1) << "All characteristics were discovered for service: " |
| 229 << object_path.value(); | 216 << object_path.value(); |
| 230 discovery_complete_ = true; | 217 discovery_complete_ = true; |
| 231 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, | 218 DCHECK(adapter_); |
| 232 observers_, | 219 adapter_->NotifyGattDiscoveryComplete(this); |
| 233 GattDiscoveryCompleteForService(this)); | |
| 234 } | 220 } |
| 235 | 221 |
| 236 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicAdded( | 222 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicAdded( |
| 237 const dbus::ObjectPath& object_path) { | 223 const dbus::ObjectPath& object_path) { |
| 238 if (characteristics_.find(object_path) != characteristics_.end()) { | 224 if (characteristics_.find(object_path) != characteristics_.end()) { |
| 239 VLOG(1) << "Remote GATT characteristic already exists: " | 225 VLOG(1) << "Remote GATT characteristic already exists: " |
| 240 << object_path.value(); | 226 << object_path.value(); |
| 241 return; | 227 return; |
| 242 } | 228 } |
| 243 | 229 |
| 244 BluetoothGattCharacteristicClient::Properties* properties = | 230 BluetoothGattCharacteristicClient::Properties* properties = |
| 245 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | 231 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 246 GetProperties(object_path); | 232 GetProperties(object_path); |
| 247 DCHECK(properties); | 233 DCHECK(properties); |
| 248 if (properties->service.value() != object_path_) { | 234 if (properties->service.value() != object_path_) { |
| 249 VLOG(2) << "Remote GATT characteristic does not belong to this service."; | 235 VLOG(2) << "Remote GATT characteristic does not belong to this service."; |
| 250 return; | 236 return; |
| 251 } | 237 } |
| 252 | 238 |
| 253 VLOG(1) << "Adding new remote GATT characteristic for GATT service: " | 239 VLOG(1) << "Adding new remote GATT characteristic for GATT service: " |
| 254 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); | 240 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
| 255 | 241 |
| 256 BluetoothRemoteGattCharacteristicChromeOS* characteristic = | 242 BluetoothRemoteGattCharacteristicChromeOS* characteristic = |
| 257 new BluetoothRemoteGattCharacteristicChromeOS(this, object_path); | 243 new BluetoothRemoteGattCharacteristicChromeOS(this, object_path); |
| 258 characteristics_[object_path] = characteristic; | 244 characteristics_[object_path] = characteristic; |
| 259 DCHECK(characteristic->GetIdentifier() == object_path.value()); | 245 DCHECK(characteristic->GetIdentifier() == object_path.value()); |
| 260 DCHECK(characteristic->GetUUID().IsValid()); | 246 DCHECK(characteristic->GetUUID().IsValid()); |
| 261 | 247 |
| 262 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, | 248 DCHECK(adapter_); |
| 263 GattCharacteristicAdded(this, characteristic)); | 249 adapter_->NotifyGattCharacteristicAdded(characteristic); |
| 264 } | 250 } |
| 265 | 251 |
| 266 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicRemoved( | 252 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicRemoved( |
| 267 const dbus::ObjectPath& object_path) { | 253 const dbus::ObjectPath& object_path) { |
| 268 CharacteristicMap::iterator iter = characteristics_.find(object_path); | 254 CharacteristicMap::iterator iter = characteristics_.find(object_path); |
| 269 if (iter == characteristics_.end()) { | 255 if (iter == characteristics_.end()) { |
| 270 VLOG(2) << "Unknown GATT characteristic removed: " << object_path.value(); | 256 VLOG(2) << "Unknown GATT characteristic removed: " << object_path.value(); |
| 271 return; | 257 return; |
| 272 } | 258 } |
| 273 | 259 |
| 274 VLOG(1) << "Removing remote GATT characteristic from service: " | 260 VLOG(1) << "Removing remote GATT characteristic from service: " |
| 275 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); | 261 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
| 276 | 262 |
| 277 BluetoothRemoteGattCharacteristicChromeOS* characteristic = iter->second; | 263 BluetoothRemoteGattCharacteristicChromeOS* characteristic = iter->second; |
| 278 DCHECK(characteristic->object_path() == object_path); | 264 DCHECK(characteristic->object_path() == object_path); |
| 279 characteristics_.erase(iter); | 265 characteristics_.erase(iter); |
| 280 | 266 |
| 281 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, | 267 DCHECK(adapter_); |
| 282 GattCharacteristicRemoved(this, characteristic)); | 268 adapter_->NotifyGattCharacteristicRemoved(characteristic); |
| 283 | 269 |
| 284 delete characteristic; | 270 delete characteristic; |
| 285 } | 271 } |
| 286 | 272 |
| 287 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicPropertyChanged( | 273 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicPropertyChanged( |
| 288 const dbus::ObjectPath& object_path, | 274 const dbus::ObjectPath& object_path, |
| 289 const std::string& property_name) { | 275 const std::string& property_name) { |
| 290 if (characteristics_.find(object_path) == characteristics_.end()) { | 276 if (characteristics_.find(object_path) == characteristics_.end()) { |
| 291 VLOG(3) << "Properties of unknown characteristic changed"; | 277 VLOG(3) << "Properties of unknown characteristic changed"; |
| 292 return; | 278 return; |
| 293 } | 279 } |
| 294 | 280 |
| 295 // We may receive a property changed event in certain cases, e.g. when the | 281 // We may receive a property changed event in certain cases, e.g. when the |
| 296 // characteristic "Flags" property has been updated with values from the | 282 // characteristic "Flags" property has been updated with values from the |
| 297 // "Characteristic Extended Properties" descriptor. In this case, kick off | 283 // "Characteristic Extended Properties" descriptor. In this case, kick off |
| 298 // a service changed observer event to let observers refresh the | 284 // a service changed observer event to let observers refresh the |
| 299 // characteristics. | 285 // characteristics. |
| 300 BluetoothGattCharacteristicClient::Properties* properties = | 286 BluetoothGattCharacteristicClient::Properties* properties = |
| 301 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | 287 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 302 GetProperties(object_path); | 288 GetProperties(object_path); |
| 303 DCHECK(properties); | 289 DCHECK(properties); |
| 304 if (property_name != properties->flags.name()) | 290 if (property_name != properties->flags.name()) |
| 305 return; | 291 return; |
| 306 | 292 |
| 307 NotifyServiceChanged(); | 293 NotifyServiceChanged(); |
| 308 } | 294 } |
| 309 | 295 |
| 310 } // namespace chromeos | 296 } // namespace chromeos |
| OLD | NEW |