system/corennnnn
Revision | fb989b4313f6d5ce40376fe8165ba5e7f5e137d5 (tree) |
---|---|
Time | 2016-07-20 18:01:29 |
Author | Leo Sartre <leox.sartre@inte...> |
Commiter | Chih-Wei Huang |
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
@@ -198,7 +198,10 @@ static void help() { | ||
198 | 198 | " adb version - show version num\n" |
199 | 199 | "\n" |
200 | 200 | "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" | |
202 | 205 | " adb start-server - ensure that there is a server running\n" |
203 | 206 | " adb kill-server - kill the server if it is running\n" |
204 | 207 | " adb get-state - prints: offline | bootloader | device\n" |
@@ -674,19 +677,49 @@ static int ppp(int argc, const char** argv) { | ||
674 | 677 | #endif /* !defined(_WIN32) */ |
675 | 678 | } |
676 | 679 | |
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 | + | |
677 | 706 | static bool wait_for_device(const char* service, transport_type t, const char* serial) { |
678 | 707 | // Was the caller vague about what they'd like us to wait for? |
679 | 708 | // If so, check they weren't more specific in their choice of transport type. |
680 | 709 | if (strcmp(service, "wait-for-device") == 0) { |
681 | 710 | if (t == kTransportUsb) { |
682 | - service = "wait-for-usb"; | |
711 | + service = "wait-for-usb-device"; | |
683 | 712 | } else if (t == kTransportLocal) { |
684 | - service = "wait-for-local"; | |
713 | + service = "wait-for-local-device"; | |
685 | 714 | } else { |
686 | - service = "wait-for-any"; | |
715 | + service = "wait-for-any-device"; | |
687 | 716 | } |
688 | 717 | } |
689 | 718 | |
719 | + if (!check_wait_for_device_syntax(service)) { | |
720 | + return false; | |
721 | + } | |
722 | + | |
690 | 723 | std::string cmd = format_host_command(service, t, serial); |
691 | 724 | std::string error; |
692 | 725 | if (adb_command(cmd, &error)) { |
@@ -649,11 +649,13 @@ asocket* host_service_to_socket(const char* name, const char *serial) | ||
649 | 649 | { |
650 | 650 | if (!strcmp(name,"track-devices")) { |
651 | 651 | 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); | |
654 | 656 | if (sinfo == nullptr) { |
655 | 657 | fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno)); |
656 | - return NULL; | |
658 | + return nullptr; | |
657 | 659 | } |
658 | 660 | |
659 | 661 | if (serial) |
@@ -661,29 +663,38 @@ asocket* host_service_to_socket(const char* name, const char *serial) | ||
661 | 663 | else |
662 | 664 | sinfo->serial = NULL; |
663 | 665 | |
664 | - name += strlen("wait-for-"); | |
665 | - | |
666 | - if (!strncmp(name, "local", strlen("local"))) { | |
666 | + if (android::base::StartsWith(name, "local")) { | |
667 | + name += strlen("local"); | |
667 | 668 | 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"); | |
670 | 671 | 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"); | |
673 | 674 | sinfo->transport = kTransportAny; |
675 | + } else { | |
676 | + return nullptr; | |
677 | + } | |
678 | + | |
679 | + if (!strcmp(name, "-device")) { | |
674 | 680 | 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; | |
675 | 687 | } else { |
676 | - free(sinfo); | |
677 | - return NULL; | |
688 | + return nullptr; | |
678 | 689 | } |
679 | 690 | |
680 | - int fd = create_service_thread(wait_for_state, sinfo); | |
691 | + int fd = create_service_thread(wait_for_state, sinfo.release()); | |
681 | 692 | return create_local_socket(fd); |
682 | 693 | } else if (!strncmp(name, "connect:", 8)) { |
683 | 694 | const char *host = name + 8; |
684 | 695 | int fd = create_service_thread(connect_service, (void *)host); |
685 | 696 | return create_local_socket(fd); |
686 | 697 | } |
687 | - return NULL; | |
698 | + return nullptr; | |
688 | 699 | } |
689 | 700 | #endif /* ADB_HOST */ |