• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

system/corennnnn


Commit MetaInfo

Revisionfb989b4313f6d5ce40376fe8165ba5e7f5e137d5 (tree)
Time2016-07-20 18:01:29
AuthorLeo Sartre <leox.sartre@inte...>
CommiterChih-Wei Huang

Log Message

adb host: add device state in "adb wait-for-*"

The current implementation of the host commands "adb wait-for-*" allows
to specify only the transport layer (local, usb or any).
This patch allows the specification of the expected device state
(bootloader, recovery, device or sideload), this is usefull for
scripting purposes.

Use case:
$ adb reboot sideload-auto-reboot
$ adb wait-for-usb-sideload && adb sideload package.zip

This is a port of: https://android-review.googlesource.com/#/c/184290

Change-Id: I56a64b2d0f089cf1eb77424956296d660132b629
Signed-off-by: Leo Sartre <leox.sartre@intel.com>
Tracked-On: https://jira01.devtools.intel.com/browse/OAM-15622
Reviewed-on: https://android.intel.com:443/450503

Change Summary

Incremental Difference

--- a/adb/commandline.cpp
+++ b/adb/commandline.cpp
@@ -198,7 +198,10 @@ static void help() {
198198 " adb version - show version num\n"
199199 "\n"
200200 "scripting:\n"
201- " adb wait-for-device - block until device is online\n"
201+ " adb wait-for-<transport>-<state>\n"
202+ " - wait for device to be in the given state:\n"
203+ " device, recovery, sideload, or bootloader\n"
204+ " Transport is: usb, local or any\n"
202205 " adb start-server - ensure that there is a server running\n"
203206 " adb kill-server - kill the server if it is running\n"
204207 " adb get-state - prints: offline | bootloader | device\n"
@@ -674,19 +677,49 @@ static int ppp(int argc, const char** argv) {
674677 #endif /* !defined(_WIN32) */
675678 }
676679
680+static bool check_wait_for_device_syntax(const char* service) {
681+ // TODO: when we have libc++ for Windows, use a regular expression instead.
682+ // wait-for-((any|local|usb)-)?(bootloader|device|recovery|sideload)
683+
684+ char type[20];
685+ char state[20];
686+ int length = 0;
687+ if (sscanf(service, "wait-for-%20[a-z]-%20[a-z]%n", type, state, &length) < 2 ||
688+ length != static_cast<int>(strlen(service))) {
689+ fprintf(stderr, "adb: couldn't parse 'wait-for' command: %s\n", service);
690+ return false;
691+ }
692+
693+ if (strcmp(type, "any") != 0 && strcmp(type, "local") != 0 && strcmp(type, "usb") != 0) {
694+ fprintf(stderr, "adb: unknown type %s; expected 'any', 'local', or 'usb'\n", type);
695+ return false;
696+ }
697+ if (strcmp(state, "bootloader") != 0 && strcmp(state, "device") != 0 &&
698+ strcmp(state, "recovery") != 0 && strcmp(state, "sideload") != 0) {
699+ fprintf(stderr, "adb: unknown state %s; "
700+ "expected 'bootloader', 'device', 'recovery', or 'sideload'\n", state);
701+ return false;
702+ }
703+ return true;
704+}
705+
677706 static bool wait_for_device(const char* service, transport_type t, const char* serial) {
678707 // Was the caller vague about what they'd like us to wait for?
679708 // If so, check they weren't more specific in their choice of transport type.
680709 if (strcmp(service, "wait-for-device") == 0) {
681710 if (t == kTransportUsb) {
682- service = "wait-for-usb";
711+ service = "wait-for-usb-device";
683712 } else if (t == kTransportLocal) {
684- service = "wait-for-local";
713+ service = "wait-for-local-device";
685714 } else {
686- service = "wait-for-any";
715+ service = "wait-for-any-device";
687716 }
688717 }
689718
719+ if (!check_wait_for_device_syntax(service)) {
720+ return false;
721+ }
722+
690723 std::string cmd = format_host_command(service, t, serial);
691724 std::string error;
692725 if (adb_command(cmd, &error)) {
--- a/adb/services.cpp
+++ b/adb/services.cpp
@@ -649,11 +649,13 @@ asocket* host_service_to_socket(const char* name, const char *serial)
649649 {
650650 if (!strcmp(name,"track-devices")) {
651651 return create_device_tracker();
652- } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
653- auto sinfo = reinterpret_cast<state_info*>(malloc(sizeof(state_info)));
652+ } else if (android::base::StartsWith(name, "wait-for-")) {
653+ name += strlen("wait-for-");
654+
655+ std::unique_ptr<state_info> sinfo(new state_info);
654656 if (sinfo == nullptr) {
655657 fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno));
656- return NULL;
658+ return nullptr;
657659 }
658660
659661 if (serial)
@@ -661,29 +663,38 @@ asocket* host_service_to_socket(const char* name, const char *serial)
661663 else
662664 sinfo->serial = NULL;
663665
664- name += strlen("wait-for-");
665-
666- if (!strncmp(name, "local", strlen("local"))) {
666+ if (android::base::StartsWith(name, "local")) {
667+ name += strlen("local");
667668 sinfo->transport = kTransportLocal;
668- sinfo->state = CS_DEVICE;
669- } else if (!strncmp(name, "usb", strlen("usb"))) {
669+ } else if (android::base::StartsWith(name, "usb")) {
670+ name += strlen("usb");
670671 sinfo->transport = kTransportUsb;
671- sinfo->state = CS_DEVICE;
672- } else if (!strncmp(name, "any", strlen("any"))) {
672+ } else if (android::base::StartsWith(name, "any")) {
673+ name += strlen("any");
673674 sinfo->transport = kTransportAny;
675+ } else {
676+ return nullptr;
677+ }
678+
679+ if (!strcmp(name, "-device")) {
674680 sinfo->state = CS_DEVICE;
681+ } else if (!strcmp(name, "-recovery")) {
682+ sinfo->state = CS_RECOVERY;
683+ } else if (!strcmp(name, "-sideload")) {
684+ sinfo->state = CS_SIDELOAD;
685+ } else if (!strcmp(name, "-bootloader")) {
686+ sinfo->state = CS_BOOTLOADER;
675687 } else {
676- free(sinfo);
677- return NULL;
688+ return nullptr;
678689 }
679690
680- int fd = create_service_thread(wait_for_state, sinfo);
691+ int fd = create_service_thread(wait_for_state, sinfo.release());
681692 return create_local_socket(fd);
682693 } else if (!strncmp(name, "connect:", 8)) {
683694 const char *host = name + 8;
684695 int fd = create_service_thread(connect_service, (void *)host);
685696 return create_local_socket(fd);
686697 }
687- return NULL;
698+ return nullptr;
688699 }
689700 #endif /* ADB_HOST */