wifimodechanger.are
### Change Wifi mode if AP is available
 
 
## Variable that hold the configs for both WLAN Modes
ClientConfig="";
ApConfig="";
nextWlanMode="access-point"; // defines if we change to Client Mode or AP Mode
 
 
IFC = "WLAN1";
homeNetwork = "NMFFM";
MIN_SIGNAL = -110;
 
 
ApConfig=sprintf("%s %s",ApConfig,"wlan.0.status=1");
ApConfig=sprintf("%s %s",ApConfig,"wlan.0.mode=access-point");
ApConfig=sprintf("%s %s",ApConfig,"wlan.0.regdom=DE");
ApConfig=sprintf("%s %s",ApConfig,"wlan.0.settings.channel=1");
ApConfig=sprintf("%s %s",ApConfig,"wlan.0.vss.0.status=1");
ApConfig=sprintf("%s %s",ApConfig,"wlan.0.vss.0.ssid=NB1600");
ApConfig=sprintf("%s %s",ApConfig,"wlan.0.vss.0.secmode=wpa-psk");
ApConfig=sprintf("%s %s",ApConfig,"wlan.0.vss.0.psk=passwortpasswort");
ApConfig=sprintf("%s %s",ApConfig,"network.wlan.0.mode=routed");
ApConfig=sprintf("%s %s",ApConfig,"network.wlan.0.address=192.168.200.1");
ApConfig=sprintf("%s %s",ApConfig,"network.wlan.0.netmask=255.255.255.0");
ApConfig=sprintf("%s %s",ApConfig,"network.dhcp.200.mode=1");
ApConfig=sprintf("%s %s",ApConfig,"network.dhcp.200.start=192.168.200.100");
ApConfig=sprintf("%s %s",ApConfig,"network.dhcp.200.end=192.168.200.199");
ApConfig=sprintf("%s %s",ApConfig,"network.dhcp.200.ttl=7200");
ApConfig=sprintf("%s %s",ApConfig,"network.wlan.0.brdev=");
 
 
ClientConfig=sprintf("%s %s",ClientConfig,"wlan.0.status=1");
ClientConfig=sprintf("%s %s",ClientConfig,"wlan.0.mode=client");
ClientConfig=sprintf("%s %s",ClientConfig,"wlan.0.regdom=DE");
ClientConfig=sprintf("%s %s",ClientConfig,"wlan.0.settings.channel=1");
ClientConfig=sprintf("%s %s",ClientConfig,"wlan.0.client.0.ssid=NMFFM");
ClientConfig=sprintf("%s %s",ClientConfig,"wlan.0.client.0.secmode=wpa-psk");
ClientConfig=sprintf("%s %s",ClientConfig,"wlan.0.client.0.secproto=wpa2");
ClientConfig=sprintf("%s %s",ClientConfig,"wlan.0.client.0.cipher=ccmp");
ClientConfig=sprintf("%s %s",ClientConfig,"wlan.0.client.0.psk=passwortpasswort");
ClientConfig=sprintf("%s %s",ClientConfig,"wlan.0.client.0.minsst=-100");
ClientConfig=sprintf("%s %s",ClientConfig,"wlan.0.client.0.prio=10");
ClientConfig=sprintf("%s %s",ClientConfig,"network.wlan.0.mode=static");
ClientConfig=sprintf("%s %s",ClientConfig,"network.wlan.0.address=172.31.64.242");
ClientConfig=sprintf("%s %s",ClientConfig,"network.wlan.0.netmask=255.255.255.0");
 
 
 
currentWlanMode=nb_config_get("wlan.0.mode");
 
 
 
/* ATTENTION: Scanning for WLAN networks will tear down any running access-point */
nets = nb_scan_networks(IFC);
nr_nets = struct_get(nets, "NETWORK_COUNT");
 
if (is_void(nr_nets) || nr_nets < 1) {
    nb_syslog("no networks found on %s", IFC);
    exit(0);
}
 
/* lookup WLAN networks */
for (i = 1; i <= nr_nets; i++) {
    k = sprintf("NETWORK%d_SSID", i);
    ssid = struct_get(nets, k);
    k = sprintf("NETWORK%d_SIGNAL", i);    
    signal = (int)struct_get(nets, k);
 
    if (strlen(ssid) == 0) continue;
 
    nb_syslog("detected %s network '%s' (signal %d)", IFC, ssid, signal);
 
    if ((ssid == homeNetwork) && signal > MIN_SIGNAL) {
        nextWlanMode="client";
        break;
    }
}
 
 
if (nextWlanMode == "client" && currentWlanMode != "client" ) {
    nb_syslog("activating client mode for SSID %s", IFC, homeNetwork);
    nb_config_set(ClientConfig);
}
 
if (nextWlanMode == "access-point" && currentWlanMode != "access-point" ) {
    nb_syslog("activating access point mode");
    nb_config_set(ApConfig);
}
 
exit(0);