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