From c8e6c63fb4b0aa09330ae7aeddda083da6ec73c0 Mon Sep 17 00:00:00 2001
From: Moritz Perschke <moritz.perschke@uibk.ac.at>
Date: Thu, 28 Jul 2022 17:33:09 +0200
Subject: [PATCH] created message class, started implementing configuration

---
 client/client/lib/espnow/src/espnow.cpp | 63 ++++++++++++++++++++-----
 client/client/lib/espnow/src/espnow.hpp | 34 ++++++++++---
 2 files changed, 78 insertions(+), 19 deletions(-)

diff --git a/client/client/lib/espnow/src/espnow.cpp b/client/client/lib/espnow/src/espnow.cpp
index 503b066..0e3ab62 100644
--- a/client/client/lib/espnow/src/espnow.cpp
+++ b/client/client/lib/espnow/src/espnow.cpp
@@ -1,9 +1,12 @@
 #include <esp_now.h>
+#include <Preferences.h>
 #include "WiFi.h"
 #include "espnow.hpp"
 
-uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //probably shouldn't be defined here, works for now
+uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // leaving this in as possible backup
+uint8_t host_mac[6];
 esp_now_peer_info_t hostInfo;
+Preferences preferences;
 
 void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status){
     // go to sleep
@@ -11,31 +14,67 @@ 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
 
-int send_data(dataMessage data){
-    esp_err_t result = esp_now_send(BROADCAST_MAC, (uint8_t *) &data, sizeof(data));
-    return result;
+    preferences.begin("config", false);
+    if(!preferences.isKey("host")){
+        preferences.putBytes("host", new_config.host, sizeof(uint8_t) * 6);
+    }
 }
 
-int espnow_setup(){
+
+
+esp_err_t espnow_setup(){
+    esp_err_t result;
     WiFi.mode(WIFI_STA);
-    if(esp_now_init() != ESP_OK){
+    result = esp_now_init();
+    if(result != ESP_OK){
         //initialization failed
-        return 1;
+        return result;
     }
 
     memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC));
     hostInfo.channel = 0;
     hostInfo.encrypt = true; //if we encrypt espnow we can have no more than 10 clients per host
-    esp_err_t peerAddStatus = esp_now_add_peer(&hostInfo); //there are a couple errors that can happen here
-    if(peerAddStatus != ESP_OK){
+    result = esp_now_add_peer(&hostInfo); //there are a couple errors that can happen here
+    if(result != ESP_OK){
         //peer couldn't be added
-        return 2;
+        return result;
     }
 
     esp_now_register_recv_cb(on_data_recv);
     esp_now_register_send_cb(on_data_sent);
 
-    return 0;
+    return ESP_OK;
 }
+
+void Message::add_data(float value, int identifier){
+            data.values[amountData] = value;
+            data.identifiers[amountData] = identifier;
+            amountData++;
+
+            if(data.timestamp == NULL){
+                //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)); 
+
+    // TODO: cache data before resetting
+    data_struct empty_data;
+    data = empty_data;
+    amountData = 0;
+
+    return success;
+}
+
+void Message::set_timestamp(long millis){
+    data.timestamp = millis;
+}
\ 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 536fba1..b2cc687 100644
--- a/client/client/lib/espnow/src/espnow.hpp
+++ b/client/client/lib/espnow/src/espnow.hpp
@@ -1,13 +1,33 @@
+#ifndef _ESPNOW
+#define _ESPNOW
 
-#include <string>
 #define NUM_SENSORS 10
 
-// I originally wanted to define the mac addresses here, but i got a "multiple definition" error?
-typedef struct dataMessage{
-    std::string identifiers[NUM_SENSORS];
+// having the data be a struct of basic types makes sending easier,
+// otherwise we would have to serialize the data before sending
+typedef struct data_struct{
+    int identifiers[NUM_SENSORS];
     float values[NUM_SENSORS];
-}dataMessage;
+    long timestamp; //maybe make this array
+}data;
 
-int send_data(dataMessage data);
+// if more things are sent from the host the name might not be accurate anymore
+typedef struct config{
+    uint8_t host[6];
+    long time_millis;
+}config;
 
-int espnow_setup();
\ No newline at end of file
+class Message{
+    public:
+        void add_data(float value, int identifier);
+        void set_timestamp(long millis);
+        esp_err_t send();
+
+    private:
+        data_struct data;
+        int amountData = 0;
+
+};
+
+esp_err_t espnow_setup();
+#endif
\ No newline at end of file
-- 
GitLab