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" |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 delayed->delay_--; | 169 delayed->delay_--; |
170 error_callback.Run("org.bluez.Error.InProgress", | 170 error_callback.Run("org.bluez.Error.InProgress", |
171 "Another read is currenty in progress"); | 171 "Another read is currenty in progress"); |
172 if (delayed->delay_ == 0) { | 172 if (delayed->delay_ == 0) { |
173 delayed->callback_.Run(); | 173 delayed->callback_.Run(); |
174 action_extra_requests_.erase("ReadValue"); | 174 action_extra_requests_.erase("ReadValue"); |
175 delete delayed; | 175 delete delayed; |
176 } | 176 } |
177 return; | 177 return; |
178 } | 178 } |
| 179 |
179 base::Closure completed_callback; | 180 base::Closure completed_callback; |
180 if (!IsHeartRateVisible()) { | 181 if (!IsHeartRateVisible()) { |
181 completed_callback = | 182 completed_callback = |
182 base::Bind(error_callback, kUnknownCharacteristicError, ""); | 183 base::Bind(error_callback, kUnknownCharacteristicError, ""); |
183 } else { | 184 } else { |
184 std::vector<uint8> value; | 185 std::vector<uint8> value = {0x06}; // Location is "foot". |
185 value.push_back(0x06); // Location is "foot". | 186 completed_callback = base::Bind( |
186 completed_callback = base::Bind(callback, value); | 187 &FakeBluetoothGattCharacteristicClient::DelayedReadValueCallback, |
| 188 weak_ptr_factory_.GetWeakPtr(), object_path, callback, value); |
187 } | 189 } |
188 | 190 |
189 if (extra_requests_ > 0) { | 191 if (extra_requests_ > 0) { |
190 action_extra_requests_["ReadValue"] = | 192 action_extra_requests_["ReadValue"] = |
191 new DelayedCallback(completed_callback, extra_requests_); | 193 new DelayedCallback(completed_callback, extra_requests_); |
192 return; | 194 return; |
193 } | 195 } |
| 196 |
194 completed_callback.Run(); | 197 completed_callback.Run(); |
195 } | 198 } |
196 | 199 |
197 void FakeBluetoothGattCharacteristicClient::WriteValue( | 200 void FakeBluetoothGattCharacteristicClient::WriteValue( |
198 const dbus::ObjectPath& object_path, | 201 const dbus::ObjectPath& object_path, |
199 const std::vector<uint8>& value, | 202 const std::vector<uint8>& value, |
200 const base::Closure& callback, | 203 const base::Closure& callback, |
201 const ErrorCallback& error_callback) { | 204 const ErrorCallback& error_callback) { |
202 if (!authenticated_) { | 205 if (!authenticated_) { |
203 error_callback.Run("org.bluez.Error.NotPaired", "Please login"); | 206 error_callback.Run("org.bluez.Error.NotPaired", "Please login"); |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 ScheduleHeartRateMeasurementValueChange() { | 484 ScheduleHeartRateMeasurementValueChange() { |
482 if (!IsHeartRateVisible()) | 485 if (!IsHeartRateVisible()) |
483 return; | 486 return; |
484 | 487 |
485 // Don't send updates if the characteristic is not notifying. | 488 // Don't send updates if the characteristic is not notifying. |
486 if (!heart_rate_measurement_properties_->notifying.value()) | 489 if (!heart_rate_measurement_properties_->notifying.value()) |
487 return; | 490 return; |
488 | 491 |
489 VLOG(2) << "Updating heart rate value."; | 492 VLOG(2) << "Updating heart rate value."; |
490 std::vector<uint8> measurement = GetHeartRateMeasurementValue(); | 493 std::vector<uint8> measurement = GetHeartRateMeasurementValue(); |
491 | 494 heart_rate_measurement_properties_->value.ReplaceValue(measurement); |
492 FOR_EACH_OBSERVER( | |
493 BluetoothGattCharacteristicClient::Observer, | |
494 observers_, | |
495 GattCharacteristicValueUpdated( | |
496 dbus::ObjectPath(heart_rate_measurement_path_), measurement)); | |
497 | 495 |
498 base::MessageLoop::current()->PostDelayedTask( | 496 base::MessageLoop::current()->PostDelayedTask( |
499 FROM_HERE, | 497 FROM_HERE, |
500 base::Bind(&FakeBluetoothGattCharacteristicClient:: | 498 base::Bind(&FakeBluetoothGattCharacteristicClient:: |
501 ScheduleHeartRateMeasurementValueChange, | 499 ScheduleHeartRateMeasurementValueChange, |
502 weak_ptr_factory_.GetWeakPtr()), | 500 weak_ptr_factory_.GetWeakPtr()), |
503 base::TimeDelta::FromMilliseconds( | 501 base::TimeDelta::FromMilliseconds( |
504 kHeartRateMeasurementNotificationIntervalMs)); | 502 kHeartRateMeasurementNotificationIntervalMs)); |
505 } | 503 } |
506 | 504 |
| 505 void FakeBluetoothGattCharacteristicClient::DelayedReadValueCallback( |
| 506 const dbus::ObjectPath& object_path, |
| 507 const ValueCallback& callback, |
| 508 const std::vector<uint8_t>& value) { |
| 509 Properties* properties = GetProperties(object_path); |
| 510 DCHECK(properties); |
| 511 |
| 512 properties->value.ReplaceValue(value); |
| 513 callback.Run(value); |
| 514 } |
| 515 |
507 std::vector<uint8> | 516 std::vector<uint8> |
508 FakeBluetoothGattCharacteristicClient::GetHeartRateMeasurementValue() { | 517 FakeBluetoothGattCharacteristicClient::GetHeartRateMeasurementValue() { |
509 // TODO(armansito): We should make sure to properly pack this struct to ensure | 518 // TODO(armansito): We should make sure to properly pack this struct to ensure |
510 // correct byte alignment and endianness. It doesn't matter too much right now | 519 // correct byte alignment and endianness. It doesn't matter too much right now |
511 // as this is a fake and GCC on Linux seems to do the right thing. | 520 // as this is a fake and GCC on Linux seems to do the right thing. |
512 struct { | 521 struct { |
513 uint8 flags; | 522 uint8 flags; |
514 uint8 bpm; | 523 uint8 bpm; |
515 uint16 energy_expanded; | 524 uint16 energy_expanded; |
516 uint16 rr_interval; | 525 uint16 rr_interval; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); | 559 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); |
551 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); | 560 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); |
552 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); | 561 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); |
553 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); | 562 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); |
554 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); | 563 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); |
555 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); | 564 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); |
556 return heart_rate_visible_; | 565 return heart_rate_visible_; |
557 } | 566 } |
558 | 567 |
559 } // namespace chromeos | 568 } // namespace chromeos |
OLD | NEW |