nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | /****************************************************************************** |
| 17 | * |
| 18 | * The original Work has been changed by NXP Semiconductors. |
| 19 | * |
| 20 | * Copyright (C) 2015 NXP Semiconductors |
| 21 | * |
| 22 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 23 | * you may not use this file except in compliance with the License. |
| 24 | * You may obtain a copy of the License at |
| 25 | * |
| 26 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 27 | * |
| 28 | * Unless required by applicable law or agreed to in writing, software |
| 29 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 30 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 31 | * See the License for the specific language governing permissions and |
| 32 | * limitations under the License. |
| 33 | * |
| 34 | ******************************************************************************/ |
| 35 | #include "OverrideLog.h" |
| 36 | #include "SecureElement.h" |
| 37 | #include "JavaClassConstants.h" |
| 38 | #include "PowerSwitch.h" |
| 39 | #include "NfcTag.h" |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 40 | #include "RoutingManager.h" |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 41 | #include <ScopedPrimitiveArray.h> |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 42 | #include "phNxpConfig.h" |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 43 | |
| 44 | extern bool hold_the_transceive; |
| 45 | extern int dual_mode_current_state; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 46 | #if((NFC_NXP_ESE == TRUE)&&(CONCURRENCY_PROTECTION == TRUE)) |
| 47 | extern bool ceTransactionPending; |
| 48 | #endif |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 49 | namespace android |
| 50 | { |
| 51 | |
| 52 | extern void startRfDiscovery (bool isStart); |
| 53 | extern bool isDiscoveryStarted(); |
| 54 | |
| 55 | extern void com_android_nfc_NfcManager_disableDiscovery (JNIEnv* e, jobject o); |
| 56 | extern void com_android_nfc_NfcManager_enableDiscovery (JNIEnv* e, jobject o, jint mode); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 57 | static SyncEvent sNfaVSCResponseEvent; |
| 58 | //static bool sRfEnabled; /*commented to eliminate warning defined but not used*/ |
| 59 | |
| 60 | static void nfaVSCCallback(UINT8 event, UINT16 param_len, UINT8 *p_param); |
| 61 | |
| 62 | inline static void nfaVSCCallback(UINT8 event, UINT16 param_len, UINT8 *p_param) /*defined as inline to eliminate warning defined but not used*/ |
| 63 | { |
| 64 | (void)event; |
| 65 | (void)param_len; |
| 66 | (void)p_param; |
| 67 | SyncEventGuard guard (sNfaVSCResponseEvent); |
| 68 | sNfaVSCResponseEvent.notifyOne (); |
| 69 | } |
| 70 | |
| 71 | // These must match the EE_ERROR_ types in NfcService.java |
| 72 | static const int EE_ERROR_IO = -1; |
| 73 | static const int EE_ERROR_ALREADY_OPEN = -2; |
| 74 | static const int EE_ERROR_INIT = -3; |
| 75 | static const int EE_ERROR_LISTEN_MODE = -4; |
| 76 | static const int EE_ERROR_EXT_FIELD = -5; |
| 77 | static const int EE_ERROR_NFC_DISABLED = -6; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 78 | bool is_wired_mode_open = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 79 | /******************************************************************************* |
| 80 | ** |
| 81 | ** Function: nativeNfcSecureElement_doOpenSecureElementConnection |
| 82 | ** |
| 83 | ** Description: Connect to the secure element. |
| 84 | ** e: JVM environment. |
| 85 | ** o: Java object. |
| 86 | ** |
| 87 | ** Returns: Handle of secure element. values < 0 represent failure. |
| 88 | ** |
| 89 | *******************************************************************************/ |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 90 | #if (NXP_EXTNS == TRUE) |
| 91 | static jint nativeNfcSecureElement_doOpenSecureElementConnection (JNIEnv*, jobject,jint seId) |
| 92 | #else |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 93 | static jint nativeNfcSecureElement_doOpenSecureElementConnection (JNIEnv*, jobject) |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 94 | #endif |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 95 | { |
| 96 | ALOGD("%s: enter", __FUNCTION__); |
| 97 | bool stat = false; |
| 98 | jint secElemHandle = EE_ERROR_INIT; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 99 | #if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE)) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 100 | long ret_val = -1; |
| 101 | NFCSTATUS status = NFCSTATUS_FAILED; |
| 102 | p61_access_state_t p61_current_state = P61_STATE_INVALID; |
| 103 | #endif |
| 104 | SecureElement &se = SecureElement::getInstance(); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 105 | #if ((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE)) |
| 106 | #if(NXP_ESE_WIRED_MODE_PRIO != TRUE) |
| 107 | if(se.isBusy()) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 108 | goto TheEnd; |
| 109 | } |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 110 | #endif |
| 111 | se.mIsExclusiveWiredMode = false; // to ctlr exclusive wired mode |
| 112 | if(seId == 0xF4) |
| 113 | { |
| 114 | if(se.mIsWiredModeOpen) |
| 115 | { |
| 116 | goto TheEnd; |
| 117 | } |
| 118 | #if (NXP_ESE_UICC_EXCLUSIVE_WIRED_MODE == true) |
| 119 | se.mIsExclusiveWiredMode = true; |
| 120 | #endif |
| 121 | stat = se.checkForWiredModeAccess(); |
| 122 | if(stat == false) |
| 123 | { |
| 124 | ALOGD("Denying SE open due to SE listen mode active"); |
| 125 | secElemHandle = EE_ERROR_LISTEN_MODE; |
| 126 | goto TheEnd; |
| 127 | } |
| 128 | |
| 129 | ALOGD("%s: Activating UICC Wired Mode=0x%X", __FUNCTION__, seId); |
| 130 | stat = se.activate(seId); |
| 131 | ALOGD("%s: Check UICC activation status stat=%X", __FUNCTION__, stat); |
| 132 | if (stat) |
| 133 | { |
| 134 | //establish a pipe to UICC |
| 135 | ALOGD("%s: Creatting a pipe to UICC!", __FUNCTION__); |
| 136 | stat = se.connectEE(); |
| 137 | if (stat) |
| 138 | { |
| 139 | secElemHandle = se.mActiveEeHandle; |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | se.deactivate (0); |
| 144 | } |
| 145 | } |
| 146 | if ((!stat) && (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED))) |
| 147 | { |
| 148 | PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER); |
| 149 | } |
| 150 | se.mIsWiredModeOpen = true; |
| 151 | #if(NXP_ESE_UICC_EXCLUSIVE_WIRED_MODE == TRUE) |
| 152 | if (isDiscoveryStarted()) |
| 153 | { |
| 154 | // Stop RF Discovery if we were polling |
| 155 | startRfDiscovery (false); |
| 156 | status = NFA_DisableListening(); |
| 157 | if(status == NFCSTATUS_OK) |
| 158 | { |
| 159 | startRfDiscovery (true); |
| 160 | } |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | status = NFA_DisableListening(); |
| 165 | } |
| 166 | se.mlistenDisabled = true; |
| 167 | #endif |
| 168 | goto TheEnd; |
| 169 | } |
| 170 | #if(NXP_ESE_WIRED_MODE_PRIO == TRUE) |
| 171 | if((se.mIsWiredModeOpen)&&(se.mActiveEeHandle == 0x402)) |
| 172 | { |
| 173 | stat = SecureElement::getInstance().disconnectEE (se.mActiveEeHandle); |
| 174 | se.mActiveEeHandle = NFA_HANDLE_INVALID; |
| 175 | se.mIsWiredModeOpen = false; |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | #if(NFC_NXP_CHIP_TYPE != PN547C2) |
| 180 | #if (JCOP_WA_ENABLE == TRUE) |
| 181 | if((RoutingManager::getInstance().is_ee_recovery_ongoing())) |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 182 | { |
| 183 | ALOGD ("ee recovery ongoing!!!"); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 184 | SyncEventGuard guard (SecureElement::getInstance().mEEdatapacketEvent); |
| 185 | SecureElement::getInstance().mEEdatapacketEvent.wait(); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 186 | } |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 187 | #endif |
| 188 | #if(NXP_ESE_DUAL_MODE_PRIO_SCHEME == NXP_ESE_EXCLUSIVE_WIRED_MODE) |
| 189 | se.mIsExclusiveWiredMode = true; |
| 190 | #endif |
| 191 | stat = se.checkForWiredModeAccess(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 192 | if(stat == false) |
| 193 | { |
| 194 | ALOGD("Denying SE open due to SE listen mode active"); |
| 195 | secElemHandle = EE_ERROR_LISTEN_MODE; |
| 196 | goto TheEnd; |
| 197 | } |
| 198 | #else |
| 199 | if (se.isActivatedInListenMode()) { |
| 200 | ALOGD("Denying SE open due to SE listen mode active"); |
| 201 | secElemHandle = EE_ERROR_LISTEN_MODE; |
| 202 | goto TheEnd; |
| 203 | } |
| 204 | |
| 205 | if (se.isRfFieldOn()) { |
| 206 | ALOGD("Denying SE open due to SE in active RF field"); |
| 207 | secElemHandle = EE_ERROR_EXT_FIELD; |
| 208 | goto TheEnd; |
| 209 | } |
| 210 | #endif |
| 211 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 212 | ret_val = NFC_GetP61Status ((void *)&p61_current_state); |
| 213 | if (ret_val < 0) |
| 214 | { |
| 215 | ALOGD("NFC_GetP61Status failed"); |
| 216 | goto TheEnd; |
| 217 | } |
| 218 | ALOGD("P61 Status is: %x", p61_current_state); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 219 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 220 | #if(NFC_NXP_ESE_VER == JCOP_VER_3_1) |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 221 | if (!(p61_current_state & P61_STATE_SPI) && !(p61_current_state & P61_STATE_SPI_PRIO)) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 222 | { |
| 223 | #endif |
| 224 | if(p61_current_state & (P61_STATE_SPI)||(p61_current_state & (P61_STATE_SPI_PRIO))) |
| 225 | { |
| 226 | dual_mode_current_state |= SPI_ON; |
| 227 | } |
| 228 | if(p61_current_state & (P61_STATE_SPI_PRIO)) |
| 229 | { |
| 230 | hold_the_transceive = true; |
| 231 | } |
| 232 | |
| 233 | secElemHandle = NFC_ReqWiredAccess ((void *)&status); |
| 234 | if (secElemHandle < 0) |
| 235 | { |
| 236 | ALOGD("Denying SE open due to NFC_ReqWiredAccess failed"); |
| 237 | goto TheEnd; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | if (status != NFCSTATUS_SUCCESS) |
| 242 | { |
| 243 | ALOGD("Denying SE open due to SE is being used by SPI"); |
| 244 | secElemHandle = EE_ERROR_IO; |
| 245 | goto TheEnd; |
| 246 | } |
| 247 | else |
| 248 | { |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 249 | se.mIsWiredModeOpen = true; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 250 | ALOGD("SE Access granted"); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 251 | #if(NXP_ESE_DUAL_MODE_PRIO_SCHEME == NXP_ESE_EXCLUSIVE_WIRED_MODE) |
| 252 | if (isDiscoveryStarted()) |
| 253 | { |
| 254 | // Stop RF Discovery if we were polling |
| 255 | startRfDiscovery (false); |
| 256 | status = NFA_DisableListening(); |
| 257 | if(status == NFCSTATUS_OK) |
| 258 | { |
| 259 | startRfDiscovery (true); |
| 260 | } |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | status = NFA_DisableListening(); |
| 265 | } |
| 266 | se.mlistenDisabled = true; |
| 267 | #endif |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | #if(NFC_NXP_ESE_VER == JCOP_VER_3_1) |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | ALOGD("Denying SE open because SPI is already open"); |
| 275 | goto TheEnd; |
| 276 | |
| 277 | } |
| 278 | #endif |
| 279 | #endif |
| 280 | //tell the controller to power up to get ready for sec elem operations |
| 281 | PowerSwitch::getInstance ().setLevel (PowerSwitch::FULL_POWER); |
| 282 | PowerSwitch::getInstance ().setModeOn (PowerSwitch::SE_CONNECTED); |
| 283 | #if 0 |
| 284 | { |
| 285 | |
| 286 | sRfEnabled = isDiscoveryStarted(); |
| 287 | if (sRfEnabled) { |
| 288 | // Stop RF Discovery if we were polling |
| 289 | startRfDiscovery (false); |
| 290 | } |
| 291 | |
| 292 | UINT8 param[] = {0x00}; //Disable standby |
| 293 | SyncEventGuard guard (sNfaVSCResponseEvent); |
| 294 | tNFA_STATUS stat = NFA_SendVsCommand (0x00,0x01,param,nfaVSCCallback); |
| 295 | if(NFA_STATUS_OK == stat) |
| 296 | { |
| 297 | sNfaVSCResponseEvent.wait(); //wait for NFA VS command to finish |
| 298 | |
| 299 | } |
| 300 | |
| 301 | startRfDiscovery (true); |
| 302 | } |
| 303 | #endif |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 304 | #if(NXP_EXTNS == TRUE) && (NFC_NXP_ESE == TRUE) |
| 305 | if(!(p61_current_state & (P61_STATE_SPI | P61_STATE_SPI_PRIO))) |
| 306 | stat = se.SecEle_Modeset(0x01); //Workaround |
| 307 | usleep(150000); /*provide enough delay if NFCC enter in recovery*/ |
| 308 | #endif |
| 309 | stat = se.activate(SecureElement::ESE_ID); // It is to get the current activated handle. |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 310 | |
| 311 | if (stat) |
| 312 | { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 313 | //establish a pipe to sec elem |
| 314 | stat = se.connectEE(); |
| 315 | if (stat) |
| 316 | { |
| 317 | secElemHandle = se.mActiveEeHandle; |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | se.deactivate (0); |
| 322 | } |
| 323 | } |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 324 | #if((NFC_NXP_ESE == TRUE)&&(CONCURRENCY_PROTECTION == TRUE)) |
| 325 | if(is_wired_mode_open) |
| 326 | { |
| 327 | se.enablePassiveListen(0x00); |
| 328 | se.meseUiccConcurrentAccess = true; |
| 329 | } |
| 330 | #endif |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 331 | //if code fails to connect to the secure element, and nothing is active, then |
| 332 | //tell the controller to power down |
| 333 | if ((!stat) && (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED))) |
| 334 | { |
| 335 | PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER); |
| 336 | } |
| 337 | |
| 338 | TheEnd: |
| 339 | ALOGD("%s: exit; return handle=0x%X", __FUNCTION__, secElemHandle); |
| 340 | return secElemHandle; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /******************************************************************************* |
| 345 | ** |
| 346 | ** Function: nativeNfcSecureElement_doDisconnectSecureElementConnection |
| 347 | ** |
| 348 | ** Description: Disconnect from the secure element. |
| 349 | ** e: JVM environment. |
| 350 | ** o: Java object. |
| 351 | ** handle: Handle of secure element. |
| 352 | ** |
| 353 | ** Returns: True if ok. |
| 354 | ** |
| 355 | *******************************************************************************/ |
| 356 | static jboolean nativeNfcSecureElement_doDisconnectSecureElementConnection (JNIEnv*, jobject, jint handle) |
| 357 | { |
| 358 | ALOGD("%s: enter; handle=0x%04x", __FUNCTION__, handle); |
| 359 | bool stat = false; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 360 | #if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE)) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 361 | long ret_val = -1; |
| 362 | NFCSTATUS status = NFCSTATUS_FAILED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 363 | |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 364 | SecureElement &se = SecureElement::getInstance(); |
| 365 | se.NfccStandByOperation(STANDBY_TIMER_STOP); |
| 366 | #endif |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 367 | |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 368 | #if(NXP_EXTNS == TRUE) |
| 369 | if(handle == 0x402) |
| 370 | { |
| 371 | stat = SecureElement::getInstance().disconnectEE (handle); |
| 372 | #if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE)) |
| 373 | se.mIsWiredModeOpen = false; |
| 374 | #endif |
| 375 | #if( (NXP_ESE_UICC_EXCLUSIVE_WIRED_MODE == TRUE) && (NFC_NXP_ESE == TRUE) && (NXP_EXTNS == TRUE) ) |
| 376 | se.mIsExclusiveWiredMode = false; |
| 377 | if(se.mlistenDisabled) |
| 378 | { |
| 379 | if (isDiscoveryStarted()) |
| 380 | { |
| 381 | // Stop RF Discovery if we were polling |
| 382 | startRfDiscovery (false); |
| 383 | status = NFA_EnableListening(); |
| 384 | startRfDiscovery (true); |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | status = NFA_EnableListening(); |
| 389 | } |
| 390 | se.mlistenDisabled = false; |
| 391 | } |
| 392 | #endif |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 393 | goto TheEnd; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 394 | } |
| 395 | #endif |
| 396 | |
| 397 | #if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE)) |
| 398 | //Send the EVT_END_OF_APDU_TRANSFER event at the end of wired mode session. |
| 399 | se.NfccStandByOperation(STANDBY_MODE_ON); |
| 400 | #endif |
| 401 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 402 | #if 0 |
| 403 | { |
| 404 | sRfEnabled = isDiscoveryStarted(); |
| 405 | if (sRfEnabled) { |
| 406 | // Stop RF Discovery if we were polling |
| 407 | startRfDiscovery (false); |
| 408 | } |
| 409 | |
| 410 | UINT8 param[] = {0x01};//Enable standby |
| 411 | SyncEventGuard guard (sNfaVSCResponseEvent); |
| 412 | tNFA_STATUS stat = NFA_SendVsCommand (0x00,0x01,param,nfaVSCCallback); |
| 413 | if(NFA_STATUS_OK == stat) |
| 414 | { |
| 415 | sNfaVSCResponseEvent.wait(); //wait for NFA VS command to finish |
| 416 | |
| 417 | } |
| 418 | |
| 419 | startRfDiscovery (true); |
| 420 | } |
| 421 | #endif |
| 422 | stat = SecureElement::getInstance().disconnectEE (handle); |
| 423 | |
| 424 | //if controller is not routing AND there is no pipe connected, |
| 425 | //then turn off the sec elem |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 426 | #if(NFC_NXP_ESE == TRUE) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 427 | //Do Nothing |
| 428 | #else |
| 429 | if (! SecureElement::getInstance().isBusy()) |
| 430 | SecureElement::getInstance().deactivate (handle); |
| 431 | #endif |
| 432 | //if nothing is active after this, then tell the controller to power down |
| 433 | if (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED)) |
| 434 | PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 435 | #if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE)) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 436 | ret_val = NFC_RelWiredAccess ((void *)&status); |
| 437 | if (ret_val < 0) |
| 438 | { |
| 439 | ALOGD("Denying SE Release due to NFC_RelWiredAccess failed"); |
| 440 | goto TheEnd; |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | if (status != NFCSTATUS_SUCCESS) |
| 445 | { |
| 446 | ALOGD("Denying SE close due to SE is not being released by Pn54x driver"); |
| 447 | stat = false; |
| 448 | } |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 449 | #if((NFC_NXP_ESE == TRUE)&&(CONCURRENCY_PROTECTION == TRUE)) |
| 450 | se.enablePassiveListen(0x01); |
| 451 | SecureElement::getInstance().mPassiveListenTimer.kill(); |
| 452 | se.meseUiccConcurrentAccess = false; |
| 453 | #endif |
| 454 | se.mIsWiredModeOpen = false; |
| 455 | #if(NXP_ESE_DUAL_MODE_PRIO_SCHEME == NXP_ESE_EXCLUSIVE_WIRED_MODE) |
| 456 | if(se.mlistenDisabled) |
| 457 | { |
| 458 | if (isDiscoveryStarted()) |
| 459 | { |
| 460 | // Stop RF Discovery if we were polling |
| 461 | startRfDiscovery (false); |
| 462 | status = NFA_EnableListening(); |
| 463 | startRfDiscovery (true); |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | status = NFA_EnableListening(); |
| 468 | } |
| 469 | se.mlistenDisabled = false; |
| 470 | } |
| 471 | #endif |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 472 | } |
| 473 | #endif |
| 474 | TheEnd: |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 475 | #if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE)) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 476 | ALOGD("%s: exit stat = %d", __FUNCTION__, stat); |
| 477 | #else |
| 478 | ALOGD("%s: exit", __FUNCTION__); |
| 479 | #endif |
| 480 | return stat ? JNI_TRUE : JNI_FALSE; |
| 481 | } |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 482 | #if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE)) |
| 483 | static int checkP61Status(void) |
| 484 | { |
| 485 | jint ret_val = -1; |
| 486 | p61_access_state_t p61_current_state = P61_STATE_INVALID; |
| 487 | ret_val = NFC_GetP61Status ((void *)&p61_current_state); |
| 488 | if (ret_val < 0) |
| 489 | { |
| 490 | ALOGD("NFC_GetP61Status failed"); |
| 491 | return -1; |
| 492 | } |
| 493 | if(p61_current_state & (P61_STATE_SPI)||(p61_current_state & (P61_STATE_SPI_PRIO))) |
| 494 | { |
| 495 | ALOGD("No gpio change"); |
| 496 | ret_val = 0; |
| 497 | } |
| 498 | else |
| 499 | { |
| 500 | ret_val = -1; |
| 501 | } |
| 502 | return ret_val; |
| 503 | } |
| 504 | #endif |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 505 | /******************************************************************************* |
| 506 | ** |
| 507 | ** Function: nativeNfcSecureElement_doResetSecureElement |
| 508 | ** |
| 509 | ** Description: Reset the secure element. |
| 510 | ** e: JVM environment. |
| 511 | ** o: Java object. |
| 512 | ** handle: Handle of secure element. |
| 513 | ** |
| 514 | ** Returns: True if ok. |
| 515 | ** |
| 516 | *******************************************************************************/ |
| 517 | static jboolean nativeNfcSecureElement_doResetSecureElement (JNIEnv*, jobject, jint handle) |
| 518 | { |
| 519 | bool stat = false; |
| 520 | #if (NFC_NXP_ESE == TRUE) |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 521 | tNFA_STATUS mstatus; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 522 | SecureElement &se = SecureElement::getInstance(); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 523 | unsigned long num = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 524 | ALOGD("%s: enter; handle=0x%04x", __FUNCTION__, handle); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 525 | if(!se.mIsWiredModeOpen) |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 526 | { |
| 527 | ALOGD("wired mode is not open"); |
| 528 | return stat; |
| 529 | } |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 530 | if (GetNxpNumValue("NXP_ESE_POWER_DH_CONTROL", &num, sizeof(num))) |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 531 | { |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 532 | ALOGD("Power schemes enabled in config file is %ld", num); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 533 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 534 | |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 535 | #if(NXP_ESE_RESET_METHOD == TRUE) |
| 536 | if((num == 2) && (se.isEtsi12ApduGatePresent())) |
| 537 | { |
| 538 | ALOGD("Power Scheme : Ext PMU"); |
| 539 | mstatus = se.SecElem_sendEvt_Abort(); |
| 540 | if(mstatus == NFA_STATUS_OK){ |
| 541 | stat = true; |
| 542 | } |
| 543 | else { |
| 544 | stat = false; |
| 545 | } |
| 546 | }else |
| 547 | #endif |
| 548 | { |
| 549 | ALOGD("Power Scheme : Int PMU/Legacy"); |
| 550 | stat = se.SecEle_Modeset(0x00); |
| 551 | if (handle == 0x4C0) |
| 552 | { |
| 553 | if(checkP61Status()) |
| 554 | se.NfccStandByOperation(STANDBY_GPIO_LOW); |
| 555 | } |
| 556 | usleep(100 * 1000); |
| 557 | if (handle == 0x4C0) |
| 558 | { |
| 559 | if(checkP61Status() && (se.mIsWiredModeOpen == true)) |
| 560 | se.NfccStandByOperation(STANDBY_GPIO_HIGH); |
| 561 | } |
| 562 | stat = se.SecEle_Modeset(0x01); |
| 563 | usleep(2000 * 1000); |
| 564 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 565 | #endif |
| 566 | ALOGD("%s: exit", __FUNCTION__); |
| 567 | return stat ? JNI_TRUE : JNI_FALSE; |
| 568 | } |
| 569 | |
| 570 | /******************************************************************************* |
| 571 | ** |
| 572 | ** Function: nativeNfcSecureElement_doGetAtr |
| 573 | ** |
| 574 | ** Description: GetAtr from the connected eSE. |
| 575 | ** e: JVM environment. |
| 576 | ** o: Java object. |
| 577 | ** handle: Handle of secure element. |
| 578 | ** |
| 579 | ** Returns: Buffer of received data. |
| 580 | ** |
| 581 | *******************************************************************************/ |
| 582 | static jbyteArray nativeNfcSecureElement_doGetAtr (JNIEnv* e, jobject, jint handle) |
| 583 | { |
| 584 | bool stat = false; |
| 585 | const INT32 recvBufferMaxSize = 1024; |
| 586 | UINT8 recvBuffer [recvBufferMaxSize]; |
| 587 | INT32 recvBufferActualSize = 0; |
| 588 | #if (NFC_NXP_ESE == TRUE) |
| 589 | ALOGD("%s: enter; handle=0x%04x", __FUNCTION__, handle); |
| 590 | |
| 591 | stat = SecureElement::getInstance().getAtr(handle, recvBuffer, &recvBufferActualSize); |
| 592 | |
| 593 | //copy results back to java |
| 594 | #endif |
| 595 | jbyteArray result = e->NewByteArray(recvBufferActualSize); |
| 596 | if (result != NULL) { |
| 597 | e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer); |
| 598 | } |
| 599 | |
| 600 | ALOGD("%s: exit: recv len=%ld", __FUNCTION__, recvBufferActualSize); |
| 601 | |
| 602 | return result; |
| 603 | } |
| 604 | |
| 605 | /******************************************************************************* |
| 606 | ** |
| 607 | ** Function: nativeNfcSecureElement_doTransceive |
| 608 | ** |
| 609 | ** Description: Send data to the secure element; retrieve response. |
| 610 | ** e: JVM environment. |
| 611 | ** o: Java object. |
| 612 | ** handle: Secure element's handle. |
| 613 | ** data: Data to send. |
| 614 | ** |
| 615 | ** Returns: Buffer of received data. |
| 616 | ** |
| 617 | *******************************************************************************/ |
| 618 | static jbyteArray nativeNfcSecureElement_doTransceive (JNIEnv* e, jobject, jint handle, jbyteArray data) |
| 619 | { |
| 620 | const INT32 recvBufferMaxSize = 0x8800;//1024; 34k |
| 621 | UINT8 recvBuffer [recvBufferMaxSize]; |
| 622 | INT32 recvBufferActualSize = 0; |
| 623 | |
| 624 | ScopedByteArrayRW bytes(e, data); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 625 | #if(NXP_EXTNS == TRUE) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 626 | ALOGD("%s: enter; handle=0x%X; buf len=%zu", __FUNCTION__, handle, bytes.size()); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame^] | 627 | SecureElement::getInstance().transceive(reinterpret_cast<UINT8*>(&bytes[0]), bytes.size(), recvBuffer, recvBufferMaxSize, recvBufferActualSize, WIRED_MODE_TRANSCEIVE_TIMEOUT); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 628 | |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 629 | //copy results back to java |
| 630 | jbyteArray result = e->NewByteArray(recvBufferActualSize); |
| 631 | if (result != NULL) |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 632 | { |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 633 | e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 634 | } |
| 635 | #if((NFC_NXP_ESE == TRUE)&&(CONCURRENCY_PROTECTION == TRUE)) |
| 636 | if (ceTransactionPending) |
| 637 | { |
| 638 | ALOGD ("APDU Transceive CE wait"); |
| 639 | SecureElement::getInstance().startThread(0x01); |
| 640 | } |
| 641 | #endif |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 642 | ALOGD("%s: exit: recv len=%ld", __FUNCTION__, recvBufferActualSize); |
| 643 | return result; |
| 644 | #else |
| 645 | jbyteArray result = e->NewByteArray(0); |
| 646 | return result; |
| 647 | #endif |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /***************************************************************************** |
| 651 | ** |
| 652 | ** Description: JNI functions |
| 653 | ** |
| 654 | *****************************************************************************/ |
| 655 | static JNINativeMethod gMethods[] = |
| 656 | { |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 657 | #if(NXP_EXTNS == TRUE) |
| 658 | {"doNativeOpenSecureElementConnection", "(I)I", (void *) nativeNfcSecureElement_doOpenSecureElementConnection}, |
| 659 | #else |
| 660 | {"doNativeOpenSecureElementConnection", "()I", (void *) nativeNfcSecureElement_doOpenSecureElementConnection}, |
| 661 | #endif |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 662 | {"doNativeDisconnectSecureElementConnection", "(I)Z", (void *) nativeNfcSecureElement_doDisconnectSecureElementConnection}, |
| 663 | {"doNativeResetSecureElement", "(I)Z", (void *) nativeNfcSecureElement_doResetSecureElement}, |
| 664 | {"doTransceive", "(I[B)[B", (void *) nativeNfcSecureElement_doTransceive}, |
| 665 | {"doNativeGetAtr", "(I)[B", (void *) nativeNfcSecureElement_doGetAtr}, |
| 666 | }; |
| 667 | |
| 668 | |
| 669 | /******************************************************************************* |
| 670 | ** |
| 671 | ** Function: register_com_android_nfc_NativeNfcSecureElement |
| 672 | ** |
| 673 | ** Description: Regisgter JNI functions with Java Virtual Machine. |
| 674 | ** e: Environment of JVM. |
| 675 | ** |
| 676 | ** Returns: Status of registration. |
| 677 | ** |
| 678 | *******************************************************************************/ |
| 679 | int register_com_android_nfc_NativeNfcSecureElement(JNIEnv *e) |
| 680 | { |
| 681 | return jniRegisterNativeMethods(e, gNativeNfcSecureElementClassName, |
| 682 | gMethods, NELEM(gMethods)); |
| 683 | } |
| 684 | |
| 685 | |
| 686 | } // namespace android |