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