1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 use std::error::Error;
17 use std::fmt;
18 use std::ffi::{CString, c_char};
19 use hilog_rust::{debug, hilog, HiLogLabel, LogType};
20
21 const LOG_LABEL: HiLogLabel = HiLogLabel {
22 log_type: LogType::LogCore,
23 domain: 0xD0057CA,
24 tag: "RustStatus"
25 };
26
27 /// IPC specific Result, error is i32 type
28 pub type IpcResult<T> = std::result::Result<T, IpcStatusCode>;
29
30 /// usage:
31 /// status_result::<()>(result, ())
32 /// or
33 /// status_result::<MsgParcel>(result, reply)
status_result<T>(code: i32, val: T) -> IpcResult<T>34 pub fn status_result<T>(code: i32, val: T) -> IpcResult<T> {
35 debug!(LOG_LABEL, "rust status code: {}", code);
36 match parse_status_code(code) {
37 IpcStatusCode::Ok => Ok(val),
38 e => Err(e),
39 }
40 }
41
42 /// Parse status code
parse_status_code(code: i32) -> IpcStatusCode43 pub fn parse_status_code(code: i32) -> IpcStatusCode {
44 match code {
45 e if e == IpcStatusCode::Ok as i32 => IpcStatusCode::Ok,
46 e if e == IpcStatusCode::Failed as i32 => IpcStatusCode::Failed,
47 e if e == IpcStatusCode::Einval as i32 => IpcStatusCode::Einval,
48 e if e == IpcStatusCode::ErrNullObject as i32 => IpcStatusCode::ErrNullObject,
49 e if e == IpcStatusCode::ErrDeadObject as i32 => IpcStatusCode::ErrDeadObject,
50 e if e == IpcStatusCode::InvalidValue as i32 => IpcStatusCode::InvalidValue,
51 _ => IpcStatusCode::Unknow,
52 }
53 }
54
55 /// IPC unified status code
56 #[derive(Hash)]
57 #[derive(Eq, PartialEq)]
58 #[derive(Ord, PartialOrd)]
59 #[derive(Clone, Copy)]
60 #[derive(Debug)]
61 #[non_exhaustive]
62 pub enum IpcStatusCode {
63 /// success
64 Ok = 1,
65 /// failed
66 Failed = -1,
67 /// RemoteObj Err Code
68 /// Invalide Params
69 Einval = 22,
70 /// Object is null
71 ErrNullObject = 7,
72 /// The object has died
73 ErrDeadObject = -32,
74 /// invail value
75 InvalidValue = 0,
76 /// unknow value
77 Unknow = 99999,
78 }
79
80 impl Error for IpcStatusCode {}
81
82 /// # Safety
83 ///
84 /// IpcStatusCode is an enumeration type that can exist in multiple threads.
85 unsafe impl Send for IpcStatusCode {}
86
87 impl fmt::Display for IpcStatusCode {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result88 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89 match *self {
90 IpcStatusCode::Ok => write!(f, "Call Ok"),
91 IpcStatusCode::Failed => write!(f, "Call Failed"),
92 IpcStatusCode::Einval => write!(f, "Invalid Params"),
93 IpcStatusCode::ErrNullObject => write!(f, "Null Obj"),
94 IpcStatusCode::ErrDeadObject => write!(f, "Dead Obj"),
95 IpcStatusCode::InvalidValue => write!(f, "Invalid Value"),
96 _ => write!(f, "Unknow Error"),
97 }
98 }
99 }