From 00860ceddca375b6fcc8f11ac3d2be59d372e5b2 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Filho Date: Thu, 1 Oct 2026 08:20:11 +0200 Subject: [PATCH] refactor: Split message library into subcomponent Signed-off-by: Euripedes Rocha Filho --- .ci/check_copyright_config.yaml | 4 ++-- .idf_ci.toml | 4 ++-- CMakeLists.txt | 9 ++++++--- lib/mqtt_msg/CMakeLists.txt | 19 +++++++++++++++++++ lib/{ => mqtt_msg}/include/mqtt5_msg.h | 0 lib/{ => mqtt_msg}/include/mqtt_msg.h | 0 lib/{ => mqtt_msg}/mqtt5_msg.c | 3 +++ lib/{ => mqtt_msg}/mqtt_msg.c | 0 lib/mqtt_outbox/CMakeLists.txt | 4 +++- mqtt5_client.c | 3 +++ mqtt_client.c | 3 +++ test/apps/mqtt_conformance/CMakeLists.txt | 4 ++-- .../mqtt_outbox_host_test/main/CMakeLists.txt | 12 +++--------- 13 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 lib/mqtt_msg/CMakeLists.txt rename lib/{ => mqtt_msg}/include/mqtt5_msg.h (100%) rename lib/{ => mqtt_msg}/include/mqtt_msg.h (100%) rename lib/{ => mqtt_msg}/mqtt5_msg.c (99%) rename lib/{ => mqtt_msg}/mqtt_msg.c (100%) diff --git a/.ci/check_copyright_config.yaml b/.ci/check_copyright_config.yaml index d459127..bc8d151 100644 --- a/.ci/check_copyright_config.yaml +++ b/.ci/check_copyright_config.yaml @@ -38,8 +38,8 @@ examples_and_unit_tests: third_party_libs: include: - - "lib/mqtt_msg.c" - - "lib/include/mqtt_msg.h" + - "lib/mqtt_msg/mqtt_msg.c" + - "lib/mqtt_msg/include/mqtt_msg.h" - "test/host/mocks/include/sys/queue.h" allowed_licenses: - BSD-3-Clause diff --git a/.idf_ci.toml b/.idf_ci.toml index 5d5092c..78a9990 100644 --- a/.idf_ci.toml +++ b/.idf_ci.toml @@ -128,8 +128,8 @@ mqtt_conformance_coverage_report{{ settings.gitlab.test_pipeline.job_name_suffix gcovr_filters=( --filter="${CI_PROJECT_DIR}/mqtt_client.c" --filter="${CI_PROJECT_DIR}/mqtt5_client.c" - --filter="${CI_PROJECT_DIR}/lib/mqtt_msg.c" - --filter="${CI_PROJECT_DIR}/lib/mqtt5_msg.c" + --filter="${CI_PROJECT_DIR}/lib/mqtt_msg/mqtt_msg.c" + --filter="${CI_PROJECT_DIR}/lib/mqtt_msg/mqtt5_msg.c" --filter="${CI_PROJECT_DIR}/lib/platform.c" ) shopt -s nullglob diff --git a/CMakeLists.txt b/CMakeLists.txt index 817a7dd..d3a795f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,12 @@ -set(srcs mqtt_client.c lib/mqtt_msg.c lib/platform.c) +set(srcs mqtt_client.c lib/platform.c) if(CONFIG_MQTT_PROTOCOL_5) - list(APPEND srcs lib/mqtt5_msg.c mqtt5_client.c) + list(APPEND srcs mqtt5_client.c) endif() list(TRANSFORM srcs PREPEND ${CMAKE_CURRENT_LIST_DIR}/) idf_component_register(SRCS "${srcs}" - INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include + INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include ${CMAKE_CURRENT_LIST_DIR}/lib/mqtt_msg/include PRIV_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/lib/include REQUIRES esp_event tcp_transport PRIV_REQUIRES esp_timer http_parser esp_hw_support heap @@ -16,5 +16,8 @@ idf_component_register(SRCS "${srcs}" add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/lib/mqtt_utils) target_link_libraries(${COMPONENT_LIB} PUBLIC idf::mqtt::utils) +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/lib/mqtt_msg) +target_link_libraries(${COMPONENT_LIB} PRIVATE idf::mqtt::msg) + add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/lib/mqtt_outbox) target_link_libraries(${COMPONENT_LIB} PRIVATE idf::mqtt::outbox) diff --git a/lib/mqtt_msg/CMakeLists.txt b/lib/mqtt_msg/CMakeLists.txt new file mode 100644 index 0000000..b9b3b0c --- /dev/null +++ b/lib/mqtt_msg/CMakeLists.txt @@ -0,0 +1,19 @@ +set(srcs "${CMAKE_CURRENT_LIST_DIR}/mqtt_msg.c") + +if(CONFIG_MQTT_PROTOCOL_5) + list(APPEND srcs "${CMAKE_CURRENT_LIST_DIR}/mqtt5_msg.c") +endif() + +add_library(mqtt_msg_lib ${srcs}) +target_include_directories(mqtt_msg_lib + PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/include + ${CMAKE_CURRENT_LIST_DIR}/../../include + PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/../include) + +idf_component_get_property(heap heap COMPONENT_LIB) +idf_component_get_property(log log COMPONENT_LIB) +target_link_libraries(mqtt_msg_lib PRIVATE ${heap} ${log}) + +add_library(idf::mqtt::msg ALIAS mqtt_msg_lib) diff --git a/lib/include/mqtt5_msg.h b/lib/mqtt_msg/include/mqtt5_msg.h similarity index 100% rename from lib/include/mqtt5_msg.h rename to lib/mqtt_msg/include/mqtt5_msg.h diff --git a/lib/include/mqtt_msg.h b/lib/mqtt_msg/include/mqtt_msg.h similarity index 100% rename from lib/include/mqtt_msg.h rename to lib/mqtt_msg/include/mqtt_msg.h diff --git a/lib/mqtt5_msg.c b/lib/mqtt_msg/mqtt5_msg.c similarity index 99% rename from lib/mqtt5_msg.c rename to lib/mqtt_msg/mqtt5_msg.c index 1262b84..26685e1 100644 --- a/lib/mqtt5_msg.c +++ b/lib/mqtt_msg/mqtt5_msg.c @@ -331,6 +331,9 @@ static mqtt5_user_property_handle_t mqtt5_msg_parse_user_property(uint8_t *buffe /* False positive: user_porperty is released at err or returned to the caller. * The analyzer cannot track its cleanup across the translation-unit boundary. */ #pragma GCC diagnostic push +#ifdef __clang__ +#pragma GCC diagnostic ignored "-Wunknown-warning-option" +#endif #pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak" if (mqtt5_msg_set_user_property(&user_property, (char *)key, key_len, (char *)value, value_len) != ESP_OK) { diff --git a/lib/mqtt_msg.c b/lib/mqtt_msg/mqtt_msg.c similarity index 100% rename from lib/mqtt_msg.c rename to lib/mqtt_msg/mqtt_msg.c diff --git a/lib/mqtt_outbox/CMakeLists.txt b/lib/mqtt_outbox/CMakeLists.txt index 2ef8859..95ee9d7 100644 --- a/lib/mqtt_outbox/CMakeLists.txt +++ b/lib/mqtt_outbox/CMakeLists.txt @@ -6,5 +6,7 @@ target_include_directories(mqtt_outbox_lib PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../../include) idf_component_get_property(heap heap COMPONENT_LIB) idf_component_get_property(log log COMPONENT_LIB) -target_link_libraries(mqtt_outbox_lib PRIVATE ${heap} ${log}) +target_link_libraries(mqtt_outbox_lib + PUBLIC idf::mqtt::msg + PRIVATE ${heap} ${log}) add_library(idf::mqtt::outbox ALIAS mqtt_outbox_lib) diff --git a/mqtt5_client.c b/mqtt5_client.c index 96149fc..b2f1c07 100644 --- a/mqtt5_client.c +++ b/mqtt5_client.c @@ -796,6 +796,9 @@ esp_err_t esp_mqtt5_client_set_connect_property(esp_mqtt5_client_handle_t client * esp_mqtt_destroy_config() calls esp_mqtt5_client_destory(), which frees this allocation. * The cleanup crosses a translation-unit boundary that analyzer does not follow. */ #pragma GCC diagnostic push +#ifdef __clang__ +#pragma GCC diagnostic ignored "-Wunknown-warning-option" +#endif #pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak" client->mqtt5_config->will_property_info.correlation_data = malloc(connect_property->correlation_data_len); ESP_MEM_CHECK(TAG, client->mqtt5_config->will_property_info.correlation_data, goto _mqtt_set_config_failed); diff --git a/mqtt_client.c b/mqtt_client.c index 8673e4b..40dad91 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -301,6 +301,9 @@ bool esp_mqtt_set_if_config(char const *const new_config, char **old_config) * (e.g. client->config->uri). On the failure path esp_mqtt_destroy_config frees it, * but the analyzer cannot follow that cross-TU cleanup chain. */ #pragma GCC diagnostic push +#ifdef __clang__ +#pragma GCC diagnostic ignored "-Wunknown-warning-option" +#endif #pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak" *old_config = strdup(new_config); diff --git a/test/apps/mqtt_conformance/CMakeLists.txt b/test/apps/mqtt_conformance/CMakeLists.txt index d583ee5..4cde776 100644 --- a/test/apps/mqtt_conformance/CMakeLists.txt +++ b/test/apps/mqtt_conformance/CMakeLists.txt @@ -30,8 +30,8 @@ if(CONFIG_ESP_GCOV_ENABLE AND COMMAND idf_create_coverage_report) GCOV_OPTIONS "--filter=${mqtt_root}/mqtt_client.c" "--filter=${mqtt_root}/mqtt5_client.c" - "--filter=${mqtt_root}/lib/mqtt_msg.c" - "--filter=${mqtt_root}/lib/mqtt5_msg.c" + "--filter=${mqtt_root}/lib/mqtt_msg/mqtt_msg.c" + "--filter=${mqtt_root}/lib/mqtt_msg/mqtt5_msg.c" "--filter=${mqtt_root}/lib/platform.c" "--xml=${CMAKE_CURRENT_BINARY_DIR}/coverage_report/coverage.xml") idf_clean_coverage_report(${CMAKE_CURRENT_BINARY_DIR}/coverage_report) diff --git a/test/mqtt_outbox_host_test/main/CMakeLists.txt b/test/mqtt_outbox_host_test/main/CMakeLists.txt index 7958a2e..c79baa1 100644 --- a/test/mqtt_outbox_host_test/main/CMakeLists.txt +++ b/test/mqtt_outbox_host_test/main/CMakeLists.txt @@ -7,22 +7,16 @@ set(SANITIZER_FLAGS -fsanitize=address,undefined -fno-sanitize-recover=undefined target_compile_options(${COMPONENT_LIB} PRIVATE ${SANITIZER_FLAGS} -Wno-missing-field-initializers) target_link_options(${COMPONENT_LIB} INTERFACE ${SANITIZER_FLAGS}) +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../../lib/mqtt_msg + ${CMAKE_CURRENT_BINARY_DIR}/mqtt_msg) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../../lib/mqtt_outbox ${CMAKE_CURRENT_BINARY_DIR}/mqtt_outbox) -# mqtt_outbox.c now owns mqtt_message_t and calls mqtt_msg_destroy(). -add_library(mqtt_msg_lib "${CMAKE_CURRENT_LIST_DIR}/../../../lib/mqtt_msg.c") -target_include_directories(mqtt_msg_lib PUBLIC - ${CMAKE_CURRENT_LIST_DIR}/../../../lib/include - ${CMAKE_CURRENT_LIST_DIR}/../../../include) -idf_component_get_property(heap heap COMPONENT_LIB) -idf_component_get_property(log log COMPONENT_LIB) -target_link_libraries(mqtt_msg_lib PRIVATE ${heap} ${log}) target_compile_options(mqtt_msg_lib PRIVATE ${SANITIZER_FLAGS}) target_link_libraries(${COMPONENT_LIB} PUBLIC idf::mqtt::outbox - mqtt_msg_lib + idf::mqtt::msg Catch2::Catch2WithMain rapidcheck rapidcheck_catch)