aboutsummaryrefslogtreecommitdiffstats
path: root/nci/jni/NativeNfcAla.cpp
blob: b61452834f20c8a8abad7c96b58a62a0c7adf64f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
 * Not a Contribution.
 *
 * Copyright (C) 2015 NXP Semiconductors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "NfcJniUtil.h"
#include "PowerSwitch.h"
#include "JavaClassConstants.h"
#include "_OverrideLog.h"
#include <ScopedPrimitiveArray.h>
#include "DwpChannel.h"
#include "SecureElement.h"
#include "TransactionController.h"
extern "C"
{
    #include "AlaLib.h"
    #include "IChannel.h"
    #include "phNxpConfig.h"
}
#define LS_DEFAULT_VERSION 0x20
namespace android
{
extern SyncEvent            sNfaVSCResponseEvent;
extern void startRfDiscovery (bool isStart);
extern bool isDiscoveryStarted();
extern void nfaVSCCallback(uint8_t event, uint16_t param_len, uint8_t *p_param);

}

namespace android
{

static bool sRfEnabled;
jbyteArray nfcManager_lsExecuteScript(JNIEnv* e, jobject o, jstring src, jstring dest, jbyteArray);
jbyteArray nfcManager_lsGetVersion(JNIEnv* e, jobject o);
int nfcManager_doAppletLoadApplet(JNIEnv* e, jobject o, jstring choice, jbyteArray);
int nfcManager_getLoaderServiceConfVersion(JNIEnv* e, jobject o);
int nfcManager_GetAppletsList(JNIEnv* e, jobject o, jobjectArray list);
jbyteArray nfcManager_GetCertificateKey(JNIEnv* e, jobject);
jbyteArray nfcManager_lsGetStatus(JNIEnv* e, jobject);
jbyteArray nfcManager_lsGetAppletStatus(JNIEnv* e, jobject);
extern void DWPChannel_init(IChannel_t *DWP);
extern IChannel_t Dwp;

/*******************************************************************************
**
** Function:        nfcManager_GetListofApplets
**
** Description:     provides the list of applets present in the directory.
**                  e: JVM environment.
**                  o: Java object.
**
** Returns:         None.
**
*******************************************************************************/
int nfcManager_GetAppletsList(JNIEnv* e, jobject o, jobjectArray list)
{
    (void)e;
    (void)o;
    (void)list;
    uint8_t xx=0;
#if (NXP_LDR_SVC_VER_2 == FALSE)
    if(nfcFL.nfcNxpEse) {
        char *name[10];
        uint8_t num =0;
        uint8_t list_len = e->GetArrayLength(list);
        ALOGV("%s: enter", __func__);

        sRfEnabled = isDiscoveryStarted();
        if (sRfEnabled) {
            // Stop RF Discovery if we were polling
            startRfDiscovery (false);
        }
        ALOGV("%s: list_len=0x%x", __func__, list_len);
        ALA_GetlistofApplets(name, &num);

        if((num != 0) &&
                (list_len >= num))
        {
            while(num > 0)
            {
                jstring tmp = e->NewStringUTF(name[xx]);
                e->SetObjectArrayElement(list, xx, tmp);
                if(name[xx] != NULL)
                {
                    free(name[xx]);
                }
                xx++;
                num--;
            }
        }
        else
        {
            ALOGE("%s: No applets found",__func__);
        }
        startRfDiscovery (true);
    }
    else {
        xx = -1;
        ALOGV("%s: No p61", __func__);
    }
#endif
    ALOGV("%s: exit; num_applets =0x%X", __func__,xx);
    return xx;
}

/*******************************************************************************
**
** Function:        nfcManager_doAppletLoadApplet
**
** Description:     start jcos download.
**                  e: JVM environment.
**                  o: Java object.
**                  name: path of the ALA script file located
**                  data: SHA encrypted package name
**
** Returns:         True if ok.
**
*******************************************************************************/

int nfcManager_doAppletLoadApplet(JNIEnv* e, jobject o, jstring name, jbyteArray data)
{
    (void)e;
    (void)o;
    (void)name;
    tNFA_STATUS wStatus, status;
#if (NXP_LDR_SVC_VER_2 == FALSE)
    if(nfcFL.nfcNxpEse) {
        ALOGV("%s: enter", __func__);
        IChannel_t Dwp;
        bool stat = false;
        const char *choice = NULL;

        sRfEnabled = isDiscoveryStarted();
        wStatus = status = NFA_STATUS_FAILED;

        if(!pTransactionController->transactionAttempt(TRANSACTION_REQUESTOR(AppletLoadApplet),
                                                    TRANSACTION_ATTEMPT_FOR_SECONDS(5)))
        {
            ALOGE("%s ERROR: attempt to start transaction has failed", __FUNCTION__);
            return wStatus;
        }
        if (sRfEnabled) {
            // Stop RF Discovery if we were polling
            startRfDiscovery (false);
        }
        DWPChannel_init(&Dwp);
        wStatus = ALA_Init(&Dwp);
        if(wStatus != NFA_STATUS_OK)
        {
            ALOGE("%s: ALA initialization failed", __func__);
        }
        else
        {
            ALOGE("%s: start Applet load applet", __func__);
            choice = e->GetStringUTFChars(name, 0);
            ALOGE("choice= %s", choice);
            ScopedByteArrayRO bytes(e, data);
            uint8_t* buf = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&bytes[0]));
            size_t bufLen = bytes.size();
            wStatus = ALA_Start(choice, buf, bufLen);
        }
        stat = ALA_DeInit();
        if(choice != NULL)
            e->ReleaseStringUTFChars(name, choice);

