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

Commit 13a269ea authored by Casey Dahlin's avatar Casey Dahlin
Browse files

Add mDNS device discovery for adb client

Test: Was able to discover a raspberry pi.
Bug: 28074466
(cherry picked from e292cd16760321fccc99c8c261cb92fa4b6462ab)

Change-Id: Id9571576457a4a0a078e48a274a4e8eac78bfe2b
parent 1fe3cae6
Loading
Loading
Loading
Loading
+4 −1
Original line number Original line Diff line number Diff line
@@ -145,6 +145,7 @@ LOCAL_CFLAGS_darwin := $(LIBADB_darwin_CFLAGS)
LOCAL_SRC_FILES := \
LOCAL_SRC_FILES := \
    $(LIBADB_SRC_FILES) \
    $(LIBADB_SRC_FILES) \
    adb_auth_host.cpp \
    adb_auth_host.cpp \
    transport_mdns.cpp


LOCAL_SRC_FILES_darwin := $(LIBADB_darwin_SRC_FILES)
LOCAL_SRC_FILES_darwin := $(LIBADB_darwin_SRC_FILES)
LOCAL_SRC_FILES_linux := $(LIBADB_linux_SRC_FILES)
LOCAL_SRC_FILES_linux := $(LIBADB_linux_SRC_FILES)
@@ -154,7 +155,7 @@ LOCAL_SANITIZE := $(adb_host_sanitize)


# Even though we're building a static library (and thus there's no link step for
# Even though we're building a static library (and thus there's no link step for
# this to take effect), this adds the includes to our path.
# this to take effect), this adds the includes to our path.
LOCAL_STATIC_LIBRARIES := libcrypto_utils libcrypto libbase
LOCAL_STATIC_LIBRARIES := libcrypto_utils libcrypto libbase libmdnssd
LOCAL_STATIC_LIBRARIES_linux := libusb
LOCAL_STATIC_LIBRARIES_linux := libusb
LOCAL_STATIC_LIBRARIES_darwin := libusb
LOCAL_STATIC_LIBRARIES_darwin := libusb


@@ -225,6 +226,7 @@ LOCAL_STATIC_LIBRARIES := \
    libcutils \
    libcutils \
    libdiagnose_usb \
    libdiagnose_usb \
    libgmock_host \
    libgmock_host \
    libmdnssd \


LOCAL_STATIC_LIBRARIES_linux := libusb
LOCAL_STATIC_LIBRARIES_linux := libusb
LOCAL_STATIC_LIBRARIES_darwin := libusb
LOCAL_STATIC_LIBRARIES_darwin := libusb
@@ -292,6 +294,7 @@ LOCAL_STATIC_LIBRARIES := \
    libcrypto \
    libcrypto \
    libdiagnose_usb \
    libdiagnose_usb \
    liblog \
    liblog \
    libmdnssd \


# Don't use libcutils on Windows.
# Don't use libcutils on Windows.
LOCAL_STATIC_LIBRARIES_darwin := libcutils
LOCAL_STATIC_LIBRARIES_darwin := libcutils

adb/adb_mdns.h

0 → 100644
+22 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2016 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.
 */

#ifndef _ADB_MDNS_H_
#define _ADB_MDNS_H_

const char* kADBServiceType = "_adb._tcp";

#endif
+2 −0
Original line number Original line Diff line number Diff line
@@ -117,6 +117,8 @@ int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply


    init_transport_registration();
    init_transport_registration();


    init_mdns_transport_discovery();

    usb_init();
    usb_init();
    local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
    local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);


+1 −0
Original line number Original line Diff line number Diff line
@@ -14,6 +14,7 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


#include "adb_mdns.h"
#include "sysdeps.h"
#include "sysdeps.h"


#include <chrono>
#include <chrono>
+0 −39
Original line number Original line Diff line number Diff line
@@ -377,45 +377,6 @@ static void wait_for_state(int fd, void* data) {
    D("wait_for_state is done");
    D("wait_for_state is done");
}
}


static void connect_device(const std::string& address, std::string* response) {
    if (address.empty()) {
        *response = "empty address";
        return;
    }

    std::string serial;
    std::string host;
    int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
    if (!android::base::ParseNetAddress(address, &host, &port, &serial, response)) {
        return;
    }

    std::string error;
    int fd = network_connect(host.c_str(), port, SOCK_STREAM, 10, &error);
    if (fd == -1) {
        *response = android::base::StringPrintf("unable to connect to %s: %s",
                                                serial.c_str(), error.c_str());
        return;
    }

    D("client: connected %s remote on fd %d", serial.c_str(), fd);
    close_on_exec(fd);
    disable_tcp_nagle(fd);

    // Send a TCP keepalive ping to the device every second so we can detect disconnects.
    if (!set_tcp_keepalive(fd, 1)) {
        D("warning: failed to configure TCP keepalives (%s)", strerror(errno));
    }

    int ret = register_socket_transport(fd, serial.c_str(), port, 0);
    if (ret < 0) {
        adb_close(fd);
        *response = android::base::StringPrintf("already connected to %s", serial.c_str());
    } else {
        *response = android::base::StringPrintf("connected to %s", serial.c_str());
    }
}

void connect_emulator(const std::string& port_spec, std::string* response) {
void connect_emulator(const std::string& port_spec, std::string* response) {
    std::vector<std::string> pieces = android::base::Split(port_spec, ",");
    std::vector<std::string> pieces = android::base::Split(port_spec, ",");
    if (pieces.size() != 2) {
    if (pieces.size() != 2) {
Loading