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_HOST_H 16 #define TAG_HOST_H 17 #include <mutex> 18 #include "itag_host.h" 19 #include "synchronize_event.h" 20 21 namespace OHOS { 22 namespace NFC { 23 namespace NCI { 24 class TagHost : public ITagHost { 25 public: 26 static const int DATA_BYTE2 = 2; 27 static const int DATA_BYTE3 = 3; 28 static const int DATA_BYTE4 = 4; 29 static const int DATA_BYTE5 = 5; 30 static const int DATA_BYTE6 = 6; 31 static const int DATA_BYTE7 = 7; 32 static const int DATA_BYTE8 = 8; 33 static const int DATA_BYTE9 = 9; 34 // NFC_B 35 static const int NCI_APP_DATA_LENGTH = 4; 36 static const int NCI_PROTOCOL_INFO_LENGTH = 3; 37 // MifareUltralight 38 static const int NCI_MIFARE_ULTRALIGHT_C_RESPONSE_LENGTH = 16; 39 static const int NCI_MIFARE_ULTRALIGHT_C_BLANK_CARD = 0; 40 static const int NCI_MIFARE_ULTRALIGHT_C_VERSION_INFO_FIRST = 0x02; 41 static const int NCI_MIFARE_ULTRALIGHT_C_VERSION_INFO_SECOND = 0x00; 42 static const int NCI_MIFARE_ULTRALIGHT_C_NDEF_CC = 0xE1; 43 static const int NCI_MIFARE_ULTRALIGHT_C_NDEF_MAJOR_VERSION = 0x20; 44 static const int NCI_MIFARE_ULTRALIGHT_C_NDEF_TAG_SIZE = 0x06; 45 // Iso15693 46 static const int NCI_POLL_LENGTH_MIN = 2; 47 static const int DEFAULT_PRESENCE_CHECK_WATCH_DOG_TIMEOUT = 125; 48 // T1T~T4T for NDEF tag 49 static const auto NDEF_UNKNOWN_TYPE = -1; 50 static const auto NDEF_TYPE1_TAG = 1; 51 static const auto NDEF_TYPE2_TAG = 2; 52 static const auto NDEF_TYPE3_TAG = 3; 53 static const auto NDEF_TYPE4_TAG = 4; 54 static const auto NDEF_MIFARE_CLASSIC_TAG = 101; 55 56 static const auto TARGET_TYPE_UNKNOWN = 0; 57 static const auto TARGET_TYPE_ISO14443_3A = 1; 58 static const auto TARGET_TYPE_ISO14443_3B = 2; 59 static const auto TARGET_TYPE_ISO14443_4 = 3; 60 static const auto TARGET_TYPE_FELICA = 4; 61 static const auto TARGET_TYPE_V = 5; 62 static const auto TARGET_TYPE_NDEF = 6; 63 static const auto TARGET_TYPE_NDEF_FORMATABLE = 7; 64 static const auto TARGET_TYPE_MIFARE_CLASSIC = 8; 65 static const auto TARGET_TYPE_MIFARE_UL = 9; 66 67 // NfcF, Felica 68 static const int SENSF_RES_LENGTH = 8; 69 static const int F_POLL_LENGTH = 10; 70 71 public: 72 TagHost(const std::vector<int>& tagTechList, 73 const std::vector<int>& tagRfDiscIdList, 74 const std::vector<int>& tagActivatedProtocols, 75 const std::string& uid, 76 const std::vector<std::string>& tagPollBytes, 77 const std::vector<std::string>& tagActivatedBytes); 78 ~TagHost() override; 79 bool Connect(int technology) override; 80 bool Disconnect() override; 81 bool Reconnect() override; 82 int Transceive(std::string& request, std::string& response) override; 83 84 // get the tag related technologies or uid info. 85 std::vector<int> GetTechList() override; 86 int GetConnectedTech() override; 87 void RemoveTech(int tech) override; 88 std::vector<AppExecFwk::PacMap> GetTechExtrasData() override; 89 std::string GetTagUid() override; 90 int GetTagRfDiscId() override; 91 92 // functions for nedf tag only. 93 std::string ReadNdef() override; 94 bool WriteNdef(std::string& data) override; 95 bool IsNdefFormatable() override; 96 bool FormatNdef(const std::string& key) override; 97 bool SetNdefReadOnly() override; 98 bool IsNdefMsgContained(std::vector<int>& ndefInfo) override; 99 100 // functions for checking the tag field on or not. 101 bool FieldOnCheckingThread() override; 102 bool IsTagFieldOn() override; 103 void OnFieldChecking(TagDisconnectedCallBack callback, int delayedMs) override; 104 void OffFieldChecking() override; 105 106 private: 107 AppExecFwk::PacMap ParseTechExtras(int index); 108 void FieldCheckingThread(TagHost::TagDisconnectedCallBack callback, int delayedMs); 109 void PauseFieldChecking(); 110 void ResumeFieldChecking(); 111 void AddNdefTech(); 112 void AddNdefTechToTagInfo(int tech, int discId, int actProto, AppExecFwk::PacMap pacMap); 113 int GetNdefType(int protocol) const; 114 bool IsUltralightC(); 115 116 void DoTargetTypeIso144433a(AppExecFwk::PacMap &pacMap, int index); 117 void DoTargetTypeIso144433b(AppExecFwk::PacMap &pacMap, int index); 118 void DoTargetTypeIso144434(AppExecFwk::PacMap &pacMap, int index); 119 void DoTargetTypeV(AppExecFwk::PacMap &pacMap, int index); 120 void DoTargetTypeF(AppExecFwk::PacMap &pacMap, int index); 121 void DoTargetTypeNdef(AppExecFwk::PacMap &pacMap); 122 123 static OHOS::NFC::SynchronizeEvent fieldCheckWatchDog_; 124 std::mutex mutex_ {}; 125 /* NFC-A NFC-B NFC-F NFC-V... */ 126 std::vector<int> tagTechList_; 127 std::vector<AppExecFwk::PacMap> techExtras_; 128 std::vector<int> tagRfDiscIdList_; 129 std::vector<int> tagActivatedProtocols_; 130 std::string tagUid_; 131 std::vector<std::string> tagPollBytes_; 132 std::vector<std::string> tagActivatedBytes_; 133 int connectedTagDiscId_; 134 int connectedTechIndex_; 135 volatile bool isTagFieldOn_; 136 volatile bool isFieldChecking_; 137 volatile bool isPauseFieldChecking_; 138 bool addNdefTech_; 139 /* IsoDep Felica ISO15693... */ 140 std::vector<int> technologyList_ {}; 141 /* NDEF */ 142 static const int NDEF_INFO_SIZE = 2; // includes size + mode; 143 static const int NDEF_SIZE_INDEX = 0; 144 static const int NDEF_MODE_INDEX = 1; 145 AppExecFwk::PacMap ndefExtras_; 146 }; 147 } // namespace NCI 148 } // namespace NFC 149 } // namespace OHOS 150 #endif // TAG_HOST_H 151