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_NCI_ADAPTER_H 16 #define TAG_NCI_ADAPTER_H 17 18 #include <memory> 19 #include <mutex> 20 #include <vector> 21 22 #include "infc_nci.h" 23 #include "nfa_api.h" 24 #include "nfa_rw_api.h" 25 #include "synchronize_event.h" 26 #include "tag_host.h" 27 28 namespace OHOS { 29 namespace NFC { 30 namespace NCI { 31 class TagNciAdapter final { 32 public: 33 static TagNciAdapter& GetInstance(); 34 ~TagNciAdapter(); 35 void SetNciAdaptations(std::shared_ptr<INfcNci> nciAdaptations); 36 static void HandleSelectResult(); 37 static void HandleTranceiveData(unsigned char status, unsigned char* data, int dataLen); 38 static void HandleReadComplete(unsigned char status); 39 static void HandleWriteComplete(unsigned char status); 40 static void HandleFormatComplete(unsigned char status); 41 static void HandleNdefCheckResult(unsigned char status, int currentSize, uint32_t flag, int maxSize); 42 static void HandleActivatedResult(); 43 static void HandleDeactivatedResult(); 44 static void HandleFieldCheckResult(unsigned char status); 45 static bool IsReconnecting(); 46 void HandleDiscResult(tNFA_CONN_EVT_DATA* eventData); 47 48 // tag connection and read or write. 49 void BuildTagInfo(const tNFA_CONN_EVT_DATA* eventData); 50 tNFA_STATUS Connect(int discId, int protocol, int tech); 51 bool Disconnect(); 52 bool Reconnect(int discId, int protocol, int tech, bool restart); 53 bool NfaDeactivateAndSelect(int discId, int protocol); 54 int Transceive(std::string& request, std::string& response); 55 int GetTimeout(int technology) const; 56 void ResetTimeout(); 57 void ResetTag(); 58 59 // functions for ndef tag only. 60 void RegisterNdefHandler(); 61 void ReadNdef(std::string& response); 62 bool WriteNdef(std::string& ndefMessage); 63 bool IsNdefFormatable(); 64 bool FormatNdef(); 65 bool SetReadOnly() const; 66 bool IsNdefMsgContained(std::vector<int>& ndefInfo); 67 bool IsNdefFormattable(); 68 69 // functions for checking the tag field on or not. 70 bool IsTagFieldOn(); 71 void ResetTagFieldOnFlag(); 72 void OnRfDiscLock(); 73 void OffRfDiscLock(); 74 75 static void AbortWait(); 76 77 // functions for multiple protocol tag 78 void SetIsMultiTag(bool isMultiTag); 79 bool GetIsMultiTag() const; 80 void SetDiscRstEvtNum(uint32_t num); 81 uint32_t GetDiscRstEvtNum() const; 82 void GetMultiTagTechsFromData(const tNFA_DISC_RESULT& discoveryData); 83 void SelectTheFirstTag(); 84 void SelectTheNextTag(); 85 86 private: 87 TagNciAdapter(); 88 int GetT1tMaxMessageSize(tNFA_ACTIVATED activated) const; 89 std::string GetUidFromData(tNFA_ACTIVATED activated) const; 90 tNFA_INTF_TYPE GetRfInterface(int protocol) const; 91 bool IsTagActive() const; 92 bool IsDiscTypeA(char discType) const; 93 bool IsDiscTypeB(char discType) const; 94 bool IsDiscTypeF(char discType) const; 95 bool IsDiscTypeV(char discType) const; 96 97 std::string GetTechPollForTypeB(tNFC_RF_TECH_PARAMS nfcRfTechParams, int tech); 98 std::string GetTechActForIsoDep(tNFA_ACTIVATED activated, tNFC_RF_TECH_PARAMS nfcRfTechParams, int tech) const; 99 void GetTechFromData(tNFA_ACTIVATED activated); 100 void GetTechPollFromData(tNFA_ACTIVATED activated); 101 void GetTechActFromData(tNFA_ACTIVATED activated); 102 void ParseSpecTagType(tNFA_ACTIVATED activated); 103 static void NdefCallback(unsigned char event, tNFA_NDEF_EVT_DATA* eventData); 104 105 bool Reselect(tNFA_INTF_TYPE rfInterface); 106 bool SendReselectReqIfNeed(int protocol, int tech); 107 tNFA_STATUS DoSelectForMultiTag(int currIdx); 108 109 // synchronized lock 110 static std::mutex rfDiscoveryMutex_; 111 static OHOS::NFC::SynchronizeEvent transceiveEvent_; 112 static OHOS::NFC::SynchronizeEvent filedCheckEvent_; 113 static OHOS::NFC::SynchronizeEvent readNdefEvent_; 114 static OHOS::NFC::SynchronizeEvent writeNdefEvent_; 115 static OHOS::NFC::SynchronizeEvent formatNdefEvent_; 116 static OHOS::NFC::SynchronizeEvent checkNdefEvent_; 117 static OHOS::NFC::SynchronizeEvent selectEvent_; 118 static OHOS::NFC::SynchronizeEvent activatedEvent_; 119 static OHOS::NFC::SynchronizeEvent deactivatedEvent_; 120 121 static bool isTagFieldOn_; 122 static bool isReconnecting_; 123 static bool isInTransceive_; 124 static int t1tMaxMessageSize_; 125 static std::string receivedData_; 126 127 // const values for Mifare Ultralight 128 static const int MANUFACTURER_ID_NXP = 0x04; 129 static const int SAK_MIFARE_UL_1 = 0x00; 130 static const int SAK_MIFARE_UL_2 = 0x04; 131 static const int ATQA_MIFARE_UL_0 = 0x44; 132 static const int ATQA_MIFARE_UL_1 = 0x00; 133 134 // const values for Mifare DESFire 135 static const int SAK_MIFARE_DESFIRE = 0x20; 136 static const int ATQA_MIFARE_DESFIRE_0 = 0x44; 137 static const int ATQA_MIFARE_DESFIRE_1 = 0x03; 138 139 // tag technology and protocols discovery. 140 static const uint32_t MAX_NUM_TECHNOLOGY = 12; 141 int technologyTimeoutsTable_[MAX_NUM_TECHNOLOGY + 1] {}; // index equals to the technology value. 142 143 static std::shared_ptr<INfcNci> nciAdaptations_; 144 std::vector<int> tagTechList_ {}; // tag type 145 std::vector<int> tagRfDiscIdList_ {}; // disc id 146 std::vector<int> tagActivatedProtocols_ {}; // protocol 147 std::vector<std::string> tagPollBytes_ {}; 148 std::vector<std::string> tagActivatedBytes_ {}; 149 uint32_t techListIndex_; // current tech list index 150 std::vector<int> tagDiscIdListOfDiscResult_ {}; // disc id 151 std::vector<int> tagProtocolsOfDiscResult_ {}; // protocol 152 int tagActivatedProtocol_; 153 static int connectedProtocol_; 154 static int connectedTargetType_; 155 static int connectedTagDiscId_; 156 157 // spec tag type 158 bool isFelicaLite_; 159 bool isMifareUltralight_; 160 bool isMifareDESFire_; 161 tNFA_RW_PRES_CHK_OPTION presChkOption_; 162 163 // ndef checked status. 164 static int lastNdefCheckedStatus_; 165 static bool isNdefCapable_; 166 static int lastCheckedNdefSize_; 167 static int lastCheckedNdefMaxSize_; 168 static int lastCheckedNdefMode_; 169 static bool isNdefWriteSuccess_; 170 static bool isNdefFormatSuccess_; 171 static unsigned short int ndefTypeHandle_; 172 static std::string readNdefData; 173 174 // multiple protocol tag values 175 bool isMultiTag_; 176 uint32_t discRstEvtNum_; // number of tag, increased with the times of NFA_DISC_RESULT_EVT 177 // and decreased while selecting next tag 178 uint32_t discNtfIndex_; 179 uint32_t multiTagTmpTechIdx_; // to store the last techlist index for the last tag 180 unsigned int selectedTagIdx_; // to store the last selected tag index 181 int multiTagDiscId_[MAX_NUM_TECHNOLOGY] {}; 182 int multiTagDiscProtocol_[MAX_NUM_TECHNOLOGY] {}; 183 }; 184 } // namespace NCI 185 } // namespace NFC 186 } // namespace OHOS 187 #endif // TAG_NCI_ADAPTER_H 188