Merge changes Ibc8038b1,I1876ecd7,I57d70f8b

* changes:
  adb: after `adb root`, wait for the device in any state.
  adb: add support for 'wait-for-any'.
  adb: make 'wait-for-{recovery,sideload,bootloader} work.
This commit is contained in:
Treehugger Robot 2016-04-13 20:25:53 +00:00 committed by Gerrit Code Review
commit 3e0eaa4103
2 changed files with 25 additions and 33 deletions

View file

@ -1025,50 +1025,40 @@ static int ppp(int argc, const char** argv) {
#endif /* !defined(_WIN32) */ #endif /* !defined(_WIN32) */
} }
static bool check_wait_for_device_syntax(const char* service) { static bool wait_for_device(const char* service, TransportType t, const char* serial) {
// TODO: when we have libc++ for Windows, use a regular expression instead. std::vector<std::string> components = android::base::Split(service, "-");
// wait-for-((any|local|usb)-)?(bootloader|device|recovery|sideload) if (components.size() < 3 || components.size() > 4) {
char type[20 + 1]; // sscanf's %20[...] doesn't include the NUL.
char state[20 + 1];
int length = 0;
if (sscanf(service, "wait-for-%20[a-z]-%20[a-z]%n", type, state, &length) < 2 ||
length != static_cast<int>(strlen(service))) {
fprintf(stderr, "adb: couldn't parse 'wait-for' command: %s\n", service); fprintf(stderr, "adb: couldn't parse 'wait-for' command: %s\n", service);
return false; return false;
} }
if (strcmp(type, "any") != 0 && strcmp(type, "local") != 0 && strcmp(type, "usb") != 0) {
fprintf(stderr, "adb: unknown type %s; expected 'any', 'local', or 'usb'\n", type);
return false;
}
if (strcmp(state, "bootloader") != 0 && strcmp(state, "device") != 0 &&
strcmp(state, "recovery") != 0 && strcmp(state, "sideload") != 0) {
fprintf(stderr, "adb: unknown state %s; "
"expected 'bootloader', 'device', 'recovery', or 'sideload'\n", state);
return false;
}
return true;
}
static bool wait_for_device(const char* service, TransportType t, const char* serial) {
// Was the caller vague about what they'd like us to wait for? // Was the caller vague about what they'd like us to wait for?
// If so, check they weren't more specific in their choice of transport type. // If so, check they weren't more specific in their choice of transport type.
if (strcmp(service, "wait-for-device") == 0) { if (components.size() == 3) {
auto it = components.begin() + 2;
if (t == kTransportUsb) { if (t == kTransportUsb) {
service = "wait-for-usb-device"; components.insert(it, "usb");
} else if (t == kTransportLocal) { } else if (t == kTransportLocal) {
service = "wait-for-local-device"; components.insert(it, "local");
} else { } else {
service = "wait-for-any-device"; components.insert(it, "any");
} }
} } else if (components[2] != "any" && components[2] != "local" && components[2] != "usb") {
fprintf(stderr, "adb: unknown type %s; expected 'any', 'local', or 'usb'\n",
if (!check_wait_for_device_syntax(service)) { components[2].c_str());
return false; return false;
} }
std::string cmd = format_host_command(service, t, serial); if (components[3] != "any" && components[3] != "bootloader" && components[3] != "device" &&
components[3] != "recovery" && components[3] != "sideload") {
fprintf(stderr,
"adb: unknown state %s; "
"expected 'any', 'bootloader', 'device', 'recovery', or 'sideload'\n",
components[3].c_str());
return false;
}
std::string cmd = format_host_command(android::base::Join(components, "-").c_str(), t, serial);
return adb_command(cmd); return adb_command(cmd);
} }
@ -1114,7 +1104,7 @@ static bool adb_root(const char* command) {
TransportType type; TransportType type;
const char* serial; const char* serial;
adb_get_transport(&type, &serial); adb_get_transport(&type, &serial);
return wait_for_device("wait-for-device", type, serial); return wait_for_device("wait-for-any", type, serial);
} }
// Connects to the device "shell" service with |command| and prints the // Connects to the device "shell" service with |command| and prints the

View file

@ -370,7 +370,7 @@ static void wait_for_state(int fd, void* data) {
std::string error = "unknown error"; std::string error = "unknown error";
const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : NULL; const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : NULL;
atransport* t = acquire_one_transport(sinfo->transport_type, serial, &is_ambiguous, &error); atransport* t = acquire_one_transport(sinfo->transport_type, serial, &is_ambiguous, &error);
if (t != nullptr && t->connection_state == sinfo->state) { if (t != nullptr && (sinfo->state == kCsAny || sinfo->state == t->connection_state)) {
SendOkay(fd); SendOkay(fd);
break; break;
} else if (!is_ambiguous) { } else if (!is_ambiguous) {
@ -534,6 +534,8 @@ asocket* host_service_to_socket(const char* name, const char* serial) {
sinfo->state = kCsSideload; sinfo->state = kCsSideload;
} else if (!strcmp(name, "-bootloader")) { } else if (!strcmp(name, "-bootloader")) {
sinfo->state = kCsBootloader; sinfo->state = kCsBootloader;
} else if (!strcmp(name, "-any")) {
sinfo->state = kCsAny;
} else { } else {
return nullptr; return nullptr;
} }