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_impl.h" 16bc2ed2b3Sopenharmony_ci#include "nfc_sdk_common.h" 17bc2ed2b3Sopenharmony_ci#include "nfc_service.h" 18bc2ed2b3Sopenharmony_ci#include "loghelper.h" 19bc2ed2b3Sopenharmony_ci#include "external_deps_proxy.h" 20bc2ed2b3Sopenharmony_ci 21bc2ed2b3Sopenharmony_cinamespace OHOS { 22bc2ed2b3Sopenharmony_cinamespace NFC { 23bc2ed2b3Sopenharmony_ciconst std::string DUMP_LINE = "---------------------------"; 24bc2ed2b3Sopenharmony_ciconst std::string DUMP_END = "\n"; 25bc2ed2b3Sopenharmony_ci 26bc2ed2b3Sopenharmony_ciNfcControllerImpl::NfcControllerImpl(std::weak_ptr<NfcService> nfcService) 27bc2ed2b3Sopenharmony_ci : NfcControllerStub(), nfcService_(nfcService) 28bc2ed2b3Sopenharmony_ci{ 29bc2ed2b3Sopenharmony_ci} 30bc2ed2b3Sopenharmony_ci 31bc2ed2b3Sopenharmony_ciNfcControllerImpl::~NfcControllerImpl() 32bc2ed2b3Sopenharmony_ci{ 33bc2ed2b3Sopenharmony_ci} 34bc2ed2b3Sopenharmony_ci 35bc2ed2b3Sopenharmony_ciint NfcControllerImpl::GetState() 36bc2ed2b3Sopenharmony_ci{ 37bc2ed2b3Sopenharmony_ci if (nfcService_.expired()) { 38bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 39bc2ed2b3Sopenharmony_ci } 40bc2ed2b3Sopenharmony_ci return nfcService_.lock()->GetNfcState(); 41bc2ed2b3Sopenharmony_ci} 42bc2ed2b3Sopenharmony_ci 43bc2ed2b3Sopenharmony_ciint NfcControllerImpl::TurnOn() 44bc2ed2b3Sopenharmony_ci{ 45bc2ed2b3Sopenharmony_ci if (nfcService_.expired()) { 46bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 47bc2ed2b3Sopenharmony_ci } 48bc2ed2b3Sopenharmony_ci return nfcService_.lock()->ExecuteTask(KITS::TASK_TURN_ON); 49bc2ed2b3Sopenharmony_ci} 50bc2ed2b3Sopenharmony_ci 51bc2ed2b3Sopenharmony_ciint NfcControllerImpl::TurnOff() 52bc2ed2b3Sopenharmony_ci{ 53bc2ed2b3Sopenharmony_ci if (nfcService_.expired()) { 54bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 55bc2ed2b3Sopenharmony_ci } 56bc2ed2b3Sopenharmony_ci return nfcService_.lock()->ExecuteTask(KITS::TASK_TURN_OFF); 57bc2ed2b3Sopenharmony_ci} 58bc2ed2b3Sopenharmony_ci 59bc2ed2b3Sopenharmony_ciint NfcControllerImpl::IsNfcOpen(bool &isOpen) 60bc2ed2b3Sopenharmony_ci{ 61bc2ed2b3Sopenharmony_ci if (nfcService_.expired()) { 62bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 63bc2ed2b3Sopenharmony_ci } 64bc2ed2b3Sopenharmony_ci isOpen = nfcService_.lock()->IsNfcEnabled(); 65bc2ed2b3Sopenharmony_ci return KITS::ERR_NONE; 66bc2ed2b3Sopenharmony_ci} 67bc2ed2b3Sopenharmony_ci 68bc2ed2b3Sopenharmony_ciKITS::ErrorCode NfcControllerImpl::RegisterCallBack(const sptr<INfcControllerCallback> &callback, 69bc2ed2b3Sopenharmony_ci const std::string& type, Security::AccessToken::AccessTokenID callerToken) 70bc2ed2b3Sopenharmony_ci{ 71bc2ed2b3Sopenharmony_ci if (nfcService_.expired()) { 72bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 73bc2ed2b3Sopenharmony_ci } 74bc2ed2b3Sopenharmony_ci if (!nfcService_.lock()->SetRegisterCallBack(callback, type, callerToken)) { 75bc2ed2b3Sopenharmony_ci return KITS::ERR_NONE; 76bc2ed2b3Sopenharmony_ci } 77bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 78bc2ed2b3Sopenharmony_ci} 79bc2ed2b3Sopenharmony_ci 80bc2ed2b3Sopenharmony_ciKITS::ErrorCode NfcControllerImpl::UnRegisterCallBack(const std::string& type, 81bc2ed2b3Sopenharmony_ci Security::AccessToken::AccessTokenID callerToken) 82bc2ed2b3Sopenharmony_ci{ 83bc2ed2b3Sopenharmony_ci if (nfcService_.expired()) { 84bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 85bc2ed2b3Sopenharmony_ci } 86bc2ed2b3Sopenharmony_ci if (!nfcService_.lock()->RemoveRegisterCallBack(type, callerToken)) { 87bc2ed2b3Sopenharmony_ci return KITS::ERR_NONE; 88bc2ed2b3Sopenharmony_ci } 89bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 90bc2ed2b3Sopenharmony_ci} 91bc2ed2b3Sopenharmony_ci 92bc2ed2b3Sopenharmony_ciKITS::ErrorCode NfcControllerImpl::UnRegisterAllCallBack(Security::AccessToken::AccessTokenID callerToken) 93bc2ed2b3Sopenharmony_ci{ 94bc2ed2b3Sopenharmony_ci if (nfcService_.expired()) { 95bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 96bc2ed2b3Sopenharmony_ci } 97bc2ed2b3Sopenharmony_ci if (!nfcService_.lock()->RemoveAllRegisterCallBack(callerToken)) { 98bc2ed2b3Sopenharmony_ci return KITS::ERR_NONE; 99bc2ed2b3Sopenharmony_ci } 100bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 101bc2ed2b3Sopenharmony_ci} 102bc2ed2b3Sopenharmony_ci 103bc2ed2b3Sopenharmony_ciOHOS::sptr<IRemoteObject> NfcControllerImpl::GetTagServiceIface() 104bc2ed2b3Sopenharmony_ci{ 105bc2ed2b3Sopenharmony_ci if (nfcService_.expired()) { 106bc2ed2b3Sopenharmony_ci return nullptr; 107bc2ed2b3Sopenharmony_ci } 108bc2ed2b3Sopenharmony_ci return nfcService_.lock()->GetTagServiceIface(); 109bc2ed2b3Sopenharmony_ci} 110bc2ed2b3Sopenharmony_ci 111bc2ed2b3Sopenharmony_ciKITS::ErrorCode NfcControllerImpl::RegNdefMsgCallback(const sptr<INdefMsgCallback> &callback) 112bc2ed2b3Sopenharmony_ci{ 113bc2ed2b3Sopenharmony_ci if (nfcService_.expired()) { 114bc2ed2b3Sopenharmony_ci ErrorLog("NfcControllerImpl::RegNdefMsgCallback nfcService_ expired"); 115bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 116bc2ed2b3Sopenharmony_ci } 117bc2ed2b3Sopenharmony_ci if (nfcService_.lock()->RegNdefMsgCb(callback)) { 118bc2ed2b3Sopenharmony_ci return KITS::ERR_NONE; 119bc2ed2b3Sopenharmony_ci } 120bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 121bc2ed2b3Sopenharmony_ci} 122bc2ed2b3Sopenharmony_ci 123bc2ed2b3Sopenharmony_ci#ifdef VENDOR_APPLICATIONS_ENABLED 124bc2ed2b3Sopenharmony_ciKITS::ErrorCode NfcControllerImpl::RegQueryApplicationCb(const sptr<IQueryAppInfoCallback> callback) 125bc2ed2b3Sopenharmony_ci{ 126bc2ed2b3Sopenharmony_ci ExternalDepsProxy::GetInstance().RegQueryApplicationCb(callback); 127bc2ed2b3Sopenharmony_ci return KITS::ERR_NONE; 128bc2ed2b3Sopenharmony_ci} 129bc2ed2b3Sopenharmony_ci 130bc2ed2b3Sopenharmony_ciKITS::ErrorCode NfcControllerImpl::RegCardEmulationNotifyCb(const sptr<IOnCardEmulationNotifyCb> callback) 131bc2ed2b3Sopenharmony_ci{ 132bc2ed2b3Sopenharmony_ci ExternalDepsProxy::GetInstance().RegCardEmulationNotifyCb(callback); 133bc2ed2b3Sopenharmony_ci return KITS::ERR_NONE; 134bc2ed2b3Sopenharmony_ci} 135bc2ed2b3Sopenharmony_ciKITS::ErrorCode NfcControllerImpl::NotifyEventStatus(int eventType, int arg1, std::string arg2) 136bc2ed2b3Sopenharmony_ci{ 137bc2ed2b3Sopenharmony_ci if (nfcService_.lock() == nullptr) { 138bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 139bc2ed2b3Sopenharmony_ci } 140bc2ed2b3Sopenharmony_ci 141bc2ed2b3Sopenharmony_ci nfcService_.lock()->OnVendorEvent(eventType, arg1, arg2); 142bc2ed2b3Sopenharmony_ci return KITS::ErrorCode(); 143bc2ed2b3Sopenharmony_ci} 144bc2ed2b3Sopenharmony_ci#endif 145bc2ed2b3Sopenharmony_ci 146bc2ed2b3Sopenharmony_ciOHOS::sptr<IRemoteObject> NfcControllerImpl::GetHceServiceIface() 147bc2ed2b3Sopenharmony_ci{ 148bc2ed2b3Sopenharmony_ci if (nfcService_.lock() == nullptr) { 149bc2ed2b3Sopenharmony_ci return nullptr; 150bc2ed2b3Sopenharmony_ci } 151bc2ed2b3Sopenharmony_ci return nfcService_.lock()->GetHceServiceIface(); 152bc2ed2b3Sopenharmony_ci} 153bc2ed2b3Sopenharmony_ci 154bc2ed2b3Sopenharmony_ciint32_t NfcControllerImpl::Dump(int32_t fd, const std::vector<std::u16string>& args) 155bc2ed2b3Sopenharmony_ci{ 156bc2ed2b3Sopenharmony_ci if (nfcService_.expired()) { 157bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 158bc2ed2b3Sopenharmony_ci } 159bc2ed2b3Sopenharmony_ci std::string info = GetDumpInfo(); 160bc2ed2b3Sopenharmony_ci int ret = dprintf(fd, "%s\n", info.c_str()); 161bc2ed2b3Sopenharmony_ci if (ret < 0) { 162bc2ed2b3Sopenharmony_ci ErrorLog("NfcControllerImpl Dump ret = %{public}d", ret); 163bc2ed2b3Sopenharmony_ci return KITS::ERR_NFC_PARAMETERS; 164bc2ed2b3Sopenharmony_ci } 165bc2ed2b3Sopenharmony_ci return KITS::ERR_NONE; 166bc2ed2b3Sopenharmony_ci} 167bc2ed2b3Sopenharmony_ci 168bc2ed2b3Sopenharmony_cistd::string NfcControllerImpl::GetDumpInfo() 169bc2ed2b3Sopenharmony_ci{ 170bc2ed2b3Sopenharmony_ci std::string info; 171bc2ed2b3Sopenharmony_ci return info.append(DUMP_LINE) 172bc2ed2b3Sopenharmony_ci .append(" NFC DUMP ") 173bc2ed2b3Sopenharmony_ci .append(DUMP_LINE) 174bc2ed2b3Sopenharmony_ci .append(DUMP_END) 175bc2ed2b3Sopenharmony_ci .append("NFC_STATE : ") 176bc2ed2b3Sopenharmony_ci .append(std::to_string(nfcService_.lock()->GetNfcState())) 177bc2ed2b3Sopenharmony_ci .append(DUMP_END) 178bc2ed2b3Sopenharmony_ci .append("SCREEN_STATE : ") 179bc2ed2b3Sopenharmony_ci .append(std::to_string(nfcService_.lock()->GetScreenState())) 180bc2ed2b3Sopenharmony_ci .append(DUMP_END) 181bc2ed2b3Sopenharmony_ci .append("NCI_VERSION : ") 182bc2ed2b3Sopenharmony_ci .append(std::to_string(nfcService_.lock()->GetNciVersion())) 183bc2ed2b3Sopenharmony_ci .append(DUMP_END); 184bc2ed2b3Sopenharmony_ci} 185bc2ed2b3Sopenharmony_ci} // namespace NFC 186bc2ed2b3Sopenharmony_ci} // namespace OHOS 187