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