SDK Script scan-wlan.are

scan-wlan.are
/* DESC: This script can be used to switch the WLAN client network according to availability
 * Copyright (C) 2013 NetModule AG, Switzerland
 */
 
IFC = "WLAN1";
NETWORK = "";
MIN_SIGNAL = -90;
 
/* 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 == "home-network" || ssid == "office-network") && signal > MIN_SIGNAL) {
        NETWORK = ssid;       
        break;
    }
}
 
if (NETWORK != "") {
    nb_syslog("activating %s network '%s'", IFC, NETWORK);
    cfg = sprintf("wlan.0.client.0.ssid=%s", NETWORK);
    nb_config_set(cfg);
} else {
    nb_syslog("no known networks found on %s", IFC);
}
 
exit(0);