Merge changes I7d338eef,I95c9f796,I56b160b6 into udc-dev

* changes:
  Change BindToDeviceSocketMutator to lib.
  Specify a eth interface for grpc connection.
  Add trace to measure VHAL performance.
diff --git a/automotive/remoteaccess/bind_to_device_socket_mutator/Android.bp b/automotive/remoteaccess/bind_to_device_socket_mutator/Android.bp
new file mode 100644
index 0000000..113b14e
--- /dev/null
+++ b/automotive/remoteaccess/bind_to_device_socket_mutator/Android.bp
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+cc_defaults {
+    name: "BindToDeviceSocketMutatorDefaults",
+    static_libs: [
+        "android.hardware.automotive.can@libnetdevice",
+        "libnl++",
+    ],
+    shared_libs: [
+        "libbase",
+        "liblog",
+        "libgrpc++",
+    ],
+    cflags: [
+        "-Wno-unused-parameter",
+    ],
+}
+
+cc_library {
+    name: "BindToDeviceSocketMutatorLib",
+    vendor_available: true,
+    srcs: ["src/*"],
+    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
new file mode 100644
index 0000000..bafcc65
--- /dev/null
+++ b/automotive/remoteaccess/bind_to_device_socket_mutator/include/BindToDeviceSocketMutator.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <grpc++/grpc++.h>
+#include <src/core/lib/iomgr/socket_mutator.h>
+#include <string>
+
+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;
+};
+
+}  // 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
new file mode 100644
index 0000000..c6a96de
--- /dev/null
+++ b/automotive/remoteaccess/bind_to_device_socket_mutator/src/BindToDeviceSocketMutator.cpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "BindToDeviceSocketMutator.h"
+
+#include <android-base/logging.h>
+#include <errno.h>
+#include <sys/socket.h>
+
+namespace android::hardware::automotive::remoteaccess {
+
+bool BindToDeviceSocketMutator::mutateFd(int fd) {
+    int ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, mIfname.c_str(), mIfname.size());
+    if (ret != 0) {
+        PLOG(ERROR) << "Can't bind socket to interface " << mIfname;
+        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) {
+    return ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0));
+}
+
+void bind_to_device_mutator_destroy(grpc_socket_mutator* mutator) {
+    BindToDeviceSocketMutator* bsm = (BindToDeviceSocketMutator*)mutator;
+    delete bsm;
+}
+
+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};
+
+BindToDeviceSocketMutator::BindToDeviceSocketMutator(const std::string_view& interface_name) {
+    mIfname = interface_name;
+    grpc_socket_mutator_init(this, &bind_to_device_mutator_vtable);
+}
+
+}  // namespace android::hardware::automotive::remoteaccess
diff --git a/automotive/remoteaccess/hal/default/Android.bp b/automotive/remoteaccess/hal/default/Android.bp
index f27b8f8..0155667 100644
--- a/automotive/remoteaccess/hal/default/Android.bp
+++ b/automotive/remoteaccess/hal/default/Android.bp
@@ -22,22 +22,27 @@
     name: "remote-access-hal-defaults",
     vendor: true,
     relative_install_path: "hw",
-    srcs: ["src/RemoteAccessImpl.cpp"],
+    srcs: [
+        "src/RemoteAccessImpl.cpp",
+    ],
     whole_static_libs: [
         "RemoteAccessService",
     ],
+    static_libs: [
+        "BindToDeviceSocketMutatorLib",
+    ],
     shared_libs: [
-        "libbase",
         "libbinder_ndk",
-        "liblog",
         "libutils",
-        "libgrpc++",
         "libprotobuf-cpp-full",
     ],
     defaults: [
         "vhalclient_defaults",
+        "BindToDeviceSocketMutatorDefaults",
     ],
     cflags: [
+        // This is already included in BindToDeviceSocketMutatorDefaults but
+        // might be overridden by vhalclient_defaults.
         "-Wno-unused-parameter",
     ],
 }
@@ -59,6 +64,7 @@
     init_rc: ["remoteaccess-tcu-test-service.rc"],
     cflags: [
         "-DGRPC_SERVICE_ADDRESS=\"10.10.10.1:50051\"",
+        "-DGRPC_SERVICE_IFNAME=\"eth1\"",
     ],
 }
 
diff --git a/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp b/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp
index 8720c2f..d525141 100644
--- a/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp
+++ b/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp
@@ -18,45 +18,60 @@
 
 #include "RemoteAccessService.h"
 
+#include "BindToDeviceSocketMutator.h"
+
+#include <android-base/logging.h>
 #include <android/binder_manager.h>
 #include <android/binder_process.h>
 #include <grpcpp/create_channel.h>
+#include <libnetdevice/libnetdevice.h>
 #include <stdlib.h>
