-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcloud_shell.c
More file actions
66 lines (53 loc) · 1.74 KB
/
cloud_shell.c
File metadata and controls
66 lines (53 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <zephyr/kernel.h>
#include <zephyr/shell/shell.h>
#include <zephyr/logging/log.h>
#include <zephyr/zbus/zbus.h>
#include <net/nrf_cloud_defs.h>
#include <date_time.h>
#include "cloud.h"
LOG_MODULE_DECLARE(cloud, CONFIG_APP_CLOUD_LOG_LEVEL);
#define PAYLOAD_MSG_TEMPLATE \
"{\"" NRF_CLOUD_JSON_MSG_TYPE_KEY "\":\"" NRF_CLOUD_JSON_MSG_TYPE_VAL_DATA "\"," \
"\"" NRF_CLOUD_JSON_APPID_KEY "\":\"%s\"," \
"\"" NRF_CLOUD_JSON_DATA_KEY "\":\"%s\"," \
"\"" NRF_CLOUD_MSG_TIMESTAMP_KEY "\":%lld}"
static int cmd_publish(const struct shell *sh, size_t argc, char **argv)
{
int err;
int64_t current_time;
struct cloud_msg msg = {
.type = CLOUD_PAYLOAD_JSON,
};
if (argc != 3) {
(void)shell_print(sh, "Invalid number of arguments (%d)", argc);
(void)shell_print(sh, "Usage: att_cloud_publish <appid> <data>");
return 1;
}
err = date_time_now(¤t_time);
if (err) {
(void)shell_print(sh, "Failed to get current time, error: %d", err);
return 1;
}
err = snprintk(msg.payload.buffer, sizeof(msg.payload.buffer),
PAYLOAD_MSG_TEMPLATE,
argv[1], argv[2], current_time);
if (err < 0 || err >= sizeof(msg.payload.buffer)) {
(void)shell_print(sh, "Failed to format payload, error: %d", err);
return 1;
}
msg.payload.buffer_data_len = err;
(void)shell_print(sh, "Sending on payload channel: %s (%d bytes)",
msg.payload.buffer, msg.payload.buffer_data_len);
err = zbus_chan_pub(&CLOUD_CHAN, &msg, K_SECONDS(1));
if (err) {
(void)shell_print(sh, "zbus_chan_pub, error: %d", err);
return 1;
}
return 0;
}
SHELL_CMD_REGISTER(att_cloud_publish, NULL, "Asset Tracker Template Cloud CMDs", cmd_publish);