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