• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "event_handler.h"
32 
33 namespace OHOS {
34 namespace NFC {
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 : public NCI::INciTagInterface::ITagListener,
43     public NCI::INciCeInterface::ICeHostListener,
44     public INfcService,
45     public std::enable_shared_from_this<NfcService> {
46 public:
47     NfcService();
48     ~NfcService() override;
49     NfcService& operator=(const NfcService&) = delete;
50     NfcService(const NfcService&) = delete;
51     bool Initialize();
52     std::weak_ptr<NfcService> GetInstance() const;
53     void OnTagDiscovered(uint32_t tagDiscId) override;
54     void OnTagLost(uint32_t tagDiscId) override;
55     void FieldActivated() override;
56     void FieldDeactivated() override;
57 #ifdef VENDOR_APPLICATIONS_ENABLED
58     void OnVendorEvent(int eventType, int arg1, std::string arg2);
59 #endif
60     void OnCardEmulationData(const std::vector<uint8_t>& data) override;
61     void OnCardEmulationActivated() override;
62     void OnCardEmulationDeactivated() override;
63     OHOS::sptr<IRemoteObject> GetTagServiceIface() override;
64     OHOS::sptr<IRemoteObject> GetHceServiceIface() override;
65 
66     bool IsNfcEnabled() override;
67     int GetNfcState() override;
68     int GetScreenState() override;
69     int GetNciVersion() override;
70     std::weak_ptr<NCI::INciNfccInterface> GetNciNfccProxy(void);
71     std::weak_ptr<NCI::INciTagInterface> GetNciTagProxy(void);
72     std::weak_ptr<NfcPollingManager> GetNfcPollingManager() override;
73     std::weak_ptr<NfcRoutingManager> GetNfcRoutingManager() override;
74 
75     std::weak_ptr<CeService> GetCeService() override;
76     std::string GetSimVendorBundleName() override;
77 
78     std::weak_ptr<TAG::TagDispatcher> GetTagDispatcher() override;
79     void NotifyMessageToVendor(int key, const std::string &value);
80 
81 private:
82     int ExecuteTask(KITS::NfcTask param);
83     void UpdateNfcState(int newState);
84     // TurnOn/TurnOff Nfc
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     class NfcSwitchEventHandler final : public AppExecFwk::EventHandler {
102     public:
103         explicit NfcSwitchEventHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner,
104                                         std::weak_ptr<NfcService> service);
105         ~NfcSwitchEventHandler();
106         NfcSwitchEventHandler& operator=(const NfcSwitchEventHandler&) = delete;
107         NfcSwitchEventHandler(const NfcSwitchEventHandler&) = delete;
108 
109         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
110 
111     private:
112         bool CheckNfcState(int param);
113 
114     private:
115         std::weak_ptr<NfcService> nfcService_;
116     };
117 
118 private:
119     // ms wait for initialization, included firmware download.
120     static constexpr const int WAIT_MS_INIT = 90 * 1000;
121     static constexpr const int WAIT_ROUTING_INIT = 10 * 1000;
122     static constexpr const int TASK_THREAD_WAIT_MS = 50;
123     static constexpr const int TASK_THREAD_WAIT_US = 50 * 1000;
124     static constexpr const int MAX_RETRY_TIME = 10;
125     int nciVersion_ = 0;
126 
127     // service
128     std::weak_ptr<NfcService> nfcService_ {};
129     std::shared_ptr<NCI::INciNfccInterface> nciNfccProxy_ {};
130     std::shared_ptr<NCI::INciTagInterface> nciTagProxy_ {};
131     std::shared_ptr<NCI::INciCeInterface> nciCeProxy_ {};
132     // polling manager
133     std::shared_ptr<NfcPollingManager> nfcPollingManager_ {};
134     // routing manager
135     std::shared_ptr<NfcRoutingManager> nfcRoutingManager_ {};
136     OHOS::sptr<IRemoteObject> tagSessionIface_{};
137     OHOS::sptr<IRemoteObject> hceSessionIface_ {};
138     std::shared_ptr<NfcEventHandler> eventHandler_ {};
139     std::shared_ptr<CeService> ceService_ {};
140     std::shared_ptr<TAG::TagDispatcher> tagDispatcher_ {};
141     OHOS::sptr<NfcControllerImpl> nfcControllerImpl_ {};
142     // save current state.
143     int nfcState_;
144     // save screen state
145     int screenState_ {};
146     // current polling params
147     std::shared_ptr<NfcPollingParams> currPollingParams_ {};
148 
149     std::vector<NfcStateRegistryRecord> stateRecords_;
150     std::shared_ptr<NfcSwitchEventHandler> nfcSwitchHandler_ = nullptr;
151 
152     // lock
153     std::mutex mutex_ {};
154 
155     // unload sa timer id
156     static uint32_t unloadStaSaTimerId;
157 
158     friend class NfcWatchDog;
159     friend class NfcControllerImpl;
160     friend class TAG::TagDispatcher;
161     friend class NfcSaManager;
162     friend class NfcEventHandler;
163     friend class CeService;
164 #ifdef NDEF_WIFI_ENABLED
165     friend class TAG::WifiConnectionManager;
166 #endif
167 #ifdef NDEF_BT_ENABLED
168     friend class TAG::BtConnectionManager;
169 #endif
170 };
171 }  // namespace NFC
172 }  // namespace OHOS
173 #endif  // NFC_SERVICE_H
174