Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules: Block triggers during location search #284

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ west flash --erase
| LED effect | Color | Meaning | Duration (seconds) |
|----------------|------------|----------------------------------------------|-----------------------------------------------------|
| Blinking | Yellow | Device is (re-)connecting to the LTE network | NA |
| Blinking | Green | GNSS searching | NA |
| Blinking | Green | Location searching | NA |
| Blinking slow | Blue | Device is actively polling cloud | 10 minutes after last config update or button press |
| Solid | Configured | Device has received a LED configuration | NA |
| Blinking rapid | Red | Fatal error, the device will reboot | NA |
Expand Down
4 changes: 2 additions & 2 deletions app/src/common/message_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ enum time_status {
#define MSG_TO_TIME_STATUS(_msg) (*(const enum time_status *)_msg)

enum location_status {
GNSS_ENABLED = 0x1,
GNSS_DISABLED,
LOCATION_SEARCH_STARTED = 0x1,
LOCATION_SEARCH_DONE,
};

enum error_type {
Expand Down
22 changes: 10 additions & 12 deletions app/src/modules/led/led.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ char *led_state_name(enum led_state state)
return "LED_CONFIGURED";
case LED_POLL_MODE:
return "LED_POLL_MODE";
case LED_GNSS_SEARCHING:
return "LED_GNSS_SEARCHING";
case LED_LOCATION_SEARCHING:
return "LED_LOCATION_SEARCHING";
case LED_LTE_CONNECTING:
return "LED_LTE_CONNECTING";
case LED_ERROR_SYSTEM_FAULT:
Expand Down Expand Up @@ -226,9 +226,9 @@ static void on_network_disconnected(void)
* - STATE_RUNNING: Initial state, the module is running
* - STATE_LED_SET: LED is configured by the user
* - STATE_LED_NOT_SET: LED is not configured by the user, operational pattern is displayed
* - STATE_POLL: Poll pattern or GNSS search pattern is displayed depending on
* - STATE_POLL: Poll pattern or location search pattern is displayed depending on
the location status
* - STATE_NORMAL: Led is off or GNSS search pattern is displayed depending on
* - STATE_NORMAL: Led is off or location search pattern is displayed depending on
* the location status
* - STATE_ERROR: An error has occured
*/
Expand Down Expand Up @@ -337,18 +337,17 @@ static void poll_running(void *o)
return;
}

if ((&LOCATION_CHAN == user_object->chan) && user_object->location_status == GNSS_ENABLED) {
if ((&LOCATION_CHAN == user_object->chan) && user_object->location_status == LOCATION_SEARCH_STARTED) {

transition_list_clear();
transition_list_append(LED_GNSS_SEARCHING, HOLD_FOREVER, 0, 0, 0);
transition_list_append(LED_LOCATION_SEARCHING, HOLD_FOREVER, 0, 0, 0);

k_work_reschedule(&led_pattern_update_work, K_NO_WAIT);
return;
}

if ((&LOCATION_CHAN == user_object->chan) && user_object->location_status == GNSS_DISABLED) {
if ((&LOCATION_CHAN == user_object->chan) && user_object->location_status == LOCATION_SEARCH_DONE) {

/* When GNSS is disabled, we just reenter the same state */
smf_set_state(SMF_CTX(user_object), &states[STATE_POLL]);
return;
}
Expand Down Expand Up @@ -379,18 +378,17 @@ static void normal_running(void *o)
return;
}

if ((&LOCATION_CHAN == user_object->chan) && user_object->location_status == GNSS_ENABLED) {
if ((&LOCATION_CHAN == user_object->chan) && user_object->location_status == LOCATION_SEARCH_STARTED) {

transition_list_clear();
transition_list_append(LED_GNSS_SEARCHING, HOLD_FOREVER, 0, 0, 0);
transition_list_append(LED_LOCATION_SEARCHING, HOLD_FOREVER, 0, 0, 0);

k_work_reschedule(&led_pattern_update_work, K_NO_WAIT);
return;
}

if ((&LOCATION_CHAN == user_object->chan) && user_object->location_status == GNSS_DISABLED) {
if ((&LOCATION_CHAN == user_object->chan) && user_object->location_status == LOCATION_SEARCH_DONE) {

/* When GNSS is disabled, we just reenter the same state */
smf_set_state(SMF_CTX(user_object), &states[STATE_NORMAL]);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/modules/led/led.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extern "C" {
#define LED_COLOR_WHITE LED_COLOR(LED_MAX, LED_MAX, LED_MAX)

#define LED_LTE_CONNECTING_COLOR LED_COLOR_YELLOW
#define LED_GNSS_SEARCHING_COLOR LED_COLOR_GREEN
#define LED_LOCATION_SEARCHING_COLOR LED_COLOR_GREEN
#define LED_POLL_MODE_COLOR LED_COLOR_BLUE
#define LED_ERROR_SYSTEM_FAULT_COLOR LED_COLOR_RED
#define LED_OFF_COLOR LED_COLOR_OFF
Expand All @@ -63,7 +63,7 @@ extern "C" {
enum led_state {
LED_LTE_CONNECTING,
LED_POLL_MODE,
LED_GNSS_SEARCHING,
LED_LOCATION_SEARCHING,
LED_ERROR_SYSTEM_FAULT,
LED_ERROR_IRRECOVERABLE,
LED_CONFIGURED,
Expand Down
4 changes: 2 additions & 2 deletions app/src/modules/led/led_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ static const struct led_effect effect[] = {
LED_OFF_PERIOD_LONG,
LED_POLL_MODE_COLOR
),
[LED_GNSS_SEARCHING] = LED_EFFECT_LED_BREATHE(
[LED_LOCATION_SEARCHING] = LED_EFFECT_LED_BREATHE(
LED_ON_PERIOD_NORMAL,
LED_OFF_PERIOD_NORMAL,
LED_GNSS_SEARCHING_COLOR
LED_LOCATION_SEARCHING_COLOR
),
[LED_ERROR_SYSTEM_FAULT] = LED_EFFECT_LED_BREATHE(
LED_ON_PERIOD_ERROR,
Expand Down
26 changes: 4 additions & 22 deletions app/src/modules/location/location.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,6 @@ static void apply_gnss_time(const struct nrf_modem_gnss_pvt_data_frame *pvt_data

static void location_event_handler(const struct location_event_data *event_data)
{
static bool is_method_gnss = false;

switch (event_data->id) {
case LOCATION_EVT_LOCATION:
LOG_DBG("Got location: lat: %f, lon: %f, acc: %f",
Expand All @@ -239,35 +237,19 @@ static void location_event_handler(const struct location_event_data *event_data)
LOG_WRN("Got GNSS location without valid time data");
}

status_send(GNSS_DISABLED);
is_method_gnss = false;
}
break;
case LOCATION_EVT_FALLBACK:

if (is_method_gnss) {
is_method_gnss = false;

status_send(GNSS_DISABLED);
}

status_send(LOCATION_SEARCH_DONE);
break;
case LOCATION_EVT_STARTED:

if (event_data->method == LOCATION_METHOD_GNSS) {
status_send(GNSS_ENABLED);
is_method_gnss = true;
}

break;
case LOCATION_EVT_RESULT_UNKNOWN:
LOG_DBG("Getting location completed with undefined result");
status_send(LOCATION_SEARCH_STARTED);
break;
case LOCATION_EVT_TIMEOUT:
LOG_DBG("Getting location timed out");
status_send(LOCATION_SEARCH_DONE);
break;
case LOCATION_EVT_ERROR:
LOG_WRN("Getting location failed");
SEND_FATAL_ERROR();
break;
default:
LOG_DBG("Getting location: Unknown event %d", event_data->id);
Expand Down
26 changes: 13 additions & 13 deletions app/src/modules/trigger/trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ struct s_object {
*/
uint64_t shadow_poll_interval_used_sec;

/* GNSS status, used to block trigger events */
bool gnss;
/* Location search status, used to block trigger events */
bool location_search;

/* Button number */
uint8_t button_number;
Expand Down Expand Up @@ -222,7 +222,7 @@ static void frequent_poll_duration_timer_stop(void)
* - STATE_FREQUENT_POLL: Sending poll and data sample triggers every 20 seconds for 10 minutes
* - STATE_NORMAL: Sending poll triggers every configured update interval
* Sending data sample triggers every configured update interval
* - STATE_BLOCKED: Sending of triggers is blocked due to GNSS activity
* - STATE_BLOCKED: Sending of triggers is blocked due to an active location search
* STATE_DISCONNECTED: Sending of triggers is blocked due to being disconnected
*
*
Expand Down Expand Up @@ -287,8 +287,8 @@ static void blocked_run(void *o)

LOG_DBG("blocked_run");

if (user_object->chan == &LOCATION_CHAN && !user_object->gnss) {
LOG_DBG("GNSS disabled");
if (user_object->chan == &LOCATION_CHAN && !user_object->location_search) {
LOG_DBG("Location search done");

if (user_object->trigger_mode == TRIGGER_MODE_NORMAL) {
LOG_DBG("Going into normal state");
Expand All @@ -301,7 +301,7 @@ static void blocked_run(void *o)
} else if (user_object->chan == &PRIV_TRIGGER_CHAN) {
/* Frequent poll duration timer expired. Since the current state is BLOCKED,
* continue to remain in this state but only change the trigger mode so that
* when the GNSS is disabled, the state machine transitions into Normal mode.
* when the location search is done, the state machine transitions into Normal mode.
*/
LOG_DBG("Changing the trigger mode in state object ");
user_object->trigger_mode = TRIGGER_MODE_NORMAL;
Expand Down Expand Up @@ -334,8 +334,8 @@ static void frequent_poll_entry(void *o)
user_object->shadow_poll_interval_used_sec = FREQUENT_POLL_SHADOW_POLL_TRIGGER_INTERVAL_SEC;
user_object->trigger_mode = TRIGGER_MODE_POLL;

if (user_object->chan == &LOCATION_CHAN && !user_object->gnss) {
LOG_DBG("GNSS disabled");
if (user_object->chan == &LOCATION_CHAN && !user_object->location_search) {
LOG_DBG("Location search done");

k_work_reschedule(&trigger_work, K_SECONDS(user_object->update_interval_used_sec));
k_work_reschedule(&trigger_shadow_poll_work,
Expand Down Expand Up @@ -375,8 +375,8 @@ static void frequent_poll_run(void *o)

LOG_DBG("frequent_poll_run");

if (user_object->chan == &LOCATION_CHAN && user_object->gnss) {
LOG_DBG("GNSS enabled, going into blocked state");
if (user_object->chan == &LOCATION_CHAN && user_object->location_search) {
LOG_DBG("Location search started, going into blocked state");

smf_set_state(SMF_CTX(&state_object), &states[STATE_BLOCKED]);
return;
Expand Down Expand Up @@ -457,8 +457,8 @@ static void normal_run(void *o)

LOG_DBG("normal_run");

if (user_object->chan == &LOCATION_CHAN && user_object->gnss) {
LOG_DBG("GNSS enabled, going into blocked state");
if (user_object->chan == &LOCATION_CHAN && user_object->location_search) {
LOG_DBG("Location search started, going into blocked state");

smf_set_state(SMF_CTX(&state_object), &states[STATE_BLOCKED]);
return;
Expand Down Expand Up @@ -598,7 +598,7 @@ void trigger_callback(const struct zbus_channel *chan)
} else if (&LOCATION_CHAN == chan) {
const enum location_status *location_status = zbus_chan_const_msg(chan);

state_object.gnss = (*location_status == GNSS_ENABLED);
state_object.location_search = (*location_status == LOCATION_SEARCH_STARTED);
} else {
/* PRIV_TRIGGER_CHAN event. Frequent Poll Duration timer expired*/
LOG_DBG("Message received on PRIV_TRIGGER_CHAN channel.");
Expand Down
14 changes: 7 additions & 7 deletions tests/module/trigger/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static void send_cloud_disconnected(void)
TEST_ASSERT_EQUAL(0, err);
}

static void send_gnss_event(enum location_status location_status)
static void send_location_event(enum location_status location_status)
{
int err = zbus_chan_pub(&LOCATION_CHAN, &location_status, K_SECONDS(1));

Expand Down Expand Up @@ -254,7 +254,7 @@ void test_frequent_poll_to_blocked(void)
go_to_frequent_poll_state();

/* When */
send_gnss_event(GNSS_ENABLED);
send_location_event(LOCATION_SEARCH_STARTED);

/* Then */
check_no_trigger_events(FREQUENT_POLL_TRIGGER_INTERVAL_SEC * 2);
Expand All @@ -269,7 +269,7 @@ void test_normal_to_blocked(void)
go_to_normal_state();

/* When */
send_gnss_event(GNSS_ENABLED);
send_location_event(LOCATION_SEARCH_STARTED);

/* Then */
check_no_trigger_events(CONFIG_APP_TRIGGER_TIMEOUT_SECONDS * 2);
Expand Down Expand Up @@ -318,10 +318,10 @@ void test_frequent_poll_to_blocked_to_frequent_poll(void)
{
/* Given */
go_to_frequent_poll_state();
send_gnss_event(GNSS_ENABLED);
send_location_event(LOCATION_SEARCH_STARTED);

/* When */
send_gnss_event(GNSS_DISABLED);
send_location_event(LOCATION_SEARCH_DONE);

/* Then */
k_sleep(K_SECONDS(FREQUENT_POLL_TRIGGER_INTERVAL_SEC));
Expand All @@ -340,11 +340,11 @@ void test_frequent_poll_to_blocked_to_normal(void)
{
/* Given */
go_to_frequent_poll_state();
send_gnss_event(GNSS_ENABLED);
send_location_event(LOCATION_SEARCH_STARTED);

/* When */
k_sleep(K_SECONDS(CONFIG_FREQUENT_POLL_DURATION_INTERVAL_SEC));
send_gnss_event(GNSS_DISABLED);
send_location_event(LOCATION_SEARCH_DONE);

/* Then */
check_trigger_mode_event(TRIGGER_MODE_NORMAL);
Expand Down
Loading