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 "chromeos/dbus/bluetooth_gatt_service_client.h" | 9 #include "chromeos/dbus/bluetooth_gatt_service_client.h" |
9 #include "chromeos/dbus/dbus_thread_manager.h" | 10 #include "chromeos/dbus/dbus_thread_manager.h" |
| 11 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" |
10 | 12 |
11 namespace chromeos { | 13 namespace chromeos { |
12 | 14 |
| 15 namespace { |
| 16 |
| 17 // Stream operator for logging vector<uint8>. |
| 18 std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) { |
| 19 out << "["; |
| 20 for (std::vector<uint8>::const_iterator iter = bytes.begin(); |
| 21 iter != bytes.end(); ++iter) { |
| 22 out << base::StringPrintf("%02X", *iter); |
| 23 } |
| 24 return out << "]"; |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
13 BluetoothRemoteGattServiceChromeOS::BluetoothRemoteGattServiceChromeOS( | 29 BluetoothRemoteGattServiceChromeOS::BluetoothRemoteGattServiceChromeOS( |
14 BluetoothDeviceChromeOS* device, | 30 BluetoothDeviceChromeOS* device, |
15 const dbus::ObjectPath& object_path) | 31 const dbus::ObjectPath& object_path) |
16 : object_path_(object_path), | 32 : object_path_(object_path), |
17 device_(device), | 33 device_(device), |
18 weak_ptr_factory_(this) { | 34 weak_ptr_factory_(this) { |
19 VLOG(1) << "Creating remote GATT service with identifier: " | 35 VLOG(1) << "Creating remote GATT service with identifier: " |
20 << object_path.value() << ", UUID: " << GetUUID().canonical_value(); | 36 << object_path.value() << ", UUID: " << GetUUID().canonical_value(); |
21 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->AddObserver(this); | 37 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->AddObserver(this); |
| 38 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 39 AddObserver(this); |
| 40 |
| 41 // Add all known GATT characteristics. |
| 42 const std::vector<dbus::ObjectPath> gatt_chars = |
| 43 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 44 GetCharacteristics(); |
| 45 for (std::vector<dbus::ObjectPath>::const_iterator iter = gatt_chars.begin(); |
| 46 iter != gatt_chars.end(); ++iter) |
| 47 GattCharacteristicAdded(*iter); |
22 } | 48 } |
23 | 49 |
24 BluetoothRemoteGattServiceChromeOS::~BluetoothRemoteGattServiceChromeOS() { | 50 BluetoothRemoteGattServiceChromeOS::~BluetoothRemoteGattServiceChromeOS() { |
25 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> | 51 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> |
26 RemoveObserver(this); | 52 RemoveObserver(this); |
| 53 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 54 RemoveObserver(this); |
| 55 |
| 56 // Clean up all the characteristics. Copy the characteristics list here and |
| 57 // clear the original so that when we send GattCharacteristicRemoved(), |
| 58 // GetCharacteristics() returns no characteristics. |
| 59 CharacteristicMap characteristics = characteristics_; |
| 60 characteristics_.clear(); |
| 61 for (CharacteristicMap::iterator iter = characteristics.begin(); |
| 62 iter != characteristics.end(); ++iter) { |
| 63 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
| 64 GattCharacteristicRemoved(this, iter->second)); |
| 65 delete iter->second; |
| 66 } |
| 67 } |
| 68 |
| 69 void BluetoothRemoteGattServiceChromeOS::AddObserver( |
| 70 device::BluetoothGattService::Observer* observer) { |
| 71 DCHECK(observer); |
| 72 observers_.AddObserver(observer); |
| 73 } |
| 74 |
| 75 void BluetoothRemoteGattServiceChromeOS::RemoveObserver( |
| 76 device::BluetoothGattService::Observer* observer) { |
| 77 DCHECK(observer); |
| 78 observers_.RemoveObserver(observer); |
27 } | 79 } |
28 | 80 |
29 std::string BluetoothRemoteGattServiceChromeOS::GetIdentifier() const { | 81 std::string BluetoothRemoteGattServiceChromeOS::GetIdentifier() const { |
30 return object_path_.value(); | 82 return object_path_.value(); |
31 } | 83 } |
32 | 84 |
33 device::BluetoothUUID BluetoothRemoteGattServiceChromeOS::GetUUID() const { | 85 device::BluetoothUUID BluetoothRemoteGattServiceChromeOS::GetUUID() const { |
34 BluetoothGattServiceClient::Properties* properties = | 86 BluetoothGattServiceClient::Properties* properties = |
35 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> | 87 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> |
36 GetProperties(object_path_); | 88 GetProperties(object_path_); |
37 DCHECK(properties); | 89 DCHECK(properties); |
38 return device::BluetoothUUID(properties->uuid.value()); | 90 return device::BluetoothUUID(properties->uuid.value()); |
39 } | 91 } |
40 | 92 |
41 bool BluetoothRemoteGattServiceChromeOS::IsLocal() const { | 93 bool BluetoothRemoteGattServiceChromeOS::IsLocal() const { |
42 return false; | 94 return false; |
43 } | 95 } |
44 | 96 |
45 bool BluetoothRemoteGattServiceChromeOS::IsPrimary() const { | 97 bool BluetoothRemoteGattServiceChromeOS::IsPrimary() const { |
46 BluetoothGattServiceClient::Properties* properties = | 98 BluetoothGattServiceClient::Properties* properties = |
47 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> | 99 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> |
48 GetProperties(object_path_); | 100 GetProperties(object_path_); |
49 DCHECK(properties); | 101 DCHECK(properties); |
50 return properties->primary.value(); | 102 return properties->primary.value(); |
51 } | 103 } |
52 | 104 |
53 const std::vector<device::BluetoothGattCharacteristic*>& | 105 std::vector<device::BluetoothGattCharacteristic*> |
54 BluetoothRemoteGattServiceChromeOS::GetCharacteristics() const { | 106 BluetoothRemoteGattServiceChromeOS::GetCharacteristics() const { |
55 return characteristics_; | 107 std::vector<device::BluetoothGattCharacteristic*> characteristics; |
| 108 for (CharacteristicMap::const_iterator iter = characteristics_.begin(); |
| 109 iter != characteristics_.end(); ++iter) { |
| 110 characteristics.push_back(iter->second); |
| 111 } |
| 112 return characteristics; |
56 } | 113 } |
57 | 114 |
58 const std::vector<device::BluetoothGattService*>& | 115 std::vector<device::BluetoothGattService*> |
59 BluetoothRemoteGattServiceChromeOS::GetIncludedServices() const { | 116 BluetoothRemoteGattServiceChromeOS::GetIncludedServices() const { |
60 return includes_; | 117 // TODO(armansito): Return the actual included services here. |
| 118 return std::vector<device::BluetoothGattService*>(); |
61 } | 119 } |
62 | 120 |
63 void BluetoothRemoteGattServiceChromeOS::AddObserver( | 121 device::BluetoothGattCharacteristic* |
64 device::BluetoothGattService::Observer* observer) { | 122 BluetoothRemoteGattServiceChromeOS::GetCharacteristic( |
65 DCHECK(observer); | 123 const std::string& identifier) { |
66 observers_.AddObserver(observer); | 124 CharacteristicMap::const_iterator iter = |
67 } | 125 characteristics_.find(dbus::ObjectPath(identifier)); |
68 | 126 if (iter == characteristics_.end()) |
69 void BluetoothRemoteGattServiceChromeOS::RemoveObserver( | 127 return NULL; |
70 device::BluetoothGattService::Observer* observer) { | 128 return iter->second; |
71 DCHECK(observer); | |
72 observers_.RemoveObserver(observer); | |
73 } | 129 } |
74 | 130 |
75 bool BluetoothRemoteGattServiceChromeOS::AddCharacteristic( | 131 bool BluetoothRemoteGattServiceChromeOS::AddCharacteristic( |
76 device::BluetoothGattCharacteristic* characteristic) { | 132 device::BluetoothGattCharacteristic* characteristic) { |
77 VLOG(1) << "Characteristics cannot be added to a remote GATT service."; | 133 VLOG(1) << "Characteristics cannot be added to a remote GATT service."; |
78 return false; | 134 return false; |
79 } | 135 } |
80 | 136 |
81 bool BluetoothRemoteGattServiceChromeOS::AddIncludedService( | 137 bool BluetoothRemoteGattServiceChromeOS::AddIncludedService( |
82 device::BluetoothGattService* service) { | 138 device::BluetoothGattService* service) { |
(...skipping 15 matching lines...) Expand all Loading... |
98 error_callback.Run(); | 154 error_callback.Run(); |
99 } | 155 } |
100 | 156 |
101 void BluetoothRemoteGattServiceChromeOS::GattServicePropertyChanged( | 157 void BluetoothRemoteGattServiceChromeOS::GattServicePropertyChanged( |
102 const dbus::ObjectPath& object_path, | 158 const dbus::ObjectPath& object_path, |
103 const std::string& property_name){ | 159 const std::string& property_name){ |
104 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, | 160 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
105 GattServiceChanged(this)); | 161 GattServiceChanged(this)); |
106 } | 162 } |
107 | 163 |
| 164 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicAdded( |
| 165 const dbus::ObjectPath& object_path) { |
| 166 if (characteristics_.find(object_path) != characteristics_.end()) { |
| 167 VLOG(1) << "Remote GATT characteristic already exists: " |
| 168 << object_path.value(); |
| 169 return; |
| 170 } |
| 171 |
| 172 BluetoothGattCharacteristicClient::Properties* properties = |
| 173 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 174 GetProperties(object_path); |
| 175 DCHECK(properties); |
| 176 if (properties->service.value() != object_path_) { |
| 177 VLOG(2) << "Remote GATT characteristic does not belong to this service."; |
| 178 return; |
| 179 } |
| 180 |
| 181 VLOG(1) << "Adding new remote GATT characteristic for GATT service: " |
| 182 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
| 183 |
| 184 BluetoothRemoteGattCharacteristicChromeOS* characteristic = |
| 185 new BluetoothRemoteGattCharacteristicChromeOS(this, object_path); |
| 186 characteristics_[object_path] = characteristic; |
| 187 DCHECK(characteristic->GetIdentifier() == object_path.value()); |
| 188 DCHECK(characteristic->GetUUID().IsValid()); |
| 189 |
| 190 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
| 191 GattCharacteristicAdded(this, characteristic)); |
| 192 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
| 193 GattServiceChanged(this)); |
| 194 } |
| 195 |
| 196 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicRemoved( |
| 197 const dbus::ObjectPath& object_path) { |
| 198 CharacteristicMap::iterator iter = characteristics_.find(object_path); |
| 199 if (iter == characteristics_.end()) { |
| 200 VLOG(2) << "Unknown GATT characteristic removed: " << object_path.value(); |
| 201 return; |
| 202 } |
| 203 |
| 204 VLOG(1) << "Removing remote GATT characteristic from service: " |
| 205 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
| 206 |
| 207 BluetoothRemoteGattCharacteristicChromeOS* characteristic = iter->second; |
| 208 DCHECK(characteristic->object_path() == object_path); |
| 209 characteristics_.erase(iter); |
| 210 |
| 211 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
| 212 GattCharacteristicRemoved(this, characteristic)); |
| 213 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
| 214 GattServiceChanged(this)); |
| 215 |
| 216 delete characteristic; |
| 217 } |
| 218 |
| 219 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicPropertyChanged( |
| 220 const dbus::ObjectPath& object_path, |
| 221 const std::string& property_name) { |
| 222 CharacteristicMap::iterator iter = characteristics_.find(object_path); |
| 223 if (iter == characteristics_.end()) { |
| 224 VLOG(2) << "Unknown GATT characteristic property changed: " |
| 225 << object_path.value(); |
| 226 return; |
| 227 } |
| 228 |
| 229 // Ignore all property changes except for "Value". |
| 230 BluetoothGattCharacteristicClient::Properties* properties = |
| 231 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 232 GetProperties(object_path); |
| 233 DCHECK(properties); |
| 234 if (property_name != properties->value.name()) |
| 235 return; |
| 236 |
| 237 VLOG(1) << "GATT characteristic value has changed: " << object_path.value() |
| 238 << ": " << properties->value.value(); |
| 239 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
| 240 GattCharacteristicValueChanged(this, iter->second, |
| 241 properties->value.value())); |
| 242 } |
| 243 |
108 } // namespace chromeos | 244 } // namespace chromeos |
OLD | NEW |