Fix seen dangling ptr issue
This commit is contained in:
parent
51c7acfa8c
commit
b05f831f9a
|
@ -12,7 +12,7 @@ static const int defaultTxPower = -59;
|
||||||
|
|
||||||
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8))
|
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8))
|
||||||
#define Sprintf(f, ...) ({ char* s; asprintf(&s, f, __VA_ARGS__); String r = s; free(s); r; })
|
#define Sprintf(f, ...) ({ char* s; asprintf(&s, f, __VA_ARGS__); String r = s; free(s); r; })
|
||||||
#define SDateTimef(f) ({ struct tm firstSeenTm; gmtime_r(&f, &firstSeenTm); Sprintf("%d/%d/%d %d:%.2d:%.2d", firstSeenTm.tm_mon, firstSeenTm.tm_mday, 1900 + firstSeenTm.tm_year, firstSeenTm.tm_hour, firstSeenTm.tm_min, firstSeenTm.tm_sec); })
|
//define SDateTimef(f) ({ struct tm firstSeenTm; gmtime_r(&f, &firstSeenTm); Sprintf("%d/%d/%d %d:%.2d:%.2d", firstSeenTm.tm_mon, firstSeenTm.tm_mday, 1900 + firstSeenTm.tm_year, firstSeenTm.tm_hour, firstSeenTm.tm_min, firstSeenTm.tm_sec); })
|
||||||
#define SMacf(f) ({ auto nativeAddress = f.getNative(); Sprintf("%02x%02x%02x%02x%02x%02x", nativeAddress[5], nativeAddress[4], nativeAddress[3], nativeAddress[2], nativeAddress[1], nativeAddress[0]); })
|
#define SMacf(f) ({ auto nativeAddress = f.getNative(); Sprintf("%02x%02x%02x%02x%02x%02x", nativeAddress[5], nativeAddress[4], nativeAddress[3], nativeAddress[2], nativeAddress[1], nativeAddress[0]); })
|
||||||
|
|
||||||
static String getProximityUUIDString(BLEBeacon beacon)
|
static String getProximityUUIDString(BLEBeacon beacon)
|
||||||
|
@ -45,7 +45,7 @@ BleFingerprint::~BleFingerprint()
|
||||||
|
|
||||||
BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice)
|
BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice)
|
||||||
{
|
{
|
||||||
firstSeen = time(nullptr);
|
firstSeenMicros = esp_timer_get_time();
|
||||||
address = advertisedDevice->getAddress();
|
address = advertisedDevice->getAddress();
|
||||||
|
|
||||||
String mac_address = SMacf(address);
|
String mac_address = SMacf(address);
|
||||||
|
@ -167,7 +167,7 @@ BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice)
|
||||||
|
|
||||||
void BleFingerprint::seen(BLEAdvertisedDevice *advertisedDevice)
|
void BleFingerprint::seen(BLEAdvertisedDevice *advertisedDevice)
|
||||||
{
|
{
|
||||||
lastSeen = time(nullptr);
|
lastSeenMicros = esp_timer_get_time();
|
||||||
|
|
||||||
rssi = advertisedDevice->getRSSI();
|
rssi = advertisedDevice->getRSSI();
|
||||||
if (!calRssi)
|
if (!calRssi)
|
||||||
|
@ -176,6 +176,7 @@ void BleFingerprint::seen(BLEAdvertisedDevice *advertisedDevice)
|
||||||
float ratio = (calRssi - rssi) / 35.0f;
|
float ratio = (calRssi - rssi) / 35.0f;
|
||||||
raw = pow(10, ratio);
|
raw = pow(10, ratio);
|
||||||
setDistance(raw);
|
setDistance(raw);
|
||||||
|
reported = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BleFingerprint::setDistance(float distFl)
|
void BleFingerprint::setDistance(float distFl)
|
||||||
|
@ -207,6 +208,18 @@ bool BleFingerprint::report(JsonDocument *doc, int maxDistance)
|
||||||
if (maxDistance > 0 && output.value.position > maxDistance)
|
if (maxDistance > 0 && output.value.position > maxDistance)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if (reported)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
auto now = esp_timer_get_time();
|
||||||
|
|
||||||
|
if (abs(output.value.position - lastReported) < 0.05f && abs(now - lastReportedMicros) < 5000000)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
lastReportedMicros = now;
|
||||||
|
lastReported = output.value.position;
|
||||||
|
reported = true;
|
||||||
|
|
||||||
String mac = SMacf(address);
|
String mac = SMacf(address);
|
||||||
if (output.value.position < 0.5)
|
if (output.value.position < 0.5)
|
||||||
{
|
{
|
||||||
|
@ -235,8 +248,5 @@ bool BleFingerprint::report(JsonDocument *doc, int maxDistance)
|
||||||
(*doc)[F("distance")] = round(output.value.position * 100.0f) / 100.0f;
|
(*doc)[F("distance")] = round(output.value.position * 100.0f) / 100.0f;
|
||||||
(*doc)[F("speed")] = round(output.value.speed * 1e7f) / 10.0f;
|
(*doc)[F("speed")] = round(output.value.speed * 1e7f) / 10.0f;
|
||||||
|
|
||||||
(*doc)[F("first")] = SDateTimef(firstSeen);
|
|
||||||
(*doc)[F("last")] = SDateTimef(lastSeen);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,13 +28,12 @@ public:
|
||||||
void setAddress(NimBLEAddress newAddr) { address = newAddr; }
|
void setAddress(NimBLEAddress newAddr) { address = newAddr; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool hasValue = false, enroll = false;
|
bool hasValue = false, enroll = false, reported = false;
|
||||||
NimBLEAddress address;
|
NimBLEAddress address;
|
||||||
String id, name, url;
|
String id, name, url;
|
||||||
int rssi, calRssi;
|
int rssi, calRssi;
|
||||||
float raw;
|
float raw, lastReported = 0;
|
||||||
time_t firstSeen;
|
long firstSeenMicros, lastSeenMicros = 0, lastReportedMicros = 0;
|
||||||
time_t lastSeen;
|
|
||||||
|
|
||||||
Reading<Differential<float>> output;
|
Reading<Differential<float>> output;
|
||||||
|
|
||||||
|
|
32
src/main.cpp
32
src/main.cpp
|
@ -58,16 +58,14 @@ bool sendTelemetry(int totalSeen = -1, int totalReported = -1, int totalAdverts
|
||||||
tele["adverts"] = totalAdverts;
|
tele["adverts"] = totalAdverts;
|
||||||
if (sendFailures > 0)
|
if (sendFailures > 0)
|
||||||
tele["sendFails"] = sendFailures;
|
tele["sendFails"] = sendFailures;
|
||||||
if (reconnectAttempts > 0)
|
if (reconnectTries > 0)
|
||||||
tele["reconnectAttempts"] = reconnectAttempts;
|
tele["reconnectTries"] = reconnectTries;
|
||||||
|
|
||||||
tele["resetCore0"] = resetReason(rtc_get_reset_reason(0));
|
|
||||||
tele["resetCore1"] = resetReason(rtc_get_reset_reason(1));
|
|
||||||
|
|
||||||
tele["freeHeap"] = ESP.getFreeHeap();
|
tele["freeHeap"] = ESP.getFreeHeap();
|
||||||
tele["minFreeHeap"] = ESP.getMinFreeHeap();
|
tele["minFreeHeap"] = ESP.getMinFreeHeap();
|
||||||
tele["heapSize"] = ESP.getHeapSize();
|
tele["heapSize"] = ESP.getHeapSize();
|
||||||
tele["maxAllocHeap"] = ESP.getMaxAllocHeap();
|
tele["maxAllocHeap"] = ESP.getMaxAllocHeap();
|
||||||
|
tele["resetReason"] = resetReason(rtc_get_reset_reason(0));
|
||||||
|
|
||||||
char teleMessageBuffer[512];
|
char teleMessageBuffer[512];
|
||||||
serializeJson(tele, teleMessageBuffer);
|
serializeJson(tele, teleMessageBuffer);
|
||||||
|
@ -88,16 +86,15 @@ void connectToWifi()
|
||||||
{
|
{
|
||||||
Serial.printf("Connecting to WiFi (%s)...", WiFi.macAddress().c_str());
|
Serial.printf("Connecting to WiFi (%s)...", WiFi.macAddress().c_str());
|
||||||
|
|
||||||
// Set custom callback functions
|
|
||||||
WiFiSettings.onSuccess = []() {
|
WiFiSettings.onSuccess = []() {
|
||||||
digitalWrite(LED_BUILTIN, LED_BUILTIN_ON); // Turn LED on
|
digitalWrite(LED_BUILTIN, LED_BUILTIN_ON);
|
||||||
};
|
};
|
||||||
WiFiSettings.onFailure = []() {
|
WiFiSettings.onFailure = []() {
|
||||||
digitalWrite(LED_BUILTIN, !LED_BUILTIN_ON); // Turn LED off
|
digitalWrite(LED_BUILTIN, !LED_BUILTIN_ON);
|
||||||
};
|
};
|
||||||
WiFiSettings.onWaitLoop = []() {
|
WiFiSettings.onWaitLoop = []() {
|
||||||
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // Toggle LED
|
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
|
||||||
return 500; // Delay next function call by 500ms
|
return 500;
|
||||||
};
|
};
|
||||||
WiFiSettings.onPortalWaitLoop = []() {
|
WiFiSettings.onPortalWaitLoop = []() {
|
||||||
if (getUptimeSeconds() > 600)
|
if (getUptimeSeconds() > 600)
|
||||||
|
@ -133,7 +130,7 @@ void connectToWifi()
|
||||||
void onMqttConnect(bool sessionPresent)
|
void onMqttConnect(bool sessionPresent)
|
||||||
{
|
{
|
||||||
Serial.println("Connected to MQTT");
|
Serial.println("Connected to MQTT");
|
||||||
reconnectAttempts = 0;
|
reconnectTries = 0;
|
||||||
|
|
||||||
if (mqttClient.publish(availabilityTopic.c_str(), 0, 1, "CONNECTED") == true)
|
if (mqttClient.publish(availabilityTopic.c_str(), 0, 1, "CONNECTED") == true)
|
||||||
{
|
{
|
||||||
|
@ -161,7 +158,7 @@ void reconnect(TimerHandle_t xTimer)
|
||||||
if (WiFi.isConnected() && mqttClient.connected())
|
if (WiFi.isConnected() && mqttClient.connected())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (reconnectAttempts++ > 10)
|
if (reconnectTries++ > 10)
|
||||||
{
|
{
|
||||||
log_e("Too many reconnect attempts; Restarting");
|
log_e("Too many reconnect attempts; Restarting");
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
|
@ -191,11 +188,9 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
int getTotalAdverts() { return totalSeen; }
|
int getTotalAdverts() { return totalSeen; }
|
||||||
std::set<BleFingerprint *> getSeen() { return seen; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int totalSeen = 0;
|
int totalSeen = 0;
|
||||||
std::set<BleFingerprint *> seen;
|
|
||||||
|
|
||||||
void onResult(BLEAdvertisedDevice *advertisedDevice)
|
void onResult(BLEAdvertisedDevice *advertisedDevice)
|
||||||
{
|
{
|
||||||
|
@ -205,7 +200,6 @@ private:
|
||||||
digitalWrite(LED_BUILTIN, LED_BUILTIN_ON);
|
digitalWrite(LED_BUILTIN, LED_BUILTIN_ON);
|
||||||
BleFingerprint *f = getFingerprint(advertisedDevice);
|
BleFingerprint *f = getFingerprint(advertisedDevice);
|
||||||
f->seen(advertisedDevice);
|
f->seen(advertisedDevice);
|
||||||
seen.insert(f);
|
|
||||||
digitalWrite(LED_BUILTIN, !LED_BUILTIN_ON);
|
digitalWrite(LED_BUILTIN, !LED_BUILTIN_ON);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -268,7 +262,13 @@ void scanForDevices(void *parameter)
|
||||||
|
|
||||||
int totalSeen = 0;
|
int totalSeen = 0;
|
||||||
int totalReported = 0;
|
int totalReported = 0;
|
||||||
auto seen = results.getSeen();
|
|
||||||
|
if (xSemaphoreTake(fingerprintSemaphore, 1000) != pdTRUE)
|
||||||
|
log_e("Couldn't take semaphore!");
|
||||||
|
std::list<BleFingerprint *> seen(fingerprints);
|
||||||
|
if (xSemaphoreGive(fingerprintSemaphore) != pdTRUE)
|
||||||
|
log_e("Couldn't give semaphore!");
|
||||||
|
|
||||||
for (auto it = seen.begin(); it != seen.end(); ++it)
|
for (auto it = seen.begin(); it != seen.end(); ++it)
|
||||||
{
|
{
|
||||||
totalSeen++;
|
totalSeen++;
|
||||||
|
|
|
@ -49,7 +49,7 @@ TaskHandle_t scannerTask;
|
||||||
|
|
||||||
bool updateInProgress = false;
|
bool updateInProgress = false;
|
||||||
String localIp;
|
String localIp;
|
||||||
int reconnectAttempts = 0;
|
int reconnectTries = 0;
|
||||||
int sendFailures = 0;
|
int sendFailures = 0;
|
||||||
|
|
||||||
String mqttHost;
|
String mqttHost;
|
||||||
|
|
Loading…
Reference in New Issue