Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*
     18  *  Communicate with secure elements that are attached to the NFC
     19  *  controller.
     20  */
     21 #pragma once
     22 #include "SyncEvent.h"
     23 #include "DataQueue.h"
     24 #include "NfcJniUtil.h"
     25 #include "RouteDataSet.h"
     26 extern "C"
     27 {
     28     #include "nfa_ee_api.h"
     29     #include "nfa_hci_api.h"
     30     #include "nfa_hci_defs.h"
     31     #include "nfa_ce_api.h"
     32 }
     33 
     34 
     35 class SecureElement
     36 {
     37 public:
     38     tNFA_HANDLE  mActiveEeHandle;
     39 
     40 
     41     /*******************************************************************************
     42     **
     43     ** Function:        getInstance
     44     **
     45     ** Description:     Get the SecureElement singleton object.
     46     **
     47     ** Returns:         SecureElement object.
     48     **
     49     *******************************************************************************/
     50     static SecureElement& getInstance ();
     51 
     52 
     53     /*******************************************************************************
     54     **
     55     ** Function:        initialize
     56     **
     57     ** Description:     Initialize all member variables.
     58     **                  native: Native data.
     59     **
     60     ** Returns:         True if ok.
     61     **
     62     *******************************************************************************/
     63     bool initialize (nfc_jni_native_data* native);
     64 
     65 
     66     /*******************************************************************************
     67     **
     68     ** Function:        finalize
     69     **
     70     ** Description:     Release all resources.
     71     **
     72     ** Returns:         None
     73     **
     74     *******************************************************************************/
     75     void finalize ();
     76 
     77 
     78     /*******************************************************************************
     79     **
     80     ** Function:        getSecureElementIdList
     81     **
     82     ** Description:     Get a list of ID's of all secure elements.
     83     **                  e: Java Virtual Machine.
     84     **
     85     ** Returns:         List of ID's.
     86     **
     87     *******************************************************************************/
     88     jintArray getSecureElementIdList (JNIEnv* e);
     89 
     90 
     91     /*******************************************************************************
     92     **
     93     ** Function:        activate
     94     **
     95     ** Description:     Turn on the secure element.
     96     **                  seID: ID of secure element.
     97     **
     98     ** Returns:         True if ok.
     99     **
    100     *******************************************************************************/
    101     bool activate (jint seID);
    102 
    103 
    104     /*******************************************************************************
    105     **
    106     ** Function:        deactivate
    107     **
    108     ** Description:     Turn off the secure element.
    109     **                  seID: ID of secure element.
    110     **
    111     ** Returns:         True if ok.
    112     **
    113     *******************************************************************************/
    114     bool deactivate (jint seID);
    115 
    116 
    117     /*******************************************************************************
    118     **
    119     ** Function:        connectEE
    120     **
    121     ** Description:     Connect to the execution environment.
    122     **
    123     ** Returns:         True if ok.
    124     **
    125     *******************************************************************************/
    126     bool connectEE ();
    127 
    128 
    129     /*******************************************************************************
    130     **
    131     ** Function:        disconnectEE
    132     **
    133     ** Description:     Disconnect from the execution environment.
    134     **                  seID: ID of secure element.
    135     **
    136     ** Returns:         True if ok.
    137     **
    138     *******************************************************************************/
    139     bool disconnectEE (jint seID);
    140 
    141 
    142     /*******************************************************************************
    143     **
    144     ** Function:        transceive
    145     **
    146     ** Description:     Send data to the secure element; read it's response.
    147     **                  xmitBuffer: Data to transmit.
    148     **                  xmitBufferSize: Length of data.
    149     **                  recvBuffer: Buffer to receive response.
    150     **                  recvBufferMaxSize: Maximum size of buffer.
    151     **                  recvBufferActualSize: Actual length of response.
    152     **                  timeoutMillisec: timeout in millisecond
    153     **
    154     ** Returns:         True if ok.
    155     **
    156     *******************************************************************************/
    157     bool transceive (UINT8* xmitBuffer, INT32 xmitBufferSize, UINT8* recvBuffer,
    158                      INT32 recvBufferMaxSize, INT32& recvBufferActualSize, INT32 timeoutMillisec);
    159 
    160     void notifyModeSet (tNFA_HANDLE eeHandle, bool success);
    161 
    162     /*******************************************************************************
    163     **
    164     ** Function:        notifyListenModeState
    165     **
    166     ** Description:     Notify the NFC service about whether the SE was activated
    167     **                  in listen mode.
    168     **                  isActive: Whether the secure element is activated.
    169     **
    170     ** Returns:         None
    171     **
    172     *******************************************************************************/
    173     void notifyListenModeState (bool isActivated);
    174 
    175     /*******************************************************************************
    176     **
    177     ** Function:        notifyRfFieldEvent
    178     **
    179     ** Description:     Notify the NFC service about RF field events from the stack.
    180     **                  isActive: Whether any secure element is activated.
    181     **
    182     ** Returns:         None
    183     **
    184     *******************************************************************************/
    185     void notifyRfFieldEvent (bool isActive);
    186 
    187 
    188     /*******************************************************************************
    189     **
    190     ** Function:        resetRfFieldStatus ();
    191     **
    192     ** Description:     Resets the field status.
    193     **
    194     ** Returns:         None
    195     **
    196     *******************************************************************************/
    197     void resetRfFieldStatus ();
    198 
    199     /*******************************************************************************
    200     **
    201     ** Function:        storeUiccInfo
    202     **
    203     ** Description:     Store a copy of the execution environment information from the stack.
    204     **                  info: execution environment information.
    205     **
    206     ** Returns:         None
    207     **
    208     *******************************************************************************/
    209     void storeUiccInfo (tNFA_EE_DISCOVER_REQ& info);
    210 
    211 
    212     /*******************************************************************************
    213     **
    214     ** Function:        getUiccId
    215     **
    216     ** Description:     Get the ID of the secure element.
    217     **                  eeHandle: Handle to the secure element.
    218     **                  uid: Array to receive the ID.
    219     **
    220     ** Returns:         True if ok.
    221     **
    222     *******************************************************************************/
    223     bool getUiccId (tNFA_HANDLE eeHandle, jbyteArray& uid);
    224 
    225 
    226     /*******************************************************************************
    227     **
    228     ** Function:        getTechnologyList
    229     **
    230     ** Description:     Get all the technologies supported by a secure element.
    231     **                  eeHandle: Handle of secure element.
    232     **                  techList: List to receive the technologies.
    233     **
    234     ** Returns:         True if ok.
    235     **
    236     *******************************************************************************/
    237     bool getTechnologyList (tNFA_HANDLE eeHandle, jintArray& techList);
    238 
    239 
    240     /*******************************************************************************
    241     **
    242     ** Function:        notifyTransactionListenersOfAid
    243     **
    244     ** Description:     Notify the NFC service about a transaction event from secure element.
    245     **                  aid: Buffer contains application ID.
    246     **                  aidLen: Length of application ID.
    247     **
    248     ** Returns:         None
    249     **
    250     *******************************************************************************/
    251     void notifyTransactionListenersOfAid (const UINT8* aid, UINT8 aidLen);
    252 
    253 
    254     /*******************************************************************************
    255     **
    256     ** Function:        notifyTransactionListenersOfTlv
    257     **
    258     ** Description:     Notify the NFC service about a transaction event from secure element.
    259     **                  The type-length-value contains AID and parameter.
    260     **                  tlv: type-length-value encoded in Basic Encoding Rule.
    261     **                  tlvLen: Length tlv.
    262     **
    263     ** Returns:         None
    264     **
    265     *******************************************************************************/
    266     void notifyTransactionListenersOfTlv (const UINT8* tlv, UINT8 tlvLen);
    267 
    268 
    269     /*******************************************************************************
    270     **
    271     ** Function:        connectionEventHandler
    272     **
    273     ** Description:     Receive card-emulation related events from stack.
    274     **                  event: Event code.
    275     **                  eventData: Event data.
    276     **
    277     ** Returns:         None
    278     **
    279     *******************************************************************************/
    280     void connectionEventHandler (UINT8 event, tNFA_CONN_EVT_DATA* eventData);
    281 
    282 
    283     /*******************************************************************************
    284     **
    285     ** Function:        applyRoutes
    286     **
    287     ** Description:     Read route data from XML and apply them again
    288     **                  to every secure element.
    289     **
    290     ** Returns:         None
    291     **
    292     *******************************************************************************/
    293     void applyRoutes ();
    294 
    295 
    296     /*******************************************************************************
    297     **
    298     ** Function:        setActiveSeOverride
    299     **
    300     ** Description:     Specify which secure element to turn on.
    301     **                  activeSeOverride: ID of secure element
    302     **
    303     ** Returns:         None
    304     **
    305     *******************************************************************************/
    306     void setActiveSeOverride (UINT8 activeSeOverride);
    307 
    308 
    309     /*******************************************************************************
    310     **
    311     ** Function:        isBusy
    312     **
    313     ** Description:     Whether NFC controller is routing listen-mode events or a pipe is connected.
    314     **
    315     ** Returns:         True if either case is true.
    316     **
    317     *******************************************************************************/
    318     bool isBusy ();
    319 
    320 
    321     /*******************************************************************************
    322     **
    323     ** Function         getActualNumEe
    324     **
    325     ** Description      Returns number of secure elements we know about.
    326     **
    327     ** Returns          Number of secure elements we know about.
    328     **
    329     *******************************************************************************/
    330     UINT8 getActualNumEe();
    331 
    332 
    333     /*******************************************************************************
    334     **
    335     ** Function         getSeVerInfo
    336     **
    337     ** Description      Gets version information and id for a secure element.  The
    338     **                  seIndex parmeter is the zero based index of the secure
    339     **                  element to get verion info for.  The version infommation
    340     **                  is returned as a string int the verInfo parameter.
    341     **
    342     ** Returns          ture on success, false on failure
    343     **
    344     *******************************************************************************/
    345     bool getSeVerInfo(int seIndex, char * verInfo, int verInfoSz, UINT8 * seid);
    346 
    347 
    348     /*******************************************************************************
    349     **
    350     ** Function:        isActivatedInListenMode
    351     **
    352     ** Description:     Can be used to determine if the SE is activated in listen mode
    353     **
    354     ** Returns:         True if the SE is activated in listen mode
    355     **
    356     *******************************************************************************/
    357     bool isActivatedInListenMode();
    358 
    359     /*******************************************************************************
    360     **
    361     ** Function:        isRfFieldOn
    362     **
    363     ** Description:     Can be used to determine if the SE is in an RF field
    364     **
    365     ** Returns:         True if the SE is activated in an RF field
    366     **
    367     *******************************************************************************/
    368     bool isRfFieldOn();
    369 
    370 private:
    371     static const unsigned int MAX_RESPONSE_SIZE = 1024;
    372     enum RouteSelection {NoRoute, DefaultRoute, SecElemRoute};
    373     static const int MAX_NUM_EE = 5;    //max number of EE's
    374     static const UINT8 STATIC_PIPE_0x70 = 0x70; //Broadcom's proprietary static pipe
    375     static const UINT8 STATIC_PIPE_0x71 = 0x71; //Broadcom's proprietary static pipe
    376     static const UINT8 EVT_SEND_DATA = 0x10;    //see specification ETSI TS 102 622 v9.0.0 (Host Controller Interface); section 9.3.3.3
    377     static const tNFA_HANDLE EE_HANDLE_0xF3 = 0x4F3; //handle to secure element in slot 0
    378     static const tNFA_HANDLE EE_HANDLE_0xF4 = 0x4F4; //handle to secure element in slot 1
    379     static SecureElement sSecElem;
    380     static const char* APP_NAME;
    381 
    382     UINT8           mDestinationGate;       //destination gate of the UICC
    383     tNFA_HANDLE     mNfaHciHandle;          //NFA handle to NFA's HCI component
    384     nfc_jni_native_data* mNativeData;
    385     bool    mIsInit;                // whether EE is initialized
    386     UINT8   mActualNumEe;           // actual number of EE's reported by the stack
    387     UINT8   mNumEePresent;          // actual number of usable EE's
    388     bool    mbNewEE;
    389     UINT8   mNewPipeId;
    390     UINT8   mNewSourceGate;
    391     UINT16  mActiveSeOverride;      // active "enable" seid, 0 means activate all SEs
    392     tNFA_STATUS mCommandStatus;     //completion status of the last command
    393     bool    mIsPiping;              //is a pipe connected to the controller?
    394     RouteSelection mCurrentRouteSelection;
    395     int     mActualResponseSize;         //number of bytes in the response received from secure element
    396     bool    mUseOberthurWarmReset;  //whether to use warm-reset command
    397     bool    mActivatedInListenMode; // whether we're activated in listen mode
    398     UINT8   mOberthurWarmResetCommand; //warm-reset command byte
    399     tNFA_EE_INFO mEeInfo [MAX_NUM_EE];  //actual size stored in mActualNumEe
    400     tNFA_EE_DISCOVER_REQ mUiccInfo;
    401     tNFA_HCI_GET_GATE_PIPE_LIST mHciCfg;
    402     SyncEvent       mEeRegisterEvent;
    403     SyncEvent       mHciRegisterEvent;
    404     SyncEvent       mEeSetModeEvent;
    405     SyncEvent       mPipeListEvent;
    406     SyncEvent       mCreatePipeEvent;
    407     SyncEvent       mPipeOpenedEvent;
    408     SyncEvent       mAllocateGateEvent;
    409     SyncEvent       mDeallocateGateEvent;
    410     SyncEvent       mRoutingEvent;
    411     SyncEvent       mUiccInfoEvent;
    412     SyncEvent       mUiccListenEvent;
    413     SyncEvent       mAidAddRemoveEvent;
    414     SyncEvent       mTransceiveEvent;
    415     SyncEvent       mVerInfoEvent;
    416     SyncEvent       mRegistryEvent;
    417     UINT8           mVerInfo [3];
    418     UINT8           mResponseData [MAX_RESPONSE_SIZE];
    419     RouteDataSet    mRouteDataSet; //routing data
    420     std::vector<std::string> mUsedAids; //AID's that are used in current routes
    421     UINT8           mAidForEmptySelect[NCI_MAX_AID_LEN+1];
    422     Mutex           mMutex; // protects fields below
    423     bool            mRfFieldIsOn; // last known RF field state
    424     struct timespec mLastRfFieldToggle; // last time RF field went off
    425     /*******************************************************************************
    426     **
    427     ** Function:        SecureElement
    428     **
    429     ** Description:     Initialize member variables.
    430     **
    431     ** Returns:         None
    432     **
    433     *******************************************************************************/
    434     SecureElement ();
    435 
    436 
    437     /*******************************************************************************
    438     **
    439     ** Function:        ~SecureElement
    440     **
    441     ** Description:     Release all resources.
    442     **
    443     ** Returns:         None
    444     **
    445     *******************************************************************************/
    446     ~SecureElement ();
    447 
    448 
    449     /*******************************************************************************
    450     **
    451     ** Function:        nfaEeCallback
    452     **
    453     ** Description:     Receive execution environment-related events from stack.
    454     **                  event: Event code.
    455     **                  eventData: Event data.
    456     **
    457     ** Returns:         None
    458     **
    459     *******************************************************************************/
    460     static void nfaEeCallback (tNFA_EE_EVT event, tNFA_EE_CBACK_DATA* eventData);
    461 
    462 
    463     /*******************************************************************************
    464     **
    465     ** Function:        nfaHciCallback
    466     **
    467     ** Description:     Receive Host Controller Interface-related events from stack.
    468     **                  event: Event code.
    469     **                  eventData: Event data.
    470     **
    471     ** Returns:         None
    472     **
    473     *******************************************************************************/
    474     static void nfaHciCallback (tNFA_HCI_EVT event, tNFA_HCI_EVT_DATA* eventData);
    475 
    476 
    477     /*******************************************************************************
    478     **
    479     ** Function:        findEeByHandle
    480     **
    481     ** Description:     Find information about an execution environment.
    482     **                  eeHandle: Handle to execution environment.
    483     **
    484     ** Returns:         Information about an execution environment.
    485     **
    486     *******************************************************************************/
    487     tNFA_EE_INFO *findEeByHandle (tNFA_HANDLE eeHandle);
    488 
    489 
    490     /*******************************************************************************
    491     **
    492     ** Function:        findUiccByHandle
    493     **
    494     ** Description:     Find information about an execution environment.
    495     **                  eeHandle: Handle of the execution environment.
    496     **
    497     ** Returns:         Information about the execution environment.
    498     **
    499     *******************************************************************************/
    500     tNFA_EE_DISCOVER_INFO *findUiccByHandle (tNFA_HANDLE eeHandle);
    501 
    502 
    503     /*******************************************************************************
    504     **
    505     ** Function:        getDefaultEeHandle
    506     **
    507     ** Description:     Get the handle to the execution environment.
    508     **
    509     ** Returns:         Handle to the execution environment.
    510     **
    511     *******************************************************************************/
    512     tNFA_HANDLE getDefaultEeHandle ();
    513 
    514 
    515     /*******************************************************************************
    516     **
    517     ** Function:        adjustRoutes
    518     **
    519     ** Description:     Adjust routes in the controller's listen-mode routing table.
    520     **                  selection: which set of routes to configure the controller.
    521     **
    522     ** Returns:         None
    523     **
    524     *******************************************************************************/
    525     void adjustRoutes (RouteSelection selection);
    526 
    527 
    528     /*******************************************************************************
    529     **
    530     ** Function:        adjustProtocolRoutes
    531     **
    532     ** Description:     Adjust default routing based on protocol in NFC listen mode.
    533     **                  isRouteToEe: Whether routing to EE (true) or host (false).
    534     **
    535     ** Returns:         None
    536     **
    537     *******************************************************************************/
    538     void adjustProtocolRoutes (RouteDataSet::Database* db, RouteSelection routeSelection);
    539 
    540 
    541     /*******************************************************************************
    542     **
    543     ** Function:        adjustTechnologyRoutes
    544     **
    545     ** Description:     Adjust default routing based on technology in NFC listen mode.
    546     **                  isRouteToEe: Whether routing to EE (true) or host (false).
    547     **
    548     ** Returns:         None
    549     **
    550     *******************************************************************************/
    551     void adjustTechnologyRoutes (RouteDataSet::Database* db, RouteSelection routeSelection);
    552 
    553 
    554     /*******************************************************************************
    555     **
    556     ** Function:        getEeInfo
    557     **
    558     ** Description:     Get latest information about execution environments from stack.
    559     **
    560     ** Returns:         True if at least 1 EE is available.
    561     **
    562     *******************************************************************************/
    563     bool getEeInfo ();
    564 
    565     /*******************************************************************************
    566     **
    567     ** Function:        eeStatusToString
    568     **
    569     ** Description:     Convert status code to status text.
    570     **                  status: Status code
    571     **
    572     ** Returns:         None
    573     **
    574     *******************************************************************************/
    575     static const char* eeStatusToString (UINT8 status);
    576 
    577 
    578     /*******************************************************************************
    579     **
    580     ** Function:        encodeAid
    581     **
    582     ** Description:     Encode AID in type-length-value using Basic Encoding Rule.
    583     **                  tlv: Buffer to store TLV.
    584     **                  tlvMaxLen: TLV buffer's maximum length.
    585     **                  tlvActualLen: TLV buffer's actual length.
    586     **                  aid: Buffer of Application ID.
    587     **                  aidLen: Aid buffer's actual length.
    588     **
    589     ** Returns:         True if ok.
    590     **
    591     *******************************************************************************/
    592     bool encodeAid (UINT8* tlv, UINT16 tlvMaxLen, UINT16& tlvActualLen, const UINT8* aid, UINT8 aidLen);
    593 };
    594 
    595