Network manager implemented and relatively stable

This commit is contained in:
Sebastien L
2021-12-10 13:07:27 -05:00
parent 81756a7649
commit 63fbc2f645
66 changed files with 4528 additions and 2679 deletions

View File

@@ -1,3 +1,7 @@
#ifdef NETWORK_STATUS_LOG_LEVEL
#define LOG_LOCAL_LEVEL NETWORK_STATUS_LOG_LEVEL
#endif
#include "network_status.h"
#include <string.h>
#include "bt_app_core.h"
@@ -16,10 +20,12 @@
#define CONFIG_SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases"
#endif
static const char TAG[] = "network_status";
SemaphoreHandle_t wifi_manager_json_mutex = NULL;
SemaphoreHandle_t wifi_manager_sta_ip_mutex = NULL;
SemaphoreHandle_t network_status_json_mutex = NULL;
static TaskHandle_t network_json_locked_task = NULL;
SemaphoreHandle_t network_status_ip_address_mutex = NULL;
static TaskHandle_t network_status_ip_address_locked_task = NULL;
char* release_url = NULL;
char* wifi_manager_sta_ip = NULL;
char* network_status_ip_address = NULL;
char* ip_info_json = NULL;
cJSON* ip_info_cjson = NULL;
static char lms_server_ip[IP4ADDR_STRLEN_MAX] = {0};
@@ -33,11 +39,11 @@ void init_network_status() {
chained_notify = server_notify;
server_notify = connect_notify;
ESP_LOGD(TAG, "init_network_status. Creating mutexes");
wifi_manager_json_mutex = xSemaphoreCreateMutex();
wifi_manager_sta_ip_mutex = xSemaphoreCreateMutex();
network_status_json_mutex = xSemaphoreCreateMutex();
network_status_ip_address_mutex = xSemaphoreCreateMutex();
ip_info_json = NULL;
ESP_LOGD(TAG, "init_network_status. Creating status json structure");
ip_info_cjson = wifi_manager_clear_ip_info_json(&ip_info_cjson);
ip_info_cjson = network_status_clear_ip_info_json(&ip_info_cjson);
ESP_LOGD(TAG, "Getting release url ");
char* release_url = (char*)config_alloc_get_default(NVS_TYPE_STR, "release_url", QUOTE(CONFIG_SQUEEZELITE_ESP32_RELEASE_URL), 0);
if (release_url == NULL) {
@@ -46,57 +52,73 @@ void init_network_status() {
ESP_LOGD(TAG, "Found release url %s", release_url);
}
ESP_LOGD(TAG, "About to set the STA IP String to 0.0.0.0");
wifi_manager_sta_ip = (char*)malloc(STA_IP_LEN);
wifi_manager_safe_update_sta_ip_string(NULL);
network_status_ip_address = (char*)malloc_init_external(STA_IP_LEN);
network_status_safe_update_sta_ip_string(NULL);
}
void destroy_network_status() {
FREE_AND_NULL(release_url);
FREE_AND_NULL(ip_info_json);
FREE_AND_NULL(wifi_manager_sta_ip);
FREE_AND_NULL(network_status_ip_address);
cJSON_Delete(ip_info_cjson);
vSemaphoreDelete(wifi_manager_json_mutex);
wifi_manager_json_mutex = NULL;
vSemaphoreDelete(wifi_manager_sta_ip_mutex);
wifi_manager_sta_ip_mutex = NULL;
vSemaphoreDelete(network_status_json_mutex);
network_status_json_mutex = NULL;
vSemaphoreDelete(network_status_ip_address_mutex);
network_status_ip_address_mutex = NULL;
ip_info_cjson = NULL;
}
cJSON* wifi_manager_get_new_json(cJSON** old) {
ESP_LOGV(TAG, "wifi_manager_get_new_json called");
cJSON* network_status_get_new_json(cJSON** old) {
ESP_LOGV(TAG, "network_status_get_new_json called");
cJSON* root = *old;
if (root != NULL) {
cJSON_Delete(root);
*old = NULL;
}
ESP_LOGV(TAG, "wifi_manager_get_new_json done");
ESP_LOGV(TAG, "network_status_get_new_json done");
return cJSON_CreateObject();
}
cJSON* wifi_manager_clear_ip_info_json(cJSON** old) {
ESP_LOGV(TAG, "wifi_manager_clear_ip_info_json called");
cJSON* root = wifi_manager_get_basic_info(old);
ESP_LOGV(TAG, "wifi_manager_clear_ip_info_json done");
cJSON* network_status_clear_ip_info_json(cJSON** old) {
ESP_LOGV(TAG, "network_status_clear_ip_info_json called");
cJSON* root = network_status_get_basic_info(old);
cJSON_DeleteItemFromObjectCaseSensitive(root, "ip");
cJSON_DeleteItemFromObjectCaseSensitive(root, "netmask");
cJSON_DeleteItemFromObjectCaseSensitive(root, "gw");
cJSON_DeleteItemFromObjectCaseSensitive(root, "rssi");
cJSON_DeleteItemFromObjectCaseSensitive(root, "ssid");
cJSON_DeleteItemFromObjectCaseSensitive(root, "eth");
ESP_LOGV(TAG, "network_status_clear_ip_info_json done");
return root;
}
void network_status_clear_ip() {
if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
ip_info_cjson = wifi_manager_clear_ip_info_json(&ip_info_cjson);
wifi_manager_unlock_json_buffer();
if (network_status_lock_json_buffer(portMAX_DELAY)) {
ip_info_cjson = network_status_clear_ip_info_json(&ip_info_cjson);
network_status_unlock_json_buffer();
}
}
char* wifi_manager_alloc_get_ip_info_json() {
char* network_status_alloc_get_ip_info_json() {
return cJSON_PrintUnformatted(ip_info_cjson);
}
void wifi_manager_unlock_json_buffer() {
void network_status_unlock_json_buffer() {
ESP_LOGV(TAG, "Unlocking json buffer!");
xSemaphoreGive(wifi_manager_json_mutex);
network_json_locked_task = NULL;
xSemaphoreGive(network_status_json_mutex);
}
bool wifi_manager_lock_json_buffer(TickType_t xTicksToWait) {
bool network_status_lock_json_buffer(TickType_t xTicksToWait) {
ESP_LOGV(TAG, "Locking json buffer");
if (wifi_manager_json_mutex) {
if (xSemaphoreTake(wifi_manager_json_mutex, xTicksToWait) == pdTRUE) {
TaskHandle_t calling_task = xTaskGetCurrentTaskHandle();
if (calling_task == network_json_locked_task) {
ESP_LOGV(TAG, "json buffer already locked to current task");
return true;
}
if (network_status_json_mutex) {
if (xSemaphoreTake(network_status_json_mutex, xTicksToWait) == pdTRUE) {
ESP_LOGV(TAG, "Json buffer locked!");
network_json_locked_task = calling_task;
return true;
} else {
ESP_LOGE(TAG, "Semaphore take failed. Unable to lock json buffer mutex");
@@ -108,9 +130,15 @@ bool wifi_manager_lock_json_buffer(TickType_t xTicksToWait) {
}
}
bool wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait) {
if (wifi_manager_sta_ip_mutex) {
if (xSemaphoreTake(wifi_manager_sta_ip_mutex, xTicksToWait) == pdTRUE) {
bool network_status_lock_sta_ip_string(TickType_t xTicksToWait) {
TaskHandle_t calling_task = xTaskGetCurrentTaskHandle();
if (calling_task == network_status_ip_address_locked_task) {
ESP_LOGD(TAG, "json buffer already locked to current task ");
return true;
}
if (network_status_ip_address_mutex) {
if (xSemaphoreTake(network_status_ip_address_mutex, xTicksToWait) == pdTRUE) {
network_status_ip_address_locked_task = calling_task;
return true;
} else {
return false;
@@ -120,26 +148,27 @@ bool wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait) {
}
}
void wifi_manager_unlock_sta_ip_string() {
xSemaphoreGive(wifi_manager_sta_ip_mutex);
void network_status_unlock_sta_ip_string() {
network_status_ip_address_locked_task = NULL;
xSemaphoreGive(network_status_ip_address_mutex);
}
void wifi_manager_safe_update_sta_ip_string(esp_ip4_addr_t* ip4) {
if (wifi_manager_lock_sta_ip_string(portMAX_DELAY)) {
strcpy(wifi_manager_sta_ip, ip4 != NULL ? ip4addr_ntoa(ip4) : "0.0.0.0");
ESP_LOGD(TAG, "Set STA IP String to: %s", wifi_manager_sta_ip);
wifi_manager_unlock_sta_ip_string();
void network_status_safe_update_sta_ip_string(esp_ip4_addr_t* ip4) {
if (network_status_lock_sta_ip_string(portMAX_DELAY)) {
strcpy(network_status_ip_address, ip4 != NULL ? ip4addr_ntoa((ip4_addr_t*)ip4) : "0.0.0.0");
ESP_LOGD(TAG, "Set STA IP String to: %s", network_status_ip_address);
network_status_unlock_sta_ip_string();
}
}
void wifi_manager_safe_reset_sta_ip_string() {
if (wifi_manager_lock_sta_ip_string(portMAX_DELAY)) {
strcpy(wifi_manager_sta_ip, "0.0.0.0");
ESP_LOGD(TAG, "Set STA IP String to: %s", wifi_manager_sta_ip);
wifi_manager_unlock_sta_ip_string();
void network_status_safe_reset_sta_ip_string() {
if (network_status_lock_sta_ip_string(portMAX_DELAY)) {
strcpy(network_status_ip_address, "0.0.0.0");
ESP_LOGD(TAG, "Set STA IP String to: %s", network_status_ip_address);
network_status_unlock_sta_ip_string();
}
}
char* wifi_manager_get_sta_ip_string() {
return wifi_manager_sta_ip;
char* network_status_get_sta_ip_string() {
return network_status_ip_address;
}
void set_lms_server_details(in_addr_t ip, u16_t hport, u16_t cport) {
strncpy(lms_server_ip, inet_ntoa(ip), sizeof(lms_server_ip));
@@ -152,98 +181,47 @@ static void connect_notify(in_addr_t ip, u16_t hport, u16_t cport) {
set_lms_server_details(ip, hport, cport);
if (chained_notify)
(*chained_notify)(ip, hport, cport);
network_manager_async_update_status();
network_async_update_status();
}
void wifi_manager_update_basic_info() {
int32_t total_connected_time = 0;
int64_t last_connected = 0;
uint16_t num_disconnect = 0;
network_wifi_get_stats(&total_connected_time, &last_connected, &num_disconnect);
if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
monitor_gpio_t* mgpio = get_jack_insertion_gpio();
cJSON* voltage = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "Voltage");
if (voltage) {
cJSON_SetNumberValue(voltage, battery_value_svc());
}
cJSON* bt_status = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "bt_status");
if (bt_status) {
cJSON_SetNumberValue(bt_status, bt_app_source_get_a2d_state());
}
cJSON* bt_sub_status = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "bt_sub_status");
if (bt_sub_status) {
cJSON_SetNumberValue(bt_sub_status, bt_app_source_get_media_state());
}
cJSON* jack = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "Jack");
if (jack) {
jack->type = mgpio->gpio >= 0 && jack_inserted_svc() ? cJSON_True : cJSON_False;
}
cJSON* disconnect_count = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "disconnect_count");
if (disconnect_count) {
cJSON_SetNumberValue(disconnect_count, num_disconnect);
}
cJSON* avg_conn_time = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "avg_conn_time");
if (avg_conn_time) {
cJSON_SetNumberValue(avg_conn_time, num_disconnect > 0 ? (total_connected_time / num_disconnect) : 0);
}
if (lms_server_cport > 0) {
cJSON* value = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "lms_cport");
if (value) {
cJSON_SetNumberValue(value, lms_server_cport);
} else {
cJSON_AddNumberToObject(ip_info_cjson, "lms_cport", lms_server_cport);
}
}
if (lms_server_port > 0) {
cJSON* value = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "lms_port");
if (value) {
cJSON_SetNumberValue(value, lms_server_port);
} else {
cJSON_AddNumberToObject(ip_info_cjson, "lms_port", lms_server_port);
}
}
if (strlen(lms_server_ip) > 0) {
cJSON* value = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "lms_ip");
if (!value) {
// only create if it does not exist. Since we're creating a reference
// to a char buffer, updates to cJSON aren't needed
cJSON_AddItemToObject(ip_info_cjson, "lms_ip", cJSON_CreateStringReference(lms_server_ip));
}
}
if (network_ethernet_enabled()) {
cJSON* eth = cJSON_GetObjectItemCaseSensitive(ip_info_cjson, "eth_up");
if (eth) {
eth->type = network_ethernet_is_up() ? cJSON_True : cJSON_False;
}
}
wifi_manager_unlock_json_buffer();
}
void network_status_update_basic_info() {
// locking happens below this level
network_status_get_basic_info(&ip_info_cjson);
}
cJSON* network_status_update_string(cJSON** root, const char* key, const char* value) {
if (*root == NULL) {
*root = cJSON_CreateObject();
}
if (network_status_lock_json_buffer(portMAX_DELAY)) {
if (*root == NULL) {
*root = cJSON_CreateObject();
if (*root == NULL) {
ESP_LOGE(TAG, "Error creating cJSON object!");
}
}
if (!key || !value || strlen(key) == 0)
return *root;
cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
if (cjsonvalue && strcasecmp(cJSON_GetStringValue(cjsonvalue), value) != 0) {
ESP_LOGD(TAG, "Value %s changed from %s to %s", key, cJSON_GetStringValue(cjsonvalue), value);
cJSON_SetValuestring(cjsonvalue, value);
if (!key || !value || strlen(key) == 0) {
ESP_LOGE(TAG, "network_status_update_string. Invalid key or value passed! key: %s, value: %s", STR_OR_ALT(key, ""), STR_OR_ALT(value, ""));
network_status_unlock_json_buffer();
return *root;
}
cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
if (cjsonvalue && strcasecmp(cJSON_GetStringValue(cjsonvalue), value) != 0) {
ESP_LOGD(TAG, "Value %s changed from %s to %s", key, cJSON_GetStringValue(cjsonvalue), value);
cJSON_SetValuestring(cjsonvalue, value);
} else {
cJSON_AddItemToObject(*root, key, cJSON_CreateString(value));
}
network_status_unlock_json_buffer();
} else {
cJSON_AddItemToObject(*root, key, cJSON_CreateString(value));
ESP_LOGW(TAG, "Unable to lock status json buffer. ");
}
return *root;
}
cJSON* network_status_update_number(cJSON** root, const char* key, int value) {
if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
if (network_status_lock_json_buffer(portMAX_DELAY)) {
if (*root == NULL) {
*root = cJSON_CreateObject();
}
if (key && value && strlen(key) != 0) {
if (key && strlen(key) != 0) {
cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
if (cjsonvalue) {
cJSON_SetNumberValue(cjsonvalue, value);
@@ -251,14 +229,14 @@ cJSON* network_status_update_number(cJSON** root, const char* key, int value) {
cJSON_AddNumberToObject(*root, key, value);
}
}
wifi_manager_unlock_json_buffer();
network_status_unlock_json_buffer();
} else {
ESP_LOGW(TAG, "Unable to lock status json buffer. ");
}
return *root;
}
cJSON* network_status_update_float(cJSON** root, const char* key, float value) {
if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
if (network_status_lock_json_buffer(portMAX_DELAY)) {
if (*root == NULL) {
*root = cJSON_CreateObject();
}
@@ -271,14 +249,14 @@ cJSON* network_status_update_float(cJSON** root, const char* key, float value) {
cJSON_AddNumberToObject(*root, key, value);
}
}
wifi_manager_unlock_json_buffer();
network_status_unlock_json_buffer();
} else {
ESP_LOGW(TAG, "Unable to lock status json buffer. ");
}
return *root;
}
cJSON* network_status_update_bool(cJSON** root, const char* key, bool value) {
if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
if (network_status_lock_json_buffer(portMAX_DELAY)) {
if (*root == NULL) {
*root = cJSON_CreateObject();
}
@@ -291,91 +269,110 @@ cJSON* network_status_update_bool(cJSON** root, const char* key, bool value) {
cJSON_AddBoolToObject(*root, key, value);
}
}
wifi_manager_unlock_json_buffer();
network_status_unlock_json_buffer();
} else {
ESP_LOGW(TAG, "Unable to lock status json buffer. ");
}
return *root;
}
cJSON* wifi_manager_get_basic_info(cJSON** old) {
int32_t total_connected_time = 0;
int64_t last_connected = 0;
uint16_t num_disconnect = 0;
network_wifi_get_stats(&total_connected_time, &last_connected, &num_disconnect);
cJSON* network_status_get_basic_info(cJSON** old) {
if (network_status_lock_json_buffer(portMAX_DELAY)) {
network_t* nm = network_get_state_machine();
monitor_gpio_t* mgpio = get_jack_insertion_gpio();
const esp_app_desc_t* desc = esp_ota_get_app_description();
monitor_gpio_t* mgpio = get_jack_insertion_gpio();
const esp_app_desc_t* desc = esp_ota_get_app_description();
ESP_LOGV(TAG, "wifi_manager_get_basic_info called");
cJSON* root = network_status_update_string(&root, "project_name", desc->project_name);
*old = network_status_update_string(old, "project_name", desc->project_name);
#ifdef CONFIG_FW_PLATFORM_NAME
root = network_status_update_string(&root, "platform_name", CONFIG_FW_PLATFORM_NAME);
*old = network_status_update_string(old, "platform_name", CONFIG_FW_PLATFORM_NAME);
#endif
root = network_status_update_string(&root, "version", desc->version);
if (release_url != NULL)
root = network_status_update_string(&root, "release_url", release_url);
root = network_status_update_number(&root, "recovery", is_recovery_running ? 1 : 0);
root = network_status_update_bool(&root, "Jack", mgpio->gpio >= 0 && jack_inserted_svc());
root = network_status_update_float(&root, "Voltage", battery_value_svc());
root = network_status_update_number(&root, "disconnect_count", num_disconnect);
root = network_status_update_float(&root, "avg_conn_time", num_disconnect > 0 ? (total_connected_time / num_disconnect) : 0);
root = network_status_update_number(&root, "bt_status", bt_app_source_get_a2d_state());
root = network_status_update_number(&root, "bt_sub_status", bt_app_source_get_media_state());
*old = network_status_update_string(old, "version", desc->version);
if (release_url != NULL)
*old = network_status_update_string(old, "release_url", release_url);
*old = network_status_update_number(old, "recovery", is_recovery_running ? 1 : 0);
*old = network_status_update_bool(old, "Jack", mgpio->gpio >= 0 && jack_inserted_svc());
*old = network_status_update_float(old, "Voltage", battery_value_svc());
*old = network_status_update_number(old, "disconnect_count", nm->num_disconnect);
*old = network_status_update_float(old, "avg_conn_time", nm->num_disconnect > 0 ? (nm->total_connected_time / nm->num_disconnect) : 0);
*old = network_status_update_number(old, "bt_status", bt_app_source_get_a2d_state());
*old = network_status_update_number(old, "bt_sub_status", bt_app_source_get_media_state());
#if CONFIG_I2C_LOCKED
root = network_status_update_bool(&root, "is_i2c_locked", true);
*old = network_status_update_bool(old, "is_i2c_locked", true);
#else
root = network_status_update_bool(&root, "is_i2c_locked", false);
*old = network_status_update_bool(old, "is_i2c_locked", false);
#endif
if (network_ethernet_enabled()) {
root = network_status_update_bool(&root, "eth_up", network_ethernet_is_up());
}
ESP_LOGV(TAG, "wifi_manager_get_basic_info done");
return root;
}
if (network_ethernet_enabled()) {
*old = network_status_update_bool(old, "eth_up", network_ethernet_is_up());
}
if (lms_server_cport > 0) {
*old = network_status_update_number(old, "lms_cport", lms_server_cport);
}
void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code) {
ESP_LOGD(TAG, "wifi_manager_generate_ip_info_json called");
if (lms_server_port > 0) {
*old = network_status_update_number(old, "lms_port", lms_server_port);
}
if (wifi_manager_lock_json_buffer(portMAX_DELAY)) {
/* generate the connection info with success */
ip_info_cjson = wifi_manager_get_basic_info(&ip_info_cjson);
wifi_manager_unlock_json_buffer();
if (strlen(lms_server_ip) > 0) {
*old = network_status_update_string(old, "lms_ip", lms_server_ip);
}
ESP_LOGV(TAG, "network_status_get_basic_info done");
network_status_unlock_json_buffer();
} else {
ESP_LOGW(TAG, "Unable to lock status json buffer. ");
}
ip_info_cjson = network_status_update_number(&ip_info_cjson, "urc", update_reason_code);
if (update_reason_code == UPDATE_CONNECTION_OK || update_reason_code == UPDATE_ETHERNET_CONNECTED) {
/* rest of the information is copied after the ssid */
tcpip_adapter_ip_info_t ip_info;
esp_netif_get_ip_info(network_wifi_get_interface(), &ip_info);
network_status_update_string(&ip_info_cjson, "ip", ip4addr_ntoa((ip4_addr_t*)&ip_info.ip));
network_status_update_string(&ip_info_cjson, "netmask", ip4addr_ntoa((ip4_addr_t*)&ip_info.netmask));
network_status_update_string(&ip_info_cjson, "gw", ip4addr_ntoa((ip4_addr_t*)&ip_info.gw));
wifi_mode_t mode;
if (esp_wifi_get_mode(&mode) == ESP_OK && (mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA)) {
/* wifi is active, and associated to an AP */
wifi_ap_record_t ap;
esp_wifi_sta_get_ap_info(&ap);
network_status_update_string(&ip_info_cjson, "ssid", ((char*)ap.ssid));
network_status_update_number(&ip_info_cjson, "rssi", ap.rssi);
}
if (network_ethernet_is_up()) {
esp_netif_get_ip_info(network_ethernet_get_interface(), &ip_info);
cJSON* ethernet_ip = cJSON_CreateObject();
cJSON_AddItemToObject(ethernet_ip, "ip", cJSON_CreateString(ip4addr_ntoa((ip4_addr_t*)&ip_info.ip)));
cJSON_AddItemToObject(ethernet_ip, "netmask", cJSON_CreateString(ip4addr_ntoa((ip4_addr_t*)&ip_info.netmask)));
cJSON_AddItemToObject(ethernet_ip, "gw", cJSON_CreateString(ip4addr_ntoa((ip4_addr_t*)&ip_info.gw)));
cJSON_AddItemToObject(ip_info_cjson, "eth", ethernet_ip);
}
} else {
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "ip");
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "netmask");
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "gw");
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "rssi");
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "ssid");
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "eth");
}
ESP_LOGV(TAG, "wifi_manager_generate_ip_info_json done");
return *old;
}
void network_status_update_address(cJSON* root, esp_netif_ip_info_t* ip_info) {
if (!root || !ip_info) {
ESP_LOGE(TAG, "Cannor update IP address. JSON structure or ip_info is null");
return;
}
network_status_update_string(&root, "ip", ip4addr_ntoa((ip4_addr_t*)&ip_info->ip));
network_status_update_string(&root, "netmask", ip4addr_ntoa((ip4_addr_t*)&ip_info->netmask));
network_status_update_string(&root, "gw", ip4addr_ntoa((ip4_addr_t*)&ip_info->gw));
}
void network_status_update_ip_info(update_reason_code_t update_reason_code) {
ESP_LOGV(TAG, "network_status_update_ip_info called");
esp_netif_ip_info_t ip_info;
if (network_status_lock_json_buffer(portMAX_DELAY)) {
/* generate the connection info with success */
ip_info_cjson = network_status_get_basic_info(&ip_info_cjson);
ip_info_cjson = network_status_update_number(&ip_info_cjson, "urc", update_reason_code);
ESP_LOGD(TAG,"Updating ip info with reason code %d. Checking if Wifi interface is connected",update_reason_code);
if (network_is_interface_connected(network_wifi_get_interface())) {
esp_netif_get_ip_info(network_wifi_get_interface(), &ip_info);
network_status_update_address(ip_info_cjson, &ip_info);
if (!network_wifi_is_ap_mode()) {
/* wifi is active, and associated to an AP */
wifi_ap_record_t ap;
esp_wifi_sta_get_ap_info(&ap);
network_status_update_string(&ip_info_cjson, "ssid", ((char*)ap.ssid));
network_status_update_number(&ip_info_cjson, "rssi", ap.rssi);
}
} else {
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "ip");
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "netmask");
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "gw");
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "rssi");
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "ssid");
}
ESP_LOGD(TAG,"Checking if ethernet interface is connected");
if (network_is_interface_connected(network_ethernet_get_interface())) {
cJSON* ethernet_ip = cJSON_GetObjectItem(ip_info_cjson, "eth");
if (!ethernet_ip) {
ethernet_ip = cJSON_CreateObject();
cJSON_AddItemToObject(ip_info_cjson, "eth", ethernet_ip);
}
esp_netif_get_ip_info(network_ethernet_get_interface(), &ip_info);
network_status_update_address(ethernet_ip, &ip_info);
} else {
cJSON_DeleteItemFromObjectCaseSensitive(ip_info_cjson, "eth");
}
network_status_unlock_json_buffer();
} else {
ESP_LOGW(TAG, "Unable to lock status json buffer. ");
}
ESP_LOGV(TAG, "wifi_status_generate_ip_info_json done");
}