Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSID retain with GUI #1448

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/httpserver/http_fns.c
Original file line number Diff line number Diff line change
Expand Up @@ -1313,15 +1313,29 @@ int http_fn_cfg_wifi(http_request_t* request) {
<input type=\"submit\" value=\"Convert to Open Access WiFi\" onclick=\"return confirm('Are you sure you want to switch to open access WiFi?')\">\
</form>");
poststr_h2(request, "Use this to connect to your WiFi");
add_label_text_field(request, "SSID", "ssid", CFG_GetWiFiSSID(), "<form action=\"/cfg_wifi_set\">");
add_label_password_field(request, "", "pass", CFG_GetWiFiPass(), "<br>Password<span style=\"float:right;\"><input type=\"checkbox\" onclick=\"e=getElement('pass');if(this.checked){e.value='';e.type='text'}else e.type='password'\" > enable clear text password (clears existing)</span>");
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)");
add_label_text_field(request, "SSID"
#if ALLOW_SSID2
"1"
#endif
, "ssid", CFG_GetWiFiSSID(), "<form action=\"/cfg_wifi_set\">");
add_label_password_field(request, "", "pass", CFG_GetWiFiPass(), "<br>Password"
#if ALLOW_SSID2
"1"
#endif
"<span style=\"float:right;\"><input type=\"checkbox\" onclick=\"e=getElement('pass');if(this.checked){e.value='';e.type='text'}else e.type='password'\" > enable clear text password (clears existing)</span>");
#if PLATFORM_BEKEN
int fval = (HAL_FlashVars_GetChannelValue(SSIDRetainChannel)%1000)/100;
poststr_h4(request, "Alternate WiFi (used when first one is not responding)");
// poststr_h2(request, "SSID2 only on Beken Platform (BK7231T, BK7231N)");
add_label_text_field(request, "SSID2", "ssid2", CFG_GetWiFiSSID2(), "");
add_label_password_field(request, "", "pass2", CFG_GetWiFiPass2(), "<br>Password2<span style=\"float:right;\"><input type=\"checkbox\" onclick=\"e=getElement('pass2');if(this.checked){e.value='';e.type='text'}else e.type='password'\" > enable clear text password (clears existing)</span>");
poststr(request, "<p>Select SSID used first: <select class='hele' name='startSSID'><option value='0'>SSID1</option>");
hprintf255(request, "<option value='1' %s>SSID2</option>"
"<option value='2' %s>Last used SSID</option>"
"<option value='4' %s>Only SSID1 (no switch to SSID2)</option>"
"<option value='5' %s>Only SSID2 (no switch to SSID1)</option>"
"</select>",(fval==1) ? "selected":"",(fval==2 || fval==3) ? "selected":"",(fval==4) ? "selected":"",(fval==5) ? "selected":"");
#endif
#if ALLOW_WEB_PASSWORD
int web_password_enabled = strcmp(CFG_GetWebPassword(), "") == 0 ? 0 : 1;
poststr_h2(request, "Web Authentication");
Expand Down Expand Up @@ -1379,7 +1393,6 @@ int http_fn_cfg_name(http_request_t* request) {
int http_fn_cfg_wifi_set(http_request_t* request) {
char tmpA[128];
int bChanged;

addLogAdv(LOG_INFO, LOG_FEATURE_HTTP, "HTTP_ProcessPacket: generating cfg_wifi_set \r\n");
bChanged = 0;

Expand All @@ -1405,6 +1418,13 @@ int http_fn_cfg_wifi_set(http_request_t* request) {
if (http_getArg(request->url, "pass2", tmpA, sizeof(tmpA))) {
bChanged |= CFG_SetWiFiPass2(tmpA);
}
#if ALLOW_SSID2
if (http_getArg(request->url, "startSSID", tmpA, sizeof(tmpA))) {
int start_SSID = atoi(tmpA);
addLogAdv(LOG_DEBUG, LOG_FEATURE_GENERAL, "start_SSID with value %i", start_SSID);
HandleSSIDretainedFromGUI(start_SSID); // handle in user_main.c - needs access to actual SSID used (g_SSIDactual) - defined "static"
}
#endif
#if ALLOW_WEB_PASSWORD
if (http_getArg(request->url, "web_admin_password_enabled", tmpA, sizeof(tmpA))) {
int web_password_enabled = atoi(tmpA);
Expand Down
8 changes: 7 additions & 1 deletion src/new_pins.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ int g_initialPinStates = 0;
// 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) {
Expand All @@ -60,6 +61,7 @@ void FV_UpdateStartupSSIDIfChanged_StoredValue(int assidindex) {
}
HAL_FlashVars_SaveChannel(g_StartupSSIDRetainChannel,assidindex);
}
*/
#endif

void PIN_DeepSleep_MakeSureEdgesAreAlloced() {
Expand Down Expand Up @@ -2197,11 +2199,11 @@ static commandResult_t CMD_SetChannelType(const void* context, const char* cmd,
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)) {
Expand All @@ -2219,6 +2221,7 @@ static commandResult_t CMD_setStartupSSIDChannel(const void* context, const char
// 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);
Expand All @@ -2245,6 +2248,7 @@ static commandResult_t CMD_setStartupSSID(const void* context, const char* cmd,
}
return CMD_RES_OK;
}
*/
#endif
/// @brief Computes the Relay and PWM count.
/// @param relayCount Number of relay and LED channels.
Expand Down Expand Up @@ -2407,6 +2411,7 @@ 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)",
Expand All @@ -2419,4 +2424,5 @@ void PIN_AddCommands(void)
//cmddetail:"examples":""}
CMD_RegisterCommand("setStartupSSID", CMD_setStartupSSID, NULL);
#endif
*/
}
26 changes: 26 additions & 0 deletions src/new_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -1435,8 +1435,34 @@ 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);
*/
#define SSIDRetainChannel 7
// we will use this channel to store all information for the retain SSID feature:
// if retaining is set and
// which was the last used channel
// the fixed starting SSID
//
// possible values stored in retain channel (including possible future extension for counters/tries in values > 1000):
//
// 15 0 42
// __ __
// ^ ^ ^
// | | |
// | | |
// | | ---- "magic number" 42
// | |
// | ------- mode: 0/1 try both SSIDs, start with 0/1
// | 2/3 try both SSIDs, start with 0/1, save last connected (SSID=val%2 2=0 3=1)
// | 4/5 only use SSID set, (SSID=val%4 4=0 5=1)
// |
// --------- possible extension, e.g # of tries before switching to next SSID (if both are used)
//
// SSID to use on startup is always mode-value%2 !
void UpdateSSIDretainedIfChanged_StoredValue(int val); // needs g_SSIDactual, so defined in user_main.c
void HandleSSIDretainedFromGUI(int val); // needs g_SSIDactual, so defined in user_main.c
#endif

#endif
127 changes: 118 additions & 9 deletions src/user_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,92 @@ void LN882H_ApplyPowerSave(int bOn);

// SSID switcher by xjikka 20240525
#if ALLOW_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
//#define SSID_USE_SSID1 0
//#define SSID_USE_SSID2 1
// static int g_SSIDactual = SSID_USE_SSID1; // -1 not initialized, 0=SSID1 1=SSID2

// settings for retain SSID feature
// strictly speaking we only have to take into account one situation:
// "retaining" is set and SSID2 should be used first
// else it's SSID1
// and doesn't matter if we start with SSID1 because it's default
// of if SSID1 was the last one used ...

// retaining is set and SSID1 was last --> 242
// retaining is set and SSID2 was last --> 342
// everything else: retain is unset
// only if there is a value 342 or 142 or 542, we need to use SSID2 (g_SSIDactual = 1)
// in all other cases use SSID1 (g_SSIDactual = 0)
static int g_SSIDactual=0;

// set the retained SSID
//
void UpdateSSIDretainedIfChanged_StoredValue(int val) {
int fval = HAL_FlashVars_GetChannelValue(SSIDRetainChannel);
addLogAdv(LOG_DEBUG, LOG_FEATURE_GENERAL, "UpdateSSIDretainedIfChanged_StoredValue value %i", val);
int valSSID=(val/100)%2;
if (fval == val) {
addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "SSID unchanged (SSID%i), HAL_FlashVars_SaveChannel skipped", valSSID);
return; //same value, no update
}
addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "setting SSID to SSID%i", valSSID);
HAL_FlashVars_SaveChannel(SSIDRetainChannel, val);
}

