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