-#include <utils/Log.h>
 
 constexpr char SERVICE_NAME[] = "android.hardware.automotive.remoteaccess.IRemoteAccess/default";
 
 int main(int /* argc */, char* /* argv */[]) {
-    ALOGI("Registering RemoteAccessService as service...");
+    LOG(INFO) << "Registering RemoteAccessService as service...";
 
 #ifndef GRPC_SERVICE_ADDRESS
-    ALOGE("GRPC_SERVICE_ADDRESS is not defined, exiting");
+    LOG(ERROR) << "GRPC_SERVICE_ADDRESS is not defined, exiting";
     exit(1);
 #endif
-    auto channel = grpc::CreateChannel(GRPC_SERVICE_ADDRESS, grpc::InsecureChannelCredentials());
+    grpc::ChannelArguments grpcargs = {};
+
+#ifdef GRPC_SERVICE_IFNAME
+    grpcargs.SetSocketMutator(
+            new android::hardware::automotive::remoteaccess::BindToDeviceSocketMutator(
+                    GRPC_SERVICE_IFNAME));
+    LOG(DEBUG) << "GRPC_SERVICE_IFNAME specified as: " << GRPC_SERVICE_IFNAME;
+    LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME;
+    android::netdevice::waitFor({GRPC_SERVICE_IFNAME},
+                                android::netdevice::WaitCondition::PRESENT_AND_UP);
+    LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME << " done";
+#endif
+    auto channel = grpc::CreateCustomChannel(GRPC_SERVICE_ADDRESS,
+                                             grpc::InsecureChannelCredentials(), grpcargs);
     auto clientStub = android::hardware::automotive::remoteaccess::WakeupClient::NewStub(channel);
     auto service = ndk::SharedRefBase::make<
             android::hardware::automotive::remoteaccess::RemoteAccessService>(clientStub.get());
 
     binder_exception_t err = AServiceManager_addService(service->asBinder().get(), SERVICE_NAME);
     if (err != EX_NONE) {
-        ALOGE("failed to register android.hardware.automotive.remote.IRemoteAccess service, "
-              "exception: %d",
-              err);
+        LOG(ERROR) << "failed to register android.hardware.automotive.remote.IRemoteAccess service"
+                   << ", exception: " << err;
         exit(1);
     }
 
     if (!ABinderProcess_setThreadPoolMaxThreadCount(1)) {
-        ALOGE("%s", "failed to set thread pool max thread count");
+        LOG(ERROR) << "failed to set thread pool max thread count";
         exit(1);
     }
     ABinderProcess_startThreadPool();
 
-    ALOGI("RemoteAccess service Ready");
+    LOG(INFO) << "RemoteAccess service Ready";
 
     ABinderProcess_joinThreadPool();
 
-    ALOGW("Should not reach here");
+    LOG(ERROR) << "Should not reach here";
 
     return 0;
 }
diff --git a/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp b/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
index 00ebdda..7b4fa16 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
+++ b/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
@@ -15,6 +15,7 @@
  */
 
 #define LOG_TAG "FakeVehicleHardware"
+#define ATRACE_TAG ATRACE_TAG_HAL
 #define FAKE_VEHICLEHARDWARE_DEBUG false  // STOPSHIP if true.
 
 #include "FakeVehicleHardware.h"
@@ -33,6 +34,7 @@
 #include <android-base/strings.h>
 #include <utils/Log.h>
 #include <utils/SystemClock.h>
+#include <utils/Trace.h>
 
 #include <dirent.h>
 #include <inttypes.h>
@@ -1628,11 +1630,15 @@
     std::unordered_map<std::shared_ptr<const GetValuesCallback>, std::vector<GetValueResult>>
             callbackToResults;
     for (const auto& rwc : mRequests.flush()) {
+        ATRACE_BEGIN("FakeVehicleHardware:handleGetValueRequest");
         auto result = mHardware->handleGetValueRequest(rwc.request);
+        ATRACE_END();
         callbackToResults[rwc.callback].push_back(std::move(result));
     }
     for (const auto& [callback, results] : callbackToResults) {
+        ATRACE_BEGIN("FakeVehicleHardware:call get value result callback");
         (*callback)(std::move(results));
+        ATRACE_END();
     }
 }
 
@@ -1642,11 +1648,15 @@
     std::unordered_map<std::shared_ptr<const SetValuesCallback>, std::vector<SetValueResult>>
             callbackToResults;
     for (const auto& rwc : mRequests.flush()) {
+        ATRACE_BEGIN("FakeVehicleHardware:handleSetValueRequest");
         auto result = mHardware->handleSetValueRequest(rwc.request);
+        ATRACE_END();
         callbackToResults[rwc.callback].push_back(std::move(result));
     }
     for (const auto& [callback, results] : callbackToResults) {
+        ATRACE_BEGIN("FakeVehicleHardware:call set value result callback");
         (*callback)(std::move(results));
+        ATRACE_END();
     }
 }
 
diff --git a/automotive/vehicle/aidl/impl/vhal/src/DefaultVehicleHal.cpp b/automotive/vehicle/aidl/impl/vhal/src/DefaultVehicleHal.cpp
index 4a4e023..a7ac1b4 100644
--- a/automotive/vehicle/aidl/impl/vhal/src/DefaultVehicleHal.cpp
+++ b/automotive/vehicle/aidl/impl/vhal/src/DefaultVehicleHal.cpp
@@ -15,6 +15,7 @@
  */
 
 #define LOG_TAG "DefaultVehicleHal"
+#define ATRACE_TAG ATRACE_TAG_HAL
 
 #include <DefaultVehicleHal.h>
 
@@ -28,6 +29,7 @@
 #include <private/android_filesystem_config.h>
 #include <utils/Log.h>
 #include <utils/SystemClock.h>
+#include <utils/Trace.h>
 
 #include <inttypes.h>
 #include <set>
@@ -350,6 +352,7 @@
 
 ScopedAStatus DefaultVehicleHal::getValues(const CallbackType& callback,
                                            const GetValueRequests& requests) {
+    ATRACE_CALL();
     if (callback == nullptr) {
         return ScopedAStatus::fromExceptionCode(EX_NULL_POINTER);
     }
@@ -436,6 +439,7 @@
 
 ScopedAStatus DefaultVehicleHal::setValues(const CallbackType& callback,
                                            const SetValueRequests& requests) {
+    ATRACE_CALL();
     if (callback == nullptr) {
         return ScopedAStatus::fromExceptionCode(EX_NULL_POINTER);
     }