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