Skip to content

Commit

Permalink
fix(ap/wifi): working implementation
Browse files Browse the repository at this point in the history
This commit includes better error handling and security settings for the
access point creation method. Also included is setting the default wifi
encryption from the connectToNetwork method instead of having it as a
device default, which is crucial for setting up an access point without
connecting to wifi.
  • Loading branch information
HipsterBrown committed Nov 16, 2015
1 parent 5b3baee commit f6106f7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 42 deletions.
2 changes: 1 addition & 1 deletion bin/tessel-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ makeCommand('ap')
})
.option('security', {
abbr: 's',
help: 'Encryption to use on network (i.e. WEP, WPA, PSK).'
help: 'Encryption to use on network (i.e. wep, psk, psk2, wpa, wpa2).'
})
.option('trigger', {
position: 1,
Expand Down
91 changes: 55 additions & 36 deletions lib/tessel/access-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,46 +64,65 @@ Tessel.prototype.createAccessPoint = function(opts) {
var ssid = opts.ssid;
var password = opts.pass;
var security = opts.security;
var securityOptions = ['none', 'wep', 'psk', 'psk2', 'wpa', 'wpa2'];

return new Promise(function(resolve, reject) {
if (!ssid) {
return reject(new Error('Invalid credentials. Must set ssid'));
}
if (!ssid) {
return Promise.reject(new Error('Invalid credentials. Must set ssid'));
}

if (security && !password) {
return reject(new Error('Invalid credentials. Must set a password with security option'));
}
if (security && !password) {
return Promise.reject(new Error('Invalid credentials. Must set a password with security option'));
}

if (password && !security) {
security = 'psk2';
}
if (security && securityOptions.indexOf(security) < 0) {
return Promise.reject(new Error(security + ' is not a valid security option. Please choose on of the following: ' + securityOptions.join(', ')));
}

if (password && !security) {
security = 'psk2';
}

if (password && security) {
logs.info('Setting Access Point with SSID:', ssid, 'and password:', password, 'and security mode:', security);
} else if (!password && !security) {
security = 'none';
logs.info('Setting Access Point with SSID:', ssid);
}

var setSSID = function() {
return self.connection.exec(commands.setAccessPointSSID(ssid));
};

var setAccessPointPassword = function() {
return self.connection.exec(commands.setAccessPointPassword(password));
};

var setAccessPointSecurity = function() {
self.connection.exec(commands.setAccessPointSecurity(security));
};

if (password && security) {
logs.info('Setting Access Point with SSID:', ssid, 'and password:', password, 'and security mode:', security);
} else if (!password && !security) {
logs.info('Setting Access Point with SSID:', ssid);
var turnAccessPointOn = function() {
return self.connection.exec(commands.turnAccessPointOn());
};

var commitAndClosePromise = function() {
return new Promise(function(resolve) {
commitAndClose(self, resolve);
});
};

var setup = function() {
if (password) {
return setSSID(ssid)
.then(setAccessPointPassword)
.then(setAccessPointSecurity);
} else {
return setSSID(ssid)
.then(setAccessPointSecurity);
}
};

return self.connection.exec(commands.setAccessPointSSID(ssid))
.then(function() {
if (password) {
return self.connection.exec(commands.setAccessPointPassword(password))
.then(function() {
if (security) {
return self.connection.exec(commands.setAccessPointSecurity(security))
.then(function() {
return self.connection.exec(commands.turnAccessPointOn())
.then(commitAndClose.bind(null, self, resolve));
});
} else {
return self.connection.exec(commands.turnAccessPointOn())
.then(commitAndClose.bind(null, self, resolve));
}
});
} else {
return self.connection.exec(commands.turnAccessPointOn())
.then(commitAndClose.bind(null, self, resolve));
}
});
});
return setup()
.then(turnAccessPointOn)
.then(commitAndClosePromise);
};
4 changes: 4 additions & 0 deletions lib/tessel/wifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ Tessel.prototype.connectToNetwork = function(opts) {
// Then set the password
return self.connection.exec(commands.setNetworkPassword(password));
})
.then(function() {
// Then set the encryption
return self.connection.exec(commands.setNetworkEncryption('psk2'));
})
.then(function() {
// Then make sure wireless is enabled
return self.connection.exec(commands.turnOnWifi(true));
Expand Down
5 changes: 3 additions & 2 deletions test/unit/access-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ exports['Tessel.prototype.createAccessPoint'] = {
},

noPasswordNoSecurity: function(test) {
test.expect(7);
test.expect(8);
var self = this;
var creds = {
ssid: 'test',
Expand All @@ -74,11 +74,12 @@ exports['Tessel.prototype.createAccessPoint'] = {
.then(function() {
test.equal(self.setAccessPointSSID.callCount, 1);
test.equal(self.setAccessPointPassword.callCount, 0);
test.equal(self.setAccessPointSecurity.callCount, 0);
test.equal(self.setAccessPointSecurity.callCount, 1);
test.equal(self.reconnectWifi.callCount, 1);
test.equal(self.reconnectDnsmasq.callCount, 1);
test.equal(self.reconnectDhcp.callCount, 1);
test.ok(self.setAccessPointSSID.lastCall.calledWith(creds.ssid));
test.ok(self.setAccessPointSecurity.lastCall.calledWith('none'));
test.done();
})
.catch(function(error) {
Expand Down
12 changes: 9 additions & 3 deletions test/unit/wifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
this.logsInfo = sinon.stub(logs, 'info', function() {});
this.setNetworkSSID = sinon.spy(commands, 'setNetworkSSID');
this.setNetworkPassword = sinon.spy(commands, 'setNetworkPassword');
this.setNetworkEncryption = sinon.spy(commands, 'setNetworkEncryption');
this.commitWirelessCredentials = sinon.spy(commands, 'commitWirelessCredentials');
this.reconnectWifi = sinon.spy(commands, 'reconnectWifi');
this.tessel = TesselSimulator();
Expand All @@ -140,13 +141,14 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
this.logsInfo.restore();
this.setNetworkSSID.restore();
this.setNetworkPassword.restore();
this.setNetworkEncryption.restore();
this.commitWirelessCredentials.restore();
this.reconnectWifi.restore();
done();
},
noSSID: function(test) {
var self = this;
test.expect(3);
test.expect(4);
this.tessel.connectToNetwork({
ssid: undefined,
password: 'fish'
Expand All @@ -155,12 +157,13 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
test.ok(error);
test.equal(self.setNetworkSSID.callCount, 0);
test.equal(self.setNetworkPassword.callCount, 0);
test.equal(self.setNetworkEncryption.callCount, 0);
test.done();
});
},
noPassword: function(test) {
var self = this;
test.expect(3);
test.expect(4);
this.tessel.connectToNetwork({
ssid: 'tank',
password: undefined
Expand All @@ -169,12 +172,13 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
test.ok(error);
test.equal(self.setNetworkSSID.callCount, 0);
test.equal(self.setNetworkPassword.callCount, 0);
test.equal(self.setNetworkEncryption.callCount, 0);
test.done();
});
},
properCredentials: function(test) {
var self = this;
test.expect(6);
test.expect(8);
var creds = {
ssid: 'tank',
password: 'fish'
Expand All @@ -191,10 +195,12 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
.then(function() {
test.equal(self.setNetworkSSID.callCount, 1);
test.equal(self.setNetworkPassword.callCount, 1);
test.equal(self.setNetworkEncryption.callCount, 1);
test.equal(self.commitWirelessCredentials.callCount, 1);
test.equal(self.reconnectWifi.callCount, 1);
test.ok(self.setNetworkSSID.lastCall.calledWith(creds.ssid));
test.ok(self.setNetworkPassword.lastCall.calledWith(creds.password));
test.ok(self.setNetworkEncryption.lastCall.calledWith('psk2'));
test.done();
})
.catch(function(error) {
Expand Down

0 comments on commit f6106f7

Please sign in to comment.