Only start scan/advertise when host re-synced if duration was indefinite.

Previously when the host reset while scanning (if active prior) it would be restarted automatically.
This caused errors for some applications and has been removed since the event invokes the scan
ended callback for the app to take action. Instead scanning will now only be restarted if the duration
was indefinite and a callback was set for the advertisment events, this use case is less likely to have
a scan ended callback.

Advertising (if active prior) would be started without regard to it's previous state.
This has been corrected to only start advertising again if it was advertising for an idefinite time.
This commit is contained in:
h2zero 2021-01-12 20:42:19 -07:00
parent 28573f5abe
commit 372c79a6b8
5 changed files with 52 additions and 30 deletions

View file

@ -312,6 +312,9 @@ void NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdv
return; return;
} }
// Save the duration incase of host reset so we can restart with the same params
m_duration = duration;
if(duration == 0){ if(duration == 0){
duration = BLE_HS_FOREVER; duration = BLE_HS_FOREVER;
} }
@ -495,6 +498,7 @@ void NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdv
*/ */
void NimBLEAdvertising::stop() { void NimBLEAdvertising::stop() {
NIMBLE_LOGD(LOG_TAG, ">> stop"); NIMBLE_LOGD(LOG_TAG, ">> stop");
int rc = ble_gap_adv_stop(); int rc = ble_gap_adv_stop();
if (rc != 0 && rc != BLE_HS_EALREADY) { if (rc != 0 && rc != BLE_HS_EALREADY) {
NIMBLE_LOGE(LOG_TAG, "ble_gap_adv_stop rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc)); NIMBLE_LOGE(LOG_TAG, "ble_gap_adv_stop rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
@ -528,8 +532,14 @@ bool NimBLEAdvertising::isAdvertising() {
* Host reset seems to clear advertising data, * Host reset seems to clear advertising data,
* we need clear the flag so it reloads it. * we need clear the flag so it reloads it.
*/ */
void NimBLEAdvertising::onHostReset() { void NimBLEAdvertising::onHostSync() {
NIMBLE_LOGD(LOG_TAG, "Host re-synced");
m_advDataSet = false; m_advDataSet = false;
// If we were advertising forever, restart it now
if(m_duration == 0) {
start(m_duration, m_advCompCB);
}
} }
@ -543,6 +553,19 @@ int NimBLEAdvertising::handleGapEvent(struct ble_gap_event *event, void *arg) {
NimBLEAdvertising *pAdv = (NimBLEAdvertising*)arg; NimBLEAdvertising *pAdv = (NimBLEAdvertising*)arg;
if(event->type == BLE_GAP_EVENT_ADV_COMPLETE) { if(event->type == BLE_GAP_EVENT_ADV_COMPLETE) {
switch(event->adv_complete.reason) {
// Don't call the callback if host reset, we want to
// preserve the active flag until re-sync to restart advertising.
case BLE_HS_ETIMEOUT_HCI:
case BLE_HS_EOS:
case BLE_HS_ECONTROLLER:
case BLE_HS_ENOTSYNCED:
NIMBLE_LOGC(LOG_TAG, "host reset, rc=%d", event->adv_complete.reason);
NimBLEDevice::onReset(event->adv_complete.reason);
return 0;
default:
break;
}
pAdv->advCompleteCB(); pAdv->advCompleteCB();
} }
return 0; return 0;

View file

@ -95,7 +95,7 @@ public:
private: private:
friend class NimBLEDevice; friend class NimBLEDevice;
void onHostReset(); void onHostSync();
static int handleGapEvent(struct ble_gap_event *event, void *arg); static int handleGapEvent(struct ble_gap_event *event, void *arg);
ble_hs_adv_fields m_advData; ble_hs_adv_fields m_advData;
@ -108,7 +108,7 @@ private:
bool m_advDataSet; bool m_advDataSet;
void (*m_advCompCB)(NimBLEAdvertising *pAdv); void (*m_advCompCB)(NimBLEAdvertising *pAdv);
uint8_t m_slaveItvl[4]; uint8_t m_slaveItvl[4];
uint32_t m_duration;
}; };
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)

View file

@ -410,30 +410,16 @@ void NimBLEDevice::stopAdvertising() {
m_synced = false; m_synced = false;
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
if(m_pScan != nullptr) {
m_pScan->onHostReset();
}
#endif
/* Not needed
if(m_pServer != nullptr) {
m_pServer->onHostReset();
}
for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) {
(*it)->onHostReset();
}
*/
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
if(m_bleAdvertising != nullptr) {
m_bleAdvertising->onHostReset();
}
#endif
NIMBLE_LOGC(LOG_TAG, "Resetting state; reason=%d, %s", reason, NIMBLE_LOGC(LOG_TAG, "Resetting state; reason=%d, %s", reason,
NimBLEUtils::returnCodeToString(reason)); NimBLEUtils::returnCodeToString(reason));
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
if(initialized) {
if(m_pScan != nullptr) {
m_pScan->onHostReset();
}
}
#endif
} // onReset } // onReset
@ -462,15 +448,13 @@ void NimBLEDevice::stopAdvertising() {
if(initialized) { if(initialized) {
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
if(m_pScan != nullptr) { if(m_pScan != nullptr) {
// Restart scanning with the last values sent, allow to clear results. m_pScan->onHostSync();
m_pScan->start(m_pScan->m_duration, m_pScan->m_scanCompleteCB);
} }
#endif #endif
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
if(m_bleAdvertising != nullptr) { if(m_bleAdvertising != nullptr) {
// Restart advertisng, parameters should already be set. m_bleAdvertising->onHostSync();
m_bleAdvertising->start();
} }
#endif #endif
} }

View file

@ -41,6 +41,7 @@ NimBLEScan::NimBLEScan() {
m_ignoreResults = false; m_ignoreResults = false;
m_wantDuplicates = false; m_wantDuplicates = false;
m_pTaskData = nullptr; m_pTaskData = nullptr;
m_duration = BLE_HS_FOREVER; // make sure this is non-zero in the event of a host reset
} }
@ -378,12 +379,25 @@ void NimBLEScan::erase(const NimBLEAddress &address) {
/** /**
* @brief If the host reset the scan will have stopped so we should set the flag as stopped. * @brief Called when host reset, we set a flag to stop scanning until synced.
*/ */
void NimBLEScan::onHostReset() { void NimBLEScan::onHostReset() {
m_ignoreResults = true;
} }
/**
* @brief If the host reset and re-synced this is called.
* If the application was scanning indefinitely with a callback, restart it.
*/
void NimBLEScan::onHostSync() {
m_ignoreResults = false;
if(m_duration == 0 && m_pAdvertisedDeviceCallbacks != nullptr) {
start(m_duration, m_scanCompleteCB);
}
}
/** /**
* @brief Get the results of the scan. * @brief Get the results of the scan.
* @return NimBLEScanResults object. * @return NimBLEScanResults object.

View file

@ -83,6 +83,7 @@ private:
~NimBLEScan(); ~NimBLEScan();
static int handleGapEvent(ble_gap_event* event, void* arg); static int handleGapEvent(ble_gap_event* event, void* arg);
void onHostReset(); void onHostReset();
void onHostSync();
NimBLEAdvertisedDeviceCallbacks* m_pAdvertisedDeviceCallbacks = nullptr; NimBLEAdvertisedDeviceCallbacks* m_pAdvertisedDeviceCallbacks = nullptr;
void (*m_scanCompleteCB)(NimBLEScanResults scanResults); void (*m_scanCompleteCB)(NimBLEScanResults scanResults);