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 std::os::raw::c_char;
19
20 use asset_definition::{impl_enum_trait, log_throw_error, AssetError, ErrCode, Result};
21 mod calling_info;
22 mod counter;
23 mod process_info;
24 mod task_manager;
25 pub use calling_info::{CallingInfo, Group};
26 pub use counter::{AutoCounter, Counter};
27 pub use process_info::{ProcessInfo, ProcessInfoDetail};
28 pub use task_manager::TaskManager;
29 /// success code.
30 pub const SUCCESS: i32 = 0;
31 /// root user upper bound.
32 pub const ROOT_USER_UPPERBOUND: u32 = 99;
33 /// Separator in owner info of calling info between app id and app index.
34 pub const OWNER_INFO_SEPARATOR: char = '_';
35 /// Separator in group of calling info between developer id and group id.
36 pub const GROUP_SEPARATOR: char = ',';
37
38 /// Immutable asset blob
39 #[repr(C)]
40 pub struct ConstAssetBlob {
41 /// Data size
42 pub size: u32,
43 /// Immutable data
44 pub data: *const u8,
45 }
46
47 /// Immutable asset blob array
48 #[repr(C)]
49 pub struct ConstAssetBlobArray {
50 /// blobs size
51 pub size: u32,
52 /// Immutable blobs
53 pub blobs: *const ConstAssetBlob,
54 }
55
56 /// Mutable asset blob
57 #[repr(C)]
58 pub struct MutAssetBlob {
59 /// Data size
60 pub size: u32,
61 /// Mutable data
62 pub data: *mut u8,
63 }
64
65 /// Modify asset c char
66 #[repr(C)]
67 pub struct ModifyAssetBlob {
68 /// Modify
69 pub modify: bool,
70 /// Immutable
71 pub blob: *const c_char,
72 }
73
74 /// Mutable asset blob array
75 #[repr(C)]
76 pub struct MutAssetBlobArray {
77 /// blobs size
78 pub size: u32,
79 /// Mutable blobs
80 pub blobs: *mut ModifyAssetBlob,
81 }
82
83
84 impl_enum_trait! {
85 /// The type of the calling.
86 #[repr(C)]
87 #[derive(PartialEq, Eq)]
88 #[derive(Copy, Clone)]
89 #[derive(Debug)]
90 pub enum OwnerType {
91 /// The calling is an application.
92 Hap = 0,
93 /// The calling is a native process.
94 Native = 1,
95 /// The calling is a group of applications.
96 HapGroup = 2,
97 }
98 }
99
100 /// Transfer error code to AssetError
transfer_error_code(err_code: ErrCode) -> AssetError101 pub fn transfer_error_code(err_code: ErrCode) -> AssetError {
102 match err_code {
103 ErrCode::AccessDenied => {
104 AssetError::new(ErrCode::AccessDenied, "[FATAL]HUKS verify auth token failed".to_string())
105 },
106 ErrCode::StatusMismatch => {
107 AssetError::new(ErrCode::StatusMismatch, "[FATAL]Screen status does not match".to_string())
108 },
109 ErrCode::InvalidArgument => AssetError::new(ErrCode::InvalidArgument, "[FATAL]Invalid argument.".to_string()),
110 ErrCode::BmsError => AssetError::new(ErrCode::BmsError, "[FATAL]Get owner info from bms failed.".to_string()),
111 ErrCode::AccessTokenError => {
112 AssetError::new(ErrCode::AccessTokenError, "[FATAL]Get process info failed.".to_string())
113 },
114 _ => AssetError::new(ErrCode::CryptoError, "[FATAL]HUKS execute crypt failed".to_string()),
115 }
116 }
117
118 extern "C" {
GetUserIdByUid(uid: u64, userId: &mut u32) -> bool119 fn GetUserIdByUid(uid: u64, userId: &mut u32) -> bool;
IsUserIdExist(userId: i32, exist: &mut bool) -> bool120 fn IsUserIdExist(userId: i32, exist: &mut bool) -> bool;
121 }
122
123 /// Calculate user id.
get_user_id(uid: u64) -> Result<u32>124 pub fn get_user_id(uid: u64) -> Result<u32> {
125 unsafe {
126 let mut user_id = 0;
127 if GetUserIdByUid(uid, &mut user_id) {
128 Ok(user_id)
129 } else {
130 log_throw_error!(ErrCode::AccountError, "[FATAL]Get user id failed.")
131 }
132 }
133 }
134
135 /// Check user id exist.
is_user_id_exist(user_id: i32) -> Result<bool>136 pub fn is_user_id_exist(user_id: i32) -> Result<bool> {
137 unsafe {
138 let mut exist = false;
139 if IsUserIdExist(user_id, &mut exist) {
140 Ok(exist)
141 } else {
142 log_throw_error!(ErrCode::AccountError, "[FATAL]Check user id failed.")
143 }
144 }
145 }
146