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::{self, Manager, PortCfg, Uuid};
24
25 pub(crate) const RUST_SERVICE_PORT: &CStr = c"com.android.trusty.rust.hwcryptohal.V1";
26 pub(crate) const NUM_IPC_QUEUES: u32 = 4;
27
create_device_key_service(uuid: Uuid) -> Option<SpIBinder>28 fn create_device_key_service(uuid: Uuid) -> Option<SpIBinder> {
29 Some(hwcrypto_device_key::HwCryptoKey::new_binder(uuid).as_binder())
30 }
31
main_loop() -> Result<(), HwCryptoError>32 pub fn main_loop() -> Result<(), HwCryptoError> {
33 let hwdk_rpc_server = RpcServer::new_per_session(create_device_key_service);
34
35 let cfg = PortCfg::new(RUST_SERVICE_PORT.to_str().expect("should not happen, valid utf-8"))
36 .map_err(|e| {
37 hwcrypto_err!(
38 GENERIC_ERROR,
39 "could not create port config for {:?}: {:?}",
40 RUST_SERVICE_PORT,
41 e
42 )
43 })?
44 .msg_queue_len(NUM_IPC_QUEUES)
45 .allow_ta_connect()
46 .allow_ns_connect();
47
48 let manager = Manager::<_, _, 1, 4>::new_unbuffered(hwdk_rpc_server, cfg)
49 .map_err(|e| hwcrypto_err!(GENERIC_ERROR, "could not create service manager: {:?}", e))?;
50
51 manager
52 .run_event_loop()
53 .map_err(|e| hwcrypto_err!(GENERIC_ERROR, "service manager received error: {:?}", e))
54 }
55
56 #[cfg(test)]
57 mod tests {
58 use android_hardware_security_see_hwcrypto::aidl::android::hardware::security::see::hwcrypto::IHwCryptoKey::IHwCryptoKey;
59 use rpcbinder::RpcSession;
60 use binder::{IBinder, Strong};
61 use test::expect_eq;
62 use super::*;
63
64 #[test]
connect_server()65 fn connect_server() {
66 let session_device_key: Strong<dyn IHwCryptoKey> =
67 RpcSession::new().setup_trusty_client(RUST_SERVICE_PORT).expect("Failed to connect");
68 expect_eq!(session_device_key.as_binder().ping_binder(), Ok(()));
69 }
70 }
71