| 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() { |
| 77 } | 89 } |
| 78 | 90 |
| 79 void FakeBluetoothGattCharacteristicClient::Init(dbus::Bus* bus) { | 91 void FakeBluetoothGattCharacteristicClient::Init(dbus::Bus* bus) { |
| 80 } | 92 } |
| 81 | 93 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 DCHECK(heart_rate_control_point_properties_.get()); | 125 DCHECK(heart_rate_control_point_properties_.get()); |
| 114 return heart_rate_control_point_properties_.get(); | 126 return heart_rate_control_point_properties_.get(); |
| 115 } | 127 } |
| 116 return NULL; | 128 return NULL; |
| 117 } | 129 } |
| 118 | 130 |
| 119 void FakeBluetoothGattCharacteristicClient::ReadValue( | 131 void FakeBluetoothGattCharacteristicClient::ReadValue( |
| 120 const dbus::ObjectPath& object_path, | 132 const dbus::ObjectPath& object_path, |
| 121 const ValueCallback& callback, | 133 const ValueCallback& callback, |
| 122 const ErrorCallback& error_callback) { | 134 const ErrorCallback& error_callback) { |
| 123 if (!IsHeartRateVisible()) { | 135 if (!authenticated_) { |
| 124 error_callback.Run(kUnknownCharacteristicError, ""); | 136 error_callback.Run("org.bluez.Error.NotPaired", "Please login"); |
| 125 return; | 137 return; |
| 126 } | 138 } |
| 127 | 139 |
| 128 if (object_path.value() == heart_rate_measurement_path_ || | 140 if (!authorized_) { |
| 129 object_path.value() == heart_rate_control_point_path_) { | 141 error_callback.Run("org.bluez.Error.NotAuthorized", "Authorize first"); |
| 142 return; |
| 143 } |
| 144 |
| 145 if (object_path.value() == heart_rate_control_point_path_) { |
| 130 error_callback.Run("org.bluez.Error.ReadNotPermitted", | 146 error_callback.Run("org.bluez.Error.ReadNotPermitted", |
| 131 "Reads of this value are not allowed"); | 147 "Reads of this value are not allowed"); |
| 132 return; | 148 return; |
| 133 } | 149 } |
| 134 | 150 |
| 151 if (object_path.value() == heart_rate_measurement_path_) { |
| 152 error_callback.Run("org.bluez.Error.NotSupported", |
| 153 "Action not supported on this characteristic"); |
| 154 return; |
| 155 } |
| 156 |
| 135 if (object_path.value() != body_sensor_location_path_) { | 157 if (object_path.value() != body_sensor_location_path_) { |
| 136 error_callback.Run(kUnknownCharacteristicError, ""); | 158 error_callback.Run(kUnknownCharacteristicError, ""); |
| 137 return; | 159 return; |
| 138 } | 160 } |
| 139 | 161 |
| 140 std::vector<uint8> value; | 162 if (action_extra_requests_.find("ReadValue") != |
| 141 value.push_back(0x06); // Location is "foot". | 163 action_extra_requests_.end()) { |
| 142 callback.Run(value); | 164 DelayedCallback* delayed = action_extra_requests_["ReadValue"]; |
| 165 delayed->delay_--; |
| 166 error_callback.Run("org.bluez.Error.InProgress", |
| 167 "Another read is currenty in progress"); |
| 168 if (delayed->delay_ == 0) { |
| 169 delayed->callback_.Run(); |
| 170 action_extra_requests_.erase("ReadValue"); |
| 171 delete delayed; |
| 172 } |
| 173 return; |
| 174 } |
| 175 base::Closure completed_callback; |
| 176 if (!IsHeartRateVisible()) { |
| 177 completed_callback = |
| 178 base::Bind(error_callback, kUnknownCharacteristicError, ""); |
| 179 } else { |
| 180 std::vector<uint8> value; |
| 181 value.push_back(0x06); // Location is "foot". |
| 182 completed_callback = base::Bind(callback, value); |
| 183 } |
| 184 |
| 185 if (extra_requests_ > 0) { |
| 186 action_extra_requests_["ReadValue"] = |
| 187 new DelayedCallback(completed_callback, extra_requests_); |
| 188 return; |
| 189 } |
| 190 completed_callback.Run(); |
| 143 } | 191 } |
| 144 | 192 |
| 145 void FakeBluetoothGattCharacteristicClient::WriteValue( | 193 void FakeBluetoothGattCharacteristicClient::WriteValue( |
| 146 const dbus::ObjectPath& object_path, | 194 const dbus::ObjectPath& object_path, |
| 147 const std::vector<uint8>& value, | 195 const std::vector<uint8>& value, |
| 148 const base::Closure& callback, | 196 const base::Closure& callback, |
| 149 const ErrorCallback& error_callback) { | 197 const ErrorCallback& error_callback) { |
| 198 if (!authenticated_) { |
| 199 error_callback.Run("org.bluez.Error.NotPaired", "Please login"); |
| 200 return; |
| 201 } |
| 202 |
| 203 if (!authorized_) { |
| 204 error_callback.Run("org.bluez.Error.NotAuthorized", "Authorize first"); |
| 205 return; |
| 206 } |
| 207 |
| 150 if (!IsHeartRateVisible()) { | 208 if (!IsHeartRateVisible()) { |
| 151 error_callback.Run(kUnknownCharacteristicError, ""); | 209 error_callback.Run(kUnknownCharacteristicError, ""); |
| 152 return; | 210 return; |
| 153 } | 211 } |
| 154 | 212 |
| 213 if (object_path.value() == heart_rate_measurement_path_) { |
| 214 error_callback.Run("org.bluez.Error.NotSupported", |
| 215 "Action not supported on this characteristic"); |
| 216 return; |
| 217 } |
| 218 |
| 155 if (object_path.value() != heart_rate_control_point_path_) { | 219 if (object_path.value() != heart_rate_control_point_path_) { |
| 156 error_callback.Run("org.bluez.Error.WriteNotPermitted", | 220 error_callback.Run("org.bluez.Error.WriteNotPermitted", |
| 157 "Writes of this value are not allowed"); | 221 "Writes of this value are not allowed"); |
| 158 return; | 222 return; |
| 159 } | 223 } |
| 160 | 224 |
| 161 DCHECK(heart_rate_control_point_properties_.get()); | 225 DCHECK(heart_rate_control_point_properties_.get()); |
| 162 if (value.size() != 1) { | 226 if (action_extra_requests_.find("WriteValue") != |
| 163 error_callback.Run("org.bluez.Error.InvalidValueLength", | 227 action_extra_requests_.end()) { |
| 164 "Invalid length for write"); | 228 DelayedCallback* delayed = action_extra_requests_["WriteValue"]; |
| 229 delayed->delay_--; |
| 230 error_callback.Run("org.bluez.Error.InProgress", |
| 231 "Another write is in progress"); |
| 232 if (delayed->delay_ == 0) { |
| 233 delayed->callback_.Run(); |
| 234 action_extra_requests_.erase("WriteValue"); |
| 235 delete delayed; |
| 236 } |
| 165 return; | 237 return; |
| 166 } | 238 } |
| 167 if (value[0] > 1) { | 239 base::Closure completed_callback; |
| 168 error_callback.Run("org.bluez.Error.Failed", | 240 if (value.size() != 1) { |
| 169 "Invalid value given for write"); | 241 completed_callback = base::Bind(error_callback, |
| 242 "org.bluez.Error.InvalidValueLength", |
| 243 "Invalid length for write"); |
| 244 } else if (value[0] > 1) { |
| 245 completed_callback = base::Bind(error_callback, |
| 246 "org.bluez.Error.Failed", |
| 247 "Invalid value given for write"); |
| 248 } else if (value[0] == 1) { |
| 249 // TODO(jamuraa): make this happen when the callback happens |
| 250 calories_burned_ = 0; |
| 251 completed_callback = callback; |
| 252 } |
| 253 |
| 254 if (extra_requests_ > 0) { |
| 255 action_extra_requests_["WriteValue"] = |
| 256 new DelayedCallback(completed_callback, extra_requests_); |
| 170 return; | 257 return; |
| 171 } | 258 } |
| 172 | 259 completed_callback.Run(); |
| 173 if (value[0] == 1) | |
| 174 calories_burned_ = 0; | |
| 175 | |
| 176 callback.Run(); | |
| 177 } | 260 } |
| 178 | 261 |
| 179 void FakeBluetoothGattCharacteristicClient::StartNotify( | 262 void FakeBluetoothGattCharacteristicClient::StartNotify( |
| 180 const dbus::ObjectPath& object_path, | 263 const dbus::ObjectPath& object_path, |
| 181 const base::Closure& callback, | 264 const base::Closure& callback, |
| 182 const ErrorCallback& error_callback) { | 265 const ErrorCallback& error_callback) { |
| 183 if (!IsHeartRateVisible()) { | 266 if (!IsHeartRateVisible()) { |
| 184 error_callback.Run(kUnknownCharacteristicError, ""); | 267 error_callback.Run(kUnknownCharacteristicError, ""); |
| 185 return; | 268 return; |
| 186 } | 269 } |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 heart_rate_measurement_properties_.reset(); | 408 heart_rate_measurement_properties_.reset(); |
| 326 body_sensor_location_properties_.reset(); | 409 body_sensor_location_properties_.reset(); |
| 327 heart_rate_control_point_properties_.reset(); | 410 heart_rate_control_point_properties_.reset(); |
| 328 | 411 |
| 329 heart_rate_measurement_path_.clear(); | 412 heart_rate_measurement_path_.clear(); |
| 330 body_sensor_location_path_.clear(); | 413 body_sensor_location_path_.clear(); |
| 331 heart_rate_control_point_path_.clear(); | 414 heart_rate_control_point_path_.clear(); |
| 332 heart_rate_visible_ = false; | 415 heart_rate_visible_ = false; |
| 333 } | 416 } |
| 334 | 417 |
| 418 void FakeBluetoothGattCharacteristicClient::SetExtraProcessing( |
| 419 size_t requests) { |
| 420 extra_requests_ = requests; |
| 421 if (extra_requests_ == 0) { |
| 422 for (const auto& it : action_extra_requests_) { |
| 423 it.second->callback_.Run(); |
| 424 delete it.second; |
| 425 } |
| 426 action_extra_requests_.clear(); |
| 427 return; |
| 428 } |
| 429 VLOG(2) << "Requests SLOW now, " << requests << " InProgress errors each."; |
| 430 } |
| 431 |
| 432 size_t FakeBluetoothGattCharacteristicClient::GetExtraProcessing() const { |
| 433 return extra_requests_; |
| 434 } |
| 435 |
| 335 dbus::ObjectPath | 436 dbus::ObjectPath |
| 336 FakeBluetoothGattCharacteristicClient::GetHeartRateMeasurementPath() const { | 437 FakeBluetoothGattCharacteristicClient::GetHeartRateMeasurementPath() const { |
| 337 return dbus::ObjectPath(heart_rate_measurement_path_); | 438 return dbus::ObjectPath(heart_rate_measurement_path_); |
| 338 } | 439 } |
| 339 | 440 |
| 340 dbus::ObjectPath | 441 dbus::ObjectPath |
| 341 FakeBluetoothGattCharacteristicClient::GetBodySensorLocationPath() const { | 442 FakeBluetoothGattCharacteristicClient::GetBodySensorLocationPath() const { |
| 342 return dbus::ObjectPath(body_sensor_location_path_); | 443 return dbus::ObjectPath(body_sensor_location_path_); |
| 343 } | 444 } |
| 344 | 445 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); | 546 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); |
| 446 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); | 547 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); |
| 447 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); | 548 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); |
| 448 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); | 549 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); |
| 449 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); | 550 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); |
| 450 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); | 551 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); |
| 451 return heart_rate_visible_; | 552 return heart_rate_visible_; |
| 452 } | 553 } |
| 453 | 554 |
| 454 } // namespace chromeos | 555 } // namespace chromeos |
| OLD | NEW |