blob: 262a689b12881540d0c325762566e276d8f0d22b [file] [log] [blame]
Victor Hsiehbb7dd082021-10-12 16:39:02 -07001/*
2 * Copyright (C) 2021 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//! Helpers for implementing an RPC Binder client.
18
19use binder::public_api::{StatusCode, Strong};
20use binder::unstable_api::{new_spibinder, AIBinder};
21
22/// Connects to a binder RPC server.
23pub fn connect_rpc_binder<T: binder::FromIBinder + ?Sized>(
24 cid: u32,
25 port: u32,
26) -> binder::Result<Strong<T>> {
27 // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be
28 // safely taken by new_spibinder.
29 let ibinder = unsafe {
30 new_spibinder(binder_rpc_unstable_bindgen::RpcClient(cid, port) as *mut AIBinder)
31 };
32 if let Some(ibinder) = ibinder {
33 <T>::try_from(ibinder)
34 } else {
35 Err(StatusCode::BAD_VALUE)
36 }
37}