adb: switch host_service_to_socket to string_view.
Test: manual
Change-Id: I09b83cc82edb10c1a51ecc5ebb8d9fe5294222c3
diff --git a/adb/adb.h b/adb/adb.h
index 89d7663..f575adb 100644
--- a/adb/adb.h
+++ b/adb/adb.h
@@ -145,7 +145,8 @@
#endif
#if ADB_HOST
-asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id);
+asocket* host_service_to_socket(std::string_view name, std::string_view serial,
+ TransportId transport_id);
#endif
#if !ADB_HOST
diff --git a/adb/services.cpp b/adb/services.cpp
index 0061f0e..1ed7182 100644
--- a/adb/services.cpp
+++ b/adb/services.cpp
@@ -25,6 +25,7 @@
#include <string.h>
#include <thread>
+
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <cutils/sockets.h>
@@ -63,11 +64,11 @@
adb_setsockopt(s[0], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf));
adb_setsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf));
}
-#endif // !ADB_HOST
+#endif // !ADB_HOST
std::thread(service_bootstrap_func, service_name, func, unique_fd(s[1])).detach();
- D("service thread started, %d:%d",s[0], s[1]);
+ D("service thread started, %d:%d", s[0], s[1]);
return unique_fd(s[0]);
}
@@ -112,7 +113,7 @@
SendOkay(fd);
break;
} else if (!is_ambiguous) {
- adb_pollfd pfd = {.fd = fd, .events = POLLIN };
+ adb_pollfd pfd = {.fd = fd, .events = POLLIN};
int rc = adb_poll(&pfd, 1, 1000);
if (rc < 0) {
SendFail(fd, error);
@@ -187,45 +188,41 @@
#endif
#if ADB_HOST
-asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id) {
- if (!strcmp(name,"track-devices")) {
+asocket* host_service_to_socket(std::string_view name, std::string_view serial,
+ TransportId transport_id) {
+ if (name == "track-devices") {
return create_device_tracker(false);
- } else if (!strcmp(name, "track-devices-l")) {
+ } else if (name == "track-devices-l") {
return create_device_tracker(true);
- } else if (android::base::StartsWith(name, "wait-for-")) {
- name += strlen("wait-for-");
-
+ } else if (ConsumePrefix(&name, "wait-for-")) {
std::shared_ptr<state_info> sinfo = std::make_shared<state_info>();
if (sinfo == nullptr) {
fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno));
return nullptr;
}
- if (serial) sinfo->serial = serial;
+ sinfo->serial = serial;
sinfo->transport_id = transport_id;
- if (android::base::StartsWith(name, "local")) {
- name += strlen("local");
+ if (ConsumePrefix(&name, "local")) {
sinfo->transport_type = kTransportLocal;
- } else if (android::base::StartsWith(name, "usb")) {
- name += strlen("usb");
+ } else if (ConsumePrefix(&name, "usb")) {
sinfo->transport_type = kTransportUsb;
- } else if (android::base::StartsWith(name, "any")) {
- name += strlen("any");
+ } else if (ConsumePrefix(&name, "any")) {
sinfo->transport_type = kTransportAny;
} else {
return nullptr;
}
- if (!strcmp(name, "-device")) {
+ if (name == "-device") {
sinfo->state = kCsDevice;
- } else if (!strcmp(name, "-recovery")) {
+ } else if (name == "-recovery") {
sinfo->state = kCsRecovery;
- } else if (!strcmp(name, "-sideload")) {
+ } else if (name == "-sideload") {
sinfo->state = kCsSideload;
- } else if (!strcmp(name, "-bootloader")) {
+ } else if (name == "-bootloader") {
sinfo->state = kCsBootloader;
- } else if (!strcmp(name, "-any")) {
+ } else if (name == "-any") {
sinfo->state = kCsAny;
} else {
return nullptr;
@@ -235,8 +232,8 @@
wait_for_state(fd, sinfo.get());
});
return create_local_socket(std::move(fd));
- } else if (!strncmp(name, "connect:", 8)) {
- std::string host(name + strlen("connect:"));
+ } else if (ConsumePrefix(&name, "connect:")) {
+ std::string host(name);
unique_fd fd = create_service_thread(
"connect", std::bind(connect_service, std::placeholders::_1, host));
return create_local_socket(std::move(fd));
diff --git a/adb/sockets.cpp b/adb/sockets.cpp
index df0104b..04d92db 100644
--- a/adb/sockets.cpp
+++ b/adb/sockets.cpp
@@ -426,22 +426,6 @@
return s;
}
-#if ADB_HOST
-static asocket* create_host_service_socket(const char* name, const char* serial,
- TransportId transport_id) {
- asocket* s;
-
- s = host_service_to_socket(name, serial, transport_id);
-
- if (s != nullptr) {
- D("LS(%d) bound to '%s'", s->id, name);
- return s;
- }
-
- return s;
-}
-#endif /* ADB_HOST */
-
static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
apacket* p = get_apacket();
@@ -825,8 +809,7 @@
** and tear down here.
*/
// TODO: Convert to string_view.
- s2 = create_host_service_socket(std::string(service).c_str(), std::string(serial).c_str(),
- transport_id);
+ s2 = host_service_to_socket(service, serial, transport_id);
if (s2 == nullptr) {
LOG(VERBOSE) << "SS(" << s->id << "): couldn't create host service '" << service << "'";
SendFail(s->peer->fd, "unknown host service");