8000 splitted BLEScan::start to blocking & non-blocking oveloads · Donderda/esp32-snippets@f778541 · GitHub
[go: up one dir, main page]

Skip to content

Commit f778541

Browse files
committed
splitted BLEScan::start to blocking & non-blocking oveloads
1 parent 61604a9 commit f778541

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

cpp_utils/BLEScan.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,14 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
189189
/**
190190
* @brief Start scanning.
191191
* @param [in] duration The duration in seconds for which to scan.
192-
* @param [in] scanCompleteCB A function to be called when scanning has completed. This can
193-
* be supplied as nullptr (the default) in which case the call to start will block until scanning has
194-
* been completed.
195-
* @return The BLEScanResults. Only applicable if we are waiting for results.
192+
* @param [in] scanCompleteCB A function to be called when scanning has completed.
193+
* @return True if scan started or false if there was an error.
196194
*/
197-
BLEScanResults BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
195+
bool BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
198196
ESP_LOGD(LOG_TAG, ">> start(duration=%d)", duration);
199-
m_scanCompleteCB = scanCompleteCB; // Save the callback to be invoked when the scan completes.
200197

201198
m_semaphoreScanEnd.take(std::string("start"));
199+
m_scanCompleteCB = scanCompleteCB; // Save the callback to be invoked when the scan completes.
202200

203201
m_scanResults.m_vectorAdvertisedDevices.clear();
204202

@@ -207,24 +205,33 @@ BLEScanResults BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanR
207205
if (errRc != ESP_OK) {
208206
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_scan_params: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc));
209207
m_semaphoreScanEnd.give();
210-
return m_scanResults;
208+
return false;
211209
}
212210

213211
errRc = ::esp_ble_gap_start_scanning(duration);
214212

215213
if (errRc != ESP_OK) {
216214
ESP_LOGE(LOG_TAG, "esp_ble_gap_start_scanning: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc));
217215
m_semaphoreScanEnd.give();
218-
return m_scanResults;
216+
return false;
219217
}
220218

221219
m_stopped = false;
222220

223-
if (m_scanCompleteCB == nullptr) {
221+
ESP_LOGD(LOG_TAG, "<< start()");
222+
return true;
223+
} // start
224+
225+
226+
/**
227+
* @brief Start scanning and block until scanning has been completed.
228+
* @param [in] duration The duration in seconds for which to scan.
229+
* @return The BLEScanResults.
230+
*/
231+
BLEScanResults BLEScan::start(uint32_t duration) {
232+
if(start(duration, nullptr)) {
224233
m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release.
225234
}
226-
227-
ESP_LOGD(LOG_TAG, "<< start()");
228235
return m_scanResults;
229236
} // start
230237

cpp_utils/BLEScan.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class BLEScan {
5353
bool wantDuplicates = false);
5454
void setInterval(uint16_t intervalMSecs);
5555
void setWindow(uint16_t windowMSecs);
56-
BLEScanResults start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults) = nullptr);
56+
bool start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults));
57+
BLEScanResults start(uint32_t duration);
5758
void stop();
5859

5960
private:

0 commit comments

Comments
 (0)
0