@@ -279,9 +279,13 @@ void BLECharacteristic::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_ga
279
279
280
280
log_d (" - Response to write event: New value: handle: %.2x, uuid: %s" , getHandle (), getUUID ().toString ().c_str ());
281
281
282
+ // The call to BLEUtils::buildHexData() doesn't output anything if the log level is not
283
+ // "DEBUG". As it is quite CPU intensive, it is much better to not call it if not needed.
284
+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
282
285
char *pHexData = BLEUtils::buildHexData (nullptr , param->write .value , param->write .len );
283
286
log_d (" - Data: length: %d, data: %s" , param->write .len , pHexData);
284
287
free (pHexData);
288
+ #endif
285
289
286
290
if (param->write .need_rsp ) {
287
291
esp_gatt_rsp_t rsp;
@@ -390,9 +394,13 @@ void BLECharacteristic::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_ga
390
394
rsp.attr_value .handle = param->read .handle ;
391
395
rsp.attr_value .auth_req = ESP_GATT_AUTH_REQ_NONE;
392
396
397
+ // The call to BLEUtils::buildHexData() doesn't output anything if the log level is not
398
+ // "DEBUG". As it is quite CPU intensive, it is much better to not call it if not needed.
399
+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
393
400
char *pHexData = BLEUtils::buildHexData (nullptr , rsp.attr_value .value , rsp.attr_value .len );
394
401
log_d (" - Data: length=%d, data=%s, offset=%d" , rsp.attr_value .len , pHexData, rsp.attr_value .offset );
395
402
free (pHexData);
403
+ #endif
396
404
397
405
esp_err_t errRc = ::esp_ble_gatts_send_response (gatts_if, param->read .conn_id , param->read .trans_id , ESP_GATT_OK, &rsp);
398
406
if (errRc != ESP_OK) {
@@ -471,7 +479,20 @@ void BLECharacteristic::notify(bool is_notification) {
471
479
472
480
m_pCallbacks->onNotify (this ); // Invoke the notify callback.
473
481
482
+ // GeneralUtils::hexDump() doesn't output anything if the log level is not
483
+ // "VERBOSE". Additionally, it is very CPU intensive, even when it doesn't
484
+ // output anything! So it is much better to *not* call it at all if not needed.
485
+ // In a simple program which calls BLECharacteristic::notify() every 50 ms,
486
+ // the performance gain of this little optimization is 37% in release mode
487
+ // (-O3) and 57% in debug mode.
488
+ // Of course, the "#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE" guard
489
+ // could also be put inside the GeneralUtils::hexDump() function itself. But
490
+ // it's better to put it here also, as it is clearer (indicating a verbose log
491
+ // thing) and it allows to remove the "m_value.getValue().c_str()" call, which
492
+ // is, in itself, quite CPU intensive.
493
+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
474
494
GeneralUtils::hexDump ((uint8_t *)m_value.getValue ().c_str (), m_value.getValue ().length ());
495
+ #endif
475
496
476
497
if (getService ()->getServer ()->getConnectedCount () == 0 ) {
477
498
log_v (" << notify: No connected clients." );
@@ -624,9 +645,13 @@ void BLECharacteristic::setReadProperty(bool value) {
624
645
* @param [in] length The length of the data in bytes.
625
646
*/
626
647
void BLECharacteristic::setValue (uint8_t *data, size_t length) {
648
+ // The call to BLEUtils::buildHexData() doesn't output anything if the log level is not
649
+ // "VERBOSE". As it is quite CPU intensive, it is much better to not call it if not needed.
650
+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
627
651
char *pHex = BLEUtils::buildHexData (nullptr , data, length);
628
652
log_v (" >> setValue: length=%d, data=%s, characteristic UUID=%s" , length, pHex, getUUID ().toString ().c_str ());
629
653
free (pHex);
654
+ #endif
630
655
if (length > ESP_GATT_MAX_ATTR_LEN) {
631
656
log_e (" Size %d too large, must be no bigger than %d" , length, ESP_GATT_MAX_ATTR_LEN);
632
657
return ;
0 commit comments