include "wifi-networks.cfg"; DEBUG=0; SLEEP=5; SCAN_TIME_WAIT=30; CFG_TIME_WAIT=120; string enc_to_cfg(enc) { switch(enc) { case "WPA2-SAE": enc = "wpa3"; break; case "WPA2-PSK": enc = "wpa2"; break; default: enc = "wpa2"; if(DEBUG) nb_syslog("wifi swticher: undefinded enc: %s, using wpa2...", enc); } return enc; } while(1) { // we get the current state of wanlink2 (wlan) wan_status=nb_status("wan"); state=struct_get(wan_status, "WANLINK2_STATE"); if(DEBUG) nb_syslog("wifi switcher: current state = %s", state); // in case the wlan connection is up or dialing we do not start a new scan if (state != "down") { sleep(SLEEP); continue; } // only scan networks if last scan is more than SCAN_TIME_WAIT in the past if(DEBUG) nb_syslog("wifi switcher: last scan was %ds ago", uptime() - SCAN_TIME); if (uptime() - SCAN_TIME > SCAN_TIME_WAIT){ SCAN_TIME=uptime(); scan_res = nb_scan_networks("WLAN1"); if(DEBUG) nb_syslog("wifi switcher: scanning WLAN networks took %ds", uptime() - SCAN_TIME); } found=false; i=1; // get all the wifi information from the ap do { ssid= struct_get(scan_res, sprintf("NETWORK%d_SSID", i)); chipher=struct_get(scan_res, sprintf("NETWORK%d_CIPHER_1", i)); enc= struct_get(scan_res, sprintf("NETWORK%d_ENCRYPTION_1", i)); /* if(DEBUG) nb_syslog("wifi switcher: position in do loop i = %d",i); */ i++; for (j=0; j CFG_TIME_WAIT){ CFG_TIME = uptime(); nb_config_set(cfg); if(DEBUG) nb_syslog("wifi switcher: new wlan config got setup"); } found=true; break; } } if(found) break; } while ( !is_void(ssid) ); sleep(SLEEP); }