Refactor BindToDeviceSocketMutator.
Only expose a factory function in the header. This limits the
internal gRPC dependency to the .cpp file and simplifies the
implementation.
Bug: 280043032
Test: local build
Change-Id: Ic7ea8dac9935231ceb05bec22c2a5902c50ea8db
diff --git a/automotive/remoteaccess/bind_to_device_socket_mutator/Android.bp b/automotive/remoteaccess/bind_to_device_socket_mutator/Android.bp
index 113b14e..58df21a 100644
--- a/automotive/remoteaccess/bind_to_device_socket_mutator/Android.bp
+++ b/automotive/remoteaccess/bind_to_device_socket_mutator/Android.bp
@@ -37,7 +37,7 @@
cc_library {
name: "BindToDeviceSocketMutatorLib",
vendor_available: true,
- srcs: ["src/*"],
+ srcs: ["src/BindToDeviceSocketMutator.cpp"],
export_include_dirs: ["include"],
defaults: ["BindToDeviceSocketMutatorDefaults"],
}
diff --git a/automotive/remoteaccess/bind_to_device_socket_mutator/include/BindToDeviceSocketMutator.h b/automotive/remoteaccess/bind_to_device_socket_mutator/include/BindToDeviceSocketMutator.h
index bafcc65..5974c4b 100644
--- a/automotive/remoteaccess/bind_to_device_socket_mutator/include/BindToDeviceSocketMutator.h
+++ b/automotive/remoteaccess/bind_to_device_socket_mutator/include/BindToDeviceSocketMutator.h
@@ -16,20 +16,11 @@
#pragma once
-#include <grpc++/grpc++.h>
-#include <src/core/lib/iomgr/socket_mutator.h>
-#include <string>
+#include <grpc/grpc.h>
+#include <string_view>
namespace android::hardware::automotive::remoteaccess {
-class BindToDeviceSocketMutator final : public grpc_socket_mutator {
- public:
- BindToDeviceSocketMutator(const std::string_view& interface_name);
-
- bool mutateFd(int fd);
-
- private:
- std::string mIfname;
-};
+grpc_socket_mutator* MakeBindToDeviceSocketMutator(std::string_view interface_name);
} // namespace android::hardware::automotive::remoteaccess
diff --git a/automotive/remoteaccess/bind_to_device_socket_mutator/src/BindToDeviceSocketMutator.cpp b/automotive/remoteaccess/bind_to_device_socket_mutator/src/BindToDeviceSocketMutator.cpp
index c6a96de..04a4c5b 100644
--- a/automotive/remoteaccess/bind_to_device_socket_mutator/src/BindToDeviceSocketMutator.cpp
+++ b/automotive/remoteaccess/bind_to_device_socket_mutator/src/BindToDeviceSocketMutator.cpp
@@ -18,40 +18,50 @@
#include <android-base/logging.h>
#include <errno.h>
+#include <src/core/lib/iomgr/socket_mutator.h>
#include <sys/socket.h>
-namespace android::hardware::automotive::remoteaccess {
+#include <memory>
-bool BindToDeviceSocketMutator::mutateFd(int fd) {
- int ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, mIfname.c_str(), mIfname.size());
+namespace android::hardware::automotive::remoteaccess {
+namespace {
+
+struct BindToDeviceMutator : grpc_socket_mutator {
+ std::string ifname;
+};
+
+bool MutateFd(int fd, grpc_socket_mutator* mutator) {
+ auto* bdm = static_cast<BindToDeviceMutator*>(mutator);
+ int ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bdm->ifname.c_str(), bdm->ifname.size());
if (ret != 0) {
- PLOG(ERROR) << "Can't bind socket to interface " << mIfname;
+ PLOG(ERROR) << "Can't bind socket to interface " << bdm->ifname;
return false;
}
return true;
}
-bool bind_to_device_mutator_mutate_fd(int fd, grpc_socket_mutator* mutator) {
- BindToDeviceSocketMutator* bsm = (BindToDeviceSocketMutator*)mutator;
- return bsm->mutateFd(fd);
-}
-
-int bind_to_device_mutator_compare(grpc_socket_mutator* a, grpc_socket_mutator* b) {
+int Compare(grpc_socket_mutator* a, grpc_socket_mutator* b) {
return ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0));
}
-void bind_to_device_mutator_destroy(grpc_socket_mutator* mutator) {
- BindToDeviceSocketMutator* bsm = (BindToDeviceSocketMutator*)mutator;
- delete bsm;
+void Destroy(grpc_socket_mutator* mutator) {
+ auto* bdm = static_cast<BindToDeviceMutator*>(mutator);
+ delete bdm;
}
-grpc_socket_mutator_vtable bind_to_device_mutator_vtable = {bind_to_device_mutator_mutate_fd,
- bind_to_device_mutator_compare,
- bind_to_device_mutator_destroy};
+constexpr grpc_socket_mutator_vtable kMutatorVtable = {
+ .mutate_fd = MutateFd,
+ .compare = Compare,
+ .destroy = Destroy,
+};
-BindToDeviceSocketMutator::BindToDeviceSocketMutator(const std::string_view& interface_name) {
- mIfname = interface_name;
- grpc_socket_mutator_init(this, &bind_to_device_mutator_vtable);
+} // namespace
+
+grpc_socket_mutator* MakeBindToDeviceSocketMutator(std::string_view interface_name) {
+ auto* bdm = new BindToDeviceMutator;
+ grpc_socket_mutator_init(bdm, &kMutatorVtable);
+ bdm->ifname = interface_name;
+ return bdm;
}
} // namespace android::hardware::automotive::remoteaccess
diff --git a/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp b/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp
index d525141..b091162 100644
--- a/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp
+++ b/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp
@@ -40,7 +40,7 @@
#ifdef GRPC_SERVICE_IFNAME
grpcargs.SetSocketMutator(
- new android::hardware::automotive::remoteaccess::BindToDeviceSocketMutator(
+ android::hardware::automotive::remoteaccess::MakeBindToDeviceSocketMutator(
GRPC_SERVICE_IFNAME));
LOG(DEBUG) << "GRPC_SERVICE_IFNAME specified as: " << GRPC_SERVICE_IFNAME;
LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME;