From 87ee3a66968daf67d1dbc9b7ca833a358acfa0cf Mon Sep 17 00:00:00 2001 From: Nick Hehr Date: Tue, 20 Sep 2016 17:13:03 -0400 Subject: [PATCH] Check if wireless.radio0 is disabled (#987) * tesT(wifi): check for disabled radio0 * fix(wifi): check for disabled radio0 * test(wifi): checks that wifi radio check still resolves --- lib/tessel/commands.js | 3 ++ lib/tessel/wifi.js | 13 +++++- test/unit/wifi.js | 91 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) diff --git a/lib/tessel/commands.js b/lib/tessel/commands.js index 50705f61..c1c233c5 100644 --- a/lib/tessel/commands.js +++ b/lib/tessel/commands.js @@ -167,6 +167,9 @@ module.exports.turnAccessPointOn = function() { module.exports.turnAccessPointOff = function() { return ['uci', 'set', 'wireless.@wifi-iface[1].disabled=1']; }; +module.exports.turnRadioOn = function() { + return ['uci', 'set', 'wireless.radio0.disabled=0']; +}; module.exports.getAccessPointConfig = function() { return ['uci', 'show', 'wireless.@wifi-iface[1]']; }; diff --git a/lib/tessel/wifi.js b/lib/tessel/wifi.js index 297bc3f4..c425965e 100644 --- a/lib/tessel/wifi.js +++ b/lib/tessel/wifi.js @@ -163,7 +163,18 @@ Tessel.prototype.setWiFiState = function(enable) { return new Promise((resolve, reject) => { return this.simpleExec(commands.turnOnWifi(enable)) .then(() => this.simpleExec(commands.commitWirelessCredentials())) - .then(() => this.simpleExec(commands.reconnectWifi())) + .then(() => { + return this.simpleExec(commands.reconnectWifi()) + .then((result) => { + // check if the actual wifi radio (wireless.radio0) is disabled + if (result.includes("'radio0' is disabled")) { + return this.simpleExec(commands.turnRadioOn()) + .then(() => this.simpleExec(commands.commitWirelessCredentials())) + .then(() => this.simpleExec(commands.reconnectWifi())) + .catch(reject); + } + }); + }) .then(() => { var settle = (rejection) => { if (rejection) { diff --git a/test/unit/wifi.js b/test/unit/wifi.js index f61c0e45..483c5087 100644 --- a/test/unit/wifi.js +++ b/test/unit/wifi.js @@ -524,6 +524,7 @@ module.exports['Tessel.setWifiState'] = { this.tessel = TesselSimulator(); this.simpleExec = this.sandbox.spy(this.tessel, 'simpleExec'); this.turnOnWifi = this.sandbox.spy(commands, 'turnOnWifi'); + this.turnRadioOn = this.sandbox.spy(commands, 'turnRadioOn'); this.commitWirelessCredentials = this.sandbox.spy(commands, 'commitWirelessCredentials'); this.reconnectWifi = this.sandbox.spy(commands, 'reconnectWifi'); this.getWifiInfo = this.sandbox.spy(commands, 'getWifiInfo'); @@ -613,6 +614,96 @@ module.exports['Tessel.setWifiState'] = { test.ok(false, error.toString()); test.done(); }); + }, + setWifiStateWhenRadioOff: function(test) { + test.expect(9); + var state = true; + + // Test is expecting several closes...; + this.tessel._rps.on('control', (command) => { + if (command.toString() === 'ubus call iwinfo info {"device":"wlan0"}' && state) { + // Write to stdout so it completes as expected + // Wrap in setImmediate to make sure listener is set up before emitting + setImmediate(() => { + this.tessel._rps.stdout.emit('data', new Buffer('signal')); + this.tessel._rps.emit('close'); + }); + } else if (command.toString() === 'wifi') { + setImmediate(() => { + this.tessel._rps.stdout.emit('data', new Buffer(`'radio0' is disabled`)); + this.tessel._rps.emit('close'); + }); + } else { + setImmediate(() => { + // Remove any listeners on stdout so we don't break anything when we write to it + this.tessel._rps.stdout.removeAllListeners(); + this.tessel._rps.emit('close'); + }); + } + }); + + this.tessel.setWiFiState(state) + .then(() => { + test.equal(this.simpleExec.callCount, 6); + test.equal(this.turnOnWifi.callCount, 1); + test.equal(this.turnRadioOn.callCount, 1); + test.deepEqual(this.turnOnWifi.lastCall.returnValue, ['uci', 'set', 'wireless.@wifi-iface[0].disabled=0']); + test.equal(this.commitWirelessCredentials.callCount, 2); + test.equal(this.reconnectWifi.callCount, 2); + test.equal(this.info.calledOnce, true); + test.equal(this.info.lastCall.args[1].indexOf('Enabled.') !== -1, true); + test.equal(this.getWifiInfo.callCount, 1); + test.done(); + }) + .catch(error => { + test.ok(false, error.toString()); + test.done(); + }); + }, + setWifiStateError: function(test) { + test.expect(9); + var state = true; + + // Test is expecting several closes...; + this.tessel._rps.on('control', (command) => { + if (command.toString() === 'ubus call iwinfo info {"device":"wlan0"}' && state) { + // Write to stdout so it completes as expected + // Wrap in setImmediate to make sure listener is set up before emitting + setImmediate(() => { + this.tessel._rps.stdout.emit('data', new Buffer('signal')); + this.tessel._rps.emit('close'); + }); + } else if (command.toString() === 'wifi') { + setImmediate(() => { + this.tessel._rps.stdout.emit('data', new Buffer(`Some other error`)); + this.tessel._rps.emit('close'); + }); + } else { + setImmediate(() => { + // Remove any listeners on stdout so we don't break anything when we write to it + this.tessel._rps.stdout.removeAllListeners(); + this.tessel._rps.emit('close'); + }); + } + }); + + this.tessel.setWiFiState(state) + .then(() => { + test.equal(this.simpleExec.callCount, 3); + test.equal(this.turnOnWifi.callCount, 1); + test.equal(this.turnRadioOn.callCount, 0); + test.deepEqual(this.turnOnWifi.lastCall.returnValue, ['uci', 'set', 'wireless.@wifi-iface[0].disabled=0']); + test.equal(this.commitWirelessCredentials.callCount, 1); + test.equal(this.reconnectWifi.callCount, 1); + test.equal(this.info.calledOnce, true); + test.equal(this.info.lastCall.args[1].indexOf('Enabled.') !== -1, true); + test.equal(this.getWifiInfo.callCount, 1); + test.done(); + }) + .catch(error => { + test.ok(false, error.toString()); + test.done(); + }); } };