Skip to content

Commit

Permalink
net: lib: nrf_cloud: Log connection info from lib
Browse files Browse the repository at this point in the history
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 <peter.skeggs@nordicsemi.no>
Co-authored-by: Justin Morton <justin.morton@nordicsemi.no>
  • Loading branch information
2 people authored and rlubos committed Oct 3, 2024
1 parent 9be2617 commit 547aaab
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,18 +406,15 @@ 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:

* Added:

* 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 <nRF9160 RESETREAS_>`_.
Expand All @@ -430,19 +427,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 <nRF9160 RESETREAS_>`_.

* 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
--------------------
Expand Down Expand Up @@ -819,8 +818,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:

Expand Down
16 changes: 14 additions & 2 deletions include/net/nrf_cloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -714,14 +714,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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 1 addition & 5 deletions samples/cellular/nrf_cloud_rest_cell_location/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,7 @@ int init(void)
LOG_ERR("Failed to get device ID, error: %d", err);
return err;
}

err = nrf_cloud_print_details();
if (err) {
LOG_ERR("Error printing cloud information: %d", err);
}
LOG_INF("Device ID: %s", device_id);

/* Check modem FW version */
check_modem_fw_version();
Expand Down
5 changes: 0 additions & 5 deletions samples/cellular/nrf_cloud_rest_device_message/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
5 changes: 0 additions & 5 deletions samples/cellular/nrf_cloud_rest_fota/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 10 additions & 3 deletions subsys/net/lib/nrf_cloud/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,19 @@ rsource "Kconfig.nrf_cloud_log"

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"
Expand Down
2 changes: 2 additions & 0 deletions subsys/net/lib/nrf_cloud/coap/src/nrf_cloud_coap_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions subsys/net/lib/nrf_cloud/src/nrf_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ int nrf_cloud_init(const struct nrf_cloud_init_param *param)
if (err) {
return err;
}
(void)nrf_cloud_print_details();

app_event_handler = param->event_handler;

Expand Down
1 change: 1 addition & 0 deletions subsys/net/lib/nrf_cloud/src/nrf_cloud_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
34 changes: 20 additions & 14 deletions subsys/net/lib/nrf_cloud/src/nrf_cloud_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

Expand All @@ -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;
}
7 changes: 7 additions & 0 deletions subsys/net/lib/nrf_cloud/src/nrf_cloud_rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/subsys/net/lib/nrf_cloud/cloud/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ if (CONFIG_NRF_CLOUD_MQTT OR CONFIG_NRF_CLOUD_FOTA OR CONFIG_NRF_MODEM_LIB)
${ZEPHYR_NRF_MODULE_DIR}/subsys/net/lib/nrf_cloud/src/nrf_cloud_fsm.c
${ZEPHYR_NRF_MODULE_DIR}/subsys/net/lib/nrf_cloud/src/nrf_cloud_transport.c
${ZEPHYR_NRF_MODULE_DIR}/subsys/net/lib/nrf_cloud/src/nrf_cloud_codec.c
${ZEPHYR_NRF_MODULE_DIR}/subsys/net/lib/nrf_cloud/src/nrf_cloud_info.c
DIRECTORY ${ZEPHYR_NRF_MODULE_DIR}/subsys/net/lib/nrf_cloud/
PROPERTIES HEADER_FILE_ONLY ON
)
Expand Down
6 changes: 6 additions & 0 deletions tests/subsys/net/lib/nrf_cloud/cloud/src/fakes.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ FAKE_VOID_FUNC(nrf_cloud_free, void *);
FAKE_VALUE_FUNC(int, poll, struct zsock_pollfd *, int, int);
FAKE_VALUE_FUNC(int, nrf_cloud_obj_cloud_encode, struct nrf_cloud_obj *const);
FAKE_VALUE_FUNC(int, nrf_cloud_obj_cloud_encoded_free, struct nrf_cloud_obj *const);
FAKE_VALUE_FUNC(int, nrf_cloud_print_details);

/* Custom fakes implementation */
int fake_nct_initialize__succeeds(const struct nrf_cloud_init_param *param)
Expand Down Expand Up @@ -346,3 +347,8 @@ int fake_nrf_cloud_obj_cloud_encode__fails(struct nrf_cloud_obj *const obj)
ARG_UNUSED(obj);
return -ENOMEM;
}

int fake_nrf_cloud_print_details__succeeds(void)
{
return 0;
}

0 comments on commit 547aaab

Please sign in to comment.