Skip to content

Commit

Permalink
app_state: convert observe to work with json
Browse files Browse the repository at this point in the history
Convert obsevation of the "desired" LightDB State path to directly monitor
the "reset_cumulative" value.

This is necessary because the LightDB Observe API call for the Golioth
Firmware SDK only allows JSON-type observation. A PR has been opened to
allow CBOR to be selected. This commit can be reverted when that option is
available.

Signed-off-by: Mike Szczys <[email protected]>
  • Loading branch information
szczys committed May 17, 2024
1 parent 190b13f commit cc45e62
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 35 deletions.
4 changes: 4 additions & 0 deletions prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ CONFIG_NET_LOG=y
CONFIG_NET_SHELL=y
CONFIG_REBOOT=y

# Adjust coap setting for a long (16-char) LightDB State sub-path
CONFIG_COAP_EXTENDED_OPTIONS_LEN=y
CONFIG_COAP_EXTENDED_OPTIONS_LEN_VALUE=16

# Flash memory (etc.) for firmware upgrade
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
Expand Down
40 changes: 39 additions & 1 deletion src/app_sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ static int log_sensor_values(adc_node_t *sensor, bool get_new_reading)
sensor_value_to_double(&cur),
sensor_value_to_double(&pow)
);

IF_ENABLED(CONFIG_LIB_OSTENTUS, (
char ostentus_buf[32];
uint8_t slide_num;

snprintk(ostentus_buf, sizeof(ostentus_buf), "%.02f V",
sensor_value_to_double(&vol));
slide_num = (sensor->ch_num == 0) ? CH0_VOLTAGE : CH1_VOLTAGE;
slide_set(slide_num, ostentus_buf, strlen(ostentus_buf));

snprintk(ostentus_buf, sizeof(ostentus_buf), "%.02f mA",
sensor_value_to_double(&cur) * 1000);
slide_num = (sensor->ch_num == 0) ? CH0_CURRENT : CH1_CURRENT;
slide_set(slide_num, ostentus_buf, strlen(ostentus_buf));

snprintk(ostentus_buf, sizeof(ostentus_buf), "%.02f W",
sensor_value_to_double(&pow));
slide_num = (sensor->ch_num == 0) ? CH0_POWER : CH1_POWER;
slide_set(slide_num, ostentus_buf, strlen(ostentus_buf));
));
} else {
return -ENODATA;
}
Expand Down Expand Up @@ -288,11 +308,19 @@ int reset_cumulative_totals(void)
adc_ch0.total_unreported = 0;
adc_ch1.total_unreported = 0;
k_sem_give(&adc_data_sem);
return 0;
} else {
LOG_ERR("Could not reset cumulative values; blocked by semaphore.");
return -EACCES;
}

/* Send new values to Golioth */
int err = app_state_report_ontime(&adc_ch0, &adc_ch1);

if (err) {
LOG_ERR("Unable to send ontime to server: %d", err);
}

return err;
}

/* This will be called by the main() loop */
Expand Down Expand Up @@ -365,6 +393,16 @@ static void get_cumulative_handler(struct golioth_client *client,
return;
}

if ((payload_size == 1) && (payload[0] == 0xf6)) {
/* 0xf6 is Null in CBOR */
if (k_sem_take(&adc_data_sem, K_MSEC(300)) == 0) {
adc_ch0.loaded_from_cloud = true;
adc_ch1.loaded_from_cloud = true;
k_sem_give(&adc_data_sem);
}
return;
}

uint64_t decoded_ch0 = 0;
uint64_t decoded_ch1 = 0;
bool found_ch0 = 0;
Expand Down
49 changes: 15 additions & 34 deletions src/app_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ int app_state_observe(struct golioth_client *state_client)

app_state_update_actual();

char observe_path[64];

snprintk(observe_path, sizeof(observe_path), "%s/%s", APP_STATE_DESIRED_ENDP,
DESIRED_RESET_KEY);
int err = golioth_lightdb_observe_async(client,
APP_STATE_DESIRED_ENDP,
observe_path,
app_state_desired_handler,
NULL);
if (err) {
LOG_WRN("failed to observe lightdb path: %d", err);
LOG_WRN("failed to observe lightdb path: %d", err);
}
return err;
}
Expand Down Expand Up @@ -168,6 +172,8 @@ int app_state_report_ontime(adc_node_t *ch0, adc_node_t *ch1)
}
}
k_sem_give(&adc_data_sem);
} else {
return -EACCES;
}

return 0;
Expand All @@ -189,41 +195,16 @@ static void app_state_desired_handler(struct golioth_client *client,

LOG_HEXDUMP_DBG(payload, payload_size, APP_STATE_DESIRED_ENDP);

if ((payload_size == 1) && (payload[0] == 0xf6)) {
/* This is `null` in CBOR */
LOG_ERR("Endpoint is null, resetting desired to defaults");
app_state_reset_desired();
if (strncmp(payload, "false", strlen("false")) == 0) {
return;
}

struct zcbor_string key;
bool reset_cumulative;
bool ok;

ZCBOR_STATE_D(decoding_state, 1, payload, payload_size, 1);
ok = zcbor_map_start_decode(decoding_state) &&
zcbor_tstr_decode(decoding_state, &key) &&
zcbor_bool_decode(decoding_state, &reset_cumulative) &&
zcbor_map_end_decode(decoding_state);

if (!ok) {
LOG_ERR("ZCBOR Decoding Error");
LOG_HEXDUMP_ERR(payload, payload_size, "cbor_payload");
} else if (strncmp(payload, "true", strlen("true")) == 0) {
LOG_INF("Request to reset cumulative values received. Processing now.");
reset_cumulative_totals();
app_state_reset_desired();
return;
} else if (strncmp(key.value, DESIRED_RESET_KEY, strlen(DESIRED_RESET_KEY)) != 0){
LOG_ERR("Unexpected key received: %.*s", key.len, key.value);
} else {
LOG_ERR("Desired State Decoding Error");
LOG_HEXDUMP_ERR(payload, payload_size, "desired_state");
app_state_reset_desired();
return;
} else {
LOG_DBG("Decoded: %.*s == %s",
key.len,
key.value,
reset_cumulative ? "true" : "false");
if (reset_cumulative) {
LOG_INF("Request to reset cumulative values received. Processing now.");
reset_cumulative_totals();
app_state_reset_desired();
}
}
}

0 comments on commit cc45e62

Please sign in to comment.