1 /*
2 * Copyright (c) 2021-2024 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 "hiappevent_c.h"
17
18 #include <memory>
19 #include <vector>
20
21 #include "ffrt.h"
22 #include "hiappevent_base.h"
23 #include "hiappevent_clean.h"
24 #include "hiappevent_config.h"
25 #include "hiappevent_verify.h"
26 #include "hiappevent_write.h"
27 #include "hilog/log.h"
28
29 #undef LOG_DOMAIN
30 #define LOG_DOMAIN 0xD002D07
31
32 #undef LOG_TAG
33 #define LOG_TAG "AppEventC"
34
35 using namespace OHOS::HiviewDFX;
36
37 namespace {
38 constexpr int MAX_SIZE_OF_LIST_PARAM = 100;
39
40 template<typename T>
AddArrayParam(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const T * arr,int len)41 void AddArrayParam(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const T* arr, int len)
42 {
43 std::vector<T> params(arr, (len > MAX_SIZE_OF_LIST_PARAM) ? (arr + MAX_SIZE_OF_LIST_PARAM + 1) : (arr + len));
44 appEventPack->AddParam(name, params);
45 }
46
47 using ParamAdder = void (*)(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value);
48
AddBoolParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)49 void AddBoolParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
50 {
51 appEventPack->AddParam(name, value->value.bool_v);
52 }
53
AddBoolArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)54 void AddBoolArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
55 {
56 AddArrayParam(appEventPack, name, value->value.bool_arr_v, value->arrSize);
57 }
58
AddInt8ParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)59 void AddInt8ParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
60 {
61 appEventPack->AddParam(name, value->value.int8_v);
62 }
63
AddInt8ArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)64 void AddInt8ArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
65 {
66 AddArrayParam(appEventPack, name, value->value.int8_arr_v, value->arrSize);
67 }
68
AddInt16ParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)69 void AddInt16ParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
70 {
71 appEventPack->AddParam(name, value->value.int16_v);
72 }
73
AddInt16ArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)74 void AddInt16ArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
75 {
76 AddArrayParam(appEventPack, name, value->value.int16_arr_v, value->arrSize);
77 }
78
AddInt32ParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)79 void AddInt32ParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
80 {
81 appEventPack->AddParam(name, value->value.int32_v);
82 }
83
AddInt32ArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)84 void AddInt32ArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
85 {
86 AddArrayParam(appEventPack, name, value->value.int32_arr_v, value->arrSize);
87 }
88
AddInt64ParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)89 void AddInt64ParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
90 {
91 appEventPack->AddParam(name, value->value.int64_v);
92 }
93
AddInt64ArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)94 void AddInt64ArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
95 {
96 AddArrayParam(appEventPack, name, value->value.int64_arr_v, value->arrSize);
97 }
98
AddFloatParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)99 void AddFloatParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
100 {
101 appEventPack->AddParam(name, value->value.float_v);
102 }
103
AddFloatArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)104 void AddFloatArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
105 {
106 AddArrayParam(appEventPack, name, value->value.float_arr_v, value->arrSize);
107 }
108
AddDoubleParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)109 void AddDoubleParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
110 {
111 appEventPack->AddParam(name, value->value.double_v);
112 }
113
AddDoubleArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)114 void AddDoubleArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
115 {
116 AddArrayParam(appEventPack, name, value->value.double_arr_v, value->arrSize);
117 }
118
AddStringParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)119 void AddStringParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
120 {
121 appEventPack->AddParam(name, value->value.str_v);
122 }
123
AddStringArrayParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)124 void AddStringArrayParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
125 {
126 AddArrayParam(appEventPack, name, value->value.str_arr_v, value->arrSize);
127 }
128
129 const ParamAdder PARAM_ADDERS[] = {
130 &AddBoolParamValue,
131 &AddBoolArrayParamValue,
132 &AddInt8ParamValue,
133 &AddInt8ArrayParamValue,
134 &AddInt16ParamValue,
135 &AddInt16ArrayParamValue,
136 &AddInt32ParamValue,
137 &AddInt32ArrayParamValue,
138 &AddInt64ParamValue,
139 &AddInt64ArrayParamValue,
140 &AddFloatParamValue,
141 &AddFloatArrayParamValue,
142 &AddDoubleParamValue,
143 &AddDoubleArrayParamValue,
144 &AddStringParamValue,
145 &AddStringArrayParamValue
146 };
147
AddParamValue(std::shared_ptr<AppEventPack> & appEventPack,const char * name,const ParamValue * value)148 void AddParamValue(std::shared_ptr<AppEventPack>& appEventPack, const char* name, const ParamValue* value)
149 {
150 if (name == nullptr || value == nullptr) {
151 HILOG_ERROR(LOG_CORE, "Failed to add the param because the name or value is null.");
152 return;
153 }
154 unsigned int paramType = value->type;
155 if (paramType < (sizeof(PARAM_ADDERS) / sizeof(PARAM_ADDERS[0]))) {
156 PARAM_ADDERS[paramType](appEventPack, name, value);
157 } else {
158 HILOG_ERROR(LOG_CORE, "Failed to add the param because the param type is unknown.");
159 }
160 }
161
AddParamEntry(std::shared_ptr<AppEventPack> & appEventPack,const ParamEntry * entry)162 void AddParamEntry(std::shared_ptr<AppEventPack>& appEventPack, const ParamEntry* entry)
163 {
164 if (entry == nullptr) {
165 HILOG_ERROR(LOG_CORE, "Failed to add the param because the entry is null.");
166 return;
167 }
168 AddParamValue(appEventPack, entry->name, entry->value);
169 }
170
AddParamList(std::shared_ptr<AppEventPack> & appEventPack,const ParamList list)171 void AddParamList(std::shared_ptr<AppEventPack>& appEventPack, const ParamList list)
172 {
173 ParamList curNode = list;
174 while (curNode != nullptr) {
175 AddParamEntry(appEventPack, curNode->entry);
176 curNode = curNode->next;
177 }
178 }
179 }
180
HiAppEventInnerConfigure(const char * name,const char * value)181 bool HiAppEventInnerConfigure(const char* name, const char* value)
182 {
183 if (name == nullptr || value == nullptr) {
184 HILOG_ERROR(LOG_CORE, "Failed to configure, because the input params contain a null pointer.");
185 return false;
186 }
187 return HiAppEventConfig::GetInstance().SetConfigurationItem(name, value);
188 }
189
HiAppEventInnerWrite(const char * domain,const char * name,EventType type,const ParamList list)190 int HiAppEventInnerWrite(const char* domain, const char* name, EventType type, const ParamList list)
191 {
192 if (domain == nullptr) {
193 HILOG_ERROR(LOG_CORE, "Failed to write event, domain is null");
194 return ErrorCode::ERROR_INVALID_EVENT_DOMAIN;
195 }
196 if (name == nullptr) {
197 HILOG_ERROR(LOG_CORE, "Failed to write event, name is null");
198 return ErrorCode::ERROR_INVALID_EVENT_NAME;
199 }
200
201 std::shared_ptr<AppEventPack> appEventPack = std::make_shared<AppEventPack>(domain, name, type);
202 AddParamList(appEventPack, list);
203 int res = VerifyAppEvent(appEventPack);
204 if (res >= 0) {
205 ffrt::submit([appEventPack]() {
206 WriteEvent(appEventPack);
207 }, {}, {}, ffrt::task_attr().name("app_inner_event"));
208 }
209 return res;
210 }
211
ClearData()212 void ClearData()
213 {
214 HiAppEventClean::ClearData(HiAppEventConfig::GetInstance().GetStorageDir());
215 }