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