From c295af3463c8253fcd837ce9aa9981a6455a90aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simen=20S=2E=20R=C3=B8stad?= Date: Mon, 24 Jun 2024 13:04:31 +0200 Subject: [PATCH] modules: app: Trace decoding errors to Memfault MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Trace decoding errors to Memfault Signed-off-by: Simen S. Røstad --- app/CMakeLists.txt | 3 +++ .../memfault_trace_reason_user_config.def | 5 +++++ app/src/common/message_channel.c | 4 ++-- app/src/common/message_channel.h | 11 ++++++++--- app/src/modules/app/app.c | 12 ++++++++++-- app/src/modules/led/led.c | 19 ++++++++++++------- app/src/modules/memfault/memfault.c | 9 +++++++++ .../transport/src/transport_module_test.c | 15 +++++++++------ 8 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 app/config/memfault_trace_reason_user_config.def diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index b19bfe75..d7714d48 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -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) diff --git a/app/config/memfault_trace_reason_user_config.def b/app/config/memfault_trace_reason_user_config.def new file mode 100644 index 00000000..4adb3eac --- /dev/null +++ b/app/config/memfault_trace_reason_user_config.def @@ -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) diff --git a/app/src/common/message_channel.c b/app/src/common/message_channel.c index 76401bfa..080a3601 100644 --- a/app/src/common/message_channel.c +++ b/app/src/common/message_channel.c @@ -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, diff --git a/app/src/common/message_channel.h b/app/src/common/message_channel.h index 18a7513c..f66b5d85 100644 --- a/app/src/common/message_channel.h +++ b/app/src/common/message_channel.h @@ -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, ¬_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())); \ @@ -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; @@ -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, diff --git a/app/src/modules/app/app.c b/app/src/modules/app/app.c index 2d0fe468..8c07185f 100644 --- a/app/src/modules/app/app.c +++ b/app/src/modules/app/app.c @@ -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; } @@ -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; diff --git a/app/src/modules/led/led.c b/app/src/modules/led/led.c index c10062a6..5149de73 100644 --- a/app/src/modules/led/led.c +++ b/app/src/modules/led/led.c @@ -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) @@ -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; + } } } } diff --git a/app/src/modules/memfault/memfault.c b/app/src/modules/memfault/memfault.c index d96b016e..17ac79f2 100644 --- a/app/src/modules/memfault/memfault.c +++ b/app/src/modules/memfault/memfault.c @@ -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); diff --git a/tests/module/transport/src/transport_module_test.c b/tests/module/transport/src/transport_module_test.c index a0247eab..2f1bba5e 100644 --- a/tests/module/transport/src/transport_module_test.c +++ b/tests/module/transport/src/transport_module_test.c @@ -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); + } } } @@ -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) { @@ -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)