blob: 1aabe841e09d69de4b1dad09c1af4108c6540d3a [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
Victor Hsiehbb7dd082021-10-12 16:39:02 -070019use binder::unstable_api::{new_spibinder, AIBinder};
Stephen Crane44fd9bc2022-01-19 17:49:46 +000020use binder::{StatusCode, Strong};
Victor Hsiehbb7dd082021-10-12 16:39:02 -070021
22/// Connects to a binder RPC server.
23pub fn connect_rpc_binder<T: binder::FromIBinder + ?Sized>(
24 cid: u32,
25 port: u32,
Stephen Crane44fd9bc2022-01-19 17:49:46 +000026) -> Result<Strong<T>, StatusCode> {
Victor Hsiehbb7dd082021-10-12 16:39:02 -070027 // 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}