admin管理员组

文章数量:1279112

I am working on an embedded Linux system (kernel-5.10.188) with busybox and adbd in file system.

During my testing, I occasionally found I failed to login to the system through adb shell with error of error: no devices/emulators found. I have to restart the adbd from serial console to make adb shell work.

So I want to find out a way to detect that adbd is not working and should be restarted.
Here is other info about adbd in my system (got from adb shell)

# lsof | grep adbd
201     /usr/bin/adbd   0       /dev/null
201     /usr/bin/adbd   1       /dev/console
201     /usr/bin/adbd   2       /dev/console
201     /usr/bin/adbd   3       socket:[3341]
201     /usr/bin/adbd   4       socket:[3342]
201     /usr/bin/adbd   5       socket:[3343]
201     /usr/bin/adbd   6       socket:[3344]
201     /usr/bin/adbd   7       socket:[3345]
201     /usr/bin/adbd   8       socket:[3346]
201     /usr/bin/adbd   9       /dev/usb-ffs/adb/ep0
201     /usr/bin/adbd   10      /dev/usb-ffs/adb/ep1
201     /usr/bin/adbd   11      /dev/usb-ffs/adb/ep2
201     /usr/bin/adbd   12      socket:[3943]
201     /usr/bin/adbd   13      socket:[3944]
201     /usr/bin/adbd   14      /dev/ptmx

# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 192.168.0.1:www         0.0.0.0:*               LISTEN
tcp        0      0 192.168.0.1:domain      0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:56797           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:56800           0.0.0.0:*               LISTEN
tcp        0      0 localhost:5037          0.0.0.0:*               LISTEN
udp        0      0 0.0.0.0:56797           0.0.0.0:*
udp        0      0 192.168.0.1:domain      0.0.0.0:*
udp        0      0 0.0.0.0:bootps          0.0.0.0:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ]         DGRAM                      1565 /var/run/hostapd/wlan0
unix  5      [ ]         DGRAM                      1212 /dev/log
unix  2      [ ACC ]     STREAM     LISTENING       3344 @jdwp-control
unix  3      [ ]         STREAM     CONNECTED       3346
unix  2      [ ]         DGRAM                      1553
unix  3      [ ]         STREAM     CONNECTED       3944
unix  3      [ ]         STREAM     CONNECTED       3341
unix  3      [ ]         STREAM     CONNECTED       3342
unix  2      [ ]         DGRAM                      1213
unix  3      [ ]         STREAM     CONNECTED       3345
unix  3      [ ]         STREAM     CONNECTED       3943
unix  2      [ ]         DGRAM                      1541

I have tried to communicate with adbd in the same device through 127.0.0.1:5037 as follows to send host:devices, it failed (as expected).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define ADB_HOST "127.0.0.1"
#define ADB_PORT 5037

void send_adb_command(const char *command) {
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr = {
        .sin_family = AF_INET,
        .sin_port = htons(ADB_PORT),
        .sin_addr.s_addr = inet_addr(ADB_HOST)
    };

    struct timeval timeout = {2, 0};
    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));

    connect(sock, (struct sockaddr*)&addr, sizeof(addr));

    char header[5];
    snprintf(header, sizeof(header), "%04X", (unsigned int)strlen(command));
    send(sock, header, 4, 0);
    send(sock, command, strlen(command), 0);

    char response[1024];
    recv(sock, response, sizeof(response)-1, 0);
    printf("Response: %s\n", response);

    close(sock);
}

int main() {
    send_adb_command("host:version");
    send_adb_command("host:devices");
    return 0;
}

Above codes could NOT get expected results when running it in the same device as adbd is in for adbd is built with ADB_HOST = 0, so it would NOT accept host commands like host:version. I want to ping adbd, so what command (and in what format) is valid to adbd in the same device?

Updated with questions on endpoints of USB FFS.

May I emulate a adb shell command from within the Linux device through reading/writting /dev/usb-ffs/adb/ep1 and /dev/usb-ffs/adb/ep2? If so, what is the right message format of the adb shell command can use?

本文标签: androidHow to chat with adbd through port 5037 or USB FFS in the deviceStack Overflow