Skip to content

Commit

Permalink
Support WPA2-Enterprise (maybe) and correct EMF default wifi creds
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsci committed May 21, 2022
1 parent 76b6639 commit 1e88574
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 27 deletions.
6 changes: 6 additions & 0 deletions drivers/tidal_helpers/micropython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ target_sources(usermod_tidal_helpers INTERFACE
${CMAKE_CURRENT_LIST_DIR}/tidal_helpers.c
)


# I'm sure there's probably a better way to do this, but...
target_include_directories(usermod_tidal_helpers INTERFACE
"$ENV{IDF_PATH}/components/wpa_supplicant/esp_supplicant/include"
)

# Link our INTERFACE library to the usermod target.
target_link_libraries(usermod INTERFACE usermod_tidal_helpers)
56 changes: 56 additions & 0 deletions drivers/tidal_helpers/tidal_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "esp32s2/rom/usb/usb_dc.h"
#include "esp32s2/rom/usb/chip_usb_dw_wrapper.h"
#include "esp32s2/rom/usb/usb_persist.h"
#include "esp_wpa2.h"

static const char *TAG = "tidal_helpers";

Expand Down Expand Up @@ -253,6 +254,57 @@ STATIC mp_obj_t tidal_esp_wifi_set_max_tx_power(mp_obj_t pwr_obj) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(tidal_esp_wifi_set_max_tx_power_obj, tidal_esp_wifi_set_max_tx_power);

