Skip to content

Commit

Permalink
modules: trigger: Add state handling
Browse files Browse the repository at this point in the history
Add states handling to the trigger module:

STATE_INIT: Initializing module
STATE_CONNECTED: Connected to cloud.
 - STATE_FREQUENT_POLL:
    Sending poll triggers every 10 seconds for 10 minutes
    Sending data sample triggers every configured update interval
 - STATE_NORMAL:
    Sending poll triggers every configured update interval
    Sending data sample triggers every configured update interval
STATE_DISCONNECTED: Sending of triggers is blocked

Signed-off-by: Simen S. Røstad <[email protected]>
  • Loading branch information
simensrostad committed Jun 11, 2024
1 parent 3b062c4 commit 86aafc1
Show file tree
Hide file tree
Showing 10 changed files with 450 additions and 57 deletions.
3 changes: 2 additions & 1 deletion app/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ CONFIG_ZBUS=y
CONFIG_ZBUS_MSG_SUBSCRIBER=y
CONFIG_ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_SIZE=32
CONFIG_SMF=y
CONFIG_SMF_ANCESTOR_SUPPORT=y
CONFIG_SMF_INITIAL_TRANSITION=y

# Location
CONFIG_LOCATION=y
Expand Down Expand Up @@ -182,7 +184,6 @@ CONFIG_NET_IF_MAX_IPV6_COUNT=2
CONFIG_NET_IF_MAX_IPV4_COUNT=2

# debug stuff
CONFIG_APP_TRIGGER_TIMEOUT_SECONDS=60
CONFIG_LOG_MODE_IMMEDIATE=y
CONFIG_RESET_ON_FATAL_ERROR=n
CONFIG_THREAD_NAME=y
Expand Down
10 changes: 9 additions & 1 deletion app/src/common/message_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "message_channel.h"

ZBUS_CHAN_DEFINE(TRIGGER_CHAN,
int,
enum trigger_type,
NULL,
NULL,
ZBUS_OBSERVERS(fota, app, battery, location, environmental),
Expand Down Expand Up @@ -65,3 +65,11 @@ ZBUS_CHAN_DEFINE(CLOUD_CHAN,
ZBUS_OBSERVERS(fota, app, location, trigger),
CLOUD_DISCONNECTED
);

ZBUS_CHAN_DEFINE(BUTTON_CHAN,
uint8_t,
NULL,
NULL,
ZBUS_OBSERVERS(trigger),
ZBUS_MSG_INIT(0)
);
8 changes: 7 additions & 1 deletion app/src/common/message_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ enum cloud_status {
CLOUD_DISCONNECTED,
};

enum trigger_type {
TRIGGER_POLL = 0x1,
TRIGGER_DATA_SAMPLE,
};

struct configuration {
bool led_present;
int led_red;
Expand All @@ -60,7 +65,8 @@ ZBUS_CHAN_DECLARE(
LED_CHAN,
CLOUD_CHAN,
FOTA_ONGOING_CHAN,
CONFIG_CHAN
CONFIG_CHAN,
BUTTON_CHAN
);

#ifdef __cplusplus
Expand Down
17 changes: 15 additions & 2 deletions app/src/modules/app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ static void app_task(void)
const uint32_t execution_time_ms = (CONFIG_APP_MODULE_EXEC_TIME_SECONDS_MAX * MSEC_PER_SEC);
const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms);
enum cloud_status cloud_status = 0;
enum trigger_type trigger_type = 0;

LOG_DBG("Application module task started");

Expand Down Expand Up @@ -168,8 +169,20 @@ static void app_task(void)
}
}

if ((&TRIGGER_CHAN == chan) && (cloud_status == CLOUD_CONNECTED)) {
shadow_get(false);
if (&TRIGGER_CHAN == chan) {
err = zbus_chan_read(&TRIGGER_CHAN, &trigger_type, K_FOREVER);
if (err) {
LOG_ERR("zbus_chan_read, error: %d", err);
SEND_FATAL_ERROR();
return;
}

if (trigger_type == TRIGGER_POLL) {
LOG_DBG("Poll trigger received");
LOG_DBG("Getting latest device configuration from device shadow");

shadow_get(false);
}
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions app/src/modules/battery/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ static void battery_task(void)
.model = &battery_model
};
int32_t chg_status;
enum trigger_type trigger_type;

LOG_DBG("Battery module task started");

Expand Down Expand Up @@ -198,8 +199,17 @@ static void battery_task(void)
}

if (&TRIGGER_CHAN == chan) {
LOG_DBG("Trigger received");
sample();
err = zbus_chan_read(&TRIGGER_CHAN, &trigger_type, K_FOREVER);
if (err) {
LOG_ERR("zbus_chan_read, error: %d", err);
SEND_FATAL_ERROR();
return;
}

if (trigger_type == TRIGGER_DATA_SAMPLE) {
LOG_DBG("Data sample trigger received, getting battery data");
sample();
}
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions app/src/modules/environmental/environmental.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ static void environmental_task(void)
const uint32_t wdt_timeout_ms = (CONFIG_APP_ENVIRONMENTAL_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC);
const uint32_t execution_time_ms = (CONFIG_APP_ENVIRONMENTAL_EXEC_TIME_SECONDS_MAX * MSEC_PER_SEC);
const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms);
enum trigger_type trigger_type;

LOG_DBG("Environmental module task started");

Expand All @@ -129,8 +130,17 @@ static void environmental_task(void)
}

if (&TRIGGER_CHAN == chan) {
LOG_DBG("Trigger received");
sample();
err = zbus_chan_read(&TRIGGER_CHAN, &trigger_type, K_FOREVER);
if (err) {
LOG_ERR("zbus_chan_read, error: %d", err);
SEND_FATAL_ERROR();
return;
}

if (trigger_type == TRIGGER_DATA_SAMPLE) {
LOG_DBG("Data sample trigger received, getting environmental data");
sample();
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions app/src/modules/fota/fota.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ void fota_callback(const struct zbus_channel *chan)
}
if (&TRIGGER_CHAN == chan && fota_initialized)
{

const enum trigger_type *trigger_type = zbus_chan_const_msg(chan);

if (*trigger_type != TRIGGER_POLL) {
return;
}

/* tell the rest of the app not to disturb */
bool fota_ongoing = true;
err = zbus_chan_pub(&FOTA_ONGOING_CHAN, &fota_ongoing, K_NO_WAIT);
Expand Down
14 changes: 12 additions & 2 deletions app/src/modules/location/location.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void location_task(void)
int task_wdt_id;
const uint32_t wdt_timeout_ms = (CONFIG_APP_LOCATION_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC);
const k_timeout_t zbus_timeout = K_SECONDS(CONFIG_APP_LOCATION_ZBUS_TIMEOUT_SECONDS);
enum trigger_type trigger_type = 0;

LOG_DBG("Location module task started");

Expand Down Expand Up @@ -107,8 +108,17 @@ void location_task(void)
}

if (&TRIGGER_CHAN == chan) {
LOG_DBG("Trigger received");
trigger_location_update();
err = zbus_chan_read(&TRIGGER_CHAN, &trigger_type, K_FOREVER);
if (err) {
LOG_ERR("zbus_chan_read, error: %d", err);
SEND_FATAL_ERROR();
return;
}

if (trigger_type == TRIGGER_DATA_SAMPLE) {
LOG_DBG("Data sample trigger received, getting location");
trigger_location_update();
}
}
}
}
Expand Down
Loading

0 comments on commit 86aafc1

Please sign in to comment.