Skip to content

Commit ee79924

Browse files
author
Jeppe Odgaard
committed
shell: mqtt: use topic levels
Change topic from <device_id>_rx (and tx) to <device_id>/sh/rx. This allows use of wildcards. E.g. subscribe to all devices "+/sh/tx". Level "sh" is added to the topic to make it less generic and prevent potential clashes with other topics. The topic part after <device_id> is configurable via Kconfig. Signed-off-by: Jeppe Odgaard <[email protected]>
1 parent dece779 commit ee79924

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

doc/releases/migration-guide-4.3.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ Other subsystems
8585

8686
.. zephyr-keep-sorted-start re(^\w)
8787
88+
Shell
89+
=====
90+
91+
* The MQTT topics related to :kconfig:option:`SHELL_BACKEND_MQTT` have been renamed. Renamed
92+
``<device_id>_rx`` to ``<device_id>/sh/rx`` and ``<device_id>_tx`` to ``<device_id>/sh/rx``. The
93+
part after the ``<device_id>`` is now configurable via :kconfig:option:`SHELL_MQTT_TOPIC_RX_ID`
94+
and :kconfig:option:`SHELL_MQTT_TOPIC_TX_ID`. This allows keeping the previous topics for backward
95+
compatibility.
96+
(:github:`92677`).
97+
8898
.. zephyr-keep-sorted-stop
8999
90100
Modules

include/zephyr/shell/shell_mqtt.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ extern "C" {
2525
#define SH_MQTT_BUFFER_SIZE 64
2626
#define DEVICE_ID_BIN_MAX_SIZE 3
2727
#define DEVICE_ID_HEX_MAX_SIZE ((DEVICE_ID_BIN_MAX_SIZE * 2) + 1)
28-
#define SH_MQTT_TOPIC_MAX_SIZE DEVICE_ID_HEX_MAX_SIZE + 3
28+
#define SH_MQTT_TOPIC_RX_MAX_SIZE DEVICE_ID_HEX_MAX_SIZE + sizeof(CONFIG_SHELL_MQTT_TOPIC_RX_ID)
29+
#define SH_MQTT_TOPIC_TX_MAX_SIZE DEVICE_ID_HEX_MAX_SIZE + sizeof(CONFIG_SHELL_MQTT_TOPIC_TX_ID)
2930

3031
extern const struct shell_transport_api shell_mqtt_transport_api;
3132

@@ -40,8 +41,8 @@ struct shell_mqtt_tx_buf {
4041
/** MQTT-based shell transport. */
4142
struct shell_mqtt {
4243
char device_id[DEVICE_ID_HEX_MAX_SIZE];
43-
char sub_topic[SH_MQTT_TOPIC_MAX_SIZE];
44-
char pub_topic[SH_MQTT_TOPIC_MAX_SIZE];
44+
char sub_topic[SH_MQTT_TOPIC_RX_MAX_SIZE];
45+
char pub_topic[SH_MQTT_TOPIC_TX_MAX_SIZE];
4546

4647
/** Handler function registered by shell. */
4748
shell_transport_handler_t shell_handler;

subsys/shell/backends/Kconfig.backends

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,18 @@ config SHELL_MQTT_TX_BUF_SIZE
330330
help
331331
Buffer size for the MQTT data transmission.
332332

333+
config SHELL_MQTT_TOPIC_RX_ID
334+
string "MQTT topic receive identifier"
335+
default "/sh/rx"
336+
help
337+
String used as receive identifier in the MQTT topic.
338+
339+
config SHELL_MQTT_TOPIC_TX_ID
340+
string "MQTT topic transmit identifier"
341+
default "/sh/tx"
342+
help
343+
String used as transmit identifier in the MQTT topic.
344+
333345
module = SHELL_BACKEND_MQTT
334346
default-timeout = 100
335347
source "subsys/shell/Kconfig.template.shell_log_queue_timeout"

subsys/shell/backends/shell_mqtt.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,10 @@ static int init(const struct shell_transport *transport, const void *config,
657657

658658
LOG_DBG("Client ID is %s", sh->device_id);
659659

660-
(void)snprintf(sh->pub_topic, SH_MQTT_TOPIC_MAX_SIZE, "%s_tx", sh->device_id);
661-
(void)snprintf(sh->sub_topic, SH_MQTT_TOPIC_MAX_SIZE, "%s_rx", sh->device_id);
660+
(void)snprintf(sh->pub_topic, SH_MQTT_TOPIC_TX_MAX_SIZE, "%s" CONFIG_SHELL_MQTT_TOPIC_TX_ID,
661+
sh->device_id);
662+
(void)snprintf(sh->sub_topic, SH_MQTT_TOPIC_RX_MAX_SIZE, "%s" CONFIG_SHELL_MQTT_TOPIC_RX_ID,
663+
sh->device_id);
662664

663665
ring_buf_init(&sh->rx_rb, RX_RB_SIZE, sh->rx_rb_buf);
664666

0 commit comments

Comments
 (0)