forked from ferjm/b2g-js-ril
/
adb.js
335 lines (288 loc) · 8.58 KB
/
adb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/Services.jsm");
const gTransportService = Cc["@mozilla.org/network/socket-transport-service;1"]
.getService(Ci.nsISocketTransportService);
const BinaryInputStream = Components.Constructor(
"@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream",
"setInputStream");
const BinaryOutputStream = Components.Constructor(
"@mozilla.org/binaryoutputstream;1",
"nsIBinaryOutputStream",
"setOutputStream");
const ADB_DEFAULT_HOST = "127.0.0.1";
const ADB_DEFAULT_PORT = "5037";
const ADB_REQUEST_LENGTH_LEN = 4;
const ADB_RESPONSE_STATUS_LEN = 4;
const ADB_PACKET_LENGTH_LEN = 4;
const ADB_STATUS_OKAY = "OKAY";
const ADB_STATUS_FAIL = "FAIL";
function ADBRequest(host, port) {
this.host = host;
this.port = port;
}
ADBRequest.prototype = {
host: null,
port: null,
socket: null,
inputStream: null,
binaryInputStream: null,
outputStream: null,
binaryOUtputStream: null,
connect: function connect() {
this.socket = gTransportService.createTransport(null, 0, this.host,
this.port, null);
this.inputStream = this.socket.openInputStream(0, 0, 0);
this.binaryInputStream = BinaryInputStream(this.inputStream);
this.outputStream = this.socket.openOutputStream(0, 0, 0);
this.binaryOutputStream = BinaryOutputStream(this.outputStream);
this.inputStream.asyncWait(this, 0, 0, Services.tm.currentThread);
this.connected = true;
debug("Connected to " + this.host + ":" + this.port);
},
disconnect: function disconnect() {
this.connected = false;
this.socket.close(0);
this.socket = null;
this.inputStream = null;
this.binaryInputStream = null;
this.outputStream = null;
this.binaryOutputStream = null;
},
transmit: function transmit(data) {
debug("Transmitting " + data);
this.binaryOutputStream.writeBytes(data, data.length);
this.binaryOutputStream.flush();
},
/**
* nsIInputStreamCallback
*/
onInputStreamReady: function onInputStreamReady() {
if (!this.connected) {
return;
}
while (true) {
let data;
try {
let length = this.inputStream.available();
if (!length) {
break;
}
data = this.binaryInputStream.readBytes(length);
} catch(ex) {
this.disconnect();
this.handleReadError(ex);
return;
}
this.processData(data);
}
this.inputStream.asyncWait(this, 0, 0, Services.tm.currentThread);
},
handleReadError: function handleReadError(error) {
debug("Error");
debug(error);
//TODO dispatch to either oncomplete or onerror
},
/**
* Incoming read buffer.
*/
buffer: "",
/**
* Transient attributes.
*/
responseStatus: null,
packetLength: null,
responseDispatched: false,
processData: function processData(data) {
debug("Received " + data.length + " bytes: " + JSON.stringify(data));
this.buffer += data;
while (true) {
// If we don't know the response status yet, try to read it from
// the buffer. This is the very first thing that gets sent back
// and only once per connection.
if (this.responseStatus == null) {
if (this.buffer.length < ADB_RESPONSE_STATUS_LEN) {
return;
}
this.responseStatus = this.buffer.slice(0, ADB_RESPONSE_STATUS_LEN);
this.buffer = this.buffer.slice(ADB_RESPONSE_STATUS_LEN);
}
// If we don't know the current packet's length yet, try to read
// it from the buffer. This gets reset to null every time we have
// handled a packet.
if (this.packetLength == null) {
if (this.buffer.length < ADB_PACKET_LENGTH_LEN) {
return;
}
let lengthstr = this.buffer.slice(0, ADB_PACKET_LENGTH_LEN);
this.packetLength = parseInt(lengthstr, 16);
this.buffer = this.buffer.slice(ADB_PACKET_LENGTH_LEN);
}
// Bail if we haven't received the whole packet yet.
if (this.buffer.length < this.packetLength) {
return;
}
let data = this.buffer.slice(0, this.packetLength);
this.buffer = this.buffer.slice(this.packetLength);
try {
if (this.responseDispatched) {
this.onincremental(data);
} else {
this.responseDispatched = true;
this.onresponse(this.responseStatus, data);
}
} catch (ex) {
debug("Exception while calling event handlers:");
debug(ex);
}
this.packetLength = null;
}
},
/**
* Public API
*/
/**
* Indicate whether the socket is still open.
*/
connected: false,
/**
* One of "OKAY", "FAIL", etc.
*/
status: null,
/**
* Send the request.
*
* @param request
* String specifying the request
*/
send: function send(request) {
this.connect();
let length = ("000" + request.length.toString(16))
.slice(-ADB_REQUEST_LENGTH_LEN);
request = length + request;
this.transmit(request);
// If we're calling send() multiple times, we expect another full
// blown response.
this.responseDispatched = false;
},
onresponse: function onresponse(status, data) {
},
onincremental: function onincremental(data) {
},
oncomplete: function oncomplete() {
},
onerror: function onerror(error) {
},
};
function ADBClient(host, port) {
if (!host) {
this.host = ADB_DEFAULT_HOST;
}
if (!port) {
this.port = ADB_DEFAULT_PORT;
}
}
ADBClient.prototype = {
newRequest: function newRequest() {
return new ADBRequest(this.host, this.port);
},
simpleRequest: function simpleRequest(data, callback) {
let request = this.newRequest();
request.onresponse = function onresponse(status, response) {
request.disconnect();
callback(status, response);
};
request.send(data);
return request;
},
parseDeviceData: function parseDeviceData(data) {
let lines = data.split("\n");
let devices = [];
lines.forEach(function (line) {
if (!line) {
return;
}
let data = line.split("\t");
devices.push({serial: data[0],
state: data[1]});
});
return devices;
},
/**
* API
*/
getDevices: function getDevices(callback) {
this.simpleRequest("host:track-devices", function (status, response) {
if (status != ADB_STATUS_OKAY) {
callback(status);
return;
}
let devices = this.parseDeviceData(response);
debug("Devices: " + JSON.stringify(devices));
callback(null, devices);
}.bind(this));
},
trackDevices: function trackDevices(callback) {
let request = this.newRequest();
request.onresponse = function onresponse(status, data) {
if (status != ADB_STATUS_OKAY) {
callback(status);
return;
}
let devices = this.parseDeviceData(data);
callback(null, devices);
};
request.onincremental = function onincremental(data) {
let devices = this.parseDeviceData(data);
callback(null, devices);
};
request.send("host:track-devices");
return request;
},
setDevice: function setDevice(request, serial, callback) {
request.onresponse = function onresponse(status, response) {
debug("setdevice: " + status);
debug(response);
callback();
};
request.onincremental = function onincremental(data) {
debug("incremental setdevice " + data);
};
request.send("host:transport:" + serial);
return request;
},
getEventLog: function getEventLog(request, callback) {
request.onresponse = function onresponse(status, response) {
debug("eventlog: " + status);
debug(response);
};
request.onincremental = function onincremental(data) {
debug("incremental eventlog " + data);
};
request.send("log:event");
return request;
},
logcat: function logcat(request) {
let request = this.newRequest();
request.onresponse = function onresponse(status, data) {
debug("logcat: " + status);
debug(data);
};
request.onincremental = function onincremental(data) {
debug("logcat " + data);
};
request.send("shell:exec logcat");
return request;
},
};
function testlogcat() {
client.getDevices(function (devices) {
let serial = devices[0].serial;
let request = client.newRequest();
client.setDevice(request, serial, function () {
//client.waitForDevice()
client.logcat(request);
});
});
}
//[14:40:56.265] Devices: [{"serial":"304D19678B33447E","state":"device"}] @ chrome://ril/content/adb.html:31