2021-03-22 14:11:42 +01:00
|
|
|
#include "BleFingerprint.h"
|
2021-09-30 18:48:50 +02:00
|
|
|
#include "BleFingerprintCollection.h"
|
2022-08-07 13:31:42 +02:00
|
|
|
#include "mbedtls/aes.h"
|
2021-09-30 18:48:50 +02:00
|
|
|
#include "rssi.h"
|
2022-06-11 17:25:25 +02:00
|
|
|
#include "string_utils.h"
|
2021-04-07 02:13:13 +02:00
|
|
|
#include "util.h"
|
2021-03-22 14:11:42 +01:00
|
|
|
|
2022-03-14 02:54:50 +01:00
|
|
|
class ClientCallbacks : public BLEClientCallbacks
|
2022-02-13 18:58:17 +01:00
|
|
|
{
|
2022-03-14 02:54:50 +01:00
|
|
|
bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params)
|
2022-02-13 18:58:17 +01:00
|
|
|
{
|
2022-03-14 02:54:50 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
static ClientCallbacks clientCB;
|
2022-02-13 18:58:17 +01:00
|
|
|
|
2022-08-07 13:31:42 +02:00
|
|
|
bool BleFingerprint::shouldHide(const String &s) {
|
2022-03-06 17:48:04 +01:00
|
|
|
if (BleFingerprintCollection::include.length() > 0 && !prefixExists(BleFingerprintCollection::include, s)) return true;
|
|
|
|
return (BleFingerprintCollection::exclude.length() > 0 && prefixExists(BleFingerprintCollection::exclude, s));
|
2022-02-13 18:58:17 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 13:31:42 +02:00
|
|
|
bool BleFingerprint::setId(const String &newId, short newIdType, const String &newName) {
|
2022-09-02 00:12:58 +02:00
|
|
|
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\n", newId.c_str(), newIdType, newName.c_str(), idType);
|
2022-02-13 18:58:17 +01:00
|
|
|
|
2022-03-14 02:54:50 +01:00
|
|
|
ignore = newIdType < 0;
|
2022-08-07 13:31:42 +02:00
|
|
|
idType = newIdType;
|
2022-02-13 18:58:17 +01:00
|
|
|
|
2022-08-07 13:31:42 +02:00
|
|
|
DeviceConfig dc;
|
2022-08-23 12:55:04 +02:00
|
|
|
if (BleFingerprintCollection::FindDeviceConfig(newId, dc))
|
2022-08-07 13:31:42 +02:00
|
|
|
{
|
|
|
|
if (!dc.alias.isEmpty())
|
|
|
|
return setId(dc.alias, ID_TYPE_ALIAS, dc.name);
|
|
|
|
if (!dc.name.isEmpty())
|
|
|
|
name = dc.name;
|
|
|
|
} else
|
|
|
|
if (!newName.isEmpty() && name!=newName)
|
|
|
|
name = newName;
|
2022-07-19 03:49:25 +02:00
|
|
|
|
2022-08-07 13:31:42 +02:00
|
|
|
if (id != newId) {
|
|
|
|
|
|
|
|
hidden = shouldHide(newId);
|
2022-07-31 06:51:34 +02:00
|
|
|
countable = !ignore && !hidden && !BleFingerprintCollection::countIds.isEmpty() && prefixExists(BleFingerprintCollection::countIds, newId);
|
2022-07-19 03:49:25 +02:00
|
|
|
bool newQuery = !ignore && !BleFingerprintCollection::query.isEmpty() && prefixExists(BleFingerprintCollection::query, newId);
|
|
|
|
if (newQuery != allowQuery)
|
2022-02-13 18:58:17 +01:00
|
|
|
{
|
2022-07-19 03:49:25 +02:00
|
|
|
allowQuery = newQuery;
|
|
|
|
if (allowQuery) {
|
|
|
|
qryAttempts = 0;
|
|
|
|
if (rssi < -80)
|
|
|
|
{
|
|
|
|
qryDelayMillis = 30000;
|
|
|
|
lastQryMillis = millis();
|
2022-08-07 13:31:42 +02:00
|
|
|
} else if (rssi < -70) {
|
2022-07-19 03:49:25 +02:00
|
|
|
qryDelayMillis = 5000;
|
|
|
|
lastQryMillis = millis();
|
|
|
|
}
|
2022-02-13 18:58:17 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-18 08:30:20 +02:00
|
|
|
id = newId;
|
|
|
|
added = false;
|
|
|
|
}
|
2022-08-07 13:31:42 +02:00
|
|
|
|
2022-03-06 17:48:04 +01:00
|
|
|
return true;
|
2022-02-13 18:58:17 +01:00
|
|
|
}
|
|
|
|
|
2022-03-06 17:48:04 +01:00
|
|
|
int BleFingerprint::get1mRssi() const
|
2021-03-22 14:11:42 +01:00
|
|
|
{
|
2021-09-30 18:48:50 +02:00
|
|
|
if (calRssi != NO_RSSI) return calRssi;
|
|
|
|
if (mdRssi != NO_RSSI) return mdRssi;
|
|
|
|
if (asRssi != NO_RSSI) return asRssi;
|
2022-12-28 00:05:56 +01:00
|
|
|
return BleFingerprintCollection::rxRefRssi + DEFAULT_TX;
|
2021-09-30 18:48:50 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 12:55:04 +02:00
|
|
|
BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float fcmin, float beta, float dcutoff) : oneEuro{OneEuroFilter<float, unsigned long>(1, fcmin, beta, dcutoff)}
|
2021-09-30 18:48:50 +02:00
|
|
|
{
|
|
|
|
firstSeenMillis = millis();
|
2022-02-20 14:06:49 +01:00
|
|
|
address = NimBLEAddress(advertisedDevice->getAddress());
|
2022-07-18 08:30:20 +02:00
|
|
|
addressType = advertisedDevice->getAddressType();
|
2021-04-07 02:13:13 +02:00
|
|
|
newest = recent = oldest = rssi = advertisedDevice->getRSSI();
|
2022-02-20 22:03:03 +01:00
|
|
|
seenCount = 1;
|
2022-03-14 02:54:50 +01:00
|
|
|
|
2022-07-18 08:30:20 +02:00
|
|
|
fingerprintAddress();
|
2021-09-15 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
2022-03-06 17:48:04 +01:00
|
|
|
void BleFingerprint::fingerprint(NimBLEAdvertisedDevice *advertisedDevice)
|
2021-09-15 18:11:32 +02:00
|
|
|
{
|
2021-03-22 14:11:42 +01:00
|
|
|
if (advertisedDevice->haveName())
|
2022-06-25 00:09:48 +02:00
|
|
|
{
|
|
|
|
std::string name = advertisedDevice->getName();
|
2022-06-25 10:43:21 +02:00
|
|
|
if (!name.empty()) setId(String("name:") + kebabify(name).c_str(), ID_TYPE_NAME, String(name.c_str()));
|
2022-06-25 00:09:48 +02:00
|
|
|
}
|
2021-03-22 14:11:42 +01:00
|
|
|
|
2022-02-13 18:58:17 +01:00
|
|
|
if (advertisedDevice->getAdvType() > 0)
|
|
|
|
connectable = true;
|
|
|
|
|
2022-03-06 17:48:04 +01:00
|
|
|
size_t serviceAdvCount = advertisedDevice->getServiceUUIDCount();
|
|
|
|
size_t serviceDataCount = advertisedDevice->getServiceDataCount();
|
2022-03-14 02:54:50 +01:00
|
|
|
bool haveTxPower = advertisedDevice->haveTXPower();
|
|
|
|
int8_t txPower = advertisedDevice->getTXPower();
|
2022-03-06 17:48:04 +01:00
|
|
|
|
2022-03-14 02:54:50 +01:00
|
|
|
if (serviceAdvCount > 0) fingerprintServiceAdvertisements(advertisedDevice, serviceAdvCount, haveTxPower, txPower);
|
|
|
|
if (serviceDataCount > 0) fingerprintServiceData(advertisedDevice, serviceDataCount, haveTxPower, txPower);
|
|
|
|
if (advertisedDevice->haveManufacturerData()) fingerprintManufactureData(advertisedDevice, haveTxPower, txPower);
|
2022-03-06 17:48:04 +01:00
|
|
|
}
|
2021-09-30 18:48:50 +02:00
|
|
|
|
2022-07-18 08:30:20 +02:00
|
|
|
int bt_encrypt_be(const uint8_t *key, const uint8_t *plaintext, uint8_t *enc_data)
|
|
|
|
{
|
|
|
|
mbedtls_aes_context s = {0};
|
|
|
|
mbedtls_aes_init(&s);
|
|
|
|
|
|
|
|
if (mbedtls_aes_setkey_enc(&s, key, 128) != 0) {
|
|
|
|
mbedtls_aes_free(&s);
|
|
|
|
return BLE_HS_EUNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbedtls_aes_crypt_ecb(&s, MBEDTLS_AES_ENCRYPT, plaintext, enc_data) != 0) {
|
|
|
|
mbedtls_aes_free(&s);
|
|
|
|
return BLE_HS_EUNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_aes_free(&s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct encryption_block
|
|
|
|
{
|
2022-08-07 13:31:42 +02:00
|
|
|
uint8_t key[16];
|
|
|
|
uint8_t plain_text[16];
|
|
|
|
uint8_t cipher_text[16];
|
2022-07-18 08:30:20 +02:00
|
|
|
};
|
|
|
|
|
2022-08-07 13:31:42 +02:00
|
|
|
bool ble_ll_resolv_rpa(const uint8_t *rpa, const uint8_t *irk) {
|
2022-07-18 08:30:20 +02:00
|
|
|
struct encryption_block ecb;
|
|
|
|
|
|
|
|
auto irk32 = (const uint32_t *)irk;
|
|
|
|
auto key32 = (uint32_t *)&ecb.key[0];
|
|
|
|
auto pt32 = (uint32_t *)&ecb.plain_text[0];
|
|
|
|
|
|
|
|
key32[0] = irk32[0];
|
|
|
|
key32[1] = irk32[1];
|
|
|
|
key32[2] = irk32[2];
|
|
|
|
key32[3] = irk32[3];
|
|
|
|
|
|
|
|
pt32[0] = 0;
|
|
|
|
pt32[1] = 0;
|
|
|
|
pt32[2] = 0;
|
|
|
|
pt32[3] = 0;
|
|
|
|
|
|
|
|
ecb.plain_text[15] = rpa[3];
|
|
|
|
ecb.plain_text[14] = rpa[4];
|
|
|
|
ecb.plain_text[13] = rpa[5];
|
|
|
|
|
|
|
|
auto err = bt_encrypt_be(ecb.key, ecb.plain_text, ecb.cipher_text);
|
|
|
|
|
2022-08-07 13:31:42 +02:00
|
|
|
if (ecb.cipher_text[15] != rpa[0] || ecb.cipher_text[14] != rpa[1] || ecb.cipher_text[13] != rpa[2]) return false;
|
2022-07-18 08:30:20 +02:00
|
|
|
|
2022-08-07 13:31:42 +02:00
|
|
|
// Serial.printf("RPA resolved %d %02x%02x%02x %02x%02x%02x\n", err, rpa[0], rpa[1], rpa[2], ecb.cipher_text[15], ecb.cipher_text[14], ecb.cipher_text[13]);
|
2022-07-18 08:30:20 +02:00
|
|
|
|
2022-08-07 13:31:42 +02:00
|
|
|
return true;
|
2022-07-18 08:30:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BleFingerprint::fingerprintAddress()
|
|
|
|
{
|
|
|
|
auto mac = getMac();
|
|
|
|
if (!BleFingerprintCollection::knownMacs.isEmpty() && prefixExists(BleFingerprintCollection::knownMacs, mac))
|
|
|
|
setId("known:" + mac, ID_TYPE_KNOWN_MAC);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (addressType)
|
|
|
|
{
|
2022-08-07 13:31:42 +02:00
|
|
|
case BLE_ADDR_PUBLIC:
|
|
|
|
case BLE_ADDR_PUBLIC_ID:
|
|
|
|
setId(mac, ID_TYPE_PUBLIC_MAC);
|
|
|
|
break;
|
|
|
|
case BLE_ADDR_RANDOM: {
|
|
|
|
auto naddress = address.getNative();
|
2022-08-23 12:55:04 +02:00
|
|
|
auto irks = BleFingerprintCollection::irks;
|
2022-08-07 13:31:42 +02:00
|
|
|
auto it = std::find_if(irks.begin(), irks.end(), [naddress](uint8_t *irk) { return ble_ll_resolv_rpa(naddress, irk); });
|
|
|
|
if (it != irks.end()) {
|
|
|
|
auto irk_hex = hexStr(*it, 16);
|
|
|
|
setId(String("irk:") + irk_hex.c_str(), ID_TYPE_KNOWN_IRK);
|
2022-07-18 08:30:20 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-08-07 13:31:42 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
setId(mac, ID_TYPE_RAND_MAC);
|
|
|
|
break;
|
2022-07-18 08:30:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 02:54:50 +01:00
|
|
|
void BleFingerprint::fingerprintServiceAdvertisements(NimBLEAdvertisedDevice *advertisedDevice, size_t serviceAdvCount, bool haveTxPower, int8_t txPower)
|
2022-03-06 17:48:04 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < serviceAdvCount; i++)
|
|
|
|
{
|
|
|
|
auto uuid = advertisedDevice->getServiceUUID(i);
|
2021-09-30 18:48:50 +02:00
|
|
|
#ifdef VERBOSE
|
2022-06-25 10:43:21 +02:00
|
|
|
Serial.printf("Verbose | MAC: %s, ID: %-58s%ddBm AD: %s\n", getMac().c_str(), getId().c_str(), rssi, advertisedDevice->getServiceUUID(i).toString().c_str());
|
2021-09-30 18:48:50 +02:00
|
|
|
#endif
|
2022-03-06 17:48:04 +01:00
|
|
|
if (uuid == roomAssistantService)
|
2021-03-22 14:11:42 +01:00
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = BleFingerprintCollection::rxRefRssi + RM_ASST_TX;
|
2022-03-06 17:48:04 +01:00
|
|
|
if (!rmAsst)
|
|
|
|
{
|
|
|
|
rmAsst = true;
|
|
|
|
if (didQuery)
|
|
|
|
{
|
|
|
|
qryDelayMillis = 0;
|
|
|
|
qryAttempts = 0;
|
|
|
|
didQuery = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (uuid == tileUUID)
|
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = BleFingerprintCollection::rxRefRssi + TILE_TX;
|
2021-10-11 04:49:56 +02:00
|
|
|
setId("tile:" + getMac(), ID_TYPE_TILE);
|
2022-03-06 17:48:04 +01:00
|
|
|
return;
|
2021-03-22 14:11:42 +01:00
|
|
|
}
|
2022-03-06 17:48:04 +01:00
|
|
|
else if (uuid == sonosUUID)
|
2021-09-16 01:19:50 +02:00
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2021-10-11 04:49:56 +02:00
|
|
|
setId("sonos:" + getMac(), ID_TYPE_SONOS);
|
2022-03-06 17:48:04 +01:00
|
|
|
return;
|
2021-09-16 01:19:50 +02:00
|
|
|
}
|
2022-03-06 17:48:04 +01:00
|
|
|
else if (uuid == itagUUID)
|
2021-09-30 18:48:50 +02:00
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = BleFingerprintCollection::rxRefRssi + (haveTxPower ? txPower : ITAG_TX);
|
2021-10-11 04:49:56 +02:00
|
|
|
setId("itag:" + getMac(), ID_TYPE_ITAG);
|
2022-03-06 17:48:04 +01:00
|
|
|
return;
|
2021-10-05 13:54:41 +02:00
|
|
|
}
|
2022-03-06 17:48:04 +01:00
|
|
|
else if (uuid == trackrUUID)
|
2021-11-11 13:32:12 +01:00
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2021-11-11 13:32:12 +01:00
|
|
|
setId("trackr:" + getMac(), ID_TYPE_TRACKR);
|
2022-03-06 17:48:04 +01:00
|
|
|
return;
|
2021-11-11 13:32:12 +01:00
|
|
|
}
|
2022-05-23 02:43:47 +02:00
|
|
|
else if (uuid == tractiveUUID)
|
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-05-23 02:43:47 +02:00
|
|
|
setId("tractive:" + getMac(), ID_TYPE_TRACTIVE);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-06 17:48:04 +01:00
|
|
|
else if (uuid == vanmoofUUID)
|
2022-02-23 04:10:19 +01:00
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-02-23 04:10:19 +01:00
|
|
|
setId("vanmoof:" + getMac(), ID_TYPE_VANMOOF);
|
2022-03-06 17:48:04 +01:00
|
|
|
return;
|
2022-02-23 04:10:19 +01:00
|
|
|
}
|
2022-03-06 17:48:04 +01:00
|
|
|
else if (uuid == (meaterService))
|
2021-11-11 03:55:10 +01:00
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2021-11-11 03:55:10 +01:00
|
|
|
setId("meater:" + getMac(), ID_TYPE_MEATER);
|
2022-03-06 17:48:04 +01:00
|
|
|
return;
|
2021-11-11 03:55:10 +01:00
|
|
|
}
|
2022-05-14 17:57:42 +02:00
|
|
|
else if (uuid == nutUUID)
|
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = BleFingerprintCollection::rxRefRssi + (haveTxPower ? txPower : NUT_TX);
|
2022-05-14 17:57:42 +02:00
|
|
|
setId("nut:" + getMac(), ID_TYPE_NUT);
|
|
|
|
return;
|
|
|
|
}
|
2021-11-11 03:55:10 +01:00
|
|
|
}
|
|
|
|
|
2022-03-06 17:48:04 +01:00
|
|
|
String fingerprint = "ad:";
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-03-06 17:48:04 +01:00
|
|
|
for (int i = 0; i < serviceAdvCount; i++)
|
|
|
|
{
|
|
|
|
std::string sid = advertisedDevice->getServiceUUID(i).toString();
|
|
|
|
fingerprint = fingerprint + sid.c_str();
|
|
|
|
}
|
2022-03-14 02:54:50 +01:00
|
|
|
if (haveTxPower) fingerprint = fingerprint + String(-txPower);
|
2022-03-06 17:48:04 +01:00
|
|
|
setId(fingerprint, ID_TYPE_AD);
|
|
|
|
}
|
|
|
|
|
2022-03-14 02:54:50 +01:00
|
|
|
void BleFingerprint::fingerprintServiceData(NimBLEAdvertisedDevice *advertisedDevice, size_t serviceDataCount, bool haveTxPower, int8_t txPower)
|
2022-03-06 17:48:04 +01:00
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-07-08 16:22:21 +02:00
|
|
|
String fingerprint = "";
|
2022-03-14 02:54:50 +01:00
|
|
|
for (int i = 0; i < serviceDataCount; i++)
|
2021-11-11 03:55:10 +01:00
|
|
|
{
|
2022-03-14 02:54:50 +01:00
|
|
|
BLEUUID uuid = advertisedDevice->getServiceDataUUID(i);
|
|
|
|
std::string strServiceData = advertisedDevice->getServiceData(i);
|
2021-11-11 03:55:10 +01:00
|
|
|
#ifdef VERBOSE
|
2022-06-25 10:43:21 +02:00
|
|
|
Serial.printf("Verbose | MAC: %s, ID: %-58s%ddBm SD: %s/%s\n", getMac().c_str(), getId().c_str(), rssi, uuid.toString().c_str(), hexStr(strServiceData).c_str());
|
2021-11-11 03:55:10 +01:00
|
|
|
#endif
|
|
|
|
|
2022-03-14 02:54:50 +01:00
|
|
|
if (uuid == exposureUUID)
|
|
|
|
{ // found COVID-19 exposure tracker
|
2022-12-28 00:05:56 +01:00
|
|
|
calRssi = BleFingerprintCollection::rxRefRssi + EXPOSURE_TX;
|
2022-03-14 02:54:50 +01:00
|
|
|
setId("exp:" + String(strServiceData.length()), ID_TYPE_EXPOSURE);
|
2022-08-07 13:31:42 +02:00
|
|
|
//disc = hexStr(strServiceData).c_str();
|
2022-03-14 02:54:50 +01:00
|
|
|
}
|
2022-05-23 02:43:29 +02:00
|
|
|
else if (uuid == smartTagUUID)
|
2022-03-14 02:54:50 +01:00
|
|
|
{ // found Samsung smart tag
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-05-23 02:43:29 +02:00
|
|
|
setId("smarttag:" + String(strServiceData.length()), ID_TYPE_SMARTTAG);
|
2022-03-14 02:54:50 +01:00
|
|
|
}
|
|
|
|
else if (uuid == miThermUUID)
|
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
asRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-03-14 02:54:50 +01:00
|
|
|
if (strServiceData.length() == 15)
|
|
|
|
{ // custom format
|
|
|
|
auto serviceData = strServiceData.c_str();
|
|
|
|
temp = float(*(int16_t *)(serviceData + 6)) / 100.0f;
|
|
|
|
humidity = float(*(uint16_t *)(serviceData + 8)) / 100.0f;
|
|
|
|
mv = *(uint16_t *)(serviceData + 10);
|
|
|
|
battery = serviceData[12];
|
2021-11-11 03:55:10 +01:00
|
|
|
#ifdef VERBOSE
|
2022-03-14 02:54:50 +01:00
|
|
|
Serial.printf("Temp: %.1f°, Humidity: %.1f%%, mV: %hu, Battery: %hhu%%, flg: 0x%02hhx, cout: %hhu\n", temp, humidity, mv, battery, serviceData[14], serviceData[13]);
|
2021-11-11 03:55:10 +01:00
|
|
|
#endif
|
2022-03-14 02:54:50 +01:00
|
|
|
setId("miTherm:" + getMac(), ID_TYPE_MITHERM);
|
|
|
|
}
|
|
|
|
else if (strServiceData.length() == 13)
|
|
|
|
{ // format atc1441
|
|
|
|
auto serviceData = strServiceData.c_str();
|
|
|
|
int16_t x = (serviceData[6] << 8) | serviceData[7];
|
|
|
|
temp = float(x) / 10.0f;
|
|
|
|
humidity = serviceData[8];
|
|
|
|
mv = x = (serviceData[10] << 8) | serviceData[11];
|
|
|
|
battery = serviceData[9];
|
|
|
|
|
2021-11-11 03:55:10 +01:00
|
|
|
#ifdef VERBOSE
|
2022-03-14 02:54:50 +01:00
|
|
|
Serial.printf("Temp: %.1f°, Humidity: %.1f%%, mV: %hu, Battery: %hhu%%, cout: %hhu\n", temp, humidity, mv, battery, serviceData[12]);
|
2021-11-11 03:55:10 +01:00
|
|
|
#endif
|
2022-03-14 02:54:50 +01:00
|
|
|
setId("miTherm:" + getMac(), ID_TYPE_MITHERM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (uuid == eddystoneUUID && strServiceData.length() > 0)
|
|
|
|
{
|
|
|
|
if (strServiceData[0] == EDDYSTONE_URL_FRAME_TYPE && strServiceData.length() <= 18)
|
|
|
|
{
|
|
|
|
BLEEddystoneURL oBeacon = BLEEddystoneURL();
|
|
|
|
oBeacon.setData(strServiceData);
|
|
|
|
calRssi = EDDYSTONE_ADD_1M + oBeacon.getPower();
|
|
|
|
}
|
|
|
|
else if (strServiceData[0] == EDDYSTONE_TLM_FRAME_TYPE)
|
|
|
|
{
|
|
|
|
BLEEddystoneTLM oBeacon = BLEEddystoneTLM();
|
|
|
|
oBeacon.setData(strServiceData);
|
|
|
|
temp = oBeacon.getTemp();
|
|
|
|
mv = oBeacon.getVolt();
|
2021-09-15 18:11:32 +02:00
|
|
|
#ifdef VERBOSE
|
2022-03-14 02:54:50 +01:00
|
|
|
Serial.println(oBeacon.toString().c_str());
|
2021-09-15 18:11:32 +02:00
|
|
|
#endif
|
2021-09-11 20:04:43 +02:00
|
|
|
}
|
2022-03-14 02:54:50 +01:00
|
|
|
else if (strServiceData[0] == 0x00)
|
|
|
|
{
|
|
|
|
auto serviceData = strServiceData.c_str();
|
|
|
|
int8_t rss0m = *(int8_t *)(serviceData + 1);
|
|
|
|
calRssi = EDDYSTONE_ADD_1M + rss0m;
|
|
|
|
setId(Sprintf("eddy:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x-%02x%02x%02x%02x%02x%02x",
|
|
|
|
strServiceData[2], strServiceData[3], strServiceData[4], strServiceData[5], strServiceData[6],
|
|
|
|
strServiceData[6], strServiceData[7], strServiceData[8], strServiceData[9], strServiceData[10],
|
|
|
|
strServiceData[11], strServiceData[12], strServiceData[13], strServiceData[14], strServiceData[15],
|
|
|
|
strServiceData[16], strServiceData[17]),
|
|
|
|
ID_TYPE_EBEACON);
|
|
|
|
}
|
2021-03-24 18:16:49 +01:00
|
|
|
}
|
2022-03-14 02:54:50 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fingerprint = fingerprint + uuid.toString().c_str();
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 16:22:21 +02:00
|
|
|
if (!fingerprint.isEmpty())
|
|
|
|
{
|
|
|
|
if (haveTxPower) fingerprint = fingerprint + String(-txPower);
|
|
|
|
setId("sd:" + fingerprint, ID_TYPE_SD);
|
|
|
|
}
|
2022-03-06 17:48:04 +01:00
|
|
|
}
|
2021-09-18 19:05:07 +02:00
|
|
|
|
2022-03-14 02:54:50 +01:00
|
|
|
void BleFingerprint::fingerprintManufactureData(NimBLEAdvertisedDevice *advertisedDevice, bool haveTxPower, int8_t txPower)
|
2022-03-06 17:48:04 +01:00
|
|
|
{
|
|
|
|
std::string strManufacturerData = advertisedDevice->getManufacturerData();
|
2021-09-25 16:08:30 +02:00
|
|
|
#ifdef VERBOSE
|
2022-06-25 10:43:21 +02:00
|
|
|
Serial.printf("Verbose | MAC: %s, ID: %-58s%ddBm MD: %s\n", getMac().c_str(), getId().c_str(), rssi, hexStr(strManufacturerData).c_str());
|
2021-09-25 16:08:30 +02:00
|
|
|
#endif
|
2022-03-06 17:48:04 +01:00
|
|
|
if (strManufacturerData.length() >= 2)
|
|
|
|
{
|
|
|
|
String manuf = Sprintf("%02x%02x", strManufacturerData[1], strManufacturerData[0]);
|
2021-03-22 14:11:42 +01:00
|
|
|
|
2022-03-06 17:48:04 +01:00
|
|
|
if (manuf == "004c") // Apple
|
|
|
|
{
|
|
|
|
if (strManufacturerData.length() == 25 && strManufacturerData[2] == 0x02 && strManufacturerData[3] == 0x15)
|
2021-03-22 14:11:42 +01:00
|
|
|
{
|
2022-03-06 17:48:04 +01:00
|
|
|
BLEBeacon oBeacon = BLEBeacon();
|
|
|
|
oBeacon.setData(strManufacturerData);
|
|
|
|
calRssi = oBeacon.getSignalPower();
|
2022-08-07 13:31:42 +02:00
|
|
|
setId(Sprintf("iBeacon:%s-%u-%u", std::string(oBeacon.getProximityUUID()).c_str(), ENDIAN_CHANGE_U16(oBeacon.getMajor()), ENDIAN_CHANGE_U16(oBeacon.getMinor())), calRssi != 3 ? ID_TYPE_IBEACON : ID_TYPE_ECHO_LOST);
|
2022-03-06 17:48:04 +01:00
|
|
|
}
|
|
|
|
else if (strManufacturerData.length() >= 4 && strManufacturerData[2] == 0x10)
|
|
|
|
{
|
2022-06-11 17:25:25 +02:00
|
|
|
String pid = Sprintf("apple:%02x%02x:%u", strManufacturerData[2], strManufacturerData[3], strManufacturerData.length());
|
2022-03-14 02:54:50 +01:00
|
|
|
if (haveTxPower) pid += -txPower;
|
|
|
|
setId(pid, ID_TYPE_APPLE_NEARBY);
|
2022-03-06 17:48:04 +01:00
|
|
|
disc = hexStr(strManufacturerData.substr(4)).c_str();
|
2022-12-28 00:05:56 +01:00
|
|
|
mdRssi = BleFingerprintCollection::rxRefRssi + APPLE_TX;
|
2021-03-22 14:11:42 +01:00
|
|
|
}
|
2022-06-24 22:39:43 +02:00
|
|
|
else if (strManufacturerData.length() >= 4 && strManufacturerData[2] == 0x12 && strManufacturerData.length() == 29)
|
|
|
|
{
|
|
|
|
String pid = "apple:findmy";
|
|
|
|
setId(pid, ID_TYPE_FINDMY);
|
2022-12-28 00:05:56 +01:00
|
|
|
mdRssi = BleFingerprintCollection::rxRefRssi + APPLE_TX;
|
2022-06-24 22:39:43 +02:00
|
|
|
}
|
2022-03-14 02:54:50 +01:00
|
|
|
else if (strManufacturerData.length() >= 4)
|
2021-03-22 14:11:42 +01:00
|
|
|
{
|
2022-06-11 17:25:25 +02:00
|
|
|
String pid = Sprintf("apple:%02x%02x:%u", strManufacturerData[2], strManufacturerData[3], strManufacturerData.length());
|
2022-03-14 02:54:50 +01:00
|
|
|
if (haveTxPower) pid += -txPower;
|
|
|
|
setId(pid, ID_TYPE_MISC_APPLE);
|
2022-12-28 00:05:56 +01:00
|
|
|
mdRssi = BleFingerprintCollection::rxRefRssi + APPLE_TX;
|
2021-03-22 14:11:42 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-06 17:48:04 +01:00
|
|
|
else if (manuf == "05a7") // Sonos
|
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
mdRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-03-06 17:48:04 +01:00
|
|
|
setId("sonos:" + getMac(), ID_TYPE_SONOS);
|
|
|
|
}
|
2022-03-14 02:54:50 +01:00
|
|
|
else if (manuf == "0087") // Garmin
|
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
mdRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-03-14 02:54:50 +01:00
|
|
|
setId("garmin:" + getMac(), ID_TYPE_GARMIN);
|
|
|
|
}
|
2022-07-01 01:38:12 +02:00
|
|
|
else if (manuf == "4d4b") // iTrack
|
2022-06-18 22:30:42 +02:00
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
mdRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-06-18 22:30:42 +02:00
|
|
|
setId("iTrack:" + getMac(), ID_TYPE_ITRACK);
|
|
|
|
}
|
2022-03-06 17:48:04 +01:00
|
|
|
else if (manuf == "0157") // Mi-fit
|
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
mdRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-03-06 17:48:04 +01:00
|
|
|
setId("mifit:" + getMac(), ID_TYPE_MIFIT);
|
|
|
|
}
|
|
|
|
else if (manuf == "0006" && strManufacturerData.length() == 29) // microsoft
|
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
mdRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-03-06 17:48:04 +01:00
|
|
|
setId(Sprintf("msft:cdp:%02x%02x", strManufacturerData[3], strManufacturerData[5]), ID_TYPE_MSFT);
|
2022-08-07 13:31:42 +02:00
|
|
|
/*disc = Sprintf("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
|
2022-03-06 17:48:04 +01:00
|
|
|
strManufacturerData[6], strManufacturerData[7], strManufacturerData[8], strManufacturerData[9], strManufacturerData[10],
|
|
|
|
strManufacturerData[11], strManufacturerData[12], strManufacturerData[13], strManufacturerData[14], strManufacturerData[15],
|
|
|
|
strManufacturerData[16], strManufacturerData[17], strManufacturerData[18], strManufacturerData[19], strManufacturerData[20],
|
|
|
|
strManufacturerData[21], strManufacturerData[22], strManufacturerData[23], strManufacturerData[24], strManufacturerData[25]);
|
2022-08-07 13:31:42 +02:00
|
|
|
*/
|
2022-03-06 17:48:04 +01:00
|
|
|
}
|
|
|
|
else if (manuf == "0075") // samsung
|
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
mdRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-03-06 17:48:04 +01:00
|
|
|
setId("samsung:" + getMac(), ID_TYPE_MISC);
|
|
|
|
}
|
2022-03-14 02:54:50 +01:00
|
|
|
else if (manuf == "beac" && strManufacturerData.length() == 26)
|
2022-03-06 17:48:04 +01:00
|
|
|
{
|
|
|
|
BLEBeacon oBeacon = BLEBeacon();
|
|
|
|
oBeacon.setData(strManufacturerData.substr(0, 25));
|
2022-03-14 02:54:50 +01:00
|
|
|
setId(Sprintf("altBeacon:%s-%u-%u", std::string(oBeacon.getProximityUUID()).c_str(), ENDIAN_CHANGE_U16(oBeacon.getMajor()), ENDIAN_CHANGE_U16(oBeacon.getMinor())), ID_TYPE_ABEACON);
|
2022-03-06 17:48:04 +01:00
|
|
|
calRssi = oBeacon.getSignalPower();
|
|
|
|
}
|
|
|
|
else if (manuf != "0000")
|
|
|
|
{
|
2022-12-28 00:05:56 +01:00
|
|
|
mdRssi = haveTxPower ? BleFingerprintCollection::rxRefRssi + txPower : NO_RSSI;
|
2022-06-11 17:25:25 +02:00
|
|
|
String fingerprint = Sprintf("md:%s:%u", manuf.c_str(), strManufacturerData.length());
|
2022-03-14 02:54:50 +01:00
|
|
|
if (haveTxPower) fingerprint = fingerprint + String(-txPower);
|
2022-03-06 17:48:04 +01:00
|
|
|
setId(fingerprint, ID_TYPE_MD);
|
|
|
|
}
|
2021-03-22 14:11:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 02:13:13 +02:00
|
|
|
bool BleFingerprint::filter()
|
|
|
|
{
|
2022-02-20 22:03:03 +01:00
|
|
|
Reading<float, unsigned long> inter1, inter2;
|
|
|
|
inter1.timestamp = millis();
|
2021-09-30 18:48:50 +02:00
|
|
|
inter1.value = raw;
|
2021-04-07 02:13:13 +02:00
|
|
|
|
2021-09-30 18:48:50 +02:00
|
|
|
return oneEuro.push(&inter1, &inter2) && diffFilter.push(&inter2, &output);
|
2021-04-07 02:13:13 +02:00
|
|
|
}
|
|
|
|
|
2021-09-30 18:48:50 +02:00
|
|
|
bool BleFingerprint::seen(BLEAdvertisedDevice *advertisedDevice)
|
2021-03-22 14:11:42 +01:00
|
|
|
{
|
2021-09-30 18:48:50 +02:00
|
|
|
lastSeenMillis = millis();
|
2022-02-20 22:03:03 +01:00
|
|
|
reported = false;
|
|
|
|
|
2021-10-05 13:54:41 +02:00
|
|
|
seenCount++;
|
2021-09-30 18:48:50 +02:00
|
|
|
|
2022-06-24 22:39:43 +02:00
|
|
|
fingerprint(advertisedDevice);
|
|
|
|
|
2021-09-30 18:48:50 +02:00
|
|
|
if (ignore) return false;
|
2021-03-29 20:50:23 +02:00
|
|
|
|
2021-04-07 02:13:13 +02:00
|
|
|
oldest = recent;
|
|
|
|
recent = newest;
|
|
|
|
newest = advertisedDevice->getRSSI();
|
|
|
|
rssi = median_of_3(oldest, recent, newest);
|
|
|
|
|
2022-03-06 17:48:04 +01:00
|
|
|
float ratio = (get1mRssi() - rssi) / (10.0f * BleFingerprintCollection::absorption);
|
2021-03-30 00:12:59 +02:00
|
|
|
raw = pow(10, ratio);
|
2022-02-20 22:03:03 +01:00
|
|
|
if (filter()) hasValue = true;
|
2021-03-24 14:10:33 +01:00
|
|
|
|
2022-02-20 22:03:03 +01:00
|
|
|
if (!close && newest > CLOSE_RSSI)
|
2021-03-22 23:56:29 +01:00
|
|
|
{
|
2022-08-23 12:55:04 +02:00
|
|
|
BleFingerprintCollection::Close(this, true);
|
2022-02-20 22:03:03 +01:00
|
|
|
close = true;
|
|
|
|
}
|
|
|
|
else if (close && newest < LEFT_RSSI)
|
|
|
|
{
|
2022-08-23 12:55:04 +02:00
|
|
|
BleFingerprintCollection::Close(this, false);
|
2022-02-20 22:03:03 +01:00
|
|
|
close = false;
|
|
|
|
}
|
2021-09-30 18:48:50 +02:00
|
|
|
|
2022-02-20 22:03:03 +01:00
|
|
|
if (!added)
|
|
|
|
{
|
|
|
|
added = true;
|
|
|
|
return true;
|
2021-03-22 23:56:29 +01:00
|
|
|
}
|
2021-09-30 18:48:50 +02:00
|
|
|
|
|
|
|
return false;
|
2021-03-29 20:50:23 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:13:13 +02:00
|
|
|
void BleFingerprint::setInitial(int initalRssi, float initalDistance)
|
2021-03-30 00:12:59 +02:00
|
|
|
{
|
2021-04-07 02:13:13 +02:00
|
|
|
newest = recent = oldest = rssi = initalRssi;
|
|
|
|
raw = initalDistance;
|
|
|
|
hasValue = filter() || filter();
|
2021-03-30 00:12:59 +02:00
|
|
|
}
|
|
|
|
|
2022-07-31 06:51:34 +02:00
|
|
|
void BleFingerprint::fill(JsonObject *doc)
|
2021-03-29 20:50:23 +02:00
|
|
|
{
|
2022-03-14 02:54:50 +01:00
|
|
|
(*doc)[F("id")] = id;
|
2021-10-05 13:54:41 +02:00
|
|
|
if (!name.isEmpty()) (*doc)[F("name")] = name;
|
2021-10-11 04:49:56 +02:00
|
|
|
if (!disc.isEmpty()) (*doc)[F("disc")] = disc;
|
2021-11-11 03:55:10 +01:00
|
|
|
if (idType) (*doc)[F("idType")] = idType;
|
2021-03-31 19:36:20 +02:00
|
|
|
|
2021-10-05 13:54:41 +02:00
|
|
|
(*doc)[F("rssi@1m")] = get1mRssi();
|
|
|
|
(*doc)[F("rssi")] = rssi;
|
2021-04-02 13:56:27 +02:00
|
|
|
|
2022-07-08 01:34:59 +02:00
|
|
|
(*doc)[F("raw")] = serialized(String(raw, 2));
|
|
|
|
(*doc)[F("distance")] = serialized(String(output.value.position, 2));
|
|
|
|
(*doc)[F("speed")] = serialized(String(output.value.speed * 1e3f, 2));
|
2022-02-20 22:03:03 +01:00
|
|
|
(*doc)[F("mac")] = SMacf(address);
|
2021-03-22 14:11:42 +01:00
|
|
|
|
2022-07-31 06:51:34 +02:00
|
|
|
(*doc)[F("interval")] = (millis() - firstSeenMillis) / seenCount;
|
2022-03-14 02:54:50 +01:00
|
|
|
|
2021-11-11 03:55:10 +01:00
|
|
|
if (mv) (*doc)[F("mV")] = mv;
|
2022-03-14 02:54:50 +01:00
|
|
|
if (battery != 0xFF) (*doc)[F("batt")] = battery;
|
2022-07-08 01:34:59 +02:00
|
|
|
if (temp) (*doc)[F("temp")] = serialized(String(temp, 1));
|
|
|
|
if (humidity) (*doc)[F("rh")] = serialized(String(humidity, 1));
|
2022-07-31 06:51:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BleFingerprint::report(JsonObject *doc)
|
|
|
|
{
|
2022-08-07 13:31:42 +02:00
|
|
|
if (ignore || idType <= ID_TYPE_RAND_MAC || hidden)
|
2022-07-31 06:51:34 +02:00
|
|
|
return false;
|
2021-03-30 00:12:59 +02:00
|
|
|
|
2022-07-31 06:51:34 +02:00
|
|
|
if (reported || !hasValue)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto maxDistance = BleFingerprintCollection::maxDistance;
|
|
|
|
if (maxDistance > 0 && output.value.position > maxDistance)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto now = millis();
|
|
|
|
if ((abs(output.value.position - lastReported) < BleFingerprintCollection::skipDistance) && (lastReportedMillis > 0) && (now - lastReportedMillis < BleFingerprintCollection::skipMs))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
lastReportedMillis = now;
|
|
|
|
lastReported = output.value.position;
|
|
|
|
reported = true;
|
|
|
|
fill(doc);
|
2021-10-05 13:54:41 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BleFingerprint::query()
|
|
|
|
{
|
2022-02-14 13:50:30 +01:00
|
|
|
if (!(allowQuery || rmAsst) || didQuery) return false;
|
2021-10-12 17:23:48 +02:00
|
|
|
if (rssi < -90) return false;
|
2021-10-05 13:54:41 +02:00
|
|
|
auto now = millis();
|
2022-02-14 13:50:30 +01:00
|
|
|
|
2022-06-25 10:43:21 +02:00
|
|
|
if (now - lastSeenMillis > 5) return false;
|
2022-02-14 13:50:30 +01:00
|
|
|
|
2021-10-12 17:23:48 +02:00
|
|
|
if (now - lastQryMillis < qryDelayMillis) return false;
|
2021-10-05 13:54:41 +02:00
|
|
|
didQuery = true;
|
|
|
|
lastQryMillis = now;
|
2021-10-12 01:57:30 +02:00
|
|
|
|
2021-10-12 17:23:48 +02:00
|
|
|
bool success = false;
|
|
|
|
|
2022-06-25 10:43:21 +02:00
|
|
|
Serial.printf("%u Query | MAC: %s, ID: %-58s%ddBm %lums\n", xPortGetCoreID(), getMac().c_str(), id.c_str(), rssi, now - lastSeenMillis);
|
2022-02-13 18:58:17 +01:00
|
|
|
|
2021-10-12 01:57:30 +02:00
|
|
|
NimBLEClient *pClient = NimBLEDevice::getClientListSize() ? NimBLEDevice::getClientByPeerAddress(address) : nullptr;
|
|
|
|
if (!pClient) pClient = NimBLEDevice::getDisconnectedClient();
|
2021-10-05 13:54:41 +02:00
|
|
|
if (!pClient) pClient = NimBLEDevice::createClient();
|
2022-03-14 02:54:50 +01:00
|
|
|
pClient->setClientCallbacks(&clientCB, false);
|
2022-06-25 10:43:21 +02:00
|
|
|
pClient->setConnectionParams(12, 12, 0, 48);
|
2022-02-14 13:50:30 +01:00
|
|
|
pClient->setConnectTimeout(5);
|
2022-03-14 02:54:50 +01:00
|
|
|
NimBLEDevice::getScan()->stop();
|
2021-10-05 13:54:41 +02:00
|
|
|
if (pClient->connect(address))
|
|
|
|
{
|
2022-02-14 19:04:41 +01:00
|
|
|
bool iphone = true;
|
|
|
|
if (allowQuery)
|
2021-10-05 13:54:41 +02:00
|
|
|
{
|
2022-02-15 06:14:32 +01:00
|
|
|
std::string sMdl = pClient->getValue(deviceInformationService, modelChar);
|
|
|
|
std::string sName = pClient->getValue(genericAccessService, nameChar);
|
2022-02-14 19:04:41 +01:00
|
|
|
iphone = sMdl.find("iPhone") == 0;
|
2022-03-06 17:48:04 +01:00
|
|
|
if (!sName.empty() && sMdl.find(sName) == std::string::npos && sName != "Apple Watch")
|
2021-10-12 17:23:48 +02:00
|
|
|
{
|
2022-03-06 17:48:04 +01:00
|
|
|
if (setId(String("name:") + kebabify(sName).c_str(), ID_TYPE_QUERY_NAME, String(sName.c_str())))
|
2022-03-14 02:54:50 +01:00
|
|
|
{
|
2022-06-25 10:43:21 +02:00
|
|
|
Serial.printf("\u001b[38;5;104m%u Name | MAC: %s, ID: %-58s%ddBm %s\u001b[0m\n", xPortGetCoreID(), getMac().c_str(), id.c_str(), rssi, sName.c_str());
|
2022-03-14 02:54:50 +01:00
|
|
|
}
|
2022-02-13 18:58:17 +01:00
|
|
|
success = true;
|
2021-10-12 17:23:48 +02:00
|
|
|
}
|
2022-03-06 17:48:04 +01:00
|
|
|
|
|
|
|
if (!sMdl.empty())
|
2021-10-05 13:54:41 +02:00
|
|
|
{
|
2022-03-06 17:48:04 +01:00
|
|
|
if (setId(String("apple:") + kebabify(sMdl).c_str(), ID_TYPE_QUERY_MODEL, String(sMdl.c_str())))
|
2022-03-14 02:54:50 +01:00
|
|
|
{
|
2022-06-25 10:43:21 +02:00
|
|
|
Serial.printf("\u001b[38;5;136m%u Model | MAC: %s, ID: %-58s%ddBm %s\u001b[0m\n", xPortGetCoreID(), getMac().c_str(), id.c_str(), rssi, sMdl.c_str());
|
2022-03-14 02:54:50 +01:00
|
|
|
}
|
2022-02-13 18:58:17 +01:00
|
|
|
success = true;
|
2021-10-05 13:54:41 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-14 19:04:41 +01:00
|
|
|
|
2022-03-06 17:48:04 +01:00
|
|
|
if (rmAsst || iphone) // For some reason we often don't get room assistant's service advertisement
|
2022-02-14 19:04:41 +01:00
|
|
|
{
|
2022-02-15 06:14:32 +01:00
|
|
|
std::string sRmAst = pClient->getValue(roomAssistantService, rootAssistantCharacteristic);
|
2022-02-14 19:04:41 +01:00
|
|
|
if (!sRmAst.empty())
|
|
|
|
{
|
2022-03-06 17:48:04 +01:00
|
|
|
if (setId(String("roomAssistant:") + kebabify(sRmAst).c_str(), ID_TYPE_RM_ASST))
|
2022-03-14 02:54:50 +01:00
|
|
|
{
|
2022-06-25 10:43:21 +02:00
|
|
|
Serial.printf("\u001b[38;5;129m%u RmAst | MAC: %s, ID: %-58s%ddBm %s\u001b[0m\n", xPortGetCoreID(), getMac().c_str(), id.c_str(), rssi, sRmAst.c_str());
|
2022-03-14 02:54:50 +01:00
|
|
|
}
|
2022-02-14 19:04:41 +01:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
2021-10-05 13:54:41 +02:00
|
|
|
}
|
2021-10-12 17:23:48 +02:00
|
|
|
|
2022-02-14 14:32:03 +01:00
|
|
|
NimBLEDevice::deleteClient(pClient);
|
|
|
|
|
2021-10-12 17:23:48 +02:00
|
|
|
if (success) return true;
|
|
|
|
|
2022-02-13 18:58:17 +01:00
|
|
|
qryAttempts++;
|
2022-06-25 10:43:21 +02:00
|
|
|
qryDelayMillis = min(int(pow(10, qryAttempts)), 60000);
|
|
|
|
Serial.printf("%u QryErr| MAC: %s, ID: %-58s%ddBm Try %d, retry after %dms\n", xPortGetCoreID(), getMac().c_str(), id.c_str(), rssi, qryAttempts, qryDelayMillis);
|
2021-10-12 17:23:48 +02:00
|
|
|
|
|
|
|
didQuery = false;
|
|
|
|
|
2021-10-05 13:54:41 +02:00
|
|
|
return true;
|
2021-03-22 14:11:42 +01:00
|
|
|
}
|
2022-03-14 02:54:50 +01:00
|
|
|
|
|
|
|
bool BleFingerprint::shouldCount()
|
|
|
|
{
|
|
|
|
bool prevCounting = counting;
|
2022-07-31 06:51:34 +02:00
|
|
|
if (ignore || !countable || !hasValue)
|
2022-03-14 02:54:50 +01:00
|
|
|
counting = false;
|
2022-09-13 00:35:30 +02:00
|
|
|
else if (getMsSinceLastSeen() > BleFingerprintCollection::countMs)
|
2022-03-14 02:54:50 +01:00
|
|
|
counting = false;
|
|
|
|
else if (counting && output.value.position > BleFingerprintCollection::countExit)
|
|
|
|
counting = false;
|
|
|
|
else if (!counting && output.value.position <= BleFingerprintCollection::countEnter)
|
|
|
|
counting = true;
|
|
|
|
|
|
|
|
if (prevCounting != counting)
|
|
|
|
{
|
2022-08-23 12:55:04 +02:00
|
|
|
BleFingerprintCollection::Count(this, counting);
|
2022-03-14 02:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return counting;
|
|
|
|
}
|
2022-08-07 13:31:42 +02:00
|
|
|
|
|
|
|
void BleFingerprint::expire()
|
|
|
|
{
|
|
|
|
lastSeenMillis = 0;
|
|
|
|
}
|