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, Group};
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 /// Separator in owner info of calling info between app id and app index.
30 pub const OWNER_INFO_SEPARATOR: char = '_';
31 /// Separator in group of calling info between developer id and group id.
32 pub const GROUP_SEPARATOR: char = ',';
33
34 /// Immutable asset blob
35 #[repr(C)]
36 pub struct ConstAssetBlob {
37 /// Data size
38 pub size: u32,
39 /// Immutable data
40 pub data: *const u8,
41 }
42
43 /// Immutable asset blob array
44 #[repr(C)]
45 pub struct ConstAssetBlobArray {
46 /// blobs size
47 pub size: u32,
48 /// Immutable blobs
49 pub blobs: *const ConstAssetBlob,
50 }
51
52 /// Mutable asset blob
53 #[repr(C)]
54 pub struct MutAssetBlob {
55 /// Data size
56 pub size: u32,
57 /// Mutable data
58 pub data: *mut u8,
59 }
60
61 impl_enum_trait! {
62 /// The type of the calling.
63 #[repr(C)]
64 #[derive(PartialEq, Eq)]
65 #[derive(Copy, Clone)]
66 #[derive(Debug)]
67 pub enum OwnerType {
68 /// The calling is an application.
69 Hap = 0,
70 /// The calling is a native process.
71 Native = 1,
72 /// The calling is a group of applications.
73 HapGroup = 2,
74 }
75 }
76
77 /// Transfer error code to AssetError
transfer_error_code(err_code: ErrCode) -> AssetError78 pub fn transfer_error_code(err_code: ErrCode) -> AssetError {
79 match err_code {
80 ErrCode::AccessDenied => {
81 AssetError::new(ErrCode::AccessDenied, "[FATAL]HUKS verify auth token failed".to_string())
82 },
83 ErrCode::StatusMismatch => {
84 AssetError::new(ErrCode::StatusMismatch, "[FATAL]Screen status does not match".to_string())
85 },
86 ErrCode::InvalidArgument => AssetError::new(ErrCode::InvalidArgument, "[FATAL]Invalid argument.".to_string()),
87 ErrCode::BmsError => AssetError::new(ErrCode::BmsError, "[FATAL]Get owner info from bms failed.".to_string()),
88 ErrCode::AccessTokenError => {
89 AssetError::new(ErrCode::AccessTokenError, "[FATAL]Get process info failed.".to_string())
90 },
91 _ => AssetError::new(ErrCode::CryptoError, "[FATAL]HUKS execute crypt failed".to_string()),
92 }
93 }
94
95 extern "C" {
GetUserIdByUid(uid: u64, userId: &mut u32) -> bool96 fn GetUserIdByUid(uid: u64, userId: &mut u32) -> bool;
IsUserIdExist(userId: i32, exist: &mut bool) -> bool97 fn IsUserIdExist(userId: i32, exist: &mut bool) -> bool;
98 }
99
100 /// Calculate user id.
get_user_id(uid: u64) -> Result<u32>101 pub fn get_user_id(uid: u64) -> Result<u32> {
102 unsafe {
103 let mut user_id = 0;
104 if GetUserIdByUid(uid, &mut user_id) {
105 Ok(user_id)
106 } else {
107 log_throw_error!(ErrCode::AccountError, "[FATAL]Get user id failed.")
108 }
109 }
110 }
111
112 /// Check user id exist.
is_user_id_exist(user_id: i32) -> Result<bool>113 pub fn is_user_id_exist(user_id: i32) -> Result<bool> {
114 unsafe {
115 let mut exist = false;
116 if IsUserIdExist(user_id, &mut exist) {
117 Ok(exist)
118 } else {
119 log_throw_error!(ErrCode::AccountError, "[FATAL]Check user id failed.")
120 }
121 }
122 }
123