Main program for AIDL Vehicle HAL

Test: Build by `mmm :android.hardware.automotive.vehicle@V1-trout-service`
Bug: 266001013

Change-Id: Ieecaca975c3604cb924e680c9538419825d5e98a
diff --git a/hal/vehicle/aidl/Android.bp b/hal/vehicle/aidl/Android.bp
index e5bbdff..7efca20 100644
--- a/hal/vehicle/aidl/Android.bp
+++ b/hal/vehicle/aidl/Android.bp
@@ -99,3 +99,34 @@
         "-Wno-unused-parameter",
     ],
 }
+
+cc_binary {
+    name: "android.hardware.automotive.vehicle@V1-trout-service",
+    defaults: ["VehicleHalDefaults"],
+    init_rc: ["android.hardware.automotive.vehicle@V1-trout-service.rc"],
+    vendor: true,
+    relative_install_path: "hw",
+    srcs: [
+        "VirtualizedVehicleService.cpp",
+    ],
+    header_libs: [
+        "IVehicleHardware",
+    ],
+    static_libs: [
+        "android.hardware.automotive.utils.vsockinfo",
+        "android.hardware.automotive.vehicle@V1-grpc-trout-hardware",
+        "DefaultVehicleHal",
+        "VehicleHalUtils",
+    ],
+    shared_libs: [
+        "libbase",
+        "libcutils",
+        "libgrpc++",
+        "liblog",
+        "libprotobuf-cpp-full",
+    ],
+    cflags: [
+        "-Wno-unused-parameter",
+    ],
+    vintf_fragments: ["manifest_android.hardware.automotive.vehicle@V1-trout-service.xml"],
+}
diff --git a/hal/vehicle/aidl/VirtualizedVehicleService.cpp b/hal/vehicle/aidl/VirtualizedVehicleService.cpp
new file mode 100644
index 0000000..59f10b9
--- /dev/null
+++ b/hal/vehicle/aidl/VirtualizedVehicleService.cpp
@@ -0,0 +1,71 @@
+/*
+ * 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 "DefaultVehicleHal.h"
+#include "GRPCVehicleHardware.h"
+#include "vsockinfo.h"
+
+#include <android-base/logging.h>
+#include <android/binder_manager.h>
+#include <android/binder_process.h>
+
+#include <memory>
+#include <utility>
+
+using ::android::hardware::automotive::vehicle::DefaultVehicleHal;
+using ::android::hardware::automotive::vehicle::virtualization::GRPCVehicleHardware;
+
+int main(int /* argc */, char* /* argv */[]) {
+    LOG(INFO) << "Starting thread pool...";
+    if (!ABinderProcess_setThreadPoolMaxThreadCount(4)) {
+        LOG(ERROR) << "Failed to set thread pool max thread count.";
+        return 1;
+    }
+    ABinderProcess_startThreadPool();
+
+    auto vsock = android::hardware::automotive::utils::VsockConnectionInfo::fromRoPropertyStore(
+            {
+                    "ro.boot.vendor.vehiclehal.server.cid",
+                    "ro.vendor.vehiclehal.server.cid",
+            },
+            {
+                    "ro.boot.vendor.vehiclehal.server.port",
+                    "ro.vendor.vehiclehal.server.port",
+            });
+    CHECK(vsock.has_value()) << "Cannot read VHAL server address.";
+
+    LOG(INFO) << "Connecting to vsock server at " << vsock->str();
+
+    auto hardware = std::make_unique<GRPCVehicleHardware>(vsock->str());
+    auto vhal = ::ndk::SharedRefBase::make<DefaultVehicleHal>(std::move(hardware));
+
+    LOG(INFO) << "Registering as service...";
+    binder_exception_t err = AServiceManager_addService(
+            vhal->asBinder().get(), "android.hardware.automotive.vehicle.IVehicle/default");
+    if (err != EX_NONE) {
+        LOG(ERROR) << "Failed to register android.hardware.automotive.vehicle service, exception: "
+                   << err << ".";
+        return 1;
+    }
+
+    LOG(INFO) << "Vehicle Service Ready.";
+
+    ABinderProcess_joinThreadPool();
+
+    LOG(INFO) << "Vehicle Service Exiting.";
+
+    return 0;
+}
diff --git a/hal/vehicle/aidl/android.hardware.automotive.vehicle@V1-trout-service.rc b/hal/vehicle/aidl/android.hardware.automotive.vehicle@V1-trout-service.rc
new file mode 100644
index 0000000..8193e80
--- /dev/null
+++ b/hal/vehicle/aidl/android.hardware.automotive.vehicle@V1-trout-service.rc
@@ -0,0 +1,4 @@
+service vendor.vehicle-hal-trout /vendor/bin/hw/android.hardware.automotive.vehicle@V1-trout-service
+    class hal
+    user vehicle_network
+    group system inet
diff --git a/hal/vehicle/aidl/manifest_android.hardware.automotive.vehicle@V1-trout-service.xml b/hal/vehicle/aidl/manifest_android.hardware.automotive.vehicle@V1-trout-service.xml
new file mode 100644
index 0000000..5dcfa12
--- /dev/null
+++ b/hal/vehicle/aidl/manifest_android.hardware.automotive.vehicle@V1-trout-service.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+** Copyright 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.
+** limitations under the License.
+*/
+-->
+<manifest version="1.0" type="device">
+    <hal format="aidl">
+        <name>android.hardware.automotive.vehicle</name>
+        <version>1</version>
+        <fqname>IVehicle/default</fqname>
+    </hal>
+</manifest>