blob: c4ed73a94158bd814a51cc2c4e443dcee96a82ae [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
nxpandroid7d44e572016-08-01 19:11:04 +053079bool is_wired_mode_open = false;
nxpandroid64fd68c2015-09-23 16:45:15 +053080/*******************************************************************************
81**
82** Function: nativeNfcSecureElement_doOpenSecureElementConnection
83**
84** Description: Connect to the secure element.
85** e: JVM environment.
86** o: Java object.
87**
88** Returns: Handle of secure element. values < 0 represent failure.
89**
90*******************************************************************************/
nxpandroid7d44e572016-08-01 19:11:04 +053091#if (NXP_EXTNS == TRUE)
92static jint nativeNfcSecureElement_doOpenSecureElementConnection (JNIEnv*, jobject,jint seId)
93#else
nxpandroid64fd68c2015-09-23 16:45:15 +053094static jint nativeNfcSecureElement_doOpenSecureElementConnection (JNIEnv*, jobject)
nxpandroid7d44e572016-08-01 19:11:04 +053095#endif
nxpandroid64fd68c2015-09-23 16:45:15 +053096{
97 ALOGD("%s: enter", __FUNCTION__);
98 bool stat = false;
99 jint secElemHandle = EE_ERROR_INIT;
nxpandroida9a68ba2016-01-14 21:12:17 +0530100#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid64fd68c2015-09-23 16:45:15 +0530101 long ret_val = -1;
102 NFCSTATUS status = NFCSTATUS_FAILED;
103 p61_access_state_t p61_current_state = P61_STATE_INVALID;
nxpandroid5d64ce92016-11-18 19:48:53 +0530104 se_apdu_gate_info gateInfo = NO_APDU_GATE;
nxpandroid64fd68c2015-09-23 16:45:15 +0530105#endif
106 SecureElement &se = SecureElement::getInstance();
nxpandroid7d44e572016-08-01 19:11:04 +0530107#if ((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
108#if(NXP_ESE_WIRED_MODE_PRIO != TRUE)
109 if(se.isBusy()) {
nxpandroid64fd68c2015-09-23 16:45:15 +0530110 goto TheEnd;
111 }
nxpandroid7d44e572016-08-01 19:11:04 +0530112#endif
113 se.mIsExclusiveWiredMode = false; // to ctlr exclusive wired mode
114 if(seId == 0xF4)
115 {
116 if(se.mIsWiredModeOpen)
117 {
118 goto TheEnd;
119 }
120#if (NXP_ESE_UICC_EXCLUSIVE_WIRED_MODE == true)
121 se.mIsExclusiveWiredMode = true;
122#endif
123 stat = se.checkForWiredModeAccess();
124 if(stat == false)
125 {
126 ALOGD("Denying SE open due to SE listen mode active");
127 secElemHandle = EE_ERROR_LISTEN_MODE;
128 goto TheEnd;
129 }
130
131 ALOGD("%s: Activating UICC Wired Mode=0x%X", __FUNCTION__, seId);
132 stat = se.activate(seId);
133 ALOGD("%s: Check UICC activation status stat=%X", __FUNCTION__, stat);
134 if (stat)
135 {
136 //establish a pipe to UICC
137 ALOGD("%s: Creatting a pipe to UICC!", __FUNCTION__);
138 stat = se.connectEE();
139 if (stat)
140 {
141 secElemHandle = se.mActiveEeHandle;
142 }
143 else
144 {
145 se.deactivate (0);
146 }
147 }
148 if ((!stat) && (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED)))
149 {
150 PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER);
151 }
152 se.mIsWiredModeOpen = true;
153#if(NXP_ESE_UICC_EXCLUSIVE_WIRED_MODE == TRUE)
154 if (isDiscoveryStarted())
155 {
156 // Stop RF Discovery if we were polling
157 startRfDiscovery (false);
158 status = NFA_DisableListening();
159 if(status == NFCSTATUS_OK)
160 {
161 startRfDiscovery (true);
162 }
163 }
164 else
165 {
166 status = NFA_DisableListening();
167 }
168 se.mlistenDisabled = true;
169#endif
170 goto TheEnd;
171 }
172#if(NXP_ESE_WIRED_MODE_PRIO == TRUE)
173 if((se.mIsWiredModeOpen)&&(se.mActiveEeHandle == 0x402))
174 {
175 stat = SecureElement::getInstance().disconnectEE (se.mActiveEeHandle);
176 se.mActiveEeHandle = NFA_HANDLE_INVALID;
177 se.mIsWiredModeOpen = false;
178 }
179#endif
180
181#if(NFC_NXP_CHIP_TYPE != PN547C2)
nxpandroidebf53fb2016-12-22 18:48:59 +0530182#if (NXP_NFCEE_REMOVED_NTF_RECOVERY == TRUE)
nxpandroid7d44e572016-08-01 19:11:04 +0530183if((RoutingManager::getInstance().is_ee_recovery_ongoing()))
nxpandroida9a68ba2016-01-14 21:12:17 +0530184 {
nxpandroidebf53fb2016-12-22 18:48:59 +0530185 SyncEventGuard guard (se.mEEdatapacketEvent);
186 if(se.mEEdatapacketEvent.wait(android::gMaxEERecoveryTimeout) == false)
187 {
188 goto TheEnd;
189 }
nxpandroida9a68ba2016-01-14 21:12:17 +0530190 }
nxpandroid7d44e572016-08-01 19:11:04 +0530191#endif
192#if(NXP_ESE_DUAL_MODE_PRIO_SCHEME == NXP_ESE_EXCLUSIVE_WIRED_MODE)
193 se.mIsExclusiveWiredMode = true;
194#endif
195 stat = se.checkForWiredModeAccess();
nxpandroid64fd68c2015-09-23 16:45:15 +0530196 if(stat == false)
197 {
198 ALOGD("Denying SE open due to SE listen mode active");
199 secElemHandle = EE_ERROR_LISTEN_MODE;
200 goto TheEnd;
201 }
202#else
203 if (se.isActivatedInListenMode()) {
204 ALOGD("Denying SE open due to SE listen mode active");
205 secElemHandle = EE_ERROR_LISTEN_MODE;
206 goto TheEnd;
207 }
208
209 if (se.isRfFieldOn()) {
210 ALOGD("Denying SE open due to SE in active RF field");
211 secElemHandle = EE_ERROR_EXT_FIELD;
212 goto TheEnd;
213 }
214#endif
215
nxpandroid64fd68c2015-09-23 16:45:15 +0530216 ret_val = NFC_GetP61Status ((void *)&p61_current_state);
217 if (ret_val < 0)
218 {
219 ALOGD("NFC_GetP61Status failed");
220 goto TheEnd;
221 }
222 ALOGD("P61 Status is: %x", p61_current_state);
nxpandroid7d44e572016-08-01 19:11:04 +0530223
nxpandroid1680a6d2017-01-13 19:13:14 +0530224#if (NXP_ESE_JCOP_DWNLD_PROTECTION == TRUE && NXP_EXTNS == TRUE)
nxpandroid5d64ce92016-11-18 19:48:53 +0530225 if(p61_current_state & P61_STATE_JCP_DWNLD)
226 {
227 ALOGD("Denying SE open due to JCOP OS Download is in progress");
228 secElemHandle = EE_ERROR_IO;
229 goto TheEnd;
230 }
231#endif
232
nxpandroid64fd68c2015-09-23 16:45:15 +0530233#if(NFC_NXP_ESE_VER == JCOP_VER_3_1)
nxpandroid34627bd2016-05-27 15:52:30 +0530234 if (!(p61_current_state & P61_STATE_SPI) && !(p61_current_state & P61_STATE_SPI_PRIO))
nxpandroid64fd68c2015-09-23 16:45:15 +0530235 {
236#endif
237 if(p61_current_state & (P61_STATE_SPI)||(p61_current_state & (P61_STATE_SPI_PRIO)))
238 {
239 dual_mode_current_state |= SPI_ON;
240 }
241 if(p61_current_state & (P61_STATE_SPI_PRIO))
242 {
243 hold_the_transceive = true;
244 }
245
246 secElemHandle = NFC_ReqWiredAccess ((void *)&status);
247 if (secElemHandle < 0)
248 {
249 ALOGD("Denying SE open due to NFC_ReqWiredAccess failed");
250 goto TheEnd;
251 }
252 else
253 {
254 if (status != NFCSTATUS_SUCCESS)
255 {
256 ALOGD("Denying SE open due to SE is being used by SPI");
257 secElemHandle = EE_ERROR_IO;
258 goto TheEnd;
259 }
260 else
261 {
262 ALOGD("SE Access granted");
nxpandroid7d44e572016-08-01 19:11:04 +0530263#if(NXP_ESE_DUAL_MODE_PRIO_SCHEME == NXP_ESE_EXCLUSIVE_WIRED_MODE)
264 if (isDiscoveryStarted())
265 {
266 // Stop RF Discovery if we were polling
267 startRfDiscovery (false);
268 status = NFA_DisableListening();
269 if(status == NFCSTATUS_OK)
270 {
271 startRfDiscovery (true);
272 }
273 }
274 else
275 {
276 status = NFA_DisableListening();
277 }
278 se.mlistenDisabled = true;
279#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530280 }
281 }
282#if(NFC_NXP_ESE_VER == JCOP_VER_3_1)
283 }
284 else
285 {
286 ALOGD("Denying SE open because SPI is already open");
287 goto TheEnd;
288
289 }
290#endif
291#endif
nxpandroid5d64ce92016-11-18 19:48:53 +0530292 /* Tell the controller to power up to get ready for sec elem operations */
nxpandroid64fd68c2015-09-23 16:45:15 +0530293 PowerSwitch::getInstance ().setLevel (PowerSwitch::FULL_POWER);
294 PowerSwitch::getInstance ().setModeOn (PowerSwitch::SE_CONNECTED);
nxpandroid5d64ce92016-11-18 19:48:53 +0530295 /* If controller is not routing AND there is no pipe connected,
296 then turn on the sec elem */
nxpandroid34627bd2016-05-27 15:52:30 +0530297#if(NXP_EXTNS == TRUE) && (NFC_NXP_ESE == TRUE)
nxpandroid5d64ce92016-11-18 19:48:53 +0530298 if((!(p61_current_state & (P61_STATE_SPI | P61_STATE_SPI_PRIO))) && (!(dual_mode_current_state & CL_ACTIVE)))
nxpandroid34627bd2016-05-27 15:52:30 +0530299 stat = se.SecEle_Modeset(0x01); //Workaround
300 usleep(150000); /*provide enough delay if NFCC enter in recovery*/
301#endif
302 stat = se.activate(SecureElement::ESE_ID); // It is to get the current activated handle.
nxpandroid64fd68c2015-09-23 16:45:15 +0530303
304 if (stat)
305 {
nxpandroid64fd68c2015-09-23 16:45:15 +0530306 //establish a pipe to sec elem
307 stat = se.connectEE();
308 if (stat)
309 {
310 secElemHandle = se.mActiveEeHandle;
311 }
312 else
313 {
314 se.deactivate (0);
315 }
316 }
nxpandroid5d64ce92016-11-18 19:48:53 +0530317#if((NXP_EXTNS == TRUE) && (NFC_NXP_ESE == TRUE))
318 if(stat)
nxpandroid7d44e572016-08-01 19:11:04 +0530319 {
nxpandroid5d64ce92016-11-18 19:48:53 +0530320 status = NFA_STATUS_OK;
321#if((NFC_NXP_ESE == TRUE)&&(NXP_NFCC_ESE_UICC_CONCURRENT_ACCESS_PROTECTION == TRUE))
322 if(!(se.isActivatedInListenMode() || isp2pActivated() || NfcTag::getInstance ().isActivated ()))
323 {
324 se.enablePassiveListen(0x00);
325 }
nxpandroid7d44e572016-08-01 19:11:04 +0530326 se.meseUiccConcurrentAccess = true;
nxpandroid5d64ce92016-11-18 19:48:53 +0530327#endif
nxpandroidebf53fb2016-12-22 18:48:59 +0530328#if (NXP_WIRED_MODE_STANDBY == TRUE)
329 if(se.mNfccPowerMode == 1)
330 {
nxpandroid1680a6d2017-01-13 19:13:14 +0530331 status = se.setNfccPwrConfig(se.POWER_ALWAYS_ON|se.COMM_LINK_ACTIVE);
nxpandroidebf53fb2016-12-22 18:48:59 +0530332 if(status != NFA_STATUS_OK)
333 {
334 ALOGD("%s: power link command failed", __FUNCTION__);
335 }
nxpandroid1680a6d2017-01-13 19:13:14 +0530336 stat = se.SecEle_Modeset(0x01);
337 status = se.setNfccPwrConfig(se.POWER_ALWAYS_ON);
nxpandroidebf53fb2016-12-22 18:48:59 +0530338 }
339#endif
340 if((status == NFA_STATUS_OK) && (se.mIsIntfRstEnabled))
nxpandroid5d64ce92016-11-18 19:48:53 +0530341 {
342 gateInfo = se.getApduGateInfo();
343 if(gateInfo == ETSI_12_APDU_GATE)
344 {
345 se.NfccStandByOperation(STANDBY_TIMER_STOP);
346 status = se.SecElem_sendEvt_Abort();
347 if(status != NFA_STATUS_OK)
348 {
349 ALOGD("%s: EVT_ABORT failed", __FUNCTION__);
350 }
351 }
352 }
nxpandroid5d64ce92016-11-18 19:48:53 +0530353 if(status != NFA_STATUS_OK)
354 {
355 se.disconnectEE (secElemHandle);
356 secElemHandle = EE_ERROR_INIT;
357 }
358 else
359 {
360 se.mIsWiredModeOpen = true;
361 }
nxpandroid7d44e572016-08-01 19:11:04 +0530362 }
363#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530364 //if code fails to connect to the secure element, and nothing is active, then
365 //tell the controller to power down
366 if ((!stat) && (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED)))
367 {
368 PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER);
369 }
370
371TheEnd:
372 ALOGD("%s: exit; return handle=0x%X", __FUNCTION__, secElemHandle);
373 return secElemHandle;
374}
375
376
377/*******************************************************************************
378**
379** Function: nativeNfcSecureElement_doDisconnectSecureElementConnection
380**
381** Description: Disconnect from the secure element.
382** e: JVM environment.
383** o: Java object.
384** handle: Handle of secure element.
385**
386** Returns: True if ok.
387**
388*******************************************************************************/
389static jboolean nativeNfcSecureElement_doDisconnectSecureElementConnection (JNIEnv*, jobject, jint handle)
390{
391 ALOGD("%s: enter; handle=0x%04x", __FUNCTION__, handle);
392 bool stat = false;
nxpandroida9a68ba2016-01-14 21:12:17 +0530393#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid64fd68c2015-09-23 16:45:15 +0530394 long ret_val = -1;
395 NFCSTATUS status = NFCSTATUS_FAILED;
nxpandroid64fd68c2015-09-23 16:45:15 +0530396
nxpandroid34627bd2016-05-27 15:52:30 +0530397 SecureElement &se = SecureElement::getInstance();
398 se.NfccStandByOperation(STANDBY_TIMER_STOP);
399#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530400
nxpandroid7d44e572016-08-01 19:11:04 +0530401#if(NXP_EXTNS == TRUE)
402 if(handle == 0x402)
403 {
404 stat = SecureElement::getInstance().disconnectEE (handle);
405#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
406 se.mIsWiredModeOpen = false;
407#endif
408#if( (NXP_ESE_UICC_EXCLUSIVE_WIRED_MODE == TRUE) && (NFC_NXP_ESE == TRUE) && (NXP_EXTNS == TRUE) )
409 se.mIsExclusiveWiredMode = false;
410 if(se.mlistenDisabled)
411 {
412 if (isDiscoveryStarted())
413 {
414 // Stop RF Discovery if we were polling
415 startRfDiscovery (false);
416 status = NFA_EnableListening();
417 startRfDiscovery (true);
418 }
419 else
420 {
421 status = NFA_EnableListening();
422 }
423 se.mlistenDisabled = false;
424 }
425#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530426 goto TheEnd;
nxpandroid7d44e572016-08-01 19:11:04 +0530427 }
428#endif
429
430#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
431 //Send the EVT_END_OF_APDU_TRANSFER event at the end of wired mode session.
432 se.NfccStandByOperation(STANDBY_MODE_ON);
433#endif
434
nxpandroid64fd68c2015-09-23 16:45:15 +0530435 stat = SecureElement::getInstance().disconnectEE (handle);
436
nxpandroid5d64ce92016-11-18 19:48:53 +0530437 /* if nothing is active after this, then tell the controller to power down */
nxpandroid64fd68c2015-09-23 16:45:15 +0530438 if (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED))
439 PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER);
nxpandroida9a68ba2016-01-14 21:12:17 +0530440#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid64fd68c2015-09-23 16:45:15 +0530441 ret_val = NFC_RelWiredAccess ((void *)&status);
442 if (ret_val < 0)
443 {
444 ALOGD("Denying SE Release due to NFC_RelWiredAccess failed");
445 goto TheEnd;
446 }
447 else
448 {
449 if (status != NFCSTATUS_SUCCESS)
450 {
451 ALOGD("Denying SE close due to SE is not being released by Pn54x driver");
452 stat = false;
453 }
nxpandroid5d64ce92016-11-18 19:48:53 +0530454#if((NFC_NXP_ESE == TRUE)&&(NXP_NFCC_ESE_UICC_CONCURRENT_ACCESS_PROTECTION == TRUE))
nxpandroid7d44e572016-08-01 19:11:04 +0530455 se.enablePassiveListen(0x01);
456 SecureElement::getInstance().mPassiveListenTimer.kill();
457 se.meseUiccConcurrentAccess = false;
458#endif
459 se.mIsWiredModeOpen = false;
460#if(NXP_ESE_DUAL_MODE_PRIO_SCHEME == NXP_ESE_EXCLUSIVE_WIRED_MODE)
461 if(se.mlistenDisabled)
462 {
463 if (isDiscoveryStarted())
464 {
465 // Stop RF Discovery if we were polling
466 startRfDiscovery (false);
467 status = NFA_EnableListening();
468 startRfDiscovery (true);
469 }
470 else
471 {
472 status = NFA_EnableListening();
473 }
474 se.mlistenDisabled = false;
475 }
476#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530477 }
478#endif
479TheEnd:
nxpandroida9a68ba2016-01-14 21:12:17 +0530480#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
nxpandroid64fd68c2015-09-23 16:45:15 +0530481 ALOGD("%s: exit stat = %d", __FUNCTION__, stat);
482#else
483 ALOGD("%s: exit", __FUNCTION__);
484#endif
485 return stat ? JNI_TRUE : JNI_FALSE;
486}
nxpandroida9a68ba2016-01-14 21:12:17 +0530487#if((NFC_NXP_ESE == TRUE)&&(NXP_EXTNS == TRUE))
488static int checkP61Status(void)
489{
490 jint ret_val = -1;
491 p61_access_state_t p61_current_state = P61_STATE_INVALID;
492 ret_val = NFC_GetP61Status ((void *)&p61_current_state);
493 if (ret_val < 0)
494 {
495 ALOGD("NFC_GetP61Status failed");
496 return -1;
497 }
498 if(p61_current_state & (P61_STATE_SPI)||(p61_current_state & (P61_STATE_SPI_PRIO)))
499 {
500 ALOGD("No gpio change");
501 ret_val = 0;
502 }
503 else
504 {
505 ret_val = -1;
506 }
507 return ret_val;
508}
509#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530510/*******************************************************************************
511**
512** Function: nativeNfcSecureElement_doResetSecureElement
513**
514** Description: Reset the secure element.
515** e: JVM environment.
516** o: Java object.
517** handle: Handle of secure element.
518**
519** Returns: True if ok.
520**
521*******************************************************************************/
522static jboolean nativeNfcSecureElement_doResetSecureElement (JNIEnv*, jobject, jint handle)
523{
524 bool stat = false;
525#if (NFC_NXP_ESE == TRUE)
nxpandroid5d64ce92016-11-18 19:48:53 +0530526 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
nxpandroid64fd68c2015-09-23 16:45:15 +0530527 SecureElement &se = SecureElement::getInstance();
nxpandroidebf53fb2016-12-22 18:48:59 +0530528
nxpandroid64fd68c2015-09-23 16:45:15 +0530529 ALOGD("%s: enter; handle=0x%04x", __FUNCTION__, handle);
nxpandroid7d44e572016-08-01 19:11:04 +0530530 if(!se.mIsWiredModeOpen)
nxpandroida9a68ba2016-01-14 21:12:17 +0530531 {
532 ALOGD("wired mode is not open");
533 return stat;
534 }
nxpandroidebf53fb2016-12-22 18:48:59 +0530535#if ((NXP_ESE_DWP_SPI_SYNC_ENABLE == TRUE) && (NXP_WIRED_MODE_STANDBY == TRUE))
536 if(!checkP61Status())
537 {
538 ALOGD("Reset is not allowed while SPI ON");
539 return stat;
540 }
541#endif
nxpandroid5d64ce92016-11-18 19:48:53 +0530542#if (NXP_WIRED_MODE_STANDBY == TRUE)
543 if(se.mNfccPowerMode == 1)
544 {
545 nfaStat = se.setNfccPwrConfig(se.NFCC_DECIDES);
546 ALOGD ("%s Power Mode is Legacy", __FUNCTION__);
nxpandroid7d44e572016-08-01 19:11:04 +0530547 }
nxpandroid7d44e572016-08-01 19:11:04 +0530548#endif
549 {
nxpandroid7d44e572016-08-01 19:11:04 +0530550 stat = se.SecEle_Modeset(0x00);
551 if (handle == 0x4C0)
552 {
553 if(checkP61Status())
554 se.NfccStandByOperation(STANDBY_GPIO_LOW);
555 }
556 usleep(100 * 1000);
557 if (handle == 0x4C0)
558 {
559 if(checkP61Status() && (se.mIsWiredModeOpen == true))
560 se.NfccStandByOperation(STANDBY_GPIO_HIGH);
561 }
562 stat = se.SecEle_Modeset(0x01);
563 usleep(2000 * 1000);
564 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530565#endif
566 ALOGD("%s: exit", __FUNCTION__);
567 return stat ? JNI_TRUE : JNI_FALSE;
568}
569
570/*******************************************************************************
nxpandroid5d64ce92016-11-18 19:48:53 +0530571 **
572** Function: nativeNfcSecureElement_doeSEChipResetSecureElement
573**
574** Description: Reset the secure element.
575** e: JVM environment.
576** o: Java object.
577** handle: Handle of secure element.
578**
579** Returns: True if ok.
580**
581*******************************************************************************/
582static jboolean nativeNfcSecureElement_doeSEChipResetSecureElement (JNIEnv*, jobject)
583{
584 bool stat = false;
585 NFCSTATUS status = NFCSTATUS_FAILED;
586 unsigned long num = 0x01;
587#if ((NFC_NXP_ESE == TRUE) && (NXP_EXTNS == TRUE))
588 SecureElement &se = SecureElement::getInstance();
589 if (GetNxpNumValue("NXP_ESE_POWER_DH_CONTROL", &num, sizeof(num)))
590 {
591 ALOGD("Power schemes enabled in config file is %ld", num);
592 }
593 if(num == 0x02)
594 {
595 status = se.eSE_Chip_Reset();
596 if(status == NFCSTATUS_SUCCESS)
597 {
598 stat = true;
599 }
600 }
601#endif
602 return stat ? JNI_TRUE : JNI_FALSE;
603}
604
605
606/*******************************************************************************
nxpandroid64fd68c2015-09-23 16:45:15 +0530607**
608** Function: nativeNfcSecureElement_doGetAtr
609**
610** Description: GetAtr from the connected eSE.
611** e: JVM environment.
612** o: Java object.
613** handle: Handle of secure element.
614**
615** Returns: Buffer of received data.
616**
617*******************************************************************************/
618static jbyteArray nativeNfcSecureElement_doGetAtr (JNIEnv* e, jobject, jint handle)
619{
620 bool stat = false;
621 const INT32 recvBufferMaxSize = 1024;
622 UINT8 recvBuffer [recvBufferMaxSize];
623 INT32 recvBufferActualSize = 0;
624#if (NFC_NXP_ESE == TRUE)
625 ALOGD("%s: enter; handle=0x%04x", __FUNCTION__, handle);
626
627 stat = SecureElement::getInstance().getAtr(handle, recvBuffer, &recvBufferActualSize);
628
629 //copy results back to java
630#endif
631 jbyteArray result = e->NewByteArray(recvBufferActualSize);
632 if (result != NULL) {
633 e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer);
634 }
635
636 ALOGD("%s: exit: recv len=%ld", __FUNCTION__, recvBufferActualSize);
637
638 return result;
639}
640
641/*******************************************************************************
642**
643** Function: nativeNfcSecureElement_doTransceive
644**
645** Description: Send data to the secure element; retrieve response.
646** e: JVM environment.
647** o: Java object.
648** handle: Secure element's handle.
649** data: Data to send.
650**
651** Returns: Buffer of received data.
652**
653*******************************************************************************/
654static jbyteArray nativeNfcSecureElement_doTransceive (JNIEnv* e, jobject, jint handle, jbyteArray data)
655{
656 const INT32 recvBufferMaxSize = 0x8800;//1024; 34k
657 UINT8 recvBuffer [recvBufferMaxSize];
658 INT32 recvBufferActualSize = 0;
659
660 ScopedByteArrayRW bytes(e, data);
nxpandroida9a68ba2016-01-14 21:12:17 +0530661#if(NXP_EXTNS == TRUE)
nxpandroid64fd68c2015-09-23 16:45:15 +0530662 ALOGD("%s: enter; handle=0x%X; buf len=%zu", __FUNCTION__, handle, bytes.size());
nxpandroid281eb922016-08-25 20:27:46 +0530663 SecureElement::getInstance().transceive(reinterpret_cast<UINT8*>(&bytes[0]), bytes.size(), recvBuffer, recvBufferMaxSize, recvBufferActualSize, WIRED_MODE_TRANSCEIVE_TIMEOUT);
nxpandroid64fd68c2015-09-23 16:45:15 +0530664
nxpandroida9a68ba2016-01-14 21:12:17 +0530665 //copy results back to java
666 jbyteArray result = e->NewByteArray(recvBufferActualSize);
667 if (result != NULL)
nxpandroid7d44e572016-08-01 19:11:04 +0530668 {
nxpandroida9a68ba2016-01-14 21:12:17 +0530669 e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer);
nxpandroid7d44e572016-08-01 19:11:04 +0530670 }
nxpandroid5d64ce92016-11-18 19:48:53 +0530671#if((NFC_NXP_ESE == TRUE)&&(NXP_NFCC_ESE_UICC_CONCURRENT_ACCESS_PROTECTION == TRUE))
672 if (SecureElement::getInstance().mIsWiredModeBlocked == true)
nxpandroid7d44e572016-08-01 19:11:04 +0530673 {
674 ALOGD ("APDU Transceive CE wait");
675 SecureElement::getInstance().startThread(0x01);
676 }
677#endif
nxpandroida9a68ba2016-01-14 21:12:17 +0530678 ALOGD("%s: exit: recv len=%ld", __FUNCTION__, recvBufferActualSize);
679 return result;
680#else
681 jbyteArray result = e->NewByteArray(0);
682 return result;
683#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530684}
685
686/*****************************************************************************
687**
688** Description: JNI functions
689**
690*****************************************************************************/
691static JNINativeMethod gMethods[] =
692{
nxpandroid7d44e572016-08-01 19:11:04 +0530693#if(NXP_EXTNS == TRUE)
694 {"doNativeOpenSecureElementConnection", "(I)I", (void *) nativeNfcSecureElement_doOpenSecureElementConnection},
695#else
696 {"doNativeOpenSecureElementConnection", "()I", (void *) nativeNfcSecureElement_doOpenSecureElementConnection},
697#endif
nxpandroid64fd68c2015-09-23 16:45:15 +0530698 {"doNativeDisconnectSecureElementConnection", "(I)Z", (void *) nativeNfcSecureElement_doDisconnectSecureElementConnection},
699 {"doNativeResetSecureElement", "(I)Z", (void *) nativeNfcSecureElement_doResetSecureElement},
nxpandroid5d64ce92016-11-18 19:48:53 +0530700 {"doNativeeSEChipResetSecureElement", "()Z", (void *) nativeNfcSecureElement_doeSEChipResetSecureElement},
nxpandroid64fd68c2015-09-23 16:45:15 +0530701 {"doTransceive", "(I[B)[B", (void *) nativeNfcSecureElement_doTransceive},
702 {"doNativeGetAtr", "(I)[B", (void *) nativeNfcSecureElement_doGetAtr},
703};
704
705
706/*******************************************************************************
707**
708** Function: register_com_android_nfc_NativeNfcSecureElement
709**
710** Description: Regisgter JNI functions with Java Virtual Machine.
711** e: Environment of JVM.
712**
713** Returns: Status of registration.
714**
715*******************************************************************************/
716int register_com_android_nfc_NativeNfcSecureElement(JNIEnv *e)
717{
718 return jniRegisterNativeMethods(e, gNativeNfcSecureElementClassName,
719 gMethods, NELEM(gMethods));
720}
721
722
723} // namespace android