diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 8bb48e1b6..7285ebad6 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -1316,6 +1316,7 @@ int http_fn_cfg_wifi(http_request_t* request) { add_label_text_field(request, "SSID", "ssid", CFG_GetWiFiSSID(), "
"); add_label_password_field(request, "", "pass", CFG_GetWiFiPass(), "
Password enable clear text password (clears existing)"); poststr_h2(request, "Alternate WiFi (used when first one is not responding)"); + poststr(request, "Note: It is possible to retain used SSID using command setStartupSSIDChannel in early.bat"); #ifndef PLATFORM_BEKEN poststr_h2(request, "SSID2 only on Beken Platform (BK7231T, BK7231N)"); #endif diff --git a/src/new_pins.c b/src/new_pins.c index e9b246ed6..4c0411a90 100644 --- a/src/new_pins.c +++ b/src/new_pins.c @@ -38,6 +38,30 @@ int BTN_HOLD_REPEAT_MS; byte *g_defaultWakeEdge = 0; int g_initialPinStates = 0; +#if ALLOW_SSID2 +//20241125 XJIKKA SSID retain - last used SSID will be preserved +// To enable this feature, the channel that will be used to store the last SSID +// must be set using the setStartupSSIDChannel command in early.bat. +// It has to be in early.bat.Autoexec.bat is processed after the wifi data is loaded. +int g_StartupSSIDRetainChannel = -1; // -1 disabled, 0..MAX_RETAIN_CHANNELS-1 channel to store last SSID + +int FV_GetStartupSSID_StoredValue(int adefault) { + if ((g_StartupSSIDRetainChannel < 0) || (g_StartupSSIDRetainChannel >= MAX_RETAIN_CHANNELS)) return adefault; + int fval = HAL_FlashVars_GetChannelValue(g_StartupSSIDRetainChannel); + return (fval & 1); ////only SSID1 (0) and SSID2 (1) allowed +} +void FV_UpdateStartupSSIDIfChanged_StoredValue(int assidindex) { + if ((g_StartupSSIDRetainChannel < 0) || (g_StartupSSIDRetainChannel >= MAX_RETAIN_CHANNELS)) return; + if ((assidindex < 0) && (assidindex > 1)) return; //only SSID1 (0) and SSID2 (1) allowed + int fval = HAL_FlashVars_GetChannelValue(g_StartupSSIDRetainChannel); + if (fval == assidindex) { + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "WiFi unchanged (SSID%i), HAL_FlashVars_SaveChannel skipped", assidindex+1); + return; //same value, no update + } + HAL_FlashVars_SaveChannel(g_StartupSSIDRetainChannel,assidindex); +} +#endif + void PIN_DeepSleep_MakeSureEdgesAreAlloced() { int i; if (g_defaultWakeEdge == 0) { @@ -2172,7 +2196,56 @@ static commandResult_t CMD_SetChannelType(const void* context, const char* cmd, addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "Channel %i type changed to %s", channel, type); return CMD_RES_OK; } +#if ALLOW_SSID2 +// setStartupSSIDChannel [-1 or RetainChannelIndex] +static commandResult_t CMD_setStartupSSIDChannel(const void* context, const char* cmd, const char* args, int cmdFlags) { + Tokenizer_TokenizeString(args, 0); + + if (Tokenizer_GetArgsCount() >= 1) { + int fval = Tokenizer_GetArgInteger(0); + if ((fval < -1) || (fval >= MAX_RETAIN_CHANNELS - 1)) { + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "StartupSSIDChannel value error: %i Allowed values (-1, 0..%i)", fval, MAX_RETAIN_CHANNELS - 1); + return CMD_RES_BAD_ARGUMENT; + } + g_StartupSSIDRetainChannel = fval; + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "StartupSSIDChannel changed to %i", g_StartupSSIDRetainChannel); + } + else { + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "StartupSSIDChannel is %i", g_StartupSSIDRetainChannel); + } + return CMD_RES_OK; +} +// setStartupSSID [0/1] +// Sets startup SSID - 0=SSID1 1=SSID2 - which SSID will be used after reboot. +// for this to work, setStartupSSIDChannel and SSID2 must be set +static commandResult_t CMD_setStartupSSID(const void* context, const char* cmd, const char* args, int cmdFlags) { + + Tokenizer_TokenizeString(args, 0); + + int fold = FV_GetStartupSSID_StoredValue(0); + if (Tokenizer_GetArgsCount() >= 1) { + int fval = Tokenizer_GetArgInteger(0); + if ((fval < 0) || (fval >1)) { + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "StartupSSID value error: %i Allowed values (0, 1)", fval); + return CMD_RES_BAD_ARGUMENT; + } + if (g_StartupSSIDRetainChannel<0) { + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "Cannot set StartupSSID, StartupSSIDChannel is not set."); + return CMD_RES_BAD_ARGUMENT; + } + if (!(fval==fold)) { + FV_UpdateStartupSSIDIfChanged_StoredValue(fval);//update flash only when changed + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "StartupSSID changed to %i", fval); + } else { + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "StartupSSID unchanged %i", fval); + } + } else { + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "StartupSSID is %i", fold); + } + return CMD_RES_OK; +} +#endif /// @brief Computes the Relay and PWM count. /// @param relayCount Number of relay and LED channels. /// @param pwmCount Number of PWM channels. @@ -2334,5 +2407,16 @@ void PIN_AddCommands(void) //cmddetail:"fn":"CMD_setButtonHoldRepeat","file":"new_pins.c","requires":"", //cmddetail:"examples":""} CMD_RegisterCommand("setButtonHoldRepeat", CMD_setButtonHoldRepeat, NULL); - +#if ALLOW_SSID2 + //cmddetail:{"name":"setStartupSSIDChannel","args":"[Value]", + //cmddetail:"descr":"Sets retain channel number to store last used SSID, 0..MAX_RETAIN_CHANNELS-1, -1 to disable. Suggested channel number is 7 (MAXMAX_RETAIN_CHANNELS-5)", + //cmddetail:"fn":"CMD_setStartupSSIDChannel","file":"new_pins.c","requires":"", + //cmddetail:"examples":""} + CMD_RegisterCommand("setStartupSSIDChannel", CMD_setStartupSSIDChannel, NULL); + //cmddetail:{"name":"setStartupSSID","args":"[Value]", + //cmddetail:"descr":"Sets startup SSID, 0 (SSID0) 1 (SSID1)", + //cmddetail:"fn":"CMD_setStartupSSID","file":"new_pins.c","requires":"", + //cmddetail:"examples":""} + CMD_RegisterCommand("setStartupSSID", CMD_setStartupSSID, NULL); +#endif } diff --git a/src/new_pins.h b/src/new_pins.h index 574c85e57..1bd8cbcb2 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -1434,4 +1434,9 @@ int PIN_IOR_NofChan(int test); extern const char* g_channelTypeNames[]; +#if ALLOW_SSID2 +int FV_GetStartupSSID_StoredValue(int adefault); +void FV_UpdateStartupSSIDIfChanged_StoredValue(int assidindex); +#endif + #endif diff --git a/src/user_main.c b/src/user_main.c index 5ff2987ad..0c58ef100 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -289,7 +289,9 @@ void LN882H_ApplyPowerSave(int bOn); // SSID switcher by xjikka 20240525 #if ALLOW_SSID2 -static int g_SSIDactual = 0; // 0=SSID1 1=SSID2 +#define SSID_USE_SSID1 0 +#define SSID_USE_SSID2 1 +static int g_SSIDactual = SSID_USE_SSID1; // -1 not initialized, 0=SSID1 1=SSID2 static int g_SSIDSwitchAfterTry = 3;// switch to opposite SSID after static int g_SSIDSwitchCnt = 0; // switch counter #endif @@ -308,6 +310,13 @@ void CheckForSSID12_Switch() { #endif } +//20241125 XJIKKA Init last stored SSID from RetailChannel if set +//Note that it must be set in early.bat using CMD_setStartupSSIDChannel +void Init_WiFiSSIDactual_FromChannelIfSet(void) { +#if ALLOW_SSID2 + g_SSIDactual = FV_GetStartupSSID_StoredValue(SSID_USE_SSID1); +#endif +} const char* CFG_GetWiFiSSIDX() { #if ALLOW_SSID2 if (g_SSIDactual) { @@ -370,6 +379,9 @@ void Main_OnWiFiStatusChange(int code) ADDLOGF_INFO("Main_OnWiFiStatusChange - WIFI_STA_AUTH_FAILED - %i\r\n", code); break; case WIFI_STA_CONNECTED: +#if ALLOW_SSID2 + if (!g_bHasWiFiConnected) FV_UpdateStartupSSIDIfChanged_StoredValue(g_SSIDactual); //update ony on first connect +#endif g_bHasWiFiConnected = 1; #if ALLOW_SSID2 g_SSIDSwitchCnt = 0; @@ -1343,9 +1355,11 @@ void Main_Init_After_Delay() if (bSafeMode) { ADDLOGF_INFO("###### safe mode activated - boot failures %d", g_bootFailures); } - - wifi_ssid = CFG_GetWiFiSSID(); - wifi_pass = CFG_GetWiFiPass(); +#if ALLOW_SSID2 + Init_WiFiSSIDactual_FromChannelIfSet();//Channel must be set in early.bat using CMD_setStartupSSIDChannel +#endif + wifi_ssid = CFG_GetWiFiSSIDX(); + wifi_pass = CFG_GetWiFiPassX(); #if 0 // you can use this if you bricked your module by setting wrong access point data