Skip to content

Commit

Permalink
feat(ap): initial access point creation implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
HipsterBrown committed Nov 16, 2015
1 parent e7299f0 commit 8ccd1a0
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bin/tessel-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,28 @@ makeCommand('version')
.callback(callControllerCallback('tesselFirmwareVerion'))
.help('Display Tessel\'s current firmware version');

makeCommand('ap')
.option('ssid', {
abbr: 'n',
help: 'Name of the network.'
})
.option('pass', {
abbr: 'p',
help: 'Password to access network.'
})
.option('security', {
abbr: 's',
help: 'Encryption to use on network (i.e. WEP, WPA, PSK).'
})
.option('trigger', {
position: 0,
help: 'Trigger, i.e. on OR off, the access point'
})
.help('Configure the Tessel as an access point')
.callback(function(opts) {
callControllerWith('createAccessPoint', opts);
});


module.exports = function(args) {
parser.parse(args);
Expand Down
7 changes: 7 additions & 0 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,13 @@ controller.connectToNetwork = function(opts) {
});
};

controller.createAccessPoint = function(opts) {
opts.authorized = true;
return controller.standardTesselCommand(opts, function(tessel) {
return tessel.createAccessPoint(opts);
});
};

controller.printAvailableUpdates = function() {
return updates.requestBuildList().then(function(builds) {
logs.info('Latest builds:');
Expand Down
65 changes: 65 additions & 0 deletions lib/tessel/access-point.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var Tessel = require('./tessel'),
commands = require('./commands'),
logs = require('../logs');

function commitAndClose(resolve) {
var self = Tessel;

return self.connection.exec(commands.commitWirelessCredentials())
.then(function(remoteProcess) {
remoteProcess.once('close', function() {
return self.connection.exec(commands.reconnectWifi())
.then(function(remoteProcess) {
return self.receive(remoteProcess).then(function() {
logs.info('Access Point credentials set!');

return self.connection.end()
.then(resolve);
});
});
});
});
}

Tessel.prototype.createAccessPoint = function(opts) {
var self = this;
var ssid = opts.ssid;
var password = opts.password;
var security = opts.security;

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

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

return self.connection.exec(commands.setAccessPointSSID(ssid))
.then(function() {
if (password) {
return self.connection.exec(commands.setAccessPointPassword(password));
} else {
return commitAndClose(resolve);
}
})
.then(function() {
if (security) {
return self.connection.exec(commands.setAccessPointSecurity(security));
} else {
return commitAndClose(resolve);
}
})
.then(function() {
return self.connection.exec(commands.turnAccessPointOn(true))
.then(commitAndClose.bind(null, resolve));
});
});
};
12 changes: 12 additions & 0 deletions lib/tessel/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,15 @@ module.exports.sysupgrade = function(path) {
module.exports.getMemoryInfo = function() {
return ['cat', '/proc/meminfo'];
};
module.exports.setAccessPointSSID = function(ssid) {
return ['uci', 'set', 'wireless.@wifi-iface[1].ssid=' + ssid];
};
module.exports.setAccessPointPassword = function(password) {
return ['uci', 'set', 'wireless.@wifi-iface[1].key=' + password];
};
module.exports.setAccessPointSecurity = function(security) {
return ['uci', 'set', 'wireless.@wifi-iface[1].encryption=' + security];
};
module.exports.turnAccessPointOn = function(enabled) {
return ['uci', 'set', 'wireless.@wifi-iface[1].disabled=' + Number(enabled ? 0 : 1).toString()];
};

0 comments on commit 8ccd1a0

Please sign in to comment.