[API] Add new service to notify host from microdroid manager

This CL adds a new service to notify the host from
microdroid manager instead of the payload process.

Bug: 243512047
Test: MicrodroidTests microdroid_manager_test
Change-Id: Ic634944f6dd5266c084f495787bf4568a4d18ed3
diff --git a/microdroid_manager/src/main.rs b/microdroid_manager/src/main.rs
index b4b2c8b..1349ede 100644
--- a/microdroid_manager/src/main.rs
+++ b/microdroid_manager/src/main.rs
@@ -17,8 +17,10 @@
 mod instance;
 mod ioutil;
 mod payload;
+mod vm_payload_service;
 
 use crate::instance::{ApexData, ApkData, InstanceDisk, MicrodroidData, RootHash};
+use crate::vm_payload_service::register_vm_payload_service;
 use android_hardware_security_dice::aidl::android::hardware::security::dice::{
     Config::Config, InputValues::InputValues, Mode::Mode,
 };
@@ -29,7 +31,7 @@
 };
 use anyhow::{anyhow, bail, ensure, Context, Error, Result};
 use apkverify::{get_public_key_der, verify, V4Signature};
-use binder::{wait_for_interface, Strong};
+use binder::{ProcessState, wait_for_interface, Strong};
 use diced_utils::cbor::{encode_header, encode_number};
 use glob::glob;
 use itertools::sorted;
@@ -386,6 +388,8 @@
     }
 
     system_properties::write("dev.bootcomplete", "1").context("set dev.bootcomplete")?;
+    register_vm_payload_service(service.clone())?;
+    ProcessState::start_thread_pool();
     exec_task(task, service)
 }
 
diff --git a/microdroid_manager/src/vm_payload_service.rs b/microdroid_manager/src/vm_payload_service.rs
new file mode 100644
index 0000000..6eb3240
--- /dev/null
+++ b/microdroid_manager/src/vm_payload_service.rs
@@ -0,0 +1,56 @@
+// Copyright 2022, 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.
+
+//! Implementation of the AIDL interface `IVmPayloadService`.
+
+use android_system_virtualization_payload::aidl::android::system::virtualization::payload::IVmPayloadService::{BnVmPayloadService, IVmPayloadService};
+use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::IVirtualMachineService;
+use anyhow::{Context, Result};
+use binder::{Interface, BinderFeatures, Strong, add_service};
+
+const VM_PAYLOAD_SERVICE_NAME: &str = "virtual_machine_payload_service";
+
+/// Implementation of `IVmPayloadService`.
+struct VmPayloadService {
+    virtual_machine_service: Strong<dyn IVirtualMachineService>,
+}
+
+impl IVmPayloadService for VmPayloadService {
+    fn notifyPayloadReady(&self) -> binder::Result<()> {
+        self.virtual_machine_service.notifyPayloadReady()
+    }
+}
+
+impl Interface for VmPayloadService {}
+
+impl VmPayloadService {
+    /// Creates a new `VmPayloadService` instance from the `IVirtualMachineService` reference.
+    fn new(vm_service: Strong<dyn IVirtualMachineService>) -> Self {
+        Self { virtual_machine_service: vm_service }
+    }
+}
+
+/// Registers the `IVmPayloadService` service.
+pub(crate) fn register_vm_payload_service(
+    vm_service: Strong<dyn IVirtualMachineService>,
+) -> Result<()> {
+    let vm_payload_binder = BnVmPayloadService::new_binder(
+        VmPayloadService::new(vm_service),
+        BinderFeatures::default(),
+    );
+    add_service(VM_PAYLOAD_SERVICE_NAME, vm_payload_binder.as_binder())
+        .with_context(|| format!("Failed to register service {}", VM_PAYLOAD_SERVICE_NAME))?;
+    log::info!("{} is running", VM_PAYLOAD_SERVICE_NAME);
+    Ok(())
+}