From 86aafc1da205f188de74aef83168166bae8ee267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simen=20S=2E=20R=C3=B8stad?= Date: Thu, 6 Jun 2024 13:09:11 +0200 Subject: [PATCH] modules: trigger: Add state handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- app/prj.conf | 3 +- app/src/common/message_channel.c | 10 +- app/src/common/message_channel.h | 8 +- app/src/modules/app/app.c | 17 +- app/src/modules/battery/battery.c | 14 +- app/src/modules/environmental/environmental.c | 14 +- app/src/modules/fota/fota.c | 7 + app/src/modules/location/location.c | 14 +- app/src/modules/trigger/trigger.c | 408 ++++++++++++++++-- tests/module/environmental/src/main.c | 12 +- 10 files changed, 450 insertions(+), 57 deletions(-) diff --git a/app/prj.conf b/app/prj.conf index d728e51d..59ae02e8 100644 --- a/app/prj.conf +++ b/app/prj.conf @@ -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 @@ -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 diff --git a/app/src/common/message_channel.c b/app/src/common/message_channel.c index af333bc6..064104f6 100644 --- a/app/src/common/message_channel.c +++ b/app/src/common/message_channel.c @@ -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), @@ -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) +); diff --git a/app/src/common/message_channel.h b/app/src/common/message_channel.h index c8ad3eda..1376deac 100644 --- a/app/src/common/message_channel.h +++ b/app/src/common/message_channel.h @@ -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; @@ -60,7 +65,8 @@ ZBUS_CHAN_DECLARE( LED_CHAN, CLOUD_CHAN, FOTA_ONGOING_CHAN, - CONFIG_CHAN + CONFIG_CHAN, + BUTTON_CHAN ); #ifdef __cplusplus diff --git a/app/src/modules/app/app.c b/app/src/modules/app/app.c index c18c0cdb..817df89b 100644 --- a/app/src/modules/app/app.c +++ b/app/src/modules/app/app.c @@ -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"); @@ -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); + } } } } diff --git a/app/src/modules/battery/battery.c b/app/src/modules/battery/battery.c index 68381cec..e39e39f8 100644 --- a/app/src/modules/battery/battery.c +++ b/app/src/modules/battery/battery.c @@ -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"); @@ -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(); + } } } } diff --git a/app/src/modules/environmental/environmental.c b/app/src/modules/environmental/environmental.c index 3eda6325..e68328c3 100644 --- a/app/src/modules/environmental/environmental.c +++ b/app/src/modules/environmental/environmental.c @@ -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"); @@ -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(); + } } } } diff --git a/app/src/modules/fota/fota.c b/app/src/modules/fota/fota.c index bf5db1c7..f29b297d 100644 --- a/app/src/modules/fota/fota.c +++ b/app/src/modules/fota/fota.c @@ -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); diff --git a/app/src/modules/location/location.c b/app/src/modules/location/location.c index 60709382..344416da 100644 --- a/app/src/modules/location/location.c +++ b/app/src/modules/location/location.c @@ -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"); @@ -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(); + } } } } diff --git a/app/src/modules/trigger/trigger.c b/app/src/modules/trigger/trigger.c index 232b23a7..25dcff35 100644 --- a/app/src/modules/trigger/trigger.c +++ b/app/src/modules/trigger/trigger.c @@ -9,64 +9,386 @@ #include #include #include +#include #include "message_channel.h" /* Register log module */ LOG_MODULE_REGISTER(trigger, CONFIG_APP_TRIGGER_LOG_LEVEL); -#define MSG_SEND_TIMEOUT_SECONDS 1 +/* Trigger interval in the frequent poll state */ +#define FREQUENT_POLL_TRIGGER_INTERVAL_SEC 10 + +/* Duration that the trigger module is in the frequent poll state */ +#define FREQUENT_POLL_DURATION_INTERVAL_SEC 600 /* Forward declarations */ -static void trigger_work_fn(struct k_work *work); +static void trigger_poll_work_fn(struct k_work *work); +static void trigger_data_sample_work_fn(struct k_work *work); +static const struct smf_state states[]; + +/* Delayable work used to schedule triggers for polling */ +static K_WORK_DELAYABLE_DEFINE(trigger_poll_work, trigger_poll_work_fn); +/* Delayable work used to schedule triggers for data sampling */ +static K_WORK_DELAYABLE_DEFINE(trigger_data_sample_work, trigger_data_sample_work_fn); + +/* Timer used to exit the frequent poll state after 10 minutes */ +static void frequent_poll_state_duration_timer_handler(struct k_timer * timer_id); +static K_TIMER_DEFINE(frequent_poll_duration_timer, frequent_poll_state_duration_timer_handler, NULL); + +/* Zephyr SMF states */ +enum state { + STATE_INIT, + STATE_CONNECTED, + /* Sub-state to STATE_CONNECTED */ + STATE_FREQUENT_POLL, + /* Sub-state to STATE_CONNECTED */ + STATE_NORMAL, + STATE_DISCONNECTED +}; + +/* Private channel used to signal when the duration in the frequent poll state expires */ +ZBUS_CHAN_DECLARE(PRIV_TRIGGER_CHAN); +ZBUS_CHAN_DEFINE(PRIV_TRIGGER_CHAN, + int, /* Unused */ + NULL, + NULL, + ZBUS_OBSERVERS(trigger), + ZBUS_MSG_INIT(0) +); + +/* User defined state object. + * Used to transfer data between state changes. + */ +struct s_object { + /* This must be first */ + struct smf_ctx ctx; + + /* Last channel type that a message was received on */ + const struct zbus_channel *chan; + + /* Set default update interval */ + uint64_t update_interval_sec; -/* Delayable work used to schedule triggers. */ -static K_WORK_DELAYABLE_DEFINE(trigger_work, trigger_work_fn); + /* Button number */ + uint8_t button_number; -/* Set default update interval */ -k_timeout_t update_interval = K_SECONDS(CONFIG_APP_TRIGGER_TIMEOUT_SECONDS); + /* Cloud status */ + enum cloud_status status; -static void trigger_work_fn(struct k_work *work) +}; + +/* SMF state object variable */ +static struct s_object state_object; + +static void trigger_send(enum trigger_type type, k_timeout_t timeout) { - int err; - int not_used = -1; - bool fota_ongoing = true; + enum trigger_type trigger_type = type; + + int err = zbus_chan_pub(&TRIGGER_CHAN, &trigger_type, timeout); - err = zbus_chan_read(&FOTA_ONGOING_CHAN, &fota_ongoing, K_NO_WAIT); if (err) { - LOG_ERR("zbus_chan_read, error: %d", err); + LOG_ERR("zbus_chan_pub, error: %d", err); SEND_FATAL_ERROR(); return; } +} - if (fota_ongoing) { - LOG_DBG("FOTA ongoing, skipping trigger message"); - return; - } +/* Handler called when the frequent poll duration timer expires */ +static void frequent_poll_state_duration_timer_handler(struct k_timer * timer_id) +{ + ARG_UNUSED(timer_id); - LOG_DBG("Sending trigger message"); + int unused = 0; + int err; + + LOG_DBG("Frequent poll duration timer expired"); - err = zbus_chan_pub(&TRIGGER_CHAN, ¬_used, K_SECONDS(MSG_SEND_TIMEOUT_SECONDS)); + err = zbus_chan_pub(&PRIV_TRIGGER_CHAN, &unused, K_NO_WAIT); if (err) { LOG_ERR("zbus_chan_pub, error: %d", err); SEND_FATAL_ERROR(); return; } +} + +/* Delayed work used to signal poll triggers to the rest of the system */ +static void trigger_poll_work_fn(struct k_work *work) +{ + ARG_UNUSED(work); - k_work_reschedule(&trigger_work, update_interval); + LOG_DBG("Sending trigger poll message"); + LOG_DBG("trigger poll work timeout: %d seconds", FREQUENT_POLL_TRIGGER_INTERVAL_SEC); + + trigger_send(TRIGGER_POLL, K_SECONDS(1)); + k_work_reschedule(&trigger_poll_work, K_SECONDS(FREQUENT_POLL_TRIGGER_INTERVAL_SEC)); } +/* Delayed work used to signal data sample triggers to the rest of the system */ +static void trigger_data_sample_work_fn(struct k_work *work) +{ + ARG_UNUSED(work); + + LOG_DBG("Sending trigger data sample message"); + LOG_DBG("trigger data sample work timeout: %lld seconds", state_object.update_interval_sec); + + trigger_send(TRIGGER_DATA_SAMPLE, K_SECONDS(1)); + k_work_reschedule(&trigger_data_sample_work, K_SECONDS(state_object.update_interval_sec)); +} + +/* Button handler called when a user pushes a button */ static void button_handler(uint32_t button_states, uint32_t has_changed) { - if (has_changed & button_states) { - trigger_work_fn(NULL); + int err; + uint8_t button_number = 1; + + if (has_changed & button_states & DK_BTN1_MSK) { + LOG_DBG("Button 1 pressed!"); + + err = zbus_chan_pub(&BUTTON_CHAN, &button_number, K_SECONDS(1)); + if (err) { + LOG_ERR("zbus_chan_pub, error: %d", err); + SEND_FATAL_ERROR(); + return; + } + } +} + +static void frequent_poll_duration_timer_start(void) +{ + LOG_DBG("Starting frequent poll duration timer: %d seconds", + FREQUENT_POLL_DURATION_INTERVAL_SEC); + + k_timer_start(&frequent_poll_duration_timer, + K_SECONDS(FREQUENT_POLL_DURATION_INTERVAL_SEC), K_NO_WAIT); +} + +/* Zephyr State Machine framework handlers */ + +/* HSM states: + * + * 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 + */ + +/* STATE_INIT */ + +static void init_entry(void *o) +{ + ARG_UNUSED(o); + + LOG_DBG("init_entry"); + + int err = dk_buttons_init(button_handler); + + if (err) { + LOG_ERR("dk_buttons_init, error: %d", err); + SEND_FATAL_ERROR(); + return; + } +} + +static void init_run(void *o) +{ + struct s_object *user_object = o; + + LOG_DBG("init_run"); + + if ((user_object->chan == &CLOUD_CHAN) && (user_object->status == CLOUD_CONNECTED)) { + smf_set_state(SMF_CTX(&state_object), &states[STATE_CONNECTED]); + return; + } +} + +/* STATE_CONNECTED */ + +static void connected_run(void *o) +{ + struct s_object *user_object = o; + + LOG_DBG("connected_run"); + + if ((user_object->chan == &CLOUD_CHAN) && (user_object->status == CLOUD_DISCONNECTED)) { + smf_set_state(SMF_CTX(&state_object), &states[STATE_DISCONNECTED]); + return; + } +} + +/* STATE_FREQUENT_POLL */ + +static void frequent_poll_entry(void *o) +{ + struct s_object *user_object = o; + + LOG_DBG("frequent_poll_entry"); + + LOG_DBG("trigger poll work timeout: %d seconds", FREQUENT_POLL_TRIGGER_INTERVAL_SEC); + LOG_DBG("trigger data sample work timeout: %lld seconds", user_object->update_interval_sec); + + frequent_poll_duration_timer_start(); + trigger_send(TRIGGER_DATA_SAMPLE, K_SECONDS(1)); + k_work_reschedule(&trigger_poll_work, K_SECONDS(FREQUENT_POLL_TRIGGER_INTERVAL_SEC)); + k_work_reschedule(&trigger_data_sample_work, K_SECONDS(user_object->update_interval_sec)); +} + +static void frequent_poll_run(void *o) +{ + struct s_object *user_object = o; + + LOG_DBG("frequent_poll_run"); + + if (user_object->chan == &BUTTON_CHAN) { + LOG_DBG("Button %d pressed in frequent poll state, restarting duration timer", + user_object->button_number); + + frequent_poll_duration_timer_start(); + trigger_send(TRIGGER_DATA_SAMPLE, K_SECONDS(1)); + } else if (user_object->chan == &CONFIG_CHAN) { + LOG_DBG("Configuration received, restarting duration timer"); + + frequent_poll_duration_timer_start(); + trigger_send(TRIGGER_DATA_SAMPLE, K_SECONDS(1)); + } else if (user_object->chan == &PRIV_TRIGGER_CHAN) { + LOG_DBG("Frequent poll duration timer expired, going into normal state"); + smf_set_state(SMF_CTX(&state_object), &states[STATE_NORMAL]); + return; + } +} + +static void frequent_poll_exit(void *o) +{ + ARG_UNUSED(o); + + LOG_DBG("frequent_poll_exit"); + LOG_DBG("Clearing delayed work and frequent poll duration timer"); + + k_timer_stop(&frequent_poll_duration_timer); + k_work_cancel_delayable(&trigger_poll_work); + k_work_cancel_delayable(&trigger_data_sample_work); +} + +/* STATE_NORMAL */ + +static void normal_entry(void *o) +{ + struct s_object *user_object = o; + + LOG_DBG("normal_entry"); + + LOG_DBG("trigger poll work timeout: %lld seconds", user_object->update_interval_sec); + LOG_DBG("trigger data sample work timeout: %lld seconds", user_object->update_interval_sec); + + k_work_reschedule(&trigger_poll_work, K_SECONDS(user_object->update_interval_sec)); + k_work_reschedule(&trigger_data_sample_work, K_SECONDS(user_object->update_interval_sec)); +} + +static void normal_run(void *o) +{ + struct s_object *user_object = o; + + LOG_DBG("normal_run"); + + if (user_object->chan == &BUTTON_CHAN) { + LOG_DBG("Button %d pressed in normal state, going into frequent poll state", + user_object->button_number); + + smf_set_state(SMF_CTX(&state_object), &states[STATE_FREQUENT_POLL]); + } else if (user_object->chan == &CONFIG_CHAN) { + LOG_DBG("Configuration received in normal state, going into frequent poll state"); + + smf_set_state(SMF_CTX(&state_object), &states[STATE_FREQUENT_POLL]); + } +} + +static void normal_exit(void *o) +{ + ARG_UNUSED(o); + + LOG_DBG("normal_exit"); + LOG_DBG("Clearning delayed work"); + + k_work_cancel_delayable(&trigger_poll_work); + k_work_cancel_delayable(&trigger_data_sample_work); +} + +/* STATE_DISCONNECTED */ + +static void disconnected_run(void *o) +{ + ARG_UNUSED(o); + + LOG_DBG("disconnected_run"); + + struct s_object *user_object = o; + + if (user_object->chan == &CLOUD_CHAN && (user_object->status == CLOUD_CONNECTED)) { + smf_set_state(SMF_CTX(&state_object), &states[STATE_CONNECTED]); + return; } } +/* Construct state table */ +static const struct smf_state states[] = { + [STATE_INIT] = SMF_CREATE_STATE( + init_entry, + init_run, + NULL, + NULL, + NULL + ), + [STATE_CONNECTED] = SMF_CREATE_STATE( + NULL, + connected_run, + NULL, + NULL, + &states[STATE_FREQUENT_POLL] + ), + [STATE_FREQUENT_POLL] = SMF_CREATE_STATE( + frequent_poll_entry, + frequent_poll_run, + frequent_poll_exit, + &states[STATE_CONNECTED], + NULL + ), + [STATE_NORMAL] = SMF_CREATE_STATE( + normal_entry, + normal_run, + normal_exit, + &states[STATE_CONNECTED], + NULL + ), + [STATE_DISCONNECTED] = SMF_CREATE_STATE( + NULL, + disconnected_run, + NULL, + NULL, + NULL + ) +}; + +/* Function called when there is a message received on a channel that the module listens to */ void trigger_callback(const struct zbus_channel *chan) { + int err; + + if ((chan != &CONFIG_CHAN) && + (chan != &CLOUD_CHAN) && + (chan != &BUTTON_CHAN) && + (chan != &PRIV_TRIGGER_CHAN)) { + LOG_ERR("Unknown channel"); + return; + } + + /* Update the state object with the channel that the message was received on */ + state_object.chan = chan; + + /* Copy corresponding data to the state object depending on the incoming channel */ if (&CONFIG_CHAN == chan) { - /* Get update interval configuration from channel. */ const struct configuration *config = zbus_chan_const_msg(chan); if (config->config_present == false) { @@ -74,36 +396,42 @@ void trigger_callback(const struct zbus_channel *chan) return; } - LOG_DBG("New update interval: %lld", config->update_interval); + state_object.update_interval_sec = config->update_interval; + } - update_interval = K_SECONDS(config->update_interval); + if (&CLOUD_CHAN == chan) { + const enum cloud_status *status = zbus_chan_const_msg(chan); - /* Reschedule work */ - k_work_reschedule(&trigger_work, update_interval); + state_object.status = *status; } - if (&CLOUD_CHAN == chan) { - LOG_DBG("Cloud connection status received"); + if (&BUTTON_CHAN == chan) { + const int *button_number = zbus_chan_const_msg(chan); - const enum cloud_status *status = zbus_chan_const_msg(chan); + state_object.button_number = (uint8_t)*button_number; + } - if (*status == CLOUD_CONNECTED) { - LOG_DBG("Cloud connected, starting trigger"); - k_work_reschedule(&trigger_work, update_interval); - } else { - LOG_DBG("Cloud disconnected, stopping trigger"); - k_work_cancel_delayable(&trigger_work); - } + /* State object updated, run SMF */ + err = smf_run_state(SMF_CTX(&state_object)); + if (err) { + LOG_ERR("smf_run_state, error: %d", err); + SEND_FATAL_ERROR(); + return; } } -ZBUS_LISTENER_DEFINE(trigger, trigger_callback); - static int trigger_init(void) { - __ASSERT((dk_buttons_init(button_handler) == 0), "dk_buttons_init failure"); + /* Set default update interval */ + state_object.update_interval_sec = CONFIG_APP_TRIGGER_TIMEOUT_SECONDS; + + smf_set_initial(SMF_CTX(&state_object), &states[STATE_INIT]); return 0; } -SYS_INIT(trigger_init, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY); +/* Define a ZBUS listener for this module */ +ZBUS_LISTENER_DEFINE(trigger, trigger_callback); + +/* Initialize module at SYS_INIT() */ +SYS_INIT(trigger_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); diff --git a/tests/module/environmental/src/main.c b/tests/module/environmental/src/main.c index 4b031102..cc3d1769 100644 --- a/tests/module/environmental/src/main.c +++ b/tests/module/environmental/src/main.c @@ -104,11 +104,11 @@ void setUp(void) void test_all_zeroes(void) { - int not_used = -1; + enum trigger_type trigger_type = TRIGGER_DATA_SAMPLE; int err; /* send trigger */ - err = zbus_chan_pub(&TRIGGER_CHAN, ¬_used, K_SECONDS(1)); + err = zbus_chan_pub(&TRIGGER_CHAN, &trigger_type, K_SECONDS(1)); TEST_ASSERT_EQUAL(0, err); /* Allow the test thread to sleep so that the DUT's thread is allowed to run. */ @@ -120,14 +120,14 @@ void test_all_zeroes(void) void test_only_timestamp(void) { - int not_used = -1; + enum trigger_type trigger_type = TRIGGER_DATA_SAMPLE; int err; date_time_uptime_to_unix_time_ms_fake.custom_fake = date_time_uptime_to_unix_time_ms_custom_fake; /* send trigger */ - err = zbus_chan_pub(&TRIGGER_CHAN, ¬_used, K_SECONDS(1)); + err = zbus_chan_pub(&TRIGGER_CHAN, &trigger_type, K_SECONDS(1)); TEST_ASSERT_EQUAL(0, err); /* Allow the test thread to sleep so that the DUT's thread is allowed to run. */ @@ -140,7 +140,7 @@ void test_only_timestamp(void) void test_common_case(void) { struct gas_sensor_dummy_data *data = sensor_dev->data; - int not_used = -1; + enum trigger_type trigger_type = TRIGGER_DATA_SAMPLE; int err; data->temperature = 25.5; @@ -153,7 +153,7 @@ void test_common_case(void) date_time_uptime_to_unix_time_ms_custom_fake; /* send trigger */ - err = zbus_chan_pub(&TRIGGER_CHAN, ¬_used, K_SECONDS(1)); + err = zbus_chan_pub(&TRIGGER_CHAN, &trigger_type, K_SECONDS(1)); TEST_ASSERT_EQUAL(0, err); /* Allow the test thread to sleep so that the DUT's thread is allowed to run. */