Loading adb/adb_client.cpp +24 −0 Original line number Diff line number Diff line Loading @@ -295,3 +295,27 @@ bool adb_query(const std::string& service, std::string* result, std::string* err adb_close(fd); return true; } std::string format_host_command(const char* command, TransportType type, const char* serial) { if (serial) { return android::base::StringPrintf("host-serial:%s:%s", serial, command); } const char* prefix = "host"; if (type == kTransportUsb) { prefix = "host-usb"; } else if (type == kTransportLocal) { prefix = "host-local"; } return android::base::StringPrintf("%s:%s", prefix, command); } bool adb_get_feature_set(FeatureSet* feature_set, std::string* error) { std::string result; if (adb_query(format_host_command("features", __adb_transport, __adb_serial), &result, error)) { *feature_set = StringToFeatureSet(result); return true; } feature_set->clear(); return false; } adb/adb_client.h +17 −7 Original line number Diff line number Diff line Loading @@ -18,13 +18,14 @@ #define _ADB_CLIENT_H_ #include "adb.h" #include "transport.h" #include <string> // Connect to adb, connect to the named service, and return a valid fd for // interacting with that service upon success or a negative number on failure. int adb_connect(const std::string& service, std::string* error); int _adb_connect(const std::string& service, std::string* error); int adb_connect(const std::string& service, std::string* _Nonnull error); int _adb_connect(const std::string& service, std::string* _Nonnull error); // Connect to adb, connect to the named service, returns true if the connection // succeeded AND the service returned OKAY. Outputs any returned error otherwise. Loading @@ -32,24 +33,33 @@ bool adb_command(const std::string& service); // Connects to the named adb service and fills 'result' with the response. // Returns true on success; returns false and fills 'error' on failure. bool adb_query(const std::string& service, std::string* result, std::string* error); bool adb_query(const std::string& service, std::string* _Nonnull result, std::string* _Nonnull error); // Set the preferred transport to connect to. void adb_set_transport(TransportType type, const char* serial); void adb_set_transport(TransportType type, const char* _Nullable serial); // Set TCP specifics of the transport to use. void adb_set_tcp_specifics(int server_port); // Set TCP Hostname of the transport to use. void adb_set_tcp_name(const char* hostname); void adb_set_tcp_name(const char* _Nullable hostname); // Send commands to the current emulator instance. Will fail if there is not // exactly one emulator connected (or if you use -s <serial> with a <serial> // that does not designate an emulator). int adb_send_emulator_command(int argc, const char** argv, const char* serial); int adb_send_emulator_command(int argc, const char* _Nonnull* _Nonnull argv, const char* _Nullable serial); // Reads a standard adb status response (OKAY|FAIL) and returns true in the // event of OKAY, false in the event of FAIL or protocol error. bool adb_status(int fd, std::string* error); bool adb_status(int fd, std::string* _Nonnull error); // Create a host command corresponding to selected transport type/serial. std::string format_host_command(const char* _Nonnull command, TransportType type, const char* _Nullable serial); // Get the feature set of the current preferred transport. bool adb_get_feature_set(FeatureSet* _Nonnull feature_set, std::string* _Nonnull error); #endif adb/commandline.cpp +17 −51 Original line number Diff line number Diff line Loading @@ -428,49 +428,6 @@ static void copy_to_file(int inFd, int outFd) { free(buf); } static std::string format_host_command(const char* command, TransportType type, const char* serial) { if (serial) { return android::base::StringPrintf("host-serial:%s:%s", serial, command); } const char* prefix = "host"; if (type == kTransportUsb) { prefix = "host-usb"; } else if (type == kTransportLocal) { prefix = "host-local"; } return android::base::StringPrintf("%s:%s", prefix, command); } namespace { enum class ErrorAction { kPrintToStderr, kDoNotPrint }; } // namespace // Fills |feature_set| using the target indicated by |transport_type| and |serial|. Returns false // and clears |feature_set| on failure. |error_action| selects whether to also print error messages // on failure. static bool GetFeatureSet(TransportType transport_type, const char* serial, FeatureSet* feature_set, ErrorAction error_action) { std::string result, error; if (adb_query(format_host_command("features", transport_type, serial), &result, &error)) { *feature_set = StringToFeatureSet(result); return true; } if (error_action == ErrorAction::kPrintToStderr) { fprintf(stderr, "error: %s\n", error.c_str()); } feature_set->clear(); return false; } static void send_window_size_change(int fd, std::unique_ptr<ShellProtocol>& shell) { // Old devices can't handle window size changes. if (shell == nullptr) return; Loading Loading @@ -738,10 +695,12 @@ static int RemoteShell(bool use_shell_protocol, const std::string& type_arg, return exit_code; } static int adb_shell(int argc, const char** argv, TransportType transport_type, const char* serial) { static int adb_shell(int argc, const char** argv) { FeatureSet features; if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) { std::string error; if (!adb_get_feature_set(&features, &error)) { fprintf(stderr, "error: %s\n", error.c_str()); return 1; } Loading Loading @@ -1129,7 +1088,8 @@ static int send_shell_command(TransportType transport_type, const char* serial, // Use shell protocol if it's supported and the caller doesn't explicitly disable it. if (!disable_shell_protocol) { FeatureSet features; if (GetFeatureSet(transport_type, serial, &features, ErrorAction::kDoNotPrint)) { std::string error; if (adb_get_feature_set(&features, &error)) { use_shell_protocol = CanUseFeature(features, kFeatureShell2); } else { // Device was unreachable. Loading Loading @@ -1608,7 +1568,7 @@ int adb_commandline(int argc, const char **argv) { return adb_send_emulator_command(argc, argv, serial); } else if (!strcmp(argv[0], "shell")) { return adb_shell(argc, argv, transport_type, serial); return adb_shell(argc, argv); } else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) { int exec_in = !strcmp(argv[0], "exec-in"); Loading Loading @@ -1764,7 +1724,9 @@ int adb_commandline(int argc, const char **argv) { else if (!strcmp(argv[0], "install")) { if (argc < 2) return usage(); FeatureSet features; if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) { std::string error; if (!adb_get_feature_set(&features, &error)) { fprintf(stderr, "error: %s\n", error.c_str()); return 1; } Loading @@ -1780,7 +1742,9 @@ int adb_commandline(int argc, const char **argv) { else if (!strcmp(argv[0], "uninstall")) { if (argc < 2) return usage(); FeatureSet features; if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) { std::string error; if (!adb_get_feature_set(&features, &error)) { fprintf(stderr, "error: %s\n", error.c_str()); return 1; } Loading Loading @@ -1883,7 +1847,9 @@ int adb_commandline(int argc, const char **argv) { else if (!strcmp(argv[0], "features")) { // Only list the features common to both the adb client and the device. FeatureSet features; if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) { std::string error; if (!adb_get_feature_set(&features, &error)) { fprintf(stderr, "error: %s\n", error.c_str()); return 1; } Loading adb/sysdeps.h +4 −0 Original line number Diff line number Diff line Loading @@ -61,6 +61,10 @@ #ifdef _WIN32 // Clang-only nullability specifiers #define _Nonnull #define _Nullable #include <ctype.h> #include <direct.h> #include <dirent.h> Loading Loading
adb/adb_client.cpp +24 −0 Original line number Diff line number Diff line Loading @@ -295,3 +295,27 @@ bool adb_query(const std::string& service, std::string* result, std::string* err adb_close(fd); return true; } std::string format_host_command(const char* command, TransportType type, const char* serial) { if (serial) { return android::base::StringPrintf("host-serial:%s:%s", serial, command); } const char* prefix = "host"; if (type == kTransportUsb) { prefix = "host-usb"; } else if (type == kTransportLocal) { prefix = "host-local"; } return android::base::StringPrintf("%s:%s", prefix, command); } bool adb_get_feature_set(FeatureSet* feature_set, std::string* error) { std::string result; if (adb_query(format_host_command("features", __adb_transport, __adb_serial), &result, error)) { *feature_set = StringToFeatureSet(result); return true; } feature_set->clear(); return false; }
adb/adb_client.h +17 −7 Original line number Diff line number Diff line Loading @@ -18,13 +18,14 @@ #define _ADB_CLIENT_H_ #include "adb.h" #include "transport.h" #include <string> // Connect to adb, connect to the named service, and return a valid fd for // interacting with that service upon success or a negative number on failure. int adb_connect(const std::string& service, std::string* error); int _adb_connect(const std::string& service, std::string* error); int adb_connect(const std::string& service, std::string* _Nonnull error); int _adb_connect(const std::string& service, std::string* _Nonnull error); // Connect to adb, connect to the named service, returns true if the connection // succeeded AND the service returned OKAY. Outputs any returned error otherwise. Loading @@ -32,24 +33,33 @@ bool adb_command(const std::string& service); // Connects to the named adb service and fills 'result' with the response. // Returns true on success; returns false and fills 'error' on failure. bool adb_query(const std::string& service, std::string* result, std::string* error); bool adb_query(const std::string& service, std::string* _Nonnull result, std::string* _Nonnull error); // Set the preferred transport to connect to. void adb_set_transport(TransportType type, const char* serial); void adb_set_transport(TransportType type, const char* _Nullable serial); // Set TCP specifics of the transport to use. void adb_set_tcp_specifics(int server_port); // Set TCP Hostname of the transport to use. void adb_set_tcp_name(const char* hostname); void adb_set_tcp_name(const char* _Nullable hostname); // Send commands to the current emulator instance. Will fail if there is not // exactly one emulator connected (or if you use -s <serial> with a <serial> // that does not designate an emulator). int adb_send_emulator_command(int argc, const char** argv, const char* serial); int adb_send_emulator_command(int argc, const char* _Nonnull* _Nonnull argv, const char* _Nullable serial); // Reads a standard adb status response (OKAY|FAIL) and returns true in the // event of OKAY, false in the event of FAIL or protocol error. bool adb_status(int fd, std::string* error); bool adb_status(int fd, std::string* _Nonnull error); // Create a host command corresponding to selected transport type/serial. std::string format_host_command(const char* _Nonnull command, TransportType type, const char* _Nullable serial); // Get the feature set of the current preferred transport. bool adb_get_feature_set(FeatureSet* _Nonnull feature_set, std::string* _Nonnull error); #endif
adb/commandline.cpp +17 −51 Original line number Diff line number Diff line Loading @@ -428,49 +428,6 @@ static void copy_to_file(int inFd, int outFd) { free(buf); } static std::string format_host_command(const char* command, TransportType type, const char* serial) { if (serial) { return android::base::StringPrintf("host-serial:%s:%s", serial, command); } const char* prefix = "host"; if (type == kTransportUsb) { prefix = "host-usb"; } else if (type == kTransportLocal) { prefix = "host-local"; } return android::base::StringPrintf("%s:%s", prefix, command); } namespace { enum class ErrorAction { kPrintToStderr, kDoNotPrint }; } // namespace // Fills |feature_set| using the target indicated by |transport_type| and |serial|. Returns false // and clears |feature_set| on failure. |error_action| selects whether to also print error messages // on failure. static bool GetFeatureSet(TransportType transport_type, const char* serial, FeatureSet* feature_set, ErrorAction error_action) { std::string result, error; if (adb_query(format_host_command("features", transport_type, serial), &result, &error)) { *feature_set = StringToFeatureSet(result); return true; } if (error_action == ErrorAction::kPrintToStderr) { fprintf(stderr, "error: %s\n", error.c_str()); } feature_set->clear(); return false; } static void send_window_size_change(int fd, std::unique_ptr<ShellProtocol>& shell) { // Old devices can't handle window size changes. if (shell == nullptr) return; Loading Loading @@ -738,10 +695,12 @@ static int RemoteShell(bool use_shell_protocol, const std::string& type_arg, return exit_code; } static int adb_shell(int argc, const char** argv, TransportType transport_type, const char* serial) { static int adb_shell(int argc, const char** argv) { FeatureSet features; if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) { std::string error; if (!adb_get_feature_set(&features, &error)) { fprintf(stderr, "error: %s\n", error.c_str()); return 1; } Loading Loading @@ -1129,7 +1088,8 @@ static int send_shell_command(TransportType transport_type, const char* serial, // Use shell protocol if it's supported and the caller doesn't explicitly disable it. if (!disable_shell_protocol) { FeatureSet features; if (GetFeatureSet(transport_type, serial, &features, ErrorAction::kDoNotPrint)) { std::string error; if (adb_get_feature_set(&features, &error)) { use_shell_protocol = CanUseFeature(features, kFeatureShell2); } else { // Device was unreachable. Loading Loading @@ -1608,7 +1568,7 @@ int adb_commandline(int argc, const char **argv) { return adb_send_emulator_command(argc, argv, serial); } else if (!strcmp(argv[0], "shell")) { return adb_shell(argc, argv, transport_type, serial); return adb_shell(argc, argv); } else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) { int exec_in = !strcmp(argv[0], "exec-in"); Loading Loading @@ -1764,7 +1724,9 @@ int adb_commandline(int argc, const char **argv) { else if (!strcmp(argv[0], "install")) { if (argc < 2) return usage(); FeatureSet features; if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) { std::string error; if (!adb_get_feature_set(&features, &error)) { fprintf(stderr, "error: %s\n", error.c_str()); return 1; } Loading @@ -1780,7 +1742,9 @@ int adb_commandline(int argc, const char **argv) { else if (!strcmp(argv[0], "uninstall")) { if (argc < 2) return usage(); FeatureSet features; if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) { std::string error; if (!adb_get_feature_set(&features, &error)) { fprintf(stderr, "error: %s\n", error.c_str()); return 1; } Loading Loading @@ -1883,7 +1847,9 @@ int adb_commandline(int argc, const char **argv) { else if (!strcmp(argv[0], "features")) { // Only list the features common to both the adb client and the device. FeatureSet features; if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) { std::string error; if (!adb_get_feature_set(&features, &error)) { fprintf(stderr, "error: %s\n", error.c_str()); return 1; } Loading
adb/sysdeps.h +4 −0 Original line number Diff line number Diff line Loading @@ -61,6 +61,10 @@ #ifdef _WIN32 // Clang-only nullability specifiers #define _Nonnull #define _Nullable #include <ctype.h> #include <direct.h> #include <dirent.h> Loading