Skip to content

Commit

Permalink
modules: Fix optional CDDL fields
Browse files Browse the repository at this point in the history
Some CBOR fields are optional. Update the CDDL schema and code to
not fail when missing.

Signed-off-by: Gregers Gram Rygg <[email protected]>
  • Loading branch information
gregersrygg committed Jun 4, 2024
1 parent 0b124bd commit 3e844fd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 26 deletions.
2 changes: 2 additions & 0 deletions app/src/common/message_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ enum cloud_status {
};

struct configuration {
bool led_present;
int led_red;
int led_green;
int led_blue;
bool config_present;
bool gnss;
uint64_t update_interval;
};
Expand Down
43 changes: 25 additions & 18 deletions app/src/modules/app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,31 @@ static void shadow_get(bool get_desired)
return;
}

struct configuration configuration = {
.led_red = app_object.lwm2m._1424010._0._0,
.led_green = app_object.lwm2m._1424010._0._1,
.led_blue = app_object.lwm2m._1424010._0._2,
.gnss = app_object.lwm2m._1430110._0._1,
.update_interval = app_object.lwm2m._1430110._0._0,
};

LOG_DBG("LED object (1424010) values received from cloud:");
LOG_DBG("R: %d", app_object.lwm2m._1424010._0._0);
LOG_DBG("G: %d", app_object.lwm2m._1424010._0._1);
LOG_DBG("B: %d", app_object.lwm2m._1424010._0._2);
LOG_DBG("Timestamp: %lld", app_object.lwm2m._1424010._0._99);

LOG_DBG("Application configuration object (1430110) values received from cloud:");
LOG_DBG("Update interval: %lld", app_object.lwm2m._1430110._0._0);
LOG_DBG("GNSS: %d", app_object.lwm2m._1430110._0._1);
LOG_DBG("Timestamp: %lld", app_object.lwm2m._1430110._0._99);
struct configuration configuration = { 0 };

if (app_object.lwm2m._1424010_present) {
configuration.led_present = true;
configuration.led_red = app_object.lwm2m._1424010._1424010._0._0;
configuration.led_green = app_object.lwm2m._1424010._1424010._0._1;
configuration.led_blue = app_object.lwm2m._1424010._1424010._0._2;

LOG_DBG("LED object (1424010) values received from cloud:");
LOG_DBG("R: %d", app_object.lwm2m._1424010._1424010._0._0);
LOG_DBG("G: %d", app_object.lwm2m._1424010._1424010._0._1);
LOG_DBG("B: %d", app_object.lwm2m._1424010._1424010._0._2);
LOG_DBG("Timestamp: %lld", app_object.lwm2m._1424010._1424010._0._99);
}

if (app_object.lwm2m._1430110_present) {
configuration.config_present = true;
configuration.update_interval = app_object.lwm2m._1430110._1430110._0._0;
configuration.gnss = app_object.lwm2m._1430110._1430110._0._1;

LOG_DBG("Application configuration object (1430110) values received from cloud:");
LOG_DBG("Update interval: %lld", app_object.lwm2m._1430110._1430110._0._0);
LOG_DBG("GNSS: %d", app_object.lwm2m._1430110._1430110._0._1);
LOG_DBG("Timestamp: %lld", app_object.lwm2m._1430110._1430110._0._99);
}

/* Distribute configuration */
err = zbus_chan_pub(&CONFIG_CHAN, &configuration, K_SECONDS(1));
Expand Down
16 changes: 8 additions & 8 deletions app/src/modules/app/app_object.cddl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
; Define the basic structure of the JSON object
app-object = {
"nrfcloud_mqtt_topic_prefix": tstr,
"pairing": pairing-type,
? "nrfcloud_mqtt_topic_prefix": tstr,
? "pairing": pairing-type,
"lwm2m": lwm2m-map
}

Expand All @@ -15,26 +15,26 @@ pairing-type = {
}

lwm2m-map = {
"14240:1.0": led,
"14301:1.0": config
? "14240:1.0": led,
? "14301:1.0": config
}

led = {
"0": lwm2m_inner_object_1
"0": led_inner_object
}

lwm2m_inner_object_1 = {
led_inner_object = {
"0": int .size 4,
"1": int .size 4,
"2": int .size 4,
"99": int .size 8
}

config = {
"0": lwm2m_inner_object
"0": config_inner_object
}

lwm2m_inner_object = {
config_inner_object = {
"0": int .size 8,
"1": bool,
"99": int .size 8
Expand Down
5 changes: 5 additions & 0 deletions app/src/modules/led/led.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ void led_callback(const struct zbus_channel *chan)

const struct configuration *config = zbus_chan_const_msg(chan);

if (config->led_present == false) {
LOG_DBG("LED configuration not present");
return;
}

LOG_DBG("LED configuration: red:%d, green:%d, blue:%d",
config->led_red, config->led_green, config->led_blue);

Expand Down
5 changes: 5 additions & 0 deletions app/src/modules/trigger/trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ void trigger_callback(const struct zbus_channel *chan)
/* Get update interval configuration from channel. */
const struct configuration *config = zbus_chan_const_msg(chan);

if (config->config_present == false) {
LOG_DBG("Configuration not present");
return;
}

LOG_DBG("New update interval: %lld", config->update_interval);

update_interval = K_SECONDS(config->update_interval);
Expand Down

0 comments on commit 3e844fd

Please sign in to comment.