Skip to content

Commit 31c441d

Browse files
authored
Merge pull request #195 from HipsterBrown/fetch-network-settings
Wifi.connection(): Fetch network settings, normalize values
2 parents fa5c31a + 0c12fd8 commit 31c441d

File tree

3 files changed

+270
-96
lines changed

3 files changed

+270
-96
lines changed

node/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"version": "0.0.9",
44
"description": "",
55
"main": "tessel.js",
6-
"dependencies": {
7-
},
6+
"dependencies": {},
87
"devDependencies": {
98
"grunt": "^0.4.5",
109
"grunt-cli": "^0.1.13",
@@ -13,10 +12,11 @@
1312
"grunt-contrib-watch": "^0.6.1",
1413
"grunt-jsbeautifier": "^0.2.10",
1514
"grunt-jscs": "^2.6.0",
15+
"npm": "^3.10.5",
1616
"sinon": "^1.14.1"
1717
},
1818
"scripts": {
19-
"test": "echo \"Error: no test specified\" && exit 1"
19+
"test": "grunt"
2020
},
2121
"author": "Technical Machine <[email protected]>",
2222
"license": "MIT"

node/tessel-export.js

+101-50
Original file line numberDiff line numberDiff line change
@@ -1223,19 +1223,10 @@ Tessel.LED.prototype.read = function(callback) {
12231223

12241224
Tessel.Wifi = function() {
12251225
var state = {
1226-
settings: {},
1227-
connected: false
1226+
settings: {}
12281227
};
12291228

12301229
Object.defineProperties(this, {
1231-
isConnected: {
1232-
get: () => state.connected
1233-
},
1234-
connected: {
1235-
set: (value) => {
1236-
state.connected = value;
1237-
}
1238-
},
12391230
settings: {
12401231
get: () => state.settings,
12411232
set: (settings) => {
@@ -1255,9 +1246,10 @@ Tessel.Wifi.prototype.enable = function(callback) {
12551246
turnOnWifi()
12561247
.then(commitWireless)
12571248
.then(restartWifi)
1258-
.then(() => {
1249+
.then(getWifiInfo)
1250+
.then((network) => {
1251+
Object.assign(this.settings, network);
12591252
this.emit('connect', this.settings);
1260-
this.connected = true;
12611253
callback();
12621254
})
12631255
.catch((error) => {
@@ -1276,7 +1268,6 @@ Tessel.Wifi.prototype.disable = function(callback) {
12761268
.then(commitWireless)
12771269
.then(restartWifi)
12781270
.then(() => {
1279-
this.connected = false;
12801271
this.emit('disconnect');
12811272
callback();
12821273
})
@@ -1291,11 +1282,11 @@ Tessel.Wifi.prototype.reset = function(callback) {
12911282
callback = function() {};
12921283
}
12931284

1294-
this.connected = false;
12951285
this.emit('disconnect', 'Resetting connection');
12961286
restartWifi()
1297-
.then(() => {
1298-
this.connected = true;
1287+
.then(getWifiInfo)
1288+
.then((network) => {
1289+
Object.assign(this.settings, network);
12991290
this.emit('connect', this.settings);
13001291
callback();
13011292
})
@@ -1305,12 +1296,34 @@ Tessel.Wifi.prototype.reset = function(callback) {
13051296
});
13061297
};
13071298

1308-
Tessel.Wifi.prototype.connection = function() {
1309-
if (this.isConnected) {
1310-
return this.settings;
1311-
} else {
1312-
return null;
1299+
Tessel.Wifi.prototype.connection = function(callback) {
1300+
if (typeof callback !== 'function') {
1301+
callback = function() {};
13131302
}
1303+
1304+
isEnabled()
1305+
.then((enabled) => {
1306+
if (enabled) {
1307+
getWifiInfo()
1308+
.then((network) => {
1309+
delete network.password;
1310+
1311+
this.settings = network;
1312+
1313+
callback(null, network);
1314+
})
1315+
.catch((error) => {
1316+
this.emit('error', error);
1317+
callback(error);
1318+
});
1319+
} else {
1320+
callback(null, null);
1321+
}
1322+
})
1323+
.catch((error) => {
1324+
this.emit('error', error);
1325+
callback(error);
1326+
});
13141327
};
13151328

13161329
Tessel.Wifi.prototype.connect = function(settings, callback) {
@@ -1340,7 +1353,6 @@ Tessel.Wifi.prototype.connect = function(settings, callback) {
13401353
delete settings.password;
13411354

13421355
this.settings = Object.assign(network, settings);
1343-
this.connected = true;
13441356
this.emit('connect', this.settings);
13451357

13461358
callback(null, this.settings);
@@ -1456,40 +1468,69 @@ function isEnabled() {
14561468
function getWifiInfo() {
14571469
return new Promise((resolve, reject) => {
14581470
var checkCount = 0;
1471+
var rbcast = /(Bcast):([\w\.]+)/;
14591472

14601473
function recursiveWifi() {
1461-
setImmediate(() => {
1462-
childProcess.exec(`ubus call iwinfo info '{"device":"wlan0"}'`, (error, results) => {
1463-
if (error) {
1464-
recursiveWifi();
1465-
} else {
1466-
try {
1467-
var network = JSON.parse(results);
1468-
1469-
if (network.ssid === undefined) {
1470-
// using 6 because it's the lowest count with accurate results after testing
1471-
if (checkCount < 6) {
1472-
checkCount++;
1473-
recursiveWifi();
1474-
} else {
1475-
var msg = 'Tessel is unable to connect, please check your credentials or list of available networks (using tessel.network.wifi.findAvailableNetworks()) and try again.';
1476-
throw msg;
1477-
}
1474+
childProcess.exec(`ubus call iwinfo info '{"device":"wlan0"}'`, (error, results) => {
1475+
if (error) {
1476+
recursiveWifi();
1477+
} else {
1478+
try {
1479+
var network = JSON.parse(results);
1480+
1481+
if (network.ssid === undefined) {
1482+
// using 6 because it's the lowest count with accurate results after testing
1483+
if (checkCount < 6) {
1484+
checkCount++;
1485+
recursiveWifi();
14781486
} else {
1479-
childProcess.exec('ifconfig wlan0', (error, ipResults) => {
1480-
if (error) {
1481-
reject(error);
1482-
} else {
1483-
network.ips = ipResults.split('\n');
1484-
resolve(network);
1485-
}
1486-
});
1487+
var msg = 'Tessel is unable to connect, please check your credentials or list of available networks (using tessel.network.wifi.findAvailableNetworks()) and try again.';
1488+
throw msg;
14871489
}
1488-
} catch (error) {
1489-
reject(error);
1490+
} else {
1491+
recursiveIP(network);
14901492
}
1493+
} catch (error) {
1494+
reject(error);
14911495
}
1492-
});
1496+
}
1497+
});
1498+
}
1499+
1500+
// when immediately connecting and restarting the wifi chip, it takes a few moments before an IP address is broadcast to Tessel.
1501+
// This function keeps checking for that IP until it's available.
1502+
function recursiveIP(network) {
1503+
childProcess.exec('ifconfig wlan0', (error, ipResults) => {
1504+
if (error) {
1505+
reject(error);
1506+
} else {
1507+
var bcastMatches = ipResults.match(rbcast);
1508+
1509+
if (bcastMatches === null) {
1510+
recursiveWifi(network);
1511+
} else {
1512+
// Successful matches will have a result that looks like:
1513+
// ["Bcast:0.0.0.0", "Bcast", "0.0.0.0"]
1514+
if (bcastMatches.length === 3) {
1515+
network.ip = bcastMatches[2];
1516+
} else {
1517+
recursiveWifi(network);
1518+
}
1519+
}
1520+
1521+
// attempt to parse out the security configuration from the returned network object
1522+
if (network.encryption.enabled) {
1523+
if (network.encryption.wep) {
1524+
network.security = 'wep';
1525+
} else if (network.encryption.authentication && network.encryption.wpa) {
1526+
// sets "security" to either psk or psk2
1527+
network.security = `${network.encryption.authentication[0]}${network.encryption.wpa[0] === 2 ? 2 : null}`;
1528+
}
1529+
} else {
1530+
network.security = 'none';
1531+
}
1532+
resolve(network);
1533+
}
14931534
});
14941535
}
14951536

@@ -1522,6 +1563,16 @@ function scanWifi() {
15221563
// Parse the security type - unused at the moment
15231564
security: encryptionRegex.exec(entry)[1],
15241565
};
1566+
1567+
// normalize security info to match configuration settings, i.e. none, wep, psk, psk2. "none" is already set correctly
1568+
if (networkInfo.security.includes('WEP')) {
1569+
networkInfo.security = 'wep';
1570+
} else if (networkInfo.security.includes('WPA2')) {
1571+
networkInfo.security = 'psk2';
1572+
} else if (networkInfo.security.includes('WPA')) {
1573+
networkInfo.security = 'psk';
1574+
}
1575+
15251576
// Add this parsed network to our array
15261577
networks.push(networkInfo);
15271578
} catch (error) {

0 commit comments

Comments
 (0)