From 905b84fb9767170a88ac59984441b2287640042f Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Wed, 5 Mar 2025 15:03:59 +0100 Subject: [PATCH] feat(mosq): Allow user to enable SYS topic Mosquitto provides several topics under $SYS/# to capture logs and load statistics. This allows users to enable it. --- components/mosquitto/CMakeLists.txt | 7 +++++-- components/mosquitto/Kconfig | 15 +++++++++++++++ components/mosquitto/port/config.c | 11 +++++++---- components/mosquitto/port/mosq_time.c | 19 +++++++++++++++++++ 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 components/mosquitto/Kconfig create mode 100644 components/mosquitto/port/mosq_time.c diff --git a/components/mosquitto/CMakeLists.txt b/components/mosquitto/CMakeLists.txt index a7e337009..3608021e2 100644 --- a/components/mosquitto/CMakeLists.txt +++ b/components/mosquitto/CMakeLists.txt @@ -21,7 +21,6 @@ set(m_srcs ${m_lib_dir}/handle_pubrec.c ${m_lib_dir}/handle_pubrel.c ${m_lib_dir}/handle_ping.c - ${m_lib_dir}/time_mosq.c ${m_lib_dir}/utf8_mosq.c ${m_src_dir}/bridge.c @@ -75,6 +74,7 @@ idf_component_register(SRCS ${m_srcs} port/config.c port/signals.c port/broker.c + port/mosq_time.c port/files.c port/net__esp_tls.c port/sysconf.c @@ -82,9 +82,12 @@ idf_component_register(SRCS ${m_srcs} ${m_incl_dir} ${m_lib_dir} ${m_deps_dir} INCLUDE_DIRS ${m_incl_dir} port/include REQUIRES esp-tls - PRIV_REQUIRES newlib sock_utils) + PRIV_REQUIRES newlib sock_utils esp_timer) target_compile_definitions(${COMPONENT_LIB} PRIVATE "WITH_BROKER") +if (CONFIG_MOSQ_ENABLE_SYS) + target_compile_definitions(${COMPONENT_LIB} PRIVATE "WITH_SYS_TREE") +endif() target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") # Some mosquitto source unconditionally define `_GNU_SOURCE` which collides with IDF build system diff --git a/components/mosquitto/Kconfig b/components/mosquitto/Kconfig new file mode 100644 index 000000000..00d5e0a01 --- /dev/null +++ b/components/mosquitto/Kconfig @@ -0,0 +1,15 @@ +menu "Mosquitto" + + config MOSQ_ENABLE_SYS + bool "Enable $SYS topics" + default n + help + Enable the $SYS topics for the broker + + config MOSQ_SYS_UPDATE_INTERVAL + int "Update interval for the SYS topic" + default 10 + depends on MOSQ_ENABLE_SYS + help + Time in seconds for the update of the $SYS topics for the broker +endmenu diff --git a/components/mosquitto/port/config.c b/components/mosquitto/port/config.c index 04ab00b97..29f927ee2 100644 --- a/components/mosquitto/port/config.c +++ b/components/mosquitto/port/config.c @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: EPL-2.0 * - * SPDX-FileContributor: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileContributor: 2024-2025 Espressif Systems (Shanghai) CO LTD */ #include "mosquitto_internal.h" #include "mosquitto_broker.h" @@ -14,6 +14,7 @@ #include "utlist.h" #include "lib_load.h" #include "syslog.h" +#include "sdkconfig.h" void config__init(struct mosquitto__config *config) @@ -66,7 +67,7 @@ void config__init(struct mosquitto__config *config) config->log_file = NULL; config->log_facility = LOG_DAEMON; - config->log_dest = MQTT3_LOG_STDERR | MQTT3_LOG_DLT; + config->log_dest = MQTT3_LOG_STDERR | MQTT3_LOG_TOPIC; if (db.verbose) { config->log_type = UINT_MAX; } else { @@ -91,7 +92,9 @@ void config__init(struct mosquitto__config *config) config->queue_qos0_messages = false; config->retain_available = true; config->set_tcp_nodelay = false; - config->sys_interval = 10; +#if defined(WITH_SYS_TREE) + config->sys_interval = CONFIG_MOSQ_SYS_UPDATE_INTERVAL; +#endif config->upgrade_outgoing_qos = false; config->daemon = false; @@ -236,7 +239,7 @@ char *misc__trimblanks(char *str) // Dummy definition of fork() to work around IDF warning: " warning: _fork is not implemented and will always fail" // fork() is used in mosquitto.c to deamonize the broker, which we do not call. -pid_t fork (void) +pid_t fork(void) { abort(); return 0; diff --git a/components/mosquitto/port/mosq_time.c b/components/mosquitto/port/mosq_time.c new file mode 100644 index 000000000..add6b3c00 --- /dev/null +++ b/components/mosquitto/port/mosq_time.c @@ -0,0 +1,19 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "time_mosq.h" + +#include "esp_timer.h" + +void mosquitto_time_init(void) +{ +} + +time_t mosquitto_time(void) +{ + return esp_timer_get_time() / 1000000; // Convert microseconds to seconds +}