From b4d9f24f0e971e78de14c209bcecfa4718b9163c Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 22 Jul 2025 14:22:49 +0200 Subject: [PATCH] Added Git Hash To Build --- main/CMakeLists.txt | 23 +++++++++++- main/main.c | 3 ++ tests/Makefile | 14 ++++++++ tests/main_tests.c | 9 +++++ tests/test_parser.c | 85 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 tests/Makefile create mode 100644 tests/main_tests.c create mode 100644 tests/test_parser.c diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 9d84066..8726a49 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,3 +1,24 @@ -idf_component_register(SRCS "main.c" "uart_handler.c" "communication_handler.c" "uart_prot.c" +idf_component_register(SRCS "main.c" "uart_handler.c" "communication_handler.c" "uart_prot.c" "client_handler.c" "message_parser.c" "message_builder.c" "message_handler.c" INCLUDE_DIRS ".") +# Get the short Git commit hash of the current HEAD. +# If not in a Git repository or git command fails, it will default to "N/A". +execute_process( + COMMAND git rev-parse --short HEAD + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE GIT_COMMIT_HASH_SHORT + OUTPUT_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE GIT_HASH_RESULT +) +# Fallback if git is not available or not in a git repo +if(GIT_HASH_RESULT_CODE) + set(GIT_COMMIT_HASH_SHORT "N/A") +endif() + +# Add the Git hash as a preprocessor definition to your component. +# This makes BUILD_GIT_HASH available in your C/C++ source files. +target_compile_definitions(${COMPONENT_LIB} PRIVATE + BUILD_GIT_HASH="${GIT_COMMIT_HASH_SHORT}" +) + + diff --git a/main/main.c b/main/main.c index 7b1682f..c191a44 100644 --- a/main/main.c +++ b/main/main.c @@ -29,6 +29,9 @@ void SendClientInfoTask() { } void app_main(void) { +ESP_LOGI(TAG, "Starting Alox Powerpod Version %d Build: %s", version, + BUILD_GIT_HASH); + esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..859c6cf --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,14 @@ +CC=gcc +CFLAGS=-Wall -Wextra -I../main +SRC = Unity/src/unity.c test_parser.c \ + ../main/message_parser.c +TARGET = test_runner + +all: $(TARGET) + +$(TARGET): $(SRC) + $(CC) $(CFLAGS) -o $@ $^ + ./$(TARGET) + +clean: + rm -f $(TARGET) diff --git a/tests/main_tests.c b/tests/main_tests.c new file mode 100644 index 0000000..327fb03 --- /dev/null +++ b/tests/main_tests.c @@ -0,0 +1,9 @@ +#include "Unity/src/unity.h" + +void setUp(void) {} // optional +void tearDown(void) {} // optional + +int main(void) { + UNITY_BEGIN(); + return UNITY_END(); +} diff --git a/tests/test_parser.c b/tests/test_parser.c new file mode 100644 index 0000000..bf6056b --- /dev/null +++ b/tests/test_parser.c @@ -0,0 +1,85 @@ +#include "Unity/src/unity.h" +#include "message_parser.h" +#include + +void setUp(void) {} // optional +void tearDown(void) {} // optional + +// Gültige Nachricht mit Payload +void test_valid_message_parses_correctly(void) { + struct MessageRecieve mr = { + .state = WaitingForStartByte, + .messageid = 0, + .index = 0, + .checksum = 0, + }; + + uint8_t msg[] = {0xAA, 0x01, 0x03, 0x01, 0x02, 0x03, 0x00}; + msg[6] = msg[1] ^ msg[2] ^ msg[3] ^ msg[4] ^ msg[5]; // korrekte Checksumme + + for (uint8_t i = 0; i < sizeof(msg); ++i) + parse_byte(&mr, msg[i]); + + TEST_ASSERT_EQUAL_UINT8(1, mr.messageid); + TEST_ASSERT_EQUAL_UINT8(3, mr.length); + uint8_t expected[] = {1, 2, 3}; + TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, mr.message, 3); +} + +// Ungültige Checksumme – Zustand wird zurückgesetzt +void test_invalid_checksum_resets_state(void) { + struct MessageRecieve mr = { + .state = WaitingForStartByte, + .messageid = 0, + .index = 0, + .checksum = 0, + }; + + uint8_t msg[] = { + 0xAA, 0x02, 0x02, + 0x10, 0x20, 0x00}; // falsche Checksumme (korr. wäre 0x10 ^ 0x20 ^ 2 ^ 2) + + for (uint8_t i = 0; i < sizeof(msg); ++i) + parse_byte(&mr, msg[i]); + + TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state); +} + +// Kein Startbyte – Nachricht ignorieren +void test_no_startbyte_ignored(void) { + struct MessageRecieve mr = { + .state = WaitingForStartByte, .index = 0, .checksum = 0}; + + uint8_t msg[] = {2, 0x10, 0x20, 0x30}; // kein Startbyte + + for (uint8_t i = 0; i < sizeof(msg); ++i) + parse_byte(&mr, msg[i]); + + TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state); + TEST_ASSERT_EQUAL_UINT8(0, mr.index); +} + +// Länge 0 – gültige Nachricht ohne Payload +void test_zero_length_message(void) { + struct MessageRecieve mr = { + .state = WaitingForStartByte, .index = 0, .checksum = 0}; + + uint8_t msg[] = {0xAA, 3, 0, 0}; // Länge = 0, Checksumme = 3 ^ 0 = 3 + msg[3] = msg[1] ^ msg[2]; + + for (uint8_t i = 0; i < sizeof(msg); ++i) + parse_byte(&mr, msg[i]); + + TEST_ASSERT_EQUAL_UINT8(0, mr.index); + TEST_ASSERT_EQUAL_UINT8(3, mr.messageid); + TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state); +} + +int main(void) { + UNITY_BEGIN(); + RUN_TEST(test_valid_message_parses_correctly); + RUN_TEST(test_invalid_checksum_resets_state); + RUN_TEST(test_no_startbyte_ignored); + RUN_TEST(test_zero_length_message); + return UNITY_END(); +}