Chromium Code Reviews| 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 return; | |
|
armansito
2014/10/01 20:08:22
Return statement necessary?
Marie Janssen
2014/10/01 22:22:22
Hmm. I swore I had issues with the Chromium about
| |
| 32 } | |
| 33 | |
| 24 // static | 34 // static |
| 25 const char FakeBluetoothGattCharacteristicClient:: | 35 const char FakeBluetoothGattCharacteristicClient:: |
| 26 kHeartRateMeasurementPathComponent[] = "char0000"; | 36 kHeartRateMeasurementPathComponent[] = "char0000"; |
| 27 const char FakeBluetoothGattCharacteristicClient:: | 37 const char FakeBluetoothGattCharacteristicClient:: |
| 28 kBodySensorLocationPathComponent[] = "char0001"; | 38 kBodySensorLocationPathComponent[] = "char0001"; |
| 29 const char FakeBluetoothGattCharacteristicClient:: | 39 const char FakeBluetoothGattCharacteristicClient:: |
| 30 kHeartRateControlPointPathComponent[] = "char0002"; | 40 kHeartRateControlPointPathComponent[] = "char0002"; |
| 31 | 41 |
| 32 // static | 42 // static |
| 33 const char FakeBluetoothGattCharacteristicClient::kHeartRateMeasurementUUID[] = | 43 const char FakeBluetoothGattCharacteristicClient::kHeartRateMeasurementUUID[] = |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 61 | 71 |
| 62 void FakeBluetoothGattCharacteristicClient::Properties::Set( | 72 void FakeBluetoothGattCharacteristicClient::Properties::Set( |
| 63 dbus::PropertyBase* property, | 73 dbus::PropertyBase* property, |
| 64 dbus::PropertySet::SetCallback callback) { | 74 dbus::PropertySet::SetCallback callback) { |
| 65 VLOG(1) << "Set " << property->name(); | 75 VLOG(1) << "Set " << property->name(); |
| 66 callback.Run(false); | 76 callback.Run(false); |
| 67 } | 77 } |
| 68 | 78 |
| 69 FakeBluetoothGattCharacteristicClient::FakeBluetoothGattCharacteristicClient() | 79 FakeBluetoothGattCharacteristicClient::FakeBluetoothGattCharacteristicClient() |
| 70 : heart_rate_visible_(false), | 80 : heart_rate_visible_(false), |
| 81 authorized_(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 (!authorized_) { |
| 124 error_callback.Run(kUnknownCharacteristicError, ""); | 136 error_callback.Run("org.bluez.Error.NotAuthorized", "Authenticate first"); |
|
armansito
2014/10/01 20:08:22
"Authorize first"?
Marie Janssen
2014/10/01 22:22:22
Done.
| |
| 125 return; | 137 return; |
| 126 } | 138 } |
| 127 | 139 |
| 128 if (object_path.value() == heart_rate_measurement_path_ || | 140 if (object_path.value() == heart_rate_measurement_path_ || |
| 129 object_path.value() == heart_rate_control_point_path_) { | 141 object_path.value() == heart_rate_control_point_path_) { |
| 130 error_callback.Run("org.bluez.Error.ReadNotPermitted", | 142 error_callback.Run("org.bluez.Error.ReadNotPermitted", |
| 131 "Reads of this value are not allowed"); | 143 "Reads of this value are not allowed"); |
| 132 return; | 144 return; |
| 133 } | 145 } |
| 134 | 146 |
| 135 if (object_path.value() != body_sensor_location_path_) { | 147 if (object_path.value() != body_sensor_location_path_) { |
| 136 error_callback.Run(kUnknownCharacteristicError, ""); | 148 error_callback.Run(kUnknownCharacteristicError, ""); |
| 137 return; | 149 return; |
| 138 } | 150 } |
| 139 | 151 |
| 140 std::vector<uint8> value; | 152 if (action_extra_requests_.find("ReadValue") != |
| 141 value.push_back(0x06); // Location is "foot". | 153 action_extra_requests_.end()) { |
| 142 callback.Run(value); | 154 struct DelayedCallback* delayed = action_extra_requests_["ReadValue"]; |
|
armansito
2014/10/01 20:08:22
struct keyword not necessary in C++
Marie Janssen
2014/10/01 22:22:22
Done.
| |
| 155 delayed->delay_--; | |
| 156 error_callback.Run("org.bluez.Error.InProgress", | |
| 157 "Another read is currenty in progress"); | |
| 158 if (delayed->delay_ == 0) { | |
| 159 delayed->callback_.Run(); | |
| 160 action_extra_requests_.erase("ReadValue"); | |
| 161 delete delayed; | |
| 162 } | |
| 163 return; | |
| 164 } | |
| 165 base::Closure completed_callback; | |
| 166 if (!IsHeartRateVisible()) { | |
| 167 completed_callback = | |
| 168 base::Bind(error_callback, kUnknownCharacteristicError, ""); | |
| 169 } else { | |
| 170 std::vector<uint8> value; | |
| 171 value.push_back(0x06); // Location is "foot". | |
| 172 completed_callback = base::Bind(callback, value); | |
| 173 } | |
| 174 | |
| 175 if (extra_requests_ > 0) { | |
| 176 action_extra_requests_["ReadValue"] = | |
| 177 new DelayedCallback(completed_callback, extra_requests_); | |
| 178 return; | |
| 179 } | |
| 180 completed_callback.Run(); | |
| 143 } | 181 } |
| 144 | 182 |
| 145 void FakeBluetoothGattCharacteristicClient::WriteValue( | 183 void FakeBluetoothGattCharacteristicClient::WriteValue( |
| 146 const dbus::ObjectPath& object_path, | 184 const dbus::ObjectPath& object_path, |
| 147 const std::vector<uint8>& value, | 185 const std::vector<uint8>& value, |
| 148 const base::Closure& callback, | 186 const base::Closure& callback, |
| 149 const ErrorCallback& error_callback) { | 187 const ErrorCallback& error_callback) { |
| 188 if (!authorized_) { | |
| 189 error_callback.Run("org.bluez.Error.NotAuthorized", "Authenticate first"); | |
| 190 return; | |
| 191 } | |
| 192 | |
| 150 if (!IsHeartRateVisible()) { | 193 if (!IsHeartRateVisible()) { |
| 151 error_callback.Run(kUnknownCharacteristicError, ""); | 194 error_callback.Run(kUnknownCharacteristicError, ""); |
| 152 return; | 195 return; |
| 153 } | 196 } |
| 154 | 197 |
| 155 if (object_path.value() != heart_rate_control_point_path_) { | 198 if (object_path.value() != heart_rate_control_point_path_) { |
| 156 error_callback.Run("org.bluez.Error.WriteNotPermitted", | 199 error_callback.Run("org.bluez.Error.WriteNotPermitted", |
| 157 "Writes of this value are not allowed"); | 200 "Writes of this value are not allowed"); |
| 158 return; | 201 return; |
| 159 } | 202 } |
| 160 | 203 |
| 161 DCHECK(heart_rate_control_point_properties_.get()); | 204 DCHECK(heart_rate_control_point_properties_.get()); |
| 162 if (value.size() != 1) { | 205 if (action_extra_requests_.find("WriteValue") != |
| 163 error_callback.Run("org.bluez.Error.InvalidValueLength", | 206 action_extra_requests_.end()) { |
| 164 "Invalid length for write"); | 207 struct DelayedCallback* delayed = action_extra_requests_["WriteValue"]; |
| 208 delayed->delay_--; | |
| 209 error_callback.Run("org.bluez.Error.InProgress", | |
| 210 "Another write is in progress"); | |
| 211 if (delayed->delay_ == 0) { | |
| 212 delayed->callback_.Run(); | |
| 213 action_extra_requests_.erase("WriteValue"); | |
| 214 delete delayed; | |
| 215 } | |
| 165 return; | 216 return; |
| 166 } | 217 } |
| 167 if (value[0] > 1) { | 218 base::Closure completed_callback; |
| 168 error_callback.Run("org.bluez.Error.Failed", | 219 if (value.size() != 1) { |
| 169 "Invalid value given for write"); | 220 completed_callback = base::Bind(error_callback, |
| 221 "org.bluez.Error.InvalidValueLength", | |
| 222 "Invalid length for write"); | |
| 223 } else if (value[0] > 1) { | |
| 224 completed_callback = base::Bind(error_callback, | |
| 225 "org.bluez.Error.Failed", | |
| 226 "Invalid value given for write"); | |
| 227 } else if (value[0] == 1) { | |
| 228 // TODO(jamuraa): make this happen when the callback happens | |
| 229 calories_burned_ = 0; | |
| 230 completed_callback = callback; | |
| 231 } | |
| 232 | |
| 233 if (extra_requests_ > 0) { | |
| 234 action_extra_requests_["WriteValue"] = | |
| 235 new DelayedCallback(completed_callback, extra_requests_); | |
| 170 return; | 236 return; |
| 171 } | 237 } |
| 172 | 238 completed_callback.Run(); |
| 173 if (value[0] == 1) | |
| 174 calories_burned_ = 0; | |
| 175 | |
| 176 callback.Run(); | |
| 177 } | 239 } |
| 178 | 240 |
| 179 void FakeBluetoothGattCharacteristicClient::StartNotify( | 241 void FakeBluetoothGattCharacteristicClient::StartNotify( |
| 180 const dbus::ObjectPath& object_path, | 242 const dbus::ObjectPath& object_path, |
| 181 const base::Closure& callback, | 243 const base::Closure& callback, |
| 182 const ErrorCallback& error_callback) { | 244 const ErrorCallback& error_callback) { |
| 183 if (!IsHeartRateVisible()) { | 245 if (!IsHeartRateVisible()) { |
| 184 error_callback.Run(kUnknownCharacteristicError, ""); | 246 error_callback.Run(kUnknownCharacteristicError, ""); |
| 185 return; | 247 return; |
| 186 } | 248 } |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 heart_rate_measurement_properties_.reset(); | 387 heart_rate_measurement_properties_.reset(); |
| 326 body_sensor_location_properties_.reset(); | 388 body_sensor_location_properties_.reset(); |
| 327 heart_rate_control_point_properties_.reset(); | 389 heart_rate_control_point_properties_.reset(); |
| 328 | 390 |
| 329 heart_rate_measurement_path_.clear(); | 391 heart_rate_measurement_path_.clear(); |
| 330 body_sensor_location_path_.clear(); | 392 body_sensor_location_path_.clear(); |
| 331 heart_rate_control_point_path_.clear(); | 393 heart_rate_control_point_path_.clear(); |
| 332 heart_rate_visible_ = false; | 394 heart_rate_visible_ = false; |
| 333 } | 395 } |
| 334 | 396 |
| 397 void FakeBluetoothGattCharacteristicClient::SetExtraProcessing( | |
| 398 size_t requests) { | |
| 399 extra_requests_ = requests; | |
| 400 if (extra_requests_ == 0) { | |
| 401 for (const auto& it : action_extra_requests_) { | |
|
armansito
2014/10/01 20:08:22
Nice, I guess we should start adopting these C++11
Marie Janssen
2014/10/01 22:22:22
This is the only foreach loop in this file.
| |
| 402 it.second->callback_.Run(); | |
| 403 delete it.second; | |
| 404 } | |
| 405 action_extra_requests_.clear(); | |
| 406 } | |
| 407 } | |
| 408 | |
| 409 size_t FakeBluetoothGattCharacteristicClient::GetExtraProcessing() const { | |
| 410 return extra_requests_; | |
| 411 } | |
| 412 | |
| 335 dbus::ObjectPath | 413 dbus::ObjectPath |
| 336 FakeBluetoothGattCharacteristicClient::GetHeartRateMeasurementPath() const { | 414 FakeBluetoothGattCharacteristicClient::GetHeartRateMeasurementPath() const { |
| 337 return dbus::ObjectPath(heart_rate_measurement_path_); | 415 return dbus::ObjectPath(heart_rate_measurement_path_); |
| 338 } | 416 } |
| 339 | 417 |
| 340 dbus::ObjectPath | 418 dbus::ObjectPath |
| 341 FakeBluetoothGattCharacteristicClient::GetBodySensorLocationPath() const { | 419 FakeBluetoothGattCharacteristicClient::GetBodySensorLocationPath() const { |
| 342 return dbus::ObjectPath(body_sensor_location_path_); | 420 return dbus::ObjectPath(body_sensor_location_path_); |
| 343 } | 421 } |
| 344 | 422 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 445 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); | 523 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); |
| 446 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); | 524 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); |
| 447 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); | 525 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); |
| 448 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); | 526 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); |
| 449 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); | 527 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); |
| 450 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); | 528 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); |
| 451 return heart_rate_visible_; | 529 return heart_rate_visible_; |
| 452 } | 530 } |
| 453 | 531 |
| 454 } // namespace chromeos | 532 } // namespace chromeos |
| OLD | NEW |