main/task_wifi.c

changeset 77
15dc572a7fcb
parent 70
e82bb707c671
child 79
332e85569339
--- a/main/task_wifi.c	Thu Sep 28 11:29:23 2023 +0200
+++ b/main/task_wifi.c	Tue Oct 03 17:24:06 2023 +0200
@@ -6,6 +6,7 @@
 
 #include "config.h"
 
+#define	MAX_AP_NUM		10
 
 static const char *TAG = "task_wifi";
 
@@ -18,32 +19,32 @@
 WIFI_State			*wifi_state = NULL;		///< Public state for other tasks.
 esp_netif_t			*sta_netif = NULL;		///< Station interface
 
-extern wifiStation_t		wifiStation;
-extern strConfig_t		config;
 
+wifi_scan_config_t scan_config = {				///< WiFi scanner configuration.
+    .ssid = (uint8_t *)CONFIG_ESP_WIFI_SSID,
+    .bssid = 0,
+    .channel = 0,
+    .show_hidden = false
+};
 
-wifi_scan_config_t scan_config = {
-	.ssid = 0,
-	.bssid = 0,
-	.channel = 0,
-	.show_hidden = false,
-	.scan_time.passive = 130				///< Beacons are usually sent every 100 mSec.
-};								///< WiFi scanner configuration.
+bool				_wifi_ScanDone = false;		///< Scan ready
+bool				_wifi_BetterAP = false;		///< If better AP available.
+int8_t				_wifi_RSSI = -127;		///< Latest RSSI level.
+uint16_t			_wifi_Scanned = 0;		///< Total scanned APs.
 
-uint8_t				_wifi_ssid[33];			///< Current SSID
-bool				_wifi_ScanAPs = false;		///< Scanning
-bool				_wifi_ScanDone = false;		///< Scan ready
-uint16_t			_wifi_Scanned = 0;		///< Total scanned APs.
+extern char			hostname[];			///< Generated hostname
+
 
 
 const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT0;		///< When set, means a client requested to disconnect from currently connected AP.
 const int TASK_WIFI_REQUEST_STA_CONNECT = BIT1;			///< When set, means a client requested to connect to an access point.
-const int TASK_WIFI_REQUEST_STA_STATUS = BIT2;			///< When set, means a client requested to update the connection status.
+const int TASK_WIFI_REQUEST_STA_SCAN = BIT2;			///< When set, means a client requested a AP scan.
+const int TASK_WIFI_REQUEST_STA_STATUS = BIT3;			///< When set, means a client requested to update the connection status.
 
-const int TASK_WIFI_HAS_IP = BIT3;				///< Indicate that we have an IP address
-const int TASK_WIFI_STA_FAILED = BIT4;				///< Indicate that we could not get a connection to AP as station.
-const int TASK_WIFI_STA_DISCONNECTED = BIT5;			///< Indicate that we are disconnected from an ap station.
-const int TASK_WIFI_STA_CONNECTED = BIT6;			///< Indicate that we are connected to AP as station, flip of BIT5.
+const int TASK_WIFI_HAS_IP = BIT4;				///< Indicate that we have an IP address
+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 BIT5.
 
 
 
@@ -78,24 +79,53 @@
 }
 
 
+void scan_WiFi(void)
+{
+    xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_SCAN);
+}
+
+
+void disconnect_WiFi(void)
+{
+    xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT);
+    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
+}
+
+
 
 static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
 {
     switch (event_id) {
 
 	case WIFI_EVENT_SCAN_DONE:
-            // Get the results so the memory used is freed.
-            ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_num, accessp_records));
-            _wifi_Scanned = ap_num;
-            _wifi_ScanDone = true;
-	    break;
+	    {
+            	/* Get the results so the memory used is freed. */
+            	ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_num, accessp_records));
+		ESP_LOGI(TAG, "Event wifi Scane done, %d records", ap_num);
+		for (int i = 0; i < ap_num; i++) {
+		    wifi_ap_record_t ap = accessp_records[i];
+		    ESP_LOGI(TAG, "AP:%d bssid:%02x:%02x:%02x:%02x:%02x:%02x ssid:%s ch:%d rssi:%d",
+					i, ap.bssid[0], ap.bssid[1], ap.bssid[2], ap.bssid[3], ap.bssid[4], ap.bssid[5],
+					ap.ssid, ap.primary, ap.rssi);
+		    if (ap.rssi > CONFIG_ESP_WIFI_ROAMING_LEVEL && ap.rssi > (_wifi_RSSI + 3)) {
+			_wifi_BetterAP = true;
+			ESP_LOGI(TAG, "AP:%d is a better AP", i);
+		    }
+		}
+            	_wifi_Scanned = ap_num;
+            	_wifi_ScanDone = true;
+	    	break;
+	    }
 
 	case WIFI_EVENT_STA_START:
