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 #ifndef NFC_SERVICE_H 16 #define NFC_SERVICE_H 17 #include <future> 18 #include <mutex> 19 #include "access_token.h" 20 #include "ce_service.h" 21 #include "infc_controller_callback.h" 22 #include "infc_service.h" 23 #include "nfc_controller_impl.h" 24 #include "nfc_polling_manager.h" 25 #include "nfc_routing_manager.h" 26 #include "nfc_sdk_common.h" 27 #include "inci_ce_interface.h" 28 #include "inci_nfcc_interface.h" 29 #include "inci_tag_interface.h" 30 #include "host_card_emulation_manager.h" 31 #include "event_handler.h" 32 33 namespace OHOS { 34 namespace NFC { 35 class NfcStateRegistryRecord { 36 public: 37 std::string type_ = ""; 38 Security::AccessToken::AccessTokenID callerToken_ = 0; 39 sptr<INfcControllerCallback> nfcStateChangeCallback_ = nullptr; 40 }; 41 42 class NfcService : public NCI::INciTagInterface::ITagListener, 43 public NCI::INciCeInterface::ICeHostListener, 44 public INfcService, 45 public std::enable_shared_from_this<NfcService> { 46 public: 47 NfcService(); 48 ~NfcService() override; 49 NfcService& operator=(const NfcService&) = delete; 50 NfcService(const NfcService&) = delete; 51 bool Initialize(); 52 std::weak_ptr<NfcService> GetInstance() const; 53 void OnTagDiscovered(uint32_t tagDiscId) override; 54 void OnTagLost(uint32_t tagDiscId) override; 55 void FieldActivated() override; 56 void FieldDeactivated() override; 57 #ifdef VENDOR_APPLICATIONS_ENABLED 58 void OnVendorEvent(int eventType, int arg1, std::string arg2); 59 #endif 60 void OnCardEmulationData(const std::vector<uint8_t>& data) override; 61 void OnCardEmulationActivated() override; 62 void OnCardEmulationDeactivated() override; 63 OHOS::sptr<IRemoteObject> GetTagServiceIface() override; 64 OHOS::sptr<IRemoteObject> GetHceServiceIface() override; 65 66 bool IsNfcEnabled() override; 67 int GetNfcState() override; 68 int GetScreenState() override; 69 int GetNciVersion() override; 70 std::weak_ptr<NCI::INciNfccInterface> GetNciNfccProxy(void); 71 std::weak_ptr<NCI::INciTagInterface> GetNciTagProxy(void); 72 std::weak_ptr<NfcPollingManager> GetNfcPollingManager() override; 73 std::weak_ptr<NfcRoutingManager> GetNfcRoutingManager() override; 74 75 std::weak_ptr<CeService> GetCeService() override; 76 std::string GetSimVendorBundleName() override; 77 78 std::weak_ptr<TAG::TagDispatcher> GetTagDispatcher() override; 79 void NotifyMessageToVendor(int key, const std::string &value); 80 81 private: 82 int ExecuteTask(KITS::NfcTask param); 83 void UpdateNfcState(int newState); 84 // TurnOn/TurnOff Nfc 85 bool DoTurnOn(); 86 bool DoTurnOff(); 87 void DoInitialize(); 88 void UnloadNfcSa(); 89 90 // register callback based on different access token ID. 91 int SetRegisterCallBack(const sptr<INfcControllerCallback> &callback, 92 const std::string& type, Security::AccessToken::AccessTokenID callerToken); 93 int RemoveRegisterCallBack(const std::string& type, Security::AccessToken::AccessTokenID callerToken); 94 int RemoveAllRegisterCallBack(Security::AccessToken::AccessTokenID callerToken); 95 bool RegNdefMsgCb(const sptr<INdefMsgCallback> &callback); 96 // shutdown event 97 void HandleShutdown(); 98 void SetupUnloadNfcSaTimer(bool shouldRestartTimer); 99 void CancelUnloadNfcSaTimer(); 100 101 bool IsMaxSwitchRetryTime(); 102 103 class NfcSwitchEventHandler final : public AppExecFwk::EventHandler { 104 public: 105 explicit NfcSwitchEventHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner, 106 std::weak_ptr<NfcService> service); 107 ~NfcSwitchEventHandler(); 108 NfcSwitchEventHandler& operator=(const NfcSwitchEventHandler&) = delete; 109 NfcSwitchEventHandler(const NfcSwitchEventHandler&) = delete; 110 111 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 112 113 private: 114 bool CheckNfcState(int param); 115 116 private: 117 std::weak_ptr<NfcService> nfcService_; 118 }; 119 120 private: 121 // ms wait for initialization, included firmware download. 122 static constexpr const int WAIT_MS_INIT = 90 * 1000; 123 static constexpr const int WAIT_ROUTING_INIT = 10 * 1000; 124 static constexpr const int TASK_THREAD_WAIT_MS = 50; 125 static constexpr const int TASK_THREAD_WAIT_US = 50 * 1000; 126 static constexpr const int MAX_RETRY_TIME = 10; 127 static constexpr const int MAX_ABORT_RETRY_TIME = 3; 128 int nciVersion_ = 0; 129 130 // service 131 std::weak_ptr<NfcService> nfcService_ {}; 132 std::shared_ptr<NCI::INciNfccInterface> nciNfccProxy_ {}; 133 std::shared_ptr<NCI::INciTagInterface> nciTagProxy_ {}; 134 std::shared_ptr<NCI::INciCeInterface> nciCeProxy_ {}; 135 // polling manager 136 std::shared_ptr<NfcPollingManager> nfcPollingManager_ {}; 137 // routing manager 138 std::shared_ptr<NfcRoutingManager> nfcRoutingManager_ {}; 139 OHOS::sptr<IRemoteObject> tagSessionIface_{}; 140 OHOS::sptr<IRemoteObject> hceSessionIface_ {}; 141 std::shared_ptr<NfcEventHandler> eventHandler_ {}; 142 std::shared_ptr<CeService> ceService_ {}; 143 std::shared_ptr<TAG::TagDispatcher> tagDispatcher_ {}; 144 OHOS::sptr<NfcControllerImpl> nfcControllerImpl_ {}; 145 // save current state. 146 volatile int nfcState_; 147 // save screen state 148 int screenState_ {}; 149 // current polling params 150 std::shared_ptr<NfcPollingParams> currPollingParams_ {}; 151 152 std::vector<NfcStateRegistryRecord> stateRecords_; 153 std::shared_ptr<NfcSwitchEventHandler> nfcSwitchHandler_ = nullptr; 154 155 // lock 156 std::mutex mutex_ {}; 157 158 bool isAlreadyInited_ = false; 159 160 // unload sa timer id 161 static uint32_t unloadStaSaTimerId; 162 163 friend class NfcWatchDog; 164 friend class NfcControllerImpl; 165 friend class TAG::TagDispatcher; 166 friend class NfcSaManager; 167 friend class NfcEventHandler; 168 friend class CeService; 169 #ifdef NDEF_WIFI_ENABLED 170 friend class TAG::WifiConnectionManager; 171 #endif 172 #ifdef NDEF_BT_ENABLED 173 friend class TAG::BtConnectionManager; 174 #endif 175 }; 176 } // namespace NFC 177 } // namespace OHOS 178 #endif // NFC_SERVICE_H 179