Prepare for enterprise WiFi.
This commit is contained in:
parent
1b0fd5da10
commit
64da1576ad
3 changed files with 184 additions and 78 deletions
|
@ -7,3 +7,5 @@
|
||||||
|
|
||||||
void wifi_init();
|
void wifi_init();
|
||||||
bool wifi_connect(const char* aSsid, const char* aPassword, wifi_auth_mode_t aAuthmode, uint8_t aRetryMax);
|
bool wifi_connect(const char* aSsid, const char* aPassword, wifi_auth_mode_t aAuthmode, uint8_t aRetryMax);
|
||||||
|
bool wifi_connect_ent(const char* aSsid, const char *aIdent, const char *aAnonIdent, const char* aPassword, uint8_t aRetryMax);
|
||||||
|
void wifi_scan();
|
||||||
|
|
86
main/main.c
86
main/main.c
|
@ -306,28 +306,77 @@ void display_fatal_error(pax_buf_t* pax_buffer, ILI9341* ili9341, const char* li
|
||||||
ili9341_write(ili9341, pax_buffer->buf);
|
ili9341_write(ili9341, pax_buffer->buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// void wifi_connect_to_stored() {
|
||||||
|
// wifi_connect_ent("MCH2022-legacy", "thefunny", "q", "nope, you are not", 3);
|
||||||
|
// }
|
||||||
|
|
||||||
void wifi_connect_to_stored() {
|
void wifi_connect_to_stored() {
|
||||||
|
// Open NVS.
|
||||||
nvs_handle_t handle;
|
nvs_handle_t handle;
|
||||||
nvs_open("system", NVS_READWRITE, &handle);
|
nvs_open("system", NVS_READWRITE, &handle);
|
||||||
char ssid[33];
|
uint8_t use_ent;
|
||||||
char password[33];
|
char *ssid = NULL;
|
||||||
size_t requiredSize;
|
char *ident = NULL;
|
||||||
esp_err_t res = nvs_get_str(handle, "wifi.ssid", NULL, &requiredSize);
|
char *anon_ident = NULL;
|
||||||
if (res != ESP_OK) {
|
char *password = NULL;
|
||||||
strcpy(ssid, "");
|
size_t len;
|
||||||
} else if (requiredSize < sizeof(ssid)) {
|
|
||||||
res = nvs_get_str(handle, "wifi.ssid", ssid, &requiredSize);
|
// Read NVS.
|
||||||
if (res != ESP_OK) strcpy(ssid, "");
|
esp_err_t res;
|
||||||
res = nvs_get_str(handle, "wifi.password", NULL, &requiredSize);
|
// Read SSID.
|
||||||
if (res != ESP_OK) {
|
res = nvs_get_str(handle, "wifi.ssid", NULL, &len);
|
||||||
strcpy(password, "");
|
if (res) goto errcheck;
|
||||||
} else if (requiredSize < sizeof(password)) {
|
ssid = malloc(len);
|
||||||
res = nvs_get_str(handle, "wifi.password", password, &requiredSize);
|
res = nvs_get_str(handle, "wifi.ssid", ssid, &len);
|
||||||
if (res != ESP_OK) strcpy(password, "");
|
if (res) goto errcheck;
|
||||||
}
|
// Check whether connection is enterprise.
|
||||||
|
res = nvs_get_u8(handle, "wifi.use_ent", &use_ent);
|
||||||
|
if (res) goto errcheck;
|
||||||
|
if (use_ent) {
|
||||||
|
// Read enterprise-specific parameters.
|
||||||
|
// Read identity.
|
||||||
|
res = nvs_get_str(handle, "wifi.ident", NULL, &len);
|
||||||
|
if (res) goto errcheck;
|
||||||
|
ident = malloc(len);
|
||||||
|
res = nvs_get_str(handle, "wifi.ident", ident, &len);
|
||||||
|
// Read anonymous identity.
|
||||||
|
res = nvs_get_str(handle, "wifi.anon_ident", NULL, &len);
|
||||||
|
if (res) goto errcheck;
|
||||||
|
anon_ident = malloc(len);
|
||||||
|
res = nvs_get_str(handle, "wifi.anon_ident", anon_ident, &len);
|
||||||
|
if (res) goto errcheck;
|
||||||
}
|
}
|
||||||
|
// Read password.
|
||||||
|
res = nvs_get_str(handle, "wifi.password", NULL, &len);
|
||||||
|
if (res) goto errcheck;
|
||||||
|
password = malloc(len);
|
||||||
|
res = nvs_get_str(handle, "wifi.password", password, &len);
|
||||||
|
if (res) goto errcheck;
|
||||||
|
|
||||||
|
// Close NVS.
|
||||||
nvs_close(handle);
|
nvs_close(handle);
|
||||||
wifi_connect(ssid, password, WIFI_AUTH_WPA2_PSK, 3);
|
|
||||||
|
// Open the appropriate connection.
|
||||||
|
if (use_ent) {
|
||||||
|
wifi_connect_ent(ssid, ident, anon_ident, password, 3);
|
||||||
|
} else {
|
||||||
|
wifi_connect(ssid, password, WIFI_AUTH_WPA2_PSK, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
errcheck:
|
||||||
|
if (res == ESP_ERR_NVS_NOT_FOUND || res == ESP_ERR_NVS_NOT_INITIALIZED) {
|
||||||
|
// When NVS is not initialised.
|
||||||
|
ESP_LOGI(TAG, "WiFi settings not stored in NVS.");
|
||||||
|
} else if (res) {
|
||||||
|
// Other errors.
|
||||||
|
ESP_LOGE(TAG, "Error connecting to WiFi: %s", esp_err_to_name(res));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free memory.
|
||||||
|
if (ssid) free(ssid);
|
||||||
|
if (ident) free(ident);
|
||||||
|
if (anon_ident) free(anon_ident);
|
||||||
|
if (password) free(password);
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_files_in_folder(const char* path) {
|
void list_files_in_folder(const char* path) {
|
||||||
|
@ -769,7 +818,7 @@ void app_main(void) {
|
||||||
esp_restart();
|
esp_restart();
|
||||||
} else if (menu_action == ACTION_INSTALLER) {
|
} else if (menu_action == ACTION_INSTALLER) {
|
||||||
graphics_task(pax_buffer, ili9341, NULL, "Not implemented");
|
graphics_task(pax_buffer, ili9341, NULL, "Not implemented");
|
||||||
} else if (menu_action == ACTION_WIFI_CONNECT) {
|
} else if (menu_action == ACTION_WIFI_CONNECT) {
|
||||||
graphics_task(pax_buffer, ili9341, NULL, "Connecting...");
|
graphics_task(pax_buffer, ili9341, NULL, "Connecting...");
|
||||||
wifi_connect_to_stored();
|
wifi_connect_to_stored();
|
||||||
} else if (menu_action == ACTION_OTA) {
|
} else if (menu_action == ACTION_OTA) {
|
||||||
|
@ -818,6 +867,7 @@ void app_main(void) {
|
||||||
} else {
|
} else {
|
||||||
graphics_task(pax_buffer, ili9341, NULL, "Canceled");
|
graphics_task(pax_buffer, ili9341, NULL, "Canceled");
|
||||||
}
|
}
|
||||||
|
nvs_set_u8(handle, "wifi.use_ent", 0);
|
||||||
nvs_close(handle);
|
nvs_close(handle);
|
||||||
} else if (menu_action == ACTION_WIFI_LIST) {
|
} else if (menu_action == ACTION_WIFI_LIST) {
|
||||||
nvs_handle_t handle;
|
nvs_handle_t handle;
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include "freertos/event_groups.h"
|
#include "freertos/event_groups.h"
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
|
#include "esp_wpa2.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "lwip/err.h"
|
#include "lwip/err.h"
|
||||||
|
@ -68,9 +69,13 @@ bool wifi_connect(const char* aSsid, const char* aPassword, wifi_auth_mode_t aAu
|
||||||
strcpy((char*) wifi_config.sta.password, aPassword);
|
strcpy((char*) wifi_config.sta.password, aPassword);
|
||||||
wifi_config.sta.threshold.authmode = aAuthmode;
|
wifi_config.sta.threshold.authmode = aAuthmode;
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
|
// Set WiFi config.
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||||
ESP_ERROR_CHECK(esp_wifi_start() );
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||||
|
// Disable 11b as NOC asked.
|
||||||
|
esp_wifi_config_11b_rate(WIFI_IF_STA, true);
|
||||||
|
// Start WiFi.
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_start());
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Connecting to WiFi...");
|
ESP_LOGI(TAG, "Connecting to WiFi...");
|
||||||
|
|
||||||
|
@ -90,3 +95,52 @@ bool wifi_connect(const char* aSsid, const char* aPassword, wifi_auth_mode_t aAu
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool wifi_connect_ent(const char* aSsid, const char *aIdent, const char *aAnonIdent, const char* aPassword, uint8_t aRetryMax) {
|
||||||
|
gRetryCounter = 0;
|
||||||
|
gRetryMax = aRetryMax;
|
||||||
|
wifi_config_t wifi_config = {0};
|
||||||
|
if (strlen(aSsid) > 32) {
|
||||||
|
ESP_LOGE(TAG, "SSID is too long (%zu > 32)!", strlen(aSsid));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
strncpy((char*) wifi_config.sta.ssid, aSsid, 32);
|
||||||
|
|
||||||
|
// Set WiFi config.
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
||||||
|
|
||||||
|
// Set WPA2 ENT config.
|
||||||
|
esp_wifi_sta_wpa2_ent_set_identity((const uint8_t *) aAnonIdent, strlen(aAnonIdent));
|
||||||
|
esp_wifi_sta_wpa2_ent_set_username((const uint8_t *) aIdent, strlen(aIdent));
|
||||||
|
esp_wifi_sta_wpa2_ent_set_password((const uint8_t *) aPassword, strlen(aPassword));
|
||||||
|
esp_wifi_sta_wpa2_ent_set_ttls_phase2_method(ESP_EAP_TTLS_PHASE2_MSCHAPV2);
|
||||||
|
// Enable enterprise auth.
|
||||||
|
esp_wifi_sta_wpa2_ent_enable();
|
||||||
|
// Disable 11b as NOC asked.
|
||||||
|
esp_wifi_config_11b_rate(WIFI_IF_STA, true);
|
||||||
|
// Start the connection.
|
||||||
|
esp_wifi_start();
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Connecting to WiFi...");
|
||||||
|
|
||||||
|
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
|
||||||
|
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
|
||||||
|
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||||
|
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually happened. */
|
||||||
|
if (bits & WIFI_CONNECTED_BIT) {
|
||||||
|
ESP_LOGI(TAG, "Connected to WiFi");
|
||||||
|
return true;
|
||||||
|
} else if (bits & WIFI_FAIL_BIT) {
|
||||||
|
ESP_LOGE(TAG, "Failed to connect");
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_stop());
|
||||||
|
} else {
|
||||||
|
ESP_LOGE(TAG, "Unknown event received while waiting on connection");
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_stop());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wifi_scan() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue