Skip to content

Commit

Permalink
Rewrite OnScanComplete to make it more readable.
Browse files Browse the repository at this point in the history
Create a separate function to handle scan errors.
  • Loading branch information
jlatusek committed Nov 25, 2022
1 parent d9f8100 commit 1e8a744
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 44 deletions.
22 changes: 18 additions & 4 deletions src/controller/python/chip/ble/LinuxImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ class ScannerDelegateImpl : public ChipDeviceScannerDelegate
using DeviceScannedCallback = void (*)(PyObject * context, const char * address, uint16_t discriminator, uint16_t vendorId,
uint16_t productId);
using ScanCompleteCallback = void (*)(PyObject * context);
using ScanErrorCallback = void (*)(PyObject * context, CHIP_ERROR::StorageType error);

ScannerDelegateImpl(PyObject * context, DeviceScannedCallback scanCallback, ScanCompleteCallback completeCallback) :
mContext(context), mScanCallback(scanCallback), mCompleteCallback(completeCallback)
ScannerDelegateImpl(PyObject * context, DeviceScannedCallback scanCallback, ScanCompleteCallback completeCallback,
ScanErrorCallback errorCallback) :
mContext(context),
mScanCallback(scanCallback), mCompleteCallback(completeCallback), mErrorCallback(errorCallback)
{}

void SetScanner(std::unique_ptr<ChipDeviceScanner> scanner) { mScanner = std::move(scanner); }
Expand All @@ -94,20 +97,31 @@ class ScannerDelegateImpl : public ChipDeviceScannerDelegate
delete this;
}

virtual void OnScanError(CHIP_ERROR error) override
{
if (mErrorCallback)
{
mErrorCallback(mContext, error.AsInteger());
}
}

private:
std::unique_ptr<ChipDeviceScanner> mScanner;
PyObject * const mContext;
const DeviceScannedCallback mScanCallback;
const ScanCompleteCallback mCompleteCallback;
const ScanErrorCallback mErrorCallback;
};

} // namespace

extern "C" void * pychip_ble_start_scanning(PyObject * context, void * adapter, uint32_t timeoutMs,
ScannerDelegateImpl::DeviceScannedCallback scanCallback,
ScannerDelegateImpl::ScanCompleteCallback completeCallback)
ScannerDelegateImpl::ScanCompleteCallback completeCallback,
ScannerDelegateImpl::ScanErrorCallback errorCallback)
{
std::unique_ptr<ScannerDelegateImpl> delegate = std::make_unique<ScannerDelegateImpl>(context, scanCallback, completeCallback);
std::unique_ptr<ScannerDelegateImpl> delegate =
std::make_unique<ScannerDelegateImpl>(context, scanCallback, completeCallback, errorCallback);

std::unique_ptr<ChipDeviceScanner> scanner = ChipDeviceScanner::Create(static_cast<BluezAdapter1 *>(adapter), delegate.get());

Expand Down
10 changes: 8 additions & 2 deletions src/controller/python/chip/ble/darwin/Scanning.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using DeviceScannedCallback
= void (*)(PyObject * context, const char * address, uint16_t discriminator, uint16_t vendorId, uint16_t productId);
using ScanCompleteCallback = void (*)(PyObject * context);
using ScanErrorCallback = void (*)(PyObject * context, uint32_t error);
}

@interface ChipDeviceBleScanner : NSObject <CBCentralManagerDelegate>
Expand All @@ -23,10 +24,12 @@ @interface ChipDeviceBleScanner : NSObject <CBCentralManagerDelegate>
@property (assign, nonatomic) PyObject * context;
@property (assign, nonatomic) DeviceScannedCallback scanCallback;
@property (assign, nonatomic) ScanCompleteCallback completeCallback;
@property (assign, nonatomic) ScanErrorCallback errorCallback;

- (id)initWithContext:(PyObject *)context
scanCallback:(DeviceScannedCallback)scanCallback
completeCallback:(ScanCompleteCallback)completeCallback
errorCallback:(ScanErrorCallback)errorCallback
timeoutMs:(uint32_t)timeout;