        if(dwpChannelForceClose == false)
            startRfDiscovery (true);

        pTransactionController->transactionEnd(TRANSACTION_REQUESTOR(AppletLoadApplet));

        ALOGV("%s: exit; status =0x%X", __func__,wStatus);
    }
    else {

        wStatus = 0x0F;
        ALOGV("%s: No p61", __func__);
    }
#endif
    return wStatus;

}

/*******************************************************************************
**
** Function:        nfcManager_lsExecuteScript
**
** Description:     start jcos download.
**                  e: JVM environment.
**                  o: Java object.
**                  name: path of the ALA script file located
**                  data: SHA encrypted package name
**
** Returns:         True if ok.
**
*******************************************************************************/
jbyteArray nfcManager_lsExecuteScript(JNIEnv* e, jobject o, jstring name, jstring dest, jbyteArray data)
{
    (void)e;
    (void)o;
    (void)name;
    (void)dest;
    const char *destpath = NULL;
    const uint8_t lsExecuteResponseSize = 4;
    uint8_t resSW [4]={0x4e,0x02,0x69,0x87};
    jbyteArray result = e->NewByteArray(0);
    #if (NXP_LDR_SVC_VER_2 == TRUE)
    if(nfcFL.nfcNxpEse) {

        ALOGV("%s: enter", __func__);
        tNFA_STATUS wStatus, status;
        IChannel_t Dwp;
        bool stat = false;
        const char *choice = NULL;

        sRfEnabled = isDiscoveryStarted();
        wStatus = status = NFA_STATUS_FAILED;

        if(!pTransactionController->transactionAttempt(TRANSACTION_REQUESTOR(lsExecuteScript),
                                                    TRANSACTION_ATTEMPT_FOR_SECONDS(5)))
        {
            ALOGE("%s ERROR: Attempt to start transaction failed", __FUNCTION__);
            return result;
        }
        if (sRfEnabled) {
            // Stop RF Discovery if we were polling
            startRfDiscovery (false);
        }
        DWPChannel_init(&Dwp);
        wStatus = ALA_Init(&Dwp);
        if(wStatus != NFA_STATUS_OK)
        {
            ALOGE("%s: ALA initialization failed", __func__);
        }
        else
        {
            // Commented the Disabling standby
            /* uint8_t param[] = {0x00}; //Disable standby
        SyncEventGuard guard (sNfaVSCResponseEvent);
        status = NFA_SendVsCommand (0x00,0x01,param,nfaVSCCallback);
        if(NFA_STATUS_OK == status)
        {
            sNfaVSCResponseEvent.wait(); //wait for NFA VS command to finish
            ALOGE("%s: start Applet load applet", __func__);
            choice = e->GetStringUTFChars(name, 0);
            ALOGE("choice= %s", choice);
            wStatus = ALA_Start(choice);
        }*/
            destpath = e->GetStringUTFChars(dest, 0);
            ALOGE("destpath= %s", destpath);
            ALOGE("%s: start Applet load applet", __func__);
            choice = e->GetStringUTFChars(name, 0);
            ALOGE("choice= %s", choice);
            ScopedByteArrayRO bytes(e, data);
            uint8_t* buf = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&bytes[0]));
            size_t bufLen = bytes.size();
            wStatus = ALA_Start(choice,destpath, buf, bufLen,resSW);

            //copy results back to java
            result = e->NewByteArray(lsExecuteResponseSize);
            if (result != NULL)
            {
                e->SetByteArrayRegion(result, 0, lsExecuteResponseSize, (jbyte *) resSW);
            }
        }

        // Commented the Enabling standby
        /* uint8_t param[] = {0x01}; //Enable standby
    SyncEventGuard guard (sNfaVSCResponseEvent);
    status = NFA_SendVsCommand (0x00,0x01,param,nfaVSCCallback);
    if(NFA_STATUS_OK == status)
    {
        sNfaVSCResponseEvent.wait(); //wait for NFA VS command to finish
    }*/

        stat = ALA_DeInit();
        if(choice != NULL)
            e->ReleaseStringUTFChars(name, choice);

        if(dwpChannelForceClose == false)
            startRfDiscovery (true);

        pTransactionController->transactionEnd(TRANSACTION_REQUESTOR(lsExecuteScript));

        ALOGV("%s: exit; status =0x%X", __func__,wStatus);
    }
    else {
        if(destpath != NULL)
            e->ReleaseStringUTFChars(dest, destpath);

        result = e->NewByteArray(0);
        tNFA_STATUS wStatus = 0x0F;
        ALOGV("%s: No p61", __func__);
    }
