Skip to content

Commit

Permalink
SSID2 autoswitch mod v2, LN882H update
Browse files Browse the repository at this point in the history
  • Loading branch information
xjikka committed May 27, 2024
1 parent c2c21f8 commit 7bb6c00
Showing 1 changed file with 22 additions and 33 deletions.
55 changes: 22 additions & 33 deletions src/user_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,47 +290,31 @@ void LN882H_ApplyPowerSave(int bOn);

// SSID switcher by xjikka 20240525
#if ALLOW_SSID2
static int g_SSIDactual = 0; // 0=firsttime 1=ssid1 2=ssid2
static int g_SSIDSwitchAfterTry = 3; // switch to opposite SSID after
static int g_SSIDSwitchCnt = 0; // switch counter
static int g_SSIDactual = 0; // 0=SSID1 1=SSID2
static int g_SSIDSwitchAfterTry = 3;// switch to opposite SSID after
static int g_SSIDSwitchCnt = 0; // switch counter
#endif

void CheckForSSID12_Switch() {
#if ALLOW_SSID2
if (g_SSIDactual == 0) {
g_SSIDactual = 1; // firsttime after start force use SSID1=SSID=CFG_GetWiFiSSID()
// nothing to do if SSID2 is unset
if (CFG_GetWiFiSSID2()[0] == 0) return;
if (g_SSIDSwitchCnt++ < g_SSIDSwitchAfterTry) {
ADDLOGF_INFO("WiFi SSID: waiting for SSID switch %d/%d (using SSID%d)\r\n", g_SSIDSwitchCnt, g_SSIDSwitchAfterTry, g_SSIDactual+1);
return;
};
if (CFG_GetWiFiSSID2() == "") return; // no ssid2, no reason to change
if (g_SSIDSwitchAfterTry > 1) {
g_SSIDSwitchCnt++;
if (g_SSIDSwitchCnt < g_SSIDSwitchAfterTry) {
// g_SSIDSwitchCnt<g_SSIDSwitchAfterTry next try
ADDLOGF_INFO("WiFi SSID: Waiting for SSID switch %d/%d (using SSID%d)\r\n", g_SSIDSwitchCnt +1, g_SSIDSwitchAfterTry, g_SSIDactual);
return;
}
// all tries done, no success, switch SSID
g_SSIDSwitchCnt = 0;
}
if (g_SSIDactual == 1) {
g_SSIDactual = 2;
ADDLOGF_INFO("WiFi SSID: switching to SSID2\r\n");
}
else {
g_SSIDactual = 1;
ADDLOGF_INFO("WiFi SSID: switching to SSID1\r\n");
};
g_SSIDSwitchCnt = 0;
g_SSIDactual ^= 1; // toggle SSID
ADDLOGF_INFO("WiFi SSID: switching to SSID%i\r\n", g_SSIDactual + 1);
#endif
}

const char* CFG_GetWiFiSSIDX() {
#if ALLOW_SSID2
if (g_SSIDactual == 2) {
ADDLOGF_DEBUG("WiFi Get SSID using SSID2\r\n");
if (g_SSIDactual) {
return CFG_GetWiFiSSID2();
}
else {
ADDLOGF_DEBUG("WiFi Get SSID using SSID1\r\n");
return CFG_GetWiFiSSID();
}
#else
Expand All @@ -340,7 +324,7 @@ const char* CFG_GetWiFiSSIDX() {

const char* CFG_GetWiFiPassX() {
#if ALLOW_SSID2
if (g_SSIDactual == 2) {
if (g_SSIDactual) {
return CFG_GetWiFiPass2();
}
else {
Expand Down Expand Up @@ -381,6 +365,7 @@ void Main_OnWiFiStatusChange(int code)
break;
case WIFI_STA_CONNECTED:
g_bHasWiFiConnected = 1;
g_SSIDSwitchCnt = 0;
ADDLOGF_INFO("Main_OnWiFiStatusChange - WIFI_STA_CONNECTED - %i\r\n", code);

if (bSafeMode == 0) {
Expand Down Expand Up @@ -507,12 +492,16 @@ void Main_ConnectToWiFiNow() {
CheckForSSID12_Switch();
wifi_ssid = CFG_GetWiFiSSIDX();
wifi_pass = CFG_GetWiFiPassX();
ADDLOGF_INFO("Connecting to SSID [%s]\r\n", wifi_ssid);
HAL_ConnectToWiFi(wifi_ssid, wifi_pass,&g_cfg.staticIP);
// register function to get callbacks about wifi changes.
// register function to get callbacks about wifi changes ..
// ... but do it, before calling HAL_ConnectToWiFi(),
// otherwise callbacks are not possible (e.g. WIFI_STA_CONNECTING can never be called )!!
HAL_WiFi_SetupStatusCallback(Main_OnWiFiStatusChange);
ADDLOGF_DEBUG("Registered for wifi changes\r\n");
g_connectToWiFi = 0;
ADDLOGF_INFO("Registered for wifi changes\r\n");
ADDLOGF_INFO("Connecting to SSID [%s]\r\n", wifi_ssid);
HAL_ConnectToWiFi(wifi_ssid, wifi_pass, &g_cfg.staticIP);
// don't set g_connectToWiFi = 0; here!
// this would overwrite any changes, e.g. from Main_OnWiFiStatusChange !
// so don't do this here, but e.g. set in Main_OnWiFiStatusChange if connected!!!
}
bool Main_HasFastConnect() {
if (g_bootFailures > 2)
Expand Down

0 comments on commit 7bb6c00

Please sign in to comment.