Skip to content

Commit

Permalink
Check if wireless.radio0 is disabled (#987)
Browse files Browse the repository at this point in the history
* tesT(wifi): check for disabled radio0

* fix(wifi): check for disabled radio0

* test(wifi): checks that wifi radio check still resolves
  • Loading branch information
HipsterBrown authored and rwaldron committed Sep 20, 2016
1 parent b9ec5a9 commit 87ee3a6
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/tessel/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]'];
};
Expand Down
13 changes: 12 additions & 1 deletion lib/tessel/wifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
91 changes: 91 additions & 0 deletions test/unit/wifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
});
}

};
Expand Down

0 comments on commit 87ee3a6

Please sign in to comment.