Browse Source

Move a few network manager parameters to nvs config

Sebastien L 3 years ago
parent
commit
a354e6248a

+ 7 - 3
components/driver_bt/bt_app_sink.c

@@ -37,9 +37,9 @@
 #define APP_RC_CT_TL_RN_PLAYBACK_CHANGE  (3)
 #define APP_RC_CT_TL_RN_PLAY_POS_CHANGE  (4)
 
-#define BT_AV_TAG               "BT_AV"
-#define BT_RC_TG_TAG            "RCTG"
-#define BT_RC_CT_TAG            "RCCT"
+#define BT_AV_TAG               "bt_app"
+#define BT_RC_TG_TAG            "bt_rctg"
+#define BT_RC_CT_TAG            "bt_rcct"
 
 #ifndef CONFIG_BT_NAME
 #define CONFIG_BT_NAME	"ESP32-BT"
@@ -218,6 +218,10 @@ void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
         bt_app_work_dispatch(bt_av_hdl_a2d_evt, event, param, sizeof(esp_a2d_cb_param_t), NULL);
         break;
     }
+    case ESP_A2D_PROF_STATE_EVT: {
+        ESP_LOGI(BT_AV_TAG, "Bluetooth Init complete");
+        break;
+    }    
     default:
         ESP_LOGE(BT_AV_TAG, "Invalid A2DP event: %d", event);
         break;

+ 5 - 1
components/driver_bt/bt_app_source.c

@@ -27,6 +27,7 @@ extern int32_t 	output_bt_data(uint8_t *data, int32_t len);
 extern void 	output_bt_tick(void);
 extern char*	output_state_str(void);
 extern bool		output_stopped(void);
+extern bool is_recovery_running;
 
 static void bt_app_av_state_connecting(uint16_t event, void *param);
 static void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param);
