You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by je...@apache.org on 2013/09/27 16:26:18 UTC

[14/50] [abbrv] webworks commit: [CB-4342] Detect USB connected device

[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


Project: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/commit/9a766e00
Tree: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/tree/9a766e00
Diff: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/diff/9a766e00

Branch: refs/heads/3.1.x
Commit: 9a766e00ac21905f325eed0746547efa7ade21cd
Parents: 0dbdf30
Author: Bryan Higgins <br...@bryanhiggins.net>
Authored: Tue Jul 23 12:58:38 2013 -0400
Committer: Bryan Higgins <bh...@blackberry.com>
Committed: Mon Aug 12 11:16:23 2013 -0400

----------------------------------------------------------------------
 .../bin/templates/project/cordova/lib/run       | 37 +++++++++-
 .../project/cordova/lib/target-utils.js         | 78 +++++++++++++++++++-
 2 files changed, 107 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/9a766e00/blackberry10/bin/templates/project/cordova/lib/run
----------------------------------------------------------------------
diff --git a/blackberry10/bin/templates/project/cordova/lib/run b/blackberry10/bin/templates/project/cordova/lib/run
index e47e6c6..e604dd5 100755
--- a/blackberry10/bin/templates/project/cordova/lib/run
+++ b/blackberry10/bin/templates/project/cordova/lib/run
@@ -97,6 +97,10 @@ 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) {
@@ -104,9 +108,33 @@ function setTarget(callback) {
                 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')

http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/9a766e00/blackberry10/bin/templates/project/cordova/lib/target-utils.js
----------------------------------------------------------------------
diff --git a/blackberry10/bin/templates/project/cordova/lib/target-utils.js b/blackberry10/bin/templates/project/cordova/lib/target-utils.js
index f74b4f2..535a104 100644
--- a/blackberry10/bin/templates/project/cordova/lib/target-utils.js
+++ b/blackberry10/bin/templates/project/cordova/lib/target-utils.js
@@ -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);
             }
         });
     }