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

Commit 74d7ff8c authored by Mike Lockwood's avatar Mike Lockwood
Browse files

adb: Add "adb disconnect" command for disconnecting TCP/IP devices.



Also check that device is not already connected in "adb connect"

Change-Id: I5f84b56b63d8c6932f23791cac319fd6bc39d36c
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 1fbf27bf
Loading
Loading
Loading
Loading
+28 −5
Original line number Diff line number Diff line
@@ -1020,19 +1020,24 @@ int handle_host_request(char *service, transport_type ttype, char* serial, int r
        char* portstr = strchr(host, ':');

        if (!portstr) {
            snprintf(buffer, sizeof(buffer), "unable to parse %s as <host>:<port>\n", host);
            snprintf(buffer, sizeof(buffer), "unable to parse %s as <host>:<port>", host);
            goto done;
        }
        if (find_transport(host)) {
            snprintf(buffer, sizeof(buffer), "Already connected to %s", host);
            goto done;
        }

        // zero terminate host by overwriting the ':'
        *portstr++ = 0;
        if (sscanf(portstr, "%d", &port) == 0) {
            snprintf(buffer, sizeof(buffer), "bad port number %s\n", portstr);
            snprintf(buffer, sizeof(buffer), "bad port number %s", portstr);
            goto done;
        }

        fd = socket_network_client(host, port, SOCK_STREAM);
        if (fd < 0) {
            snprintf(buffer, sizeof(buffer), "unable to connect to %s:%d\n", host, port);
            snprintf(buffer, sizeof(buffer), "unable to connect to %s:%d", host, port);
            goto done;
        }

@@ -1041,7 +1046,7 @@ int handle_host_request(char *service, transport_type ttype, char* serial, int r
        disable_tcp_nagle(fd);
        snprintf(buf, sizeof buf, "%s:%d", host, port);
        register_socket_transport(fd, buf, port, 0);
        snprintf(buffer, sizeof(buffer), "connected to %s:%d\n", host, port);
        snprintf(buffer, sizeof(buffer), "connected to %s:%d", host, port);

done:
        snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
@@ -1049,6 +1054,24 @@ done:
        return 0;
    }

    // remove TCP transport
    if (!strncmp(service, "disconnect:", 11)) {
        char buffer[4096];
        memset(buffer, 0, sizeof(buffer));
        char* serial = service + 11;
        atransport *t = find_transport(serial);

        if (t) {
            unregister_transport(t);
        } else {
            snprintf(buffer, sizeof(buffer), "No such device %s", serial);
        }

        snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
        writex(reply_fd, buf, strlen(buf));
        return 0;
    }

    // returns our value for ADB_SERVER_VERSION
    if (!strcmp(service, "version")) {
        char version[12];
+6 −0
Original line number Diff line number Diff line
@@ -270,11 +270,17 @@ void close_usb_devices();

/* cause new transports to be init'd and added to the list */
void register_socket_transport(int s, const char *serial, int port, int local);

/* this should only be used for the "adb disconnect" command */
void unregister_transport(atransport *t);

void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);

/* this should only be used for transports with connection_state == CS_NOPERM */
void unregister_usb_transport(usb_handle *usb);

atransport *find_transport(const char *serial);

int service_to_fd(const char *name);
#if ADB_HOST
asocket *host_service_to_socket(const char*  name, const char *serial);
+3 −2
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ void help()
        "                                 be an absolute path.\n"
        " devices                       - list all connected devices\n"
        " connect <host>:<port>         - connect to a device via TCP/IP"
        " disconnect <host>:<port>      - disconnect from a TCP/IP device"
        "\n"
        "device commands:\n"
        "  adb push <local> <remote>    - copy file/dir to device\n"
@@ -853,10 +854,10 @@ top:
        }
    }

    if(!strcmp(argv[0], "connect")) {
    if(!strcmp(argv[0], "connect") || !strcmp(argv[0], "disconnect")) {
        char *tmp;
        if (argc != 2) {
            fprintf(stderr, "Usage: adb connect <host>:<port>\n");
            fprintf(stderr, "Usage: adb %s <host>:<port>\n", argv[0]);
            return 1;
        }
        snprintf(buf, sizeof buf, "host:%s:%s", argv[0], argv[1]);
+1 −1
Original line number Diff line number Diff line
@@ -164,7 +164,7 @@ jdwp_process_free( JdwpProcess* proc )
        proc->next->prev = proc->prev;

        if (proc->socket >= 0) {
            shutdown(proc->socket, SHUT_RDWR);
            adb_shutdown(proc->socket);
            adb_close(proc->socket);
            proc->socket = -1;
        }
+8 −0
Original line number Diff line number Diff line
@@ -113,6 +113,7 @@ extern int adb_creat(const char* path, int mode);
extern int  adb_read(int  fd, void* buf, int len);
extern int  adb_write(int  fd, const void*  buf, int  len);
extern int  adb_lseek(int  fd, int  pos, int  where);
extern int  adb_shutdown(int  fd);
extern int  adb_close(int  fd);

static __inline__ int  unix_close(int fd)
@@ -327,6 +328,13 @@ static __inline__ int adb_open( const char* pathname, int options )
#undef   open
#define  open    ___xxx_open

static __inline__ int  adb_shutdown(int fd)
{
    return shutdown(fd, SHUT_RDWR);
}
#undef   shutdown
#define  shutdown   ____xxx_shutdown

static __inline__ int  adb_close(int fd)
{
    return close(fd);
Loading