1 /* 2 * Copyright (C) 2023 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_POLLING_MANAGER_H 16 #define NFC_POLLING_MANAGER_H 17 #include "access_token.h" 18 #include "common_event_manager.h" 19 #include "iforeground_callback.h" 20 #include "ireader_mode_callback.h" 21 #include "nfc_polling_params.h" 22 #include "taginfo_parcelable.h" 23 #include "inci_nfcc_interface.h" 24 #include "inci_tag_interface.h" 25 26 namespace OHOS { 27 namespace NFC { 28 // ms wait for setting the routing table. 29 static const int WAIT_MS_SET_ROUTE = 10 * 1000; 30 class NfcService; 31 class NfcPollingManager { 32 public: 33 NfcPollingManager(std::weak_ptr<NfcService> nfcService, 34 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy, 35 std::weak_ptr<NCI::INciTagInterface> nciTagProxy); 36 ~NfcPollingManager(); 37 class ForegroundRegistryData { 38 public: 39 bool isEnabled_ = false; 40 bool isVendorApp_ = false; 41 uint16_t techMask_ = 0xFFFF; 42 AppExecFwk::ElementName element_; 43 Security::AccessToken::AccessTokenID callerToken_ = 0; 44 sptr<KITS::IForegroundCallback> callback_ = nullptr; 45 }; 46 47 class ReaderModeRegistryData { 48 public: 49 bool isEnabled_ = false; 50 bool isVendorApp_ = false; 51 uint16_t techMask_ = 0xFFFF; 52 AppExecFwk::ElementName element_; 53 Security::AccessToken::AccessTokenID callerToken_ = 0; 54 sptr<KITS::IReaderModeCallback> callback_ = nullptr; 55 }; 56 57 void ResetCurrPollingParams(); 58 std::shared_ptr<NfcPollingParams> GetCurrentParameters(); 59 std::shared_ptr<NfcPollingParams> GetPollingParameters(int screenState); 60 bool CheckForegroundAbility(const std::string &readerBundle, const std::string &readerAbility); 61 62 void StartPollingLoop(bool force); 63 // screen changed 64 void HandleScreenChanged(int screenState); 65 // package updated 66 bool HandlePackageUpdated(std::shared_ptr<EventFwk::CommonEventData> data); 67 68 bool EnableForegroundDispatch(const AppExecFwk::ElementName &element, const std::vector<uint32_t> &discTech, 69 const sptr<KITS::IForegroundCallback> &callback, bool isVendorApp = false); 70 bool DisableForegroundDispatch(const AppExecFwk::ElementName &element); 71 bool DisableForegroundByDeathRcpt(); 72 bool IsForegroundEnabled(); 73 void SendTagToForeground(KITS::TagInfoParcelable* tagInfo); 74 std::shared_ptr<NfcPollingManager::ForegroundRegistryData> GetForegroundData(); 75 76 bool EnableReaderMode(const AppExecFwk::ElementName &element, const std::vector<uint32_t> &discTech, 77 const sptr<KITS::IReaderModeCallback> &callback, bool isVendorApp = false); 78 bool DisableReaderMode(const AppExecFwk::ElementName &element); 79 bool DisableReaderModeByDeathRcpt(); 80 bool IsReaderModeEnabled(); 81 void SendTagToReaderApp(KITS::TagInfoParcelable* tagInfo); 82 std::shared_ptr<NfcPollingManager::ReaderModeRegistryData> GetReaderModeData(); 83 void SetForegroundAbility(const std::string &bundleName, const std::string &abilityName, int abilityState); 84 85 private: 86 bool CheckForegroundApp(const std::string &readerBundle); 87 88 int screenState_ = 0; 89 std::shared_ptr<NfcPollingManager::ForegroundRegistryData> foregroundData_ {}; 90 std::shared_ptr<NfcPollingManager::ReaderModeRegistryData> readerModeData_ {}; 91 std::shared_ptr<NfcPollingParams> currPollingParams_ {}; 92 std::weak_ptr<NfcService> nfcService_ {}; 93 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy_ {}; 94 std::weak_ptr<NCI::INciTagInterface> nciTagProxy_ {}; 95 96 std::mutex mutex_ {}; 97 }; 98 } // namespace NFC 99 } // namespace OHOS 100 #endif // NFC_POLLING_MANAGER_H