Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
[CB-4342] Detect USB connected device
- Use 'os' module find IP address of USB connected device
- Add new target to blackberry10.json as Model-PIN
  • Loading branch information
bryanhiggins committed Aug 12, 2013
1 parent 0dbdf30 commit 9a766e00ac21905f325eed0746547efa7ade21cd
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 8 deletions.
@@ -97,16 +97,44 @@ function execNativeDeploy(optionsArray, callback) {
}

function setTarget(callback) {
var props = utils.getProperties(),
targetName;


target = program.args[0] ? program.args[0] : targets.defaultTarget;
if (program["device"]) {
targetUtils.getTargetList("device", true, function (targets) {
if (targets && targets.length > 0) {
target = targets[0].name;
callback();
} else {
console.error("No connected device found");
console.error("Devices must first be configured using platforms/blackberry/cordova/target");
process.exit(1);
targetUtils.findConnectedDevice(function (ip) {
if (!ip) {
console.error("No connected device found");
process.exit(1);
} else {
targetUtils.getDeviceInfo(ip, program["password"], function (device) {
if (device.name) {
targetName = device.name + "-" + device.pin;
props.targets[targetName] = {
ip: ip,
pin: device.pin,
type: "device",
password: program["password"]
};
utils.writeToPropertiesFile(props);
target = targetName;
callback();
} else {
console.error("Unable to authenticate with device at " + ip);
if (!program["password"]) {
console.error("Please provide device password using --password");
}
process.exit(1);
}
});
}
});
}
});
} else if (program["emulator"]) {
@@ -286,10 +314,11 @@ function postBuild() {

function exec() {
program
.usage('[--device] [--emulator] [--target=<id>] [-k | --keystorepass] [--no-launch] [--no-uninstall] [--no-build]')
.usage('[--device] [--emulator] [--password] [--target=<id>] [-k | --keystorepass] [--no-launch] [--no-uninstall] [--no-build]')
.option('-k, --keystorepass <password>', 'the password of signing key; needed for creating debug token')
.option('--device', 'run on connected device')
.option('--emulator', 'run on BB10 simulator')
.option('--password <password>', 'device password')
.option('--target', 'specifies the target to run the application')
.option('--no-uninstall', 'does not uninstall application from device')
.option('--no-launch', 'do not launch the application on device')
@@ -15,6 +15,7 @@
*/

var _self,
os = require("os"),
exec = require('child_process').exec,
path = require('path'),
bb10_utils = require('./utils'),
@@ -42,7 +43,9 @@ _self = {
if (count === Object.keys(targets).length) {
callback(targList);
}
};
},
t;

if (targets) {
for (t in targets) {
if (targets.hasOwnProperty(t) && targets[t].type === type) {
@@ -61,9 +64,76 @@ _self = {
complete();
},

getDeviceInfo: function(ip, password, callback) {
var cmd = path.join(process.env.CORDOVA_BBTOOLS, 'blackberry-deploy') + ' -listDeviceInfo ' + ip;
if (password) {
cmd += ' -password ' + password;
}
exec(cmd, function(error, stdout, stderr) {
var result = {},
name = /modelname::(.*?)\n/.exec(stdout),
pin = /devicepin::0x(.*?)\n/.exec(stdout);
if (name && name.length > 0) {
result.name = name[1];
}
if (pin && pin.length > 0) {
result.pin = pin[1];
}
callback(result);
});
},

findConnectedDevice: function(callback) {
var defaultIp = '169.254.0.1';
_self.discoverUsb(function (result) {
if (result) {
_self.checkConnection(result, 'device', function (connection) {
if (connection) {
callback(result);
} else {
callback();
}
});
} else {
_self.checkConnection(defaultIp, 'device', function (connection) {
if (connection) {
callback(defaultIp);
} else {
callback();
}
});
}
});
},

discoverUsb: function(callback) {
var IPV4_TYPE = "IPv4",
IP_SPLIT_REGEXP = /(169\.254\.\d{1,3}\.)(\d{1,3})/,
networkInterfaces = os.networkInterfaces(),
result,
ni,
i;

for (ni in networkInterfaces) {
if (networkInterfaces.hasOwnProperty(ni)) {
for (i=0; i< networkInterfaces[ni].length; i++) {
if (networkInterfaces[ni][i].family === IPV4_TYPE) {
result = IP_SPLIT_REGEXP.exec(networkInterfaces[ni][i].address);
if (result && result[1] && result[2]) {
callback(result[1] + (result[2] -1));
return;
}
}
}

}
}
//If we haven't found anything callback in defeat
callback();
},

checkConnection: function(ip, type, callback) {
var script = bb10_utils.inQuotes(path.join(process.env.CORDOVA_BBTOOLS, 'blackberry-deploy'));

exec(script + ' -test ' + ip, function(error, stdout, stderr) {
// error code 3 corresponds to a connected device, null corresponds to connected sim
callback((type === 'simulator' && error === null) || (type == 'device' && error.code === 3));
@@ -72,8 +142,8 @@ _self = {

listTargets : function(type, pruneDisconnected) {
_self.getTargetList(type, pruneDisconnected, function (targets) {
for (t in targets) {
console.log(targets[t].name + ' ip: ' + targets[t].ip + (pruneDisconnected ? ' status: connected' : ''));
for (var t in targets) {
console.log(targets[t].name + ' ip: ' + targets[t].ip);
}
});
}

0 comments on commit 9a766e0

Please sign in to comment.