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