1dfe32fa1Soh_ci/*
2dfe32fa1Soh_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3dfe32fa1Soh_ci * Licensed under the Apache License, Version 2.0 (the "License");
4dfe32fa1Soh_ci * you may not use this file except in compliance with the License.
5dfe32fa1Soh_ci * You may obtain a copy of the License at
6dfe32fa1Soh_ci *
7dfe32fa1Soh_ci *     http://www.apache.org/licenses/LICENSE-2.0
8dfe32fa1Soh_ci *
9dfe32fa1Soh_ci * Unless required by applicable law or agreed to in writing, software
10dfe32fa1Soh_ci * distributed under the License is distributed on an "AS IS" BASIS,
11dfe32fa1Soh_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12dfe32fa1Soh_ci * See the License for the specific language governing permissions and
13dfe32fa1Soh_ci * limitations under the License.
14dfe32fa1Soh_ci */
15dfe32fa1Soh_ci
16dfe32fa1Soh_ci//! This crate defines the common constants.
17dfe32fa1Soh_ci
18dfe32fa1Soh_ciuse asset_definition::{impl_enum_trait, log_throw_error, AssetError, ErrCode, Result};
19dfe32fa1Soh_cimod calling_info;
20dfe32fa1Soh_cimod counter;
21dfe32fa1Soh_cimod process_info;
22dfe32fa1Soh_cipub use calling_info::CallingInfo;
23dfe32fa1Soh_cipub use counter::{AutoCounter, Counter};
24dfe32fa1Soh_cipub use process_info::{ProcessInfo, ProcessInfoDetail};
25dfe32fa1Soh_ci/// success code.
26dfe32fa1Soh_cipub const SUCCESS: i32 = 0;
27dfe32fa1Soh_ci/// root user upper bound
28dfe32fa1Soh_cipub const ROOT_USER_UPPERBOUND: u32 = 99;
29dfe32fa1Soh_ci
30dfe32fa1Soh_ciimpl_enum_trait! {
31dfe32fa1Soh_ci    /// The type of the calling.
32dfe32fa1Soh_ci    #[repr(C)]
33dfe32fa1Soh_ci    #[derive(PartialEq, Eq)]
34dfe32fa1Soh_ci    #[derive(Copy, Clone)]
35dfe32fa1Soh_ci    #[derive(Debug)]
36dfe32fa1Soh_ci    pub enum OwnerType {
37dfe32fa1Soh_ci        /// The calling is a application.
38dfe32fa1Soh_ci        Hap = 0,
39dfe32fa1Soh_ci        /// The calling is a native process.
40dfe32fa1Soh_ci        Native = 1,
41dfe32fa1Soh_ci    }
42dfe32fa1Soh_ci}
43dfe32fa1Soh_ci
44dfe32fa1Soh_ci/// Transfer error code to AssetError
45dfe32fa1Soh_cipub fn transfer_error_code(err_code: ErrCode) -> AssetError {
46dfe32fa1Soh_ci    match err_code {
47dfe32fa1Soh_ci        ErrCode::AccessDenied => {
48dfe32fa1Soh_ci            AssetError::new(ErrCode::AccessDenied, "[FATAL]HUKS verify auth token failed".to_string())
49dfe32fa1Soh_ci        },
50dfe32fa1Soh_ci        ErrCode::StatusMismatch => {
51dfe32fa1Soh_ci            AssetError::new(ErrCode::StatusMismatch, "[FATAL]Screen status does not match".to_string())
52dfe32fa1Soh_ci        },
53dfe32fa1Soh_ci        ErrCode::InvalidArgument => AssetError::new(ErrCode::InvalidArgument, "[FATAL]Invalid argument.".to_string()),
54dfe32fa1Soh_ci        ErrCode::BmsError => AssetError::new(ErrCode::BmsError, "[FATAL]Get owner info from bms failed.".to_string()),
55dfe32fa1Soh_ci        ErrCode::AccessTokenError => {
56dfe32fa1Soh_ci            AssetError::new(ErrCode::AccessTokenError, "[FATAL]Get process info failed.".to_string())
57dfe32fa1Soh_ci        },
58dfe32fa1Soh_ci        _ => AssetError::new(ErrCode::CryptoError, "[FATAL]HUKS execute crypt failed".to_string()),
59dfe32fa1Soh_ci    }
60dfe32fa1Soh_ci}
61dfe32fa1Soh_ci
62dfe32fa1Soh_ciextern "C" {
63dfe32fa1Soh_ci    fn GetUserIdByUid(uid: u64, userId: &mut u32) -> bool;
64dfe32fa1Soh_ci    fn IsUserIdExist(userId: i32, exist: &mut bool) -> bool;
65dfe32fa1Soh_ci}
66dfe32fa1Soh_ci
67dfe32fa1Soh_ci/// Calculate user id.
68dfe32fa1Soh_cipub fn get_user_id(uid: u64) -> Result<u32> {
69dfe32fa1Soh_ci    unsafe {
70dfe32fa1Soh_ci        let mut user_id = 0;
71dfe32fa1Soh_ci        if GetUserIdByUid(uid, &mut user_id) {
72dfe32fa1Soh_ci            Ok(user_id)
73dfe32fa1Soh_ci        } else {
74dfe32fa1Soh_ci            log_throw_error!(ErrCode::AccountError, "[FATAL]Get user id failed.")
75dfe32fa1Soh_ci        }
76dfe32fa1Soh_ci    }
77dfe32fa1Soh_ci}
78dfe32fa1Soh_ci
79dfe32fa1Soh_ci/// Check user id exist.
80dfe32fa1Soh_cipub fn is_user_id_exist(user_id: i32) -> Result<bool> {
81dfe32fa1Soh_ci    unsafe {
82dfe32fa1Soh_ci        let mut exist = false;
83dfe32fa1Soh_ci        if IsUserIdExist(user_id, &mut exist) {
84dfe32fa1Soh_ci            Ok(exist)
85dfe32fa1Soh_ci        } else {
86dfe32fa1Soh_ci            log_throw_error!(ErrCode::AccountError, "[FATAL]Check user id failed.")
87dfe32fa1Soh_ci        }
88dfe32fa1Soh_ci    }
89dfe32fa1Soh_ci}
90