Last active
November 28, 2019 10:19
-
-
Save ElectricImpSampleCode/327ff62669c6b1a0062be3f7fe1b6fba to your computer and use it in GitHub Desktop.
impOS 42 local networking interface availability checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The 'interfaces' variables holds references to available interfaces | |
local interfaces = {}; | |
// Set flags that the app can check to see if an interface can be used | |
local availableForLocal = { "wifi" : false, | |
"ethernet" : false, | |
"cell" : false }; | |
// These globals are used for reporting | |
local interfaceCount = 0; | |
local reportCount = 0; | |
// Functions | |
function stateChangeCallback(state) { | |
// This is the state-change callback function, which we bind to the interface record | |
// so that we know which interface's state change triggered the callback | |
// Is the interface available for use? It is if it's connected | |
this.isAvailable = (state == imp.net.CONNECTED); | |
// Set the app-level indicators | |
if (this.isAvailable) { | |
availableForLocal[this.type] = true; | |
} | |
// When we reach a terminal state consider the interface reported | |
if (state == imp.net.CONNECTED || | |
(state >= imp.net.WIFI_STOPPED && state <= imp.net.WIFI_STOPPED_UNHAPPY) || | |
(state >= imp.net.ETHERNET_STOPPED && state <= imp.net.ETHERNET_STOPPED_NO_LINK)) { | |
// Indirectly close the interface, by nulling the variable referring to it | |
// NOTE You may prefer to comment out this line in order to keep the interface | |
// open, so that the app's availability information is updated continuously. | |
// This is useful with, for example, a mobile device that may need to change | |
// to another interface due to connectivity at its current location. | |
this.interface = null; | |
reportCount++; | |
} | |
} | |
function getAvailableInterfaces() { | |
// Use 'imp.net.info()' to acquire a list of available interfaces | |
// then check each for their current state | |
local netInfo = imp.net.info(); | |
interfaceCount = 0; | |
reportCount = 0; | |
// Run through the known interfaces to see which may be used: these are | |
// recorded by impOS in the 'interface' table from imp.net.info()'s results | |
foreach (interface in netInfo.interface) { | |
// Prepare a new app-level record for the interface | |
local interfaceRecord = {}; | |
interfaceRecord.isAvailable <- true; | |
interfaceRecord.type <- interface.type; | |
// Now open the interface for local network to test its availability | |
// When the callback triggers, it sets the availability-by-type flags, etc. | |
// NOTE Don't action for cellular -- if we're on a cellular link to the impCloud, | |
// the state-change callback is not triggered | |
if (interface.type != "cell") { | |
interfaceCount++; | |
interfaceRecord.interface <- imp.net.open({"interface": interface.name}, | |
stateChangeCallback.bindenv(interfaceRecord)); | |
} | |
// Record the interface in the table of interfaces, using the interface name as the slot's key | |
interfaces[interface.name] <- interfaceRecord; | |
} | |
} | |
function reportInterfaces() { | |
// Subsidiary output function | |
function generateReport(type) { | |
local list = ""; | |
foreach (interfaceName, interfaceRecord in interfaces) { | |
if (interfaceRecord.type == type) list += interfaceName + ", " | |
} | |
local report = "can connect to the impCloud"; | |
if (list.len() > 0) { | |
if (availableForLocal[type]) { | |
report += (" and is available for local networking on interface(s) " + list.slice(0, list.len() - 2)); | |
} else { | |
report += " but not to local networks"; | |
} | |
return report; | |
} | |
return "no interface(s) present"; | |
} | |
if (reportCount < interfaceCount) { | |
// Not all interfaces have completed checking, so try again in 0.5s | |
imp.wakeup(0.5, reportInterfaces); | |
} else { | |
// We have completed checks on all possible interfaces, so report the results | |
server.log(" WiFi : " + generateReport("wifi")); | |
server.log("Ethernet : " + generateReport("ethernet")); | |
server.log("Cellular : " + generateReport("cell")); | |
} | |
} | |
// Probe the imp's interfaces | |
getAvailableInterfaces(); | |
// Begin checking whether we can report back | |
reportInterfaces(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment