Skip to content

Commit

Permalink
feat(wifi): wifi does not require password, allows security setting
Browse files Browse the repository at this point in the history
  • Loading branch information
HipsterBrown committed Dec 4, 2015
1 parent 492a144 commit 18f9001
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 100 deletions.
6 changes: 5 additions & 1 deletion bin/tessel-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ makeCommand('wifi')
opts.on = false;
}
callControllerWith('setWiFiState', opts);
} else if (opts.ssid && opts.password) {
} else if (opts.ssid) {
callControllerWith('connectToNetwork', opts);
} else {
callControllerWith('getWifiInfo', opts);
Expand All @@ -232,6 +232,10 @@ makeCommand('wifi')
metavar: 'PASSWORD',
help: 'Set the password of the network to connect to'
})
.option('security', {
abbr: 's',
help: 'Set the encryption of the network to connect to (i.e. wep, psk, psk2, wpa, wpa2).'
})
.option('off', {
flag: true,
help: 'Disable the wireless network'
Expand Down
27 changes: 24 additions & 3 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,30 @@ controller.getWifiInfo = function(opts) {

controller.connectToNetwork = function(opts) {
opts.authorized = true;
return controller.standardTesselCommand(opts, function(tessel) {
return tessel.connectToNetwork(opts);
});
var ssid = opts.ssid;
var password = opts.password;
var security = opts.security;
var securityOptions = ['none', 'wep', 'psk', 'psk2', 'wpa', 'wpa2'];

return new Promise(function(resolve, reject) {
if (!ssid) {
return reject('Invalid credentials: must set SSID with the -n or --ssid option.');
}

if (security && !password) {
return reject('Invalid credentials: must set a password with the -p or --password option.');
}

if (security && securityOptions.indexOf(security) < 0) {
return reject(`"${security}" is not a valid security option. Please choose on of the following: ${securityOptions.join(', ')}`);
}
resolve();
})
.then(function() {
return controller.standardTesselCommand(opts, function(tessel) {
return tessel.connectToNetwork(opts);
});
});
};

controller.setWiFiState = function(opts) {
Expand Down
186 changes: 90 additions & 96 deletions lib/tessel/wifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,20 @@ Tessel.prototype.findAvailableNetworks = function() {
};

Tessel.prototype.getWifiInfo = function() {
var self = this;
return self.simpleExec(commands.getWifiInfo())
.then(function fetchedWiFiInfo(resultsJSON) {
return this.simpleExec(commands.getWifiInfo())
.then((resultsJSON) => {
try {
var network = JSON.parse(resultsJSON);

if (network.ssid === undefined) {
var msg = self.name + ' is not connected to Wi-Fi (run "tessel wifi -l" to see available networks)';
var msg = this.name + ' is not connected to Wi-Fi (run "tessel wifi -l" to see available networks)';
return Promise.reject(msg);
}
} catch (err) {
return Promise.reject(err);
}

return self.simpleExec(commands.getIPAddress())
return this.simpleExec(commands.getIPAddress())
.then(function(ipResults) {
network.ips = ipResults.split('\n');
return Promise.resolve(network);
Expand All @@ -71,106 +70,101 @@ function compareBySignal(a, b) {
}

Tessel.prototype.connectToNetwork = function(opts) {
var self = this;
var ssid = opts.ssid;
var password = opts.password;
var security = opts.security;
var status = 'Wifi connection successful.';

return new Promise(function(resolve, reject) {
if (!ssid || !password) {
return reject(new Error('Invalid credentials. Must set ssid and password'));
}
if (password && !security) {
security = 'psk2';
}

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

// Set the network SSID
return self.simpleExec(commands.setNetworkSSID(ssid))
.then(function() {
// Then set the password
return self.simpleExec(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.simpleExec(commands.turnOnWifi(true));
})
.then(function() {
// Then set the new credientials
return self.connection.exec(commands.commitWirelessCredentials());
})
.then(function(remoteProcess) {
// Once the credentials have been comitted
remoteProcess.once('close', function() {
// Restart the wifi
return self.simpleExec(commands.reconnectWifi())
.then(function() {
// Once the wifi restart process closes
logs.info('Credentials set. Checking connection...');
self.connection.exec(commands.ubusListen())
.then(function(remoteProcess) {
var result = {};
var timeout = setTimeout(function() {
result = {
type: 'reject',
message: 'Timed out waiting to verify connection. Run `t2 wifi` to manually verify connection. If not connected, ensure you have entered the correct network credentials.'
};
// End the connection
remoteProcess.close();
}, 10000);
remoteProcess.stdout.on('data', function(data) {
if (data.indexOf('ifup') > -1) {
logs.info('Successfully connected!');
clearTimeout(timeout);
result = {
type: 'resolve',
message: '' // resolve doesn't report messages
};
remoteProcess.close();
}
});
remoteProcess.stderr.on('data', function(error) {
clearTimeout(timeout);
result = {
type: 'reject',
message: error
};
remoteProcess.close();
});
remoteProcess.on('close', function() {
if (result.type === 'reject') {
reject(result.message);
} else {
resolve();
}
});
});
});
});
});
});
var setSSID = () => this.simpleExec(commands.setNetworkSSID(ssid));

var setNetworkPassword = () => this.simpleExec(commands.setNetworkPassword(password));

var setNetworkSecurity = () => this.connection.exec(commands.setNetworkEncryption(security));

var turnWifiOn = () => this.setWiFiState(true);

var logStatus = () => logs.info(status);

var setup = () => {
if (password) {
return setSSID()
.then(setNetworkPassword)
.then(setNetworkSecurity);
} else {
return setSSID()
.then(setNetworkSecurity);
}
};

return setup()
.then(turnWifiOn)
.then(logStatus);
};

Tessel.prototype.resetMDNS = function() {
var self = this;

return self.simpleExec(commands.callMDNSDaemon('restart'))
.then(function restarted() {
return self.simpleExec(commands.callTesselMDNS('restart'));
});
return this.simpleExec(commands.callMDNSDaemon('restart'))
.then(() => this.simpleExec(commands.callTesselMDNS('restart')));
};

Tessel.prototype.setWiFiState = function(enable) {
var self = this;
return self.simpleExec(commands.turnOnWifi(enable))
.then(function stateSet() {
return self.simpleExec(commands.commitWirelessCredentials());
})
.then(function committed() {
return self.simpleExec(commands.reconnectWifi());
})
.then(function log() {
logs.info('Wifi', enable ? 'Enabled.' : 'Disabled.');
});
return new Promise((resolve, reject) => {
return this.simpleExec(commands.turnOnWifi(enable))
.then(() => this.simpleExec(commands.commitWirelessCredentials()))
.then(() => this.simpleExec(commands.reconnectWifi()))
.then(() => this.connection.exec(commands.ubusListen()))
.then((remoteProcess) => {
if (enable) {
var result = {};
var timeout = setTimeout(function() {
result = {
type: 'reject',
message: 'Timed out waiting to verify connection. Run `t2 wifi` to manually verify connection. If not connected, ensure you have entered the correct network credentials.'
};
// End the connection
remoteProcess.close();
}, 10000);
remoteProcess.stdout.on('data', function(data) {
if (data.indexOf('ifup') > -1) {
logs.info('Successfully connected!');
clearTimeout(timeout);
result = {
type: 'resolve',
message: '' // resolve doesn't report messages
};
remoteProcess.close();
}
});
remoteProcess.stderr.on('data', function(error) {
clearTimeout(timeout);
result = {
type: 'reject',
message: error
};
remoteProcess.close();
});
remoteProcess.on('close', function() {
if (result.type === 'reject') {
reject(result.message);
} else {
resolve();
}
});
} else {
return this.receive(remoteProcess)
.then(resolve);
}
})
.then(() => logs.info('Wifi', enable ? 'Enabled.' : 'Disabled.'));
});
};

0 comments on commit 18f9001

Please sign in to comment.