Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 1 | /* |
| 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 Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 19 | pub mod lazy_service; |
Victor Hsieh | bb7dd08 | 2021-10-12 16:39:02 -0700 | [diff] [blame] | 20 | pub mod rpc_client; |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 21 | pub mod rpc_server; |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 22 | |
Stephen Crane | 44fd9bc | 2022-01-19 17:49:46 +0000 | [diff] [blame^] | 23 | use binder::{ExceptionCode, Status}; |
Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 24 | use std::ffi::CString; |
| 25 | |
| 26 | /// Constructs a new Binder error `Status` with the given `ExceptionCode` and message. |
| 27 | pub fn new_binder_exception<T: AsRef<str>>(exception: ExceptionCode, message: T) -> Status { |
| 28 | match exception { |
| 29 | ExceptionCode::SERVICE_SPECIFIC => new_binder_service_specific_error(-1, message), |
| 30 | _ => Status::new_exception(exception, to_cstring(message).as_deref()), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /// Constructs a Binder `Status` representing a service-specific exception with the given code and |
| 35 | /// message. |
| 36 | pub fn new_binder_service_specific_error<T: AsRef<str>>(code: i32, message: T) -> Status { |
| 37 | Status::new_service_specific_error(code, to_cstring(message).as_deref()) |
| 38 | } |
| 39 | |
| 40 | fn to_cstring<T: AsRef<str>>(message: T) -> Option<CString> { |
| 41 | CString::new(message.as_ref()).ok() |
| 42 | } |