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 #include <semaphore.h>
     18 #include <errno.h>
     19 #include "_OverrideLog.h"
     20 #include "NfcJniUtil.h"
     21 #include "NfcAdaptation.h"
     22 #include "SyncEvent.h"
     23 #include "PeerToPeer.h"
     24 #include "RoutingManager.h"
     25 #include "NfcTag.h"
     26 #include "config.h"
     27 #include "PowerSwitch.h"
     28 #include "JavaClassConstants.h"
     29 #include "Pn544Interop.h"
     30 #include <ScopedLocalRef.h>
     31 #include <ScopedUtfChars.h>
     32 #include <ScopedPrimitiveArray.h>
     33 
     34 extern "C"
     35 {
     36     #include "nfa_api.h"
     37     #include "nfa_p2p_api.h"
     38     #include "rw_api.h"
     39     #include "nfa_ee_api.h"
     40     #include "nfc_brcm_defs.h"
     41     #include "ce_api.h"
     42     #include "phNxpExtns.h"
     43 }
     44 
     45 extern const uint8_t nfca_version_string [];
     46 extern const uint8_t nfa_version_string [];
     47 extern tNFA_DM_DISC_FREQ_CFG* p_nfa_dm_rf_disc_freq_cfg; //defined in stack
     48 namespace android
     49 {
     50     extern bool gIsTagDeactivating;
     51     extern bool gIsSelectingRfInterface;
     52     extern void nativeNfcTag_doTransceiveStatus (tNFA_STATUS status, uint8_t * buf, uint32_t buflen);
     53     extern void nativeNfcTag_notifyRfTimeout ();
     54     extern void nativeNfcTag_doConnectStatus (jboolean is_connect_ok);
     55     extern void nativeNfcTag_doDeactivateStatus (int status);
     56     extern void nativeNfcTag_doWriteStatus (jboolean is_write_ok);
     57     extern void nativeNfcTag_doCheckNdefResult (tNFA_STATUS status, uint32_t max_size, uint32_t current_size, uint8_t flags);
     58     extern void nativeNfcTag_doMakeReadonlyResult (tNFA_STATUS status);
     59     extern void nativeNfcTag_doPresenceCheckResult (tNFA_STATUS status);
     60     extern void nativeNfcTag_formatStatus (bool is_ok);
     61     extern void nativeNfcTag_resetPresenceCheck ();
     62     extern void nativeNfcTag_doReadCompleted (tNFA_STATUS status);
     63     extern void nativeNfcTag_setRfInterface (tNFA_INTF_TYPE rfInterface);
     64     extern void nativeNfcTag_abortWaits ();
     65     extern void nativeLlcpConnectionlessSocket_abortWait ();
     66     extern void nativeNfcTag_registerNdefTypeHandler ();
     67     extern void nativeLlcpConnectionlessSocket_receiveData (uint8_t* data, uint32_t len, uint32_t remote_sap);
     68 }
     69 
     70 
     71 /*****************************************************************************
     72 **
     73 ** public variables and functions
     74 **
     75 *****************************************************************************/
     76 bool                        gActivated = false;
     77 SyncEvent                   gDeactivatedEvent;
     78 
     79 namespace android
     80 {
     81     jmethodID               gCachedNfcManagerNotifyNdefMessageListeners;
     82     jmethodID               gCachedNfcManagerNotifyTransactionListeners;
     83     jmethodID               gCachedNfcManagerNotifyLlcpLinkActivation;
     84     jmethodID               gCachedNfcManagerNotifyLlcpLinkDeactivated;
     85     jmethodID               gCachedNfcManagerNotifyLlcpFirstPacketReceived;
     86     jmethodID               gCachedNfcManagerNotifyHostEmuActivated;
     87     jmethodID               gCachedNfcManagerNotifyHostEmuData;
     88     jmethodID               gCachedNfcManagerNotifyHostEmuDeactivated;
     89     jmethodID               gCachedNfcManagerNotifyRfFieldActivated;
     90     jmethodID               gCachedNfcManagerNotifyRfFieldDeactivated;
     91     const char*             gNativeP2pDeviceClassName                 = "com/android/nfc/dhimpl/NativeP2pDevice";
     92     const char*             gNativeLlcpServiceSocketClassName         = "com/android/nfc/dhimpl/NativeLlcpServiceSocket";
     93     const char*             gNativeLlcpConnectionlessSocketClassName  = "com/android/nfc/dhimpl/NativeLlcpConnectionlessSocket";
     94     const char*             gNativeLlcpSocketClassName                = "com/android/nfc/dhimpl/NativeLlcpSocket";
     95     const char*             gNativeNfcTagClassName                    = "com/android/nfc/dhimpl/NativeNfcTag";
     96     const char*             gNativeNfcManagerClassName                = "com/android/nfc/dhimpl/NativeNfcManager";
     97     void                    doStartupConfig ();
     98     void                    startStopPolling (bool isStartPolling);
     99     void                    startRfDiscovery (bool isStart);
    100     bool                    isDiscoveryStarted ();
    101 }
    102 
    103 
    104 /*****************************************************************************
    105 **
    106 ** private variables and functions
    107 **
    108 *****************************************************************************/
    109 namespace android
    110 {
    111 static jint                 sLastError = ERROR_BUFFER_TOO_SMALL;
    112 static SyncEvent            sNfaEnableEvent;  //event for NFA_Enable()
    113 static SyncEvent            sNfaDisableEvent;  //event for NFA_Disable()
    114 static SyncEvent            sNfaEnableDisablePollingEvent;  //event for NFA_EnablePolling(), NFA_DisablePolling()
    115 static SyncEvent            sNfaSetConfigEvent;  // event for Set_Config....
    116 static SyncEvent            sNfaGetConfigEvent;  // event for Get_Config....
    117 static bool                 sIsNfaEnabled = false;
    118 static bool                 sDiscoveryEnabled = false;  //is polling or listening
    119 static bool                 sPollingEnabled = false;  //is polling for tag?
    120 static bool                 sIsDisabling = false;
    121 static bool                 sRfEnabled = false; // whether RF discovery is enabled
    122 static bool                 sSeRfActive = false;  // whether RF with SE is likely active
    123 static bool                 sReaderModeEnabled = false; // whether we're only reading tags, not allowing P2p/card emu
    124 static bool                 sP2pEnabled = false;
    125 static bool                 sP2pActive = false; // whether p2p was last active
    126 static bool                 sAbortConnlessWait = false;
    127 static jint                 sLfT3tMax = 0;
    128 
    129 #define CONFIG_UPDATE_TECH_MASK     (1 << 1)
    130 #define DEFAULT_TECH_MASK           (NFA_TECHNOLOGY_MASK_A \
    131                                      | NFA_TECHNOLOGY_MASK_B \
    132                                      | NFA_TECHNOLOGY_MASK_F \
    133                                      | NFA_TECHNOLOGY_MASK_ISO15693 \
    134                                      | NFA_TECHNOLOGY_MASK_B_PRIME \
    135                                      | NFA_TECHNOLOGY_MASK_A_ACTIVE \
    136                                      | NFA_TECHNOLOGY_MASK_F_ACTIVE \
    137                                      | NFA_TECHNOLOGY_MASK_KOVIO)
    138 #define DEFAULT_DISCOVERY_DURATION       500
    139 #define READER_MODE_DISCOVERY_DURATION   200
    140 
    141 static void nfaConnectionCallback (uint8_t event, tNFA_CONN_EVT_DATA *eventData);
    142 static void nfaDeviceManagementCallback (uint8_t event, tNFA_DM_CBACK_DATA *eventData);
    143 static bool isPeerToPeer (tNFA_ACTIVATED& activated);
    144 static bool isListenMode(tNFA_ACTIVATED& activated);
    145 static void enableDisableLptd (bool enable);
    146 static tNFA_STATUS stopPolling_rfDiscoveryDisabled();
    147 static tNFA_STATUS startPolling_rfDiscoveryDisabled(tNFA_TECHNOLOGY_MASK tech_mask);
    148 
    149 static uint16_t sCurrentConfigLen;
    150 static uint8_t sConfig[256];
    151 
    152 /////////////////////////////////////////////////////////////
    153 /////////////////////////////////////////////////////////////
    154 
    155 
    156 /*******************************************************************************
    157 **
    158 ** Function:        getNative
    159 **
    160 ** Description:     Get native data
    161 **
    162 ** Returns:         Native data structure.
    163 **
    164 *******************************************************************************/
    165 nfc_jni_native_data *getNative (JNIEnv* e, jobject o)
    166 {
    167     static struct nfc_jni_native_data *sCachedNat = NULL;
    168     if (e)
    169     {
    170         sCachedNat = nfc_jni_get_nat(e, o);
    171     }
    172     return sCachedNat;
    173 }
    174 
    175 
    176 /*******************************************************************************
    177 **
    178 ** Function:        handleRfDiscoveryEvent
    179 **
    180 ** Description:     Handle RF-discovery events from the stack.
    181 **                  discoveredDevice: Discovered device.
    182 **
    183 ** Returns:         None
    184 **
    185 *******************************************************************************/
    186 static void handleRfDiscoveryEvent (tNFC_RESULT_DEVT* discoveredDevice)
    187 {
    188     if (discoveredDevice->more == NCI_DISCOVER_NTF_MORE)
    189     {
    190         //there is more discovery notification coming
    191         return;
    192     }
    193 
    194     bool isP2p = NfcTag::getInstance ().isP2pDiscovered ();
    195     if (!sReaderModeEnabled && isP2p)
    196     {
    197         //select the peer that supports P2P
    198         NfcTag::getInstance ().selectP2p();
    199     }
    200     else
    201     {
    202         //select the first of multiple tags that is discovered
    203         NfcTag::getInstance ().selectFirstTag();
    204     }
    205 }
    206 
    207 
    208 /*******************************************************************************
    209 **
    210 ** Function:        nfaConnectionCallback
    211 **
    212 ** Description:     Receive connection-related events from stack.
    213 **                  connEvent: Event code.
    214 **                  eventData: Event data.
    215 **
    216 ** Returns:         None
    217 **
    218 *******************************************************************************/
    219 static void nfaConnectionCallback (uint8_t connEvent, tNFA_CONN_EVT_DATA* eventData)
    220 {
    221     tNFA_STATUS status = NFA_STATUS_FAILED;
    222     ALOGV("%s: event= %u", __func__, connEvent);
    223 
    224     switch (connEvent)
    225     {
    226     case NFA_POLL_ENABLED_EVT: // whether polling successfully started
    227         {
    228             ALOGV("%s: NFA_POLL_ENABLED_EVT: status = %u", __func__, eventData->status);
    229 
    230             SyncEventGuard guard (sNfaEnableDisablePollingEvent);
    231             sNfaEnableDisablePollingEvent.notifyOne ();
    232         }
    233         break;
    234 
    235     case NFA_POLL_DISABLED_EVT: // Listening/Polling stopped
    236         {
    237             ALOGV("%s: NFA_POLL_DISABLED_EVT: status = %u", __func__, eventData->status);
    238 
    239             SyncEventGuard guard (sNfaEnableDisablePollingEvent);
    240             sNfaEnableDisablePollingEvent.notifyOne ();
    241         }
    242         break;
    243 
    244     case NFA_RF_DISCOVERY_STARTED_EVT: // RF Discovery started
    245         {
    246             ALOGV("%s: NFA_RF_DISCOVERY_STARTED_EVT: status = %u", __func__, eventData->status);
    247 
    248             SyncEventGuard guard (sNfaEnableDisablePollingEvent);
    249             sNfaEnableDisablePollingEvent.notifyOne ();
    250         }
    251         break;
    252 
    253     case NFA_RF_DISCOVERY_STOPPED_EVT: // RF Discovery stopped event
    254         {
    255             ALOGV("%s: NFA_RF_DISCOVERY_STOPPED_EVT: status = %u", __func__, eventData->status);
    256 
    257             SyncEventGuard guard (sNfaEnableDisablePollingEvent);
    258             sNfaEnableDisablePollingEvent.notifyOne ();
    259         }
    260         break;
    261 
    262     case NFA_DISC_RESULT_EVT: // NFC link/protocol discovery notificaiton
    263         status = eventData->disc_result.status;
    264         ALOGV("%s: NFA_DISC_RESULT_EVT: status = %d", __func__, status);
    265         if (status != NFA_STATUS_OK)
    266         {
    267             ALOGE("%s: NFA_DISC_RESULT_EVT error: status = %d", __func__, status);
    268         }
    269         else
    270         {
    271             NfcTag::getInstance().connectionEventHandler(connEvent, eventData);
    272             handleRfDiscoveryEvent(&eventData->disc_result.discovery_ntf);
    273         }
    274         break;
    275 
    276     case NFA_SELECT_RESULT_EVT: // NFC link/protocol discovery select response
    277         ALOGV("%s: NFA_SELECT_RESULT_EVT: status = %d, gIsSelectingRfInterface = %d, sIsDisabling=%d", __func__, eventData->status, gIsSelectingRfInterface, sIsDisabling);
    278 
    279         if (sIsDisabling)
    280             break;
    281 
    282         if (eventData->status != NFA_STATUS_OK)
    283         {
    284             if (gIsSelectingRfInterface)
    285             {
    286                 nativeNfcTag_doConnectStatus(false);
    287             }
    288 
    289             ALOGE("%s: NFA_SELECT_RESULT_EVT error: status = %d", __func__, eventData->status);
    290             NFA_Deactivate (FALSE);
    291         }
    292         break;
    293 
    294     case NFA_DEACTIVATE_FAIL_EVT:
    295         ALOGV("%s: NFA_DEACTIVATE_FAIL_EVT: status = %d", __func__, eventData->status);
    296         break;
    297 
    298     case NFA_ACTIVATED_EVT: // NFC link/protocol activated
    299         ALOGV("%s: NFA_ACTIVATED_EVT: gIsSelectingRfInterface=%d, sIsDisabling=%d", __func__, gIsSelectingRfInterface, sIsDisabling);
    300         if((eventData->activated.activate_ntf.protocol != NFA_PROTOCOL_NFC_DEP) && (!isListenMode (eventData->activated)))
    301         {
    302             nativeNfcTag_setRfInterface ((tNFA_INTF_TYPE) eventData->activated.activate_ntf.intf_param.type);
    303         }
    304         if (EXTNS_GetConnectFlag () == TRUE)
    305         {
    306             NfcTag::getInstance().setActivationState ();
    307             nativeNfcTag_doConnectStatus (true);
    308             break;
    309         }
    310         NfcTag::getInstance().setActive(true);
    311         if (sIsDisabling || !sIsNfaEnabled)
    312             break;
    313         gActivated = true;
    314 
    315         NfcTag::getInstance().setActivationState ();
    316         if (gIsSelectingRfInterface)
    317         {
    318             nativeNfcTag_doConnectStatus(true);
    319             break;
    320         }
    321 
    322         nativeNfcTag_resetPresenceCheck();
    323         if (isPeerToPeer(eventData->activated))
    324         {
    325             if (sReaderModeEnabled)
    326             {
    327                 ALOGV("%s: ignoring peer target in reader mode.", __func__);
    328                 NFA_Deactivate (FALSE);
    329                 break;
    330             }
    331             sP2pActive = true;
    332             ALOGV("%s: NFA_ACTIVATED_EVT; is p2p", __func__);
    333             // Disable RF field events in case of p2p
    334             uint8_t  nfa_disable_rf_events[] = { 0x00 };
    335             ALOGV("%s: Disabling RF field events", __func__);
    336             status = NFA_SetConfig(NCI_PARAM_ID_RF_FIELD_INFO, sizeof(nfa_disable_rf_events),
    337                     &nfa_disable_rf_events[0]);
    338             if (status == NFA_STATUS_OK) {
    339                 ALOGV("%s: Disabled RF field events", __func__);
    340             } else {
    341                 ALOGE("%s: Failed to disable RF field events", __func__);
    342             }
    343         }
    344         else if (pn544InteropIsBusy() == false)
    345         {
    346             NfcTag::getInstance().connectionEventHandler (connEvent, eventData);
    347 
    348             // We know it is not activating for P2P.  If it activated in
    349             // listen mode then it is likely for an SE transaction.
    350             // Send the RF Event.
    351             if (isListenMode(eventData->activated))
    352             {
    353                 sSeRfActive = true;
    354             }
    355         }
    356         break;
    357 
    358     case NFA_DEACTIVATED_EVT: // NFC link/protocol deactivated
    359         ALOGV("%s: NFA_DEACTIVATED_EVT   Type: %u, gIsTagDeactivating: %d", __func__, eventData->deactivated.type,gIsTagDeactivating);
    360         NfcTag::getInstance().setDeactivationState (eventData->deactivated);
    361         if (eventData->deactivated.type != NFA_DEACTIVATE_TYPE_SLEEP)
    362         {
    363             {
    364                 SyncEventGuard g (gDeactivatedEvent);
    365                 gActivated = false; //guard this variable from multi-threaded access
    366                 gDeactivatedEvent.notifyOne ();
    367             }
    368             nativeNfcTag_resetPresenceCheck();
    369             NfcTag::getInstance().connectionEventHandler (connEvent, eventData);
    370             nativeNfcTag_abortWaits();
    371             NfcTag::getInstance().abort ();
    372         }
    373         else if (gIsTagDeactivating)
    374         {
    375             NfcTag::getInstance ().setActive (false);
    376             nativeNfcTag_doDeactivateStatus (0);
    377         }
    378         else if (EXTNS_GetDeactivateFlag () == TRUE)
    379         {
    380             NfcTag::getInstance().setActive (false);
    381             nativeNfcTag_doDeactivateStatus (0);
    382         }
    383 
    384         // If RF is activated for what we think is a Secure Element transaction
    385         // and it is deactivated to either IDLE or DISCOVERY mode, notify w/event.
    386         if ((eventData->deactivated.type == NFA_DEACTIVATE_TYPE_IDLE)
    387                 || (eventData->deactivated.type == NFA_DEACTIVATE_TYPE_DISCOVERY))
    388         {
    389             if (sSeRfActive) {
    390                 sSeRfActive = false;
    391             } else if (sP2pActive) {
    392                 sP2pActive = false;
    393                 // Make sure RF field events are re-enabled
    394                 ALOGV("%s: NFA_DEACTIVATED_EVT; is p2p", __func__);
    395                 // Disable RF field events in case of p2p
    396                 uint8_t  nfa_enable_rf_events[] = { 0x01 };
    397 
    398                 if (!sIsDisabling && sIsNfaEnabled)
    399                 {
    400                     ALOGV("%s: Enabling RF field events", __func__);
    401                     status = NFA_SetConfig(NCI_PARAM_ID_RF_FIELD_INFO, sizeof(nfa_enable_rf_events),
    402                             &nfa_enable_rf_events[0]);
    403                     if (status == NFA_STATUS_OK) {
    404                         ALOGV("%s: Enabled RF field events", __func__);
    405                     } else {
    406                         ALOGE("%s: Failed to enable RF field events", __func__);
    407                     }
    408                 }
    409             }
    410         }
    411 
    412         break;
    413 
    414     case NFA_TLV_DETECT_EVT: // TLV Detection complete
    415         status = eventData->tlv_detect.status;
    416         ALOGV("%s: NFA_TLV_DETECT_EVT: status = %d, protocol = %d, num_tlvs = %d, num_bytes = %d",
    417              __func__, status, eventData->tlv_detect.protocol,
    418              eventData->tlv_detect.num_tlvs, eventData->tlv_detect.num_bytes);
    419         if (status != NFA_STATUS_OK)
    420         {
    421             ALOGE("%s: NFA_TLV_DETECT_EVT error: status = %d", __func__, status);
    422         }
    423         break;
    424 
    425     case NFA_NDEF_DETECT_EVT: // NDEF Detection complete;
    426         //if status is failure, it means the tag does not contain any or valid NDEF data;
    427         //pass the failure status to the NFC Service;
    428         status = eventData->ndef_detect.status;
    429         ALOGV("%s: NFA_NDEF_DETECT_EVT: status = 0x%X, protocol = %u, "
    430              "max_size = %u, cur_size = %u, flags = 0x%X", __func__,
    431              status,
    432              eventData->ndef_detect.protocol, eventData->ndef_detect.max_size,
    433              eventData->ndef_detect.cur_size, eventData->ndef_detect.flags);
    434         NfcTag::getInstance().connectionEventHandler (connEvent, eventData);
    435         nativeNfcTag_doCheckNdefResult(status,
    436             eventData->ndef_detect.max_size, eventData->ndef_detect.cur_size,
    437             eventData->ndef_detect.flags);
    438         break;
    439 
    440     case NFA_DATA_EVT: // Data message received (for non-NDEF reads)
    441         ALOGV("%s: NFA_DATA_EVT: status = 0x%X, len = %d", __func__, eventData->status, eventData->data.len);
    442         nativeNfcTag_doTransceiveStatus(eventData->status, eventData->data.p_data, eventData->data.len);
    443         break;
    444     case NFA_RW_INTF_ERROR_EVT:
    445         ALOGV("%s: NFC_RW_INTF_ERROR_EVT", __func__);
    446         nativeNfcTag_notifyRfTimeout();
    447         nativeNfcTag_doReadCompleted (NFA_STATUS_TIMEOUT);
    448         break;
    449     case NFA_SELECT_CPLT_EVT: // Select completed
    450         status = eventData->status;
    451         ALOGV("%s: NFA_SELECT_CPLT_EVT: status = %d", __func__, status);
    452         if (status != NFA_STATUS_OK)
    453         {
    454             ALOGE("%s: NFA_SELECT_CPLT_EVT error: status = %d", __func__, status);
    455         }
    456         break;
    457 
    458     case NFA_READ_CPLT_EVT: // NDEF-read or tag-specific-read completed
    459         ALOGV("%s: NFA_READ_CPLT_EVT: status = 0x%X", __func__, eventData->status);
    460         nativeNfcTag_doReadCompleted (eventData->status);
    461         NfcTag::getInstance().connectionEventHandler (connEvent, eventData);
    462         break;
    463 
    464     case NFA_WRITE_CPLT_EVT: // Write completed
    465         ALOGV("%s: NFA_WRITE_CPLT_EVT: status = %d", __func__, eventData->status);
    466         nativeNfcTag_doWriteStatus (eventData->status == NFA_STATUS_OK);
    467         break;
    468 
    469     case NFA_SET_TAG_RO_EVT: // Tag set as Read only
    470         ALOGV("%s: NFA_SET_TAG_RO_EVT: status = %d", __func__, eventData->status);
    471         nativeNfcTag_doMakeReadonlyResult(eventData->status);
    472         break;
    473 
    474     case NFA_CE_NDEF_WRITE_START_EVT: // NDEF write started
    475         ALOGV("%s: NFA_CE_NDEF_WRITE_START_EVT: status: %d", __func__, eventData->status);
    476 
    477         if (eventData->status != NFA_STATUS_OK)
    478             ALOGE("%s: NFA_CE_NDEF_WRITE_START_EVT error: status = %d", __func__, eventData->status);
    479         break;
    480 
    481     case NFA_CE_NDEF_WRITE_CPLT_EVT: // NDEF write completed
    482         ALOGV("%s: FA_CE_NDEF_WRITE_CPLT_EVT: len = %u", __func__, eventData->ndef_write_cplt.len);
    483         break;
    484 
    485     case NFA_LLCP_ACTIVATED_EVT: // LLCP link is activated
    486         ALOGV("%s: NFA_LLCP_ACTIVATED_EVT: is_initiator: %d  remote_wks: %d, remote_lsc: %d, remote_link_miu: %d, local_link_miu: %d",
    487              __func__,
    488              eventData->llcp_activated.is_initiator,
    489              eventData->llcp_activated.remote_wks,
    490              eventData->llcp_activated.remote_lsc,
    491              eventData->llcp_activated.remote_link_miu,
    492              eventData->llcp_activated.local_link_miu);
    493 
    494         PeerToPeer::getInstance().llcpActivatedHandler (getNative(0, 0), eventData->llcp_activated);
    495         break;
    496 
    497     case NFA_LLCP_DEACTIVATED_EVT: // LLCP link is deactivated
    498         ALOGV("%s: NFA_LLCP_DEACTIVATED_EVT", __func__);
    499         PeerToPeer::getInstance().llcpDeactivatedHandler (getNative(0, 0), eventData->llcp_deactivated);
    500         break;
    501     case NFA_LLCP_FIRST_PACKET_RECEIVED_EVT: // Received first packet over llcp
    502         ALOGV("%s: NFA_LLCP_FIRST_PACKET_RECEIVED_EVT", __func__);
    503         PeerToPeer::getInstance().llcpFirstPacketHandler (getNative(0, 0));
    504         break;
    505     case NFA_PRESENCE_CHECK_EVT:
    506         ALOGV("%s: NFA_PRESENCE_CHECK_EVT", __func__);
    507         nativeNfcTag_doPresenceCheckResult (eventData->status);
    508         break;
    509     case NFA_FORMAT_CPLT_EVT:
    510         ALOGV("%s: NFA_FORMAT_CPLT_EVT: status=0x%X", __func__, eventData->status);
    511         nativeNfcTag_formatStatus (eventData->status == NFA_STATUS_OK);
    512         break;
    513 
    514     case NFA_I93_CMD_CPLT_EVT:
    515         ALOGV("%s: NFA_I93_CMD_CPLT_EVT: status=0x%X", __func__, eventData->status);
    516         break;
    517 
    518     case NFA_CE_UICC_LISTEN_CONFIGURED_EVT :
    519         ALOGV("%s: NFA_CE_UICC_LISTEN_CONFIGURED_EVT : status=0x%X", __func__, eventData->status);
    520         break;
    521 
    522     case NFA_SET_P2P_LISTEN_TECH_EVT:
    523         ALOGV("%s: NFA_SET_P2P_LISTEN_TECH_EVT", __func__);
    524         PeerToPeer::getInstance().connectionEventHandler (connEvent, eventData);
    525         break;
    526 
    527     default:
    528         ALOGV("%s: unknown event ????", __func__);
    529         break;
    530     }
    531 }
    532 
    533 
    534 /*******************************************************************************
    535 **
    536 ** Function:        nfcManager_initNativeStruc
    537 **
    538 ** Description:     Initialize variables.
    539 **                  e: JVM environment.
    540 **                  o: Java object.
    541 **
    542 ** Returns:         True if ok.
    543 **
    544 *******************************************************************************/
    545 static jboolean nfcManager_initNativeStruc (JNIEnv* e, jobject o)
    546 {
    547     ALOGV("%s: enter", __func__);
    548 
    549     nfc_jni_native_data* nat = (nfc_jni_native_data*)malloc(sizeof(struct nfc_jni_native_data));
    550     if (nat == NULL)
    551     {
    552         ALOGE("%s: fail allocate native data", __func__);
    553         return JNI_FALSE;
    554     }
    555 
    556     memset (nat, 0, sizeof(*nat));
    557     e->GetJavaVM(&(nat->vm));
    558     nat->env_version = e->GetVersion();
    559     nat->manager = e->NewGlobalRef(o);
    560 
    561     ScopedLocalRef<jclass> cls(e, e->GetObjectClass(o));
    562     jfieldID f = e->GetFieldID(cls.get(), "mNative", "J");
    563     e->SetLongField(o, f, (jlong)nat);
    564 
    565     /* Initialize native cached references */
    566     gCachedNfcManagerNotifyNdefMessageListeners = e->GetMethodID(cls.get(),
    567             "notifyNdefMessageListeners", "(Lcom/android/nfc/dhimpl/NativeNfcTag;)V");
    568     gCachedNfcManagerNotifyLlcpLinkActivation = e->GetMethodID(cls.get(),
    569             "notifyLlcpLinkActivation", "(Lcom/android/nfc/dhimpl/NativeP2pDevice;)V");
    570     gCachedNfcManagerNotifyLlcpLinkDeactivated = e->GetMethodID(cls.get(),
    571             "notifyLlcpLinkDeactivated", "(Lcom/android/nfc/dhimpl/NativeP2pDevice;)V");
    572     gCachedNfcManagerNotifyLlcpFirstPacketReceived = e->GetMethodID(cls.get(),
    573             "notifyLlcpLinkFirstPacketReceived", "(Lcom/android/nfc/dhimpl/NativeP2pDevice;)V");
    574 
    575     gCachedNfcManagerNotifyHostEmuActivated = e->GetMethodID(cls.get(),
    576             "notifyHostEmuActivated", "(I)V");
    577 
    578     gCachedNfcManagerNotifyHostEmuData = e->GetMethodID(cls.get(),
    579             "notifyHostEmuData", "(I[B)V");
    580 
    581     gCachedNfcManagerNotifyHostEmuDeactivated = e->GetMethodID(cls.get(),
    582             "notifyHostEmuDeactivated", "(I)V");
    583 
    584     gCachedNfcManagerNotifyRfFieldActivated = e->GetMethodID(cls.get(),
    585             "notifyRfFieldActivated", "()V");
    586     gCachedNfcManagerNotifyRfFieldDeactivated = e->GetMethodID(cls.get(),
    587             "notifyRfFieldDeactivated", "()V");
    588 
    589     if (nfc_jni_cache_object(e, gNativeNfcTagClassName, &(nat->cached_NfcTag)) == -1)
    590     {
    591         ALOGE("%s: fail cache NativeNfcTag", __func__);
    592         return JNI_FALSE;
    593     }
    594 
    595     if (nfc_jni_cache_object(e, gNativeP2pDeviceClassName, &(nat->cached_P2pDevice)) == -1)
    596     {
    597         ALOGE("%s: fail cache NativeP2pDevice", __func__);
    598         return JNI_FALSE;
    599     }
    600 
    601     ALOGV("%s: exit", __func__);
    602     return JNI_TRUE;
    603 }
    604 
    605 
    606 /*******************************************************************************
    607 **
    608 ** Function:        nfaDeviceManagementCallback
    609 **
    610 ** Description:     Receive device management events from stack.
    611 **                  dmEvent: Device-management event ID.
    612 **                  eventData: Data associated with event ID.
    613 **
    614 ** Returns:         None
    615 **
    616 *******************************************************************************/
    617 void nfaDeviceManagementCallback (uint8_t dmEvent, tNFA_DM_CBACK_DATA* eventData)
    618 {
    619     ALOGV("%s: enter; event=0x%X", __func__, dmEvent);
    620 
    621     switch (dmEvent)
    622     {
    623     case NFA_DM_ENABLE_EVT: /* Result of NFA_Enable */
    624         {
    625             SyncEventGuard guard (sNfaEnableEvent);
    626             ALOGV("%s: NFA_DM_ENABLE_EVT; status=0x%X",
    627                     __func__, eventData->status);
    628             sIsNfaEnabled = eventData->status == NFA_STATUS_OK;
    629             sIsDisabling = false;
    630             sNfaEnableEvent.notifyOne ();
    631         }
    632         break;
    633 
    634     case NFA_DM_DISABLE_EVT: /* Result of NFA_Disable */
    635         {
    636             SyncEventGuard guard (sNfaDisableEvent);
    637             ALOGV("%s: NFA_DM_DISABLE_EVT", __func__);
    638             sIsNfaEnabled = false;
    639             sIsDisabling = false;
    640             sNfaDisableEvent.notifyOne ();
    641         }
    642         break;
    643 
    644     case NFA_DM_SET_CONFIG_EVT: //result of NFA_SetConfig
    645         ALOGV("%s: NFA_DM_SET_CONFIG_EVT", __func__);
    646         {
    647             SyncEventGuard guard (sNfaSetConfigEvent);
    648             sNfaSetConfigEvent.notifyOne();
    649         }
    650         break;
    651 
    652     case NFA_DM_GET_CONFIG_EVT: /* Result of NFA_GetConfig */
    653         ALOGV("%s: NFA_DM_GET_CONFIG_EVT", __func__);
    654         {
    655             SyncEventGuard guard (sNfaGetConfigEvent);
    656             if (eventData->status == NFA_STATUS_OK &&
    657                     eventData->get_config.tlv_size <= sizeof(sConfig))
    658             {
    659                 sCurrentConfigLen = eventData->get_config.tlv_size;
    660                 memcpy(sConfig, eventData->get_config.param_tlvs, eventData->get_config.tlv_size);
    661             }
    662             else
    663             {
    664                 ALOGE("%s: NFA_DM_GET_CONFIG failed", __func__);
    665                 sCurrentConfigLen = 0;
    666             }
    667             sNfaGetConfigEvent.notifyOne();
    668         }
    669         break;
    670 
    671     case NFA_DM_RF_FIELD_EVT:
    672         ALOGV("%s: NFA_DM_RF_FIELD_EVT; status=0x%X; field status=%u", __func__,
    673               eventData->rf_field.status, eventData->rf_field.rf_field_status);
    674         if (!sP2pActive && eventData->rf_field.status == NFA_STATUS_OK)
    675         {
    676             struct nfc_jni_native_data *nat = getNative(NULL, NULL);
    677             JNIEnv* e = NULL;
    678             ScopedAttach attach(nat->vm, &e);
    679             if (e == NULL)
    680             {
    681                 ALOGE("jni env is null");
    682                 return;
    683             }
    684             if (eventData->rf_field.rf_field_status == NFA_DM_RF_FIELD_ON)
    685                 e->CallVoidMethod (nat->manager, android::gCachedNfcManagerNotifyRfFieldActivated);
    686             else
    687                 e->CallVoidMethod (nat->manager, android::gCachedNfcManagerNotifyRfFieldDeactivated);
    688         }
    689         break;
    690 
    691     case NFA_DM_NFCC_TRANSPORT_ERR_EVT:
    692     case NFA_DM_NFCC_TIMEOUT_EVT:
    693         {
    694             if (dmEvent == NFA_DM_NFCC_TIMEOUT_EVT)
    695                 ALOGE("%s: NFA_DM_NFCC_TIMEOUT_EVT; abort", __func__);
    696             else if (dmEvent == NFA_DM_NFCC_TRANSPORT_ERR_EVT)
    697                 ALOGE("%s: NFA_DM_NFCC_TRANSPORT_ERR_EVT; abort", __func__);
    698 
    699             nativeNfcTag_abortWaits();
    700             NfcTag::getInstance().abort ();
    701             sAbortConnlessWait = true;
    702             nativeLlcpConnectionlessSocket_abortWait();
    703             {
    704                 ALOGV("%s: aborting  sNfaEnableDisablePollingEvent", __func__);
    705                 SyncEventGuard guard (sNfaEnableDisablePollingEvent);
    706                 sNfaEnableDisablePollingEvent.notifyOne();
    707             }
    708             {
    709                 ALOGV("%s: aborting  sNfaEnableEvent", __func__);
    710                 SyncEventGuard guard (sNfaEnableEvent);
    711                 sNfaEnableEvent.notifyOne();
    712             }
    713             {
    714                 ALOGV("%s: aborting  sNfaDisableEvent", __func__);
    715                 SyncEventGuard guard (sNfaDisableEvent);
    716                 sNfaDisableEvent.notifyOne();
    717             }
    718             sDiscoveryEnabled = false;
    719             sPollingEnabled = false;
    720             PowerSwitch::getInstance ().abort ();
    721 
    722             if (!sIsDisabling && sIsNfaEnabled)
    723             {
    724                 EXTNS_Close ();
    725                 NFA_Disable(FALSE);
    726                 sIsDisabling = true;
    727             }
    728             else
    729             {
    730                 sIsNfaEnabled = false;
    731                 sIsDisabling = false;
    732             }
    733             PowerSwitch::getInstance ().initialize (PowerSwitch::UNKNOWN_LEVEL);
    734             ALOGE("%s: crash NFC service", __func__);
    735             //////////////////////////////////////////////
    736             //crash the NFC service process so it can restart automatically
    737             abort ();
    738             //////////////////////////////////////////////
    739         }
    740         break;
    741 
    742     case NFA_DM_PWR_MODE_CHANGE_EVT:
    743         PowerSwitch::getInstance ().deviceManagementCallback (dmEvent, eventData);
    744         break;
    745 
    746     default:
    747         ALOGV("%s: unhandled event", __func__);
    748         break;
    749     }
    750 }
    751 
    752 /*******************************************************************************
    753 **
    754 ** Function:        nfcManager_sendRawFrame
    755 **
    756 ** Description:     Send a raw frame.
    757 **                  e: JVM environment.
    758 **                  o: Java object.
    759 **
    760 ** Returns:         True if ok.
    761 **
    762 *******************************************************************************/
    763 static jboolean nfcManager_sendRawFrame (JNIEnv* e, jobject, jbyteArray data)
    764 {
    765     ScopedByteArrayRO bytes(e, data);
    766     uint8_t* buf = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&bytes[0]));
    767     size_t bufLen = bytes.size();
    768     tNFA_STATUS status = NFA_SendRawFrame (buf, bufLen, 0);
    769 
    770     return (status == NFA_STATUS_OK);
    771 }
    772 
    773 /*******************************************************************************
    774 **
    775 ** Function:        nfcManager_routeAid
    776 **
    777 ** Description:     Route an AID to an EE
    778 **                  e: JVM environment.
    779 **                  o: Java object.
    780 **
    781 ** Returns:         True if ok.
    782 **
    783 *******************************************************************************/
    784 static jboolean nfcManager_routeAid (JNIEnv* e, jobject, jbyteArray aid, jint route)
    785 {
    786     ScopedByteArrayRO bytes(e, aid);
    787     uint8_t* buf = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&bytes[0]));
    788     size_t bufLen = bytes.size();
    789     bool result = RoutingManager::getInstance().addAidRouting(buf, bufLen, route);
    790     return result;
    791 }
    792 
    793 /*******************************************************************************
    794 **
    795 ** Function:        nfcManager_unrouteAid
    796 **
    797 ** Description:     Remove a AID routing
    798 **                  e: JVM environment.
    799 **                  o: Java object.
    800 **
    801 ** Returns:         True if ok.
    802 **
    803 *******************************************************************************/
    804 static jboolean nfcManager_unrouteAid (JNIEnv* e, jobject, jbyteArray aid)
    805 {
    806     ScopedByteArrayRO bytes(e, aid);
    807     uint8_t* buf = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&bytes[0]));
    808     size_t bufLen = bytes.size();
    809     bool result = RoutingManager::getInstance().removeAidRouting(buf, bufLen);
    810     return result;
    811 }
    812 
    813 /*******************************************************************************
    814 **
    815 ** Function:        nfcManager_commitRouting
    816 **
    817 ** Description:     Sends the AID routing table to the controller
    818 **                  e: JVM environment.
    819 **                  o: Java object.
    820 **
    821 ** Returns:         True if ok.
    822 **
    823 *******************************************************************************/
    824 static jboolean nfcManager_commitRouting (JNIEnv* e, jobject)
    825 {
    826     return RoutingManager::getInstance().commitRouting();
    827 }
    828 
    829 /*******************************************************************************
    830 **
    831 ** Function:        nfcManager_doRegisterT3tIdentifier
    832 **
    833 ** Description:     Registers LF_T3T_IDENTIFIER for NFC-F.
    834 **                  e: JVM environment.
    835 **                  o: Java object.
    836 **                  t3tIdentifier: LF_T3T_IDENTIFIER value (10 or 18 bytes)
    837 **
    838 ** Returns:         Handle retrieve from RoutingManager.
    839 **
    840 *******************************************************************************/
    841 static jint nfcManager_doRegisterT3tIdentifier(JNIEnv* e, jobject, jbyteArray t3tIdentifier)
    842 {
    843     ALOGV("%s: enter", __func__);
    844 
    845     ScopedByteArrayRO bytes(e, t3tIdentifier);
    846     uint8_t* buf = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&bytes[0]));
    847     size_t bufLen = bytes.size();
    848     int handle = RoutingManager::getInstance().registerT3tIdentifier(buf, bufLen);
    849 
    850     ALOGV("%s: handle=%d", __func__, handle);
    851     ALOGV("%s: exit", __func__);
    852 
    853     return handle;
    854 }
    855 
    856 /*******************************************************************************
    857 **
    858 ** Function:        nfcManager_doDeregisterT3tIdentifier
    859 **
    860 ** Description:     Deregisters LF_T3T_IDENTIFIER for NFC-F.
    861 **                  e: JVM environment.
    862 **                  o: Java object.
    863 **                  handle: Handle retrieve from libnfc-nci.
    864 **
    865 ** Returns:         None
    866 **
    867 *******************************************************************************/
    868 static void nfcManager_doDeregisterT3tIdentifier(JNIEnv*, jobject, jint handle)
    869 {
    870     ALOGV("%s: enter; handle=%d", __func__, handle);
    871 
    872     RoutingManager::getInstance().deregisterT3tIdentifier(handle);
    873 
    874     ALOGV("%s: exit", __func__);
    875 }
    876 
    877 /*******************************************************************************
    878 **
    879 ** Function:        nfcManager_getLfT3tMax
    880 **
    881 ** Description:     Returns LF_T3T_MAX value.
    882 **                  e: JVM environment.
    883 **                  o: Java object.
    884 **
    885 ** Returns:         LF_T3T_MAX value.
    886 **
    887 *******************************************************************************/
    888 static jint nfcManager_getLfT3tMax(JNIEnv*, jobject)
    889 {
    890     ALOGV("%s: enter", __func__);
    891     ALOGV("LF_T3T_MAX=%d", sLfT3tMax);
    892     ALOGV("%s: exit", __func__);
    893 
    894     return sLfT3tMax;
    895 }
    896 
    897 /*******************************************************************************
    898 **
    899 ** Function:        nfcManager_doInitialize
    900 **
    901 ** Description:     Turn on NFC.
    902 **                  e: JVM environment.
    903 **                  o: Java object.
    904 **
    905 ** Returns:         True if ok.
    906 **
    907 *******************************************************************************/
    908 static jboolean nfcManager_doInitialize (JNIEnv* e, jobject o)
    909 {
    910     ALOGV("%s: enter; ver=%s nfa=%s NCI_VERSION=0x%02X",
    911         __func__, nfca_version_string, nfa_version_string, NCI_VERSION);
    912     tNFA_STATUS stat = NFA_STATUS_OK;
    913 
    914     PowerSwitch & powerSwitch = PowerSwitch::getInstance ();
    915 
    916     if (sIsNfaEnabled)
    917     {
    918         ALOGV("%s: already enabled", __func__);
    919         goto TheEnd;
    920     }
    921 
    922     powerSwitch.initialize (PowerSwitch::FULL_POWER);
    923 
    924     {
    925         unsigned long num = 0;
    926 
    927         NfcAdaptation& theInstance = NfcAdaptation::GetInstance();
    928         theInstance.Initialize(); //start GKI, NCI task, NFC task
    929 
    930         {
    931             SyncEventGuard guard (sNfaEnableEvent);
    932             tHAL_NFC_ENTRY* halFuncEntries = theInstance.GetHalEntryFuncs ();
    933 
    934             NFA_Init (halFuncEntries);
    935 
    936             stat = NFA_Enable (nfaDeviceManagementCallback, nfaConnectionCallback);
    937             if (stat == NFA_STATUS_OK)
    938             {
    939                 num = initializeGlobalAppLogLevel ();
    940                 CE_SetTraceLevel (num);
    941                 LLCP_SetTraceLevel (num);
    942                 NFC_SetTraceLevel (num);
    943                 RW_SetTraceLevel (num);
    944                 NFA_SetTraceLevel (num);
    945                 NFA_P2pSetTraceLevel (num);
    946                 sNfaEnableEvent.wait(); //wait for NFA command to finish
    947             }
    948             EXTNS_Init (nfaDeviceManagementCallback, nfaConnectionCallback);
    949         }
    950 
    951         if (stat == NFA_STATUS_OK)
    952         {
    953             //sIsNfaEnabled indicates whether stack started successfully
    954             if (sIsNfaEnabled)
    955             {
    956                 RoutingManager::getInstance().initialize(getNative(e, o));
    957                 nativeNfcTag_registerNdefTypeHandler ();
    958                 NfcTag::getInstance().initialize (getNative(e, o));
    959                 PeerToPeer::getInstance().initialize ();
    960                 PeerToPeer::getInstance().handleNfcOnOff (true);
    961 
    962                 /////////////////////////////////////////////////////////////////////////////////
    963                 // Add extra configuration here (work-arounds, etc.)
    964 
    965                 struct nfc_jni_native_data *nat = getNative(e, o);
    966 
    967                 if ( nat )
    968                 {
    969                     if (GetNumValue(NAME_POLLING_TECH_MASK, &num, sizeof(num)))
    970                         nat->tech_mask = num;
    971                     else
    972                         nat->tech_mask = DEFAULT_TECH_MASK;
    973                     ALOGV("%s: tag polling tech mask=0x%X", __func__, nat->tech_mask);
    974                 }
    975 
    976                 // if this value exists, set polling interval.
    977                 if (GetNumValue(NAME_NFA_DM_DISC_DURATION_POLL, &num, sizeof(num)))
    978                     nat->discovery_duration = num;
    979                 else
    980                     nat->discovery_duration = DEFAULT_DISCOVERY_DURATION;
    981 
    982                 NFA_SetRfDiscoveryDuration(nat->discovery_duration);
    983 
    984                 // get LF_T3T_MAX
    985                 {
    986                     SyncEventGuard guard (sNfaGetConfigEvent);
    987                     tNFA_PMID configParam[1] = {NCI_PARAM_ID_LF_T3T_MAX};
    988                     stat = NFA_GetConfig(1, configParam);
    989                     if (stat == NFA_STATUS_OK)
    990                     {
    991                         sNfaGetConfigEvent.wait ();
    992                         if (sCurrentConfigLen >= 4 || sConfig[1] == NCI_PARAM_ID_LF_T3T_MAX) {
    993                             ALOGV("%s: lfT3tMax=%d", __func__, sConfig[3]);
    994                             sLfT3tMax = sConfig[3];
    995                         }
    996                     }
    997                 }
    998 
    999                 // Do custom NFCA startup configuration.
   1000                 doStartupConfig();
   1001                 goto TheEnd;
   1002             }
   1003         }
   1004 
   1005         ALOGE("%s: fail nfa enable; error=0x%X", __func__, stat);
   1006 
   1007         if (sIsNfaEnabled)
   1008         {
   1009             EXTNS_Close ();
   1010             stat = NFA_Disable (FALSE /* ungraceful */);
   1011         }
   1012 
   1013         theInstance.Finalize();
   1014     }
   1015 
   1016 TheEnd:
   1017     if (sIsNfaEnabled)
   1018         PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER);
   1019     ALOGV("%s: exit", __func__);
   1020     return sIsNfaEnabled ? JNI_TRUE : JNI_FALSE;
   1021 }
   1022 
   1023 
   1024 /*******************************************************************************
   1025 **
   1026 ** Function:        nfcManager_enableDiscovery
   1027 **
   1028 ** Description:     Start polling and listening for devices.
   1029 **                  e: JVM environment.
   1030 **                  o: Java object.
   1031 **                  technologies_mask: the bitmask of technologies for which to enable discovery
   1032 **                  enable_lptd: whether to enable low power polling (default: false)
   1033 **
   1034 ** Returns:         None
   1035 **
   1036 *******************************************************************************/
   1037 static void nfcManager_enableDiscovery (JNIEnv* e, jobject o, jint technologies_mask, \
   1038     jboolean enable_lptd, jboolean reader_mode, jboolean enable_host_routing, jboolean enable_p2p,
   1039     jboolean restart)
   1040 {
   1041     tNFA_TECHNOLOGY_MASK tech_mask = DEFAULT_TECH_MASK;
   1042     struct nfc_jni_native_data *nat = getNative(e, o);
   1043 
   1044     if (technologies_mask == -1 && nat)
   1045         tech_mask = (tNFA_TECHNOLOGY_MASK)nat->tech_mask;
   1046     else if (technologies_mask != -1)
   1047         tech_mask = (tNFA_TECHNOLOGY_MASK) technologies_mask;
   1048     ALOGV("%s: enter; tech_mask = %02x", __func__, tech_mask);
   1049 
   1050     if (sDiscoveryEnabled && !restart)
   1051     {
   1052         ALOGE("%s: already discovering", __func__);
   1053         return;
   1054     }
   1055 
   1056     PowerSwitch::getInstance ().setLevel (PowerSwitch::FULL_POWER);
   1057 
   1058     if (sRfEnabled) {
   1059         // Stop RF discovery to reconfigure
   1060         startRfDiscovery(false);
   1061     }
   1062 
   1063     // Check polling configuration
   1064     if (tech_mask != 0)
   1065     {
   1066         stopPolling_rfDiscoveryDisabled();
   1067         enableDisableLptd(enable_lptd);
   1068         startPolling_rfDiscoveryDisabled(tech_mask);
   1069 
   1070         // Start P2P listening if tag polling was enabled
   1071         if (sPollingEnabled)
   1072         {
   1073             ALOGV("%s: Enable p2pListening", __func__);
   1074 
   1075             if (enable_p2p && !sP2pEnabled) {
   1076                 sP2pEnabled = true;
   1077                 PeerToPeer::getInstance().enableP2pListening (true);
   1078                 NFA_ResumeP2p();
   1079             } else if (!enable_p2p && sP2pEnabled) {
   1080                 sP2pEnabled = false;
   1081                 PeerToPeer::getInstance().enableP2pListening (false);
   1082                 NFA_PauseP2p();
   1083             }
   1084 
   1085             if (reader_mode && !sReaderModeEnabled)
   1086             {
   1087                 sReaderModeEnabled = true;
   1088                 NFA_DisableListening();
   1089                 NFA_SetRfDiscoveryDuration(READER_MODE_DISCOVERY_DURATION);
   1090             }
   1091             else if (!reader_mode && sReaderModeEnabled)
   1092             {
   1093                 struct nfc_jni_native_data *nat = getNative(e, o);
   1094                 sReaderModeEnabled = false;
   1095                 NFA_EnableListening();
   1096                 NFA_SetRfDiscoveryDuration(nat->discovery_duration);
   1097             }
   1098         }
   1099     }
   1100     else
   1101     {
   1102         // No technologies configured, stop polling
   1103         stopPolling_rfDiscoveryDisabled();
   1104     }
   1105 
   1106     // Check listen configuration
   1107     if (enable_host_routing)
   1108     {
   1109         RoutingManager::getInstance().enableRoutingToHost();
   1110         RoutingManager::getInstance().commitRouting();
   1111     }
   1112     else
   1113     {
   1114         RoutingManager::getInstance().disableRoutingToHost();
   1115         RoutingManager::getInstance().commitRouting();
   1116     }
   1117     // Actually start discovery.
   1118     startRfDiscovery (true);
   1119     sDiscoveryEnabled = true;
   1120 
   1121     PowerSwitch::getInstance ().setModeOn (PowerSwitch::DISCOVERY);
   1122 
   1123     ALOGV("%s: exit", __func__);
   1124 }
   1125 
   1126 
   1127 /*******************************************************************************
   1128 **
   1129 ** Function:        nfcManager_disableDiscovery
   1130 **
   1131 ** Description:     Stop polling and listening for devices.
   1132 **                  e: JVM environment.
   1133 **                  o: Java object.
   1134 **
   1135 ** Returns:         None
   1136 **
   1137 *******************************************************************************/
   1138 void nfcManager_disableDiscovery (JNIEnv* e, jobject o)
   1139 {
   1140     tNFA_STATUS status = NFA_STATUS_OK;
   1141     ALOGV("%s: enter;", __func__);
   1142 
   1143     pn544InteropAbortNow ();
   1144     if (sDiscoveryEnabled == false)
   1145     {
   1146         ALOGV("%s: already disabled", __func__);
   1147         goto TheEnd;
   1148     }
   1149 
   1150     // Stop RF Discovery.
   1151     startRfDiscovery (false);
   1152 
   1153     if (sPollingEnabled)
   1154         status = stopPolling_rfDiscoveryDisabled();
   1155 
   1156     PeerToPeer::getInstance().enableP2pListening (false);
   1157     sP2pEnabled = false;
   1158     sDiscoveryEnabled = false;
   1159     //if nothing is active after this, then tell the controller to power down
   1160     if (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::DISCOVERY))
   1161         PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER);
   1162 TheEnd:
   1163     ALOGV("%s: exit", __func__);
   1164 }
   1165 
   1166 void enableDisableLptd (bool enable)
   1167 {
   1168     // This method is *NOT* thread-safe. Right now
   1169     // it is only called from the same thread so it's
   1170     // not an issue.
   1171     static bool sCheckedLptd = false;
   1172     static bool sHasLptd = false;
   1173 
   1174     tNFA_STATUS stat = NFA_STATUS_OK;
   1175     if (!sCheckedLptd)
   1176     {
   1177         sCheckedLptd = true;
   1178         SyncEventGuard guard (sNfaGetConfigEvent);
   1179         tNFA_PMID configParam[1] = {NCI_PARAM_ID_TAGSNIFF_CFG};
   1180         stat = NFA_GetConfig(1, configParam);
   1181         if (stat != NFA_STATUS_OK)
   1182         {
   1183             ALOGE("%s: NFA_GetConfig failed", __func__);
   1184             return;
   1185         }
   1186         sNfaGetConfigEvent.wait ();
   1187         if (sCurrentConfigLen < 4 || sConfig[1] != NCI_PARAM_ID_TAGSNIFF_CFG) {
   1188             ALOGE("%s: Config TLV length %d returned is too short", __func__,
   1189                     sCurrentConfigLen);
   1190             return;
   1191         }
   1192         if (sConfig[3] == 0) {
   1193             ALOGE("%s: LPTD is disabled, not enabling in current config", __func__);
   1194             return;
   1195         }
   1196         sHasLptd = true;
   1197     }
   1198     // Bail if we checked and didn't find any LPTD config before
   1199     if (!sHasLptd) return;
   1200     uint8_t enable_byte = enable ? 0x01 : 0x00;
   1201 
   1202     SyncEventGuard guard(sNfaSetConfigEvent);
   1203 
   1204     stat = NFA_SetConfig(NCI_PARAM_ID_TAGSNIFF_CFG, 1, &enable_byte);
   1205     if (stat == NFA_STATUS_OK)
   1206         sNfaSetConfigEvent.wait ();
   1207     else
   1208         ALOGE("%s: Could not configure LPTD feature", __func__);
   1209     return;
   1210 }
   1211 
   1212 
   1213 /*******************************************************************************
   1214 **
   1215 ** Function:        nfcManager_doCreateLlcpServiceSocket
   1216 **
   1217 ** Description:     Create a new LLCP server socket.
   1218 **                  e: JVM environment.
   1219 **                  o: Java object.
   1220 **                  nSap: Service access point.
   1221 **                  sn: Service name
   1222 **                  miu: Maximum information unit.
   1223 **                  rw: Receive window size.
   1224 **                  linearBufferLength: Max buffer size.
   1225 **
   1226 ** Returns:         NativeLlcpServiceSocket Java object.
   1227 **
   1228 *******************************************************************************/
   1229 static jobject nfcManager_doCreateLlcpServiceSocket (JNIEnv* e, jobject, jint nSap, jstring sn, jint miu, jint rw, jint linearBufferLength)
   1230 {
   1231     PeerToPeer::tJNI_HANDLE jniHandle = PeerToPeer::getInstance().getNewJniHandle ();
   1232 
   1233     ScopedUtfChars serviceName(e, sn);
   1234 
   1235     ALOGV("%s: enter: sap=%i; name=%s; miu=%i; rw=%i; buffLen=%i", __func__, nSap, serviceName.c_str(), miu, rw, linearBufferLength);
   1236 
   1237     /* Create new NativeLlcpServiceSocket object */
   1238     jobject serviceSocket = NULL;
   1239     if (nfc_jni_cache_object_local(e, gNativeLlcpServiceSocketClassName, &(serviceSocket)) == -1)
   1240     {
   1241         ALOGE("%s: Llcp socket object creation error", __func__);
   1242         return NULL;
   1243     }
   1244 
   1245     /* Get NativeLlcpServiceSocket class object */
   1246     ScopedLocalRef<jclass> clsNativeLlcpServiceSocket(e, e->GetObjectClass(serviceSocket));
   1247     if (e->ExceptionCheck())
   1248     {
   1249         e->ExceptionClear();
   1250         ALOGE("%s: Llcp Socket get object class error", __func__);
   1251         return NULL;
   1252     }
   1253 
   1254     if (!PeerToPeer::getInstance().registerServer (jniHandle, serviceName.c_str()))
   1255     {
   1256         ALOGE("%s: RegisterServer error", __func__);
   1257         return NULL;
   1258     }
   1259 
   1260     jfieldID f;
   1261 
   1262     /* Set socket handle to be the same as the NfaHandle*/
   1263     f = e->GetFieldID(clsNativeLlcpServiceSocket.get(), "mHandle", "I");
   1264     e->SetIntField(serviceSocket, f, (jint) jniHandle);
   1265     ALOGV("%s: socket Handle = 0x%X", __func__, jniHandle);
   1266 
   1267     /* Set socket linear buffer length */
   1268     f = e->GetFieldID(clsNativeLlcpServiceSocket.get(), "mLocalLinearBufferLength", "I");
   1269     e->SetIntField(serviceSocket, f,(jint)linearBufferLength);
   1270     ALOGV("%s: buffer length = %d", __func__, linearBufferLength);
   1271 
   1272     /* Set socket MIU */
   1273     f = e->GetFieldID(clsNativeLlcpServiceSocket.get(), "mLocalMiu", "I");
   1274     e->SetIntField(serviceSocket, f,(jint)miu);
   1275     ALOGV("%s: MIU = %d", __func__, miu);
   1276 
   1277     /* Set socket RW */
   1278     f = e->GetFieldID(clsNativeLlcpServiceSocket.get(), "mLocalRw", "I");
   1279     e->SetIntField(serviceSocket, f,(jint)rw);
   1280     ALOGV("%s:  RW = %d", __func__, rw);
   1281 
   1282     sLastError = 0;
   1283     ALOGV("%s: exit", __func__);
   1284     return serviceSocket;
   1285 }
   1286 
   1287 
   1288 /*******************************************************************************
   1289 **
   1290 ** Function:        nfcManager_doGetLastError
   1291 **
   1292 ** Description:     Get the last error code.
   1293 **                  e: JVM environment.
   1294 **                  o: Java object.
   1295 **
   1296 ** Returns:         Last error code.
   1297 **
   1298 *******************************************************************************/
   1299 static jint nfcManager_doGetLastError(JNIEnv*, jobject)
   1300 {
   1301     ALOGV("%s: last error=%i", __func__, sLastError);
   1302     return sLastError;
   1303 }
   1304 
   1305 
   1306 /*******************************************************************************
   1307 **
   1308 ** Function:        nfcManager_doDeinitialize
   1309 **
   1310 ** Description:     Turn off NFC.
   1311 **                  e: JVM environment.
   1312 **                  o: Java object.
   1313 **
   1314 ** Returns:         True if ok.
   1315 **
   1316 *******************************************************************************/
   1317 static jboolean nfcManager_doDeinitialize (JNIEnv*, jobject)
   1318 {
   1319     ALOGV("%s: enter", __func__);
   1320 
   1321     sIsDisabling = true;
   1322 
   1323     pn544InteropAbortNow ();
   1324     RoutingManager::getInstance().onNfccShutdown();
   1325     PowerSwitch::getInstance ().initialize (PowerSwitch::UNKNOWN_LEVEL);
   1326 
   1327     if (sIsNfaEnabled)
   1328     {
   1329         SyncEventGuard guard (sNfaDisableEvent);
   1330         EXTNS_Close ();
   1331         tNFA_STATUS stat = NFA_Disable (TRUE /* graceful */);
   1332         if (stat == NFA_STATUS_OK)
   1333         {
   1334             ALOGV("%s: wait for completion", __func__);
   1335             sNfaDisableEvent.wait (); //wait for NFA command to finish
   1336             PeerToPeer::getInstance ().handleNfcOnOff (false);
   1337         }
   1338         else
   1339         {
   1340             ALOGE("%s: fail disable; error=0x%X", __func__, stat);
   1341         }
   1342     }
   1343     nativeNfcTag_abortWaits();
   1344     NfcTag::getInstance().abort ();
   1345     sAbortConnlessWait = true;
   1346     nativeLlcpConnectionlessSocket_abortWait();
   1347     sIsNfaEnabled = false;
   1348     sDiscoveryEnabled = false;
   1349     sPollingEnabled = false;
   1350     sIsDisabling = false;
   1351     sP2pEnabled = false;
   1352     gActivated = false;
   1353     sLfT3tMax = 0;
   1354 
   1355     {
   1356         //unblock NFA_EnablePolling() and NFA_DisablePolling()
   1357         SyncEventGuard guard (sNfaEnableDisablePollingEvent);
   1358         sNfaEnableDisablePollingEvent.notifyOne ();
   1359     }
   1360 
   1361     NfcAdaptation& theInstance = NfcAdaptation::GetInstance();
   1362     theInstance.Finalize();
   1363 
   1364     ALOGV("%s: exit", __func__);
   1365     return JNI_TRUE;
   1366 }
   1367 
   1368 
   1369 /*******************************************************************************
   1370 **
   1371 ** Function:        nfcManager_doCreateLlcpSocket
   1372 **
   1373 ** Description:     Create a LLCP connection-oriented socket.
   1374 **                  e: JVM environment.
   1375 **                  o: Java object.
   1376 **                  nSap: Service access point.
   1377 **                  miu: Maximum information unit.
   1378 **                  rw: Receive window size.
   1379 **                  linearBufferLength: Max buffer size.
   1380 **
   1381 ** Returns:         NativeLlcpSocket Java object.
   1382 **
   1383 *******************************************************************************/
   1384 static jobject nfcManager_doCreateLlcpSocket (JNIEnv* e, jobject, jint nSap, jint miu, jint rw, jint linearBufferLength)
   1385 {
   1386     ALOGV("%s: enter; sap=%d; miu=%d; rw=%d; buffer len=%d", __func__, nSap, miu, rw, linearBufferLength);
   1387 
   1388     PeerToPeer::tJNI_HANDLE jniHandle = PeerToPeer::getInstance().getNewJniHandle ();
   1389     PeerToPeer::getInstance().createClient (jniHandle, miu, rw);
   1390 
   1391     /* Create new NativeLlcpSocket object */
   1392     jobject clientSocket = NULL;
   1393     if (nfc_jni_cache_object_local(e, gNativeLlcpSocketClassName, &(clientSocket)) == -1)
   1394     {
   1395         ALOGE("%s: fail Llcp socket creation", __func__);
   1396         return clientSocket;
   1397     }
   1398 
   1399     /* Get NativeConnectionless class object */
   1400     ScopedLocalRef<jclass> clsNativeLlcpSocket(e, e->GetObjectClass(clientSocket));
   1401     if (e->ExceptionCheck())
   1402     {
   1403         e->ExceptionClear();
   1404         ALOGE("%s: fail get class object", __func__);
   1405         return clientSocket;
   1406     }
   1407 
   1408     jfieldID f;
   1409 
   1410     /* Set socket SAP */
   1411     f = e->GetFieldID (clsNativeLlcpSocket.get(), "mSap", "I");
   1412     e->SetIntField (clientSocket, f, (jint) nSap);
   1413 
   1414     /* Set socket handle */
   1415     f = e->GetFieldID (clsNativeLlcpSocket.get(), "mHandle", "I");
   1416     e->SetIntField (clientSocket, f, (jint) jniHandle);
   1417 
   1418     /* Set socket MIU */
   1419     f = e->GetFieldID (clsNativeLlcpSocket.get(), "mLocalMiu", "I");
   1420     e->SetIntField (clientSocket, f, (jint) miu);
   1421 
   1422     /* Set socket RW */
   1423     f = e->GetFieldID (clsNativeLlcpSocket.get(), "mLocalRw", "I");
   1424     e->SetIntField (clientSocket, f, (jint) rw);
   1425 
   1426     ALOGV("%s: exit", __func__);
   1427     return clientSocket;
   1428 }
   1429 
   1430 
   1431 /*******************************************************************************
   1432 **
   1433 ** Function:        nfcManager_doCreateLlcpConnectionlessSocket
   1434 **
   1435 ** Description:     Create a connection-less socket.
   1436 **                  e: JVM environment.
   1437 **                  o: Java object.
   1438 **                  nSap: Service access point.
   1439 **                  sn: Service name.
   1440 **
   1441 ** Returns:         NativeLlcpConnectionlessSocket Java object.
   1442 **
   1443 *******************************************************************************/
   1444 static jobject nfcManager_doCreateLlcpConnectionlessSocket (JNIEnv *, jobject, jint nSap, jstring /*sn*/)
   1445 {
   1446     ALOGV("%s: nSap=0x%X", __func__, nSap);
   1447     return NULL;
   1448 }
   1449 
   1450 /*******************************************************************************
   1451 **
   1452 ** Function:        isPeerToPeer
   1453 **
   1454 ** Description:     Whether the activation data indicates the peer supports NFC-DEP.
   1455 **                  activated: Activation data.
   1456 **
   1457 ** Returns:         True if the peer supports NFC-DEP.
   1458 **
   1459 *******************************************************************************/
   1460 static bool isPeerToPeer (tNFA_ACTIVATED& activated)
   1461 {
   1462     return activated.activate_ntf.protocol == NFA_PROTOCOL_NFC_DEP;
   1463 }
   1464 
   1465 /*******************************************************************************
   1466 **
   1467 ** Function:        isListenMode
   1468 **
   1469 ** Description:     Indicates whether the activation data indicates it is
   1470 **                  listen mode.
   1471 **
   1472 ** Returns:         True if this listen mode.
   1473 **
   1474 *******************************************************************************/
   1475 static bool isListenMode(tNFA_ACTIVATED& activated)
   1476 {
   1477     return ((NFC_DISCOVERY_TYPE_LISTEN_A == activated.activate_ntf.rf_tech_param.mode)
   1478             || (NFC_DISCOVERY_TYPE_LISTEN_B == activated.activate_ntf.rf_tech_param.mode)
   1479             || (NFC_DISCOVERY_TYPE_LISTEN_F == activated.activate_ntf.rf_tech_param.mode)
   1480             || (NFC_DISCOVERY_TYPE_LISTEN_A_ACTIVE == activated.activate_ntf.rf_tech_param.mode)
   1481             || (NFC_DISCOVERY_TYPE_LISTEN_F_ACTIVE == activated.activate_ntf.rf_tech_param.mode)
   1482             || (NFC_DISCOVERY_TYPE_LISTEN_ISO15693 == activated.activate_ntf.rf_tech_param.mode)
   1483             || (NFC_DISCOVERY_TYPE_LISTEN_B_PRIME == activated.activate_ntf.rf_tech_param.mode));
   1484 }
   1485 
   1486 /*******************************************************************************
   1487 **
   1488 ** Function:        nfcManager_doCheckLlcp
   1489 **
   1490 ** Description:     Not used.
   1491 **
   1492 ** Returns:         True
   1493 **
   1494 *******************************************************************************/
   1495 static jboolean nfcManager_doCheckLlcp(JNIEnv*, jobject)
   1496 {
   1497     ALOGV("%s", __func__);
   1498     return JNI_TRUE;
   1499 }
   1500 
   1501 
   1502 /*******************************************************************************
   1503 **
   1504 ** Function:        nfcManager_doActivateLlcp
   1505 **
   1506 ** Description:     Not used.
   1507 **
   1508 ** Returns:         True
   1509 **
   1510 *******************************************************************************/
   1511 static jboolean nfcManager_doActivateLlcp(JNIEnv*, jobject)
   1512 {
   1513     ALOGV("%s", __func__);
   1514     return JNI_TRUE;
   1515 }
   1516 
   1517 
   1518 /*******************************************************************************
   1519 **
   1520 ** Function:        nfcManager_doAbort
   1521 **
   1522 ** Description:     Not used.
   1523 **
   1524 ** Returns:         None
   1525 **
   1526 *******************************************************************************/
   1527 static void nfcManager_doAbort(JNIEnv* e, jobject, jstring msg)
   1528 {
   1529     ScopedUtfChars message = {e, msg};
   1530     e->FatalError(message.c_str());
   1531     abort(); // <-- Unreachable
   1532 }
   1533 
   1534 
   1535 /*******************************************************************************
   1536 **
   1537 ** Function:        nfcManager_doDownload
   1538 **
   1539 ** Description:     Download firmware patch files.  Do not turn on NFC.
   1540 **
   1541 ** Returns:         True if ok.
   1542 **
   1543 *******************************************************************************/
   1544 static jboolean nfcManager_doDownload(JNIEnv*, jobject)
   1545 {
   1546     ALOGV("%s: enter", __func__);
   1547     NfcAdaptation& theInstance = NfcAdaptation::GetInstance();
   1548 
   1549     theInstance.Initialize(); //start GKI, NCI task, NFC task
   1550     theInstance.DownloadFirmware ();
   1551     theInstance.Finalize();
   1552     ALOGV("%s: exit", __func__);
   1553     return JNI_TRUE;
   1554 }
   1555 
   1556 
   1557 /*******************************************************************************
   1558 **
   1559 ** Function:        nfcManager_doResetTimeouts
   1560 **
   1561 ** Description:     Not used.
   1562 **
   1563 ** Returns:         None
   1564 **
   1565 *******************************************************************************/
   1566 static void nfcManager_doResetTimeouts(JNIEnv*, jobject)
   1567 {
   1568     ALOGV("%s", __func__);
   1569     NfcTag::getInstance().resetAllTransceiveTimeouts ();
   1570 }
   1571 
   1572 
   1573 /*******************************************************************************
   1574 **
   1575 ** Function:        nfcManager_doSetTimeout
   1576 **
   1577 ** Description:     Set timeout value.
   1578 **                  e: JVM environment.
   1579 **                  o: Java object.
   1580 **                  tech: technology ID.
   1581 **                  timeout: Timeout value.
   1582 **
   1583 ** Returns:         True if ok.
   1584 **
   1585 *******************************************************************************/
   1586 static bool nfcManager_doSetTimeout(JNIEnv*, jobject, jint tech, jint timeout)
   1587 {
   1588     if (timeout <= 0)
   1589     {
   1590         ALOGE("%s: Timeout must be positive.",__func__);
   1591         return false;
   1592     }
   1593     ALOGV("%s: tech=%d, timeout=%d", __func__, tech, timeout);
   1594     NfcTag::getInstance().setTransceiveTimeout (tech, timeout);
   1595     return true;
   1596 }
   1597 
   1598 
   1599 /*******************************************************************************
   1600 **
   1601 ** Function:        nfcManager_doGetTimeout
   1602 **
   1603 ** Description:     Get timeout value.
   1604 **                  e: JVM environment.
   1605 **                  o: Java object.
   1606 **                  tech: technology ID.
   1607 **
   1608 ** Returns:         Timeout value.
   1609 **
   1610 *******************************************************************************/
   1611 static jint nfcManager_doGetTimeout(JNIEnv*, jobject, jint tech)
   1612 {
   1613     int timeout = NfcTag::getInstance().getTransceiveTimeout (tech);
   1614     ALOGV("%s: tech=%d, timeout=%d", __func__, tech, timeout);
   1615     return timeout;
   1616 }
   1617 
   1618 
   1619 /*******************************************************************************
   1620 **
   1621 ** Function:        nfcManager_doDump
   1622 **
   1623 ** Description:     Not used.
   1624 **                  e: JVM environment.
   1625 **                  o: Java object.
   1626 **
   1627 ** Returns:         Text dump.
   1628 **
   1629 *******************************************************************************/
   1630 static jstring nfcManager_doDump(JNIEnv* e, jobject)
   1631 {
   1632     char buffer[100];
   1633     snprintf(buffer, sizeof(buffer), "libnfc llc error_count=%u", /*libnfc_llc_error_count*/ 0);
   1634     return e->NewStringUTF(buffer);
   1635 }
   1636 
   1637 
   1638 /*******************************************************************************
   1639 **
   1640 ** Function:        nfcManager_doSetP2pInitiatorModes
   1641 **
   1642 ** Description:     Set P2P initiator's activation modes.
   1643 **                  e: JVM environment.
   1644 **                  o: Java object.
   1645 **                  modes: Active and/or passive modes.  The values are specified
   1646 **                          in external/libnfc-nxp/inc/phNfcTypes.h.  See
   1647 **                          enum phNfc_eP2PMode_t.
   1648 **
   1649 ** Returns:         None.
   1650 **
   1651 *******************************************************************************/
   1652 static void nfcManager_doSetP2pInitiatorModes (JNIEnv *e, jobject o, jint modes)
   1653 {
   1654     ALOGV("%s: modes=0x%X", __func__, modes);
   1655     struct nfc_jni_native_data *nat = getNative(e, o);
   1656 
   1657     tNFA_TECHNOLOGY_MASK mask = 0;
   1658     if (modes & 0x01) mask |= NFA_TECHNOLOGY_MASK_A;
   1659     if (modes & 0x02) mask |= NFA_TECHNOLOGY_MASK_F;
   1660     if (modes & 0x04) mask |= NFA_TECHNOLOGY_MASK_F;
   1661     if (modes & 0x08) mask |= NFA_TECHNOLOGY_MASK_A_ACTIVE;
   1662     if (modes & 0x10) mask |= NFA_TECHNOLOGY_MASK_F_ACTIVE;
   1663     if (modes & 0x20) mask |= NFA_TECHNOLOGY_MASK_F_ACTIVE;
   1664     nat->tech_mask = mask;
   1665 }
   1666 
   1667 
   1668 /*******************************************************************************
   1669 **
   1670 ** Function:        nfcManager_doSetP2pTargetModes
   1671 **
   1672 ** Description:     Set P2P target's activation modes.
   1673 **                  e: JVM environment.
   1674 **                  o: Java object.
   1675 **                  modes: Active and/or passive modes.
   1676 **
   1677 ** Returns:         None.
   1678 **
   1679 *******************************************************************************/
   1680 static void nfcManager_doSetP2pTargetModes (JNIEnv*, jobject, jint modes)
   1681 {
   1682     ALOGV("%s: modes=0x%X", __func__, modes);
   1683     // Map in the right modes
   1684     tNFA_TECHNOLOGY_MASK mask = 0;
   1685     if (modes & 0x01) mask |= NFA_TECHNOLOGY_MASK_A;
   1686     if (modes & 0x02) mask |= NFA_TECHNOLOGY_MASK_F;
   1687     if (modes & 0x04) mask |= NFA_TECHNOLOGY_MASK_F;
   1688     if (modes & 0x08) mask |= NFA_TECHNOLOGY_MASK_A_ACTIVE | NFA_TECHNOLOGY_MASK_F_ACTIVE;
   1689 
   1690     PeerToPeer::getInstance().setP2pListenMask(mask);
   1691 }
   1692 
   1693 static void nfcManager_doEnableScreenOffSuspend(JNIEnv* e, jobject o)
   1694 {
   1695     PowerSwitch::getInstance().setScreenOffPowerState(PowerSwitch::POWER_STATE_FULL);
   1696 }
   1697 
   1698 static void nfcManager_doDisableScreenOffSuspend(JNIEnv* e, jobject o)
   1699 {
   1700     PowerSwitch::getInstance().setScreenOffPowerState(PowerSwitch::POWER_STATE_OFF);
   1701 }
   1702 
   1703 /*****************************************************************************
   1704 **
   1705 ** JNI functions for android-4.0.1_r1
   1706 **
   1707 *****************************************************************************/
   1708 static JNINativeMethod gMethods[] =
   1709 {
   1710     {"doDownload", "()Z",
   1711             (void *)nfcManager_doDownload},
   1712 
   1713     {"initializeNativeStructure", "()Z",
   1714             (void*) nfcManager_initNativeStruc},
   1715 
   1716     {"doInitialize", "()Z",
   1717             (void*) nfcManager_doInitialize},
   1718 
   1719     {"doDeinitialize", "()Z",
   1720             (void*) nfcManager_doDeinitialize},
   1721 
   1722     {"sendRawFrame", "([B)Z",
   1723             (void*) nfcManager_sendRawFrame},
   1724 
   1725     {"routeAid", "([BI)Z",
   1726             (void*) nfcManager_routeAid},
   1727 
   1728     {"unrouteAid", "([B)Z",
   1729             (void*) nfcManager_unrouteAid},
   1730 
   1731     {"commitRouting", "()Z",
   1732             (void*) nfcManager_commitRouting},
   1733 
   1734     {"doRegisterT3tIdentifier", "([B)I",
   1735             (void*) nfcManager_doRegisterT3tIdentifier},
   1736 
   1737     {"doDeregisterT3tIdentifier", "(I)V",
   1738             (void*) nfcManager_doDeregisterT3tIdentifier},
   1739 
   1740     {"getLfT3tMax", "()I",
   1741             (void*) nfcManager_getLfT3tMax},
   1742 
   1743     {"doEnableDiscovery", "(IZZZZZ)V",
   1744             (void*) nfcManager_enableDiscovery},
   1745 
   1746     {"doCheckLlcp", "()Z",
   1747             (void *)nfcManager_doCheckLlcp},
   1748 
   1749     {"doActivateLlcp", "()Z",
   1750             (void *)nfcManager_doActivateLlcp},
   1751 
   1752     {"doCreateLlcpConnectionlessSocket", "(ILjava/lang/String;)Lcom/android/nfc/dhimpl/NativeLlcpConnectionlessSocket;",
   1753             (void *)nfcManager_doCreateLlcpConnectionlessSocket},
   1754 
   1755     {"doCreateLlcpServiceSocket", "(ILjava/lang/String;III)Lcom/android/nfc/dhimpl/NativeLlcpServiceSocket;",
   1756             (void*) nfcManager_doCreateLlcpServiceSocket},
   1757 
   1758     {"doCreateLlcpSocket", "(IIII)Lcom/android/nfc/dhimpl/NativeLlcpSocket;",
   1759             (void*) nfcManager_doCreateLlcpSocket},
   1760 
   1761     {"doGetLastError", "()I",
   1762             (void*) nfcManager_doGetLastError},
   1763 
   1764     {"disableDiscovery", "()V",
   1765             (void*) nfcManager_disableDiscovery},
   1766 
   1767     {"doSetTimeout", "(II)Z",
   1768             (void *)nfcManager_doSetTimeout},
   1769 
   1770     {"doGetTimeout", "(I)I",
   1771             (void *)nfcManager_doGetTimeout},
   1772 
   1773     {"doResetTimeouts", "()V",
   1774             (void *)nfcManager_doResetTimeouts},
   1775 
   1776     {"doAbort", "(Ljava/lang/String;)V",
   1777             (void *)nfcManager_doAbort},
   1778 
   1779     {"doSetP2pInitiatorModes", "(I)V",
   1780             (void *)nfcManager_doSetP2pInitiatorModes},
   1781 
   1782     {"doSetP2pTargetModes", "(I)V",
   1783             (void *)nfcManager_doSetP2pTargetModes},
   1784 
   1785     {"doEnableScreenOffSuspend", "()V",
   1786             (void *)nfcManager_doEnableScreenOffSuspend},
   1787 
   1788     {"doDisableScreenOffSuspend", "()V",
   1789             (void *)nfcManager_doDisableScreenOffSuspend},
   1790 
   1791     {"doDump", "()Ljava/lang/String;",
   1792             (void *)nfcManager_doDump},
   1793 };
   1794 
   1795 
   1796 /*******************************************************************************
   1797 **
   1798 ** Function:        register_com_android_nfc_NativeNfcManager
   1799 **
   1800 ** Description:     Regisgter JNI functions with Java Virtual Machine.
   1801 **                  e: Environment of JVM.
   1802 **
   1803 ** Returns:         Status of registration.
   1804 **
   1805 *******************************************************************************/
   1806 int register_com_android_nfc_NativeNfcManager (JNIEnv *e)
   1807 {
   1808     ALOGV("%s: enter", __func__);
   1809     PowerSwitch::getInstance ().initialize (PowerSwitch::UNKNOWN_LEVEL);
   1810     ALOGV("%s: exit", __func__);
   1811     return jniRegisterNativeMethods (e, gNativeNfcManagerClassName, gMethods, NELEM (gMethods));
   1812 }
   1813 
   1814 
   1815 /*******************************************************************************
   1816 **
   1817 ** Function:        startRfDiscovery
   1818 **
   1819 ** Description:     Ask stack to start polling and listening for devices.
   1820 **                  isStart: Whether to start.
   1821 **
   1822 ** Returns:         None
   1823 **
   1824 *******************************************************************************/
   1825 void startRfDiscovery(bool isStart)
   1826 {
   1827     tNFA_STATUS status = NFA_STATUS_FAILED;
   1828 
   1829     ALOGV("%s: is start=%d", __func__, isStart);
   1830     SyncEventGuard guard (sNfaEnableDisablePollingEvent);
   1831     status  = isStart ? NFA_StartRfDiscovery () : NFA_StopRfDiscovery ();
   1832     if (status == NFA_STATUS_OK)
   1833     {
   1834         sNfaEnableDisablePollingEvent.wait (); //wait for NFA_RF_DISCOVERY_xxxx_EVT
   1835         sRfEnabled = isStart;
   1836     }
   1837     else
   1838     {
   1839         ALOGE("%s: Failed to start/stop RF discovery; error=0x%X", __func__, status);
   1840     }
   1841 }
   1842 
   1843 
   1844 /*******************************************************************************
   1845 **
   1846 ** Function:        isDiscoveryStarted
   1847 **
   1848 ** Description:     Indicates whether the discovery is started.
   1849 **
   1850 ** Returns:         True if discovery is started
   1851 **
   1852 *******************************************************************************/
   1853 bool isDiscoveryStarted ()
   1854 {
   1855     return sRfEnabled;
   1856 }
   1857 
   1858 
   1859 /*******************************************************************************
   1860 **
   1861 ** Function:        doStartupConfig
   1862 **
   1863 ** Description:     Configure the NFC controller.
   1864 **
   1865 ** Returns:         None
   1866 **
   1867 *******************************************************************************/
   1868 void doStartupConfig()
   1869 {
   1870     struct nfc_jni_native_data *nat = getNative(0, 0);
   1871     tNFA_STATUS stat = NFA_STATUS_FAILED;
   1872     int actualLen = 0;
   1873 
   1874     // If polling for Active mode, set the ordering so that we choose Active over Passive mode first.
   1875     if (nat && (nat->tech_mask & (NFA_TECHNOLOGY_MASK_A_ACTIVE | NFA_TECHNOLOGY_MASK_F_ACTIVE)))
   1876     {
   1877         uint8_t  act_mode_order_param[] = { 0x01 };
   1878         SyncEventGuard guard (sNfaSetConfigEvent);
   1879         stat = NFA_SetConfig(NCI_PARAM_ID_ACT_ORDER, sizeof(act_mode_order_param), &act_mode_order_param[0]);
   1880         if (stat == NFA_STATUS_OK)
   1881             sNfaSetConfigEvent.wait ();
   1882     }
   1883 
   1884     //configure RF polling frequency for each technology
   1885     static tNFA_DM_DISC_FREQ_CFG nfa_dm_disc_freq_cfg;
   1886     //values in the polling_frequency[] map to members of nfa_dm_disc_freq_cfg
   1887     uint8_t polling_frequency [8] = {1, 1, 1, 1, 1, 1, 1, 1};
   1888     actualLen = GetStrValue(NAME_POLL_FREQUENCY, (char*)polling_frequency, 8);
   1889     if (actualLen == 8)
   1890     {
   1891         ALOGV("%s: polling frequency", __func__);
   1892         memset (&nfa_dm_disc_freq_cfg, 0, sizeof(nfa_dm_disc_freq_cfg));
   1893         nfa_dm_disc_freq_cfg.pa = polling_frequency [0];
   1894         nfa_dm_disc_freq_cfg.pb = polling_frequency [1];
   1895         nfa_dm_disc_freq_cfg.pf = polling_frequency [2];
   1896         nfa_dm_disc_freq_cfg.pi93 = polling_frequency [3];
   1897         nfa_dm_disc_freq_cfg.pbp = polling_frequency [4];
   1898         nfa_dm_disc_freq_cfg.pk = polling_frequency [5];
   1899         nfa_dm_disc_freq_cfg.paa = polling_frequency [6];
   1900         nfa_dm_disc_freq_cfg.pfa = polling_frequency [7];
   1901         p_nfa_dm_rf_disc_freq_cfg = &nfa_dm_disc_freq_cfg;
   1902     }
   1903 }
   1904 
   1905 
   1906 /*******************************************************************************
   1907 **
   1908 ** Function:        nfcManager_isNfcActive
   1909 **
   1910 ** Description:     Used externaly to determine if NFC is active or not.
   1911 **
   1912 ** Returns:         'true' if the NFC stack is running, else 'false'.
   1913 **
   1914 *******************************************************************************/
   1915 bool nfcManager_isNfcActive()
   1916 {
   1917     return sIsNfaEnabled;
   1918 }
   1919 
   1920 /*******************************************************************************
   1921 **
   1922 ** Function:        startStopPolling
   1923 **
   1924 ** Description:     Start or stop polling.
   1925 **                  isStartPolling: true to start polling; false to stop polling.
   1926 **
   1927 ** Returns:         None.
   1928 **
   1929 *******************************************************************************/
   1930 void startStopPolling (bool isStartPolling)
   1931 {
   1932     ALOGV("%s: enter; isStart=%u", __func__, isStartPolling);
   1933     startRfDiscovery (false);
   1934 
   1935     if (isStartPolling) startPolling_rfDiscoveryDisabled(0);
   1936     else stopPolling_rfDiscoveryDisabled();
   1937 
   1938     startRfDiscovery (true);
   1939     ALOGV("%s: exit", __func__);
   1940 }
   1941 
   1942 
   1943 static tNFA_STATUS startPolling_rfDiscoveryDisabled(tNFA_TECHNOLOGY_MASK tech_mask) {
   1944     tNFA_STATUS stat = NFA_STATUS_FAILED;
   1945 
   1946     unsigned long num = 0;
   1947 
   1948     if (tech_mask == 0 && GetNumValue(NAME_POLLING_TECH_MASK, &num, sizeof(num)))
   1949         tech_mask = num;
   1950     else if (tech_mask == 0) tech_mask = DEFAULT_TECH_MASK;
   1951 
   1952     SyncEventGuard guard (sNfaEnableDisablePollingEvent);
   1953     ALOGV("%s: enable polling", __func__);
   1954     stat = NFA_EnablePolling (tech_mask);
   1955     if (stat == NFA_STATUS_OK)
   1956     {
   1957         ALOGV("%s: wait for enable event", __func__);
   1958         sPollingEnabled = true;
   1959         sNfaEnableDisablePollingEvent.wait (); //wait for NFA_POLL_ENABLED_EVT
   1960     }
   1961     else
   1962     {
   1963         ALOGE("%s: fail enable polling; error=0x%X", __func__, stat);
   1964     }
   1965 
   1966     return stat;
   1967 }
   1968 
   1969 static tNFA_STATUS stopPolling_rfDiscoveryDisabled() {
   1970     tNFA_STATUS stat = NFA_STATUS_FAILED;
   1971 
   1972     SyncEventGuard guard (sNfaEnableDisablePollingEvent);
   1973     ALOGV("%s: disable polling", __func__);
   1974     stat = NFA_DisablePolling ();
   1975     if (stat == NFA_STATUS_OK) {
   1976         sPollingEnabled = false;
   1977         sNfaEnableDisablePollingEvent.wait (); //wait for NFA_POLL_DISABLED_EVT
   1978     } else {
   1979         ALOGE("%s: fail disable polling; error=0x%X", __func__, stat);
   1980     }
   1981 
   1982     return stat;
   1983 }
   1984 
   1985 
   1986 } /* namespace android */
   1987