Skip to content

Commit

Permalink
[Tizen] Improve BLE scan logging (#25817)
Browse files Browse the repository at this point in the history
* Use System::Clock::Timeout for timeout

* Improve logging for BLE scanning - do not report progress as error

* Implement __IsScanFilterSupported with internal API
  • Loading branch information
arkq authored Mar 25, 2023
1 parent 1a4932a commit ec2ad41
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
27 changes: 10 additions & 17 deletions src/platform/Tizen/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}
Expand Down
63 changes: 34 additions & 29 deletions src/platform/Tizen/ChipDeviceScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <utility>

#include <bluetooth.h>
#include <bluetooth_internal.h>

#include <lib/support/CodeUtils.h>
#include <lib/support/Span.h>
Expand All @@ -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()
Expand Down Expand Up @@ -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));
Expand All @@ -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)
Expand All @@ -187,27 +187,31 @@ 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);

// 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...");
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/platform/Tizen/ChipDeviceScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <ble/CHIPBleServiceData.h>
#include <lib/core/CHIPError.h>
#include <system/SystemClock.h>

namespace chip {
namespace DeviceLayer {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
};

Expand Down

0 comments on commit ec2ad41

Please sign in to comment.