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 "drm_dfx.h"
17 #include "drm_dfx_utils.h"
18 #include <unistd.h>
19 #include "drm_log.h"
20 #include "dump_usage.h"
21 #include "nlohmann/json.hpp"
22 #include "native_drm_err.h"
23 #include "securec.h"
24 #include "ipc_skeleton.h"
25 #include "hitrace/tracechain.h"
26 #include <cstdint>
27
28 namespace OHOS {
29 namespace DrmStandard {
30 using namespace OHOS::HiviewDFX;
31 using json = nlohmann::json;
32 constexpr uint32_t MIN_DECRYPTION_TIMES = 1000;
33
GetInstance()34 DrmEvent& DrmEvent::GetInstance()
35 {
36 static DrmEvent instance;
37 return instance;
38 }
39
WriteServiceEvent(std::string eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,DrmServiveInfo & info)40 int32_t DrmEvent::WriteServiceEvent(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
41 DrmServiveInfo &info)
42 {
43 int32_t res = DRM_ERR_OK;
44 res = HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
45 "MODULE", info.module,
46 "SERVICE_NAME", info.serviceName,
47 "ACTION", info.action,
48 "MEMORY", info.memoryUsage);
49 if (res != DRM_ERR_OK) {
50 DRM_ERR_LOG("EventWrite failed, res = %d", res);
51 return res;
52 } else {
53 DRM_INFO_LOG("EventWrite success");
54 }
55 return DRM_ERR_OK;
56 }
57
WriteLicenseEvent(std::string eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,DrmLicenseInfo & info)58 int32_t DrmEvent::WriteLicenseEvent(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
59 DrmLicenseInfo &info)
60 {
61 int32_t res = DRM_ERR_OK;
62 res = HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
63 "MODULE", info.module,
64 "APP_NAME", info.appName,
65 "INSTANCE_ID", info.instanceId,
66 "DRM_NAME", info.drmName,
67 "DRM_UUID", info.drmUuid,
68 "CLIENT_VERSION", info.clientVersion,
69 "LICENSE_TYPE", info.licenseType,
70 "GENERATION_DURATION", info.generationDuration,
71 "GENERATION_RESULT", info.generationResult,
72 "PROCESS_DURATION", info.processDuration,
73 "PROCESS_RESULT", info.processResult);
74 if (res != DRM_ERR_OK) {
75 DRM_ERR_LOG("EventWrite failed, res = %d", res);
76 return res;
77 } else {
78 DRM_INFO_LOG("EventWrite success");
79 }
80 return DRM_ERR_OK;
81 }
82
WriteCertificateEvent(std::string eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,DrmCertificateInfo & info)83 int32_t DrmEvent::WriteCertificateEvent(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
84 DrmCertificateInfo &info)
85 {
86 int32_t res = DRM_ERR_OK;
87 res = HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
88 "MODULE", info.module,
89 "APP_NAME", info.appName,
90 "INSTANCE_ID", info.instanceId,
91 "DRM_NAME", info.drmName,
92 "DRM_UUID", info.drmUuid,
93 "CLIENT_VERSION", info.clientVersion,
94 "GENERATION_DURATION", info.generationDuration,
95 "GENERATION_RESULT", info.generationResult,
96 "PROCESS_DURATION", info.processDuration,
97 "PROCESS_RESULT", info.processResult,
98 "CALL_SERVER_TIME", info.callServerTime,
99 "SERVER_COST_DURATION", info.serverCostDuration,
100 "SERVER_RESULT", info.serverResult);
101 if (res != DRM_ERR_OK) {
102 DRM_ERR_LOG("EventWrite failed, res = %d", res);
103 return res;
104 } else {
105 DRM_INFO_LOG("EventWrite success");
106 }
107 return DRM_ERR_OK;
108 }
109
WriteFaultEvent(std::string eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,DrmFaultInfo & info)110 int32_t DrmEvent::WriteFaultEvent(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
111 DrmFaultInfo &info)
112 {
113 int32_t res = DRM_ERR_OK;
114 res = HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
115 "MODULE", info.module,
116 "APP_NAME", info.appName,
117 "INSTANCE_ID", info.instanceId,
118 "ERROR_CODE", info.errorCode,
119 "ERROR_MESG", info.errorMesg,
120 "EXTRA_MESG", info.extraMesg);
121 if (res != DRM_ERR_OK) {
122 DRM_ERR_LOG("EventWrite failed, res = %d", res);
123 return res;
124 } else {
125 DRM_INFO_LOG("EventWrite success");
126 }
127 return DRM_ERR_OK;
128 }
129
WriteDecryptionEvent(std::string eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,DrmDecryptionInfo & info)130 int32_t DrmEvent::WriteDecryptionEvent(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
131 DrmDecryptionInfo &info)
132 {
133 int32_t res = DRM_ERR_OK;
134
135 res = HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
136 "MODULE", info.module,
137 "APP_NAME", info.appName,
138 "INSTANCE_ID", info.instanceId,
139 "ERROR_CODE", info.errorCode,
140 "ERROR_MESG", info.errorMesg,
141 "DECRYPT_ALGO", info.decryptAlgo,
142 "DECRYPT_KEYID", info.decryptKeyid,
143 "DECRYPT_IV", info.decryptIv);
144 if (res != DRM_ERR_OK) {
145 DRM_ERR_LOG("EventWrite failed, res = %d", res);
146 return res;
147 } else {
148 DRM_INFO_LOG("EventWrite success");
149 }
150 return DRM_ERR_OK;
151 }
152
ReportServiceBehaviorEvent(std::string serviceName,std::string action)153 int32_t ReportServiceBehaviorEvent(std::string serviceName, std::string action)
154 {
155 DrmEvent event;
156 OHOS::HiviewDFX::DumpUsage dumpUse;
157 uint32_t memoryUsage = dumpUse.GetPss(getpid());
158 struct DrmServiveInfo drmServiveInfo = {
159 "DRM_SERVICE",
160 0,
161 serviceName,
162 action,
163 memoryUsage,
164 };
165 return event.WriteServiceEvent("DRM_SERVICE_INFO", OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
166 drmServiveInfo);
167 }
168
ReportLicenseBehaviorEvent(StatisticsInfo statisticsInfo,std::string licenseType,DownLoadInfo downLoadInfo)169 int32_t ReportLicenseBehaviorEvent(StatisticsInfo statisticsInfo, std::string licenseType, DownLoadInfo downLoadInfo)
170 {
171 DrmEvent event;
172 struct DrmLicenseInfo drmLicenseInfo = {
173 "DRM_SERVICE",
174 0,
175 GetClientBundleName(IPCSkeleton::GetCallingUid()),
176 std::to_string(HiTraceChain::GetId().GetChainId()),
177 statisticsInfo.pluginName,
178 statisticsInfo.pluginUuid,
179 statisticsInfo.versionName,
180 licenseType,
181 downLoadInfo.generationDuration,
182 downLoadInfo.generationResult,
183 downLoadInfo.processDuration,
184 downLoadInfo.processResult,
185 };
186 return event.WriteLicenseEvent("DRM_LICENSE_DOWNLOAD_INFO", OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
187 drmLicenseInfo);
188 }
189
ReportCertificateBehaviorEvent(StatisticsInfo statisticsInfo,DownLoadInfo downLoadInfo,uint32_t callServerTime,uint32_t serverCostDuration,std::string serverResult)190 int32_t ReportCertificateBehaviorEvent(StatisticsInfo statisticsInfo, DownLoadInfo downLoadInfo,
191 uint32_t callServerTime, uint32_t serverCostDuration, std::string serverResult)
192 {
193 DrmEvent event;
194 struct DrmCertificateInfo drmCertificateInfo = {
195 "DRM_SERVICE",
196 0,
197 GetClientBundleName(IPCSkeleton::GetCallingUid()),
198 std::to_string(HiTraceChain::GetId().GetChainId()),
199 statisticsInfo.pluginName,
200 statisticsInfo.pluginUuid,
201 statisticsInfo.versionName,
202 downLoadInfo.generationDuration,
203 downLoadInfo.generationResult,
204 downLoadInfo.processDuration,
205 downLoadInfo.processResult,
206 callServerTime,
207 serverCostDuration,
208 serverResult,
209 };
210 return event.WriteCertificateEvent("DRM_CERTIFICATE_DOWNLOAD_INFO",
211 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, drmCertificateInfo);
212 }
213
ReportFaultEvent(uint32_t errorCode,std::string errorMesg,std::string extraMesg)214 int32_t ReportFaultEvent(uint32_t errorCode, std::string errorMesg, std::string extraMesg)
215 {
216 DrmEvent event;
217 struct DrmFaultInfo drmFaultInfo = {
218 "DRM_SERVICE",
219 GetClientBundleName(IPCSkeleton::GetCallingUid()),
220 std::to_string(HiTraceChain::GetId().GetChainId()),
221 errorCode,
222 errorMesg,
223 extraMesg,
224 };
225 return event.WriteFaultEvent("DRM_COMMON_FAILURE", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, drmFaultInfo);
226 }
227
ReportDecryptionFaultEvent(int32_t errorCode,std::string errorMesg,std::string decryptAlgo,std::string decryptKeyid,std::string decryptIv)228 int32_t ReportDecryptionFaultEvent(int32_t errorCode, std::string errorMesg, std::string decryptAlgo,
229 std::string decryptKeyid, std::string decryptIv)
230 {
231 DrmEvent event;
232 struct DrmDecryptionInfo drmDecryptionInfo = {
233 "DRM_SERVICE",
234 GetClientBundleName(IPCSkeleton::GetCallingUid()),
235 std::to_string(HiTraceChain::GetId().GetChainId()),
236 errorCode,
237 errorMesg,
238 decryptAlgo,
239 decryptKeyid,
240 decryptIv,
241 };
242 return event.WriteDecryptionEvent("DRM_DECRYPTION_FAILURE", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
243 drmDecryptionInfo);
244 }
245
InitDownLoadInfo(uint32_t generationDuration,std::string generationResult,uint32_t processDuration,std::string processResult)246 DownLoadInfo InitDownLoadInfo(uint32_t generationDuration, std::string generationResult, uint32_t processDuration,
247 std::string processResult)
248 {
249 struct DownLoadInfo downLoadInfo = {
250 generationDuration,
251 generationResult,
252 processDuration,
253 processResult,
254 };
255 return downLoadInfo;
256 }
257
ReportDecryptionStatisticsEvent(uint64_t instanceId,std::string appName,DecryptionStatistics & statistics)258 void ReportDecryptionStatisticsEvent(uint64_t instanceId, std::string appName,
259 DecryptionStatistics &statistics)
260 {
261 DRM_INFO_LOG("ReportDecryptionStatisticsEvent.");
262
263 int32_t res = DRM_ERR_OK;
264 if (statistics.decryptTimes < MIN_DECRYPTION_TIMES) {
265 return;
266 }
267 uint32_t avgSize = statistics.decryptSumSize / statistics.decryptTimes;
268 uint32_t avgDuration = statistics.decryptSumDuration / statistics.decryptTimes;
269
270 res = HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA,
271 "DRM_STATISTICS",
272 HiviewDFX::HiSysEvent::EventType::STATISTIC,
273 "APP_NAME", appName,
274 "INSTANCE_ID", instanceId,
275 "ERROR_CODE", statistics.errCode,
276 "ERROR_MESG", statistics.errMessage,
277 "DECRYPT_TIMES", statistics.decryptTimes,
278 "DECRYPT_AVG_SIZE", avgSize,
279 "DECRYPT_AVG_DURATION", avgDuration,
280 "DECRYPT_MAX_SIZE", statistics.decryptMaxSize,
281 "DECRYPT_MAX_DURATION", statistics.decryptMaxDuration);
282 if (res != DRM_ERR_OK) {
283 DRM_ERR_LOG("DRM_STATISTICS Event Write failed, res = %d", res);
284 } else {
285 DRM_INFO_LOG("DRM_STATISTICS Event Write success");
286 }
287 DRM_INFO_LOG("ReportDecryptionStatisticsEvent done!");
288 }
289 } // namespace DrmStandard
290 } // namespace OHOS