@@ -191,7 +192,10 @@ static void peers_list_maintain(const char * s_peer_bdname, int32_t rssi){
 }
 
 int bt_app_source_get_a2d_state(){
-    ESP_LOGD(TAG,"a2dp status: %u = %s", bt_app_source_a2d_state, APP_AV_STATE_DESC[bt_app_source_a2d_state]);
+    if(!is_recovery_running){
+        // if we are in recovery mode, don't log BT status
+        ESP_LOGD(TAG,"a2dp status: %u = %s", bt_app_source_a2d_state, APP_AV_STATE_DESC[bt_app_source_a2d_state]);
+    }
     return bt_app_source_a2d_state;
 }
 int bt_app_source_get_media_state(){

+ 5 - 7
components/wifi-manager/network_manager.h

@@ -14,13 +14,6 @@
 extern "C" {
 #endif
 
-
-#define STA_POLLING_MIN (15 * 1000)
-#define STA_POLLING_MAX (10 * 60 * 1000)
-#define ETH_LINK_DOWN_REBOOT (4 * 1000)
-#define ETH_DHCP_FAIL (6 * 1000)
-#define WIFI_DHCP_FAIL (6 * 1000)
-
 /*
  *  --------------------- ENUMERATION ---------------------
  */
@@ -145,6 +138,11 @@ typedef struct
 	esp_netif_t *wifi_netif;
 	esp_netif_t *eth_netif;
 	esp_netif_t *wifi_ap_netif;
+    uint16_t sta_polling_min_ms;
+    uint16_t sta_polling_max_ms;
+    uint16_t eth_link_down_reboot_ms;
+    uint16_t dhcp_timeout;
+    uint16_t wifi_dhcp_fail_ms;    
 	queue_message * event_parameters;
 } network_t;
 

+ 38 - 8
components/wifi-manager/network_manager_handlers.c

@@ -146,7 +146,7 @@ static void network_connect_active_ssid(state_machine_t* const State_Machine) {
             network_async(EN_ETHERNET_FALLBACK);
         } else {
             // returning to AP mode
-            nm->STA_duration = STA_POLLING_MIN;
+            nm->STA_duration = nm->sta_polling_min_ms;
             ESP_LOGD(TAG, "No ethernet and no wifi configured. Go to configuration mode");
             network_async_configure();
         }
@@ -219,8 +219,34 @@ static state_machine_result_t NETWORK_INSTANTIATED_STATE_entry_handler(state_mac
 static state_machine_result_t NETWORK_INSTANTIATED_STATE_handler(state_machine_t* const State_Machine) {
     network_handler_print(State_Machine,true);
     state_machine_result_t result = EVENT_UN_HANDLED;
+    network_t* const nm = (network_t *)State_Machine;
     State_Machine->State = &network_states[NETWORK_INSTANTIATED_STATE];
     State_Machine->Event = EN_START;
+      char * valuestr=NULL;
+    valuestr=config_alloc_get_default(NVS_TYPE_STR,"pollmx","600",0);
+    if (valuestr) {
+        nm->sta_polling_max_ms = atoi(valuestr)*1000;
+        ESP_LOGD(TAG, "sta_polling_max_ms set to %d", nm->sta_polling_max_ms);
+        FREE_AND_NULL(valuestr);
+    }   
+    valuestr=config_alloc_get_default(NVS_TYPE_STR,"pollmin","15",0);
+    if (valuestr) {
+        nm->sta_polling_min_ms = atoi(valuestr)*1000;
+        ESP_LOGD(TAG, "sta_polling_min_ms set to %d", nm->sta_polling_min_ms);
+        FREE_AND_NULL(valuestr);
+    }
+    valuestr=config_alloc_get_default(NVS_TYPE_STR,"ethtmout","8",0);
+    if (valuestr) {
+        nm->eth_link_down_reboot_ms = atoi(valuestr)*1000;
+        ESP_LOGD(TAG, "ethtmout set to %d", nm->eth_link_down_reboot_ms);
+        FREE_AND_NULL(valuestr);
+    }
+    valuestr=config_alloc_get_default(NVS_TYPE_STR,"dhcp_tmout","8",0);
+    if(valuestr){
+        nm->dhcp_timeout = atoi(valuestr)*1000;
+        ESP_LOGD(TAG, "dhcp_timeout set to %d", nm->dhcp_timeout);
+        FREE_AND_NULL(valuestr);
+    }
     HANDLE_GLOBAL_EVENT(State_Machine);
     if (State_Machine->Event == EN_START) {
         result= local_traverse_state(State_Machine, &network_states[NETWORK_INITIALIZING_STATE],__FUNCTION__);
@@ -333,7 +359,8 @@ static state_machine_result_t ETH_STARTING_STATE_exit_handler(state_machine_t* c
  */
 static state_machine_result_t NETWORK_ETH_ACTIVE_STATE_entry_handler(state_machine_t* const State_Machine) {
     network_handler_entry_print(State_Machine,true);
-    network_set_timer(ETH_LINK_DOWN_REBOOT);
+    network_t* const nm = (network_t *)State_Machine;
+    network_set_timer(nm->eth_link_down_reboot_ms);
     NETWORK_EXECUTE_CB(State_Machine);
     network_handler_entry_print(State_Machine,false);
     return EVENT_HANDLED;
@@ -438,7 +465,8 @@ static state_machine_result_t ETH_CONNECTING_NEW_STATE_exit_handler(state_machin
  */
 static state_machine_result_t ETH_ACTIVE_LINKDOWN_STATE_entry_handler(state_machine_t* const State_Machine) {
     network_handler_entry_print(State_Machine,true);
-    network_set_timer(ETH_LINK_DOWN_REBOOT);
+    network_t* const nm = (network_t *)State_Machine;
+    network_set_timer(nm->eth_link_down_reboot_ms);
     NETWORK_EXECUTE_CB(State_Machine);
     messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "Ethernet link down.");
     network_handler_entry_print(State_Machine,false);
@@ -463,7 +491,8 @@ static state_machine_result_t ETH_ACTIVE_LINKDOWN_STATE_exit_handler(state_machi
  */
 static state_machine_result_t ETH_ACTIVE_LINKUP_STATE_entry_handler(state_machine_t* const State_Machine) {
     network_handler_entry_print(State_Machine,true);
-    network_set_timer(ETH_DHCP_FAIL);
+    network_t* const nm = (network_t *)State_Machine;
+    network_set_timer(nm->dhcp_timeout);
     NETWORK_EXECUTE_CB(State_Machine);
     messaging_post_message(MESSAGING_INFO, MESSAGING_CLASS_SYSTEM, "Ethernet link up.");
     network_handler_entry_print(State_Machine,false);
@@ -682,12 +711,13 @@ static state_machine_result_t WIFI_CONFIGURING_CONNECT_STATE_entry_handler(state
 static state_machine_result_t WIFI_CONFIGURING_CONNECT_STATE_handler(state_machine_t* const State_Machine) {
     HANDLE_GLOBAL_EVENT(State_Machine);
     network_handler_print(State_Machine,true);
+    network_t* const nm = (network_t *)State_Machine;
     state_machine_result_t result = EVENT_HANDLED;
     switch (State_Machine->Event) {
         case EN_CONNECTED:
             result=EVENT_HANDLED;
             ESP_LOGD(TAG,"Wifi was connected. Waiting for IP address");
-            network_set_timer(WIFI_DHCP_FAIL);
+            network_set_timer(nm->dhcp_timeout);
             break;
         case EN_GOT_IP:
             network_status_update_ip_info(UPDATE_CONNECTION_OK); 
@@ -791,7 +821,7 @@ static state_machine_result_t WIFI_CONNECTING_STATE_entry_handler(state_machine_
     network_handler_entry_print(State_Machine,true);
     network_start_stop_dhcp(nm->wifi_netif, true);
     network_connect_active_ssid(State_Machine);
-    nm->STA_duration = STA_POLLING_MIN;
+    nm->STA_duration = nm->sta_polling_min_ms;
     /* create timer for background STA connection */
     network_set_timer(nm->STA_duration);    
     NETWORK_EXECUTE_CB(State_Machine);
@@ -1038,9 +1068,9 @@ static state_machine_result_t WIFI_LOST_CONNECTION_STATE_entry_handler(state_mac
             /* put us in softAP mode first */
             esp_wifi_get_mode(&mode);
             if (WIFI_MODE_APSTA != mode) {
-                nm->STA_duration = STA_POLLING_MIN;
+                 nm->STA_duration = nm->sta_polling_min_ms;
                 network_async_configure();
-            } else if (nm->STA_duration < STA_POLLING_MAX) {
+            } else if (nm->STA_duration < nm->sta_polling_max_ms) {
                 nm->STA_duration *= 1.25;
             }
 

+ 17 - 14
main/esp_app_main.c

@@ -47,13 +47,6 @@
 #include "cmd_system.h"
 #include "tools.h"
 
-#ifndef CONFIG_DAC_KNOWN_CONFIGURATIONS
-#define CONFIG_DAC_KNOWN_CONFIGURATIONS ""
-#endif
-#ifndef CONFIG_DAC_KNOWN_CONFIGURATIONS_GPIOS
-#define CONFIG_DAC_KNOWN_CONFIGURATIONS_GPIOS ""
-#endif
-
 static const char certs_namespace[] = "certificates";
 static const char certs_key[] = "blob";
 static const char certs_version[] = "version";
@@ -69,6 +62,7 @@ static const char TAG[] = "esp_app_main";
 #define DEFAULT_HOST_NAME "squeezelite"
 char * fwurl = NULL;
 RTC_NOINIT_ATTR uint32_t RebootCounter ;
+RTC_NOINIT_ATTR uint32_t RecoveryRebootCounter ;
 
 static bool bNetworkConnected=false;
 extern const uint8_t server_cert_pem_start[] asm("_binary_github_pem_start");
@@ -358,9 +352,11 @@ void register_default_nvs(){
 	register_default_string_val( "telnet_block", "500");
 	register_default_string_val( "stats", "n");
 	register_default_string_val( "rel_api", CONFIG_RELEASE_API);
+	register_default_string_val("pollmx","600");
+    register_default_string_val("pollmin","15");
+    register_default_string_val("ethtmout","8");
+    register_default_string_val("dhcp_tmout","8");
 	register_default_string_val("wifi_smode", "A");
-	register_default_string_val("kndac", CONFIG_DAC_KNOWN_CONFIGURATIONS);
-	register_default_string_val("kngpio", CONFIG_DAC_KNOWN_CONFIGURATIONS_GPIOS);
 	wait_for_commit();
 	ESP_LOGD(TAG,"Done setting default values in nvs.");
 }
@@ -369,8 +365,11 @@ uint32_t halSTORAGE_RebootCounterRead(void) { return RebootCounter ; }
 uint32_t halSTORAGE_RebootCounterUpdate(int32_t xValue) { 
 	if(RebootCounter >100) {
 		RebootCounter = 0;
+		RecoveryRebootCounter = 0;
 	}
-	return (RebootCounter = (xValue != 0) ? (RebootCounter + xValue) : 0) ; 
+	RebootCounter = (xValue != 0) ? (RebootCounter + xValue) : 0;
+	RecoveryRebootCounter  = (xValue != 0) && is_recovery_running ? (RecoveryRebootCounter + xValue) : 0;
+	return (RebootCounter) ; 
 	}
 
 void handle_ap_connect(nm_state_t new_state, int sub_state){
@@ -393,22 +392,26 @@ void app_main()
 		uint32_t Counter = halSTORAGE_RebootCounterUpdate(1) ;		// increment counter
 		ESP_LOGI(TAG,"Reboot counter=%u\n", Counter) ;
 		if (Counter == 5) {
-			// before we change the partition, update the info for current running partition.
-			halSTORAGE_RebootCounterUpdate(0);
 			guided_factory();
 		}
 	}
 	else {
 		uint32_t Counter = halSTORAGE_RebootCounterUpdate(1) ;		// increment counter
+		if(RecoveryRebootCounter==1 && Counter>=5){
+			// First time we are rebooting in recovery after crashing
+			messaging_post_message(MESSAGING_ERROR,MESSAGING_CLASS_SYSTEM,"System was forced into recovery mode after crash likely caused by some bad configuration\n");
+		}
 		ESP_LOGI(TAG,"Recovery Reboot counter=%u\n", Counter) ;
-		if (Counter == 5) {
+			if (RecoveryRebootCounter == 5) {
 			ESP_LOGW(TAG,"System rebooted too many times. This could be an indication that configuration is corrupted. Erasing config.");
-			halSTORAGE_RebootCounterUpdate(0);
 			erase_settings_partition();
 			// reboot one more time
 			guided_factory();
 			
 		}		
+		if (RecoveryRebootCounter >5){
+			messaging_post_message(MESSAGING_ERROR,MESSAGING_CLASS_SYSTEM,"System was forced into recovery mode after crash likely caused by some bad configuration. Configuration was reset to factory.\n");
+		}	
 	}
 
 	char * fwurl = NULL;