Revert^2 "Add vhal_proxy_server component."

606a9272d07d1e6dc52a2b9e03b905a010e74b32

Test: Local run
Bug: 328316981

Change-Id: Ic79873754673235f8a30da7e409c4e71787b2e10
diff --git a/build/Android.bp b/build/Android.bp
index 1e69ce0..d004e80 100644
--- a/build/Android.bp
+++ b/build/Android.bp
@@ -191,6 +191,7 @@
     "vulkan.pastel",
     "automotive_vsock_proxy",
     "vhost_device_vsock",
+    "vhal_proxy_server",
 ]
 
 cvd_openwrt_images = [
diff --git a/guest/hals/vehicle/VehicleService.cpp b/guest/hals/vehicle/VehicleService.cpp
index 6555f55..173eea0 100644
--- a/guest/hals/vehicle/VehicleService.cpp
+++ b/guest/hals/vehicle/VehicleService.cpp
@@ -36,9 +36,7 @@
 
 const char* SERVICE_NAME =
     "android.hardware.automotive.vehicle.IVehicle/default";
-
-// TODO(b/323236247): Read this from boot config.
-constexpr int VHAL_PROXY_SERVER_VSOCK_PORT = 9300;
+const char* BOOTCONFIG_PORT = "ro.boot.vhal_proxy_server_port";
 
 int main(int /* argc */, char* /* argv */[]) {
   LOG(INFO) << "Starting thread pool...";
@@ -50,8 +48,11 @@
 
   VsockConnectionInfo vsock = {
       .cid = VMADDR_CID_HOST,
-      .port = VHAL_PROXY_SERVER_VSOCK_PORT,
+      .port =
+          static_cast<unsigned int>(property_get_int32(BOOTCONFIG_PORT, -1)),
   };
+  CHECK(vsock.port >= 0) << "Failed to read port number from: "
+                         << BOOTCONFIG_PORT;
   std::string vsockStr = vsock.str();
 
   LOG(INFO) << "Connecting to vsock server at " << vsockStr;
diff --git a/host/commands/assemble_cvd/bootconfig_args.cpp b/host/commands/assemble_cvd/bootconfig_args.cpp
index f7a6d96..0d9739a 100644
--- a/host/commands/assemble_cvd/bootconfig_args.cpp
+++ b/host/commands/assemble_cvd/bootconfig_args.cpp
@@ -201,6 +201,11 @@
           ? "com.android.hardware.gatekeeper.nonsecure"
           : "com.android.hardware.gatekeeper.cf_remote";
 
+  if (config.vhal_proxy_server_port()) {
+    bootconfig_args["androidboot.vhal_proxy_server_port"] =
+        std::to_string(config.vhal_proxy_server_port());
+  }
+
   std::vector<std::string> args = instance.extra_bootconfig_args();
 
   LOG(DEBUG) << "Parsing extra_bootconfig_args of size:" << args.size()
diff --git a/host/commands/assemble_cvd/flags.cc b/host/commands/assemble_cvd/flags.cc
index f872dc1..b3f906c 100644
--- a/host/commands/assemble_cvd/flags.cc
+++ b/host/commands/assemble_cvd/flags.cc
@@ -221,6 +221,16 @@
 DEFINE_bool(enable_automotive_proxy, CF_DEFAULTS_ENABLE_AUTOMOTIVE_PROXY,
             "Enable the automotive proxy service on the host.");
 
+DEFINE_bool(enable_vhal_proxy_server, CF_DEFAULTS_ENABLE_VHAL_PROXY_SERVER,
+            "Enable the vhal proxy service on the host.");
+DEFINE_int32(vhal_proxy_server_instance_num,
+             CF_DEFAULTS_VHAL_PROXY_SERVER_INSTANCE_NUM,
+             "If it is greater than 0, use an existing vhal proxy server "
+             "instance which is "
+             "launched from cuttlefish instance "
+             "with vhal_proxy_server_instance_num. Else, launch a new vhal "
+             "proxy server instance");
+
 /**
  * crosvm sandbox feature requires /var/empty and seccomp directory
  *
@@ -1282,6 +1292,16 @@
 
   tmp_config_obj.set_host_sandbox(FLAGS_enable_host_sandbox);
 
+  auto vhal_proxy_server_instance_num = *instance_nums.begin() - 1;
+  if (FLAGS_vhal_proxy_server_instance_num > 0) {
+    vhal_proxy_server_instance_num = FLAGS_vhal_proxy_server_instance_num - 1;
+  }
+  tmp_config_obj.set_vhal_proxy_server_port(9300 +
+                                            vhal_proxy_server_instance_num);
+  LOG(DEBUG) << "launch vhal proxy server: "
+             << (FLAGS_enable_vhal_proxy_server &&
+                 vhal_proxy_server_instance_num <= 0);
+
   // Environment specific configs
   // Currently just setting for the default environment
   auto environment_name =
@@ -1739,6 +1759,9 @@
 
     instance.set_start_pica(is_first_instance && !is_uwb_netsim &&
                             FLAGS_pica_instance_num <= 0);
+    instance.set_start_vhal_proxy_server(
+        is_first_instance && FLAGS_enable_vhal_proxy_server &&
+        FLAGS_vhal_proxy_server_instance_num <= 0);
 
     // TODO(b/288987294) Remove this when separating environment is done
     bool instance_start_wmediumd = is_first_instance && start_wmediumd;
diff --git a/host/commands/assemble_cvd/flags_defaults.h b/host/commands/assemble_cvd/flags_defaults.h
index 93618a7..b2f594f 100644
--- a/host/commands/assemble_cvd/flags_defaults.h
+++ b/host/commands/assemble_cvd/flags_defaults.h
@@ -182,6 +182,10 @@
 // Automotive Proxy default parameter
 #define CF_DEFAULTS_ENABLE_AUTOMOTIVE_PROXY false
 
+// Vhal Proxy Server default parameter
+#define CF_DEFAULTS_ENABLE_VHAL_PROXY_SERVER false
+#define CF_DEFAULTS_VHAL_PROXY_SERVER_INSTANCE_NUM 0
+
 // Bluetooth default parameters
 #define CF_DEFAULTS_ENABLE_HOST_BLUETOOTH true
 #define CF_DEFAULTS_ROOTCANAL_INSTANCE_NUM 0
diff --git a/host/commands/run_cvd/Android.bp b/host/commands/run_cvd/Android.bp
index faacebb..e04ccee 100644
--- a/host/commands/run_cvd/Android.bp
+++ b/host/commands/run_cvd/Android.bp
@@ -45,6 +45,7 @@
         "launch/webrtc_recorder.cpp",
         "launch/streamer.cpp",
         "launch/netsim_server.cpp",
+        "launch/vhal_proxy_server.cpp",
         "main.cc",
         "reporting.cpp",
         "server_loop.cpp",
diff --git a/host/commands/run_cvd/launch/launch.h b/host/commands/run_cvd/launch/launch.h
index 4d93984..01abfbb 100644
--- a/host/commands/run_cvd/launch/launch.h
+++ b/host/commands/run_cvd/launch/launch.h
@@ -137,4 +137,7 @@
     fruit::Required<const CuttlefishConfig,
                     const CuttlefishConfig::InstanceSpecific, LogTeeCreator>>
 McuComponent();
+
+std::optional<MonitorCommand> VhalProxyServer(
+    const CuttlefishConfig&, const CuttlefishConfig::InstanceSpecific&);
 }  // namespace cuttlefish
diff --git a/host/commands/run_cvd/launch/vhal_proxy_server.cpp b/host/commands/run_cvd/launch/vhal_proxy_server.cpp
new file mode 100644
index 0000000..97bfca7
--- /dev/null
+++ b/host/commands/run_cvd/launch/vhal_proxy_server.cpp
@@ -0,0 +1,35 @@
+//
+// Copyright (C) 2024 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 "host/commands/run_cvd/launch/launch.h"
+
+#include "common/libs/utils/subprocess.h"
+#include "host/libs/config/command_source.h"
+#include "host/libs/config/known_paths.h"
+
+namespace cuttlefish {
+
+std::optional<MonitorCommand> VhalProxyServer(
+    const CuttlefishConfig& config,
+    const CuttlefishConfig::InstanceSpecific& instance) {
+  if (!instance.start_vhal_proxy_server()) {
+    return {};
+  }
+  return Command(VhalProxyServerBinary())
+      .AddParameter(VhalProxyServerConfig())
+      .AddParameter(config.vhal_proxy_server_port());
+}
+
+}  // namespace cuttlefish
diff --git a/host/commands/run_cvd/main.cc b/host/commands/run_cvd/main.cc
index 05bd8d2..163199b 100644
--- a/host/commands/run_cvd/main.cc
+++ b/host/commands/run_cvd/main.cc
@@ -134,6 +134,7 @@
       .install(VhostDeviceVsockComponent)
       .install(WmediumdServerComponent)
       .install(launchStreamerComponent)
+      .install(AutoCmd<VhalProxyServer>::Component)
 #endif
       .install(AdbConfigComponent)
       .install(AdbConfigFragmentComponent)
diff --git a/host/libs/config/cuttlefish_config.cpp b/host/libs/config/cuttlefish_config.cpp
index 091b591..ab11c98 100644
--- a/host/libs/config/cuttlefish_config.cpp
+++ b/host/libs/config/cuttlefish_config.cpp
@@ -306,6 +306,14 @@
   return (*dictionary_)[kEnableAutomotiveProxy].asBool();
 }
 
+static constexpr char kVhalProxyServerPort[] = "vhal_proxy_server_port";
+void CuttlefishConfig::set_vhal_proxy_server_port(int port) {
+  (*dictionary_)[kVhalProxyServerPort] = port;
+}
+int CuttlefishConfig::vhal_proxy_server_port() const {
+  return (*dictionary_)[kVhalProxyServerPort].asInt();
+}
+
 static constexpr char kEnableHostNfc[] = "enable_host_nfc";
 void CuttlefishConfig::set_enable_host_nfc(bool enable_host_nfc) {
   (*dictionary_)[kEnableHostNfc] = enable_host_nfc;
diff --git a/host/libs/config/cuttlefish_config.h b/host/libs/config/cuttlefish_config.h
index f23148d..8cec1cf 100644
--- a/host/libs/config/cuttlefish_config.h
+++ b/host/libs/config/cuttlefish_config.h
@@ -154,6 +154,10 @@
   void set_enable_automotive_proxy(bool enable_automotive_proxy);
   bool enable_automotive_proxy() const;
 
+  // The vsock port used by vhal_proxy_server
+  void set_vhal_proxy_server_port(int port);
+  int vhal_proxy_server_port() const;
+
   // Bluetooth is enabled by bt_connector and rootcanal
   void set_enable_host_bluetooth_connector(bool enable_host_bluetooth);
   bool enable_host_bluetooth_connector() const;
@@ -688,6 +692,8 @@
     bool bootconfig_supported() const;
     std::string filename_encryption_mode() const;
     ExternalNetworkMode external_network_mode() const;
+
+    bool start_vhal_proxy_server() const;
   };
 
   // A view into an existing CuttlefishConfig object for a particular instance.
@@ -895,6 +901,10 @@
     void set_filename_encryption_mode(const std::string& userdata_format);
     void set_external_network_mode(ExternalNetworkMode network_mode);
 
+    // Whether we should start vhal_proxy_server for the guest-side VHAL to
+    // connect to.
+    void set_start_vhal_proxy_server(bool enable_vhal_proxy_server);
+
    private:
     void SetPath(const std::string& key, const std::string& path);
   };
diff --git a/host/libs/config/cuttlefish_config_instance.cpp b/host/libs/config/cuttlefish_config_instance.cpp
index 2bc2a3a..4301250 100644
--- a/host/libs/config/cuttlefish_config_instance.cpp
+++ b/host/libs/config/cuttlefish_config_instance.cpp
@@ -1840,6 +1840,15 @@
   (*Dictionary())[kWifiMacPrefix] = wifi_mac_prefix;
 }
 
+static constexpr char kStartVhalProxyServer[] = "start_vhal_proxy_server";
+void CuttlefishConfig::MutableInstanceSpecific::set_start_vhal_proxy_server(
+    bool start_vhal_proxy_server) {
+  (*Dictionary())[kStartVhalProxyServer] = start_vhal_proxy_server;
+}
+bool CuttlefishConfig::InstanceSpecific::start_vhal_proxy_server() const {
+  return (*Dictionary())[kStartVhalProxyServer].asBool();
+}
+
 std::string CuttlefishConfig::InstanceSpecific::factory_reset_protected_path() const {
   return PerInstanceInternalPath("factory_reset_protected.img");
 }
diff --git a/host/libs/config/known_paths.cpp b/host/libs/config/known_paths.cpp
index a5bfb0c..37e7b4e 100644
--- a/host/libs/config/known_paths.cpp
+++ b/host/libs/config/known_paths.cpp
@@ -132,4 +132,12 @@
   return HostBinaryPath("automotive_vsock_proxy");
 }
 
+std::string VhalProxyServerBinary() {
+  return HostBinaryPath("vhal_proxy_server");
+}
+
+std::string VhalProxyServerConfig() {
+  return DefaultHostArtifactsPath("etc/automotive/vhalconfig");
+}
+
 } // namespace cuttlefish
diff --git a/host/libs/config/known_paths.h b/host/libs/config/known_paths.h
index 355d255..7895b20 100644
--- a/host/libs/config/known_paths.h
+++ b/host/libs/config/known_paths.h
@@ -52,5 +52,7 @@
 std::string WmediumdBinary();
 std::string WmediumdGenConfigBinary();
 std::string AutomotiveProxyBinary();
+std::string VhalProxyServerBinary();
+std::string VhalProxyServerConfig();
 
 } // namespace cuttlefish
diff --git a/shared/auto/sepolicy/vendor/hal_vehicle_default.te b/shared/auto/sepolicy/vendor/hal_vehicle_default.te
index d49d1d4..96c447a 100644
--- a/shared/auto/sepolicy/vendor/hal_vehicle_default.te
+++ b/shared/auto/sepolicy/vendor/hal_vehicle_default.te
@@ -2,3 +2,4 @@
 typeattribute hal_vehicle_default hal_automotive_socket_exemption;
 
 net_domain(hal_vehicle_default)
+get_prop(hal_vehicle_default, vendor_vhal_proxy_server_port_prop)
diff --git a/shared/sepolicy/vendor/property.te b/shared/sepolicy/vendor/property.te
index e9ba199..589e729 100644
--- a/shared/sepolicy/vendor/property.te
+++ b/shared/sepolicy/vendor/property.te
@@ -5,3 +5,4 @@
 vendor_internal_prop(vendor_device_prop)
 vendor_internal_prop(vendor_uwb_prop)
 vendor_internal_prop(vendor_otsim_local_interface_prop)
+vendor_internal_prop(vendor_vhal_proxy_server_port_prop)
diff --git a/shared/sepolicy/vendor/property_contexts b/shared/sepolicy/vendor/property_contexts
index ade0a8e..1150414 100644
--- a/shared/sepolicy/vendor/property_contexts
+++ b/shared/sepolicy/vendor/property_contexts
@@ -7,6 +7,7 @@
 ro.boot.enable_confirmationui  u:object_r:vendor_enable_confirmationui_prop:s0
 ro.boot.modem_simulator_ports  u:object_r:vendor_modem_simulator_ports_prop:s0
 ro.boot.wifi_mac_prefix  u:object_r:vendor_wifi_mac_prefix:s0 exact string
+ro.boot.vhal_proxy_server_port  u:object_r:vendor_vhal_proxy_server_port_prop:s0
 ro.vendor.wifi_impl u:object_r:vendor_wifi_impl:s0 exact string
 ro.vendor.boot_security_patch u:object_r:vendor_boot_security_patch_level_prop:s0
 ro.vendor.hwcomposer.display_finder_mode  u:object_r:vendor_hwcomposer_prop:s0 exact string