Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/esp_insights/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.0")
list(APPEND priv_req esp_wifi)
endif()

if(CONFIG_ESP_INSIGHTS_THREAD_ENABLED)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not create on the new config for thread enabled. We can use the global one.

list(APPEND priv_req openthread)
endif()

set(pub_req esp_diagnostics)

idf_component_register(SRCS ${srcs}
Expand Down
7 changes: 7 additions & 0 deletions components/esp_insights/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ menu "ESP Insights"
help
For users already using older metadata, this provides an option to keep using the same.
This is important as the new metadata version (1.1), is not backwad compatible.

config ESP_INSIGHTS_THREAD_ENABLED
bool "Check for Thread networks"
default n
help
For nodes connected to Thread networks, this option must be set to 'yes'.
This tells Insights to use the thread network for sending data to cloud.
endmenu
57 changes: 52 additions & 5 deletions components/esp_insights/src/esp_insights.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include "esp_insights_encoder.h"
#include "esp_insights_cbor_decoder.h"

#ifdef CONFIG_ESP_INSIGHTS_THREAD_ENABLED
#include "esp_openthread.h"
#endif

#ifdef CONFIG_ESP_INSIGHTS_CMD_RESP_ENABLED
#define INSIGHTS_CMD_RESP 1
#endif
Expand Down Expand Up @@ -133,12 +137,15 @@ static void esp_insights_first_call(void *priv_data)
xTimerStart(entry->timer, 0);
}

/* Returns true if wifi is connected and insights is enabled, false otherwise */
static bool is_insights_active(void)
/* Returns true if ethernet is connected, false otherwise */
static bool is_ethernet_connected(void)
{
wifi_ap_record_t ap_info;
bool wifi_connected = esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK;
return wifi_connected && s_insights_data.enabled;
esp_netif_t *eth = esp_netif_get_handle_from_ifkey("ETH_DEF");
if (eth == NULL) {
return false;
}

return esp_netif_is_netif_up(eth);
}

/* Returns true if wifi is connected, false otherwise */
Expand All @@ -149,6 +156,46 @@ static bool is_wifi_connected(void)
return wifi_connected;
}

/**
* Returns true if connected to a thread network with a border router, false if not.
* In case its unable to identify a BR, returns true.
*/
static bool is_thread_with_br_connected(void)
{
#ifdef CONFIG_ESP_INSIGHTS_THREAD_ENABLED
esp_netif_t *ot = esp_netif_get_handle_from_ifkey("OT_DEF");
if (ot == NULL) return false;

if(!esp_netif_is_netif_up(ot)) return false;

otInstance *instance = esp_openthread_get_instance();
if (!instance) return false;

otNetworkDataIterator it = OT_NETWORK_DATA_ITERATOR_INIT;
otBorderRouterConfig cfg;

while (otNetDataGetNextOnMeshPrefix(instance, &it, &cfg) == OT_ERROR_NONE) {
if (cfg.mDefaultRoute) {
return true;
}
}

return false;
#else
return false;
#endif
}

/* Returns true if connected to the internet and insights is enabled, false otherwise */
static bool is_insights_active(void)
{
bool wifi_connected = is_wifi_connected();
bool ethernet_connected = is_ethernet_connected();
bool is_thread_with_br = is_thread_with_br_connected();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be more clear, let's add new variable named, bool is_connected_to_internet = wifi_connected || ethernet_connected || is_thread_with_br;

return (wifi_connected || ethernet_connected || is_thread_with_br) && s_insights_data.enabled;
}

/* This executes in the context of timer task.
*
* There is a dynamic logic to decide the next instance when the insights
Expand Down