1 /*
2  * Copyright (c) 2022-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 #include "hisysevent_operation.h"
17 
18 #include "def.h"
19 #include "stats_log.h"
20 #include "string_filter.h"
21 
22 using namespace OHOS::HiviewDFX;
23 
24 namespace OHOS {
25 namespace PowerMgr {
IsError(HiSysEventOperation::EventBase & eventBase)26 bool HiSysEventOperation::IsError(HiSysEventOperation::EventBase &eventBase)
27 {
28     return (eventBase.retCode_ < SUCCESS);
29 }
30 
ExplainRetCode(HiSysEventOperation::EventBase & eventBase)31 void HiSysEventOperation::ExplainRetCode(HiSysEventOperation::EventBase &eventBase)
32 {
33     if (eventBase.retCode_ > SUCCESS) {
34         STATS_HILOGD(LABEL_TEST, "invalid data, error=%{public}d, message=%{public}s",
35             eventBase.retCode_, ERR_MSG_LEVEL1[eventBase.retCode_ - 1]);
36     } else if (eventBase.retCode_ < SUCCESS) {
37         STATS_HILOGD(LABEL_TEST, "discard data, error=%{public}d, message=%{public}s",
38             eventBase.retCode_, ERR_MSG_LEVEL0[-eventBase.retCode_ - 1]);
39     }
40 }
41 
CheckKey(const std::string & key)42 int HiSysEventOperation::CheckKey(const std::string &key)
43 {
44     if (!StringFilter::GetInstance().IsValidName(key, MAX_PARAM_NAME_LENGTH)) {
45         return ERR_KEY_NAME_INVALID;
46     }
47     return SUCCESS;
48 }
49 
IsWarnAndUpdate(int retCode,EventBase & eventBase)50 bool HiSysEventOperation::IsWarnAndUpdate(int retCode, EventBase &eventBase)
51 {
52     if (retCode != SUCCESS) {
53         eventBase.retCode_ = retCode;
54         return true;
55     }
56     return false;
57 }
58 
UpdateAndCheckKeyNumIsOver(HiSysEventOperation::EventBase & eventBase)59 bool HiSysEventOperation::UpdateAndCheckKeyNumIsOver(HiSysEventOperation::EventBase &eventBase)
60 {
61     eventBase.keyCnt_++;
62     if (eventBase.keyCnt_ > MAX_PARAM_NUMBER) {
63         eventBase.retCode_ = ERR_KEY_NUMBER_TOO_MUCH;
64         return true;
65     }
66     return false;
67 }
68 
CombineBaseInfo(HiSysEventOperation::EventBase & eventBase)69 void HiSysEventOperation::CombineBaseInfo(HiSysEventOperation::EventBase &eventBase)
70 {
71     if (!StringFilter::GetInstance().IsValidName(eventBase.domain_, MAX_DOMAIN_LENGTH)) {
72         eventBase.retCode_ = ERR_DOMAIN_NAME_INVALID;
73         return;
74     }
75     if (!StringFilter::GetInstance().IsValidName(eventBase.eventName_, MAX_EVENT_NAME_LENGTH)) {
76         eventBase.retCode_ = ERR_EVENT_NAME_INVALID;
77         return;
78     }
79     AppendData(eventBase, "domain_", eventBase.domain_);
80     AppendData(eventBase, "name_", eventBase.eventName_);
81     AppendData(eventBase, "type_", eventBase.type_);
82     eventBase.keyCnt_ = 0;
83 }
84 
InnerCombine(HiSysEventOperation::EventBase & eventBase)85 void HiSysEventOperation::InnerCombine(HiSysEventOperation::EventBase &eventBase)
86 {
87     if (eventBase.jsonStr_.tellp() != 0) {
88         eventBase.jsonStr_.seekp(-1, std::ios_base::end);
89     }
90 }
91 
AppendValue(HiSysEventOperation::EventBase & eventBase,const std::string & item)92 void HiSysEventOperation::AppendValue(HiSysEventOperation::EventBase &eventBase, const std::string &item)
93 {
94     std::string text = item;
95     if (item.length() > MAX_STRING_LENGTH) {
96         text = item.substr(0, MAX_STRING_LENGTH);
97         eventBase.retCode_ = ERR_VALUE_LENGTH_TOO_LONG;
98     }
99     eventBase.jsonStr_ << "\"" << StringFilter::GetInstance().EscapeToRaw(text) << "\"";
100 }
101 
AppendValue(HiSysEventOperation::EventBase & eventBase,const char item)102 void HiSysEventOperation::AppendValue(HiSysEventOperation::EventBase &eventBase, const char item)
103 {
104     eventBase.jsonStr_ << static_cast<short>(item);
105 }
106 
AppendValue(EventBase & eventBase,const signed char item)107 void HiSysEventOperation::AppendValue(EventBase &eventBase, const signed char item)
108 {
109     eventBase.jsonStr_ << static_cast<short>(item);
110 }
111 
AppendValue(HiSysEventOperation::EventBase & eventBase,const unsigned char item)112 void HiSysEventOperation::AppendValue(HiSysEventOperation::EventBase &eventBase, const unsigned char item)
113 {
114     eventBase.jsonStr_ << static_cast<unsigned short>(item);
115 }
116 } // namespace PowerMgr
117 } // namespace OHOS