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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 } | 62 } |
63 | 63 |
64 void FakeBluetoothGattCharacteristicClient::Properties::Set( | 64 void FakeBluetoothGattCharacteristicClient::Properties::Set( |
65 dbus::PropertyBase* property, | 65 dbus::PropertyBase* property, |
66 dbus::PropertySet::SetCallback callback) { | 66 dbus::PropertySet::SetCallback callback) { |
67 VLOG(1) << "Set " << property->name(); | 67 VLOG(1) << "Set " << property->name(); |
68 if (property->name() != value.name()) { | 68 if (property->name() != value.name()) { |
69 callback.Run(false); | 69 callback.Run(false); |
70 return; | 70 return; |
71 } | 71 } |
| 72 |
72 // Allow writing to only certain characteristics that are defined with the | 73 // Allow writing to only certain characteristics that are defined with the |
73 // write permission. | 74 // write permission. |
74 // TODO(armansito): Actually check against the permissions property instead of | 75 bool write = false; |
75 // UUID, once that property is fully defined in the API. | 76 for (std::vector<std::string>::const_iterator iter = flags.value().begin(); |
76 if (uuid.value() != kHeartRateControlPointUUID) { | 77 iter != flags.value().end(); |
| 78 ++iter) { |
| 79 if (*iter == bluetooth_gatt_characteristic::kFlagWrite || |
| 80 *iter == bluetooth_gatt_characteristic::kFlagWriteWithoutResponse || |
| 81 *iter == |
| 82 bluetooth_gatt_characteristic::kFlagAuthenticatedSignedWrites || |
| 83 *iter == bluetooth_gatt_characteristic::kFlagReliableWrite) { |
| 84 write = true; |
| 85 break; |
| 86 } |
| 87 } |
| 88 |
| 89 if (!write) { |
77 callback.Run(false); | 90 callback.Run(false); |
78 return; | 91 return; |
79 } | 92 } |
| 93 |
80 callback.Run(true); | 94 callback.Run(true); |
81 property->ReplaceValueWithSetValue(); | 95 property->ReplaceValueWithSetValue(); |
82 } | 96 } |
83 | 97 |
84 FakeBluetoothGattCharacteristicClient::FakeBluetoothGattCharacteristicClient() | 98 FakeBluetoothGattCharacteristicClient::FakeBluetoothGattCharacteristicClient() |
85 : heart_rate_visible_(false), | 99 : heart_rate_visible_(false), |
86 calories_burned_(0), | 100 calories_burned_(0), |
87 weak_ptr_factory_(this) { | 101 weak_ptr_factory_(this) { |
88 } | 102 } |
89 | 103 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 147 |
134 void FakeBluetoothGattCharacteristicClient::ExposeHeartRateCharacteristics( | 148 void FakeBluetoothGattCharacteristicClient::ExposeHeartRateCharacteristics( |
135 const dbus::ObjectPath& service_path) { | 149 const dbus::ObjectPath& service_path) { |
136 if (IsHeartRateVisible()) { | 150 if (IsHeartRateVisible()) { |
137 VLOG(2) << "Fake Heart Rate characteristics are already visible."; | 151 VLOG(2) << "Fake Heart Rate characteristics are already visible."; |
138 return; | 152 return; |
139 } | 153 } |
140 | 154 |
141 VLOG(2) << "Exposing fake Heart Rate characteristics."; | 155 VLOG(2) << "Exposing fake Heart Rate characteristics."; |
142 | 156 |
| 157 std::vector<std::string> flags; |
| 158 |
143 // ==== Heart Rate Measurement Characteristic ==== | 159 // ==== Heart Rate Measurement Characteristic ==== |
144 heart_rate_measurement_path_ = | 160 heart_rate_measurement_path_ = |
145 service_path.value() + "/" + kHeartRateMeasurementPathComponent; | 161 service_path.value() + "/" + kHeartRateMeasurementPathComponent; |
146 heart_rate_measurement_properties_.reset(new Properties(base::Bind( | 162 heart_rate_measurement_properties_.reset(new Properties(base::Bind( |
147 &FakeBluetoothGattCharacteristicClient::OnPropertyChanged, | 163 &FakeBluetoothGattCharacteristicClient::OnPropertyChanged, |
148 weak_ptr_factory_.GetWeakPtr(), | 164 weak_ptr_factory_.GetWeakPtr(), |
149 dbus::ObjectPath(heart_rate_measurement_path_)))); | 165 dbus::ObjectPath(heart_rate_measurement_path_)))); |
150 heart_rate_measurement_properties_->uuid.ReplaceValue( | 166 heart_rate_measurement_properties_->uuid.ReplaceValue( |
151 kHeartRateMeasurementUUID); | 167 kHeartRateMeasurementUUID); |
152 heart_rate_measurement_properties_->service.ReplaceValue(service_path); | 168 heart_rate_measurement_properties_->service.ReplaceValue(service_path); |
153 | 169 flags.push_back(bluetooth_gatt_characteristic::kFlagNotify); |
154 // TODO(armansito): Fill out the flags field once bindings for the values have | 170 heart_rate_measurement_properties_->flags.ReplaceValue(flags); |
155 // been added. For now, leave it empty. | |
156 | 171 |
157 std::vector<uint8> measurement_value = GetHeartRateMeasurementValue(); | 172 std::vector<uint8> measurement_value = GetHeartRateMeasurementValue(); |
158 heart_rate_measurement_properties_->value.ReplaceValue(measurement_value); | 173 heart_rate_measurement_properties_->value.ReplaceValue(measurement_value); |
159 | 174 |
160 // ==== Body Sensor Location Characteristic ==== | 175 // ==== Body Sensor Location Characteristic ==== |
161 body_sensor_location_path_ = | 176 body_sensor_location_path_ = |
162 service_path.value() + "/" + kBodySensorLocationPathComponent; | 177 service_path.value() + "/" + kBodySensorLocationPathComponent; |
163 body_sensor_location_properties_.reset(new Properties(base::Bind( | 178 body_sensor_location_properties_.reset(new Properties(base::Bind( |
164 &FakeBluetoothGattCharacteristicClient::OnPropertyChanged, | 179 &FakeBluetoothGattCharacteristicClient::OnPropertyChanged, |
165 weak_ptr_factory_.GetWeakPtr(), | 180 weak_ptr_factory_.GetWeakPtr(), |
166 dbus::ObjectPath(body_sensor_location_path_)))); | 181 dbus::ObjectPath(body_sensor_location_path_)))); |
167 body_sensor_location_properties_->uuid.ReplaceValue(kBodySensorLocationUUID); | 182 body_sensor_location_properties_->uuid.ReplaceValue(kBodySensorLocationUUID); |
168 body_sensor_location_properties_->service.ReplaceValue(service_path); | 183 body_sensor_location_properties_->service.ReplaceValue(service_path); |
169 | 184 flags.clear(); |
170 // TODO(armansito): Fill out the flags field once bindings for the values have | 185 flags.push_back(bluetooth_gatt_characteristic::kFlagRead); |
171 // been added. For now, leave it empty. | 186 body_sensor_location_properties_->flags.ReplaceValue(flags); |
172 | 187 |
173 // The sensor is in the "Other" location. | 188 // The sensor is in the "Other" location. |
174 std::vector<uint8> body_sensor_location_value; | 189 std::vector<uint8> body_sensor_location_value; |
175 body_sensor_location_value.push_back(0); | 190 body_sensor_location_value.push_back(0); |
176 body_sensor_location_properties_->value.ReplaceValue( | 191 body_sensor_location_properties_->value.ReplaceValue( |
177 body_sensor_location_value); | 192 body_sensor_location_value); |
178 | 193 |
179 // ==== Heart Rate Control Point Characteristic ==== | 194 // ==== Heart Rate Control Point Characteristic ==== |
180 heart_rate_control_point_path_ = | 195 heart_rate_control_point_path_ = |
181 service_path.value() + "/" + kHeartRateControlPointPathComponent; | 196 service_path.value() + "/" + kHeartRateControlPointPathComponent; |
182 heart_rate_control_point_properties_.reset(new Properties(base::Bind( | 197 heart_rate_control_point_properties_.reset(new Properties(base::Bind( |
183 &FakeBluetoothGattCharacteristicClient::OnPropertyChanged, | 198 &FakeBluetoothGattCharacteristicClient::OnPropertyChanged, |
184 weak_ptr_factory_.GetWeakPtr(), | 199 weak_ptr_factory_.GetWeakPtr(), |
185 dbus::ObjectPath(heart_rate_control_point_path_)))); | 200 dbus::ObjectPath(heart_rate_control_point_path_)))); |
186 heart_rate_control_point_properties_->uuid.ReplaceValue( | 201 heart_rate_control_point_properties_->uuid.ReplaceValue( |
187 kHeartRateControlPointUUID); | 202 kHeartRateControlPointUUID); |
188 heart_rate_control_point_properties_->service.ReplaceValue(service_path); | 203 heart_rate_control_point_properties_->service.ReplaceValue(service_path); |
189 | 204 flags.clear(); |
190 // TODO(armansito): Fill out the flags field once bindings for the values have | 205 flags.push_back(bluetooth_gatt_characteristic::kFlagWrite); |
191 // been added. For now, leave it empty. | 206 heart_rate_control_point_properties_->flags.ReplaceValue(flags); |
192 | 207 |
193 // Set the initial value to 0. Whenever this gets set to 1, we will reset the | 208 // Set the initial value to 0. Whenever this gets set to 1, we will reset the |
194 // total calories burned and change the value back to 0. | 209 // total calories burned and change the value back to 0. |
195 std::vector<uint8> heart_rate_control_point_value; | 210 std::vector<uint8> heart_rate_control_point_value; |
196 heart_rate_control_point_value.push_back(0); | 211 heart_rate_control_point_value.push_back(0); |
197 heart_rate_control_point_properties_->value.ReplaceValue( | 212 heart_rate_control_point_properties_->value.ReplaceValue( |
198 heart_rate_control_point_value); | 213 heart_rate_control_point_value); |
199 | 214 |
200 heart_rate_visible_ = true; | 215 heart_rate_visible_ = true; |
201 | 216 |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); | 384 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); |
370 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); | 385 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); |
371 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); | 386 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); |
372 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); | 387 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); |
373 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); | 388 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); |
374 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); | 389 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); |
375 return heart_rate_visible_; | 390 return heart_rate_visible_; |
376 } | 391 } |
377 | 392 |
378 } // namespace chromeos | 393 } // namespace chromeos |
OLD | NEW |