main/task_wifi.c

changeset 94
87aa80b8e452
parent 88
7f02dbee58d0
child 119
1cef3c25426b
--- a/main/task_wifi.c	Tue May 18 16:52:23 2021 +0200
+++ b/main/task_wifi.c	Wed May 19 14:35:48 2021 +0200
@@ -1,7 +1,7 @@
 /**
  * @file task_wifi.c
  * @brief WiFi task. Connects to a known Access Point. If we know more then
- *        one AP, try to connect all of them until it succeeds (Not yet written).
+ *        one AP, try to connect all of them until it succeeds.
  */
 
 
@@ -17,6 +17,8 @@
 wifi_ap_record_t		*accessp_records;		///< [MAX_AP_NUM] records array with scan results.
 wifi_config_t			*task_wifi_ConfigSTA = NULL;	///< Current STA configuration.
 WIFI_State			*wifi_state = NULL;		///< Public state for other tasks.
+esp_netif_t			*sta_netif = NULL;		///< Station interface
+
 
 wifi_scan_config_t scan_config = {				///< WiFi scanner configuration.
 	.ssid = 0,
@@ -43,7 +45,6 @@
 const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2;			///< When set, means a client requested to connect to an access point.
 
 const int TASK_WIFI_HAS_IP = BIT3;				///< Indicate that we have an IP address
-const int TASK_WIFI_AP_STARTED = BIT4;				///< Indicate that the SoftAP is started
 const int TASK_WIFI_STA_FAILED = BIT5;				///< Indicate that we could not get a connection to AP as station.
 const int TASK_WIFI_STA_DISCONNECTED = BIT6;			///< Indicate that we are disconnected from an ap station.
 const int TASK_WIFI_STA_CONNECTED = BIT7;			///< Indicate that we are connected to AP as station, flip of BIT6.
@@ -156,7 +157,7 @@
 
 	case WIFI_EVENT_STA_START:
 		// Set the configured hostname for the dhcp client.
-		ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, config.hostname));   
+		ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, config.hostname));
 		esp_wifi_connect();
 		break;
 
@@ -236,55 +237,6 @@
 		}
 		break;
 	}
-    	case WIFI_EVENT_AP_START:
-    		xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_AP_STARTED);
-		if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
-		    wifi_state->AP_active = true;
-		    wifi_state->AP_clients = 0;
-		    xSemaphoreGive(xSemaphoreWiFi);
-		} else {
-                    ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_START");
-                }
-		break;
-
-	case WIFI_EVENT_AP_STOP:
-		xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_AP_STARTED);
-		if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
-		    wifi_state->AP_active = false;
-		    wifi_state->AP_clients = 0;
-		    xSemaphoreGive(xSemaphoreWiFi);
-		} else {
-                    ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_STOP");
-                }
-		break;
-
-    	case WIFI_EVENT_AP_STACONNECTED: {
-		if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
-		    wifi_state->AP_clients++;
-		    xSemaphoreGive(xSemaphoreWiFi);
-		} else {
-                    ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_STACONNECTED");
-                }
-		wifi_event_ap_staconnected_t* staconnected = (wifi_event_ap_staconnected_t*) event_data;
-            	ESP_LOGI(TAG, "Event AP connected, mac:" MACSTR ", aid:%d, conns:%d",
-                       MAC2STR(staconnected->mac), staconnected->aid, wifi_state->AP_clients);
-		break;
-	}
-    	case WIFI_EVENT_AP_STADISCONNECTED: {
-		if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
-		    if (wifi_state->AP_clients > 0)
-		    	wifi_state->AP_clients--;
-		    else
-		    	wifi_state->AP_clients = 0;
-		    xSemaphoreGive(xSemaphoreWiFi);
-		} else {
-                    ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_STADISCONNECTED");
-                }
-		wifi_event_ap_stadisconnected_t* stadisconnected = (wifi_event_ap_stadisconnected_t*) event_data;
-            	ESP_LOGI(TAG, "Event AP disconnected, mac:" MACSTR ", aid:%d, conns:%d",
-                       MAC2STR(stadisconnected->mac), stadisconnected->aid, wifi_state->AP_clients);
-		break;
-	}
 	default:
 		ESP_LOGW(TAG, "Unknown WiFi event %d", event_id);
         	break;
@@ -302,9 +254,9 @@
 		ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
                 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
                     wifi_state->STA_online = true;
-                    snprintf(wifi_state->STA_ip, 16, "%s", ip4addr_ntoa(&event->ip_info.ip));
-                    snprintf(wifi_state->STA_nm, 16, "%s", ip4addr_ntoa(&event->ip_info.netmask));
-                    snprintf(wifi_state->STA_gw, 16, "%s", ip4addr_ntoa(&event->ip_info.gw));
+                    snprintf(wifi_state->STA_ip, 16, IPSTR, IP2STR(&event->ip_info.ip));
+                    snprintf(wifi_state->STA_nm, 16, IPSTR, IP2STR(&event->ip_info.netmask));
+                    snprintf(wifi_state->STA_gw, 16, IPSTR, IP2STR(&event->ip_info.gw));
                     xSemaphoreGive(xSemaphoreWiFi);
                 } else {
                     ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_GOT_IP");
@@ -347,7 +299,7 @@
                 break;
 
 	case IP_EVENT_AP_STAIPASSIGNED:
-		ESP_LOGD(TAG, "IP_EVENT_AP_STAIPASSIGNED");
+		ESP_LOGI(TAG, "IP_EVENT_AP_STAIPASSIGNED");
 		break;
 
         default:
@@ -385,7 +337,7 @@
      * Initialize NVS
      */
     ret = nvs_flash_init();
-    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
 	ESP_ERROR_CHECK(nvs_flash_erase());
 	ret = nvs_flash_init();
     }
