1 /*
2 * Copyright 2022 NXP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "NfcDta.h"
18
19 #include <android-base/logging.h>
20 #include <android-base/stringprintf.h>
21 #include <cutils/properties.h>
22
23 #include "SyncEvent.h"
24
25 using android::base::StringPrintf;
26
27 namespace android {
28 extern SyncEvent gNfaSetConfigEvent;
29 extern SyncEvent gNfaGetConfigEvent;
30 extern uint16_t gCurrentConfigLen;
31 extern uint8_t gConfig[256];
32 } // namespace android
33
34 using namespace android;
35
36 /*******************************************************************************
37 **
38 ** Function: NfcDta
39 **
40 ** Description: Initialize member variables.
41 **
42 ** Returns: None
43 **
44 *******************************************************************************/
NfcDta()45 NfcDta::NfcDta() {
46 mDefaultTlv.clear();
47 mUpdatedParamIds.clear();
48 }
49
50 /*******************************************************************************
51 **
52 ** Function: getInstance
53 **
54 ** Description: Get a reference to the singleton NfcDta object.
55 **
56 ** Returns: Reference to NfcDta object.
57 **
58 *******************************************************************************/
getInstance()59 NfcDta& NfcDta::getInstance() {
60 static NfcDta dtaInstance;
61 return dtaInstance;
62 }
63
64 /*******************************************************************************
65 **
66 ** Function: parseConfigParams
67 **
68 ** Description: Parse config TLV from the String
69 **
70 ** Returns: uint8_t vector
71 **
72 *******************************************************************************/
parseConfigParams(std::string configParams)73 std::vector<uint8_t> NfcDta::parseConfigParams(std::string configParams) {
74 std::vector<uint8_t> configTlv;
75 uint16_t index = 0;
76 while (index < configParams.size()) {
77 // Parsing tag
78 configTlv.push_back(
79 (uint8_t)strtol(configParams.substr(index, 2).c_str(), NULL, 16));
80 // Parsing length
81 index += 2;
82 uint16_t len =
83 (uint8_t)strtol(configParams.substr(index, 2).c_str(), NULL, 16);
84 configTlv.push_back(len);
85 // Parsing value
86 for (uint16_t i = 0; i < len; i++) {
87 index += 2;
88 configTlv.push_back(
89 (uint8_t)strtol(configParams.substr(index, 2).c_str(), NULL, 16));
90 }
91 index += 2;
92 if (configParams[index] == '_') {
93 index++;
94 }
95 }
96 return configTlv;
97 }
98
99 /*******************************************************************************
100 **
101 ** Function: getNonupdatedConfigParamIds
102 **
103 ** Description: Get config parameter Ids whose values are not modified.
104 **
105 ** Returns: config Parameter ids vector.
106 **
107 *******************************************************************************/
getNonupdatedConfigParamIds(std::vector<uint8_t> configTlv)108 std::vector<uint8_t> NfcDta::getNonupdatedConfigParamIds(
109 std::vector<uint8_t> configTlv) {
110 std::vector<uint8_t> paramIds;
111 uint16_t index = 0;
112 uint8_t len = 0;
113 uint8_t paramId = 0;
114 while (index < configTlv.size()) {
115 paramId = configTlv[index++];
116 std::unordered_set<uint8_t>::const_iterator found =
117 mUpdatedParamIds.find(paramId);
118 if (found == mUpdatedParamIds.end()) {
119 mUpdatedParamIds.insert(paramId);
120 paramIds.push_back(paramId);
121 }
122 len = configTlv[index++];
123 index += len;
124 }
125 return paramIds;
126 }
127
128 /*******************************************************************************
129 **
130 ** Function: getConfigParamValues
131 **
132 ** Description: Read the current config parameter values.
133 **
134 ** Returns: None.
135 **
136 *******************************************************************************/
getConfigParamValues(std::vector<uint8_t> paramIds)137 tNFA_STATUS NfcDta::getConfigParamValues(std::vector<uint8_t> paramIds) {
138 tNFA_STATUS status = NFA_STATUS_OK;
139 if (!paramIds.empty()) {
140 SyncEventGuard guard(gNfaGetConfigEvent);
141 status = NFA_GetConfig(paramIds.size(), paramIds.data());
142 if (status == NFA_STATUS_OK) {
143 gNfaGetConfigEvent.wait();
144 // gCurrentConfigLen contains number of bytes without NCI header length.
145 // i.e., status(one byte) + config tag count(one byte) + NCI config
146 // Tag(one byte) + length(one byte) + value(bytes). So valid getConfig
147 // response length should be greater than 4.
148 if (gCurrentConfigLen > 4) {
149 // Config tlv length = gCurrentConfigLen - status byte - tag count byte
150 uint16_t len = gCurrentConfigLen - 2;
151 // First Config TLV starts from index 1 of gConfig
152 uint16_t index = 1;
153 LOG(DEBUG) << StringPrintf("%s: default_config len: %d", __func__, len);
154 while (index <= len) {
155 mDefaultTlv.push_back(gConfig[index++]);
156 }
157 } else {
158 LOG(DEBUG) << StringPrintf("%s: getConfig failed len: %d", __func__,
159 gCurrentConfigLen);
160 status = NFA_STATUS_FAILED;
161 }
162 } else {
163 LOG(DEBUG) << StringPrintf("%s: getConfig failed", __func__);
164 }
165 }
166 return status;
167 }
168
169 /*******************************************************************************
170 **
171 ** Function: setConfigParams
172 **
173 ** Description: Set config param with new value.
174 **
175 ** Returns: NFA_STATUS_OK if success
176 **
177 *******************************************************************************/
setConfigParams(std::vector<uint8_t> configTlv)178 tNFA_STATUS NfcDta::setConfigParams(std::vector<uint8_t> configTlv) {
179 tNFA_STATUS status = NFA_STATUS_FAILED;
180 uint16_t index = 0;
181 uint8_t paramId = 0;
182 uint8_t len = 0;
183 while (index < configTlv.size()) {
184 paramId = configTlv[index++];
185 len = configTlv[index++];
186 LOG(DEBUG) << StringPrintf("%s: Param Id: %02X, Length: %02X", __func__,
187 paramId, len);
188 SyncEventGuard guard(gNfaSetConfigEvent);
189 status = NFA_SetConfig(paramId, len, &configTlv[index]);
190 if (status == NFA_STATUS_OK) {
191 gNfaSetConfigEvent.wait();
192 } else {
193 LOG(DEBUG) << StringPrintf("%s: setConfig failed", __func__);
194 break;
195 }
196 index += len;
197 }
198 return status;
199 }
200
201 /*******************************************************************************
202 **
203 ** Function: setNfccConfigParams
204 **
205 ** Description: Set NCI config params from nfc.configTLV System Property.
206 **
207 ** Returns: None.
208 **
209 *******************************************************************************/
setNfccConfigParams()210 void NfcDta::setNfccConfigParams() {
211 char sysPropTlvs[256];
212 int len = 0;
213 len = property_get("nfc.dta.configTLV", sysPropTlvs, "");
214 std::string configTlvs(sysPropTlvs);
215 LOG(DEBUG) << StringPrintf("%s: SysProperty nfc.configTLV: %s", __func__,
216 configTlvs.c_str());
217 if (len <= 0 || (configTlvs.empty() && mDefaultTlv.empty())) {
218 LOG(DEBUG) << StringPrintf("%s: Config TLVs not available", __func__);
219 return;
220 }
221 tNFA_STATUS status = NFA_STATUS_FAILED;
222 if (configTlvs.empty() && !mDefaultTlv.empty()) {
223 status = setConfigParams(mDefaultTlv);
224 if (status == NFA_STATUS_OK) {
225 mDefaultTlv.clear();
226 mUpdatedParamIds.clear();
227 LOG(DEBUG) << StringPrintf("%s: Restored default config params",
228 __func__);
229 }
230 return;
231 }
232
233 std::vector<uint8_t> tlv = parseConfigParams(configTlvs);
234 std::vector<uint8_t> paramIds = getNonupdatedConfigParamIds(tlv);
235 status = getConfigParamValues(paramIds);
236 if (status == NFA_STATUS_OK) {
237 setConfigParams(tlv);
238 }
239 LOG(DEBUG) << StringPrintf("%s: Exit", __func__);
240 }
241