Skip to content

shell: mqtt: use topic levels #92677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/releases/migration-guide-4.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ Other subsystems

.. zephyr-keep-sorted-start re(^\w)

Shell
=====

* The MQTT topics related to :kconfig:option:`SHELL_BACKEND_MQTT` have been renamed. Renamed
``<device_id>_rx`` to ``<device_id>/sh/rx`` and ``<device_id>_tx`` to ``<device_id>/sh/rx``. The
part after the ``<device_id>`` is now configurable via :kconfig:option:`SHELL_MQTT_TOPIC_RX_ID`
and :kconfig:option:`SHELL_MQTT_TOPIC_TX_ID`. This allows keeping the previous topics for backward
compatibility.
(:github:`92677`).

.. zephyr-keep-sorted-stop

Modules
Expand Down
7 changes: 4 additions & 3 deletions include/zephyr/shell/shell_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ extern "C" {
#define SH_MQTT_BUFFER_SIZE 64
#define DEVICE_ID_BIN_MAX_SIZE 3
#define DEVICE_ID_HEX_MAX_SIZE ((DEVICE_ID_BIN_MAX_SIZE * 2) + 1)
#define SH_MQTT_TOPIC_MAX_SIZE DEVICE_ID_HEX_MAX_SIZE + 3
#define SH_MQTT_TOPIC_RX_MAX_SIZE (DEVICE_ID_HEX_MAX_SIZE + sizeof(CONFIG_SHELL_MQTT_TOPIC_RX_ID))
#define SH_MQTT_TOPIC_TX_MAX_SIZE (DEVICE_ID_HEX_MAX_SIZE + sizeof(CONFIG_SHELL_MQTT_TOPIC_TX_ID))

extern const struct shell_transport_api shell_mqtt_transport_api;

Expand All @@ -40,8 +41,8 @@ struct shell_mqtt_tx_buf {
/** MQTT-based shell transport. */
struct shell_mqtt {
char device_id[DEVICE_ID_HEX_MAX_SIZE];
char sub_topic[SH_MQTT_TOPIC_MAX_SIZE];
char pub_topic[SH_MQTT_TOPIC_MAX_SIZE];
char sub_topic[SH_MQTT_TOPIC_RX_MAX_SIZE];
char pub_topic[SH_MQTT_TOPIC_TX_MAX_SIZE];

/** Handler function registered by shell. */
shell_transport_handler_t shell_handler;
Expand Down
15 changes: 13 additions & 2 deletions subsys/shell/backends/Kconfig.backends
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ config SHELL_BACKEND_MQTT
select DNS_RESOLVER
select HWINFO
select MQTT_LIB
select NET_MGMT
select NET_MGMT_EVENT
select NET_CONNECTION_MANAGER
help
Enable MQTT backend.

Expand Down Expand Up @@ -348,6 +347,18 @@ config SHELL_MQTT_LISTEN_TIMEOUT_MS
help
Time to listen for incoming packets in milliseconds.

config SHELL_MQTT_TOPIC_RX_ID
string "MQTT topic receive identifier"
default "/sh/rx"
help
String used as receive identifier in the MQTT topic.

config SHELL_MQTT_TOPIC_TX_ID
string "MQTT topic transmit identifier"
default "/sh/tx"
help
String used as transmit identifier in the MQTT topic.

module = SHELL_BACKEND_MQTT
default-timeout = 100
source "subsys/shell/Kconfig.template.shell_log_queue_timeout"
Expand Down
6 changes: 4 additions & 2 deletions subsys/shell/backends/shell_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,10 @@ static int init(const struct shell_transport *transport, const void *config,

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

(void)snprintf(sh->pub_topic, SH_MQTT_TOPIC_MAX_SIZE, "%s_tx", sh->device_id);
(void)snprintf(sh->sub_topic, SH_MQTT_TOPIC_MAX_SIZE, "%s_rx", sh->device_id);
(void)snprintf(sh->pub_topic, SH_MQTT_TOPIC_TX_MAX_SIZE, "%s" CONFIG_SHELL_MQTT_TOPIC_TX_ID,
sh->device_id);
(void)snprintf(sh->sub_topic, SH_MQTT_TOPIC_RX_MAX_SIZE, "%s" CONFIG_SHELL_MQTT_TOPIC_RX_ID,
sh->device_id);

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

Expand Down