2015-02-25 00:51:19 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 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.
|
|
|
|
*/
|
|
|
|
|
2015-03-19 23:21:08 +01:00
|
|
|
#define TRACE_TAG TRACE_ADB
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
#include "adb_client.h"
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <sys/stat.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <sys/types.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-27 23:20:17 +02:00
|
|
|
#include <string>
|
2015-04-29 17:35:59 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <base/stringprintf.h>
|
2015-04-29 21:28:13 +02:00
|
|
|
#include <base/strings.h>
|
2015-04-27 23:20:17 +02:00
|
|
|
|
2015-02-25 06:26:58 +01:00
|
|
|
#include "adb_io.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
static TransportType __adb_transport = kTransportAny;
|
2009-03-04 04:32:55 +01:00
|
|
|
static const char* __adb_serial = NULL;
|
|
|
|
|
2010-04-19 13:21:12 +02:00
|
|
|
static int __adb_server_port = DEFAULT_ADB_PORT;
|
2012-11-14 19:16:17 +01:00
|
|
|
static const char* __adb_server_name = NULL;
|
2010-04-19 13:21:12 +02:00
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
static std::string perror_str(const char* msg) {
|
|
|
|
return android::base::StringPrintf("%s: %s", msg, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ReadProtocolString(int fd, std::string* s, std::string* error) {
|
|
|
|
char buf[5];
|
|
|
|
if (!ReadFdExactly(fd, buf, 4)) {
|
|
|
|
*error = perror_str("protocol fault (couldn't read status length)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
buf[4] = 0;
|
|
|
|
|
|
|
|
unsigned long len = strtoul(buf, 0, 16);
|
2015-05-01 02:32:03 +02:00
|
|
|
s->resize(len, '\0');
|
2015-04-29 21:28:13 +02:00
|
|
|
if (!ReadFdExactly(fd, &(*s)[0], len)) {
|
|
|
|
*error = perror_str("protocol fault (couldn't read status message)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-05 22:10:43 +02:00
|
|
|
void adb_set_transport(TransportType type, const char* serial)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
__adb_transport = type;
|
|
|
|
__adb_serial = serial;
|
|
|
|
}
|
|
|
|
|
2010-04-19 13:21:12 +02:00
|
|
|
void adb_set_tcp_specifics(int server_port)
|
|
|
|
{
|
|
|
|
__adb_server_port = server_port;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:16:17 +01:00
|
|
|
void adb_set_tcp_name(const char* hostname)
|
|
|
|
{
|
|
|
|
__adb_server_name = hostname;
|
|
|
|
}
|
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
static int switch_socket_transport(int fd, std::string* error) {
|
2015-04-27 23:20:17 +02:00
|
|
|
std::string service;
|
|
|
|
if (__adb_serial) {
|
|
|
|
service += "host:transport:";
|
|
|
|
service += __adb_serial;
|
|
|
|
} else {
|
2015-02-26 02:51:28 +01:00
|
|
|
const char* transport_type = "???";
|
2015-04-27 23:20:17 +02:00
|
|
|
switch (__adb_transport) {
|
|
|
|
case kTransportUsb:
|
|
|
|
transport_type = "transport-usb";
|
|
|
|
break;
|
|
|
|
case kTransportLocal:
|
|
|
|
transport_type = "transport-local";
|
|
|
|
break;
|
|
|
|
case kTransportAny:
|
|
|
|
transport_type = "transport-any";
|
|
|
|
break;
|
|
|
|
case kTransportHost:
|
|
|
|
// no switch necessary
|
|
|
|
return 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-04-27 23:20:17 +02:00
|
|
|
service += "host:";
|
|
|
|
service += transport_type;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
if (!SendProtocolString(fd, service)) {
|
2015-04-29 17:35:59 +02:00
|
|
|
*error = perror_str("write failure during connection");
|
2009-03-04 04:32:55 +01:00
|
|
|
adb_close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
D("Switch transport in progress\n");
|
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
if (!adb_status(fd, error)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
adb_close(fd);
|
2015-04-29 17:35:59 +02:00
|
|
|
D("Switch transport failed: %s\n", error->c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
D("Switch transport success\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
bool adb_status(int fd, std::string* error) {
|
|
|
|
char buf[5];
|
|
|
|
if (!ReadFdExactly(fd, buf, 4)) {
|
|
|
|
*error = perror_str("protocol fault (couldn't read status)");
|
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
if (!memcmp(buf, "OKAY", 4)) {
|
|
|
|
return true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
if (memcmp(buf, "FAIL", 4)) {
|
|
|
|
*error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)",
|
|
|
|
buf[0], buf[1], buf[2], buf[3]);
|
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
ReadProtocolString(fd, error, error);
|
2015-04-29 17:35:59 +02:00
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
int _adb_connect(const std::string& service, std::string* error) {
|
|
|
|
D("_adb_connect: %s\n", service.c_str());
|
|
|
|
if (service.empty() || service.size() > 1024) {
|
2015-05-13 09:02:55 +02:00
|
|
|
*error = android::base::StringPrintf("bad service name length (%zd)",
|
|
|
|
service.size());
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
int fd;
|
|
|
|
if (__adb_server_name) {
|
2012-11-14 19:16:17 +01:00
|
|
|
fd = socket_network_client(__adb_server_name, __adb_server_port, SOCK_STREAM);
|
2015-04-29 21:28:13 +02:00
|
|
|
} else {
|
2012-11-14 19:16:17 +01:00
|
|
|
fd = socket_loopback_client(__adb_server_port, SOCK_STREAM);
|
2015-04-29 21:28:13 +02:00
|
|
|
}
|
|
|
|
if (fd < 0) {
|
2015-04-29 17:35:59 +02:00
|
|
|
*error = perror_str("cannot connect to daemon");
|
2009-03-04 04:32:55 +01:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
if (memcmp(&service[0],"host",4) != 0 && switch_socket_transport(fd, error)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
if(!SendProtocolString(fd, service)) {
|
2015-04-29 17:35:59 +02:00
|
|
|
*error = perror_str("write failure during connection");
|
2009-03-04 04:32:55 +01:00
|
|
|
adb_close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
if (!adb_status(fd, error)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
adb_close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-16 23:57:42 +01:00
|
|
|
D("_adb_connect: return fd %d\n", fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
int adb_connect(const std::string& service, std::string* error) {
|
2009-03-04 04:32:55 +01:00
|
|
|
// first query the adb server's version
|
2015-04-29 17:35:59 +02:00
|
|
|
int fd = _adb_connect("host:version", error);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
D("adb_connect: service %s\n", service.c_str());
|
2015-04-29 17:35:59 +02:00
|
|
|
if (fd == -2 && __adb_server_name) {
|
2012-11-14 19:16:17 +01:00
|
|
|
fprintf(stderr,"** Cannot start server on remote host\n");
|
|
|
|
return fd;
|
2015-04-29 17:35:59 +02:00
|
|
|
} else if (fd == -2) {
|
2010-04-19 13:21:12 +02:00
|
|
|
fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
|
|
|
|
__adb_server_port);
|
2009-03-04 04:32:55 +01:00
|
|
|
start_server:
|
2015-04-29 17:35:59 +02:00
|
|
|
if (launch_server(__adb_server_port)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr,"* failed to start daemon *\n");
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
fprintf(stdout,"* daemon started successfully *\n");
|
|
|
|
}
|
|
|
|
/* give the server some time to start properly and detect devices */
|
2009-05-22 02:47:43 +02:00
|
|
|
adb_sleep_ms(3000);
|
2009-03-04 04:32:55 +01:00
|
|
|
// fall through to _adb_connect
|
|
|
|
} else {
|
|
|
|
// if server was running, check its version to make sure it is not out of date
|
|
|
|
int version = ADB_SERVER_VERSION - 1;
|
|
|
|
|
|
|
|
// if we have a file descriptor, then parse version result
|
2015-04-29 17:35:59 +02:00
|
|
|
if (fd >= 0) {
|
2015-04-29 21:28:13 +02:00
|
|
|
std::string version_string;
|
|
|
|
if (!ReadProtocolString(fd, &version_string, error)) {
|
|
|
|
goto error;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
adb_close(fd);
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
if (sscanf(&version_string[0], "%04x", &version) != 1) {
|
|
|
|
goto error;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
|
|
|
// if fd is -1, then check for "unknown host service",
|
|
|
|
// which would indicate a version of adb that does not support the version command
|
2015-04-29 17:35:59 +02:00
|
|
|
if (*error == "unknown host service") {
|
2009-03-04 04:32:55 +01:00
|
|
|
return fd;
|
2015-04-29 17:35:59 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
if (version != ADB_SERVER_VERSION) {
|
2009-03-04 04:32:55 +01:00
|
|
|
printf("adb server is out of date. killing...\n");
|
2015-04-29 17:35:59 +02:00
|
|
|
fd = _adb_connect("host:kill", error);
|
2009-03-04 04:32:55 +01:00
|
|
|
adb_close(fd);
|
|
|
|
|
|
|
|
/* XXX can we better detect its death? */
|
|
|
|
adb_sleep_ms(2000);
|
|
|
|
goto start_server;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the command is start-server, we are done.
|
2015-04-29 21:28:13 +02:00
|
|
|
if (service == "host:start-server") {
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
2015-04-29 17:35:59 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-29 17:35:59 +02:00
|
|
|
fd = _adb_connect(service, error);
|
|
|
|
if (fd == -1) {
|
|
|
|
D("_adb_connect error: %s", error->c_str());
|
2013-10-18 22:58:48 +02:00
|
|
|
} else if(fd == -2) {
|
2012-11-14 19:16:17 +01:00
|
|
|
fprintf(stderr,"** daemon still not running\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2011-03-16 23:57:42 +01:00
|
|
|
D("adb_connect: return fd %d\n", fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
error:
|
|
|
|
adb_close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-30 02:55:19 +02:00
|
|
|
bool adb_command(const std::string& service) {
|
|
|
|
std::string error;
|
|
|
|
int fd = adb_connect(service, &error);
|
2015-04-29 17:35:59 +02:00
|
|
|
if (fd < 0) {
|
2015-05-30 02:55:19 +02:00
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-30 02:55:19 +02:00
|
|
|
if (!adb_status(fd, &error)) {
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
adb_close(fd);
|
2015-05-30 02:55:19 +02:00
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-30 02:55:19 +02:00
|
|
|
return true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
bool adb_query(const std::string& service, std::string* result, std::string* error) {
|
|
|
|
D("adb_query: %s\n", service.c_str());
|
2015-04-29 17:35:59 +02:00
|
|
|
int fd = adb_connect(service, error);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr,"error: %s\n", error->c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:28:13 +02:00
|
|
|
result->clear();
|
|
|
|
if (!ReadProtocolString(fd, result, error)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
adb_close(fd);
|
2015-04-29 21:28:13 +02:00
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-04-29 21:28:13 +02:00
|
|
|
return true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|