Skip to content

Commit

Permalink
Refactored use of LOG_X(LOG_TAG, ...) to log_x(...) (#2672)
Browse files Browse the repository at this point in the history
* Replaced ARDUINO_VARIANT with const char

* Fixed missing return value

* Added quotes around defined value in macro (Issue #2193)

* Change logging from Error to Verbose when not found and default available

* Move Enter and Exit logging to Verbose Level

* Refactored LOG_X() into log_x()
  • Loading branch information
Bascy authored and me-no-dev committed Apr 15, 2019
1 parent af23d0b commit 01d7ea7
Show file tree
Hide file tree
Showing 21 changed files with 399 additions and 530 deletions.
28 changes: 11 additions & 17 deletions libraries/BLE/src/BLEAdvertisedDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
#include <sstream>
#include "BLEAdvertisedDevice.h"
#include "BLEUtils.h"
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
#define LOG_TAG ""
#else
#include "esp_log.h"
static const char* LOG_TAG="BLEAdvertisedDevice";
#endif

BLEAdvertisedDevice::BLEAdvertisedDevice() {
m_adFlag = 0;
Expand Down Expand Up @@ -249,7 +243,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
length--;

char* pHex = BLEUtils::buildHexData(nullptr, payload, length);
ESP_LOGD(LOG_TAG, "Type: 0x%.2x (%s), length: %d, data: %s",
log_d("Type: 0x%.2x (%s), length: %d, data: %s",
ad_type, BLEUtils::advTypeToString(ad_type), length, pHex);
free(pHex);

Expand Down Expand Up @@ -308,7 +302,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)

case ESP_BLE_AD_TYPE_SERVICE_DATA: { // Adv Data Type: 0x16 (Service Data) - 2 byte UUID
if (length < 2) {
ESP_LOGE(LOG_TAG, "Length too small for ESP_BLE_AD_TYPE_SERVICE_DATA");
log_e("Length too small for ESP_BLE_AD_TYPE_SERVICE_DATA");
break;
}
uint16_t uuid = *(uint16_t*)payload;
Expand All @@ -321,7 +315,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)

case ESP_BLE_AD_TYPE_32SERVICE_DATA: { // Adv Data Type: 0x20 (Service Data) - 4 byte UUID
if (length < 4) {
ESP_LOGE(LOG_TAG, "Length too small for ESP_BLE_AD_TYPE_32SERVICE_DATA");
log_e("Length too small for ESP_BLE_AD_TYPE_32SERVICE_DATA");
break;
}
uint32_t uuid = *(uint32_t*) payload;
Expand All @@ -334,7 +328,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)

case ESP_BLE_AD_TYPE_128SERVICE_DATA: { // Adv Data Type: 0x21 (Service Data) - 16 byte UUID
if (length < 16) {
ESP_LOGE(LOG_TAG, "Length too small for ESP_BLE_AD_TYPE_128SERVICE_DATA");
log_e("Length too small for ESP_BLE_AD_TYPE_128SERVICE_DATA");
break;
}

Expand All @@ -346,7 +340,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
} //ESP_BLE_AD_TYPE_32SERVICE_DATA

default: {
ESP_LOGD(LOG_TAG, "Unhandled type: adType: %d - 0x%.2x", ad_type, ad_type);
log_d("Unhandled type: adType: %d - 0x%.2x", ad_type, ad_type);
break;
}
} // switch
Expand Down Expand Up @@ -386,7 +380,7 @@ void BLEAdvertisedDevice::setAdFlag(uint8_t adFlag) {
void BLEAdvertisedDevice::setAppearance(uint16_t appearance) {
m_appearance = appearance;
m_haveAppearance = true;
ESP_LOGD(LOG_TAG, "- appearance: %d", m_appearance);
log_d("- appearance: %d", m_appearance);
} // setAppearance


Expand All @@ -398,7 +392,7 @@ void BLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) {
m_manufacturerData = manufacturerData;
m_haveManufacturerData = true;
char* pHex = BLEUtils::buildHexData(nullptr, (uint8_t*) m_manufacturerData.data(), (uint8_t) m_manufacturerData.length());
ESP_LOGD(LOG_TAG, "- manufacturer data: %s", pHex);
log_d("- manufacturer data: %s", pHex);
free(pHex);
} // setManufacturerData

Expand All @@ -410,7 +404,7 @@ void BLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) {
void BLEAdvertisedDevice::setName(std::string name) {
m_name = name;
m_haveName = true;
ESP_LOGD(LOG_TAG, "- setName(): name: %s", m_name.c_str());
log_d("- setName(): name: %s", m_name.c_str());
} // setName


Expand All @@ -421,7 +415,7 @@ void BLEAdvertisedDevice::setName(std::string name) {
void BLEAdvertisedDevice::setRSSI(int rssi) {
m_rssi = rssi;
m_haveRSSI = true;
ESP_LOGD(LOG_TAG, "- setRSSI(): rssi: %d", m_rssi);
log_d("- setRSSI(): rssi: %d", m_rssi);
} // setRSSI


Expand Down Expand Up @@ -450,7 +444,7 @@ void BLEAdvertisedDevice::setServiceUUID(const char* serviceUUID) {
void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
m_serviceUUIDs.push_back(serviceUUID);
m_haveServiceUUID = true;
ESP_LOGD(LOG_TAG, "- addServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str());
log_d("- addServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str());
} // setServiceUUID


Expand Down Expand Up @@ -481,7 +475,7 @@ void BLEAdvertisedDevice::setServiceDataUUID(BLEUUID uuid) {
void BLEAdvertisedDevice::setTXPower(int8_t txPower) {
m_txPower = txPower;
m_haveTXPower = true;
ESP_LOGD(LOG_TAG, "- txPower: %d", m_txPower);
log_d("- txPower: %d", m_txPower);
} // setTXPower


Expand Down
67 changes: 29 additions & 38 deletions libraries/BLE/src/BLEAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,7 @@
#include <esp_err.h>
#include "BLEUtils.h"
#include "GeneralUtils.h"

#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
#define LOG_TAG ""
#else
#include "esp_log.h"
static const char* LOG_TAG = "BLEAdvertising";
#endif



/**
* @brief Construct a default advertising object.
Expand Down Expand Up @@ -120,25 +111,25 @@ void BLEAdvertising::setScanResponse(bool set) {
* @param [in] connectWhitelistOnly If true, only allow connections from those on the white list.
*/
void BLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly) {
ESP_LOGD(LOG_TAG, ">> setScanFilter: scanRequestWhitelistOnly: %d, connectWhitelistOnly: %d", scanRequestWhitelistOnly, connectWhitelistOnly);
log_v(">> setScanFilter: scanRequestWhitelistOnly: %d, connectWhitelistOnly: %d", scanRequestWhitelistOnly, connectWhitelistOnly);
if (!scanRequestWhitelistOnly && !connectWhitelistOnly) {
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
ESP_LOGD(LOG_TAG, "<< setScanFilter");
log_v("<< setScanFilter");
return;
}
if (scanRequestWhitelistOnly && !connectWhitelistOnly) {
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY;
ESP_LOGD(LOG_TAG, "<< setScanFilter");
log_v("<< setScanFilter");
return;
}
if (!scanRequestWhitelistOnly && connectWhitelistOnly) {
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST;
ESP_LOGD(LOG_TAG, "<< setScanFilter");
log_v("<< setScanFilter");
return;
}
if (scanRequestWhitelistOnly && connectWhitelistOnly) {
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST;
ESP_LOGD(LOG_TAG, "<< setScanFilter");
log_v("<< setScanFilter");
return;
}
} // setScanFilter
Expand All @@ -149,15 +140,15 @@ void BLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connectWh
* @param [in] advertisementData The data to be advertised.
*/
void BLEAdvertising::setAdvertisementData(BLEAdvertisementData& advertisementData) {
ESP_LOGD(LOG_TAG, ">> setAdvertisementData");
log_v(">> setAdvertisementData");
esp_err_t errRc = ::esp_ble_gap_config_adv_data_raw(
(uint8_t*)advertisementData.getPayload().data(),
advertisementData.getPayload().length());
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gap_config_adv_data_raw: %d %s", errRc, GeneralUtils::errorToString(errRc));
log_e("esp_ble_gap_config_adv_data_raw: %d %s", errRc, GeneralUtils::errorToString(errRc));
}
m_customAdvData = true; // Set the flag that indicates we are using custom advertising data.
ESP_LOGD(LOG_TAG, "<< setAdvertisementData");
log_v("<< setAdvertisementData");
} // setAdvertisementData


Expand All @@ -166,15 +157,15 @@ void BLEAdvertising::setAdvertisementData(BLEAdvertisementData& advertisementDat
* @param [in] advertisementData The data to be advertised.
*/
void BLEAdvertising::setScanResponseData(BLEAdvertisementData& advertisementData) {
ESP_LOGD(LOG_TAG, ">> setScanResponseData");
log_v(">> setScanResponseData");
esp_err_t errRc = ::esp_ble_gap_config_scan_rsp_data_raw(
(uint8_t*)advertisementData.getPayload().data(),
advertisementData.getPayload().length());
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gap_config_scan_rsp_data_raw: %d %s", errRc, GeneralUtils::errorToString(errRc));
log_e("esp_ble_gap_config_scan_rsp_data_raw: %d %s", errRc, GeneralUtils::errorToString(errRc));
}
m_customScanResponseData = true; // Set the flag that indicates we are using custom scan response data.
ESP_LOGD(LOG_TAG, "<< setScanResponseData");
log_v("<< setScanResponseData");
} // setScanResponseData

/**
Expand All @@ -183,7 +174,7 @@ void BLEAdvertising::setScanResponseData(BLEAdvertisementData& advertisementData
* @return N/A.
*/
void BLEAdvertising::start() {
ESP_LOGD(LOG_TAG, ">> start: customAdvData: %d, customScanResponseData: %d", m_customAdvData, m_customScanResponseData);
log_v(">> start: customAdvData: %d, customScanResponseData: %d", m_customAdvData, m_customScanResponseData);

// We have a vector of service UUIDs that we wish to advertise. In order to use the
// ESP-IDF framework, these must be supplied in a contiguous array of their 128bit (16 byte)
Expand All @@ -195,14 +186,14 @@ void BLEAdvertising::start() {
m_advData.p_service_uuid = new uint8_t[m_advData.service_uuid_len];
uint8_t* p = m_advData.p_service_uuid;
for (int i = 0; i < numServices; i++) {
ESP_LOGD(LOG_TAG, "- advertising service: %s", m_serviceUUIDs[i].toString().c_str());
log_d("- advertising service: %s", m_serviceUUIDs[i].toString().c_str());
BLEUUID serviceUUID128 = m_serviceUUIDs[i].to128();
memcpy(p, serviceUUID128.getNative()->uuid.uuid128, 16);
p += 16;
}
} else {
m_advData.service_uuid_len = 0;
ESP_LOGD(LOG_TAG, "- no services advertised");
log_d("- no services advertised");
}

esp_err_t errRc;
Expand All @@ -214,7 +205,7 @@ void BLEAdvertising::start() {
m_advData.include_txpower = !m_scanResp;
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
log_e("<< esp_ble_gap_config_adv_data: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
}
Expand All @@ -225,7 +216,7 @@ void BLEAdvertising::start() {
m_advData.include_txpower = m_scanResp;
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
log_e("<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
}
Expand All @@ -240,10 +231,10 @@ void BLEAdvertising::start() {
// Start advertising.
errRc = ::esp_ble_gap_start_advertising(&m_advParams);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_start_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
log_e("<< esp_ble_gap_start_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
ESP_LOGD(LOG_TAG, "<< start");
log_v("<< start");
} // start


Expand All @@ -253,13 +244,13 @@ void BLEAdvertising::start() {
* @return N/A.
*/
void BLEAdvertising::stop() {
ESP_LOGD(LOG_TAG, ">> stop");
log_v(">> stop");
esp_err_t errRc = ::esp_ble_gap_stop_advertising();
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gap_stop_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
log_e("esp_ble_gap_stop_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
ESP_LOGD(LOG_TAG, "<< stop");
log_v("<< stop");
} // stop

/**
Expand Down Expand Up @@ -352,12 +343,12 @@ void BLEAdvertisementData::setFlags(uint8_t flag) {
* @param [in] data Manufacturer data.
*/
void BLEAdvertisementData::setManufacturerData(std::string data) {
ESP_LOGD("BLEAdvertisementData", ">> setManufacturerData");
log_d("BLEAdvertisementData", ">> setManufacturerData");
char cdata[2];
cdata[0] = data.length() + 1;
cdata[1] = ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE; // 0xff
addData(std::string(cdata, 2) + data);
ESP_LOGD("BLEAdvertisementData", "<< setManufacturerData");
log_d("BLEAdvertisementData", "<< setManufacturerData");
} // setManufacturerData


Expand All @@ -366,12 +357,12 @@ void BLEAdvertisementData::setManufacturerData(std::string data) {
* @param [in] The complete name of the device.
*/
void BLEAdvertisementData::setName(std::string name) {
ESP_LOGD("BLEAdvertisementData", ">> setName: %s", name.c_str());
log_d("BLEAdvertisementData", ">> setName: %s", name.c_str());
char cdata[2];
cdata[0] = name.length() + 1;
cdata[1] = ESP_BLE_AD_TYPE_NAME_CMPL; // 0x09
addData(std::string(cdata, 2) + name);
ESP_LOGD("BLEAdvertisementData", "<< setName");
log_d("BLEAdvertisementData", "<< setName");
} // setName


Expand Down Expand Up @@ -455,12 +446,12 @@ void BLEAdvertisementData::setServiceData(BLEUUID uuid, std::string data) {
* @param [in] The short name of the device.
*/
void BLEAdvertisementData::setShortName(std::string name) {
ESP_LOGD("BLEAdvertisementData", ">> setShortName: %s", name.c_str());
log_d("BLEAdvertisementData", ">> setShortName: %s", name.c_str());
char cdata[2];
cdata[0] = name.length() + 1;
cdata[1] = ESP_BLE_AD_TYPE_NAME_SHORT; // 0x08
addData(std::string(cdata, 2) + name);
ESP_LOGD("BLEAdvertisementData", "<< setShortName");
log_d("BLEAdvertisementData", "<< setShortName");
} // setShortName


Expand All @@ -476,7 +467,7 @@ void BLEAdvertising::handleGAPEvent(
esp_gap_ble_cb_event_t event,
esp_ble_gap_cb_param_t* param) {

ESP_LOGD(LOG_TAG, "handleGAPEvent [event no: %d]", (int)event);
log_d("handleGAPEvent [event no: %d]", (int)event);

switch(event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: {
Expand All @@ -492,7 +483,7 @@ void BLEAdvertising::handleGAPEvent(
break;
}
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: {
ESP_LOGI(LOG_TAG, "STOP advertising");
log_i("STOP advertising");
start();
break;
}
Expand Down
8 changes: 1 addition & 7 deletions libraries/BLE/src/BLEBeacon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
#if defined(CONFIG_BT_ENABLED)
#include <string.h>
#include "BLEBeacon.h"
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
#define LOG_TAG ""
#else
#include "esp_log.h"
static const char* LOG_TAG = "BLEBeacon";
#endif

#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))

Expand Down Expand Up @@ -58,7 +52,7 @@ int8_t BLEBeacon::getSignalPower() {
*/
void BLEBeacon::setData(std::string data) {
if (data.length() != sizeof(m_beaconData)) {
ESP_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and expected %d", data.length(), sizeof(m_beaconData));
log_e("Unable to set the data ... length passed in was %d and expected %d", data.length(), sizeof(m_beaconData));
return;
}
memcpy(&m_beaconData, data.data(), sizeof(m_beaconData));
Expand Down
Loading

0 comments on commit 01d7ea7

Please sign in to comment.