2015-02-25 13:26:58 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-03-20 06:21:08 +08:00
|
|
|
#define TRACE_TAG TRACE_RWX
|
2015-02-25 13:26:58 +08:00
|
|
|
|
|
|
|
#include "adb_io.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-05-02 08:04:38 +08:00
|
|
|
#include <base/stringprintf.h>
|
|
|
|
|
2015-02-25 13:26:58 +08:00
|
|
|
#include "adb_trace.h"
|
2015-05-01 08:32:03 +08:00
|
|
|
#include "adb_utils.h"
|
2015-05-02 08:04:38 +08:00
|
|
|
#include "sysdeps.h"
|
2015-05-01 08:32:03 +08:00
|
|
|
|
|
|
|
bool SendProtocolString(int fd, const std::string& s) {
|
|
|
|
int length = s.size();
|
|
|
|
if (length > 0xffff) {
|
|
|
|
length = 0xffff;
|
|
|
|
}
|
|
|
|
|
2015-05-02 08:04:38 +08:00
|
|
|
return WriteFdFmt(fd, "%04x", length) && WriteFdExactly(fd, s);
|
2015-05-01 08:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SendOkay(int fd) {
|
|
|
|
return WriteFdExactly(fd, "OKAY", 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SendFail(int fd, const std::string& reason) {
|
|
|
|
return WriteFdExactly(fd, "FAIL", 4) && SendProtocolString(fd, reason);
|
|
|
|
}
|
2015-02-25 13:26:58 +08:00
|
|
|
|
|
|
|
bool ReadFdExactly(int fd, void* buf, size_t len) {
|
|
|
|
char* p = reinterpret_cast<char*>(buf);
|
|
|
|
|
|
|
|
size_t len0 = len;
|
|
|
|
|
|
|
|
D("readx: fd=%d wanted=%zu\n", fd, len);
|
|
|
|
while (len > 0) {
|
2015-02-26 09:51:28 +08:00
|
|
|
int r = adb_read(fd, p, len);
|
2015-02-25 13:26:58 +08:00
|
|
|
if (r > 0) {
|
|
|
|
len -= r;
|
|
|
|
p += r;
|
|
|
|
} else if (r == -1) {
|
|
|
|
D("readx: fd=%d error %d: %s\n", fd, errno, strerror(errno));
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
D("readx: fd=%d disconnected\n", fd);
|
|
|
|
errno = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len);
|
|
|
|
if (ADB_TRACING) {
|
|
|
|
dump_hex(reinterpret_cast<const unsigned char*>(buf), len0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteFdExactly(int fd, const void* buf, size_t len) {
|
|
|
|
const char* p = reinterpret_cast<const char*>(buf);
|
|
|
|
int r;
|
|
|
|
|
|
|
|
D("writex: fd=%d len=%d: ", fd, (int)len);
|
|
|
|
if (ADB_TRACING) {
|
|
|
|
dump_hex(reinterpret_cast<const unsigned char*>(buf), len);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len > 0) {
|
2015-02-26 09:51:28 +08:00
|
|
|
r = adb_write(fd, p, len);
|
2015-02-25 13:26:58 +08:00
|
|
|
if (r == -1) {
|
|
|
|
D("writex: fd=%d error %d: %s\n", fd, errno, strerror(errno));
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
adb_sleep_ms(1); // just yield some cpu time
|
|
|
|
continue;
|
|
|
|
} else if (errno == EPIPE) {
|
|
|
|
D("writex: fd=%d disconnected\n", fd);
|
|
|
|
errno = 0;
|
|
|
|
return false;
|
2015-03-20 09:09:59 +08:00
|
|
|
} else {
|
|
|
|
return false;
|
2015-02-25 13:26:58 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
len -= r;
|
|
|
|
p += r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-01 08:32:03 +08:00
|
|
|
bool WriteFdExactly(int fd, const char* str) {
|
|
|
|
return WriteFdExactly(fd, str, strlen(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteFdExactly(int fd, const std::string& str) {
|
|
|
|
return WriteFdExactly(fd, str.c_str(), str.size());
|
|
|
|
}
|
|
|
|
|
2015-05-02 08:04:38 +08:00
|
|
|
bool WriteFdFmt(int fd, const char* fmt, ...) {
|
|
|
|
std::string str;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
android::base::StringAppendV(&str, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return WriteFdExactly(fd, str);
|
2015-02-25 13:26:58 +08:00
|
|
|
}
|