Skip to content

Commit

Permalink
[wiring] BLE: minors.
Browse files Browse the repository at this point in the history
  • Loading branch information
XuGuohui committed Oct 26, 2020
1 parent 46595ef commit d567d5b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 67 deletions.
4 changes: 2 additions & 2 deletions wiring/inc/spark_wiring_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ class BleLocalDevice {

template<typename T>
int scanWithFilter(const BleScanFilter& filter, void(T::*callback)(const BleScanResult&), T* instance) {
return scan(filter, std::bind(callback, instance, _1));
return scanWithFilter(filter, std::bind(callback, instance, _1));
}

int stopScanning() const;
Expand Down Expand Up @@ -954,7 +954,7 @@ class BleLocalDevice {
return onConnected(std::bind(callback, instance, _1));
}

void onDisconnected(BleOnDisconnectedCallback callback, void* context=nullptr) const;
void onDisconnected(BleOnDisconnectedCallback callback, void* context = nullptr) const;
void onDisconnected(const std::function<void(const BlePeerDevice& peer)>& callback) const;

template<typename T>
Expand Down
106 changes: 41 additions & 65 deletions wiring/src/spark_wiring_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2088,43 +2088,54 @@ class BleScanDelegator {
foundCount_(0),
callback_(nullptr),
callbackRef_(nullptr),
context_(nullptr) {
context_(nullptr),
wiringCallback_(nullptr) {
resultsVector_.clear();
}

~BleScanDelegator() = default;

int start(BleOnScanResultCallback callback, void* context) {
callback_ = callback;
callbackRef_ = nullptr;
context_ = context;
callbackRef_ = nullptr;
wiringCallback_ = nullptr;
CHECK(hal_ble_gap_start_scan(onScanResultCallback, this, nullptr));
return foundCount_;
}

int start(BleOnScanResultCallbackRef callback, void* context) {
callbackRef_ = callback;
callback_ = nullptr;
context_ = context;
CHECK(hal_ble_gap_start_scan(onScanResultCallbackV1, this, nullptr));
callback_ = nullptr;
wiringCallback_ = nullptr;
CHECK(hal_ble_gap_start_scan(onScanResultCallback, this, nullptr));
return foundCount_;
}

int start(BleScanResult* results, size_t resultCount) {
callback_ = nullptr;
callbackRef_ = nullptr;
wiringCallback_ = nullptr;
resultsPtr_ = results;
targetCount_ = resultCount;
CHECK(hal_ble_gap_start_scan(onScanResultCallbackV2, this, nullptr));
CHECK(hal_ble_gap_start_scan(onScanResultCallback, this, nullptr));
return foundCount_;
}

Vector<BleScanResult> start() {
hal_ble_gap_start_scan(onScanResultCallbackV3, this, nullptr);
callback_ = nullptr;
callbackRef_ = nullptr;
wiringCallback_ = nullptr;
hal_ble_gap_start_scan(onScanResultCallback, this, nullptr);
return resultsVector_;
}

int start(const std::function<void(const BleScanResult&)>& callback) {
wiringCallback_ = callback;
CHECK(hal_ble_gap_start_scan(onScanResultCallbackV4, this, nullptr));
callback_ = nullptr;
callbackRef_ = nullptr;
CHECK(hal_ble_gap_start_scan(onScanResultCallback, this, nullptr));
return foundCount_;
}

Expand Down Expand Up @@ -2272,76 +2283,41 @@ class BleScanDelegator {
static void onScanResultCallback(const hal_ble_scan_result_evt_t* event, void* context) {
BleScanDelegator* delegator = static_cast<BleScanDelegator*>(context);
BleScanResult result = {};
if (!delegator->constructScanResult(event)) {
result.address(event->peer_addr)
. rssi(event->rssi)
.scanResponse(event->sr_data, event->sr_data_len)
.advertisingData(event->adv_data, event->adv_data_len);
if (!delegator->filterByRssi(result) ||
!delegator->filterByAddress(result) ||
!delegator->filterByDeviceName(result) ||
!delegator->filterByServiceUUID(result) ||
!delegator->filterByAppearance(result) ||
!delegator->filterByCustomData(result)) {
return;
}
if (delegator->callback_) {
delegator->callback_(&result, delegator->context_);
delegator->foundCount_++;
}
}

static void onScanResultCallbackV1(const hal_ble_scan_result_evt_t* event, void* context) {
BleScanDelegator* delegator = static_cast<BleScanDelegator*>(context);
BleScanResult result = {};
if (!delegator->constructScanResult(event)) {
return;
}
if (delegator->callbackRef_) {
} else if (delegator->callbackRef_) {
delegator->callbackRef_(result, delegator->context_);
delegator->foundCount_++;
}
}

static void onScanResultCallbackV2(const hal_ble_scan_result_evt_t* event, void* context) {
BleScanDelegator* delegator = static_cast<BleScanDelegator*>(context);
BleScanResult result = {};
if (!delegator->constructScanResult(event)) {
return;
}
if (!delegator->resultsPtr_ || delegator->foundCount_ >= delegator->targetCount_) {
hal_ble_gap_stop_scan(nullptr);
return;
}
delegator->resultsPtr_[delegator->foundCount_++] = result;
}

static void onScanResultCallbackV3(const hal_ble_scan_result_evt_t* event, void* context) {
BleScanDelegator* delegator = static_cast<BleScanDelegator*>(context);
BleScanResult result = {};
if (!delegator->constructScanResult(event)) {
return;
}
if (delegator->resultsVector_.append(result)) {
} else if (delegator->wiringCallback_) {
delegator->wiringCallback_(result);
delegator->foundCount_++;
}
}

static void onScanResultCallbackV4(const hal_ble_scan_result_evt_t* event, void* context) {
BleScanDelegator* delegator = static_cast<BleScanDelegator*>(context);
BleScanResult result = {};
if (!delegator->constructScanResult(event)) {
if (delegator->resultsPtr_) {
if (delegator->foundCount_ <= delegator->targetCount_) {
delegator->resultsPtr_[delegator->foundCount_ - 1] = result;
if (delegator->foundCount_ >= delegator->targetCount_) {
LOG_DEBUG(TRACE, "Target number of devices found. Stop scanning...");
hal_ble_gap_stop_scan(nullptr);
}
}
return;
}
delegator->wiringCallback_(result);
delegator->foundCount_++;
}

bool constructScanResult(BleScanResult& result, const hal_ble_scan_result_evt_t* event) {
result.address(event->peer_addr)
. rssi(event->rssi)
.scanResponse(event->sr_data, event->sr_data_len)
.advertisingData(event->adv_data, event->adv_data_len);

if (!filterByRssi(result) ||
!filterByAddress(result) ||
!filterByDeviceName(result) ||
!filterByServiceUUID(result) ||
!filterByAppearance(result) ||
!filterByCustomData(result)) {
return false;
}
return true;
delegator->resultsVector_.append(result);
}

Vector<BleScanResult> resultsVector_;
Expand All @@ -2350,8 +2326,8 @@ class BleScanDelegator {
size_t foundCount_;
BleOnScanResultCallback callback_;
BleOnScanResultCallbackRef callbackRef_;
std::function<void(const BleScanResult&)> wiringCallback_;
void* context_;
std::function<void(const BleScanResult&)> wiringCallback_;
BleScanFilter filter_;
};

Expand Down

0 comments on commit d567d5b

Please sign in to comment.