void HandleSSIDretainedFromGUI(int val) {
// SSID retain results from GUI options
// 0 --> start with SSID1
// 1 --> start with SSID2
// 2 --> remember last SSID (will need to be combined wit actual SSID during setting)
// 4 --> only use SSID1
// 5 --> only use SSID2
int ret=val%10; // only 2 needs special treatment to store "actual" used SSID
if (val%10 == 2) ret += g_SSIDactual%2 ; // 2 means: start with "actual SSID"
if (ret >5) return; // unknown value
// in case we switched from "non-switching" to switching, change g_SSIDactual accordingly
if (ret < 4 && g_SSIDactual > 1 ) g_SSIDactual = g_SSIDactual%2 ;
// in case we switched from switching to "non-switching", change g_SSIDactual accordingly
if (ret >= 4 && g_SSIDactual < 4 ) g_SSIDactual = ret ;

// maybe we later want to use values >10 for other purposes
// so we will readd them here later
//
// possible values stored in retain channel:
//
// 15 0 42
// __ __
// ^ ^ ^
// | | |
// | | |
// | | ---- "magic number" 42
// | |
// | ------- mode: 0/1 try both SSIDs, start with 0/1
// | 2/3 try both SSIDs, start with 0/1, save last connected (SSID=val%2 2=0 3=1)
// | 4/5 only use SSID set, (SSID=val%4 4=0 5=1)
// |
// --------- possible extension, e.g # of tries before switching to next SSID (if both are used)
//
// SSID to use on startup is always mode-value%2 !

// handle higher values from above 1000 in channel = above 10 here (we will add magic value 42 in the last two decimal places)
int highval = 1000 * (val/10); // values e.g. for # of tries
if (! highval) highval = 1000*(HAL_FlashVars_GetChannelValue(SSIDRetainChannel)/1000); // no values? reuse actual values from flash

ret *= 100; // push to third place
ret += 42; // add "magic" number
ret += highval; // restore or set higher values from above 1000

UpdateSSIDretainedIfChanged_StoredValue(ret);
}

