From 132021630c5f30bc8a9cba93bce837ef313f45e3 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Sat, 25 Mar 2023 15:52:29 +0100 Subject: [PATCH] [Tizen] Improve BLE scan logging (#25817) * Use System::Clock::Timeout for timeout * Improve logging for BLE scanning - do not report progress as error * Implement __IsScanFilterSupported with internal API --- src/platform/Tizen/BLEManagerImpl.cpp | 27 ++++------ src/platform/Tizen/ChipDeviceScanner.cpp | 63 +++++++++++++----------- src/platform/Tizen/ChipDeviceScanner.h | 4 +- 3 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/platform/Tizen/BLEManagerImpl.cpp b/src/platform/Tizen/BLEManagerImpl.cpp index ef9c73aed1436b..29cd066d7144eb 100644 --- a/src/platform/Tizen/BLEManagerImpl.cpp +++ b/src/platform/Tizen/BLEManagerImpl.cpp @@ -93,10 +93,9 @@ const char * desc_uuid_short = "2902"; const char * chip_ble_service_uuid_short = "FFF6"; /* Tizen Default Scan Timeout */ -static constexpr unsigned kNewConnectionScanTimeoutMs = 10000; - +static constexpr System::Clock::Timeout kNewConnectionScanTimeout = System::Clock::Seconds16(10); /* Tizen Default Connect Timeout */ -constexpr System::Clock::Timeout kConnectTimeoutMs = System::Clock::Seconds16(10); +static constexpr System::Clock::Timeout kConnectTimeout = System::Clock::Seconds16(10); static void __AdapterStateChangedCb(int result, bt_adapter_state_e adapterState, void * userData) { @@ -105,16 +104,15 @@ static void __AdapterStateChangedCb(int result, bt_adapter_state_e adapterState, void BLEManagerImpl::GattConnectionStateChangedCb(int result, bool connected, const char * remoteAddress, void * userData) { - ChipLogProgress(DeviceLayer, "Gatt Connection State Changed [%u]: %s", result, connected ? "Connected" : "Disconnected"); switch (result) { case BT_ERROR_NONE: case BT_ERROR_ALREADY_DONE: + ChipLogProgress(DeviceLayer, "GATT %s", connected ? "connected" : "disconnected"); sInstance.HandleConnectionEvent(connected, remoteAddress); break; default: - ChipLogError(DeviceLayer, "%s: %s", connected ? "Connection req failed" : "Disconnection req failed", - get_error_message(result)); + ChipLogError(DeviceLayer, "GATT %s failed: %s", connected ? "connection" : "disconnection", get_error_message(result)); if (connected) sInstance.NotifyHandleConnectFailed(CHIP_ERROR_INTERNAL); } @@ -406,9 +404,9 @@ void BLEManagerImpl::NotifyBLEDisconnection(BLE_CONNECTION_OBJECT conId, CHIP_ER void BLEManagerImpl::NotifyHandleConnectFailed(CHIP_ERROR error) { + ChipLogProgress(DeviceLayer, "Connection failed: %" CHIP_ERROR_FORMAT, error.Format()); if (sInstance.mIsCentral) { - ChipLogProgress(DeviceLayer, "Connection Failed: Post Platform event"); ChipDeviceEvent event; event.Type = DeviceEventType::kPlatformTizenBLECentralConnectFailed; event.Platform.BLECentralConnectFailed.mError = error; @@ -513,7 +511,7 @@ void BLEManagerImpl::OnChipDeviceScanned(void * device, const Ble::ChipBLEDevice /* Set CHIP Connecting state */ mBLEScanConfig.mBleScanState = BleScanState::kConnecting; - DeviceLayer::SystemLayer().StartTimer(kConnectTimeoutMs, HandleConnectionTimeout, nullptr); + DeviceLayer::SystemLayer().StartTimer(kConnectTimeout, HandleConnectionTimeout, nullptr); mDeviceScanner->StopChipScan(); /* Initiate Connect */ @@ -1405,20 +1403,15 @@ void BLEManagerImpl::InitiateScan(BleScanState scanType) } /* Send StartChipScan Request to Scanner Class */ - err = mDeviceScanner->StartChipScan(kNewConnectionScanTimeoutMs, ScanFilterType::kServiceData, data); - - if (err != CHIP_NO_ERROR) - { - ChipLogError(DeviceLayer, "Failed to start a BLE Scan: %s", ErrorStr(err)); - goto exit; - } + err = mDeviceScanner->StartChipScan(kNewConnectionScanTimeout, ScanFilterType::kServiceData, data); + VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to start a BLE scan")); - ChipLogError(DeviceLayer, "BLE Scan Initiation Successful"); + ChipLogProgress(DeviceLayer, "BLE scan initiation successful"); mBLEScanConfig.mBleScanState = scanType; return; exit: - ChipLogError(DeviceLayer, "Scan Initiation Failed!"); + ChipLogError(DeviceLayer, "BLE scan initiation failed: %" CHIP_ERROR_FORMAT, err.Format()); mBLEScanConfig.mBleScanState = BleScanState::kNotScanning; BleConnectionDelegate::OnConnectionError(mBLEScanConfig.mAppState, err); } diff --git a/src/platform/Tizen/ChipDeviceScanner.cpp b/src/platform/Tizen/ChipDeviceScanner.cpp index 2fb48203a1ca1e..2cee8ed1aa6eb1 100644 --- a/src/platform/Tizen/ChipDeviceScanner.cpp +++ b/src/platform/Tizen/ChipDeviceScanner.cpp @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -43,9 +44,6 @@ namespace Internal { const char * chip_service_uuid = "0000FFF6-0000-1000-8000-00805F9B34FB"; const char * chip_service_uuid_short = "FFF6"; -// Default CHIP Scan Timeout in Millisecond -static unsigned int kScanTimeout = 10000; - ChipDeviceScanner::ChipDeviceScanner(ChipDeviceScannerDelegate * delegate) : mDelegate(delegate) {} ChipDeviceScanner::~ChipDeviceScanner() @@ -156,11 +154,11 @@ gboolean ChipDeviceScanner::TriggerScan(GMainLoop * mainLoop, gpointer userData) // All set, trigger LE Scan ret = bt_adapter_le_start_scan(LeScanResultCb, userData); - VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_adapter_le_start_scan() ret: %d", ret)); + VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_adapter_le_start_scan() failed: %s", get_error_message(ret))); ChipLogProgress(DeviceLayer, "Scan started"); // Start Timer - idleSource = g_timeout_source_new(kScanTimeout); + idleSource = g_timeout_source_new(self->mScanTimeoutMs); g_source_set_callback(idleSource, TimerExpiredCb, userData, nullptr); g_source_set_priority(idleSource, G_PRIORITY_HIGH_IDLE); g_source_attach(idleSource, g_main_loop_get_context(self->mAsyncLoop)); @@ -173,9 +171,11 @@ gboolean ChipDeviceScanner::TriggerScan(GMainLoop * mainLoop, gpointer userData) static bool __IsScanFilterSupported() { - // Tizen API: bt_adapter_le_is_scan_filter_supported() is currently internal - // Defaulting to true - return true; + bool is_supported; + int ret = bt_adapter_le_is_scan_filter_supported(&is_supported); + VerifyOrReturnValue(ret == BT_ERROR_NONE, false, + ChipLogError(DeviceLayer, "bt_adapter_le_is_scan_filter_supported() failed: %s", get_error_message(ret))); + return is_supported; } void ChipDeviceScanner::CheckScanFilter(ScanFilterType filterType, ScanFilterData & filterData) @@ -187,16 +187,20 @@ void ChipDeviceScanner::CheckScanFilter(ScanFilterType filterType, ScanFilterDat return; ret = CreateLEScanFilter(filterType, filterData); - VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "Scan Filter Creation Failed! ret: %d Do Normal Scan", ret)); + VerifyOrExit(ret == BT_ERROR_NONE, + ChipLogError(DeviceLayer, "BLE scan filter creation failed: %s. Do Normal Scan", get_error_message(ret))); ret = RegisterScanFilter(filterType, filterData); - VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "Scan Filter Registration Failed! ret: %d Do Normal Scan", ret)); + VerifyOrExit(ret == BT_ERROR_NONE, + ChipLogError(DeviceLayer, "BLE scan filter registration failed: %s. Do Normal Scan", get_error_message(ret))); + return; + exit: UnRegisterScanFilter(); } -CHIP_ERROR ChipDeviceScanner::StartChipScan(unsigned timeoutMs, ScanFilterType filterType, ScanFilterData & filterData) +CHIP_ERROR ChipDeviceScanner::StartChipScan(System::Clock::Timeout timeout, ScanFilterType filterType, ScanFilterData & filterData) { CHIP_ERROR err = CHIP_NO_ERROR; ReturnErrorCodeIf(mIsScanning, CHIP_ERROR_INCORRECT_STATE); @@ -204,10 +208,10 @@ CHIP_ERROR ChipDeviceScanner::StartChipScan(unsigned timeoutMs, ScanFilterType f // Scan Filter Setup if supported: silently bypass error & do filterless scan in case of error CheckScanFilter(filterType, filterData); - kScanTimeout = timeoutMs; + mScanTimeoutMs = System::Clock::Milliseconds32(timeout).count(); // All set to trigger LE Scan - ChipLogProgress(DeviceLayer, "Start CHIP Scan..."); + ChipLogProgress(DeviceLayer, "Start CHIP BLE scan: timeout=%ums", mScanTimeoutMs); if (MainLoop::Instance().AsyncRequest(TriggerScan, this) == false) { ChipLogError(DeviceLayer, "Failed to trigger Scan..."); @@ -233,7 +237,7 @@ CHIP_ERROR ChipDeviceScanner::StopChipScan() ret = bt_adapter_le_stop_scan(); if (ret != BT_ERROR_NONE) { - ChipLogError(DeviceLayer, "bt_adapter_le_stop_scan() failed. ret: %d", ret); + ChipLogError(DeviceLayer, "bt_adapter_le_stop_scan() failed: %s", get_error_message(ret)); } g_main_loop_quit(mAsyncLoop); @@ -265,46 +269,47 @@ int ChipDeviceScanner::RegisterScanFilter(ScanFilterType filterType, ScanFilterD switch (filterType) { case ScanFilterType::kAddress: { - ChipLogProgress(DeviceLayer, "Register Scan filter: Address"); + ChipLogProgress(DeviceLayer, "Register BLE scan filter: Address"); ret = bt_adapter_le_scan_filter_set_device_address(mScanFilter, filterData.address); - VerifyOrExit(ret == BT_ERROR_NONE, - ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_set_device_address() failed. ret: %d", ret)); + VerifyOrExit( + ret == BT_ERROR_NONE, + ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_set_device_address() failed: %s", get_error_message(ret))); break; } case ScanFilterType::kServiceUUID: { - ChipLogProgress(DeviceLayer, "Register Scan filter: Service UUID"); + ChipLogProgress(DeviceLayer, "Register BLE scan filter: Service UUID"); ret = bt_adapter_le_scan_filter_set_service_uuid(mScanFilter, filterData.service_uuid); VerifyOrExit(ret == BT_ERROR_NONE, - ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_set_service_uuid() failed. ret: %d", ret)); + ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_set_service_uuid() failed: %s", get_error_message(ret))); break; } case ScanFilterType::kServiceData: { - ChipLogProgress(DeviceLayer, "Register Scan filter: Service Data"); + ChipLogProgress(DeviceLayer, "Register BLE scan filter: Service Data"); ret = bt_adapter_le_scan_filter_set_service_data(mScanFilter, filterData.service_uuid, filterData.service_data, filterData.service_data_len); VerifyOrExit(ret == BT_ERROR_NONE, - ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_set_service_data() failed. ret: %d", ret)); + ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_set_service_data() failed: %s", get_error_message(ret))); break; } case ScanFilterType::kNoFilter: default: - return ret; // Without Scan Filter + goto exit; } ret = bt_adapter_le_scan_filter_register(mScanFilter); - VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_register failed(). ret: %d", ret)); - return ret; + VerifyOrExit(ret == BT_ERROR_NONE, + ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_register() failed: %s", get_error_message(ret))); + exit: return ret; } int ChipDeviceScanner::CreateLEScanFilter(ScanFilterType filterType, ScanFilterData & filterData) { - int ret = BT_ERROR_NONE; - - ret = bt_adapter_le_scan_filter_create(&mScanFilter); - VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_create() Failed. ret: %d", ret)); - ChipLogError(DeviceLayer, "Scan Filter Created Successfully"); + int ret = bt_adapter_le_scan_filter_create(&mScanFilter); + VerifyOrExit(ret == BT_ERROR_NONE, + ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_create() failed: %s", get_error_message(ret))); + ChipLogProgress(DeviceLayer, "BLE scan filter created successfully"); exit: return ret; } diff --git a/src/platform/Tizen/ChipDeviceScanner.h b/src/platform/Tizen/ChipDeviceScanner.h index ac8049fdd1ecdd..fedfba500d95ad 100644 --- a/src/platform/Tizen/ChipDeviceScanner.h +++ b/src/platform/Tizen/ChipDeviceScanner.h @@ -30,6 +30,7 @@ #include #include +#include namespace chip { namespace DeviceLayer { @@ -85,7 +86,7 @@ class ChipDeviceScanner ~ChipDeviceScanner(void); /// Initiate a scan for devices, with the given timeout & scan filter data - CHIP_ERROR StartChipScan(unsigned timeoutMs, ScanFilterType filterType, ScanFilterData & filterData); + CHIP_ERROR StartChipScan(System::Clock::Timeout timeout, ScanFilterType filterType, ScanFilterData & filterData); /// Stop any currently running scan CHIP_ERROR StopChipScan(void); @@ -107,6 +108,7 @@ class ChipDeviceScanner bool mIsScanning = false; bool mIsStopping = false; GMainLoop * mAsyncLoop = nullptr; + unsigned int mScanTimeoutMs = 10000; bt_scan_filter_h mScanFilter = nullptr; };