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 "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" | 5 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 return heart_rate_control_point_properties_.get(); | 127 return heart_rate_control_point_properties_.get(); |
128 } | 128 } |
129 return NULL; | 129 return NULL; |
130 } | 130 } |
131 | 131 |
132 void FakeBluetoothGattCharacteristicClient::ReadValue( | 132 void FakeBluetoothGattCharacteristicClient::ReadValue( |
133 const dbus::ObjectPath& object_path, | 133 const dbus::ObjectPath& object_path, |
134 const ValueCallback& callback, | 134 const ValueCallback& callback, |
135 const ErrorCallback& error_callback) { | 135 const ErrorCallback& error_callback) { |
136 if (!authenticated_) { | 136 if (!authenticated_) { |
137 error_callback.Run("org.bluez.Error.NotPaired", "Please login"); | 137 error_callback.Run(bluetooth_gatt_service::kErrorNotPaired, "Please login"); |
138 return; | 138 return; |
139 } | 139 } |
140 | 140 |
141 if (!authorized_) { | 141 if (!authorized_) { |
142 error_callback.Run("org.bluez.Error.NotAuthorized", "Authorize first"); | 142 error_callback.Run(bluetooth_gatt_service::kErrorNotAuthorized, |
| 143 "Authorize first"); |
143 return; | 144 return; |
144 } | 145 } |
145 | 146 |
146 if (object_path.value() == heart_rate_control_point_path_) { | 147 if (object_path.value() == heart_rate_control_point_path_) { |
147 error_callback.Run("org.bluez.Error.NotPermitted", | 148 error_callback.Run(bluetooth_gatt_service::kErrorReadNotPermitted, |
148 "Reads of this value are not allowed"); | 149 "Reads of this value are not allowed"); |
149 return; | 150 return; |
150 } | 151 } |
151 | 152 |
152 if (object_path.value() == heart_rate_measurement_path_) { | 153 if (object_path.value() == heart_rate_measurement_path_) { |
153 error_callback.Run("org.bluez.Error.NotSupported", | 154 error_callback.Run(bluetooth_gatt_service::kErrorNotSupported, |
154 "Action not supported on this characteristic"); | 155 "Action not supported on this characteristic"); |
155 return; | 156 return; |
156 } | 157 } |
157 | 158 |
158 if (object_path.value() != body_sensor_location_path_) { | 159 if (object_path.value() != body_sensor_location_path_) { |
159 error_callback.Run(kUnknownCharacteristicError, ""); | 160 error_callback.Run(kUnknownCharacteristicError, ""); |
160 return; | 161 return; |
161 } | 162 } |
162 | 163 |
163 if (action_extra_requests_.find("ReadValue") != | 164 if (action_extra_requests_.find("ReadValue") != |
164 action_extra_requests_.end()) { | 165 action_extra_requests_.end()) { |
165 DelayedCallback* delayed = action_extra_requests_["ReadValue"]; | 166 DelayedCallback* delayed = action_extra_requests_["ReadValue"]; |
166 delayed->delay_--; | 167 delayed->delay_--; |
167 error_callback.Run("org.bluez.Error.InProgress", | 168 error_callback.Run(bluetooth_gatt_service::kErrorInProgress, |
168 "Another read is currenty in progress"); | 169 "Another read is currenty in progress"); |
169 if (delayed->delay_ == 0) { | 170 if (delayed->delay_ == 0) { |
170 delayed->callback_.Run(); | 171 delayed->callback_.Run(); |
171 action_extra_requests_.erase("ReadValue"); | 172 action_extra_requests_.erase("ReadValue"); |
172 delete delayed; | 173 delete delayed; |
173 } | 174 } |
174 return; | 175 return; |
175 } | 176 } |
176 | 177 |
177 base::Closure completed_callback; | 178 base::Closure completed_callback; |
(...skipping 15 matching lines...) Expand all Loading... |
193 | 194 |
194 completed_callback.Run(); | 195 completed_callback.Run(); |
195 } | 196 } |
196 | 197 |
197 void FakeBluetoothGattCharacteristicClient::WriteValue( | 198 void FakeBluetoothGattCharacteristicClient::WriteValue( |
198 const dbus::ObjectPath& object_path, | 199 const dbus::ObjectPath& object_path, |
199 const std::vector<uint8_t>& value, | 200 const std::vector<uint8_t>& value, |
200 const base::Closure& callback, | 201 const base::Closure& callback, |
201 const ErrorCallback& error_callback) { | 202 const ErrorCallback& error_callback) { |
202 if (!authenticated_) { | 203 if (!authenticated_) { |
203 error_callback.Run("org.bluez.Error.NotPaired", "Please login"); | 204 error_callback.Run(bluetooth_gatt_service::kErrorNotPaired, "Please login"); |
204 return; | 205 return; |
205 } | 206 } |
206 | 207 |
207 if (!authorized_) { | 208 if (!authorized_) { |
208 error_callback.Run("org.bluez.Error.NotAuthorized", "Authorize first"); | 209 error_callback.Run(bluetooth_gatt_service::kErrorNotAuthorized, |
| 210 "Authorize first"); |
209 return; | 211 return; |
210 } | 212 } |
211 | 213 |
212 if (!IsHeartRateVisible()) { | 214 if (!IsHeartRateVisible()) { |
213 error_callback.Run(kUnknownCharacteristicError, ""); | 215 error_callback.Run(kUnknownCharacteristicError, ""); |
214 return; | 216 return; |
215 } | 217 } |
216 | 218 |
217 if (object_path.value() == heart_rate_measurement_path_) { | 219 if (object_path.value() == heart_rate_measurement_path_) { |
218 error_callback.Run("org.bluez.Error.NotSupported", | 220 error_callback.Run(bluetooth_gatt_service::kErrorNotSupported, |
219 "Action not supported on this characteristic"); | 221 "Action not supported on this characteristic"); |
220 return; | 222 return; |
221 } | 223 } |
222 | 224 |
223 if (object_path.value() != heart_rate_control_point_path_) { | 225 if (object_path.value() != heart_rate_control_point_path_) { |
224 error_callback.Run("org.bluez.Error.NotPermitted", | 226 error_callback.Run(bluetooth_gatt_service::kErrorWriteNotPermitted, |
225 "Writes of this value are not allowed"); | 227 "Writes of this value are not allowed"); |
226 return; | 228 return; |
227 } | 229 } |
228 | 230 |
229 DCHECK(heart_rate_control_point_properties_.get()); | 231 DCHECK(heart_rate_control_point_properties_.get()); |
230 if (action_extra_requests_.find("WriteValue") != | 232 if (action_extra_requests_.find("WriteValue") != |
231 action_extra_requests_.end()) { | 233 action_extra_requests_.end()) { |
232 DelayedCallback* delayed = action_extra_requests_["WriteValue"]; | 234 DelayedCallback* delayed = action_extra_requests_["WriteValue"]; |
233 delayed->delay_--; | 235 delayed->delay_--; |
234 error_callback.Run("org.bluez.Error.InProgress", | 236 error_callback.Run(bluetooth_gatt_service::kErrorInProgress, |
235 "Another write is in progress"); | 237 "Another write is in progress"); |
236 if (delayed->delay_ == 0) { | 238 if (delayed->delay_ == 0) { |
237 delayed->callback_.Run(); | 239 delayed->callback_.Run(); |
238 action_extra_requests_.erase("WriteValue"); | 240 action_extra_requests_.erase("WriteValue"); |
239 delete delayed; | 241 delete delayed; |
240 } | 242 } |
241 return; | 243 return; |
242 } | 244 } |
243 base::Closure completed_callback; | 245 base::Closure completed_callback; |
244 if (value.size() != 1) { | 246 if (value.size() != 1) { |
| 247 completed_callback = base::Bind( |
| 248 error_callback, bluetooth_gatt_service::kErrorInvalidValueLength, |
| 249 "Invalid length for write"); |
| 250 } else if (value[0] > 1) { |
245 completed_callback = | 251 completed_callback = |
246 base::Bind(error_callback, "org.bluez.Error.InvalidValueLength", | 252 base::Bind(error_callback, bluetooth_gatt_service::kErrorFailed, |
247 "Invalid length for write"); | 253 "Invalid value given for write"); |
248 } else if (value[0] > 1) { | |
249 completed_callback = base::Bind(error_callback, "org.bluez.Error.Failed", | |
250 "Invalid value given for write"); | |
251 } else if (value[0] == 1) { | 254 } else if (value[0] == 1) { |
252 // TODO(jamuraa): make this happen when the callback happens | 255 // TODO(jamuraa): make this happen when the callback happens |
253 calories_burned_ = 0; | 256 calories_burned_ = 0; |
254 completed_callback = callback; | 257 completed_callback = callback; |
255 } | 258 } |
256 | 259 |
257 if (extra_requests_ > 0) { | 260 if (extra_requests_ > 0) { |
258 action_extra_requests_["WriteValue"] = | 261 action_extra_requests_["WriteValue"] = |
259 new DelayedCallback(completed_callback, extra_requests_); | 262 new DelayedCallback(completed_callback, extra_requests_); |
260 return; | 263 return; |
261 } | 264 } |
262 completed_callback.Run(); | 265 completed_callback.Run(); |
263 } | 266 } |
264 | 267 |
265 void FakeBluetoothGattCharacteristicClient::StartNotify( | 268 void FakeBluetoothGattCharacteristicClient::StartNotify( |
266 const dbus::ObjectPath& object_path, | 269 const dbus::ObjectPath& object_path, |
267 const base::Closure& callback, | 270 const base::Closure& callback, |
268 const ErrorCallback& error_callback) { | 271 const ErrorCallback& error_callback) { |
269 if (!IsHeartRateVisible()) { | 272 if (!IsHeartRateVisible()) { |
270 error_callback.Run(kUnknownCharacteristicError, ""); | 273 error_callback.Run(kUnknownCharacteristicError, ""); |
271 return; | 274 return; |
272 } | 275 } |
273 | 276 |
274 if (object_path.value() != heart_rate_measurement_path_) { | 277 if (object_path.value() != heart_rate_measurement_path_) { |
275 error_callback.Run("org.bluez.Error.NotSupported", | 278 error_callback.Run(bluetooth_gatt_service::kErrorNotSupported, |
276 "This characteristic does not support notifications"); | 279 "This characteristic does not support notifications"); |
277 return; | 280 return; |
278 } | 281 } |
279 | 282 |
280 if (heart_rate_measurement_properties_->notifying.value()) { | 283 if (heart_rate_measurement_properties_->notifying.value()) { |
281 error_callback.Run("org.bluez.Error.InProgress", | 284 error_callback.Run(bluetooth_gatt_service::kErrorInProgress, |
282 "Characteristic already notifying"); | 285 "Characteristic already notifying"); |
283 return; | 286 return; |
284 } | 287 } |
285 | 288 |
286 heart_rate_measurement_properties_->notifying.ReplaceValue(true); | 289 heart_rate_measurement_properties_->notifying.ReplaceValue(true); |
287 ScheduleHeartRateMeasurementValueChange(); | 290 ScheduleHeartRateMeasurementValueChange(); |
288 | 291 |
289 // Respond asynchronously. | 292 // Respond asynchronously. |
290 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 293 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
291 FROM_HERE, callback, | 294 FROM_HERE, callback, |
292 base::TimeDelta::FromMilliseconds(kStartNotifyResponseIntervalMs)); | 295 base::TimeDelta::FromMilliseconds(kStartNotifyResponseIntervalMs)); |
293 } | 296 } |
294 | 297 |
295 void FakeBluetoothGattCharacteristicClient::StopNotify( | 298 void FakeBluetoothGattCharacteristicClient::StopNotify( |
296 const dbus::ObjectPath& object_path, | 299 const dbus::ObjectPath& object_path, |
297 const base::Closure& callback, | 300 const base::Closure& callback, |
298 const ErrorCallback& error_callback) { | 301 const ErrorCallback& error_callback) { |
299 if (!IsHeartRateVisible()) { | 302 if (!IsHeartRateVisible()) { |
300 error_callback.Run(kUnknownCharacteristicError, ""); | 303 error_callback.Run(kUnknownCharacteristicError, ""); |
301 return; | 304 return; |
302 } | 305 } |
303 | 306 |
304 if (object_path.value() != heart_rate_measurement_path_) { | 307 if (object_path.value() != heart_rate_measurement_path_) { |
305 error_callback.Run("org.bluez.Error.NotSupported", | 308 error_callback.Run(bluetooth_gatt_service::kErrorNotSupported, |
306 "This characteristic does not support notifications"); | 309 "This characteristic does not support notifications"); |
307 return; | 310 return; |
308 } | 311 } |
309 | 312 |
310 if (!heart_rate_measurement_properties_->notifying.value()) { | 313 if (!heart_rate_measurement_properties_->notifying.value()) { |
311 error_callback.Run("org.bluez.Error.Failed", "Not notifying"); | 314 error_callback.Run(bluetooth_gatt_service::kErrorFailed, "Not notifying"); |
312 return; | 315 return; |
313 } | 316 } |
314 | 317 |
315 heart_rate_measurement_properties_->notifying.ReplaceValue(false); | 318 heart_rate_measurement_properties_->notifying.ReplaceValue(false); |
316 | 319 |
317 callback.Run(); | 320 callback.Run(); |
318 } | 321 } |
319 | 322 |
320 void FakeBluetoothGattCharacteristicClient::ExposeHeartRateCharacteristics( | 323 void FakeBluetoothGattCharacteristicClient::ExposeHeartRateCharacteristics( |
321 const dbus::ObjectPath& service_path) { | 324 const dbus::ObjectPath& service_path) { |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); | 556 DCHECK(heart_rate_visible_ != heart_rate_measurement_path_.empty()); |
554 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); | 557 DCHECK(heart_rate_visible_ != body_sensor_location_path_.empty()); |
555 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); | 558 DCHECK(heart_rate_visible_ != heart_rate_control_point_path_.empty()); |
556 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); | 559 DCHECK(heart_rate_visible_ == !!heart_rate_measurement_properties_.get()); |
557 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); | 560 DCHECK(heart_rate_visible_ == !!body_sensor_location_properties_.get()); |
558 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); | 561 DCHECK(heart_rate_visible_ == !!heart_rate_control_point_properties_.get()); |
559 return heart_rate_visible_; | 562 return heart_rate_visible_; |
560 } | 563 } |
561 | 564 |
562 } // namespace bluez | 565 } // namespace bluez |
OLD | NEW |