Skip to content

Commit

Permalink
modules: app: Trace decoding errors to Memfault
Browse files Browse the repository at this point in the history
Trace decoding errors to Memfault

Signed-off-by: Simen S. Røstad <[email protected]>
  • Loading branch information
simensrostad committed Jul 3, 2024
1 parent e73bc04 commit c295af3
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 20 deletions.
3 changes: 3 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ add_subdirectory(src/modules/environmental)
add_subdirectory_ifdef(CONFIG_APP_LED src/modules/led)
add_subdirectory_ifdef(CONFIG_APP_MEMFAULT src/modules/memfault)
add_subdirectory_ifdef(CONFIG_APP_SHELL src/modules/shell)

# Include Memfault configuration folder
zephyr_include_directories(config)
5 changes: 5 additions & 0 deletions app/config/memfault_trace_reason_user_config.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Application-specific trace reasons can be defined here.
* Please refer to https://docs.memfault.com/docs/embedded/trace-events for more details.
*/

MEMFAULT_TRACE_REASON_DEFINE(decode_error)
4 changes: 2 additions & 2 deletions app/src/common/message_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ ZBUS_CHAN_DEFINE(NETWORK_CHAN,
NETWORK_DISCONNECTED
);

ZBUS_CHAN_DEFINE(FATAL_ERROR_CHAN,
int,
ZBUS_CHAN_DEFINE(ERROR_CHAN,
enum error_type,
NULL,
NULL,
ZBUS_OBSERVERS_EMPTY,
Expand Down
11 changes: 8 additions & 3 deletions app/src/common/message_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ extern "C" {
* @param is_watchdog_timeout Boolean indicating if the macro was called upon a watchdog timeout.
*/
#define FATAL_ERROR_HANDLE(is_watchdog_timeout) do { \
int not_used = -1; \
(void)zbus_chan_pub(&FATAL_ERROR_CHAN, &not_used, K_SECONDS(10)); \
enum error_type type = ERROR_FATAL; \
(void)zbus_chan_pub(&ERROR_CHAN, &type, K_SECONDS(10)); \
LOG_PANIC(); \
if (is_watchdog_timeout) { \
IF_ENABLED(CONFIG_MEMFAULT, (MEMFAULT_SOFTWARE_WATCHDOG())); \
Expand Down Expand Up @@ -63,6 +63,11 @@ enum time_status {
TIME_AVAILABLE = 0x1,
};

enum error_type {
ERROR_FATAL = 0x1,
ERROR_DECODE,
};

struct configuration {
bool led_present;
int led_red;
Expand All @@ -77,7 +82,7 @@ ZBUS_CHAN_DECLARE(
BUTTON_CHAN,
CLOUD_CHAN,
CONFIG_CHAN,
FATAL_ERROR_CHAN,
ERROR_CHAN,
FOTA_ONGOING_CHAN,
LED_CHAN,
NETWORK_CHAN,
Expand Down
12 changes: 10 additions & 2 deletions app/src/modules/app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ static void shadow_get(bool delta_only)
* Hardfaulting would prevent FOTA, hence it should be avoided.
*/
LOG_ERR("Ignoring incoming configuration change due to decoding error: %d", err);
LOG_HEXDUMP_ERR(buf_cbor, buf_cbor_len, "CBOR data");

enum error_type type = ERROR_DECODE;

err = zbus_chan_pub(&ERROR_CHAN, &type, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
}

return;
}

Expand All @@ -71,8 +81,6 @@ static void shadow_get(bool delta_only)
return;
}

struct configuration configuration = { 0 };

if (app_object.lwm2m.lwm2m._1424010_present) {
configuration.led_present = true;
configuration.led_red = app_object.lwm2m.lwm2m._1424010._1424010._0._0;
Expand Down
19 changes: 12 additions & 7 deletions app/src/modules/led/led.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void led_callback(const struct zbus_channel *chan);
ZBUS_LISTENER_DEFINE(led, led_callback);

/* Observe channels */
ZBUS_CHAN_ADD_OBS(FATAL_ERROR_CHAN, led, 0);
ZBUS_CHAN_ADD_OBS(ERROR_CHAN, led, 0);
ZBUS_CHAN_ADD_OBS(CONFIG_CHAN, led, 0);

#define PWM_LED0_NODE DT_ALIAS(pwm_led0)
Expand Down Expand Up @@ -95,13 +95,18 @@ void led_callback(const struct zbus_channel *chan)
}
}

if (&FATAL_ERROR_CHAN == chan) {
if (&ERROR_CHAN == chan) {
/* Red LED */
err = dk_set_led_on(DK_LED1);
if (err) {
LOG_ERR("dk_set_led_on, error:%d", err);
SEND_FATAL_ERROR();
return;

const enum error_type *type = zbus_chan_const_msg(chan);

if (*type == ERROR_FATAL) {
err = dk_set_led_on(DK_LED1);
if (err) {
LOG_ERR("dk_set_led_on, error:%d", err);
SEND_FATAL_ERROR();
return;
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/modules/memfault/memfault.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ void callback(const struct zbus_channel *chan)
on_connected();
}
}

if (&ERROR_CHAN == chan) {
const enum error_type *type = zbus_chan_const_msg(chan);

if (*type == ERROR_DECODE) {
MEMFAULT_TRACE_EVENT(decode_error);
}
}
}

ZBUS_LISTENER_DEFINE(memfault, callback);
ZBUS_CHAN_ADD_OBS(CLOUD_CHAN, memfault, 0);
ZBUS_CHAN_ADD_OBS(ERROR_CHAN, memfault, 0);
15 changes: 9 additions & 6 deletions tests/module/transport/src/transport_module_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ static void cloud_chan_cb(const struct zbus_channel *chan)
}
}

static void fatal_error_cb(const struct zbus_channel *chan)
static void error_cb(const struct zbus_channel *chan)
{
if (chan == &FATAL_ERROR_CHAN) {
k_sem_give(&fatal_error_received);
if (chan == &ERROR_CHAN) {
enum error_type type = *(enum error_type *)chan->message;

if (type == ERROR_FATAL) {
k_sem_give(&fatal_error_received);
}
}
}

Expand All @@ -62,8 +66,7 @@ ZBUS_SUBSCRIBER_DEFINE(led, 1);
ZBUS_SUBSCRIBER_DEFINE(location, 1);
ZBUS_LISTENER_DEFINE(trigger, dummy_cb);
ZBUS_LISTENER_DEFINE(cloud, cloud_chan_cb);
ZBUS_LISTENER_DEFINE(fatal_error, fatal_error_cb);

ZBUS_LISTENER_DEFINE(error, error_cb);

void setUp(void)
{
Expand All @@ -81,7 +84,7 @@ void setUp(void)
zbus_sub_wait(&battery, &chan, K_NO_WAIT);

zbus_chan_add_obs(&CLOUD_CHAN, &cloud, K_NO_WAIT);
zbus_chan_add_obs(&FATAL_ERROR_CHAN, &fatal_error, K_NO_WAIT);
zbus_chan_add_obs(&ERROR_CHAN, &error, K_NO_WAIT);
}

void test_initial_transition_to_disconnected(void)
Expand Down

0 comments on commit c295af3

Please sign in to comment.