| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "device/bluetooth/bluetooth_adapter_chromeos.h" | 5 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "chromeos/dbus/bluetooth_adapter_client.h" | 17 #include "chromeos/dbus/bluetooth_adapter_client.h" |
| 18 #include "chromeos/dbus/bluetooth_agent_manager_client.h" | 18 #include "chromeos/dbus/bluetooth_agent_manager_client.h" |
| 19 #include "chromeos/dbus/bluetooth_agent_service_provider.h" | 19 #include "chromeos/dbus/bluetooth_agent_service_provider.h" |
| 20 #include "chromeos/dbus/bluetooth_device_client.h" | 20 #include "chromeos/dbus/bluetooth_device_client.h" |
| 21 #include "chromeos/dbus/bluetooth_input_client.h" | 21 #include "chromeos/dbus/bluetooth_input_client.h" |
| 22 #include "chromeos/dbus/dbus_thread_manager.h" | 22 #include "chromeos/dbus/dbus_thread_manager.h" |
| 23 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" | 23 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" |
| 24 #include "device/bluetooth/bluetooth_audio_sink_chromeos.h" |
| 24 #include "device/bluetooth/bluetooth_device.h" | 25 #include "device/bluetooth/bluetooth_device.h" |
| 25 #include "device/bluetooth/bluetooth_device_chromeos.h" | 26 #include "device/bluetooth/bluetooth_device_chromeos.h" |
| 26 #include "device/bluetooth/bluetooth_pairing_chromeos.h" | 27 #include "device/bluetooth/bluetooth_pairing_chromeos.h" |
| 27 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" | 28 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" |
| 28 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" | 29 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" |
| 29 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" | 30 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" |
| 30 #include "device/bluetooth/bluetooth_socket_chromeos.h" | 31 #include "device/bluetooth/bluetooth_socket_chromeos.h" |
| 31 #include "device/bluetooth/bluetooth_socket_thread.h" | 32 #include "device/bluetooth/bluetooth_socket_thread.h" |
| 32 #include "device/bluetooth/bluetooth_uuid.h" | 33 #include "device/bluetooth/bluetooth_uuid.h" |
| 33 #include "third_party/cros_system_api/dbus/service_constants.h" | 34 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 34 | 35 |
| 35 using device::BluetoothAdapter; | 36 using device::BluetoothAdapter; |
| 37 using device::BluetoothAudioSink; |
| 36 using device::BluetoothDevice; | 38 using device::BluetoothDevice; |
| 37 using device::BluetoothSocket; | 39 using device::BluetoothSocket; |
| 38 using device::BluetoothUUID; | 40 using device::BluetoothUUID; |
| 39 | 41 |
| 40 namespace { | 42 namespace { |
| 41 | 43 |
| 42 // The agent path is relatively meaningless since BlueZ only permits one to | 44 // The agent path is relatively meaningless since BlueZ only permits one to |
| 43 // exist per D-Bus connection, it just has to be unique within Chromium. | 45 // exist per D-Bus connection, it just has to be unique within Chromium. |
| 44 const char kAgentPath[] = "/org/chromium/bluetooth_agent"; | 46 const char kAgentPath[] = "/org/chromium/bluetooth_agent"; |
| 45 | 47 |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 ui_task_runner_, socket_thread_); | 298 ui_task_runner_, socket_thread_); |
| 297 socket->Listen(this, | 299 socket->Listen(this, |
| 298 BluetoothSocketChromeOS::kL2cap, | 300 BluetoothSocketChromeOS::kL2cap, |
| 299 uuid, | 301 uuid, |
| 300 options, | 302 options, |
| 301 base::Bind(callback, socket), | 303 base::Bind(callback, socket), |
| 302 error_callback); | 304 error_callback); |
| 303 } | 305 } |
| 304 | 306 |
| 305 void BluetoothAdapterChromeOS::RegisterAudioSink( | 307 void BluetoothAdapterChromeOS::RegisterAudioSink( |
| 306 const device::BluetoothAudioSink::Options& options, | 308 const BluetoothAudioSink::Options& options, |
| 307 const device::BluetoothAdapter::AcquiredCallback& callback, | 309 const device::BluetoothAdapter::AcquiredCallback& callback, |
| 308 const device::BluetoothAudioSink::ErrorCallback& error_callback) { | 310 const BluetoothAudioSink::ErrorCallback& error_callback) { |
| 309 // TODO(mcchou): Create and register a BluetoothAudioSink. Add the | 311 VLOG(1) << "Registering audio sink"; |
| 310 // newly-created audio sink as an observer of the adapter. | 312 if (!this->IsPresent()) { |
| 311 // Add OnRegisterAudioSink(AcquiredCallback& , BluetoothAudioSink*) and pass | 313 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER); |
| 312 // it as an argument to BluetoothAudioSinkChromeOS::Register. | 314 return; |
| 313 error_callback.Run(device::BluetoothAudioSink::ERROR_UNSUPPORTED_PLATFORM); | 315 } |
| 316 scoped_refptr<BluetoothAudioSinkChromeOS> audio_sink( |
| 317 new BluetoothAudioSinkChromeOS(this)); |
| 318 audio_sink->Register( |
| 319 options, |
| 320 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAudioSink, |
| 321 weak_ptr_factory_.GetWeakPtr(), callback, audio_sink), |
| 322 error_callback); |
| 314 } | 323 } |
| 315 | 324 |
| 316 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( | 325 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( |
| 317 BluetoothDevice::PairingDelegate* pairing_delegate) { | 326 BluetoothDevice::PairingDelegate* pairing_delegate) { |
| 318 DCHECK(IsPresent()); | 327 DCHECK(IsPresent()); |
| 319 // Before removing a pairing delegate make sure that there aren't any devices | 328 // Before removing a pairing delegate make sure that there aren't any devices |
| 320 // currently using it; if there are, clear the pairing context which will | 329 // currently using it; if there are, clear the pairing context which will |
| 321 // make any responses no-ops. | 330 // make any responses no-ops. |
| 322 for (DevicesMap::iterator iter = devices_.begin(); | 331 for (DevicesMap::iterator iter = devices_.begin(); |
| 323 iter != devices_.end(); ++iter) { | 332 iter != devices_.end(); ++iter) { |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 } | 654 } |
| 646 | 655 |
| 647 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( | 656 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( |
| 648 const std::string& error_name, | 657 const std::string& error_name, |
| 649 const std::string& error_message) { | 658 const std::string& error_message) { |
| 650 DCHECK(IsPresent()); | 659 DCHECK(IsPresent()); |
| 651 LOG(WARNING) << ": Failed to make pairing agent default: " | 660 LOG(WARNING) << ": Failed to make pairing agent default: " |
| 652 << error_name << ": " << error_message; | 661 << error_name << ": " << error_message; |
| 653 } | 662 } |
| 654 | 663 |
| 664 void BluetoothAdapterChromeOS::OnRegisterAudioSink( |
| 665 const device::BluetoothAdapter::AcquiredCallback& callback, |
| 666 scoped_refptr<BluetoothAudioSink> audio_sink) { |
| 667 DCHECK(audio_sink.get()); |
| 668 callback.Run(audio_sink); |
| 669 } |
| 670 |
| 655 BluetoothDeviceChromeOS* | 671 BluetoothDeviceChromeOS* |
| 656 BluetoothAdapterChromeOS::GetDeviceWithPath( | 672 BluetoothAdapterChromeOS::GetDeviceWithPath( |
| 657 const dbus::ObjectPath& object_path) { | 673 const dbus::ObjectPath& object_path) { |
| 658 if (!IsPresent()) | 674 if (!IsPresent()) |
| 659 return NULL; | 675 return NULL; |
| 660 | 676 |
| 661 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); | 677 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); |
| 662 ++iter) { | 678 ++iter) { |
| 663 BluetoothDeviceChromeOS* device_chromeos = | 679 BluetoothDeviceChromeOS* device_chromeos = |
| 664 static_cast<BluetoothDeviceChromeOS*>(iter->second); | 680 static_cast<BluetoothDeviceChromeOS*>(iter->second); |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1048 // The adapter is already discovering. | 1064 // The adapter is already discovering. |
| 1049 if (num_discovery_sessions_ > 0) { | 1065 if (num_discovery_sessions_ > 0) { |
| 1050 DCHECK(IsDiscovering()); | 1066 DCHECK(IsDiscovering()); |
| 1051 DCHECK(!discovery_request_pending_); | 1067 DCHECK(!discovery_request_pending_); |
| 1052 num_discovery_sessions_++; | 1068 num_discovery_sessions_++; |
| 1053 callback.Run(); | 1069 callback.Run(); |
| 1054 return; | 1070 return; |
| 1055 } | 1071 } |
| 1056 | 1072 |
| 1057 // There are no active discovery sessions. | 1073 // There are no active discovery sessions. |
| 1058 DCHECK(num_discovery_sessions_ == 0); | 1074 DCHECK_EQ(num_discovery_sessions_, 0); |
| 1059 | 1075 |
| 1060 // This is the first request to start device discovery. | 1076 // This is the first request to start device discovery. |
| 1061 discovery_request_pending_ = true; | 1077 discovery_request_pending_ = true; |
| 1062 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 1078 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 1063 StartDiscovery( | 1079 StartDiscovery( |
| 1064 object_path_, | 1080 object_path_, |
| 1065 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, | 1081 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, |
| 1066 weak_ptr_factory_.GetWeakPtr(), | 1082 weak_ptr_factory_.GetWeakPtr(), |
| 1067 callback), | 1083 callback), |
| 1068 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, | 1084 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1098 // TODO(armansito): This should never happen once we have the | 1114 // TODO(armansito): This should never happen once we have the |
| 1099 // DiscoverySession API. Replace this case with an assert once it's | 1115 // DiscoverySession API. Replace this case with an assert once it's |
| 1100 // the deprecated methods have been removed. (See crbug.com/3445008). | 1116 // the deprecated methods have been removed. (See crbug.com/3445008). |
| 1101 VLOG(1) << "No active discovery sessions. Returning error."; | 1117 VLOG(1) << "No active discovery sessions. Returning error."; |
| 1102 error_callback.Run(); | 1118 error_callback.Run(); |
| 1103 return; | 1119 return; |
| 1104 } | 1120 } |
| 1105 | 1121 |
| 1106 // There is exactly one active discovery session. Request BlueZ to stop | 1122 // There is exactly one active discovery session. Request BlueZ to stop |
| 1107 // discovery. | 1123 // discovery. |
| 1108 DCHECK(num_discovery_sessions_ == 1); | 1124 DCHECK_EQ(num_discovery_sessions_, 1); |
| 1109 discovery_request_pending_ = true; | 1125 discovery_request_pending_ = true; |
| 1110 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 1126 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 1111 StopDiscovery( | 1127 StopDiscovery( |
| 1112 object_path_, | 1128 object_path_, |
| 1113 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, | 1129 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, |
| 1114 weak_ptr_factory_.GetWeakPtr(), | 1130 weak_ptr_factory_.GetWeakPtr(), |
| 1115 callback), | 1131 callback), |
| 1116 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, | 1132 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, |
| 1117 weak_ptr_factory_.GetWeakPtr(), | 1133 weak_ptr_factory_.GetWeakPtr(), |
| 1118 error_callback)); | 1134 error_callback)); |
| 1119 } | 1135 } |
| 1120 | 1136 |
| 1121 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { | 1137 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { |
| 1122 DCHECK(IsPresent()); | 1138 DCHECK(IsPresent()); |
| 1123 // Report success on the original request and increment the count. | 1139 // Report success on the original request and increment the count. |
| 1124 VLOG(1) << __func__; | 1140 VLOG(1) << __func__; |
| 1125 DCHECK(discovery_request_pending_); | 1141 DCHECK(discovery_request_pending_); |
| 1126 DCHECK(num_discovery_sessions_ == 0); | 1142 DCHECK_EQ(num_discovery_sessions_, 0); |
| 1127 discovery_request_pending_ = false; | 1143 discovery_request_pending_ = false; |
| 1128 num_discovery_sessions_++; | 1144 num_discovery_sessions_++; |
| 1129 callback.Run(); | 1145 callback.Run(); |
| 1130 | 1146 |
| 1131 // Try to add a new discovery session for each queued request. | 1147 // Try to add a new discovery session for each queued request. |
| 1132 ProcessQueuedDiscoveryRequests(); | 1148 ProcessQueuedDiscoveryRequests(); |
| 1133 } | 1149 } |
| 1134 | 1150 |
| 1135 void BluetoothAdapterChromeOS::OnStartDiscoveryError( | 1151 void BluetoothAdapterChromeOS::OnStartDiscoveryError( |
| 1136 const base::Closure& callback, | 1152 const base::Closure& callback, |
| 1137 const ErrorCallback& error_callback, | 1153 const ErrorCallback& error_callback, |
| 1138 const std::string& error_name, | 1154 const std::string& error_name, |
| 1139 const std::string& error_message) { | 1155 const std::string& error_message) { |
| 1140 DCHECK(IsPresent()); | 1156 DCHECK(IsPresent()); |
| 1141 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " | 1157 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " |
| 1142 << error_name << ": " << error_message; | 1158 << error_name << ": " << error_message; |
| 1143 | 1159 |
| 1144 // Failed to start discovery. This can only happen if the count is at 0. | 1160 // Failed to start discovery. This can only happen if the count is at 0. |
| 1145 DCHECK(num_discovery_sessions_ == 0); | 1161 DCHECK_EQ(num_discovery_sessions_, 0); |
| 1146 DCHECK(discovery_request_pending_); | 1162 DCHECK(discovery_request_pending_); |
| 1147 discovery_request_pending_ = false; | 1163 discovery_request_pending_ = false; |
| 1148 | 1164 |
| 1149 // Discovery request may fail if discovery was previously initiated by Chrome, | 1165 // Discovery request may fail if discovery was previously initiated by Chrome, |
| 1150 // but the session were invalidated due to the discovery state unexpectedly | 1166 // but the session were invalidated due to the discovery state unexpectedly |
| 1151 // changing to false and then back to true. In this case, report success. | 1167 // changing to false and then back to true. In this case, report success. |
| 1152 if (error_name == bluetooth_device::kErrorInProgress && IsDiscovering()) { | 1168 if (error_name == bluetooth_device::kErrorInProgress && IsDiscovering()) { |
| 1153 VLOG(1) << "Discovery previously initiated. Reporting success."; | 1169 VLOG(1) << "Discovery previously initiated. Reporting success."; |
| 1154 num_discovery_sessions_++; | 1170 num_discovery_sessions_++; |
| 1155 callback.Run(); | 1171 callback.Run(); |
| 1156 } else { | 1172 } else { |
| 1157 error_callback.Run(); | 1173 error_callback.Run(); |
| 1158 } | 1174 } |
| 1159 | 1175 |
| 1160 // Try to add a new discovery session for each queued request. | 1176 // Try to add a new discovery session for each queued request. |
| 1161 ProcessQueuedDiscoveryRequests(); | 1177 ProcessQueuedDiscoveryRequests(); |
| 1162 } | 1178 } |
| 1163 | 1179 |
| 1164 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { | 1180 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { |
| 1165 DCHECK(IsPresent()); | 1181 DCHECK(IsPresent()); |
| 1166 // Report success on the original request and decrement the count. | 1182 // Report success on the original request and decrement the count. |
| 1167 VLOG(1) << __func__; | 1183 VLOG(1) << __func__; |
| 1168 DCHECK(discovery_request_pending_); | 1184 DCHECK(discovery_request_pending_); |
| 1169 DCHECK(num_discovery_sessions_ == 1); | 1185 DCHECK_EQ(num_discovery_sessions_, 1); |
| 1170 discovery_request_pending_ = false; | 1186 discovery_request_pending_ = false; |
| 1171 num_discovery_sessions_--; | 1187 num_discovery_sessions_--; |
| 1172 callback.Run(); | 1188 callback.Run(); |
| 1173 | 1189 |
| 1174 // Try to add a new discovery session for each queued request. | 1190 // Try to add a new discovery session for each queued request. |
| 1175 ProcessQueuedDiscoveryRequests(); | 1191 ProcessQueuedDiscoveryRequests(); |
| 1176 } | 1192 } |
| 1177 | 1193 |
| 1178 void BluetoothAdapterChromeOS::OnStopDiscoveryError( | 1194 void BluetoothAdapterChromeOS::OnStopDiscoveryError( |
| 1179 const ErrorCallback& error_callback, | 1195 const ErrorCallback& error_callback, |
| 1180 const std::string& error_name, | 1196 const std::string& error_name, |
| 1181 const std::string& error_message) { | 1197 const std::string& error_message) { |
| 1182 DCHECK(IsPresent()); | 1198 DCHECK(IsPresent()); |
| 1183 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " | 1199 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " |
| 1184 << error_name << ": " << error_message; | 1200 << error_name << ": " << error_message; |
| 1185 | 1201 |
| 1186 // Failed to stop discovery. This can only happen if the count is at 1. | 1202 // Failed to stop discovery. This can only happen if the count is at 1. |
| 1187 DCHECK(discovery_request_pending_); | 1203 DCHECK(discovery_request_pending_); |
| 1188 DCHECK(num_discovery_sessions_ == 1); | 1204 DCHECK_EQ(num_discovery_sessions_, 1); |
| 1189 discovery_request_pending_ = false; | 1205 discovery_request_pending_ = false; |
| 1190 error_callback.Run(); | 1206 error_callback.Run(); |
| 1191 | 1207 |
| 1192 // Try to add a new discovery session for each queued request. | 1208 // Try to add a new discovery session for each queued request. |
| 1193 ProcessQueuedDiscoveryRequests(); | 1209 ProcessQueuedDiscoveryRequests(); |
| 1194 } | 1210 } |
| 1195 | 1211 |
| 1196 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { | 1212 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { |
| 1197 while (!discovery_request_queue_.empty()) { | 1213 while (!discovery_request_queue_.empty()) { |
| 1198 VLOG(1) << "Process queued discovery request."; | 1214 VLOG(1) << "Process queued discovery request."; |
| 1199 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); | 1215 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); |
| 1200 discovery_request_queue_.pop(); | 1216 discovery_request_queue_.pop(); |
| 1201 AddDiscoverySession(callbacks.first, callbacks.second); | 1217 AddDiscoverySession(callbacks.first, callbacks.second); |
| 1202 | 1218 |
| 1203 // If the queued request resulted in a pending call, then let it | 1219 // If the queued request resulted in a pending call, then let it |
| 1204 // asynchonously process the remaining queued requests once the pending | 1220 // asynchonously process the remaining queued requests once the pending |
| 1205 // call returns. | 1221 // call returns. |
| 1206 if (discovery_request_pending_) | 1222 if (discovery_request_pending_) |
| 1207 return; | 1223 return; |
| 1208 } | 1224 } |
| 1209 } | 1225 } |
| 1210 | 1226 |
| 1211 } // namespace chromeos | 1227 } // namespace chromeos |
| OLD | NEW |