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 #ifndef DRM_DFX_H
17 #define DRM_DFX_H
18 
19 #include <list>
20 #include <string>
21 #include <queue>
22 #include <refbase.h>
23 #include "drm_dfx_utils.h"
24 #include "nocopyable.h"
25 #include "nlohmann/json.hpp"
26 #include "hisysevent.h"
27 #include "hitrace/tracechain.h"
28 #include <chrono>
29 
30 namespace OHOS {
31 namespace DrmStandard {
32 using namespace OHOS::HiviewDFX;
33 
34 struct DrmServiveInfo {
35     std::string module;
36     uint32_t currentTime;
37     std::string serviceName;
38     std::string action;
39     uint32_t memoryUsage;
40 };
41 
42 struct DrmLicenseInfo {
43     std::string module;
44     uint32_t currentTime;
45     std::string appName;
46     std::string instanceId;
47     std::string drmName;
48     std::string drmUuid;
49     std::string clientVersion;
50     std::string licenseType;
51     uint32_t generationDuration;
52     std::string generationResult;
53     uint32_t processDuration;
54     std::string processResult;
55 };
56 
57 struct DrmCertificateInfo {
58     std::string module;
59     uint32_t currentTime;
60     std::string appName;
61     std::string instanceId;
62     std::string drmName;
63     std::string drmUuid;
64     std::string clientVersion;
65     uint32_t generationDuration;
66     std::string generationResult;
67     uint32_t processDuration;
68     std::string processResult;
69     uint32_t callServerTime;
70     uint32_t serverCostDuration;
71     std::string serverResult;
72 };
73 
74 struct DrmFaultInfo {
75     std::string module;
76     std::string appName;
77     std::string instanceId;
78     int32_t errorCode;
79     std::string errorMesg;
80     std::string extraMesg;
81 };
82 
83 struct DrmDecryptionInfo {
84     std::string module;
85     std::string appName;
86     std::string instanceId;
87     int32_t errorCode;
88     std::string errorMesg;
89     std::string decryptAlgo;
90     std::string decryptKeyid;
91     std::string decryptIv;
92 };
93 
94 struct DownLoadInfo {
95     uint32_t generationDuration;
96     std::string generationResult;
97     uint32_t processDuration;
98     std::string processResult;
99 };
100 
101 struct DecryptionStatistics {
102     uint32_t decryptTimes = 0;
103     uint64_t decryptSumSize = 0;
104     uint64_t decryptSumDuration = 0;
105     uint32_t decryptMaxSize = 0;
106     uint32_t decryptMaxDuration = 0;
107     uint64_t errorDecryptTimes = 0;
108     int32_t errCode = 0;
109     std::string errMessage = "no error";
110     std::priority_queue<int, std::vector<int>, std::greater<int>> topThree;
111 };
112 
113 class DrmEvent {
114 public:
115     static DrmEvent& GetInstance();
116     int32_t WriteServiceEvent(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type, DrmServiveInfo &info);
117     int32_t WriteLicenseEvent(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type, DrmLicenseInfo &info);
118     int32_t WriteCertificateEvent(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
119         DrmCertificateInfo &info);
120     int32_t WriteFaultEvent(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type, DrmFaultInfo &info);
121     int32_t WriteDecryptionEvent(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
122         DrmDecryptionInfo &info);
123 };
124 
125 __attribute__((visibility("default"))) int32_t ReportServiceBehaviorEvent(std::string serviceName, std::string action);
126 __attribute__((visibility("default"))) int32_t ReportLicenseBehaviorEvent(StatisticsInfo statisticsInfo,
127     std::string licenseType, DownLoadInfo downLoadInfo);
128 __attribute__((visibility("default"))) int32_t ReportCertificateBehaviorEvent(StatisticsInfo statisticsInfo,
129     DownLoadInfo downLoadInfo, uint32_t callServerTime, uint32_t serverCostDuration, std::string serverResult);
130 __attribute__((visibility("default"))) int32_t ReportFaultEvent(uint32_t errorCode, std::string errorMesg,
131     std::string extraMesg);
132 __attribute__((visibility("default"))) int32_t ReportDecryptionFaultEvent(int32_t errorCode, std::string errorMesg,
133     std::string decryptAlgo, std::string decryptKeyid, std::string decryptIv);
134 __attribute__((visibility("default"))) DownLoadInfo InitDownLoadInfo(uint32_t generationDuration,
135     std::string generationResult, uint32_t processDuration, std::string processResult);
136 __attribute__((visibility("default"))) void ReportDecryptionStatisticsEvent(uint64_t instanceId, std::string appName,
137     DecryptionStatistics &statistics);
138 } // namespace DrmStandard
139 } // namespace OHOS
140 #endif // DRM_DFX_H