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 void SetNciAdaptations(std::shared_ptr<INfcNci> nciAdaptations); 35 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 ~TagNciAdapter(); 89 int GetT1tMaxMessageSize(tNFA_ACTIVATED activated) const; 90 std::string GetUidFromData(tNFA_ACTIVATED activated) const; 91 tNFA_INTF_TYPE GetRfInterface(int protocol) const; 92 bool IsTagActive() const; 93 bool IsDiscTypeA(char discType) const; 94 bool IsDiscTypeB(char discType) const; 95 bool IsDiscTypeF(char discType) const; 96 bool IsDiscTypeV(char discType) const; 97 98 std::string GetTechPollForTypeB(tNFC_RF_TECH_PARAMS nfcRfTechParams, int tech); 99 std::string GetTechActForIsoDep(tNFA_ACTIVATED activated, tNFC_RF_TECH_PARAMS nfcRfTechParams, int tech) const; 100 void GetTechFromData(tNFA_ACTIVATED activated); 101 void GetTechPollFromData(tNFA_ACTIVATED activated); 102 void GetTechActFromData(tNFA_ACTIVATED activated); 103 void ParseSpecTagType(tNFA_ACTIVATED activated); 104 static void NdefCallback(unsigned char event, tNFA_NDEF_EVT_DATA* eventData); 105 106 bool Reselect(tNFA_INTF_TYPE rfInterface); 107 bool SendReselectReqIfNeed(int protocol, int tech); 108 tNFA_STATUS DoSelectForMultiTag(int currIdx); 109 110 // synchronized lock 111 static std::mutex rfDiscoveryMutex_; 112 static OHOS::NFC::SynchronizeEvent transceiveEvent_; 113 static OHOS::NFC::SynchronizeEvent filedCheckEvent_; 114 static OHOS::NFC::SynchronizeEvent readNdefEvent_; 115 static OHOS::NFC::SynchronizeEvent writeNdefEvent_; 116 static OHOS::NFC::SynchronizeEvent formatNdefEvent_; 117 static OHOS::NFC::SynchronizeEvent checkNdefEvent_; 118 static OHOS::NFC::SynchronizeEvent selectEvent_; 119 static OHOS::NFC::SynchronizeEvent activatedEvent_; 120 static OHOS::NFC::SynchronizeEvent deactivatedEvent_; 121 122 static bool isTagFieldOn_; 123 static bool isReconnecting_; 124 static bool isInTransceive_; 125 static int t1tMaxMessageSize_; 126 static std::string receivedData_; 127 128 // const values for Mifare Ultralight 129 static const int MANUFACTURER_ID_NXP = 0x04; 130 static const int SAK_MIFARE_UL_1 = 0x00; 131 static const int SAK_MIFARE_UL_2 = 0x04; 132 static const int ATQA_MIFARE_UL_0 = 0x44; 133 static const int ATQA_MIFARE_UL_1 = 0x00; 134 135 // const values for Mifare DESFire 136 static const int SAK_MIFARE_DESFIRE = 0x20; 137 static const int ATQA_MIFARE_DESFIRE_0 = 0x44; 138 static const int ATQA_MIFARE_DESFIRE_1 = 0x03; 139 140 // tag technology and protocols discovery. 141 static const uint32_t MAX_NUM_TECHNOLOGY = 12; 142 int technologyTimeoutsTable_[MAX_NUM_TECHNOLOGY + 1] {}; // index equals to the technology value. 143 144 static std::shared_ptr<INfcNci> nciAdaptations_; 145 std::vector<int> tagTechList_ {}; // tag type 146 std::vector<int> tagRfDiscIdList_ {}; // disc id 147 std::vector<int> tagActivatedProtocols_ {}; // protocol 148 std::vector<std::string> tagPollBytes_ {}; 149 std::vector<std::string> tagActivatedBytes_ {}; 150 uint32_t techListIndex_; // current tech list index 151 std::vector<int> tagDiscIdListOfDiscResult_ {}; // disc id 152 std::vector<int> tagProtocolsOfDiscResult_ {}; // protocol 153 int tagActivatedProtocol_; 154 static int connectedProtocol_; 155 static int connectedTargetType_; 156 static int connectedTagDiscId_; 157 158 // spec tag type 159 bool isFelicaLite_; 160 bool isMifareUltralight_; 161 bool isMifareDESFire_; 162 tNFA_RW_PRES_CHK_OPTION presChkOption_; 163 164 // ndef checked status. 165 static int lastNdefCheckedStatus_; 166 static bool isNdefCapable_; 167 static int lastCheckedNdefSize_; 168 static int lastCheckedNdefMaxSize_; 169 static int lastCheckedNdefMode_; 170 static bool isNdefWriteSuccess_; 171 static bool isNdefFormatSuccess_; 172 static unsigned short int ndefTypeHandle_; 173 static std::string readNdefData; 174 175 // multiple protocol tag values 176 bool isMultiTag_; 177 uint32_t discRstEvtNum_; // number of tag, increased with the times of NFA_DISC_RESULT_EVT 178 // and decreased while selecting next tag 179 uint32_t discNtfIndex_; 180 uint32_t multiTagTmpTechIdx_; // to store the last techlist index for the last tag 181 unsigned int selectedTagIdx_; // to store the last selected tag index 182 int multiTagDiscId_[MAX_NUM_TECHNOLOGY] {}; 183 int multiTagDiscProtocol_[MAX_NUM_TECHNOLOGY] {}; 184 }; 185 } // namespace NCI 186 } // namespace NFC 187 } // namespace OHOS 188 #endif // TAG_NCI_ADAPTER_H 189