aboutsummaryrefslogtreecommitdiffstats
path: root/nci/jni/SecureElement.h
blob: 479862c3a032bbf34753808df08280e17adb83f6 (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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * 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.
 */
/******************************************************************************
 *
 *  The original Work has been changed by NXP Semiconductors.
 *
 *  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.
 *
 ******************************************************************************/
/*
 *  Communicate with secure elements that are attached to the NFC
 *  controller.
 */
#pragma once
#include "SyncEvent.h"
#include "DataQueue.h"
#include "NfcJniUtil.h"
#include "RouteDataSet.h"
#include "IntervalTimer.h"
extern "C"
{
    #include "nfa_ee_api.h"
    #include "nfa_hci_api.h"
    #include "nfa_hci_defs.h"
    #include "nfa_ce_api.h"
    #include "phNxpExtns.h"
#if(NXP_EXTNS == TRUE)
    #include "phNfcTypes.h"
#endif
}
#if(NXP_EXTNS == TRUE)
#define CONNECTIVITY_PIPE_ID_UICC1 0x0A
#define CONNECTIVITY_PIPE_ID_UICC2 0x23

#define SIG_NFC 44
#endif
#define SIGNAL_EVENT_SIZE 0x02
typedef enum {
    RESET_TRANSACTION_STATE,
    SET_TRANSACTION_STATE
}transaction_state_t;


typedef enum dual_mode{
 SPI_DWPCL_NOT_ACTIVE = 0x00,
 CL_ACTIVE = 0x01,
 SPI_ON = 0x02,
 SPI_DWPCL_BOTH_ACTIVE = 0x03,
}dual_mode_state;
#if(NXP_EXTNS == TRUE)
typedef enum connectivity_pipe_status{
PIPE_DELETED,
PIPE_CLOSED,
PIPE_OPENED
}pipe_status;

typedef enum
{
    UICC_SESSION_NOT_INTIALIZED = 0x00,
    UICC_CLEAR_ALL_PIPE_NTF_RECEIVED = 0x01,
    UICC_SESSION_INTIALIZATION_DONE = 0x02
}nfcee_disc_state;

typedef enum
{
    TRANSCEIVE_STATUS_OK,
    TRANSCEIVE_STATUS_FAILED,
    TRANSCEIVE_STATUS_MAX_WTX_REACHED
} eTransceiveStatus;
#endif
typedef enum {
    STATE_IDLE = 0x00,
    STATE_WK_ENBLE = 0x01,
    STATE_WK_WAIT_RSP = 0x02,
    STATE_TIME_OUT = 0x04,
    STATE_DWP_CLOSE = 0x08,
}spiDwpSyncState_t;

typedef enum reset_management{
 TRANS_IDLE = 0x00,
 TRANS_WIRED_ONGOING = 0x01,
 TRANS_CL_ONGOING = 0x02,
 RESET_BLOCKED = 0x04,
}ese_reset_control;
typedef struct {
    tNFA_HANDLE src;
    tNFA_TECHNOLOGY_MASK tech_mask;
    bool reCfg;
}rd_swp_req_t;

typedef enum
{
    STATE_SE_RDR_MODE_INVALID =0x00,
    STATE_SE_RDR_MODE_START_CONFIG,
    STATE_SE_RDR_MODE_START_IN_PROGRESS,
    STATE_SE_RDR_MODE_STARTED,
    STATE_SE_RDR_MODE_ACTIVATED,
    STATE_SE_RDR_MODE_STOP_CONFIG,
    STATE_SE_RDR_MODE_STOP_IN_PROGRESS,
    STATE_SE_RDR_MODE_STOPPED,

}se_rd_req_state_t;

#if(NXP_EXTNS == TRUE)

typedef enum
{
    UICC_01_SELECTED_ENABLED = 0x01,
    UICC_01_SELECTED_DISABLED,
    UICC_01_REMOVED,
    UICC_02_SELECTED_ENABLED,
    UICC_02_SELECTED_DISABLED,
    UICC_02_REMOVED,
    UICC_STATUS_UNKNOWN
}uicc_stat_t;
typedef enum
{
    SWP_DEFAULT = 0x00,
    SWP1_UICC1 = 0x01,
    SWP2_ESE = 0x02,
    SWP1A_UICC2 = 0x04,
    T4T_NDEFEE = 0x08,
    HCI_ACESS = 0x10
}nfcee_swp_getconfig_status;
#endif
typedef enum
{   STATE_SE_RDR_FAILURE_NOT_SUPPORTED ,
    STATE_SE_RDR_FAILURE_NOT_ALLOWED

}se_rd_req_failures_t;

#if (NXP_EXTNS == TRUE)
typedef struct{
    rd_swp_req_t swp_rd_req_info ;
    rd_swp_req_t swp_rd_req_current_info ;
    se_rd_req_state_t swp_rd_state;
    se_rd_req_failures_t swp_rd_req_fail_cause;
    Mutex mMutex;
}Rdr_req_ntf_info_t;

typedef enum operation{
    STANDBY_TIMER_START,
    STANDBY_TIMER_STOP,
    STANDBY_TIMER_TIMEOUT,
    STANDBY_GPIO_HIGH,
    STANDBY_GPIO_LOW,
    STANDBY_MODE_ON,      /* standby mode is on */
    STANDBY_MODE_OFF,     /* standby mode is off */
    STANDBY_MODE_SUSPEND, /* standby timer timed out */
    STANDBY_MODE_TIMER_ON /* standby timer running */
}nfcc_standby_operation_t;

void spi_prio_signal_handler (int signum, siginfo_t *info, void *unused);

typedef enum apdu_gate{
    NO_APDU_GATE,
    PROPREITARY_APDU_GATE,
    ETSI_12_APDU_GATE
}se_apdu_gate_info;
#endif
typedef enum nfcee_type
{
    UICC1 = 0x01,
    UICC2 = 0x02,
    ESE   = 0x04
}nfcee_type_t;
typedef enum
{
    NONE = 0x00,
    FW_DOWNLOAD,
    JCOP_DOWNLOAD
}Downlaod_mode_t;
namespace android {
extern SyncEvent sNfaEnableDisablePollingEvent;
extern void startStopPolling (bool isStartPolling);

}  // namespace android

class SecureElement
{
public:
    tNFA_HANDLE  mActiveEeHandle;
#if(NXP_EXTNS == TRUE)
#define MAX_NFCEE 5
    struct mNfceeData
    {
        tNFA_HANDLE mNfceeHandle[MAX_NFCEE];
        tNFA_EE_STATUS mNfceeStatus[MAX_NFCEE];
        uint8_t mNfceePresent;
    };
    mNfceeData  mNfceeData_t;
    uint8_t     mHostsPresent;
    uint8_t     mETSI12InitStatus;
    uint8_t     mHostsId[MAX_NFCEE];
    uint8_t     eSE_Compliancy;
    uint8_t     mCreatedPipe;
    uint8_t     mDeletePipeHostId;
    uint16_t    mWmMaxWtxCount;
    bool        meseETSI12Recovery;
    SyncEvent   mCreatePipeEvent;
    SyncEvent   mPipeOpenedEvent;
    SyncEvent   mAbortEvent;
    bool        mAbortEventWaitOk;
    bool enableDwp(void);
    IntervalTimer sSwpReaderTimer; /*timer swp reader timeout*/
    static const tNFA_HANDLE EE_HANDLE_0xF3 = 0x4C0;//0x401; //handle to secure element in slot 0
    static const tNFA_HANDLE EE_HANDLE_0xF8 = 0x481; //handle to secure element in slot 2
    tNFA_HANDLE EE_HANDLE_0xF4;               //handle to secure element in slot 1
    static const tNFA_HANDLE EE_HANDLE_0xF0 = 0x400;//NFCEE handle for host
#endif
#define NCI_INTERFACE_UICC_DIRECT_STAT  0x82
#define NCI_INTERFACE_ESE_DIRECT_STAT  0x83
    static const int MAX_NUM_EE = NFA_EE_MAX_EE_SUPPORTED;    /*max number of EE's*/

    /*******************************************************************************
    **
    ** Function:        getInstance
    **
    ** Description:     Get the SecureElement singleton object.
    **
    ** Returns:         SecureElement object.
    **
    *******************************************************************************/
    static SecureElement& getInstance ();


    /*******************************************************************************
    **
    ** Function:        initialize
    **
    ** Description:     Initialize all member variables.
    **                  native: Native data.
    **
    ** Returns:         True if ok.
    **
    *******************************************************************************/
    bool initialize (nfc_jni_native_data* native);


    /*******************************************************************************
    **
    ** Function:        finalize
    **
    ** Description:     Release all resources.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void finalize ();


    /*******************************************************************************
    **
    ** Function:        getListOfEeHandles
    **
    ** Description:     Get the list of handles of all execution environments.
    **                  e: Java Virtual Machine.
    **
    ** Returns:         List of handles of all execution environments.
    **
    *******************************************************************************/
    jintArray getListOfEeHandles (JNIEnv* e);

    /*******************************************************************************
    **
    ** Function:        getActiveSecureElementList
    **
    ** Description:     Get the list of handles of all execution environments.
    **                  e: Java Virtual Machine.
    **
    ** Returns:         List of handles of all execution environments.
    **
    *******************************************************************************/
    jintArray getActiveSecureElementList (JNIEnv* e);


    /*******************************************************************************
    **
    ** Function:        activate
    **
    ** Description:     Turn on the secure element.
    **                  seID: ID of secure element.
    **
    ** Returns:         True if ok.
    **
    *******************************************************************************/
    bool activate (jint seID);


    /*******************************************************************************
    **
    ** Function:        deactivate
    **
    ** Description:     Turn off the secure element.
    **                  seID: ID of secure element.
    **
    ** Returns:         True if ok.
    **
    *******************************************************************************/
    bool deactivate (jint seID);


    /*******************************************************************************
    **
    ** Function:        connectEE
    **
    ** Description:     Connect to the execution environment.
    **
    ** Returns:         True if ok.
    **
    *******************************************************************************/
    bool connectEE ();

    bool activateAllNfcee();
    /*******************************************************************************
    **
    ** Function:        disconnectEE
    **
    ** Description:     Disconnect from the execution environment.
    **                  seID: ID of secure element.
    **
    ** Returns:         True if ok.
    **
    *******************************************************************************/
    bool disconnectEE (jint seID);


    /*******************************************************************************
    **
    ** Function:        transceive
    **
    ** Description:     Send data to the secure element; read it's response.
    **                  xmitBuffer: Data to transmit.
    **                  xmitBufferSize: Length of data.
    **                  recvBuffer: Buffer to receive response.
    **                  recvBufferMaxSize: Maximum size of buffer.
    **                  recvBufferActualSize: Actual length of response.
    **                  timeoutMillisec: timeout in millisecond
    **
    ** Returns:         True if ok.
    **
    *******************************************************************************/
#if(NXP_EXTNS == TRUE)
    eTransceiveStatus transceive (uint8_t* xmitBuffer, int32_t xmitBufferSize, uint8_t* recvBuffer,
                     int32_t recvBufferMaxSize, int32_t& recvBufferActualSize, int32_t timeoutMillisec);
#else
    bool transceive (uint8_t* xmitBuffer, int32_t xmitBufferSize, uint8_t* recvBuffer,
                     int32_t recvBufferMaxSize, int32_t& recvBufferActualSize, int32_t timeoutMillisec);
#endif

    void notifyModeSet (tNFA_HANDLE eeHandle, bool success, tNFA_EE_STATUS eeStatus);

    /*******************************************************************************
    **
    ** Function:        notifyListenModeState
    **
    ** Description:     Notify the NFC service about whether the SE was activated
    **                  in listen mode.
    **                  isActive: Whether the secure element is activated.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void notifyListenModeState (bool isActivated);

    /*******************************************************************************
    **
    ** Function:        notifyRfFieldEvent
    **
    ** Description:     Notify the NFC service about RF field events from the stack.
    **                  isActive: Whether any secure element is activated.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void notifyRfFieldEvent (bool isActive);

#if(NXP_EXTNS == TRUE)
    /*******************************************************************************
    **
    ** Function:        notifyEEReaderEvent
    **
    ** Description:     Notify the NFC service about Reader over SWP events from the stack.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void notifyEEReaderEvent (int evt, int data);

    /*******************************************************************************
    **
    ** Function:        initializeEeHandle
    **
    ** Description:     Set NFCEE handle.
    **
    ** Returns:         True if ok.
    **
    *******************************************************************************/
    bool initializeEeHandle ();
#endif

    /*******************************************************************************
    **
    ** Function:        resetRfFieldStatus ();
    **
    ** Description:     Resets the field status.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void resetRfFieldStatus ();

    /*******************************************************************************
    **
    ** Function:        storeUiccInfo
    **
    ** Description:     Store a copy of the execution environment information from the stack.
    **                  info: execution environment information.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void storeUiccInfo (tNFA_EE_DISCOVER_REQ& info);


    /*******************************************************************************
    **
    ** Function:        getUiccId
    **
    ** Description:     Get the ID of the secure element.
    **                  eeHandle: Handle to the secure element.
    **                  uid: Array to receive the ID.
    **
    ** Returns:         True if ok.
    **
    *******************************************************************************/
    bool getUiccId (tNFA_HANDLE eeHandle, jbyteArray& uid);

    /*******************************************************************************
    **
    ** Function:        notifyTransactionListenersOfAid
    **
    ** Description:     Notify the NFC service about a transaction event from secure element.
    **                  aid: Buffer contains application ID.
    **                  aidLen: Length of application ID.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void notifyTransactionListenersOfAid (const uint8_t* aid, uint8_t aidLen, const uint8_t* data, uint32_t dataLen,uint32_t evtSrc);

    /*******************************************************************************
    **
    ** Function:        notifyConnectivityListeners
    **
    ** Description:     Notify the NFC service about a connectivity event from secure element.
    **                  evtSrc: source of event UICC/eSE.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void notifyConnectivityListeners (uint8_t evtSrc);

    /*******************************************************************************
    **
    ** Function:        notifyEmvcoMultiCardDetectedListeners
    **
    ** Description:     Notify the NFC service about a multiple card presented to
    **                  Emvco reader.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void notifyEmvcoMultiCardDetectedListeners ();

    /*******************************************************************************
    **
    ** Function:        notifyTransactionListenersOfTlv
    **
    ** Description:     Notify the NFC service about a transaction event from secure element.
    **                  The type-length-value contains AID and parameter.
    **                  tlv: type-length-value encoded in Basic Encoding Rule.
    **                  tlvLen: Length tlv.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void notifyTransactionListenersOfTlv (const uint8_t* tlv, uint8_t tlvLen);


    /*******************************************************************************
    **
    ** Function:        connectionEventHandler
    **
    ** Description:     Receive card-emulation related events from stack.
    **                  event: Event code.
    **                  eventData: Event data.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void connectionEventHandler (uint8_t event, tNFA_CONN_EVT_DATA* eventData);


    /*******************************************************************************
    **
    ** Function:        applyRoutes
    **
    ** Description:     Read route data from XML and apply them again
    **                  to every secure element.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void applyRoutes ();


    /*******************************************************************************
    **
    ** Function:        setActiveSeOverride
    **
    ** Description:     Specify which secure element to turn on.
    **                  activeSeOverride: ID of secure element
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void setActiveSeOverride (uint8_t activeSeOverride);

    bool SecEle_Modeset(uint8_t type);
    /*******************************************************************************
    **
    ** Function:        routeToSecureElement
    **
    ** Description:     Adjust controller's listen-mode routing table so transactions
    **                  are routed to the secure elements as specified in route.xml.
    **
    ** Returns:         True if ok.
    **
    *******************************************************************************/
    bool routeToSecureElement ();


    /*******************************************************************************
    **
    ** Function:        isBusy
    **
    ** Description:     Whether NFC controller is routing listen-mode events or a pipe is connected.
    **
    ** Returns:         True if either case is true.
    **
    *******************************************************************************/
    bool isBusy ();


    /*******************************************************************************
    **
    ** Function         getActualNumEe
    **
    ** Description      Returns number of secure elements we know about.
    **
    ** Returns          Number of secure elements we know about.
    **
    *******************************************************************************/
    uint8_t getActualNumEe();


    /*******************************************************************************
    **
    ** Function         getSeVerInfo
    **
    ** Description      Gets version information and id for a secure element.  The
    **                  seIndex parmeter is the zero based index of the secure
    **                  element to get verion info for.  The version infommation
    **                  is returned as a string int the verInfo parameter.
    **
    ** Returns          ture on success, false on failure
    **
    *******************************************************************************/
    bool getSeVerInfo(int seIndex, char * verInfo, int verInfoSz, uint8_t * seid);


    /*******************************************************************************
    **
    ** Function:        isActivatedInListenMode
    **
    ** Description:     Can be used to determine if the SE is activated in listen mode
    **
    ** Returns:         True if the SE is activated in listen mode
    **
    *******************************************************************************/
    bool isActivatedInListenMode();

    /*******************************************************************************
    **
    ** Function:        isRfFieldOn
    **
    ** Description:     Can be used to determine if the SE is in an RF field
    **
    ** Returns:         True if the SE is activated in an RF field
    **
    *******************************************************************************/
    bool isRfFieldOn();

    /*******************************************************************************
    **
    ** Function:        setEseListenTechMask
    **
    ** Description:     Can be used to force ESE to only listen the specific
    **                  Technologies.
    **                  NFA_TECHNOLOGY_MASK_A       0x01
    **                  NFA_TECHNOLOGY_MASK_B       0x02
    **
    ** Returns:         True if listening is configured.
    **
    *******************************************************************************/
    bool setEseListenTechMask(uint8_t tech_mask);

    bool sendEvent(uint8_t event);
    /*******************************************************************************
    **
    ** Function:        getAtr
    **
    ** Description:     Can be used to get the ATR response from connected eSE
    **
    ** Returns:         True if ATR response is returned successfully
    **
    *******************************************************************************/
    bool getAtr(jint seID, uint8_t* recvBuffer, int32_t *recvBufferSize);
#if(NXP_EXTNS == TRUE)
    bool getNfceeHostTypeList (void);
    bool configureNfceeETSI12 ();
    void eSE_ClearAllPipe_handler(uint8_t host);
    /**********************************************************************************
     **
     ** Function:        getEeStatus
     **
     ** Description:     get the status of EE
     **
     ** Returns:         EE status .
     **
     **********************************************************************************/
    uint16_t getEeStatus(uint16_t eehandle);

    /**********************************************************************************
     **
     ** Function:        getUiccStatus
     **
     ** Description:     get the status of EE
     **
     ** Returns:         EE status .
     **
     **********************************************************************************/
    uicc_stat_t getUiccStatus(uint8_t selected_uicc);

    /*******************************************************************************
     **
     ** Function:        updateEEStatus
     **
     ** Description:     updateEEStatus
     **                  Reads EE related information from libnfc
     **                  and updates in JNI
     **
     ** Returns:         True if ok.
     **
    *******************************************************************************/
    bool updateEEStatus ();

    /*******************************************************************************
     **
     ** Function:        isTeckInfoReceived
     **
     ** Description:     isTeckInfoReceived
     **                  Checks if discovery_req_ntf received
     **                  for a given EE
     **
     ** Returns:         True if ok.
     **
     *******************************************************************************/
    bool isTeckInfoReceived (uint16_t eeHandle);

#endif

#if(NXP_EXTNS == TRUE)
    void etsiInitConfig();
    tNFC_STATUS etsiReaderConfig(int eeHandle);
    tNFC_STATUS etsiResetReaderConfig();

    /*******************************************************************************
    **
    ** Function:        enablePassiveListen
    **
    ** Description:     Enable or disable listening to Passive A/B
    **
    ** Returns:         True if ok.
    **
    *******************************************************************************/
    uint16_t enablePassiveListen (uint8_t event);

    uint16_t startThread(uint8_t thread_arg);

    bool            mPassiveListenEnabled;
    bool            meseUiccConcurrentAccess;
    IntervalTimer   mPassiveListenTimer;
    uint32_t        mPassiveListenTimeout;             //Retry timout value for passive listen enable timer
    uint8_t         mPassiveListenCnt;                 //Retry cnt for passive listen enable timer
    SyncEvent       mPassiveListenEvt;
    Mutex           mPassiveListenMutex;
    Mutex           mNfccStandbyMutex;
#endif
    jint getSETechnology(tNFA_HANDLE eeHandle);
    static const uint8_t UICC_ID = 0x02;
    static const uint8_t UICC2_ID = 0x04;
    static const uint8_t ESE_ID = 0x01;
    static const uint8_t DH_ID = 0x00;
#if(NXP_EXTNS == TRUE)
    static const uint8_t eSE_Compliancy_ETSI_9 = 9;
    static const uint8_t eSE_Compliancy_ETSI_12 = 12;
#endif

    void getEeHandleList(tNFA_HANDLE *list, uint8_t* count);

    tNFA_HANDLE getEseHandleFromGenericId(jint eseId);

    jint getGenericEseId(tNFA_HANDLE handle);
    uint8_t     mDownloadMode;
#if(NXP_EXTNS == TRUE)

    bool        meSESessionIdOk;
    void        setCPTimeout();
    SyncEvent   mRfFieldOffEvent;
    void        NfccStandByOperation(nfcc_standby_operation_t value);
    NFCSTATUS   eSE_Chip_Reset(void);
    tNFA_STATUS SecElem_sendEvt_Abort();
    tNFA_STATUS reconfigureEseHciInit();
#endif
    bool checkForWiredModeAccess();
#if(NXP_EXTNS == TRUE)
    se_apdu_gate_info getApduGateInfo();
#endif
    SyncEvent       mRoutingEvent;
    SyncEvent       mAidAddRemoveEvent;
    SyncEvent       mUiccListenEvent;
    SyncEvent       mEseListenEvent;
    SyncEvent       mEeSetModeEvent;
    SyncEvent       mModeSetNtf;
    SyncEvent       mHciAddStaticPipe;
    SyncEvent       mApduPaternAddRemoveEvent;
#if ((NXP_EXTNS == TRUE))
    SyncEvent       mPwrLinkCtrlEvent;
#endif

#if(NXP_EXTNS == TRUE)
    uint8_t getUiccGateAndPipeList(uint8_t uiccNo);
    /*******************************************************************************
    **
    ** Function:        updateNfceeDiscoverInfo
    **
    ** Description:     Update the gSeDiscoverycount based on new NFCEE handle
    **                  discovered
    **
    ** Returns:         Count of NFCEE discovered.
    **
    *******************************************************************************/
    uint8_t updateNfceeDiscoverInfo();
    tNFA_HANDLE getHciHandleInfo();
    SyncEvent       mNfceeInitCbEvent;
    tNFA_STATUS SecElem_EeModeSet(uint16_t handle, uint8_t mode);
    SyncEvent       mEEdatapacketEvent;
    SyncEvent       mTransceiveEvent;
    static const uint8_t EVT_END_OF_APDU_TRANSFER = 0x21;    //NXP Propritory
    bool            mIsWiredModeOpen;
    bool            mlistenDisabled;
    bool            mIsExclusiveWiredMode;
    bool            mIsAllowWiredInDesfireMifareCE;
    static const uint8_t EVT_ABORT = 0x11;  //ETSI12
    static const uint8_t EVT_ABORT_MAX_RSP_LEN = 40;
    bool    mIsWiredModeBlocked;   /* for wired mode resume feature support */
    IntervalTimer   mRfFieldEventTimer;
    uint32_t        mRfFieldEventTimeout;
    tNFA_STATUS  mModeSetInfo;/*Mode set info status*/
    static const uint8_t NFCC_DECIDES     = 0x00;     //NFCC decides
    static const uint8_t POWER_ALWAYS_ON  = 0x01;     //NFCEE Power Supply always On
    static const uint8_t COMM_LINK_ACTIVE = 0x02;     //NFCC to NFCEE Communication link always active when the NFCEE  is powered on.
    static const uint8_t EVT_SUSPEND_APDU_TRANSFER = 0x31;
    tNFA_STATUS  mPwrCmdstatus;     //completion status of the power link control command
    uint8_t      mNfccPowerMode;
    tNFA_STATUS  setNfccPwrConfig(uint8_t value);
    bool mIsIntfRstEnabled;
    void setCLState(bool mState);
    void setDwpTranseiveState(bool state, tNFCC_EVTS_NTF action);
#endif

private:
    static const unsigned int MAX_RESPONSE_SIZE = 0x8800;//1024; //34K
    enum RouteSelection {NoRoute, DefaultRoute, SecElemRoute};
#ifndef GEMATO_SE_SUPPORT
    static const uint8_t STATIC_PIPE_0x70 = 0x19; //PN54X Gemalto's proprietary static pipe
#else
    static const uint8_t STATIC_PIPE_0x70 = 0x70; //Broadcom's proprietary static pipe
#endif
    static const uint8_t STATIC_PIPE_0x71 = 0x71; //Broadcom's proprietary static pipe
    static const uint8_t EVT_SEND_DATA = 0x10;    //see specification ETSI TS 102 622 v9.0.0 (Host Controller Interface); section 9.3.3.3
#if(NXP_EXTNS == TRUE)
    static const uint8_t STATIC_PIPE_UICC = 0x20; //UICC's proprietary static pipe
    static const tNFA_HANDLE EE_HANDLE_HCI  = 0x401;
    static const tNFA_HANDLE EE_HANDLE_NDEFEE = 0x410;
#else
    static const tNFA_HANDLE EE_HANDLE_0xF3 = 0x4F3; //handle to secure element in slot 0
    static const tNFA_HANDLE EE_HANDLE_0xF4 = 0x4F4; //handle to secure element in slot 1
#endif

    static SecureElement sSecElem;
    static const char* APP_NAME;

    uint8_t         mDestinationGate;       //destination gate of the UICC
    tNFA_HANDLE     mNfaHciHandle;          //NFA handle to NFA's HCI component
    nfc_jni_native_data* mNativeData;
    bool    mIsInit;                // whether EE is initialized
    uint8_t mActualNumEe;           // actual number of EE's reported by the stack
    uint8_t mNumEePresent;          // actual number of usable EE's
    bool    mbNewEE;
    uint8_t mNewPipeId;
    uint8_t mNewSourceGate;
    uint16_t  mActiveSeOverride;      // active "enable" seid, 0 means activate all SEs
    tNFA_STATUS mCommandStatus;     //completion status of the last command
    bool    mIsPiping;              //is a pipe connected to the controller?
    RouteSelection mCurrentRouteSelection;
    int     mActualResponseSize;         //number of bytes in the response received from secure element
    int     mAtrInfolen;
    uint8_t mAtrStatus;
    bool    mUseOberthurWarmReset;  //whether to use warm-reset command
    bool    mActivatedInListenMode; // whether we're activated in listen mode
    uint8_t mOberthurWarmResetCommand; //warm-reset command byte
    tNFA_EE_INFO mEeInfo [MAX_NUM_EE];  //actual size stored in mActualNumEe
    tNFA_EE_DISCOVER_REQ mUiccInfo;
    tNFA_HCI_GET_GATE_PIPE_LIST mHciCfg;
    SyncEvent       mEeRegisterEvent;
    SyncEvent       mHciRegisterEvent;
    SyncEvent       mResetEvent;
    SyncEvent       mResetOngoingEvent;
    SyncEvent       mPipeListEvent;
#if(NXP_EXTNS != TRUE)
    SyncEvent       mCreatePipeEvent;
    SyncEvent       mPipeOpenedEvent;
#endif
    SyncEvent       mAllocateGateEvent;
    SyncEvent       mDeallocateGateEvent;
//    SyncEvent       mRoutingEvent;
    SyncEvent       mUiccInfoEvent;
//    SyncEvent       mAidAddRemoveEvent;
    SyncEvent       mGetRegisterEvent;
    SyncEvent       mVerInfoEvent;
    SyncEvent       mRegistryEvent;
    SyncEvent       mDiscMapEvent;
    uint8_t         mVerInfo [3];
    uint8_t         mAtrInfo[40];
    bool            mGetAtrRspwait;
    uint8_t         mResponseData [MAX_RESPONSE_SIZE];
    RouteDataSet    mRouteDataSet; //routing data
    std::vector<std::string> mUsedAids; //AID's that are used in current routes
    uint8_t         mAidForEmptySelect[NCI_MAX_AID_LEN+1];
    Mutex           mMutex; // protects fields below
    bool            mRfFieldIsOn; // last known RF field state
    struct timespec mLastRfFieldToggle; // last time RF field went off
    IntervalTimer   mTransceiveTimer;
    bool            mTransceiveWaitOk;

#if(NXP_EXTNS == TRUE)
#define             WIRED_MODE_TRANSCEIVE_TIMEOUT 120000
#endif
    /*******************************************************************************
    **
    ** Function:        SecureElement
    **
    ** Description:     Initialize member variables.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    SecureElement ();


    /*******************************************************************************
    **
    ** Function:        ~SecureElement
    **
    ** Description:     Release all resources.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    ~SecureElement ();

    /*******************************************************************************
    **
    ** Function:        handleClearAllPipe
    **
    ** Description:     To handle clear all pipe event received from HCI based on the
    **                  deleted host
    **                  eventData: Event data.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    static void handleClearAllPipe (tNFA_HCI_EVT_DATA* eventData);

    /*******************************************************************************
    **
    ** Function:        nfaEeCallback
    **
    ** Description:     Receive execution environment-related events from stack.
    **                  event: Event code.
    **                  eventData: Event data.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    static void nfaEeCallback (tNFA_EE_EVT event, tNFA_EE_CBACK_DATA* eventData);


    /*******************************************************************************
    **
    ** Function:        nfaHciCallback
    **
    ** Description:     Receive Host Controller Interface-related events from stack.
    **                  event: Event code.
    **                  eventData: Event data.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    static void nfaHciCallback (tNFA_HCI_EVT event, tNFA_HCI_EVT_DATA* eventData);


    /*******************************************************************************
    **
    ** Function:        findEeByHandle
    **
    ** Description:     Find information about an execution environment.
    **                  eeHandle: Handle to execution environment.
    **
    ** Returns:         Information about an execution environment.
    **
    *******************************************************************************/
    tNFA_EE_INFO *findEeByHandle (tNFA_HANDLE eeHandle);


    /*******************************************************************************
    **
    ** Function:        findUiccByHandle
    **
    ** Description:     Find information about an execution environment.
    **                  eeHandle: Handle of the execution environment.
    **
    ** Returns:         Information about the execution environment.
    **
    *******************************************************************************/
    tNFA_EE_DISCOVER_INFO *findUiccByHandle (tNFA_HANDLE eeHandle);


    /*******************************************************************************
    **
    ** Function:        getDefaultEeHandle
    **
    ** Description:     Get the handle to the execution environment.
    **
    ** Returns:         Handle to the execution environment.
    **
    *******************************************************************************/
    tNFA_HANDLE getDefaultEeHandle ();


#if(NXP_EXTNS == TRUE)
    /*******************************************************************************
    **
    ** Function:        getActiveEeHandle
    **
    ** Description:     Get the handle of the active execution environment.
    **
    ** Returns:         Handle to the execution environment.
    **
    *******************************************************************************/
    tNFA_HANDLE getActiveEeHandle (tNFA_HANDLE eeHandle);
#endif
    /*******************************************************************************
    **
    ** Function:        adjustRoutes
    **
    ** Description:     Adjust routes in the controller's listen-mode routing table.
    **                  selection: which set of routes to configure the controller.
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void adjustRoutes (RouteSelection selection);


    /*******************************************************************************
    **
    ** Function:        adjustProtocolRoutes
    **
    ** Description:     Adjust default routing based on protocol in NFC listen mode.
    **                  isRouteToEe: Whether routing to EE (true) or host (false).
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void adjustProtocolRoutes (RouteDataSet::Database* db, RouteSelection routeSelection);


    /*******************************************************************************
    **
    ** Function:        adjustTechnologyRoutes
    **
    ** Description:     Adjust default routing based on technology in NFC listen mode.
    **                  isRouteToEe: Whether routing to EE (true) or host (false).
    **
    ** Returns:         None
    **
    *******************************************************************************/
    void adjustTechnologyRoutes (RouteDataSet::Database* db, RouteSelection routeSelection);


    /*******************************************************************************
    **
    ** Function:        getEeInfo
    **
    ** Description:     Get latest information about execution environments from stack.
    **
    ** Returns:         True if at least 1 EE is available.
    **
    *******************************************************************************/
    bool getEeInfo ();

    /*******************************************************************************
    **
    ** Function:        eeStatusToString
    **
    ** Description:     Convert status code to status text.
    **                  status: Status code
    **
    ** Returns:         None
    **
    *******************************************************************************/
    static const char* eeStatusToString (uint8_t status);


    /*******************************************************************************
    **
    ** Function:        encodeAid
    **
    ** Description:     Encode AID in type-length-value using Basic Encoding Rule.
    **                  tlv: Buffer to store TLV.
    **                  tlvMaxLen: TLV buffer's maximum length.
    **                  tlvActualLen: TLV buffer's actual length.
    **                  aid: Buffer of Application ID.
    **                  aidLen: Aid buffer's actual length.
    **
    ** Returns:         True if ok.
    **
    *******************************************************************************/
    bool encodeAid (uint8_t* tlv, uint16_t tlvMaxLen, uint16_t& tlvActualLen, const uint8_t* aid, uint8_t aidLen);

    static int decodeBerTlvLength(uint8_t* data,int index, int data_length );

    static void discovery_map_cb (tNFC_DISCOVER_EVT event, tNFC_DISCOVER *p_data);


};