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 32 namespace OHOS { 33 namespace NFC { 34 class NfcStateRegistryRecord { 35 public: 36 std::string type_ = ""; 37 Security::AccessToken::AccessTokenID callerToken_ = 0; 38 sptr<INfcControllerCallback> nfcStateChangeCallback_ = nullptr; 39 }; 40 41 class NfcService final : public NCI::INciTagInterface::ITagListener, 42 public NCI::INciCeInterface::ICeHostListener, 43 public INfcService, 44 public std::enable_shared_from_this<NfcService> { 45 public: 46 NfcService(); 47 ~NfcService() override; 48 NfcService& operator=(const NfcService&) = delete; 49 NfcService(const NfcService&) = delete; 50 bool Initialize(); 51 std::weak_ptr<NfcService> GetInstance() const; 52 void OnTagDiscovered(uint32_t tagDiscId) override; 53 void OnTagLost(uint32_t tagDiscId) override; 54 void FieldActivated() override; 55 void FieldDeactivated() override; 56 #ifdef VENDOR_APPLICATIONS_ENABLED 57 void OnVendorEvent(int eventType, int arg1, std::string arg2); 58 #endif 59 void OnCardEmulationData(const std::vector<uint8_t>& data) override; 60 void OnCardEmulationActivated() override; 61 void OnCardEmulationDeactivated() override; 62 OHOS::sptr<IRemoteObject> GetTagServiceIface() override; 63 OHOS::sptr<IRemoteObject> GetHceServiceIface() override; 64 65 bool IsNfcEnabled() override; 66 int GetNfcState() override; 67 int GetScreenState() override; 68 int GetNciVersion() override; 69 std::weak_ptr<NCI::INciTagInterface> GetNciTagProxy(void); 70 std::weak_ptr<NfcPollingManager> GetNfcPollingManager() override; 71 std::weak_ptr<NfcRoutingManager> GetNfcRoutingManager() override; 72 73 std::weak_ptr<CeService> GetCeService() override; 74 std::string GetSimVendorBundleName() override; 75 76 std::weak_ptr<TAG::TagDispatcher> GetTagDispatcher() override; 77 78 private: 79 bool IsNfcTaskReady(std::future<int>& future) const; 80 int ExecuteTask(KITS::NfcTask param); 81 void UpdateNfcState(int newState); 82 // TurnOn/TurnOff Nfc 83 void NfcTaskThread(KITS::NfcTask params, std::promise<int> promise); 84 bool DoTurnOn(); 85 bool DoTurnOff(); 86 void DoInitialize(); 87 static void UnloadNfcSa(); 88 89 // register callback based on different access token ID. 90 int SetRegisterCallBack(const sptr<INfcControllerCallback> &callback, 91 const std::string& type, Security::AccessToken::AccessTokenID callerToken); 92 int RemoveRegisterCallBack(const std::string& type, Security::AccessToken::AccessTokenID callerToken); 93 int RemoveAllRegisterCallBack(Security::AccessToken::AccessTokenID callerToken); 94 bool RegNdefMsgCb(const sptr<INdefMsgCallback> &callback); 95 // shutdown event 96 void HandleShutdown(); 97 void SetupUnloadNfcSaTimer(bool shouldRestartTimer); 98 void CancelUnloadNfcSaTimer(); 99 100 private: 101 // ms wait for initialization, included firmware download. 102 static constexpr const int WAIT_MS_INIT = 90 * 1000; 103 static constexpr const int WAIT_ROUTING_INIT = 10 * 1000; 104 static constexpr const int TASK_THREAD_WAIT_MS = 50; 105 static constexpr const int TASK_THREAD_WAIT_US = 50 * 1000; 106 static constexpr const int MAX_RETRY_TIME = 10; 107 int nciVersion_ = 0; 108 109 // service 110 std::weak_ptr<NfcService> nfcService_ {}; 111 std::shared_ptr<NCI::INciNfccInterface> nciNfccProxy_ {}; 112 std::shared_ptr<NCI::INciTagInterface> nciTagProxy_ {}; 113 std::shared_ptr<NCI::INciCeInterface> nciCeProxy_ {}; 114 // polling manager 115 std::shared_ptr<NfcPollingManager> nfcPollingManager_ {}; 116 // routing manager 117 std::shared_ptr<NfcRoutingManager> nfcRoutingManager_ {}; 118 OHOS::sptr<IRemoteObject> tagSessionIface_{}; 119 OHOS::sptr<IRemoteObject> hceSessionIface_ {}; 120 std::shared_ptr<NfcEventHandler> eventHandler_ {}; 121 std::shared_ptr<CeService> ceService_ {}; 122 std::shared_ptr<TAG::TagDispatcher> tagDispatcher_ {}; 123 OHOS::sptr<NfcControllerImpl> nfcControllerImpl_ {}; 124 // save current state. 125 int nfcState_; 126 // save screen state 127 int screenState_ {}; 128 // current polling params 129 std::shared_ptr<NfcPollingParams> currPollingParams_ {}; 130 131 std::vector<NfcStateRegistryRecord> stateRecords_; 132 // lock 133 std::mutex mutex_ {}; 134 std::future<int> future_ {}; 135 std::unique_ptr<std::thread> task_ {}; 136 std::unique_ptr<std::thread> rootTask_ {}; 137 138 // unload sa timer id 139 static uint32_t unloadStaSaTimerId; 140 141 friend class NfcWatchDog; 142 friend class NfcControllerImpl; 143 friend class TAG::TagDispatcher; 144 friend class NfcSaManager; 145 friend class NfcEventHandler; 146 friend class CeService; 147 #ifdef NDEF_WIFI_ENABLED 148 friend class TAG::WifiConnectionManager; 149 #endif 150 #ifdef NDEF_BT_ENABLED 151 friend class TAG::BtConnectionManager; 152 #endif 153 }; 154 } // namespace NFC 155 } // namespace OHOS 156 #endif // NFC_SERVICE_H 157