Skip to content

Commit

Permalink
fix(wifi): remove setImmediate, add Bcast regex
Browse files Browse the repository at this point in the history
  • Loading branch information
HipsterBrown committed Aug 16, 2016
1 parent 91a540c commit 0c12fd8
Showing 1 changed file with 46 additions and 45 deletions.
91 changes: 46 additions & 45 deletions node/tessel-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -1387,68 +1387,69 @@ function isEnabled() {
function getWifiInfo() {
return new Promise((resolve, reject) => {
var checkCount = 0;
var rbcast = /(Bcast):([\w\.]+)/;

function recursiveWifi() {
setImmediate(() => {
childProcess.exec(`ubus call iwinfo info '{"device":"wlan0"}'`, (error, results) => {
if (error) {
recursiveWifi();
} else {
try {
var network = JSON.parse(results);

if (network.ssid === undefined) {
// using 6 because it's the lowest count with accurate results after testing
if (checkCount < 6) {
checkCount++;
recursiveWifi();
} else {
var msg = 'Tessel is unable to connect, please check your credentials or list of available networks (using tessel.network.wifi.findAvailableNetworks()) and try again.';
throw msg;
}
childProcess.exec(`ubus call iwinfo info '{"device":"wlan0"}'`, (error, results) => {
if (error) {
recursiveWifi();
} else {
try {
var network = JSON.parse(results);

if (network.ssid === undefined) {
// using 6 because it's the lowest count with accurate results after testing
if (checkCount < 6) {
checkCount++;
recursiveWifi();
} else {
recursiveIP(network);
var msg = 'Tessel is unable to connect, please check your credentials or list of available networks (using tessel.network.wifi.findAvailableNetworks()) and try again.';
throw msg;
}
} catch (error) {
reject(error);
} else {
recursiveIP(network);
}
} catch (error) {
reject(error);
}
});
}
});
}

// when immediately connecting and restarting the wifi chip, it takes a few moments before an IP address is broadcast to Tessel.
// This function keeps checking for that IP until it's available.
function recursiveIP(network) {
setImmediate(() => {
childProcess.exec('ifconfig wlan0', (error, ipResults) => {
if (error) {
reject(error);
} else if (ipResults.includes('Bcast') === false) {
childProcess.exec('ifconfig wlan0', (error, ipResults) => {
if (error) {
reject(error);
} else {
var bcastMatches = ipResults.match(rbcast);

if (bcastMatches === null) {
recursiveWifi(network);
} else {
network.ip = ipResults
.split('\n') // split multiple line string into array of strings
.filter((string) => string.includes('Bcast'))[0] // find the string containing a reference to "Bcast" (Broadcast IP)
.trim() // remove extra whitespace from either side of the string
.split(' ') // split the string by whitespace between words
.filter((string) => string.includes('Bcast'))[0] // find the string containing a reference to "Bcast"
.split(':')[1]; // split the string by the ":", i.e. "Bcast:10.0.0.11" and grab the number as the 2nd item in that array

// attempt to parse out the security configuration from the returned network object
if (network.encryption.enabled) {
if (network.encryption.wep) {
network.security = 'wep';
} else if (network.encryption.authentication && network.encryption.wpa) {
// sets "security" to either psk or psk2
network.security = `${network.encryption.authentication[0]}${network.encryption.wpa[0] === 2 ? 2 : null}`;
}
// Successful matches will have a result that looks like:
// ["Bcast:0.0.0.0", "Bcast", "0.0.0.0"]
if (bcastMatches.length === 3) {
network.ip = bcastMatches[2];
} else {
network.security = 'none';
recursiveWifi(network);
}
resolve(network);
}
});

// attempt to parse out the security configuration from the returned network object
if (network.encryption.enabled) {
if (network.encryption.wep) {
network.security = 'wep';
} else if (network.encryption.authentication && network.encryption.wpa) {
// sets "security" to either psk or psk2
network.security = `${network.encryption.authentication[0]}${network.encryption.wpa[0] === 2 ? 2 : null}`;
}
} else {
network.security = 'none';
}
resolve(network);
}
});
}

Expand Down

0 comments on commit 0c12fd8

Please sign in to comment.