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