1 /*
2  * Copyright (c) 2023 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 //! This crate defines the common constants.
17 
18 use asset_definition::{impl_enum_trait, log_throw_error, AssetError, ErrCode, Result};
19 mod calling_info;
20 mod counter;
21 mod process_info;
22 pub use calling_info::CallingInfo;
23 pub use counter::{AutoCounter, Counter};
24 pub use process_info::{ProcessInfo, ProcessInfoDetail};
25 /// success code.
26 pub const SUCCESS: i32 = 0;
27 /// root user upper bound
28 pub const ROOT_USER_UPPERBOUND: u32 = 99;
29 
30 impl_enum_trait! {
31     /// The type of the calling.
32     #[repr(C)]
33     #[derive(PartialEq, Eq)]
34     #[derive(Copy, Clone)]
35     #[derive(Debug)]
36     pub enum OwnerType {
37         /// The calling is a application.
38         Hap = 0,
39         /// The calling is a native process.
40         Native = 1,
41     }
42 }
43 
44 /// Transfer error code to AssetError
transfer_error_code(err_code: ErrCode) -> AssetError45 pub fn transfer_error_code(err_code: ErrCode) -> AssetError {
46     match err_code {
47         ErrCode::AccessDenied => {
48             AssetError::new(ErrCode::AccessDenied, "[FATAL]HUKS verify auth token failed".to_string())
49         },
50         ErrCode::StatusMismatch => {
51             AssetError::new(ErrCode::StatusMismatch, "[FATAL]Screen status does not match".to_string())
52         },
53         ErrCode::InvalidArgument => AssetError::new(ErrCode::InvalidArgument, "[FATAL]Invalid argument.".to_string()),
54         ErrCode::BmsError => AssetError::new(ErrCode::BmsError, "[FATAL]Get owner info from bms failed.".to_string()),
55         ErrCode::AccessTokenError => {
56             AssetError::new(ErrCode::AccessTokenError, "[FATAL]Get process info failed.".to_string())
57         },
58         _ => AssetError::new(ErrCode::CryptoError, "[FATAL]HUKS execute crypt failed".to_string()),
59     }
60 }
61 
62 extern "C" {
GetUserIdByUid(uid: u64, userId: &mut u32) -> bool63     fn GetUserIdByUid(uid: u64, userId: &mut u32) -> bool;
IsUserIdExist(userId: i32, exist: &mut bool) -> bool64     fn IsUserIdExist(userId: i32, exist: &mut bool) -> bool;
65 }
66 
67 /// Calculate user id.
get_user_id(uid: u64) -> Result<u32>68 pub fn get_user_id(uid: u64) -> Result<u32> {
69     unsafe {
70         let mut user_id = 0;
71         if GetUserIdByUid(uid, &mut user_id) {
72             Ok(user_id)
73         } else {
74             log_throw_error!(ErrCode::AccountError, "[FATAL]Get user id failed.")
75         }
76     }
77 }
78 
79 /// Check user id exist.
is_user_id_exist(user_id: i32) -> Result<bool>80 pub fn is_user_id_exist(user_id: i32) -> Result<bool> {
81     unsafe {
82         let mut exist = false;
83         if IsUserIdExist(user_id, &mut exist) {
84             Ok(exist)
85         } else {
86             log_throw_error!(ErrCode::AccountError, "[FATAL]Check user id failed.")
87         }
88     }
89 }
90