• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cj_nfc_controller_event.h"
17 
18 #include "cj_lambda.h"
19 #include "loghelper.h"
20 #include "nfc_controller.h"
21 #include "nfc_sdk_common.h"
22 
23 namespace OHOS {
24 namespace NFC {
25 namespace KITS {
26 const std::string EVENT_NFC_STATE_CHANGE = "nfcStateChange";
27 
28 static std::set<std::string> g_supportEventList = {
29     EVENT_NFC_STATE_CHANGE,
30 };
31 
32 static std::mutex g_regInfoMutex;
33 static std::map<std::string, std::vector<std::tuple<int64_t, std::function<void(int32_t nfcState)>>>>
34     g_eventRegisterInfo;
35 
36 class NfcStateListenerEvent : public INfcControllerCallback {
37 public:
NfcStateListenerEvent()38     NfcStateListenerEvent() {}
39 
~NfcStateListenerEvent()40     virtual ~NfcStateListenerEvent() {}
41 
42 public:
OnNfcStateChanged(int nfcState)43     void OnNfcStateChanged(int nfcState) override
44     {
45         InfoLog("OnNotify rcvd nfcRfState: %{public}d", nfcState);
46         std::unique_lock<std::mutex> guard(g_regInfoMutex);
47         auto iter = g_eventRegisterInfo.find(EVENT_NFC_STATE_CHANGE);
48         if (iter == g_eventRegisterInfo.end()) {
49             return;
50         }
51         for (auto cb : iter->second) {
52             std::get<1>(cb)(nfcState);
53         }
54     }
55 
AsObject()56     OHOS::sptr<OHOS::IRemoteObject> AsObject() override
57     {
58         return nullptr;
59     }
60 };
61 
62 sptr<NfcStateListenerEvent> nfcStateListenerEvent = sptr<NfcStateListenerEvent>(new NfcStateListenerEvent());
63 
OnStateChange(int64_t callbackId)64 void OnStateChange(int64_t callbackId)
65 {
66     OHOS::DelayedSingleton<EventRegister>::GetInstance()->Register(EVENT_NFC_STATE_CHANGE, callbackId);
67 }
68 
OffStateChange(int64_t callbackId)69 void OffStateChange(int64_t callbackId)
70 {
71     OHOS::DelayedSingleton<EventRegister>::GetInstance()->Unregister(EVENT_NFC_STATE_CHANGE, callbackId);
72 }
73 
OffAllStateChange()74 void OffAllStateChange()
75 {
76     OHOS::DelayedSingleton<EventRegister>::GetInstance()->Unregister(EVENT_NFC_STATE_CHANGE);
77 }
78 
RegisterNfcStateChangedEvents(const std::string & type)79 ErrorCode EventRegister::RegisterNfcStateChangedEvents(const std::string& type)
80 {
81     NfcController nfcCtrl = NfcController::GetInstance();
82     ErrorCode ret = nfcCtrl.RegListener(nfcStateListenerEvent, type);
83     if (ret != KITS::ERR_NONE) {
84         ErrorLog("RegisterNfcStateChangedEvents nfcListenerEvent failed!");
85     }
86     return ret;
87 }
88 
UnRegisterNfcEvents(const std::string & type)89 ErrorCode EventRegister::UnRegisterNfcEvents(const std::string& type)
90 {
91     NfcController nfcCtrl = OHOS::NFC::KITS::NfcController::GetInstance();
92     ErrorCode ret = nfcCtrl.UnregListener(type);
93     if (ret != KITS::ERR_NONE) {
94         ErrorLog("UnRegisterNfcEvents nfcListenerEvent failed!");
95     }
96     return ret;
97 }
98 
IsEventSupport(const std::string & type)99 bool EventRegister::IsEventSupport(const std::string& type)
100 {
101     return g_supportEventList.find(type) != g_supportEventList.end();
102 }
103 
Register(const std::string & type,int64_t callbackId)104 void EventRegister::Register(const std::string& type, int64_t callbackId)
105 {
106     InfoLog("Register event: %{public}s", type.c_str());
107     if (!IsEventSupport(type)) {
108         ErrorLog("Register type error or not support!");
109         return;
110     }
111     std::unique_lock<std::mutex> guard(g_regInfoMutex);
112     RegisterNfcStateChangedEvents(type);
113     auto func = reinterpret_cast<void (*)(int32_t nfcState)>(callbackId);
114     auto cFunc = [lambda = CJLambda::Create(func)](int32_t nfcState) -> void { lambda(nfcState); };
115     auto iter = g_eventRegisterInfo.find(type);
116     if (iter == g_eventRegisterInfo.end()) {
117         g_eventRegisterInfo[type] = std::vector<std::tuple<int64_t, std::function<void(int32_t nfcState)>>> {
118             std::make_tuple(callbackId, cFunc)
119         };
120         return;
121     }
122     bool hasSameObj = false;
123     for (auto value : iter->second) {
124         if (callbackId == std::get<0>(value)) {
125             WarnLog("handler function is same");
126             hasSameObj = true;
127             break;
128         }
129     }
130     if (!hasSameObj) {
131         iter->second.emplace_back(std::make_tuple(callbackId, cFunc));
132     }
133 }
134 
Unregister(const std::string & type,int64_t callbackId)135 void EventRegister::Unregister(const std::string& type, int64_t callbackId)
136 {
137     InfoLog("Unregister event: %{public}s", type.c_str());
138     if (!IsEventSupport(type)) {
139         ErrorLog("Unregister type error or not support!");
140         return;
141     }
142     std::unique_lock<std::mutex> guard(g_regInfoMutex);
143     auto iter = g_eventRegisterInfo.find(type);
144     if (iter == g_eventRegisterInfo.end()) {
145         WarnLog("Unregister type not registered!");
146         if (UnRegisterNfcEvents(type) != KITS::ERR_NONE) {
147             ErrorLog("UnRegisterNfcEvents failed.");
148         }
149         return;
150     }
151     iter->second.erase(std::remove_if(iter->second.begin(), iter->second.end(),
152                            [callbackId](const std::tuple<int64_t, std::function<void(int32_t)>>& item) {
153                                return std::get<0>(item) == callbackId;
154                            }),
155         iter->second.end());
156     if (iter->second.empty()) {
157         g_eventRegisterInfo.erase(iter);
158         if (UnRegisterNfcEvents(type) != KITS::ERR_NONE) {
159             ErrorLog("UnRegisterNfcEvents failed.");
160         }
161     }
162 }
163 
Unregister(const std::string & type)164 void EventRegister::Unregister(const std::string& type)
165 {
166     InfoLog("Unregister event: %{public}s", type.c_str());
167     if (!IsEventSupport(type)) {
168         ErrorLog("Unregister type error or not support!");
169         return;
170     }
171     std::unique_lock<std::mutex> guard(g_regInfoMutex);
172     auto iter = g_eventRegisterInfo.find(type);
173     if (iter == g_eventRegisterInfo.end()) {
174         WarnLog("Unregister type not registered!");
175         if (UnRegisterNfcEvents(type) != KITS::ERR_NONE) {
176             ErrorLog("UnRegisterNfcEvents failed.");
177         }
178         return;
179     }
180     g_eventRegisterInfo.erase(iter);
181     if (UnRegisterNfcEvents(type) != KITS::ERR_NONE) {
182         ErrorLog("UnRegisterNfcEvents failed.");
183     }
184 }
185 } // namespace KITS
186 } // namespace NFC
187 } // namespace OHOS
188