Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 99d3d705 authored by Josh Gao's avatar Josh Gao
Browse files

adb: extract helper for dumping a packet header.

Test: mma
Change-Id: I3e15296eb917d9df11ca13591d26d3aa54d66412
parent 560a547d
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -186,6 +186,48 @@ std::string dump_hex(const void* data, size_t byte_count) {
    return line;
}

std::string dump_header(const amessage* msg) {
    unsigned command = msg->command;
    int len = msg->data_length;
    char cmd[9];
    char arg0[12], arg1[12];
    int n;

    for (n = 0; n < 4; n++) {
        int b = (command >> (n * 8)) & 255;
        if (b < 32 || b >= 127) break;
        cmd[n] = (char)b;
    }
    if (n == 4) {
        cmd[4] = 0;
    } else {
        // There is some non-ASCII name in the command, so dump the hexadecimal value instead
        snprintf(cmd, sizeof cmd, "%08x", command);
    }

    if (msg->arg0 < 256U)
        snprintf(arg0, sizeof arg0, "%d", msg->arg0);
    else
        snprintf(arg0, sizeof arg0, "0x%x", msg->arg0);

    if (msg->arg1 < 256U)
        snprintf(arg1, sizeof arg1, "%d", msg->arg1);
    else
        snprintf(arg1, sizeof arg1, "0x%x", msg->arg1);

    return android::base::StringPrintf("[%s] arg0=%s arg1=%s (len=%d) ", cmd, arg0, arg1, len);
}

std::string dump_packet(const char* name, const char* func, const apacket* p) {
    std::string result = name;
    result += ": ";
    result += func;
    result += ": ";
    result += dump_header(&p->msg);
    result += dump_hex(p->payload.data(), p->payload.size());
    return result;
}

std::string perror_str(const char* msg) {
    return android::base::StringPrintf("%s: %s", msg, strerror(errno));
}
+4 −0
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@

#include <android-base/macros.h>

#include "adb.h"

int syntax_error(const char*, ...) __attribute__((__format__(__printf__, 1, 2)));

void close_stdin();
@@ -42,6 +44,8 @@ bool mkdirs(const std::string& path);
std::string escape_arg(const std::string& s);

std::string dump_hex(const void* ptr, size_t byte_count);
std::string dump_header(const amessage* msg);
std::string dump_packet(const char* name, const char* func, const apacket* p);

std::string perror_str(const char* msg);

+0 −36
Original line number Diff line number Diff line
@@ -408,42 +408,6 @@ void FdConnection::Close() {
    fd_.reset();
}

static std::string dump_packet(const char* name, const char* func, apacket* p) {
    unsigned command = p->msg.command;
    int len = p->msg.data_length;
    char cmd[9];
    char arg0[12], arg1[12];
    int n;

    for (n = 0; n < 4; n++) {
        int b = (command >> (n * 8)) & 255;
        if (b < 32 || b >= 127) break;
        cmd[n] = (char)b;
    }
    if (n == 4) {
        cmd[4] = 0;
    } else {
        /* There is some non-ASCII name in the command, so dump
            * the hexadecimal value instead */
        snprintf(cmd, sizeof cmd, "%08x", command);
    }

    if (p->msg.arg0 < 256U)
        snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
    else
        snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);

    if (p->msg.arg1 < 256U)
        snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
    else
        snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);

    std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", name,
                                                     func, cmd, arg0, arg1, len);
    result += dump_hex(p->payload.data(), p->payload.size());
    return result;
}

void send_packet(apacket* p, atransport* t) {
    p->msg.magic = p->msg.command ^ 0xffffffff;
    // compute a checksum for connection/auth packets for compatibility reasons