Skip to content

Commit 878a404

Browse files
committed
modules: shell: Implement zbus publish command for payload
Add support for `zbus publish payload_chan <appId> <data>`. The command will convert the input appId and data into a JSON object and send it on the payload channel. Signed-off-by: Jan Tore Guggedal <jantore.guggedal@nordicsemi.no>
1 parent 71c202d commit 878a404

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

app/src/modules/shell/shell.c

+42-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@
1818
#include <date_time.h>
1919
#include <zephyr/task_wdt/task_wdt.h>
2020
#include <modem/nrf_modem_lib_trace.h>
21+
#include <net/nrf_cloud_defs.h>
2122

2223
#include "message_channel.h"
2324

2425
LOG_MODULE_REGISTER(shell, CONFIG_APP_SHELL_LOG_LEVEL);
2526

27+
#define PAYLOAD_MSG_TEMPLATE \
28+
"{\""NRF_CLOUD_JSON_MSG_TYPE_KEY"\":\""NRF_CLOUD_JSON_MSG_TYPE_VAL_DATA"\"," \
29+
"\""NRF_CLOUD_JSON_APPID_KEY"\":\"%s\"," \
30+
"\""NRF_CLOUD_JSON_DATA_KEY"\":\"%s\"," \
31+
"\""NRF_CLOUD_MSG_TIMESTAMP_KEY"\":%lld}"
32+
2633
static const struct device *const shell_uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_shell_uart));
2734
static const struct device *const uart1_dev = DEVICE_DT_GET(DT_NODELABEL(uart1));
2835

@@ -191,13 +198,44 @@ static int cmd_button_press(const struct shell *sh, size_t argc,
191198
return 0;
192199
}
193200

194-
static int cmd_publish_on_payload_chan(const struct shell *sh, size_t argc,
195-
char **argv)
201+
static int cmd_publish_on_payload_chan(const struct shell *sh, size_t argc, char **argv)
196202
{
203+
int err, ret;
204+
struct payload payload = {
205+
.buffer_len = strlen(argv[1]),
206+
};
207+
int64_t current_time;
208+
197209
ARG_UNUSED(argc);
198-
ARG_UNUSED(argv);
199210

200-
shell_print(sh, "Not implemented yet!");
211+
if (argc != 3) {
212+
shell_print(sh, "Invalid number of arguments (%d)", argc);
213+
shell_print(sh, "Usage: zbus publish payload_chan <appid> <data>");
214+
return 1;
215+
}
216+
217+
err = date_time_now(&current_time);
218+
if (err) {
219+
shell_print(sh, "Failed to get current time, error: %d", err);
220+
return 1;
221+
}
222+
223+
ret = snprintk(payload.buffer, sizeof(payload.buffer),
224+
PAYLOAD_MSG_TEMPLATE,
225+
argv[1], argv[2], current_time);
226+
if (ret < 0 || ret >= sizeof(payload.buffer)) {
227+
shell_print(sh, "Failed to format payload, error: %d", ret);
228+
return 1;
229+
}
230+
231+
shell_print(sh, "Sending on payload channel: %s (%d bytes)",
232+
payload.buffer, payload.buffer_len);
233+
234+
err = zbus_chan_pub(&PAYLOAD_CHAN, &payload, K_SECONDS(1));
235+
if (err) {
236+
shell_print(sh, "zbus_chan_pub, error: %d", err);
237+
return 1;
238+
}
201239

202240
return 0;
203241
}

0 commit comments

Comments
 (0)