nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1 | /****************************************************************************** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 2 | * |
| 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> |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 25 | #include <semaphore.h> |
| 26 | #include <errno.h> |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 27 | #include "config.h" |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 28 | #include "phNxpConfig.h" |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 29 | #include "nfc_config.h" |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 30 | #include "RoutingManager.h" |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 31 | using android::base::StringPrintf; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 32 | |
| 33 | SecureElement SecureElement::sSecElem; |
| 34 | const char* SecureElement::APP_NAME = "nfc_jni"; |
nxpandroid | ecca011 | 2018-06-06 14:41:40 +0530 | [diff] [blame] | 35 | extern bool nfc_debug_enabled; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 36 | |
nxpandroid | ecca011 | 2018-06-06 14:41:40 +0530 | [diff] [blame] | 37 | #include "MposManager.h" |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 38 | namespace android |
| 39 | { |
| 40 | extern void startRfDiscovery (bool isStart); |
| 41 | } |
| 42 | uint8_t SecureElement::mStaticPipeProp; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 43 | /******************************************************************************* |
| 44 | ** |
| 45 | ** Function: SecureElement |
| 46 | ** |
| 47 | ** Description: Initialize member variables. |
| 48 | ** |
| 49 | ** Returns: None |
| 50 | ** |
| 51 | *******************************************************************************/ |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 52 | SecureElement::SecureElement() : |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 53 | mNewPipeId (0), |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 54 | mNativeData(NULL), |
| 55 | mbNewEE (true), |
| 56 | mIsInit (false), |
nxpandroid | ecca011 | 2018-06-06 14:41:40 +0530 | [diff] [blame] | 57 | mNewSourceGate (0), |
| 58 | mRfFieldIsOn(false), |
| 59 | mActivatedInListenMode (false) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 60 | { |
nxpandroid | db4a204 | 2017-08-14 11:27:34 +0530 | [diff] [blame] | 61 | memset (&mEeInfo, 0, nfcFL.nfccFL._NFA_EE_MAX_EE_SUPPORTED *sizeof(tNFA_EE_INFO)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 62 | memset (mAidForEmptySelect, 0, sizeof(mAidForEmptySelect)); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 63 | memset (&mHciCfg, 0, sizeof(mHciCfg)); |
nxpandroid | ecca011 | 2018-06-06 14:41:40 +0530 | [diff] [blame] | 64 | memset (&mLastRfFieldToggle, 0, sizeof(mLastRfFieldToggle)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 65 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 66 | /******************************************************************************* |
| 67 | ** |
| 68 | ** Function: getInstance |
| 69 | ** |
| 70 | ** Description: Get the SecureElement singleton object. |
| 71 | ** |
| 72 | ** Returns: SecureElement object. |
| 73 | ** |
| 74 | *******************************************************************************/ |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 75 | SecureElement& SecureElement::getInstance() { return sSecElem; } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 76 | |
| 77 | /******************************************************************************* |
| 78 | ** |
| 79 | ** Function: initialize |
| 80 | ** |
| 81 | ** Description: Initialize all member variables. |
| 82 | ** native: Native data. |
| 83 | ** |
| 84 | ** Returns: True if ok. |
| 85 | ** |
| 86 | *******************************************************************************/ |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 87 | bool SecureElement::initialize(nfc_jni_native_data* native) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 88 | static const char fn [] = "SecureElement::initialize"; |
| 89 | tNFA_STATUS nfaStat; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 90 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 91 | LOG(INFO) << StringPrintf("%s: enter", fn); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 92 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 93 | |
| 94 | mActiveEeHandle = NFA_HANDLE_INVALID; |
| 95 | mNfaHciHandle = NFA_HANDLE_INVALID; |
| 96 | |
| 97 | mNativeData = native; |
| 98 | mthreadnative = native; |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 99 | mActualNumEe = nfcFL.nfccFL._NFA_EE_MAX_EE_SUPPORTED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 100 | mbNewEE = true; |
| 101 | mNewPipeId = 0; |
| 102 | mNewSourceGate = 0; |
Sreenivas | 426138e | 2018-04-04 14:56:08 +0530 | [diff] [blame] | 103 | unsigned long val = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 104 | memset (mEeInfo, 0, sizeof(mEeInfo)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 105 | memset (&mHciCfg, 0, sizeof(mHciCfg)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 106 | memset(mAidForEmptySelect, 0, sizeof(mAidForEmptySelect)); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 107 | if (GetNxpNumValue(NAME_NXP_DEFAULT_UICC2_SELECT, &muicc2_selected, sizeof(muicc2_selected)) == false) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 108 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 109 | muicc2_selected = UICC2_ID; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 110 | } |
Sreenivas | 426138e | 2018-04-04 14:56:08 +0530 | [diff] [blame] | 111 | 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); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 120 | initializeEeHandle(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 121 | |
| 122 | // Get Fresh EE info. |
| 123 | if (! getEeInfo()) |
| 124 | return (false); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 125 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 126 | // If the controller has an HCI Network, register for that |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 127 | //for (size_t xx = 0; xx < mActualNumEe; xx++) |
| 128 | for (size_t xx = 0; xx < MAX_NUM_EE; xx++) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 129 | { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 130 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 131 | if((mEeInfo[xx].ee_handle != EE_HANDLE_0xF4) |
| 132 | || ((((mEeInfo[xx].ee_interface[0] == NCI_NFCEE_INTERFACE_HCI_ACCESS) |
Yerriswamy | 70bfbcd | 2017-09-27 22:38:42 +0530 | [diff] [blame] | 133 | && (mEeInfo[xx].ee_status == NFC_NFCEE_STATUS_ACTIVE)) || (NFA_GetNCIVersion() == NCI_VERSION_2_0)))) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 134 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 135 | LOG(INFO) << StringPrintf("%s: Found HCI network, try hci register", fn); |
nxpandroid | eb52db4 | 2017-08-14 11:29:32 +0530 | [diff] [blame] | 136 | |
| 137 | SyncEventGuard guard (mHciRegisterEvent); |
| 138 | |
| 139 | nfaStat = NFA_HciRegister (const_cast<char*>(APP_NAME), nfaHciCallback, true); |
| 140 | if (nfaStat != NFA_STATUS_OK) |
| 141 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 142 | LOG(ERROR) << StringPrintf("%s: fail hci register; error=0x%X", fn, nfaStat); |
nxpandroid | eb52db4 | 2017-08-14 11:29:32 +0530 | [diff] [blame] | 143 | return (false); |
| 144 | } |
| 145 | mHciRegisterEvent.wait(); |
| 146 | break; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 147 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 148 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 149 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 150 | mIsInit = true; |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 151 | LOG(INFO) << StringPrintf("%s: exit", fn); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 152 | return (true); |
| 153 | } |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 154 | |
| 155 | /******************************************************************************* |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 156 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 157 | ** Function: getGenericEseId |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 158 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 159 | ** Description: Whether controller is routing listen-mode events to |
| 160 | ** secure elements or a pipe is connected. |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 161 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 162 | ** Returns: Return the generic SE id ex:- 00,01,02,04 |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 163 | ** |
| 164 | *******************************************************************************/ |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 165 | jint 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); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 169 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 170 | //Map the actual handle to generic id |
| 171 | if(handle == (EE_HANDLE_0xF3 & ~NFA_HANDLE_GROUP_EE) ) //ESE - 0xC0 |
| 172 | { |
| 173 | ret = ESE_ID; |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 174 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 175 | 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; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 189 | } |
nxpandroid | ecca011 | 2018-06-06 14:41:40 +0530 | [diff] [blame] | 190 | /******************************************************************************* |
| 191 | ** |
| 192 | ** Function TimeDiff |
| 193 | ** |
| 194 | ** Description Computes time difference in milliseconds. |
| 195 | ** |
| 196 | ** Returns Time difference in milliseconds |
| 197 | ** |
| 198 | *******************************************************************************/ |
| 199 | static uint32_t TimeDiff(timespec start, timespec end) |
| 200 | { |
| 201 | end.tv_sec -= start.tv_sec; |
| 202 | end.tv_nsec -= start.tv_nsec; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 203 | |
nxpandroid | ecca011 | 2018-06-06 14:41:40 +0530 | [diff] [blame] | 204 | 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 | *******************************************************************************/ |
| 220 | bool 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 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 240 | /******************************************************************************* |
| 241 | ** |
Suraj Uday Kotharkar | 2a0ee9c | 2018-04-27 03:48:23 +0530 | [diff] [blame^] | 242 | ** 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 | *******************************************************************************/ |
| 251 | void 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 | ** |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 296 | ** Function: notifyTransactionListenersOfAid |
| 297 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 298 | ** Description: Notify the NFC service about a transaction event from |
| 299 | ** secure element. |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 300 | ** aid: Buffer contains application ID. |
| 301 | ** aidLen: Length of application ID. |
| 302 | ** |
| 303 | ** Returns: None |
| 304 | ** |
| 305 | *******************************************************************************/ |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 306 | void SecureElement::notifyTransactionListenersOfAid(const uint8_t* aidBuffer, |
| 307 | uint8_t aidBufferLen, |
| 308 | const uint8_t* dataBuffer, |
| 309 | uint32_t dataBufferLen, |
| 310 | uint32_t evtSrc) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 311 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 312 | static const char fn [] = "SecureElement::notifyTransactionListenersOfAid"; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 313 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 314 | if (aidBufferLen == 0) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 318 | JNIEnv* e = NULL; |
| 319 | ScopedAttach attach(mNativeData->vm, &e); |
| 320 | if (e == NULL) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 321 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 322 | LOG(ERROR) << StringPrintf("%s: jni env is null", fn); |
| 323 | return; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 324 | } |
| 325 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 326 | const uint16_t tlvMaxLen = aidBufferLen + 10; |
| 327 | uint8_t* tlv = new uint8_t [tlvMaxLen]; |
| 328 | if (tlv == NULL) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 329 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 330 | LOG(ERROR) << StringPrintf("%s: fail allocate tlv", fn); |
| 331 | return; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 332 | } |
| 333 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 334 | memcpy (tlv, aidBuffer, aidBufferLen); |
| 335 | uint16_t tlvActualLen = aidBufferLen; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 336 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 337 | ScopedLocalRef<jobject> tlvJavaArray(e, e->NewByteArray(tlvActualLen)); |
| 338 | if (tlvJavaArray.get() == NULL) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 339 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 340 | LOG(ERROR) << StringPrintf("%s: fail allocate array", fn); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 341 | goto TheEnd; |
| 342 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 343 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 344 | e->SetByteArrayRegion ((jbyteArray)tlvJavaArray.get(), 0, tlvActualLen, (jbyte *)tlv); |
| 345 | if (e->ExceptionCheck()) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 346 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 347 | e->ExceptionClear(); |
| 348 | LOG(ERROR) << StringPrintf("%s: fail fill array", fn); |
| 349 | goto TheEnd; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 350 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 351 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 352 | if(dataBufferLen > 0) |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 353 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 354 | const uint32_t dataTlvMaxLen = dataBufferLen + 10; |
| 355 | uint8_t* datatlv = new uint8_t [dataTlvMaxLen]; |
| 356 | if (datatlv == NULL) |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 357 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 358 | 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); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 398 | goto TheEnd; |
| 399 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 400 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 401 | TheEnd: |
| 402 | delete [] tlv; |
| 403 | LOG(INFO) << StringPrintf("%s: exit", fn); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 404 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 405 | } |
| 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 | *******************************************************************************/ |
| 419 | int 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 | } |
| 485 | TheEnd: |
| 486 | LOG(INFO) << StringPrintf("decoded_length = %d", decoded_length); |
| 487 | |
| 488 | return decoded_length; |
| 489 | } |
Suraj Uday Kotharkar | 2a0ee9c | 2018-04-27 03:48:23 +0530 | [diff] [blame^] | 490 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
| 500 | void 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); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 505 | |
Suraj Uday Kotharkar | 2a0ee9c | 2018-04-27 03:48:23 +0530 | [diff] [blame^] | 506 | |
| 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 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 526 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
| 537 | void 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) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 544 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 545 | case NFA_HCI_REGISTER_EVT: |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 546 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 547 | 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; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 554 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 555 | 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; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 741 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 742 | 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]; |
nxpandroid | ecca011 | 2018-06-06 14:41:40 +0530 | [diff] [blame] | 755 | |
| 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); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 765 | } |
| 766 | else |
| 767 | { |
| 768 | LOG(ERROR) << StringPrintf("Event data TLV length encoding Unsupported!"); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 772 | else if (eventData->rcvd_evt.evt_code == NFA_HCI_EVT_CONNECTIVITY) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 773 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 774 | LOG(INFO) << StringPrintf("%s: NFA_HCI_EVENT_RCVD_EVT; NFA_HCI_EVT_CONNECTIVITY", fn); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 775 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 776 | else |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 777 | { |
| 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; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 796 | } |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 797 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 798 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 799 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 814 | bool SecureElement::transceive (uint8_t* xmitBuffer, int32_t xmitBufferSize, uint8_t* recvBuffer, |
| 815 | int32_t recvBufferMaxSize, int32_t& recvBufferActualSize, int32_t timeoutMillisec) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 816 | { |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 817 | |
| 818 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 819 | static const char fn [] = "SecureElement::transceive"; |
| 820 | tNFA_STATUS nfaStat = NFA_STATUS_FAILED; |
| 821 | bool isSuccess = false; |
| 822 | mTransceiveWaitOk = false; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 823 | uint8_t newSelectCmd[NCI_MAX_AID_LEN + 10]; |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 824 | isSuccess = false; |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 825 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 826 | |
| 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 |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 838 | && (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 |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 843 | { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 844 | uint8_t idx = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 845 | |
| 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 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 865 | LOG(INFO) << StringPrintf("%s: Empty AID SELECT cmd detected, substituting AID from config file, new length=%d", fn, idx); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 866 | } |
| 867 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 868 | { |
| 869 | SyncEventGuard guard (mTransceiveEvent); |
| 870 | mActualResponseSize = 0; |
| 871 | memset (mResponseData, 0, sizeof(mResponseData)); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 872 | nfaStat = NFA_HciSendApdu (mNfaHciHandle, mActiveEeHandle, xmitBufferSize, xmitBuffer, sizeof(mResponseData), mResponseData, timeoutMillisec); |
nxpandroid | eb52db4 | 2017-08-14 11:29:32 +0530 | [diff] [blame] | 873 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 874 | if (nfaStat == NFA_STATUS_OK) |
| 875 | { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 876 | mTransceiveEvent.wait (); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 877 | } |
| 878 | else |
| 879 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 880 | LOG(ERROR) << StringPrintf("%s: fail send data; error=0x%X", fn, nfaStat); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 881 | goto TheEnd; |
| 882 | } |
| 883 | } |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 884 | if (mActualResponseSize > recvBufferMaxSize) |
| 885 | recvBufferActualSize = recvBufferMaxSize; |
| 886 | else |
| 887 | recvBufferActualSize = mActualResponseSize; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 888 | |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 889 | memcpy (recvBuffer, mResponseData, recvBufferActualSize); |
nxpandroid | 9b94e6e | 2017-09-14 12:19:16 +0530 | [diff] [blame] | 890 | isSuccess = true; |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 891 | TheEnd: |
nxpandroid | 9b94e6e | 2017-09-14 12:19:16 +0530 | [diff] [blame] | 892 | return (isSuccess); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 893 | } |
| 894 | /******************************************************************************* |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 895 | ** |
Ayush | 73d6891 | 2018-04-04 21:36:26 +0530 | [diff] [blame] | 896 | ** 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 | *******************************************************************************/ |
| 904 | jintArray 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 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 947 | ** Function: activate |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 948 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 949 | ** Description: Turn on the secure element. |
| 950 | ** seID: ID of secure element; 0xF3 or 0xF4. |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 951 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 952 | ** Returns: True if ok. |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 953 | ** |
| 954 | *******************************************************************************/ |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 955 | bool SecureElement::activate (jint seID) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 956 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 957 | static const char fn [] = "SecureElement::activate"; |
| 958 | tNFA_STATUS nfaStat = NFA_STATUS_FAILED; |
| 959 | int numActivatedEe = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 960 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 961 | 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()) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 967 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 968 | LOG(ERROR) << StringPrintf("%s: no EE info", fn); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 969 | return false; |
| 970 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 971 | if(SecureElement::getInstance().getGateAndPipeList() != SMX_PIPE_ID) |
| 972 | return false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 973 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 974 | //activate every discovered secure element |
| 975 | for (int index=0; index < mActualNumEe; index++) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 976 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 977 | tNFA_EE_INFO& eeItem = mEeInfo[index]; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 978 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 979 | if (eeItem.ee_handle == EE_HANDLE_0xF3) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 980 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 981 | if (eeItem.ee_status != NFC_NFCEE_STATUS_INACTIVE) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 982 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 983 | LOG(INFO) << StringPrintf("%s: h=0x%X already activated", fn, eeItem.ee_handle); |
| 984 | numActivatedEe++; |
| 985 | continue; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 986 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 987 | |
nxpandroid | 9b94e6e | 2017-09-14 12:19:16 +0530 | [diff] [blame] | 988 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 989 | 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) |
nxpandroid | f68f012 | 2017-08-14 11:31:59 +0530 | [diff] [blame] | 991 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 992 | if (eeItem.ee_status == NFC_NFCEE_STATUS_ACTIVE) |
| 993 | numActivatedEe++; |
| 994 | if(eeItem.ee_handle == EE_HANDLE_0xF3) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 995 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 996 | 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 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1001 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1002 | } |
| 1003 | else |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1004 | LOG(ERROR) << StringPrintf("%s: NFA_EeModeSet failed; error=0x%X", fn, nfaStat); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1005 | } |
| 1006 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1007 | } //for |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1008 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1009 | mActiveEeHandle = getActiveEeHandle(handle); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1010 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1011 | if (mActiveEeHandle == NFA_HANDLE_INVALID) |
| 1012 | LOG(ERROR) << StringPrintf("%s: ee handle not found", fn); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1013 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1014 | LOG(INFO) << StringPrintf("%s: exit; active ee h=0x%X", fn, mActiveEeHandle); |
| 1015 | return mActiveEeHandle != NFA_HANDLE_INVALID; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1016 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1017 | /******************************************************************************* |
| 1018 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1019 | ** Function: deactivate |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1020 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1021 | ** Description: Turn off the secure element. |
| 1022 | ** seID: ID of secure element; 0xF3 or 0xF4. |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1023 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1024 | ** Returns: True if ok. |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1025 | ** |
| 1026 | *******************************************************************************/ |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1027 | bool SecureElement::deactivate (jint seID) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1028 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1029 | static const char fn [] = "SecureElement::deactivate"; |
| 1030 | tNFA_STATUS nfaStat = NFA_STATUS_FAILED; |
| 1031 | bool retval = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1032 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1033 | 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 | |
| 1086 | TheEnd: |
| 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 | *******************************************************************************/ |
| 1099 | tNFA_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 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1113 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
| 1123 | jint SecureElement::getSETechnology(tNFA_HANDLE eeHandle) |
| 1124 | { |
| 1125 | int tech_mask = 0x00; |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1126 | //static const char fn [] = "SecureElement::getSETechnology"; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1127 | // Get Fresh EE info. |
| 1128 | if (! getEeInfo()) |
| 1129 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1130 | //ALOGE("%s: No updated eeInfo available", fn); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1131 | } |
| 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 | /******************************************************************************* |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1155 | ** |
| 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 | *******************************************************************************/ |
| 1163 | void SecureElement::notifyModeSet (tNFA_HANDLE eeHandle, bool success, tNFA_EE_STATUS eeStatus) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1164 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1165 | static const char* fn = "SecureElement::notifyModeSet"; |
| 1166 | if (success) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1167 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1168 | tNFA_EE_INFO *pEE = sSecElem.findEeByHandle (eeHandle); |
| 1169 | if (pEE) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1170 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1171 | pEE->ee_status = eeStatus; |
| 1172 | LOG(INFO) << StringPrintf("%s: NFA_EE_MODE_SET_EVT; (0x%04x)", fn, pEE->ee_status); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1173 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1174 | else |
| 1175 | LOG(INFO) << StringPrintf("%s: NFA_EE_MODE_SET_EVT; EE: 0x%04x not found. mActiveEeHandle: 0x%04x", fn, eeHandle, sSecElem.mActiveEeHandle); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1176 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1177 | SyncEventGuard guard (sSecElem.mEeSetModeEvent); |
| 1178 | sSecElem.mEeSetModeEvent.notifyOne(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1179 | } |
| 1180 | /******************************************************************************* |
| 1181 | ** |
| 1182 | ** Function: getAtr |
| 1183 | ** |
| 1184 | ** Description: GetAtr response from the connected eSE |
| 1185 | ** |
| 1186 | ** Returns: Returns True if success |
| 1187 | ** |
| 1188 | *******************************************************************************/ |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1189 | bool SecureElement::getAtr(jint seID, uint8_t* recvBuffer, int32_t *recvBufferSize) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1190 | { |
| 1191 | static const char fn[] = "SecureElement::getAtr"; |
| 1192 | tNFA_STATUS nfaStat = NFA_STATUS_FAILED; |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 1193 | int timeoutMillisec = 10000; |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1194 | |
| 1195 | LOG(INFO) << StringPrintf("%s: enter; seID=0x%X", fn, seID); |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 1196 | if(nfcFL.nfcNxpEse) { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1197 | /*ETSI 12 Gate Info ATR */ |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 1198 | 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); |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 1203 | { |
| 1204 | mAbortEvent.wait(); |
| 1205 | } |
| 1206 | if(mAbortEventWaitOk == false) |
| 1207 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1208 | LOG(INFO) << StringPrintf("%s (EVT_ABORT)Wait reposne timeout", fn); |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 1209 | nfaStat = NFA_STATUS_FAILED; |
| 1210 | } |
| 1211 | else |
| 1212 | { |
| 1213 | *recvBufferSize = mAtrInfolen; |
| 1214 | memcpy(recvBuffer, mAtrInfo, mAtrInfolen); |
| 1215 | } |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 1216 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1217 | |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 1218 | return (nfaStat == NFA_STATUS_OK)?true:false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1219 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1220 | /******************************************************************************* |
| 1221 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1222 | ** Function: SecEle_Modeset |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1223 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1224 | ** Description: reSet NFCEE. |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1225 | ** |
| 1226 | ** Returns: True if ok. |
| 1227 | ** |
| 1228 | *******************************************************************************/ |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1229 | bool SecureElement::SecEle_Modeset(uint8_t type) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1230 | { |
| 1231 | tNFA_STATUS nfaStat = NFA_STATUS_FAILED; |
| 1232 | bool retval = true; |
| 1233 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1234 | LOG(INFO) << StringPrintf("set EE mode = 0x%X", type); |
| 1235 | nfaStat = SecElem_EeModeSet (EE_HANDLE_0xF3, type); |
| 1236 | if ( nfaStat == NFA_STATUS_OK) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1237 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1238 | LOG(INFO) << StringPrintf("SecEle_Modeset: Success"); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1239 | } |
| 1240 | else |
| 1241 | { |
| 1242 | retval = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1243 | } |
| 1244 | return retval; |
| 1245 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1246 | /******************************************************************************* |
| 1247 | ** |
| 1248 | ** Function: initializeEeHandle |
| 1249 | ** |
| 1250 | ** Description: Set NFCEE handle. |
| 1251 | ** |
| 1252 | ** Returns: True if ok. |
| 1253 | ** |
| 1254 | *******************************************************************************/ |
| 1255 | bool 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 | } |
nxpandroid | 1680a6d | 2017-01-13 19:13:14 +0530 | [diff] [blame] | 1263 | /******************************************************************************* |
| 1264 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1265 | ** Function: getEeInfo |
nxpandroid | 1680a6d | 2017-01-13 19:13:14 +0530 | [diff] [blame] | 1266 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1267 | ** Description: Get latest information about execution environments from stack. |
nxpandroid | 1680a6d | 2017-01-13 19:13:14 +0530 | [diff] [blame] | 1268 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1269 | ** Returns: True if at least 1 EE is available. |
nxpandroid | 1680a6d | 2017-01-13 19:13:14 +0530 | [diff] [blame] | 1270 | ** |
| 1271 | *******************************************************************************/ |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1272 | bool SecureElement::getEeInfo() |
nxpandroid | 1680a6d | 2017-01-13 19:13:14 +0530 | [diff] [blame] | 1273 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1274 | static const char fn [] = "SecureElement::getEeInfo"; |
| 1275 | LOG(INFO) << StringPrintf("%s: enter; mbNewEE=%d, mActualNumEe=%d", fn, mbNewEE, mActualNumEe); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 1276 | tNFA_STATUS nfaStat = NFA_STATUS_FAILED; |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 1277 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1278 | /*Reading latest eEinfo incase it is updated*/ |
| 1279 | mbNewEE = true; |
| 1280 | mNumEePresent = 0; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 1281 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1282 | 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) |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 1289 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1290 | LOG(ERROR) << StringPrintf("%s: fail get info; error=0x%X", fn, nfaStat); |
| 1291 | mActualNumEe = 0; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 1292 | } |
| 1293 | else |
| 1294 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1295 | mbNewEE = false; |
| 1296 | |
| 1297 | LOG(ERROR) << StringPrintf("%s: num EEs discovered: %u", fn, mActualNumEe); |
| 1298 | if (mActualNumEe != 0) |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 1299 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1300 | for (uint8_t xx = 0; xx < mActualNumEe; xx++) |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 1301 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1302 | 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; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1311 | LOG(INFO) << StringPrintf("%s: exit; mActualNumEe=%d, mNumEePresent=%d", fn, mActualNumEe,mNumEePresent); |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1312 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1313 | mNfceeData_t.mNfceePresent = mNumEePresent; |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1314 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1315 | return (mActualNumEe != 0); |
ubuntu | 63527d2 | 2017-10-11 14:27:02 +0530 | [diff] [blame] | 1316 | } |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1317 | /******************************************************************************* |
| 1318 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1319 | ** Function: findEeByHandle |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1320 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1321 | ** Description: Find information about an execution environment. |
| 1322 | ** eeHandle: Handle to execution environment. |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1323 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1324 | ** Returns: Information about an execution environment. |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1325 | ** |
| 1326 | *******************************************************************************/ |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1327 | tNFA_EE_INFO *SecureElement::findEeByHandle (tNFA_HANDLE eeHandle) |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1328 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1329 | for (uint8_t xx = 0; xx < mActualNumEe; xx++) |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1330 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1331 | 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 | *******************************************************************************/ |
| 1346 | tNFA_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); |
nxpandroid | 5c5b215 | 2017-09-14 12:30:17 +0530 | [diff] [blame] | 1351 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1352 | //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 | *******************************************************************************/ |
| 1389 | tNFA_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) |
nxpandroid | 5c5b215 | 2017-09-14 12:30:17 +0530 | [diff] [blame] | 1397 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1398 | return (mEeInfo[xx].ee_handle); |
nxpandroid | 5c5b215 | 2017-09-14 12:30:17 +0530 | [diff] [blame] | 1399 | } |
nxpandroid | 5c5b215 | 2017-09-14 12:30:17 +0530 | [diff] [blame] | 1400 | |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1401 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1402 | return NFA_HANDLE_INVALID; |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1403 | } |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1404 | /******************************************************************************* |
| 1405 | ** |
| 1406 | ** Function setNfccPwrConfig |
| 1407 | ** |
| 1408 | ** Description sends the link cntrl command to eSE with the value passed |
| 1409 | ** |
| 1410 | ** Returns status |
| 1411 | ** |
| 1412 | *******************************************************************************/ |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1413 | tNFA_STATUS SecureElement::setNfccPwrConfig(uint8_t value) |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1414 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1415 | static const char fn [] = "SecureElement::setNfccPwrConfig()"; |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1416 | tNFA_STATUS nfaStat = NFA_STATUS_FAILED; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1417 | static uint8_t cur_value = 0xFF; |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1418 | 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); |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1425 | return mPwrCmdstatus; |
| 1426 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1427 | bool 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); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1432 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1433 | 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 | *******************************************************************************/ |
| 1449 | void 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 | *******************************************************************************/ |
| 1481 | uint8_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 | ** |
nxpandroid | ecca011 | 2018-06-06 14:41:40 +0530 | [diff] [blame] | 1516 | ** Function getLastRfFiledToggleTime |
| 1517 | ** |
| 1518 | ** Description Provides the last RF filed toggile timer |
| 1519 | ** |
| 1520 | ** Returns timespec |
| 1521 | ** |
| 1522 | *******************************************************************************/ |
| 1523 | struct timespec SecureElement::getLastRfFiledToggleTime(void) |
| 1524 | { |
| 1525 | return mLastRfFieldToggle; |
| 1526 | } |
| 1527 | /******************************************************************************* |
| 1528 | ** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 1529 | ** Function: finalize |
| 1530 | ** |
| 1531 | ** Description: Release all resources. |
| 1532 | ** |
| 1533 | ** Returns: None |
| 1534 | ** |
| 1535 | *******************************************************************************/ |
| 1536 | void SecureElement::finalize() { |
| 1537 | mIsInit = false; |
| 1538 | mNativeData = NULL; |
| 1539 | } |