Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if wireless.radio0 is disabled #987

Merged
merged 3 commits into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if there is an error that doesnt include "'radio0' is disabled"? I think this needs to return Promise.resolve()?

(Sorry, I reposted this because I'm not sure if we're officially using Github Review yet)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe, and after experimenting, once the execution of a thenable function finishes, the Promise will continue to the next chained method. Returning in a thenable function will pass that value to the next thenable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping track of promises is not my cup of tea. Thanks for clarifying

});
})
.then(() => {
var settle = (rejection) => {
if (rejection) {
Expand Down
46 changes: 46 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,51 @@ 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();
});
}

};
Expand Down