Skip to content

Commit 8622147

Browse files
FidelSchSuGlider
andauthored
fix: BLEAdvertising methods return error codes (#11154)
* fix: BLEAdvertising methods return error codes Some methods returned void even if they could produce an error, in which case they failed silently. They now return the corresponding error code. * fix: methods which could error out return bool Changed from returning esp_error_t, in order to be more arduino compatible. --------- Co-authored-by: Sugar Glider <[email protected]>
1 parent 1ca9dab commit 8622147

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

libraries/BLE/src/BLEAdvertising.cpp

+19-15
Original file line numberDiff line numberDiff line change
@@ -183,36 +183,38 @@ void BLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connectWh
183183
* @brief Set the advertisement data that is to be published in a regular advertisement.
184184
* @param [in] advertisementData The data to be advertised.
185185
*/
186-
void BLEAdvertising::setAdvertisementData(BLEAdvertisementData &advertisementData) {
186+
bool BLEAdvertising::setAdvertisementData(BLEAdvertisementData &advertisementData) {
187187
log_v(">> setAdvertisementData");
188188
esp_err_t errRc = ::esp_ble_gap_config_adv_data_raw((uint8_t *)advertisementData.getPayload().c_str(), advertisementData.getPayload().length());
189189
if (errRc != ESP_OK) {
190190
log_e("esp_ble_gap_config_adv_data_raw: %d %s", errRc, GeneralUtils::errorToString(errRc));
191191
}
192192
m_customAdvData = true; // Set the flag that indicates we are using custom advertising data.
193193
log_v("<< setAdvertisementData");
194+
return ESP_OK == errRc;
194195
} // setAdvertisementData
195196

196197
/**
197198
* @brief Set the advertisement data that is to be published in a scan response.
198199
* @param [in] advertisementData The data to be advertised.
199200
*/
200-
void BLEAdvertising::setScanResponseData(BLEAdvertisementData &advertisementData) {
201+
bool BLEAdvertising::setScanResponseData(BLEAdvertisementData &advertisementData) {
201202
log_v(">> setScanResponseData");
202203
esp_err_t errRc = ::esp_ble_gap_config_scan_rsp_data_raw((uint8_t *)advertisementData.getPayload().c_str(), advertisementData.getPayload().length());
203204
if (errRc != ESP_OK) {
204205
log_e("esp_ble_gap_config_scan_rsp_data_raw: %d %s", errRc, GeneralUtils::errorToString(errRc));
205206
}
206207
m_customScanResponseData = true; // Set the flag that indicates we are using custom scan response data.
207208
log_v("<< setScanResponseData");
209+
return ESP_OK == errRc;
208210
} // setScanResponseData
209211

210212
/**
211213
* @brief Start advertising.
212214
* Start advertising.
213215
* @return N/A.
214216
*/
215-
void BLEAdvertising::start() {
217+
bool BLEAdvertising::start() {
216218
log_v(">> start: customAdvData: %d, customScanResponseData: %d", m_customAdvData, m_customScanResponseData);
217219

218220
// We have a vector of service UUIDs that we wish to advertise. In order to use the
@@ -225,7 +227,7 @@ void BLEAdvertising::start() {
225227
m_advData.p_service_uuid = (uint8_t *)malloc(m_advData.service_uuid_len);
226228
if (!m_advData.p_service_uuid) {
227229
log_e(">> start failed: out of memory");
228-
return;
230+
return false;
229231
}
230232

231233
uint8_t *p = m_advData.p_service_uuid;
@@ -250,7 +252,7 @@ void BLEAdvertising::start() {
250252
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
251253
if (errRc != ESP_OK) {
252254
log_e("<< esp_ble_gap_config_adv_data: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
253-
return;
255+
return false;
254256
}
255257
}
256258

@@ -266,7 +268,7 @@ void BLEAdvertising::start() {
266268
errRc = ::esp_ble_gap_config_adv_data(&m_scanRespData);
267269
if (errRc != ESP_OK) {
268270
log_e("<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
269-
return;
271+
return false;
270272
}
271273
}
272274

@@ -279,24 +281,26 @@ void BLEAdvertising::start() {
279281
errRc = ::esp_ble_gap_start_advertising(&m_advParams);
280282
if (errRc != ESP_OK) {
281283
log_e("<< esp_ble_gap_start_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
282-
return;
284+
} else {
285+
log_v("<< start");
283286
}
284-
log_v("<< start");
287+
return ESP_OK == errRc;
285288
} // start
286289

287290
/**
288291
* @brief Stop advertising.
289292
* Stop advertising.
290293
* @return N/A.
291294
*/
292-
void BLEAdvertising::stop() {
295+
bool BLEAdvertising::stop() {
293296
log_v(">> stop");
294297
esp_err_t errRc = ::esp_ble_gap_stop_advertising();
295298
if (errRc != ESP_OK) {
296299
log_e("esp_ble_gap_stop_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
297-
return;
300+
} else {
301+
log_v("<< stop");
298302
}
299-
log_v("<< stop");
303+
return ESP_OK == errRc;
300304
} // stop
301305

302306
/**
@@ -305,17 +309,17 @@ void BLEAdvertising::stop() {
305309
* @param [in] Bluetooth address type.
306310
* Set BLE address.
307311
*/
308-
309-
void BLEAdvertising::setDeviceAddress(esp_bd_addr_t addr, esp_ble_addr_type_t type) {
312+
bool BLEAdvertising::setDeviceAddress(esp_bd_addr_t addr, esp_ble_addr_type_t type) {
310313
log_v(">> setPrivateAddress");
311314

312315
m_advParams.own_addr_type = type;
313316
esp_err_t errRc = esp_ble_gap_set_rand_addr((uint8_t *)addr);
314317
if (errRc != ESP_OK) {
315318
log_e("esp_ble_gap_set_rand_addr: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
316-
return;
319+
} else {
320+
log_v("<< setPrivateAddress");
317321
}
318-
log_v("<< setPrivateAddress");
322+
return ESP_OK == errRc;
319323
} // setPrivateAddress
320324

321325
/**

libraries/BLE/src/BLEAdvertising.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@ class BLEAdvertising {
5454
bool removeServiceUUID(int index);
5555
bool removeServiceUUID(BLEUUID serviceUUID);
5656
bool removeServiceUUID(const char *serviceUUID);
57-
void start();
58-
void stop();
57+
bool start();
58+
bool stop();
5959
void setAppearance(uint16_t appearance);
6060
void setAdvertisementType(esp_ble_adv_type_t adv_type);
6161
void setAdvertisementChannelMap(esp_ble_adv_channel_t channel_map);
6262
void setMaxInterval(uint16_t maxinterval);
6363
void setMinInterval(uint16_t mininterval);
64-
void setAdvertisementData(BLEAdvertisementData &advertisementData);
64+
bool setAdvertisementData(BLEAdvertisementData &advertisementData);
6565
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
66-
void setScanResponseData(BLEAdvertisementData &advertisementData);
66+
bool setScanResponseData(BLEAdvertisementData &advertisementData);
6767
void setPrivateAddress(esp_ble_addr_type_t type = BLE_ADDR_TYPE_RANDOM);
68-
void setDeviceAddress(esp_bd_addr_t addr, esp_ble_addr_type_t type = BLE_ADDR_TYPE_RANDOM);
68+
bool setDeviceAddress(esp_bd_addr_t addr, esp_ble_addr_type_t type = BLE_ADDR_TYPE_RANDOM);
6969

7070
void handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
7171
void setMinPreferred(uint16_t);

0 commit comments

Comments
 (0)