|
|
|
@@ -1,3 +1,5 @@
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include "BleFingerprint.h"
|
|
|
|
|
|
|
|
|
|
#include "MiFloraHandler.h"
|
|
|
|
@@ -42,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;
|
|
|
|
|
// Serial.printf("setId: %s %d %s OLD idType: %d\r\n", newId.c_str(), newIdType, newName.c_str(), idType);
|
|
|
|
|
// ESP_LOGD("setId: %s %d %s OLD idType: %d\r\n", newId.c_str(), newIdType, newName.c_str(), idType);
|
|
|
|
|
|
|
|
|
|
ignore = newIdType < 0;
|
|
|
|
|
idType = newIdType;
|
|
|
|
@@ -51,17 +53,17 @@ bool BleFingerprint::setId(const std::string &newId, short newIdType, const std:
|
|
|
|
|
if (BleFingerprintCollection::FindDeviceConfig(newId, dc)) {
|
|
|
|
|
if (dc.calRssi != NO_RSSI)
|
|
|
|
|
calRssi = dc.calRssi;
|
|
|
|
|
if (!dc.alias.isEmpty())
|
|
|
|
|
if (!dc.alias.empty())
|
|
|
|
|
return setId(dc.alias, ID_TYPE_ALIAS, dc.name);
|
|
|
|
|
if (!dc.name.isEmpty())
|
|
|
|
|
if (!dc.name.empty())
|
|
|
|
|
name = dc.name;
|
|
|
|
|
} else if (!newName.isEmpty() && name != newName)
|
|
|
|
|
} else if (!newName.empty() && name != newName)
|
|
|
|
|
name = newName;
|
|
|
|
|
|
|
|
|
|
if (id != newId) {
|
|
|
|
|
bool newHidden = shouldHide(newId);
|
|
|
|
|
countable = !ignore && !hidden && !BleFingerprintCollection::countIds.isEmpty() && prefixExists(BleFingerprintCollection::countIds, newId);
|
|
|
|
|
bool newQuery = !ignore && !BleFingerprintCollection::query.isEmpty() && prefixExists(BleFingerprintCollection::query, newId);
|
|
|
|
|
countable = !ignore && !hidden && !BleFingerprintCollection::countIds.empty() && prefixExists(BleFingerprintCollection::countIds, newId);
|
|
|
|
|
bool newQuery = !ignore && !BleFingerprintCollection::query.empty() && prefixExists(BleFingerprintCollection::query, newId);
|
|
|
|
|
if (newQuery != allowQuery) {
|
|
|
|
|
allowQuery = newQuery;
|
|
|
|
|
if (allowQuery) {
|
|
|
|
@@ -164,14 +166,14 @@ 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;
|
|
|
|
|
|
|
|
|
|
// Serial.printf("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("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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BleFingerprint::fingerprintAddress() {
|
|
|
|
|
auto mac = getMac();
|
|
|
|
|
if (!BleFingerprintCollection::knownMacs.isEmpty() && prefixExists(BleFingerprintCollection::knownMacs, mac))
|
|
|
|
|
if (!BleFingerprintCollection::knownMacs.empty() && prefixExists(BleFingerprintCollection::knownMacs, mac))
|
|
|
|
|
setId("known:" + mac, ID_TYPE_KNOWN_MAC);
|
|
|
|
|
else {
|
|
|
|
|
switch (addressType) {
|
|
|
|
@@ -207,7 +209,7 @@ void BleFingerprint::fingerprintServiceAdvertisements(NimBLEAdvertisedDevice *ad
|
|
|
|
|
for (auto i = 0; i < serviceAdvCount; i++) {
|
|
|
|
|
auto uuid = advertisedDevice->getServiceUUID(i);
|
|
|
|
|
#ifdef VERBOSE
|
|
|
|
|
Serial.printf("Verbose | %s | %-58s%ddBm AD: %s\r\n", getMac().c_str(), getId().c_str(), rssi, advertisedDevice->getServiceUUID(i).toString().c_str());
|
|
|
|
|
ESP_LOGD("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;
|
|
|
|
@@ -254,7 +256,7 @@ void BleFingerprint::fingerprintServiceAdvertisements(NimBLEAdvertisedDevice *ad
|
|
|
|
|
std::string sid = advertisedDevice->getServiceUUID(i).toString();
|
|
|
|
|
fingerprint = fingerprint + sid.c_str();
|
|
|
|
|
}
|
|
|
|
|
if (haveTxPower) fingerprint = fingerprint + std::string(-txPower);
|
|
|
|
|
if (haveTxPower) fingerprint = fingerprint + std::to_string(-txPower);
|
|
|
|
|
setId(fingerprint, ID_TYPE_AD);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -265,16 +267,16 @@ void BleFingerprint::fingerprintServiceData(NimBLEAdvertisedDevice *advertisedDe
|
|
|
|
|
BLEUUID uuid = advertisedDevice->getServiceDataUUID(i);
|
|
|
|
|
std::string strServiceData = advertisedDevice->getServiceData(i);
|
|
|
|
|
#ifdef VERBOSE
|
|
|
|
|
Serial.printf("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("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
|
|
|
|
|
bcnRssi = BleFingerprintCollection::rxRefRssi + EXPOSURE_TX;
|
|
|
|
|
setId("exp:" + std::string(strServiceData.length()), ID_TYPE_EXPOSURE);
|
|
|
|
|
setId("exp:" + std::to_string(strServiceData.length()), ID_TYPE_EXPOSURE);
|
|
|
|
|
// disc = hexStr(strServiceData).c_str();
|
|
|
|
|
} else if (uuid == smartTagUUID) { // found Samsung smart tag
|
|
|
|
|
asRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
|
|
|
|
setId("smarttag:" + std::string(strServiceData.length()), ID_TYPE_SMARTTAG);
|
|
|
|
|
setId("smarttag:" + std::to_string(strServiceData.length()), ID_TYPE_SMARTTAG);
|
|
|
|
|
} else if (uuid == miThermUUID) {
|
|
|
|
|
asRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
|
|
|
|
if (strServiceData.length() == 15) { // custom format
|
|
|
|
@@ -284,7 +286,7 @@ void BleFingerprint::fingerprintServiceData(NimBLEAdvertisedDevice *advertisedDe
|
|
|
|
|
mv = *(uint16_t *)(serviceData + 10);
|
|
|
|
|
battery = serviceData[12];
|
|
|
|
|
#ifdef VERBOSE
|
|
|
|
|
Serial.printf("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("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
|
|
|
|
@@ -296,7 +298,7 @@ void BleFingerprint::fingerprintServiceData(NimBLEAdvertisedDevice *advertisedDe
|
|
|
|
|
battery = serviceData[9];
|
|
|
|
|
|
|
|
|
|
#ifdef VERBOSE
|
|
|
|
|
Serial.printf("Temp: %.1f°, Humidity: %.1f%%, mV: %hu, Battery: %hhu%%, cout: %hhu\r\n", temp, humidity, mv, battery, serviceData[12]);
|
|
|
|
|
ESP_LOGD("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);
|
|
|
|
|
}
|
|
|
|
@@ -328,8 +330,8 @@ void BleFingerprint::fingerprintServiceData(NimBLEAdvertisedDevice *advertisedDe
|
|
|
|
|
fingerprint = fingerprint + uuid.toString().c_str();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!fingerprint.isEmpty()) {
|
|
|
|
|
if (haveTxPower) fingerprint = fingerprint + std::string(-txPower);
|
|
|
|
|
if (!fingerprint.empty()) {
|
|
|
|
|
if (haveTxPower) fingerprint = fingerprint + std::to_string(-txPower);
|
|
|
|
|
setId("sd:" + fingerprint, ID_TYPE_SD);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -337,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
|
|
|
|
|
Serial.printf("Verbose | %s | %-58s%ddBm MD: %s\r\n", getMac().c_str(), getId().c_str(), rssi, hexStr(strManufacturerData).c_str());
|
|
|
|
|
ESP_LOGD("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]);
|
|
|
|
@@ -402,7 +404,7 @@ void BleFingerprint::fingerprintManufactureData(NimBLEAdvertisedDevice *advertis
|
|
|
|
|
} else if (manuf != "0000") {
|
|
|
|
|
mdRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
|
|
|
|
std::string fingerprint = Sprintf("md:%s:%u", manuf.c_str(), strManufacturerData.length());
|
|
|
|
|
if (haveTxPower) fingerprint = fingerprint + std::string(-txPower);
|
|
|
|
|
if (haveTxPower) fingerprint = fingerprint + std::to_string(-txPower);
|
|
|
|
|
setId(fingerprint, ID_TYPE_MD);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -433,25 +435,25 @@ bool BleFingerprint::seen(BLEAdvertisedDevice *advertisedDevice) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BleFingerprint::fill(JsonObject *doc) {
|
|
|
|
|
(*doc)[F("mac")] = getMac();
|
|
|
|
|
(*doc)[F("id")] = id;
|
|
|
|
|
if (!name.isEmpty()) (*doc)[F("name")] = name;
|
|
|
|
|
if (idType) (*doc)[F("idType")] = idType;
|
|
|
|
|
(*doc)["mac"] = getMac();
|
|
|
|
|
(*doc)["id"] = id;
|
|
|
|
|
if (!name.empty()) (*doc)["name"] = name;
|
|
|
|
|
if (idType) (*doc)["idType"] = idType;
|
|
|
|
|
|
|
|
|
|
(*doc)[F("rssi@1m")] = get1mRssi();
|
|
|
|
|
(*doc)[F("rssi")] = rssi;
|
|
|
|
|
(*doc)["rssi@1m"] = get1mRssi();
|
|
|
|
|
(*doc)["rssi"] = rssi;
|
|
|
|
|
|
|
|
|
|
if (isnormal(raw)) (*doc)[F("raw")] = serialized(std::string(raw, 2));
|
|
|
|
|
if (isnormal(dist)) (*doc)[F("distance")] = serialized(std::string(dist, 2));
|
|
|
|
|
if (isnormal(vari)) (*doc)[F("var")] = serialized(std::string(vari, 2));
|
|
|
|
|
if (close) (*doc)[F("close")] = true;
|
|
|
|
|
if (isnormal(raw)) (*doc)["raw"] = serialized(std::string(raw, 2));
|
|
|
|
|
if (isnormal(dist)) (*doc)["distance"] = serialized(std::string(dist, 2));
|
|
|
|
|
if (isnormal(vari)) (*doc)["var"] = serialized(std::string(vari, 2));
|
|
|
|
|
if (close) (*doc)["close"] = true;
|
|
|
|
|
|
|
|
|
|
(*doc)[F("int")] = (millis() - firstSeenMillis) / seenCount;
|
|
|
|
|
(*doc)["int"] = (millis() - firstSeenMillis) / seenCount;
|
|
|
|
|
|
|
|
|
|
if (mv) (*doc)[F("mV")] = mv;
|
|
|
|
|
if (battery != 0xFF) (*doc)[F("batt")] = battery;
|
|
|
|
|
if (temp) (*doc)[F("temp")] = serialized(std::string(temp, 1));
|
|
|
|
|
if (humidity) (*doc)[F("rh")] = serialized(std::string(humidity, 1));
|
|
|
|
|
if (mv) (*doc)["mV"] = mv;
|
|
|
|
|
if (battery != 0xFF) (*doc)["batt"] = battery;
|
|
|
|
|
if (temp) (*doc)["temp"] = serialized(std::string(temp, 1));
|
|
|
|
|
if (humidity) (*doc)["rh"] = serialized(std::string(humidity, 1));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -490,7 +492,7 @@ bool BleFingerprint::query() {
|
|
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
|
|
Serial.printf("%u Query | %s | %-58s%ddBm %lums\r\n", xPortGetCoreID(), getMac().c_str(), id.c_str(), rssi, now - lastSeenMillis);
|
|
|
|
|
ESP_LOGD("%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();
|
|
|
|
@@ -501,7 +503,7 @@ bool BleFingerprint::query() {
|
|
|
|
|
NimBLEDevice::getScan()->stop();
|
|
|
|
|
if (pClient->connect(address)) {
|
|
|
|
|
if (allowQuery) {
|
|
|
|
|
if (id.startsWith("flora:"))
|
|
|
|
|
if (id.rfind("flora:", 0) == 0)
|
|
|
|
|
success = MiFloraHandler::requestData(pClient, this);
|
|
|
|
|
else
|
|
|
|
|
success = NameModelHandler::requestData(pClient, this);
|
|
|
|
@@ -515,8 +517,8 @@ bool BleFingerprint::query() {
|
|
|
|
|
qryDelayMillis = BleFingerprintCollection::requeryMs;
|
|
|
|
|
} else {
|
|
|
|
|
qryAttempts++;
|
|
|
|
|
qryDelayMillis = min(int(pow(10, qryAttempts)), 60000);
|
|
|
|
|
Serial.printf("%u QryErr | %s | %-58s%ddBm Try %d, retry after %dms\r\n", xPortGetCoreID(), getMac().c_str(), id.c_str(), rssi, qryAttempts, qryDelayMillis);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
isQuerying = false;
|
|
|
|
|
return true;
|
|
|
|
|