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