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