diff --git a/host/mock/.gitignore b/host/mock/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..3fe18ad4795165c312b8998befee223818bd4b33 --- /dev/null +++ b/host/mock/.gitignore @@ -0,0 +1,3 @@ +.pio +CMakeListsPrivate.txt +cmake-build-*/ diff --git a/host/mock/include/README b/host/mock/include/README new file mode 100644 index 0000000000000000000000000000000000000000..194dcd43252dcbeb2044ee38510415041a0e7b47 --- /dev/null +++ b/host/mock/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/host/mock/platformio.ini b/host/mock/platformio.ini new file mode 100644 index 0000000000000000000000000000000000000000..0ff4a4f14ee0965865eb3ca282fa9234b346d248 --- /dev/null +++ b/host/mock/platformio.ini @@ -0,0 +1,17 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32-c3-devkitm-1] +platform = espressif32 +board = esp32-c3-devkitm-1 +framework = arduino +lib_deps = + bblanchon/ArduinoJson@^6.19.4 + fbiego/ESP32Time@^2.0.0 diff --git a/host/mock/src/main.cpp b/host/mock/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..268d8f7f1e4204500ca060584f659551b63a3e33 --- /dev/null +++ b/host/mock/src/main.cpp @@ -0,0 +1,113 @@ +#include "ESP32Time.h" +#include <Arduino.h> +#include <ArduinoJson.h> +#include <WiFi.h> +#include <esp_log.h> +#include <esp_now.h> + +static const std::string TAG = "MAIN"; +static const std::string TAG_ESPNOW = "ESPNOW"; +static const std::string TAG_GSM = "GSM"; + +ESP32Time rtc; +TaskHandle_t ESPNOWTask; + +void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) +{ + // go to sleep +} + +String getMacAddressAsString(const uint8_t *mac); +DynamicJsonDocument parseReceivedJsonData(char *data); + +void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) +{ + esp_log_write(ESP_LOG_INFO, TAG_ESPNOW.c_str(), "Message recieved\n"); + // copy received data to a char array + char data[len]; + memcpy(data, incomingData, len); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Raw received Data: %s\n", data); + + DynamicJsonDocument doc = parseReceivedJsonData(data); + + String macAddress = getMacAddressAsString(mac); + + // add timestamp and mac address + doc["timestamp"] = rtc.getEpoch(); + doc["clientMac"] = macAddress; + + // serialize json document again + std::string dataString{}; + serializeJson(doc, dataString); +} + +DynamicJsonDocument parseReceivedJsonData(char *data) +{ + DynamicJsonDocument doc(250); + auto error = deserializeJson(doc, data); + if (error) { + esp_log_write(ESP_LOG_ERROR, TAG_ESPNOW.c_str(), "Error while parsing json: %s\n", error.f_str()); + // TODO error handling + } + return doc; +} +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.c_str(), "MAC: %s\n", macAddress.c_str()); + return macAddress; +} + +[[noreturn]] void ESPNOWReceiveTask(void *parameter) +{ + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "ESPNOWReceiveTask started on core %d\n", xPortGetCoreID()); + + WiFi.mode(WIFI_STA); + + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Initialising ESPNow...\n"); + + if (esp_now_init() != ESP_OK) { + // initialization failed + esp_log_write(ESP_LOG_ERROR, TAG_ESPNOW.c_str(), "Initialising ESPNow FAILED\n"); + exit(ESP_FAIL); + } + + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Initialising ESPNow SUCCESS\n"); + + esp_now_register_recv_cb(on_data_recv); + + while (true) { + } +} + +void setup() +{ + // Set console baud rate + Serial.begin(115200); + delay(10); + + // https://stackoverflow.com/questions/60442350/arduinos-esp-log-set-vprintf-does-not-work-on-esp32 + esp_log_level_set("*", ESP_LOG_VERBOSE); + esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "%s", WiFi.macAddress().c_str()); + + delay(1000); + + // create ESPNOWReceiveTask. TODO: Until the UTC time is not synced, this will not add the correct time. If we + // TODO: create the task after the time is synced, no messages will be received until synchronization is done + xTaskCreatePinnedToCore(ESPNOWReceiveTask, /* Function to implement the task */ + "ESPNOWReceiveTask", /* Name of the task */ + 10000, /* Stack size in words */ + nullptr, /* Task input parameter */ + 0, /* Priority of the task */ + &ESPNOWTask, /* Task handle. */ + 0); /* Core where the task should run */ + + // Restart takes quite some time + // To skip it, call init() instead of restart() + esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Initializing modem...\n"); +} + +void loop() {} diff --git a/host/mock/test/README b/host/mock/test/README new file mode 100644 index 0000000000000000000000000000000000000000..9b1e87bc67c90e7f09a92a3e855444b085c655a6 --- /dev/null +++ b/host/mock/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html