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