#endif
    return result;
}

/*******************************************************************************
**
** Function:        nfcManager_GetCertificateKey
**
** Description:     It is ued to get the reference certificate key
**                  e: JVM environment.
**                  o: Java object.
**
** Returns:         key value.
**
*******************************************************************************/

jbyteArray nfcManager_GetCertificateKey(JNIEnv* e, jobject)
{
    jbyteArray result = e->NewByteArray(0);
#if (NXP_LDR_SVC_VER_2 == FALSE)
    if(nfcFL.nfcNxpEse) {
        ALOGV("%s: enter", __func__);
        tNFA_STATUS wStatus = NFA_STATUS_FAILED;
        IChannel_t Dwp;
        bool stat = false;
        const int32_t recvBufferMaxSize = 256;
        uint8_t recvBuffer [recvBufferMaxSize];
        int32_t recvBufferActualSize = 0;

        sRfEnabled = isDiscoveryStarted();

        if (sRfEnabled) {
            // Stop RF Discovery if we were polling
            startRfDiscovery (false);
        }
        DWPChannel_init(&Dwp);
        wStatus = ALA_Init(&Dwp);
        if(wStatus != NFA_STATUS_OK)
        {
            ALOGE("%s: ALA initialization failed", __func__);
        }
        else
        {
            ALOGE("%s: start Get reference Certificate Key", __func__);
            wStatus = ALA_GetCertificateKey(recvBuffer, &recvBufferActualSize);
        }

        //copy results back to java
        result = e->NewByteArray(recvBufferActualSize);
        if (result != NULL)
        {
            e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer);
        }

        stat = ALA_DeInit();
        startRfDiscovery (true);

        ALOGV("%s: exit: recv len=%ld", __func__, recvBufferActualSize);
    }
    else {
        result = e->NewByteArray(0);
        ALOGV("%s: No p61", __func__);
    }
#endif
    return result;
}

