From d1fac93efccc204d2a3491a2767e4909f375adf3 Mon Sep 17 00:00:00 2001
From: Moritz Perschke <moritz.perschke@uibk.ac.at>
Date: Mon, 24 Oct 2022 13:50:08 +0200
Subject: [PATCH] moved from memcpy to a for loop, memcpy caused core
 panic(Store access fault)

---
 client/client/lib/caching/src/ram_caching.cpp | 61 +++++++------------
 client/client/lib/caching/src/ram_caching.hpp |  2 +-
 client/client/lib/espnow/src/Message.cpp      |  7 +--
 client/client/src/main.cpp                    | 21 +++++--
 host/esp32-espnow-recv/platformio.ini         |  4 +-
 5 files changed, 42 insertions(+), 53 deletions(-)

diff --git a/client/client/lib/caching/src/ram_caching.cpp b/client/client/lib/caching/src/ram_caching.cpp
index 08d30ee..3b16f6e 100644
--- a/client/client/lib/caching/src/ram_caching.cpp
+++ b/client/client/lib/caching/src/ram_caching.cpp
@@ -2,7 +2,7 @@
 
 namespace RtcMemory{
     // 2D array of 20 Strings with maxLength of 251
-    RTC_DATA_ATTR char storage[maxSize_][maxLength_];
+    RTC_DATA_ATTR char storage[maxLength_][maxSize_];
     RTC_DATA_ATTR int headElement = 0;
     RTC_DATA_ATTR int tailElement = 0;
 
@@ -14,57 +14,38 @@ namespace RtcMemory{
         // turn data into char array
         const char* jsonString = message.c_str();
     
-        // move head to beginning of new element
-        // head = (head + (sizeof(char) * 251)) % sizeof(storage);
+        // move head to new element
         headElement = (headElement + 1) % maxSize_;
-        // copy data to earliest free block
-        memcpy( &storage + headElement*maxSize_,
-                jsonString,
-                sizeof(jsonString)
-        );
-        ESP_LOGI(TAG, "Moved message to storage.");
+        // apparently I am not allowed to copy to rtc mem
+        for(int i=0; i<maxLength_; i++){
+            storage[i][headElement] = jsonString[i];
+            Serial.print(jsonString[i]);
+            if(jsonString[i] == '\0'){
+                Serial.println("\n");
+                Serial.flush();
+                break;
+            }
+        }
+        ESP_LOGE(TAG, "Moved message to storage.");
     }
 
     String get_from_storage(){
         // remove element pointed at by tail
         String buf = "";
-        char current;
-        for(int i = 0; i<251; i++){
-            // i dont like this
-            memcpy(&current, &storage + (tailElement*maxSize_) + i, sizeof(char));
+        char current = '\0';
+        for(int i = 0; i<maxLength_; i++){
+            current = storage[i][tailElement];
             buf += current;
-            if(current = '\0'){
+            Serial.print(current);
+            if(current == '\0'){
+                Serial.println("\n");
+                Serial.flush();
                 break;
             }
         }
         // move tail to next element
         tailElement = (tailElement + 1) % maxLength_;
-        Serial.println(buf);
-        ESP_LOGI(TAG, "Retrieved message from storage");
+        ESP_LOGE(TAG, "Retrieved message from storage");
         return buf;
     }
 }
-
-//RTC_DATA_ATTR int cachedAmount = -1;
-//RTC_DATA_ATTR ClientDataPackage backup[NUM_SENSORS];
-//
-//ClientDataPackage ram_cache_pop()
-//{
-//	return backup[cachedAmount--];
-//}
-//
-//void ram_cache_push(ClientDataPackage data)
-//{
-//	backup[++cachedAmount] = data;
-//	ESP_LOGI(TAG, "ClientDataPackage saved");
-//}
-//
-//bool ram_cache_is_empty()
-//{
-//	return cachedAmount == -1;
-//}
-//
-//bool ram_cache_is_full()
-//{
-//	return cachedAmount == 9;
-//}
\ No newline at end of file
diff --git a/client/client/lib/caching/src/ram_caching.hpp b/client/client/lib/caching/src/ram_caching.hpp
index 7a0dd97..b16e248 100644
--- a/client/client/lib/caching/src/ram_caching.hpp
+++ b/client/client/lib/caching/src/ram_caching.hpp
@@ -3,7 +3,7 @@
 #include "esp_log.h"
 #include <ESP32Time.h>
 
-#define maxSize_ 20
+#define maxSize_ 2
 #define maxLength_ 251
 
 namespace RtcMemory {
diff --git a/client/client/lib/espnow/src/Message.cpp b/client/client/lib/espnow/src/Message.cpp
index bd1e08a..7f7f8e2 100644
--- a/client/client/lib/espnow/src/Message.cpp
+++ b/client/client/lib/espnow/src/Message.cpp
@@ -15,12 +15,9 @@ esp_err_t Message::send() const
 	if (success != ESP_OK) {
 		ESP_LOGE(TAG, "Error sending the data");
 		Serial.println(success, HEX);
-		// TODO REWRITE FOR JSON
-		//		if (!ram_cache_is_full()) {
-		//			//			ram_cache_push(messageData);
-		//		}
+		// Removed caching from here, better do this in main
 	}
-	ESP_LOGD(TAG, "Sent data: %s", messageData.c_str());
+	ESP_LOGE(TAG, "Sent data: %s", messageData.c_str());
 
 	ESP_LOGD(TAG, "time sent: %l", clientDataPackage.getTimestamp());
 	ESP_LOGD(TAG, "send status: %d", success);
diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp
index 6281ac7..4656cf0 100644
--- a/client/client/src/main.cpp
+++ b/client/client/src/main.cpp
@@ -32,10 +32,21 @@ void loop()
 		auto messages = drs26.buildMessages();
 
 		for (const Message &message : messages) {
-			if(message.send() != ESP_OK){
-				RtcMemory::store(message.getMessageAsMinifiedJsonString());
-			}
+			// if(message.send() != ESP_OK){
+			// 	RtcMemory::store(message.getMessageAsMinifiedJsonString());
+			// }
+			// some sort of if(message received) is needed as well
+			RtcMemory::store(message.getMessageAsMinifiedJsonString());
+			// Serial.println("original string:");
+			// Serial.println(message.getMessageAsMinifiedJsonString().c_str());
+			// Serial.println("length: " + (String) sizeof(message.getMessageAsMinifiedJsonString().c_str()));
+			// Serial.println("\n after retrieval:");
+			String test = RtcMemory::get_from_storage();
+			// Serial.println(test);
+			// Serial.println("length: " + (String) sizeof(test));
+			Serial.println("------------------------");
 		}
+		Serial.println("================================");
 
 	} catch (const NoDataAvailableException &e) {
 		std::cerr << e.what() << '\n';
@@ -48,6 +59,6 @@ void loop()
 	Serial.print("This device: ");
 	Serial.println(WiFi.macAddress());
 	Serial.println("\n");
-	delay(5000);
-	DeepSleep::deep_sleep(5);
+	delay(10000);
+	// DeepSleep::deep_sleep(5);
 }
diff --git a/host/esp32-espnow-recv/platformio.ini b/host/esp32-espnow-recv/platformio.ini
index 73a15dd..808a5bb 100644
--- a/host/esp32-espnow-recv/platformio.ini
+++ b/host/esp32-espnow-recv/platformio.ini
@@ -13,8 +13,8 @@ platform = espressif32
 board = esp32-c3-devkitm-1
 monitor_speed = 115200
 framework = arduino
-monitor_port = /dev/ttyUSB1
-upload_port = /dev/ttyUSB1
+; monitor_port = /dev/ttyUSB1
+; upload_port = /dev/ttyUSB1
 build_flags =
     -I include
     -DCORE_DEBUG_LEVEL=5
-- 
GitLab