blob: f694300fd3441e088657b48edd528714543ec651 [file] [log] [blame]
nxpandroid64fd68c2015-09-23 16:45:15 +05301/******************************************************************************
nxf24591dc0bc2c2018-02-21 17:33:08 +05302*
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*
15* Copyright 2018 NXP
16*
17******************************************************************************/
18
nxpandroid64fd68c2015-09-23 16:45:15 +053019#pragma once
nxf24591dc0bc2c2018-02-21 17:33:08 +053020#include "nfa_hci_api.h"
21#include "nfa_hci_defs.h"
nxpandroid64fd68c2015-09-23 16:45:15 +053022#include "NfcJniUtil.h"
nxf24591dc0bc2c2018-02-21 17:33:08 +053023#include "nfc_api.h"
24#include "config.h"
25#include "phNxpConfig.h"
26#include "SyncEvent.h"
27#include "nfa_ee_api.h"
28#include "nfa_hci_api.h"
29#include "nfa_hci_defs.h"
30#include "nfa_ce_api.h"
31#include "phNxpExtns.h"
32#include "phNfcTypes.h"
33
34#define MAX_NFCEE 5
35#define WIRED_MODE_TRANSCEIVE_TIMEOUT 120000
nxpandroid1680a6d2017-01-13 19:13:14 +053036#define CONNECTIVITY_PIPE_ID_UICC1 0x0A
37#define CONNECTIVITY_PIPE_ID_UICC2 0x23
nxf24591dc0bc2c2018-02-21 17:33:08 +053038#define CONNECTIVITY_PIPE_ID_UICC3 0x31
39#define NFA_EE_TAG_HCI_HOST_ID 0xA0 /* HCI host ID */
40#define SMX_PIPE_ID 0x19
nxpandroid1e2a13c2017-04-10 18:19:00 +053041
nxf24591dc0bc2c2018-02-21 17:33:08 +053042class SecureElement {
nxpandroid64fd68c2015-09-23 16:45:15 +053043public:
nxf24591dc0bc2c2018-02-21 17:33:08 +053044 tNFA_HANDLE mActiveEeHandle;
nxpandroid64fd68c2015-09-23 16:45:15 +053045
nxf24591dc0bc2c2018-02-21 17:33:08 +053046 static const uint8_t UICC_ID = 0x02;
47 static const uint8_t UICC2_ID = 0x04;
48 static const uint8_t UICC3_ID = 0x08;
49 static const uint8_t ESE_ID = 0x01;
50 static const uint8_t DH_ID = 0x00;
51 static const uint8_t NFCC_DECIDES = 0x00; //NFCC decides
52 static const uint8_t POWER_ALWAYS_ON = 0x01; //NFCEE Power Supply always On
53 static const uint8_t COMM_LINK_ACTIVE = 0x02; //NFCC to NFCEE Communication link always active when the NFCEE is powered on.
54 static const uint8_t EVT_END_OF_APDU_TRANSFER = 0x61;
nxpandroid82f7e9e2018-04-05 14:54:41 +053055 static const uint8_t NFCEE_DISABLE = 0x00;
56 static const uint8_t NFCEE_ENABLE = 0x01;
nxf24591dc0bc2c2018-02-21 17:33:08 +053057 tNFA_STATUS mPwrCmdstatus; //completion status of the power link control command
58 uint8_t mNfccPowerMode;
59 uint8_t mNewPipeId;
nxpandroid64fd68c2015-09-23 16:45:15 +053060
nxf24591dc0bc2c2018-02-21 17:33:08 +053061 bool mIsWiredModeOpen;
Sreenivas426138e2018-04-04 14:56:08 +053062 uint32_t SmbTransceiveTimeOutVal;/* maximum time to wait for APDU response */
nxpandroid64fd68c2015-09-23 16:45:15 +053063
nxf24591dc0bc2c2018-02-21 17:33:08 +053064 SyncEvent mPwrLinkCtrlEvent;
65 tNFA_HANDLE EE_HANDLE_0xF4; //handle to secure element in slot 1
66 static const tNFA_HANDLE EE_HANDLE_0xF3 = 0x4C0;//0x401; //handle to secure element in slot 0
67 static const tNFA_HANDLE EE_HANDLE_0xF8 = 0x481; //handle to secure element in slot 2
68 static const tNFA_HANDLE EE_HANDLE_0xF9 = 0x482; //handle to secure element in slot 3
69 static const tNFA_HANDLE EE_HANDLE_0xF0 = 0x400;//NFCEE handle for host
70 static const uint8_t EE_APP_HANLDE_ESE = 0xF3;
71 static const uint8_t EE_APP_HANLDE_UICC = 0xF4;
72 static const uint8_t EE_APP_HANLDE_UICC2 = 0xF8;
73 static const uint8_t EE_APP_HANLDE_UICC3 = 0xF9;
74 uint8_t muicc2_selected; /* UICC2 or UICC3 selected from config file*/
75/*******************************************************************************
76**
77** Function: getGenericEseId
78**
79** Description: Whether controller is routing listen-mode events to
80** secure elements or a pipe is connected.
81**
82** Returns: Return the generic SE id ex:- 00,01,02,04
83**
84*******************************************************************************/
85jint getGenericEseId(tNFA_HANDLE handle);
nxpandroid64fd68c2015-09-23 16:45:15 +053086
nxf24591dc0bc2c2018-02-21 17:33:08 +053087jint getSETechnology(tNFA_HANDLE eeHandle);
nxpandroid64fd68c2015-09-23 16:45:15 +053088
nxf24591dc0bc2c2018-02-21 17:33:08 +053089void getEeHandleList(tNFA_HANDLE *list, uint8_t* count);
nxpandroid64fd68c2015-09-23 16:45:15 +053090
nxf24591dc0bc2c2018-02-21 17:33:08 +053091 private:
92 static SecureElement sSecElem;
93 static const char* APP_NAME;
94 static const tNFA_HANDLE EE_HANDLE_ESE = 0x4C0;
95 static const tNFA_HANDLE EE_HANDLE_UICC = 0x480;
96 static const uint8_t NFCEE_ID_ESE = 0x01;
97 static const uint8_t NFCEE_ID_UICC = 0x02;
98 static const int MAX_NUM_EE = NFA_EE_MAX_EE_SUPPORTED; /*max number of EE's*/
99 static const unsigned int MAX_RESPONSE_SIZE = 0x8800;//1024; //34K
100 static const uint8_t STATIC_PIPE_0x71 = 0x71; //Broadcom's proprietary static pipe
101 static const uint8_t EVT_ABORT_MAX_RSP_LEN = 40;
102 static const uint8_t EVT_ABORT = 0x11; //ETSI12
103 static const uint8_t STATIC_PIPE_UICC = 0x20; //UICC's proprietary static pipe
104 const uint16_t ACTIVE_SE_USE_ANY = 0xFFFF;
nxpandroid64fd68c2015-09-23 16:45:15 +0530105
nxf24591dc0bc2c2018-02-21 17:33:08 +0530106 nfc_jni_native_data* mNativeData;
107 nfc_jni_native_data* mthreadnative;
nxpandroid64fd68c2015-09-23 16:45:15 +0530108
nxf24591dc0bc2c2018-02-21 17:33:08 +0530109 bool mbNewEE;
110 bool mIsInit; // whether EE is initialized
111 bool mTransceiveWaitOk;
112 bool mGetAtrRspwait;
113 bool mAbortEventWaitOk;
114 bool mIsPiping; //is a pipe connected to the controller?
nxpandroid64fd68c2015-09-23 16:45:15 +0530115
nxf24591dc0bc2c2018-02-21 17:33:08 +0530116 tNFA_HCI_GET_GATE_PIPE_LIST mHciCfg;
117 tNFA_STATUS mCommandStatus; //completion status of the last command
118 tNFA_HANDLE mNfaHciHandle; //NFA handle to NFA's HCI component
119 tNFA_EE_INFO mEeInfo [MAX_NUM_EE]; //actual size stored in mActualNumEe
nxpandroid64fd68c2015-09-23 16:45:15 +0530120
nxf24591dc0bc2c2018-02-21 17:33:08 +0530121 SyncEvent mCreatePipeEvent;
122 SyncEvent mAllocateGateEvent;
123 SyncEvent mDeallocateGateEvent;
124 SyncEvent mPipeListEvent;
125 SyncEvent mHciRegisterEvent;
126 SyncEvent mPipeOpenedEvent;
127 SyncEvent mTransceiveEvent;
128 SyncEvent mEeSetModeEvent;
129 SyncEvent mAbortEvent;
130 SyncEvent mVerInfoEvent;
131 SyncEvent mGetRegisterEvent;
132 SyncEvent mRegistryEvent;
133 SyncEvent mWiredModeHoldEvent;
134 SyncEvent mModeSetNtf;
nxpandroid64fd68c2015-09-23 16:45:15 +0530135
nxf24591dc0bc2c2018-02-21 17:33:08 +0530136 int mActualResponseSize; //number of bytes in the response received from secure element
137 int mAtrInfolen;
nxpandroid64fd68c2015-09-23 16:45:15 +0530138
nxf24591dc0bc2c2018-02-21 17:33:08 +0530139 uint8_t mNewSourceGate;
140 uint8_t mActualNumEe; // actual number of EE's reported by the stack
141 uint8_t mAidForEmptySelect[NCI_MAX_AID_LEN+1];
142 uint8_t mDestinationGate; //destination gate of the UICC
143 uint8_t mAtrStatus;
144 uint8_t mVerInfo [3];
145 uint8_t mAtrInfo[40];
146 uint8_t mResponseData [MAX_RESPONSE_SIZE];
147 uint8_t mNumEePresent; // actual number of usable EE's
148 uint8_t mCreatedPipe;
149 static uint8_t mStaticPipeProp;
nxpandroidecca0112018-06-06 14:41:40 +0530150 Mutex mMutex; // protects fields below
151 bool mRfFieldIsOn; // last known RF field state
152 struct timespec mLastRfFieldToggle; // last time RF field went off
nxpandroid64fd68c2015-09-23 16:45:15 +0530153
Sreenivas426138e2018-04-04 14:56:08 +0530154
nxf24591dc0bc2c2018-02-21 17:33:08 +0530155struct mNfceeData
156{
157 tNFA_HANDLE mNfceeHandle[MAX_NFCEE];
158 tNFA_EE_STATUS mNfceeStatus[MAX_NFCEE];
159 uint8_t mNfceePresent;
160};
161mNfceeData mNfceeData_t;
162/*******************************************************************************
163**
164** Function: SecureElement
165**
166** Description: Initialize member variables.
167**
168** Returns: None
169**
170*******************************************************************************/
171SecureElement();
nxpandroid64fd68c2015-09-23 16:45:15 +0530172
nxf24591dc0bc2c2018-02-21 17:33:08 +0530173/*******************************************************************************
174**
175** Function: decodeBerTlvLength
176**
177** Description: Decodes BER TLV length from the data provided
178** data : array of data to be processed
179** index : offset from which to consider processing
180** data_length : length of data to be processed
181**
182** Returns: decoded_length
183**
184*******************************************************************************/
185static int decodeBerTlvLength(uint8_t* data, int index, int data_length);
nxpandroid64fd68c2015-09-23 16:45:15 +0530186
nxf24591dc0bc2c2018-02-21 17:33:08 +0530187/*******************************************************************************
188**
189** Function: notifyTransactionListenersOfAid
190**
191** Description: Notify the NFC service about a transaction event from
192** secure element.
193** aid: Buffer contains application ID.
194** aidLen: Length of application ID.
195**
196** Returns: None
197**
198*******************************************************************************/
199void notifyTransactionListenersOfAid(const uint8_t* aidBuffer,
200 uint8_t aidBufferLen,
201 const uint8_t* dataBuffer,
202 uint32_t dataBufferLen, uint32_t evtSrc);
nxpandroid64fd68c2015-09-23 16:45:15 +0530203
nxf24591dc0bc2c2018-02-21 17:33:08 +0530204/*******************************************************************************
205**
206** Function: nfaHciCallback
207**
208** Description: Receive HCI-related events from stack.
209** event: Event code.
210** eventData: Event data.
211**
212** Returns: None
213**
214*******************************************************************************/
215static void nfaHciCallback(tNFA_HCI_EVT event, tNFA_HCI_EVT_DATA* eventData);
nxpandroid64fd68c2015-09-23 16:45:15 +0530216
nxf24591dc0bc2c2018-02-21 17:33:08 +0530217public:
nxpandroidecca0112018-06-06 14:41:40 +0530218bool mActivatedInListenMode; // whether we're activated in listen mode
nxf24591dc0bc2c2018-02-21 17:33:08 +0530219/*******************************************************************************
220**
221** Function: getInstance
222**
223** Description: Get the SecureElement singleton object.
224**
225** Returns: SecureElement object.
226**
227*******************************************************************************/
228static SecureElement& getInstance();
229/*******************************************************************************
230**
231** Function: initialize
232**
233** Description: Initialize all member variables.
234** native: Native data.
235**
236** Returns: True if ok.
237**
238*******************************************************************************/
239bool initialize(nfc_jni_native_data* native);
nxpandroid64fd68c2015-09-23 16:45:15 +0530240
nxf24591dc0bc2c2018-02-21 17:33:08 +0530241/*******************************************************************************
242**
243** Function: connectEE
244**
245** Description: Connect to the execution environment.
246**
247** Returns: True if ok.
248**
249*******************************************************************************/
250bool connectEE ();
251/*******************************************************************************
252**
253** Function: disconnectEE
254**
255** Description: Disconnect from the execution environment.
256** seID: ID of secure element.
257**
258** Returns: True if ok.
259**
260*******************************************************************************/
261bool disconnectEE (jint seID);
262/*******************************************************************************
263**
264** Function: transceive
265**
266** Description: Send data to the secure element; read it's response.
267** xmitBuffer: Data to transmit.
268** xmitBufferSize: Length of data.
269** recvBuffer: Buffer to receive response.
270** recvBufferMaxSize: Maximum size of buffer.
271** recvBufferActualSize: Actual length of response.
272** timeoutMillisec: timeout in millisecond.
273**
274** Returns: True if ok.
275**
276*******************************************************************************/
277bool transceive (uint8_t* xmitBuffer, int32_t xmitBufferSize, uint8_t* recvBuffer,
278 int32_t recvBufferMaxSize, int32_t& recvBufferActualSize, int32_t timeoutMillisec);
279/*******************************************************************************
280**
281** Function: activate
282**
283** Description: Turn on the secure element.
284** seID: ID of secure element; 0xF3 or 0xF4.
285**
286** Returns: True if ok.
287**
288*******************************************************************************/
289bool activate (jint seID);
290/*******************************************************************************
291**
Ayush73d68912018-04-04 21:36:26 +0530292** Function: getActiveSecureElementList
293**
294** Description: Get the list of handles of all execution environments.
295** e: Java Virtual Machine.
296**
297** Returns: List of handles of all execution environments.
298**
299*******************************************************************************/
300jintArray getActiveSecureElementList (JNIEnv* e);
301
302/*******************************************************************************
303**
nxf24591dc0bc2c2018-02-21 17:33:08 +0530304** Function: deactivate
305**
306** Description: Turn off the secure element.
307** seID: ID of secure element; 0xF3 or 0xF4.
308**
309** Returns: True if ok.
310**
311*******************************************************************************/
312bool deactivate (jint seID);
313/*******************************************************************************
314**
315** Function: SecElem_EeModeSet
316**
317** Description: Perform SE mode set ON/OFF based on mode type
318**
319** Returns: NFA_STATUS_OK/NFA_STATUS_FAILED.
320**
321*******************************************************************************/
322tNFA_STATUS SecElem_EeModeSet(uint16_t handle, uint8_t mode);
323/*******************************************************************************
324**
325** Function: getAtr
326**
327** Description: GetAtr response from the connected eSE
328**
329** Returns: Returns True if success
330**
331*******************************************************************************/
332bool getAtr(jint seID, uint8_t* recvBuffer, int32_t *recvBufferSize);
333/*******************************************************************************
334 **
335 ** Function: SecElem_EeModeSet
336 **
337 ** Description: Perform SE mode set ON/OFF based on mode type
338 **
339 ** Returns: NFA_STATUS_OK/NFA_STATUS_FAILED.
340 **
341 *******************************************************************************/
342bool SecEle_Modeset(uint8_t type);
343/*******************************************************************************
Suraj Uday Kotharkar2a0ee9c2018-04-27 03:48:23 +0530344 **
345 ** Function: notifyRfFieldEvent
346 **
347 ** Description: Notify the NFC service about RF field events from the stack.
348 ** isActive: Whether any secure element is activated.
349 **
350 ** Returns: None
351 **
352*******************************************************************************/
353 void notifyRfFieldEvent (bool isActive);
354/*******************************************************************************
nxf24591dc0bc2c2018-02-21 17:33:08 +0530355**
356** Function: initializeEeHandle
357**
358** Description: Set NFCEE handle.
359**
360** Returns: True if ok.
361**
362*******************************************************************************/
363bool initializeEeHandle ();
364/*******************************************************************************
365**
366** Function: getEseHandleFromGenericId
367**
368** Description: Whether controller is routing listen-mode events to
369** secure elements or a pipe is connected.
370**
371** Returns: Returns Secure element Handle ex:- 402, 4C0, 481
372**
373*******************************************************************************/
374tNFA_HANDLE getEseHandleFromGenericId(jint eseId);
375/*******************************************************************************
376**
377** Function: getEeInfo
378**
379** Description: Get latest information about execution environments from stack.
380**
381** Returns: True if at least 1 EE is available.
382**
383*******************************************************************************/
384bool getEeInfo ();
385/*******************************************************************************
386**
nxpandroidecca0112018-06-06 14:41:40 +0530387** Function: isRfFieldOn
388**
389** Description: Can be used to determine if the SE is in an RF field
390**
391** Returns: True if the SE is activated in an RF field
392**
393*******************************************************************************/
394bool isRfFieldOn();
395/*******************************************************************************
396**
nxf24591dc0bc2c2018-02-21 17:33:08 +0530397** Function: findEeByHandle
398**
399** Description: Find information about an execution environment.
400** eeHandle: Handle to execution environment.
401**
402** Returns: Information about an execution environment.
403**
404*******************************************************************************/
405tNFA_EE_INFO *findEeByHandle (tNFA_HANDLE eeHandle);
406/*******************************************************************************
407**
Suraj Uday Kotharkar2a0ee9c2018-04-27 03:48:23 +0530408** Function: notifyListenModeState
409**
410** Description: Notify the NFC service about whether the SE was activated
411** in listen mode.
412** isActive: Whether the secure element is activated.
413**
414** Returns: None
415**
416*******************************************************************************/
417void notifyListenModeState (bool isActivated);
418/*******************************************************************************
nxf24591dc0bc2c2018-02-21 17:33:08 +0530419** Function: getActiveEeHandle
420**
421** Description: Get the handle of the active execution environment.
422**
423** Returns: Handle to the execution environment.
424**
425*******************************************************************************/
426tNFA_HANDLE getActiveEeHandle (tNFA_HANDLE eeHandle);
nxpandroidecca0112018-06-06 14:41:40 +0530427 /*******************************************************************************
428 **
429 ** Function getLastRfFiledToggleTime
430 **
431 ** Description Provides the last RF filed toggile timer
432 **
433 ** Returns timespec
434 **
435 *******************************************************************************/
436 struct timespec getLastRfFiledToggleTime(void);
nxf24591dc0bc2c2018-02-21 17:33:08 +0530437/*******************************************************************************
438**
439** Function setNfccPwrConfig
440**
441** Description sends the link cntrl command to eSE with the value passed
442**
443** Returns status
444**
445*******************************************************************************/
446tNFA_STATUS setNfccPwrConfig(uint8_t value);
447/*******************************************************************************
448**
449** Function sendEvent
450**
451** Description sends the HCI event
452**
453** Returns status
454**
455*******************************************************************************/
456bool sendEvent(uint8_t event);
457/*******************************************************************************
458 **
459 ** Function: notifyModeSet
460 **
461 ** Description: Perform SE mode set ON/OFF based on mode type
462 **
463 ** Returns: NFA_STATUS_OK/NFA_STATUS_FAILED.
464 **
465 *******************************************************************************/
466void notifyModeSet (tNFA_HANDLE eeHandle, bool success, tNFA_EE_STATUS eeStatus);
467/*******************************************************************************
468**
469** Function: getGateAndPipeList
470**
471** Description: Get the gate and pipe list.
472**
473** Returns: None
474**
475*******************************************************************************/
476uint8_t getGateAndPipeList();
477/*******************************************************************************
478**
479** Function: finalize
480**
481** Description: Release all resources.
482**
483** Returns: None
484**
485*******************************************************************************/
486void finalize();
nxpandroid64fd68c2015-09-23 16:45:15 +0530487};