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.h" 16 17 #include "loghelper.h" 18 #include "nfc_controller_callback_stub.h" 19 #include "nfc_sdk_common.h" 20 #include "infc_controller_callback.h" 21 #include "iservice_registry.h" 22 #include "system_ability_definition.h" 23 24 namespace OHOS { 25 namespace NFC { 26 namespace KITS { 27 std::shared_ptr<OHOS::NFC::NfcControllerProxy> NfcController::nfcControllerProxy_; 28 std::weak_ptr<INfcControllerService> NfcController::nfcControllerService_; 29 bool NfcController::initialized_ = false; 30 std::mutex NfcController::mutex_; 31 NfcController()32NfcController::NfcController() 33 { 34 DebugLog("[NfcController::NfcController] new ability manager"); 35 } 36 ~NfcController()37NfcController::~NfcController() 38 { 39 DebugLog("destruct NfcController"); 40 } 41 InitNfcController()42void NfcController::InitNfcController() 43 { 44 DebugLog("NfcController::InitNfcController in."); 45 std::lock_guard<std::mutex> guard(mutex_); 46 if (!initialized_ || nfcControllerService_.expired()) { 47 sptr<ISystemAbilityManager> systemAbilityMgr = 48 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 49 OHOS::sptr<OHOS::IRemoteObject> obj = systemAbilityMgr->GetSystemAbility(NFC_MANAGER_SYS_ABILITY_ID); 50 if (obj == nullptr) { 51 ErrorLog("Nfc Controller Is Unexist."); 52 return; 53 } 54 nfcControllerProxy_ = std::make_shared<NfcControllerProxy>(obj); 55 nfcControllerService_ = nfcControllerProxy_; 56 57 initialized_ = true; 58 } 59 60 DebugLog("NfcController::InitNfcController success."); 61 } 62 GetInstance()63NfcController &NfcController::GetInstance() 64 { 65 DebugLog("NfcController::GetInstance in."); 66 InitNfcController(); 67 static NfcController instance; 68 return instance; 69 } 70 71 // Open NFC TurnOn()72int NfcController::TurnOn() 73 { 74 if (nfcControllerService_.expired()) { 75 return ErrorCode::ERR_NFC_STATE_UNBIND; 76 } 77 return nfcControllerService_.lock()->TurnOn(); 78 } 79 80 // Close NFC TurnOff()81int NfcController::TurnOff() 82 { 83 if (nfcControllerService_.expired()) { 84 return ErrorCode::ERR_NFC_STATE_UNBIND; 85 } 86 return nfcControllerService_.lock()->TurnOff(); 87 } 88 89 // get NFC state GetNfcState()90int NfcController::GetNfcState() 91 { 92 if (nfcControllerService_.expired()) { 93 return NfcState::STATE_OFF; 94 } 95 return nfcControllerService_.lock()->GetState(); 96 } 97 98 // check whether NFC is supported IsNfcAvailable()99bool NfcController::IsNfcAvailable() 100 { 101 return true; 102 } 103 104 // check whether NFC is enabled IsNfcOpen(bool & isOpen)105int NfcController::IsNfcOpen(bool &isOpen) 106 { 107 if (nfcControllerService_.expired()) { 108 return ErrorCode::ERR_NFC_STATE_UNBIND; 109 } 110 return nfcControllerService_.lock()->IsNfcOpen(isOpen); 111 } 112 113 // register NFC state change callback RegListener(const sptr<INfcControllerCallback> & callback,const std::string & type)114ErrorCode NfcController::RegListener(const sptr<INfcControllerCallback> &callback, 115 const std::string& type) 116 { 117 DebugLog("NfcController::RegListener"); 118 return nfcControllerService_.lock()->RegisterCallBack(callback, type); 119 } 120 121 // unregister NFC state change UnregListener(const std::string & type)122ErrorCode NfcController::UnregListener(const std::string& type) 123 { 124 DebugLog("NfcController::UnregListener"); 125 return nfcControllerService_.lock()->UnRegisterCallBack(type); 126 } 127 GetTagServiceIface()128OHOS::sptr<IRemoteObject> NfcController::GetTagServiceIface() 129 { 130 return nfcControllerService_.lock()->GetTagServiceIface(); 131 } 132 } // namespace KITS 133 } // namespace NFC 134 } // namespace OHOS