/*******************************************************************************
**
** Function:        nfcManager_lsGetVersion
**
** Description:     It is ued to get version of Loader service client & Applet
**                  e: JVM environment.
**                  o: Java object.
**
** Returns:         version of Loder service.
**
*******************************************************************************/
jbyteArray nfcManager_lsGetVersion(JNIEnv* e, jobject)
{
    jbyteArray result;
#if (NXP_LDR_SVC_VER_2 == TRUE)
    if(nfcFL.nfcNxpEse) {
        ALOGV("%s: enter", __func__);
        tNFA_STATUS wStatus = NFA_STATUS_FAILED;
        IChannel_t Dwp;
        bool stat = false;
        const int32_t recvBufferMaxSize = 4;
        uint8_t recvBuffer [recvBufferMaxSize];
        result = e->NewByteArray(0);
        sRfEnabled = isDiscoveryStarted();

        if(!pTransactionController->transactionAttempt(TRANSACTION_REQUESTOR(lsGetVersion),TRANSACTION_ATTEMPT_FOR_SECONDS(5)))
        {
            ALOGE("%s ERRROR: Attempt to start transaction, failed",__FUNCTION__);
            return result;
        }
        if (sRfEnabled) {
            // Stop RF Discovery if we were polling
            startRfDiscovery (false);
        }
        DWPChannel_init(&Dwp);
        wStatus = ALA_Init(&Dwp);
        if(wStatus != NFA_STATUS_OK)
        {
            ALOGE("%s: ALA initialization failed", __func__);
        }
        else
        {
            ALOGE("%s: start Get reference Certificate Key", __func__);
            wStatus = ALA_lsGetVersion(recvBuffer);
        }

        //copy results back to java
        result = e->NewByteArray(recvBufferMaxSize);
        if (result != NULL)
        {
            e->SetByteArrayRegion(result, 0, recvBufferMaxSize, (jbyte *) recvBuffer);
        }

        stat = ALA_DeInit();
        if(dwpChannelForceClose == false)
            startRfDiscovery (true);

        pTransactionController->transactionEnd(TRANSACTION_REQUESTOR(lsGetVersion));

        ALOGV("%s: exit: recv len=%ld", __func__, recvBufferMaxSize);
    }
    else
    {
        result = e->NewByteArray(0);
        ALOGV("%s: No p61", __func__);
    }
#endif
    return result;

}

/*******************************************************************************
**
** Function:        nfcManager_lsGetAppletStatus
**
** Description:     It is ued to get LS Applet status SUCCESS/FAIL
**                  e: JVM environment.
**                  o: Java object.
**
** Returns:         LS Previous execution Applet status .
**
*******************************************************************************/
jbyteArray nfcManager_lsGetAppletStatus(JNIEnv* e, jobject)
{
    jbyteArray result = e->NewByteArray(0);
#if (NXP_LDR_SVC_VER_2 == TRUE)
    if(nfcFL.nfcNxpEse) {
        ALOGV("%s: enter", __func__);
        tNFA_STATUS wStatus = NFA_STATUS_FAILED;
        bool stat = false;
        const int32_t recvBufferMaxSize = 2;
        uint8_t recvBuffer [recvBufferMaxSize]={0x63,0x40};
        IChannel_t Dwp;
        sRfEnabled = isDiscoveryStarted();

        if (sRfEnabled) {
            // Stop RF Discovery if we were polling
            startRfDiscovery (false);
        }
        DWPChannel_init(&Dwp);
        wStatus = ALA_Init(&Dwp);
        if(wStatus != NFA_STATUS_OK)
        {
            ALOGE("%s: ALA initialization failed", __func__);
        }
        else
        {
            ALOGE("%s: start Get reference Certificate Key", __func__);
            wStatus = ALA_lsGetAppletStatus(recvBuffer);
        }

        ALOGV("%s: lsGetAppletStatus values %x %x", __func__, recvBuffer[0], recvBuffer[1]);
        //copy results back to java
        result = e->NewByteArray(recvBufferMaxSize);
        if (result != NULL)
        {
            e->SetByteArrayRegion(result, 0, recvBufferMaxSize, (jbyte *) recvBuffer);
        }
        stat = ALA_DeInit();

        if(dwpChannelForceClose == false)
            startRfDiscovery (true);

        ALOGV("%s: exit: recv len=%ld", __func__, recvBufferMaxSize);
    }
    else {
        result = e->NewByteArray(0);
        ALOGV("%s: No p61", __func__);
    }
#endif
    return result;
}

