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

Add new control request to set wifi creds #739

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cmd/serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ module.exports = class SerialCommand extends CLICommandBase {

// if device's firmware version is less than 6.0.0, use the old way
const fwVer = device.firmwareVersion;
// FIXME: get the correct version number
if (semver.lt(fwVer, '6.0.0')) {
// configure serial
if (file){
Expand Down
34 changes: 33 additions & 1 deletion src/lib/wifi-control-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = class WiFiControlRequest {
} else {
network = await this.getNetworkToConnect();
}
await this.joinWifi(network);
await this.setWifiCredentials(network);
}

async getNetworkToConnectFromJson() {
Expand Down Expand Up @@ -205,6 +205,38 @@ module.exports = class WiFiControlRequest {
throw this._handleDeviceError(lastError, { action: 'join Wi-Fi network' });
}

async setWifiCredentials({ ssid, password }) {
// open device by id
let retries = RETRY_COUNT;
this.newSpin(`Setting Wi-Fi credentials for '${ssid}'`).start();
let lastError;
while (retries > 0) {
try {
if (!this.device || this.device.isOpen === false) {
this.device = await usbUtils.getOneUsbDevice({ api: this.api, idOrName: this.deviceId });
}
const { pass } = await this.device.setWifiCredentials({ ssid, password }, { timeout: JOIN_NETWORK_TIMEOUT });
if (pass) {
this.stopSpin();
this.ui.stdout.write('Wi-Fi network configured successfully, your device should now restart.');
this.ui.stdout.write(os.EOL);
await this.device.reset();
return;
}
} catch (error) {
lastError = error;
await utilities.delay(TIME_BETWEEN_RETRIES);
retries--;
} finally {
if (this.device && this.device.isOpen) {
await this.device.close();
}
}
}
this.stopSpin();
throw this._handleDeviceError(lastError, { action: 'set Wi-Fi credentials' });
}

async pickNetworkManually() {
const ssid = await this._promptForSSID();
const password = await this._promptForPassword();
Expand Down
41 changes: 36 additions & 5 deletions src/lib/wifi-control-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe('Wifi Control Request', () => {
close: sinon.stub(),
reset: sinon.stub(),
scanWifiNetworks: sinon.stub(),
joinNewWifiNetwork: sinon.stub()
joinNewWifiNetwork: sinon.stub(),
setWifiCredentials: sinon.stub()
};
usbUtil.getOneUsbDevice = sinon.stub();
});
Expand Down Expand Up @@ -420,26 +421,56 @@ describe('Wifi Control Request', () => {
});
});

describe('setWifiCredentials', () => {
it('joins a network', async () => {
openDevice.setWifiCredentials.resolves({ pass: true });
usbUtil.getOneUsbDevice.resolves(openDevice);
const wifiControlRequest = new WifiControlRequest('deviceId', { ui, newSpin, stopSpin });
await wifiControlRequest.setWifiCredentials({ ssid: 'network1', password: 'password' });
expect(openDevice.setWifiCredentials).to.have.been.calledOnce;
expect(openDevice.setWifiCredentials).to.have.been.calledWith({ ssid: 'network1', password: 'password' }, { timeout: 30000 });
expect(newSpin).to.have.been.calledWith('Setting Wi-Fi credentials for \'network1\'');
expect(stopSpin).to.have.been.calledOnce;
expect(ui.stdout.write).to.have.been.calledWith('Wi-Fi network configured successfully, your device should now restart.');
});

it('throw error if fails', async () => {
openDevice.setWifiCredentials.rejects(new Error('error'));
usbUtil.getOneUsbDevice.resolves(openDevice);
let error;
const wifiControlRequest = new WifiControlRequest('deviceId', { ui, newSpin, stopSpin });
try {
await wifiControlRequest.setWifiCredentials({ ssid: 'network1', password: 'password' });
} catch (_error) {
error = _error;
}
expect(error.message).to.eql('Unable to set Wi-Fi credentials: error');
expect(openDevice.setWifiCredentials).to.have.been.calledWith({ ssid: 'network1', password: 'password' }, { timeout: 30000 });
expect(newSpin).to.have.been.calledWith('Setting Wi-Fi credentials for \'network1\'');
expect(stopSpin).to.have.been.calledOnce;
});
});

describe('configureWifi', () => {
it('performs the wifi configuration flow', async () => {
const wifiControlRequest = new WifiControlRequest('deviceId', { ui, newSpin, stopSpin });
wifiControlRequest.getNetworkToConnect = sinon.stub().resolves({ ssid: 'network1', password: 'password' });
wifiControlRequest.getNetworkToConnectFromJson = sinon.stub();
wifiControlRequest.joinWifi = sinon.stub().resolves(true);
wifiControlRequest.setWifiCredentials = sinon.stub().resolves(true);
await wifiControlRequest.configureWifi();
expect(wifiControlRequest.getNetworkToConnect).to.have.been.calledOnce;
expect(wifiControlRequest.joinWifi).to.have.been.calledOnce;
expect(wifiControlRequest.setWifiCredentials).to.have.been.calledOnce;
expect(wifiControlRequest.getNetworkToConnectFromJson).not.to.have.been.called;
});

it('performs the wifi configuration flow from json', async () => {
const wifiControlRequest = new WifiControlRequest('deviceId', { ui, newSpin, stopSpin, file: 'file' });
wifiControlRequest.getNetworkToConnect = sinon.stub();
wifiControlRequest.getNetworkToConnectFromJson = sinon.stub().resolves({ ssid: 'network1', password: 'password' });
wifiControlRequest.joinWifi = sinon.stub().resolves(true);
wifiControlRequest.setWifiCredentials = sinon.stub().resolves(true);
await wifiControlRequest.configureWifi();
expect(wifiControlRequest.getNetworkToConnect).not.to.have.been.called;
expect(wifiControlRequest.joinWifi).to.have.been.calledOnce;
expect(wifiControlRequest.setWifiCredentials).to.have.been.calledOnce;
expect(wifiControlRequest.getNetworkToConnectFromJson).to.have.been.calledOnce;
});
});
Expand Down
Loading