From 46fbf3e791d93783e6149d715a2906c481b51c26 Mon Sep 17 00:00:00 2001 From: Rene Bartkowiak Date: Sun, 6 Jan 2019 16:04:30 +0100 Subject: [PATCH 1/2] Implemented 3G USB dongle setup CLI command --- bin/tessel-2.js | 40 ++++++++++++++ lib/controller.js | 11 ++++ lib/tessel/setup-3g.js | 119 +++++++++++++++++++++++++++++++++++++++++ lib/tessel/tessel.js | 1 + 4 files changed, 171 insertions(+) create mode 100644 lib/tessel/setup-3g.js diff --git a/bin/tessel-2.js b/bin/tessel-2.js index 15ba998d..2d0704d2 100755 --- a/bin/tessel-2.js +++ b/bin/tessel-2.js @@ -203,6 +203,46 @@ makeCommand('reboot') }) .help('Reboot your Tessel'); +makeCommand('3g') + .callback(options => { + log.level(options.loglevel); + + callControllerWith('setup3g', options); + }) + .option('apn', { + default: 'internet', + help: 'Access point name of your mobile provider' + }) + .option('dialnumber', { + default: '*99#', + help: 'Dialnumber provided by your provider, e.g. *99#' + }) + .option('username', { + default: '', + help: 'Username provided by your provider' + }) + .option('password', { + default: '', + help: 'Password provided by your provider' + }) + .option('config', { + flag, + help: 'Change 3G config with parameters from above' + }) + .option('on', { + flag, + help: 'Enable 3G connection' + }) + .option('off', { + flag, + help: 'Disable 3G connection' + }) + .option('status', { + flag, + help: 'Show 3G connection status information' + }) + .help('3G USB dongle setup'); + makeCommand('run') .callback(options => { log.level(options.loglevel); diff --git a/lib/controller.js b/lib/controller.js index 807b1c05..4b6adefe 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -1057,6 +1057,17 @@ controller.reboot = opts => { }); }; +controller.setup3g = opts => { + opts.authorized = true; + return controller.standardTesselCommand(opts, tessel => { + return new Promise((resolve, reject) => { + tessel.setup3g(opts).then(() => { + resolve(); + }).catch(error => reject(error)); + }); + }); +}; + controller.createNewProject = init.createNewProject; controller.crashReporter = function(options) { diff --git a/lib/tessel/setup-3g.js b/lib/tessel/setup-3g.js new file mode 100644 index 00000000..bf948652 --- /dev/null +++ b/lib/tessel/setup-3g.js @@ -0,0 +1,119 @@ +var Tessel = require('./tessel'); +var log = require('../log'); + +Tessel.prototype.setup3g = function(options) { + + var apn = options.apn; + var dialnumber = options.dialnumber; + var username = options.username; + var password = options.password; + var config = options.config; + var on = options.on; + var off = options.off; + var status = options.status; + + if (config) { + return this.simpleExec(['uci', 'get', 'network.wan']).then(() => { + // If dongle already exists remove it and store new configuration + return this.simpleExec(['uci', 'delete', 'network.wan']).then(() => { + return this.configure3gDongle(apn, dialnumber, username, password); + }) + }).catch(() => { + return this.configure3gDongle(apn. dialnumber, username, password); + }); + } + + if (on) { + return this.enable3gConnection(); + } + + if (off) { + return this.disable3gConnection(); + } + + if (status) { + return this.show3gConnectionStatus(); + } + +}; + +Tessel.prototype.configure3gDongle = function(apn, dialnumber, username, password) { + return this.add3gDongle() + .then(() => this.set3gDongleName()) + .then(() => this.set3gDongleIfname()) + .then(() => this.set3gDongleDevice()) + .then(() => this.set3gDongleApn(apn)) + .then(() => this.set3gDongleService()) + .then(() => this.set3gDongleProto()) + .then(() => this.set3gDongleDialnumber(dialnumber)) + .then(() => this.set3gDongleUsername(username)) + .then(() => this.set3gDonglePassword(password)) + .then(() => this.storeNew3gDongle()) + .then(() => this.enable3gConnection()) + .then(() => this.print3gSetupMessage()); +} + +Tessel.prototype.add3gDongle = function() { + return this.simpleExec(['uci', 'add', 'network', 'interface']); + } + +Tessel.prototype.set3gDongleName = function() { + return this.simpleExec(['uci', 'rename', 'network.@interface[-1]=wan']); +} + +Tessel.prototype.set3gDongleIfname = function() { + return this.simpleExec(['uci', 'set', 'network.@interface[-1].ifname=ppp0']); +} + +Tessel.prototype.set3gDongleDevice = function() { + return this.simpleExec(['uci', 'set', 'network.@interface[-1].device=/dev/ttyUSB0']); +} + +Tessel.prototype.set3gDongleApn = function(apn) { + return this.simpleExec(['uci', 'set', 'network.@interface[-1].apn=' + apn]); +} + +Tessel.prototype.set3gDongleService = function() { + return this.simpleExec(['uci', 'set', 'network.@interface[-1].service=umts']); +} + +Tessel.prototype.set3gDongleProto = function() { + return this.simpleExec(['uci', 'set', 'network.@interface[-1].proto=3g']); +} + +Tessel.prototype.set3gDongleDialnumber = function(dialnumber) { + return this.simpleExec(['uci', 'set', 'network.@interface[-1].dialnumber=' + dialnumber]); +} + +Tessel.prototype.set3gDongleUsername = function(username) { + return this.simpleExec(['uci', 'set', 'network.@interface[-1].username=' + username]); +} + +Tessel.prototype.set3gDonglePassword = function(password) { + return this.simpleExec(['uci', 'set', 'network.@interface[-1].password=' + password]); +} + +Tessel.prototype.storeNew3gDongle = function() { + return this.simpleExec(['uci', 'commit']); +} + +Tessel.prototype.enable3gConnection = function() { + return this.simpleExec(['ifup', 'wan']); +} + +Tessel.prototype.disable3gConnection = function() { + return this.simpleExec(['ifdown', 'wan']); +} + +Tessel.prototype.show3gConnectionStatus = function() { + return this.simpleExec(['ifstatus', 'wan']).then(data => { + log.info(data); + }).catch(() => { + log.info('Connection data not available'); + }); +} + +Tessel.prototype.print3gSetupMessage = function() { + log.info('3G USB dongle has been configured successfully.'); + log.info('It should connect automatically to the internet.'); +} \ No newline at end of file diff --git a/lib/tessel/tessel.js b/lib/tessel/tessel.js index c98c4fc4..2504c460 100644 --- a/lib/tessel/tessel.js +++ b/lib/tessel/tessel.js @@ -150,3 +150,4 @@ require('./version'); require('./wifi'); require('./restore'); require('./reboot'); +require('./setup-3g'); From 90428e66922fa125dcd2f3e3bfc44f89921b1169 Mon Sep 17 00:00:00 2001 From: Rene Bartkowiak Date: Sun, 6 Jan 2019 16:17:04 +0100 Subject: [PATCH 2/2] Fixed eslint issues --- lib/tessel/setup-3g.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/tessel/setup-3g.js b/lib/tessel/setup-3g.js index bf948652..eb4e1768 100644 --- a/lib/tessel/setup-3g.js +++ b/lib/tessel/setup-3g.js @@ -17,7 +17,7 @@ Tessel.prototype.setup3g = function(options) { // If dongle already exists remove it and store new configuration return this.simpleExec(['uci', 'delete', 'network.wan']).then(() => { return this.configure3gDongle(apn, dialnumber, username, password); - }) + }); }).catch(() => { return this.configure3gDongle(apn. dialnumber, username, password); }); @@ -51,59 +51,59 @@ Tessel.prototype.configure3gDongle = function(apn, dialnumber, username, passwor .then(() => this.storeNew3gDongle()) .then(() => this.enable3gConnection()) .then(() => this.print3gSetupMessage()); -} +}; Tessel.prototype.add3gDongle = function() { return this.simpleExec(['uci', 'add', 'network', 'interface']); - } +}; Tessel.prototype.set3gDongleName = function() { return this.simpleExec(['uci', 'rename', 'network.@interface[-1]=wan']); -} +}; Tessel.prototype.set3gDongleIfname = function() { return this.simpleExec(['uci', 'set', 'network.@interface[-1].ifname=ppp0']); -} +}; Tessel.prototype.set3gDongleDevice = function() { return this.simpleExec(['uci', 'set', 'network.@interface[-1].device=/dev/ttyUSB0']); -} +}; Tessel.prototype.set3gDongleApn = function(apn) { return this.simpleExec(['uci', 'set', 'network.@interface[-1].apn=' + apn]); -} +}; Tessel.prototype.set3gDongleService = function() { return this.simpleExec(['uci', 'set', 'network.@interface[-1].service=umts']); -} +}; Tessel.prototype.set3gDongleProto = function() { return this.simpleExec(['uci', 'set', 'network.@interface[-1].proto=3g']); -} +}; Tessel.prototype.set3gDongleDialnumber = function(dialnumber) { return this.simpleExec(['uci', 'set', 'network.@interface[-1].dialnumber=' + dialnumber]); -} +}; Tessel.prototype.set3gDongleUsername = function(username) { return this.simpleExec(['uci', 'set', 'network.@interface[-1].username=' + username]); -} +}; Tessel.prototype.set3gDonglePassword = function(password) { return this.simpleExec(['uci', 'set', 'network.@interface[-1].password=' + password]); -} +}; Tessel.prototype.storeNew3gDongle = function() { return this.simpleExec(['uci', 'commit']); -} +}; Tessel.prototype.enable3gConnection = function() { return this.simpleExec(['ifup', 'wan']); -} +}; Tessel.prototype.disable3gConnection = function() { return this.simpleExec(['ifdown', 'wan']); -} +}; Tessel.prototype.show3gConnectionStatus = function() { return this.simpleExec(['ifstatus', 'wan']).then(data => { @@ -111,9 +111,9 @@ Tessel.prototype.show3gConnectionStatus = function() { }).catch(() => { log.info('Connection data not available'); }); -} +}; Tessel.prototype.print3gSetupMessage = function() { log.info('3G USB dongle has been configured successfully.'); log.info('It should connect automatically to the internet.'); -} \ No newline at end of file +}; \ No newline at end of file