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 module is used to Asset service hisysevent.
17 
18 use std::time::Instant;
19 
20 use ipc::Skeleton;
21 
22 use asset_common::CallingInfo;
23 use asset_definition::{AssetError, Result};
24 use asset_log::{loge, logi};
25 
26 use hisysevent::{build_number_param, build_str_param, write, EventType, HiSysEventParam};
27 
28 /// System events structure which base on `Hisysevent`.
29 struct SysEvent<'a> {
30     event_type: EventType,
31     params: Vec<HiSysEventParam<'a>>,
32 }
33 
34 impl<'a> SysEvent<'a> {
35     const DOMAIN: &str = "ASSET";
36     const ASSET_FAULT: &str = "SECRET_STORE_OPERATION_FAILED";
37     const ASSET_STATISTIC: &str = "SECRET_STORE_INFO_COLLECTION";
38 
39     pub(crate) const FUNCTION: &str = "FUNCTION";
40     pub(crate) const USER_ID: &str = "USER_ID";
41     pub(crate) const CALLER: &str = "CALLER";
42     pub(crate) const ERROR_CODE: &str = "ERROR_CODE";
43     pub(crate) const RUN_TIME: &str = "RUN_TIME";
44     pub(crate) const EXTRA: &str = "EXTRA";
45 
new(event_type: EventType) -> Self46     fn new(event_type: EventType) -> Self {
47         Self { event_type, params: Vec::new() }
48     }
49 
set_param(mut self, param: HiSysEventParam<'a>) -> Self50     fn set_param(mut self, param: HiSysEventParam<'a>) -> Self {
51         self.params.push(param);
52         self
53     }
54 
write(self)55     fn write(self) {
56         let event_name = match self.event_type {
57             EventType::Fault => Self::ASSET_FAULT,
58             EventType::Statistic => Self::ASSET_STATISTIC,
59             _ => "UNKNOWN_EVENT",
60         };
61         write(Self::DOMAIN, event_name, self.event_type, self.params.as_slice());
62     }
63 }
64 
upload_statistic_system_event(calling_info: &CallingInfo, start_time: Instant, func_name: &str)65 pub(crate) fn upload_statistic_system_event(calling_info: &CallingInfo, start_time: Instant, func_name: &str) {
66     let duration = start_time.elapsed();
67     let owner_info = String::from_utf8_lossy(calling_info.owner_info()).to_string();
68     SysEvent::new(EventType::Statistic)
69         .set_param(build_str_param!(SysEvent::FUNCTION, func_name))
70         .set_param(build_number_param!(SysEvent::USER_ID, calling_info.user_id()))
71         .set_param(build_str_param!(SysEvent::CALLER, owner_info.clone()))
72         .set_param(build_number_param!(SysEvent::RUN_TIME, duration.as_millis() as u32))
73         .set_param(build_str_param!(SysEvent::EXTRA, format!("CallingUid={}", Skeleton::calling_uid())))
74         .write();
75     logi!(
76         "[INFO]Calling fun:[{}], user_id:[{}], caller:[{}], start_time:[{:?}], run_time:[{}]",
77         func_name,
78         calling_info.user_id(),
79         owner_info,
80         start_time,
81         duration.as_millis()
82     )
83 }
84 
upload_fault_system_event( calling_info: &CallingInfo, start_time: Instant, func_name: &str, e: &AssetError, )85 pub(crate) fn upload_fault_system_event(
86     calling_info: &CallingInfo,
87     start_time: Instant,
88     func_name: &str,
89     e: &AssetError,
90 ) {
91     let owner_info = String::from_utf8_lossy(calling_info.owner_info()).to_string();
92     SysEvent::new(EventType::Fault)
93         .set_param(build_str_param!(SysEvent::FUNCTION, func_name))
94         .set_param(build_number_param!(SysEvent::USER_ID, calling_info.user_id()))
95         .set_param(build_str_param!(SysEvent::CALLER, owner_info.clone()))
96         .set_param(build_number_param!(SysEvent::ERROR_CODE, e.code as i32))
97         .set_param(build_str_param!(SysEvent::EXTRA, e.msg.clone()))
98         .write();
99     loge!(
100         "[ERROR]Calling fun:[{}], user_id:[{}], caller:[{}], start_time:[{:?}], error_code:[{}], error_msg:[{}]",
101         func_name,
102         calling_info.user_id(),
103         owner_info,
104         start_time,
105         e.code,
106         e.msg.clone()
107     );
108 }
109 
upload_system_event<T>( result: Result<T>, calling_info: &CallingInfo, start_time: Instant, func_name: &str, ) -> Result<T>110 pub(crate) fn upload_system_event<T>(
111     result: Result<T>,
112     calling_info: &CallingInfo,
113     start_time: Instant,
114     func_name: &str,
115 ) -> Result<T> {
116     match &result {
117         Ok(_) => upload_statistic_system_event(calling_info, start_time, func_name),
118         Err(e) => upload_fault_system_event(calling_info, start_time, func_name, e),
119     }
120     result
121 }
122