Skip to content

Commit b3ba549

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 4bf0381 commit b3ba549

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
@@ -107,6 +107,16 @@ Other subsystems
107107

108108
.. zephyr-keep-sorted-start re(^\w)
109109
110+
Shell
111+
=====
112+
113+
* The MQTT topics related to :kconfig:option:`SHELL_BACKEND_MQTT` have been renamed. Renamed
114+
``<device_id>_rx`` to ``<device_id>/sh/rx`` and ``<device_id>_tx`` to ``<device_id>/sh/rx``. The
115+
part after the ``<device_id>`` is now configurable via :kconfig:option:`SHELL_MQTT_TOPIC_RX_ID`
116+
and :kconfig:option:`SHELL_MQTT_TOPIC_TX_ID`. This allows keeping the previous topics for backward
117+
compatibility.
118+
(:github:`92677`).
119+
110120
.. zephyr-keep-sorted-stop
111121
112122
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
@@ -348,6 +348,18 @@ config SHELL_MQTT_LISTEN_TIMEOUT_MS
348348
help
349349
Time to listen for incoming packets in milliseconds.
350350

351+
config SHELL_MQTT_TOPIC_RX_ID
352+
string "MQTT topic receive identifier"
353+
default "/sh/rx"
354+
help
355+
String used as receive identifier in the MQTT topic.
356+
357+
config SHELL_MQTT_TOPIC_TX_ID
358+
string "MQTT topic transmit identifier"
359+
default "/sh/tx"
360+
help
361+
String used as transmit identifier in the MQTT topic.
362+
351363
module = SHELL_BACKEND_MQTT
352364
default-timeout = 100
353365
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)