/* * Copyright (C) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef NFC_SERVICE_H #define NFC_SERVICE_H #include #include #include "access_token.h" #include "ce_service.h" #include "infc_controller_callback.h" #include "infc_service.h" #include "nfc_controller_impl.h" #include "nfc_polling_manager.h" #include "nfc_routing_manager.h" #include "nfc_sdk_common.h" #include "inci_ce_interface.h" #include "inci_nfcc_interface.h" #include "inci_tag_interface.h" #include "host_card_emulation_manager.h" namespace OHOS { namespace NFC { class NfcStateRegistryRecord { public: std::string type_ = ""; Security::AccessToken::AccessTokenID callerToken_ = 0; sptr nfcStateChangeCallback_ = nullptr; }; class NfcService final : public NCI::INciTagInterface::ITagListener, public NCI::INciCeInterface::ICeHostListener, public INfcService, public std::enable_shared_from_this { public: NfcService(); ~NfcService() override; NfcService& operator=(const NfcService&) = delete; NfcService(const NfcService&) = delete; bool Initialize(); std::weak_ptr GetInstance() const; void OnTagDiscovered(uint32_t tagDiscId) override; void OnTagLost(uint32_t tagDiscId) override; void FieldActivated() override; void FieldDeactivated() override; #ifdef VENDOR_APPLICATIONS_ENABLED void OnVendorEvent(int eventType, int arg1, std::string arg2); #endif void OnCardEmulationData(const std::vector& data) override; void OnCardEmulationActivated() override; void OnCardEmulationDeactivated() override; OHOS::sptr GetTagServiceIface() override; OHOS::sptr GetHceServiceIface() override; bool IsNfcEnabled() override; int GetNfcState() override; int GetScreenState() override; int GetNciVersion() override; std::weak_ptr GetNciTagProxy(void); std::weak_ptr GetNfcPollingManager() override; std::weak_ptr GetNfcRoutingManager() override; std::weak_ptr GetCeService() override; std::string GetSimVendorBundleName() override; std::weak_ptr GetTagDispatcher() override; void NotifyMessageToVendor(int key, const std::string &value); private: bool IsNfcTaskReady(std::future& future) const; int ExecuteTask(KITS::NfcTask param); void UpdateNfcState(int newState); // TurnOn/TurnOff Nfc void NfcTaskThread(KITS::NfcTask params, std::promise promise); bool DoTurnOn(); bool DoTurnOff(); void DoInitialize(); void UnloadNfcSa(); // register callback based on different access token ID. int SetRegisterCallBack(const sptr &callback, const std::string& type, Security::AccessToken::AccessTokenID callerToken); int RemoveRegisterCallBack(const std::string& type, Security::AccessToken::AccessTokenID callerToken); int RemoveAllRegisterCallBack(Security::AccessToken::AccessTokenID callerToken); bool RegNdefMsgCb(const sptr &callback); // shutdown event void HandleShutdown(); void SetupUnloadNfcSaTimer(bool shouldRestartTimer); void CancelUnloadNfcSaTimer(); private: // ms wait for initialization, included firmware download. static constexpr const int WAIT_MS_INIT = 90 * 1000; static constexpr const int WAIT_ROUTING_INIT = 10 * 1000; static constexpr const int TASK_THREAD_WAIT_MS = 50; static constexpr const int TASK_THREAD_WAIT_US = 50 * 1000; static constexpr const int MAX_RETRY_TIME = 10; int nciVersion_ = 0; // service std::weak_ptr nfcService_ {}; std::shared_ptr nciNfccProxy_ {}; std::shared_ptr nciTagProxy_ {}; std::shared_ptr nciCeProxy_ {}; // polling manager std::shared_ptr nfcPollingManager_ {}; // routing manager std::shared_ptr nfcRoutingManager_ {}; OHOS::sptr tagSessionIface_{}; OHOS::sptr hceSessionIface_ {}; std::shared_ptr eventHandler_ {}; std::shared_ptr ceService_ {}; std::shared_ptr tagDispatcher_ {}; OHOS::sptr nfcControllerImpl_ {}; // save current state. int nfcState_; // save screen state int screenState_ {}; // current polling params std::shared_ptr currPollingParams_ {}; std::vector stateRecords_; // lock std::mutex mutex_ {}; std::future future_ {}; std::unique_ptr task_ {}; std::unique_ptr rootTask_ {}; // unload sa timer id static uint32_t unloadStaSaTimerId; friend class NfcWatchDog; friend class NfcControllerImpl; friend class TAG::TagDispatcher; friend class NfcSaManager; friend class NfcEventHandler; friend class CeService; #ifdef NDEF_WIFI_ENABLED friend class TAG::WifiConnectionManager; #endif #ifdef NDEF_BT_ENABLED friend class TAG::BtConnectionManager; #endif }; } // namespace NFC } // namespace OHOS #endif // NFC_SERVICE_H