@@ -279,9 +279,13 @@ void BLECharacteristic::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_ga
279279
280280 log_d (" - Response to write event: New value: handle: %.2x, uuid: %s" , getHandle (), getUUID ().toString ().c_str ());
281281
282- char *pHexData = BLEUtils::buildHexData (nullptr , param->write .value , param->write .len );
283- log_d (" - Data: length: %d, data: %s" , param->write .len , pHexData);
284- free (pHexData);
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
285+ char *pHexData = BLEUtils::buildHexData (nullptr , param->write .value , param->write .len );
286+ log_d (" - Data: length: %d, data: %s" , param->write .len , pHexData);
287+ free (pHexData);
288+ #endif
285289
286290 if (param->write .need_rsp ) {
287291 esp_gatt_rsp_t rsp;
@@ -390,9 +394,13 @@ void BLECharacteristic::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_ga
390394 rsp.attr_value .handle = param->read .handle ;
391395 rsp.attr_value .auth_req = ESP_GATT_AUTH_REQ_NONE;
392396
393- char *pHexData = BLEUtils::buildHexData (nullptr , rsp.attr_value .value , rsp.attr_value .len );
394- log_d (" - Data: length=%d, data=%s, offset=%d" , rsp.attr_value .len , pHexData, rsp.attr_value .offset );
395- free (pHexData);
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
400+ char *pHexData = BLEUtils::buildHexData (nullptr , rsp.attr_value .value , rsp.attr_value .len );
401+ log_d (" - Data: length=%d, data=%s, offset=%d" , rsp.attr_value .len , pHexData, rsp.attr_value .offset );
402+ free (pHexData);
403+ #endif
396404
397405 esp_err_t errRc = ::esp_ble_gatts_send_response (gatts_if, param->read .conn_id , param->read .trans_id , ESP_GATT_OK, &rsp);
398406 if (errRc != ESP_OK) {
@@ -637,9 +645,13 @@ void BLECharacteristic::setReadProperty(bool value) {
637645 * @param [in] length The length of the data in bytes.
638646 */
639647void BLECharacteristic::setValue (uint8_t *data, size_t length) {
640- char *pHex = BLEUtils::buildHexData (nullptr , data, length);
641- log_v (" >> setValue: length=%d, data=%s, characteristic UUID=%s" , length, pHex, getUUID ().toString ().c_str ());
642- free (pHex);
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
651+ char *pHex = BLEUtils::buildHexData (nullptr , data, length);
652+ log_v (" >> setValue: length=%d, data=%s, characteristic UUID=%s" , length, pHex, getUUID ().toString ().c_str ());
653+ free (pHex);
654+ #endif
643655 if (length > ESP_GATT_MAX_ATTR_LEN) {
644656 log_e (" Size %d too large, must be no bigger than %d" , length, ESP_GATT_MAX_ATTR_LEN);
645657 return ;
0 commit comments