1 /*
2  * Copyright (C) 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 "event_report.h"
17 
18 #include <vector>
19 
20 #include "effect_log.h"
21 #include "hisysevent_c.h"
22 #include "file_ex.h"
23 
24 namespace OHOS {
25 namespace Media {
26 namespace Effect {
27 namespace {
28 constexpr char IMAGE_EFFECT_UE[] = "IMAGE_EFFECT_UE";
29 const std::string DEFAULT_STR = "default";
30 const std::string DEFAULT_PACKAGE_NAME = "entry";
31 const std::string DEFAULT_VERSION_ID = "1";
32 const std::string PROC_SELF_CMDLINE_PATH = "/proc/self/cmdline";
33 }
34 
35 std::unordered_map<std::string, void (*)(const EventInfo &eventInfo)> EventReport::sysEventFuncMap_ = {
__anon629d4e640202() 36     { REGISTER_CUSTOM_FILTER_STATISTIC, [](const EventInfo &eventInfo) {
37         ReportRegisterCustomFilterEvent(eventInfo);
38     }},
__anon629d4e640302() 39     { ADD_FILTER_STATISTIC, [](const EventInfo &eventInfo) {
40         ReportAddFilterEvent(eventInfo);
41     }},
__anon629d4e640402() 42     { REMOVE_FILTER_STATISTIC, [](const EventInfo &eventInfo) {
43         ReportRemoveFilterEvent(eventInfo);
44     }},
__anon629d4e640502() 45     { INPUT_DATA_TYPE_STATISTIC, [](const EventInfo &eventInfo) {
46         ReportInputDataTypeEvent(eventInfo);
47     }},
__anon629d4e640602() 48     { OUTPUT_DATA_TYPE_STATISTIC, [](const EventInfo &eventInfo) {
49         ReportOutputDataTypeEvent(eventInfo);
50     }},
__anon629d4e640702() 51     { RENDER_FAILED_FAULT, [](const EventInfo &eventInfo) {
52         ReportRenderFailedEvent(eventInfo);
53     }},
__anon629d4e640802() 54     { SAVE_IMAGE_EFFECT_BEHAVIOR, [](const EventInfo &eventInfo) {
55         ReportSaveImageEffectEvent(eventInfo);
56     }},
__anon629d4e640902() 57     { RESTORE_IMAGE_EFFECT_BEHAVIOR, [](const EventInfo &eventInfo) {
58         ReportRestoreImageEffectEvent(eventInfo);
59     }},
60 };
61 
62 std::unordered_map<EventDataType, std::string> EventReport::sysEventDataTypeMap_ = {
63     { EventDataType::PIXEL_MAP, "pixelmap" },
64     { EventDataType::URI, "uri" },
65     { EventDataType::SURFACE, "surface" },
66     { EventDataType::SURFACE_BUFFER, "surfacebuffer" },
67 };
68 
GetCurrentProcessName()69 std::string GetCurrentProcessName()
70 {
71     std::string procName;
72     std::string cmdline;
73     if (OHOS::LoadStringFromFile(PROC_SELF_CMDLINE_PATH, cmdline)) {
74         size_t pos = cmdline.find_first_of('\0');
75         if (pos != std::string::npos) {
76             procName = cmdline.substr(0, pos);
77         }
78     }
79     return procName;
80 }
81 
EventWrite(const char * func,int64_t line,const char * name,HiSysEventEventType type,std::vector<HiSysEventParam> & params)82 void EventWrite(const char *func, int64_t line, const char *name, HiSysEventEventType type,
83     std::vector<HiSysEventParam> &params)
84 {
85     std::string processName = GetCurrentProcessName();
86     HiSysEventParam processNamePara = {
87         .name = "PNAMEID",
88         .t = HISYSEVENT_STRING,
89         .v = { .s = processName.empty() ? const_cast<char *>(DEFAULT_PACKAGE_NAME.c_str()) :
90             const_cast<char *>(processName.c_str()) },
91         .arraySize = 0,
92     };
93     HiSysEventParam versionPara = {
94         .name = "PVERSIONID",
95         .t = HISYSEVENT_STRING,
96         .v = { .s = const_cast<char *>(DEFAULT_VERSION_ID.c_str()) },
97         .arraySize = 0,
98     };
99     params.insert(params.begin(), processNamePara);
100     params.insert(params.begin() + 1, versionPara);
101     HiSysEvent_Write(func, line, IMAGE_EFFECT_UE, name, type, params.data(), params.size());
102 }
103 
ReportHiSysEvent(const std::string & eventName,const EventInfo & eventInfo)104 void EventReport::ReportHiSysEvent(const std::string &eventName, const EventInfo &eventInfo)
105 {
106     auto iter = sysEventFuncMap_.find(eventName);
107     if (iter == sysEventFuncMap_.end()) {
108         EFFECT_LOGE("ReportHiSysEvent: eventName is not supported! eventName=%{public}s", eventName.c_str());
109         return;
110     }
111 
112     iter->second(eventInfo);
113 }
114 
ReportRegisterCustomFilterEvent(const EventInfo & eventInfo)115 void EventReport::ReportRegisterCustomFilterEvent(const EventInfo &eventInfo)
116 {
117     HiSysEventParam processNamePara = {
118         .name = { "FILTER_NAME" },
119         .t = HISYSEVENT_STRING,
120         .v = { .s = const_cast<char *>(eventInfo.filterName.c_str()) },
121         .arraySize = 0,
122     };
123     HiSysEventParam supportedFormatsPara = {
124         .name = { "SUPPORTED_FORMATS" },
125         .t = HISYSEVENT_UINT32,
126         .v = { .ui32 = eventInfo.supportedFormats },
127         .arraySize = 0,
128     };
129     std::vector<HiSysEventParam> params = { processNamePara, supportedFormatsPara };
130     EventWrite(__FUNCTION__, __LINE__, REGISTER_CUSTOM_FILTER_STATISTIC, HiSysEventEventType::HISYSEVENT_BEHAVIOR,
131         params);
132 }
133 
ReportAddFilterEvent(const EventInfo & eventInfo)134 void EventReport::ReportAddFilterEvent(const EventInfo &eventInfo)
135 {
136     HiSysEventParam processNamePara = {
137         .name = { "FILTER_NAME" },
138         .t = HISYSEVENT_STRING,
139         .v = { .s = const_cast<char *>(eventInfo.filterName.c_str()) },
140         .arraySize = 0,
141     };
142     std::vector<HiSysEventParam> params = { processNamePara };
143     EventWrite(__FUNCTION__, __LINE__, ADD_FILTER_STATISTIC, HiSysEventEventType::HISYSEVENT_BEHAVIOR,
144         params);
145 }
146 
ReportRemoveFilterEvent(const EventInfo & eventInfo)147 void EventReport::ReportRemoveFilterEvent(const EventInfo &eventInfo)
148 {
149     HiSysEventParam processNamePara = {
150         .name = { "FILTER_NAME" },
151         .t = HISYSEVENT_STRING,
152         .v = { .s = const_cast<char *>(eventInfo.filterName.c_str()) },
153         .arraySize = 0,
154     };
155     HiSysEventParam filterNumPara = {
156         .name = { "FILTER_NUMBER" },
157         .t = HISYSEVENT_INT32,
158         .v = { .i32 = eventInfo.filterNum },
159         .arraySize = 0,
160     };
161     std::vector<HiSysEventParam> params = { processNamePara, filterNumPara };
162     EventWrite(__FUNCTION__, __LINE__, REMOVE_FILTER_STATISTIC, HiSysEventEventType::HISYSEVENT_BEHAVIOR,
163         params);
164 }
165 
ConvertDataType(const EventDataType & dataType)166 std::string EventReport::ConvertDataType(const EventDataType &dataType)
167 {
168     auto iter = sysEventDataTypeMap_.find(dataType);
169     if (iter == sysEventDataTypeMap_.end()) {
170         return DEFAULT_STR;
171     }
172 
173     return iter->second;
174 }
175 
ReportInputDataTypeEvent(const EventInfo & eventInfo)176 void EventReport::ReportInputDataTypeEvent(const EventInfo &eventInfo)
177 {
178     std::string dataType = ConvertDataType(eventInfo.dataType);
179     HiSysEventParam dataTypePara = {
180         .name = { "DATA_TYPE" },
181         .t = HISYSEVENT_STRING,
182         .v = { .s = const_cast<char *>(dataType.c_str()) },
183         .arraySize = 0,
184     };
185     std::vector<HiSysEventParam> params = { dataTypePara };
186     EventWrite(__FUNCTION__, __LINE__, INPUT_DATA_TYPE_STATISTIC, HiSysEventEventType::HISYSEVENT_BEHAVIOR,
187         params);
188 }
189 
ReportOutputDataTypeEvent(const EventInfo & eventInfo)190 void EventReport::ReportOutputDataTypeEvent(const EventInfo &eventInfo)
191 {
192     std::string dataType = ConvertDataType(eventInfo.dataType);
193     HiSysEventParam dataTypePara = {
194         .name = { "DATA_TYPE" },
195         .t = HISYSEVENT_STRING,
196         .v = { .s = const_cast<char *>(dataType.c_str()) },
197         .arraySize = 0,
198     };
199     std::vector<HiSysEventParam> params = { dataTypePara };
200     EventWrite(__FUNCTION__, __LINE__, OUTPUT_DATA_TYPE_STATISTIC, HiSysEventEventType::HISYSEVENT_BEHAVIOR,
201         params);
202 }
203 
ReportRenderFailedEvent(const EventInfo & eventInfo)204 void EventReport::ReportRenderFailedEvent(const EventInfo &eventInfo)
205 {
206     HiSysEventParam errorCodePara = {
207         .name = { "ERROR_TYPE" },
208         .t = HISYSEVENT_INT32,
209         .v = { .i32 = eventInfo.errorInfo.errorCode },
210         .arraySize = 0,
211     };
212     HiSysEventParam errorMsgPara = {
213         .name = { "ERROR_MSG" },
214         .t = HISYSEVENT_STRING,
215         .v = { .s = const_cast<char *>(eventInfo.errorInfo.errorMsg.c_str()) },
216         .arraySize = 0,
217     };
218 
219     std::vector<HiSysEventParam> params = { errorCodePara, errorMsgPara };
220     EventWrite(__FUNCTION__, __LINE__, RENDER_FAILED_FAULT, HiSysEventEventType::HISYSEVENT_BEHAVIOR,
221         params);
222 }
223 
ReportSaveImageEffectEvent(const EventInfo & eventInfo)224 void EventReport::ReportSaveImageEffectEvent(const EventInfo &eventInfo)
225 {
226     std::vector<HiSysEventParam> params;
227     EventWrite(__FUNCTION__, __LINE__, SAVE_IMAGE_EFFECT_BEHAVIOR, HiSysEventEventType::HISYSEVENT_BEHAVIOR,
228         params);
229 }
230 
ReportRestoreImageEffectEvent(const EventInfo & eventInfo)231 void EventReport::ReportRestoreImageEffectEvent(const EventInfo &eventInfo)
232 {
233     std::vector<HiSysEventParam> params;
234     EventWrite(__FUNCTION__, __LINE__, RESTORE_IMAGE_EFFECT_BEHAVIOR, HiSysEventEventType::HISYSEVENT_BEHAVIOR,
235         params);
236 }
237 
238 } // namespace Effect
239 } // namespace Media
240 } // namespace OHOS