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