Skip to content

Commit

Permalink
modules: app: Use task watchdog
Browse files Browse the repository at this point in the history
Use task watchdog. This will reset the device if the application task
is blocked for a configurable amount of time.

and, minor cleanup.

Signed-off-by: Simen S. Røstad <[email protected]>
Signed-off-by: Balaji Srinivasan <[email protected]>
  • Loading branch information
simensrostad committed May 29, 2024
1 parent 042e06c commit f3fd774
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 36 deletions.
3 changes: 3 additions & 0 deletions app/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,6 @@ CONFIG_APP_LOG_LEVEL_DBG=y
# Fuel Gage
CONFIG_NRF_FUEL_GAUGE=y
CONFIG_NPM1300_CHARGER=y

# Task Watchdog
CONFIG_TASK_WDT=y
4 changes: 2 additions & 2 deletions app/src/common/message_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ struct payload {
};

enum network_status {
NETWORK_DISCONNECTED,
NETWORK_DISCONNECTED = 0x1,
NETWORK_CONNECTED,
};

enum cloud_status {
CLOUD_CONNECTED,
CLOUD_CONNECTED = 0x1,
CLOUD_DISCONNECTED,
};

Expand Down
4 changes: 4 additions & 0 deletions app/src/modules/app/Kconfig.app
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ config APP_MODULE_MESSAGE_QUEUE_SIZE
help
ZBus subscriber message queue size.

config APP_MODULE_WATCDHOG_TIMEOUT_SECONDS
int "Watchdog timeout seconds"
default 60

endmenu
98 changes: 64 additions & 34 deletions app/src/modules/app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <zephyr/logging/log.h>
#include <zephyr/zbus/zbus.h>
#include <simple_config/simple_config.h>
#include <zephyr/task_wdt/task_wdt.h>

#include "message_channel.h"

Expand Down Expand Up @@ -48,18 +49,6 @@ int config_cb(const char *key, const struct simple_config_val *val)
return -EINVAL;
}

static bool check_cloud_connection(void)
{
enum cloud_status cloud_status;
int err = zbus_chan_read(&CLOUD_CHAN, &cloud_status, K_FOREVER);

if (err) {
LOG_ERR("zbus_chan_read, error: %d", err);
SEND_FATAL_ERROR();
}
return cloud_status == CLOUD_CONNECTED;
}

static void init_app_settings(void)
{
struct simple_config_val val = {.type = SIMPLE_CONFIG_VAL_BOOL, .val._bool = true};
Expand All @@ -70,43 +59,75 @@ static void init_app_settings(void)
}
}

static void wait_for_cloud_connection(void)
static void task_wdt_callback(int channel_id, void *user_data)
{
const struct zbus_channel *chan;
LOG_ERR("Watchdog expired, Channel: %d, Thread: %s",
channel_id, k_thread_name_get((k_tid_t)user_data));

while (!zbus_sub_wait(&app, &chan, K_FOREVER)) {
if (&CLOUD_CHAN == chan) {
if (check_cloud_connection()) {
return;
}
}
}
SEND_FATAL_ERROR();
}

static void app_task(void)
{
const struct zbus_channel *chan;
int err;
const struct zbus_channel *chan = NULL;
int task_wdt_id;
const uint32_t wdt_timeout_ms = CONFIG_APP_MODULE_WATCDHOG_TIMEOUT_SECONDS * MSEC_PER_SEC;
const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - 100);
enum cloud_status cloud_status = 0;

LOG_DBG("Application module task started");

task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get());

/* Set up callback for runtime config changes from cloud */
simple_config_set_callback(config_cb);

/* Wait for cloud connection */
wait_for_cloud_connection();

/* Set initial settting values (can be skipped if cloud initializes shadow) */
init_app_settings();

/* Initial config update */
simple_config_update();
while (true) {
err = zbus_sub_wait(&app, &chan, zbus_wait_ms);
if (err && err != -EAGAIN) {
LOG_ERR("zbus_sub_wait, error: %d", err);
SEND_FATAL_ERROR();
return;
}

err = task_wdt_feed(task_wdt_id);
if (err) {
LOG_ERR("task_wdt_feed, error: %d", err);
SEND_FATAL_ERROR();
return;
}

if (err == -EAGAIN) {
continue;
}

if ((&CLOUD_CHAN == chan)) {
LOG_DBG("Cloud connection status received");

err = zbus_chan_read(&CLOUD_CHAN, &cloud_status, K_FOREVER);
if (err) {
LOG_ERR("zbus_chan_read, error: %d", err);
SEND_FATAL_ERROR();
return;
}

while (!zbus_sub_wait(&app, &chan, K_FOREVER)) {
if (&CLOUD_CHAN == chan) {
if (!check_cloud_connection()) {
wait_for_cloud_connection();
if (cloud_status == CLOUD_CONNECTED) {
LOG_INF("Cloud connected");
LOG_INF("Getting latest device configuration fron cloud");

simple_config_update();
} else {
LOG_INF("Cloud disconnected");
}
} else if (&TRIGGER_CHAN == chan) {
LOG_DBG("Trigger received");
/* handle incoming cloud settings */
}

if ((&TRIGGER_CHAN == chan) && (cloud_status == CLOUD_CONNECTED)) {
LOG_INF("Getting latest device configuration fron cloud");

simple_config_update();
}
}
Expand All @@ -115,3 +136,12 @@ static void app_task(void)
K_THREAD_DEFINE(app_task_id,
CONFIG_APP_MODULE_THREAD_STACK_SIZE,
app_task, NULL, NULL, NULL, 3, 0, 0);

static int watchdog_init(void)
{
__ASSERT((task_wdt_init(NULL) == 0), "Task watchdog init failure");

return 0;
}

SYS_INIT(watchdog_init, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY);

0 comments on commit f3fd774

Please sign in to comment.