From bc6814c650bd77d0155ab24d15dd778f4006bd61 Mon Sep 17 00:00:00 2001 From: Moritz Perschke <moritz.perschke@uibk.ac.at> Date: Mon, 16 Jan 2023 16:30:03 +0100 Subject: [PATCH] moved mac storage out of espnow lib --- client/client_mock/libs | 1 + client/client_mock/src/main.cpp | 4 +- client/libs/caching/src/ram_caching.cpp | 116 +++++++++++++++--------- client/libs/caching/src/ram_caching.hpp | 35 +++++-- client/libs/espnow/src/ESPNow.cpp | 39 +++----- client/libs/espnow/src/ESPNow.hpp | 1 + 6 files changed, 118 insertions(+), 78 deletions(-) create mode 120000 client/client_mock/libs diff --git a/client/client_mock/libs b/client/client_mock/libs new file mode 120000 index 0000000..2ef4020 --- /dev/null +++ b/client/client_mock/libs @@ -0,0 +1 @@ +/home/moritz/Documents/TEAM/sensor-system/client/libs/ \ No newline at end of file diff --git a/client/client_mock/src/main.cpp b/client/client_mock/src/main.cpp index faae21b..4e322c5 100644 --- a/client/client_mock/src/main.cpp +++ b/client/client_mock/src/main.cpp @@ -16,14 +16,14 @@ void send_msgs(const std::__cxx11::list<Message> msgs) { for (const Message &msg: msgs) { if (msg.send() != ESP_OK) { - RtcMemory::store(msg.getMessageAsMinifiedJsonString()); + RtcMemory::getInstance().store_data(msg.getMessageAsMinifiedJsonString()); } unsigned long ts = millis(); // it takes ~110ms for receiving an acknowledgement by the host in perfect conditions uint16_t message_timeout = 2000; while (!was_msg_received()) { if ((millis() - ts) > message_timeout) { - RtcMemory::store(msg.getMessageAsMinifiedJsonString()); + RtcMemory::getInstance().store_data(msg.getMessageAsMinifiedJsonString()); ESP_LOGE(TAG, "Timeout: Host not available\n"); break; } diff --git a/client/libs/caching/src/ram_caching.cpp b/client/libs/caching/src/ram_caching.cpp index 641f974..a7c9a65 100644 --- a/client/libs/caching/src/ram_caching.cpp +++ b/client/libs/caching/src/ram_caching.cpp @@ -1,56 +1,86 @@ #include "ram_caching.hpp" -namespace RtcMemory{ - // 2D array of 20 Strings with maxLength of 251 - RTC_DATA_ATTR char storage[maxLength_][maxSize_]; - RTC_DATA_ATTR int storedElements; - RTC_DATA_ATTR int headElement = 0; - RTC_DATA_ATTR int tailElement = 0; +static const char* TAG = "CACHING"; +RTC_DATA_ATTR char storage[maxLength_][maxSize_]; +RTC_DATA_ATTR int storedElements; +RTC_DATA_ATTR int headElement = 0; +RTC_DATA_ATTR int tailElement = 0; - static const char* TAG = "CACHING"; +void RtcMemory::store_data(std::string message){ + // turn data into char array + const char* jsonString = message.c_str(); - void store(std::string message){ - - // turn data into char array - const char* jsonString = message.c_str(); - - // move head to new element - headElement = (headElement + 1) % maxSize_; - // apparently I am not allowed to copy to rtc mem - for(int i=0; i<maxLength_; i++){ - storage[i][headElement] = jsonString[i]; - if(jsonString[i] == '\0'){ - break; - } + // move head to new element + headElement = (headElement + 1) % maxSize_; + // apparently I am not allowed to copy to rtc mem + for(int i=0; i<maxLength_; i++){ + storage[i][headElement] = jsonString[i]; + if(jsonString[i] == '\0'){ + break; } - storedElements++; - ESP_LOGE(TAG, "Moved message to storage."); } + storedElements++; + ESP_LOGE(TAG, "Moved message to storage."); +} - String get_from_storage(){ - // remove element pointed at by tail - String buf = ""; - char current = '\0'; - for(int i = 0; i<maxLength_; i++){ - current = storage[i][tailElement]; - buf += current; - if(current == '\0'){ - break; - } +String RtcMemory::get_data_from_storage(){ + // remove element pointed at by tail + String buf = ""; + char current = '\0'; + for(int i = 0; i<maxLength_; i++){ + current = storage[i][tailElement]; + buf += current; + if(current == '\0'){ + break; } - // move tail to next element - tailElement = (tailElement + 1) % maxSize_; - storedElements--; - ESP_LOGE(TAG, "Retrieved message from storage"); - return buf; } + // move tail to next element + tailElement = (tailElement + 1) % maxSize_; + storedElements--; + ESP_LOGE(TAG, "Retrieved message from storage"); + return buf; +} - bool is_full(){ - return headElement == tailElement; - } +bool RtcMemory::is_data_storage_full(){ + return headElement == tailElement; +} + +int RtcMemory::amount_stored_data(){ + return storedElements; +} + +void RtcMemory::get_host_mac(uint8_t* destination, bool open){ + + if(!open){preferences.begin("config", true);} + + if (preferences.isKey("host")) { + preferences.getBytes("host", destination, sizeof(uint8_t) * 6); + ESP_LOGI(TAG, "Host Mac retrieved from flash"); + } else { + memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC)); + ESP_LOGI(TAG, "Backup MAC address used"); + } + + if(!open){preferences.end();} - int stored_amount(){ - return storedElements; - } } + +void RtcMemory::store_mac_address(uint8_t* mac){ + preferences.begin("config", false); + if(preferences.putBytes("host", mac, sizeof(mac)) > 0){ + ESP_LOGI(TAG, "Host MAC address saved to flash %02X:%02X:%02X:%02X:%02X:%02X", mac[0], + mac[1],mac[2],mac[3],mac[4],mac[5]); + } + else{ + ESP_LOGI(TAG, "Couldn't save Host Mac to flash"); + } + preferences.end(); +} + +bool RtcMemory::does_host_exist(){ + preferences.begin("config", true); + bool answer = preferences.isKey("host"); + preferences.end(); + return answer; +} \ No newline at end of file diff --git a/client/libs/caching/src/ram_caching.hpp b/client/libs/caching/src/ram_caching.hpp index c1a4ac5..aea4108 100644 --- a/client/libs/caching/src/ram_caching.hpp +++ b/client/libs/caching/src/ram_caching.hpp @@ -2,15 +2,38 @@ #define _RAM_CACHE #include "esp_log.h" #include <ESP32Time.h> +#include <Preferences.h> #define maxSize_ 20 #define maxLength_ 251 -namespace RtcMemory { - void store(std::string message); - String get_from_storage(); - bool is_full(); - int stored_amount(); -} +// move to singleton according to https://stackoverflow.com/a/1008289 +// used to save data/mac addresses without loosing them in deep sleep +class RtcMemory { + public: + static RtcMemory &getInstance(){ + static RtcMemory instance; + return instance; + } + + RtcMemory(RtcMemory const&) = delete; + void operator=(RtcMemory const&) = delete; + + void store_data(std::string message); + String get_data_from_storage(); + bool is_data_storage_full(); + int amount_stored_data(); + void store_mac_address(uint8_t* mac); + void get_host_mac(uint8_t* destination, bool open=false); + bool does_host_exist(); + + private: + RtcMemory(){} + // used for data storage + // used for MAC storage + Preferences preferences; + uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + +}; #endif \ No newline at end of file diff --git a/client/libs/espnow/src/ESPNow.cpp b/client/libs/espnow/src/ESPNow.cpp index e230b55..7c005fd 100644 --- a/client/libs/espnow/src/ESPNow.cpp +++ b/client/libs/espnow/src/ESPNow.cpp @@ -58,50 +58,35 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) case hostChange:{ ESP_LOGI(TAG, "hostChange received"); Time::getInstance().setTime(received_msg.epoch_seconds); + // delete old host - preferences.begin("config", false); - if(preferences.isKey("host")){ + uint8_t old[6]; + RtcMemory::getInstance().get_host_mac(old); + if(memcmp(received_msg.host, old, 6) == 0){ ESP_LOGI(TAG, "removing old host"); - uint8_t old[6]; - preferences.end(); - get_host_mac(old); // maybe problem here, re-opening preferences esp_now_del_peer(old); } // add new host - preferences.begin("config", false); - if(preferences.putBytes("host", received_msg.host, sizeof(received_msg.host)) > 0){ - ESP_LOGI(TAG, "Host MAC address saved to flash %02X:%02X:%02X:%02X:%02X:%02X", received_msg.host[0], - received_msg.host[1],received_msg.host[2],received_msg.host[3],received_msg.host[4],received_msg.host[5]); - } - else{ - ESP_LOGI(TAG, "Couldn't save Host Mac to flash"); - } - preferences.end(); + RtcMemory::getInstance().store_mac_address(received_msg.host); add_host_to_peers(received_msg); } case dataAck:{ ESP_LOGI(TAG, "dataAck received."); Time::getInstance().setTime( received_msg.epoch_seconds); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset - ESP_LOGI(TAG, "Timestamp received: %ld", Time::getInstance().getEpochSeconds()); - preferences.begin("config", false); - if (!preferences.isKey("host")) { - if(preferences.putBytes("host", received_msg.host, sizeof(received_msg.host)) > 0){ - ESP_LOGI(TAG, "host MAC address saved to flash %02X:%02X:%02X:%02X:%02X:%02X", received_msg.host[0], - received_msg.host[1],received_msg.host[2],received_msg.host[3],received_msg.host[4],received_msg.host[5]); - } - - // add host to peers - add_host_to_peers(received_msg); + if(RtcMemory::getInstance().does_host_exist()){ + RtcMemory::getInstance().store_mac_address(received_msg.host); + add_host_to_peers(received_msg); } - preferences.end(); - // delay(50); - msg_recv = true; + // add host to peers } + // delay(50); + default:{ break; } + msg_recv = true; } } diff --git a/client/libs/espnow/src/ESPNow.hpp b/client/libs/espnow/src/ESPNow.hpp index 266b139..6a9ab47 100644 --- a/client/libs/espnow/src/ESPNow.hpp +++ b/client/libs/espnow/src/ESPNow.hpp @@ -10,6 +10,7 @@ #include <Preferences.h> #include <WiFi.h> #include <esp_now.h> +#include <algorithm> enum MessageType{ dataAck, -- GitLab