blob: f2391e388f36de5ea32d66939f9a842079ef922c [file] [log] [blame]
Alan Stokes3189af02021-09-30 17:51:19 +01001/*
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//! Common items useful for binder clients and/or servers.
18
Alan Stokes0cc59ee2021-09-24 11:20:34 +010019pub mod lazy_service;
Alan Stokescd359bb2021-10-08 18:22:42 +010020pub mod rpc_server;
Alan Stokes0cc59ee2021-09-24 11:20:34 +010021
Alan Stokes3189af02021-09-30 17:51:19 +010022use binder::public_api::{ExceptionCode, Status};
23use std::ffi::CString;
24
25/// Constructs a new Binder error `Status` with the given `ExceptionCode` and message.
26pub fn new_binder_exception<T: AsRef<str>>(exception: ExceptionCode, message: T) -> Status {
27 match exception {
28 ExceptionCode::SERVICE_SPECIFIC => new_binder_service_specific_error(-1, message),
29 _ => Status::new_exception(exception, to_cstring(message).as_deref()),
30 }
31}
32
33/// Constructs a Binder `Status` representing a service-specific exception with the given code and
34/// message.
35pub fn new_binder_service_specific_error<T: AsRef<str>>(code: i32, message: T) -> Status {
36 Status::new_service_specific_error(code, to_cstring(message).as_deref())
37}
38
39fn to_cstring<T: AsRef<str>>(message: T) -> Option<CString> {
40 CString::new(message.as_ref()).ok()
41}