- (void)stopTimeoutReached;
Expand All @@ -38,6 +41,7 @@ @implementation ChipDeviceBleScanner
- (id)initWithContext:(PyObject *)context
scanCallback:(DeviceScannedCallback)scanCallback
completeCallback:(ScanCompleteCallback)completeCallback
errorCallback:(ScanErrorCallback)errorCallback
timeoutMs:(uint32_t)timeout
{
self = [super init];
Expand All @@ -50,6 +54,7 @@ - (id)initWithContext:(PyObject *)context
_context = context;
_scanCallback = scanCallback;
_completeCallback = completeCallback;
_errorCallback = errorCallback;

dispatch_source_set_event_handler(_timer, ^{
[self stopTimeoutReached];
Expand Down Expand Up @@ -125,14 +130,15 @@ - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPerip

@end

extern "C" void * pychip_ble_start_scanning(
PyObject * context, void * adapter, uint32_t timeout, DeviceScannedCallback scanCallback, ScanCompleteCallback completeCallback)
extern "C" void * pychip_ble_start_scanning(PyObject * context, void * adapter, uint32_t timeout,
DeviceScannedCallback scanCallback, ScanCompleteCallback completeCallback, ScanErrorCallback errorCallback)
{
// NOTE: adapter is ignored as it does not apply to mac

ChipDeviceBleScanner * scanner = [[ChipDeviceBleScanner alloc] initWithContext:context
scanCallback:scanCallback
completeCallback:completeCallback
errorCallback:errorCallback
timeoutMs:timeout];

return (__bridge_retained void *) (scanner);
Expand Down
3 changes: 1 addition & 2 deletions src/platform/Linux/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,13 +811,12 @@ void BLEManagerImpl::OnScanComplete()
break;
case BleScanState::kScanForAddress:
case BleScanState::kScanForDiscriminator:
mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
ChipLogProgress(Ble, "Scan complete. No matching device found.");
break;
case BleScanState::kConnecting:
break;
}

mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
}

void BLEManagerImpl::OnScanError(CHIP_ERROR err)
Expand Down
24 changes: 16 additions & 8 deletions src/platform/Tizen/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,18 +483,26 @@ void BLEManagerImpl::OnChipDeviceScanned(void * device, const Ble::ChipBLEDevice
ConnectHandler(deviceInfo->remote_address);
}

void BLEManagerImpl::OnChipScanComplete()
void BLEManagerImpl::OnScanComplete()
{
if (mBLEScanConfig.mBleScanState != BleScanState::kScanForDiscriminator &&
mBLEScanConfig.mBleScanState != BleScanState::kScanForAddress)
switch (mBLEScanConfig.mBleScanState)
{
ChipLogProgress(DeviceLayer, "Scan complete notification without an active scan.");
return;
case BleScanState::kNotScanning:
ChipLogProgress(Ble, "Scan complete notification without an active scan.");
break;
case BleScanState::kScanForAddress:
case BleScanState::kScanForDiscriminator:
mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
ChipLogProgress(Ble, "Scan complete. No matching device found.");
break;
case BleScanState::kConnecting:
break;
}
}

ChipLogError(DeviceLayer, "Scan Completed with Timeout: Notify Upstream.");
BleConnectionDelegate::OnConnectionError(mBLEScanConfig.mAppState, CHIP_ERROR_TIMEOUT);
mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
void BLEManagerImpl::OnScanError(CHIP_ERROR err)
{
ChipLogDetail(Ble, "BLE scan error: %" CHIP_ERROR_FORMAT, err.Format());
}

int BLEManagerImpl::RegisterGATTServer()
Expand Down
3 changes: 2 additions & 1 deletion src/platform/Tizen/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ class BLEManagerImpl final : public BLEManager,

// ===== Members that implement virtual methods on ChipDeviceScannerDelegate
void OnChipDeviceScanned(void * device, const Ble::ChipBLEDeviceIdentificationInfo & info) override;
void OnChipScanComplete() override;
void OnScanComplete() override;
void OnScanError(CHIP_ERROR err) override;

// ===== Members for internal use by the following friends.

Expand Down
2 changes: 1 addition & 1 deletion src/platform/Tizen/ChipDeviceScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ CHIP_ERROR ChipDeviceScanner::StopChipScan(void)
UnRegisterScanFilter();

// Report to Impl class
mDelegate->OnChipScanComplete();
mDelegate->OnScanComplete();

mIsScanning = false;

Expand Down
5 changes: 4 additions & 1 deletion src/platform/Tizen/ChipDeviceScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ class ChipDeviceScannerDelegate
virtual void OnChipDeviceScanned(void * device, const chip::Ble::ChipBLEDeviceIdentificationInfo & info) = 0;

// Called when a scan was completed (stopped or timed out)
virtual void OnChipScanComplete(void) = 0;
virtual void OnScanComplete(void) = 0;

// Called on scan error
virtual void OnScanError(CHIP_ERROR err) = 0;
};

/// Allows scanning for CHIP devices
Expand Down
33 changes: 14 additions & 19 deletions src/platform/webos/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,31 +917,26 @@ void BLEManagerImpl::NotifyBLEPeripheralAdvStopComplete(bool aIsSuccess, void *
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::OnChipScanComplete(void)
{
if (mBLEScanConfig.mBleScanState != BleScanState::kScanForDiscriminator &&
mBLEScanConfig.mBleScanState != BleScanState::kScanForAddress)
{
ChipLogProgress(DeviceLayer, "Scan complete notification without an active scan.");
return;
}

ChipLogError(DeviceLayer, "Scan Completed with Timeout: Notify Upstream.");
BleConnectionDelegate::OnConnectionError(mBLEScanConfig.mAppState, CHIP_ERROR_TIMEOUT);
mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
}

void BLEManagerImpl::OnScanComplete()
{
if (mBLEScanConfig.mBleScanState != BleScanState::kScanForDiscriminator &&
mBLEScanConfig.mBleScanState != BleScanState::kScanForAddress)
switch (mBLEScanConfig.mBleScanState)
{
case BleScanState::kNotScanning:
ChipLogProgress(Ble, "Scan complete notification without an active scan.");
return;
break;
case BleScanState::kScanForAddress:
case BleScanState::kScanForDiscriminator:
mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
ChipLogProgress(Ble, "Scan complete. No matching device found.");
break;
case BleScanState::kConnecting:
break;
}
}

BleConnectionDelegate::OnConnectionError(mBLEScanConfig.mAppState, CHIP_ERROR_TIMEOUT);
mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
void BLEManagerImpl::OnScanError(CHIP_ERROR err)
{
ChipLogDetail(Ble, "BLE scan error: %" CHIP_ERROR_FORMAT, err.Format());
}

bool BLEManagerImpl::gattGetServiceCb(LSHandle * sh, LSMessage * message, void * userData)
Expand Down
5 changes: 2 additions & 3 deletions src/platform/webos/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,9 @@ class BLEManagerImpl final : public BLEManager,
CHIP_ERROR CancelConnection() override;

// ===== Members that implement virtual methods on ChipDeviceScannerDelegate
void OnScanComplete() override;
void OnChipDeviceScanned(char * address) override;
void OnChipScanComplete() override;

void OnScanComplete() override;
void OnScanError(CHIP_ERROR err) override;
// ===== Members for internal use by the following friends.

friend BLEManager & BLEMgr();
Expand Down
2 changes: 1 addition & 1 deletion src/platform/webos/ChipDeviceScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ CHIP_ERROR ChipDeviceScanner::StopChipScan(void)
ChipLogProgress(DeviceLayer, "CHIP Scanner Async Thread Quit Done..Wait for Thread Windup...!");

// Report to Impl class
mDelegate->OnChipScanComplete();
mDelegate->OnScanComplete();

mIsScanning = false;

Expand Down
6 changes: 4 additions & 2 deletions src/platform/webos/ChipDeviceScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class ChipDeviceScannerDelegate
virtual void OnChipDeviceScanned(char * address) = 0;

// Called when a scan was completed (stopped or timed out)
virtual void OnScanComplete() = 0;
virtual void OnChipScanComplete() = 0;
virtual void OnScanComplete() = 0;

// Called on scan error
virtual void OnScanError(CHIP_ERROR err) = 0;
};

/// Allows scanning for CHIP devices
Expand Down

0 comments on commit 1e8a744

Please sign in to comment.