Skip to content

Commit 905b84f

Browse files
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.
1 parent 37f84ee commit 905b84f

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

components/mosquitto/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ set(m_srcs
2121
${m_lib_dir}/handle_pubrec.c
2222
${m_lib_dir}/handle_pubrel.c
2323
${m_lib_dir}/handle_ping.c
24-
${m_lib_dir}/time_mosq.c
2524
${m_lib_dir}/utf8_mosq.c
2625

2726
${m_src_dir}/bridge.c
@@ -75,16 +74,20 @@ idf_component_register(SRCS ${m_srcs}
7574
port/config.c
7675
port/signals.c
7776
port/broker.c
77+
port/mosq_time.c
7878
port/files.c
7979
port/net__esp_tls.c
8080
port/sysconf.c
8181
PRIV_INCLUDE_DIRS port/priv_include port/priv_include/sys ${m_dir} ${m_src_dir}
8282
${m_incl_dir} ${m_lib_dir} ${m_deps_dir}
8383
INCLUDE_DIRS ${m_incl_dir} port/include
8484
REQUIRES esp-tls
85-
PRIV_REQUIRES newlib sock_utils)
85+
PRIV_REQUIRES newlib sock_utils esp_timer)
8686

8787
target_compile_definitions(${COMPONENT_LIB} PRIVATE "WITH_BROKER")
88+
if (CONFIG_MOSQ_ENABLE_SYS)
89+
target_compile_definitions(${COMPONENT_LIB} PRIVATE "WITH_SYS_TREE")
90+
endif()
8891
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
8992

9093
# Some mosquitto source unconditionally define `_GNU_SOURCE` which collides with IDF build system

components/mosquitto/Kconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
menu "Mosquitto"
2+
3+
config MOSQ_ENABLE_SYS
4+
bool "Enable $SYS topics"
5+
default n
6+
help
7+
Enable the $SYS topics for the broker
8+
9+
config MOSQ_SYS_UPDATE_INTERVAL
10+
int "Update interval for the SYS topic"
11+
default 10
12+
depends on MOSQ_ENABLE_SYS
13+
help
14+
Time in seconds for the update of the $SYS topics for the broker
15+
endmenu

components/mosquitto/port/config.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* SPDX-License-Identifier: EPL-2.0
55
*
6-
* SPDX-FileContributor: 2024 Espressif Systems (Shanghai) CO LTD
6+
* SPDX-FileContributor: 2024-2025 Espressif Systems (Shanghai) CO LTD
77
*/
88
#include "mosquitto_internal.h"
99
#include "mosquitto_broker.h"
@@ -14,6 +14,7 @@
1414
#include "utlist.h"
1515
#include "lib_load.h"
1616
#include "syslog.h"
17+
#include "sdkconfig.h"
1718

1819

1920
void config__init(struct mosquitto__config *config)
@@ -66,7 +67,7 @@ void config__init(struct mosquitto__config *config)
6667
config->log_file = NULL;
6768

6869
config->log_facility = LOG_DAEMON;
69-
config->log_dest = MQTT3_LOG_STDERR | MQTT3_LOG_DLT;
70+
config->log_dest = MQTT3_LOG_STDERR | MQTT3_LOG_TOPIC;
7071
if (db.verbose) {
7172
config->log_type = UINT_MAX;
7273
} else {
@@ -91,7 +92,9 @@ void config__init(struct mosquitto__config *config)
9192
config->queue_qos0_messages = false;
9293
config->retain_available = true;
9394
config->set_tcp_nodelay = false;
94-
config->sys_interval = 10;
95+
#if defined(WITH_SYS_TREE)
96+
config->sys_interval = CONFIG_MOSQ_SYS_UPDATE_INTERVAL;
97+
#endif
9598
config->upgrade_outgoing_qos = false;
9699

97100
config->daemon = false;
@@ -236,7 +239,7 @@ char *misc__trimblanks(char *str)
236239

237240
// Dummy definition of fork() to work around IDF warning: " warning: _fork is not implemented and will always fail"
238241
// fork() is used in mosquitto.c to deamonize the broker, which we do not call.
239-
pid_t fork (void)
242+
pid_t fork(void)
240243
{
241244
abort();
242245
return 0;

components/mosquitto/port/mosq_time.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <time.h>
8+
#include "time_mosq.h"
9+
10+
#include "esp_timer.h"
11+
12+
void mosquitto_time_init(void)
13+
{
14+
}
15+
16+
time_t mosquitto_time(void)
17+
{
18+
return esp_timer_get_time() / 1000000; // Convert microseconds to seconds
19+
}

0 commit comments

Comments
 (0)