Merge "adb: add adb reconnect offline to reconnect offline devices."

This commit is contained in:
Josh Gao 2016-10-31 21:26:23 +00:00 committed by Gerrit Code Review
commit 0945380e36
5 changed files with 53 additions and 11 deletions

View file

@ -1089,6 +1089,31 @@ int handle_host_request(const char* service, TransportType type,
return 1;
}
if (!strcmp(service, "reconnect-offline")) {
std::string response;
close_usb_devices([&response](const atransport* transport) {
switch (transport->connection_state) {
case kCsOffline:
case kCsUnauthorized:
response += "reconnecting ";
if (transport->serial) {
response += transport->serial;
} else {
response += "<unknown>";
}
response += "\n";
return true;
default:
return false;
}
});
if (!response.empty()) {
response.resize(response.size() - 1);
}
SendOkay(reply_fd, response);
return 0;
}
if (!strcmp(service, "features")) {
std::string error;
atransport* t = acquire_one_transport(type, serial, nullptr, &error);

View file

@ -1950,10 +1950,17 @@ int adb_commandline(int argc, const char** argv) {
} else if (!strcmp(argv[0], "reconnect")) {
if (argc == 1) {
return adb_query_command("host:reconnect");
} else if (argc == 2 && !strcmp(argv[1], "device")) {
std::string err;
adb_connect("reconnect", &err);
return 0;
} else if (argc == 2) {
if (!strcmp(argv[1], "device")) {
std::string err;
adb_connect("reconnect", &err);
return 0;
} else if (!strcmp(argv[1], "offline")) {
std::string err;
return adb_query_command("host:reconnect-offline");
} else {
return usage();
}
}
}

View file

@ -791,11 +791,14 @@ static int smart_socket_enqueue(asocket* s, apacket* p) {
}
#endif
if (!(s->transport) || (s->transport->connection_state == kCsOffline)) {
if (!s->transport) {
SendFail(s->peer->fd, "device offline (no transport)");
goto fail;
} else if (s->transport->connection_state == kCsOffline) {
/* if there's no remote we fail the connection
** right here and terminate it
*/
SendFail(s->peer->fd, "device offline (x)");
SendFail(s->peer->fd, "device offline (transport offline)");
goto fail;
}

View file

@ -908,12 +908,18 @@ std::string list_transports(bool long_listing) {
return result;
}
void close_usb_devices(std::function<bool(const atransport*)> predicate) {
std::lock_guard<std::mutex> lock(transport_lock);
for (auto& t : transport_list) {
if (predicate(t)) {
t->Kick();
}
}
}
/* hack for osx */
void close_usb_devices() {
std::lock_guard<std::mutex> lock(transport_lock);
for (const auto& t : transport_list) {
t->Kick();
}
close_usb_devices([](const atransport*) { return true; });
}
#endif // ADB_HOST

View file

@ -20,6 +20,7 @@
#include <sys/types.h>
#include <deque>
#include <functional>
#include <list>
#include <memory>
#include <string>
@ -199,8 +200,8 @@ void unregister_usb_transport(usb_handle* usb);
int check_header(apacket* p, atransport* t);
int check_data(apacket* p);
/* for MacOS X cleanup */
void close_usb_devices();
void close_usb_devices(std::function<bool(const atransport*)> predicate);
void send_packet(apacket* p, atransport* t);