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:        getListOfEeHandles
     81     **
     82     ** Description:     Get the list of handles of all execution environments.
     83     **                  e: Java Virtual Machine.
     84     **
     85     ** Returns:         List of handles of all execution environments.
     86     **
     87     *******************************************************************************/
     88     jintArray getListOfEeHandles (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     /*******************************************************************************
    161     **
    162     ** Function:        notifyListenModeState
    163     **
    164     ** Description:     Notify the NFC service about whether the SE was activated
    165     **                  in listen mode.
    166     **                  isActive: Whether the secure element is activated.
    167     **
    168     ** Returns:         None
    169     **
    170     *******************************************************************************/
    171     void notifyListenModeState (bool isActivated);
    172 
    173     /*******************************************************************************
    174     **
    175     ** Function:        notifyRfFieldEvent
    176     **
    177     ** Description:     Notify the NFC service about RF field events from the stack.
    178     **                  isActive: Whether any secure element is activated.
    179     **
    180     ** Returns:         None
    181     **
    182     *******************************************************************************/
    183     void notifyRfFieldEvent (bool isActive);
    184 
    185 
    186     /*******************************************************************************
    187     **
    188     ** Function:        resetRfFieldStatus ();
    189     **
    190     ** Description:     Resets the field status.
    191     **
    192     ** Returns:         None
    193     **
    194     *******************************************************************************/
    195     void resetRfFieldStatus ();
    196 
    197     /*******************************************************************************
    198     **
    199     ** Function:        storeUiccInfo
    200     **
    201     ** Description:     Store a copy of the execution environment information from the stack.
    202     **                  info: execution environment information.
    203     **
    204     ** Returns:         None
    205     **
    206     *******************************************************************************/
    207     void storeUiccInfo (tNFA_EE_DISCOVER_REQ& info);
    208 
    209 
    210     /*******************************************************************************
    211     **
    212     ** Function:        getUiccId
    213     **
    214     ** Description:     Get the ID of the secure element.
    215     **                  eeHandle: Handle to the secure element.
    216     **                  uid: Array to receive the ID.
    217     **
    218     ** Returns:         True if ok.
    219     **
    220     *******************************************************************************/
    221     bool getUiccId (tNFA_HANDLE eeHandle, jbyteArray& uid);
    222 
    223 
    224     /*******************************************************************************
    225     **
    226     ** Function:        getTechnologyList
    227     **
    228     ** Description:     Get all the technologies supported by a secure element.
    229     **                  eeHandle: Handle of secure element.
    230     **                  techList: List to receive the technologies.
    231     **
    232     ** Returns:         True if ok.
    233     **
    234     *******************************************************************************/
    235     bool getTechnologyList (tNFA_HANDLE eeHandle, jintArray& techList);
    236 
    237 
    238     /*******************************************************************************
    239     **
    240     ** Function:        notifyTransactionListenersOfAid
    241     **
    242     ** Description:     Notify the NFC service about a transaction event from secure element.
    243     **                  aid: Buffer contains application ID.
    244     **                  aidLen: Length of application ID.
    245     **
    246     ** Returns:         None
    247     **
    248     *******************************************************************************/
    249     void notifyTransactionListenersOfAid (const UINT8* aid, UINT8 aidLen);
    250 
    251 
    252     /*******************************************************************************
    253     **
    254     ** Function:        notifyTransactionListenersOfTlv
    255     **
    256     ** Description:     Notify the NFC service about a transaction event from secure element.
    257     **                  The type-length-value contains AID and parameter.
    258     **                  tlv: type-length-value encoded in Basic Encoding Rule.
    259     **                  tlvLen: Length tlv.
    260     **
    261     ** Returns:         None
    262     **
    263     *******************************************************************************/
    264     void notifyTransactionListenersOfTlv (const UINT8* tlv, UINT8 tlvLen);
    265 
    266 
    267     /*******************************************************************************
    268     **
    269     ** Function:        connectionEventHandler
    270     **
    271     ** Description:     Receive card-emulation related events from stack.
    272     **                  event: Event code.
    273     **                  eventData: Event data.
    274     **
    275     ** Returns:         None
    276     **
    277     *******************************************************************************/
    278     void connectionEventHandler (UINT8 event, tNFA_CONN_EVT_DATA* eventData);
    279 
    280 
    281     /*******************************************************************************
    282     **
    283     ** Function:        applyRoutes
    284     **
    285     ** Description:     Read route data from XML and apply them again
    286     **                  to every secure element.
    287     **
    288     ** Returns:         None
    289     **
    290     *******************************************************************************/
    291     void applyRoutes ();
    292 
    293 
    294     /*******************************************************************************
    295     **
    296     ** Function:        setActiveSeOverride
    297     **
    298     ** Description:     Specify which secure element to turn on.
    299     **                  activeSeOverride: ID of secure element
    300     **
    301     ** Returns:         None
    302     **
    303     *******************************************************************************/
    304     void setActiveSeOverride (UINT8 activeSeOverride);
    305 
    306 
    307     /*******************************************************************************
    308     **
    309     ** Function:        routeToSecureElement
    310     **
    311     ** Description:     Adjust controller's listen-mode routing table so transactions
    312     **                  are routed to the secure elements as specified in route.xml.
    313     **
    314     ** Returns:         True if ok.
    315     **
    316     *******************************************************************************/
    317     bool routeToSecureElement ();
    318 
    319 
    320     /*******************************************************************************
    321     **
    322     ** Function:        routeToDefault
    323     **
    324     ** Description:     Adjust controller's listen-mode routing table so transactions
    325     **                  are routed to the default destination specified in route.xml.
    326     **
    327     ** Returns:         True if ok.
    328     **
    329     *******************************************************************************/
    330     bool routeToDefault ();
    331 
    332 
    333     /*******************************************************************************
    334     **
    335     ** Function:        isBusy
    336     **
    337     ** Description:     Whether NFC controller is routing listen-mode events or a pipe is connected.
    338     **
    339     ** Returns:         True if either case is true.
    340     **
    341     *******************************************************************************/
    342     bool isBusy ();
    343 
    344 
    345     /*******************************************************************************
    346     **
    347     ** Function         getActualNumEe
    348     **
    349     ** Description      Returns number of secure elements we know about.
    350     **
    351     ** Returns          Number of secure elements we know about.
    352     **
    353     *******************************************************************************/
    354     UINT8 getActualNumEe();
    355 
    356 
    357     /*******************************************************************************
    358     **
    359     ** Function         getSeVerInfo
    360     **
    361     ** Description      Gets version information and id for a secure element.  The
    362     **                  seIndex parmeter is the zero based index of the secure
    363     **                  element to get verion info for.  The version infommation
    364     **                  is returned as a string int the verInfo parameter.
    365     **
    366     ** Returns          ture on success, false on failure
    367     **
    368     *******************************************************************************/
    369     bool getSeVerInfo(int seIndex, char * verInfo, int verInfoSz, UINT8 * seid);
    370 
    371 
    372     /*******************************************************************************
    373     **
    374     ** Function:        isActivatedInListenMode
    375     **
    376     ** Description:     Can be used to determine if the SE is activated in listen mode
    377     **
    378     ** Returns:         True if the SE is activated in listen mode
    379     **
    380     *******************************************************************************/
    381     bool isActivatedInListenMode();
    382 
    383     /*******************************************************************************
    384     **
    385     ** Function:        isRfFieldOn
    386     **
    387     ** Description:     Can be used to determine if the SE is in an RF field
    388     **
    389     ** Returns:         True if the SE is activated in an RF field
    390     **
    391     *******************************************************************************/
    392     bool isRfFieldOn();
    393 
    394 private:
    395     static const unsigned int MAX_RESPONSE_SIZE = 1024;
    396     enum RouteSelection {NoRoute, DefaultRoute, SecElemRoute};
    397     static const int MAX_NUM_EE = 5;    //max number of EE's
    398     static const UINT8 STATIC_PIPE_0x70 = 0x70; //Broadcom's proprietary static pipe
    399     static const UINT8 STATIC_PIPE_0x71 = 0x71; //Broadcom's proprietary static pipe
    400     static const UINT8 EVT_SEND_DATA = 0x10;    //see specification ETSI TS 102 622 v9.0.0 (Host Controller Interface); section 9.3.3.3
    401     static const tNFA_HANDLE EE_HANDLE_0xF3 = 0x4F3; //handle to secure element in slot 0
    402     static const tNFA_HANDLE EE_HANDLE_0xF4 = 0x4F4; //handle to secure element in slot 1
    403     static SecureElement sSecElem;
    404     static const char* APP_NAME;
    405 
    406     UINT8           mDestinationGate;       //destination gate of the UICC
    407     tNFA_HANDLE     mNfaHciHandle;          //NFA handle to NFA's HCI component
    408     nfc_jni_native_data* mNativeData;
    409     bool    mIsInit;                // whether EE is initialized
    410     UINT8   mActualNumEe;           // actual number of EE's reported by the stack
    411     UINT8   mNumEePresent;          // actual number of usable EE's
    412     bool    mbNewEE;
    413     UINT8   mNewPipeId;
    414     UINT8   mNewSourceGate;
    415     UINT16  mActiveSeOverride;      // active "enable" seid, 0 means activate all SEs
    416     tNFA_STATUS mCommandStatus;     //completion status of the last command
    417     bool    mIsPiping;              //is a pipe connected to the controller?
    418     RouteSelection mCurrentRouteSelection;
    419     int     mActualResponseSize;         //number of bytes in the response received from secure element
    420     bool    mUseOberthurWarmReset;  //whether to use warm-reset command
    421     bool    mActivatedInListenMode; // whether we're activated in listen mode
    422     UINT8   mOberthurWarmResetCommand; //warm-reset command byte
    423     tNFA_EE_INFO mEeInfo [MAX_NUM_EE];  //actual size stored in mActualNumEe
    424     tNFA_EE_DISCOVER_REQ mUiccInfo;
    425     tNFA_HCI_GET_GATE_PIPE_LIST mHciCfg;
    426     SyncEvent       mEeRegisterEvent;
    427     SyncEvent       mHciRegisterEvent;
    428     SyncEvent       mEeSetModeEvent;
    429     SyncEvent       mPipeListEvent;
    430     SyncEvent       mCreatePipeEvent;
    431     SyncEvent       mPipeOpenedEvent;
    432     SyncEvent       mAllocateGateEvent;
    433     SyncEvent       mDeallocateGateEvent;
    434     SyncEvent       mRoutingEvent;
    435     SyncEvent       mUiccInfoEvent;
    436     SyncEvent       mUiccListenEvent;
    437     SyncEvent       mAidAddRemoveEvent;
    438     SyncEvent       mTransceiveEvent;
    439     SyncEvent       mVerInfoEvent;
    440     SyncEvent       mRegistryEvent;
    441     UINT8           mVerInfo [3];
    442     UINT8           mResponseData [MAX_RESPONSE_SIZE];
    443     RouteDataSet    mRouteDataSet; //routing data
    444     std::vector<std::string> mUsedAids; //AID's that are used in current routes
    445     UINT8           mAidForEmptySelect[NCI_MAX_AID_LEN+1];
    446     Mutex           mMutex; // protects fields below
    447     bool            mRfFieldIsOn; // last known RF field state
    448     struct timespec mLastRfFieldToggle; // last time RF field went off
    449     /*******************************************************************************
    450     **
    451     ** Function:        SecureElement
    452     **
    453     ** Description:     Initialize member variables.
    454     **
    455     ** Returns:         None
    456     **
    457     *******************************************************************************/
    458     SecureElement ();
    459 
    460 
    461     /*******************************************************************************
    462     **
    463     ** Function:        ~SecureElement
    464     **
    465     ** Description:     Release all resources.
    466     **
    467     ** Returns:         None
    468     **
    469     *******************************************************************************/
    470     ~SecureElement ();
    471 
    472 
    473     /*******************************************************************************
    474     **
    475     ** Function:        nfaEeCallback
    476     **
    477     ** Description:     Receive execution environment-related events from stack.
    478     **                  event: Event code.
    479     **                  eventData: Event data.
    480     **
    481     ** Returns:         None
    482     **
    483     *******************************************************************************/
    484     static void nfaEeCallback (tNFA_EE_EVT event, tNFA_EE_CBACK_DATA* eventData);
    485 
    486 
    487     /*******************************************************************************
    488     **
    489     ** Function:        nfaHciCallback
    490     **
    491     ** Description:     Receive Host Controller Interface-related events from stack.
    492     **                  event: Event code.
    493     **                  eventData: Event data.
    494     **
    495     ** Returns:         None
    496     **
    497     *******************************************************************************/
    498     static void nfaHciCallback (tNFA_HCI_EVT event, tNFA_HCI_EVT_DATA* eventData);
    499 
    500 
    501     /*******************************************************************************
    502     **
    503     ** Function:        findEeByHandle
    504     **
    505     ** Description:     Find information about an execution environment.
    506     **                  eeHandle: Handle to execution environment.
    507     **
    508     ** Returns:         Information about an execution environment.
    509     **
    510     *******************************************************************************/
    511     tNFA_EE_INFO *findEeByHandle (tNFA_HANDLE eeHandle);
    512 
    513 
    514     /*******************************************************************************
    515     **
    516     ** Function:        findUiccByHandle
    517     **
    518     ** Description:     Find information about an execution environment.
    519     **                  eeHandle: Handle of the execution environment.
    520     **
    521     ** Returns:         Information about the execution environment.
    522     **
    523     *******************************************************************************/
    524     tNFA_EE_DISCOVER_INFO *findUiccByHandle (tNFA_HANDLE eeHandle);
    525 
    526 
    527     /*******************************************************************************
    528     **
    529     ** Function:        getDefaultEeHandle
    530     **
    531     ** Description:     Get the handle to the execution environment.
    532     **
    533     ** Returns:         Handle to the execution environment.
    534     **
    535     *******************************************************************************/
    536     tNFA_HANDLE getDefaultEeHandle ();
    537 
    538 
    539     /*******************************************************************************
    540     **
    541     ** Function:        adjustRoutes
    542     **
    543     ** Description:     Adjust routes in the controller's listen-mode routing table.
    544     **                  selection: which set of routes to configure the controller.
    545     **
    546     ** Returns:         None
    547     **
    548     *******************************************************************************/
    549     void adjustRoutes (RouteSelection selection);
    550 
    551 
    552     /*******************************************************************************
    553     **
    554     ** Function:        adjustProtocolRoutes
    555     **
    556     ** Description:     Adjust default routing based on protocol in NFC listen mode.
    557     **                  isRouteToEe: Whether routing to EE (true) or host (false).
    558     **
    559     ** Returns:         None
    560     **
    561     *******************************************************************************/
    562     void adjustProtocolRoutes (RouteDataSet::Database* db, RouteSelection routeSelection);
    563 
    564 
    565     /*******************************************************************************
    566     **
    567     ** Function:        adjustTechnologyRoutes
    568     **
    569     ** Description:     Adjust default routing based on technology in NFC listen mode.
    570     **                  isRouteToEe: Whether routing to EE (true) or host (false).
    571     **
    572     ** Returns:         None
    573     **
    574     *******************************************************************************/
    575     void adjustTechnologyRoutes (RouteDataSet::Database* db, RouteSelection routeSelection);
    576 
    577 
    578     /*******************************************************************************
    579     **
    580     ** Function:        getEeInfo
    581     **
    582     ** Description:     Get latest information about execution environments from stack.
    583     **
    584     ** Returns:         True if at least 1 EE is available.
    585     **
    586     *******************************************************************************/
    587     bool getEeInfo ();
    588 
    589     /*******************************************************************************
    590     **
    591     ** Function:        eeStatusToString
    592     **
    593     ** Description:     Convert status code to status text.
    594     **                  status: Status code
    595     **
    596     ** Returns:         None
    597     **
    598     *******************************************************************************/
    599     static const char* eeStatusToString (UINT8 status);
    600 
    601 
    602     /*******************************************************************************
    603     **
    604     ** Function:        encodeAid
    605     **
    606     ** Description:     Encode AID in type-length-value using Basic Encoding Rule.
    607     **                  tlv: Buffer to store TLV.
    608     **                  tlvMaxLen: TLV buffer's maximum length.
    609     **                  tlvActualLen: TLV buffer's actual length.
    610     **                  aid: Buffer of Application ID.
    611     **                  aidLen: Aid buffer's actual length.
    612     **
    613     ** Returns:         True if ok.
    614     **
    615     *******************************************************************************/
    616     bool encodeAid (UINT8* tlv, UINT16 tlvMaxLen, UINT16& tlvActualLen, const UINT8* aid, UINT8 aidLen);
    617 };
    618 
    619