From 4f6a78ea3a62bfc10052a9d7a8dcbb4d989236b8 Mon Sep 17 00:00:00 2001
From: Moritz Perschke <moritz.perschke@uibk.ac.at>
Date: Fri, 29 Jul 2022 14:53:21 +0200
Subject: [PATCH] improvements (hopefully)

---
 .gitignore                              |  1 +
 client/client/lib/espnow/src/espnow.cpp | 60 ++++++++++++++++---------
 client/client/lib/espnow/src/espnow.hpp |  9 ++--
 client/client/src/main.cpp              |  5 ++-
 4 files changed, 50 insertions(+), 25 deletions(-)

diff --git a/.gitignore b/.gitignore
index 88fc45a..b539c11 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,6 +15,7 @@ downloads/
 eggs/
 .eggs/
 lib/
+!client/client/lib
 lib64/
 parts/
 sdist/
diff --git a/client/client/lib/espnow/src/espnow.cpp b/client/client/lib/espnow/src/espnow.cpp
index 0e3ab62..d21e50b 100644
--- a/client/client/lib/espnow/src/espnow.cpp
+++ b/client/client/lib/espnow/src/espnow.cpp
@@ -4,7 +4,7 @@
 #include "espnow.hpp"
 
 uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // leaving this in as possible backup
-uint8_t host_mac[6];
+bool host_defined = false;
 esp_now_peer_info_t hostInfo;
 Preferences preferences;
 
@@ -13,14 +13,16 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status){
 }
 
 void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len){
-    // sync time
     config new_config;
     memcpy(&new_config, incomingData, sizeof(new_config)); // TODO: check for valid mac
 
+    // put the host address in flash mem
     preferences.begin("config", false);
     if(!preferences.isKey("host")){
         preferences.putBytes("host", new_config.host, sizeof(uint8_t) * 6);
-    }
+    }// host change shouldn't be an issue
+    preferences.end();
+    // sync time
 }
 
 
@@ -31,13 +33,22 @@ esp_err_t espnow_setup(){
     result = esp_now_init();
     if(result != ESP_OK){
         //initialization failed
-        return result;
+        return result; // not sure about this
     }
 
-    memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC));
+    preferences.begin("config", true);
+    if(preferences.isKey("host")){
+        // add host from flash mem as peer
+        preferences.getBytes("host", hostInfo.peer_addr, sizeof(uint8_t) * 6);
+    }
+    else{
+        // add Broadcast Mac as default
+        memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC));
+    }
+    preferences.end();
     hostInfo.channel = 0;
-    hostInfo.encrypt = true; //if we encrypt espnow we can have no more than 10 clients per host
-    result = esp_now_add_peer(&hostInfo); //there are a couple errors that can happen here
+    hostInfo.encrypt = true;
+    result = esp_now_add_peer(&hostInfo);
     if(result != ESP_OK){
         //peer couldn't be added
         return result;
@@ -50,31 +61,38 @@ esp_err_t espnow_setup(){
 }
 
 void Message::add_data(float value, int identifier){
-            data.values[amountData] = value;
-            data.identifiers[amountData] = identifier;
-            amountData++;
+            if(amountData < NUM_SENSORS){
+                data->values[amountData] = value;
+                data->identifiers[amountData] = identifier;
+                amountData++;
+            }
 
-            if(data.timestamp == NULL){
+            if(data->timestamp == 0){
                 //add timestamp
             }
         }
 
-// BROADCAST_MAC is to be changed
 esp_err_t Message::send(){
     esp_err_t success;
-    if(host_mac == NULL){
-        success = esp_now_send(BROADCAST_MAC, (uint8_t *) &data, sizeof(data));
-    }// if no host defined broadcast message
-    success = esp_now_send(host_mac, (uint8_t *) &data, sizeof(data)); 
-
+    success = esp_now_send(recipient, (uint8_t *) &data, sizeof(data)); 
     // TODO: cache data before resetting
-    data_struct empty_data;
-    data = empty_data;
-    amountData = 0;
+    free((void*) data);
 
     return success;
 }
 
+// maybe do this in constructor
 void Message::set_timestamp(long millis){
-    data.timestamp = millis;
+    data->timestamp = millis;
+}
+
+Message :: Message(){
+    data = (data_struct*) malloc(sizeof(data_struct));
+    
+    preferences.begin("config", true);
+    preferences.getBytes("host", recipient, sizeof(uint8_t) * 6);
+    preferences.end();
+
+    amountData = 0;
+    data->timestamp = 0; // I am assuming we are not sending data from Unix Epoch
 }
\ No newline at end of file
diff --git a/client/client/lib/espnow/src/espnow.hpp b/client/client/lib/espnow/src/espnow.hpp
index b2cc687..0cfd977 100644
--- a/client/client/lib/espnow/src/espnow.hpp
+++ b/client/client/lib/espnow/src/espnow.hpp
@@ -9,8 +9,9 @@ typedef struct data_struct{
     int identifiers[NUM_SENSORS];
     float values[NUM_SENSORS];
     long timestamp; //maybe make this array
-}data;
+}data_struct;
 
+// Format of the message sent from host to client
 // if more things are sent from the host the name might not be accurate anymore
 typedef struct config{
     uint8_t host[6];
@@ -19,13 +20,15 @@ typedef struct config{
 
 class Message{
     public:
+        Message();
         void add_data(float value, int identifier);
         void set_timestamp(long millis);
         esp_err_t send();
 
     private:
-        data_struct data;
-        int amountData = 0;
+        data_struct *data;
+        int amountData;
+        uint8_t recipient[6];
 
 };
 
diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp
index 91da067..0ce3c63 100644
--- a/client/client/src/main.cpp
+++ b/client/client/src/main.cpp
@@ -7,9 +7,12 @@ void setup() {
   Serial.begin(115200);
   while(!Serial);
 
-  int result = espnow_setup();
+  esp_err_t result = espnow_setup();
 }
 
 void loop() {
   // put your main code here, to run repeatedly:
+  // Message* new_data = new Message();
+  // new_data->send();
+  // delete new_data;
 }
\ No newline at end of file
-- 
GitLab