mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-08 12:37:01 +03:00
initial refactoring
This commit is contained in:
@@ -12,7 +12,8 @@
|
||||
#include "monitor.h"
|
||||
#include "network_ethernet.h"
|
||||
#include "network_wifi.h"
|
||||
#include "platform_config.h"
|
||||
// #include "Configurator.h"
|
||||
#pragma message("fixme: search for TODO below")
|
||||
#include "platform_esp32.h"
|
||||
#include "tools.h"
|
||||
#include "trace.h"
|
||||
@@ -21,162 +22,69 @@
|
||||
#define CONFIG_SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases"
|
||||
#endif
|
||||
static const char TAG[] = "network_status";
|
||||
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* network_status_ip_address = NULL;
|
||||
char* ip_info_json = NULL;
|
||||
cJSON* ip_info_cjson = NULL;
|
||||
static char lms_server_ip[IP4ADDR_STRLEN_MAX] = {0};
|
||||
static uint16_t lms_server_port = 0;
|
||||
static uint16_t lms_server_cport = 0;
|
||||
sys_Status status;
|
||||
SemaphoreHandle_t network_status_structure_mutex = NULL;
|
||||
static TaskHandle_t network_structure_locked_task = NULL;
|
||||
static void (*chained_notify)(in_addr_t, u16_t, u16_t);
|
||||
static void connect_notify(in_addr_t ip, u16_t hport, u16_t cport);
|
||||
|
||||
#define STA_IP_LEN sizeof(char) * IP4ADDR_STRLEN_MAX
|
||||
|
||||
void init_network_status() {
|
||||
chained_notify = server_notify;
|
||||
server_notify = connect_notify;
|
||||
ESP_LOGD(TAG, "init_network_status. Creating mutexes");
|
||||
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 = network_status_clear_ip_info_json(&ip_info_cjson);
|
||||
network_status_structure_mutex = xSemaphoreCreateMutex();
|
||||
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) {
|
||||
ESP_LOGE(TAG, "Unable to retrieve the release url from nvs");
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Found release url %s", release_url);
|
||||
|
||||
}
|
||||
ESP_LOGD(TAG, "About to set the STA IP String to 0.0.0.0");
|
||||
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(network_status_ip_address);
|
||||
cJSON_Delete(ip_info_cjson);
|
||||
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* 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, "network_status_get_new_json done");
|
||||
return cJSON_CreateObject();
|
||||
vSemaphoreDelete(network_status_structure_mutex);
|
||||
network_status_structure_mutex = NULL;
|
||||
}
|
||||
|
||||
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 (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* network_status_alloc_get_ip_info_json() {
|
||||
return cJSON_PrintUnformatted(ip_info_cjson);
|
||||
}
|
||||
|
||||
void network_status_unlock_json_buffer() {
|
||||
void network_status_unlock_structure() {
|
||||
ESP_LOGV(TAG, "Unlocking json buffer!");
|
||||
network_json_locked_task = NULL;
|
||||
xSemaphoreGive(network_status_json_mutex);
|
||||
network_structure_locked_task = NULL;
|
||||
xSemaphoreGive(network_status_structure_mutex);
|
||||
}
|
||||
|
||||
bool network_status_lock_json_buffer(TickType_t xTicksToWait) {
|
||||
ESP_LOGV(TAG, "Locking json buffer");
|
||||
bool network_status_lock_structure(TickType_t xTicksToWait) {
|
||||
ESP_LOGV(TAG, "Locking structure buffer");
|
||||
|
||||
TaskHandle_t calling_task = xTaskGetCurrentTaskHandle();
|
||||
if (calling_task == network_json_locked_task) {
|
||||
ESP_LOGV(TAG, "json buffer already locked to current task");
|
||||
if (calling_task == network_structure_locked_task) {
|
||||
ESP_LOGV(TAG, "structure 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;
|
||||
if (network_status_structure_mutex) {
|
||||
if (xSemaphoreTake(network_status_structure_mutex, xTicksToWait) == pdTRUE) {
|
||||
ESP_LOGV(TAG, "structure locked!");
|
||||
network_structure_locked_task = calling_task;
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Semaphore take failed. Unable to lock json buffer mutex");
|
||||
ESP_LOGE(TAG, "Semaphore take failed. Unable to lock structure mutex");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGV(TAG, "Unable to lock json buffer mutex");
|
||||
ESP_LOGV(TAG, "Unable to lock structure mutex");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void network_status_unlock_sta_ip_string() {
|
||||
network_status_ip_address_locked_task = NULL;
|
||||
xSemaphoreGive(network_status_ip_address_mutex);
|
||||
}
|
||||
|
||||
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 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* network_status_get_sta_ip_string() {
|
||||
return network_status_ip_address;
|
||||
return status.has_net && status.net.has_ip?status.net.ip.ip:"0.0.0.0";
|
||||
}
|
||||
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));
|
||||
lms_server_ip[sizeof(lms_server_ip) - 1] = '\0';
|
||||
ESP_LOGI(TAG, "LMS IP: %s, hport: %d, cport: %d", lms_server_ip, hport, cport);
|
||||
lms_server_port = hport;
|
||||
lms_server_cport = cport;
|
||||
if (network_status_lock_structure(portMAX_DELAY)) {
|
||||
status.has_LMS = true;
|
||||
strncpy(status.LMS.ip, inet_ntoa(ip), sizeof(status.LMS.ip));
|
||||
status.LMS.port = hport;
|
||||
status.LMS.cport = cport;
|
||||
ESP_LOGI(TAG, "LMS IP: %s, hport: %d, cport: %d", status.LMS.ip, status.LMS.port, status.LMS.cport);
|
||||
network_status_unlock_structure();
|
||||
}
|
||||
}
|
||||
static void connect_notify(in_addr_t ip, u16_t hport, u16_t cport) {
|
||||
set_lms_server_details(ip, hport, cport);
|
||||
@@ -185,163 +93,94 @@ static void connect_notify(in_addr_t ip, u16_t hport, u16_t cport) {
|
||||
network_async_update_status();
|
||||
}
|
||||
|
||||
void network_status_update_basic_info() {
|
||||
// locking happens below this level
|
||||
network_status_get_basic_info(&ip_info_cjson);
|
||||
}
|
||||
|
||||
cJSON* network_status_update_float(cJSON** root, const char* key, float value) {
|
||||
if (network_status_lock_json_buffer(portMAX_DELAY)) {
|
||||
if (*root == NULL) {
|
||||
*root = cJSON_CreateObject();
|
||||
}
|
||||
|
||||
if (key && strlen(key) != 0) {
|
||||
cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
|
||||
if (cjsonvalue) {
|
||||
cJSON_SetNumberValue(cjsonvalue, value);
|
||||
} else {
|
||||
cJSON_AddNumberToObject(*root, key, value);
|
||||
}
|
||||
}
|
||||
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 (network_status_lock_json_buffer(portMAX_DELAY)) {
|
||||
if (*root == NULL) {
|
||||
*root = cJSON_CreateObject();
|
||||
}
|
||||
|
||||
if (key && strlen(key) != 0) {
|
||||
cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
|
||||
if (cjsonvalue) {
|
||||
cjsonvalue->type = value ? cJSON_True : cJSON_False;
|
||||
} else {
|
||||
cJSON_AddBoolToObject(*root, key, value);
|
||||
}
|
||||
}
|
||||
network_status_unlock_json_buffer();
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Unable to lock status json buffer. ");
|
||||
}
|
||||
return *root;
|
||||
}
|
||||
cJSON * network_update_cjson_string(cJSON** root, const char* key, const char* value){
|
||||
if (network_status_lock_json_buffer(portMAX_DELAY)) {
|
||||
cjson_update_string(root, key, value);
|
||||
network_status_unlock_json_buffer();
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Unable to lock status json buffer. ");
|
||||
}
|
||||
return *root;
|
||||
}
|
||||
cJSON * network_update_cjson_number(cJSON** root, const char* key, int value){
|
||||
if (network_status_lock_json_buffer(portMAX_DELAY)) {
|
||||
cjson_update_number(root, key, value);
|
||||
network_status_unlock_json_buffer();
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Unable to lock status json buffer. ");
|
||||
}
|
||||
return *root;
|
||||
}
|
||||
cJSON* network_status_get_basic_info(cJSON** old) {
|
||||
if (network_status_lock_json_buffer(portMAX_DELAY)) {
|
||||
void network_status_set_basic_info() {
|
||||
if (network_status_lock_structure(portMAX_DELAY)) {
|
||||
network_t* nm = network_get_state_machine();
|
||||
monitor_gpio_t* mgpio = get_jack_insertion_gpio();
|
||||
sys_GPIO * gpio = NULL;
|
||||
const esp_app_desc_t* desc = esp_ota_get_app_description();
|
||||
|
||||
*old = network_update_cjson_string(old, "project_name", desc->project_name);
|
||||
status.has_platform = true;
|
||||
strncpy(status.platform.project, desc->project_name, sizeof(status.platform.project));
|
||||
#ifdef CONFIG_FW_PLATFORM_NAME
|
||||
*old = network_update_cjson_string(old, "platform_name", CONFIG_FW_PLATFORM_NAME);
|
||||
strncpy(status.platform.name, CONFIG_FW_PLATFORM_NAME, sizeof(status.platform.name));
|
||||
#endif
|
||||
*old = network_update_cjson_string(old, "version", desc->version);
|
||||
if (release_url != NULL)
|
||||
*old = network_update_cjson_string(old, "release_url", release_url);
|
||||
*old = network_update_cjson_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_update_cjson_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);
|
||||
strncpy(status.platform.version, desc->version, sizeof(status.platform.version));
|
||||
strncpy(status.platform.version, desc->version, sizeof(status.platform.version));
|
||||
status.platform.recovery = is_recovery_running;
|
||||
status.platform.depth = 16;
|
||||
#if DEPTH == 16 || DEPTH == 32
|
||||
status.platform.depth = DEPTH;
|
||||
#endif
|
||||
status.has_hw = true;
|
||||
|
||||
status.hw.jack_inserted = jack_inserted_svc();
|
||||
status.hw.has_jack_inserted = SYS_GPIOS_NAME(jack,gpio) && gpio->pin>=0;
|
||||
status.hw.has_spk_fault = SYS_GPIOS_NAME(spkfault,gpio) && gpio->pin>=0;
|
||||
status.hw.spk_fault = spkfault_svc();
|
||||
status.hw.batt_voltage = battery_value_svc();
|
||||
status.has_net = true;
|
||||
status.net.has_wifi = true;
|
||||
status.net.wifi.disconnect_count = nm->num_disconnect;
|
||||
status.net.wifi.avg_conn_time = nm->num_disconnect > 0 ? (nm->total_connected_time / nm->num_disconnect) : 0;
|
||||
|
||||
#ifdef CONFIG_BT_ENABLED
|
||||
*old = network_update_cjson_number(old, "bt_status", bt_app_source_get_a2d_state());
|
||||
*old = network_update_cjson_number(old, "bt_sub_status", bt_app_source_get_media_state());
|
||||
if(platform->has_services && platform->services.has_bt_sink && platform->services.bt_sink.enabled){
|
||||
status.has_bt = true;
|
||||
status.bt.bt_status = bt_app_source_get_a2d_state();
|
||||
status.bt.bt_media_state = bt_app_source_get_media_state();
|
||||
}
|
||||
#endif
|
||||
#if DEPTH == 16
|
||||
*old = network_update_cjson_number(old, "depth", 16);
|
||||
#elif DEPTH == 32
|
||||
*old = network_update_cjson_number(old, "depth", 32);
|
||||
#endif
|
||||
#if CONFIG_I2C_LOCKED
|
||||
*old = network_status_update_bool(old, "is_i2c_locked", true);
|
||||
#else
|
||||
*old = network_status_update_bool(old, "is_i2c_locked", false);
|
||||
#endif
|
||||
|
||||
if (network_ethernet_enabled()) {
|
||||
*old = network_status_update_bool(old, "eth_up", network_ethernet_is_up());
|
||||
}
|
||||
if (lms_server_cport > 0) {
|
||||
*old = network_update_cjson_number(old, "lms_cport", lms_server_cport);
|
||||
}
|
||||
|
||||
if (lms_server_port > 0) {
|
||||
*old = network_update_cjson_number(old, "lms_port", lms_server_port);
|
||||
}
|
||||
|
||||
if (strlen(lms_server_ip) > 0) {
|
||||
*old = network_update_cjson_string(old, "lms_ip", lms_server_ip);
|
||||
status.net.eth_up = network_ethernet_is_up();
|
||||
}
|
||||
ESP_LOGV(TAG, "network_status_get_basic_info done");
|
||||
network_status_unlock_json_buffer();
|
||||
network_status_unlock_structure();
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Unable to lock status json buffer. ");
|
||||
}
|
||||
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_update_cjson_string(&root, "ip", ip4addr_ntoa((ip4_addr_t*)&ip_info->ip));
|
||||
network_update_cjson_string(&root, "netmask", ip4addr_ntoa((ip4_addr_t*)&ip_info->netmask));
|
||||
network_update_cjson_string(&root, "gw", ip4addr_ntoa((ip4_addr_t*)&ip_info->gw));
|
||||
void network_status_update_address(esp_netif_ip_info_t* ip_info) {
|
||||
status.has_net = true;
|
||||
status.net.has_ip = true;
|
||||
strncpy(status.net.ip.ip,ip4addr_ntoa((ip4_addr_t*)&ip_info->ip),sizeof(status.net.ip.ip));
|
||||
strncpy(status.net.ip.netmask,ip4addr_ntoa((ip4_addr_t*)&ip_info->netmask),sizeof(status.net.ip.netmask));
|
||||
strncpy(status.net.ip.gw,ip4addr_ntoa((ip4_addr_t*)&ip_info->gw),sizeof(status.net.ip.gw));
|
||||
}
|
||||
void network_status_update_ip_info(update_reason_code_t update_reason_code) {
|
||||
void network_status_update_ip_info(sys_UPDATE_REASONS 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)) {
|
||||
if (network_status_lock_structure(portMAX_DELAY)) {
|
||||
/* generate the connection info with success */
|
||||
|
||||
ip_info_cjson = network_status_get_basic_info(&ip_info_cjson);
|
||||
ip_info_cjson = network_update_cjson_number(&ip_info_cjson, "urc", (int)update_reason_code);
|
||||
network_status_set_basic_info();
|
||||
status.net.updt_reason = update_reason_code;
|
||||
status.net.wifi.has_connected_sta = false;
|
||||
status.net.wifi.connected_sta.connected = false;
|
||||
|
||||
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()) || update_reason_code == UPDATE_FAILED_ATTEMPT ) {
|
||||
network_update_cjson_string(&ip_info_cjson, "if", "wifi");
|
||||
if (network_is_interface_connected(network_wifi_get_interface()) || update_reason_code == sys_UPDATE_REASONS_R_FAILED_ATTEMPT ) {
|
||||
status.net.interface = sys_CONNECTED_IF_IF_WIFI;
|
||||
|
||||
esp_netif_get_ip_info(network_wifi_get_interface(), &ip_info);
|
||||
network_status_update_address(ip_info_cjson, &ip_info);
|
||||
network_status_update_address(&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_update_cjson_string(&ip_info_cjson, "ssid", ((char*)ap.ssid));
|
||||
network_update_cjson_number(&ip_info_cjson, "rssi", ap.rssi);
|
||||
network_wifi_esp_sta_to_sta(&ap, &status.net.wifi.connected_sta);
|
||||
status.net.wifi.connected_sta.connected = true;
|
||||
status.net.has_wifi = true;
|
||||
status.net.wifi.has_connected_sta = true;
|
||||
}
|
||||
|
||||
} else {
|
||||
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())) {
|
||||
network_update_cjson_string(&ip_info_cjson, "if", "eth");
|
||||
status.net.interface = sys_CONNECTED_IF_IF_ETHERNET;
|
||||
esp_netif_get_ip_info(network_ethernet_get_interface(), &ip_info);
|
||||
network_status_update_address(ip_info_cjson, &ip_info);
|
||||
network_status_update_address(&ip_info);
|
||||
}
|
||||
network_status_unlock_json_buffer();
|
||||
network_status_unlock_structure();
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Unable to lock status json buffer. ");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user