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 void NotifyMessageToVendor(int key, const std::string &value); 78 79 private: 80 bool IsNfcTaskReady(std::future<int>& future) const; 81 int ExecuteTask(KITS::NfcTask param); 82 void UpdateNfcState(int newState); 83 // TurnOn/TurnOff Nfc 84 void NfcTaskThread(KITS::NfcTask params, std::promise<int> promise); 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 private: 102 // ms wait for initialization, included firmware download. 103 static constexpr const int WAIT_MS_INIT = 90 * 1000; 104 static constexpr const int WAIT_ROUTING_INIT = 10 * 1000; 105 static constexpr const int TASK_THREAD_WAIT_MS = 50; 106 static constexpr const int TASK_THREAD_WAIT_US = 50 * 1000; 107 static constexpr const int MAX_RETRY_TIME = 10; 108 int nciVersion_ = 0; 109 110 // service 111 std::weak_ptr<NfcService> nfcService_ {}; 112 std::shared_ptr<NCI::INciNfccInterface> nciNfccProxy_ {}; 113 std::shared_ptr<NCI::INciTagInterface> nciTagProxy_ {}; 114 std::shared_ptr<NCI::INciCeInterface> nciCeProxy_ {}; 115 // polling manager 116 std::shared_ptr<NfcPollingManager> nfcPollingManager_ {}; 117 // routing manager 118 std::shared_ptr<NfcRoutingManager> nfcRoutingManager_ {}; 119 OHOS::sptr<IRemoteObject> tagSessionIface_{}; 120 OHOS::sptr<IRemoteObject> hceSessionIface_ {}; 121 std::shared_ptr<NfcEventHandler> eventHandler_ {}; 122 std::shared_ptr<CeService> ceService_ {}; 123 std::shared_ptr<TAG::TagDispatcher> tagDispatcher_ {}; 124 OHOS::sptr<NfcControllerImpl> nfcControllerImpl_ {}; 125 // save current state. 126 int nfcState_; 127 // save screen state 128 int screenState_ {}; 129 // current polling params 130 std::shared_ptr<NfcPollingParams> currPollingParams_ {}; 131 132 std::vector<NfcStateRegistryRecord> stateRecords_; 133 // lock 134 std::mutex mutex_ {}; 135 std::future<int> future_ {}; 136 std::unique_ptr<std::thread> task_ {}; 137 std::unique_ptr<std::thread> rootTask_ {}; 138 139 // unload sa timer id 140 static uint32_t unloadStaSaTimerId; 141 142 friend class NfcWatchDog; 143 friend class NfcControllerImpl; 144 friend class TAG::TagDispatcher; 145 friend class NfcSaManager; 146 friend class NfcEventHandler; 147 friend class CeService; 148 #ifdef NDEF_WIFI_ENABLED 149 friend class TAG::WifiConnectionManager; 150 #endif 151 #ifdef NDEF_BT_ENABLED 152 friend class TAG::BtConnectionManager; 153 #endif 154 }; 155 } // namespace NFC 156 } // namespace OHOS 157 #endif // NFC_SERVICE_H 158