diff --git a/.gitignore b/.gitignore index c5705828cd309cd3abdb66ab19a97343448f4bf3..285d2b02689741510e677e821bf3dcb7b58f34e3 100644 --- a/.gitignore +++ b/.gitignore @@ -237,7 +237,7 @@ fabric.properties # Android studio 3.1+ serialized cache file .idea/caches/build_file_checksums.ser -.vscode/* +**/.vscode/** # Local History for Visual Studio Code .history/ diff --git a/client/client_central_mast/.gitignore b/client/client_central_mast/.gitignore index 89cc49cbd652508924b868ea609fa8f6b758ec56..8da68796a5d56e6532b1bba26481d97b52e4a99e 100644 --- a/client/client_central_mast/.gitignore +++ b/client/client_central_mast/.gitignore @@ -1,5 +1,2 @@ .pio -.vscode/.browse.c_cpp.db* -.vscode/c_cpp_properties.json -.vscode/launch.json -.vscode/ipch +.vscode/** diff --git a/client/client_mock/libs b/client/client_mock/libs new file mode 120000 index 0000000000000000000000000000000000000000..2ef4020de352bbd9b974275b7331f90f815d9b17 --- /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 faae21bb2a8b5ff08a56c6c0f26348cd1cc7cdaa..4e322c5fd2aa7baf9d260b3d8a8f760327b17038 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 641f9744f12e4b7755ea78e786fcf06448f627f6..9571102f542645103726e1859f673c748a193b91 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(uint8_t) * 6) > 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 c1a4ac57312afe9505b565b0ccfb1cf73f9175e9..316fa17638e05f5b3e11156fee8a77b80e07472b 100644 --- a/client/libs/caching/src/ram_caching.hpp +++ b/client/libs/caching/src/ram_caching.hpp @@ -2,15 +2,37 @@ #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 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 e230b55ba89f670e1ef7a9e99983081afea26ecf..b5627716f2101f11bb91e24d65b08baedae090b2 100644 --- a/client/libs/espnow/src/ESPNow.cpp +++ b/client/libs/espnow/src/ESPNow.cpp @@ -15,18 +15,6 @@ bool was_msg_received(){ return false; } -void get_host_mac(uint8_t *destination) -{ - preferences.begin("config", true); - if (preferences.isKey("host")) { - preferences.getBytes("host", destination, sizeof(uint8_t) * 6); - } else { - memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC)); - ESP_LOGI(TAG, "Backup MAC address used"); - } - preferences.end(); -} - esp_err_t add_host_to_peers(config received){ esp_now_peer_info_t host; memset(&host, 0, sizeof(host)); @@ -56,49 +44,30 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) // you can also set your own MAC https://randomnerdtutorials.com/get-change-esp32-esp8266-mac-address-arduino/ switch (received_msg.type){ case hostChange:{ + msg_recv = true; 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:{ + msg_recv = true; 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); - } - preferences.end(); - // delay(50); - msg_recv = true; } + // delay(50); + default:{ break; } @@ -117,7 +86,7 @@ esp_err_t espnow_setup() return result; // not sure about this } - get_host_mac(hostInfo.peer_addr); // check if there is a host saved in flash mem, broadcast otherwise + RtcMemory::getInstance().get_host_mac(hostInfo.peer_addr); // check if there is a host saved in flash mem, broadcast otherwise hostInfo.channel = 0; diff --git a/client/libs/espnow/src/ESPNow.hpp b/client/libs/espnow/src/ESPNow.hpp index 266b139b46c184d91ddf0b7e741b6a70a3adf72a..6a9ab47ba6a8c3ad8b7c2df085322f64f4534b25 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, diff --git a/client/libs/espnow/src/Message.cpp b/client/libs/espnow/src/Message.cpp index b24ecadcf682489c227894733fc5c612a166700e..fb5960e7ce182691e0da83daeb774e65320b592f 100644 --- a/client/libs/espnow/src/Message.cpp +++ b/client/libs/espnow/src/Message.cpp @@ -30,11 +30,11 @@ std::string Message::getMessageAsMinifiedJsonString() const Message::Message(ClientDataPackage data) : clientDataPackage(std::move(data)) { // check for existing host mac address, use broadcast otherwise - get_host_mac(recipient); + RtcMemory::getInstance().get_host_mac(recipient); } Message::Message(MeasurementData const &data, const SensorInformation &information, unsigned long timestamp) : clientDataPackage(data, information, timestamp) { // check for existing host mac address, use broadcast otherwise - get_host_mac(recipient); + RtcMemory::getInstance().get_host_mac(recipient); }