Skip to content
Snippets Groups Projects
Commit 59445779 authored by Zoe Michaela Dietmar Pfister's avatar Zoe Michaela Dietmar Pfister :gay_pride_flag:
Browse files

Merge branch 'espnow_cleanup' into 'develop'

moved mac storage to singleton, tried to reduce response time

See merge request !26
parents e9bcf0e1 e4806432
No related branches found
No related tags found
2 merge requests!39Merge Develop into Main,!26moved mac storage to singleton, tried to reduce response time
......@@ -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/
......
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.vscode/**
/home/moritz/Documents/TEAM/sensor-system/client/libs/
\ No newline at end of file
......@@ -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;
}
......
#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
......@@ -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
......@@ -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;
......
......@@ -10,6 +10,7 @@
#include <Preferences.h>
#include <WiFi.h>
#include <esp_now.h>
#include <algorithm>
enum MessageType{
dataAck,
......
......@@ -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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment