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> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 20 #include "components/arc/arc_bridge_service.h" | 20 #include "components/arc/arc_bridge_service.h" |
| 21 #include "components/arc/bluetooth/bluetooth_type_converters.h" | 21 #include "components/arc/bluetooth/bluetooth_type_converters.h" |
| 22 #include "device/bluetooth/bluetooth_adapter_factory.h" | 22 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 23 #include "device/bluetooth/bluetooth_common.h" | 23 #include "device/bluetooth/bluetooth_common.h" |
| 24 #include "device/bluetooth/bluetooth_device.h" | 24 #include "device/bluetooth/bluetooth_device.h" |
| 25 #include "device/bluetooth/bluetooth_gatt_connection.h" | 25 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 26 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | 26 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
| 27 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 27 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
| 28 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" | 28 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" |
| 29 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 29 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
| 30 #include "device/bluetooth/bluez/bluetooth_device_bluez.h" | |
| 30 | 31 |
| 31 using device::BluetoothAdapter; | 32 using device::BluetoothAdapter; |
| 32 using device::BluetoothAdapterFactory; | 33 using device::BluetoothAdapterFactory; |
| 33 using device::BluetoothAdvertisement; | 34 using device::BluetoothAdvertisement; |
| 34 using device::BluetoothDevice; | 35 using device::BluetoothDevice; |
| 35 using device::BluetoothDiscoveryFilter; | 36 using device::BluetoothDiscoveryFilter; |
| 36 using device::BluetoothDiscoverySession; | 37 using device::BluetoothDiscoverySession; |
| 37 using device::BluetoothGattConnection; | 38 using device::BluetoothGattConnection; |
| 38 using device::BluetoothGattNotifySession; | 39 using device::BluetoothGattNotifySession; |
| 39 using device::BluetoothGattCharacteristic; | 40 using device::BluetoothGattCharacteristic; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 50 constexpr uint32_t kGattReadPermission = | 51 constexpr uint32_t kGattReadPermission = |
| 51 BluetoothGattCharacteristic::Permission::PERMISSION_READ | | 52 BluetoothGattCharacteristic::Permission::PERMISSION_READ | |
| 52 BluetoothGattCharacteristic::Permission::PERMISSION_READ_ENCRYPTED | | 53 BluetoothGattCharacteristic::Permission::PERMISSION_READ_ENCRYPTED | |
| 53 BluetoothGattCharacteristic::Permission:: | 54 BluetoothGattCharacteristic::Permission:: |
| 54 PERMISSION_READ_ENCRYPTED_AUTHENTICATED; | 55 PERMISSION_READ_ENCRYPTED_AUTHENTICATED; |
| 55 constexpr uint32_t kGattWritePermission = | 56 constexpr uint32_t kGattWritePermission = |
| 56 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE | | 57 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE | |
| 57 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE_ENCRYPTED | | 58 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE_ENCRYPTED | |
| 58 BluetoothGattCharacteristic::Permission:: | 59 BluetoothGattCharacteristic::Permission:: |
| 59 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED; | 60 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED; |
| 61 constexpr uint16_t kServiceClassIDListAttributeID = 0x0001; | |
| 62 | |
| 63 using GetSdpRecordsCallback = | |
| 64 base::Callback<void(arc::mojom::BluetoothGetSdpRecordsResultPtr)>; | |
| 65 using CreateSdpRecordCallback = | |
| 66 base::Callback<void(arc::mojom::BluetoothCreateSdpRecordResultPtr)>; | |
| 67 using RemoveSdpRecordCallback = | |
| 68 base::Callback<void(arc::mojom::BluetoothStatus)>; | |
| 69 | |
| 70 void OnGetServiceRecordsDone( | |
| 71 const GetSdpRecordsCallback& callback, | |
| 72 const std::vector<bluez::BluetoothServiceRecordBlueZ>& records) { | |
| 73 arc::mojom::BluetoothGetSdpRecordsResultPtr result = | |
| 74 arc::mojom::BluetoothGetSdpRecordsResult::New(); | |
| 75 | |
| 76 result->status = arc::mojom::BluetoothStatus::SUCCESS; | |
| 77 | |
| 78 for (auto& rcd : records) { | |
| 79 result->records.push_back( | |
| 80 mojo::ConvertTo<arc::mojom::BluetoothSdpRecordPtr>(rcd)); | |
| 81 } | |
| 82 | |
| 83 callback.Run(std::move(result)); | |
| 84 } | |
| 85 | |
| 86 void OnGetServiceRecordsError( | |
| 87 const GetSdpRecordsCallback& callback, | |
| 88 bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code) { | |
| 89 arc::mojom::BluetoothGetSdpRecordsResultPtr result = | |
| 90 arc::mojom::BluetoothGetSdpRecordsResult::New(); | |
| 91 | |
| 92 switch (error_code) { | |
| 93 case bluez::BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY: | |
| 94 result->status = arc::mojom::BluetoothStatus::NOT_READY; | |
| 95 break; | |
| 96 case bluez::BluetoothServiceRecordBlueZ::ErrorCode:: | |
| 97 ERROR_DEVICE_DISCONNECTED: | |
| 98 result->status = arc::mojom::BluetoothStatus::RMT_DEV_DOWN; | |
| 99 break; | |
| 100 default: | |
| 101 result->status = arc::mojom::BluetoothStatus::FAIL; | |
| 102 break; | |
| 103 } | |
| 104 | |
| 105 callback.Run(std::move(result)); | |
| 106 } | |
| 107 | |
| 108 void OnCreateServiceRecordDone( | |
| 109 const CreateSdpRecordCallback& callback, | |
| 110 uint32_t service_handle) { | |
| 111 arc::mojom::BluetoothCreateSdpRecordResultPtr result = | |
| 112 arc::mojom::BluetoothCreateSdpRecordResult::New(); | |
| 113 result->status = arc::mojom::BluetoothStatus::SUCCESS; | |
| 114 result->service_handle = service_handle; | |
| 115 | |
| 116 callback.Run(std::move(result)); | |
| 117 } | |
| 118 | |
| 119 void OnCreateServiceRecordError( | |
| 120 const CreateSdpRecordCallback& callback, | |
| 121 bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code) { | |
| 122 arc::mojom::BluetoothCreateSdpRecordResultPtr result = | |
| 123 arc::mojom::BluetoothCreateSdpRecordResult::New(); | |
| 124 if (error_code == | |
| 125 bluez::BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY) | |
| 126 result->status = arc::mojom::BluetoothStatus::NOT_READY; | |
| 127 else | |
| 128 result->status = arc::mojom::BluetoothStatus::FAIL; | |
| 129 | |
| 130 callback.Run(std::move(result)); | |
| 131 } | |
| 132 | |
| 133 void OnRemoveServiceRecordDone( | |
| 134 const RemoveSdpRecordCallback& callback) { | |
| 135 callback.Run(arc::mojom::BluetoothStatus::SUCCESS); | |
| 136 } | |
| 137 | |
| 138 void OnRemoveServiceRecordError( | |
| 139 const RemoveSdpRecordCallback& callback, | |
| 140 bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code) { | |
| 141 arc::mojom::BluetoothStatus status; | |
| 142 if (error_code == | |
| 143 bluez::BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY) | |
| 144 status = arc::mojom::BluetoothStatus::NOT_READY; | |
| 145 else | |
| 146 status = arc::mojom::BluetoothStatus::FAIL; | |
| 147 | |
| 148 callback.Run(status); | |
| 149 } | |
| 150 | |
| 60 } // namespace | 151 } // namespace |
| 61 | 152 |
| 62 namespace arc { | 153 namespace arc { |
| 63 | 154 |
| 64 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) | 155 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) |
| 65 : ArcService(bridge_service), binding_(this), weak_factory_(this) { | 156 : ArcService(bridge_service), binding_(this), weak_factory_(this) { |
| 66 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 157 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 67 VLOG(1) << "registering bluetooth adapter"; | 158 VLOG(1) << "registering bluetooth adapter"; |
| 68 BluetoothAdapterFactory::GetAdapter(base::Bind( | 159 BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 69 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); | 160 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); |
| (...skipping 947 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1017 | 1108 |
| 1018 void ArcBluetoothBridge::ReadRemoteRssi( | 1109 void ArcBluetoothBridge::ReadRemoteRssi( |
| 1019 mojom::BluetoothAddressPtr remote_addr, | 1110 mojom::BluetoothAddressPtr remote_addr, |
| 1020 const ReadRemoteRssiCallback& callback) { | 1111 const ReadRemoteRssiCallback& callback) { |
| 1021 BluetoothDevice* device = | 1112 BluetoothDevice* device = |
| 1022 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | 1113 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
| 1023 int rssi = device->GetInquiryRSSI(); | 1114 int rssi = device->GetInquiryRSSI(); |
| 1024 callback.Run(rssi); | 1115 callback.Run(rssi); |
| 1025 } | 1116 } |
| 1026 | 1117 |
| 1118 void ArcBluetoothBridge::GetSdpRecords( | |
| 1119 arc::mojom::BluetoothAddressPtr remote_addr, | |
| 1120 const GetSdpRecordsCallback& callback) { | |
| 1121 BluetoothDevice* device = | |
| 1122 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | |
| 1123 | |
| 1124 // Do an early return if there is no device with |remote_addr|. | |
| 1125 if (!device) { | |
| 1126 arc::mojom::BluetoothGetSdpRecordsResultPtr result = | |
| 1127 arc::mojom::BluetoothGetSdpRecordsResult::New(); | |
| 1128 result->status = arc::mojom::BluetoothStatus::FAIL; | |
| 1129 callback.Run(std::move(result)); | |
| 1130 return; | |
| 1131 } | |
| 1132 | |
| 1133 bluez::BluetoothDeviceBlueZ* device_bluez = | |
| 1134 static_cast<bluez::BluetoothDeviceBlueZ*>(device); | |
| 1135 | |
| 1136 device_bluez->GetServiceRecords( | |
| 1137 base::Bind(&OnGetServiceRecordsDone, callback), | |
| 1138 base::Bind(&OnGetServiceRecordsError, callback)); | |
| 1139 } | |
| 1140 | |
| 1141 void ArcBluetoothBridge::CreateSdpRecord( | |
| 1142 arc::mojom::BluetoothSdpRecordPtr record, | |
| 1143 const CreateSdpRecordCallback& callback) { | |
| 1144 bluez::BluetoothServiceRecordBlueZ rcd_bluez( | |
| 1145 mojo::ConvertTo<bluez::BluetoothServiceRecordBlueZ>(record)); | |
| 1146 | |
| 1147 std::vector<uint16_t> v = rcd_bluez.GetAttributeIds(); | |
| 1148 | |
| 1149 // Check if ServiceClassIDList attribute (attribute ID 0x0001) is included | |
| 1150 // after type conversion, since it is mandatory for creating a service record. | |
| 1151 bool found = false; | |
| 1152 for (auto id : v) { | |
| 1153 if (id == kServiceClassIDListAttributeID) { | |
| 1154 found = true; | |
| 1155 break; | |
| 1156 } | |
| 1157 } | |
| 1158 | |
| 1159 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.
| |
| 1160 arc::mojom::BluetoothCreateSdpRecordResultPtr result = | |
| 1161 mojom::BluetoothCreateSdpRecordResult::New(); | |
| 1162 result->status = arc::mojom::BluetoothStatus::FAIL; | |
| 1163 callback.Run(std::move(result)); | |
| 1164 return; | |
| 1165 } | |
| 1166 | |
| 1167 bluetooth_adapter_->CreateServiceRecord( | |
| 1168 rcd_bluez, base::Bind(&OnCreateServiceRecordDone, callback), | |
| 1169 base::Bind(&OnCreateServiceRecordError, callback)); | |
| 1170 } | |
| 1171 | |
| 1172 void ArcBluetoothBridge::RemoveSdpRecord( | |
| 1173 uint32_t service_handle, | |
| 1174 const RemoveSdpRecordCallback& callback) { | |
| 1175 bluetooth_adapter_->RemoveServiceRecord( | |
| 1176 service_handle, base::Bind(&OnRemoveServiceRecordDone, callback), | |
| 1177 base::Bind(&OnRemoveServiceRecordError, callback)); | |
| 1178 } | |
| 1179 | |
| 1027 void ArcBluetoothBridge::OnDiscoveryError() { | 1180 void ArcBluetoothBridge::OnDiscoveryError() { |
| 1028 LOG(WARNING) << "failed to change discovery state"; | 1181 LOG(WARNING) << "failed to change discovery state"; |
| 1029 } | 1182 } |
| 1030 | 1183 |
| 1031 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { | 1184 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { |
| 1032 if (!HasBluetoothInstance()) | 1185 if (!HasBluetoothInstance()) |
| 1033 return; | 1186 return; |
| 1034 | 1187 |
| 1035 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1188 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( |
| 1036 mojom::BluetoothStatus::SUCCESS, std::move(addr), | 1189 mojom::BluetoothStatus::SUCCESS, std::move(addr), |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1355 int32_t version_need) const { | 1508 int32_t version_need) const { |
| 1356 int32_t version = arc_bridge_service()->bluetooth_version(); | 1509 int32_t version = arc_bridge_service()->bluetooth_version(); |
| 1357 if (version >= version_need) | 1510 if (version >= version_need) |
| 1358 return true; | 1511 return true; |
| 1359 LOG(WARNING) << "Bluetooth instance is too old (version " << version | 1512 LOG(WARNING) << "Bluetooth instance is too old (version " << version |
| 1360 << ") need version " << version_need; | 1513 << ") need version " << version_need; |
| 1361 return false; | 1514 return false; |
| 1362 } | 1515 } |
| 1363 | 1516 |
| 1364 } // namespace arc | 1517 } // namespace arc |
| OLD | NEW |