1bc2ed2b3Sopenharmony_ci/*
2bc2ed2b3Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3bc2ed2b3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc2ed2b3Sopenharmony_ci * you may not use this file except in compliance with the License.
5bc2ed2b3Sopenharmony_ci * You may obtain a copy of the License at
6bc2ed2b3Sopenharmony_ci *
7bc2ed2b3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc2ed2b3Sopenharmony_ci *
9bc2ed2b3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc2ed2b3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc2ed2b3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc2ed2b3Sopenharmony_ci * See the License for the specific language governing permissions and
13bc2ed2b3Sopenharmony_ci * limitations under the License.
14bc2ed2b3Sopenharmony_ci */
15bc2ed2b3Sopenharmony_ci#include "nfc_hisysevent.h"
16bc2ed2b3Sopenharmony_ci#include "hisysevent.h"
17bc2ed2b3Sopenharmony_ci#include "loghelper.h"
18bc2ed2b3Sopenharmony_ci#include "taginfo.h"
19bc2ed2b3Sopenharmony_ci
20bc2ed2b3Sopenharmony_cinamespace OHOS {
21bc2ed2b3Sopenharmony_cinamespace NFC {
22bc2ed2b3Sopenharmony_citemplate<typename... Types>
23bc2ed2b3Sopenharmony_cistatic void WriteEvent(const std::string& eventType, HiviewDFX::HiSysEvent::EventType type, Types... args)
24bc2ed2b3Sopenharmony_ci{
25bc2ed2b3Sopenharmony_ci    int ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::NFC, eventType, type, args...);
26bc2ed2b3Sopenharmony_ci    if (ret != 0) {
27bc2ed2b3Sopenharmony_ci        ErrorLog("Write event fail: %{public}s", eventType.c_str());
28bc2ed2b3Sopenharmony_ci    }
29bc2ed2b3Sopenharmony_ci}
30bc2ed2b3Sopenharmony_ci
31bc2ed2b3Sopenharmony_civoid NfcHisysEvent::WriteNfcFailedHiSysEvent(const NfcFailedParams* failedParams)
32bc2ed2b3Sopenharmony_ci{
33bc2ed2b3Sopenharmony_ci    WriteEvent("OPERATION_FAILED", HiviewDFX::HiSysEvent::EventType::FAULT,
34bc2ed2b3Sopenharmony_ci               "MAIN_ERROR_CODE", static_cast<int>(failedParams->mainErrorCode),
35bc2ed2b3Sopenharmony_ci               "SUB_ERROR_CODE", static_cast<int>(failedParams->subErrorCode),
36bc2ed2b3Sopenharmony_ci               "CURRENT_DEFAULT_ROUTE", failedParams->defaultRoute,
37bc2ed2b3Sopenharmony_ci               "CURRENT_SCREEN_STATE", failedParams->screenState,
38bc2ed2b3Sopenharmony_ci               "CURRENT_NFC_STATE", failedParams->nfcState,
39bc2ed2b3Sopenharmony_ci               "CURRENT_PASSIVE_LISTEN_STATE", failedParams->passiveListenState,
40bc2ed2b3Sopenharmony_ci               "CURRENT_FIRMWARE_VERSION", failedParams->version,
41bc2ed2b3Sopenharmony_ci               "APP_PACKAGE_NAME", failedParams->appPackageName);
42bc2ed2b3Sopenharmony_ci}
43bc2ed2b3Sopenharmony_ci
44bc2ed2b3Sopenharmony_civoid NfcHisysEvent::WriteOpenAndCloseHiSysEvent(int openRequestCnt, int openFailCnt,
45bc2ed2b3Sopenharmony_ci                                                int closeRequestCnt, int closeFailCnt)
46bc2ed2b3Sopenharmony_ci{
47bc2ed2b3Sopenharmony_ci    WriteEvent("OPEN_AND_CLOSE", HiviewDFX::HiSysEvent::EventType::STATISTIC,
48bc2ed2b3Sopenharmony_ci               "OPEN_REQUEST_CNT", openRequestCnt,
49bc2ed2b3Sopenharmony_ci               "OPEN_FAILED_CNT", openFailCnt,
50bc2ed2b3Sopenharmony_ci               "CLOSE_REQUEST_CNT", closeRequestCnt,
51bc2ed2b3Sopenharmony_ci               "CLOSE_FAILED_CNT", closeFailCnt);
52bc2ed2b3Sopenharmony_ci}
53bc2ed2b3Sopenharmony_ci
54bc2ed2b3Sopenharmony_civoid NfcHisysEvent::WriteTagFoundHiSysEvent(const std::vector<int>& techList)
55bc2ed2b3Sopenharmony_ci{
56bc2ed2b3Sopenharmony_ci    int tagFoundCnt = 1;
57bc2ed2b3Sopenharmony_ci    int typeACnt = 0;
58bc2ed2b3Sopenharmony_ci    int typeBCnt = 0;
59bc2ed2b3Sopenharmony_ci    int typeFCnt = 0;
60bc2ed2b3Sopenharmony_ci    int typeVCnt = 0;
61bc2ed2b3Sopenharmony_ci    for (size_t i = 0; i < techList.size(); i++) {
62bc2ed2b3Sopenharmony_ci        std::string discStrTech = KITS::TagInfo::GetStringTech(techList[i]);
63bc2ed2b3Sopenharmony_ci        if (discStrTech.compare("NfcA") == 0) {
64bc2ed2b3Sopenharmony_ci            typeACnt++;
65bc2ed2b3Sopenharmony_ci        } else if (discStrTech.compare("NfcB") == 0) {
66bc2ed2b3Sopenharmony_ci            typeBCnt++;
67bc2ed2b3Sopenharmony_ci        } else if (discStrTech.compare("NfcF") == 0) {
68bc2ed2b3Sopenharmony_ci            typeFCnt++;
69bc2ed2b3Sopenharmony_ci        } else if (discStrTech.compare("NfcV") == 0) {
70bc2ed2b3Sopenharmony_ci            typeVCnt++;
71bc2ed2b3Sopenharmony_ci        }
72bc2ed2b3Sopenharmony_ci    }
73bc2ed2b3Sopenharmony_ci    WriteEvent("TAG_FOUND", HiviewDFX::HiSysEvent::EventType::STATISTIC,
74bc2ed2b3Sopenharmony_ci               "TOTAL_TAG_FOUND_CNT", tagFoundCnt,
75bc2ed2b3Sopenharmony_ci               "TYPE_A_TAG_FOUND", typeACnt,
76bc2ed2b3Sopenharmony_ci               "TYPE_B_TAG_FOUND", typeBCnt,
77bc2ed2b3Sopenharmony_ci               "TYPE_F_TAG_FOUND", typeFCnt,
78bc2ed2b3Sopenharmony_ci               "TYPE_V_TAG_FOUND", typeVCnt);
79bc2ed2b3Sopenharmony_ci}
80bc2ed2b3Sopenharmony_ci
81bc2ed2b3Sopenharmony_civoid NfcHisysEvent::WritePassiveListenHiSysEvent(int requestCnt, int failCnt)
82bc2ed2b3Sopenharmony_ci{
83bc2ed2b3Sopenharmony_ci    WriteEvent("PASSIVE_LISTEN", HiviewDFX::HiSysEvent::EventType::STATISTIC,
84bc2ed2b3Sopenharmony_ci               "REQUEST_PASSIVE_LISTEN_CNT", requestCnt,
85bc2ed2b3Sopenharmony_ci               "FAILED_PASSIVE_LISTEN_CNT", failCnt);
86bc2ed2b3Sopenharmony_ci}
87bc2ed2b3Sopenharmony_ci
88bc2ed2b3Sopenharmony_civoid NfcHisysEvent::WriteFirmwareUpdateHiSysEvent(int requestCnt, int failCnt)
89bc2ed2b3Sopenharmony_ci{
90bc2ed2b3Sopenharmony_ci    WriteEvent("FIRMWARE_UPDATE", HiviewDFX::HiSysEvent::EventType::STATISTIC,
91bc2ed2b3Sopenharmony_ci               "REQUEST_FIRMWARE_UPDATE_CNT", requestCnt,
92bc2ed2b3Sopenharmony_ci               "FAILED_FIRMWARE_UPDATE_CNT", failCnt);
93bc2ed2b3Sopenharmony_ci}
94bc2ed2b3Sopenharmony_ci
95bc2ed2b3Sopenharmony_civoid NfcHisysEvent::BuildFailedParams(NfcFailedParams &nfcFailedParams,
96bc2ed2b3Sopenharmony_ci                                      MainErrorCode mainErrorCode,
97bc2ed2b3Sopenharmony_ci                                      SubErrorCode subErrorCode)
98bc2ed2b3Sopenharmony_ci{
99bc2ed2b3Sopenharmony_ci    nfcFailedParams.mainErrorCode = mainErrorCode;
100bc2ed2b3Sopenharmony_ci    nfcFailedParams.subErrorCode = subErrorCode;
101bc2ed2b3Sopenharmony_ci    nfcFailedParams.defaultRoute = 0;
102bc2ed2b3Sopenharmony_ci    nfcFailedParams.screenState = 0;
103bc2ed2b3Sopenharmony_ci    nfcFailedParams.nfcState = 0;
104bc2ed2b3Sopenharmony_ci    nfcFailedParams.passiveListenState = 0;
105bc2ed2b3Sopenharmony_ci    nfcFailedParams.version = "VERSION";
106bc2ed2b3Sopenharmony_ci    nfcFailedParams.appPackageName = "APPNAME";
107bc2ed2b3Sopenharmony_ci}
108bc2ed2b3Sopenharmony_civoid NfcHisysEvent::WriteHceSwipeResultHiSysEvent(const std::string &appPackageName, int hceSwipeCnt)
109bc2ed2b3Sopenharmony_ci{
110bc2ed2b3Sopenharmony_ci    WriteEvent("HCE_SWIPE_RESULT", HiviewDFX::HiSysEvent::EventType::STATISTIC,
111bc2ed2b3Sopenharmony_ci               "HCE_SWIPE_CNT", hceSwipeCnt,
112bc2ed2b3Sopenharmony_ci               "APP_PACKAGE_NAME", appPackageName);
113bc2ed2b3Sopenharmony_ci}
114bc2ed2b3Sopenharmony_civoid NfcHisysEvent::WriteDefaultPaymentAppChangeHiSysEvent(const std::string &oldAppPackageName,
115bc2ed2b3Sopenharmony_ci                                                           const std::string &newAppPackageName)
116bc2ed2b3Sopenharmony_ci{
117bc2ed2b3Sopenharmony_ci    WriteEvent("HCE_DEFAULT_PAYMENT_APP_CHANGE", HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
118bc2ed2b3Sopenharmony_ci               "OLD_APP_PACKAGE_NAME", oldAppPackageName, "NEW_APP_PACKAGE_NAME", newAppPackageName);
119bc2ed2b3Sopenharmony_ci}
120bc2ed2b3Sopenharmony_ci
121bc2ed2b3Sopenharmony_civoid NfcHisysEvent::WriteForegroundAppChangeHiSysEvent(const std::string &appPackageName)
122bc2ed2b3Sopenharmony_ci{
123bc2ed2b3Sopenharmony_ci    InfoLog("WriteForegroundAppChangeHiSysEvent, appPackageName[%{public}s]", appPackageName.c_str());
124bc2ed2b3Sopenharmony_ci    WriteEvent("HCE_FORGROUND_APP_CHANGE", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "NEW_APP_PACKAGE_NAME",
125bc2ed2b3Sopenharmony_ci               appPackageName);
126bc2ed2b3Sopenharmony_ci}
127bc2ed2b3Sopenharmony_ci
128bc2ed2b3Sopenharmony_civoid NfcHisysEvent::WriteDefaultRouteChangeHiSysEvent(int oldRoute, int newRoute)
129bc2ed2b3Sopenharmony_ci{
130bc2ed2b3Sopenharmony_ci    InfoLog("WriteDefaultRouteChangeHiSysEvent, oldRoute[%{public}d], newRoute[%{public}d]", oldRoute, newRoute);
131bc2ed2b3Sopenharmony_ci    WriteEvent("HCE_DEFAULT_ROUTE_CHANGE", HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
132bc2ed2b3Sopenharmony_ci               "OLD_DEFAULT_ROUTE", oldRoute,
133bc2ed2b3Sopenharmony_ci               "NEW_DEFAULT_ROUTE", newRoute);
134bc2ed2b3Sopenharmony_ci}
135bc2ed2b3Sopenharmony_ci
136bc2ed2b3Sopenharmony_civoid NfcHisysEvent::WriteAppBehaviorHiSysEvent(SubErrorCode behaviorCode, const std::string &appName)
137bc2ed2b3Sopenharmony_ci{
138bc2ed2b3Sopenharmony_ci    NfcFailedParams failedParams;
139bc2ed2b3Sopenharmony_ci    failedParams.mainErrorCode = APP_BEHAVIOR;
140bc2ed2b3Sopenharmony_ci    failedParams.subErrorCode = behaviorCode;
141bc2ed2b3Sopenharmony_ci    failedParams.appPackageName = appName;
142bc2ed2b3Sopenharmony_ci    WriteNfcFailedHiSysEvent(&failedParams);
143bc2ed2b3Sopenharmony_ci}
144bc2ed2b3Sopenharmony_ci}  // namespace NFC
145bc2ed2b3Sopenharmony_ci}  // namespace OHOS