1 // Copyright 2024, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! VM with the simplest service for IAccessor demo
16
17 use anyhow::Result;
18 use com_android_virt_accessor_demo_vm_service::{
19 aidl::com::android::virt::accessor_demo::vm_service::IAccessorVmService::{
20 BnAccessorVmService, IAccessorVmService,
21 },
22 binder::{self, BinderFeatures, Interface, Strong},
23 };
24 use log::{error, info};
25
26 // Private contract between IAccessor impl and VM service.
27 const PORT: u32 = 5678;
28
29 vm_payload::main!(main);
30
31 // Entry point of the Service VM client.
main()32 fn main() {
33 android_logger::init_once(
34 android_logger::Config::default()
35 .with_tag("accessor_vm")
36 .with_max_level(log::LevelFilter::Debug),
37 );
38 if let Err(e) = try_main() {
39 error!("failed with {:?}", e);
40 std::process::exit(1);
41 }
42 }
43
try_main() -> Result<()>44 fn try_main() -> Result<()> {
45 info!("Starting stub payload for IAccessor demo");
46
47 vm_payload::run_single_vsock_service(AccessorVmService::new_binder(), PORT)
48 }
49
50 struct AccessorVmService {}
51
52 impl Interface for AccessorVmService {}
53
54 impl AccessorVmService {
new_binder() -> Strong<dyn IAccessorVmService>55 fn new_binder() -> Strong<dyn IAccessorVmService> {
56 BnAccessorVmService::new_binder(AccessorVmService {}, BinderFeatures::default())
57 }
58 }
59
60 impl IAccessorVmService for AccessorVmService {
add(&self, a: i32, b: i32) -> binder::Result<i32>61 fn add(&self, a: i32, b: i32) -> binder::Result<i32> {
62 Ok(a + b)
63 }
64 }
65