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
|
#include "usb_transport_sniffer.h"
#include <android-base/stringprintf.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <iomanip>
#include <sstream>
namespace fastboot {
UsbTransportSniffer::UsbTransportSniffer(std::unique_ptr<UsbTransport> transport,
const int serial_fd)
: transport_(std::move(transport)), serial_fd_(serial_fd) {}
UsbTransportSniffer::~UsbTransportSniffer() {
Close();
}
ssize_t UsbTransportSniffer::Read(void* data, size_t len) {
ProcessSerial();
ssize_t ret = transport_->Read(data, len);
if (ret < 0) {
const char* err = strerror(errno);
std::vector<char> buf(err, err + strlen(err));
Event e(READ_ERROR, std::move(buf));
transfers_.push_back(e);
return ret;
}
char* cdata = static_cast<char*>(data);
std::vector<char> buf(cdata, cdata + ret);
Event e(READ, std::move(buf));
transfers_.push_back(e);
ProcessSerial();
return ret;
}
ssize_t UsbTransportSniffer::Write(const void* data, size_t len) {
ProcessSerial();
size_t ret = transport_->Write(data, len);
if (ret != len) {
const char* err = strerror(errno);
std::vector<char> buf(err, err + strlen(err));
Event e(WRITE_ERROR, std::move(buf));
transfers_.push_back(e);
return ret;
}
const char* cdata = static_cast<const char*>(data);
std::vector<char> buf(cdata, cdata + len);
Event e(WRITE, std::move(buf));
transfers_.push_back(e);
ProcessSerial();
return ret;
}
int UsbTransportSniffer::Close() {
return transport_->Close();
}
int UsbTransportSniffer::Reset() {
ProcessSerial();
int ret = transport_->Reset();
std::vector<char> buf;
Event e(RESET, std::move(buf));
transfers_.push_back(e);
ProcessSerial();
return ret;
}
const std::vector<UsbTransportSniffer::Event> UsbTransportSniffer::Transfers() {
return transfers_;
}
/*
* When a test fails, we want a human readable log of everything going on up until
* the failure. This method will look through its log of captured events, and
* create a clean printable string of everything that happened.
*/
std::string UsbTransportSniffer::CreateTrace() {
std::string ret;
const auto no_print = [](char c) -> bool { return !isprint(c); };
// This lambda creates a humand readable representation of a byte buffer
// It first attempts to figure out whether it should be interpreted as an ASCII buffer,
// and be printed as a string, or just a raw byte-buffer
const auto msg = [&ret, no_print](const std::vector<char>& buf) {
ret += android::base::StringPrintf("(%lu bytes): ", buf.size());
std::vector<const char>::iterator iter = buf.end();
const unsigned max_chars = 50;
if (buf.size() > max_chars) {
iter = buf.begin() + max_chars;
}
ret += '"';
if (std::count_if(buf.begin(), iter, no_print) == 0) { // print as ascii
ret.insert(ret.end(), buf.begin(), iter);
} else { // print as hex
std::stringstream ss;
for (auto c = buf.begin(); c < iter; c++) {
ss << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<uint16_t>(static_cast<uint8_t>(*c));
ss << ',';
}
ret += ss.str();
}
if (buf.size() > max_chars) {
ret += android::base::StringPrintf("...\"(+%lu bytes)\n", buf.size() - max_chars);
} else {
ret += "\"\n";
}
};
// Now we just scan through the log of everything that happened and create a
// printable string for each one
for (const auto& event : transfers_) {
const std::vector<char>& cbuf = event.buf;
const std::string tmp(cbuf.begin(), cbuf.end());
auto start = transfers_.front().start;
auto durr = event.start - start;
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(durr).count();
switch (event.type) {
case READ:
ret += android::base::StringPrintf("[READ %lldms]", millis);
msg(cbuf);
break;
case WRITE:
ret += android::base::StringPrintf("[WRITE %lldms]", millis);
msg(cbuf);
break;
case RESET:
ret += android::base::StringPrintf("[RESET %lldms]\n", millis);
break;
case READ_ERROR:
ret += android::base::StringPrintf("[READ_ERROR %lldms] %s\n", millis, tmp.c_str());
break;
case WRITE_ERROR:
ret += android::base::StringPrintf("[WRITE_ERROR %lldms] %s\n", millis,
tmp.c_str());
break;
case SERIAL:
ret += android::base::StringPrintf("[SERIAL %lldms] %s", millis, tmp.c_str());
if (ret.back() != '\n') ret += '\n';
break;
}
}
return ret;
}
// This is a quick call to flush any UART logs the device might have sent
// to our internal event log. It will wait up to 10ms for data to appear
void UsbTransportSniffer::ProcessSerial() {
if (serial_fd_ <= 0) return;
fd_set set;
struct timeval timeout;
FD_ZERO(&set);
FD_SET(serial_fd_, &set);
timeout.tv_sec = 0;
timeout.tv_usec = 10000; // 10ms
int count = 0;
int n = 0;
std::vector<char> buf;
buf.resize(1000);
while (select(serial_fd_ + 1, &set, NULL, NULL, &timeout) > 0) {
n = read(serial_fd_, buf.data() + count, buf.size() - count);
if (n > 0) {
count += n;
} else {
break;
}
}
buf.resize(count);
if (count > 0) {
Event e(SERIAL, std::move(buf));
transfers_.push_back(e);
}
}
} // namespace fastboot
|