Просмотр исходного кода

allow bypassing the wifi manager

to disable wifi manager, use command:  nvs_set bypass_wm str -v "1"
to enable wifi manager, use command:  nvs_set bypass_wm str -v "0"
Sebastien 5 лет назад
Родитель
Сommit
2ed8b50fbe
6 измененных файлов с 238 добавлено и 42 удалено
  1. 7 5
      components/cmd_system/cmd_system.c
  2. 28 28
      components/wifi-manager/wifi_manager.c
  3. 188 0
      main/cmd_wifi.c
  4. 3 5
      main/cmd_wifi.h
  5. 1 0
      main/console.c
  6. 11 4
      main/esp_app_main.c

+ 7 - 5
components/cmd_system/cmd_system.c

@@ -136,11 +136,13 @@ esp_err_t guided_boot(esp_partition_subtype_t partition_subtype)
 
 static int restart(int argc, char **argv)
 {
-    ESP_LOGI(TAG, "Restarting");
-    guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
-    // If we're still alive, then there may not be an ota partition to boot from
-    guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
-	return 0; // return fail.  This should never return... we're rebooting!
+    ESP_LOGW(TAG, "Restarting");
+    esp_restart();
+
+//    guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
+//    // If we're still alive, then there may not be an ota partition to boot from
+//    guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
+//	return 0; // return fail.  This should never return... we're rebooting!
 }
 esp_err_t guided_restart_ota(){
     guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);

+ 28 - 28
components/wifi-manager/wifi_manager.c

@@ -320,33 +320,33 @@ bool wifi_manager_fetch_wifi_sta_config(){
 		FREE_AND_NULL(buff);
 
 		/* settings */
-		sz = sizeof(wifi_settings);
-		buff = (uint8_t*)malloc(sizeof(uint8_t) * sz);
-		esp_err = nvs_get_blob(handle, "settings", buff, &sz);
-		if(esp_err != ESP_OK){
-			// SSID was found, we should have some settings as well.  Log this as an error
-			ESP_LOGW(TAG,"No wifi settings found in nvs. Freeing nvs buffer");
-			FREE_AND_NULL(buff);
-			ESP_LOGD(TAG,"Closing nvs Handle");
-			nvs_close(handle);
-			ESP_LOGD(TAG,"load sta config done");
-			return wifi_manager_config_sta->sta.ssid[0] != '\0';
-		}
-		if(sz!=sizeof(wifi_settings)){
-			ESP_LOGW(TAG,"Unable to retrieve settings buffer from nvs.  Size did not match");
-		}
-		else {
-			ESP_LOGD(TAG,"Copying configuration restored from nvs");
-			memcpy(&wifi_settings, buff, sz);
-			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_only (0 = APSTA, 1 = STA when connected):%i",wifi_settings.sta_only);
-			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_power_save (1 = yes):%i",wifi_settings.sta_power_save);
-			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_static_ip (0 = dhcp client, 1 = static ip):%i",wifi_settings.sta_static_ip);
-			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_static_ip_config: IP: %s , GW: %s , Mask: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.ip), ip4addr_ntoa(&wifi_settings.sta_static_ip_config.gw), ip4addr_ntoa(&wifi_settings.sta_static_ip_config.netmask));
-			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_ip_addr: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.ip));
-			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_gw_addr: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.gw));
-			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_netmask: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.netmask));
-		}
-		FREE_AND_NULL(buff);
+//		sz = sizeof(wifi_settings);
+//		buff = (uint8_t*)malloc(sizeof(uint8_t) * sz);
+//		esp_err = nvs_get_blob(handle, "settings", buff, &sz);
+//		if(esp_err != ESP_OK){
+//			// SSID was found, we should have some settings as well.  Log this as an error
+//			ESP_LOGW(TAG,"No wifi settings found in nvs. Freeing nvs buffer");
+//			FREE_AND_NULL(buff);
+//			ESP_LOGD(TAG,"Closing nvs Handle");
+//			nvs_close(handle);
+//			ESP_LOGD(TAG,"load sta config done");
+//			return wifi_manager_config_sta->sta.ssid[0] != '\0';
+//		}
+//		if(sz!=sizeof(wifi_settings)){
+//			ESP_LOGW(TAG,"Unable to retrieve settings buffer from nvs.  Size did not match");
+//		}
+//		else {
+//			ESP_LOGD(TAG,"Copying configuration restored from nvs");
+//			memcpy(&wifi_settings, buff, sz);
+//			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_only (0 = APSTA, 1 = STA when connected):%i",wifi_settings.sta_only);
+//			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_power_save (1 = yes):%i",wifi_settings.sta_power_save);
+//			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_static_ip (0 = dhcp client, 1 = static ip):%i",wifi_settings.sta_static_ip);
+//			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_static_ip_config: IP: %s , GW: %s , Mask: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.ip), ip4addr_ntoa(&wifi_settings.sta_static_ip_config.gw), ip4addr_ntoa(&wifi_settings.sta_static_ip_config.netmask));
+//			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_ip_addr: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.ip));
+//			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_gw_addr: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.gw));
+//			ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_netmask: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.netmask));
+//		}
+//		FREE_AND_NULL(buff);
 		nvs_close(handle);
 
 		return wifi_manager_config_sta->sta.ssid[0] != '\0';
