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 TAG_SESSION_H
16 #define TAG_SESSION_H
17 #include <shared_mutex>
18 #include "element_name.h"
19 #include "itag_session.h"
20 #include "nfc_service.h"
21 #include "tag_dispatcher.h"
22 #include "tag_session_stub.h"
23 #include "nfc_polling_manager.h"
24 #include "inci_tag_interface.h"
25 #include "app_mgr_constants.h"
26 #include "infc_app_state_observer.h"
27 
28 namespace OHOS {
29 namespace NFC {
30 namespace TAG {
31 using OHOS::AppExecFwk::ElementName;
32 class FgData {
33 public:
34     // Indicates whether to enable the application to be foreground dispatcher
35     bool isEnableForeground_ = false;
36     ElementName element_;
37     std::vector<uint32_t> techs_ = {};
38     sptr<KITS::IForegroundCallback> cb_ = nullptr;
39 
FgData(bool isEnable,ElementName element,const std::vector<uint32_t> & techs,sptr<KITS::IForegroundCallback> cb)40     explicit FgData(bool isEnable, ElementName element, const std::vector<uint32_t> &techs,
41         sptr<KITS::IForegroundCallback> cb)
42         : isEnableForeground_(isEnable),
43         element_(element),
44         techs_(techs),
45         cb_(cb) {};
~FgData()46     ~FgData() {};
47 };
48 
49 class ReaderData {
50 public:
51     // Indicates whether to enable the application to be foreground dispatcher
52     bool isEnabled_ = false;
53     ElementName element_;
54     std::vector<uint32_t> techs_ = {};
55     sptr<KITS::IReaderModeCallback> cb_ = nullptr;
56 
ReaderData(bool isEnable,ElementName element,const std::vector<uint32_t> & techs,sptr<KITS::IReaderModeCallback> cb)57     explicit ReaderData(bool isEnable, ElementName element, const std::vector<uint32_t> &techs,
58         sptr<KITS::IReaderModeCallback> cb)
59         : isEnabled_(isEnable),
60         element_(element),
61         techs_(techs),
62         cb_(cb) {};
~ReaderData()63     ~ReaderData() {};
64 };
65 
66 class TagSession final : public TagSessionStub, public INfcAppStateObserver {
67 public:
68     // Constructor/Destructor
69     explicit TagSession(std::shared_ptr<NFC::NfcService> service);
70     ~TagSession() override;
71     TagSession(const TagSession&) = delete;
72     TagSession& operator=(const TagSession&) = delete;
73 
74     /**
75      * @brief To connect the tagRfDiscId by technology.
76      * @param tagRfDiscId the rf disc id of tag
77      * @param technology the tag technology
78      * @return the result to connect the tag
79      */
80     int Connect(int tagRfDiscId, int technology) override;
81     /**
82     * @brief To get connection status of tag.
83     * @param tagRfDiscId the rf disc id of tag
84     * @param isConnected the connection status of tag
85     * @return the result to get connection status of the tag
86     */
87     int IsConnected(int tagRfDiscId, bool &isConnected) override;
88     /**
89      * @brief To reconnect the tagRfDiscId.
90      * @param tagRfDiscId the rf disc id of tag
91      * @return the result to reconnect the tag
92      */
93     int Reconnect(int tagRfDiscId) override;
94     /**
95      * @brief To disconnect the tagRfDiscId.
96      * @param tagRfDiscId the rf disc id of tag
97      */
98     void Disconnect(int tagRfDiscId) override;
99         /**
100      * @brief Set the Timeout for tag operations
101      *
102      * @param timeout the timeout value to set for tag operations
103      * @param technology the tag technology
104      * @return true success of setting timeout value
105      * @return false failure of setting timeout value
106      */
107     int SetTimeout(int tagRfDiscId, int timeout, int technology) override;
108     /**
109      * @brief Get the Timeout value of tag operations
110      * @param tagRfDiscId the rf disc id of tag
111      * @param technology the tag technology
112      * @param timeout the output to read the timeout value.
113      * @return the status code of function calling.
114      */
115     int GetTimeout(int tagRfDiscId, int technology, int &timeout) override;
116     /**
117      * @brief Reset the Timeout value of tag operations
118      *
119      * @param tagRfDiscId the rf disc id of tag
120      */
121     void ResetTimeout(int tagRfDiscId) override;
122     /**
123      * @brief Get the TechList of the tagRfDiscId.
124      * @param tagRfDiscId the rf disc id of tag
125      * @return TechList
126      */
127     std::vector<int> GetTechList(int tagRfDiscId) override;
128     /**
129      * @brief Checking the tagRfDiscId is present.
130      * @param tagRfDiscId the rf disc id of tag
131      * @return true - Presnet; the other - No Presnet
132      */
133     bool IsTagFieldOn(int tagRfDiscId) override;
134     /**
135      * @brief Checking the tagRfDiscId is a Ndef Tag.
136      * @param tagRfDiscId the rf disc id of tag
137      * @return true - Ndef Tag; the other - No Ndef Tag
138      */
139     bool IsNdef(int tagRfDiscId) override;
140 
141     int SendRawFrame(const int tagRfDiscId, std::string hexCmdData, bool raw, std::string &hexRespData) override;
142     /**
143      * @brief Reading from the host tag
144      * @param tagRfDiscId the rf disc id of tag
145      * @param ndefMessage the read data
146      * @return the read Result
147      */
148     int NdefRead(int tagRfDiscId, std::string &ndefMessage) override;
149     /**
150      * @brief Writing the data into the host tag.
151      * @param tagRfDiscId the rf disc id of tag
152      * @param msg the wrote data
153      * @return the Writing Result
154      */
155     int NdefWrite(int tagRfDiscId, std::string msg) override;
156     /**
157      * @brief Making the host tag to read only.
158      * @param tagRfDiscId the rf disc id of tag
159      * @return the making result
160      */
161     int NdefMakeReadOnly(int tagRfDiscId) override;
162     /**
163      * @brief format the tag by Ndef
164      * @param tagRfDiscId the rf disc id of tag
165      * @param key the format key
166      * @return the format result
167      */
168     int FormatNdef(int tagRfDiscId, const std::string& key) override;
169 
170     int CanMakeReadOnly(int ndefType, bool &canSetReadOnly) override;
171     int GetMaxTransceiveLength(int technology, int &maxSize) override;
172     int IsSupportedApdusExtended(bool &isSupported) override;
173 
174     /**
175      * @brief register foreground dispatch
176      *
177      * @param element the element name of the hap that request to register foreground dispatch.
178      * @param discTech the tag technologies in int array the the hap wants to discover.
179      * @param callback the callback to be registered
180      * @return The status code for register operation.
181      */
182     int RegForegroundDispatch(ElementName &element,
183         std::vector<uint32_t> &discTech, const sptr<KITS::IForegroundCallback> &callback) override;
184 
185     /**
186      * @brief unregister foreground dispatch
187      *
188      * @param element the element name of the hap that request to unregister foreground dispatch.
189      * @return The status code for unregister operation.
190      */
191     int UnregForegroundDispatch(ElementName &element) override;
192 
193     /**
194      * @brief register reader mode
195      *
196      * @param element the element name of the hap that request to register reader mode.
197      * @param discTech the tag technologies in int array the the hap wants to discover.
198      * @param callback the callback to be registered
199      * @return The status code for register operation.
200      */
201     int RegReaderMode(ElementName &element,
202         std::vector<uint32_t> &discTech, const sptr<KITS::IReaderModeCallback> &callback) override;
203 
204     /**
205      * @brief unregister reader mode
206      *
207      * @param element the element name of the hap that request to unregister reader mode
208      * @return The status code for unregister operation.
209      */
210     int UnregReaderMode(ElementName &element) override;
211 
212     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
213 
214     /**
215      * @brief Get numbers of apps in registration of foregroundDispatch.
216      *
217      * @return FgDataVector Size.
218      */
219     uint16_t GetFgDataVecSize();
220 
221     /**
222      * @brief Get numbers of apps in registration of readerMode.
223      *
224      * @return readerDataVector Size.
225      */
226     uint16_t GetReaderDataVecSize();
227 
228     /**
229      * @brief Handle app state changed.
230      *
231      * @param bundleName bundle name.
232      * @param abilityName ability name.
233      * @param abilityState ability state.
234      */
235     void HandleAppStateChanged(const std::string &bundleName, const std::string &abilityName,
236                                int abilityState) override;
237 
238 private:
239     void CheckFgAppStateChanged(const std::string &bundleName, const std::string &abilityName, int abilityState);
240     void CheckReaderAppStateChanged(const std::string &bundleName, const std::string &abilityName, int abilityState);
241     bool IsFgRegistered(const ElementName &element, const std::vector<uint32_t> &discTech,
242         const sptr<KITS::IForegroundCallback> &callback);
243     bool IsFgUnregistered(const ElementName &element, bool isAppUnregister);
244     int RegForegroundDispatchInner(ElementName &element,
245         const std::vector<uint32_t> &discTech, const sptr<KITS::IForegroundCallback> &callback);
246     int UnregForegroundDispatchInner(const ElementName &element, bool isAppUnregister);
247     bool IsReaderRegistered(const ElementName &element, const std::vector<uint32_t> &discTech,
248         const sptr<KITS::IReaderModeCallback> &callback);
249     bool IsReaderUnregistered(const ElementName &element, bool isAppUnregistered);
250     int RegReaderModeInner(ElementName &element,
251         std::vector<uint32_t> &discTech, const sptr<KITS::IReaderModeCallback> &callback);
252     int UnregReaderModeInner(ElementName &element, bool isAppUnregister);
253     bool IsSameAppAbility(const ElementName &element, const ElementName &fgElement);
254     std::string GetDumpInfo();
255 #ifdef VENDOR_APPLICATIONS_ENABLED
256     bool IsVendorProcess();
257 #endif
258     std::weak_ptr<NFC::NfcService> nfcService_ {};
259     std::weak_ptr<NCI::INciTagInterface> nciTagProxy_ {};
260     // polling manager
261     std::weak_ptr<NfcPollingManager> nfcPollingManager_ {};
262     std::vector<FgData> fgDataVec_;
263     std::vector<ReaderData> readerDataVec_;
264     std::shared_mutex fgMutex_;
265 };
266 }  // namespace TAG
267 }  // namespace NFC
268 }  // namespace OHOS
269 #endif  // TAG_SESSION_H
270