diff --git a/host/host_central_mast/include/QueueStruct.h b/host/host_central_mast/include/QueueStruct.h new file mode 100644 index 0000000000000000000000000000000000000000..15607ea353f5b0fc52b5f4385132deb2fa761595 --- /dev/null +++ b/host/host_central_mast/include/QueueStruct.h @@ -0,0 +1,13 @@ +// +// Created by zoe on 2/7/23. +// + +#ifndef HOST_CENTRAL_MAST_QUEUESTRUCT_H +#define HOST_CENTRAL_MAST_QUEUESTRUCT_H + +struct QueueStruct { + std::array<ClientDataPackage, 6> data; + uint8_t mac[6]{}; +}; + +#endif // HOST_CENTRAL_MAST_QUEUESTRUCT_H diff --git a/host/host_central_mast/lib/Utilities/Utilities.cpp b/host/host_central_mast/lib/Utilities/Utilities.cpp index 1f3c8d8d49ecef0ac885dae9ad21b6d7d1ebcfc5..50e18e0adbb892bd01ceeac66dde1a77eb57f5f1 100644 --- a/host/host_central_mast/lib/Utilities/Utilities.cpp +++ b/host/host_central_mast/lib/Utilities/Utilities.cpp @@ -237,6 +237,40 @@ namespace SDUtilities { } } // namespace SDUtilities +namespace ESPUtilities { + + String getMacAddressAsString(const uint8_t *mac) { + String macAddress; + for (int i = 0; i < 6; i++) { + macAddress += String(mac[i], HEX); + } + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "MAC: %s\n", macAddress.c_str()); + return macAddress; + } + + void checkPeerExistence(const uint8_t *mac) { + if (!esp_now_is_peer_exist(mac)) { + esp_now_peer_info_t client = {}; + memcpy(client.peer_addr, mac, sizeof(uint8_t) * 6); + client.encrypt = false; + client.channel = 0; + + esp_err_t status = esp_now_add_peer(&client); + if (status != ESP_OK) { + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Failed to add new Peer: %d", status); + } + } + } + + void sendResponse(const uint8_t *mac) { + response response = {}; + response.type = dataAck; + esp_read_mac(response.mac, ESP_MAC_WIFI_STA); + response.time = rtc.getEpoch(); + esp_err_t success = esp_now_send(mac, (uint8_t *)&response, sizeof(response)); + } +} // namespace ESPUtilities + // I don't think this does anything. Copied from the example void turnOffLEDs() { // Set LED OFF pinMode(LED_PIN, OUTPUT); @@ -248,15 +282,6 @@ void turnOffLEDs() { // Set LED OFF digitalWrite(PWR_PIN, LOW); } -String getMacAddressAsString(const uint8_t *mac) { - String macAddress; - for (int i = 0; i < 6; i++) { - macAddress += String(mac[i], HEX); - } - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "MAC: %s\n", macAddress.c_str()); - return macAddress; -} - String documentToLineProtocolString(const DynamicJsonDocument &doc) { String measurementType = doc["measurementType"].as<String>(); String sensorName = doc["sensorName"].as<String>(); diff --git a/host/host_central_mast/lib/Utilities/Utilities.h b/host/host_central_mast/lib/Utilities/Utilities.h index e3a7abb55f0293afe801d88bce2b76963f15e40c..a196ea8c18fbf854aa482b5b84f673bec7d260e9 100644 --- a/host/host_central_mast/lib/Utilities/Utilities.h +++ b/host/host_central_mast/lib/Utilities/Utilities.h @@ -6,6 +6,7 @@ #define HOST_CENTRAL_MAST_UTILITIES_H #include "ArduinoJson.h" +#include "MessageType.h" #include "SD.h" #include "SDCardException.h" #include "SDSetupException.h" @@ -14,6 +15,7 @@ #include <ClientDataPackage.hpp> #include <Definitions.h> #include <WString.h> +#include <esp_now.h> #include <list> namespace SDUtilities { @@ -30,6 +32,13 @@ namespace SDUtilities { bool isSDAvailable(); } // namespace SDUtilities +namespace ESPUtilities { + + String getMacAddressAsString(const uint8_t *mac); + void checkPeerExistence(const uint8_t *mac); + void sendResponse(const uint8_t *mac); +} // namespace ESPUtilities + void turnOffLEDs(); String getMacAddressAsString(const uint8_t *mac); String documentToLineProtocolString(const DynamicJsonDocument &doc); diff --git a/host/host_central_mast/src/main.cpp b/host/host_central_mast/src/main.cpp index 6faa74c110044c70de6efaae96378d65fe1b2f64..68b7710938fde3cf3f3d74f7c4d25001394a7fe8 100644 --- a/host/host_central_mast/src/main.cpp +++ b/host/host_central_mast/src/main.cpp @@ -8,6 +8,7 @@ #include "ClientDataPackage.hpp" #include "ConnectionManager.h" #include "MessageType.h" +#include "QueueStruct.h" #include "SDCardLogger.h" #include "SPI.h" #include "Utilities.h" @@ -51,31 +52,6 @@ SemaphoreHandle_t xMutex; TaskHandle_t ESPNOWTask; -struct QueueStruct { - std::array<ClientDataPackage, 6> data; - uint8_t mac[6]{}; -}; - -void sendResponse(const uint8_t *mac) { - response response = {}; - response.type = dataAck; - esp_read_mac(response.mac, ESP_MAC_WIFI_STA); - response.time = rtc.getEpoch(); - esp_err_t success = esp_now_send(mac, (uint8_t *)&response, sizeof(response)); -} -void checkPeerExistence(const uint8_t *mac) { - if (!esp_now_is_peer_exist(mac)) { - esp_now_peer_info_t client = {}; - memcpy(client.peer_addr, mac, sizeof(uint8_t) * 6); - client.encrypt = false; - client.channel = 0; - - esp_err_t status = esp_now_add_peer(&client); - if (status != ESP_OK) { - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Failed to add new Peer: %d", status); - } - } -} void handleDataReceive(const uint8_t *mac, const uint8_t *incomingData); static std::queue<QueueStruct> queue; @@ -90,10 +66,11 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) { * @param len length of the incoming data in bytes */ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { - checkPeerExistence(mac); + ESPUtilities::checkPeerExistence(mac); - sendResponse(mac); + ESPUtilities::sendResponse(mac); + // requires queue and I am too lazy to extern declare the queue handleDataReceive(mac, incomingData); } @@ -179,27 +156,28 @@ void setup() { // To skip it, call init() instead of restart() } -void loop() { - +bool isQueueEmpty() { bool emptyQueue; - - // check if queue is empty xSemaphoreTake(xMutex, portMAX_DELAY); emptyQueue = queue.empty(); xSemaphoreGive(xMutex); + return emptyQueue; +} + +void loop() { // exit after 10 retries int earlyExitCounter = 0; // do while loop that takes one element of the queue each until the queue is empty - while (!emptyQueue) { + while (!isQueueEmpty()) { xSemaphoreTake(xMutex, portMAX_DELAY); auto data = queue.front(); queue.pop(); xSemaphoreGive(xMutex); // get mac address as string - String macAddress = getMacAddressAsString(data.mac); + String macAddress = ESPUtilities::getMacAddressAsString(data.mac); for (const auto &dataPackage : data.data) { // ignore padding messages @@ -222,11 +200,6 @@ void loop() { break; } earlyExitCounter++; - - // check if queue is empty - xSemaphoreTake(xMutex, portMAX_DELAY); - emptyQueue = queue.empty(); - xSemaphoreGive(xMutex); } connectionManager.modemPowerOn();