From 24398d03c9c7bec38ab787d068357f44106c53af Mon Sep 17 00:00:00 2001 From: Pete Skeggs Date: Wed, 25 Sep 2024 14:49:18 -0700 Subject: [PATCH] net: lib: nrf_cloud: Log connection info from lib Some nRF Cloud connection information is available regardless of whether the device has successfully connected, whereas other information is only available post-connection. Separate out the post-connection info into a new function, and call both at the appropriate times in the nrf_cloud_libraries. Remove previous requirement to call these functions from the application. Jira: IRIS-9653 Signed-off-by: Pete Skeggs Co-authored-by: Justin Morton --- .../releases/release-notes-changelog.rst | 18 +++++----- include/net/nrf_cloud.h | 16 +++++++-- .../src/cloud_connection.c | 3 -- .../nrf_cloud_rest_cell_location/src/main.c | 5 --- .../nrf_cloud_rest_device_message/src/main.c | 5 --- .../cellular/nrf_cloud_rest_fota/src/main.c | 5 --- subsys/net/lib/nrf_cloud/Kconfig | 13 +++++-- .../coap/src/nrf_cloud_coap_transport.c | 2 ++ subsys/net/lib/nrf_cloud/src/nrf_cloud.c | 2 ++ subsys/net/lib/nrf_cloud/src/nrf_cloud_fsm.c | 1 + subsys/net/lib/nrf_cloud/src/nrf_cloud_info.c | 34 +++++++++++-------- subsys/net/lib/nrf_cloud/src/nrf_cloud_rest.c | 7 ++++ 12 files changed, 65 insertions(+), 46 deletions(-) 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 43caa0e5eb88..a5e65c264883 100644 --- a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst +++ b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst @@ -394,10 +394,8 @@ Cellular samples * :ref:`nrf_cloud_rest_fota` sample: - * Added: - - * Support for setting the FOTA update check interval using the config section in the shadow. - * A call to the :c:func:`nrf_cloud_print_details` function and removed redundant logging. + * Added support for setting the FOTA update check interval using the config section in the shadow. + * Removed redundant logging now done by the :ref:`lib_nrf_cloud` library. * :ref:`nrf_cloud_multi_service` sample: @@ -405,7 +403,6 @@ Cellular samples * The :kconfig:option:`CONFIG_TEST_COUNTER_MULTIPLIER` Kconfig option to multiply the number of test counter messages sent, for testing purposes. * 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 `_. @@ -418,19 +415,21 @@ Cellular samples * Renamed the :file:`overlay_nrf7002ek_wifi_coap_no_lte.conf` overlay to :file:`overlay_nrf700x_wifi_coap_no_lte.conf`. * Fixed an issue where the accepted shadow was not marked as received because the config section did not yet exist in the shadow. + * Removed redundant logging now done by the :ref:`lib_nrf_cloud` library. * :ref:`nrf_cloud_rest_device_message` sample: * Added: * 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 `_. + * Removed redundant logging now done by the :ref:`lib_nrf_cloud` library. + * :ref:`nrf_cloud_rest_cell_pos_sample` sample: - * Added a call to the :c:func:`nrf_cloud_print_details` function and removed redundant logging. + * Removed redundant logging now done by the :ref:`lib_nrf_cloud` library. Cryptography samples -------------------- @@ -807,8 +806,9 @@ Libraries for networking * The function :c:func:`nrf_cloud_client_id_runtime_set` to set the device ID string if the :kconfig:option:`CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME` Kconfig option is enabled. * The functions :c:func:`nrf_cloud_sec_tag_set` and :c:func:`nrf_cloud_sec_tag_get` to set and get the sec tag used for nRF Cloud credentials. * A new nRF Cloud event type ``NRF_CLOUD_EVT_RX_DATA_DISCON`` which is generated when a device is deleted from nRF Cloud. - * The function :c:func:`nrf_cloud_print_details` to log common nRF Cloud connection information in a uniform way. - * The :kconfig:option:`CONFIG_NRF_CLOUD_VERBOSE_DETAILS` Kconfig option to enable the :c:func:`nrf_cloud_print_details` function to print all details instead of only the device ID. + * The functions :c:func:`nrf_cloud_print_details` and :c:func:`nrf_cloud_print_cloud_details` to log common nRF Cloud connection information in a uniform way. + * The :kconfig:option:`CONFIG_NRF_CLOUD_PRINT_DETAILS` Kconfig option to enable the above functions. + * The :kconfig:option:`CONFIG_NRF_CLOUD_VERBOSE_DETAILS` Kconfig option to print all details instead of only the device ID. * Updated: diff --git a/include/net/nrf_cloud.h b/include/net/nrf_cloud.h index 21962e6bd77c..8f8843fae3fc 100644 --- a/include/net/nrf_cloud.h +++ b/include/net/nrf_cloud.h @@ -704,14 +704,26 @@ int nrf_cloud_uninit(void); /** * @brief Print details about cloud connection. * - * if @kconfig{CONFIG_NRF_CLOUD_VERBOSE_DETAILS} is not enabled, + * If @kconfig{CONFIG_NRF_CLOUD_VERBOSE_DETAILS} is not enabled, * only print the device id. If enabled, also print the protocol, - * sec tag, host name, stage, and team id. + * sec tag, and host name. * * @return A negative value indicates an error. */ int nrf_cloud_print_details(void); +/** + * @brief Print details about cloud connection after connected. + * + * Some information is only available after the device is connected. + * When using MQTT, the tenant will be printed. + * + * When using CoAP and REST, there is no further information to print. + * + * @return A negative value indicates an error. + */ +int nrf_cloud_print_cloud_details(void); + /** * @brief Retrieve the IMEI. * diff --git a/samples/cellular/nrf_cloud_multi_service/src/cloud_connection.c b/samples/cellular/nrf_cloud_multi_service/src/cloud_connection.c index d01b0162b180..8a024a9894b9 100644 --- a/samples/cellular/nrf_cloud_multi_service/src/cloud_connection.c +++ b/samples/cellular/nrf_cloud_multi_service/src/cloud_connection.c @@ -255,8 +255,6 @@ static bool connect_cloud(void) /* Clear the disconnected flag, no longer accurate. */ k_event_clear(&cloud_events, CLOUD_DISCONNECTED); - nrf_cloud_print_details(); - #if defined(CONFIG_NRF_CLOUD_MQTT) /* Connect to nRF Cloud -- Non-blocking. State updates are handled in callbacks. */ err = nrf_cloud_connect(); @@ -467,7 +465,6 @@ static void cloud_event_handler(const struct nrf_cloud_evt *nrf_cloud_evt) * device's shadow based on the build configuration. * See config NRF_CLOUD_SEND_SHADOW_INFO for details. */ - break; case NRF_CLOUD_EVT_SENSOR_DATA_ACK: LOG_DBG("NRF_CLOUD_EVT_SENSOR_DATA_ACK"); diff --git a/samples/cellular/nrf_cloud_rest_cell_location/src/main.c b/samples/cellular/nrf_cloud_rest_cell_location/src/main.c index ea623407a532..d5ddb075fea7 100644 --- a/samples/cellular/nrf_cloud_rest_cell_location/src/main.c +++ b/samples/cellular/nrf_cloud_rest_cell_location/src/main.c @@ -443,11 +443,6 @@ int init(void) return err; } - err = nrf_cloud_print_details(); - if (err) { - LOG_ERR("Error printing cloud information: %d", err); - } - /* Check modem FW version */ check_modem_fw_version(); 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 c4bb5276721a..bd1d276090f6 100644 --- a/samples/cellular/nrf_cloud_rest_device_message/src/main.c +++ b/samples/cellular/nrf_cloud_rest_device_message/src/main.c @@ -527,11 +527,6 @@ static int init(void) return -EFAULT; } - err = nrf_cloud_print_details(); - if (err) { - LOG_ERR("Error printing cloud information: %d", err); - } - /* If provisioning library is disabled, ensure device has credentials installed * before proceeding */ diff --git a/samples/cellular/nrf_cloud_rest_fota/src/main.c b/samples/cellular/nrf_cloud_rest_fota/src/main.c index d8c92a1fb6fb..85ff9b845080 100644 --- a/samples/cellular/nrf_cloud_rest_fota/src/main.c +++ b/samples/cellular/nrf_cloud_rest_fota/src/main.c @@ -347,11 +347,6 @@ int init(void) return -EFAULT; } - err = nrf_cloud_print_details(); - if (err) { - LOG_ERR("Error printing cloud information: %d", err); - } - err = nrf_cloud_fota_poll_init(&fota_ctx); if (err) { LOG_ERR("FOTA support init failed: %d", err); diff --git a/subsys/net/lib/nrf_cloud/Kconfig b/subsys/net/lib/nrf_cloud/Kconfig index 38aa7a370ca4..4f326ba57f8f 100644 --- a/subsys/net/lib/nrf_cloud/Kconfig +++ b/subsys/net/lib/nrf_cloud/Kconfig @@ -27,12 +27,19 @@ rsource "Kconfig.nrf_cloud_coap" rsource "Kconfig.nrf_cloud_shadow_info" +config NRF_CLOUD_PRINT_DETAILS + bool "Log info about cloud connection" + default y + help + Log at the INF level the device ID. + config NRF_CLOUD_VERBOSE_DETAILS bool "Log more info about cloud connection" - default y + depends on NRF_CLOUD_PRINT_DETAILS + default y if NRF_CLOUD_PRINT_DETAILS help - Log at INF level the stage, protocol, sec tag, host name, and team ID, - in addition to device id. + Log at INF level the protocol, sec tag, host name, and team ID, + in addition to device ID. config NRF_CLOUD_GATEWAY bool "nRF Cloud Gateway" diff --git a/subsys/net/lib/nrf_cloud/coap/src/nrf_cloud_coap_transport.c b/subsys/net/lib/nrf_cloud/coap/src/nrf_cloud_coap_transport.c index a5d2e8ae04f6..83bf54716fde 100644 --- a/subsys/net/lib/nrf_cloud/coap/src/nrf_cloud_coap_transport.c +++ b/subsys/net/lib/nrf_cloud/coap/src/nrf_cloud_coap_transport.c @@ -190,6 +190,8 @@ int nrf_cloud_coap_init(void) { int err; + (void)nrf_cloud_print_details(); + internal_cc.authenticated = false; if (!internal_cc.initialized) { diff --git a/subsys/net/lib/nrf_cloud/src/nrf_cloud.c b/subsys/net/lib/nrf_cloud/src/nrf_cloud.c index 1cc0ee89629b..b4a8e48e51ef 100644 --- a/subsys/net/lib/nrf_cloud/src/nrf_cloud.c +++ b/subsys/net/lib/nrf_cloud/src/nrf_cloud.c @@ -89,6 +89,8 @@ int nrf_cloud_init(const struct nrf_cloud_init_param *param) { int err; + (void)nrf_cloud_print_details(); + if (current_state != STATE_IDLE || atomic_get(&uninit_in_progress)) { return -EACCES; diff --git a/subsys/net/lib/nrf_cloud/src/nrf_cloud_fsm.c b/subsys/net/lib/nrf_cloud/src/nrf_cloud_fsm.c index b9d5f3d04433..517b61b2a338 100644 --- a/subsys/net/lib/nrf_cloud/src/nrf_cloud_fsm.c +++ b/subsys/net/lib/nrf_cloud/src/nrf_cloud_fsm.c @@ -627,6 +627,7 @@ static int dc_connection_handler(const struct nct_evt *nct_evt) nfsm_set_current_state_and_notify(STATE_DC_CONNECTED, &evt); } + (void)nrf_cloud_print_cloud_details(); return 0; } diff --git a/subsys/net/lib/nrf_cloud/src/nrf_cloud_info.c b/subsys/net/lib/nrf_cloud/src/nrf_cloud_info.c index 83f962ceb19e..cf99fb1b204a 100644 --- a/subsys/net/lib/nrf_cloud/src/nrf_cloud_info.c +++ b/subsys/net/lib/nrf_cloud/src/nrf_cloud_info.c @@ -92,6 +92,7 @@ int nrf_cloud_get_modem_fw(char *buf, size_t buf_sz) int nrf_cloud_print_details(void) { +#if defined(CONFIG_NRF_CLOUD_PRINT_DETAILS) int err; const char *device_id; @@ -110,19 +111,19 @@ int nrf_cloud_print_details(void) err = nrf_cloud_get_imei(buf, sizeof(buf)); if (!err) { LOG_INF("IMEI: %s", buf); - } else { + } else if (err != -ENOTSUP) { LOG_ERR("Error requesting the IMEI: %d", err); } err = nrf_cloud_get_uuid(buf, sizeof(buf)); if (!err) { LOG_INF("UUID: %s", buf); - } else { + } else if (err != -ENOTSUP) { LOG_ERR("Error requesting the UUID: %d", err); } err = nrf_cloud_get_modem_fw(buf, sizeof(buf)); if (!err) { LOG_INF("Modem FW: %s", buf); - } else { + } else if (err != -ENOTSUP) { LOG_ERR("Error requesting the modem fw version: %d", err); } @@ -140,25 +141,30 @@ int nrf_cloud_print_details(void) LOG_INF("Sec tag: %d", nrf_cloud_sec_tag_get()); LOG_INF("Host name: %s", host_name); - /* Tenant will not be known unless device is connected to cloud with MQTT. */ +#endif /* CONFIG_NRF_CLOUD_VERBOSE_DETAILS */ + + return err; +#else + return 0; +#endif /* CONFIG_NRF_CLOUD_PRINT_DETAILS */ +} + +int nrf_cloud_print_cloud_details(void) +{ + int err = 0; +#if defined(CONFIG_NRF_CLOUD_VERBOSE_DETAILS) + #if defined(CONFIG_NRF_CLOUD_MQTT) - char stage[NRF_CLOUD_STAGE_ID_MAX_LEN]; char tenant[NRF_CLOUD_TENANT_ID_MAX_LEN]; - err = nct_stage_get(stage, sizeof(stage)); - if (!err) { - LOG_INF("Stage: %s", stage); - } else { - LOG_ERR("Error determining nRF Cloud stage: %d", err); - } err = nct_tenant_id_get(tenant, sizeof(tenant)); if (!err) { - LOG_INF("Team ID: %s", tenant); + LOG_INF("Team ID: %s", tenant); } else { - LOG_ERR("Error determining team ID: %d", err); + LOG_ERR("Error determining Team ID: %d", err); } #endif -#endif /* CONFIG_NRF_CLOUD_VERBOSE_DETAILS */ +#endif return err; } diff --git a/subsys/net/lib/nrf_cloud/src/nrf_cloud_rest.c b/subsys/net/lib/nrf_cloud/src/nrf_cloud_rest.c index e1d537e5b65f..4beec1177a5a 100644 --- a/subsys/net/lib/nrf_cloud/src/nrf_cloud_rest.c +++ b/subsys/net/lib/nrf_cloud/src/nrf_cloud_rest.c @@ -172,6 +172,13 @@ static void close_connection(struct nrf_cloud_rest_context *const rest_ctx) static void init_rest_client_request(struct nrf_cloud_rest_context const *const rest_ctx, struct rest_client_req_context *const req, const enum http_method meth) { + static bool printed; + + if (!printed) { + printed = true; + (void)nrf_cloud_print_details(); + } + memset(req, 0, sizeof(*req)); req->connect_socket = rest_ctx->connect_socket;