@@ -479,7 +479,7 @@ void wifi_manager_generate_access_points_json(cJSON ** ap_list){
 		cJSON_AddItemToArray(*ap_list, ap);
 		ESP_LOGD(TAG,"New access point found: %s", cJSON_Print(ap));
 	}
-	ESP_LOGD(TAG,"Full access point list: %s", cJSON_Print(*ap_list));
+	ESP_LOGV(TAG,"Full access point list: %s", cJSON_Print(*ap_list));
 }
 
 bool wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait){

+ 188 - 0
main/cmd_wifi.c

@@ -8,3 +8,191 @@
 */
 
 // cmd_wifi has been replaced by wifi-manager
+/* Console example — WiFi commands
+
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+
+#include "cmd_wifi.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#include "cmd_decl.h"
+#include "esp_log.h"
+#include "esp_console.h"
+#include "argtable3/argtable3.h"
+#include "freertos/FreeRTOS.h"
+#include "freertos/event_groups.h"
+#include "esp_wifi.h"
+#include "tcpip_adapter.h"
+#include "esp_event.h"
+#include "led.h"
+
+#define JOIN_TIMEOUT_MS (10000)
+
+extern EventGroupHandle_t wifi_event_group;
+extern const int CONNECTED_BIT;
+static const char * TAG = "cmd_wifi";
+/** Arguments used by 'join' function */
+static struct {
+    struct arg_int *timeout;
+    struct arg_str *ssid;
+    struct arg_str *password;
+    struct arg_end *end;
+} join_args;
+
+///** Arguments used by 'join' function */
+//static struct {
+//    struct arg_int *autoconnect;
+//    struct arg_end *end;
+//} auto_connect_args;
+
+static void event_handler(void* arg, esp_event_base_t event_base,
+                                int32_t event_id, void* event_data)
+{
+    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
+		led_blink_pushed(LED_GREEN, 250, 250);
+        esp_wifi_connect();
+        xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
+    } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
+		led_unpush(LED_GREEN);
+        xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
+    }
+}
+//bool wait_for_wifi(){
+//
+//	bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
+//
+//	if(!connected){
+//		ESP_LOGD(TAG,"Waiting for WiFi...");
+//	    connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
+//	                                   pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
+//	    if(!connected){
+//	    	ESP_LOGD(TAG,"wifi timeout.");
+//	    }
+//	    else
+//	    {
+//	    	ESP_LOGI(TAG,"WiFi Connected!");
+//	    }
+//	}
+//
+//
+//    return connected;
+//
+//}
+static void initialise_wifi(void)
+{
+    static bool initialized = false;
+    if (initialized) {
+        return;
+    }
+    tcpip_adapter_init();
+    wifi_event_group = xEventGroupCreate();
+    ESP_ERROR_CHECK(esp_event_loop_create_default());
+    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
+    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
+    ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) );
+    ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
+    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
+    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
+    ESP_ERROR_CHECK( esp_wifi_start() );
+    initialized = true;
+	led_blink(LED_GREEN, 250, 250);
+}
+
+static bool wifi_join(const char *ssid, const char *pass, int timeout_ms)
+{
+    initialise_wifi();
+    wifi_config_t wifi_config = { 0 };
+    strncpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
+    if (pass) {
+        strncpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
+    }
+
+    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
+    ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
+    ESP_ERROR_CHECK( esp_wifi_connect() );
+
+    int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
+                                   pdFALSE, pdTRUE, timeout_ms / portTICK_PERIOD_MS);
+    return (bits & CONNECTED_BIT) != 0;
+}
+
+
+static int set_auto_connect(int argc, char **argv)
+{
+//    int nerrors = arg_parse(argc, argv, (void **) &join_args);
+//    if (nerrors != 0) {
+//        arg_print_errors(stderr, join_args.end, argv[0]);
+//        return 1;
+//    }
+//    ESP_LOGI(__func__, "Connecting to '%s'",
+//             join_args.ssid->sval[0]);
+//
+//    /* set default value*/
+//    if (join_args.timeout->count == 0) {
+//        join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
+//    }
+//
+//    bool connected = wifi_join(join_args.ssid->sval[0],
+//                               join_args.password->sval[0],
+//                               join_args.timeout->ival[0]);
+//    if (!connected) {
+//        ESP_LOGW(__func__, "Connection timed out");
+//        return 1;
+//    }
+//    ESP_LOGI(__func__, "Connected");
+    return 0;
+}
+static int connect(int argc, char **argv)
+{
+    int nerrors = arg_parse(argc, argv, (void **) &join_args);
+    if (nerrors != 0) {
+        arg_print_errors(stderr, join_args.end, argv[0]);
+        return 1;
+    }
+    ESP_LOGI(__func__, "Connecting to '%s'",
+             join_args.ssid->sval[0]);
+
+    /* set default value*/
+    if (join_args.timeout->count == 0) {
+        join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
+    }
+
+    bool connected = wifi_join(join_args.ssid->sval[0],
+                               join_args.password->sval[0],
+                               join_args.timeout->ival[0]);
+    if (!connected) {
+        ESP_LOGW(__func__, "Connection timed out");
+        return 1;
+    }
+    ESP_LOGI(__func__, "Connected");
+    return 0;
+}
+void register_wifi_join()
+{
+    join_args.timeout = arg_int0(NULL, "timeout", "<t>", "Connection timeout, ms");
+    join_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
+    join_args.password = arg_str0(NULL, NULL, "<pass>", "PSK of AP");
+    join_args.end = arg_end(2);
+
+    const esp_console_cmd_t join_cmd = {
+        .command = "join",
+        .help = "Join WiFi AP as a station",
+        .hint = NULL,
+        .func = &connect,
+        .argtable = &join_args
+    };
+    ESP_ERROR_CHECK( esp_console_cmd_register(&join_cmd) );
+}
+
+void register_wifi()
+{
+    register_wifi_join();
+    initialise_wifi();
+}