STATIC mp_obj_t tidal_esp_wifi_sta_wpa2_ent_enable(mp_obj_t flag_obj) {
esp_err_t err;
if (mp_obj_is_true(flag_obj)) {
err = esp_wifi_sta_wpa2_ent_enable();
} else {
err = esp_wifi_sta_wpa2_ent_disable();
}
check_esp_err(err);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(tidal_esp_wifi_sta_wpa2_ent_enable_obj, tidal_esp_wifi_sta_wpa2_ent_enable);

STATIC mp_obj_t tidal_esp_wifi_sta_wpa2_ent_set_identity(mp_obj_t id_obj) {
if (mp_obj_is_true(id_obj)) {
size_t len = 0;
const char* id = mp_obj_str_get_data(id_obj, &len);
esp_err_t err = esp_wifi_sta_wpa2_ent_set_identity((const unsigned char*)id, len);
check_esp_err(err);
} else {
esp_wifi_sta_wpa2_ent_clear_identity();
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(tidal_esp_wifi_sta_wpa2_ent_set_identity_obj, tidal_esp_wifi_sta_wpa2_ent_set_identity);

STATIC mp_obj_t tidal_esp_wifi_sta_wpa2_ent_set_username(mp_obj_t username_obj) {
if (mp_obj_is_true(username_obj)) {
size_t len = 0;
const char* username = mp_obj_str_get_data(username_obj, &len);
esp_err_t err = esp_wifi_sta_wpa2_ent_set_username((const unsigned char*)username, len);
check_esp_err(err);
} else {
esp_wifi_sta_wpa2_ent_clear_username();
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(tidal_esp_wifi_sta_wpa2_ent_set_username_obj, tidal_esp_wifi_sta_wpa2_ent_set_username);

STATIC mp_obj_t tidal_esp_wifi_sta_wpa2_ent_set_password(mp_obj_t pass_obj) {
if (mp_obj_is_true(pass_obj)) {
size_t len = 0;
const char* password = mp_obj_str_get_data(pass_obj, &len);
esp_err_t err = esp_wifi_sta_wpa2_ent_set_password((const unsigned char*)password, len);
check_esp_err(err);
} else {
esp_wifi_sta_wpa2_ent_clear_password();
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(tidal_esp_wifi_sta_wpa2_ent_set_password_obj, tidal_esp_wifi_sta_wpa2_ent_set_password);

STATIC const mp_rom_map_elem_t tidal_helpers_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ota) },
{ MP_ROM_QSTR(MP_QSTR_get_variant), MP_ROM_PTR(&tidal_helper_get_variant_obj) },
Expand All @@ -270,6 +322,10 @@ STATIC const mp_rom_map_elem_t tidal_helpers_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_get_irq_handler), MP_ROM_PTR(&tidal_get_irq_handler_obj) },
{ MP_ROM_QSTR(MP_QSTR_pin_number), MP_ROM_PTR(&tidal_pin_number_obj) },
{ MP_ROM_QSTR(MP_QSTR_esp_wifi_set_max_tx_power), MP_ROM_PTR(&tidal_esp_wifi_set_max_tx_power_obj) },
{ MP_ROM_QSTR(MP_QSTR_esp_wifi_sta_wpa2_ent_enable), MP_ROM_PTR(&tidal_esp_wifi_sta_wpa2_ent_enable_obj) },
{ MP_ROM_QSTR(MP_QSTR_esp_wifi_sta_wpa2_ent_set_identity), MP_ROM_PTR(&tidal_esp_wifi_sta_wpa2_ent_set_identity_obj) },
{ MP_ROM_QSTR(MP_QSTR_esp_wifi_sta_wpa2_ent_set_username), MP_ROM_PTR(&tidal_esp_wifi_sta_wpa2_ent_set_username_obj) },
{ MP_ROM_QSTR(MP_QSTR_esp_wifi_sta_wpa2_ent_set_password), MP_ROM_PTR(&tidal_esp_wifi_sta_wpa2_ent_set_password_obj) },

{ MP_ROM_QSTR(MP_QSTR_ESP_PD_DOMAIN_RTC_PERIPH), MP_ROM_INT(ESP_PD_DOMAIN_RTC_PERIPH) },
{ MP_ROM_QSTR(MP_QSTR_ESP_PD_OPTION_OFF), MP_ROM_INT(ESP_PD_OPTION_OFF) },
Expand Down
49 changes: 34 additions & 15 deletions modules/wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,28 @@

DEFAULT_CONNECT_TIMEOUT = 20
DEFAULT_TX_POWER = 20
DEFAULT_SSID = "emfcamp-legacy22"
DEFAULT_USERNAME = "badge"
DEFAULT_PASSWORD = "badge"

WIFI_AUTH_OPEN = 0
WIFI_AUTH_WEP = 1
WIFI_AUTH_WPA_PSK = 2
WIFI_AUTH_WPA2_PSK = 3
WIFI_AUTH_WPA_WPA2_PSK = 4
WIFI_AUTH_WPA2_ENTERPRISE = 5
WIFI_AUTH_WPA3_PSK = 6
WIFI_AUTH_WPA2_WPA3_PSK = 7
WIFI_AUTH_WAPI_PSK = 8

def get_default_ssid():
return settings.get("wifi_ssid", "badge")
return settings.get("wifi_ssid", DEFAULT_SSID)

def get_default_username():
return settings.get("wifi_wpa2ent_username", DEFAULT_USERNAME)

def get_default_password():
return settings.get("wifi_password", "badge")
return settings.get("wifi_password", DEFAULT_PASSWORD)

def get_sta_status():
return _STA_IF.status()
Expand Down Expand Up @@ -41,8 +57,9 @@ def active():
def get_connection_timeout():
return settings.get("wifi_connection_timeout", DEFAULT_CONNECT_TIMEOUT)

def save_defaults(ssid, password):
def save_defaults(ssid, password, usename):
settings.set("wifi_ssid", ssid)
settings.set("wifi_wpa2ent_username", username)
settings.set("wifi_password", password)
settings.save()

Expand All @@ -52,28 +69,30 @@ def save_defaults(ssid, password):
# STATION MODE
# ------------

def connect(*args):
def connect(ssid=None, password=None, username=None):
'''
Connect to a WiFi network
:param ssid: optional, ssid of network to connect to
:param password: optional, password of network to connect to
:param username: optional, WPA2-Enterprise username of network to connect to
'''
_STA_IF.active(True)
# 20 = 5 dBm according to https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/network/esp_wifi.html?highlight=esp_wifi_set_max_tx_power#_CPPv425esp_wifi_set_max_tx_power6int8_t
# Anything above 8 dBm causes too much interference in the crystal circuit
# which basically breaks all ability to transmit
tidal_helpers.esp_wifi_set_max_tx_power(settings.get("wifi_tx_power", DEFAULT_TX_POWER))
if len(args) == 0:
if password := get_default_password():
_STA_IF.connect(get_default_ssid(), password)
else:
_STA_IF.connect(get_default_ssid())
elif len(args) == 1:
_STA_IF.connect(args[0])
elif len(args) == 2:
_STA_IF.connect(args[0], args[1])
else:
raise Exception('Expected either 0 (default network), 1 (ssid) or 2 (ssid, password) parameters.')
if not ssid:
ssid = get_default_ssid()
username = get_default_username()
password = get_default_password()
if username:
tidal_helpers.esp_wifi_sta_wpa2_ent_set_identity(username)
tidal_helpers.esp_wifi_sta_wpa2_ent_set_username(username)
tidal_helpers.esp_wifi_sta_wpa2_ent_set_password(password)
password = None # Don't pass to WLAN.connect()

tidal_helpers.esp_wifi_sta_wpa2_ent_enable(username is not None)
_STA_IF.connect(ssid, password)

def disconnect():
'''
Expand Down
37 changes: 25 additions & 12 deletions modules/wifi_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import network
import settings
import wifi
from wifi import WIFI_AUTH_OPEN, WIFI_AUTH_WPA2_PSK, WIFI_AUTH_WPA2_ENTERPRISE
from app import MenuApp

class WifiClient(MenuApp):
Expand Down Expand Up @@ -39,9 +40,9 @@ def scan(self):
for ap in wifi.scan():
print(f"Found {ap}")
if ap[0]:
pass_required = ap[4] != 0
authmode = ap[4]
try:
self.wifi_networks.append((ap[0].decode("utf-8"), pass_required))
self.wifi_networks.append((ap[0].decode("utf-8"), authmode))
except:
# Ignore any APs that don't decode as valid UTF-8
pass
Expand All @@ -51,7 +52,13 @@ def on_start(self):
super().on_start()
self.wifi_networks = []
if ssid := wifi.get_default_ssid():
self.wifi_networks.append((ssid, wifi.get_default_password() is not None))
if wifi.get_default_username():
authmode = WIFI_AUTH_WPA2_ENTERPRISE
elif wifi.get_default_password():
authmode = WIFI_AUTH_WPA2_PSK # doesn't matter exactly so long as it's not enterprise or open
else:
authmode = WIFI_AUTH_OPEN
self.wifi_networks.append((ssid, authmode))
self.connecting = None
self.connection_timer = None

Expand All @@ -70,22 +77,28 @@ def on_deactivate(self):
wifi.stop()

def join_index(self, i):
(ssid, password_required) = self.wifi_networks[i]
(ssid, authmode) = self.wifi_networks[i]
username = None
password = None
if password_required:
if ssid == "badge":
# Special case for EMF
password = "badge"
if authmode:
if ssid == wifi.DEFAULT_SSID:
# Extra special case, in case a different network has been connected to and is now the default
username = wifi.DEFAULT_USERNAME
password = wifi.DEFAULT_PASSWORD
elif ssid == wifi.get_default_ssid():
username = wifi.get_default_username()
password = wifi.get_default_password()
else:
if authmode == WIFI_AUTH_WPA2_ENTERPRISE:
username = self.keyboard_prompt("Enter username:")
password = self.keyboard_prompt("Enter password:")
return self.join_wifi(ssid, password)
return self.join_wifi(ssid, password, username)

def join_wifi(self, ssid, password):
def join_wifi(self, ssid, password, username=None):
self.window.set_choices(None)
self.username = username
self.password = password
wifi.connect(ssid, password)
wifi.connect(ssid, password, username)
self.connecting = wifi.get_connection_timeout()
self.connection_timer = self.periodic(1000, self.update_connection)
self.update_ui()
Expand Down Expand Up @@ -116,7 +129,7 @@ def update_connection(self):
if status == network.STAT_CONNECTING:
return
elif status == network.STAT_GOT_IP:
wifi.save_defaults(wifi.get_ssid(), self.password)
wifi.save_defaults(wifi.get_ssid(), self.password, self.username)
# The update_ui call will take care of everything else

self.cancel_timer()
Expand Down

0 comments on commit 1e88574

Please sign in to comment.