• 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 
18 #include <future>
19 #include <mutex>
20 
21 #include "access_token.h"
22 #include "ce_service.h"
23 #include "common_event_manager.h"
24 #include "infc_controller_callback.h"
25 #include "infc_service.h"
26 #include "infcc_host.h"
27 #include "itag_host.h"
28 #include "nfc_controller_impl.h"
29 #include "nfc_polling_params.h"
30 #include "nfc_sdk_common.h"
31 
32 namespace OHOS {
33 namespace NFC {
34 class CommonEventHandler;
35 class NfcControllerImpl;
36 class CeService;
37 class NfcStateRegistryRecord {
38 public:
39     std::string type_ = "";
40     Security::AccessToken::AccessTokenID callerToken_ = 0;
41     sptr<INfcControllerCallback> nfcStateChangeCallback_ = nullptr;
42 };
43 
44 class ForegroundRegistryData {
45 public:
46     bool isEnable_ = false;
47     uint16_t techMask_ = 0xFFFF;
48     AppExecFwk::ElementName element_;
49     Security::AccessToken::AccessTokenID callerToken_ = 0;
50     sptr<KITS::IForegroundCallback> callback_ = nullptr;
51 };
52 
53 class NfcService final : public NCI::INfccHost::INfccHostListener,
54     public INfcService,
55     public std::enable_shared_from_this<NfcService> {
56 public:
57     NfcService(std::unique_ptr<NCI::INfccHost> nfccHost = nullptr);
58     ~NfcService() override;
59     NfcService& operator=(const NfcService&) = delete;
60     NfcService(const NfcService&) = delete;
61     bool Initialize();
62     std::weak_ptr<NfcService> GetInstance() const;
63     void OnTagDiscovered(std::shared_ptr<NCI::ITagHost> tagHost) override;
64     void FieldActivated() override;
65     void FieldDeactivated() override;
66     OHOS::sptr<IRemoteObject> GetTagServiceIface() override;
67     bool EnableForegroundDispatch(AppExecFwk::ElementName element, std::vector<uint32_t> &discTech,
68         const sptr<KITS::IForegroundCallback> &callback) override;
69     bool DisableForegroundDispatch(AppExecFwk::ElementName element) override;
70     bool DisableForegroundByDeathRcpt() override;
71     bool IsForegroundEnabled() override;
72     void SendTagToForeground(KITS::TagInfoParcelable tagInfo) override;
73 
74 protected:
75     // screen changed
76     void HandleScreenChanged(int screenState);
77     // package updated
78     void HandlePackageUpdated(std::shared_ptr<EventFwk::CommonEventData> data);
79     // commit routing
80     void HandleCommitRouting();
81     void HandleComputeRoutingParams();
82 
83 private:
84     std::weak_ptr<TAG::TagDispatcher> GetTagDispatcher() override;
85 
86     bool IsNfcEnabled() override;
87     int GetNfcState() override;
88     int GetScreenState() override;
89     int GetNciVersion() override;
GetNfccHost()90     std::weak_ptr<NCI::INfccHost> GetNfccHost() override
91     {
92         return nfccHost_;
93     }
94 
95     bool IsNfcTaskReady(std::future<int>& future) const;
96     void ExecuteTask(KITS::NfcTask param);
97     void UpdateNfcState(int newState);
98     // TurnOn/TurnOff Nfc
99     void NfcTaskThread(KITS::NfcTask params, std::promise<int> promise);
100     bool DoTurnOn();
101     bool DoTurnOff();
102     void DoInitialize();
103 
104     // register callback based on different access token ID.
105     int SetRegisterCallBack(const sptr<INfcControllerCallback> &callback,
106         const std::string& type, Security::AccessToken::AccessTokenID callerToken);
107     int RemoveRegisterCallBack(const std::string& type, Security::AccessToken::AccessTokenID callerToken);
108     int RemoveAllRegisterCallBack(Security::AccessToken::AccessTokenID callerToken);
109     // polling
110     void StartPollingLoop(bool force);
111     std::shared_ptr<NfcPollingParams> GetPollingParameters(int screenState);
112     uint16_t GetTechMaskFromTechList(std::vector<uint32_t> &discTech);
113     // commit routing
114     void CommitRouting();
115     void ComputeRoutingParams();
116 
117 private:
118     // ms wait for initialization, included firmware download.
119     static constexpr const int WAIT_MS_INIT = 90 * 1000;
120     // ms wait for setting the routing table.
121     static constexpr const int WAIT_MS_SET_ROUTE = 10 * 1000;
122     int nciVersion_ = 0;
123 
124     // service
125     std::weak_ptr<NfcService> nfcService_ {};
126     // NCI
127     std::shared_ptr<NCI::INfccHost> nfccHost_ {};
128 
129     OHOS::sptr<NfcControllerImpl> nfcControllerImpl_;
130     OHOS::sptr<IRemoteObject> tagSessionIface_{};
131     std::shared_ptr<CommonEventHandler> eventHandler_ {};
132     std::shared_ptr<CeService> ceService_ {};
133     std::shared_ptr<TAG::TagDispatcher> tagDispatcher_ {};
134     // save current state.
135     int nfcState_;
136     int screenState_ {};
137     // polling
138     std::shared_ptr<NfcPollingParams> currPollingParams_;
139     ForegroundRegistryData foregroundData_;
140 
141     std::vector<NfcStateRegistryRecord> stateRecords_;
142     // lock
143     std::mutex mutex_ {};
144     std::future<int> future_ {};
145     std::unique_ptr<std::thread> task_ {};
146     std::unique_ptr<std::thread> rootTask_ {};
147 
148     friend class NfcWatchDog;
149     friend class NfcControllerImpl;
150     friend class TAG::TagDispatcher;
151     friend class NfcSaManager;
152     friend class CommonEventHandler;
153     friend class CeService;
154 };
155 }  // namespace NFC
156 }  // namespace OHOS
157 #endif  // NFC_SERVICE_H
158