• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: 0xd001510,
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(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
57 pub enum IpcStatusCode {
58     /// success
59     Ok = 1,
60     /// failed
61     Failed = -1,
62     /// RemoteObj Err Code
63     /// Invalide Params
64     Einval = 22,
65     /// Object is null
66     ErrNullObject = 7,
67     /// The object has died
68     ErrDeadObject = -32,
69     /// invail value
70     InvalidValue = 0,
71     /// unknow value
72     Unknow = 99999,
73 }
74 
75 impl Error for IpcStatusCode {}
76 
77 impl fmt::Display for IpcStatusCode {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result78     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79         match *self {
80             IpcStatusCode::Ok => write!(f, "Call Ok"),
81             IpcStatusCode::Failed => write!(f, "Call Failed"),
82             IpcStatusCode::Einval => write!(f, "Invalid Params"),
83             IpcStatusCode::ErrNullObject => write!(f, "Null Obj"),
84             IpcStatusCode::ErrDeadObject  => write!(f, "Dead Obj"),
85             IpcStatusCode::InvalidValue => write!(f, "Invalid Value"),
86             _  => write!(f, "Unknow Error"),
87         }
88     }
89 }