Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sdk:scripts:best-operator [2015/05/05 17:04] (current)
Line 1: Line 1:
 +====== SDK Script best-operator.are ======
 +<code c best-operator.are>​
 +/* DESC: This script will scan for operators on startup and choose the one with the best signal
 + * Copyright (C) 2014 NetModule AG, Switzerland
 + *
 + */
 +
 +ME = argv[0]; ​          /* script path */
 +ARGV1 = argv[1]; ​       /* 1st script argument */
 +SCAN_INTERVAL = 30;     /* scan interval */
 +REG_TIMEOUT = 60;       /* registration timeout */
 +
 +nb_syslog("​starting %s", ME);
 +
 +lai = nb_config_get("​sim.0.lai"​);​
 +if (lai != ""​) {
 +    nb_syslog("​unsetting lai '​%s'​ of SIM1", lai);
 +    nb_config_set("​sim.0.lai="​);​
 +    sleep(5);
 +}
 +
 +wanlink = nb_config_get("​wanlink.0.name"​);​
 +if (wanlink == "​wwan0"​) {
 +    mode = nb_config_get("​wanlink.0.mode"​);​
 +    if (mode == "​1"​) {
 +        nb_syslog("​disabling 1st wanlink"​);​
 +        nb_config_set("​wanlink.0.mode=0"​);​
 +        sleep(5);
 +    }
 +}
 +
 +
 +/* scan Mobile1 until we got networks */
 +while (1) {
 +    nb_syslog("​scanning networks on Mobile1"​);​
 +    nets = nb_scan_networks("​Mobile1"​);​
 +        ​
 +    nr_nets = struct_get(nets,​ "​NETWORK_COUNT"​);​
 +    if (!is_void(nr_nets) && nr_nets > 0) {
 +        nb_syslog("​%d networks found",​ nr_nets);
 +        break;
 +    }
 +
 +    nb_syslog("​no networks found, scanning again in %ds", SCAN_INTERVAL);​
 +    sleep(SCAN_INTERVAL);​
 +}
 +
 +
 +networks = mkarray();
 +netcount = 0;
 +
 +for (i = 1; i <= nr_nets; i++) {
 +    net = trim(struct_get(nets,​ sprintf("​NETWORK%d_NAME",​ i)));
 +    lai = trim(struct_get(nets,​ sprintf("​NETWORK%d_LAI",​ i)));
 +    status = struct_get(nets,​ sprintf("​NETWORK%d_STATUS",​ i));
 +
 +    if (is_void(net) || is_void(lai) || is_void(status)) continue;
 +
 +    if (trim(status) == "​available"​ && net != ""​) {
 +        nb_syslog("​detected network '​%s'​ (lai %s)", net, lai);
 +        networks[netcount++] = mkstruct("​network",​ net, 
 +                                        "​lai",​ lai,
 +                                        "​signal",​ -999); ​
 +    } else {
 +        nb_syslog("​skipping invalid network '​%s'​ (%s)", net, status);
 +    }
 +}
 +
 +bestnet = "";​
 +bestlai = -1;
 +bestsignal = -999;
 +
 +for (i = 0; i < netcount; i++) {
 +    network = networks[i];​
 +    net = struct_get(network,​ "​network"​);​
 +    lai = struct_get(network,​ "​lai"​);​
 +
 +    nb_syslog("​setting network '​%s'​ (lai %s) over Mobile1",​ net, lai);
 +    nb_config_set(sprintf("​sim.0.lai=%s",​ lai));
 +
 +    nb_syslog("​waiting until we have registered to '​%s'",​ net);
 +    sleep(10);
 +
 +    signal = -999;
 +    for (r = 0; r < REG_TIMEOUT;​ r += 10) {
 +        status = nb_status("​wwan"​);​
 +        regstate = struct_get(status,​ "​MOBILE1_REGISTRATION_STATE"​);​
 +        nb_syslog("​registration state of '​%s'​ is '​%s'",​ net, regstate);
 +
 +        if (regstate == "​registeredInHomeNetwork"​ || regstate == "​registeredRoaming"​) {
 +            nb_syslog("​waiting for signal strength of '​%s'​ to settle down", net);
 +            sleep(10);
 +            status = nb_status("​wwan"​);​
 +            s = (int) struct_get(status,​ "​MOBILE1_SIGNAL"​);​
 +            if (s < -113) {
 +                signal = s;
 +                nb_syslog("​%s at '​%s'​ with %d dBm", regstate, net, signal);
 +                break;
 +            } else {
 +                nb_syslog("​saw invalid signal strength %d", s);
 +            }
 +        }
 +        sleep(10);
 +    }
 +
 +    if (bestnet == ""​ || bestlai == -1 || signal > bestsignal) {
 +        bestnet = net;
 +        bestsignal = signal;
 +        bestlai = lai;
 +    }
 +}
 +
 +if (bestlai == -1) {
 +    nb_syslog("​no best operator found"​);​
 +} else {
 +
 +    nb_syslog("​setting best operator '​%s'​ (lai %s)", bestnet, bestlai);
 +    nb_config_set(sprintf("​sim.0.lai=%s",​ bestlai));
 +    sleep(5);
 +
 +    nb_syslog("​activating 1st wanlink"​);​
 +    nb_config_set("​wanlink.0.mode=1"​);​
 +}
 +
 +nb_syslog("​done"​);​
 +
 +exit(0);
 +
 +</​code>​