static int g_SSIDSwitchAfterTry = 3;// switch to opposite SSID after
static int g_SSIDSwitchCnt = 0; // switch counter
#endif

void CheckForSSID12_Switch() {
#if ALLOW_SSID2
// nothing to do if SSID2 is unset
if (CFG_GetWiFiSSID2()[0] == 0) return;
if ( (CFG_GetWiFiSSID2()[0] == 0) || ( g_SSIDactual > 1) ) return; // g_SSIDactual > 1 : fix SSIDs, no switching
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;
Expand All @@ -309,17 +384,18 @@ void CheckForSSID12_Switch() {
ADDLOGF_INFO("WiFi SSID: switching to SSID%i\r\n", g_SSIDactual + 1);
#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) {
if (g_SSIDactual%2) {
return CFG_GetWiFiSSID2();
}
else {
Expand All @@ -332,7 +408,7 @@ const char* CFG_GetWiFiSSIDX() {

const char* CFG_GetWiFiPassX() {
#if ALLOW_SSID2
if (g_SSIDactual) {
if (g_SSIDactual%2) {
return CFG_GetWiFiPass2();
}
else {
Expand Down Expand Up @@ -379,8 +455,33 @@ 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
int actval = HAL_FlashVars_GetChannelValue(SSIDRetainChannel);
//update retain value
// only if
// on first connect and
// retain is set and
// value is different

// remember: actval == X242 if SSID1 is used --> g_SSIDactual == 0
// actval == X342 if SSID2 is used --> g_SSIDactual == 1

// we only need to store SSID value, if this is NOT the case, because g_SSIDactual doesn't match the retain value
// actval == X242 but g_SSIDactual == 1 --> we need to store X342
// actval == X324 but g_SSIDactual == 0 --> we need to store X242
// --> in both cases: actval + 100*g_SSIDactual = X342
//
// countercheck: in all other cases (if retained vale eaquals actual value or fixed values are given):
// actval + 100*g_SSIDactual is <> X342:
// values below 242: can be max 242 ( 142 + 100*1 ) <> 342
// values above 342: can be min 442 ( 442 + 0 ) <> 342
//
// so testing for actval + 100*g_SSIDactual == X342 is o.k. to test
// retain is set and value is different in one test !!

if ( !g_bHasWiFiConnected && ( (actval%1000 + 100*(g_SSIDactual%2) ) == 342 ) )
HAL_FlashVars_SaveChannel(SSIDRetainChannel, 42 + 100*((g_SSIDactual%2)+2) + 1000*(actval/1000));
#endif
g_bHasWiFiConnected = 1;
#if ALLOW_SSID2
Expand Down Expand Up @@ -408,6 +509,7 @@ void Main_OnWiFiStatusChange(int code)
g_ln882h_pendingPowerSaveCommand = -1;
}
#endif
}
break;
/* for softap mode */
case WIFI_AP_CONNECTED:
Expand Down Expand Up @@ -1356,7 +1458,14 @@ void Main_Init_After_Delay()
ADDLOGF_INFO("###### safe mode activated - boot failures %d", g_bootFailures);
}
#if ALLOW_SSID2
Init_WiFiSSIDactual_FromChannelIfSet();//Channel must be set in early.bat using CMD_setStartupSSIDChannel
// Init_WiFiSSIDactual_FromChannelIfSet();//Channel must be set in early.bat using CMD_setStartupSSIDChannel
int ssidret= HAL_FlashVars_GetChannelValue(SSIDRetainChannel) ;
if (ssidret %100 == 42)
{
g_SSIDactual = ( (ssidret %1000) / 100) % 2 ; // check magic value and set SSID: 0=SSID1 1=SSID2
if ( ((ssidret %1000) / 100) >= 4 ) g_SSIDactual += 4; // set to 4 for "fix" SSID1 and 5 for "fix" SSID2 (no switching)
ADDLOGF_INFO("setting g_SSIDactual to %i (CH7 val=%i)", g_SSIDactual , ssidret );
} else ADDLOGF_INFO("g_SSIDactual (%i) unchanged - wrong magic number (CH7 val=%i)", g_SSIDactual , ssidret );
#endif
wifi_ssid = CFG_GetWiFiSSIDX();
wifi_pass = CFG_GetWiFiPassX();
Expand Down
Loading