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 "nfc_taihe_controller_event.h"
17
18 #include <mutex>
19 #include <thread>
20
21 #include "iservice_registry.h"
22 #include "loghelper.h"
23 #include "nfc_controller.h"
24
25 namespace OHOS {
26 namespace NFC {
27 namespace KITS {
28 constexpr const char* EVENT_NFC_STATE_CHANGE = "nfcStateChange";
29 constexpr uint32_t WAIT_ON_REMOTE_DIED_MS = 20;
30 static std::mutex g_callbackMutex {};
31 static std::shared_ptr<
32 taihe::callback_view<void(ohos::nfc::controller::nfcController::NfcState)>> g_stateCallback = nullptr;
33 sptr<NfcStateListenerEvent> nfcStateListenerEvent = sptr<NfcStateListenerEvent>(new NfcStateListenerEvent());
34
OnNfcStateChanged(int nfcState)35 void NfcStateListenerEvent::OnNfcStateChanged(int nfcState)
36 {
37 InfoLog("OnNotify rcvd nfcRfState: %{public}d", nfcState);
38 std::lock_guard<std::mutex> lock(g_callbackMutex);
39 if (g_stateCallback) {
40 (*g_stateCallback)(static_cast<ohos::nfc::controller::nfcController::NfcState::key_t>(nfcState));
41 }
42 }
43
AsObject()44 OHOS::sptr<OHOS::IRemoteObject> NfcStateListenerEvent::AsObject()
45 {
46 return nullptr;
47 }
48
GetInstance()49 NfcStateEventRegister& NfcStateEventRegister::GetInstance()
50 {
51 static NfcStateEventRegister instance;
52 return instance;
53 }
54
Register(taihe::callback_view<void (ohos::nfc::controller::nfcController::NfcState)> callback)55 void NfcStateEventRegister::Register(
56 taihe::callback_view<void(ohos::nfc::controller::nfcController::NfcState)> callback)
57 {
58 std::lock_guard<std::mutex> lock(g_callbackMutex);
59 if (g_stateCallback) {
60 WarnLog("callback already registered.");
61 return;
62 }
63 ErrorCode ret = NfcController::GetInstance().RegListener(nfcStateListenerEvent, EVENT_NFC_STATE_CHANGE);
64 if (ret != KITS::ERR_NONE) {
65 ErrorLog("Register failed!");
66 return;
67 }
68 g_stateCallback =
69 std::make_shared<taihe::callback_view<void(ohos::nfc::controller::nfcController::NfcState)>>(callback);
70 }
71
Unregister()72 void NfcStateEventRegister::Unregister()
73 {
74 ErrorCode ret = NfcController::GetInstance().UnregListener(EVENT_NFC_STATE_CHANGE);
75 if (ret != KITS::ERR_NONE) {
76 ErrorLog("Unregister failed!");
77 return;
78 }
79 std::lock_guard<std::mutex> lock(g_callbackMutex);
80 g_stateCallback = nullptr;
81 }
82
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)83 void NfcTaiheSAStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
84 {
85 InfoLog("OnAddSystemAbility, systemAbilityId = %{public}d", systemAbilityId);
86 std::lock_guard<std::mutex> lock(g_callbackMutex);
87 if (g_stateCallback) {
88 InfoLog("OnAddSystemAbility g_stateCallback is not null");
89 // sleep 20ms for waitting recv OnRemoteDied msg, to reset nfc proxy.
90 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_ON_REMOTE_DIED_MS));
91 NfcController::GetInstance().RegListener(nfcStateListenerEvent, EVENT_NFC_STATE_CHANGE);
92 } else {
93 WarnLog("OnAddSystemAbility g_stateCallback is null");
94 }
95 }
96
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)97 void NfcTaiheSAStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
98 {
99 InfoLog("OnRemoveSystemAbility, systemAbilityId = %{public}d", systemAbilityId);
100 }
101
Init(int32_t systemAbilityId)102 void NfcTaiheSAStatusChange::Init(int32_t systemAbilityId)
103 {
104 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
105 if (!samgrProxy) {
106 ErrorLog("samgrProxy is nullptr");
107 return;
108 }
109 int32_t ret = samgrProxy->SubscribeSystemAbility(systemAbilityId, this);
110 InfoLog("SubscribeSystemAbility, systemAbilityId = %{public}d, ret = %{public}d.", systemAbilityId, ret);
111 }
112 } // namespace KITS
113 } // namespace NFC
114 } // namespace OHOS
115