diff --git a/adb/adb.cpp b/adb/adb.cpp index 24d4292d0..7dff1b8d2 100644 --- a/adb/adb.cpp +++ b/adb/adb.cpp @@ -1243,11 +1243,7 @@ HostRequestResult handle_host_request(std::string_view service, TransportType ty // TODO: Switch handle_forward_request to string_view. std::string service_str(service); if (handle_forward_request( - service_str.c_str(), - [=](std::string* error) { - return acquire_one_transport(type, serial, transport_id, nullptr, error); - }, - reply_fd)) { + service_str.c_str(), [=](std::string* error) { return s->transport; }, reply_fd)) { return HostRequestResult::Handled; } diff --git a/adb/client/adb_client.cpp b/adb/client/adb_client.cpp index 7e408a87a..0f621ebb8 100644 --- a/adb/client/adb_client.cpp +++ b/adb/client/adb_client.cpp @@ -149,7 +149,8 @@ bool adb_status(borrowed_fd fd, std::string* error) { return false; } -static int _adb_connect(std::string_view service, TransportId* transport, std::string* error) { +static int _adb_connect(std::string_view service, TransportId* transport, std::string* error, + bool force_switch = false) { LOG(DEBUG) << "_adb_connect: " << service; if (service.empty() || service.size() > MAX_PAYLOAD) { *error = android::base::StringPrintf("bad service name length (%zd)", service.size()); @@ -164,7 +165,7 @@ static int _adb_connect(std::string_view service, TransportId* transport, std::s return -2; } - if (!service.starts_with("host")) { + if (!service.starts_with("host") || force_switch) { std::optional transport_result = switch_socket_transport(fd.get(), error); if (!transport_result) { return -1; @@ -323,7 +324,8 @@ bool adb_check_server_version(std::string* error) { return result; } -int adb_connect(TransportId* transport, std::string_view service, std::string* error) { +int adb_connect(TransportId* transport, std::string_view service, std::string* error, + bool force_switch_device) { LOG(DEBUG) << "adb_connect: service: " << service; // Query the adb server's version. @@ -336,7 +338,7 @@ int adb_connect(TransportId* transport, std::string_view service, std::string* e return 0; } - unique_fd fd(_adb_connect(service, transport, error)); + unique_fd fd(_adb_connect(service, transport, error, force_switch_device)); if (fd == -1) { D("_adb_connect error: %s", error->c_str()); } else if(fd == -2) { diff --git a/adb/client/adb_client.h b/adb/client/adb_client.h index fe1e584a5..ba530418d 100644 --- a/adb/client/adb_client.h +++ b/adb/client/adb_client.h @@ -34,7 +34,11 @@ bool adb_check_server_version(std::string* _Nonnull error); int adb_connect(std::string_view service, std::string* _Nonnull error); // Same as above, except returning the TransportId for the service that we've connected to. -int adb_connect(TransportId* _Nullable id, std::string_view service, std::string* _Nonnull error); +// force_switch_device forces the function to attempt to select a device, even if the service +// string appears to be a host: service (for use with host services that are device specific, like +// forward). +int adb_connect(TransportId* _Nullable id, std::string_view service, std::string* _Nonnull error, + bool force_switch_device = false); // Kill the currently running adb server, if it exists. bool adb_kill_server(); diff --git a/adb/client/commandline.cpp b/adb/client/commandline.cpp index 48853b7ff..7d0eb40dc 100644 --- a/adb/client/commandline.cpp +++ b/adb/client/commandline.cpp @@ -1739,41 +1739,33 @@ int adb_commandline(int argc, const char** argv) { // Determine the for this command. std::string host_prefix; if (reverse) { - host_prefix = "reverse"; + host_prefix = "reverse:"; } else { - if (serial) { - host_prefix = android::base::StringPrintf("host-serial:%s", serial); - } else if (transport_type == kTransportUsb) { - host_prefix = "host-usb"; - } else if (transport_type == kTransportLocal) { - host_prefix = "host-local"; - } else { - host_prefix = "host"; - } + host_prefix = "host:"; } std::string cmd, error_message; if (strcmp(argv[0], "--list") == 0) { if (argc != 1) error_exit("--list doesn't take any arguments"); - return adb_query_command(host_prefix + ":list-forward"); + return adb_query_command(host_prefix + "list-forward"); } else if (strcmp(argv[0], "--remove-all") == 0) { if (argc != 1) error_exit("--remove-all doesn't take any arguments"); - cmd = host_prefix + ":killforward-all"; + cmd = "killforward-all"; } else if (strcmp(argv[0], "--remove") == 0) { // forward --remove if (argc != 2) error_exit("--remove requires an argument"); - cmd = host_prefix + ":killforward:" + argv[1]; + cmd = std::string("killforward:") + argv[1]; } else if (strcmp(argv[0], "--no-rebind") == 0) { // forward --no-rebind if (argc != 3) error_exit("--no-rebind takes two arguments"); if (forward_targets_are_valid(argv[1], argv[2], &error_message)) { - cmd = host_prefix + ":forward:norebind:" + argv[1] + ";" + argv[2]; + cmd = std::string("forward:norebind:") + argv[1] + ";" + argv[2]; } } else { // forward if (argc != 2) error_exit("forward takes two arguments"); if (forward_targets_are_valid(argv[0], argv[1], &error_message)) { - cmd = host_prefix + ":forward:" + argv[0] + ";" + argv[1]; + cmd = std::string("forward:") + argv[0] + ";" + argv[1]; } } @@ -1781,7 +1773,7 @@ int adb_commandline(int argc, const char** argv) { error_exit("error: %s", error_message.c_str()); } - unique_fd fd(adb_connect(cmd, &error_message)); + unique_fd fd(adb_connect(nullptr, host_prefix + cmd, &error_message, true)); if (fd < 0 || !adb_status(fd.get(), &error_message)) { error_exit("error: %s", error_message.c_str()); }