-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathajax.php
More file actions
207 lines (173 loc) · 4.46 KB
/
ajax.php
File metadata and controls
207 lines (173 loc) · 4.46 KB
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
<?php
include_once('utils.php');
set_time_limit(0);
//TODO: parse the header line and use those columns to find the correct cpu column, as some adb shell top produces 10 columns not 9
function processOutput($device,$out) {
$out = trim($out);
$out = preg_split("/\n/", $out);
$result = array(
"device"=>$device,
"time"=>date("H:i:s")
);
if (trim($out[0]) != "") {
//extract the first line with system info
$sys = $out[0];
$sys = preg_split("/,/", $sys);
foreach ($sys as $value) {
$value = preg_split("/ /", trim($value));
$result[$device."-".$value[0]] = str_replace("%", "", $value[1]);
}
// remove the first few lines
$out = array_slice($out, 4);
// trim and extract process lines
foreach ($out as $value) {
if (trim($value) == "") {
continue;
}
$value = preg_split("/ /", trim($value));
$l = array();
foreach ($value as $k => $v) {
if (trim($v) != "") {
$l[] = $v;
}
}
if (isset($l[8])) {
$result[$device."-".str_replace(array('/','.',' ',':'),'_',$l[8])] = str_replace("%", "", $l[1]);
}
//echo($value[2]." = ".$value[]);
//var_dump($l);
}
} else {
if (count($result) == 0) {
$result[$device."-"."no data"] = 0;
}
}
return $result;
}
function getDevices() {
global $adb;
$out=trim(shell_exec($adb." devices"));
if (strlen($out) > 0) {
$out="egy\nketto\nharom\n".$out;
$out=preg_split("/\n/", $out);
$db=0;
while(count($out) > 0 && strpos($out[0], "List of") === false) {
array_shift($out);
$db++;
if ($db > 10) {
break;
}
}
unset($out[0]);
foreach($out as $key => $value) {
$a=preg_split("/ /",$value);
$out[$key]=$a[0];
}
$out2=array();
foreach($out as $key => $value) {
$fn="/sdcard/deviced.name";
$s=shell_exec($adb." -s ".$value." shell cat ".$fn);
if (strpos($s,$fn) === false) {
$out2[$value] = trim($s);
} else {
$out2[$value] = "";
}
}
return $out2;
} else {
return array();
}
}
function adbCheck() {
$cmdline = "shell top -d 1";
exec("ps aux | grep '$cmdline' | grep -v grep | awk '{ print $2 }' | head -1", $out);
return count($out)>0 ? $out[0] : false;
}
function adbReadData($device) {
global $process_count;
@unlink("stop");
$pid = adbCheck();
if ($pid !== false) {
$block = "";
$cnt = 0;
$firstbreak = false;
$waited = 0;
$f = fopen("/proc/$pid/fd/1", 'r');
while(true) {
$l = stream_get_line($f, 1024, PHP_EOL);
if (($l !== false) && ($firstbreak == false)) {
continue;
}
if (trim($l)) {
// echo("read: ".$l.PHP_EOL);
$block.= $l.PHP_EOL;
$cnt++;
$waited = 0;
}
if ($cnt == $process_count+3) {
$data = processOutput($device, $block);
$json = array("device" => $device, "op" => "data", "data" => $data, "message" => null, "pid" => $pid);
echo(json_encode($json));
$block = "";
$cnt = 0;
break;
}
if ($l === false) {
sleep(1);
$waited++;
$cnt = 0;
$block = "";
$firstbreak = true;
if (file_exists("stop")) {
$json = array("device" => $device, "op" => "ack", "data" => "stop", "message" => "Stop accepted.");
echo(json_encode($json));
return;
}
if ($waited > 5) {
$json = array("device" => $device, "op" => "no_data", "data" => null, "message" => "No data for 5 seconds, stopping.");
echo(json_encode($json));
break;
}
}
}
fclose($f);
} else {
return false;
}
}
function adbStart() {
global $adb;
global $process_count;
exec("$adb shell top -d 1 -m $process_count 1>/tmp/adb.1 2>&1 &");
}
function adbStop() {
$pid = adbCheck();
if ($pid !== false) {
exec("kill ".$pid);
return adbCheck() === false;
} else {
return false;
}
}
if (isset($_GET["start"]) && $_GET["start"]) {
$pid = adbCheck();
if ($pid === false) {
adbStart();
}
} else if (isset($_GET["stop"]) && $_GET["stop"]) {
touch("stop");
adbStop();
header("Content-type: application/json");
echo(json_encode(array("message"=>"Stop accepted for ".$_GET["device"].".","device"=>$_GET["device"])));
} else if (isset($_GET['devices'])) {
$devices=getDevices();
header("Content-type: application/json");
echo(json_encode($devices));
} else if (isset($_GET['op']) && $_GET['op'] = 'data') {
$pid = adbCheck();
if ($pid === false) {
adbStart();
}
header("Content-type: application/json");
adbReadData($_GET["device"]);
}