/*******************************************************************************
**
** Function:        nfcManager_lsGetStatus
**
** Description:     It is ued to get LS Client status SUCCESS/FAIL
**                  e: JVM environment.
**                  o: Java object.
**
** Returns:         version of Loder service.
**
*******************************************************************************/
jbyteArray nfcManager_lsGetStatus(JNIEnv* e, jobject)
{
    jbyteArray result = e->NewByteArray(0);
#if (NXP_LDR_SVC_VER_2 == TRUE)
    if(nfcFL.nfcNxpEse) {
        ALOGV("%s: enter", __func__);
        tNFA_STATUS wStatus = NFA_STATUS_FAILED;
        const int32_t recvBufferMaxSize = 2;
        uint8_t recvBuffer [recvBufferMaxSize] = {0x63,0x40};

        wStatus = ALA_lsGetStatus(recvBuffer);

        ALOGV("%s: lsGetStatus values %x %x", __func__, recvBuffer[0], recvBuffer[1]);
        //copy results back to java
        result = e->NewByteArray(recvBufferMaxSize);
        if (result != NULL)
        {
            e->SetByteArrayRegion(result, 0, recvBufferMaxSize, (jbyte *) recvBuffer);
        }
        ALOGV("%s: exit: recv len=%ld", __func__, recvBufferMaxSize);
    }
    else {
        result = e->NewByteArray(0);
        ALOGV("%s: No p61", __func__);
    }
#endif
    return result;
}

/*******************************************************************************
**
** Function:        nfcManager_getLoaderServiceConfVersion
**
** Description:     Get current Loader service version from config file.
**                  e: JVM environment.
**                  o: Java object.
**
** Returns:         Void.
**
*******************************************************************************/
int nfcManager_getLoaderServiceConfVersion(JNIEnv* /* e */, jobject /* o */)
{
    unsigned long num = 0;
    uint8_t ls_version = LS_DEFAULT_VERSION;
    ALOGV("%s: enter", __func__);
#if (NXP_LDR_SVC_VER_2 == TRUE)
    if(nfcFL.nfcNxpEse) {
        if(GetNxpNumValue (NAME_NXP_LOADER_SERICE_VERSION, (void*)&num, sizeof(num))==false)
        {
            ALOGV("LOADER_SERVICE_VERSION not found");
            num = 0;
        }
        /*If LS version exists in config file*/
        if(num != 0)
        {
            ls_version = num;
        }
    }
    else {
        ALOGV("%s: No P61", __func__);
    }
#endif
    ALOGV("%s: exit", __func__);
    return ls_version;
}

/*****************************************************************************
 **
 ** Description:     JNI functions
 **
 *****************************************************************************/
static JNINativeMethod gMethods[] =
{
    {"doLsExecuteScript","(Ljava/lang/String;Ljava/lang/String;[B)[B",
                (void *)nfcManager_lsExecuteScript},
    {"doLsGetVersion","()[B",
                (void *)nfcManager_lsGetVersion},
    {"doLsGetStatus","()[B",
      (void *)nfcManager_lsGetStatus},
    {"doLsGetAppletStatus","()[B",
      (void *)nfcManager_lsGetAppletStatus},
    {"doGetLSConfigVersion", "()I",
       (void *)nfcManager_getLoaderServiceConfVersion},

    {"GetAppletsList", "([Ljava/lang/String;)I",
                (void *)nfcManager_GetAppletsList},

    {"doAppletLoadApplet", "(Ljava/lang/String;[B)I",
                (void *)nfcManager_doAppletLoadApplet},

    {"GetCertificateKey", "()[B",
                (void *)nfcManager_GetCertificateKey},
};

/*******************************************************************************
 **
 ** Function:        register_com_android_nfc_NativeNfcAla
 **
 ** Description:     Regisgter JNI functions with Java Virtual Machine.
 **                  e: Environment of JVM.
 **
 ** Returns:         Status of registration.
 **
 *******************************************************************************/
int register_com_android_nfc_NativeNfcAla(JNIEnv *e)
{
    return jniRegisterNativeMethods(e, gNativeNfcAlaClassName,
            gMethods, NELEM(gMethods));
}
} /*namespace android*/