Skip to content

Commit

Permalink
net: lib: nrf_cloud: Separate cloud info by state
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 time 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>
  • Loading branch information
plskeggs committed Sep 26, 2024
1 parent c6b8384 commit 129df2e
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ 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.
* A call to the :c:func:`nrf_cloud_print_details` and :c:func:`nrf_cloud_print_cloud_details` functions 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 Down Expand Up @@ -807,8 +807,8 @@ 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_VERBOSE_DETAILS` Kconfig option to enable the above functions to print all details instead of only the device ID.

* Updated:

Expand Down
14 changes: 14 additions & 0 deletions include/net/nrf_cloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,20 @@ int nrf_cloud_uninit(void);
*/
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 stage and the tenant will be printed.
* Call this function after your cloud event handler receives the first
* NRF_CLOUD_EVT_RX_DATA_SHADOW event.
*
* 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
5 changes: 0 additions & 5 deletions samples/cellular/nrf_cloud_rest_cell_location/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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
10 changes: 8 additions & 2 deletions subsys/net/lib/nrf_cloud/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ 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
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.
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
2 changes: 2 additions & 0 deletions subsys/net/lib/nrf_cloud/src/nrf_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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_PRINT_DETAILS) && 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

0 comments on commit 129df2e

Please sign in to comment.