| 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 "chromeos/dbus/fake_bluetooth_gatt_service_client.h" | 5 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/time/time.h" |
| 8 #include "chromeos/dbus/dbus_thread_manager.h" | 10 #include "chromeos/dbus/dbus_thread_manager.h" |
| 9 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h" | 11 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h" |
| 10 #include "third_party/cros_system_api/dbus/service_constants.h" | 12 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 11 | 13 |
| 12 namespace chromeos { | 14 namespace chromeos { |
| 13 | 15 |
| 16 namespace { |
| 17 |
| 18 const int kExposeCharacteristicsDelayIntervalMs = 100; |
| 19 |
| 20 } // namespace |
| 21 |
| 14 // static | 22 // static |
| 15 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent[] = | 23 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent[] = |
| 16 "service0000"; | 24 "service0000"; |
| 17 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID[] = | 25 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID[] = |
| 18 "0000180d-0000-1000-8000-00805f9b34fb"; | 26 "0000180d-0000-1000-8000-00805f9b34fb"; |
| 19 | 27 |
| 20 FakeBluetoothGattServiceClient::Properties::Properties( | 28 FakeBluetoothGattServiceClient::Properties::Properties( |
| 21 const PropertyChangedCallback& callback) | 29 const PropertyChangedCallback& callback) |
| 22 : BluetoothGattServiceClient::Properties( | 30 : BluetoothGattServiceClient::Properties( |
| 23 NULL, | 31 NULL, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 39 VLOG(1) << "GetAll"; | 47 VLOG(1) << "GetAll"; |
| 40 } | 48 } |
| 41 | 49 |
| 42 void FakeBluetoothGattServiceClient::Properties::Set( | 50 void FakeBluetoothGattServiceClient::Properties::Set( |
| 43 dbus::PropertyBase* property, | 51 dbus::PropertyBase* property, |
| 44 dbus::PropertySet::GetCallback callback) { | 52 dbus::PropertySet::GetCallback callback) { |
| 45 VLOG(1) << "Set " << property->name(); | 53 VLOG(1) << "Set " << property->name(); |
| 46 callback.Run(false); | 54 callback.Run(false); |
| 47 } | 55 } |
| 48 | 56 |
| 49 FakeBluetoothGattServiceClient::FakeBluetoothGattServiceClient() { | 57 FakeBluetoothGattServiceClient::FakeBluetoothGattServiceClient() |
| 58 : weak_ptr_factory_(this) { |
| 50 } | 59 } |
| 51 | 60 |
| 52 FakeBluetoothGattServiceClient::~FakeBluetoothGattServiceClient() { | 61 FakeBluetoothGattServiceClient::~FakeBluetoothGattServiceClient() { |
| 53 } | 62 } |
| 54 | 63 |
| 55 void FakeBluetoothGattServiceClient::Init(dbus::Bus* bus) { | 64 void FakeBluetoothGattServiceClient::Init(dbus::Bus* bus) { |
| 56 } | 65 } |
| 57 | 66 |
| 58 void FakeBluetoothGattServiceClient::AddObserver(Observer* observer) { | 67 void FakeBluetoothGattServiceClient::AddObserver(Observer* observer) { |
| 59 observers_.AddObserver(observer); | 68 observers_.AddObserver(observer); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 75 FakeBluetoothGattServiceClient::Properties* | 84 FakeBluetoothGattServiceClient::Properties* |
| 76 FakeBluetoothGattServiceClient::GetProperties( | 85 FakeBluetoothGattServiceClient::GetProperties( |
| 77 const dbus::ObjectPath& object_path) { | 86 const dbus::ObjectPath& object_path) { |
| 78 if (object_path.value() == heart_rate_service_path_) | 87 if (object_path.value() == heart_rate_service_path_) |
| 79 return heart_rate_service_properties_.get(); | 88 return heart_rate_service_properties_.get(); |
| 80 return NULL; | 89 return NULL; |
| 81 } | 90 } |
| 82 | 91 |
| 83 void FakeBluetoothGattServiceClient::ExposeHeartRateService( | 92 void FakeBluetoothGattServiceClient::ExposeHeartRateService( |
| 84 const dbus::ObjectPath& device_path) { | 93 const dbus::ObjectPath& device_path) { |
| 85 if (heart_rate_service_properties_.get()) { | 94 if (IsHeartRateVisible()) { |
| 86 DCHECK(!heart_rate_service_path_.empty()); | 95 DCHECK(!heart_rate_service_path_.empty()); |
| 87 VLOG(1) << "Fake Heart Rate Service already exposed."; | 96 VLOG(1) << "Fake Heart Rate Service already exposed."; |
| 88 return; | 97 return; |
| 89 } | 98 } |
| 90 VLOG(2) << "Exposing fake Heart Rate Service."; | 99 VLOG(2) << "Exposing fake Heart Rate Service."; |
| 91 heart_rate_service_path_ = | 100 heart_rate_service_path_ = |
| 92 device_path.value() + "/" + kHeartRateServicePathComponent; | 101 device_path.value() + "/" + kHeartRateServicePathComponent; |
| 93 heart_rate_service_properties_.reset(new Properties(base::Bind( | 102 heart_rate_service_properties_.reset(new Properties(base::Bind( |
| 94 &FakeBluetoothGattServiceClient::OnPropertyChanged, | 103 &FakeBluetoothGattServiceClient::OnPropertyChanged, |
| 95 base::Unretained(this), | 104 base::Unretained(this), |
| 96 dbus::ObjectPath(heart_rate_service_path_)))); | 105 dbus::ObjectPath(heart_rate_service_path_)))); |
| 97 heart_rate_service_properties_->uuid.ReplaceValue(kHeartRateServiceUUID); | 106 heart_rate_service_properties_->uuid.ReplaceValue(kHeartRateServiceUUID); |
| 98 heart_rate_service_properties_->device.ReplaceValue(device_path); | 107 heart_rate_service_properties_->device.ReplaceValue(device_path); |
| 99 heart_rate_service_properties_->primary.ReplaceValue(true); | 108 heart_rate_service_properties_->primary.ReplaceValue(true); |
| 100 | 109 |
| 101 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); | 110 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); |
| 102 | 111 |
| 103 FakeBluetoothGattCharacteristicClient* char_client = | 112 base::MessageLoop::current()->PostDelayedTask( |
| 104 static_cast<FakeBluetoothGattCharacteristicClient*>( | 113 FROM_HERE, |
| 105 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()); | 114 base::Bind( |
| 106 char_client->ExposeHeartRateCharacteristics( | 115 &FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics, |
| 107 dbus::ObjectPath(heart_rate_service_path_)); | 116 weak_ptr_factory_.GetWeakPtr()), |
| 117 base::TimeDelta::FromMilliseconds( |
| 118 kExposeCharacteristicsDelayIntervalMs)); |
| 108 } | 119 } |
| 109 | 120 |
| 110 void FakeBluetoothGattServiceClient::HideHeartRateService() { | 121 void FakeBluetoothGattServiceClient::HideHeartRateService() { |
| 111 if (!heart_rate_service_properties_.get()) { | 122 if (!IsHeartRateVisible()) { |
| 112 DCHECK(heart_rate_service_path_.empty()); | 123 DCHECK(heart_rate_service_path_.empty()); |
| 113 VLOG(1) << "Fake Heart Rate Service already hidden."; | 124 VLOG(1) << "Fake Heart Rate Service already hidden."; |
| 114 return; | 125 return; |
| 115 } | 126 } |
| 116 VLOG(2) << "Hiding fake Heart Rate Service."; | 127 VLOG(2) << "Hiding fake Heart Rate Service."; |
| 117 FakeBluetoothGattCharacteristicClient* char_client = | 128 FakeBluetoothGattCharacteristicClient* char_client = |
| 118 static_cast<FakeBluetoothGattCharacteristicClient*>( | 129 static_cast<FakeBluetoothGattCharacteristicClient*>( |
| 119 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()); | 130 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()); |
| 120 char_client->HideHeartRateCharacteristics(); | 131 char_client->HideHeartRateCharacteristics(); |
| 121 | 132 |
| 122 // Notify observers before deleting the properties structure so that it | 133 // Notify observers before deleting the properties structure so that it |
| 123 // can be accessed from the observer method. | 134 // can be accessed from the observer method. |
| 124 NotifyServiceRemoved(dbus::ObjectPath(heart_rate_service_path_)); | 135 NotifyServiceRemoved(dbus::ObjectPath(heart_rate_service_path_)); |
| 125 | 136 |
| 126 heart_rate_service_properties_.reset(); | 137 heart_rate_service_properties_.reset(); |
| 127 heart_rate_service_path_.clear(); | 138 heart_rate_service_path_.clear(); |
| 128 } | 139 } |
| 129 | 140 |
| 141 bool FakeBluetoothGattServiceClient::IsHeartRateVisible() const { |
| 142 return !!heart_rate_service_properties_.get(); |
| 143 } |
| 144 |
| 145 dbus::ObjectPath |
| 146 FakeBluetoothGattServiceClient::GetHeartRateServicePath() const { |
| 147 return dbus::ObjectPath(heart_rate_service_path_); |
| 148 } |
| 149 |
| 130 void FakeBluetoothGattServiceClient::OnPropertyChanged( | 150 void FakeBluetoothGattServiceClient::OnPropertyChanged( |
| 131 const dbus::ObjectPath& object_path, | 151 const dbus::ObjectPath& object_path, |
| 132 const std::string& property_name) { | 152 const std::string& property_name) { |
| 133 VLOG(2) << "Fake GATT Service property changed: " << object_path.value() | 153 VLOG(2) << "Fake GATT Service property changed: " << object_path.value() |
| 134 << ": " << property_name; | 154 << ": " << property_name; |
| 135 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_, | 155 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_, |
| 136 GattServicePropertyChanged(object_path, property_name)); | 156 GattServicePropertyChanged(object_path, property_name)); |
| 137 } | 157 } |
| 138 | 158 |
| 139 void FakeBluetoothGattServiceClient::NotifyServiceAdded( | 159 void FakeBluetoothGattServiceClient::NotifyServiceAdded( |
| 140 const dbus::ObjectPath& object_path) { | 160 const dbus::ObjectPath& object_path) { |
| 141 VLOG(2) << "GATT service added: " << object_path.value(); | 161 VLOG(2) << "GATT service added: " << object_path.value(); |
| 142 FOR_EACH_OBSERVER( | 162 FOR_EACH_OBSERVER( |
| 143 BluetoothGattServiceClient::Observer, observers_, | 163 BluetoothGattServiceClient::Observer, observers_, |
| 144 GattServiceAdded(object_path)); | 164 GattServiceAdded(object_path)); |
| 145 } | 165 } |
| 146 | 166 |
| 147 void FakeBluetoothGattServiceClient::NotifyServiceRemoved( | 167 void FakeBluetoothGattServiceClient::NotifyServiceRemoved( |
| 148 const dbus::ObjectPath& object_path) { | 168 const dbus::ObjectPath& object_path) { |
| 149 VLOG(2) << "GATT service removed: " << object_path.value(); | 169 VLOG(2) << "GATT service removed: " << object_path.value(); |
| 150 FOR_EACH_OBSERVER( | 170 FOR_EACH_OBSERVER( |
| 151 BluetoothGattServiceClient::Observer, observers_, | 171 BluetoothGattServiceClient::Observer, observers_, |
| 152 GattServiceRemoved(object_path)); | 172 GattServiceRemoved(object_path)); |
| 153 } | 173 } |
| 154 | 174 |
| 175 void FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics() { |
| 176 if (!IsHeartRateVisible()) { |
| 177 VLOG(2) << "Heart Rate service not visible. Not exposing characteristics."; |
| 178 return; |
| 179 } |
| 180 FakeBluetoothGattCharacteristicClient* char_client = |
| 181 static_cast<FakeBluetoothGattCharacteristicClient*>( |
| 182 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()); |
| 183 char_client->ExposeHeartRateCharacteristics( |
| 184 dbus::ObjectPath(heart_rate_service_path_)); |
| 185 } |
| 186 |
| 155 } // namespace chromeos | 187 } // namespace chromeos |
| OLD | NEW |