+ 3 - 5
main/cmd_wifi.h

@@ -1,7 +1,5 @@
-/* Console example — declarations of command registration functions.
-
+/* Console example — declarations of command registration functions.
    This example code is in the Public Domain (or CC0 licensed, at your option.)
-
    Unless required by applicable law or agreed to in writing, this
    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    CONDITIONS OF ANY KIND, either express or implied.
@@ -12,9 +10,9 @@
 extern "C" {
 #endif
 
-
+// Register WiFi functions
+void register_wifi();
 
 #ifdef __cplusplus
 }
 #endif
-

+ 1 - 0
main/console.c

@@ -150,6 +150,7 @@ void console_start() {
 	esp_console_register_help_command();
 	register_system();
 	register_nvs();
+	register_wifi();
 #if RECOVERY_APPLICATION!=1
 	register_squeezelite();
 #elif RECOVERY_APPLICATION==1

+ 11 - 4
main/esp_app_main.c

@@ -45,7 +45,7 @@
 #include "squeezelite-ota.h"
 #include <math.h>
 
-static EventGroupHandle_t wifi_event_group;
+EventGroupHandle_t wifi_event_group;
 bool enable_bt_sink=false;
 bool enable_airplay=false;
 bool jack_mutes_amp=false;
@@ -185,6 +185,7 @@ void register_default_nvs(){
 	nvs_value_set_default(NVS_TYPE_STR, "airplay_port", CONFIG_AIRPLAY_PORT, 0);
 	nvs_value_set_default(NVS_TYPE_STR, "a2dp_sink_name", CONFIG_A2DP_SINK_NAME, 0);
 	nvs_value_set_default(NVS_TYPE_STR, "a2dp_dev_name", CONFIG_A2DP_DEV_NAME, 0);
+	nvs_value_set_default(NVS_TYPE_STR, "bypass_wm", "0", 0);
 
 	char * flag = get_nvs_value_alloc_default(NVS_TYPE_STR, "enable_bt_sink", STR(CONFIG_BT_SINK), 0);
 	enable_bt_sink= (strcmp(flag,"1")==0 ||strcasecmp(flag,"y")==0);
@@ -214,9 +215,15 @@ void app_main()
 
 	/* start the wifi manager */
 	led_blink(LED_GREEN, 250, 250);
-	wifi_manager_start();
-	wifi_manager_set_callback(EVENT_STA_GOT_IP, &cb_connection_got_ip);
-	wifi_manager_set_callback(WIFI_EVENT_STA_DISCONNECTED, &cb_connection_sta_disconnected);
+	char * bypass_wm = get_nvs_value_alloc_default(NVS_TYPE_STR, "bypass_wm", "0", 0);
+	if((strcmp(bypass_wm,"1")==0 ||strcasecmp(bypass_wm,"y")==0)){
+		ESP_LOGW(TAG,"wifi manager is disabled. Please use wifi commands to connect to your wifi access point.");
+	}
+	else {
+		wifi_manager_start();
+		wifi_manager_set_callback(EVENT_STA_GOT_IP, &cb_connection_got_ip);
+		wifi_manager_set_callback(WIFI_EVENT_STA_DISCONNECTED, &cb_connection_sta_disconnected);
+	}
 	console_start();
 	if(fwurl && strlen(fwurl)>0){
 		while(!bWifiConnected){