• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "hks_report_check_key_exited.h"
17 
18 #include <cstdint>
19 #include <string>
20 #include <ctime>
21 #include "hks_event_info.h"
22 #include "hks_log.h"
23 #include "hks_param.h"
24 #include "hks_template.h"
25 #include "hks_type.h"
26 #include "hks_type_enum.h"
27 #include "hks_type_inner.h"
28 #include "hks_report_common.h"
29 
30 
PreConstructCheckKeyExitedReportParamSet(const struct HksBlob * keyAlias,const struct HksParamSet * paramSetIn,uint64_t startTime,struct HksParamSet ** paramSetOut)31 int32_t PreConstructCheckKeyExitedReportParamSet(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn,
32     uint64_t startTime, struct HksParamSet **paramSetOut)
33 {
34     HKS_IF_TRUE_LOGI_RETURN(keyAlias == nullptr || paramSetIn == nullptr, HKS_ERROR_NULL_POINTER,
35         "PreConstructCheckKeyExitedReportParamSet params is null")
36     int32_t ret = HksInitParamSet(paramSetOut);
37     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "ConstructGenKeyReportParamSet InitParamSet failed")
38 
39     do {
40         ret = PreAddCommonInfo(*paramSetOut, keyAlias, paramSetIn, startTime);
41         HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "pre add common info to params failed!")
42 
43         struct HksParam params[] = {
44             {
45                 .tag = HKS_TAG_PARAM1_UINT32,
46                 .uint32Param = HKS_EVENT_CHECK_KEY_EXISTED
47             },
48             {
49                 .tag = HKS_TAG_PARAM0_UINT32,
50                 .uint32Param = HKS_EVENT_CHECK_KEY_EXISTED
51             },
52         };
53         ret = HksAddParams(*paramSetOut, params, HKS_ARRAY_SIZE(params));
54         HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "add in params failed!")
55     } while (0);
56     if (ret != HKS_SUCCESS) {
57         HKS_LOG_I("PreConstructCheckKeyExitedReportParamSet failed");
58         HksFreeParamSet(paramSetOut);
59     }
60     return ret;
61 }
62 
HksParamSetToEventInfoForCheckKeyExited(const struct HksParamSet * paramSetIn,struct HksEventInfo * eventInfo)63 int32_t HksParamSetToEventInfoForCheckKeyExited(const struct HksParamSet *paramSetIn, struct HksEventInfo *eventInfo)
64 {
65     HKS_IF_NOT_TRUE_LOGI_RETURN(paramSetIn != nullptr && eventInfo != nullptr, HKS_ERROR_NULL_POINTER,
66         "HksParamSetToEventInfoForCheckKeyExited params is null")
67     int32_t ret = HKS_SUCCESS;
68     do {
69         ret = GetCommonEventInfo(paramSetIn, eventInfo);
70         HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "report GetCommonEventInfo failed!  ret = %" LOG_PUBLIC "d", ret);
71 
72         ret = GetEventKeyInfo(paramSetIn, &(eventInfo->keyInfo));
73         HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "report GetEventKeyInfo failed!  ret = %" LOG_PUBLIC "d", ret);
74     } while (0);
75     if (ret != HKS_SUCCESS) {
76         HKS_LOG_I("report ParamSetToEventInfo failed!  ret = %" LOG_PUBLIC "d", ret);
77         FreeEventInfoSpecificPtr(eventInfo);
78     }
79     return ret;
80 }
81 
HksEventInfoIsNeedReportForCheckKeyExited(const struct HksEventInfo * eventInfo)82 bool HksEventInfoIsNeedReportForCheckKeyExited(const struct HksEventInfo *eventInfo)
83 {
84     return ((eventInfo != nullptr) && (eventInfo->common.result.code != HKS_SUCCESS) &&
85         (eventInfo->common.result.code != HKS_ERROR_NOT_EXIST));
86 }
87 
HksEventInfoIsEqualForCheckKeyExited(const struct HksEventInfo * eventInfo1,const struct HksEventInfo * eventInfo2)88 bool HksEventInfoIsEqualForCheckKeyExited(const struct HksEventInfo *eventInfo1, const struct HksEventInfo *eventInfo2)
89 {
90     return CheckEventCommon(eventInfo1, eventInfo2);
91 }
92 
HksEventInfoAddForCheckKeyExited(struct HksEventInfo * dstEventInfo,const struct HksEventInfo * srcEventInfo)93 void HksEventInfoAddForCheckKeyExited(struct HksEventInfo *dstEventInfo, const struct HksEventInfo *srcEventInfo)
94 {
95     if (HksEventInfoIsEqualForCheckKeyExited(dstEventInfo, srcEventInfo)) {
96         dstEventInfo->common.count++;
97     }
98 }
99 
HksEventInfoToMapForCheckKeyExited(const struct HksEventInfo * eventInfo,std::unordered_map<std::string,std::string> & reportData)100 int32_t HksEventInfoToMapForCheckKeyExited(const struct HksEventInfo *eventInfo,
101     std::unordered_map<std::string, std::string> &reportData)
102 {
103     HKS_IF_NULL_LOGI_RETURN(eventInfo, HKS_ERROR_NULL_POINTER, "HksEventInfoToMapForCheckKeyExited evenInfo is null")
104     auto ret = EventInfoToMapKeyInfo(&eventInfo->keyInfo, reportData);
105     HKS_IF_NOT_TRUE_LOGI_RETURN(ret.second, HKS_ERROR_BUFFER_TOO_SMALL,
106         "HksEventInfoToMapForCheckKeyExited failed! reportData insert failed!")
107     return HKS_SUCCESS;
108 }