Skip to content

Commit d5fc33c

Browse files
committed
modules: main: Add CDDL CBOR encoding of device shadow
Add missing CDDL CBOR decoding of incoming data. Used to decode update interval set in cloud. Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
1 parent 6b9e39c commit d5fc33c

File tree

4 files changed

+43
-58
lines changed

4 files changed

+43
-58
lines changed

app/src/cbor/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,31 @@
66

77
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cbor_helper.c)
88
target_include_directories(app PRIVATE .)
9+
10+
# generate encoder code using zcbor
11+
set(zcbor_command
12+
zcbor code # Invoke code generation
13+
--cddl ${CMAKE_CURRENT_SOURCE_DIR}/device_shadow.cddl
14+
--decode # Generate decoding functions
15+
--short-names # Attempt to make generated symbol names shorter (at the risk of collision)
16+
-t device-shadow # Create a public API for decoding the "device_shadow" type from the cddl file
17+
--output-cmake device_shadow.cmake # The generated cmake file will be placed here
18+
)
19+
execute_process(COMMAND ${zcbor_command}
20+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
21+
COMMAND_ERROR_IS_FATAL ANY)
22+
23+
# Include the cmake file generated by zcbor. It adds the
24+
# generated code and the necessary zcbor C code files.
25+
include(${CMAKE_CURRENT_BINARY_DIR}/device_shadow.cmake)
26+
27+
# Ensure that the cmake reconfiguration is triggerred everytime the cddl file changes.
28+
# This ensures that the codec generation is triggered.
29+
set_property(
30+
DIRECTORY
31+
PROPERTY
32+
CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/device_shadow.cddl
33+
)
34+
35+
zephyr_link_libraries(device_shadow)
36+
target_link_libraries(device_shadow PRIVATE zephyr_interface)

app/src/cbor/cbor_helper.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@
55
*/
66

77
#include <zephyr/logging/log.h>
8+
#include "device_shadow_decode.h"
89

910
LOG_MODULE_DECLARE(main, CONFIG_APP_LOG_LEVEL);
1011

1112
int get_update_interval_from_cbor_response(const uint8_t *cbor,
1213
size_t len,
1314
uint64_t *interval_sec)
1415
{
15-
*interval_sec = 1800;
16+
struct device_shadow device_shadow = { 0 };
17+
size_t not_used;
18+
19+
int err = cbor_decode_device_shadow(cbor, len, &device_shadow, &not_used);
20+
21+
if (err) {
22+
LOG_ERR("Ignoring incoming configuration change due to decoding error: %d", err);
23+
LOG_HEXDUMP_ERR(buf_cbor, buf_cbor_len, "CBOR data");
24+
return -EFAULT;
25+
}
26+
27+
*interval_sec = device_shadow.config.update_interval;
1628
return 0;
1729
}

app/src/cbor/device_shadow.cddl

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,4 @@
1-
; Define the basic structure of the JSON object
2-
3-
; The 'any' type is used in this schema to allow forward compatibility.
4-
; They are inserted into maps that may contain additional fields in the future.
5-
; This prevents cbor decoding from failing in the field when the cloud side has new fields.
6-
app-object = {
7-
? "nrfcloud_mqtt_topic_prefix": tstr,
8-
? "pairing": pairing-type,
9-
? "lwm2m": lwm2m-map,
1+
config-object = {
2+
? "update_interval": int .size 4,
103
* tstr => any
114
}
12-
13-
; Define the pairing object with nested topics
14-
pairing-type = {
15-
"state": tstr,
16-
"topics": {
17-
"d2c": tstr,
18-
"c2d": tstr,
19-
* tstr => any
20-
},
21-
* any => any
22-
}
23-
24-
lwm2m-map = {
25-
? "14240:1.0": led,
26-
? "14301:1.0": config,
27-
* tstr => any
28-
}
29-
30-
led = {
31-
"0": led_inner_object,
32-
* tstr => any
33-
}
34-
35-
led_inner_object = {
36-
? "0": int .size 4,
37-
? "1": int .size 4,
38-
? "2": int .size 4,
39-
"99": int .size 8,
40-
* tstr => any
41-
}
42-
43-
config = {
44-
"0": config_inner_object,
45-
* tstr => any
46-
}
47-
48-
config_inner_object = {
49-
? "0": int .size 8,
50-
? "1": bool,
51-
"99": int .size 8,
52-
* tstr => any
53-
}

tests/module/main/src/main.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ static void send_location_search_done(void)
141141
TEST_ASSERT_EQUAL(0, err);
142142
}
143143

144-
/*
145144
static void send_config(uint64_t interval)
146145
{
147146
const struct cloud_msg msg = {
@@ -154,7 +153,6 @@ static void send_config(uint64_t interval)
154153

155154
TEST_ASSERT_EQUAL(0, err);
156155
}
157-
*/
158156

159157
static void send_cloud_disconnected(void)
160158
{
@@ -215,7 +213,6 @@ void test_button_press_on_disconnected(void)
215213
check_no_events(7200);
216214
}
217215

218-
/*
219216
void test_trigger_interval_change_in_connected(void)
220217
{
221218
send_cloud_connected_ready_to_send();
@@ -231,9 +228,7 @@ void test_trigger_interval_change_in_connected(void)
231228
send_cloud_disconnected();
232229
check_no_events(WEEK_IN_SECONDS);
233230
}
234-
*/
235231

236-
/*
237232
void test_trigger_disconnect_and_connect_when_triggering(void)
238233
{
239234
send_cloud_connected_ready_to_send();
@@ -256,7 +251,6 @@ void test_trigger_disconnect_and_connect_when_triggering(void)
256251
send_cloud_disconnected();
257252
check_no_events(WEEK_IN_SECONDS);
258253
}
259-
*/
260254

261255
/* This is required to be added to each test. That is because unity's
262256
* main may return nonzero, while zephyr's main currently must

0 commit comments

Comments
 (0)