+	    {
 		ESP_LOGI(TAG, "Event wifi START");
         	// Set the configured hostname for the dhcp client.
-        	ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, config.hostname));
+        	ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, hostname));
         	esp_wifi_connect();
+		_wifi_BetterAP = false;
 		break;
+	    }
 
 	case WIFI_EVENT_STA_CONNECTED:
 	    {
@@ -107,11 +137,15 @@
                 if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) {
                     wifi_state->STA_connected = true;
                     wifi_state->STA_rssi = ap_info.rssi;
+		    wifi_state->STA_channel = ap_info.primary;
                     sprintf(wifi_state->STA_ssid, "%s", ap_info.ssid);
+		    snprintf(wifi_state->STA_bssid, 18, "%02x:%02x:%02x:%02x:%02x:%02x",
+			     ap_info.bssid[0], ap_info.bssid[1], ap_info.bssid[2], ap_info.bssid[3], ap_info.bssid[4], ap_info.bssid[5]);
                     xSemaphoreGive(xSemaphoreWiFi);
                 } else {
 		    ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_CONNECTED");
 		}
+		_wifi_BetterAP = false;
                 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
                 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
 	    	break;
@@ -131,7 +165,7 @@
                 } else {
 		    ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_DISCONNECTED");
 		}
-		connect_mqtt(false);
+		_wifi_BetterAP = false;
                 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
                 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
 		break;
@@ -176,7 +210,6 @@
 		} else {
 			ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_LOST_IP");
 		}
-		connect_mqtt(false);
 		break;
 
 	case IP_EVENT_AP_STAIPASSIGNED:
@@ -200,6 +233,10 @@
     /* initialize the tcp stack */
     ESP_ERROR_CHECK(esp_netif_init());
     ESP_ERROR_CHECK(esp_event_loop_create_default());
+
+    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
+    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
+
     sta_netif = esp_netif_create_default_wifi_sta();
     assert(sta_netif);
 
@@ -207,9 +244,6 @@
      * memory allocation of objects used by the task 
      */
     accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * MAX_AP_NUM);
-    task_wifi_ConfigSTA = (wifi_config_t*)malloc(sizeof(wifi_config_t));
-    memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t));
-
     xSemaphoreWiFi = xSemaphoreCreateMutex();
     wifi_state = malloc(sizeof(WIFI_State));
     memset(wifi_state, 0x00, sizeof(WIFI_State));
@@ -217,18 +251,32 @@
     /*
      * 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_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));
+    wifi_config_t wifi_config = {
+	.sta = {
+	    .ssid = CONFIG_ESP_WIFI_SSID,
+	    .password = CONFIG_ESP_WIFI_PASSWORD,
+#if CONFIG_WIFI_ALL_CHANNEL_SCAN
+	    .scan_method = WIFI_ALL_CHANNEL_SCAN,
+#elif CONFIG_WIFI_FAST_SCAN
+	    .scan_method = WIFI_FAST_SCAN,
+#endif
+	    .failure_retry_cnt = 3,
+	    .sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
+	    .threshold.rssi = CONFIG_ESP_FAST_SCAN_MINIMUM_SIGNAL,
+	    .threshold.authmode = WIFI_AUTH_WPA2_PSK,
+	},
+    };
     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
+    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
     ESP_ERROR_CHECK(esp_wifi_start());
 
+//    ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
+
     xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
     xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
     xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
@@ -238,88 +286,72 @@
 
 	/* actions that can trigger: request a connection, a scan, or a disconnection */
 	uxBits = xEventGroupWaitBits(xEventGroupWifi, 
-		                     TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT | TASK_WIFI_REQUEST_STA_STATUS,
-		                     pdFALSE, pdFALSE, portMAX_DELAY );
+		       TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT | TASK_WIFI_REQUEST_STA_SCAN | TASK_WIFI_REQUEST_STA_STATUS,
+		       pdFALSE, pdFALSE, portMAX_DELAY );
 
 	if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) {
 	    /*
 	     * user requested a disconnect, this will in effect disconnect the wifi
 	     */
-	    connect_mqtt(false);
-	    ESP_LOGD(TAG, "Request STA disconnect");
+	    ESP_LOGI(TAG, "Request STA disconnect");
 	    ESP_ERROR_CHECK(esp_wifi_disconnect());
 	    xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY );
 
 	    /* finally: release the scan request bit */
 	    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT);
+	    ESP_LOGI(TAG, "Request STA disconnect is done");
 
 	} else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) {
 
-	    ESP_LOGD(TAG, "Request STA connect");
+	    ESP_LOGI(TAG, "Request STA connect");
 	    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
-            _wifi_ScanAPs = true;
-            _wifi_ScanDone = false;
-            ap_num = MAX_AP_NUM;
-            ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false));
-	    while (_wifi_ScanDone == false) {
-		vTaskDelay(10 / portTICK_PERIOD_MS);
-            }
-	    ESP_LOGI(TAG, "Scan done %d APs", _wifi_Scanned);
-	    bool found = false;
+	    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
 
