From 042bd0bcd363787c093650898c7e09dd61594f81 Mon Sep 17 00:00:00 2001 From: Moritz Perschke <moritz.perschke@uibk.ac.at> Date: Thu, 11 Aug 2022 11:28:24 +0200 Subject: [PATCH] moved retrieval of host mac from flash to seperate function --- client/client/lib/espnow/src/espnow.cpp | 36 +++++++++---------------- 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/client/client/lib/espnow/src/espnow.cpp b/client/client/lib/espnow/src/espnow.cpp index 11c1118..81fed6e 100644 --- a/client/client/lib/espnow/src/espnow.cpp +++ b/client/client/lib/espnow/src/espnow.cpp @@ -41,15 +41,7 @@ esp_err_t espnow_setup(){ return result; // not sure about this } - preferences.begin("config", false); - if(preferences.isKey("host")){ - preferences.getBytes("host", hostInfo.peer_addr, sizeof(hostInfo.peer_addr)); - Serial.println("Mac used from flash"); - } - else{ - memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(hostInfo.peer_addr)); - } - preferences.end(); // check if there is a host saved in flash mem, broadcast otherwise + get_host_mac(hostInfo.peer_addr); // check if there is a host saved in flash mem, broadcast otherwise hostInfo.channel = 0; hostInfo.encrypt = 0; @@ -92,15 +84,8 @@ esp_err_t Message::send(){ Message :: Message(){ data = (data_struct*) malloc(sizeof(data_struct)); - preferences.begin("config", true); - if(preferences.isKey("host")){ - preferences.getBytes("host", recipient, sizeof(uint8_t) * 6); - } - else{ - memcpy(recipient, BROADCAST_MAC, sizeof(BROADCAST_MAC)); - Serial.println("backup mac used"); - } - preferences.end(); + // check for existing host mac address, use broadcast otherwise + get_host_mac(recipient); data->amountData = 0; data->timestamp = rtc.getMillis(); // I am assuming we are not sending data from Unix Epoch @@ -108,18 +93,21 @@ Message :: Message(){ Message :: Message(data_struct old_data){ memcpy(data, &old_data, sizeof(data)); + get_host_mac(recipient); +} + +Message :: ~Message(){ + free((void*) data); +} +void get_host_mac(uint8_t* destination){ preferences.begin("config", true); if(preferences.isKey("host")){ - preferences.getBytes("host", recipient, sizeof(uint8_t) * 6); + preferences.getBytes("host", destination, sizeof(uint8_t) * 6); } else{ - memcpy(recipient, BROADCAST_MAC, sizeof(BROADCAST_MAC)); + memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC)); Serial.println("backup mac used"); } preferences.end(); -} - -Message :: ~Message(){ - free((void*) data); } \ No newline at end of file -- GitLab