diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d8edc3f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,56 @@ +# Copyright (c) 2025 Dmitry Ponomarev +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +cmake_minimum_required(VERSION 3.22) +project(libparams VERSION 1.0 LANGUAGES CXX C) + +if(LIBPARAMS_PLATFORM STREQUAL "stm32f103") +elseif(LIBPARAMS_PLATFORM STREQUAL "stm32g0b1") +elseif(LIBPARAMS_PLATFORM STREQUAL "ubuntu") +else() + message(SEND_ERROR "LIBPARAMS_PLATFORM is not specified! Options: stm32f103, stm32g0b1, ubuntu.") +endif() + +execute_process( + COMMAND ${ROOT_DIR}/scripts/generate_default_params.py + --out-dir ${CMAKE_CURRENT_BINARY_DIR}/params + -f ${TESTS_DIR}/params/params.c + --out-file-name "init_params" + RESULT_VARIABLE ret +) +if(NOT ret EQUAL 0) + message( FATAL_ERROR "Default Params Generator has been failed. Abort.") +endif() +set(LIBPARAMS_PARAMS_DIR="${CMAKE_CURRENT_BINARY_DIR}/params") +add_definitions(-DLIBPARAMS_PARAMS_DIR="${CMAKE_CURRENT_BINARY_DIR}/params") + +FILE(GLOB libparamsPlatformSpecificSrc + ${CMAKE_CURRENT_LIST_DIR}/platform_specific/${LIBPARAMS_PLATFORM}/*.c* +) + +FILE(GLOB libparamsGeneratedParams + ${LIBPARAMS_GENERATED_PARAMS_DIR}/*.c* + ${LIBPARAMS_GENERATED_PARAMS_DIR}/*.cpp* +) + +add_library(${PROJECT_NAME} STATIC + src/rom.c + src/storage.c + ${libparamsPlatformSpecificSrc} + ${libparamsGeneratedParams} +) + +add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) + +target_include_directories(${PROJECT_NAME} + PUBLIC + $ + $ + $ + $ + PRIVATE + $ +) \ No newline at end of file diff --git a/README.md b/README.md index ba780c8..881904c 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ You can create params.c and params.h files with the following content: ```c++ // params.h #pragma once -#include "storage.h" +#include "libparams/storage.h" enum IntParamsIndexes : ParamIndex_t { PARAM_NODE_ID, diff --git a/include/libparams/storage.h b/include/libparams/storage.h index 5152bcd..71a9f17 100644 --- a/include/libparams/storage.h +++ b/include/libparams/storage.h @@ -12,9 +12,9 @@ #include #include #include -#include "rom.h" -#include "libparams_error_codes.h" -#include "_legacy_functions.h" +#include "libparams/rom.h" +#include "libparams/libparams_error_codes.h" +#include "libparams/_legacy_functions.h" #ifdef __cplusplus extern "C" { diff --git a/libparams.cmake b/libparams.cmake index 0a1bc7d..778de5d 100644 --- a/libparams.cmake +++ b/libparams.cmake @@ -28,7 +28,7 @@ set(libparamsSrc ) set(libparamsHeaders - ${CMAKE_CURRENT_LIST_DIR}/include/libparams/ + ${CMAKE_CURRENT_LIST_DIR}/include/ ${CMAKE_CURRENT_LIST_DIR}/platform_specific/${LIBPARAMS_PLATFORM}/ ${libparamsPlatformSpecificHeaders} ) diff --git a/platform_specific/stm32f103/flash_driver.c b/platform_specific/stm32f103/flash_driver.c index 5ca9522..9b9e2a1 100644 --- a/platform_specific/stm32f103/flash_driver.c +++ b/platform_specific/stm32f103/flash_driver.c @@ -6,9 +6,9 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "flash_driver.h" +#include "libparams/flash_driver.h" #include -#include "libparams_error_codes.h" +#include "libparams/libparams_error_codes.h" #include "flash_registers.h" diff --git a/platform_specific/stm32g0b1/flash_driver.c b/platform_specific/stm32g0b1/flash_driver.c index 43a77cc..ccb8c38 100644 --- a/platform_specific/stm32g0b1/flash_driver.c +++ b/platform_specific/stm32g0b1/flash_driver.c @@ -6,10 +6,10 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "flash_driver.h" +#include "libparams/flash_driver.h" #include #include "main.h" -#include "libparams_error_codes.h" +#include "libparams/libparams_error_codes.h" static uint8_t* flashGetPointer(); diff --git a/platform_specific/ubuntu/FlashMemoryLayout.hpp b/platform_specific/ubuntu/FlashMemoryLayout.hpp index a6a8327..0f38998 100644 --- a/platform_specific/ubuntu/FlashMemoryLayout.hpp +++ b/platform_specific/ubuntu/FlashMemoryLayout.hpp @@ -9,7 +9,7 @@ #ifndef LIBPARAM_FLASH_MEMORY_LAYOUT_HPP_ #define LIBPARAM_FLASH_MEMORY_LAYOUT_HPP_ -#include "storage.h" +#include "libparams/storage.h" typedef struct { IntegerDesc_t* integer_desc_pool; diff --git a/platform_specific/ubuntu/YamlParameters.cpp b/platform_specific/ubuntu/YamlParameters.cpp index 86b2b2a..929a65f 100644 --- a/platform_specific/ubuntu/YamlParameters.cpp +++ b/platform_specific/ubuntu/YamlParameters.cpp @@ -12,7 +12,7 @@ #include #include -#include "storage.h" +#include "libparams/storage.h" #include "YamlParameters.hpp" YamlParameters::YamlParameters(FlashMemoryLayout_t flash_desc, diff --git a/platform_specific/ubuntu/flash_driver.cpp b/platform_specific/ubuntu/flash_driver.cpp index 169b503..8fbda3a 100644 --- a/platform_specific/ubuntu/flash_driver.cpp +++ b/platform_specific/ubuntu/flash_driver.cpp @@ -7,9 +7,9 @@ */ #include -#include "storage.h" -#include "flash_driver.h" -#include "libparams_error_codes.h" +#include "libparams/storage.h" +#include "libparams/flash_driver.h" +#include "libparams/libparams_error_codes.h" #include "params.hpp" #include "YamlParameters.hpp" @@ -115,3 +115,7 @@ int32_t __save_to_files() { int8_t __read_from_files() { return yaml_params.read_from_dir(LIBPARAMS_PARAMS_DIR); } + +const char* get_libparams_params_dir() { + return LIBPARAMS_PARAMS_DIR; +} diff --git a/scripts/_constants.py b/scripts/_constants.py index 18fea2a..abc1efa 100755 --- a/scripts/_constants.py +++ b/scripts/_constants.py @@ -8,7 +8,7 @@ class CppHeader: INTEGER_HEAD = """#pragma once -#include "storage.h" +#include "libparams/storage.h" #include "string_params.hpp" enum IntParamsIndexes : ParamIndex_t { """ @@ -39,7 +39,7 @@ class CHeader: class CSource: INTEGER_HEAD="""#include "params.h" -#include "storage.h" +#include "libparams/storage.h" IntegerDesc_t integer_desc_pool[] = { """ @@ -49,7 +49,7 @@ class CSource: """ STRING_HEAD="""#include "string_params.h" -#include "storage.h" +#include "libparams/storage.h" StringDesc_t __attribute__((weak)) string_desc_pool[NUM_OF_STR_PARAMS] = { """ STRING_TAIL=""" @@ -68,7 +68,7 @@ class CppSource: """ STRING_HEAD="""#include "string_params.hpp" -#include "storage.h" +#include "libparams/storage.h" StringDesc_t __attribute__((weak)) string_desc_pool[NUM_OF_STR_PARAMS] = { """ STRING_TAIL=""" diff --git a/scripts/generate_params.py b/scripts/generate_params.py index a9c5eba..7172fab 100755 --- a/scripts/generate_params.py +++ b/scripts/generate_params.py @@ -77,7 +77,7 @@ def generate(self): hpp_content = ( f"{LICENSE_HEADER}\n" "#pragma once\n" - "#include \"storage.h\"\n\n" + "#include \"libparams/storage.h\"\n\n" "enum IntParamsIndexes {\n" f"{self.integers_enums}\n" " INTEGER_PARAMS_AMOUNT\n" diff --git a/scripts/generate_random_params.py b/scripts/generate_random_params.py index 30f8b46..4148915 100755 --- a/scripts/generate_random_params.py +++ b/scripts/generate_random_params.py @@ -84,7 +84,7 @@ def generate(self): hpp_content = ( f"{LICENSE_HEADER}\n" "#pragma once\n" - "#include \"storage.h\"\n\n" + "#include \"libparams/storage.h\"\n\n" "enum IntParamsIndexes {\n" f"{self.integers_enums}\n" " INTEGER_PARAMS_AMOUNT\n" diff --git a/src/rom.c b/src/rom.c index 81ac376..e7d4a47 100644 --- a/src/rom.c +++ b/src/rom.c @@ -6,11 +6,11 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "rom.h" +#include "libparams/rom.h" #include #include -#include "flash_driver.h" -#include "libparams_error_codes.h" +#include "libparams/flash_driver.h" +#include "libparams/libparams_error_codes.h" int32_t libparams_rom_error_code = 0; diff --git a/src/storage.c b/src/storage.c index 3460ff7..b49201b 100644 --- a/src/storage.c +++ b/src/storage.c @@ -6,11 +6,11 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "storage.h" +#include "libparams/storage.h" #include -#include "flash_driver.h" -#include "rom.h" -#include "libparams_error_codes.h" +#include "libparams/flash_driver.h" +#include "libparams/rom.h" +#include "libparams/libparams_error_codes.h" extern IntegerDesc_t integer_desc_pool[]; extern IntegerParamValue_t integer_values_pool[]; diff --git a/tests/params/params.c b/tests/params/params.c index 8f67359..f1747c0 100644 --- a/tests/params/params.c +++ b/tests/params/params.c @@ -7,8 +7,8 @@ */ #include -#include "storage.h" -#include "flash_driver.h" +#include "libparams/storage.h" +#include "libparams/flash_driver.h" IntegerDesc_t integer_desc_pool[512] = { {"uavcan.node.id", 0, 127, 50, MUTABLE}, diff --git a/tests/params/params.hpp b/tests/params/params.hpp index 09269c1..4912d26 100644 --- a/tests/params/params.hpp +++ b/tests/params/params.hpp @@ -6,7 +6,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "storage.h" +#include "libparams/storage.h" #ifndef LIBPARAM_PARAMETERS_HPP_ #define LIBPARAM_PARAMETERS_HPP_ diff --git a/tests/params_generator/CMakeLists.txt b/tests/params_generator/CMakeLists.txt index ae5a829..b328e2f 100644 --- a/tests/params_generator/CMakeLists.txt +++ b/tests/params_generator/CMakeLists.txt @@ -16,7 +16,7 @@ set(PARAMS_PATH ${LIBPARAMS_ROOT_DIR}/build/tests/params_generator) add_definitions(-DLIBPARAMS_PARAMS_DIR="${CMAKE_CURRENT_BINARY_DIR}") include_directories( - ${LIBPARAMS_ROOT_DIR}/include/libparams + ${LIBPARAMS_ROOT_DIR}/include ${PLATFORM_SPECIFIC_PATH} ${PARAMS_PATH} ) diff --git a/tests/params_generator/test.cpp b/tests/params_generator/test.cpp index 3d44677..1d38131 100644 --- a/tests/params_generator/test.cpp +++ b/tests/params_generator/test.cpp @@ -9,7 +9,7 @@ #include #include #include -#include "storage.h" +#include "libparams/storage.h" #include "params.hpp" bool strCompareSafe(const char* first, const char* second, size_t len) { diff --git a/tests/platform_specific/stm32f103/Makefile b/tests/platform_specific/stm32f103/Makefile index 765d2c4..b88a657 100644 --- a/tests/platform_specific/stm32f103/Makefile +++ b/tests/platform_specific/stm32f103/Makefile @@ -17,7 +17,7 @@ application.c # C includes C_INCLUDES = \ --I${ROOT_DIR}/include/libparams/ \ +-I${ROOT_DIR}/include/ \ -I${ROOT_DIR}/platform_specific/stm32f103/ \ -I./ \ -I../../ diff --git a/tests/platform_specific/stm32f103/application.c b/tests/platform_specific/stm32f103/application.c index 9123631..576545b 100644 --- a/tests/platform_specific/stm32f103/application.c +++ b/tests/platform_specific/stm32f103/application.c @@ -6,8 +6,8 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "rom.h" -#include "flash_driver.h" +#include "libparams/rom.h" +#include "libparams/flash_driver.h" void test_flash_wr() { RomDriverInstance rom = romInit(127, 1); diff --git a/tests/platform_specific/stm32g0b1/Makefile b/tests/platform_specific/stm32g0b1/Makefile index e6c1172..73cae66 100644 --- a/tests/platform_specific/stm32g0b1/Makefile +++ b/tests/platform_specific/stm32g0b1/Makefile @@ -17,7 +17,7 @@ application.c # C includes C_INCLUDES = \ --I${ROOT_DIR}/include/libparams/ \ +-I${ROOT_DIR}/include/ \ -I${ROOT_DIR}/platform_specific/stm32g0b1/ \ -I./ \ -I../../ diff --git a/tests/platform_specific/stm32g0b1/application.c b/tests/platform_specific/stm32g0b1/application.c index b86da39..dfd6512 100644 --- a/tests/platform_specific/stm32g0b1/application.c +++ b/tests/platform_specific/stm32g0b1/application.c @@ -6,8 +6,8 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "rom.h" -#include "flash_driver.h" +#include "libparams/rom.h" +#include "libparams/flash_driver.h" void test_flash_wr() { RomDriverInstance rom = romInit(255, 1); diff --git a/tests/platform_specific/ubuntu/main.cpp b/tests/platform_specific/ubuntu/main.cpp index 46e3933..c2166f5 100644 --- a/tests/platform_specific/ubuntu/main.cpp +++ b/tests/platform_specific/ubuntu/main.cpp @@ -7,8 +7,8 @@ */ #include -#include "flash_driver.h" -#include "libparams_error_codes.h" +#include "libparams/flash_driver.h" +#include "libparams/libparams_error_codes.h" #include "params.hpp" extern StringParamValue_t string_values_pool[]; diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 683c050..9b14a68 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -25,34 +25,17 @@ include_directories(${GTEST_INCLUDE_DIR}) # include_directories(.) set(LIBPARAMS_PLATFORM ubuntu) -include(${ROOT_DIR}/libparams.cmake) - -set(LIBPARAMS_PARAMS_DIR="${CMAKE_CURRENT_BINARY_DIR}/params") -add_definitions(-DLIBPARAMS_PARAMS_DIR="${CMAKE_CURRENT_BINARY_DIR}/params") +set(LIBPARAMS_GENERATED_PARAMS_DIR ${TESTS_DIR}/params/) +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. ${CMAKE_BINARY_DIR}/libparams) function(gen_test app_name test_file) - execute_process( - COMMAND ${ROOT_DIR}/scripts/generate_default_params.py --out-dir ${CMAKE_CURRENT_BINARY_DIR}/params -f ${TESTS_DIR}/params/params.c - --out-file-name "init_params" - RESULT_VARIABLE ret - ) - if(NOT ret EQUAL 0) - message( FATAL_ERROR "Default Params Generator has been failed. Abort.") - endif() - # Create the executable target add_executable(${app_name} - ${test_file} - ${libparamsSrc}) - - target_include_directories(${app_name} PUBLIC ${libparamsHeaders}) + ${test_file} + ${libparamsSrc} + ) - # Conditional source file based on app_name message(STATUS "${app_name} params dir ${UNIT_TESTS_DIR}") - target_sources(${app_name} PRIVATE ${TESTS_DIR}/params/params.c) - target_include_directories(${app_name} PRIVATE ${TESTS_DIR}/params/) - - # Link libraries to the target - target_link_libraries(${app_name} gtest) + target_link_libraries(${app_name} gtest libparams::libparams) endfunction() gen_test(flash_driver ${UNIT_TESTS_DIR}/test_flash_driver.cpp) diff --git a/tests/unit_tests/Makefile b/tests/unit_tests/Makefile index 5cf29a1..3e8289d 100644 --- a/tests/unit_tests/Makefile +++ b/tests/unit_tests/Makefile @@ -23,16 +23,16 @@ coverage: create_build_dir cd ${BUILD_DIR} && ./yaml_parameters echo "\n1. Storage:" - cd ${BUILD_DIR} && gcov ${BUILD_DIR}/CMakeFiles/storage.dir${REPO_DIR}/src/storage.c.gcda + cd ${BUILD_DIR} && gcov ${BUILD_DIR}/libparams/CMakeFiles/libparams.dir/src/storage.c.gcda echo "\n2. ROM:" - cd ${BUILD_DIR} && gcov ${BUILD_DIR}/CMakeFiles/rom.dir${REPO_DIR}/src/rom.c.gcda + cd ${BUILD_DIR} && gcov ${BUILD_DIR}/libparams/CMakeFiles/libparams.dir/src/rom.c.gcda echo "\n3. Ubuntu flash driver:" - cd ${BUILD_DIR} && gcov ${BUILD_DIR}/CMakeFiles/flash_driver.dir${REPO_DIR}/platform_specific/ubuntu/flash_driver.cpp.gcda + cd ${BUILD_DIR} && gcov ${BUILD_DIR}/libparams/CMakeFiles/libparams.dir/platform_specific/ubuntu/flash_driver.cpp.gcda echo "\n4. Yaml Parameters:" - cd ${BUILD_DIR} && gcov ${BUILD_DIR}/CMakeFiles/yaml_parameters.dir${REPO_DIR}/platform_specific/ubuntu/YamlParameters.cpp.gcda + cd ${BUILD_DIR} && gcov ${BUILD_DIR}/libparams/CMakeFiles/libparams.dir/platform_specific/ubuntu/YamlParameters.cpp.gcda flash_driver: create_build_dir cd ${BUILD_DIR} && cmake ${CRNT_DIR} && make && ./flash_driver diff --git a/tests/unit_tests/test_flash_driver.cpp b/tests/unit_tests/test_flash_driver.cpp index 83c70ae..2297686 100644 --- a/tests/unit_tests/test_flash_driver.cpp +++ b/tests/unit_tests/test_flash_driver.cpp @@ -8,8 +8,8 @@ #include #include -#include "flash_driver.h" -#include "libparams_error_codes.h" +#include "libparams/flash_driver.h" +#include "libparams/libparams_error_codes.h" #include "common/algorithms.hpp" // Test Case 1. Initialize flash driver diff --git a/tests/unit_tests/test_rom.cpp b/tests/unit_tests/test_rom.cpp index 1b7fbe5..fb3af7c 100644 --- a/tests/unit_tests/test_rom.cpp +++ b/tests/unit_tests/test_rom.cpp @@ -8,9 +8,9 @@ #include #include -#include "rom.h" -#include "libparams_error_codes.h" -#include "flash_driver.h" +#include "libparams/rom.h" +#include "libparams/libparams_error_codes.h" +#include "libparams/flash_driver.h" class RomDriverMultiplePagesTest : public ::testing::Test { protected: diff --git a/tests/unit_tests/test_storage.cpp b/tests/unit_tests/test_storage.cpp index f10108e..adad709 100644 --- a/tests/unit_tests/test_storage.cpp +++ b/tests/unit_tests/test_storage.cpp @@ -8,8 +8,8 @@ #include #include -#include "storage.h" -#include "libparams_error_codes.h" +#include "libparams/storage.h" +#include "libparams/libparams_error_codes.h" #include "common/algorithms.hpp" typedef enum { diff --git a/tests/unit_tests/test_yaml_parameters.cpp b/tests/unit_tests/test_yaml_parameters.cpp index 48e17b2..64a06f2 100644 --- a/tests/unit_tests/test_yaml_parameters.cpp +++ b/tests/unit_tests/test_yaml_parameters.cpp @@ -15,11 +15,13 @@ #include #include -#include "YamlParameters.hpp" #include "common/algorithms.hpp" -#include "flash_driver.h" +#include "../platform_specific/ubuntu/YamlParameters.hpp" +#include "libparams/flash_driver.h" -std::string dir(LIBPARAMS_PARAMS_DIR); +const char* get_libparams_params_dir(); + +std::string dir(get_libparams_params_dir()); uint8_t flash_memory[100]; int8_t delete_file_from_dir(const char* path) {