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 18 #include <future> 19 #include <mutex> 20 21 #include "common_event_manager.h" 22 #include "infc_service.h" 23 #include "infcc_host.h" 24 #include "itag_host.h" 25 #include "nfc_controller_impl.h" 26 #include "nfc_polling_params.h" 27 #include "nfc_sdk_common.h" 28 #include "infc_controller_callback.h" 29 #include "access_token.h" 30 31 namespace OHOS { 32 namespace NFC { 33 class CommonEventHandler; 34 class NfcControllerImpl; 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 final : public NCI::INfccHost::INfccHostListener, 43 public INfcService, 44 public std::enable_shared_from_this<NfcService> { 45 public: 46 NfcService(std::unique_ptr<NCI::INfccHost> nfccHost = nullptr); 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(std::shared_ptr<NCI::ITagHost> tagHost) override; 53 OHOS::sptr<IRemoteObject> GetTagServiceIface() override; 54 void ExecuteStartPollingLoop() override; 55 56 protected: 57 // screen changed 58 void HandleScreenChanged(int screenState); 59 // package updated 60 void HandlePackageUpdated(std::shared_ptr<EventFwk::CommonEventData> data); 61 62 private: 63 std::weak_ptr<TAG::TagDispatcher> GetTagDispatcher() override; 64 65 bool IsNfcEnabled() override; 66 int GetNfcState() override; 67 int GetScreenState() override; 68 int GetNciVersion() override; GetNfccHost()69 std::weak_ptr<NCI::INfccHost> GetNfccHost() override 70 { 71 return nfccHost_; 72 } 73 74 bool IsNfcTaskReady(std::future<int>& future) const; 75 void ExecuteTask(KITS::NfcTask param); 76 void UpdateNfcState(int newState); 77 // TurnOn/TurnOff Nfc 78 void NfcTaskThread(KITS::NfcTask params, std::promise<int> promise); 79 bool DoTurnOn(); 80 bool DoTurnOff(); 81 void DoInitialize(); 82 83 // register callback based on different access token ID. 84 int SetRegisterCallBack(const sptr<INfcControllerCallback> &callback, 85 const std::string& type, Security::AccessToken::AccessTokenID callerToken); 86 int RemoveRegisterCallBack(const std::string& type, Security::AccessToken::AccessTokenID callerToken); 87 int RemoveAllRegisterCallBack(Security::AccessToken::AccessTokenID callerToken); 88 // polling 89 void StartPollingLoop(bool force); 90 std::shared_ptr<NfcPollingParams> GetPollingParameters(int screenState); 91 92 private: 93 // ms wait for initialization, included firmware download. 94 static constexpr const int WAIT_MS_INIT = 90 * 1000; 95 // ms wait for setting the routing table. 96 static constexpr const int WAIT_MS_SET_ROUTE = 10 * 1000; 97 int nciVersion_ = 0; 98 99 // service 100 std::weak_ptr<NfcService> nfcService_ {}; 101 // NCI 102 std::shared_ptr<NCI::INfccHost> nfccHost_ {}; 103 104 OHOS::sptr<NfcControllerImpl> nfcControllerImpl_; 105 OHOS::sptr<IRemoteObject> tagSessionIface_{}; 106 std::shared_ptr<CommonEventHandler> eventHandler_ {}; 107 std::shared_ptr<TAG::TagDispatcher> tagDispatcher_ {}; 108 // save current state. 109 int nfcState_; 110 int screenState_ {}; 111 // polling 112 std::shared_ptr<NfcPollingParams> currPollingParams_; 113 114 std::vector<NfcStateRegistryRecord> stateRecords_; 115 // lock 116 std::mutex mutex_ {}; 117 std::future<int> future_ {}; 118 std::unique_ptr<std::thread> task_ {}; 119 std::unique_ptr<std::thread> rootTask_ {}; 120 121 friend class NfcWatchDog; 122 friend class NfcControllerImpl; 123 friend class TAG::TagDispatcher; 124 friend class NfcSaManager; 125 friend class CommonEventHandler; 126 }; 127 } // namespace NFC 128 } // namespace OHOS 129 #endif // NFC_SERVICE_H 130