diff options
author | Shobhit Adlakha <ShobhitAd@users.noreply.github.com> | 2021-04-06 11:54:47 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-06 11:54:47 -0400 |
commit | c163c2d5de1e6be1ffe449572c1d7002816b3352 (patch) | |
tree | 615015489c6a0a47f70bf18b74414040ae346705 /src | |
parent | ff4474db121431439ee524982cad6bbcf466fd16 (diff) | |
download | sdl_core-c163c2d5de1e6be1ffe449572c1d7002816b3352.tar.gz |
Fix/BT scan blocking UpdateAppList (#3425)
* Add status check for bt scan
* Create separate thread to handle the paired device and hci inquiries
* Style fix
* Initialize inquiry thread in constructor and add thread start check
* Remove scan for unpaired devices
* Remove inquiry thread
* Add timeout, remove found_devices_with_sdl_, fix conflicts
* Prevent automatic cloud connections
* Fix bluetooth thread issues
* Fix style
* Remove new lines
* Fix threading issues
* Prevent deadlock on shutdown
* Rename CV Lock
* Style
Co-authored-by: JackLivio <jack@livio.io>
Diffstat (limited to 'src')
4 files changed, 29 insertions, 39 deletions
diff --git a/src/components/connection_handler/src/connection_handler_impl.cc b/src/components/connection_handler/src/connection_handler_impl.cc index 2bc0aeb2e9..cb7fc31575 100644 --- a/src/components/connection_handler/src/connection_handler_impl.cc +++ b/src/components/connection_handler/src/connection_handler_impl.cc @@ -1474,6 +1474,10 @@ void ConnectionHandlerImpl::ConnectToAllDevices() { SDL_LOG_DEBUG("No need to connect to web engine device"); continue; } + if ("CLOUD_WEBSOCKET" == i->second.connection_type()) { + SDL_LOG_DEBUG("No need to connect to cloud device"); + continue; + } ConnectToDevice(i->first); } } diff --git a/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_device_scanner.h b/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_device_scanner.h index 768d9ebb54..9bfe05758f 100644 --- a/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_device_scanner.h +++ b/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_device_scanner.h @@ -171,16 +171,16 @@ class BluetoothDeviceScanner : public DeviceScanner { TransportAdapterController* controller_; threads::Thread* thread_; - bool shutdown_requested_; + std::atomic<bool> shutdown_requested_; bool ready_; bool device_scan_requested_; sync_primitives::Lock device_scan_requested_lock_; + sync_primitives::Lock terminate_lock_; sync_primitives::ConditionalVariable device_scan_requested_cv_; std::vector<bdaddr_t> paired_devices_; DeviceVector paired_devices_with_sdl_; - DeviceVector found_devices_with_sdl_; /** * @brief UUID of SmartDeviceLink service. diff --git a/src/components/transport_manager/src/bluetooth/bluetooth_device_scanner.cc b/src/components/transport_manager/src/bluetooth/bluetooth_device_scanner.cc index f152f1f78d..ce0c2900b7 100644 --- a/src/components/transport_manager/src/bluetooth/bluetooth_device_scanner.cc +++ b/src/components/transport_manager/src/bluetooth/bluetooth_device_scanner.cc @@ -95,10 +95,10 @@ int FindPairedDevs(std::vector<bdaddr_t>* result) { delete[] buffer; buffer = new char[1028]; } - pclose(pipe); + int status = pclose(pipe); SDL_LOG_TRACE("exit with 0"); delete[] buffer; - return 0; + return status; } } // namespace @@ -112,6 +112,7 @@ BluetoothDeviceScanner::BluetoothDeviceScanner( , ready_(true) , device_scan_requested_(false) , device_scan_requested_lock_() + , terminate_lock_() , device_scan_requested_cv_() , auto_repeat_search_(auto_repeat_search) , auto_repeat_pause_sec_(auto_repeat_pause_sec) { @@ -148,6 +149,7 @@ BluetoothDeviceScanner::BluetoothDeviceScanner( , ready_(true) , device_scan_requested_(false) , device_scan_requested_lock_() + , terminate_lock_() , device_scan_requested_cv_() , auto_repeat_search_(auto_repeat_search) , auto_repeat_pause_sec_(auto_repeat_pause_sec) { @@ -173,9 +175,6 @@ void BluetoothDeviceScanner::UpdateTotalDeviceList() { devices.insert(devices.end(), paired_devices_with_sdl_.begin(), paired_devices_with_sdl_.end()); - devices.insert(devices.end(), - found_devices_with_sdl_.begin(), - found_devices_with_sdl_.end()); controller_->SearchDeviceDone(devices); } @@ -212,38 +211,9 @@ void BluetoothDeviceScanner::DoInquiry() { paired_devices_, device_handle, &paired_devices_with_sdl_); UpdateTotalDeviceList(); - SDL_LOG_INFO("Starting hci_inquiry on device " << device_id); - const uint8_t inquiry_time = 8u; // Time unit is 1.28 seconds - const size_t max_devices = 256u; - inquiry_info* inquiry_info_list = new inquiry_info[max_devices]; - - const int number_of_devices = hci_inquiry(device_id, - inquiry_time, - max_devices, - 0, - &inquiry_info_list, - IREQ_CACHE_FLUSH); - - if (number_of_devices >= 0) { - SDL_LOG_INFO("hci_inquiry: found " << number_of_devices << " devices"); - std::vector<bdaddr_t> found_devices(number_of_devices); - for (int i = 0; i < number_of_devices; ++i) { - found_devices[i] = inquiry_info_list[i].bdaddr; - } - found_devices_with_sdl_.clear(); - CheckSDLServiceOnDevices( - found_devices, device_handle, &found_devices_with_sdl_); - } - UpdateTotalDeviceList(); controller_->FindNewApplicationsRequest(); close(device_handle); - delete[] inquiry_info_list; - - if (number_of_devices < 0) { - SDL_LOG_DEBUG("number_of_devices < 0"); - controller_->SearchDeviceFailed(SearchDeviceError()); - } } void BluetoothDeviceScanner::CheckSDLServiceOnDevices( @@ -424,7 +394,10 @@ void BluetoothDeviceScanner::Thread() { if (auto_repeat_search_) { while (!shutdown_requested_) { DoInquiry(); - device_scan_requested_ = false; + { + sync_primitives::AutoLock auto_lock(device_scan_requested_lock_); + device_scan_requested_ = false; + } TimedWaitForDeviceScanRequest(); } } else { // search only on demand @@ -452,12 +425,22 @@ void BluetoothDeviceScanner::TimedWaitForDeviceScanRequest() { return; } + if (shutdown_requested_) { + SDL_LOG_INFO("Bluetooth scanner Condition: shutdown_requested_ == true"); + return; + } + { sync_primitives::AutoLock auto_lock(device_scan_requested_lock_); while (!(device_scan_requested_ || shutdown_requested_)) { + if (!terminate_lock_.Try()) { + // Lock is taken by terminate thread, return + return; + } const sync_primitives::ConditionalVariable::WaitStatus wait_status = device_scan_requested_cv_.WaitFor(auto_lock, auto_repeat_pause_sec_ * 1000); + terminate_lock_.Release(); if (wait_status == sync_primitives::ConditionalVariable::kTimeout) { SDL_LOG_INFO("Bluetooth scanner timeout, performing scan"); device_scan_requested_ = true; @@ -478,13 +461,16 @@ TransportAdapter::Error BluetoothDeviceScanner::Init() { void BluetoothDeviceScanner::Terminate() { SDL_LOG_AUTO_TRACE(); + if (shutdown_requested_) + return; shutdown_requested_ = true; if (thread_) { + device_scan_requested_cv_.NotifyOne(); { sync_primitives::AutoLock auto_lock(device_scan_requested_lock_); device_scan_requested_ = false; - device_scan_requested_cv_.NotifyOne(); } + sync_primitives::AutoLock auto_lock(terminate_lock_); SDL_LOG_INFO("Waiting for bluetooth device scanner thread termination"); thread_->Stop(threads::Thread::kThreadStopDelegate); SDL_LOG_INFO("Bluetooth device scanner thread stopped"); diff --git a/src/components/transport_manager/src/bluetooth/bluetooth_transport_adapter.cc b/src/components/transport_manager/src/bluetooth/bluetooth_transport_adapter.cc index abeb50e798..88a219f57e 100644 --- a/src/components/transport_manager/src/bluetooth/bluetooth_transport_adapter.cc +++ b/src/components/transport_manager/src/bluetooth/bluetooth_transport_adapter.cc @@ -58,7 +58,7 @@ BluetoothTransportAdapter::BluetoothTransportAdapter( resumption::LastStateWrapperPtr last_state_wrapper, const TransportManagerSettings& settings) : TransportAdapterImpl( - new BluetoothDeviceScanner(this, true, 0, settings.bluetooth_uuid()), + new BluetoothDeviceScanner(this, true, 15, settings.bluetooth_uuid()), new BluetoothConnectionFactory(this), NULL, last_state_wrapper, |