1 /*
2 * Copyright (c) 2025 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 "user_auth_api_event_reporter.h"
17
18 #include <chrono>
19 #include <cinttypes>
20
21 #include "app_event.h"
22 #include "app_event_processor_mgr.h"
23
24 #include "config_parser.h"
25 #include "iam_logger.h"
26
27 #define LOG_TAG "USER_AUTH_COMMON"
28
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 using namespace HiviewDFX::HiAppEvent;
33 using namespace std::chrono;
34 namespace {
35 constexpr int32_t REPORT_SUCCESS = 0;
36 constexpr int32_t REPORT_FAILED = 1;
37 constexpr int64_t INVALID_PROCESSOR_ID = -1;
AddEventConfigs(ReportConfig & config)38 void AddEventConfigs(ReportConfig &config)
39 {
40 config.eventConfigs.clear();
41 EventConfig event1;
42 event1.domain = "api_diagnostic";
43 event1.name = "api_exec_end";
44 event1.isRealTime = false;
45 config.eventConfigs.push_back(event1);
46
47 EventConfig event2;
48 event2.domain = "api_diagnostic";
49 event2.name = "api_called_stat";
50 event2.isRealTime = true;
51 config.eventConfigs.push_back(event2);
52
53 EventConfig event3;
54 event3.domain = "api_diagnostic";
55 event3.name = "api_called_stat_cnt";
56 event3.isRealTime = true;
57 config.eventConfigs.push_back(event3);
58 }
59 } // namespace
60 int64_t UserAuthApiEventReporter::processorId_ = INVALID_PROCESSOR_ID;
61
UserAuthApiEventReporter(std::string apiName)62 UserAuthApiEventReporter::UserAuthApiEventReporter(std::string apiName) : apiName_(apiName)
63 {
64 transId_ = std::string("transId_") + std::to_string(std::rand());
65 beginTime_ = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
66 }
67
ReportSuccess()68 void UserAuthApiEventReporter::ReportSuccess()
69 {
70 Report(REPORT_SUCCESS, 0);
71 }
72
ReportFailed(UserAuthResultCode resultCode)73 void UserAuthApiEventReporter::ReportFailed(UserAuthResultCode resultCode)
74 {
75 Report(REPORT_FAILED, static_cast<int32_t>(resultCode));
76 }
77
ReportFailed(int32_t resultCode)78 void UserAuthApiEventReporter::ReportFailed(int32_t resultCode)
79 {
80 Report(REPORT_FAILED, resultCode);
81 }
82
Report(int32_t result,int32_t errCode)83 void UserAuthApiEventReporter::Report(int32_t result, int32_t errCode)
84 {
85 const char *kitName = "UserAuthenticationKit";
86
87 if (isReported_) {
88 IAM_LOGI("already reported");
89 return;
90 }
91 isReported_ = true;
92
93 int64_t processorId = GetProcessorId();
94 if (processorId <= 0) {
95 IAM_LOGE("GetProcessorId failed");
96 return;
97 }
98
99 int64_t endTime = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
100 HiviewDFX::HiAppEvent::Event event("api_diagnostic", "api_exec_end", HiviewDFX::HiAppEvent::BEHAVIOR);
101 event.AddParam("trans_id", transId_);
102 event.AddParam("api_name", apiName_);
103 event.AddParam("sdk_name", kitName);
104 event.AddParam("begin_time", beginTime_);
105 event.AddParam("end_time", endTime);
106 event.AddParam("result", result);
107 event.AddParam("error_code", errCode);
108 int ret = Write(event);
109 IAM_LOGI(
110 "WriteEndEvent transId:%{public}s, apiName:%{public}s, result:%{public}d, errCode:%{public}d, ret:%{public}d",
111 transId_.c_str(), apiName_.c_str(), result, errCode, ret);
112 }
113
GetProcessorId()114 int64_t UserAuthApiEventReporter::GetProcessorId()
115 {
116 constexpr int TRIGGER_COND_TIMEOUT = 90;
117 constexpr int TRIGGER_COND_ROW = 30;
118 const char *API_REPORT_CONFIG_PATH = "/system/etc/useriam/useriam_api_event_report.cfg";
119 const char *DEFAULT_API_CONFIG_APP_ID = "useriam_verify_ohos_sdk_ocg";
120 const char *DEFAULT_API_PROCESSOR_NAME = "useriam_verify_processor";
121
122 static std::mutex mutex;
123 std::lock_guard<std::mutex> lock(mutex);
124 if (processorId_ > 0) {
125 return processorId_;
126 }
127
128 ReportConfig config;
129 config.appId = DEFAULT_API_CONFIG_APP_ID;
130 config.name = DEFAULT_API_PROCESSOR_NAME;
131 config.routeInfo = "AUTO";
132
133 ConfigParser parser;
134 if (parser.Load(API_REPORT_CONFIG_PATH)) {
135 config.appId = parser.Get("appId", DEFAULT_API_CONFIG_APP_ID);
136 config.name = parser.Get("name", DEFAULT_API_PROCESSOR_NAME);
137 IAM_LOGI("Loaded config from file: appId=%{public}s, name=%{public}s", config.appId.c_str(),
138 config.name.c_str());
139 } else {
140 IAM_LOGI("Failed to load config file, using default values");
141 }
142
143 config.triggerCond.timeout = TRIGGER_COND_TIMEOUT;
144 config.triggerCond.row = TRIGGER_COND_ROW;
145
146 AddEventConfigs(config);
147
148 int64_t processorId = AppEventProcessorMgr::AddProcessor(config);
149 if (processorId < 0) {
150 IAM_LOGE("AddProcessor failed, ret = %{public}" PRIi64, processorId);
151 return INVALID_PROCESSOR_ID;
152 }
153
154 IAM_LOGI("AddProcessor success");
155 processorId_ = processorId;
156 return processorId_;
157 }
158 } // namespace UserAuth
159 } // namespace UserIam
160 } // namespace OHOS