Created
November 28, 2019 17:05
-
-
Save avtolstoy/8b6b5e183cf99c382207a566dfc30f1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "application.h" | |
#include "dct_hal.h" | |
#include "platform_dct.h" | |
#include "hex_to_bytes.h" | |
namespace { | |
WLanSecurityType toSecurityType(wiced_security_t sec) { | |
if (sec == WICED_SECURITY_OPEN) | |
return WLAN_SEC_UNSEC; | |
if (sec & WEP_ENABLED) | |
return WLAN_SEC_WEP; | |
if ((sec & (WPA2_SECURITY | ENTERPRISE_ENABLED)) == (WPA2_SECURITY | ENTERPRISE_ENABLED)) | |
return WLAN_SEC_WPA2_ENTERPRISE; | |
if ((sec & (WPA_SECURITY | ENTERPRISE_ENABLED)) == (WPA_SECURITY | ENTERPRISE_ENABLED)) | |
return WLAN_SEC_WPA_ENTERPRISE; | |
if (sec & WPA_SECURITY) | |
return WLAN_SEC_WPA; | |
if (sec & WPA2_SECURITY) | |
return WLAN_SEC_WPA2; | |
return WLAN_SEC_NOT_SET; | |
} | |
WLanSecurityCipher toCipherType(wiced_security_t sec) { | |
if (sec & AES_ENABLED) | |
return WLAN_CIPHER_AES; | |
if (sec & TKIP_ENABLED) | |
return WLAN_CIPHER_TKIP; | |
return WLAN_CIPHER_NOT_SET; | |
} | |
void fixupWiFiCredentialsList() { | |
const uintptr_t DCT1_ADDRESS = 0x08004000; | |
const uintptr_t DCT2_ADDRESS = 0x08008000; | |
uintptr_t curDct = (uintptr_t)dct_read_app_data_lock(0); | |
curDct -= sizeof(platform_dct_data_t); | |
if (curDct != DCT1_ADDRESS && curDct != DCT2_ADDRESS) { | |
return; | |
} | |
const platform_dct_data_t* dct = (const platform_dct_data_t*)curDct; | |
const auto& wifiConfig = dct->wifi_config; | |
wiced_config_ap_entry_t invalidEntry; | |
memset(&invalidEntry, 0xff, sizeof(invalidEntry)); | |
wiced_config_ap_entry_t entries[CONFIG_AP_LIST_SIZE]; | |
memcpy(entries, wifiConfig.stored_ap_list, sizeof(entries)); | |
dct_read_app_data_unlock(0); | |
bool fixup = false; | |
for (const auto& ap: entries) { | |
if (!memcmp(&ap, &invalidEntry, sizeof(invalidEntry))) { | |
// Found an invalid entry | |
fixup = true; | |
break; | |
} | |
} | |
if (!fixup) { | |
return; | |
} | |
WiFi.on(); | |
WiFi.clearCredentials(); | |
for (const auto& ap: entries) { | |
if (memcmp(&ap, &invalidEntry, sizeof(invalidEntry))) { | |
// Valid entry | |
WiFiCredentials cred; | |
auto security = toSecurityType(ap.details.security); | |
auto cipher = toCipherType(ap.details.security); | |
// Do a sanity check | |
if (security == WLAN_SEC_WPA2_ENTERPRISE || security == WLAN_SEC_WPA_ENTERPRISE) { | |
// XXX: Enterprise credentials are not handled | |
continue; | |
} | |
if (ap.details.SSID.length == 0 || ap.details.SSID.length > sizeof(ap.details.SSID.value)) { | |
continue; | |
} | |
if (security == WLAN_SEC_NOT_SET || cipher == WLAN_CIPHER_NOT_SET) { | |
continue; | |
} | |
if (security != WLAN_SEC_UNSEC && (ap.security_key_length == 0 || ap.security_key_length > sizeof(ap.security_key))) { | |
continue; | |
} | |
cred.setSsid((const char*)ap.details.SSID.value, (int)ap.details.SSID.length); | |
cred.setSecurity(security); | |
cred.setCipher(cipher); | |
if (security == WLAN_SEC_WEP) { | |
char tmp[sizeof(ap.security_key_length) / 2] = {}; | |
size_t len = hexToBytes(ap.security_key, tmp, ap.security_key_length); | |
cred.setPassword(tmp, len); | |
} else { | |
cred.setPassword(ap.security_key, ap.security_key_length); | |
} | |
WiFi.setCredentials(cred); | |
} | |
} | |
} | |
STARTUP(fixupWiFiCredentialsList()); | |
} // anonymous | |
void setup() { | |
} | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment