Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/arc/bluetooth/arc_bluetooth_bridge.h" | 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <iomanip> | 10 #include <iomanip> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 using device::BluetoothLocalGattService; | 43 using device::BluetoothLocalGattService; |
| 44 using device::BluetoothRemoteGattCharacteristic; | 44 using device::BluetoothRemoteGattCharacteristic; |
| 45 using device::BluetoothRemoteGattDescriptor; | 45 using device::BluetoothRemoteGattDescriptor; |
| 46 using device::BluetoothRemoteGattService; | 46 using device::BluetoothRemoteGattService; |
| 47 using device::BluetoothTransport; | 47 using device::BluetoothTransport; |
| 48 using device::BluetoothUUID; | 48 using device::BluetoothUUID; |
| 49 | 49 |
| 50 namespace { | 50 namespace { |
| 51 constexpr int32_t kMinBtleVersion = 1; | 51 constexpr int32_t kMinBtleVersion = 1; |
| 52 constexpr int32_t kMinBtleNotifyVersion = 2; | 52 constexpr int32_t kMinBtleNotifyVersion = 2; |
| 53 constexpr int32_t kMinGattServerVersion = 3; | |
| 53 constexpr uint32_t kGattReadPermission = | 54 constexpr uint32_t kGattReadPermission = |
| 54 BluetoothGattCharacteristic::Permission::PERMISSION_READ | | 55 BluetoothGattCharacteristic::Permission::PERMISSION_READ | |
| 55 BluetoothGattCharacteristic::Permission::PERMISSION_READ_ENCRYPTED | | 56 BluetoothGattCharacteristic::Permission::PERMISSION_READ_ENCRYPTED | |
| 56 BluetoothGattCharacteristic::Permission:: | 57 BluetoothGattCharacteristic::Permission:: |
| 57 PERMISSION_READ_ENCRYPTED_AUTHENTICATED; | 58 PERMISSION_READ_ENCRYPTED_AUTHENTICATED; |
| 58 constexpr uint32_t kGattWritePermission = | 59 constexpr uint32_t kGattWritePermission = |
| 59 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE | | 60 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE | |
| 60 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE_ENCRYPTED | | 61 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE_ENCRYPTED | |
| 61 BluetoothGattCharacteristic::Permission:: | 62 BluetoothGattCharacteristic::Permission:: |
| 62 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED; | 63 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 void OnGattReadError(const GattReadCallback& callback, | 131 void OnGattReadError(const GattReadCallback& callback, |
| 131 BluetoothGattService::GattErrorCode error_code) { | 132 BluetoothGattService::GattErrorCode error_code) { |
| 132 arc::mojom::BluetoothGattValuePtr gattValue = | 133 arc::mojom::BluetoothGattValuePtr gattValue = |
| 133 arc::mojom::BluetoothGattValue::New(); | 134 arc::mojom::BluetoothGattValue::New(); |
| 134 gattValue->status = | 135 gattValue->status = |
| 135 mojo::ConvertTo<arc::mojom::BluetoothGattStatus>(error_code); | 136 mojo::ConvertTo<arc::mojom::BluetoothGattStatus>(error_code); |
| 136 gattValue->value = nullptr; | 137 gattValue->value = nullptr; |
| 137 callback.Run(std::move(gattValue)); | 138 callback.Run(std::move(gattValue)); |
| 138 } | 139 } |
| 139 | 140 |
| 141 // Callback function for mojom::BluetoothInstance::RequestGattRead | |
| 142 void OnGattServerRead( | |
| 143 const BluetoothLocalGattService::Delegate::ValueCallback& success_callback, | |
| 144 const BluetoothLocalGattService::Delegate::ErrorCallback& error_callback, | |
| 145 arc::mojom::BluetoothGattStatus status, | |
| 146 mojo::Array<uint8_t> value) { | |
| 147 if (status == arc::mojom::BluetoothGattStatus::GATT_SUCCESS) | |
| 148 success_callback.Run(value.To<std::vector<uint8_t>>()); | |
| 149 else | |
| 150 error_callback.Run(); | |
| 151 } | |
| 152 | |
| 153 // Callback function for mojom::BluetoothInstance::RequestGattWrite | |
| 154 void OnGattServerWrite( | |
| 155 const base::Closure& success_callback, | |
| 156 const BluetoothLocalGattService::Delegate::ErrorCallback& error_callback, | |
| 157 arc::mojom::BluetoothGattStatus status) { | |
| 158 if (status == arc::mojom::BluetoothGattStatus::GATT_SUCCESS) | |
| 159 success_callback.Run(); | |
| 160 else | |
| 161 error_callback.Run(); | |
| 162 } | |
| 163 | |
| 140 } // namespace | 164 } // namespace |
| 141 | 165 |
| 142 namespace arc { | 166 namespace arc { |
| 143 | 167 |
| 144 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) | 168 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) |
| 145 : ArcService(bridge_service), binding_(this), weak_factory_(this) { | 169 : ArcService(bridge_service), binding_(this), weak_factory_(this) { |
| 146 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 170 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 147 VLOG(1) << "registering bluetooth adapter"; | 171 VLOG(1) << "registering bluetooth adapter"; |
| 148 BluetoothAdapterFactory::GetAdapter(base::Bind( | 172 BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 149 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); | 173 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 367 true /* is_notify */, mojo::Array<uint8_t>::From(value)); | 391 true /* is_notify */, mojo::Array<uint8_t>::From(value)); |
| 368 } | 392 } |
| 369 | 393 |
| 370 void ArcBluetoothBridge::GattDescriptorValueChanged( | 394 void ArcBluetoothBridge::GattDescriptorValueChanged( |
| 371 BluetoothAdapter* adapter, | 395 BluetoothAdapter* adapter, |
| 372 BluetoothRemoteGattDescriptor* descriptor, | 396 BluetoothRemoteGattDescriptor* descriptor, |
| 373 const std::vector<uint8_t>& value) { | 397 const std::vector<uint8_t>& value) { |
| 374 // Placeholder for GATT client functionality | 398 // Placeholder for GATT client functionality |
| 375 } | 399 } |
| 376 | 400 |
| 401 template <class LocalGattAttribute> | |
| 402 void ArcBluetoothBridge::OnGattAttributeReadRequest( | |
| 403 const BluetoothDevice* device, | |
| 404 const LocalGattAttribute* gatt_obj, | |
| 405 int offset, | |
| 406 const ValueCallback& success_callback, | |
| 407 const ErrorCallback& error_callback) { | |
| 408 DCHECK(CalledOnValidThread()); | |
| 409 if (!HasBluetoothInstance() || | |
| 410 !CheckBluetoothInstanceVersion(kMinGattServerVersion) || offset < 0) { | |
|
palmer
2016/07/22 01:46:10
Is there an upper bound for offset? Or, if sanity
puthik_chromium
2016/07/22 02:12:33
The upper bound is the size of the Gatt Attribute
palmer
2016/07/22 21:49:11
I'm not sure I'm looking at the right thing, but t
puthik_chromium
2016/07/22 22:41:05
Done. Found it in the spec.
| |
| 411 error_callback.Run(); | |
| 412 return; | |
| 413 } | |
| 414 | |
| 415 DCHECK(gatt_handle_.find(gatt_obj->GetIdentifier()) != gatt_handle_.end()); | |
|
palmer
2016/07/22 01:46:10
This could only happen by programmer error? I ask
puthik_chromium
2016/07/22 02:12:33
The caller of these function is On{Characteristic,
palmer
2016/07/22 21:49:11
Great, thanks.
| |
| 416 | |
| 417 arc_bridge_service()->bluetooth()->instance()->RequestGattRead( | |
| 418 mojom::BluetoothAddress::From(device->GetAddress()), | |
| 419 gatt_handle_[gatt_obj->GetIdentifier()], offset, false /* is_long */, | |
| 420 base::Bind(&OnGattServerRead, success_callback, error_callback)); | |
| 421 } | |
| 422 | |
| 423 template <class LocalGattAttribute> | |
| 424 void ArcBluetoothBridge::OnGattAttributeWriteRequest( | |
|
palmer
2016/07/22 01:46:10
Same questions as above for this function.
puthik_chromium
2016/07/22 02:12:33
Same answer as above
| |
| 425 const BluetoothDevice* device, | |
| 426 const LocalGattAttribute* gatt_obj, | |
| 427 const std::vector<uint8_t>& value, | |
| 428 int offset, | |
| 429 const base::Closure& success_callback, | |
| 430 const ErrorCallback& error_callback) { | |
| 431 DCHECK(CalledOnValidThread()); | |
| 432 if (!HasBluetoothInstance() || | |
| 433 !CheckBluetoothInstanceVersion(kMinGattServerVersion) || offset < 0) { | |
| 434 error_callback.Run(); | |
| 435 return; | |
| 436 } | |
| 437 | |
| 438 DCHECK(gatt_handle_.find(gatt_obj->GetIdentifier()) != gatt_handle_.end()); | |
| 439 | |
| 440 arc_bridge_service()->bluetooth()->instance()->RequestGattWrite( | |
| 441 mojom::BluetoothAddress::From(device->GetAddress()), | |
| 442 gatt_handle_[gatt_obj->GetIdentifier()], offset, | |
| 443 mojo::Array<uint8_t>::From(value), | |
| 444 base::Bind(&OnGattServerWrite, success_callback, error_callback)); | |
| 445 } | |
| 446 | |
| 377 void ArcBluetoothBridge::OnCharacteristicReadRequest( | 447 void ArcBluetoothBridge::OnCharacteristicReadRequest( |
| 378 const BluetoothDevice* device, | 448 const BluetoothDevice* device, |
| 379 const BluetoothLocalGattCharacteristic* characteristic, | 449 const BluetoothLocalGattCharacteristic* characteristic, |
| 380 int offset, | 450 int offset, |
| 381 const ValueCallback& callback, | 451 const ValueCallback& callback, |
| 382 const ErrorCallback& error_callback) {} | 452 const ErrorCallback& error_callback) { |
| 453 OnGattAttributeReadRequest(device, characteristic, offset, callback, | |
| 454 error_callback); | |
| 455 } | |
| 383 | 456 |
| 384 void ArcBluetoothBridge::OnCharacteristicWriteRequest( | 457 void ArcBluetoothBridge::OnCharacteristicWriteRequest( |
| 385 const BluetoothDevice* device, | 458 const BluetoothDevice* device, |
| 386 const BluetoothLocalGattCharacteristic* characteristic, | 459 const BluetoothLocalGattCharacteristic* characteristic, |
| 387 const std::vector<uint8_t>& value, | 460 const std::vector<uint8_t>& value, |
| 388 int offset, | 461 int offset, |
| 389 const base::Closure& callback, | 462 const base::Closure& callback, |
| 390 const ErrorCallback& error_callback) {} | 463 const ErrorCallback& error_callback) { |
| 464 OnGattAttributeWriteRequest(device, characteristic, value, offset, callback, | |
| 465 error_callback); | |
| 466 } | |
| 391 | 467 |
| 392 void ArcBluetoothBridge::OnDescriptorReadRequest( | 468 void ArcBluetoothBridge::OnDescriptorReadRequest( |
| 393 const BluetoothDevice* device, | 469 const BluetoothDevice* device, |
| 394 const BluetoothLocalGattDescriptor* descriptor, | 470 const BluetoothLocalGattDescriptor* descriptor, |
| 395 int offset, | 471 int offset, |
| 396 const ValueCallback& callback, | 472 const ValueCallback& callback, |
| 397 const ErrorCallback& error_callback) {} | 473 const ErrorCallback& error_callback) { |
| 474 OnGattAttributeReadRequest(device, descriptor, offset, callback, | |
| 475 error_callback); | |
| 476 } | |
| 398 | 477 |
| 399 void ArcBluetoothBridge::OnDescriptorWriteRequest( | 478 void ArcBluetoothBridge::OnDescriptorWriteRequest( |
| 400 const BluetoothDevice* device, | 479 const BluetoothDevice* device, |
| 401 const BluetoothLocalGattDescriptor* descriptor, | 480 const BluetoothLocalGattDescriptor* descriptor, |
| 402 const std::vector<uint8_t>& value, | 481 const std::vector<uint8_t>& value, |
| 403 int offset, | 482 int offset, |
| 404 const base::Closure& callback, | 483 const base::Closure& callback, |
| 405 const ErrorCallback& error_callback) {} | 484 const ErrorCallback& error_callback) { |
| 485 OnGattAttributeWriteRequest(device, descriptor, value, offset, callback, | |
| 486 error_callback); | |
| 487 } | |
| 406 | 488 |
| 407 void ArcBluetoothBridge::OnNotificationsStart( | 489 void ArcBluetoothBridge::OnNotificationsStart( |
| 408 const BluetoothDevice* device, | 490 const BluetoothDevice* device, |
| 409 const BluetoothLocalGattCharacteristic* characteristic) {} | 491 const BluetoothLocalGattCharacteristic* characteristic) {} |
| 410 | 492 |
| 411 void ArcBluetoothBridge::OnNotificationsStop( | 493 void ArcBluetoothBridge::OnNotificationsStop( |
| 412 const BluetoothDevice* device, | 494 const BluetoothDevice* device, |
| 413 const BluetoothLocalGattCharacteristic* characteristic) {} | 495 const BluetoothLocalGattCharacteristic* characteristic) {} |
| 414 | 496 |
| 415 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) { | 497 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) { |
| (...skipping 648 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1064 | 1146 |
| 1065 template <class LocalGattAttribute> | 1147 template <class LocalGattAttribute> |
| 1066 int32_t ArcBluetoothBridge::CreateGattAttributeHandle( | 1148 int32_t ArcBluetoothBridge::CreateGattAttributeHandle( |
| 1067 LocalGattAttribute* gatt_attr) { | 1149 LocalGattAttribute* gatt_attr) { |
| 1068 DCHECK(CalledOnValidThread()); | 1150 DCHECK(CalledOnValidThread()); |
| 1069 if (!gatt_attr) | 1151 if (!gatt_attr) |
| 1070 return kInvalidGattAttributeHandle; | 1152 return kInvalidGattAttributeHandle; |
| 1071 int32_t handle = next_gatt_server_attribute_handle(); | 1153 int32_t handle = next_gatt_server_attribute_handle(); |
| 1072 const std::string& identifier = gatt_attr->GetIdentifier(); | 1154 const std::string& identifier = gatt_attr->GetIdentifier(); |
| 1073 gatt_identifier_[handle] = identifier; | 1155 gatt_identifier_[handle] = identifier; |
| 1156 gatt_handle_[identifier] = handle; | |
| 1074 return handle; | 1157 return handle; |
| 1075 } | 1158 } |
| 1076 | 1159 |
| 1077 void ArcBluetoothBridge::AddService(mojom::BluetoothGattServiceIDPtr service_id, | 1160 void ArcBluetoothBridge::AddService(mojom::BluetoothGattServiceIDPtr service_id, |
| 1078 int32_t num_handles, | 1161 int32_t num_handles, |
| 1079 const AddServiceCallback& callback) { | 1162 const AddServiceCallback& callback) { |
| 1080 base::WeakPtr<BluetoothLocalGattService> service = | 1163 base::WeakPtr<BluetoothLocalGattService> service = |
| 1081 BluetoothLocalGattService::Create( | 1164 BluetoothLocalGattService::Create( |
| 1082 bluetooth_adapter_.get(), service_id->id->uuid.To<BluetoothUUID>(), | 1165 bluetooth_adapter_.get(), service_id->id->uuid.To<BluetoothUUID>(), |
| 1083 service_id->is_primary, nullptr /* included_service */, | 1166 service_id->is_primary, nullptr /* included_service */, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1164 base::Bind(&OnGattOperationError, callback)); | 1247 base::Bind(&OnGattOperationError, callback)); |
| 1165 } | 1248 } |
| 1166 | 1249 |
| 1167 void ArcBluetoothBridge::DeleteService(int32_t service_handle, | 1250 void ArcBluetoothBridge::DeleteService(int32_t service_handle, |
| 1168 const DeleteServiceCallback& callback) { | 1251 const DeleteServiceCallback& callback) { |
| 1169 DCHECK(CalledOnValidThread()); | 1252 DCHECK(CalledOnValidThread()); |
| 1170 DCHECK(gatt_identifier_.find(service_handle) != gatt_identifier_.end()); | 1253 DCHECK(gatt_identifier_.find(service_handle) != gatt_identifier_.end()); |
| 1171 BluetoothLocalGattService* service = | 1254 BluetoothLocalGattService* service = |
| 1172 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]); | 1255 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]); |
| 1173 DCHECK(service); | 1256 DCHECK(service); |
| 1174 | 1257 gatt_identifier_.erase(service_handle); |
| 1258 gatt_handle_.erase(service->GetIdentifier()); | |
| 1175 service->Delete(); | 1259 service->Delete(); |
| 1176 gatt_identifier_.erase(service_handle); | |
| 1177 OnGattOperationDone(callback); | 1260 OnGattOperationDone(callback); |
| 1178 } | 1261 } |
| 1179 | 1262 |
| 1180 void ArcBluetoothBridge::SendIndication( | 1263 void ArcBluetoothBridge::SendIndication( |
| 1181 int32_t attribute_handle, | 1264 int32_t attribute_handle, |
| 1182 mojom::BluetoothAddressPtr address, | 1265 mojom::BluetoothAddressPtr address, |
| 1183 bool confirm, | 1266 bool confirm, |
| 1184 mojo::Array<uint8_t> value, | 1267 mojo::Array<uint8_t> value, |
| 1185 const SendIndicationCallback& callback) {} | 1268 const SendIndicationCallback& callback) {} |
| 1186 | 1269 |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1519 LOG(WARNING) << "Bluetooth instance is too old (version " << version | 1602 LOG(WARNING) << "Bluetooth instance is too old (version " << version |
| 1520 << ") need version " << version_need; | 1603 << ") need version " << version_need; |
| 1521 return false; | 1604 return false; |
| 1522 } | 1605 } |
| 1523 | 1606 |
| 1524 bool ArcBluetoothBridge::CalledOnValidThread() { | 1607 bool ArcBluetoothBridge::CalledOnValidThread() { |
| 1525 return thread_checker_.CalledOnValidThread(); | 1608 return thread_checker_.CalledOnValidThread(); |
| 1526 } | 1609 } |
| 1527 | 1610 |
| 1528 } // namespace arc | 1611 } // namespace arc |
| OLD | NEW |