blob: ff79f0bae27d292264053a8041c6cab089588c4f [file] [log] [blame]
nxpandroid64fd68c2015-09-23 16:45:15 +05301/******************************************************************************
nxf24591dc0bc2c2018-02-21 17:33:08 +05302*
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* Copyright 2018 NXP
16*
17******************************************************************************/
18
19#include "SecureElement.h"
20#include <nativehelper/ScopedLocalRef.h>
21#include "JavaClassConstants.h"
22#include "NfcJniUtil.h"
23#include <android-base/stringprintf.h>
24#include <base/logging.h>
nxpandroid64fd68c2015-09-23 16:45:15 +053025#include <semaphore.h>
26#include <errno.h>
nxpandroid64fd68c2015-09-23 16:45:15 +053027#include "config.h"
nxpandroid64fd68c2015-09-23 16:45:15 +053028#include "phNxpConfig.h"
nxf24591dc0bc2c2018-02-21 17:33:08 +053029#include "nfc_config.h"
nxpandroid64fd68c2015-09-23 16:45:15 +053030#include "RoutingManager.h"
nxf24591dc0bc2c2018-02-21 17:33:08 +053031using android::base::StringPrintf;
nxpandroid64fd68c2015-09-23 16:45:15 +053032
33SecureElement SecureElement::sSecElem;
34const char* SecureElement::APP_NAME = "nfc_jni";
nxpandroidecca0112018-06-06 14:41:40 +053035extern bool nfc_debug_enabled;
nxpandroid64fd68c2015-09-23 16:45:15 +053036
nxpandroidecca0112018-06-06 14:41:40 +053037#include "MposManager.h"
nxf24591dc0bc2c2018-02-21 17:33:08 +053038namespace android
39{
40extern void startRfDiscovery (bool isStart);
41}
42uint8_t SecureElement::mStaticPipeProp;
nxpandroid64fd68c2015-09-23 16:45:15 +053043/*******************************************************************************
44**
45** Function: SecureElement
46**
47** Description: Initialize member variables.
48**
49** Returns: None
50**
51*******************************************************************************/
nxf24591dc0bc2c2018-02-21 17:33:08 +053052SecureElement::SecureElement() :
nxpandroid64fd68c2015-09-23 16:45:15 +053053 mNewPipeId (0),
nxf24591dc0bc2c2018-02-21 17:33:08 +053054 mNativeData(NULL),
55 mbNewEE (true),
56 mIsInit (false),
nxpandroidecca0112018-06-06 14:41:40 +053057 mNewSourceGate (0),
58 mRfFieldIsOn(false),
59 mActivatedInListenMode (false)
nxpandroid64fd68c2015-09-23 16:45:15 +053060{
nxpandroiddb4a2042017-08-14 11:27:34 +053061 memset (&mEeInfo, 0, nfcFL.nfccFL._NFA_EE_MAX_EE_SUPPORTED *sizeof(tNFA_EE_INFO));
nxpandroid64fd68c2015-09-23 16:45:15 +053062 memset (mAidForEmptySelect, 0, sizeof(mAidForEmptySelect));
nxf24591dc0bc2c2018-02-21 17:33:08 +053063 memset (&mHciCfg, 0, sizeof(mHciCfg));
nxpandroidecca0112018-06-06 14:41:40 +053064 memset (&mLastRfFieldToggle, 0, sizeof(mLastRfFieldToggle));
nxpandroid64fd68c2015-09-23 16:45:15 +053065}
nxpandroid64fd68c2015-09-23 16:45:15 +053066/*******************************************************************************
67**
68** Function: getInstance
69**
70** Description: Get the SecureElement singleton object.
71**
72** Returns: SecureElement object.
73**
74*******************************************************************************/
nxf24591dc0bc2c2018-02-21 17:33:08 +053075SecureElement& SecureElement::getInstance() { return sSecElem; }
nxpandroid64fd68c2015-09-23 16:45:15 +053076
77/*******************************************************************************
78**
79** Function: initialize
80**
81** Description: Initialize all member variables.
82** native: Native data.
83**
84** Returns: True if ok.
85**
86*******************************************************************************/
nxf24591dc0bc2c2018-02-21 17:33:08 +053087bool SecureElement::initialize(nfc_jni_native_data* native) {
nxpandroid64fd68c2015-09-23 16:45:15 +053088 static const char fn [] = "SecureElement::initialize";
89 tNFA_STATUS nfaStat;
nxpandroid64fd68c2015-09-23 16:45:15 +053090
nxf24591dc0bc2c2018-02-21 17:33:08 +053091 LOG(INFO) << StringPrintf("%s: enter", fn);
nxpandroid64fd68c2015-09-23 16:45:15 +053092
nxpandroid64fd68c2015-09-23 16:45:15 +053093
94 mActiveEeHandle = NFA_HANDLE_INVALID;
95 mNfaHciHandle = NFA_HANDLE_INVALID;
96
97 mNativeData = native;
98 mthreadnative = native;
nxf24591dc0bc2c2018-02-21 17:33:08 +053099 mActualNumEe = nfcFL.nfccFL._NFA_EE_MAX_EE_SUPPORTED;
nxpandroid64fd68c2015-09-23 16:45:15 +0530100 mbNewEE = true;
101 mNewPipeId = 0;
102 mNewSourceGate = 0;
Sreenivas426138e2018-04-04 14:56:08 +0530103 unsigned long val = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +0530104 memset (mEeInfo, 0, sizeof(mEeInfo));
nxpandroid64fd68c2015-09-23 16:45:15 +0530105 memset (&mHciCfg, 0, sizeof(mHciCfg));
nxpandroid64fd68c2015-09-23 16:45:15 +0530106 memset(mAidForEmptySelect, 0, sizeof(mAidForEmptySelect));
nxf24591dc0bc2c2018-02-21 17:33:08 +0530107 if (GetNxpNumValue(NAME_NXP_DEFAULT_UICC2_SELECT, &muicc2_selected, sizeof(muicc2_selected)) == false)
nxpandroid64fd68c2015-09-23 16:45:15 +0530108 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530109 muicc2_selected = UICC2_ID;
nxpandroid64fd68c2015-09-23 16:45:15 +0530110 }
Sreenivas426138e2018-04-04 14:56:08 +0530111 if (GetNxpNumValue(NAME_NXP_SMB_TRANSCEIVE_TIMEOUT, &val, sizeof(val)) == true)
112 {
113 SmbTransceiveTimeOutVal = val;
114 }
115 else
116 {
117 SmbTransceiveTimeOutVal = WIRED_MODE_TRANSCEIVE_TIMEOUT;
118 }
119 LOG(INFO) << StringPrintf("%s: SMB transceive timeout %d", fn, SmbTransceiveTimeOutVal);
nxf24591dc0bc2c2018-02-21 17:33:08 +0530120 initializeEeHandle();
nxpandroid64fd68c2015-09-23 16:45:15 +0530121
122 // Get Fresh EE info.
123 if (! getEeInfo())
124 return (false);
nxf24591dc0bc2c2018-02-21 17:33:08 +0530125
nxpandroid64fd68c2015-09-23 16:45:15 +0530126 // If the controller has an HCI Network, register for that
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530127 //for (size_t xx = 0; xx < mActualNumEe; xx++)
128 for (size_t xx = 0; xx < MAX_NUM_EE; xx++)
nxpandroid64fd68c2015-09-23 16:45:15 +0530129 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530130
nxf24591dc0bc2c2018-02-21 17:33:08 +0530131 if((mEeInfo[xx].ee_handle != EE_HANDLE_0xF4)
132 || ((((mEeInfo[xx].ee_interface[0] == NCI_NFCEE_INTERFACE_HCI_ACCESS)
Yerriswamy70bfbcd2017-09-27 22:38:42 +0530133 && (mEeInfo[xx].ee_status == NFC_NFCEE_STATUS_ACTIVE)) || (NFA_GetNCIVersion() == NCI_VERSION_2_0))))
nxpandroid64fd68c2015-09-23 16:45:15 +0530134 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530135 LOG(INFO) << StringPrintf("%s: Found HCI network, try hci register", fn);
nxpandroideb52db42017-08-14 11:29:32 +0530136
137 SyncEventGuard guard (mHciRegisterEvent);
138
139 nfaStat = NFA_HciRegister (const_cast<char*>(APP_NAME), nfaHciCallback, true);
140 if (nfaStat != NFA_STATUS_OK)
141 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530142 LOG(ERROR) << StringPrintf("%s: fail hci register; error=0x%X", fn, nfaStat);
nxpandroideb52db42017-08-14 11:29:32 +0530143 return (false);
144 }
145 mHciRegisterEvent.wait();
146 break;
nxpandroid64fd68c2015-09-23 16:45:15 +0530147 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530148 }
nxf24591dc0bc2c2018-02-21 17:33:08 +0530149
nxpandroid64fd68c2015-09-23 16:45:15 +0530150 mIsInit = true;
nxf24591dc0bc2c2018-02-21 17:33:08 +0530151 LOG(INFO) << StringPrintf("%s: exit", fn);
nxpandroid34627bd2016-05-27 15:52:30 +0530152 return (true);
153}
nxpandroid7d44e572016-08-01 19:11:04 +0530154
155/*******************************************************************************
nxpandroid64fd68c2015-09-23 16:45:15 +0530156**
nxf24591dc0bc2c2018-02-21 17:33:08 +0530157** Function: getGenericEseId
nxpandroid64fd68c2015-09-23 16:45:15 +0530158**
nxf24591dc0bc2c2018-02-21 17:33:08 +0530159** Description: Whether controller is routing listen-mode events to
160** secure elements or a pipe is connected.
nxpandroid64fd68c2015-09-23 16:45:15 +0530161**
nxf24591dc0bc2c2018-02-21 17:33:08 +0530162** Returns: Return the generic SE id ex:- 00,01,02,04
nxpandroid64fd68c2015-09-23 16:45:15 +0530163**
164*******************************************************************************/
nxf24591dc0bc2c2018-02-21 17:33:08 +0530165jint SecureElement::getGenericEseId(tNFA_HANDLE handle) {
166 jint ret = 0xFF;
167 static const char fn [] = "SecureElement::getGenericEseId";
168 LOG(INFO) << StringPrintf("%s: enter; ESE-Handle = 0x%X", fn, handle);
nxpandroid64fd68c2015-09-23 16:45:15 +0530169
nxf24591dc0bc2c2018-02-21 17:33:08 +0530170 //Map the actual handle to generic id
171 if(handle == (EE_HANDLE_0xF3 & ~NFA_HANDLE_GROUP_EE) ) //ESE - 0xC0
172 {
173 ret = ESE_ID;
nxpandroid0f06fde2017-08-14 11:25:28 +0530174 }
nxf24591dc0bc2c2018-02-21 17:33:08 +0530175 else if(handle == (SecureElement::getInstance().EE_HANDLE_0xF4 & ~NFA_HANDLE_GROUP_EE) ) //UICC - 0x02
176 {
177 ret = UICC_ID;
178 }
179 if(handle == (EE_HANDLE_0xF8 & ~NFA_HANDLE_GROUP_EE)) //UICC2 - 0x04
180 {
181 ret = UICC2_ID;
182 }
183 else if (handle == (EE_HANDLE_0xF9 & ~NFA_HANDLE_GROUP_EE)) //UICC2 - 0x04
184 {
185 ret = UICC3_ID;
186 }
187 LOG(INFO) << StringPrintf("%s: exit; ESE-Generic-ID = 0x%02X", fn, ret);
188 return ret;
nxpandroid64fd68c2015-09-23 16:45:15 +0530189}
nxpandroidecca0112018-06-06 14:41:40 +0530190/*******************************************************************************
191**
192** Function TimeDiff
193**
194** Description Computes time difference in milliseconds.
195**
196** Returns Time difference in milliseconds
197**
198*******************************************************************************/
199static uint32_t TimeDiff(timespec start, timespec end)
200{
201 end.tv_sec -= start.tv_sec;
202 end.tv_nsec -= start.tv_nsec;
nxpandroid64fd68c2015-09-23 16:45:15 +0530203
nxpandroidecca0112018-06-06 14:41:40 +0530204 if (end.tv_nsec < 0) {
205 end.tv_nsec += 10e8;
206 end.tv_sec -=1;
207 }
208
209 return (end.tv_sec * 1000) + (end.tv_nsec / 10e5);
210}
211/*******************************************************************************
212**
213** Function: isRfFieldOn
214**
215** Description: Can be used to determine if the SE is in an RF field
216**
217** Returns: True if the SE is activated in an RF field
218**
219*******************************************************************************/
220bool SecureElement::isRfFieldOn() {
221 AutoMutex mutex(mMutex);
222 if (mRfFieldIsOn) {
223 return true;
224 }
225 struct timespec now;
226 int ret = clock_gettime(CLOCK_MONOTONIC, &now);
227 if (ret == -1) {
228 DLOG_IF(ERROR, nfc_debug_enabled)
229 << StringPrintf("isRfFieldOn(): clock_gettime failed");
230 return false;
231 }
232 if (TimeDiff(mLastRfFieldToggle, now) < 50) {
233 // If it was less than 50ms ago that RF field
234 // was turned off, still return ON.
235 return true;
236 } else {
237 return false;
238 }
239}
nxpandroid64fd68c2015-09-23 16:45:15 +0530240/*******************************************************************************
241**
Suraj Uday Kotharkar2a0ee9c2018-04-27 03:48:23 +0530242** Function: notifyListenModeState
243**
244** Description: Notify the NFC service about whether the SE was activated
245** in listen mode.
246** isActive: Whether the secure element is activated.
247**
248** Returns: None
249**
250*******************************************************************************/
251void SecureElement::notifyListenModeState (bool isActivated) {
252 static const char fn [] = "SecureElement::notifyListenMode";
253
254 DLOG_IF(INFO, nfc_debug_enabled)
255 << StringPrintf("%s: enter; listen mode active=%u", fn, isActivated);
256
257 JNIEnv* e = NULL;
258 if (mNativeData == NULL)
259 {
260 DLOG_IF(ERROR, nfc_debug_enabled)
261 << StringPrintf("%s: mNativeData is null", fn);
262 return;
263 }
264
265 ScopedAttach attach(mNativeData->vm, &e);
266 if (e == NULL)
267 {
268 DLOG_IF(ERROR, nfc_debug_enabled)
269 << StringPrintf("%s: jni env is null", fn);
270 return;
271 }
272
273 mActivatedInListenMode = isActivated;
274
275 if (mNativeData != NULL) {
276 if (isActivated) {
277 e->CallVoidMethod (mNativeData->manager, android::gCachedNfcManagerNotifySeListenActivated);
278 }
279 else {
280 e->CallVoidMethod (mNativeData->manager, android::gCachedNfcManagerNotifySeListenDeactivated);
281 }
282 }
283
284 if (e->ExceptionCheck())
285 {
286 e->ExceptionClear();
287 DLOG_IF(ERROR, nfc_debug_enabled)
288 << StringPrintf("%s: fail notify", fn);
289 }
290
291 DLOG_IF(INFO, nfc_debug_enabled)
292 << StringPrintf("%s: exit", fn);
293}
294/*******************************************************************************
295**
nxpandroid64fd68c2015-09-23 16:45:15 +0530296** Function: notifyTransactionListenersOfAid
297**
nxf24591dc0bc2c2018-02-21 17:33:08 +0530298** Description: Notify the NFC service about a transaction event from
299** secure element.
nxpandroid64fd68c2015-09-23 16:45:15 +0530300** aid: Buffer contains application ID.
301** aidLen: Length of application ID.
302**
303** Returns: None
304**
305*******************************************************************************/
nxf24591dc0bc2c2018-02-21 17:33:08 +0530306void SecureElement::notifyTransactionListenersOfAid(const uint8_t* aidBuffer,
307 uint8_t aidBufferLen,
308 const uint8_t* dataBuffer,
309 uint32_t dataBufferLen,
310 uint32_t evtSrc) {
nxpandroid64fd68c2015-09-23 16:45:15 +0530311{
nxf24591dc0bc2c2018-02-21 17:33:08 +0530312 static const char fn [] = "SecureElement::notifyTransactionListenersOfAid";
nxpandroid64fd68c2015-09-23 16:45:15 +0530313
nxf24591dc0bc2c2018-02-21 17:33:08 +0530314 if (aidBufferLen == 0) {
nxpandroid64fd68c2015-09-23 16:45:15 +0530315 return;
316 }
317
nxf24591dc0bc2c2018-02-21 17:33:08 +0530318 JNIEnv* e = NULL;
319 ScopedAttach attach(mNativeData->vm, &e);
320 if (e == NULL)
nxpandroid64fd68c2015-09-23 16:45:15 +0530321 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530322 LOG(ERROR) << StringPrintf("%s: jni env is null", fn);
323 return;
nxpandroid64fd68c2015-09-23 16:45:15 +0530324 }
325
nxf24591dc0bc2c2018-02-21 17:33:08 +0530326 const uint16_t tlvMaxLen = aidBufferLen + 10;
327 uint8_t* tlv = new uint8_t [tlvMaxLen];
328 if (tlv == NULL)
nxpandroid64fd68c2015-09-23 16:45:15 +0530329 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530330 LOG(ERROR) << StringPrintf("%s: fail allocate tlv", fn);
331 return;
nxpandroid64fd68c2015-09-23 16:45:15 +0530332 }
333
nxf24591dc0bc2c2018-02-21 17:33:08 +0530334 memcpy (tlv, aidBuffer, aidBufferLen);
335 uint16_t tlvActualLen = aidBufferLen;
nxpandroid64fd68c2015-09-23 16:45:15 +0530336
nxf24591dc0bc2c2018-02-21 17:33:08 +0530337 ScopedLocalRef<jobject> tlvJavaArray(e, e->NewByteArray(tlvActualLen));
338 if (tlvJavaArray.get() == NULL)
nxpandroid64fd68c2015-09-23 16:45:15 +0530339 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530340 LOG(ERROR) << StringPrintf("%s: fail allocate array", fn);
nxpandroid64fd68c2015-09-23 16:45:15 +0530341 goto TheEnd;
342 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530343
nxf24591dc0bc2c2018-02-21 17:33:08 +0530344 e->SetByteArrayRegion ((jbyteArray)tlvJavaArray.get(), 0, tlvActualLen, (jbyte *)tlv);
345 if (e->ExceptionCheck())
nxpandroid64fd68c2015-09-23 16:45:15 +0530346 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530347 e->ExceptionClear();
348 LOG(ERROR) << StringPrintf("%s: fail fill array", fn);
349 goto TheEnd;
nxpandroid64fd68c2015-09-23 16:45:15 +0530350 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530351
nxf24591dc0bc2c2018-02-21 17:33:08 +0530352 if(dataBufferLen > 0)
nxpandroid7d44e572016-08-01 19:11:04 +0530353 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530354 const uint32_t dataTlvMaxLen = dataBufferLen + 10;
355 uint8_t* datatlv = new uint8_t [dataTlvMaxLen];
356 if (datatlv == NULL)
nxpandroid7d44e572016-08-01 19:11:04 +0530357 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530358 LOG(ERROR) << StringPrintf("%s: fail allocate tlv", fn);
359 return;
360 }
361
362 memcpy (datatlv, dataBuffer, dataBufferLen);
363 uint16_t dataTlvActualLen = dataBufferLen;
364
365 ScopedLocalRef<jobject> dataTlvJavaArray(e, e->NewByteArray(dataTlvActualLen));
366 if (dataTlvJavaArray.get() == NULL)
367 {
368 LOG(ERROR) << StringPrintf("%s: fail allocate array", fn);
369 goto Clean;
370 }
371
372 e->SetByteArrayRegion ((jbyteArray)dataTlvJavaArray.get(), 0, dataTlvActualLen, (jbyte *)datatlv);
373 if (e->ExceptionCheck())
374 {
375 e->ExceptionClear();
376 LOG(ERROR) << StringPrintf("%s: fail fill array", fn);
377 goto Clean;
378 }
379
380 e->CallVoidMethod (mNativeData->manager, android::gCachedNfcManagerNotifyTransactionListeners, tlvJavaArray.get(), dataTlvJavaArray.get(), evtSrc);
381 if (e->ExceptionCheck())
382 {
383 e->ExceptionClear();
384 LOG(ERROR) << StringPrintf("%s: fail notify", fn);
385 goto Clean;
386 }
387
388 Clean:
389 delete [] datatlv;
390 }
391 else
392 {
393 e->CallVoidMethod (mNativeData->manager, android::gCachedNfcManagerNotifyTransactionListeners, tlvJavaArray.get(), NULL, evtSrc);
394 if (e->ExceptionCheck())
395 {
396 e->ExceptionClear();
397 LOG(ERROR) << StringPrintf("%s: fail notify", fn);
nxpandroid7d44e572016-08-01 19:11:04 +0530398 goto TheEnd;
399 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530400 }
nxf24591dc0bc2c2018-02-21 17:33:08 +0530401 TheEnd:
402 delete [] tlv;
403 LOG(INFO) << StringPrintf("%s: exit", fn);
nxpandroid64fd68c2015-09-23 16:45:15 +0530404 }
nxf24591dc0bc2c2018-02-21 17:33:08 +0530405}
406
407/*******************************************************************************
408**
409** Function: decodeBerTlvLength
410**
411** Description: Decodes BER TLV length from the data provided
412** data : array of data to be processed
413** index : offset from which to consider processing
414** data_length : length of data to be processed
415**
416** Returns: decoded_length
417**
418*******************************************************************************/
419int SecureElement::decodeBerTlvLength(uint8_t* data, int index,
420 int data_length) {
421 int decoded_length = -1;
422 int length = 0;
423 int temp = data[index] & 0xff;
424
425 LOG(INFO) << StringPrintf("decodeBerTlvLength index= %d data[index+0]=0x%x data[index+1]=0x%x len=%d",index, data[index], data[index+1], data_length);
426
427 if (temp < 0x80) {
428 decoded_length = temp;
429 } else if (temp == 0x81) {
430 if( index < data_length ) {
431 length = data[index+1] & 0xff;
432 if (length < 0x80) {
433 LOG(ERROR) << StringPrintf("Invalid TLV length encoding!");
434 goto TheEnd;
435 }
436 if (data_length < length + index) {
437 LOG(ERROR) << StringPrintf("Not enough data provided!");
438 goto TheEnd;
439 }
440 } else {
441 LOG(ERROR) << StringPrintf("Index %d out of range! [0..[%d",index, data_length);
442 goto TheEnd;
443 }
444 decoded_length = length;
445 } else if (temp == 0x82) {
446 if( (index + 1)< data_length ) {
447 length = ((data[index] & 0xff) << 8)
448 | (data[index + 1] & 0xff);
449 } else {
450 LOG(ERROR) << StringPrintf("Index out of range! [0..[%d" , data_length);
451 goto TheEnd;
452 }
453 index += 2;
454 if (length < 0x100) {
455 LOG(ERROR) << StringPrintf("Invalid TLV length encoding!");
456 goto TheEnd;
457 }
458 if (data_length < length + index) {
459 LOG(ERROR) << StringPrintf("Not enough data provided!");
460 goto TheEnd;
461 }
462 decoded_length = length;
463 } else if (temp == 0x83) {
464 if( (index + 2)< data_length ) {
465 length = ((data[index] & 0xff) << 16)
466 | ((data[index + 1] & 0xff) << 8)
467 | (data[index + 2] & 0xff);
468 } else {
469 LOG(ERROR) << StringPrintf("Index out of range! [0..[%d", data_length);
470 goto TheEnd;
471 }
472 index += 3;
473 if (length < 0x10000) {
474 LOG(ERROR) << StringPrintf("Invalid TLV length encoding!");
475 goto TheEnd;
476 }
477 if (data_length < length + index) {
478 LOG(ERROR) << StringPrintf("Not enough data provided!");
479 goto TheEnd;
480 }
481 decoded_length = length;
482 } else {
483 LOG(ERROR) << StringPrintf("Unsupported TLV length encoding!");
484 }
485TheEnd:
486 LOG(INFO) << StringPrintf("decoded_length = %d", decoded_length);
487
488 return decoded_length;
489}
Suraj Uday Kotharkar2a0ee9c2018-04-27 03:48:23 +0530490/*******************************************************************************
491**
492** Function: notifyRfFieldEvent
493**
494** Description: Notify the NFC service about RF field events from the stack.
495** isActive: Whether any secure element is activated.
496**
497** Returns: None
498**
499*******************************************************************************/
500void SecureElement::notifyRfFieldEvent (bool isActive)
501{
502 static const char fn [] = "SecureElement::notifyRfFieldEvent";
503 DLOG_IF(ERROR, nfc_debug_enabled)
504 << StringPrintf("%s: enter; is active=%u", fn, isActive);
nxf24591dc0bc2c2018-02-21 17:33:08 +0530505
Suraj Uday Kotharkar2a0ee9c2018-04-27 03:48:23 +0530506
507 mMutex.lock();
508 int ret = clock_gettime (CLOCK_MONOTONIC, &mLastRfFieldToggle);
509 if (ret == -1) {
510 DLOG_IF(ERROR, nfc_debug_enabled)
511 << StringPrintf("%s: clock_gettime failed", fn);
512 // There is no good choice here...
513 }
514 if (isActive)
515 {
516 mRfFieldIsOn = true;
517 }
518 else
519 {
520 mRfFieldIsOn = false;
521 }
522 mMutex.unlock();
523 DLOG_IF(ERROR, nfc_debug_enabled)
524 << StringPrintf("%s: exit", fn);
525}
nxf24591dc0bc2c2018-02-21 17:33:08 +0530526/*******************************************************************************
527**
528** Function: nfaHciCallback
529**
530** Description: Receive HCI-related events from stack.
531** event: Event code.
532** eventData: Event data.
533**
534** Returns: None
535**
536*******************************************************************************/
537void SecureElement::nfaHciCallback(tNFA_HCI_EVT event,
538 tNFA_HCI_EVT_DATA* eventData) {
539 static const char fn [] = "SecureElement::nfaHciCallback";
540 LOG(INFO) << StringPrintf("%s: event=0x%X", fn, event);
541 int evtSrc = 0xFF;
542
543 switch (event)
nxpandroid64fd68c2015-09-23 16:45:15 +0530544 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530545 case NFA_HCI_REGISTER_EVT:
nxpandroid64fd68c2015-09-23 16:45:15 +0530546 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530547 LOG(INFO) << StringPrintf("%s: NFA_HCI_REGISTER_EVT; status=0x%X; handle=0x%X", fn,
548 eventData->hci_register.status, eventData->hci_register.hci_handle);
549 SyncEventGuard guard (sSecElem.mHciRegisterEvent);
550 sSecElem.mNfaHciHandle = eventData->hci_register.hci_handle;
551 sSecElem.mHciRegisterEvent.notifyOne();
552 }
553 break;
nxpandroid64fd68c2015-09-23 16:45:15 +0530554
nxf24591dc0bc2c2018-02-21 17:33:08 +0530555 case NFA_HCI_ALLOCATE_GATE_EVT:
556 {
557 LOG(INFO) << StringPrintf("%s: NFA_HCI_ALLOCATE_GATE_EVT; status=0x%X; gate=0x%X", fn, eventData->status, eventData->allocated.gate);
558 SyncEventGuard guard (sSecElem.mAllocateGateEvent);
559 sSecElem.mCommandStatus = eventData->status;
560 sSecElem.mNewSourceGate = (eventData->allocated.status == NFA_STATUS_OK) ? eventData->allocated.gate : 0;
561 sSecElem.mAllocateGateEvent.notifyOne();
562 }
563 break;
564
565 case NFA_HCI_DEALLOCATE_GATE_EVT:
566 {
567 tNFA_HCI_DEALLOCATE_GATE& deallocated = eventData->deallocated;
568 LOG(INFO) << StringPrintf("%s: NFA_HCI_DEALLOCATE_GATE_EVT; status=0x%X; gate=0x%X", fn, deallocated.status, deallocated.gate);
569 SyncEventGuard guard (sSecElem.mDeallocateGateEvent);
570 sSecElem.mDeallocateGateEvent.notifyOne();
571 }
572 break;
573
574 case NFA_HCI_GET_GATE_PIPE_LIST_EVT:
575 {
576 LOG(INFO) << StringPrintf("%s: NFA_HCI_GET_GATE_PIPE_LIST_EVT; status=0x%X; num_pipes: %u num_gates: %u", fn,
577 eventData->gates_pipes.status, eventData->gates_pipes.num_pipes, eventData->gates_pipes.num_gates);
578 SyncEventGuard guard (sSecElem.mPipeListEvent);
579 sSecElem.mCommandStatus = eventData->gates_pipes.status;
580 sSecElem.mHciCfg = eventData->gates_pipes;
581 sSecElem.mPipeListEvent.notifyOne();
582 }
583 break;
584
585 case NFA_HCI_CREATE_PIPE_EVT:
586 {
587 LOG(INFO) << StringPrintf("%s: NFA_HCI_CREATE_PIPE_EVT; status=0x%X; pipe=0x%X; src gate=0x%X; dest host=0x%X; dest gate=0x%X", fn,
588 eventData->created.status, eventData->created.pipe, eventData->created.source_gate, eventData->created.dest_host, eventData->created.dest_gate);
589 SyncEventGuard guard (sSecElem.mCreatePipeEvent);
590 sSecElem.mCommandStatus = eventData->created.status;
591 if(eventData->created.dest_gate == 0xF0)
592 {
593 LOG(ERROR) << StringPrintf("Pipe=0x%x is created and updated for se transcieve", eventData->created.pipe);
594 sSecElem.mNewPipeId = eventData->created.pipe;
595 }
596 sSecElem.mCreatedPipe = eventData->created.pipe;
597 LOG(INFO) << StringPrintf("%s: NFA_HCI_CREATE_PIPE_EVT; pipe=0x%X", fn, eventData->created.pipe);
598 sSecElem.mCreatePipeEvent.notifyOne();
599 }
600 break;
601 case NFA_HCI_OPEN_PIPE_EVT:
602 {
603 LOG(INFO) << StringPrintf("%s: NFA_HCI_OPEN_PIPE_EVT; status=0x%X; pipe=0x%X", fn, eventData->opened.status, eventData->opened.pipe);
604 SyncEventGuard guard (sSecElem.mPipeOpenedEvent);
605 sSecElem.mCommandStatus = eventData->opened.status;
606 sSecElem.mPipeOpenedEvent.notifyOne();
607 }
608 break;
609
610 case NFA_HCI_EVENT_SENT_EVT:
611 LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_SENT_EVT; status=0x%X", fn, eventData->evt_sent.status);
612 break;
613
614 case NFA_HCI_RSP_RCVD_EVT: //response received from secure element
615 {
616 tNFA_HCI_RSP_RCVD& rsp_rcvd = eventData->rsp_rcvd;
617 LOG(INFO) << StringPrintf("%s: NFA_HCI_RSP_RCVD_EVT; status: 0x%X; code: 0x%X; pipe: 0x%X; len: %u", fn,
618 rsp_rcvd.status, rsp_rcvd.rsp_code, rsp_rcvd.pipe, rsp_rcvd.rsp_len);
619 }
620 break;
621 case NFA_HCI_RSP_APDU_RCVD_EVT:
622 {
623 if(eventData->apdu_rcvd.apdu_len > 0)
624 {
625 sSecElem.mTransceiveWaitOk = true;
626 sSecElem.mActualResponseSize = (eventData->apdu_rcvd.apdu_len > MAX_RESPONSE_SIZE) ? MAX_RESPONSE_SIZE : eventData->apdu_rcvd.apdu_len;
627 }
628 sSecElem.mTransceiveEvent.notifyOne ();
629 break;
630 }
631 case NFA_HCI_APDU_ABORTED_EVT:
632 {
633 if(eventData->apdu_aborted.atr_len > 0)
634 {
635 sSecElem.mAbortEventWaitOk = true;
636 SyncEventGuard guard(sSecElem.mAbortEvent);
637 memcpy(sSecElem.mAtrInfo, eventData->apdu_aborted.p_atr, eventData->apdu_aborted.atr_len);
638 sSecElem.mAtrInfolen = eventData->apdu_aborted.atr_len;
639 sSecElem.mAtrStatus = eventData->rcvd_evt.status;
640 sSecElem.mAbortEvent.notifyOne();
641 }
642 else
643 {
644 sSecElem.mAbortEventWaitOk = false;
645 SyncEventGuard guard(sSecElem.mAbortEvent);
646 sSecElem.mAbortEvent.notifyOne();
647 }
648 break;
649 }
650 case NFA_HCI_GET_REG_RSP_EVT :
651 LOG(INFO) << StringPrintf("%s: NFA_HCI_GET_REG_RSP_EVT; status: 0x%X; pipe: 0x%X, len: %d", fn,
652 eventData->registry.status, eventData->registry.pipe, eventData->registry.data_len);
653 if(sSecElem.mGetAtrRspwait == true)
654 {
655 /*GetAtr response*/
656 sSecElem.mGetAtrRspwait = false;
657 SyncEventGuard guard (sSecElem.mGetRegisterEvent);
658 memcpy(sSecElem.mAtrInfo, eventData->registry.reg_data, eventData->registry.data_len);
659 sSecElem.mAtrInfolen = eventData->registry.data_len;
660 sSecElem.mAtrStatus = eventData->registry.status;
661 sSecElem.mGetRegisterEvent.notifyOne();
662 }
663 else if (eventData->registry.data_len >= 19 && ((eventData->registry.pipe == mStaticPipeProp) || (eventData->registry.pipe == STATIC_PIPE_0x71)))
664 {
665 SyncEventGuard guard (sSecElem.mVerInfoEvent);
666 // Oberthur OS version is in bytes 16,17, and 18
667 sSecElem.mVerInfo[0] = eventData->registry.reg_data[16];
668 sSecElem.mVerInfo[1] = eventData->registry.reg_data[17];
669 sSecElem.mVerInfo[2] = eventData->registry.reg_data[18];
670 sSecElem.mVerInfoEvent.notifyOne ();
671 }
672 break;
673
674 case NFA_HCI_EVENT_RCVD_EVT:
675 LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_RCVD_EVT; code: 0x%X; pipe: 0x%X; data len: %u", fn,
676 eventData->rcvd_evt.evt_code, eventData->rcvd_evt.pipe, eventData->rcvd_evt.evt_len);
677 if(eventData->rcvd_evt.pipe == 0x0A) //UICC
678 {
679 LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_RCVD_EVT; source UICC",fn);
680 evtSrc = SecureElement::getInstance().getGenericEseId(SecureElement::getInstance().EE_HANDLE_0xF4 & ~NFA_HANDLE_GROUP_EE); //UICC
681 }
682 else if(eventData->rcvd_evt.pipe == 0x16) //ESE
683 {
684 LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_RCVD_EVT; source ESE",fn);
685 evtSrc = SecureElement::getInstance().getGenericEseId(EE_HANDLE_0xF3 & ~NFA_HANDLE_GROUP_EE); //ESE
686 }
687 else if(eventData->rcvd_evt.pipe == CONNECTIVITY_PIPE_ID_UICC3) //UICC3
688 {
689 LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_RCVD_EVT; source UICC3",fn);
690 evtSrc = SecureElement::getInstance().getGenericEseId(SecureElement::getInstance().EE_HANDLE_0xF9 & ~NFA_HANDLE_GROUP_EE); //UICC
691 }
692 else if (((eventData->rcvd_evt.evt_code == NFA_HCI_EVT_ATR))
693 &&(eventData->rcvd_evt.pipe == mStaticPipeProp))
694 {
695 LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_RCVD_EVT: NFA_HCI_ABORT; status:0x%X, pipe:0x%X, len:%d", fn,\
696 eventData->rcvd_evt.status, eventData->rcvd_evt.pipe, eventData->rcvd_evt.evt_len);
697 if(eventData->rcvd_evt.evt_len > 0)
698 {
699 sSecElem.mAbortEventWaitOk = true;
700 SyncEventGuard guard(sSecElem.mAbortEvent);
701 memcpy(sSecElem.mAtrInfo, eventData->rcvd_evt.p_evt_buf, eventData->rcvd_evt.evt_len);
702 sSecElem.mAtrInfolen = eventData->rcvd_evt.evt_len;
703 sSecElem.mAtrStatus = eventData->rcvd_evt.status;
704 sSecElem.mAbortEvent.notifyOne();
705 }
706 else
707 {
708 sSecElem.mAbortEventWaitOk = false;
709 SyncEventGuard guard(sSecElem.mAbortEvent);
710 sSecElem.mAbortEvent.notifyOne();
711 }
712 }
713 else if (eventData->rcvd_evt.evt_code == NFA_HCI_EVT_POST_DATA)
714 {
715 LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_RCVD_EVT; NFA_HCI_EVT_POST_DATA", fn);
716 SyncEventGuard guard (sSecElem.mTransceiveEvent);
717 sSecElem.mActualResponseSize = (eventData->rcvd_evt.evt_len > MAX_RESPONSE_SIZE) ? MAX_RESPONSE_SIZE : eventData->rcvd_evt.evt_len;
718 sSecElem.mTransceiveEvent.notifyOne ();
719 }
720 else if (eventData->rcvd_evt.evt_code == NFA_HCI_EVT_TRANSACTION)
721 {
722 LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_RCVD_EVT; NFA_HCI_EVT_TRANSACTION", fn);
723 // If we got an AID, notify any listeners
724 if ((eventData->rcvd_evt.evt_len > 3) && (eventData->rcvd_evt.p_evt_buf[0] == 0x81) )
725 {
726 int aidlen = eventData->rcvd_evt.p_evt_buf[1];
727 uint8_t* data = NULL;
728 int32_t datalen = 0;
729 uint8_t dataStartPosition = 0;
730 if((eventData->rcvd_evt.evt_len > 2+aidlen) && (eventData->rcvd_evt.p_evt_buf[2+aidlen] == 0x82))
731 {
732 //BERTLV decoding here, to support extended data length for params.
733 datalen = SecureElement::decodeBerTlvLength((uint8_t *)eventData->rcvd_evt.p_evt_buf, 2+aidlen+1, eventData->rcvd_evt.evt_len);
734 }
735 if(datalen >= 0)
736 {
737 /* Over 128 bytes data of transaction can not receive on PN547, Ref. BER-TLV length fields in ISO/IEC 7816 */
738 if ( datalen < 0x80)
739 {
740 dataStartPosition = 2+aidlen+2;
nxpandroid64fd68c2015-09-23 16:45:15 +0530741 }
nxf24591dc0bc2c2018-02-21 17:33:08 +0530742 else if ( datalen < 0x100)
743 {
744 dataStartPosition = 2+aidlen+3;
745 }
746 else if ( datalen < 0x10000)
747 {
748 dataStartPosition = 2+aidlen+4;
749 }
750 else if ( datalen < 0x1000000)
751 {
752 dataStartPosition = 2+aidlen+5;
753 }
754 data = &eventData->rcvd_evt.p_evt_buf[dataStartPosition];
nxpandroidecca0112018-06-06 14:41:40 +0530755
756 if (nfcFL.nfcNxpEse && nfcFL.eseFL._ESE_ETSI_READER_ENABLE)
757 {
758 if(MposManager::getInstance().validateHCITransactionEventParams(data, datalen) == NFA_STATUS_OK)
759 sSecElem.notifyTransactionListenersOfAid(&eventData->rcvd_evt.p_evt_buf[2],
760 aidlen, data, datalen, evtSrc);
761 }
762 else
763 sSecElem.notifyTransactionListenersOfAid(&eventData->rcvd_evt.p_evt_buf[2],
764 aidlen, data, datalen, evtSrc);
nxf24591dc0bc2c2018-02-21 17:33:08 +0530765 }
766 else
767 {
768 LOG(ERROR) << StringPrintf("Event data TLV length encoding Unsupported!");
nxpandroid64fd68c2015-09-23 16:45:15 +0530769 }
770 }
771 }
nxf24591dc0bc2c2018-02-21 17:33:08 +0530772 else if (eventData->rcvd_evt.evt_code == NFA_HCI_EVT_CONNECTIVITY)
nxpandroid64fd68c2015-09-23 16:45:15 +0530773 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530774 LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_RCVD_EVT; NFA_HCI_EVT_CONNECTIVITY", fn);
nxpandroid64fd68c2015-09-23 16:45:15 +0530775 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530776 else
nxf24591dc0bc2c2018-02-21 17:33:08 +0530777 {
778 LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_RCVD_EVT; ################################### eventData->rcvd_evt.evt_code = 0x%x , NFA_HCI_EVT_CONNECTIVITY = 0x%x", fn, eventData->rcvd_evt.evt_code, NFA_HCI_EVT_CONNECTIVITY);
779
780 LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_RCVD_EVT; ################################### ", fn);
781
782 }
783 break;
784
785 case NFA_HCI_SET_REG_RSP_EVT: //received response to write registry command
786 {
787 tNFA_HCI_REGISTRY& registry = eventData->registry;
788 LOG(INFO) << StringPrintf("%s: NFA_HCI_SET_REG_RSP_EVT; status=0x%X; pipe=0x%X", fn, registry.status, registry.pipe);
789 SyncEventGuard guard (sSecElem.mRegistryEvent);
790 sSecElem.mRegistryEvent.notifyOne ();
791 break;
792 }
793 default:
794 LOG(ERROR) << StringPrintf("%s: unknown event code=0x%X ????", fn, event);
795 break;
nxpandroid64fd68c2015-09-23 16:45:15 +0530796 }
nxpandroid0f06fde2017-08-14 11:25:28 +0530797}
nxpandroid64fd68c2015-09-23 16:45:15 +0530798
nxpandroid64fd68c2015-09-23 16:45:15 +0530799/*******************************************************************************
800**
801** Function: transceive
802**
803** Description: Send data to the secure element; read it's response.
804** xmitBuffer: Data to transmit.
805** xmitBufferSize: Length of data.
806** recvBuffer: Buffer to receive response.
807** recvBufferMaxSize: Maximum size of buffer.
808** recvBufferActualSize: Actual length of response.
809** timeoutMillisec: timeout in millisecond.
810**
811** Returns: True if ok.
812**
813*******************************************************************************/
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530814bool SecureElement::transceive (uint8_t* xmitBuffer, int32_t xmitBufferSize, uint8_t* recvBuffer,
815 int32_t recvBufferMaxSize, int32_t& recvBufferActualSize, int32_t timeoutMillisec)
nxpandroid64fd68c2015-09-23 16:45:15 +0530816{
nxpandroid0f06fde2017-08-14 11:25:28 +0530817
818
nxpandroid64fd68c2015-09-23 16:45:15 +0530819 static const char fn [] = "SecureElement::transceive";
820 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
821 bool isSuccess = false;
822 mTransceiveWaitOk = false;
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530823 uint8_t newSelectCmd[NCI_MAX_AID_LEN + 10];
nxf24591dc0bc2c2018-02-21 17:33:08 +0530824 isSuccess = false;
nxpandroidebf53fb2016-12-22 18:48:59 +0530825
nxpandroid64fd68c2015-09-23 16:45:15 +0530826
827 // Check if we need to replace an "empty" SELECT command.
828 // 1. Has there been a AID configured, and
829 // 2. Is that AID a valid length (i.e 16 bytes max), and
830 // 3. Is the APDU at least 4 bytes (for header), and
831 // 4. Is INS == 0xA4 (SELECT command), and
832 // 5. Is P1 == 0x04 (SELECT by AID), and
833 // 6. Is the APDU len 4 or 5 bytes.
834 //
835 // Note, the length of the configured AID is in the first
836 // byte, and AID starts from the 2nd byte.
837 if (mAidForEmptySelect[0] // 1
nxpandroid0f06fde2017-08-14 11:25:28 +0530838 && (mAidForEmptySelect[0] <= NCI_MAX_AID_LEN) // 2
839 && (xmitBufferSize >= 4) // 3
840 && (xmitBuffer[1] == 0xA4) // 4
841 && (xmitBuffer[2] == 0x04) // 5
842 && (xmitBufferSize <= 5)) // 6
nxpandroid64fd68c2015-09-23 16:45:15 +0530843 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530844 uint8_t idx = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +0530845
846 // Copy APDU command header from the input buffer.
847 memcpy(&newSelectCmd[0], &xmitBuffer[0], 4);
848 idx = 4;
849
850 // Set the Lc value to length of the new AID
851 newSelectCmd[idx++] = mAidForEmptySelect[0];
852
853 // Copy the AID
854 memcpy(&newSelectCmd[idx], &mAidForEmptySelect[1], mAidForEmptySelect[0]);
855 idx += mAidForEmptySelect[0];
856
857 // If there is an Le (5th byte of APDU), add it to the end.
858 if (xmitBufferSize == 5)
859 newSelectCmd[idx++] = xmitBuffer[4];
860
861 // Point to the new APDU
862 xmitBuffer = &newSelectCmd[0];
863 xmitBufferSize = idx;
864
nxf24591dc0bc2c2018-02-21 17:33:08 +0530865 LOG(INFO) << StringPrintf("%s: Empty AID SELECT cmd detected, substituting AID from config file, new length=%d", fn, idx);
nxpandroid64fd68c2015-09-23 16:45:15 +0530866 }
867
nxpandroid64fd68c2015-09-23 16:45:15 +0530868 {
869 SyncEventGuard guard (mTransceiveEvent);
870 mActualResponseSize = 0;
871 memset (mResponseData, 0, sizeof(mResponseData));
nxf24591dc0bc2c2018-02-21 17:33:08 +0530872 nfaStat = NFA_HciSendApdu (mNfaHciHandle, mActiveEeHandle, xmitBufferSize, xmitBuffer, sizeof(mResponseData), mResponseData, timeoutMillisec);
nxpandroideb52db42017-08-14 11:29:32 +0530873
nxpandroid64fd68c2015-09-23 16:45:15 +0530874 if (nfaStat == NFA_STATUS_OK)
875 {
nxpandroid64fd68c2015-09-23 16:45:15 +0530876 mTransceiveEvent.wait ();
nxpandroid64fd68c2015-09-23 16:45:15 +0530877 }
878 else
879 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530880 LOG(ERROR) << StringPrintf("%s: fail send data; error=0x%X", fn, nfaStat);
nxpandroid64fd68c2015-09-23 16:45:15 +0530881 goto TheEnd;
882 }
883 }
nxpandroid0f06fde2017-08-14 11:25:28 +0530884 if (mActualResponseSize > recvBufferMaxSize)
885 recvBufferActualSize = recvBufferMaxSize;
886 else
887 recvBufferActualSize = mActualResponseSize;
nxpandroid64fd68c2015-09-23 16:45:15 +0530888
nxpandroid0f06fde2017-08-14 11:25:28 +0530889 memcpy (recvBuffer, mResponseData, recvBufferActualSize);
nxpandroid9b94e6e2017-09-14 12:19:16 +0530890 isSuccess = true;
nxpandroid0f06fde2017-08-14 11:25:28 +0530891 TheEnd:
nxpandroid9b94e6e2017-09-14 12:19:16 +0530892 return (isSuccess);
nxpandroid64fd68c2015-09-23 16:45:15 +0530893}
894/*******************************************************************************
nxpandroid64fd68c2015-09-23 16:45:15 +0530895**
Ayush73d68912018-04-04 21:36:26 +0530896** Function: getActiveSecureElementList
897**
898** Description: Get the list of Activated Secure elements.
899** e: Java Virtual Machine.
900**
901** Returns: List of Activated Secure elements.
902**
903*******************************************************************************/
904jintArray SecureElement::getActiveSecureElementList (JNIEnv* e)
905{
906 uint8_t num_of_nfcee_present = 0;
907 tNFA_HANDLE nfcee_handle[MAX_NFCEE];
908 tNFA_EE_STATUS nfcee_status[MAX_NFCEE];
909 jint seId = 0;
910 int cnt = 0;
911 int i;
912
913 if (! getEeInfo())
914 return (NULL);
915
916 num_of_nfcee_present = mNfceeData_t.mNfceePresent;
917
918 jintArray list = e->NewIntArray (num_of_nfcee_present); //allocate array
919
920 for(i = 1; i<= num_of_nfcee_present ; i++)
921 {
922 nfcee_handle[i] = mNfceeData_t.mNfceeHandle[i];
923 nfcee_status[i] = mNfceeData_t.mNfceeStatus[i];
924
925 if(nfcee_handle[i] == EE_HANDLE_0xF3 && nfcee_status[i] == NFC_NFCEE_STATUS_ACTIVE)
926 {
927 seId = getGenericEseId(EE_HANDLE_0xF3 & ~NFA_HANDLE_GROUP_EE);
928 }
929
930 if(nfcee_handle[i] == EE_HANDLE_0xF4 && nfcee_status[i] == NFC_NFCEE_STATUS_ACTIVE)
931 {
932 seId = getGenericEseId(EE_HANDLE_0xF4 & ~NFA_HANDLE_GROUP_EE);
933 }
934
935 if(nfcee_handle[i] == EE_HANDLE_0xF8 && nfcee_status[i] == NFC_NFCEE_STATUS_ACTIVE)
936 {
937 seId = getGenericEseId(EE_HANDLE_0xF8 & ~NFA_HANDLE_GROUP_EE);
938 }
939
940 e->SetIntArrayRegion (list, cnt++, 1, &seId);
941 }
942
943 return list;
944}
945/*******************************************************************************
946**
nxf24591dc0bc2c2018-02-21 17:33:08 +0530947** Function: activate
nxpandroid64fd68c2015-09-23 16:45:15 +0530948**
nxf24591dc0bc2c2018-02-21 17:33:08 +0530949** Description: Turn on the secure element.
950** seID: ID of secure element; 0xF3 or 0xF4.
nxpandroid64fd68c2015-09-23 16:45:15 +0530951**
nxf24591dc0bc2c2018-02-21 17:33:08 +0530952** Returns: True if ok.
nxpandroid64fd68c2015-09-23 16:45:15 +0530953**
954*******************************************************************************/
nxf24591dc0bc2c2018-02-21 17:33:08 +0530955bool SecureElement::activate (jint seID)
nxpandroid64fd68c2015-09-23 16:45:15 +0530956{
nxf24591dc0bc2c2018-02-21 17:33:08 +0530957 static const char fn [] = "SecureElement::activate";
958 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
959 int numActivatedEe = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +0530960
nxf24591dc0bc2c2018-02-21 17:33:08 +0530961 tNFA_HANDLE handle = getEseHandleFromGenericId(seID);
962
963 LOG(INFO) << StringPrintf("%s: enter handle=0x%X, seID=0x%X", fn, handle,seID);
964
965 // Get Fresh EE info if needed.
966 if (! getEeInfo())
nxpandroid64fd68c2015-09-23 16:45:15 +0530967 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530968 LOG(ERROR) << StringPrintf("%s: no EE info", fn);
nxpandroid64fd68c2015-09-23 16:45:15 +0530969 return false;
970 }
nxf24591dc0bc2c2018-02-21 17:33:08 +0530971 if(SecureElement::getInstance().getGateAndPipeList() != SMX_PIPE_ID)
972 return false;
nxpandroid64fd68c2015-09-23 16:45:15 +0530973
nxf24591dc0bc2c2018-02-21 17:33:08 +0530974 //activate every discovered secure element
975 for (int index=0; index < mActualNumEe; index++)
nxpandroid64fd68c2015-09-23 16:45:15 +0530976 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530977 tNFA_EE_INFO& eeItem = mEeInfo[index];
nxpandroid64fd68c2015-09-23 16:45:15 +0530978
nxf24591dc0bc2c2018-02-21 17:33:08 +0530979 if (eeItem.ee_handle == EE_HANDLE_0xF3)
nxpandroid64fd68c2015-09-23 16:45:15 +0530980 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530981 if (eeItem.ee_status != NFC_NFCEE_STATUS_INACTIVE)
nxpandroid64fd68c2015-09-23 16:45:15 +0530982 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530983 LOG(INFO) << StringPrintf("%s: h=0x%X already activated", fn, eeItem.ee_handle);
984 numActivatedEe++;
985 continue;
nxpandroid64fd68c2015-09-23 16:45:15 +0530986 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530987
nxpandroid9b94e6e2017-09-14 12:19:16 +0530988 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530989 LOG(INFO) << StringPrintf("%s: set EE mode activate; h=0x%X", fn, eeItem.ee_handle);
990 if ((nfaStat = SecElem_EeModeSet (eeItem.ee_handle, NFA_EE_MD_ACTIVATE)) == NFA_STATUS_OK)
nxpandroidf68f0122017-08-14 11:31:59 +0530991 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530992 if (eeItem.ee_status == NFC_NFCEE_STATUS_ACTIVE)
993 numActivatedEe++;
994 if(eeItem.ee_handle == EE_HANDLE_0xF3)
nxpandroid64fd68c2015-09-23 16:45:15 +0530995 {
nxf24591dc0bc2c2018-02-21 17:33:08 +0530996 SyncEventGuard guard (SecureElement::getInstance().mModeSetNtf);
997 if(SecureElement::getInstance().mModeSetNtf.wait(500) == false)
998 {
999 LOG(ERROR) << StringPrintf("%s: timeout waiting for setModeNtf", __func__);
1000 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301001 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301002 }
1003 else
nxf24591dc0bc2c2018-02-21 17:33:08 +05301004 LOG(ERROR) << StringPrintf("%s: NFA_EeModeSet failed; error=0x%X", fn, nfaStat);
nxpandroid64fd68c2015-09-23 16:45:15 +05301005 }
1006 }
nxf24591dc0bc2c2018-02-21 17:33:08 +05301007 } //for
nxpandroid64fd68c2015-09-23 16:45:15 +05301008
nxf24591dc0bc2c2018-02-21 17:33:08 +05301009 mActiveEeHandle = getActiveEeHandle(handle);
nxpandroid64fd68c2015-09-23 16:45:15 +05301010
nxf24591dc0bc2c2018-02-21 17:33:08 +05301011 if (mActiveEeHandle == NFA_HANDLE_INVALID)
1012 LOG(ERROR) << StringPrintf("%s: ee handle not found", fn);
nxpandroid64fd68c2015-09-23 16:45:15 +05301013
nxf24591dc0bc2c2018-02-21 17:33:08 +05301014 LOG(INFO) << StringPrintf("%s: exit; active ee h=0x%X", fn, mActiveEeHandle);
1015 return mActiveEeHandle != NFA_HANDLE_INVALID;
nxpandroid64fd68c2015-09-23 16:45:15 +05301016}
nxpandroid64fd68c2015-09-23 16:45:15 +05301017/*******************************************************************************
1018**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301019** Function: deactivate
nxpandroid64fd68c2015-09-23 16:45:15 +05301020**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301021** Description: Turn off the secure element.
1022** seID: ID of secure element; 0xF3 or 0xF4.
nxpandroid64fd68c2015-09-23 16:45:15 +05301023**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301024** Returns: True if ok.
nxpandroid64fd68c2015-09-23 16:45:15 +05301025**
1026*******************************************************************************/
nxf24591dc0bc2c2018-02-21 17:33:08 +05301027bool SecureElement::deactivate (jint seID)
nxpandroid64fd68c2015-09-23 16:45:15 +05301028{
nxf24591dc0bc2c2018-02-21 17:33:08 +05301029 static const char fn [] = "SecureElement::deactivate";
1030 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
1031 bool retval = false;
nxpandroid64fd68c2015-09-23 16:45:15 +05301032
nxf24591dc0bc2c2018-02-21 17:33:08 +05301033 LOG(INFO) << StringPrintf("%s: enter; seID=0x%X, mActiveEeHandle=0x%X", fn, seID, mActiveEeHandle);
1034
1035 tNFA_HANDLE handle = getEseHandleFromGenericId(seID);
1036
1037 LOG(INFO) << StringPrintf("%s: handle=0x%X", fn, handle);
1038
1039 if (!mIsInit)
1040 {
1041 LOG(ERROR) << StringPrintf("%s: not init", fn);
1042 goto TheEnd;
1043 }
1044
1045
1046 if (seID == NFA_HANDLE_INVALID)
1047 {
1048 LOG(ERROR) << StringPrintf("%s: invalid EE handle", fn);
1049 goto TheEnd;
1050 }
1051
1052 mActiveEeHandle = NFA_HANDLE_INVALID;
1053
1054 //deactivate secure element
1055 for (int index=0; index < mActualNumEe; index++)
1056 {
1057 tNFA_EE_INFO& eeItem = mEeInfo[index];
1058
1059 if (eeItem.ee_handle == handle &&
1060 ((eeItem.ee_handle == EE_HANDLE_0xF3) || (eeItem.ee_handle == EE_HANDLE_0xF4) ||
1061 (eeItem.ee_handle == EE_HANDLE_0xF8)||
1062 (eeItem.ee_handle == EE_HANDLE_0xF9))) {
1063 if (eeItem.ee_status == NFC_NFCEE_STATUS_INACTIVE)
1064 {
1065 LOG(INFO) << StringPrintf("%s: h=0x%X already deactivated", fn, eeItem.ee_handle);
1066 break;
1067 }
1068
1069 {
1070 LOG(INFO) << StringPrintf("%s: set EE mode activate; h=0x%X", fn, eeItem.ee_handle);
1071 if ((nfaStat = SecElem_EeModeSet (eeItem.ee_handle, NFA_EE_MD_DEACTIVATE)) == NFA_STATUS_OK)
1072 {
1073 LOG(INFO) << StringPrintf("%s: eeItem.ee_status =0x%X NFC_NFCEE_STATUS_INACTIVE = %x", fn, eeItem.ee_status, NFC_NFCEE_STATUS_INACTIVE);
1074 if (eeItem.ee_status == NFC_NFCEE_STATUS_INACTIVE)
1075 {
1076 LOG(ERROR) << StringPrintf("%s: NFA_EeModeSet success; status=0x%X", fn, nfaStat);
1077 retval = true;
1078 }
1079 }
1080 else
1081 LOG(ERROR) << StringPrintf("%s: NFA_EeModeSet failed; error=0x%X", fn, nfaStat);
1082 }
1083 }
1084 } //for
1085
1086TheEnd:
1087 LOG(INFO) << StringPrintf("%s: exit; ok=%u", fn, retval);
1088 return retval;
1089}
1090/*******************************************************************************
1091 **
1092 ** Function: SecElem_EeModeSet
1093 **
1094 ** Description: Perform SE mode set ON/OFF based on mode type
1095 **
1096 ** Returns: NFA_STATUS_OK/NFA_STATUS_FAILED.
1097 **
1098 *******************************************************************************/
1099tNFA_STATUS SecureElement::SecElem_EeModeSet(uint16_t handle, uint8_t mode)
1100{
1101 tNFA_STATUS stat = NFA_STATUS_FAILED;
1102 LOG(INFO) << StringPrintf("%s:Enter mode = %d", __func__, mode);
1103
1104 SyncEventGuard guard (sSecElem.mEeSetModeEvent);
1105 stat = NFA_EeModeSet(handle, mode);
1106 if(stat == NFA_STATUS_OK)
1107 {
1108 sSecElem.mEeSetModeEvent.wait ();
1109 }
1110
1111 return stat;
1112}
nxpandroid64fd68c2015-09-23 16:45:15 +05301113/*******************************************************************************
1114**
1115** Function: getSETechnology
1116**
1117** Description: return the technologies suported by se.
1118** eeHandle: Handle to execution environment.
1119**
1120** Returns: Information about an execution environment.
1121**
1122*******************************************************************************/
1123jint SecureElement::getSETechnology(tNFA_HANDLE eeHandle)
1124{
1125 int tech_mask = 0x00;
nxf24591dc0bc2c2018-02-21 17:33:08 +05301126 //static const char fn [] = "SecureElement::getSETechnology";
nxpandroid64fd68c2015-09-23 16:45:15 +05301127 // Get Fresh EE info.
1128 if (! getEeInfo())
1129 {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301130 //ALOGE("%s: No updated eeInfo available", fn);
nxpandroid64fd68c2015-09-23 16:45:15 +05301131 }
1132
1133 tNFA_EE_INFO* eeinfo = findEeByHandle(eeHandle);
1134
1135 if(eeinfo!=NULL){
1136 if(eeinfo->la_protocol != 0x00)
1137 {
1138 tech_mask |= 0x01;
1139 }
1140
1141 if(eeinfo->lb_protocol != 0x00)
1142 {
1143 tech_mask |= 0x02;
1144 }
1145
1146 if(eeinfo->lf_protocol != 0x00)
1147 {
1148 tech_mask |= 0x04;
1149 }
1150 }
1151
1152 return tech_mask;
1153}
1154/*******************************************************************************
nxf24591dc0bc2c2018-02-21 17:33:08 +05301155 **
1156 ** Function: notifyModeSet
1157 **
1158 ** Description: Perform SE mode set ON/OFF based on mode type
1159 **
1160 ** Returns: NFA_STATUS_OK/NFA_STATUS_FAILED.
1161 **
1162 *******************************************************************************/
1163void SecureElement::notifyModeSet (tNFA_HANDLE eeHandle, bool success, tNFA_EE_STATUS eeStatus)
nxpandroid64fd68c2015-09-23 16:45:15 +05301164{
nxf24591dc0bc2c2018-02-21 17:33:08 +05301165 static const char* fn = "SecureElement::notifyModeSet";
1166 if (success)
nxpandroid64fd68c2015-09-23 16:45:15 +05301167 {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301168 tNFA_EE_INFO *pEE = sSecElem.findEeByHandle (eeHandle);
1169 if (pEE)
nxpandroid64fd68c2015-09-23 16:45:15 +05301170 {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301171 pEE->ee_status = eeStatus;
1172 LOG(INFO) << StringPrintf("%s: NFA_EE_MODE_SET_EVT; (0x%04x)", fn, pEE->ee_status);
nxpandroid64fd68c2015-09-23 16:45:15 +05301173 }
nxf24591dc0bc2c2018-02-21 17:33:08 +05301174 else
1175 LOG(INFO) << StringPrintf("%s: NFA_EE_MODE_SET_EVT; EE: 0x%04x not found. mActiveEeHandle: 0x%04x", fn, eeHandle, sSecElem.mActiveEeHandle);
nxpandroid64fd68c2015-09-23 16:45:15 +05301176 }
nxf24591dc0bc2c2018-02-21 17:33:08 +05301177 SyncEventGuard guard (sSecElem.mEeSetModeEvent);
1178 sSecElem.mEeSetModeEvent.notifyOne();
nxpandroid64fd68c2015-09-23 16:45:15 +05301179}
1180/*******************************************************************************
1181**
1182** Function: getAtr
1183**
1184** Description: GetAtr response from the connected eSE
1185**
1186** Returns: Returns True if success
1187**
1188*******************************************************************************/
nxpandroid6fd9cdb2017-07-12 18:25:41 +05301189bool SecureElement::getAtr(jint seID, uint8_t* recvBuffer, int32_t *recvBufferSize)
nxpandroid64fd68c2015-09-23 16:45:15 +05301190{
1191 static const char fn[] = "SecureElement::getAtr";
1192 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
nxpandroidebf53fb2016-12-22 18:48:59 +05301193 int timeoutMillisec = 10000;
nxf24591dc0bc2c2018-02-21 17:33:08 +05301194
1195 LOG(INFO) << StringPrintf("%s: enter; seID=0x%X", fn, seID);
nxpandroid0f06fde2017-08-14 11:25:28 +05301196 if(nfcFL.nfcNxpEse) {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301197 /*ETSI 12 Gate Info ATR */
nxpandroid0f06fde2017-08-14 11:25:28 +05301198 mAbortEventWaitOk = false;
1199 uint8_t mAtrInfo1[EVT_ABORT_MAX_RSP_LEN]={0};
1200 uint8_t atr_len = EVT_ABORT_MAX_RSP_LEN;
1201 SyncEventGuard guard (mAbortEvent);
1202 nfaStat = NFA_HciSendEvent(mNfaHciHandle, mNewPipeId, EVT_ABORT, 0, NULL, atr_len, mAtrInfo1, timeoutMillisec);
nxpandroid0f06fde2017-08-14 11:25:28 +05301203 {
1204 mAbortEvent.wait();
1205 }
1206 if(mAbortEventWaitOk == false)
1207 {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301208 LOG(INFO) << StringPrintf("%s (EVT_ABORT)Wait reposne timeout", fn);
nxpandroid0f06fde2017-08-14 11:25:28 +05301209 nfaStat = NFA_STATUS_FAILED;
1210 }
1211 else
1212 {
1213 *recvBufferSize = mAtrInfolen;
1214 memcpy(recvBuffer, mAtrInfo, mAtrInfolen);
1215 }
nxpandroid0f06fde2017-08-14 11:25:28 +05301216 }
nxf24591dc0bc2c2018-02-21 17:33:08 +05301217
nxpandroid0f06fde2017-08-14 11:25:28 +05301218 return (nfaStat == NFA_STATUS_OK)?true:false;
nxpandroid64fd68c2015-09-23 16:45:15 +05301219}
nxpandroid64fd68c2015-09-23 16:45:15 +05301220/*******************************************************************************
1221**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301222** Function: SecEle_Modeset
nxpandroid64fd68c2015-09-23 16:45:15 +05301223**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301224** Description: reSet NFCEE.
nxpandroid64fd68c2015-09-23 16:45:15 +05301225**
1226** Returns: True if ok.
1227**
1228*******************************************************************************/
nxpandroid6fd9cdb2017-07-12 18:25:41 +05301229bool SecureElement::SecEle_Modeset(uint8_t type)
nxpandroid64fd68c2015-09-23 16:45:15 +05301230{
1231 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
1232 bool retval = true;
1233
nxf24591dc0bc2c2018-02-21 17:33:08 +05301234 LOG(INFO) << StringPrintf("set EE mode = 0x%X", type);
1235 nfaStat = SecElem_EeModeSet (EE_HANDLE_0xF3, type);
1236 if ( nfaStat == NFA_STATUS_OK)
nxpandroid64fd68c2015-09-23 16:45:15 +05301237 {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301238 LOG(INFO) << StringPrintf("SecEle_Modeset: Success");
nxpandroid64fd68c2015-09-23 16:45:15 +05301239 }
1240 else
1241 {
1242 retval = false;
nxpandroid64fd68c2015-09-23 16:45:15 +05301243 }
1244 return retval;
1245}
nxpandroid6fd9cdb2017-07-12 18:25:41 +05301246/*******************************************************************************
1247**
1248** Function: initializeEeHandle
1249**
1250** Description: Set NFCEE handle.
1251**
1252** Returns: True if ok.
1253**
1254*******************************************************************************/
1255bool SecureElement::initializeEeHandle ()
1256{
1257 if(NFA_GetNCIVersion() == NCI_VERSION_2_0)
1258 EE_HANDLE_0xF4 = 0x480;
1259 else
1260 EE_HANDLE_0xF4 = 0x402;
1261 return true;
1262}
nxpandroid1680a6d2017-01-13 19:13:14 +05301263/*******************************************************************************
1264**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301265** Function: getEeInfo
nxpandroid1680a6d2017-01-13 19:13:14 +05301266**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301267** Description: Get latest information about execution environments from stack.
nxpandroid1680a6d2017-01-13 19:13:14 +05301268**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301269** Returns: True if at least 1 EE is available.
nxpandroid1680a6d2017-01-13 19:13:14 +05301270**
1271*******************************************************************************/
nxf24591dc0bc2c2018-02-21 17:33:08 +05301272bool SecureElement::getEeInfo()
nxpandroid1680a6d2017-01-13 19:13:14 +05301273{
nxf24591dc0bc2c2018-02-21 17:33:08 +05301274 static const char fn [] = "SecureElement::getEeInfo";
1275 LOG(INFO) << StringPrintf("%s: enter; mbNewEE=%d, mActualNumEe=%d", fn, mbNewEE, mActualNumEe);
nxpandroid7d44e572016-08-01 19:11:04 +05301276 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
nxpandroid0f06fde2017-08-14 11:25:28 +05301277
nxf24591dc0bc2c2018-02-21 17:33:08 +05301278 /*Reading latest eEinfo incase it is updated*/
1279 mbNewEE = true;
1280 mNumEePresent = 0;
nxpandroida9a68ba2016-01-14 21:12:17 +05301281
nxf24591dc0bc2c2018-02-21 17:33:08 +05301282 if (mbNewEE)
1283 {
1284
1285 memset (&mNfceeData_t, 0, sizeof (mNfceeData_t));
1286
1287 mActualNumEe = nfcFL.nfccFL._NFA_EE_MAX_EE_SUPPORTED;
1288 if ((nfaStat = NFA_EeGetInfo(&mActualNumEe, mEeInfo)) != NFA_STATUS_OK)
nxpandroida9a68ba2016-01-14 21:12:17 +05301289 {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301290 LOG(ERROR) << StringPrintf("%s: fail get info; error=0x%X", fn, nfaStat);
1291 mActualNumEe = 0;
nxpandroida9a68ba2016-01-14 21:12:17 +05301292 }
1293 else
1294 {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301295 mbNewEE = false;
1296
1297 LOG(ERROR) << StringPrintf("%s: num EEs discovered: %u", fn, mActualNumEe);
1298 if (mActualNumEe != 0)
nxpandroida9a68ba2016-01-14 21:12:17 +05301299 {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301300 for (uint8_t xx = 0; xx < mActualNumEe; xx++)
nxpandroid7d44e572016-08-01 19:11:04 +05301301 {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301302 if (mEeInfo[xx].ee_interface[0] != NCI_NFCEE_INTERFACE_HCI_ACCESS)
1303 mNumEePresent++;
1304
1305 mNfceeData_t.mNfceeHandle[xx] = mEeInfo[xx].ee_handle;
1306 mNfceeData_t.mNfceeStatus[xx] = mEeInfo[xx].ee_status;
nxpandroid7d44e572016-08-01 19:11:04 +05301307 }
1308 }
1309 }
1310 }
nxf24591dc0bc2c2018-02-21 17:33:08 +05301311 LOG(INFO) << StringPrintf("%s: exit; mActualNumEe=%d, mNumEePresent=%d", fn, mActualNumEe,mNumEePresent);
nxpandroid5d64ce92016-11-18 19:48:53 +05301312
nxf24591dc0bc2c2018-02-21 17:33:08 +05301313 mNfceeData_t.mNfceePresent = mNumEePresent;
nxpandroid5d64ce92016-11-18 19:48:53 +05301314
nxf24591dc0bc2c2018-02-21 17:33:08 +05301315 return (mActualNumEe != 0);
ubuntu63527d22017-10-11 14:27:02 +05301316}
nxpandroid5d64ce92016-11-18 19:48:53 +05301317/*******************************************************************************
1318**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301319** Function: findEeByHandle
nxpandroid5d64ce92016-11-18 19:48:53 +05301320**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301321** Description: Find information about an execution environment.
1322** eeHandle: Handle to execution environment.
nxpandroid5d64ce92016-11-18 19:48:53 +05301323**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301324** Returns: Information about an execution environment.
nxpandroid5d64ce92016-11-18 19:48:53 +05301325**
1326*******************************************************************************/
nxf24591dc0bc2c2018-02-21 17:33:08 +05301327tNFA_EE_INFO *SecureElement::findEeByHandle (tNFA_HANDLE eeHandle)
nxpandroid5d64ce92016-11-18 19:48:53 +05301328{
nxf24591dc0bc2c2018-02-21 17:33:08 +05301329 for (uint8_t xx = 0; xx < mActualNumEe; xx++)
nxpandroid5d64ce92016-11-18 19:48:53 +05301330 {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301331 if (mEeInfo[xx].ee_handle == eeHandle)
1332 return (&mEeInfo[xx]);
1333 }
1334 return (NULL);
1335}
1336/*******************************************************************************
1337**
1338** Function: getEseHandleFromGenericId
1339**
1340** Description: Whether controller is routing listen-mode events to
1341** secure elements or a pipe is connected.
1342**
1343** Returns: Returns Secure element Handle ex:- 402, 4C0, 481
1344**
1345*******************************************************************************/
1346tNFA_HANDLE SecureElement::getEseHandleFromGenericId(jint eseId)
1347{
1348 uint16_t handle = NFA_HANDLE_INVALID;
1349 static const char fn [] = "SecureElement::getEseHandleFromGenericId";
1350 LOG(INFO) << StringPrintf("%s: enter; ESE-ID = 0x%02X", fn, eseId);
nxpandroid5c5b2152017-09-14 12:30:17 +05301351
nxf24591dc0bc2c2018-02-21 17:33:08 +05301352 //Map the generic id to actual handle
1353 if(eseId == ESE_ID || eseId == EE_APP_HANLDE_ESE) //ESE
1354 {
1355 handle = EE_HANDLE_0xF3; //0x4C0;
1356 }
1357 else if(eseId == UICC_ID || eseId == EE_APP_HANLDE_UICC) //UICC
1358 {
1359 handle = SecureElement::getInstance().EE_HANDLE_0xF4; //0x402;
1360 }
1361 else if(eseId == UICC2_ID || eseId == EE_APP_HANLDE_UICC2) //UICC
1362 {
1363 handle = RoutingManager::getInstance().getUicc2selected(); //0x402;
1364 }
1365 else if(eseId == UICC3_ID || eseId == EE_APP_HANLDE_UICC3) //UICC
1366 {
1367 handle = SecureElement::getInstance().EE_HANDLE_0xF9; //0x482;
1368 }
1369 else if(eseId == DH_ID) //Host
1370 {
1371 handle = SecureElement::getInstance().EE_HANDLE_0xF0; //0x400;
1372 }
1373 else if(eseId == EE_HANDLE_0xF3 || eseId == EE_HANDLE_0xF4 || eseId == EE_HANDLE_0xF9)
1374 {
1375 handle = eseId;
1376 }
1377 LOG(INFO) << StringPrintf("%s: enter; ESE-Handle = 0x%03X", fn, handle);
1378 return handle;
1379}
1380/*******************************************************************************
1381**
1382** Function: getActiveEeHandle
1383**
1384** Description: Get the handle to the execution environment.
1385**
1386** Returns: Handle to the execution environment.
1387**
1388*******************************************************************************/
1389tNFA_HANDLE SecureElement::getActiveEeHandle (tNFA_HANDLE handle)
1390{
1391 static const char fn [] = "SecureElement::getActiveEeHandle";
1392 LOG(INFO) << StringPrintf("%s: - Enter", fn);
1393
1394 for (uint8_t xx = 0; xx < mActualNumEe; xx++)
1395 {
1396 if (mEeInfo[xx].ee_handle == EE_HANDLE_0xF3)
nxpandroid5c5b2152017-09-14 12:30:17 +05301397 {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301398 return (mEeInfo[xx].ee_handle);
nxpandroid5c5b2152017-09-14 12:30:17 +05301399 }
nxpandroid5c5b2152017-09-14 12:30:17 +05301400
nxpandroid5d64ce92016-11-18 19:48:53 +05301401 }
nxf24591dc0bc2c2018-02-21 17:33:08 +05301402 return NFA_HANDLE_INVALID;
nxpandroid5d64ce92016-11-18 19:48:53 +05301403}
nxpandroid5d64ce92016-11-18 19:48:53 +05301404/*******************************************************************************
1405**
1406** Function setNfccPwrConfig
1407**
1408** Description sends the link cntrl command to eSE with the value passed
1409**
1410** Returns status
1411**
1412*******************************************************************************/
nxpandroid6fd9cdb2017-07-12 18:25:41 +05301413tNFA_STATUS SecureElement::setNfccPwrConfig(uint8_t value)
nxpandroid5d64ce92016-11-18 19:48:53 +05301414{
nxf24591dc0bc2c2018-02-21 17:33:08 +05301415 static const char fn [] = "SecureElement::setNfccPwrConfig()";
nxpandroid5d64ce92016-11-18 19:48:53 +05301416 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
nxpandroid6fd9cdb2017-07-12 18:25:41 +05301417 static uint8_t cur_value = 0xFF;
nxf24591dc0bc2c2018-02-21 17:33:08 +05301418 LOG(INFO) << StringPrintf("%s: Enter: config= 0x%X", fn, value);
1419 cur_value = value;
1420 SyncEventGuard guard (mPwrLinkCtrlEvent);
1421 nfaStat = NFA_SendPowerLinkCommand((uint8_t)EE_HANDLE_0xF3, value);
1422 if(nfaStat == NFA_STATUS_OK)
1423 mPwrLinkCtrlEvent.wait(NFC_CMD_TIMEOUT);
1424 LOG(INFO) << StringPrintf("%s: Exit: Status= 0x%X", fn, mPwrCmdstatus);
nxpandroid5d64ce92016-11-18 19:48:53 +05301425 return mPwrCmdstatus;
1426}
nxf24591dc0bc2c2018-02-21 17:33:08 +05301427bool SecureElement::sendEvent(uint8_t event)
1428{
1429 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
1430 bool retval = true;
1431 nfaStat = NFA_HciSendEvent (mNfaHciHandle, mNewPipeId, event, 0x00, NULL, 0x00,NULL, 0);
nxpandroid6fd9cdb2017-07-12 18:25:41 +05301432
nxf24591dc0bc2c2018-02-21 17:33:08 +05301433 if(nfaStat != NFA_STATUS_OK)
1434 retval = false;
1435
1436 return retval;
1437}
1438
1439/*******************************************************************************
1440**
1441** Function: getEeHandleList
1442**
1443** Description: Get default Secure Element handle.
1444** isHCEEnabled: whether host routing is enabled or not.
1445**
1446** Returns: Returns Secure Element list and count.
1447**
1448*******************************************************************************/
1449void SecureElement::getEeHandleList(tNFA_HANDLE *list, uint8_t* count)
1450{
1451 tNFA_HANDLE handle;
1452 int i;
1453 static const char fn [] = "SecureElement::getEeHandleList";
1454 *count = 0;
1455 for ( i = 0; i < mActualNumEe; i++)
1456 {
1457 LOG(INFO) << StringPrintf("%s: %d = 0x%X", fn, i, mEeInfo[i].ee_handle);
1458 if ((mEeInfo[i].ee_handle == 0x401) || (mEeInfo[i].ee_interface[0] == NCI_NFCEE_INTERFACE_HCI_ACCESS) ||
1459 (mEeInfo[i].ee_status == NFC_NFCEE_STATUS_INACTIVE))
1460 {
1461 LOG(INFO) << StringPrintf("%s: %u = 0x%X", fn, i, mEeInfo[i].ee_handle);
1462 continue;
1463 }
1464
1465 handle = mEeInfo[i].ee_handle & ~NFA_HANDLE_GROUP_EE;
1466 list[*count] = handle;
1467 *count = *count + 1 ;
1468 LOG(INFO) << StringPrintf("%s: Handle %d = 0x%X", fn, i, handle);
1469 }
1470}
1471
1472/*******************************************************************************
1473**
1474** Function: getGateAndPipeList
1475**
1476** Description: Get the gate and pipe list.
1477**
1478** Returns: None
1479**
1480*******************************************************************************/
1481uint8_t SecureElement::getGateAndPipeList()
1482{
1483 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
1484 static const char fn [] = "SecureElement::getActiveEeHandle";
1485 //uint8_t destHost = (EE_HANDLE_0xF3 & ~NFA_HANDLE_GROUP_EE);
1486
1487 // Get a list of existing gates and pipes
1488 LOG(INFO) << StringPrintf("%s: get gate, pipe list", fn);
1489 SyncEventGuard guard (mPipeListEvent);
1490 nfaStat = NFA_HciGetGateAndPipeList (mNfaHciHandle);
1491 if (nfaStat == NFA_STATUS_OK)
1492 {
1493 mPipeListEvent.wait();
1494 if (mHciCfg.status == NFA_STATUS_OK)
1495 {
1496 mNewPipeId = 0x19;
1497 /*WA: Not updated the pipe id from libnfc-nci
1498 for (uint8_t xx = 0; xx < mHciCfg.num_pipes; xx++)
1499 {
1500 if ( (mHciCfg.pipe[xx].dest_host == destHost))
1501 {
1502 mNewSourceGate = mHciCfg.pipe[xx].local_gate;
1503 mNewPipeId = mHciCfg.pipe[xx].pipe_id;
1504
1505 LOG(INFO) << StringPrintf("%s: found configured gate: 0x%02x pipe: 0x%02x", fn, mNewSourceGate, mNewPipeId);
1506 break;
1507 }
1508 }
1509 */
1510 }
1511 }
1512 return mNewPipeId;
1513}
1514/*******************************************************************************
1515**
nxpandroidecca0112018-06-06 14:41:40 +05301516** Function getLastRfFiledToggleTime
1517**
1518** Description Provides the last RF filed toggile timer
1519**
1520** Returns timespec
1521**
1522*******************************************************************************/
1523struct timespec SecureElement::getLastRfFiledToggleTime(void)
1524{
1525 return mLastRfFieldToggle;
1526}
1527/*******************************************************************************
1528**
nxf24591dc0bc2c2018-02-21 17:33:08 +05301529** Function: finalize
1530**
1531** Description: Release all resources.
1532**
1533** Returns: None
1534**
1535*******************************************************************************/
1536void SecureElement::finalize() {
1537 mIsInit = false;
1538 mNativeData = NULL;
1539}