@@ -394,8 +346,10 @@
     /* event handler and event group for the wifi driver */
     xEventGroupWifi = xEventGroupCreate();
     /* initialize the tcp stack */
-    tcpip_adapter_init();
+    ESP_ERROR_CHECK(esp_netif_init());
     ESP_ERROR_CHECK(esp_event_loop_create_default());
+    sta_netif = esp_netif_create_default_wifi_sta();
+    assert(sta_netif);
 
     /*
      * memory allocation of objects used by the task 
@@ -406,70 +360,26 @@
 
     xSemaphoreWiFi = xSemaphoreCreateMutex();
     wifi_state = malloc(sizeof(WIFI_State));
-    wifi_state->AP_clients = 0;
-    wifi_state->AP_active = false;
     wifi_state->STA_connected = false;
     wifi_state->STA_online = false;
     wifi_state->STA_rssi = 0;
 
     /*
-     * start the softAP access point
-     * stop DHCP server
-     */
-    ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
-
-    /*
-     * Assign a static IP to the AP network interface
-     */
-    tcpip_adapter_ip_info_t info;
-    memset(&info, 0x00, sizeof(info));
-    IP4_ADDR(&info.ip, 192, 168, 1, 1);
-    IP4_ADDR(&info.gw, 192, 168, 1, 1);
-    IP4_ADDR(&info.netmask, 255, 255, 255, 0);
-    ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info));
-
-    /* start dhcp server */
-    ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
-    ESP_LOGI(TAG, "AP start dhcps ip: 192.168.1.1 nm: 255.255.255.0 gw: 192.168.1.1");
-
-    /* start dhcp client */
-    ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA));
-
-    /*
-     * init wifi as station + access point
+     * init wifi as station
      */
     wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
     ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
 
-    ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL) );
-    ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID /*IP_EVENT_STA_GOT_IP*/, &got_ip_event_handler, NULL) );
+    esp_event_handler_instance_t instance_any_id;
+    esp_event_handler_instance_t instance_got_ip;
+    ESP_ERROR_CHECK( esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) );
+    ESP_ERROR_CHECK( esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) );
 
     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
-    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
-    ESP_ERROR_CHECK(esp_wifi_set_bandwidth(WIFI_IF_AP, config.ap_bandwidth));
+    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
 
-    /* configure the softAP and start it */
-    wifi_config_t ap_config = {
-	.ap = {
-	    .ssid_len = 0,
-	    .channel = config.ap_channel,
-	    .authmode = WIFI_AUTH_WPA2_PSK,
-	    .ssid_hidden = config.ap_ssid_hidden,
-	    .max_connection = AP_MAX_CONNECTIONS,
-	    .beacon_interval = 100,
-	},
-    };
-    memcpy(ap_config.ap.ssid, config.ap_ssid , sizeof(config.ap_ssid));
-    memcpy(ap_config.ap.password, config.ap_pwd, sizeof(config.ap_pwd));
-    ret = esp_wifi_set_config(WIFI_IF_AP, &ap_config);
-    if (ret != ESP_OK) {
-	ESP_LOGE(TAG, "esp_wifi_set_config(WIFI_IF_AP, nnn) rc=%d", ret);
-    }
     ESP_ERROR_CHECK(esp_wifi_start());
 
-    ESP_LOGI(TAG, "AP start ssid:`%s' pwd:`%s' channel:%d, hidden:%s", 
-		    ap_config.ap.ssid, ap_config.ap.password, ap_config.ap.channel, ap_config.ap.ssid_hidden ? "yes":"no");
-
     /*
      * try to get access to previously saved wifi
      */
@@ -481,7 +391,6 @@
      * Wait for access point to start
      */
     xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
-    xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_AP_STARTED, pdFALSE, pdTRUE, portMAX_DELAY );
     EventBits_t uxBits;
 
     for(;;) {
@@ -653,14 +562,14 @@
 			wifi_config_t *wconfig = task_wifi_ConfigSTA /*task_wifi_GetWifiStaConfig( ) */;
 			if (wconfig) {
 
-			    tcpip_adapter_ip_info_t ip_info;
-			    ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
+			    esp_netif_ip_info_t ip_info;
+			    ESP_ERROR_CHECK(esp_netif_get_ip_info(sta_netif, &ip_info));
 			    char ip[IP4ADDR_STRLEN_MAX];
 			    char gw[IP4ADDR_STRLEN_MAX];
 			    char netmask[IP4ADDR_STRLEN_MAX];
-			    strcpy(ip, ip4addr_ntoa(&ip_info.ip));
-			    strcpy(netmask, ip4addr_ntoa(&ip_info.netmask));
-			    strcpy(gw, ip4addr_ntoa(&ip_info.gw));
+			    sprintf(ip, IPSTR, IP2STR(&ip_info.ip));
+			    sprintf(netmask, IPSTR, IP2STR(&ip_info.netmask));
+			    sprintf(gw, IPSTR, IP2STR(&ip_info.gw));
 			    TFT_setFont(DEFAULT_FONT, NULL);
 			    _fg = TFT_WHITE;
 			    TFT_print((char *)"SSID", 155 - TFT_getStringWidth((char *)"SSID"), 40);

mercurial