Skip to content

Commit

Permalink
First version working with adb.js
Browse files Browse the repository at this point in the history
  • Loading branch information
arcturus committed Jun 5, 2013
1 parent 6b1ab23 commit 415aaf8
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 1 deletion.
110 changes: 110 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
var remote = require('./remote_debugger'),
ADB = require('adb').DebugBridge;

var FFOS_Cli = function FFOS_Cli() {

var config;
var adb = new ADB();

var configure = function configure(json) {
config = json;
};

// Start displaying the logcat for the first device we find
var logcat = function logcat() {
adb.traceDevice(function onDevices(devices) {
if (!devices || devices.length == 0) {
return;
}
devices[0].logcat();
});
};

// Takes a screenshot from device if any, pass a file name
// and a callback to know when we finished.
// The callback expected 1 parameter, in case an error
// happened
var screenshot = function screenshot(fileName, callback) {
adb.traceDevice(function onDevices(devices) {
if (!devices || devices.length == 0) {
callback('No devices');
}
var device = devices[0];
try {
device.takeSnapshot(function onSnapshot(frame) {
frame.writeImageFile(fileName);
callback(null);
});
} catch (e) {
callback(e);
}
});
};

/*
For installing an app just follow the steps:
1.- Forward the remote debugger port (use config if present)
2.- Upload the selected zip file to the app id
3.- Use the remote client to tell the system to install the app
*/
var installApp = function installApp (appId, localZip, appType, callback) {
var localPort = remotePort = 'tcp:6000';
if (config && config.localPort && config.remotePort) {
localPort = config.localPort;
remotePort = config.remotePort;
}

adb.forward(localPort, remotePort, function onForward() {
//Build the remote url with the appId
var remoteFile = '/data/local/tmp/b2g/' + appId + '/application.zip';
pushFile(localZip, remoteFile, function onPushed(err, success) {
if (err) {
callback(err);
return;
}

installRemote(remotePort, appId, appType, callback);
});
});

};

// Uses the remote protocol to tell the system to install an app
// previously uploaded
var installRemote = function installRemote(remotePort, appId, appType, cb) {
remote.init(remotePort.split(':')[1]);
remote.installApp(appId, appType, function onInstall(err, data) {
if (err) {
cb(err);
return;
}
cb(null, data);
});
};

// Push a local file to a remote location on the phone
var pushFile = function pushFile(local, remote, callback) {
adb.traceDevice(function onDevices(devices) {
// Work with the first device we found, if any
if (!devices || devices.length == 0) {
callback('No devices found');
return;
}
var device = devices[0];

device.getSyncService(function onSyncService(sync) {
sync.pushFile(local, remote, callback);
});
});
};

return {
'config': config,
'logcat': logcat,
'screenshot': screenshot,
'installApp': installApp
};

}();

module.exports = FFOS_Cli;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"url": "http://github.com/arcturus/node_ffos_cli.git"
},
"dependencies": {
"adb": "git://github.com/arcturus/adb.js.git#master"
"adb.js": "git://github.com/arcturus/adb.js.git#master"
}
}
82 changes: 82 additions & 0 deletions remote_debugger/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var net = require('net');

var RemoteDebugger = function RemoteDebugger() {

var port = -1;

var RemoteOperation = function RemoteOperation(actorName, message,
onResponse, onError) {
if (port == -1) {
throw new Exception('No port setup');
}
var client = net.connect({'port': port}, (function connected() {
// When connected ask for the list tab
var msg = '{"to": "root", "type": "listTabs"}';
client.write(msg.length + ':' + msg);
}).bind(this));
client.on('data', (function onData(data) {
// Get the json object out of the protocol data
data = data.toString();
var currentObject = JSON.parse(
data.substr(data.indexOf(':') + 1));

console.log(JSON.stringify(currentObject));

if (!this.tabList && 'tabs' in currentObject) {
// Wait till one data object contains the
// tabs information, and extract the selected
// actor from it
var tabList = currentObject;
if (!actorName in tabList) {
onError('Could not find actor ' + actorName);
client.end();
return;
}

// Flag to know if we have tab list and
// send the message to the proper actor
this.tabList = tabList;
this.actor = tabList[actorName];

message.to = tabList[actorName];
var msg = JSON.stringify(message);
client.write(msg.length + ':' + msg);
} else if (!('error' in currentObject) &&
this.actor == currentObject.from) {
// If this response is coming from the actor that
// we were invoquin previously
onResponse(currentObject);
client.end();
} else if ('error' in currentObject) {
onError(currentObject);
}
}).bind(client));
client.on('error', onError);

return client;
};

var init = function init(p) {
port = p;
};

var installApp = function installApp(name, appType, callback) {
var message = {
type: 'install',
appId: name,
appType: appType
};
var op = RemoteOperation('webappsActor', message, function onResponse(data) {
callback(null, data);
}, function onError(e) {
callback(e);
});
};

return {
'init': init,
'installApp': installApp
};
}();

module.exports = RemoteDebugger;

0 comments on commit 415aaf8

Please sign in to comment.