forked from SmartisanTech/Wrench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adbclient.cpp
613 lines (516 loc) · 14.1 KB
/
adbclient.cpp
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include "adbclient.h"
#include <stdio.h>
#include <QTcpSocket>
#include <QFileInfo>
#include <QDateTime>
#include <QDir>
#include <QProcessEnvironment>
#include <QString>
#include <QRegExp>
AdbClient::AdbClient()
{
static QString adb_serial = QProcessEnvironment::systemEnvironment().value("ANDROID_SERIAL");
static QByteArray adb_serial_buffer = adb_serial.toUtf8();
static bool adb_serial_inited = false;
if (not adb_serial_inited) {
adb_serial_inited = true;
if (! adb_serial.isEmpty()) {
__adb_serial = adb_serial_buffer.data();
}
}
isOK = true;
adbSock.connectToHost("127.0.0.1", 5037, QIODevice::ReadWrite);
adbSock.waitForConnected();
}
void AdbClient::setAdbSerial(const QString& ser)
{
static QByteArray adb_serial_buffer;
adb_serial_buffer = ser.toUtf8();
__adb_serial = adb_serial_buffer.data();
qDebug() << "set adb serial to" << __adb_serial;
AdbClient::doAdbKill();
}
QString AdbClient::getAdbSerial()
{
if (__adb_serial) {
return QString((const char*) __adb_serial);
}
return "";
}
AdbClient::~AdbClient()
{
adbSock.close();
}
volatile const char* __adb_serial = NULL;
bool AdbClient::readx(void* data, qint64 max)
{
int done = 0;
while (max > done) {
int n = adbSock.read((char*)data + done, max - done);
if (n < 0) {
qDebug() << "adb read wait failed";
return false;
} else if (n == 0) {
if (!adbSock.waitForReadyRead(-1)) {
qDebug() << "adb read ended prematurely, done: " << done << "max: " << max;
return false;
}
}
done += n;
}
return true;
}
bool _writex(QIODevice& io, const void* data, qint64 max)
{
int done = 0;
while (max > done) {
int n = io.write((char*)data + done, max - done);
if (n <= 0) {
return false;
}
done += n;
}
return true;
}
bool AdbClient::writex(const void* data, qint64 max)
{
return _writex(adbSock, data, max);
}
void AdbClient::adb_close()
{
adbSock.close();
}
bool AdbClient::adb_status()
{
char buf[5];
unsigned len;
adbSock.flush();
if(!readx(buf, 4)) {
__adb_error = "protocol fault (no status)";
return false;
}
if(!memcmp(buf, "OKAY", 4)) {
return true;
}
if(memcmp(buf, "FAIL", 4)) {
__adb_error.sprintf(
"protocol fault (status %02x %02x %02x %02x?!)",
buf[0], buf[1], buf[2], buf[3]);
return false;
}
if(!readx(buf, 4)) {
__adb_error = "protocol fault (status len)";
return false;
}
buf[4] = 0;
len = strtoul((char*)buf, 0, 16);
if(len > 255) len = 255;
char buf2[256];
if(!readx(buf2, len)) {
__adb_error = "protocol fault (status read)";
return false;
}
buf2[len] = 0;
__adb_error = buf2;
return false;
}
bool AdbClient::switch_socket_transport()
{
char service[64];
char tmp[5];
int len;
if (__adb_serial) {
snprintf(service, sizeof service, "host:transport:%s", __adb_serial);
} else {
snprintf(service, sizeof service, "host:%s", "transport-any");
}
len = strlen(service);
snprintf(tmp, sizeof tmp, "%04x", len);
if(!writex(tmp, 4) || !writex(service, len)) {
__adb_error = "write failure during connection";
return false;
}
return adb_status();
}
bool AdbClient::adb_connect(const char *service)
{
char tmp[5];
int len;
len = strlen(service);
if((len < 1) || (len > 1024)) {
__adb_error = "service name too long";
return false;
}
snprintf(tmp, sizeof tmp, "%04x", len);
if (memcmp(service,"host",4) != 0 && !switch_socket_transport()) {
return false;
}
if(!writex(tmp, 4) || !writex(service, len)) {
__adb_error = "write failure during connection";
adb_close();
return false;
}
return adb_status();
}
AdbClient* AdbClient::doAdbPipe(const QString& cmdLine)
{
return AdbClient::doAdbPipe(QStringList(cmdLine));
}
AdbClient* AdbClient::doAdbPipe(const QStringList& cmdAndArgs)
{
AdbClient *adb = new AdbClient();
QString cmdLine = "shell:";
foreach(const QString& a, cmdAndArgs) {
cmdLine += a + " ";
}
bool res = adb->adb_connect(cmdLine.toUtf8().constData());
if (!res) {
adb->isOK = false;
}
return adb;
}
QString AdbClient::doAdbShell(const QStringList& cmdAndArgs)
{
QStringList res = doAdbShellWithStderr(cmdAndArgs);
if (res.length() >= 1) {
return res[0];
}
return "";
}
QStringList AdbClient::doAdbShellWithStderr(const QStringList& cmdAndArgs)
{
AdbClient *adb = doAdbPipe(cmdAndArgs);
QStringList res;
if (!adb->isOK) {
res << "" << adb->__adb_error;
} else {
QByteArray buf;
while (adb->adbSock.waitForReadyRead(-1)) {
buf += adb->adbSock.readAll();
}
QString ret = QString::fromUtf8(buf);
ret.replace("\r", "");
while (ret.endsWith("\n")) {
ret.chop(1);
}
res << ret;
}
delete adb;
return res;
}
QString AdbClient::doAdbShell(const QString& cmdLine) {
return AdbClient::doAdbShell(QStringList(cmdLine));
}
bool AdbClient::sync_recv(const QString& rpath, const QString& lpath)
{
syncmsg msg;
int len;
QFile lfile(lpath);
char *buffer = send_buffer.data;
unsigned id;
len = rpath.toUtf8().size();
if(len > 1024) return false;
msg.req.id = ID_RECV;
msg.req.namelen = htoll(len);
if(!writex(&msg.req, sizeof(msg.req)) ||
!writex(rpath.toUtf8(), len)) {
return false;
}
if(!readx(&msg.data, sizeof(msg.data))) {
return false;
}
id = msg.data.id;
if((id == ID_DATA) || (id == ID_DONE)) {
lfile.remove();
QDir dir = QFileInfo(lpath).dir();
dir.mkpath(dir.path());
if (!lfile.open(QIODevice::WriteOnly)) {
qDebug() << "Can't open" << lpath;
return false;
}
goto handle_data;
} else {
goto remote_error;
}
for(;;) {
if(!readx(&msg.data, sizeof(msg.data))) {
return false;
}
id = msg.data.id;
handle_data:
len = ltohl(msg.data.size);
if(id == ID_DONE) break;
if(id != ID_DATA) goto remote_error;
if(len > SYNC_DATA_MAX) {
qDebug() << "data overrun\n";
lfile.close();
return false;
}
if(!readx(buffer, len)) {
lfile.close();
return false;
}
if(!_writex(lfile, buffer, len)) {
qDebug() << "cannot write" << lpath;
lfile.close();
return false;
}
}
lfile.close();
return true;
remote_error:
lfile.close();
lfile.remove();
if(id == ID_FAIL) {
len = ltohl(msg.data.size);
if(len > 256) len = 256;
if(!readx(buffer, len)) {
return false;
}
buffer[len] = 0;
} else {
memcpy(buffer, &id, 4);
buffer[4] = 0;
// strcpy(buffer,"unknown reason");
}
qDebug() << "failed to copy" << rpath << "to" << lpath << ":" << buffer;
return false;
}
bool AdbClient::do_sync_pull(const char *rpath, const char *lpath)
{
unsigned mode;
if (!adb_connect("sync:")) {
qDebug() << "error: " << adb_error();
return false;
}
if (!sync_readmode(rpath, &mode)) {
return false;
}
if(mode == 0) {
qDebug() << "remote object" << rpath << "does not exist\n";
return false;
}
if(S_ISREG(mode) || S_ISLNK(mode) || S_ISCHR(mode) || S_ISBLK(mode)) {
QString finalLpath = lpath;
QFileInfo lInfo(lpath);
if (lInfo.isDir()) {
finalLpath += "/" + QFileInfo(rpath).fileName();
}
if (!sync_recv(rpath, finalLpath)) {
return false;
} else {
sync_quit();
return true;
}
} else if(S_ISDIR(mode)) {
qDebug() << "Error: do not support directroy: " << rpath;
return false;
} else {
qDebug() << rpath << " is not a file or directroy";
return false;
}
}
bool AdbClient::sync_readmode(const char *path, quint32 *mode)
{
syncmsg msg;
int len = strlen(path);
msg.req.id = ID_STAT;
msg.req.namelen = htoll(len);
if(!writex(&msg.req, sizeof(msg.req)) ||
!writex(path, len)) {
return false;
}
if(!readx(&msg.stat, sizeof(msg.stat))) {
return false;
}
if(msg.stat.id != ID_STAT) {
return false;
}
*mode = ltohl(msg.stat.mode);
return true;
}
bool AdbClient::write_data_file(const QString& path, syncsendbuf *sbuf)
{
int isOK = true;
unsigned long long size = 0;
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Can't open" << path;
return false;
}
sbuf->id = ID_DATA;
for(;;) {
int ret;
ret = file.read(sbuf->data, SYNC_DATA_MAX);
if(!ret)
break;
if(ret < 0) {
break;
}
sbuf->size = htoll(ret);
if(!writex(sbuf, sizeof(unsigned) * 2 + ret)){
isOK = false;
break;
}
}
file.close();
return isOK;
}
bool AdbClient::sync_send(const QString& lpath, const QString& rpath,
unsigned mtime, quint32 mode)
{
syncmsg msg;
int len, r;
syncsendbuf *sbuf = &send_buffer;
char tmp[64];
len = rpath.toUtf8().size();
if(len > 1024) goto fail;
snprintf(tmp, sizeof(tmp), ",%d", mode);
r = strlen(tmp);
msg.req.id = ID_SEND;
msg.req.namelen = htoll(len + r);
if(!writex(&msg.req, sizeof(msg.req)) ||
!writex(rpath.toUtf8(), len) || !writex(tmp, r)) {
goto fail;
}
write_data_file(lpath, sbuf);
msg.data.id = ID_DONE;
msg.data.size = htoll(mtime);
if(!writex(&msg.data, sizeof(msg.data)))
goto fail;
if(!readx(&msg.status, sizeof(msg.status)))
return -1;
if(msg.status.id != ID_OKAY) {
if(msg.status.id == ID_FAIL) {
len = ltohl(msg.status.msglen);
if(len > 256) len = 256;
if(!readx(sbuf->data, len)) {
return false;
}
sbuf->data[len] = 0;
} else
strcpy(sbuf->data, "unknown reason");
fprintf(stderr,"failed to copy '%s' to '%s': %s\n", lpath.toUtf8().constData(), rpath.toUtf8().constData(), sbuf->data);
return false;
}
return true;
fail:
fprintf(stderr,"protocol failure\n");
return false;
}
void AdbClient::sync_quit()
{
syncmsg msg;
msg.req.id = ID_QUIT;
msg.req.namelen = 0;
writex(&msg.req, sizeof(msg.req));
}
bool AdbClient::do_sync_push(const char *lpath, const char *rpath)
{
quint32 mode;
int fd;
if (!adb_connect("sync:")) {
return false;
}
QFileInfo lInfo(lpath);
if (lInfo.isDir()) {
qDebug() << lpath << "is a directory, not supported for push";
return false;
} else {
if(!sync_readmode(rpath, &mode)) {
qDebug() << "can't read rpath mode";
return false;
}
QString finalRemotePath = rpath;
if((mode != 0) && S_ISDIR(mode)) {
/* if we're copying a local file to a remote directory,
** we *really* want to copy to remotedir + "/" + localfilename
*/
QString name = lInfo.fileName();
finalRemotePath += "/" + name;
}
if(!sync_send(lpath, finalRemotePath, lInfo.lastModified().toTime_t(), 0x81b6)) {
qDebug() << "send failed";
return false;
} else {
sync_quit();
return true;
}
}
return true;
}
bool AdbClient::doAdbPush(const QString& lpath, const QString& rpath)
{
AdbClient *adb = new AdbClient();
bool res = adb->do_sync_push(lpath.toUtf8().constData(), rpath.toUtf8().constData());
delete adb;
return res;
}
bool AdbClient::doAdbPull(const QString& rpath, const QString& lpath)
{
AdbClient *adb = new AdbClient();
bool res = adb->do_sync_pull(rpath.toUtf8().constData(), lpath.toUtf8().constData());
delete adb;
return res;
}
int AdbClient::doAdbKill()
{
AdbClient *adb = new AdbClient();
adb->adbSock.write("0009host:kill");
adb->adbSock.flush();
while (adb->adbSock.waitForReadyRead()) {
adb->adbSock.readAll();
}
adb->adbSock.close();
delete adb;
return 0;
}
QStringList AdbClient::doAdbDevices()
{
AdbClient *adb = new AdbClient();
bool res = adb->adb_connect("host:devices");
QByteArray buf(5, 0);
QString devicesStr;
if (res) {
if (adb->readx(buf.data(), 4)) {
int len = strtoul(buf.data(), 0, 16);
buf.reserve(len + 1);
buf[len] = 0;
adb->readx(buf.data(), len);
} else {
buf.fill(0);
}
devicesStr = QString::fromUtf8(buf);
} else {
qDebug() << "adb devices not ok:" << adb->__adb_error;
}
delete adb;
QStringList ret;
foreach(const QString& line, devicesStr.split("\n", QString::SkipEmptyParts)) {
if (line.contains("\t")) {
foreach(const QString& dev, line.split("\t")) {
ret << dev;
break;
}
}
}
return ret;
}
// "host:forward:tcp:28888;localabstract:Wrench"
int AdbClient::doAdbForward(const QString& forwardSpec)
{
AdbClient *adb = new AdbClient();
QString instanceString = QProcessEnvironment::systemEnvironment().value("WRENCH_INSTANCE", "0");
QString forwardSpecVar = forwardSpec;
if (instanceString != "0") {
QRegExp re("(.*forward:tcp:)(\\d+)(;.*)");
if (re.exactMatch(forwardSpec)) {
QString port = re.cap(2);
forwardSpecVar = re.cap(1) + QString::asprintf("%d", port.toInt() + instanceString.toInt()) + re.cap(3);
qDebug() << "forwardSpecVar is " << forwardSpecVar;
}
}
adb->adb_connect(forwardSpecVar.toUtf8().constData());
delete adb;
return 0;
}