-            // Available Access Points.
-            for (int i = 0; i < _wifi_Scanned; i++) {
-		wifi_ap_record_t ap = accessp_records[i];
-		// Check if we know this AP in the database.
-		ESP_LOGD(TAG, "%d %-20s ch: %2d rssi: %d %s", i, ap.ssid, ap.primary, ap.rssi, apsec[ap.authmode]);
-		if ((read_station(ap.ssid) >= 0)) {
-		    /* ssid */
-		    size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid);
-		    memcpy(task_wifi_ConfigSTA->sta.ssid, wifiStation.SSID, sz);
-		    /* password */
-		    sz = sizeof(task_wifi_ConfigSTA->sta.password);
-		    memcpy(task_wifi_ConfigSTA->sta.password, wifiStation.Password, sz);
-		    found = true;
-		    break;
-		}
+	    ESP_LOGI(TAG, "Connecting to `%s'", CONFIG_ESP_WIFI_SSID);
+	    esp_err_t wifierror = esp_wifi_connect();
+	    if (wifierror != ESP_OK) {
+		ESP_LOGE(TAG, "esp_wifi_connect() `%s' rc=%04x", CONFIG_ESP_WIFI_SSID, (int)wifierror);
+		xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
 	    }
-	    if (found) {
-		/*
-		 * Now connect to the known SSID
-		 */
-		ESP_LOGD(TAG, "Connecting to `%s'", task_wifi_ConfigSTA->sta.ssid);
-		ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, task_wifi_ConfigSTA));
-		esp_err_t wifierror = esp_wifi_connect();
-		if (wifierror != ESP_OK) {
-		    ESP_LOGE(TAG, "esp_wifi_connect() `%s' rc=%04x", task_wifi_ConfigSTA->sta.ssid, (int)wifierror);
-		    xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
-		}
-		uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 5000 / portTICK_PERIOD_MS);
-		if (uxBits & TASK_WIFI_STA_CONNECTED)
-		    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); // Only clear when connected.
-	    } else {
-		ESP_LOGI(TAG, "No known AP found, scan again");
-		vTaskDelay(3000 / portTICK_PERIOD_MS);
-	    }
+	    uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 5000 / portTICK_PERIOD_MS);
+//	    if (uxBits & TASK_WIFI_STA_CONNECTED)
+//		xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); // Only clear when connected.
+
 	} else if (uxBits & TASK_WIFI_REQUEST_STA_STATUS) {
 	    /*
 	     * Request WiFi update status, refresh the rssi.
 	     */
+	    ESP_LOGD(TAG, "Request STA status");
 	    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_STATUS);
 	    wifi_ap_record_t ap_info;
             esp_wifi_sta_get_ap_info(&ap_info);
-            ESP_LOGD(TAG, "Event STA status, ssid:%s, bssid:" MACSTR ", rssi: %d", ap_info.ssid, MAC2STR(ap_info.bssid), ap_info.rssi);
+            ESP_LOGI(TAG, "Event STA status, ssid:%s, bssid:" MACSTR ", rssi: %d", ap_info.ssid, MAC2STR(ap_info.bssid), ap_info.rssi);
+	    _wifi_RSSI = ap_info.rssi;
+	    _wifi_BetterAP = false;
 	    if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) {
                 wifi_state->STA_rssi = ap_info.rssi;
+		wifi_state->STA_channel = ap_info.primary;
+		snprintf(wifi_state->STA_bssid, 18, "%02x:%02x:%02x:%02x:%02x:%02x",
+			 ap_info.bssid[0], ap_info.bssid[1], ap_info.bssid[2], ap_info.bssid[3], ap_info.bssid[4], ap_info.bssid[5]);
                 xSemaphoreGive(xSemaphoreWiFi);
             } else {
                 ESP_LOGE(TAG, "lock error TASK_WIFI_REQUEST_STA_STATUS");
             }
 	    user_refresh();
+
+	} else if (uxBits & TASK_WIFI_REQUEST_STA_SCAN) {
+
+	    ESP_LOGI(TAG, "Request STA scan");
+	    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_SCAN);
+	    /* safe guard against overflow */
+	    ap_num = MAX_AP_NUM;
+	    _wifi_ScanDone = false;
+	    _wifi_BetterAP = false;
+	    ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false));
 	}
 
     } /* for(;;) */
-    vTaskDelay(10 / portTICK_PERIOD_MS);
+//    vTaskDelay(10 / portTICK_PERIOD_MS);
 }
 
 

mercurial