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 916 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 956 const RegisterForGattNotificationCallback& callback, | 957 const RegisterForGattNotificationCallback& callback, |
| 957 BluetoothGattService::GattErrorCode error_code) const { | 958 BluetoothGattService::GattErrorCode error_code) const { |
| 958 callback.Run(mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code)); | 959 callback.Run(mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code)); |
| 959 } | 960 } |
| 960 | 961 |
| 961 void ArcBluetoothBridge::OnGattNotifyStopDone( | 962 void ArcBluetoothBridge::OnGattNotifyStopDone( |
| 962 const DeregisterForGattNotificationCallback& callback) const { | 963 const DeregisterForGattNotificationCallback& callback) const { |
| 963 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); | 964 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); |
| 964 } | 965 } |
| 965 | 966 |
| 967 void ArcBluetoothBridge::OnGetServiceRecordsDone( | |
|
Luis Héctor Chávez
2016/07/14 23:13:14
This function doesn't seem to need any members fro
Miao
2016/07/15 08:39:21
Done.
| |
| 968 const GetSdpRecordsCallback& callback, | |
| 969 const std::vector<bluez::BluetoothServiceRecordBlueZ>& records) const { | |
| 970 arc::mojom::BluetoothGetSdpRecordsResultPtr result = | |
| 971 arc::mojom::BluetoothGetSdpRecordsResult::New(); | |
| 972 | |
| 973 result->status = mojom::BluetoothStatus::SUCCESS; | |
| 974 | |
| 975 if (records.empty()) { | |
| 976 callback.Run(std::move(result)); | |
| 977 return; | |
| 978 } | |
|
puthik_chromium
2016/07/14 01:32:34
Remove line 975-978?
This codes does not change an
Miao
2016/07/15 08:39:21
Done.
| |
| 979 | |
| 980 for (auto& rcd : records) | |
|
Luis Héctor Chávez
2016/07/14 23:13:14
braces are required if the only statement is line-
Miao
2016/07/15 08:39:21
Done.
| |
| 981 result->records.push_back( | |
| 982 std::move(mojo::ConvertTo<arc::mojom::BluetoothSdpRecordPtr>(rcd))); | |
| 983 | |
| 984 callback.Run(std::move(result)); | |
| 985 } | |
| 986 | |
| 987 void ArcBluetoothBridge::OnGetServiceRecordsError( | |
| 988 const GetSdpRecordsCallback& callback, | |
| 989 bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code) const { | |
| 990 arc::mojom::BluetoothGetSdpRecordsResultPtr result = | |
| 991 arc::mojom::BluetoothGetSdpRecordsResult::New(); | |
| 992 | |
| 993 switch(error_code) { | |
| 994 case bluez::BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY: | |
| 995 result->status = mojom::BluetoothStatus::NOT_READY; | |
| 996 break; | |
| 997 case bluez::BluetoothServiceRecordBlueZ::ErrorCode:: | |
| 998 ERROR_DEVICE_DISCONNECTED: | |
| 999 result->status = mojom::BluetoothStatus::RMT_DEV_DOWN; | |
| 1000 break; | |
| 1001 default: | |
| 1002 result->status = mojom::BluetoothStatus::FAIL; | |
| 1003 break; | |
| 1004 } | |
| 1005 | |
| 1006 callback.Run(std::move(result)); | |
| 1007 } | |
| 1008 | |
| 1009 void ArcBluetoothBridge::OnCreateSdpRecordDone( | |
| 1010 const CreateSdpRecordCallback& callback, | |
| 1011 uint32_t service_handle) const { | |
| 1012 arc::mojom::BluetoothCreateSdpRecordResultPtr result = | |
| 1013 arc::mojom::BluetoothCreateSdpRecordResult::New(); | |
| 1014 result->status = mojom::BluetoothStatus::SUCCESS; | |
| 1015 result->service_handle = service_handle; | |
| 1016 | |
| 1017 callback.Run(std::move(result)); | |
| 1018 } | |
| 1019 | |
| 1020 void ArcBluetoothBridge::OnCreateSdpRecordError( | |
| 1021 const CreateSdpRecordCallback& callback, | |
| 1022 bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code) const { | |
| 1023 arc::mojom::BluetoothCreateSdpRecordResultPtr result = | |
| 1024 arc::mojom::BluetoothCreateSdpRecordResult::New(); | |
| 1025 if (error_code == | |
| 1026 bluez::BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY) | |
| 1027 result->status = mojom::BluetoothStatus::NOT_READY; | |
| 1028 else | |
| 1029 result->status = mojom::BluetoothStatus::FAIL; | |
| 1030 | |
| 1031 callback.Run(std::move(result)); | |
| 1032 } | |
| 1033 | |
| 1034 void ArcBluetoothBridge::OnRemoveSdpRecordDone( | |
| 1035 const RemoveSdpRecordCallback& callback) const { | |
| 1036 callback.Run(mojom::BluetoothStatus::SUCCESS); | |
| 1037 } | |
| 1038 | |
| 1039 void ArcBluetoothBridge::OnRemoveSdpRecordError( | |
| 1040 const RemoveSdpRecordCallback& callback, | |
| 1041 bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code) const { | |
| 1042 arc::mojom::BluetoothStatus status; | |
| 1043 if (error_code == | |
| 1044 bluez::BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY) | |
| 1045 status = mojom::BluetoothStatus::NOT_READY; | |
| 1046 else | |
| 1047 status = mojom::BluetoothStatus::FAIL; | |
| 1048 | |
| 1049 callback.Run(status); | |
| 1050 } | |
| 1051 | |
| 966 void ArcBluetoothBridge::RegisterForGattNotification( | 1052 void ArcBluetoothBridge::RegisterForGattNotification( |
| 967 mojom::BluetoothAddressPtr remote_addr, | 1053 mojom::BluetoothAddressPtr remote_addr, |
| 968 mojom::BluetoothGattServiceIDPtr service_id, | 1054 mojom::BluetoothGattServiceIDPtr service_id, |
| 969 mojom::BluetoothGattIDPtr char_id, | 1055 mojom::BluetoothGattIDPtr char_id, |
| 970 const RegisterForGattNotificationCallback& callback) { | 1056 const RegisterForGattNotificationCallback& callback) { |
| 971 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( | 1057 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( |
| 972 std::move(remote_addr), std::move(service_id), std::move(char_id)); | 1058 std::move(remote_addr), std::move(service_id), std::move(char_id)); |
| 973 | 1059 |
| 974 if (!characteristic) { | 1060 if (!characteristic) { |
| 975 LOG(WARNING) << __func__ << " Characteristic is not existed."; | 1061 LOG(WARNING) << __func__ << " Characteristic is not existed."; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1017 | 1103 |
| 1018 void ArcBluetoothBridge::ReadRemoteRssi( | 1104 void ArcBluetoothBridge::ReadRemoteRssi( |
| 1019 mojom::BluetoothAddressPtr remote_addr, | 1105 mojom::BluetoothAddressPtr remote_addr, |
| 1020 const ReadRemoteRssiCallback& callback) { | 1106 const ReadRemoteRssiCallback& callback) { |
| 1021 BluetoothDevice* device = | 1107 BluetoothDevice* device = |
| 1022 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | 1108 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
| 1023 int rssi = device->GetInquiryRSSI(); | 1109 int rssi = device->GetInquiryRSSI(); |
| 1024 callback.Run(rssi); | 1110 callback.Run(rssi); |
| 1025 } | 1111 } |
| 1026 | 1112 |
| 1113 void ArcBluetoothBridge::GetSdpRecords( | |
| 1114 arc::mojom::BluetoothAddressPtr remote_addr, | |
| 1115 const GetSdpRecordsCallback& callback) { | |
| 1116 BluetoothDevice* device = | |
| 1117 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | |
| 1118 | |
| 1119 // Do an early return if there is no device with |remote_addr|. | |
| 1120 if (!device) { | |
| 1121 arc::mojom::BluetoothGetSdpRecordsResultPtr result = | |
| 1122 arc::mojom::BluetoothGetSdpRecordsResult::New(); | |
| 1123 result->status = arc::mojom::BluetoothStatus::SUCCESS; | |
|
puthik_chromium
2016/07/14 01:32:34
Why success if there is no device?
Miao
2016/07/15 08:39:21
This should be FAIL instead.
| |
| 1124 callback.Run(std::move(result)); | |
| 1125 return; | |
| 1126 } | |
| 1127 | |
| 1128 bluez::BluetoothDeviceBlueZ* device_bluez = | |
| 1129 static_cast<bluez::BluetoothDeviceBlueZ*>(device); | |
| 1130 | |
| 1131 device_bluez->GetServiceRecords( | |
| 1132 base::Bind(&ArcBluetoothBridge::OnGetServiceRecordsDone, | |
| 1133 weak_factory_.GetWeakPtr(), callback), | |
| 1134 base::Bind(&ArcBluetoothBridge::OnGetServiceRecordsError, | |
| 1135 weak_factory_.GetWeakPtr(), callback)); | |
| 1136 } | |
| 1137 | |
| 1138 void ArcBluetoothBridge::CreateSdpRecord( | |
| 1139 arc::mojom::BluetoothSdpRecordPtr record, | |
| 1140 const CreateSdpRecordCallback& callback) { | |
| 1141 bluez::BluetoothServiceRecordBlueZ rcd_bluez( | |
| 1142 mojo::ConvertTo<bluez::BluetoothServiceRecordBlueZ>(record)); | |
| 1143 | |
| 1144 std::vector<uint16_t> v = rcd_bluez.GetAttributeIds(); | |
| 1145 std::set<uint16_t> ids (v.begin(), v.end()); | |
|
puthik_chromium
2016/07/14 01:32:34
Why do we need both vector and set?
Luis Héctor Chávez
2016/07/14 23:13:14
Prefer std::unordered_set if you are searching for
Miao
2016/07/15 08:39:21
Removed set and find the element in the vector ins
| |
| 1146 | |
| 1147 // Check if ServiceClassIDList attribute (attribute ID 0x0001) is included | |
| 1148 // after type conversion, since it is mandatory for creating a service record. | |
| 1149 if (ids.find(0x0001) == ids.end()) { | |
|
puthik_chromium
2016/07/14 01:32:34
Add constexpr in anonymouse namespace above for 0x
Miao
2016/07/15 08:39:21
Done.
| |
| 1150 arc::mojom::BluetoothCreateSdpRecordResultPtr result = | |
| 1151 mojom::BluetoothCreateSdpRecordResult::New(); | |
| 1152 result->status = arc::mojom::BluetoothStatus::FAIL; | |
| 1153 callback.Run(std::move(result)); | |
| 1154 return; | |
| 1155 } | |
| 1156 | |
| 1157 bluetooth_adapter_->CreateServiceRecord( | |
| 1158 rcd_bluez, base::Bind(&ArcBluetoothBridge::OnCreateSdpRecordDone, | |
| 1159 weak_factory_.GetWeakPtr(), callback), | |
| 1160 base::Bind(&ArcBluetoothBridge::OnCreateSdpRecordError, | |
| 1161 weak_factory_.GetWeakPtr(), callback)); | |
| 1162 } | |
| 1163 | |
| 1164 void ArcBluetoothBridge::RemoveSdpRecord( | |
| 1165 uint32_t service_handle, | |
| 1166 const RemoveSdpRecordCallback& callback) { | |
| 1167 bluetooth_adapter_->RemoveServiceRecord( | |
| 1168 service_handle, base::Bind(&ArcBluetoothBridge::OnRemoveSdpRecordDone, | |
| 1169 weak_factory_.GetWeakPtr(), callback), | |
| 1170 base::Bind(&ArcBluetoothBridge::OnRemoveSdpRecordError, | |
| 1171 weak_factory_.GetWeakPtr(), callback)); | |
| 1172 } | |
| 1173 | |
| 1027 void ArcBluetoothBridge::OnDiscoveryError() { | 1174 void ArcBluetoothBridge::OnDiscoveryError() { |
| 1028 LOG(WARNING) << "failed to change discovery state"; | 1175 LOG(WARNING) << "failed to change discovery state"; |
| 1029 } | 1176 } |
| 1030 | 1177 |
| 1031 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { | 1178 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { |
| 1032 if (!HasBluetoothInstance()) | 1179 if (!HasBluetoothInstance()) |
| 1033 return; | 1180 return; |
| 1034 | 1181 |
| 1035 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1182 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( |
| 1036 mojom::BluetoothStatus::SUCCESS, std::move(addr), | 1183 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 { | 1502 int32_t version_need) const { |
| 1356 int32_t version = arc_bridge_service()->bluetooth_version(); | 1503 int32_t version = arc_bridge_service()->bluetooth_version(); |
| 1357 if (version >= version_need) | 1504 if (version >= version_need) |
| 1358 return true; | 1505 return true; |
| 1359 LOG(WARNING) << "Bluetooth instance is too old (version " << version | 1506 LOG(WARNING) << "Bluetooth instance is too old (version " << version |
| 1360 << ") need version " << version_need; | 1507 << ") need version " << version_need; |
| 1361 return false; | 1508 return false; |
| 1362 } | 1509 } |
| 1363 | 1510 |
| 1364 } // namespace arc | 1511 } // namespace arc |
| OLD | NEW |