From 3da62ad3c913ae365235a2d36749c5d03770c9a3 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Sun, 10 Dec 2023 09:39:19 +0300 Subject: [PATCH] wip --- components/esp32_presense/BleFingerprint.cpp | 18 +++++++++--------- components/esp32_presense/BleFingerprint.h | 4 +++- .../BleFingerprintCollection.cpp | 10 +++++----- components/esp32_presense/MiFloraHandler.cpp | 16 ++++++++-------- components/esp32_presense/NameModelHandler.cpp | 4 ++-- 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/components/esp32_presense/BleFingerprint.cpp b/components/esp32_presense/BleFingerprint.cpp index 7570f45..0a81034 100644 --- a/components/esp32_presense/BleFingerprint.cpp +++ b/components/esp32_presense/BleFingerprint.cpp @@ -44,7 +44,7 @@ bool BleFingerprint::shouldHide(const std::string &s) { bool BleFingerprint::setId(const std::string &newId, short newIdType, const std::string &newName) { if (idType < 0 && newIdType < 0 && newIdType >= idType) return false; if (idType > 0 && newIdType <= idType) return false; - // ESP_LOGD("setId: %s %d %s OLD idType: %d\r\n", newId.c_str(), newIdType, newName.c_str(), idType); + ESP_LOGD(TAG, "setId: %s %d %s OLD idType: %d\r\n", newId.c_str(), newIdType, newName.c_str(), idType); ignore = newIdType < 0; idType = newIdType; @@ -166,7 +166,7 @@ bool ble_ll_resolv_rpa(const uint8_t *rpa, const uint8_t *irk) { if (ecb.cipher_text[15] != rpa[0] || ecb.cipher_text[14] != rpa[1] || ecb.cipher_text[13] != rpa[2]) return false; - // ESP_LOGD("RPA resolved %d %02x%02x%02x %02x%02x%02x\r\n", err, rpa[0], rpa[1], rpa[2], ecb.cipher_text[15], ecb.cipher_text[14], ecb.cipher_text[13]); + // ESP_LOGD(TAG, "RPA resolved %d %02x%02x%02x %02x%02x%02x\r\n", err, rpa[0], rpa[1], rpa[2], ecb.cipher_text[15], ecb.cipher_text[14], ecb.cipher_text[13]); return true; } @@ -209,7 +209,7 @@ void BleFingerprint::fingerprintServiceAdvertisements(NimBLEAdvertisedDevice *ad for (auto i = 0; i < serviceAdvCount; i++) { auto uuid = advertisedDevice->getServiceUUID(i); #ifdef VERBOSE - ESP_LOGD("Verbose | %s | %-58s%ddBm AD: %s\r\n", getMac().c_str(), getId().c_str(), rssi, advertisedDevice->getServiceUUID(i).toString().c_str()); + ESP_LOGD(TAG, "Verbose | %s | %-58s%ddBm AD: %s\r\n", getMac().c_str(), getId().c_str(), rssi, advertisedDevice->getServiceUUID(i).toString().c_str()); #endif if (uuid == tileUUID) { asRssi = BleFingerprintCollection::rxRefRssi + TILE_TX; @@ -267,7 +267,7 @@ void BleFingerprint::fingerprintServiceData(NimBLEAdvertisedDevice *advertisedDe BLEUUID uuid = advertisedDevice->getServiceDataUUID(i); std::string strServiceData = advertisedDevice->getServiceData(i); #ifdef VERBOSE - ESP_LOGD("Verbose | %s | %-58s%ddBm SD: %s/%s\r\n", getMac().c_str(), getId().c_str(), rssi, uuid.toString().c_str(), hexStr(strServiceData).c_str()); + ESP_LOGD(TAG, "Verbose | %s | %-58s%ddBm SD: %s/%s\r\n", getMac().c_str(), getId().c_str(), rssi, uuid.toString().c_str(), hexStr(strServiceData).c_str()); #endif if (uuid == exposureUUID) { // found COVID-19 exposure tracker @@ -286,7 +286,7 @@ void BleFingerprint::fingerprintServiceData(NimBLEAdvertisedDevice *advertisedDe mv = *(uint16_t *)(serviceData + 10); battery = serviceData[12]; #ifdef VERBOSE - ESP_LOGD("Temp: %.1f°, Humidity: %.1f%%, mV: %hu, Battery: %hhu%%, flg: 0x%02hhx, cout: %hhu\r\n", temp, humidity, mv, battery, serviceData[14], serviceData[13]); + ESP_LOGD(TAG, "Temp: %.1f°, Humidity: %.1f%%, mV: %hu, Battery: %hhu%%, flg: 0x%02hhx, cout: %hhu\r\n", temp, humidity, mv, battery, serviceData[14], serviceData[13]); #endif setId("miTherm:" + getMac(), ID_TYPE_MITHERM); } else if (strServiceData.length() == 13) { // format atc1441 @@ -298,7 +298,7 @@ void BleFingerprint::fingerprintServiceData(NimBLEAdvertisedDevice *advertisedDe battery = serviceData[9]; #ifdef VERBOSE - ESP_LOGD("Temp: %.1f°, Humidity: %.1f%%, mV: %hu, Battery: %hhu%%, cout: %hhu\r\n", temp, humidity, mv, battery, serviceData[12]); + ESP_LOGD(TAG, "Temp: %.1f°, Humidity: %.1f%%, mV: %hu, Battery: %hhu%%, cout: %hhu\r\n", temp, humidity, mv, battery, serviceData[12]); #endif setId("miTherm:" + getMac(), ID_TYPE_MITHERM); } @@ -339,7 +339,7 @@ void BleFingerprint::fingerprintServiceData(NimBLEAdvertisedDevice *advertisedDe void BleFingerprint::fingerprintManufactureData(NimBLEAdvertisedDevice *advertisedDevice, bool haveTxPower, int8_t txPower) { std::string strManufacturerData = advertisedDevice->getManufacturerData(); #ifdef VERBOSE - ESP_LOGD("Verbose | %s | %-58s%ddBm MD: %s\r\n", getMac().c_str(), getId().c_str(), rssi, hexStr(strManufacturerData).c_str()); + ESP_LOGD(TAG, "Verbose | %s | %-58s%ddBm MD: %s\r\n", getMac().c_str(), getId().c_str(), rssi, hexStr(strManufacturerData).c_str()); #endif if (strManufacturerData.length() >= 2) { std::string manuf = Sprintf("%02x%02x", strManufacturerData[1], strManufacturerData[0]); @@ -492,7 +492,7 @@ bool BleFingerprint::query() { bool success = false; - ESP_LOGD("%u Query | %s | %-58s%ddBm %lums\r\n", xPortGetCoreID(), getMac().c_str(), id.c_str(), rssi, now - lastSeenMillis); + ESP_LOGD(TAG, "%u Query | %s | %-58s%ddBm %lums\r\n", xPortGetCoreID(), getMac().c_str(), id.c_str(), rssi, now - lastSeenMillis); NimBLEClient *pClient = NimBLEDevice::getClientListSize() ? NimBLEDevice::getClientByPeerAddress(address) : nullptr; if (!pClient) pClient = NimBLEDevice::getDisconnectedClient(); @@ -518,7 +518,7 @@ bool BleFingerprint::query() { } else { qryAttempts++; qryDelayMillis = std::min(int(pow(10, qryAttempts)), 60000); - ESP_LOGD("%u QryErr | %s | %-58s%ddBm Try %d, retry after %dms\r\n", xPortGetCoreID(), getMac().c_str(), id.c_str(), rssi, qryAttempts, qryDelayMillis); + ESP_LOGD(TAG, "%u QryErr | %s | %-58s%ddBm Try %d, retry after %dms\r\n", xPortGetCoreID(), getMac().c_str(), id.c_str(), rssi, qryAttempts, qryDelayMillis); } isQuerying = false; return true; diff --git a/components/esp32_presense/BleFingerprint.h b/components/esp32_presense/BleFingerprint.h index 720dbc3..fa7b860 100644 --- a/components/esp32_presense/BleFingerprint.h +++ b/components/esp32_presense/BleFingerprint.h @@ -19,7 +19,7 @@ #include "esphome/core/hal.h" #include "esphome/core/time.h" -#define log_e ESP_LOGE +#define TAG "esp32_presense" #define millis esphome::millis // #define delay esphome::delay @@ -71,6 +71,8 @@ #define ID_TYPE_KNOWN_MAC short(210) #define ID_TYPE_ALIAS short(250) +using namespace esphome; + class BleFingerprint { public: BleFingerprint(NimBLEAdvertisedDevice *advertisedDevice, float fcmin, float beta, float dcutoff); diff --git a/components/esp32_presense/BleFingerprintCollection.cpp b/components/esp32_presense/BleFingerprintCollection.cpp index 55e929b..a2ed6b0 100644 --- a/components/esp32_presense/BleFingerprintCollection.cpp +++ b/components/esp32_presense/BleFingerprintCollection.cpp @@ -67,7 +67,7 @@ void Seen(BLEAdvertisedDevice *advertisedDevice) { bool addOrReplace(DeviceConfig config) { if (xSemaphoreTake(deviceConfigMutex, MAX_WAIT) != pdTRUE) - log_e("Couldn't take deviceConfigMutex in addOrReplace!"); + ESP_LOGE(TAG, "Couldn't take deviceConfigMutex in addOrReplace!"); for (auto &it : deviceConfigs) { if (it.id == config.id) { @@ -198,7 +198,7 @@ void CleanupOldFingerprints() { if (!any) { auto uptime = (unsigned long)(esp_timer_get_time() / 1000000ULL); if (uptime > ALLOW_BLE_CONTROLLER_RESTART_AFTER_SECS) { - ESP_LOGE("Bluetooth controller seems stuck, restarting"); + ESP_LOGE(TAG, "Bluetooth controller seems stuck, restarting"); esp_restart(); } } @@ -227,7 +227,7 @@ BleFingerprint *getFingerprintInternal(BLEAdvertisedDevice *advertisedDevice) { BleFingerprint *GetFingerprint(BLEAdvertisedDevice *advertisedDevice) { if (xSemaphoreTake(fingerprintMutex, MAX_WAIT) != pdTRUE) - log_e("Couldn't take semaphore!"); + ESP_LOGE(TAG, "Couldn't take semaphore!"); auto f = getFingerprintInternal(advertisedDevice); xSemaphoreGive(fingerprintMutex); return f; @@ -235,7 +235,7 @@ BleFingerprint *GetFingerprint(BLEAdvertisedDevice *advertisedDevice) { const std::vector GetCopy() { if (xSemaphoreTake(fingerprintMutex, MAX_WAIT) != pdTRUE) - log_e("Couldn't take fingerprintMutex!"); + ESP_LOGE(TAG, "Couldn't take fingerprintMutex!"); CleanupOldFingerprints(); std::vector copy(fingerprints); xSemaphoreGive(fingerprintMutex); @@ -253,7 +253,7 @@ bool FindDeviceConfig(const std::string &id, DeviceConfig &config) { xSemaphoreGive(deviceConfigMutex); return false; } - log_e("Couldn't take deviceConfigMutex!"); + ESP_LOGE(TAG, "Couldn't take deviceConfigMutex!"); return false; } diff --git a/components/esp32_presense/MiFloraHandler.cpp b/components/esp32_presense/MiFloraHandler.cpp index 5e8400f..9e062c3 100644 --- a/components/esp32_presense/MiFloraHandler.cpp +++ b/components/esp32_presense/MiFloraHandler.cpp @@ -10,7 +10,7 @@ bool readSensorData(BLERemoteService* floraService, DynamicJsonDocument* doc) { floraCharacteristic = floraService->getCharacteristic(uuid_sensor_data); if (floraCharacteristic == nullptr) { - ESP_LOGD("-- Can't read characteristics"); + ESP_LOGD(TAG, "-- Can't read characteristics"); return false; } @@ -20,7 +20,7 @@ bool readSensorData(BLERemoteService* floraService, DynamicJsonDocument* doc) { value = floraCharacteristic->readValue(); if (value.size() == 0) { - ESP_LOGD("Reading Value failed"); + ESP_LOGD(TAG, "Reading Value failed"); return false; } @@ -47,7 +47,7 @@ bool readBatteryData(BLERemoteService* floraService, DynamicJsonDocument* doc) { floraCharacteristic = floraService->getCharacteristic(uuid_version_battery); if (floraCharacteristic == nullptr) { - ESP_LOGD("-- Can't read characteristics"); + ESP_LOGD(TAG, "-- Can't read characteristics"); return false; } NimBLEAttValue val; @@ -55,7 +55,7 @@ bool readBatteryData(BLERemoteService* floraService, DynamicJsonDocument* doc) { val = floraCharacteristic->readValue(); if (val.size() == 0) { - ESP_LOGD("Reading Value failed"); + ESP_LOGD(TAG, "Reading Value failed"); return false; } @@ -100,15 +100,15 @@ bool getFloraData(DynamicJsonDocument* doc, BLERemoteService* floraService, BleF fillDeviceData(doc, f); if (!MiFloraHandler::readBatteryData(floraService, doc)) - ESP_LOGD("Failed reading battery data"); + ESP_LOGD(TAG, "Failed reading battery data"); if (MiFloraHandler::forceFloraServiceDataMode(floraService)) { } else { - ESP_LOGD("Failed to force data reading mode"); + ESP_LOGD(TAG, "Failed to force data reading mode"); } if (!MiFloraHandler::readSensorData(floraService, doc)) - ESP_LOGD("Failed reading sensor data"); + ESP_LOGD(TAG, "Failed reading sensor data"); return true; } @@ -120,7 +120,7 @@ bool requestData(NimBLEClient* pClient, BleFingerprint* fingerprint) // Getting NimBLERemoteService* floraService = pClient->getService(serviceUUID); if (floraService == nullptr) { - ESP_LOGD("Getting MiFlora service failed"); + ESP_LOGD(TAG, "Getting MiFlora service failed"); return false; } diff --git a/components/esp32_presense/NameModelHandler.cpp b/components/esp32_presense/NameModelHandler.cpp index 63453e9..141ad1b 100644 --- a/components/esp32_presense/NameModelHandler.cpp +++ b/components/esp32_presense/NameModelHandler.cpp @@ -7,14 +7,14 @@ bool requestData(NimBLEClient* pClient, BleFingerprint* f) { std::string sName = pClient->getValue(genericAccessService, nameChar); if (!sName.empty() && sMdl.find(sName) == std::string::npos && sName != "Apple Watch") { if (f->setId(std::string("name:") + kebabify(sName).c_str(), ID_TYPE_QUERY_NAME, std::string(sName.c_str()))) { - ESP_LOGD("\u001b[38;5;104m%u Name | %s | %-58s%ddBm %s\u001b[0m\r\n", xPortGetCoreID(), f->getMac().c_str(), f->getId().c_str(), f->getRssi(), sName.c_str()); + // ESP_LOGD("\u001b[38;5;104m%u Name | %s | %-58s%ddBm %s\u001b[0m\r\n", xPortGetCoreID(), f->getMac().c_str(), f->getId().c_str(), f->getRssi(), sName.c_str()); } return true; } if (!sMdl.empty()) { if (f->setId(std::string("apple:") + kebabify(sMdl).c_str(), ID_TYPE_QUERY_MODEL, std::string(sMdl.c_str()))) { - ESP_LOGD("\u001b[38;5;136m%u Model | %s | %-58s%ddBm %s\u001b[0m\r\n", xPortGetCoreID(), f->getMac().c_str(), f->getId().c_str(), f->getRssi(), sMdl.c_str()); + // ESP_LOGD("\u001b[38;5;136m%u Model | %s | %-58s%ddBm %s\u001b[0m\r\n", xPortGetCoreID(), f->getMac().c_str(), f->getId().c_str(), f->getRssi(), sMdl.c_str()); } return true; }