Chromium Code Reviews| Index: components/arc/bluetooth/arc_bluetooth_bridge.cc |
| diff --git a/components/arc/bluetooth/arc_bluetooth_bridge.cc b/components/arc/bluetooth/arc_bluetooth_bridge.cc |
| index 5ba17cc2dcdd91aee6c212d06c266d3a27e92605..c3cae38be7fdad1c387f00273b96dc93bf1d3f58 100644 |
| --- a/components/arc/bluetooth/arc_bluetooth_bridge.cc |
| +++ b/components/arc/bluetooth/arc_bluetooth_bridge.cc |
| @@ -27,6 +27,7 @@ |
| #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
| #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" |
| #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
| +#include "device/bluetooth/bluez/bluetooth_device_bluez.h" |
| using device::BluetoothAdapter; |
| using device::BluetoothAdapterFactory; |
| @@ -57,6 +58,96 @@ constexpr uint32_t kGattWritePermission = |
| BluetoothGattCharacteristic::Permission::PERMISSION_WRITE_ENCRYPTED | |
| BluetoothGattCharacteristic::Permission:: |
| PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED; |
| +constexpr uint16_t kServiceClassIDListAttributeID = 0x0001; |
| + |
| +using GetSdpRecordsCallback = |
| + base::Callback<void(arc::mojom::BluetoothGetSdpRecordsResultPtr)>; |
| +using CreateSdpRecordCallback = |
| + base::Callback<void(arc::mojom::BluetoothCreateSdpRecordResultPtr)>; |
| +using RemoveSdpRecordCallback = |
| + base::Callback<void(arc::mojom::BluetoothStatus)>; |
| + |
| +void OnGetServiceRecordsDone( |
| + const GetSdpRecordsCallback& callback, |
| + const std::vector<bluez::BluetoothServiceRecordBlueZ>& records) { |
| + arc::mojom::BluetoothGetSdpRecordsResultPtr result = |
| + arc::mojom::BluetoothGetSdpRecordsResult::New(); |
| + |
| + result->status = arc::mojom::BluetoothStatus::SUCCESS; |
| + |
| + for (auto& rcd : records) { |
| + result->records.push_back( |
| + mojo::ConvertTo<arc::mojom::BluetoothSdpRecordPtr>(rcd)); |
| + } |
| + |
| + callback.Run(std::move(result)); |
| +} |
| + |
| +void OnGetServiceRecordsError( |
| + const GetSdpRecordsCallback& callback, |
| + bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code) { |
| + arc::mojom::BluetoothGetSdpRecordsResultPtr result = |
| + arc::mojom::BluetoothGetSdpRecordsResult::New(); |
| + |
| + switch (error_code) { |
| + case bluez::BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY: |
| + result->status = arc::mojom::BluetoothStatus::NOT_READY; |
| + break; |
| + case bluez::BluetoothServiceRecordBlueZ::ErrorCode:: |
| + ERROR_DEVICE_DISCONNECTED: |
| + result->status = arc::mojom::BluetoothStatus::RMT_DEV_DOWN; |
| + break; |
| + default: |
| + result->status = arc::mojom::BluetoothStatus::FAIL; |
| + break; |
| + } |
| + |
| + callback.Run(std::move(result)); |
| +} |
| + |
| +void OnCreateServiceRecordDone( |
| + const CreateSdpRecordCallback& callback, |
| + uint32_t service_handle) { |
| + arc::mojom::BluetoothCreateSdpRecordResultPtr result = |
| + arc::mojom::BluetoothCreateSdpRecordResult::New(); |
| + result->status = arc::mojom::BluetoothStatus::SUCCESS; |
| + result->service_handle = service_handle; |
| + |
| + callback.Run(std::move(result)); |
| +} |
| + |
| +void OnCreateServiceRecordError( |
| + const CreateSdpRecordCallback& callback, |
| + bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code) { |
| + arc::mojom::BluetoothCreateSdpRecordResultPtr result = |
| + arc::mojom::BluetoothCreateSdpRecordResult::New(); |
| + if (error_code == |
| + bluez::BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY) |
| + result->status = arc::mojom::BluetoothStatus::NOT_READY; |
| + else |
| + result->status = arc::mojom::BluetoothStatus::FAIL; |
| + |
| + callback.Run(std::move(result)); |
| +} |
| + |
| +void OnRemoveServiceRecordDone( |
| + const RemoveSdpRecordCallback& callback) { |
| + callback.Run(arc::mojom::BluetoothStatus::SUCCESS); |
| +} |
| + |
| +void OnRemoveServiceRecordError( |
| + const RemoveSdpRecordCallback& callback, |
| + bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code) { |
| + arc::mojom::BluetoothStatus status; |
| + if (error_code == |
| + bluez::BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY) |
| + status = arc::mojom::BluetoothStatus::NOT_READY; |
| + else |
| + status = arc::mojom::BluetoothStatus::FAIL; |
| + |
| + callback.Run(status); |
| +} |
| + |
| } // namespace |
| namespace arc { |
| @@ -1024,6 +1115,68 @@ void ArcBluetoothBridge::ReadRemoteRssi( |
| callback.Run(rssi); |
| } |
| +void ArcBluetoothBridge::GetSdpRecords( |
| + arc::mojom::BluetoothAddressPtr remote_addr, |
| + const GetSdpRecordsCallback& callback) { |
| + BluetoothDevice* device = |
| + bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
| + |
| + // Do an early return if there is no device with |remote_addr|. |
| + if (!device) { |
| + arc::mojom::BluetoothGetSdpRecordsResultPtr result = |
| + arc::mojom::BluetoothGetSdpRecordsResult::New(); |
| + result->status = arc::mojom::BluetoothStatus::FAIL; |
| + callback.Run(std::move(result)); |
| + return; |
| + } |
| + |
| + bluez::BluetoothDeviceBlueZ* device_bluez = |
| + static_cast<bluez::BluetoothDeviceBlueZ*>(device); |
| + |
| + device_bluez->GetServiceRecords( |
| + base::Bind(&OnGetServiceRecordsDone, callback), |
| + base::Bind(&OnGetServiceRecordsError, callback)); |
| +} |
| + |
| +void ArcBluetoothBridge::CreateSdpRecord( |
| + arc::mojom::BluetoothSdpRecordPtr record, |
| + const CreateSdpRecordCallback& callback) { |
| + bluez::BluetoothServiceRecordBlueZ rcd_bluez( |
| + mojo::ConvertTo<bluez::BluetoothServiceRecordBlueZ>(record)); |
| + |
| + std::vector<uint16_t> v = rcd_bluez.GetAttributeIds(); |
| + |
| + // Check if ServiceClassIDList attribute (attribute ID 0x0001) is included |
| + // after type conversion, since it is mandatory for creating a service record. |
| + bool found = false; |
| + for (auto id : v) { |
| + if (id == kServiceClassIDListAttributeID) { |
| + found = true; |
| + break; |
| + } |
| + } |
| + |
| + if (!found) { |
|
Luis Héctor Chávez
2016/07/18 15:42:02
nit:
if (std::find(v.begin(), v.end(),
Miao
2016/07/26 07:20:45
Done.
|
| + arc::mojom::BluetoothCreateSdpRecordResultPtr result = |
| + mojom::BluetoothCreateSdpRecordResult::New(); |
| + result->status = arc::mojom::BluetoothStatus::FAIL; |
| + callback.Run(std::move(result)); |
| + return; |
| + } |
| + |
| + bluetooth_adapter_->CreateServiceRecord( |
| + rcd_bluez, base::Bind(&OnCreateServiceRecordDone, callback), |
| + base::Bind(&OnCreateServiceRecordError, callback)); |
| +} |
| + |
| +void ArcBluetoothBridge::RemoveSdpRecord( |
| + uint32_t service_handle, |
| + const RemoveSdpRecordCallback& callback) { |
| + bluetooth_adapter_->RemoveServiceRecord( |
| + service_handle, base::Bind(&OnRemoveServiceRecordDone, callback), |
| + base::Bind(&OnRemoveServiceRecordError, callback)); |
| +} |
| + |
| void ArcBluetoothBridge::OnDiscoveryError() { |
| LOG(WARNING) << "failed to change discovery state"; |
| } |