1bc2ed2b3Sopenharmony_ci/* 2bc2ed2b3Sopenharmony_ci * Copyright (C) 2022 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_controller.h" 16bc2ed2b3Sopenharmony_ci 17bc2ed2b3Sopenharmony_ci#include "loghelper.h" 18bc2ed2b3Sopenharmony_ci#include "nfc_controller_callback_stub.h" 19bc2ed2b3Sopenharmony_ci#include "nfc_sa_client.h" 20bc2ed2b3Sopenharmony_ci#include "nfc_sdk_common.h" 21bc2ed2b3Sopenharmony_ci#include "indef_msg_callback.h" 22bc2ed2b3Sopenharmony_ci#include "infc_controller_callback.h" 23bc2ed2b3Sopenharmony_ci#include "iservice_registry.h" 24bc2ed2b3Sopenharmony_ci#include "system_ability_definition.h" 25bc2ed2b3Sopenharmony_ci#ifdef VENDOR_APPLICATIONS_ENABLED 26bc2ed2b3Sopenharmony_ci#include "on_card_emulation_notify_cb_stub.h" 27bc2ed2b3Sopenharmony_ci#include "query_app_info_callback_stub.h" 28bc2ed2b3Sopenharmony_ci#endif 29bc2ed2b3Sopenharmony_ci 30bc2ed2b3Sopenharmony_cinamespace OHOS { 31bc2ed2b3Sopenharmony_cinamespace NFC { 32bc2ed2b3Sopenharmony_cinamespace KITS { 33bc2ed2b3Sopenharmony_cistd::shared_ptr<OHOS::NFC::NfcControllerProxy> NfcController::nfcControllerProxy_; 34bc2ed2b3Sopenharmony_cistd::weak_ptr<INfcControllerService> NfcController::nfcControllerService_; 35bc2ed2b3Sopenharmony_cisptr<IRemoteObject::DeathRecipient> NfcController::deathRecipient_; 36bc2ed2b3Sopenharmony_cisptr<IRemoteObject> NfcController::remote_; 37bc2ed2b3Sopenharmony_cibool NfcController::initialized_ = false; 38bc2ed2b3Sopenharmony_cibool NfcController::remoteDied_ = true; 39bc2ed2b3Sopenharmony_cistd::mutex NfcController::mutex_; 40bc2ed2b3Sopenharmony_ci#ifdef VENDOR_APPLICATIONS_ENABLED 41bc2ed2b3Sopenharmony_cistatic sptr<QueryAppInfoCallbackStub> g_queryAppInfoCallbackStub = 42bc2ed2b3Sopenharmony_ci sptr<QueryAppInfoCallbackStub>(new (std::nothrow) QueryAppInfoCallbackStub()); 43bc2ed2b3Sopenharmony_cistatic sptr<OnCardEmulationNotifyCbStub> g_onCardEmulationNotifyCbStub = 44bc2ed2b3Sopenharmony_ci sptr<OnCardEmulationNotifyCbStub>(new (std::nothrow) OnCardEmulationNotifyCbStub()); 45bc2ed2b3Sopenharmony_ci#endif 46bc2ed2b3Sopenharmony_ci 47bc2ed2b3Sopenharmony_ciNfcController::NfcController() 48bc2ed2b3Sopenharmony_ci{ 49bc2ed2b3Sopenharmony_ci DebugLog("[NfcController::NfcController] new ability manager"); 50bc2ed2b3Sopenharmony_ci deathRecipient_ = new (std::nothrow) NfcServiceDeathRecipient(*this); 51bc2ed2b3Sopenharmony_ci} 52bc2ed2b3Sopenharmony_ci 53bc2ed2b3Sopenharmony_ciNfcController::~NfcController() 54bc2ed2b3Sopenharmony_ci{ 55bc2ed2b3Sopenharmony_ci DebugLog("destruct NfcController"); 56bc2ed2b3Sopenharmony_ci} 57bc2ed2b3Sopenharmony_ci 58bc2ed2b3Sopenharmony_civoid NfcController::InitNfcRemoteSA() 59bc2ed2b3Sopenharmony_ci{ 60bc2ed2b3Sopenharmony_ci DebugLog("NfcController::%{public}s in, initialized_ = %{public}d, nfcControllerService_ = %{public}d", 61bc2ed2b3Sopenharmony_ci __func__, initialized_, nfcControllerService_.expired()); 62bc2ed2b3Sopenharmony_ci std::lock_guard<std::mutex> guard(mutex_); 63bc2ed2b3Sopenharmony_ci if (!initialized_ || nfcControllerService_.expired() || remoteDied_) { 64bc2ed2b3Sopenharmony_ci remote_ = NfcSaClient::GetInstance().LoadNfcSa(NFC_MANAGER_SYS_ABILITY_ID); 65bc2ed2b3Sopenharmony_ci if (remote_ == nullptr) { 66bc2ed2b3Sopenharmony_ci ErrorLog("Nfc Controller Is Unexist."); 67bc2ed2b3Sopenharmony_ci return; 68bc2ed2b3Sopenharmony_ci } 69bc2ed2b3Sopenharmony_ci if (deathRecipient_ == nullptr) { 70bc2ed2b3Sopenharmony_ci WarnLog("deathRecipient_ is nullptr!"); 71bc2ed2b3Sopenharmony_ci } 72bc2ed2b3Sopenharmony_ci remote_->AddDeathRecipient(deathRecipient_); 73bc2ed2b3Sopenharmony_ci InfoLog("%{public}s:add remote death listener", __func__); 74bc2ed2b3Sopenharmony_ci nfcControllerProxy_ = std::make_shared<NfcControllerProxy>(remote_); 75bc2ed2b3Sopenharmony_ci nfcControllerService_ = nfcControllerProxy_; 76bc2ed2b3Sopenharmony_ci 77bc2ed2b3Sopenharmony_ci initialized_ = true; 78bc2ed2b3Sopenharmony_ci remoteDied_ = false; 79bc2ed2b3Sopenharmony_ci } 80bc2ed2b3Sopenharmony_ci DebugLog("NfcController::%{public}s success.", __func__); 81bc2ed2b3Sopenharmony_ci} 82bc2ed2b3Sopenharmony_ci 83bc2ed2b3Sopenharmony_ciNfcController &NfcController::GetInstance() 84bc2ed2b3Sopenharmony_ci{ 85bc2ed2b3Sopenharmony_ci DebugLog("NfcController::GetInstance in."); 86bc2ed2b3Sopenharmony_ci static NfcController instance; 87bc2ed2b3Sopenharmony_ci return instance; 88bc2ed2b3Sopenharmony_ci} 89bc2ed2b3Sopenharmony_ci 90bc2ed2b3Sopenharmony_civoid NfcController::OnRemoteDied(const wptr<IRemoteObject> &remoteObject) 91bc2ed2b3Sopenharmony_ci{ 92bc2ed2b3Sopenharmony_ci WarnLog("%{public}s:Remote service is died!", __func__); 93bc2ed2b3Sopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 94bc2ed2b3Sopenharmony_ci remoteDied_ = true; 95bc2ed2b3Sopenharmony_ci initialized_ = false; 96bc2ed2b3Sopenharmony_ci if (deathRecipient_ == nullptr || remoteObject == nullptr) { 97bc2ed2b3Sopenharmony_ci ErrorLog("deathRecipient_ is nullptr!"); 98bc2ed2b3Sopenharmony_ci return; 99bc2ed2b3Sopenharmony_ci } 100bc2ed2b3Sopenharmony_ci if (remote_ == nullptr) { 101bc2ed2b3Sopenharmony_ci ErrorLog("remote_ is nullptr!"); 102bc2ed2b3Sopenharmony_ci return; 103bc2ed2b3Sopenharmony_ci } 104bc2ed2b3Sopenharmony_ci remote_->RemoveDeathRecipient(deathRecipient_); 105bc2ed2b3Sopenharmony_ci 106bc2ed2b3Sopenharmony_ci nfcControllerService_.reset(); 107bc2ed2b3Sopenharmony_ci nfcControllerProxy_ = nullptr; 108bc2ed2b3Sopenharmony_ci remote_ = nullptr; 109bc2ed2b3Sopenharmony_ci} 110bc2ed2b3Sopenharmony_ci 111bc2ed2b3Sopenharmony_ci// Open NFC 112bc2ed2b3Sopenharmony_ciint NfcController::TurnOn() 113bc2ed2b3Sopenharmony_ci{ 114bc2ed2b3Sopenharmony_ci InitNfcRemoteSA(); 115bc2ed2b3Sopenharmony_ci if (nfcControllerService_.expired()) { 116bc2ed2b3Sopenharmony_ci return ErrorCode::ERR_NFC_STATE_UNBIND; 117bc2ed2b3Sopenharmony_ci } 118bc2ed2b3Sopenharmony_ci return nfcControllerService_.lock()->TurnOn(); 119bc2ed2b3Sopenharmony_ci} 120bc2ed2b3Sopenharmony_ci 121bc2ed2b3Sopenharmony_ci// Close NFC 122bc2ed2b3Sopenharmony_ciint NfcController::TurnOff() 123bc2ed2b3Sopenharmony_ci{ 124bc2ed2b3Sopenharmony_ci InitNfcRemoteSA(); 125bc2ed2b3Sopenharmony_ci if (nfcControllerService_.expired()) { 126bc2ed2b3Sopenharmony_ci return ErrorCode::ERR_NFC_STATE_UNBIND; 127bc2ed2b3Sopenharmony_ci } 128bc2ed2b3Sopenharmony_ci return nfcControllerService_.lock()->TurnOff(); 129bc2ed2b3Sopenharmony_ci} 130bc2ed2b3Sopenharmony_ci 131bc2ed2b3Sopenharmony_ci// get NFC state 132bc2ed2b3Sopenharmony_ciint NfcController::GetNfcState() 133bc2ed2b3Sopenharmony_ci{ 134bc2ed2b3Sopenharmony_ci int state = NfcState::STATE_OFF; 135bc2ed2b3Sopenharmony_ci if (!NfcSaClient::GetInstance().CheckNfcSystemAbility()) { 136bc2ed2b3Sopenharmony_ci WarnLog("Nfc SA not started yet."); 137bc2ed2b3Sopenharmony_ci return state; 138bc2ed2b3Sopenharmony_ci } 139bc2ed2b3Sopenharmony_ci InitNfcRemoteSA(); 140bc2ed2b3Sopenharmony_ci if (nfcControllerService_.expired()) { 141bc2ed2b3Sopenharmony_ci ErrorLog("Nfc controller service expired."); 142bc2ed2b3Sopenharmony_ci return state; 143bc2ed2b3Sopenharmony_ci } 144bc2ed2b3Sopenharmony_ci state = nfcControllerService_.lock()->GetState(); 145bc2ed2b3Sopenharmony_ci InfoLog("nfc state: %{public}d.", state); 146bc2ed2b3Sopenharmony_ci return state; 147bc2ed2b3Sopenharmony_ci} 148bc2ed2b3Sopenharmony_ci 149bc2ed2b3Sopenharmony_ci// check whether NFC is supported 150bc2ed2b3Sopenharmony_cibool NfcController::IsNfcAvailable() 151bc2ed2b3Sopenharmony_ci{ 152bc2ed2b3Sopenharmony_ci return true; 153bc2ed2b3Sopenharmony_ci} 154bc2ed2b3Sopenharmony_ci 155bc2ed2b3Sopenharmony_ci// check whether NFC is enabled 156bc2ed2b3Sopenharmony_ciint NfcController::IsNfcOpen(bool &isOpen) 157bc2ed2b3Sopenharmony_ci{ 158bc2ed2b3Sopenharmony_ci isOpen = (GetNfcState() == NfcState::STATE_ON); 159bc2ed2b3Sopenharmony_ci return ErrorCode::ERR_NONE; 160bc2ed2b3Sopenharmony_ci} 161bc2ed2b3Sopenharmony_ci 162bc2ed2b3Sopenharmony_ci// register NFC state change callback 163bc2ed2b3Sopenharmony_ciErrorCode NfcController::RegListener(const sptr<INfcControllerCallback> &callback, 164bc2ed2b3Sopenharmony_ci const std::string& type) 165bc2ed2b3Sopenharmony_ci{ 166bc2ed2b3Sopenharmony_ci InfoLog("NfcController::RegListener"); 167bc2ed2b3Sopenharmony_ci if (!NfcSaClient::GetInstance().CheckNfcSystemAbility()) { 168bc2ed2b3Sopenharmony_ci WarnLog("nfc SA not started yet."); 169bc2ed2b3Sopenharmony_ci return ErrorCode::ERR_NFC_STATE_UNBIND; 170bc2ed2b3Sopenharmony_ci } 171bc2ed2b3Sopenharmony_ci InitNfcRemoteSA(); 172bc2ed2b3Sopenharmony_ci if (nfcControllerService_.expired()) { 173bc2ed2b3Sopenharmony_ci ErrorLog("nfcControllerService_ expired."); 174bc2ed2b3Sopenharmony_ci return ErrorCode::ERR_NFC_STATE_UNBIND; 175bc2ed2b3Sopenharmony_ci } 176bc2ed2b3Sopenharmony_ci return nfcControllerService_.lock()->RegisterCallBack(callback, type); 177bc2ed2b3Sopenharmony_ci} 178bc2ed2b3Sopenharmony_ci 179bc2ed2b3Sopenharmony_ci// unregister NFC state change 180bc2ed2b3Sopenharmony_ciErrorCode NfcController::UnregListener(const std::string& type) 181bc2ed2b3Sopenharmony_ci{ 182bc2ed2b3Sopenharmony_ci InfoLog("NfcController::UnregListener"); 183bc2ed2b3Sopenharmony_ci if (!NfcSaClient::GetInstance().CheckNfcSystemAbility()) { 184bc2ed2b3Sopenharmony_ci WarnLog("nfc SA not started yet."); 185bc2ed2b3Sopenharmony_ci return ErrorCode::ERR_NFC_STATE_UNBIND; 186bc2ed2b3Sopenharmony_ci } 187bc2ed2b3Sopenharmony_ci InitNfcRemoteSA(); 188bc2ed2b3Sopenharmony_ci if (nfcControllerService_.expired()) { 189bc2ed2b3Sopenharmony_ci ErrorLog("nfcControllerService_ expired."); 190bc2ed2b3Sopenharmony_ci return ErrorCode::ERR_NFC_STATE_UNBIND; 191bc2ed2b3Sopenharmony_ci } 192bc2ed2b3Sopenharmony_ci return nfcControllerService_.lock()->UnRegisterCallBack(type); 193bc2ed2b3Sopenharmony_ci} 194bc2ed2b3Sopenharmony_ci 195bc2ed2b3Sopenharmony_ciOHOS::sptr<IRemoteObject> NfcController::GetTagServiceIface() 196bc2ed2b3Sopenharmony_ci{ 197bc2ed2b3Sopenharmony_ci InitNfcRemoteSA(); 198bc2ed2b3Sopenharmony_ci if (nfcControllerService_.expired()) { 199bc2ed2b3Sopenharmony_ci ErrorLog("NfcController::GetTagServiceIface nfcControllerService_ expired"); 200bc2ed2b3Sopenharmony_ci return nullptr; 201bc2ed2b3Sopenharmony_ci } 202bc2ed2b3Sopenharmony_ci return nfcControllerService_.lock()->GetTagServiceIface(); 203bc2ed2b3Sopenharmony_ci} 204bc2ed2b3Sopenharmony_ci 205bc2ed2b3Sopenharmony_ciErrorCode NfcController::RegNdefMsgCb(const sptr<INdefMsgCallback> &callback) 206bc2ed2b3Sopenharmony_ci{ 207bc2ed2b3Sopenharmony_ci DebugLog("NfcController::RegNdefMsgCb"); 208bc2ed2b3Sopenharmony_ci InitNfcRemoteSA(); 209bc2ed2b3Sopenharmony_ci if (nfcControllerService_.expired()) { 210bc2ed2b3Sopenharmony_ci ErrorLog("NfcController::RegNdefMsgCb nfcControllerService_ expired"); 211bc2ed2b3Sopenharmony_ci return ErrorCode::ERR_NFC_STATE_UNBIND; 212bc2ed2b3Sopenharmony_ci } 213bc2ed2b3Sopenharmony_ci return nfcControllerService_.lock()->RegNdefMsgCb(callback); 214bc2ed2b3Sopenharmony_ci} 215bc2ed2b3Sopenharmony_ci 216bc2ed2b3Sopenharmony_ci#ifdef VENDOR_APPLICATIONS_ENABLED 217bc2ed2b3Sopenharmony_ciErrorCode NfcController::RegQueryApplicationCb(const std::string& type, 218bc2ed2b3Sopenharmony_ci QueryApplicationByVendor tagCallback, QueryHceAppByVendor hceCallback) 219bc2ed2b3Sopenharmony_ci{ 220bc2ed2b3Sopenharmony_ci DebugLog("NfcController::RegQueryApplicationCb"); 221bc2ed2b3Sopenharmony_ci InitNfcRemoteSA(); 222bc2ed2b3Sopenharmony_ci if (nfcControllerService_.expired()) { 223bc2ed2b3Sopenharmony_ci ErrorLog("NfcController::RegQueryApplicationCb nfcControllerService_ expired"); 224bc2ed2b3Sopenharmony_ci return ErrorCode::ERR_NFC_STATE_UNBIND; 225bc2ed2b3Sopenharmony_ci } 226bc2ed2b3Sopenharmony_ci if (type.compare(KEY_TAG_APP) == 0) { 227bc2ed2b3Sopenharmony_ci g_queryAppInfoCallbackStub->RegisterQueryTagAppCallback(tagCallback); 228bc2ed2b3Sopenharmony_ci } else if (type.compare(KEY_HCE_APP) == 0) { 229bc2ed2b3Sopenharmony_ci g_queryAppInfoCallbackStub->RegisterQueryHceAppCallback(hceCallback); 230bc2ed2b3Sopenharmony_ci } 231bc2ed2b3Sopenharmony_ci return nfcControllerService_.lock()->RegQueryApplicationCb(g_queryAppInfoCallbackStub); 232bc2ed2b3Sopenharmony_ci} 233bc2ed2b3Sopenharmony_ci 234bc2ed2b3Sopenharmony_ciErrorCode NfcController::RegCardEmulationNotifyCb(OnCardEmulationNotifyCb callback) 235bc2ed2b3Sopenharmony_ci{ 236bc2ed2b3Sopenharmony_ci DebugLog("NfcController::RegCardEmulationNotifyCb"); 237bc2ed2b3Sopenharmony_ci InitNfcRemoteSA(); 238bc2ed2b3Sopenharmony_ci if (nfcControllerService_.expired()) { 239bc2ed2b3Sopenharmony_ci ErrorLog("NfcController::RegCardEmulationNotifyCb nfcControllerService_ expired"); 240bc2ed2b3Sopenharmony_ci return ErrorCode::ERR_NFC_STATE_UNBIND; 241bc2ed2b3Sopenharmony_ci } 242bc2ed2b3Sopenharmony_ci g_onCardEmulationNotifyCbStub->RegisterCallback(callback); 243bc2ed2b3Sopenharmony_ci return nfcControllerService_.lock()->RegCardEmulationNotifyCb(g_onCardEmulationNotifyCbStub); 244bc2ed2b3Sopenharmony_ci} 245bc2ed2b3Sopenharmony_ciErrorCode NfcController::NotifyEventStatus(int eventType, int arg1, std::string arg2) 246bc2ed2b3Sopenharmony_ci{ 247bc2ed2b3Sopenharmony_ci DebugLog("NfcController::NotifyEventStatus"); 248bc2ed2b3Sopenharmony_ci InitNfcRemoteSA(); 249bc2ed2b3Sopenharmony_ci if (nfcControllerService_.expired()) { 250bc2ed2b3Sopenharmony_ci ErrorLog("NfcController::NotifyEventStatus nfcControllerService_ expired"); 251bc2ed2b3Sopenharmony_ci return ErrorCode::ERR_NFC_STATE_UNBIND; 252bc2ed2b3Sopenharmony_ci } 253bc2ed2b3Sopenharmony_ci return nfcControllerService_.lock()->NotifyEventStatus(eventType, arg1, arg2); 254bc2ed2b3Sopenharmony_ci} 255bc2ed2b3Sopenharmony_ci#endif 256bc2ed2b3Sopenharmony_ci 257bc2ed2b3Sopenharmony_ciOHOS::sptr<IRemoteObject> NfcController::GetHceServiceIface() 258bc2ed2b3Sopenharmony_ci{ 259bc2ed2b3Sopenharmony_ci InitNfcRemoteSA(); 260bc2ed2b3Sopenharmony_ci if (nfcControllerService_.expired()) { 261bc2ed2b3Sopenharmony_ci ErrorLog("NfcController::GetHceServiceIface nfcControllerService_ expired"); 262bc2ed2b3Sopenharmony_ci return nullptr; 263bc2ed2b3Sopenharmony_ci } 264bc2ed2b3Sopenharmony_ci return nfcControllerService_.lock()->GetHceServiceIface(); 265bc2ed2b3Sopenharmony_ci} 266bc2ed2b3Sopenharmony_ci} // namespace KITS 267bc2ed2b3Sopenharmony_ci} // namespace NFC 268bc2ed2b3Sopenharmony_ci} // namespace OHOS