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 24 matching lines...) Expand all Loading... | |
| 87 element->end_handle = | 88 element->end_handle = |
| 88 ConvertGattIdentifierToId(gatt_attr->GetIdentifier()); | 89 ConvertGattIdentifierToId(gatt_attr->GetIdentifier()); |
| 89 element->properties = 0; | 90 element->properties = 0; |
| 90 return element; | 91 return element; |
| 91 } | 92 } |
| 92 | 93 |
| 93 // Find Gatt Service/Characteristic/Descriptor from std::vector using UUID. | 94 // Find Gatt Service/Characteristic/Descriptor from std::vector using UUID. |
| 94 template <class RemoteGattAttribute> | 95 template <class RemoteGattAttribute> |
| 95 RemoteGattAttribute* FindGattAttributeFromUuid( | 96 RemoteGattAttribute* FindGattAttributeFromUuid( |
| 96 const std::vector<RemoteGattAttribute*> gatt_attrs, | 97 const std::vector<RemoteGattAttribute*> gatt_attrs, |
| 97 const device::BluetoothUUID uuid) { | 98 const BluetoothUUID uuid) { |
| 98 auto it = std::find_if( | 99 auto it = std::find_if( |
| 99 gatt_attrs.begin(), gatt_attrs.end(), | 100 gatt_attrs.begin(), gatt_attrs.end(), |
| 100 [uuid](RemoteGattAttribute* attr) { return attr->GetUUID() == uuid; }); | 101 [uuid](RemoteGattAttribute* attr) { return attr->GetUUID() == uuid; }); |
| 101 if (it == gatt_attrs.end()) | 102 if (it == gatt_attrs.end()) |
| 102 return nullptr; | 103 return nullptr; |
| 103 return *it; | 104 return *it; |
| 104 } | 105 } |
| 105 | 106 |
| 106 // Common success callback for GATT operations that only need to report | 107 // Common success callback for GATT operations that only need to report |
| 107 // GattStatus back to Android. | 108 // GattStatus back to Android. |
| (...skipping 22 matching lines...) Expand all 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 if (!HasBluetoothInstance() || | |
| 409 !CheckBluetoothInstanceVersion(kMinGattServerVersion) || offset < 0) { | |
| 410 error_callback.Run(); | |
| 411 return; | |
| 412 } | |
| 413 | |
| 414 DCHECK(gatt_handle_.find(gatt_obj->GetIdentifier()) != gatt_handle_.end()); | |
| 415 | |
| 416 arc_bridge_service()->bluetooth()->instance()->RequestGattRead( | |
| 417 mojom::BluetoothAddress::From(device->GetAddress()), | |
| 418 gatt_handle_[gatt_obj->GetIdentifier()], offset, false /*is_long */, | |
|
Luis Héctor Chávez
2016/07/20 23:09:31
nit: /* is_long */
puthik_chromium
2016/07/21 00:05:10
Done.
| |
| 419 base::Bind(&OnGattServerRead, success_callback, error_callback)); | |
| 420 } | |
| 421 | |
| 422 template <class LocalGattAttribute> | |
| 423 void ArcBluetoothBridge::OnGattAttributeWriteRequest( | |
| 424 const BluetoothDevice* device, | |
| 425 const LocalGattAttribute* gatt_obj, | |
| 426 const std::vector<uint8_t>& value, | |
| 427 int offset, | |
| 428 const base::Closure& success_callback, | |
| 429 const ErrorCallback& error_callback) { | |
| 430 if (!HasBluetoothInstance() || | |
| 431 !CheckBluetoothInstanceVersion(kMinGattServerVersion) || offset < 0) { | |
| 432 error_callback.Run(); | |
| 433 return; | |
| 434 } | |
| 435 | |
| 436 DCHECK(gatt_handle_.find(gatt_obj->GetIdentifier()) != gatt_handle_.end()); | |
| 437 | |
| 438 arc_bridge_service()->bluetooth()->instance()->RequestGattWrite( | |
| 439 mojom::BluetoothAddress::From(device->GetAddress()), | |
| 440 gatt_handle_[gatt_obj->GetIdentifier()], offset, | |
| 441 mojo::Array<uint8_t>::From(value), | |
| 442 base::Bind(&OnGattServerWrite, success_callback, error_callback)); | |
| 443 } | |
| 444 | |
| 377 void ArcBluetoothBridge::OnCharacteristicReadRequest( | 445 void ArcBluetoothBridge::OnCharacteristicReadRequest( |
| 378 const BluetoothDevice* device, | 446 const BluetoothDevice* device, |
| 379 const BluetoothLocalGattCharacteristic* characteristic, | 447 const BluetoothLocalGattCharacteristic* characteristic, |
| 380 int offset, | 448 int offset, |
| 381 const ValueCallback& callback, | 449 const ValueCallback& callback, |
| 382 const ErrorCallback& error_callback) {} | 450 const ErrorCallback& error_callback) { |
| 451 OnGattAttributeReadRequest<BluetoothLocalGattCharacteristic>( | |
| 452 device, characteristic, offset, callback, error_callback); | |
| 453 } | |
| 383 | 454 |
| 384 void ArcBluetoothBridge::OnCharacteristicWriteRequest( | 455 void ArcBluetoothBridge::OnCharacteristicWriteRequest( |
| 385 const BluetoothDevice* device, | 456 const BluetoothDevice* device, |
| 386 const BluetoothLocalGattCharacteristic* characteristic, | 457 const BluetoothLocalGattCharacteristic* characteristic, |
| 387 const std::vector<uint8_t>& value, | 458 const std::vector<uint8_t>& value, |
| 388 int offset, | 459 int offset, |
| 389 const base::Closure& callback, | 460 const base::Closure& callback, |
| 390 const ErrorCallback& error_callback) {} | 461 const ErrorCallback& error_callback) { |
| 462 OnGattAttributeWriteRequest<BluetoothLocalGattCharacteristic>( | |
| 463 device, characteristic, value, offset, callback, error_callback); | |
| 464 } | |
| 391 | 465 |
| 392 void ArcBluetoothBridge::OnDescriptorReadRequest( | 466 void ArcBluetoothBridge::OnDescriptorReadRequest( |
| 393 const BluetoothDevice* device, | 467 const BluetoothDevice* device, |
| 394 const BluetoothLocalGattDescriptor* descriptor, | 468 const BluetoothLocalGattDescriptor* descriptor, |
| 395 int offset, | 469 int offset, |
| 396 const ValueCallback& callback, | 470 const ValueCallback& callback, |
| 397 const ErrorCallback& error_callback) {} | 471 const ErrorCallback& error_callback) { |
| 472 OnGattAttributeReadRequest<BluetoothLocalGattDescriptor>( | |
| 473 device, descriptor, offset, callback, error_callback); | |
| 474 } | |
| 398 | 475 |
| 399 void ArcBluetoothBridge::OnDescriptorWriteRequest( | 476 void ArcBluetoothBridge::OnDescriptorWriteRequest( |
| 400 const BluetoothDevice* device, | 477 const BluetoothDevice* device, |
| 401 const BluetoothLocalGattDescriptor* descriptor, | 478 const BluetoothLocalGattDescriptor* descriptor, |
| 402 const std::vector<uint8_t>& value, | 479 const std::vector<uint8_t>& value, |
| 403 int offset, | 480 int offset, |
| 404 const base::Closure& callback, | 481 const base::Closure& callback, |
| 405 const ErrorCallback& error_callback) {} | 482 const ErrorCallback& error_callback) { |
| 483 OnGattAttributeWriteRequest<BluetoothLocalGattDescriptor>( | |
| 484 device, descriptor, value, offset, callback, error_callback); | |
| 485 } | |
| 406 | 486 |
| 407 void ArcBluetoothBridge::OnNotificationsStart( | 487 void ArcBluetoothBridge::OnNotificationsStart( |
| 408 const BluetoothDevice* device, | 488 const BluetoothDevice* device, |
| 409 const BluetoothLocalGattCharacteristic* characteristic) {} | 489 const BluetoothLocalGattCharacteristic* characteristic) {} |
| 410 | 490 |
| 411 void ArcBluetoothBridge::OnNotificationsStop( | 491 void ArcBluetoothBridge::OnNotificationsStop( |
| 412 const BluetoothDevice* device, | 492 const BluetoothDevice* device, |
| 413 const BluetoothLocalGattCharacteristic* characteristic) {} | 493 const BluetoothLocalGattCharacteristic* characteristic) {} |
| 414 | 494 |
| 415 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) { | 495 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) { |
| (...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1058 | 1138 |
| 1059 template <class LocalGattAttribute> | 1139 template <class LocalGattAttribute> |
| 1060 int32_t ArcBluetoothBridge::CreateGattAttributeHandle( | 1140 int32_t ArcBluetoothBridge::CreateGattAttributeHandle( |
| 1061 LocalGattAttribute* gatt_attr) { | 1141 LocalGattAttribute* gatt_attr) { |
| 1062 DCHECK(CalledOnValidThread()); | 1142 DCHECK(CalledOnValidThread()); |
| 1063 if (!gatt_attr) | 1143 if (!gatt_attr) |
| 1064 return kInvalidGattAttributeHandle; | 1144 return kInvalidGattAttributeHandle; |
| 1065 int32_t handle = next_gatt_server_attribute_handle(); | 1145 int32_t handle = next_gatt_server_attribute_handle(); |
| 1066 const std::string& identifier = gatt_attr->GetIdentifier(); | 1146 const std::string& identifier = gatt_attr->GetIdentifier(); |
| 1067 gatt_identifier_[handle] = identifier; | 1147 gatt_identifier_[handle] = identifier; |
| 1148 gatt_handle_[identifier] = handle; | |
| 1068 return handle; | 1149 return handle; |
| 1069 } | 1150 } |
| 1070 | 1151 |
| 1071 void ArcBluetoothBridge::AddService(mojom::BluetoothGattServiceIDPtr service_id, | 1152 void ArcBluetoothBridge::AddService(mojom::BluetoothGattServiceIDPtr service_id, |
| 1072 int32_t num_handles, | 1153 int32_t num_handles, |
| 1073 const AddServiceCallback& callback) { | 1154 const AddServiceCallback& callback) { |
| 1074 base::WeakPtr<BluetoothLocalGattService> service = | 1155 base::WeakPtr<BluetoothLocalGattService> service = |
| 1075 BluetoothLocalGattService::Create( | 1156 BluetoothLocalGattService::Create( |
| 1076 bluetooth_adapter_.get(), service_id->id->uuid.To<BluetoothUUID>(), | 1157 bluetooth_adapter_.get(), service_id->id->uuid.To<BluetoothUUID>(), |
| 1077 service_id->is_primary, nullptr /*included_service */, | 1158 service_id->is_primary, nullptr /*included_service */, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1156 service->Unregister(base::Bind(&OnGattOperationDone, callback), | 1237 service->Unregister(base::Bind(&OnGattOperationDone, callback), |
| 1157 base::Bind(&OnGattOperationError, callback)); | 1238 base::Bind(&OnGattOperationError, callback)); |
| 1158 } | 1239 } |
| 1159 | 1240 |
| 1160 void ArcBluetoothBridge::DeleteService(int32_t service_handle, | 1241 void ArcBluetoothBridge::DeleteService(int32_t service_handle, |
| 1161 const DeleteServiceCallback& callback) { | 1242 const DeleteServiceCallback& callback) { |
| 1162 DCHECK(gatt_identifier_.find(service_handle) != gatt_identifier_.end()); | 1243 DCHECK(gatt_identifier_.find(service_handle) != gatt_identifier_.end()); |
| 1163 BluetoothLocalGattService* service = | 1244 BluetoothLocalGattService* service = |
| 1164 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]); | 1245 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]); |
| 1165 DCHECK(service); | 1246 DCHECK(service); |
| 1166 | 1247 gatt_identifier_.erase(service_handle); |
| 1248 gatt_handle_.erase(service->GetIdentifier()); | |
| 1167 service->Delete(); | 1249 service->Delete(); |
| 1168 gatt_identifier_.erase(service_handle); | |
| 1169 OnGattOperationDone(callback); | 1250 OnGattOperationDone(callback); |
| 1170 } | 1251 } |
| 1171 | 1252 |
| 1172 void ArcBluetoothBridge::SendIndication( | 1253 void ArcBluetoothBridge::SendIndication( |
| 1173 int32_t attribute_handle, | 1254 int32_t attribute_handle, |
| 1174 mojom::BluetoothAddressPtr address, | 1255 mojom::BluetoothAddressPtr address, |
| 1175 bool confirm, | 1256 bool confirm, |
| 1176 mojo::Array<uint8_t> value, | 1257 mojo::Array<uint8_t> value, |
| 1177 const SendIndicationCallback& callback) {} | 1258 const SendIndicationCallback& callback) {} |
| 1178 | 1259 |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1511 LOG(WARNING) << "Bluetooth instance is too old (version " << version | 1592 LOG(WARNING) << "Bluetooth instance is too old (version " << version |
| 1512 << ") need version " << version_need; | 1593 << ") need version " << version_need; |
| 1513 return false; | 1594 return false; |
| 1514 } | 1595 } |
| 1515 | 1596 |
| 1516 bool ArcBluetoothBridge::CalledOnValidThread() { | 1597 bool ArcBluetoothBridge::CalledOnValidThread() { |
| 1517 return thread_checker_.CalledOnValidThread(); | 1598 return thread_checker_.CalledOnValidThread(); |
| 1518 } | 1599 } |
| 1519 | 1600 |
| 1520 } // namespace arc | 1601 } // namespace arc |
| OLD | NEW |