forked from mozilla/adbhelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adb-client.js
89 lines (73 loc) · 2.63 KB
/
adb-client.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* A module to track device changes
* Mostly from original `adb.js`
*/
'use strict';
const { Cu, Cc, Ci } = require("chrome");
const { AdbSocket } = require("./adb-socket");
Cu.import("resource://gre/modules/Services.jsm");
let { TextEncoder, TextDecoder } = Cu.import("resource://gre/modules/Services.jsm");
const OLD_SOCKET_API =
Services.vc.compare(Services.appinfo.platformVersion, "23.0a1") < 0;
let _sockets = [ ];
// @param aPacket The packet to get the length from.
// @param aIgnoreResponse True if this packet has no OKAY/FAIL.
// @return A js object { length:...; data:... }
function unpackPacket(aPacket, aIgnoreResponse) {
let buffer = OLD_SOCKET_API ? aPacket.buffer : aPacket;
console.debug("Len buffer: " + buffer.byteLength);
if (buffer.byteLength === 4 && !aIgnoreResponse) {
console.debug("Packet empty");
return { length: 0, data: "" };
}
let lengthView = new Uint8Array(buffer, aIgnoreResponse ? 0 : 4, 4);
let decoder = new TextDecoder();
let length = parseInt(decoder.decode(lengthView), 16);
let text = new Uint8Array(buffer, aIgnoreResponse ? 4 : 8, length);
return { length: length, data: decoder.decode(text) };
}
// Checks if the response is OKAY or FAIL.
// @return true for OKAY, false for FAIL.
function checkResponse(aPacket) {
const OKAY = 0x59414b4f; // OKAY
const FAIL = 0x4c494146; // FAIL
let buffer = OLD_SOCKET_API ? aPacket.buffer : aPacket;
let view = new Uint32Array(buffer, 0 , 1);
if (view[0] == FAIL) {
console.debug("Response: FAIL");
}
console.debug("view[0] = " + view[0]);
return view[0] == OKAY;
}
// @param aCommand A protocol-level command as described in
// http://androidxref.com/4.0.4/xref/system/core/adb/OVERVIEW.TXT and
// http://androidxref.com/4.0.4/xref/system/core/adb/SERVICES.TXT
// @return A 8 bit typed array.
function createRequest(aCommand) {
let length = aCommand.length.toString(16).toUpperCase();
while(length.length < 4) {
length = "0" + length;
}
let encoder = new TextEncoder();
console.debug("Created request: " + length + aCommand);
return encoder.encode(length + aCommand);
}
function close() {
_sockets.forEach(function(s) s.close());
}
function connect() {
let tmp = new AdbSocket();
_sockets.push(tmp);
return tmp;
}
let client = {
unpackPacket: unpackPacket,
checkResponse: checkResponse,
createRequest: createRequest,
connect: connect,
close: close
};
module.exports = client;