diff options
author | Martijn Coenen <maco@google.com> | 2012-07-25 15:07:43 -0700 |
---|---|---|
committer | Martijn Coenen <maco@google.com> | 2012-07-27 18:33:54 -0700 |
commit | c981340b7157e1b1873ca741f37b1ff19edae0b8 (patch) | |
tree | 7a2d34db60e0ac35e458b7f7f146b6291969a7fa /nci/jni/SecureElement.h | |
parent | 7a1d2adf875d414625853b3a5360663e420a3769 (diff) | |
download | packages_apps_nfc-c981340b7157e1b1873ca741f37b1ff19edae0b8.zip packages_apps_nfc-c981340b7157e1b1873ca741f37b1ff19edae0b8.tar.gz packages_apps_nfc-c981340b7157e1b1873ca741f37b1ff19edae0b8.tar.bz2 |
NFC: Initial NCI DeviceHost and JNI implementation.
From partner drop at 07/20. Modified to fit into our new
JNI/DH split.
New build config that builds two targets,
Nfc and NfcNci, each with their own dependencies. Product config files
have to specify either Nfc or NfcNci in their packages config.
Change-Id: I348a3aad7167195ca03baf9636408ab8e4c55fce
Diffstat (limited to 'nci/jni/SecureElement.h')
-rwxr-xr-x | nci/jni/SecureElement.h | 560 |
1 files changed, 560 insertions, 0 deletions
diff --git a/nci/jni/SecureElement.h b/nci/jni/SecureElement.h new file mode 100755 index 0000000..3542ac7 --- /dev/null +++ b/nci/jni/SecureElement.h @@ -0,0 +1,560 @@ +/***************************************************************************** +** +** Name: SecureElement.h +** +** Description: Communicate with secure elements that are attached +** to the NFC controller. +** +** Copyright (c) 2012, Broadcom Corp., All Rights Reserved. +** Proprietary and confidential. +** +*****************************************************************************/ +#pragma once +#include "SyncEvent.h" +#include "DataQueue.h" +#include "NfcJniUtil.h" +#include "RouteDataSet.h" +extern "C" +{ + #include "nfa_brcm_api.h" + #include "nfa_ee_api.h" + #include "nfa_hci_api.h" + #include "nfa_hci_defs.h" + #include "nfa_ce_api.h" +} + +#define MAX_TRANS_RECV_SIZE 1024 + +class SecureElement +{ +public: + tNFA_HANDLE mActiveEeHandle; + + + /******************************************************************************* + ** + ** 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: 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 (); + + + /******************************************************************************* + ** + ** 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. + ** + *******************************************************************************/ + bool transceive (UINT8* xmitBuffer, INT32 xmitBufferSize, UINT8* recvBuffer, + INT32 recvBufferMaxSize, INT32& recvBufferActualSize, INT32 timeoutMillisec); + + + /******************************************************************************* + ** + ** 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); + + + /******************************************************************************* + ** + ** 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: getTechnologyList + ** + ** Description: Get all the technologies supported by a secure element. + ** eeHandle: Handle of secure element. + ** techList: List to receive the technologies. + ** + ** Returns: True if ok. + ** + *******************************************************************************/ + bool getTechnologyList (tNFA_HANDLE eeHandle, jintArray& techList); + + + /******************************************************************************* + ** + ** 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* aid, UINT8 aidLen); + + + /******************************************************************************* + ** + ** 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* tlv, UINT8 tlvLen); + + + /******************************************************************************* + ** + ** Function: connectionEventHandler + ** + ** Description: Receive card-emulation related events from stack. + ** event: Event code. + ** eventData: Event data. + ** + ** Returns: None + ** + *******************************************************************************/ + void connectionEventHandler (UINT8 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 activeSeOverride); + + + /******************************************************************************* + ** + ** 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: routeToDefault + ** + ** Description: Adjust controller's listen-mode routing table so transactions + ** are routed to the default destination specified in route.xml. + ** + ** Returns: True if ok. + ** + *******************************************************************************/ + bool routeToDefault (); + + + /******************************************************************************* + ** + ** 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 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 * seid); + + +private: + enum RouteSelection {NoRoute, DefaultRoute, SecElemRoute}; + static const int MAX_NUM_EE = 5; //max number of EE's + static const UINT8 STATIC_PIPE_0x70 = 0x70; //Broadcom's proprietary static pipe + static const UINT8 STATIC_PIPE_0x71 = 0x71; //Broadcom's proprietary static pipe + static const UINT8 EVT_SEND_DATA = 0x10; //see specification ETSI TS 102 622 v9.0.0 (Host Controller Interface); section 9.3.3.3 + 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 + static SecureElement sSecElem; + + UINT8 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 mActualNumEe; // actual number of EE's reported by the stack + UINT8 mNumEePresent; // actual number of usable EE's + bool mbNewEE; + UINT8 mNewPipeId; + UINT8 mNewSourceGate; + UINT16 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 mTransDataSize; + 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 mEeSetModeEvent; + SyncEvent mPipeListEvent; + SyncEvent mCreatePipeEvent; + SyncEvent mPipeOpenedEvent; + SyncEvent mAllocateGateEvent; + SyncEvent mDeallocateGateEvent; + SyncEvent mRoutingEvent; + SyncEvent mUiccInfoEvent; + SyncEvent mUiccListenEvent; + SyncEvent mAidAddRemoveEvent; + SyncEvent mTransceiveEvent; + SyncEvent mVerInfoEvent; + UINT8 mVerInfo [3]; + UINT8 mTransData [MAX_TRANS_RECV_SIZE]; + UINT8 mHciBufferForStack [MAX_TRANS_RECV_SIZE]; + RouteDataSet mRouteDataSet; //routing data + std::vector<std::string> mUsedAids; //AID's that are used in current routes + + + /******************************************************************************* + ** + ** Function: SecureElement + ** + ** Description: Initialize member variables. + ** + ** Returns: None + ** + *******************************************************************************/ + SecureElement (); + + + /******************************************************************************* + ** + ** Function: ~SecureElement + ** + ** Description: Release all resources. + ** + ** Returns: None + ** + *******************************************************************************/ + ~SecureElement (); + + + /******************************************************************************* + ** + ** 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 (); + + + /******************************************************************************* + ** + ** 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 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* tlv, UINT16 tlvMaxLen, UINT16& tlvActualLen, const UINT8* aid, UINT8 aidLen); +}; + |