From b38111cef61eb8b5c66ffb56990a54fe07ca83f7 Mon Sep 17 00:00:00 2001 From: Pete Skeggs Date: Fri, 20 Sep 2024 16:07:12 -0700 Subject: [PATCH] samples: cellular: nrf_cloud: Control online alert By default, no longer send an alert on startup. Add logging of the reset reason, and if the online alert is enabled, include that in the value. IRIS-9688 Signed-off-by: Pete Skeggs Co-authored-by: Pekka Niskanen Co-authored-by: Justin Morton --- doc/nrf/links.txt | 1 + .../releases/release-notes-changelog.rst | 4 ++ .../cellular/nrf_cloud_multi_service/Kconfig | 6 ++ .../nrf_cloud_multi_service/README.rst | 5 +- .../nrf_cloud_multi_service/src/application.c | 24 ++++++- .../nrf_cloud_rest_device_message/Kconfig | 6 ++ .../nrf_cloud_rest_device_message/src/main.c | 63 +++++++++++++------ 7 files changed, 89 insertions(+), 20 deletions(-) diff --git a/doc/nrf/links.txt b/doc/nrf/links.txt index aea4c379bda..651f9bba6ea 100644 --- a/doc/nrf/links.txt +++ b/doc/nrf/links.txt @@ -648,6 +648,7 @@ .. _`External memory section in the nRF9160 DK User Guide`: https://docs.nordicsemi.com/bundle/ug_nrf9160_dk/page/UG/nrf91_DK/hw_description/external_memory.html .. _`Device programming section in the nRF9160 DK User Guide`: https://docs.nordicsemi.com/bundle/ug_nrf9160_dk/page/UG/nrf91_DK/operating_modes/mcu_device_programming.html .. _`VDD supply rail section in the nRF9160 DK User Guide`: https://docs.nordicsemi.com/bundle/ug_nrf9160_dk/page/UG/nrf91_DK/hw_description/power_sources_vdd.html +.. _`nRF9160 RESETREAS`: https://docs.nordicsemi.com/bundle/ps_nrf9160/page/power.html#ariaid-title16 .. _`nRF9161 Product Specification`: https://docs.nordicsemi.com/bundle/ps_nrf9161/page/nRF9161_html5_keyfeatures.html .. _`nRF9161 GPS receiver specification`: https://docs.nordicsemi.com/bundle/ps_nrf9161/page/gps.html diff --git a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst index a03fc044b7a..43caa0e5eb8 100644 --- a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst +++ b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst @@ -407,6 +407,8 @@ Cellular samples * A handler for new nRF Cloud event type ``NRF_CLOUD_EVT_RX_DATA_DISCON`` to stop sensors and location services. * A call to the :c:func:`nrf_cloud_print_details` function and removed redundant logging. * Board support files to enable Wi-Fi scanning for the Thingy:91 X. + * The :kconfig:option:`CONFIG_SEND_ONLINE_ALERT` Kconfig option to enable calling the :c:func:`nrf_cloud_alert` function on startup. + * Logging of the `reset reason code `_. * Updated: @@ -423,6 +425,8 @@ Cellular samples * Support for dictionary logs using REST. * A call to the :c:func:`nrf_cloud_print_details` function and removed redundant logging. + * The :kconfig:option:`CONFIG_SEND_ONLINE_ALERT` Kconfig option to enable calling the :c:func:`nrf_cloud_alert` function on startup. + * Logging of the `reset reason code `_. * :ref:`nrf_cloud_rest_cell_pos_sample` sample: diff --git a/samples/cellular/nrf_cloud_multi_service/Kconfig b/samples/cellular/nrf_cloud_multi_service/Kconfig index ad5efde3790..5baee24dba9 100644 --- a/samples/cellular/nrf_cloud_multi_service/Kconfig +++ b/samples/cellular/nrf_cloud_multi_service/Kconfig @@ -239,6 +239,12 @@ config AT_CMD_REQUEST_RESPONSE_BUFFER_LENGTH error code sent in its place (-NRF_E2BIG). Cannot be less than 40 bytes. default 200 +config SEND_ONLINE_ALERT + bool "Sends a routine ALERT_TYPE_DEVICE_NOW_ONLINE on startup" + help + Enable this to demonstrate the alert feature of nRF Cloud. Reception of this alert + indicates the device has rebooted. + if NRF_CLOUD_COAP menuconfig COAP_FOTA diff --git a/samples/cellular/nrf_cloud_multi_service/README.rst b/samples/cellular/nrf_cloud_multi_service/README.rst index 6bafb368596..e66f5924fa0 100644 --- a/samples/cellular/nrf_cloud_multi_service/README.rst +++ b/samples/cellular/nrf_cloud_multi_service/README.rst @@ -122,8 +122,11 @@ Application thread and main application loop ============================================ The application thread is implemented in the :file:`src/application.c` file, and is responsible for the high-level behavior of this sample. -It performs the following major tasks: +When it starts, it logs the `reset reason code `_. +If the :kconfig:option:`CONFIG_SEND_ONLINE_ALERT` Kconfig option is enabled, it sends an alert to nRF Cloud containing the reset reason as the value field. + +It performs the following major tasks: * Establishes periodic position tracking (which the :ref:`lib_location` library performs). * Periodically samples temperature data (using the :file:`src/temperature.c` file). * Constructs timestamped sensor sample and location `device messages `_. diff --git a/samples/cellular/nrf_cloud_multi_service/src/application.c b/samples/cellular/nrf_cloud_multi_service/src/application.c index 04722bd4b79..d0ad956f019 100644 --- a/samples/cellular/nrf_cloud_multi_service/src/application.c +++ b/samples/cellular/nrf_cloud_multi_service/src/application.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -321,8 +322,29 @@ static void test_counter_send(void) } } +static void print_reset_reason(void) +{ + uint32_t reset_reason; + + reset_reason = nrfx_reset_reason_get(); + LOG_INF("Reset reason: 0x%x", reset_reason); +} + +static void report_startup(void) +{ + if (IS_ENABLED(CONFIG_SEND_ONLINE_ALERT)) { + uint32_t reset_reason; + + reset_reason = nrfx_reset_reason_get(); + nrfx_reset_reason_clear(reset_reason); + (void)nrf_cloud_alert_send(ALERT_TYPE_DEVICE_NOW_ONLINE, reset_reason, NULL); + } +} + void main_application_thread_fn(void) { + print_reset_reason(); + if (IS_ENABLED(CONFIG_AT_CMD_REQUESTS)) { /* Register with connection.c to receive general device messages and check them for * AT command requests. @@ -333,7 +355,7 @@ void main_application_thread_fn(void) /* Wait for first connection before starting the application. */ (void)await_cloud_ready(K_FOREVER); - (void)nrf_cloud_alert_send(ALERT_TYPE_DEVICE_NOW_ONLINE, 0, NULL); + report_startup(); /* Wait for the date and time to become known. * This is needed both for location services and for sensor sample timestamping. diff --git a/samples/cellular/nrf_cloud_rest_device_message/Kconfig b/samples/cellular/nrf_cloud_rest_device_message/Kconfig index 2e739aade1d..24966b591c3 100644 --- a/samples/cellular/nrf_cloud_rest_device_message/Kconfig +++ b/samples/cellular/nrf_cloud_rest_device_message/Kconfig @@ -33,6 +33,12 @@ config REST_DEVICE_MESSAGE_KEEP_ALIVE direct logging message can reuse it. This reduces data and power consumption, but is not compatible with the cloud logging backend. +config SEND_ONLINE_ALERT + bool "Sends a routine ALERT_TYPE_DEVICE_NOW_ONLINE on startup" + help + Enable this to demonstrate the alert feature of nRF Cloud. Reception of this alert + indicates the device has rebooted. + endmenu menu "Zephyr Kernel" diff --git a/samples/cellular/nrf_cloud_rest_device_message/src/main.c b/samples/cellular/nrf_cloud_rest_device_message/src/main.c index 6ef960d29da..c4bb5276721 100644 --- a/samples/cellular/nrf_cloud_rest_device_message/src/main.c +++ b/samples/cellular/nrf_cloud_rest_device_message/src/main.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -551,10 +552,52 @@ static int init(void) return 0; } +static void print_reset_reason(void) +{ + uint32_t reset_reason; + + reset_reason = nrfx_reset_reason_get(); + LOG_INF("Reset reason: 0x%x", reset_reason); +} + +static void report_startup(void) +{ + int err; + + /* Set the keep alive flag to true; + * connection will remain open to allow for multiple REST calls + */ + rest_ctx.keep_alive = true; + + if (IS_ENABLED(CONFIG_SEND_ONLINE_ALERT)) { + uint32_t reset_reason; + + reset_reason = nrfx_reset_reason_get(); + nrfx_reset_reason_clear(reset_reason); + + err = nrf_cloud_rest_alert_send(&rest_ctx, device_id, + ALERT_TYPE_DEVICE_NOW_ONLINE, + reset_reason, NULL); + if (err) { + LOG_ERR("Error sending alert to cloud: %d", err); + } + } + + err = nrf_cloud_rest_log_send(&rest_ctx, device_id, LOG_LEVEL_INF, + SAMPLE_SIGNON_FMT, + CONFIG_REST_DEVICE_MESSAGE_SAMPLE_VERSION); + + if (err) { + LOG_ERR("Error sending direct log to cloud: %d", err); + } +} + static int setup(void) { int err; + print_reset_reason(); + /* Initialize libraries and hardware */ err = init(); if (err) { @@ -622,24 +665,8 @@ int main(void) return 0; } - /* Set the keep alive flag to true; - * connection will remain open to allow for multiple REST calls - */ - rest_ctx.keep_alive = true; - err = nrf_cloud_rest_alert_send(&rest_ctx, device_id, - ALERT_TYPE_DEVICE_NOW_ONLINE, 0, NULL); - - if (err) { - LOG_ERR("Error sending alert to cloud: %d", err); - } - - err = nrf_cloud_rest_log_send(&rest_ctx, device_id, LOG_LEVEL_INF, - SAMPLE_SIGNON_FMT, - CONFIG_REST_DEVICE_MESSAGE_SAMPLE_VERSION); - - if (err) { - LOG_ERR("Error sending direct log to cloud: %d", err); - } + /* Send alert (if enabled) and log message to the cloud. */ + report_startup(); /* Set the keep alive flag to false; connection will be closed after the next REST call */ rest_ctx.keep_alive = false;