| 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_characteristic_client.h" | 5 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 12 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h" | 12 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h" |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" | 13 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 14 | 14 |
| 15 namespace chromeos { | 15 namespace chromeos { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const int kStartNotifyResponseIntervalMs = 200; | 19 const int kStartNotifyResponseIntervalMs = 200; |
| 20 const int kHeartRateMeasurementNotificationIntervalMs = 2000; | 20 const int kHeartRateMeasurementNotificationIntervalMs = 2000; |
| 21 | 21 |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 FakeBluetoothGattCharacteristicClient::DelayedCallback::DelayedCallback( |
| 25 base::Closure callback, |
| 26 size_t delay) |
| 27 : callback_(callback), delay_(delay) { |
| 28 } |
| 29 |
| 30 FakeBluetoothGattCharacteristicClient::DelayedCallback::~DelayedCallback() { |
| 31 } |
| 32 |
| 24 // static | 33 // static |
| 25 const char FakeBluetoothGattCharacteristicClient:: | 34 const char FakeBluetoothGattCharacteristicClient:: |
| 26 kHeartRateMeasurementPathComponent[] = "char0000"; | 35 kHeartRateMeasurementPathComponent[] = "char0000"; |
| 27 const char FakeBluetoothGattCharacteristicClient:: | 36 const char FakeBluetoothGattCharacteristicClient:: |
| 28 kBodySensorLocationPathComponent[] = "char0001"; | 37 kBodySensorLocationPathComponent[] = "char0001"; |
| 29 const char FakeBluetoothGattCharacteristicClient:: | 38 const char FakeBluetoothGattCharacteristicClient:: |
| 30 kHeartRateControlPointPathComponent[] = "char0002"; | 39 kHeartRateControlPointPathComponent[] = "char0002"; |
| 31 | 40 |
| 32 // static | 41 // static |
| 33 const char FakeBluetoothGattCharacteristicClient::kHeartRateMeasurementUUID[] = | 42 const char FakeBluetoothGattCharacteristicClient::kHeartRateMeasurementUUID[] = |
| (...skipping 27 matching lines...) Expand all Loading... |
| 61 | 70 |
| 62 void FakeBluetoothGattCharacteristicClient::Properties::Set( | 71 void FakeBluetoothGattCharacteristicClient::Properties::Set( |
| 63 dbus::PropertyBase* property, | 72 dbus::PropertyBase* property, |
| 64 dbus::PropertySet::SetCallback callback) { | 73 dbus::PropertySet::SetCallback callback) { |
| 65 VLOG(1) << "Set " << property->name(); | 74 VLOG(1) << "Set " << property->name(); |
| 66 callback.Run(false); | 75 callback.Run(false); |
| 67 } | 76 } |
| 68 | 77 |
| 69 FakeBluetoothGattCharacteristicClient::FakeBluetoothGattCharacteristicClient() | 78 FakeBluetoothGattCharacteristicClient::FakeBluetoothGattCharacteristicClient() |
| 70 : heart_rate_visible_(false), | 79 : heart_rate_visible_(false), |
| 80 authorized_(true), |
| 81 authenticated_(true), |
| 71 calories_burned_(0), | 82 calories_burned_(0), |
| 83 extra_requests_(0), |
| 72 weak_ptr_factory_(this) { | 84 weak_ptr_factory_(this) { |
| 73 } | 85 } |
| 74 | 86 |
| 75 FakeBluetoothGattCharacteristicClient:: | 87 FakeBluetoothGattCharacteristicClient:: |
| 76 ~FakeBluetoothGattCharacteristicClient() { | 88 ~FakeBluetoothGattCharacteristicClient() { |
| 89 for (const auto& it : action_extra_requests_) { |
| 90 delete it.second; |
| 91 } |
| 92 action_extra_requests_.clear(); |
| 77 } | 93 } |
| 78 | 94 |
| 79 void FakeBluetoothGattCharacteristicClient::Init(dbus::Bus* bus) { | 95 void FakeBluetoothGattCharacteristicClient::Init(dbus::Bus* bus) { |
| 80 } | 96 } |
| 81 | 97 |
| 82 void FakeBluetoothGattCharacteristicClient::AddObserver(Observer* observer) { | 98 void FakeBluetoothGattCharacteristicClient::AddObserver(Observer* observer) { |
| 83 observers_.AddObserver(observer); | 99 observers_.AddObserver(observer); |
| 84 } | 100 } |
| 85 | 101 |
| 86 void FakeBluetoothGattCharacteristicClient::RemoveObserver(Observer* observer) { | 102 void FakeBluetoothGattCharacteristicClient::RemoveObserver(Observer* observer) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 113 DCHECK(heart_rate_control_point_properties_.get()); | 129 DCHECK(heart_rate_control_point_properties_.get()); |
| 114 return heart_rate_control_point_properties_.get(); | 130 return heart_rate_control_point_properties_.get(); |
| 115 } | 131 } |
| 116 return NULL; | 132 return NULL; |
| 117 } | 133 } |
| 118 | 134 |
| 119 void FakeBluetoothGattCharacteristicClient::ReadValue( | 135 void FakeBluetoothGattCharacteristicClient::ReadValue( |
| 120 const dbus::ObjectPath& object_path, | 136 const dbus::ObjectPath& object_path, |
| 121 const ValueCallback& callback, | 137 const ValueCallback& callback, |
| 122 const ErrorCallback& error_callback) { | 138 const ErrorCallback& error_callback) { |
| 123 if (!IsHeartRateVisible()) { | 139 if (!authenticated_) { |
| 124 error_callback.Run(kUnknownCharacteristicError, ""); | 140 error_callback.Run("org.bluez.Error.NotPaired", "Please login"); |
| 125 return; | 141 return; |
| 126 } | 142 } |
| 127 | 143 |
| 128 if (object_path.value() == heart_rate_measurement_path_ || | 144 if (!authorized_) { |
| 129 object_path.value() == heart_rate_control_point_path_) { | 145 error_callback.Run("org.bluez.Error.NotAuthorized", "Authorize first"); |
| 146 return; |
| 147 } |
| 148 |
| 149 if (object_path.value() == heart_rate_control_point_path_) { |
| 130 error_callback.Run("org.bluez.Error.ReadNotPermitted", | 150 error_callback.Run("org.bluez.Error.ReadNotPermitted", |
| 131 "Reads of this value are not allowed"); | 151 "Reads of this value are not allowed"); |
| 132 return; | 152 return; |
| 133 } | 153 } |
| 134 | 154 |
| 155 if (object_path.value() == heart_rate_measurement_path_) { |
| 156 error_callback.Run("org.bluez.Error.NotSupported", |
| 157 "Action not supported on this characteristic"); |
| 158 return; |
| 159 } |
| 160 |
| 135 if (object_path.value() != body_sensor_location_path_) { | 161 if (object_path.value() != body_sensor_location_path_) { |
| 136 error_callback.Run(kUnknownCharacteristicError, ""); | 162 error_callback.Run(kUnknownCharacteristicError, ""); |
| 137 return; | 163 return; |
| 138 } | 164 } |
| 139 | 165 |
| 140 std::vector<uint8> value; | 166 if (action_extra_requests_.find("ReadValue") != |
| 141 value.push_back(0x06); // Location is "foot". | 167 action_extra_requests_.end()) { |
| 142 callback.Run(value); | 168 DelayedCallback* delayed = action_extra_requests_["ReadValue"]; |
| 169 delayed->delay_--; |
| 170 error_callback.Run("org.bluez.Error.InProgress", |
| 171 "Another read is currenty in progress"); |
| 172 if (delayed->delay_ == 0) { |
| 173 delayed->callback_.Run(); |
| 174 action_extra_requests_.erase("ReadValue"); |
| 175 delete delayed; |
| 176 } |
| 177 return; |
| 178 } |
| 179 base::Closure completed_callback; |
| 180 if (!IsHeartRateVisible()) { |
| 181 completed_callback = |
| 182 base::Bind(error_callback, kUnknownCharacteristicError, ""); |
| 183 } else { |
| 184 std::vector<uint8> value; |
| 185 value.push_back(0x06); // Location is "foot". |
| 186 completed_callback = base::Bind(callback, value); |
| 187 } |
| 188 |
| 189 if (extra_requests_ > 0) { |
| 190 action_extra_requests_["ReadValue"] = |
| 191 new DelayedCallback(completed_callback, extra_requests_); |
| 192 return; |
| 193 } |
| 194 completed_callback.Run(); |
| 143 } | 195 } |
| 144 | 196 |
| 145 void FakeBluetoothGattCharacteristicClient::WriteValue( | 197 void FakeBluetoothGattCharacteristicClient::WriteValue( |
| 146 const dbus::ObjectPath& object_path, | 198 const dbus::ObjectPath& object_path, |
| 147 const std::vector<uint8>& value, | 199 const std::vector<uint8>& value, |
| 148 const base::Closure& callback, | 200 const base::Closure& callback, |
| 149 const ErrorCallback& error_callback) { | 201 const ErrorCallback& error_callback) { |
| 202 if (!authenticated_) { |
| 203 error_callback.Run("org.bluez.Error.NotPaired", "Please login"); |
| 204 return; |
| 205 } |
| 206 |
| 207 if (!authorized_) { |
| 208 error_callback.Run("org.bluez.Error.NotAuthorized", "Authorize first"); |
| 209 return; |
| 210 } |
| 211 |
| 150 if (!IsHeartRateVisible()) { | 212 if (!IsHeartRateVisible()) { |
| 151 error_callback.Run(kUnknownCharacteristicError, ""); | 213 error_callback.Run(kUnknownCharacteristicError, ""); |
| 152 return; | 214 return; |
| 153 } | 215 } |
| 154 | 216 |
| 217 if (object_path.value() == heart_rate_measurement_path_) { |
| 218 error_callback.Run("org.bluez.Error.NotSupported", |
| 219 "Action not supported on this characteristic"); |
| 220 return; |
| 221 } |
| 222 |
| 155 if (object_path.value() != heart_rate_control_point_path_) { | 223 if (object_path.value() != heart_rate_control_point_path_) { |
| 156 error_callback.Run("org.bluez.Error.WriteNotPermitted", | 224 error_callback.Run("org.bluez.Error.WriteNotPermitted", |
| 157 "Writes of this value are not allowed"); | 225 "Writes of this value are not allowed"); |
| 158 return; | 226 return; |
| 159 } | 227 } |
| 160 | 228 |
| 161 DCHECK(heart_rate_control_point_properties_.get()); | 229 DCHECK(heart_rate_control_point_properties_.get()); |
| 162 if (value.size() != 1) { | 230 if (action_extra_requests_.find("WriteValue") != |
| 163 error_callback.Run("org.bluez.Error.InvalidValueLength", | 231 action_extra_requests_.end()) { |
| 164 "Invalid length for write"); | 232 DelayedCallback* delayed = action_extra_requests_["WriteValue"]; |
| 233 delayed->delay_--; |
| 234 error_callback.Run("org.bluez.Error.InProgress", |
| 235 "Another write is in progress"); |
| 236 if (delayed->delay_ == 0) { |
| 237 delayed->callback_.Run(); |
| 238 action_extra_requests_.erase("WriteValue"); |
| 239 delete delayed; |
| 240 } |
| 165 return; | 241 return; |
| 166 } | 242 } |
| 167 if (value[0] > 1) { | 243 base::Closure completed_callback; |
| 168 error_callback.Run("org.bluez.Error.Failed", | 244 if (value.size() != 1) { |
| 169 "Invalid value given for write"); | 245 completed_callback = base::Bind(error_callback, |
| 246 "org.bluez.Error.InvalidValueLength", |
| 247 "Invalid length for write"); |
| 248 } else if (value[0] > 1) { |
| 249 completed_callback = base::Bind(error_callback, |
| 250 "org.bluez.Error.Failed", |
| 251 "Invalid value given for write"); |
| 252 } else if (value[0] == 1) { |
| 253 // TODO(jamuraa): make this happen when the callback happens |
| 254 calories_burned_ = 0; |
| 255 completed_callback = callback; |
| 256 } |
| 257 |
| 258 if (extra_requests_ > 0) { |
| 259 action_extra_requests_["WriteValue"] = |
| 260 new DelayedCallback(completed_callback, extra_requests_); |
| 170 return; | 261 return; |
| 171 } | 262 } |
| 172 | 263 completed_callback.Run(); |
| 173 if (value[0] == 1) | |
| 174 calories_burned_ = 0; | |
| 175 | |
| 176 callback.Run(); | |
| 177 } | 264 } |
| 178 | 265 |
| 179 void FakeBluetoothGattCharacteristicClient::StartNotify( | 266 void FakeBluetoothGattCharacteristicClient::StartNotify( |
| 180 const dbus::ObjectPath& object_path, | 267 const dbus::ObjectPath& object_path, |
| 181 const base::Closure& callback, | 268 const base::Closure& callback, |
| 182 const ErrorCallback& error_callback) { | 269 const ErrorCallback& error_callback) { |
| 183 if (!IsHeartRateVisible()) { | 270 if (!IsHeartRateVisible()) { |
| 184 error_callback.Run(kUnknownCharacteristicError, ""); | 271 error_callback.Run(kUnknownCharacteristicError, ""); |
| 185 return; | 272 return; |
| 186 } | 273 } |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 heart_rate_measurement_properties_.reset(); | 412 heart_rate_measurement_properties_.reset(); |
| 326 body_sensor_location_properties_.reset(); | 413 body_sensor_location_properties_.reset(); |
| 327 heart_rate_control_point_properties_.reset(); | 414 heart_rate_control_point_properties_.reset(); |
| 328 | 415 |
| 329 heart_rate_measurement_path_.clear(); | 416 heart_rate_measurement_path_.clear(); |
| 330 body_sensor_location_path_.clear(); | 417 body_sensor_location_path_.clear(); |
| 331 heart_rate_control_point_path_.clear(); | 418 heart_rate_control_point_path_.clear(); |
| 332 heart_rate_visible_ = false; | 419 heart_rate_visible_ = false; |
| 333 } | 420 } |
| 334 | 421 |
| 422 void FakeBluetoothGattCharacteristicClient::SetExtraProcessing( |
| 423 size_t requests) { |
| 424 extra_requests_ = requests; |
| 425 if (extra_requests_ == 0) { |
| 426 for (const auto& it : action_extra_requests_) { |
| 427 it.second->callback_.Run(); |
| 428 delete it.second; |
| 429 } |
| 430 action_extra_requests_.clear(); |
| 431 return; |
| 432 } |
| 433 VLOG(2) << "Requests SLOW now, " << requests << " InProgress errors each."; |
| 434 } |
| 435 |
| 436 size_t FakeBluetoothGattCharacteristicClient::GetExtraProcessing() const { |
| 437 return extra_requests_; |
| 438 } |
| 439 |
| 335 dbus::ObjectPath | 440 dbus::ObjectPath |
| 336 FakeBluetoothGattCharacteristicClient::GetHeartRateMeasurementPath() const { | 441 FakeBluetoothGattCharacteristicClient::GetHeartRateMeasurementPath() const { |
| 337 return dbus::ObjectPath(heart_rate_measurement_path_); | 442 return dbus::ObjectPath(heart_rate_measurement_path_); |
| 338 } | 443 } |
| 339 | 444 |
| 340 dbus::ObjectPath | 445 dbus::ObjectPath |
| 341 FakeBluetoothGattCharacteristicClient::GetBodySensorLocationPath() const { | 446 FakeBluetoothGattCharacteristicClient::GetBodySensorLocationPath() const { |
| 342 return dbus::ObjectPath(body_sensor_location_path_); | 447 return dbus::ObjectPath(body_sensor_location_path_); |
| 343 } | 448 } |
| 344 | 449 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); | 550 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); |
| 446 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); | 551 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); |
| 447 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); | 552 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); |
| 448 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); | 553 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); |
| 449 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); | 554 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); |
| 450 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); | 555 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); |
| 451 return heart_rate_visible_; | 556 return heart_rate_visible_; |
| 452 } | 557 } |
| 453 | 558 |
| 454 } // namespace chromeos | 559 } // namespace chromeos |
| OLD | NEW |