From cc0ef9e61fc38241646a1062dde87582b241f061 Mon Sep 17 00:00:00 2001 From: dakriy Date: Wed, 15 Apr 2020 23:22:46 -0700 Subject: [PATCH] New file layout to be testable etc --- docker-compose.yml.example | 5 +++- src/embedded/CMakeLists.txt | 29 +++++++++++++++++-- src/embedded/Dockerfile | 21 ++++++++++++++ src/embedded/main/.gitignore | 2 +- src/embedded/main/CMakeLists.txt | 11 +++++-- src/embedded/main/{ => include/app}/api.h | 0 .../main/{ => include/app}/config.h.example | 0 .../main/{ => include/connectors}/http.h | 0 .../main/{ => include/connectors}/wifi.h | 2 ++ src/embedded/main/main.c | 11 +++++-- src/embedded/main/{ => src/app}/api.c | 6 ++-- .../connectors/esp32/esp_http.c} | 7 +++-- .../connectors/esp32/esp_wifi.c} | 20 ++++++------- src/embedded/main/test_main.c | 1 + src/embedded/version.txt | 1 + 15 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 src/embedded/Dockerfile rename src/embedded/main/{ => include/app}/api.h (100%) rename src/embedded/main/{ => include/app}/config.h.example (100%) rename src/embedded/main/{ => include/connectors}/http.h (100%) rename src/embedded/main/{ => include/connectors}/wifi.h (90%) rename src/embedded/main/{ => src/app}/api.c (89%) rename src/embedded/main/{http.c => src/connectors/esp32/esp_http.c} (97%) rename src/embedded/main/{wifi.c => src/connectors/esp32/esp_wifi.c} (91%) create mode 100644 src/embedded/main/test_main.c create mode 100644 src/embedded/version.txt diff --git a/docker-compose.yml.example b/docker-compose.yml.example index a1206169..36857006 100644 --- a/docker-compose.yml.example +++ b/docker-compose.yml.example @@ -95,10 +95,13 @@ services: networks: - doorcode esp32: - image: espressif/idf:latest + build: + context: src/embedded + dockerfile: Dockerfile tty: true container_name: esp32 volumes: - ./src/embedded:/project +# uncomment if you are going to be flashing a board from the container devices: - /dev/ttyUSB0:/dev/ttyUSB0 diff --git a/src/embedded/CMakeLists.txt b/src/embedded/CMakeLists.txt index 07873b30..3dde21a2 100644 --- a/src/embedded/CMakeLists.txt +++ b/src/embedded/CMakeLists.txt @@ -2,5 +2,30 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(door-controller) +if(COMPILE_UNIVERSAL_TESTS) + set(PROJECT_NAME door-controller-universal-tests) + set(CMAKE_C_STANDARD 11) + set(CMAKE_CXX_STANDARD 17) + project(${PROJECT_NAME} C CXX) + + find_package(Catch2 REQUIRED) + + + set(INCLUDE_DIR main/include) + set(APP_SRC main/src/app) + set(CONNECTOR_TEST main/src/connectors/tests) + + target_include_directories(${PROJECT_NAME} PRIVATE ${INCLUDE_DIR}) + + file(GLOB_RECURSE FILES + ${INCLUDE_DIR}/*.h + ${APP_SRC}/*.c + ${CONNECTOR_TEST_SRC}/*.c + ) + + add_executable(${PROJECT_NAME} ${FILES} "main/test_main.c") + target_link_libraries(${PROJECT_NAME} Catch2::Catch2) +else() + include($ENV{IDF_PATH}/tools/cmake/project.cmake) + project(door-controller) +endif() diff --git a/src/embedded/Dockerfile b/src/embedded/Dockerfile new file mode 100644 index 00000000..22fb9dc8 --- /dev/null +++ b/src/embedded/Dockerfile @@ -0,0 +1,21 @@ +FROM espressif/idf:latest + +RUN apt-get update && apt-get install -y \ + build-essential \ + gcc \ + g++ \ + gdb \ + cmake \ + && apt-get clean \ + && apt-get autoremove -y \ + && rm -rf /var/lib/apt/lists/* + +# Install catch2 +RUN git clone https://github.com/catchorg/Catch2.git /catch2 \ + && cd /catch2 && cmake -Bbuild -H. -DBUILD_TESTING=OFF \ + && cmake --build build/ --target install \ + && cd / \ + && rm -rf /catch2 + +ENTRYPOINT [ "/opt/esp/entrypoint.sh" ] +CMD [ "/bin/bash" ] diff --git a/src/embedded/main/.gitignore b/src/embedded/main/.gitignore index 347d1cb5..5bfbe746 100644 --- a/src/embedded/main/.gitignore +++ b/src/embedded/main/.gitignore @@ -1,2 +1,2 @@ config.h -doorcode_root_cert.pem \ No newline at end of file +*.pem \ No newline at end of file diff --git a/src/embedded/main/CMakeLists.txt b/src/embedded/main/CMakeLists.txt index c2f3ebf0..470e98a8 100644 --- a/src/embedded/main/CMakeLists.txt +++ b/src/embedded/main/CMakeLists.txt @@ -1,3 +1,10 @@ -idf_component_register(SRCS "wifi.c" "main.c" "http.c" "api.c" - INCLUDE_DIRS "." +set(INCLUDE_DIR include) + +set(APP_SRC src/app) +set(CONNECTOR_SRC src/connectors/esp32) + +file(GLOB_RECURSE FILES ${APP_SRC}/*.c ${CONNECTOR_SRC}/*.c) + +idf_component_register(SRCS ${FILES} "main.c" + INCLUDE_DIRS ${INCLUDE_DIR} EMBED_TXTFILES "doorcode_root_cert.pem") diff --git a/src/embedded/main/api.h b/src/embedded/main/include/app/api.h similarity index 100% rename from src/embedded/main/api.h rename to src/embedded/main/include/app/api.h diff --git a/src/embedded/main/config.h.example b/src/embedded/main/include/app/config.h.example similarity index 100% rename from src/embedded/main/config.h.example rename to src/embedded/main/include/app/config.h.example diff --git a/src/embedded/main/http.h b/src/embedded/main/include/connectors/http.h similarity index 100% rename from src/embedded/main/http.h rename to src/embedded/main/include/connectors/http.h diff --git a/src/embedded/main/wifi.h b/src/embedded/main/include/connectors/wifi.h similarity index 90% rename from src/embedded/main/wifi.h rename to src/embedded/main/include/connectors/wifi.h index a931fc5c..f973550e 100644 --- a/src/embedded/main/wifi.h +++ b/src/embedded/main/include/connectors/wifi.h @@ -1,6 +1,8 @@ #ifndef DOOR__WIFI_H_ #define DOOR__WIFI_H_ +#include + void door_wifi_initialize(void); void door_wifi_destroy(void); diff --git a/src/embedded/main/main.c b/src/embedded/main/main.c index a5969d17..929cb45e 100644 --- a/src/embedded/main/main.c +++ b/src/embedded/main/main.c @@ -11,9 +11,9 @@ #include #include #include -#include "wifi.h" -#include "http.h" -#include "api.h" +#include "connectors/wifi.h" +#include "connectors/http.h" +#include "app/api.h" static const char* TAG = "MAIN_MODULE"; @@ -25,6 +25,8 @@ static void check_code_task(void* pvParameters) ESP_LOGI(TAG, "Connected to AP, begin connecting to DoorCode API"); bool allowed = door_api_verify_code("123866"); + + if (allowed) { ESP_LOGI(TAG, "We are allowed in the door!"); @@ -33,6 +35,9 @@ static void check_code_task(void* pvParameters) { ESP_LOGI(TAG, "REJECTED"); } + ESP_LOGI(TAG, "Making second request"); + door_api_verify_code("123867"); + ESP_LOGI(TAG, "Done with second request"); door_http_destroy(); door_wifi_destroy(); diff --git a/src/embedded/main/api.c b/src/embedded/main/src/app/api.c similarity index 89% rename from src/embedded/main/api.c rename to src/embedded/main/src/app/api.c index dd1d7015..99f8bac3 100644 --- a/src/embedded/main/api.c +++ b/src/embedded/main/src/app/api.c @@ -1,7 +1,7 @@ #include -#include "api.h" -#include "http.h" -#include "config.h" +#include "app/api.h" +#include "connectors/http.h" +#include "app/config.h" #define ACCESS_PREFIX "/access/" #define MAX_CODE_COUNT 500 diff --git a/src/embedded/main/http.c b/src/embedded/main/src/connectors/esp32/esp_http.c similarity index 97% rename from src/embedded/main/http.c rename to src/embedded/main/src/connectors/esp32/esp_http.c index 129b4f0d..e6b170b6 100644 --- a/src/embedded/main/http.c +++ b/src/embedded/main/src/connectors/esp32/esp_http.c @@ -1,9 +1,10 @@ #include #include #include -#include "http.h" -#include "config.h" -#include "wifi.h" +#include +#include "app/config.h" +#include "connectors/http.h" +#include "connectors/wifi.h" /** * @brief Input buffer diff --git a/src/embedded/main/wifi.c b/src/embedded/main/src/connectors/esp32/esp_wifi.c similarity index 91% rename from src/embedded/main/wifi.c rename to src/embedded/main/src/connectors/esp32/esp_wifi.c index b6ddcfeb..e6f62198 100644 --- a/src/embedded/main/wifi.c +++ b/src/embedded/main/src/connectors/esp32/esp_wifi.c @@ -1,15 +1,15 @@ #include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event.h" -#include "esp_log.h" -#include "nvs_flash.h" +#include +#include +#include +#include +#include +#include +#include +#include -#include "config.h" -#include "wifi.h" +#include "app/config.h" +#include "connectors/wifi.h" /* FreeRTOS event group to signal when we are connected*/ static EventGroupHandle_t s_wifi_event_group; diff --git a/src/embedded/main/test_main.c b/src/embedded/main/test_main.c new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/src/embedded/main/test_main.c @@ -0,0 +1 @@ + diff --git a/src/embedded/version.txt b/src/embedded/version.txt new file mode 100644 index 00000000..b87eee81 --- /dev/null +++ b/src/embedded/version.txt @@ -0,0 +1 @@ +0.0.0.1 \ No newline at end of file -- GitLab