1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //! AIDL IPC Server code.
18 use crate::hwcrypto_device_key;
19 use binder::SpIBinder;
20 use core::ffi::CStr;
21 use hwcryptohal_common::{err::HwCryptoError, hwcrypto_err};
22 use rpcbinder::RpcServer;
23 use tipc::{Manager, PortCfg, Uuid};
24 
25 const RUST_SERVICE_PORT: &CStr = c"com.android.trusty.rust.hwcryptohal.V1";
26 
create_device_key_service(uuid: Uuid) -> Option<SpIBinder>27 fn create_device_key_service(uuid: Uuid) -> Option<SpIBinder> {
28     Some(hwcrypto_device_key::HwCryptoKey::new_binder(uuid).as_binder())
29 }
30 
main_loop() -> Result<(), HwCryptoError>31 pub fn main_loop() -> Result<(), HwCryptoError> {
32     let hwdk_rpc_server = RpcServer::new_per_session(create_device_key_service);
33 
34     let cfg = PortCfg::new(RUST_SERVICE_PORT.to_str().expect("should not happen, valid utf-8"))
35         .map_err(|e| {
36             hwcrypto_err!(
37                 GENERIC_ERROR,
38                 "could not create port config for {:?}: {:?}",
39                 RUST_SERVICE_PORT,
40                 e
41             )
42         })?
43         .allow_ta_connect()
44         .allow_ns_connect();
45 
46     let manager = Manager::<_, _, 1, 4>::new_unbuffered(hwdk_rpc_server, cfg)
47         .map_err(|e| hwcrypto_err!(GENERIC_ERROR, "could not create service manager: {:?}", e))?;
48 
49     manager
50         .run_event_loop()
51         .map_err(|e| hwcrypto_err!(GENERIC_ERROR, "service manager received error: {:?}", e))
52 }
53 
54 #[cfg(test)]
55 mod tests {
56     use android_hardware_security_see::aidl::android::hardware::security::see::hwcrypto::IHwCryptoKey::IHwCryptoKey;
57     use rpcbinder::RpcSession;
58     use binder::{IBinder, Strong};
59     use test::expect_eq;
60     use super::*;
61 
62     #[test]
connect_server()63     fn connect_server() {
64         let session: Strong<dyn IHwCryptoKey> =
65             RpcSession::new().setup_trusty_client(RUST_SERVICE_PORT).expect("Failed to connect");
66         expect_eq!(session.as_binder().ping_binder(), Ok(()));
67     }
68 }
69