Add mDNS device discovery for adb client
Test: Was able to discover a raspberry pi.
Bug: 28074466
(cherry picked from e292cd16760321fccc99c8c261cb92fa4b6462ab)
Change-Id: Id9571576457a4a0a078e48a274a4e8eac78bfe2b
diff --git a/transport_local.cpp b/transport_local.cpp
index b5d0ef0..12b98ba 100644
--- a/transport_local.cpp
+++ b/transport_local.cpp
@@ -30,6 +30,7 @@
#include <thread>
#include <vector>
+#include <android-base/parsenetaddress.h>
#include <android-base/stringprintf.h>
#include <cutils/sockets.h>
@@ -101,6 +102,46 @@
return local_connect_arbitrary_ports(port-1, port, &dummy) == 0;
}
+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());
+ }
+}
+
+
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
int fd = -1;