blob: 34cc4df4352c069346570f999d090de341c905a7 [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 ******************************************************************************/
35#include "OverrideLog.h"
36#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
44extern bool hold_the_transceive;
45extern int dual_mode_current_state;
nxpandroid5d64ce92016-11-18 19:48:53 +053046#if((NFC_NXP_ESE == TRUE)&&(NXP_NFCC_ESE_UICC_CONCURRENT_ACCESS_PROTECTION == TRUE))
nxpandroid7d44e572016-08-01 19:11:04 +053047extern bool ceTransactionPending;
48#endif
nxpandroid64fd68c2015-09-23 16:45:15 +053049namespace android
50{
51
52extern void startRfDiscovery (bool isStart);
53extern bool isDiscoveryStarted();
nxpandroid5d64ce92016-11-18 19:48:53 +053054extern bool isp2pActivated();
nxpandroid64fd68c2015-09-23 16:45:15 +053055extern void com_android_nfc_NfcManager_disableDiscovery (JNIEnv* e, jobject o);
56extern void com_android_nfc_NfcManager_enableDiscovery (JNIEnv* e, jobject o, jint mode);
nxpandroidebf53fb2016-12-22 18:48:59 +053057#if (NXP_EXTNS == TRUE)
58extern int gMaxEERecoveryTimeout;
59#endif
nxpandroid64fd68c2015-09-23 16:45:15 +053060static SyncEvent sNfaVSCResponseEvent;
61//static bool sRfEnabled; /*commented to eliminate warning defined but not used*/
62
63static void nfaVSCCallback(UINT8 event, UINT16 param_len, UINT8 *p_param);
64
65inline 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
75static const int EE_ERROR_IO = -1;
nxpandroid64fd68c2015-09-23 16:45:15 +053076static const int EE_ERROR_INIT = -3;
77static const int EE_ERROR_LISTEN_MODE = -4;
nxpandroidebf53fb2016-12-22 18:48:59 +053078
nxpandroid64fd68c2015-09-23 16:45:15 +053079/*******************************************************************************
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*******************************************************************************/
nxpandroid7d44e572016-08-01 19:11:04 +053090#if (NXP_EXTNS == TRUE)
91static jint nativeNfcSecureElement_doOpenSecureElementConnection (JNIEnv*, jobject,jint seId)
92#else
nxpandroid64fd68c2015-09-23 16:45:15 +053093static jint nativeNfcSecureElement_doOpenSecureElementConnection (JNIEnv*, jobject)
nxpandroid7d44e572016-08-01 19:11:04 +053094#endif
nxpandroid64fd68c2015-09-23 16:45:15 +053095{
96 ALOGD("%s: enter", __FUNCTION__);
97 bool stat = false;
98 jint secElemHandle = EE_ERROR_INIT;
nxpandroid64fd68c2015-09-23 16:45:15 +053099 long ret_val = -1;
nxpandroid875171a2017-02-24 16:10:40 +0530100#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid64fd68c2015-09-23 16:45:15 +0530101 NFCSTATUS status = NFCSTATUS_FAILED;
102 p61_access_state_t p61_current_state = P61_STATE_INVALID;
nxpandroid5d64ce92016-11-18 19:48:53 +0530103 se_apdu_gate_info gateInfo = NO_APDU_GATE;
nxpandroid64fd68c2015-09-23 16:45:15 +0530104#endif
105 SecureElement &se = SecureElement::getInstance();
nxpandroid7d44e572016-08-01 19:11:04 +0530106#if ((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
107#if(NXP_ESE_WIRED_MODE_PRIO != TRUE)
108 if(se.isBusy()) {
nxpandroid64fd68c2015-09-23 16:45:15 +0530109 goto TheEnd;
110 }
nxpandroid7d44e572016-08-01 19:11:04 +0530111#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)
nxpandroidebf53fb2016-12-22 18:48:59 +0530181#if (NXP_NFCEE_REMOVED_NTF_RECOVERY == TRUE)
nxpandroid7d44e572016-08-01 19:11:04 +0530182if((RoutingManager::getInstance().is_ee_recovery_ongoing()))
nxpandroida9a68ba2016-01-14 21:12:17 +0530183 {
nxpandroidebf53fb2016-12-22 18:48:59 +0530184 SyncEventGuard guard (se.mEEdatapacketEvent);
185 if(se.mEEdatapacketEvent.wait(android::gMaxEERecoveryTimeout) == false)
186 {
187 goto TheEnd;
188 }
nxpandroida9a68ba2016-01-14 21:12:17 +0530189 }
nxpandroid7d44e572016-08-01 19:11:04 +0530190#endif
191#if(NXP_ESE_DUAL_MODE_PRIO_SCHEME == NXP_ESE_EXCLUSIVE_WIRED_MODE)
192 se.mIsExclusiveWiredMode = true;
193#endif
194 stat = se.checkForWiredModeAccess();
nxpandroid64fd68c2015-09-23 16:45:15 +0530195 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
nxpandroid64fd68c2015-09-23 16:45:15 +0530215 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);
nxpandroid7d44e572016-08-01 19:11:04 +0530222
nxpandroid1680a6d2017-01-13 19:13:14 +0530223#if (NXP_ESE_JCOP_DWNLD_PROTECTION == TRUE && NXP_EXTNS == TRUE)
nxpandroid5d64ce92016-11-18 19:48:53 +0530224 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
nxpandroid64fd68c2015-09-23 16:45:15 +0530232#if(NFC_NXP_ESE_VER == JCOP_VER_3_1)
nxpandroid34627bd2016-05-27 15:52:30 +0530233 if (!(p61_current_state & P61_STATE_SPI) && !(p61_current_state & P61_STATE_SPI_PRIO))
nxpandroid64fd68c2015-09-23 16:45:15 +0530234 {
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");
nxpandroid7d44e572016-08-01 19:11:04 +0530262#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
nxpandroid64fd68c2015-09-23 16:45:15 +0530279 }
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
nxpandroid5d64ce92016-11-18 19:48:53 +0530291 /* Tell the controller to power up to get ready for sec elem operations */
nxpandroid64fd68c2015-09-23 16:45:15 +0530292 PowerSwitch::getInstance ().setLevel (PowerSwitch::FULL_POWER);
293 PowerSwitch::getInstance ().setModeOn (PowerSwitch::SE_CONNECTED);
nxpandroid5d64ce92016-11-18 19:48:53 +0530294 /* If controller is not routing AND there is no pipe connected,
295 then turn on the sec elem */
nxpandroid34627bd2016-05-27 15:52:30 +0530296#if(NXP_EXTNS == TRUE) && (NFC_NXP_ESE == TRUE)
nxpandroid5d64ce92016-11-18 19:48:53 +0530297 if((!(p61_current_state & (P61_STATE_SPI | P61_STATE_SPI_PRIO))) && (!(dual_mode_current_state & CL_ACTIVE)))
nxpandroid34627bd2016-05-27 15:52:30 +0530298 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.
nxpandroid64fd68c2015-09-23 16:45:15 +0530302
303 if (stat)
304 {
nxpandroid64fd68c2015-09-23 16:45:15 +0530305 //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 }
nxpandroid5d64ce92016-11-18 19:48:53 +0530316#if((NXP_EXTNS == TRUE) && (NFC_NXP_ESE == TRUE))
317 if(stat)
nxpandroid7d44e572016-08-01 19:11:04 +0530318 {
nxpandroid5d64ce92016-11-18 19:48:53 +0530319 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 }
nxpandroid7d44e572016-08-01 19:11:04 +0530325 se.meseUiccConcurrentAccess = true;
nxpandroid5d64ce92016-11-18 19:48:53 +0530326#endif
nxpandroidebf53fb2016-12-22 18:48:59 +0530327#if (NXP_WIRED_MODE_STANDBY == TRUE)
328 if(se.mNfccPowerMode == 1)
329 {
nxpandroid67bd7122017-04-10 18:18:28 +0530330 status = se.setNfccPwrConfig(se.POWER_ALWAYS_ON);
nxpandroidebf53fb2016-12-22 18:48:59 +0530331 if(status != NFA_STATUS_OK)
332 {
333 ALOGD("%s: power link command failed", __FUNCTION__);
334 }
nxpandroid875171a2017-02-24 16:10:40 +0530335 else
336 {
337 se.SecEle_Modeset(0x01);
nxpandroid875171a2017-02-24 16:10:40 +0530338 }
nxpandroidebf53fb2016-12-22 18:48:59 +0530339 }
340#endif
341 if((status == NFA_STATUS_OK) && (se.mIsIntfRstEnabled))
nxpandroid5d64ce92016-11-18 19:48:53 +0530342 {
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__);
nxpandroid875171a2017-02-24 16:10:40 +0530351 se.sendEvent(SecureElement::EVT_END_OF_APDU_TRANSFER);
nxpandroid5d64ce92016-11-18 19:48:53 +0530352 }
353 }
354 }
nxpandroid5d64ce92016-11-18 19:48:53 +0530355 if(status != NFA_STATUS_OK)
356 {
nxpandroid875171a2017-02-24 16:10:40 +0530357#if (NXP_WIRED_MODE_STANDBY == TRUE)
358 if(se.mNfccPowerMode == 1)
359 se.setNfccPwrConfig(se.NFCC_DECIDES);
360#endif
nxpandroid5d64ce92016-11-18 19:48:53 +0530361 se.disconnectEE (secElemHandle);
362 secElemHandle = EE_ERROR_INIT;
nxpandroid875171a2017-02-24 16:10:40 +0530363
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");
nxpandroid5d64ce92016-11-18 19:48:53 +0530369 }
370 else
371 {
372 se.mIsWiredModeOpen = true;
373 }
nxpandroid7d44e572016-08-01 19:11:04 +0530374 }
375#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530376 //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
383TheEnd:
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*******************************************************************************/
401static jboolean nativeNfcSecureElement_doDisconnectSecureElementConnection (JNIEnv*, jobject, jint handle)
402{
403 ALOGD("%s: enter; handle=0x%04x", __FUNCTION__, handle);
404 bool stat = false;
nxpandroida9a68ba2016-01-14 21:12:17 +0530405#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid64fd68c2015-09-23 16:45:15 +0530406 long ret_val = -1;
407 NFCSTATUS status = NFCSTATUS_FAILED;
nxpandroid64fd68c2015-09-23 16:45:15 +0530408
nxpandroid34627bd2016-05-27 15:52:30 +0530409 SecureElement &se = SecureElement::getInstance();
410 se.NfccStandByOperation(STANDBY_TIMER_STOP);
411#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530412
nxpandroid7d44e572016-08-01 19:11:04 +0530413#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
nxpandroid64fd68c2015-09-23 16:45:15 +0530438 goto TheEnd;
nxpandroid7d44e572016-08-01 19:11:04 +0530439 }
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
nxpandroid64fd68c2015-09-23 16:45:15 +0530447 stat = SecureElement::getInstance().disconnectEE (handle);
448
nxpandroid5d64ce92016-11-18 19:48:53 +0530449 /* if nothing is active after this, then tell the controller to power down */
nxpandroid64fd68c2015-09-23 16:45:15 +0530450 if (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED))
451 PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER);
nxpandroida9a68ba2016-01-14 21:12:17 +0530452#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid64fd68c2015-09-23 16:45:15 +0530453 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 }
nxpandroid5d64ce92016-11-18 19:48:53 +0530466#if((NFC_NXP_ESE == TRUE)&&(NXP_NFCC_ESE_UICC_CONCURRENT_ACCESS_PROTECTION == TRUE))
nxpandroid7d44e572016-08-01 19:11:04 +0530467 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
nxpandroid64fd68c2015-09-23 16:45:15 +0530489 }
490#endif
491TheEnd:
nxpandroida9a68ba2016-01-14 21:12:17 +0530492#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid64fd68c2015-09-23 16:45:15 +0530493 ALOGD("%s: exit stat = %d", __FUNCTION__, stat);
494#else
495 ALOGD("%s: exit", __FUNCTION__);
496#endif
497 return stat ? JNI_TRUE : JNI_FALSE;
498}
nxpandroida9a68ba2016-01-14 21:12:17 +0530499#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
500static 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
nxpandroid64fd68c2015-09-23 16:45:15 +0530522/*******************************************************************************
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*******************************************************************************/
534static jboolean nativeNfcSecureElement_doResetSecureElement (JNIEnv*, jobject, jint handle)
535{
536 bool stat = false;
537#if (NFC_NXP_ESE == TRUE)
nxpandroidc98b4d52017-02-24 16:07:04 +0530538#if (NXP_WIRED_MODE_STANDBY == TRUE)
nxpandroid5d64ce92016-11-18 19:48:53 +0530539 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
nxpandroidc98b4d52017-02-24 16:07:04 +0530540#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530541 SecureElement &se = SecureElement::getInstance();
nxpandroidebf53fb2016-12-22 18:48:59 +0530542
nxpandroid64fd68c2015-09-23 16:45:15 +0530543 ALOGD("%s: enter; handle=0x%04x", __FUNCTION__, handle);
nxpandroid7d44e572016-08-01 19:11:04 +0530544 if(!se.mIsWiredModeOpen)
nxpandroida9a68ba2016-01-14 21:12:17 +0530545 {
546 ALOGD("wired mode is not open");
547 return stat;
548 }
nxpandroidebf53fb2016-12-22 18:48:59 +0530549#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
nxpandroid5d64ce92016-11-18 19:48:53 +0530556#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__);
nxpandroid7d44e572016-08-01 19:11:04 +0530561 }
nxpandroid7d44e572016-08-01 19:11:04 +0530562#endif
563 {
nxpandroid7d44e572016-08-01 19:11:04 +0530564 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);
nxpandroid6c999f02017-02-24 15:45:35 +0530578
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
nxpandroid7d44e572016-08-01 19:11:04 +0530586 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530587#endif
588 ALOGD("%s: exit", __FUNCTION__);
589 return stat ? JNI_TRUE : JNI_FALSE;
590}
591
592/*******************************************************************************
nxpandroid5d64ce92016-11-18 19:48:53 +0530593 **
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*******************************************************************************/
604static 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/*******************************************************************************
nxpandroid64fd68c2015-09-23 16:45:15 +0530629**
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*******************************************************************************/
640static 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*******************************************************************************/
676static 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);
nxpandroida9a68ba2016-01-14 21:12:17 +0530683#if(NXP_EXTNS == TRUE)
nxpandroid64fd68c2015-09-23 16:45:15 +0530684 ALOGD("%s: enter; handle=0x%X; buf len=%zu", __FUNCTION__, handle, bytes.size());
nxpandroid281eb922016-08-25 20:27:46 +0530685 SecureElement::getInstance().transceive(reinterpret_cast<UINT8*>(&bytes[0]), bytes.size(), recvBuffer, recvBufferMaxSize, recvBufferActualSize, WIRED_MODE_TRANSCEIVE_TIMEOUT);
nxpandroid64fd68c2015-09-23 16:45:15 +0530686
nxpandroida9a68ba2016-01-14 21:12:17 +0530687 //copy results back to java
688 jbyteArray result = e->NewByteArray(recvBufferActualSize);
689 if (result != NULL)
nxpandroid7d44e572016-08-01 19:11:04 +0530690 {
nxpandroida9a68ba2016-01-14 21:12:17 +0530691 e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer);
nxpandroid7d44e572016-08-01 19:11:04 +0530692 }
nxpandroid5d64ce92016-11-18 19:48:53 +0530693#if((NFC_NXP_ESE == TRUE)&&(NXP_NFCC_ESE_UICC_CONCURRENT_ACCESS_PROTECTION == TRUE))
694 if (SecureElement::getInstance().mIsWiredModeBlocked == true)
nxpandroid7d44e572016-08-01 19:11:04 +0530695 {
696 ALOGD ("APDU Transceive CE wait");
697 SecureElement::getInstance().startThread(0x01);
698 }
699#endif
nxpandroida9a68ba2016-01-14 21:12:17 +0530700 ALOGD("%s: exit: recv len=%ld", __FUNCTION__, recvBufferActualSize);
701 return result;
702#else
703 jbyteArray result = e->NewByteArray(0);
704 return result;
705#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530706}
707
708/*****************************************************************************
709**
710** Description: JNI functions
711**
712*****************************************************************************/
713static JNINativeMethod gMethods[] =
714{
nxpandroid7d44e572016-08-01 19:11:04 +0530715#if(NXP_EXTNS == TRUE)
716 {"doNativeOpenSecureElementConnection", "(I)I", (void *) nativeNfcSecureElement_doOpenSecureElementConnection},
717#else
718 {"doNativeOpenSecureElementConnection", "()I", (void *) nativeNfcSecureElement_doOpenSecureElementConnection},
719#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530720 {"doNativeDisconnectSecureElementConnection", "(I)Z", (void *) nativeNfcSecureElement_doDisconnectSecureElementConnection},
721 {"doNativeResetSecureElement", "(I)Z", (void *) nativeNfcSecureElement_doResetSecureElement},
nxpandroid5d64ce92016-11-18 19:48:53 +0530722 {"doNativeeSEChipResetSecureElement", "()Z", (void *) nativeNfcSecureElement_doeSEChipResetSecureElement},
nxpandroid64fd68c2015-09-23 16:45:15 +0530723 {"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*******************************************************************************/
738int register_com_android_nfc_NativeNfcSecureElement(JNIEnv *e)
739{
740 return jniRegisterNativeMethods(e, gNativeNfcSecureElementClassName,
741 gMethods, NELEM(gMethods));
742}
743
744
745} // namespace android