blob: c7775d3ffdc75bfbf22245fdd07838e5c7aba0d5 [file] [log] [blame]
nxpandroid64fd68c2015-09-23 16:45:15 +05301/*
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 ******************************************************************************/
nxpandroid6fd9cdb2017-07-12 18:25:41 +053035#include "_OverrideLog.h"
nxpandroid64fd68c2015-09-23 16:45:15 +053036#include "SecureElement.h"
37#include "JavaClassConstants.h"
38#include "PowerSwitch.h"
39#include "NfcTag.h"
nxpandroida9a68ba2016-01-14 21:12:17 +053040#include "RoutingManager.h"
nxpandroid64fd68c2015-09-23 16:45:15 +053041#include <ScopedPrimitiveArray.h>
nxpandroid7d44e572016-08-01 19:11:04 +053042#include "phNxpConfig.h"
nxpandroid64fd68c2015-09-23 16:45:15 +053043
nxpandroid6fd9cdb2017-07-12 18:25:41 +053044#define ALOGV ALOGD
nxpandroid64fd68c2015-09-23 16:45:15 +053045extern bool hold_the_transceive;
46extern int dual_mode_current_state;
nxpandroid6fd9cdb2017-07-12 18:25:41 +053047#if((NFC_NXP_ESE == TRUE)&&(NXP_NFCC_ESE_UICC_CONCURRENT_ACCESS_PROTECTION == true))
nxpandroid7d44e572016-08-01 19:11:04 +053048extern bool ceTransactionPending;
49#endif
nxpandroid64fd68c2015-09-23 16:45:15 +053050namespace android
51{
52
53extern void startRfDiscovery (bool isStart);
54extern bool isDiscoveryStarted();
nxpandroid5d64ce92016-11-18 19:48:53 +053055extern bool isp2pActivated();
nxpandroid64fd68c2015-09-23 16:45:15 +053056extern void com_android_nfc_NfcManager_disableDiscovery (JNIEnv* e, jobject o);
57extern void com_android_nfc_NfcManager_enableDiscovery (JNIEnv* e, jobject o, jint mode);
nxpandroidebf53fb2016-12-22 18:48:59 +053058#if (NXP_EXTNS == TRUE)
59extern int gMaxEERecoveryTimeout;
60#endif
nxpandroid64fd68c2015-09-23 16:45:15 +053061static SyncEvent sNfaVSCResponseEvent;
62//static bool sRfEnabled; /*commented to eliminate warning defined but not used*/
63
nxpandroid6fd9cdb2017-07-12 18:25:41 +053064static void nfaVSCCallback(uint8_t event, uint16_t param_len, uint8_t *p_param);
nxpandroid64fd68c2015-09-23 16:45:15 +053065
nxpandroid6fd9cdb2017-07-12 18:25:41 +053066inline static void nfaVSCCallback(uint8_t event, uint16_t param_len, uint8_t *p_param) /*defined as inline to eliminate warning defined but not used*/
nxpandroid64fd68c2015-09-23 16:45:15 +053067{
68 (void)event;
69 (void)param_len;
70 (void)p_param;
71 SyncEventGuard guard (sNfaVSCResponseEvent);
72 sNfaVSCResponseEvent.notifyOne ();
73}
74
75// These must match the EE_ERROR_ types in NfcService.java
76static const int EE_ERROR_IO = -1;
nxpandroid64fd68c2015-09-23 16:45:15 +053077static const int EE_ERROR_INIT = -3;
78static const int EE_ERROR_LISTEN_MODE = -4;
nxpandroid88c4d222017-05-12 16:10:28 +053079static const int EE_ERROR_EXT_FIELD = -5;
nxpandroidebf53fb2016-12-22 18:48:59 +053080
nxpandroid64fd68c2015-09-23 16:45:15 +053081/*******************************************************************************
82**
83** Function: nativeNfcSecureElement_doOpenSecureElementConnection
84**
85** Description: Connect to the secure element.
86** e: JVM environment.
87** o: Java object.
88**
89** Returns: Handle of secure element. values < 0 represent failure.
90**
91*******************************************************************************/
nxpandroid7d44e572016-08-01 19:11:04 +053092#if (NXP_EXTNS == TRUE)
93static jint nativeNfcSecureElement_doOpenSecureElementConnection (JNIEnv*, jobject,jint seId)
94#else
nxpandroid64fd68c2015-09-23 16:45:15 +053095static jint nativeNfcSecureElement_doOpenSecureElementConnection (JNIEnv*, jobject)
nxpandroid7d44e572016-08-01 19:11:04 +053096#endif
nxpandroid64fd68c2015-09-23 16:45:15 +053097{
nxpandroid6fd9cdb2017-07-12 18:25:41 +053098 ALOGV("%s: enter", __func__);
nxpandroid64fd68c2015-09-23 16:45:15 +053099 bool stat = false;
100 jint secElemHandle = EE_ERROR_INIT;
nxpandroid64fd68c2015-09-23 16:45:15 +0530101 long ret_val = -1;
nxpandroid875171a2017-02-24 16:10:40 +0530102#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid64fd68c2015-09-23 16:45:15 +0530103 NFCSTATUS status = NFCSTATUS_FAILED;
104 p61_access_state_t p61_current_state = P61_STATE_INVALID;
nxpandroid5d64ce92016-11-18 19:48:53 +0530105 se_apdu_gate_info gateInfo = NO_APDU_GATE;
nxpandroid64fd68c2015-09-23 16:45:15 +0530106#endif
107 SecureElement &se = SecureElement::getInstance();
nxpandroid7d44e572016-08-01 19:11:04 +0530108#if ((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530109#if(NXP_ESE_WIRED_MODE_PRIO != true)
nxpandroid7d44e572016-08-01 19:11:04 +0530110 if(se.isBusy()) {
nxpandroid64fd68c2015-09-23 16:45:15 +0530111 goto TheEnd;
112 }
nxpandroid7d44e572016-08-01 19:11:04 +0530113#endif
114 se.mIsExclusiveWiredMode = false; // to ctlr exclusive wired mode
115 if(seId == 0xF4)
116 {
117 if(se.mIsWiredModeOpen)
118 {
119 goto TheEnd;
120 }
121#if (NXP_ESE_UICC_EXCLUSIVE_WIRED_MODE == true)
122 se.mIsExclusiveWiredMode = true;
123#endif
124 stat = se.checkForWiredModeAccess();
125 if(stat == false)
126 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530127 ALOGV("Denying SE open due to SE listen mode active");
nxpandroid7d44e572016-08-01 19:11:04 +0530128 secElemHandle = EE_ERROR_LISTEN_MODE;
129 goto TheEnd;
130 }
131
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530132 ALOGV("%s: Activating UICC Wired Mode=0x%X", __func__, seId);
nxpandroid7d44e572016-08-01 19:11:04 +0530133 stat = se.activate(seId);
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530134 ALOGV("%s: Check UICC activation status stat=%X", __func__, stat);
nxpandroid7d44e572016-08-01 19:11:04 +0530135 if (stat)
136 {
137 //establish a pipe to UICC
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530138 ALOGV("%s: Creatting a pipe to UICC!", __func__);
nxpandroid7d44e572016-08-01 19:11:04 +0530139 stat = se.connectEE();
140 if (stat)
141 {
142 secElemHandle = se.mActiveEeHandle;
143 }
144 else
145 {
146 se.deactivate (0);
147 }
148 }
149 if ((!stat) && (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED)))
150 {
151 PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER);
152 }
153 se.mIsWiredModeOpen = true;
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530154#if(NXP_ESE_UICC_EXCLUSIVE_WIRED_MODE == true)
nxpandroid7d44e572016-08-01 19:11:04 +0530155 if (isDiscoveryStarted())
156 {
157 // Stop RF Discovery if we were polling
158 startRfDiscovery (false);
159 status = NFA_DisableListening();
160 if(status == NFCSTATUS_OK)
161 {
162 startRfDiscovery (true);
163 }
164 }
165 else
166 {
167 status = NFA_DisableListening();
168 }
169 se.mlistenDisabled = true;
170#endif
171 goto TheEnd;
172 }
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530173#if(NXP_ESE_WIRED_MODE_PRIO == true)
174 if((se.mIsWiredModeOpen)&&(se.mActiveEeHandle == (se.EE_HANDLE_0xF4 || SecureElement::EE_HANDLE_0xF8))
nxpandroid7d44e572016-08-01 19:11:04 +0530175 {
176 stat = SecureElement::getInstance().disconnectEE (se.mActiveEeHandle);
177 se.mActiveEeHandle = NFA_HANDLE_INVALID;
178 se.mIsWiredModeOpen = false;
179 }
180#endif
181
182#if(NFC_NXP_CHIP_TYPE != PN547C2)
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530183#if (NXP_NFCEE_REMOVED_NTF_RECOVERY == true)
nxpandroid7d44e572016-08-01 19:11:04 +0530184if((RoutingManager::getInstance().is_ee_recovery_ongoing()))
nxpandroida9a68ba2016-01-14 21:12:17 +0530185 {
nxpandroidebf53fb2016-12-22 18:48:59 +0530186 SyncEventGuard guard (se.mEEdatapacketEvent);
187 if(se.mEEdatapacketEvent.wait(android::gMaxEERecoveryTimeout) == false)
188 {
189 goto TheEnd;
190 }
nxpandroida9a68ba2016-01-14 21:12:17 +0530191 }
nxpandroid7d44e572016-08-01 19:11:04 +0530192#endif
193#if(NXP_ESE_DUAL_MODE_PRIO_SCHEME == NXP_ESE_EXCLUSIVE_WIRED_MODE)
194 se.mIsExclusiveWiredMode = true;
195#endif
196 stat = se.checkForWiredModeAccess();
nxpandroid64fd68c2015-09-23 16:45:15 +0530197 if(stat == false)
198 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530199 ALOGV("Denying SE open due to SE listen mode active");
nxpandroid64fd68c2015-09-23 16:45:15 +0530200 secElemHandle = EE_ERROR_LISTEN_MODE;
201 goto TheEnd;
202 }
203#else
204 if (se.isActivatedInListenMode()) {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530205 ALOGV("Denying SE open due to SE listen mode active");
nxpandroid64fd68c2015-09-23 16:45:15 +0530206 secElemHandle = EE_ERROR_LISTEN_MODE;
207 goto TheEnd;
208 }
209
210 if (se.isRfFieldOn()) {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530211 ALOGV("Denying SE open due to SE in active RF field");
nxpandroid64fd68c2015-09-23 16:45:15 +0530212 secElemHandle = EE_ERROR_EXT_FIELD;
213 goto TheEnd;
214 }
215#endif
216
nxpandroid64fd68c2015-09-23 16:45:15 +0530217 ret_val = NFC_GetP61Status ((void *)&p61_current_state);
218 if (ret_val < 0)
219 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530220 ALOGV("NFC_GetP61Status failed");
nxpandroid64fd68c2015-09-23 16:45:15 +0530221 goto TheEnd;
222 }
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530223 ALOGV("P61 Status is: %x", p61_current_state);
nxpandroid7d44e572016-08-01 19:11:04 +0530224
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530225#if (NXP_ESE_JCOP_DWNLD_PROTECTION == true && NXP_EXTNS == TRUE)
nxpandroid5d64ce92016-11-18 19:48:53 +0530226 if(p61_current_state & P61_STATE_JCP_DWNLD)
227 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530228 ALOGV("Denying SE open due to JCOP OS Download is in progress");
nxpandroid5d64ce92016-11-18 19:48:53 +0530229 secElemHandle = EE_ERROR_IO;
230 goto TheEnd;
231 }
232#endif
233
nxpandroid64fd68c2015-09-23 16:45:15 +0530234#if(NFC_NXP_ESE_VER == JCOP_VER_3_1)
nxpandroid34627bd2016-05-27 15:52:30 +0530235 if (!(p61_current_state & P61_STATE_SPI) && !(p61_current_state & P61_STATE_SPI_PRIO))
nxpandroid64fd68c2015-09-23 16:45:15 +0530236 {
237#endif
238 if(p61_current_state & (P61_STATE_SPI)||(p61_current_state & (P61_STATE_SPI_PRIO)))
239 {
240 dual_mode_current_state |= SPI_ON;
241 }
242 if(p61_current_state & (P61_STATE_SPI_PRIO))
243 {
244 hold_the_transceive = true;
245 }
246
247 secElemHandle = NFC_ReqWiredAccess ((void *)&status);
248 if (secElemHandle < 0)
249 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530250 ALOGV("Denying SE open due to NFC_ReqWiredAccess failed");
nxpandroid64fd68c2015-09-23 16:45:15 +0530251 goto TheEnd;
252 }
253 else
254 {
255 if (status != NFCSTATUS_SUCCESS)
256 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530257 ALOGV("Denying SE open due to SE is being used by SPI");
nxpandroid64fd68c2015-09-23 16:45:15 +0530258 secElemHandle = EE_ERROR_IO;
259 goto TheEnd;
260 }
261 else
262 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530263 ALOGV("SE Access granted");
nxpandroid7d44e572016-08-01 19:11:04 +0530264#if(NXP_ESE_DUAL_MODE_PRIO_SCHEME == NXP_ESE_EXCLUSIVE_WIRED_MODE)
265 if (isDiscoveryStarted())
266 {
267 // Stop RF Discovery if we were polling
268 startRfDiscovery (false);
269 status = NFA_DisableListening();
270 if(status == NFCSTATUS_OK)
271 {
272 startRfDiscovery (true);
273 }
274 }
275 else
276 {
277 status = NFA_DisableListening();
278 }
279 se.mlistenDisabled = true;
280#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530281 }
282 }
283#if(NFC_NXP_ESE_VER == JCOP_VER_3_1)
284 }
285 else
286 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530287 ALOGV("Denying SE open because SPI is already open");
nxpandroid64fd68c2015-09-23 16:45:15 +0530288 goto TheEnd;
289
290 }
291#endif
292#endif
nxpandroid5d64ce92016-11-18 19:48:53 +0530293 /* Tell the controller to power up to get ready for sec elem operations */
nxpandroid64fd68c2015-09-23 16:45:15 +0530294 PowerSwitch::getInstance ().setLevel (PowerSwitch::FULL_POWER);
295 PowerSwitch::getInstance ().setModeOn (PowerSwitch::SE_CONNECTED);
nxpandroid5d64ce92016-11-18 19:48:53 +0530296 /* If controller is not routing AND there is no pipe connected,
297 then turn on the sec elem */
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530298#if((NXP_EXTNS == TRUE) && (NFC_NXP_ESE == TRUE))
nxpandroid5d64ce92016-11-18 19:48:53 +0530299 if((!(p61_current_state & (P61_STATE_SPI | P61_STATE_SPI_PRIO))) && (!(dual_mode_current_state & CL_ACTIVE)))
nxpandroid34627bd2016-05-27 15:52:30 +0530300 stat = se.SecEle_Modeset(0x01); //Workaround
301 usleep(150000); /*provide enough delay if NFCC enter in recovery*/
302#endif
303 stat = se.activate(SecureElement::ESE_ID); // It is to get the current activated handle.
nxpandroid64fd68c2015-09-23 16:45:15 +0530304
305 if (stat)
306 {
nxpandroid64fd68c2015-09-23 16:45:15 +0530307 //establish a pipe to sec elem
308 stat = se.connectEE();
309 if (stat)
310 {
311 secElemHandle = se.mActiveEeHandle;
312 }
313 else
314 {
315 se.deactivate (0);
316 }
317 }
nxpandroid5d64ce92016-11-18 19:48:53 +0530318#if((NXP_EXTNS == TRUE) && (NFC_NXP_ESE == TRUE))
319 if(stat)
nxpandroid7d44e572016-08-01 19:11:04 +0530320 {
nxpandroid5d64ce92016-11-18 19:48:53 +0530321 status = NFA_STATUS_OK;
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530322#if((NFC_NXP_ESE == TRUE)&&(NXP_NFCC_ESE_UICC_CONCURRENT_ACCESS_PROTECTION == true))
nxpandroid5d64ce92016-11-18 19:48:53 +0530323 if(!(se.isActivatedInListenMode() || isp2pActivated() || NfcTag::getInstance ().isActivated ()))
324 {
325 se.enablePassiveListen(0x00);
326 }
nxpandroid7d44e572016-08-01 19:11:04 +0530327 se.meseUiccConcurrentAccess = true;
nxpandroid5d64ce92016-11-18 19:48:53 +0530328#endif
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530329#if (NXP_WIRED_MODE_STANDBY == true)
nxpandroidebf53fb2016-12-22 18:48:59 +0530330 if(se.mNfccPowerMode == 1)
331 {
nxpandroid67bd7122017-04-10 18:18:28 +0530332 status = se.setNfccPwrConfig(se.POWER_ALWAYS_ON);
nxpandroidebf53fb2016-12-22 18:48:59 +0530333 if(status != NFA_STATUS_OK)
334 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530335 ALOGV("%s: power link command failed", __func__);
nxpandroidebf53fb2016-12-22 18:48:59 +0530336 }
nxpandroid875171a2017-02-24 16:10:40 +0530337 else
338 {
339 se.SecEle_Modeset(0x01);
nxpandroid875171a2017-02-24 16:10:40 +0530340 }
nxpandroidebf53fb2016-12-22 18:48:59 +0530341 }
342#endif
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530343
nxpandroidebf53fb2016-12-22 18:48:59 +0530344 if((status == NFA_STATUS_OK) && (se.mIsIntfRstEnabled))
nxpandroid5d64ce92016-11-18 19:48:53 +0530345 {
346 gateInfo = se.getApduGateInfo();
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530347 ALOGV("%s: GateInfo %d", __func__, gateInfo);
nxpandroid5d64ce92016-11-18 19:48:53 +0530348 if(gateInfo == ETSI_12_APDU_GATE)
349 {
350 se.NfccStandByOperation(STANDBY_TIMER_STOP);
351 status = se.SecElem_sendEvt_Abort();
352 if(status != NFA_STATUS_OK)
353 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530354 ALOGV("%s: EVT_ABORT failed", __func__);
nxpandroid875171a2017-02-24 16:10:40 +0530355 se.sendEvent(SecureElement::EVT_END_OF_APDU_TRANSFER);
nxpandroid5d64ce92016-11-18 19:48:53 +0530356 }
357 }
358 }
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530359
nxpandroid5d64ce92016-11-18 19:48:53 +0530360 if(status != NFA_STATUS_OK)
361 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530362#if (NXP_WIRED_MODE_STANDBY == true)
nxpandroid875171a2017-02-24 16:10:40 +0530363 if(se.mNfccPowerMode == 1)
364 se.setNfccPwrConfig(se.NFCC_DECIDES);
365#endif
nxpandroid5d64ce92016-11-18 19:48:53 +0530366 se.disconnectEE (secElemHandle);
367 secElemHandle = EE_ERROR_INIT;
nxpandroid875171a2017-02-24 16:10:40 +0530368
369 ret_val = NFC_RelWiredAccess((void*)&status);
370 if(ret_val < 0)
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530371 ALOGV("Denying SE release due to NFC_RelWiredAccess failure");
nxpandroid875171a2017-02-24 16:10:40 +0530372 else if(status != NFCSTATUS_SUCCESS)
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530373 ALOGV("Denying SE close, since SE is not released by PN54xx driver");
nxpandroid5d64ce92016-11-18 19:48:53 +0530374 }
375 else
376 {
377 se.mIsWiredModeOpen = true;
378 }
nxpandroid7d44e572016-08-01 19:11:04 +0530379 }
380#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530381 //if code fails to connect to the secure element, and nothing is active, then
382 //tell the controller to power down
383 if ((!stat) && (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED)))
384 {
385 PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER);
386 }
387
388TheEnd:
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530389 ALOGV("%s: exit; return handle=0x%X", __func__, secElemHandle);
nxpandroid64fd68c2015-09-23 16:45:15 +0530390 return secElemHandle;
391}
392
393
394/*******************************************************************************
395**
396** Function: nativeNfcSecureElement_doDisconnectSecureElementConnection
397**
398** Description: Disconnect from the secure element.
399** e: JVM environment.
400** o: Java object.
401** handle: Handle of secure element.
402**
403** Returns: True if ok.
404**
405*******************************************************************************/
406static jboolean nativeNfcSecureElement_doDisconnectSecureElementConnection (JNIEnv*, jobject, jint handle)
407{
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530408 ALOGV("%s: enter; handle=0x%04x", __func__, handle);
nxpandroid64fd68c2015-09-23 16:45:15 +0530409 bool stat = false;
nxpandroida9a68ba2016-01-14 21:12:17 +0530410#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid64fd68c2015-09-23 16:45:15 +0530411 long ret_val = -1;
412 NFCSTATUS status = NFCSTATUS_FAILED;
nxpandroid64fd68c2015-09-23 16:45:15 +0530413
nxpandroid34627bd2016-05-27 15:52:30 +0530414 SecureElement &se = SecureElement::getInstance();
415 se.NfccStandByOperation(STANDBY_TIMER_STOP);
416#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530417
nxpandroid7d44e572016-08-01 19:11:04 +0530418#if(NXP_EXTNS == TRUE)
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530419 if(handle == (SecureElement::EE_HANDLE_0xF8||se.EE_HANDLE_0xF4))
nxpandroid7d44e572016-08-01 19:11:04 +0530420 {
421 stat = SecureElement::getInstance().disconnectEE (handle);
422#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
423 se.mIsWiredModeOpen = false;
424#endif
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530425#if( (NXP_ESE_UICC_EXCLUSIVE_WIRED_MODE == true) && (NFC_NXP_ESE == TRUE) && (NXP_EXTNS == TRUE) )
nxpandroid7d44e572016-08-01 19:11:04 +0530426 se.mIsExclusiveWiredMode = false;
427 if(se.mlistenDisabled)
428 {
429 if (isDiscoveryStarted())
430 {
431 // Stop RF Discovery if we were polling
432 startRfDiscovery (false);
433 status = NFA_EnableListening();
434 startRfDiscovery (true);
435 }
436 else
437 {
438 status = NFA_EnableListening();
439 }
440 se.mlistenDisabled = false;
441 }
442#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530443 goto TheEnd;
nxpandroid7d44e572016-08-01 19:11:04 +0530444 }
445#endif
446
447#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
448 //Send the EVT_END_OF_APDU_TRANSFER event at the end of wired mode session.
449 se.NfccStandByOperation(STANDBY_MODE_ON);
450#endif
451
nxpandroid64fd68c2015-09-23 16:45:15 +0530452 stat = SecureElement::getInstance().disconnectEE (handle);
453
nxpandroid5d64ce92016-11-18 19:48:53 +0530454 /* if nothing is active after this, then tell the controller to power down */
nxpandroid64fd68c2015-09-23 16:45:15 +0530455 if (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED))
456 PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER);
nxpandroida9a68ba2016-01-14 21:12:17 +0530457#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid64fd68c2015-09-23 16:45:15 +0530458 ret_val = NFC_RelWiredAccess ((void *)&status);
459 if (ret_val < 0)
460 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530461 ALOGV("Denying SE Release due to NFC_RelWiredAccess failed");
nxpandroid64fd68c2015-09-23 16:45:15 +0530462 goto TheEnd;
463 }
464 else
465 {
466 if (status != NFCSTATUS_SUCCESS)
467 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530468 ALOGV("Denying SE close due to SE is not being released by Pn54x driver");
nxpandroid64fd68c2015-09-23 16:45:15 +0530469 stat = false;
470 }
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530471#if((NFC_NXP_ESE == TRUE)&&(NXP_NFCC_ESE_UICC_CONCURRENT_ACCESS_PROTECTION == true))
nxpandroid7d44e572016-08-01 19:11:04 +0530472 se.enablePassiveListen(0x01);
473 SecureElement::getInstance().mPassiveListenTimer.kill();
474 se.meseUiccConcurrentAccess = false;
475#endif
476 se.mIsWiredModeOpen = false;
477#if(NXP_ESE_DUAL_MODE_PRIO_SCHEME == NXP_ESE_EXCLUSIVE_WIRED_MODE)
478 if(se.mlistenDisabled)
479 {
480 if (isDiscoveryStarted())
481 {
482 // Stop RF Discovery if we were polling
483 startRfDiscovery (false);
484 status = NFA_EnableListening();
485 startRfDiscovery (true);
486 }
487 else
488 {
489 status = NFA_EnableListening();
490 }
491 se.mlistenDisabled = false;
492 }
493#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530494 }
495#endif
496TheEnd:
nxpandroida9a68ba2016-01-14 21:12:17 +0530497#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530498 ALOGV("%s: exit stat = %d", __func__, stat);
nxpandroid64fd68c2015-09-23 16:45:15 +0530499#else
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530500 ALOGV("%s: exit", __func__);
nxpandroid64fd68c2015-09-23 16:45:15 +0530501#endif
502 return stat ? JNI_TRUE : JNI_FALSE;
503}
nxpandroida9a68ba2016-01-14 21:12:17 +0530504#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
505static int checkP61Status(void)
506{
507 jint ret_val = -1;
508 p61_access_state_t p61_current_state = P61_STATE_INVALID;
509 ret_val = NFC_GetP61Status ((void *)&p61_current_state);
510 if (ret_val < 0)
511 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530512 ALOGV("NFC_GetP61Status failed");
nxpandroida9a68ba2016-01-14 21:12:17 +0530513 return -1;
514 }
515 if(p61_current_state & (P61_STATE_SPI)||(p61_current_state & (P61_STATE_SPI_PRIO)))
516 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530517 ALOGV("No gpio change");
nxpandroida9a68ba2016-01-14 21:12:17 +0530518 ret_val = 0;
519 }
520 else
521 {
522 ret_val = -1;
523 }
524 return ret_val;
525}
526#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530527/*******************************************************************************
528**
529** Function: nativeNfcSecureElement_doResetSecureElement
530**
531** Description: Reset the secure element.
532** e: JVM environment.
533** o: Java object.
534** handle: Handle of secure element.
535**
536** Returns: True if ok.
537**
538*******************************************************************************/
539static jboolean nativeNfcSecureElement_doResetSecureElement (JNIEnv*, jobject, jint handle)
540{
541 bool stat = false;
542#if (NFC_NXP_ESE == TRUE)
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530543#if (NXP_WIRED_MODE_STANDBY == true)
nxpandroid5d64ce92016-11-18 19:48:53 +0530544 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
nxpandroidc98b4d52017-02-24 16:07:04 +0530545#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530546 SecureElement &se = SecureElement::getInstance();
nxpandroidebf53fb2016-12-22 18:48:59 +0530547
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530548 ALOGV("%s: enter; handle=0x%04x", __func__, handle);
nxpandroid7d44e572016-08-01 19:11:04 +0530549 if(!se.mIsWiredModeOpen)
nxpandroida9a68ba2016-01-14 21:12:17 +0530550 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530551 ALOGV("wired mode is not open");
nxpandroida9a68ba2016-01-14 21:12:17 +0530552 return stat;
553 }
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530554#if ((NXP_ESE_DWP_SPI_SYNC_ENABLE == true) && (NXP_WIRED_MODE_STANDBY == true))
nxpandroidebf53fb2016-12-22 18:48:59 +0530555 if(!checkP61Status())
556 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530557 ALOGV("Reset is not allowed while SPI ON");
nxpandroidebf53fb2016-12-22 18:48:59 +0530558 return stat;
559 }
560#endif
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530561#if (NXP_WIRED_MODE_STANDBY == true)
nxpandroid5d64ce92016-11-18 19:48:53 +0530562 if(se.mNfccPowerMode == 1)
563 {
564 nfaStat = se.setNfccPwrConfig(se.NFCC_DECIDES);
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530565 ALOGV("%s Power Mode is Legacy", __func__);
nxpandroid7d44e572016-08-01 19:11:04 +0530566 }
nxpandroid7d44e572016-08-01 19:11:04 +0530567#endif
568 {
nxpandroid7d44e572016-08-01 19:11:04 +0530569 stat = se.SecEle_Modeset(0x00);
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530570 if (handle == SecureElement::EE_HANDLE_0xF3)
nxpandroid7d44e572016-08-01 19:11:04 +0530571 {
572 if(checkP61Status())
573 se.NfccStandByOperation(STANDBY_GPIO_LOW);
574 }
575 usleep(100 * 1000);
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530576 if (handle == SecureElement::EE_HANDLE_0xF3)
nxpandroid7d44e572016-08-01 19:11:04 +0530577 {
578 if(checkP61Status() && (se.mIsWiredModeOpen == true))
579 se.NfccStandByOperation(STANDBY_GPIO_HIGH);
580 }
581 stat = se.SecEle_Modeset(0x01);
582 usleep(2000 * 1000);
nxpandroid6c999f02017-02-24 15:45:35 +0530583
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530584#if (NXP_WIRED_MODE_STANDBY == true)
nxpandroid6c999f02017-02-24 15:45:35 +0530585 if(se.mNfccPowerMode == 1)
586 {
587 nfaStat = se.setNfccPwrConfig(se.POWER_ALWAYS_ON);
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530588 ALOGV("%s Power Mode is Legacy", __func__);
nxpandroid6c999f02017-02-24 15:45:35 +0530589 }
590#endif
nxpandroid7d44e572016-08-01 19:11:04 +0530591 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530592#endif
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530593 ALOGV("%s: exit", __func__);
nxpandroid64fd68c2015-09-23 16:45:15 +0530594 return stat ? JNI_TRUE : JNI_FALSE;
595}
596
597/*******************************************************************************
nxpandroid5d64ce92016-11-18 19:48:53 +0530598 **
599** Function: nativeNfcSecureElement_doeSEChipResetSecureElement
600**
601** Description: Reset the secure element.
602** e: JVM environment.
603** o: Java object.
604** handle: Handle of secure element.
605**
606** Returns: True if ok.
607**
608*******************************************************************************/
609static jboolean nativeNfcSecureElement_doeSEChipResetSecureElement (JNIEnv*, jobject)
610{
611 bool stat = false;
612 NFCSTATUS status = NFCSTATUS_FAILED;
613 unsigned long num = 0x01;
614#if ((NFC_NXP_ESE == TRUE) && (NXP_EXTNS == TRUE))
615 SecureElement &se = SecureElement::getInstance();
616 if (GetNxpNumValue("NXP_ESE_POWER_DH_CONTROL", &num, sizeof(num)))
617 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530618 ALOGV("Power schemes enabled in config file is %ld", num);
nxpandroid5d64ce92016-11-18 19:48:53 +0530619 }
620 if(num == 0x02)
621 {
622 status = se.eSE_Chip_Reset();
623 if(status == NFCSTATUS_SUCCESS)
624 {
625 stat = true;
626 }
627 }
628#endif
629 return stat ? JNI_TRUE : JNI_FALSE;
630}
631
632
633/*******************************************************************************
nxpandroid64fd68c2015-09-23 16:45:15 +0530634**
635** Function: nativeNfcSecureElement_doGetAtr
636**
637** Description: GetAtr from the connected eSE.
638** e: JVM environment.
639** o: Java object.
640** handle: Handle of secure element.
641**
642** Returns: Buffer of received data.
643**
644*******************************************************************************/
645static jbyteArray nativeNfcSecureElement_doGetAtr (JNIEnv* e, jobject, jint handle)
646{
647 bool stat = false;
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530648 const int32_t recvBufferMaxSize = 1024;
649 uint8_t recvBuffer [recvBufferMaxSize];
650 int32_t recvBufferActualSize = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +0530651#if (NFC_NXP_ESE == TRUE)
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530652 ALOGV("%s: enter; handle=0x%04x", __func__, handle);
nxpandroid64fd68c2015-09-23 16:45:15 +0530653
654 stat = SecureElement::getInstance().getAtr(handle, recvBuffer, &recvBufferActualSize);
655
656 //copy results back to java
657#endif
658 jbyteArray result = e->NewByteArray(recvBufferActualSize);
659 if (result != NULL) {
660 e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer);
661 }
662
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530663 ALOGV("%s: exit: recv len=%ld", __func__, recvBufferActualSize);
nxpandroid64fd68c2015-09-23 16:45:15 +0530664
665 return result;
666}
667
668/*******************************************************************************
669**
670** Function: nativeNfcSecureElement_doTransceive
671**
672** Description: Send data to the secure element; retrieve response.
673** e: JVM environment.
674** o: Java object.
675** handle: Secure element's handle.
676** data: Data to send.
677**
678** Returns: Buffer of received data.
679**
680*******************************************************************************/
681static jbyteArray nativeNfcSecureElement_doTransceive (JNIEnv* e, jobject, jint handle, jbyteArray data)
682{
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530683 const int32_t recvBufferMaxSize = 0x8800;//1024; 34k
684 uint8_t recvBuffer [recvBufferMaxSize];
685 int32_t recvBufferActualSize = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +0530686
687 ScopedByteArrayRW bytes(e, data);
nxpandroida9a68ba2016-01-14 21:12:17 +0530688#if(NXP_EXTNS == TRUE)
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530689 ALOGV("%s: enter; handle=0x%X; buf len=%zu", __func__, handle, bytes.size());
690 SecureElement::getInstance().transceive(reinterpret_cast<uint8_t*>(&bytes[0]), bytes.size(), recvBuffer, recvBufferMaxSize, recvBufferActualSize, WIRED_MODE_TRANSCEIVE_TIMEOUT);
nxpandroid64fd68c2015-09-23 16:45:15 +0530691
nxpandroida9a68ba2016-01-14 21:12:17 +0530692 //copy results back to java
693 jbyteArray result = e->NewByteArray(recvBufferActualSize);
694 if (result != NULL)
nxpandroid7d44e572016-08-01 19:11:04 +0530695 {
nxpandroida9a68ba2016-01-14 21:12:17 +0530696 e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer);
nxpandroid7d44e572016-08-01 19:11:04 +0530697 }
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530698#if((NFC_NXP_ESE == TRUE)&&(NXP_NFCC_ESE_UICC_CONCURRENT_ACCESS_PROTECTION == true))
nxpandroid5d64ce92016-11-18 19:48:53 +0530699 if (SecureElement::getInstance().mIsWiredModeBlocked == true)
nxpandroid7d44e572016-08-01 19:11:04 +0530700 {
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530701 ALOGV("APDU Transceive CE wait");
nxpandroid7d44e572016-08-01 19:11:04 +0530702 SecureElement::getInstance().startThread(0x01);
703 }
704#endif
nxpandroid6fd9cdb2017-07-12 18:25:41 +0530705 ALOGV("%s: exit: recv len=%ld", __func__, recvBufferActualSize);
nxpandroida9a68ba2016-01-14 21:12:17 +0530706 return result;
707#else
708 jbyteArray result = e->NewByteArray(0);
709 return result;
710#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530711}
712
713/*****************************************************************************
714**
715** Description: JNI functions
716**
717*****************************************************************************/
718static JNINativeMethod gMethods[] =
719{
nxpandroid7d44e572016-08-01 19:11:04 +0530720#if(NXP_EXTNS == TRUE)
721 {"doNativeOpenSecureElementConnection", "(I)I", (void *) nativeNfcSecureElement_doOpenSecureElementConnection},
722#else
723 {"doNativeOpenSecureElementConnection", "()I", (void *) nativeNfcSecureElement_doOpenSecureElementConnection},
724#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530725 {"doNativeDisconnectSecureElementConnection", "(I)Z", (void *) nativeNfcSecureElement_doDisconnectSecureElementConnection},
726 {"doNativeResetSecureElement", "(I)Z", (void *) nativeNfcSecureElement_doResetSecureElement},
nxpandroid5d64ce92016-11-18 19:48:53 +0530727 {"doNativeeSEChipResetSecureElement", "()Z", (void *) nativeNfcSecureElement_doeSEChipResetSecureElement},
nxpandroid64fd68c2015-09-23 16:45:15 +0530728 {"doTransceive", "(I[B)[B", (void *) nativeNfcSecureElement_doTransceive},
729 {"doNativeGetAtr", "(I)[B", (void *) nativeNfcSecureElement_doGetAtr},
730};
731
732
733/*******************************************************************************
734**
735** Function: register_com_android_nfc_NativeNfcSecureElement
736**
737** Description: Regisgter JNI functions with Java Virtual Machine.
738** e: Environment of JVM.
739**
740** Returns: Status of registration.
741**
742*******************************************************************************/
743int register_com_android_nfc_NativeNfcSecureElement(JNIEnv *e)
744{
745 return jniRegisterNativeMethods(e, gNativeNfcSecureElementClassName,
746 gMethods, NELEM(gMethods));
747}
748
749
750} // namespace android