Home | History | Annotate | Download | only in dm
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2010-2014 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 
     20 /******************************************************************************
     21  *
     22  *  This file contains the action functions for device manager state
     23  *  machine.
     24  *
     25  ******************************************************************************/
     26 #include <string.h>
     27 #include "nfa_sys.h"
     28 #include "nfa_api.h"
     29 #include "nfa_dm_int.h"
     30 #include "nfa_sys_int.h"
     31 #include "nfa_ce_int.h"
     32 #include "nfc_api.h"
     33 #include "nfa_rw_int.h"
     34 #include "nfa_rw_api.h"
     35 #include "nfa_p2p_int.h"
     36 #include "nci_hmsgs.h"
     37 
     38 #if (defined (NFA_CHO_INCLUDED) && (NFA_CHO_INCLUDED==TRUE))
     39 #include "nfa_cho_int.h"
     40 #endif
     41 
     42 #if (NFC_NFCEE_INCLUDED == TRUE)
     43 #include "nfa_ee_int.h"
     44 #include "nfa_hci_int.h"
     45 #endif
     46 
     47 #if (defined (NFA_SNEP_INCLUDED) && (NFA_SNEP_INCLUDED==TRUE))
     48 #include "nfa_snep_int.h"
     49 #endif
     50 
     51 /* This is the timeout value to guarantee disable is performed within reasonable amount of time */
     52 #ifndef NFA_DM_DISABLE_TIMEOUT_VAL
     53 #define NFA_DM_DISABLE_TIMEOUT_VAL         1000
     54 #endif
     55 
     56 static void nfa_dm_set_init_nci_params (void);
     57 static tNFA_STATUS nfa_dm_start_polling (void);
     58 static BOOLEAN nfa_dm_deactivate_polling (void);
     59 static void nfa_dm_excl_disc_cback (tNFA_DM_RF_DISC_EVT event, tNFC_DISCOVER *p_data);
     60 static void nfa_dm_poll_disc_cback (tNFA_DM_RF_DISC_EVT event, tNFC_DISCOVER *p_data);
     61 
     62 
     63 /*******************************************************************************
     64 **
     65 ** Function         nfa_dm_module_init_cback
     66 **
     67 ** Description      Processing initialization complete event from sub-modules
     68 **
     69 ** Returns          None
     70 **
     71 *******************************************************************************/
     72 static void nfa_dm_module_init_cback (void)
     73 {
     74     tNFA_DM_CBACK_DATA dm_cback_data;
     75 
     76     nfa_dm_cb.flags &= ~NFA_DM_FLAGS_ENABLE_EVT_PEND;
     77 
     78     /* All subsystem are initialized */
     79     dm_cback_data.status = NFA_STATUS_OK;
     80     (*nfa_dm_cb.p_dm_cback) (NFA_DM_ENABLE_EVT, &dm_cback_data);
     81 }
     82 
     83 /*******************************************************************************
     84 **
     85 ** Function         nfa_dm_nfcc_power_mode_proc_complete_cback
     86 **
     87 ** Description      Processing complete of processing NFCC power state change
     88 **                  from all sub-modules
     89 **
     90 ** Returns          None
     91 **
     92 *******************************************************************************/
     93 static void nfa_dm_nfcc_power_mode_proc_complete_cback (void)
     94 {
     95     tNFA_DM_PWR_MODE_CHANGE power_mode_change;
     96 
     97     NFA_TRACE_DEBUG1 ("nfa_dm_nfcc_power_mode_proc_complete_cback () nfcc_pwr_mode = 0x%x",
     98                       nfa_dm_cb.nfcc_pwr_mode);
     99 
    100     /* if NFCC power state is change to full power */
    101     if (nfa_dm_cb.nfcc_pwr_mode != NFA_DM_PWR_MODE_OFF_SLEEP)
    102     {
    103         nfa_dm_cb.flags &= ~NFA_DM_FLAGS_NFCC_IS_RESTORING;
    104 
    105         /* reconfigure BRCM NFCC */
    106         nfa_dm_disc_sm_execute (NFA_DM_RF_DISCOVER_CMD, NULL);
    107     }
    108 
    109     nfa_dm_cb.flags &= ~NFA_DM_FLAGS_SETTING_PWR_MODE;
    110 
    111     power_mode_change.status     = NFA_STATUS_OK;
    112     power_mode_change.power_mode = nfa_dm_cb.nfcc_pwr_mode;
    113     (*nfa_dm_cb.p_dm_cback) (NFA_DM_PWR_MODE_CHANGE_EVT, (tNFA_DM_CBACK_DATA*) &power_mode_change);
    114 }
    115 /*******************************************************************************
    116 **
    117 ** Function         nfa_dm_sys_enable
    118 **
    119 ** Description      This function on enable
    120 **
    121 ** Returns          void
    122 **
    123 *******************************************************************************/
    124 void nfa_dm_sys_enable (void)
    125 {
    126     nfa_dm_set_init_nci_params ();
    127 }
    128 
    129 /*******************************************************************************
    130 **
    131 ** Function         nfa_dm_set_init_nci_params
    132 **
    133 ** Description      Set initial NCI configuration parameters
    134 **
    135 ** Returns          void
    136 **
    137 *******************************************************************************/
    138 static void nfa_dm_set_init_nci_params (void)
    139 {
    140     UINT8   xx;
    141 
    142     /* set NCI default value if other than zero */
    143 
    144     /* LF_T3T_IDENTIFIERS_1/2/.../16 */
    145     for (xx = 0; xx < NFA_CE_LISTEN_INFO_MAX; xx++)
    146     {
    147         nfa_dm_cb.params.lf_t3t_id[xx][0] = 0xFF;
    148         nfa_dm_cb.params.lf_t3t_id[xx][1] = 0xFF;
    149         nfa_dm_cb.params.lf_t3t_id[xx][2] = 0x02;
    150         nfa_dm_cb.params.lf_t3t_id[xx][2] = 0xFE;
    151     }
    152 
    153     /* LF_T3T_PMM */
    154     for (xx = 0; xx < NCI_PARAM_LEN_LF_T3T_PMM; xx++)
    155     {
    156         nfa_dm_cb.params.lf_t3t_pmm[xx] = 0xFF;
    157     }
    158 
    159     /* LF_T3T_FLAGS:
    160     ** DH needs to set this configuration, even if default value (not listening) is used,
    161     ** to let NFCC know of intention (not listening) of DH.
    162     */
    163 
    164     /* FWI */
    165     nfa_dm_cb.params.fwi[0] = 0x04;
    166 
    167     /* WT */
    168     nfa_dm_cb.params.wt[0] = 14;
    169 
    170     /* Set CE default configuration */
    171     if (p_nfa_dm_ce_cfg[0])
    172     {
    173         nfa_dm_check_set_config (p_nfa_dm_ce_cfg[0], &p_nfa_dm_ce_cfg[1], FALSE);
    174     }
    175 
    176     /* Set optional general default configuration */
    177     if (p_nfa_dm_gen_cfg && p_nfa_dm_gen_cfg[0])
    178     {
    179         nfa_dm_check_set_config (p_nfa_dm_gen_cfg[0], &p_nfa_dm_gen_cfg[1], FALSE);
    180     }
    181 
    182     if (p_nfa_dm_interface_mapping && nfa_dm_num_dm_interface_mapping)
    183     {
    184         NFC_DiscoveryMap (nfa_dm_num_dm_interface_mapping, p_nfa_dm_interface_mapping, NULL);
    185     }
    186 }
    187 
    188 /*******************************************************************************
    189 **
    190 ** Function         nfa_dm_proc_nfcc_power_mode
    191 **
    192 ** Description      Processing NFCC power mode changes
    193 **
    194 ** Returns          None
    195 **
    196 *******************************************************************************/
    197 void nfa_dm_proc_nfcc_power_mode (UINT8 nfcc_power_mode)
    198 {
    199     NFA_TRACE_DEBUG1 ("nfa_dm_proc_nfcc_power_mode (): nfcc_power_mode=%d",
    200                        nfcc_power_mode);
    201 
    202     /* if NFCC power mode is change to full power */
    203     if (nfcc_power_mode == NFA_DM_PWR_MODE_FULL)
    204     {
    205         memset (&nfa_dm_cb.params, 0x00, sizeof (tNFA_DM_PARAMS));
    206         NFA_TRACE_DEBUG2 ("setcfg_pending_mask=0x%x, setcfg_pending_num=%d",
    207             nfa_dm_cb.setcfg_pending_mask, nfa_dm_cb.setcfg_pending_num);
    208         nfa_dm_cb.setcfg_pending_mask = 0;
    209         nfa_dm_cb.setcfg_pending_num  = 0;
    210 
    211         nfa_dm_set_init_nci_params ();
    212         nfa_dm_cb.flags &= ~NFA_DM_FLAGS_POWER_OFF_SLEEP;
    213     }
    214     else if (nfcc_power_mode == NFA_DM_PWR_MODE_OFF_SLEEP)
    215     {
    216         nfa_dm_cb.flags |= NFA_DM_FLAGS_POWER_OFF_SLEEP;
    217     }
    218 
    219     nfa_sys_cback_notify_nfcc_power_mode_proc_complete (NFA_ID_DM);
    220 }
    221 
    222 /*******************************************************************************
    223 **
    224 ** Function         nfa_dm_disable_event
    225 **
    226 ** Description      report disable event
    227 **
    228 ** Returns          void
    229 **
    230 *******************************************************************************/
    231 static void nfa_dm_disable_event (void)
    232 {
    233     /* Deregister DM from sys */
    234     nfa_sys_deregister (NFA_ID_DM);
    235 
    236     /* Notify app */
    237     nfa_dm_cb.flags &= ~(NFA_DM_FLAGS_DM_IS_ACTIVE
    238                         |NFA_DM_FLAGS_DM_DISABLING_NFC
    239                         |NFA_DM_FLAGS_ENABLE_EVT_PEND);
    240     (*nfa_dm_cb.p_dm_cback) (NFA_DM_DISABLE_EVT, NULL);
    241 }
    242 
    243 /*******************************************************************************
    244 **
    245 ** Function         nfa_dm_nfc_response_cback
    246 **
    247 ** Description      Call DM event hanlder with NFC response callback data
    248 **
    249 ** Returns          void
    250 **
    251 *******************************************************************************/
    252 static void nfa_dm_nfc_response_cback (tNFC_RESPONSE_EVT event, tNFC_RESPONSE *p_data)
    253 {
    254     tNFA_DM_CBACK_DATA dm_cback_data;
    255     tNFA_GET_CONFIG   *p_nfa_get_confg;
    256     tNFA_CONN_EVT_DATA conn_evt;
    257     UINT8 dm_cback_evt;
    258     UINT8 max_ee = 0;
    259 
    260 #if (BT_TRACE_VERBOSE == TRUE)
    261     NFA_TRACE_DEBUG2 ("nfa_dm_nfc_response_cback () %s(0x%x)", nfa_dm_nfc_revt_2_str (event), event);
    262 #else
    263     NFA_TRACE_DEBUG1 ("nfa_dm_nfc_response_cback () event=0x%x", event);
    264 #endif
    265 
    266     switch (event)
    267     {
    268     case NFC_ENABLE_REVT:                        /* 0  Enable event */
    269 
    270         /* NFC stack enabled. Enable nfa sub-systems */
    271         if (p_data->enable.status == NFC_STATUS_OK)
    272         {
    273             if (nfa_ee_max_ee_cfg != 0)
    274             {
    275                 if (nfa_dm_cb.get_max_ee)
    276                 {
    277                     max_ee = nfa_dm_cb.get_max_ee ();
    278                     if (max_ee)
    279                     {
    280                         nfa_ee_max_ee_cfg = max_ee;
    281                     }
    282                 }
    283             }
    284             /* Initialize NFA subsystems */
    285             nfa_sys_enable_subsystems ();
    286         }
    287         else if (nfa_dm_cb.flags & NFA_DM_FLAGS_ENABLE_EVT_PEND)
    288         {
    289             /* Notify app */
    290             nfa_dm_cb.flags &= ~(NFA_DM_FLAGS_ENABLE_EVT_PEND | NFA_DM_FLAGS_DM_IS_ACTIVE);
    291             dm_cback_data.status = p_data->enable.status;
    292             (*nfa_dm_cb.p_dm_cback) (NFA_DM_ENABLE_EVT, &dm_cback_data);
    293         }
    294         break;
    295 
    296     case NFC_DISABLE_REVT:                       /* 1  Disable event */
    297         nfa_dm_disable_event ();
    298         break;
    299 
    300     case NFC_SET_CONFIG_REVT:                    /* 2  Set Config Response */
    301         /* If this setconfig was due to NFA_SetConfig, then notify the app */
    302         if (nfa_dm_cb.setcfg_pending_mask & 1)      /* lsb=whether last NCI_SET_CONFIG was due to NFA_SetConfig */
    303         {
    304             dm_cback_data.set_config.status         = p_data->set_config.status;
    305             dm_cback_data.set_config.num_param_id   = p_data->set_config.num_param_id;
    306             memcpy (dm_cback_data.set_config.param_ids, p_data->set_config.param_ids, p_data->set_config.num_param_id);
    307             (*nfa_dm_cb.p_dm_cback) (NFA_DM_SET_CONFIG_EVT, &dm_cback_data);
    308         }
    309 
    310         /* Update the pending mask */
    311         if (nfa_dm_cb.setcfg_pending_num>0)
    312         {
    313             nfa_dm_cb.setcfg_pending_mask >>= 1;
    314             nfa_dm_cb.setcfg_pending_num--;
    315         }
    316         else
    317         {
    318             /* This should not occur (means we got a SET_CONFIG_NTF that's unaccounted for */
    319             NFA_TRACE_ERROR0 ("NFA received unexpected NFC_SET_CONFIG_REVT");
    320         }
    321         break;
    322 
    323     case NFC_GET_CONFIG_REVT:                    /* 3  Get Config Response */
    324         if (p_data->get_config.status == NFC_STATUS_OK)
    325         {
    326             if ((p_nfa_get_confg = (tNFA_GET_CONFIG *) GKI_getbuf ((UINT16) (sizeof (tNFA_GET_CONFIG) + p_data->get_config.tlv_size))) != NULL)
    327             {
    328                 p_nfa_get_confg->status = NFA_STATUS_OK;
    329                 p_nfa_get_confg->tlv_size = p_data->get_config.tlv_size;
    330                 memcpy (p_nfa_get_confg->param_tlvs, p_data->get_config.p_param_tlvs, p_data->get_config.tlv_size);
    331                 (*nfa_dm_cb.p_dm_cback) (NFA_DM_GET_CONFIG_EVT, (tNFA_DM_CBACK_DATA *) p_nfa_get_confg);
    332 
    333                 GKI_freebuf (p_nfa_get_confg);
    334                 return;
    335             }
    336             else
    337             {
    338                 NFA_TRACE_DEBUG0 ("nfa_dm_nfc_response_cback unable to allocate buffer");
    339             }
    340         }
    341 
    342         /* Return result of getconfig to the app */
    343         dm_cback_data.get_config.status = NFA_STATUS_FAILED;
    344         (*nfa_dm_cb.p_dm_cback) (NFA_DM_GET_CONFIG_EVT, &dm_cback_data);
    345         break;
    346 
    347 #if (NFC_NFCEE_INCLUDED == TRUE)
    348     case NFC_NFCEE_DISCOVER_REVT:                /* NFCEE Discover response */
    349     case NFC_NFCEE_INFO_REVT:                    /* NFCEE Discover Notification */
    350     case NFC_EE_ACTION_REVT:                     /* EE Action notification */
    351     case NFC_NFCEE_MODE_SET_REVT:                /* NFCEE Mode Set response */
    352     case NFC_SET_ROUTING_REVT:                   /* Configure Routing response */
    353         nfa_ee_proc_evt (event, p_data);
    354         break;
    355 
    356     case NFC_EE_DISCOVER_REQ_REVT:               /* EE Discover Req notification */
    357         if (nfa_dm_is_active() &&
    358             (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_DISCOVERY) )
    359         {
    360             nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_IDLE);
    361         }
    362         nfa_ee_proc_evt (event, p_data);
    363         break;
    364 
    365     case NFC_GET_ROUTING_REVT:                   /* Retrieve Routing response */
    366         break;
    367 #endif
    368 
    369     case NFC_RF_FIELD_REVT:                      /* RF Field information            */
    370         dm_cback_data.rf_field.status          = NFA_STATUS_OK;
    371         dm_cback_data.rf_field.rf_field_status = p_data->rf_field.rf_field;
    372         (*nfa_dm_cb.p_dm_cback) (NFA_DM_RF_FIELD_EVT, &dm_cback_data);
    373         break;
    374 
    375 
    376 
    377     case NFC_GEN_ERROR_REVT:                     /* generic error command or notification */
    378         break;
    379 
    380     case NFC_NFCC_RESTART_REVT:                  /* NFCC has been re-initialized */
    381 
    382         if (p_data->status == NFC_STATUS_OK)
    383         {
    384             nfa_dm_cb.nfcc_pwr_mode = NFA_DM_PWR_MODE_FULL;
    385             nfa_dm_cb.flags |= NFA_DM_FLAGS_NFCC_IS_RESTORING;
    386 
    387             /* NFCC will start from IDLE when turned on again */
    388             nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_RSP;
    389             nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_NTF;
    390             nfa_dm_cb.disc_cb.disc_state = NFA_DM_RFST_IDLE;
    391         }
    392         else
    393         {
    394             nfa_dm_cb.nfcc_pwr_mode = NFA_DM_PWR_MODE_OFF_SLEEP;
    395         }
    396         /* Notify NFA submodules change of NFCC power mode */
    397         nfa_sys_cback_reg_nfcc_power_mode_proc_complete (nfa_dm_nfcc_power_mode_proc_complete_cback);
    398         nfa_sys_notify_nfcc_power_mode (nfa_dm_cb.nfcc_pwr_mode);
    399         break;
    400 
    401     case NFC_NFCC_TIMEOUT_REVT:
    402     case NFC_NFCC_TRANSPORT_ERR_REVT:
    403         NFA_TRACE_DEBUG1 ("flags:0x%08x", nfa_dm_cb.flags);
    404         dm_cback_evt = (event == NFC_NFCC_TIMEOUT_REVT) ? NFA_DM_NFCC_TIMEOUT_EVT : NFA_DM_NFCC_TRANSPORT_ERR_EVT;
    405         (*nfa_dm_cb.p_dm_cback) (dm_cback_evt, NULL);
    406         break;
    407 
    408     case NFC_NFCC_POWER_OFF_REVT:
    409         nfa_dm_cb.nfcc_pwr_mode = NFA_DM_PWR_MODE_OFF_SLEEP;
    410 
    411         /* Notify NFA submodules change of NFCC power mode */
    412         nfa_sys_cback_reg_nfcc_power_mode_proc_complete (nfa_dm_nfcc_power_mode_proc_complete_cback);
    413         nfa_sys_notify_nfcc_power_mode (NFA_DM_PWR_MODE_OFF_SLEEP);
    414         break;
    415 
    416     case NFC_RF_COMM_PARAMS_UPDATE_REVT:
    417         conn_evt.status = p_data->status;
    418         nfa_dm_conn_cback_event_notify (NFA_UPDATE_RF_PARAM_RESULT_EVT, &conn_evt);
    419         break;
    420 
    421 
    422     default:
    423         break;
    424     }
    425 }
    426 
    427 
    428 /*******************************************************************************
    429 **
    430 ** Function         nfa_dm_enable
    431 **
    432 ** Description      Initialises the NFC device manager
    433 **
    434 ** Returns          TRUE (message buffer to be freed by caller)
    435 **
    436 *******************************************************************************/
    437 BOOLEAN nfa_dm_enable (tNFA_DM_MSG *p_data)
    438 {
    439     tNFA_DM_CBACK_DATA dm_cback_data;
    440     NFA_TRACE_DEBUG0 ("nfa_dm_enable ()");
    441 
    442     /* Check if NFA is already enabled */
    443     if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_DM_IS_ACTIVE))
    444     {
    445         /* Initialize BRCM control block, it musb be called before setting any flags */
    446         nfa_dm_cb.flags |= (NFA_DM_FLAGS_DM_IS_ACTIVE | NFA_DM_FLAGS_ENABLE_EVT_PEND);
    447         nfa_sys_cback_reg_enable_complete (nfa_dm_module_init_cback);
    448 
    449 
    450         /* Store Enable parameters */
    451         nfa_dm_cb.p_dm_cback    = p_data->enable.p_dm_cback;
    452         nfa_dm_cb.p_conn_cback  = p_data->enable.p_conn_cback;
    453 
    454         /* Enable NFC stack */
    455         NFC_Enable (nfa_dm_nfc_response_cback);
    456     }
    457     else
    458     {
    459         NFA_TRACE_ERROR0 ("nfa_dm_enable: ERROR ALREADY ENABLED.");
    460         dm_cback_data.status = NFA_STATUS_ALREADY_STARTED;
    461         (*(p_data->enable.p_dm_cback)) (NFA_DM_ENABLE_EVT, &dm_cback_data);
    462     }
    463 
    464     return (TRUE);
    465 }
    466 
    467 /*******************************************************************************
    468 **
    469 ** Function         nfa_dm_disable
    470 **
    471 ** Description      Disables the NFC device manager
    472 **
    473 ** Returns          TRUE (message buffer to be freed by caller)
    474 **
    475 *******************************************************************************/
    476 BOOLEAN nfa_dm_disable (tNFA_DM_MSG *p_data)
    477 {
    478     tNFC_DEACT_TYPE deactivate_type = NFA_DEACTIVATE_TYPE_IDLE;
    479 
    480     NFA_TRACE_DEBUG1 ("nfa_dm_disable (): graceful:%d", p_data->disable.graceful);
    481 
    482     if (p_data->disable.graceful)
    483     {
    484         /* if RF discovery is enabled */
    485         if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_ENABLED)
    486         {
    487             nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_ENABLED;
    488 
    489             if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_IDLE)
    490             {
    491                 /* if waiting RSP in idle state */
    492                 if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP)
    493                 {
    494                     nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_DISABLING;
    495                 }
    496             }
    497             else
    498             {
    499                 nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_DISABLING;
    500                 nfa_dm_disc_sm_execute (NFA_DM_RF_DEACTIVATE_CMD, (tNFA_DM_RF_DISC_DATA *) &deactivate_type);
    501                 if ((nfa_dm_cb.disc_cb.disc_flags & (NFA_DM_DISC_FLAGS_W4_RSP|NFA_DM_DISC_FLAGS_W4_NTF)) == 0)
    502                 {
    503                     /* not waiting to deactivate, clear the flag now */
    504                     nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_DISABLING;
    505                 }
    506             }
    507         }
    508         /* Start timeout for graceful shutdown. If timer expires, then force an ungraceful shutdown */
    509         nfa_sys_start_timer (&nfa_dm_cb.tle, NFA_DM_TIMEOUT_DISABLE_EVT, NFA_DM_DISABLE_TIMEOUT_VAL);
    510     }
    511 
    512     /* Disable all subsystems other than DM (DM will be disabled after all  */
    513     /* the other subsystem have been disabled)                              */
    514     nfa_sys_disable_subsystems (p_data->disable.graceful);
    515     return (TRUE);
    516 }
    517 
    518 
    519 /*******************************************************************************
    520 **
    521 ** Function         nfa_dm_disable_complete
    522 **
    523 ** Description      Called when all NFA subsytems are disabled.
    524 **
    525 **                  NFC core stack can now be disabled.
    526 **
    527 ** Returns          void
    528 **
    529 *******************************************************************************/
    530 void nfa_dm_disable_complete (void)
    531 {
    532     NFA_TRACE_DEBUG0 ("nfa_dm_disable_complete ()");
    533 
    534     if ((nfa_dm_cb.flags & NFA_DM_FLAGS_DM_DISABLING_NFC) == 0)
    535     {
    536         NFA_TRACE_DEBUG0 ("nfa_dm_disable_complete (): proceeding with nfc core shutdown.");
    537 
    538         nfa_dm_cb.flags |= NFA_DM_FLAGS_DM_DISABLING_NFC;
    539 
    540         nfa_sys_stop_timer (&nfa_dm_cb.tle);
    541 
    542         /* Free all buffers for NDEF handlers */
    543         nfa_dm_ndef_dereg_all();
    544 
    545         /* Disable nfc core stack */
    546         NFC_Disable ();
    547     }
    548 }
    549 
    550 /*******************************************************************************
    551 **
    552 ** Function         nfa_dm_set_config
    553 **
    554 ** Description      Process set config command
    555 **
    556 ** Returns          TRUE (message buffer to be freed by caller)
    557 **
    558 *******************************************************************************/
    559 BOOLEAN nfa_dm_set_config (tNFA_DM_MSG *p_data)
    560 {
    561     tNFC_STATUS status;
    562     UINT8       buff[255];
    563     UINT8      *p = buff;
    564 
    565     tNFA_DM_CBACK_DATA dm_cback_data;
    566 
    567     if (p_data->setconfig.length + 2 > 255)
    568     {
    569         /* Total length of TLV must be less than 256 (1 byte) */
    570         status = NFC_STATUS_FAILED;
    571     }
    572     else
    573     {
    574         UINT8_TO_STREAM (p, p_data->setconfig.param_id);
    575         UINT8_TO_STREAM (p, p_data->setconfig.length);
    576         ARRAY_TO_STREAM (p, p_data->setconfig.p_data, p_data->setconfig.length)
    577         status = nfa_dm_check_set_config ((UINT8) (p_data->setconfig.length + 2), buff, TRUE);
    578     }
    579 
    580     if (status != NFC_STATUS_OK)
    581     {
    582         dm_cback_data.set_config.status = NFA_STATUS_INVALID_PARAM;
    583         (*nfa_dm_cb.p_dm_cback) (NFA_DM_SET_CONFIG_EVT, &dm_cback_data);
    584     }
    585 
    586     return (TRUE);
    587 }
    588 
    589 /*******************************************************************************
    590 **
    591 ** Function         nfa_dm_get_config
    592 **
    593 ** Description      Process get config command
    594 **
    595 ** Returns          TRUE (message buffer to be freed by caller)
    596 **
    597 *******************************************************************************/
    598 BOOLEAN nfa_dm_get_config (tNFA_DM_MSG *p_data)
    599 {
    600     NFC_GetConfig (p_data->getconfig.num_ids, p_data->getconfig.p_pmids);
    601 
    602     return (TRUE);
    603 }
    604 
    605 /*******************************************************************************
    606 **
    607 ** Function         nfa_dm_conn_cback_event_notify
    608 **
    609 ** Description      Notify application of CONN_CBACK event, using appropriate
    610 **                  callback
    611 **
    612 ** Returns          nothing
    613 **
    614 *******************************************************************************/
    615 void nfa_dm_conn_cback_event_notify (UINT8 event, tNFA_CONN_EVT_DATA *p_data)
    616 {
    617     if (nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE)
    618     {
    619         /* Use exclusive RF mode callback */
    620         if (nfa_dm_cb.p_excl_conn_cback)
    621             (*nfa_dm_cb.p_excl_conn_cback) (event, p_data);
    622     }
    623     else
    624     {
    625         (*nfa_dm_cb.p_conn_cback) (event, p_data);
    626     }
    627 }
    628 
    629 /*******************************************************************************
    630 **
    631 ** Function         nfa_dm_rel_excl_rf_control_and_notify
    632 **
    633 ** Description      Stop exclusive RF control and notify app of
    634 **                  NFA_EXCLUSIVE_RF_CONTROL_STOPPED_EVT
    635 **
    636 ** Returns          void
    637 **
    638 *******************************************************************************/
    639 void nfa_dm_rel_excl_rf_control_and_notify (void)
    640 {
    641     tNFA_CONN_EVT_DATA conn_evt;
    642 
    643     NFA_TRACE_DEBUG0 ("nfa_dm_rel_excl_rf_control_and_notify ()");
    644 
    645     /* Exclusive RF control stopped. Notify app */
    646     nfa_dm_cb.flags &= ~NFA_DM_FLAGS_EXCL_RF_ACTIVE;
    647 
    648     /* Stop exclusive RF discovery for exclusive RF mode */
    649     nfa_dm_stop_excl_discovery ();
    650 
    651     /* Notify app that exclusive RF control has stopped */
    652     conn_evt.status = NFA_STATUS_OK;
    653     (*nfa_dm_cb.p_excl_conn_cback) (NFA_EXCLUSIVE_RF_CONTROL_STOPPED_EVT, &conn_evt);
    654     nfa_dm_cb.p_excl_conn_cback = NULL;
    655     nfa_dm_cb.p_excl_ndef_cback = NULL;
    656 }
    657 
    658 /*******************************************************************************
    659 **
    660 ** Function         nfa_dm_act_request_excl_rf_ctrl
    661 **
    662 ** Description      Request exclusive RF control
    663 **
    664 ** Returns          TRUE (message buffer to be freed by caller)
    665 **
    666 *******************************************************************************/
    667 BOOLEAN nfa_dm_act_request_excl_rf_ctrl (tNFA_DM_MSG *p_data)
    668 {
    669     tNFA_CONN_EVT_DATA conn_evt;
    670 
    671     NFA_TRACE_DEBUG0 ("nfa_dm_act_request_excl_rf_ctrl ()");
    672 
    673     if (!nfa_dm_cb.p_excl_conn_cback)
    674     {
    675         if (nfa_dm_cb.disc_cb.disc_state != NFA_DM_RFST_IDLE)
    676         {
    677             conn_evt.status = NFA_STATUS_FAILED;
    678             (*p_data->req_excl_rf_ctrl.p_conn_cback) (NFA_EXCLUSIVE_RF_CONTROL_STARTED_EVT, &conn_evt);
    679             return TRUE;
    680         }
    681 
    682         /* Store callbacks */
    683         nfa_dm_cb.p_excl_conn_cback = p_data->req_excl_rf_ctrl.p_conn_cback;
    684         nfa_dm_cb.p_excl_ndef_cback = p_data->req_excl_rf_ctrl.p_ndef_cback;
    685 
    686         nfa_dm_cb.flags |= NFA_DM_FLAGS_EXCL_RF_ACTIVE;
    687 
    688         /* start exclusive RF discovery */
    689         nfa_dm_start_excl_discovery (p_data->req_excl_rf_ctrl.poll_mask,
    690                                      &p_data->req_excl_rf_ctrl.listen_cfg,
    691                                      nfa_dm_excl_disc_cback);
    692     }
    693     else
    694     {
    695         NFA_TRACE_ERROR0 ("Exclusive rf control already requested");
    696 
    697         conn_evt.status = NFA_STATUS_FAILED;
    698         (*p_data->req_excl_rf_ctrl.p_conn_cback) (NFA_EXCLUSIVE_RF_CONTROL_STARTED_EVT, &conn_evt);
    699     }
    700 
    701     return TRUE;
    702 }
    703 
    704 
    705 /*******************************************************************************
    706 **
    707 ** Function         nfa_dm_act_release_excl_rf_ctrl
    708 **
    709 ** Description      Release exclusive RF control
    710 **
    711 ** Returns          TRUE (message buffer to be freed by caller)
    712 **
    713 *******************************************************************************/
    714 BOOLEAN nfa_dm_act_release_excl_rf_ctrl (tNFA_DM_MSG *p_data)
    715 {
    716     NFA_TRACE_DEBUG0 ("nfa_dm_act_release_excl_rf_ctrl ()");
    717 
    718     /* nfa_dm_rel_excl_rf_control_and_notify() is called when discovery state goes IDLE */
    719     nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_STOPPING;
    720 
    721     /* if discover command has been sent in IDLE state and waiting for response
    722     ** then just wait for responose. Otherwise initiate deactivating.
    723     */
    724     if (!(  (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_IDLE)
    725           &&(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP)  ))
    726     {
    727         nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_IDLE);
    728     }
    729 
    730     if (nfa_dm_cb.disc_cb.kovio_tle.in_use)
    731         nfa_sys_stop_timer (&nfa_dm_cb.disc_cb.kovio_tle);
    732 
    733     return TRUE;
    734 }
    735 
    736 /*******************************************************************************
    737 **
    738 ** Function         nfa_dm_act_deactivate
    739 **
    740 ** Description      Process deactivate command
    741 **
    742 ** Returns          TRUE (message buffer to be freed by caller)
    743 **
    744 *******************************************************************************/
    745 BOOLEAN nfa_dm_act_deactivate (tNFA_DM_MSG *p_data)
    746 {
    747     tNFA_CONN_EVT_DATA conn_evt;
    748     tNFA_DEACTIVATE_TYPE deact_type;
    749 
    750     NFA_TRACE_DEBUG0 ("nfa_dm_act_deactivate ()");
    751 
    752     if (  (p_data->deactivate.sleep_mode == FALSE)                 /* Always allow deactivate to IDLE */
    753         ||(  (nfa_dm_cb.disc_cb.activated_protocol != NFA_PROTOCOL_T1T)      /* Do not allow deactivate to SLEEP for T1T,NFCDEP, ISO15693 */
    754            &&(nfa_dm_cb.disc_cb.activated_protocol != NFA_PROTOCOL_NFC_DEP)
    755            &&(nfa_dm_cb.disc_cb.activated_protocol != NFA_PROTOCOL_ISO15693)
    756            &&(nfa_dm_cb.disc_cb.activated_protocol != NFC_PROTOCOL_KOVIO)  )  )
    757     {
    758         deact_type  = NFA_DEACTIVATE_TYPE_DISCOVERY;
    759         if (p_data->deactivate.sleep_mode)
    760         {
    761             if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_W4_HOST_SELECT)
    762             {
    763                 /* Deactivate to sleep mode not allowed in this state. */
    764                 deact_type = NFA_DEACTIVATE_TYPE_IDLE;
    765             }
    766             else if (nfa_dm_cb.disc_cb.disc_state != NFA_DM_RFST_LISTEN_SLEEP)
    767             {
    768                 deact_type = NFA_DEACTIVATE_TYPE_SLEEP;
    769             }
    770         }
    771         if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_W4_ALL_DISCOVERIES)
    772         {
    773             /* Only deactivate to IDLE is allowed in this state. */
    774             deact_type = NFA_DEACTIVATE_TYPE_IDLE;
    775         }
    776 
    777         if (  (nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_NFC_DEP)
    778             &&((nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE) == 0x00)  )
    779         {
    780             /* Exclusive RF control doesn't use NFA P2P */
    781             /* NFA P2P will deactivate NFC link after deactivating LLCP link */
    782             if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_P2P_PAUSED))
    783             {
    784                 nfa_p2p_deactivate_llcp ();
    785             }
    786             else
    787             {
    788                 nfa_dm_rf_deactivate (deact_type);
    789             }
    790             return (TRUE);
    791         }
    792         else
    793         {
    794 
    795             if (nfa_dm_rf_deactivate (deact_type) == NFA_STATUS_OK)
    796             {
    797                 if (nfa_dm_cb.disc_cb.kovio_tle.in_use)
    798                     nfa_sys_stop_timer (&nfa_dm_cb.disc_cb.kovio_tle);
    799                 nfa_rw_stop_presence_check_timer ();
    800                 return (TRUE);
    801             }
    802         }
    803     }
    804 
    805     NFA_TRACE_ERROR0 ("nfa_dm_act_deactivate (): invalid protocol, mode or state");
    806 
    807     /* Notify error to application */
    808     conn_evt.status = NFA_STATUS_FAILED;
    809     nfa_dm_conn_cback_event_notify (NFA_DEACTIVATE_FAIL_EVT, &conn_evt);
    810 
    811     return (TRUE);
    812 }
    813 
    814 /*******************************************************************************
    815 **
    816 ** Function         nfa_dm_act_power_off_sleep
    817 **
    818 ** Description      Process power off sleep mode request
    819 **
    820 ** Returns          TRUE (message buffer to be freed by caller)
    821 **
    822 *******************************************************************************/
    823 BOOLEAN nfa_dm_act_power_off_sleep (tNFA_DM_MSG *p_data)
    824 {
    825     NFA_TRACE_DEBUG0 ("nfa_dm_act_power_off_sleep ()");
    826 
    827     NFC_SetPowerOffSleep ((BOOLEAN) (p_data->hdr.layer_specific));
    828 
    829     return (TRUE);
    830 }
    831 
    832 /*******************************************************************************
    833 **
    834 ** Function         nfa_dm_act_reg_vsc
    835 **
    836 ** Description      Process registers VSC callback
    837 **
    838 ** Returns          TRUE (message buffer to be freed by caller)
    839 **
    840 *******************************************************************************/
    841 BOOLEAN nfa_dm_act_reg_vsc(tNFA_DM_MSG *p_data)
    842 {
    843     if (NFC_RegVSCback(p_data->reg_vsc.is_register, p_data->reg_vsc.p_cback) != NFC_STATUS_OK)
    844     {
    845         NFA_TRACE_ERROR0 ("NFC_RegVSCback failed");
    846     }
    847     return (TRUE);
    848 }
    849 
    850 /*******************************************************************************
    851 **
    852 ** Function         nfa_dm_act_send_vsc
    853 **
    854 ** Description      Send the NCI Vendor Specific command to the NCI command queue
    855 **
    856 ** Returns          FALSE (message buffer is NOT freed by caller)
    857 **
    858 *******************************************************************************/
    859 BOOLEAN nfa_dm_act_send_vsc(tNFA_DM_MSG *p_data)
    860 {
    861     BT_HDR  *p_cmd = (BT_HDR *)p_data;
    862 
    863     p_cmd->offset   = sizeof (tNFA_DM_API_SEND_VSC) - BT_HDR_SIZE;
    864     p_cmd->len      = p_data->send_vsc.cmd_params_len;
    865     NFC_SendVsCommand (p_data->send_vsc.oid, p_cmd, p_data->send_vsc.p_cback);
    866 
    867     /* Most dm action functions return TRUE, so nfa-sys frees the GKI buffer carrying the message,
    868      * This action function re-use the GKI buffer to send the VSC, so the GKI buffer can not be freed by nfa-sys */
    869     return (FALSE);
    870 }
    871 
    872 /*******************************************************************************
    873 **
    874 ** Function         nfa_dm_start_polling
    875 **
    876 ** Description      Start polling
    877 **
    878 ** Returns          tNFA_STATUS
    879 **
    880 *******************************************************************************/
    881 tNFA_STATUS nfa_dm_start_polling (void)
    882 {
    883     tNFA_STATUS             status;
    884     tNFA_TECHNOLOGY_MASK    poll_tech_mask;
    885     tNFA_DM_DISC_TECH_PROTO_MASK poll_disc_mask = 0;
    886 
    887     NFA_TRACE_DEBUG0 ("nfa_dm_start_polling ()");
    888 
    889     poll_tech_mask = nfa_dm_cb.poll_mask;
    890 
    891     /* start RF discovery with discovery callback */
    892     if (nfa_dm_cb.poll_disc_handle == NFA_HANDLE_INVALID)
    893     {
    894         if (poll_tech_mask & NFA_TECHNOLOGY_MASK_A)
    895         {
    896             poll_disc_mask |= NFA_DM_DISC_MASK_PA_T1T;
    897             poll_disc_mask |= NFA_DM_DISC_MASK_PA_T2T;
    898             poll_disc_mask |= NFA_DM_DISC_MASK_PA_ISO_DEP;
    899             poll_disc_mask |= NFA_DM_DISC_MASK_PA_NFC_DEP;
    900             poll_disc_mask |= NFA_DM_DISC_MASK_P_LEGACY;
    901         }
    902         if (poll_tech_mask & NFA_TECHNOLOGY_MASK_A_ACTIVE)
    903         {
    904             poll_disc_mask |= NFA_DM_DISC_MASK_PAA_NFC_DEP;
    905         }
    906         if (poll_tech_mask & NFA_TECHNOLOGY_MASK_B)
    907         {
    908             poll_disc_mask |= NFA_DM_DISC_MASK_PB_ISO_DEP;
    909         }
    910         if (poll_tech_mask & NFA_TECHNOLOGY_MASK_F)
    911         {
    912             poll_disc_mask |= NFA_DM_DISC_MASK_PF_T3T;
    913             poll_disc_mask |= NFA_DM_DISC_MASK_PF_NFC_DEP;
    914         }
    915         if (poll_tech_mask & NFA_TECHNOLOGY_MASK_F_ACTIVE)
    916         {
    917             poll_disc_mask |= NFA_DM_DISC_MASK_PFA_NFC_DEP;
    918         }
    919         if (poll_tech_mask & NFA_TECHNOLOGY_MASK_ISO15693)
    920         {
    921             poll_disc_mask |= NFA_DM_DISC_MASK_P_ISO15693;
    922         }
    923         if (poll_tech_mask & NFA_TECHNOLOGY_MASK_B_PRIME)
    924         {
    925             poll_disc_mask |= NFA_DM_DISC_MASK_P_B_PRIME;
    926         }
    927         if (poll_tech_mask & NFA_TECHNOLOGY_MASK_KOVIO)
    928         {
    929             poll_disc_mask |= NFA_DM_DISC_MASK_P_KOVIO;
    930         }
    931 
    932         nfa_dm_cb.poll_disc_handle = nfa_dm_add_rf_discover (poll_disc_mask,
    933                                                              NFA_DM_DISC_HOST_ID_DH,
    934                                                              nfa_dm_poll_disc_cback);
    935 
    936         if (nfa_dm_cb.poll_disc_handle != NFA_HANDLE_INVALID)
    937             status = NFA_STATUS_OK;
    938         else
    939             status = NFA_STATUS_FAILED;
    940     }
    941     else
    942     {
    943         status = NFA_STATUS_OK;
    944     }
    945 
    946     return (status);
    947 }
    948 
    949 /*******************************************************************************
    950 **
    951 ** Function         nfa_dm_act_enable_polling
    952 **
    953 ** Description      Process enable polling command
    954 **
    955 ** Returns          TRUE (message buffer to be freed by caller)
    956 **
    957 *******************************************************************************/
    958 BOOLEAN nfa_dm_act_enable_polling (tNFA_DM_MSG *p_data)
    959 {
    960     tNFA_CONN_EVT_DATA evt_data;
    961 
    962     NFA_TRACE_DEBUG0 ("nfa_dm_act_enable_polling ()");
    963 
    964     if (  (!(nfa_dm_cb.flags & NFA_DM_FLAGS_POLLING_ENABLED))
    965         &&(!(nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE)  )  )
    966     {
    967         nfa_dm_cb.poll_mask = p_data->enable_poll.poll_mask;
    968 
    969         if (nfa_dm_start_polling () == NFA_STATUS_OK)
    970         {
    971             nfa_dm_cb.flags |= NFA_DM_FLAGS_POLLING_ENABLED;
    972 
    973             evt_data.status = NFA_STATUS_OK;
    974             nfa_dm_conn_cback_event_notify (NFA_POLL_ENABLED_EVT, &evt_data);
    975             return (TRUE);
    976         }
    977     }
    978     else
    979     {
    980         NFA_TRACE_ERROR0 ("nfa_dm_act_enable_polling (): already started");
    981     }
    982 
    983     /* send NFA_POLL_ENABLED_EVT with NFA_STATUS_FAILED */
    984     evt_data.status = NFA_STATUS_FAILED;
    985     nfa_dm_conn_cback_event_notify (NFA_POLL_ENABLED_EVT, &evt_data);
    986 
    987     return (TRUE);
    988 }
    989 
    990 /*******************************************************************************
    991 **
    992 ** Function         nfa_dm_deactivate_polling
    993 **
    994 ** Description      Deactivate any polling state
    995 **
    996 ** Returns          TRUE if need to wait for deactivation
    997 **
    998 *******************************************************************************/
    999 static BOOLEAN nfa_dm_deactivate_polling (void)
   1000 {
   1001     NFA_TRACE_DEBUG0 ("nfa_dm_deactivate_polling ()");
   1002 
   1003     if (  (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_W4_ALL_DISCOVERIES)
   1004         ||(nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_W4_HOST_SELECT)  )
   1005     {
   1006         nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   1007         return FALSE;
   1008     }
   1009     else if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_POLL_ACTIVE)
   1010     {
   1011         if (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_NFC_DEP)
   1012         {
   1013             /* NFA P2P will deactivate NFC link after deactivating LLCP link */
   1014             nfa_p2p_deactivate_llcp ();
   1015         }
   1016         else
   1017         {
   1018             nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   1019         }
   1020         return TRUE;
   1021     }
   1022     else
   1023     {
   1024         return FALSE;
   1025     }
   1026 }
   1027 
   1028 /*******************************************************************************
   1029 **
   1030 ** Function         nfa_dm_act_disable_polling
   1031 **
   1032 ** Description      Process disable polling command
   1033 **
   1034 ** Returns          TRUE (message buffer to be freed by caller)
   1035 **
   1036 *******************************************************************************/
   1037 BOOLEAN nfa_dm_act_disable_polling (tNFA_DM_MSG *p_data)
   1038 {
   1039     tNFA_CONN_EVT_DATA evt_data;
   1040 
   1041     NFA_TRACE_DEBUG0 ("nfa_dm_act_disable_polling ()");
   1042 
   1043     if (nfa_dm_cb.poll_disc_handle != NFA_HANDLE_INVALID)
   1044     {
   1045         nfa_dm_cb.flags &= ~NFA_DM_FLAGS_POLLING_ENABLED;
   1046 
   1047         if (nfa_dm_deactivate_polling () == FALSE)
   1048         {
   1049             nfa_dm_delete_rf_discover (nfa_dm_cb.poll_disc_handle);
   1050             nfa_dm_cb.poll_disc_handle = NFA_HANDLE_INVALID;
   1051 
   1052             evt_data.status = NFA_STATUS_OK;
   1053             nfa_dm_conn_cback_event_notify (NFA_POLL_DISABLED_EVT, &evt_data);
   1054         }
   1055         else
   1056         {
   1057             nfa_dm_cb.flags |= NFA_DM_FLAGS_SEND_POLL_STOP_EVT;
   1058         }
   1059     }
   1060     else
   1061     {
   1062         evt_data.status = NFA_STATUS_FAILED;
   1063         nfa_dm_conn_cback_event_notify (NFA_POLL_DISABLED_EVT, &evt_data);
   1064     }
   1065 
   1066     return (TRUE);
   1067 }
   1068 
   1069 /*******************************************************************************
   1070 **
   1071 ** Function         nfa_dm_act_enable_listening
   1072 **
   1073 ** Description      Process enable listening command
   1074 **
   1075 ** Returns          TRUE (message buffer to be freed by caller)
   1076 **
   1077 *******************************************************************************/
   1078 BOOLEAN nfa_dm_act_enable_listening (tNFA_DM_MSG *p_data)
   1079 {
   1080     tNFA_CONN_EVT_DATA evt_data;
   1081 
   1082     NFA_TRACE_DEBUG0 ("nfa_dm_act_enable_listening ()");
   1083 
   1084     nfa_dm_cb.flags &= ~NFA_DM_FLAGS_LISTEN_DISABLED;
   1085     evt_data.status = NFA_STATUS_OK;
   1086     nfa_dm_conn_cback_event_notify (NFA_LISTEN_ENABLED_EVT, &evt_data);
   1087 
   1088     return (TRUE);
   1089 }
   1090 
   1091 /*******************************************************************************
   1092 **
   1093 ** Function         nfa_dm_act_disable_listening
   1094 **
   1095 ** Description      Process disable listening command
   1096 **
   1097 ** Returns          TRUE (message buffer to be freed by caller)
   1098 **
   1099 *******************************************************************************/
   1100 BOOLEAN nfa_dm_act_disable_listening (tNFA_DM_MSG *p_data)
   1101 {
   1102     tNFA_CONN_EVT_DATA evt_data;
   1103 
   1104     NFA_TRACE_DEBUG0 ("nfa_dm_act_disable_listening ()");
   1105 
   1106     nfa_dm_cb.flags |= NFA_DM_FLAGS_LISTEN_DISABLED;
   1107     evt_data.status = NFA_STATUS_OK;
   1108     nfa_dm_conn_cback_event_notify (NFA_LISTEN_DISABLED_EVT, &evt_data);
   1109 
   1110     return (TRUE);
   1111 }
   1112 
   1113 /*******************************************************************************
   1114 **
   1115 ** Function         nfa_dm_act_pause_p2p
   1116 **
   1117 ** Description      Process Pause P2P command
   1118 **
   1119 ** Returns          TRUE (message buffer to be freed by caller)
   1120 **
   1121 *******************************************************************************/
   1122 BOOLEAN nfa_dm_act_pause_p2p (tNFA_DM_MSG *p_data)
   1123 {
   1124     tNFA_CONN_EVT_DATA evt_data;
   1125 
   1126     NFA_TRACE_DEBUG0 ("nfa_dm_act_pause_p2p ()");
   1127 
   1128     nfa_dm_cb.flags |= NFA_DM_FLAGS_P2P_PAUSED;
   1129     evt_data.status = NFA_STATUS_OK;
   1130     nfa_dm_conn_cback_event_notify (NFA_P2P_PAUSED_EVT, &evt_data);
   1131 
   1132     return (TRUE);
   1133 }
   1134 
   1135 /*******************************************************************************
   1136 **
   1137 ** Function         nfa_dm_act_resume_p2p
   1138 **
   1139 ** Description      Process resume P2P command
   1140 **
   1141 ** Returns          TRUE (message buffer to be freed by caller)
   1142 **
   1143 *******************************************************************************/
   1144 BOOLEAN nfa_dm_act_resume_p2p (tNFA_DM_MSG *p_data)
   1145 {
   1146     tNFA_CONN_EVT_DATA evt_data;
   1147 
   1148     NFA_TRACE_DEBUG0 ("nfa_dm_act_resume_p2p ()");
   1149 
   1150     nfa_dm_cb.flags &= ~NFA_DM_FLAGS_P2P_PAUSED;
   1151     evt_data.status = NFA_STATUS_OK;
   1152     nfa_dm_conn_cback_event_notify (NFA_P2P_RESUMED_EVT, &evt_data);
   1153 
   1154     return (TRUE);
   1155 }
   1156 
   1157 /*******************************************************************************
   1158 **
   1159 ** Function         nfa_dm_act_send_raw_frame
   1160 **
   1161 ** Description      Send an raw frame on RF link
   1162 **
   1163 ** Returns          TRUE (message buffer to be freed by caller)
   1164 **
   1165 *******************************************************************************/
   1166 BOOLEAN nfa_dm_act_send_raw_frame (tNFA_DM_MSG *p_data)
   1167 {
   1168     tNFC_STATUS status = NFC_STATUS_FAILED;
   1169 
   1170     NFA_TRACE_DEBUG0 ("nfa_dm_act_send_raw_frame ()");
   1171 
   1172     /* If NFC link is activated */
   1173     if (  (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_POLL_ACTIVE)
   1174         ||(nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_LISTEN_ACTIVE)  )
   1175     {
   1176         nfa_dm_cb.flags |= NFA_DM_FLAGS_RAW_FRAME;
   1177         NFC_SetReassemblyFlag (FALSE);
   1178         /* If not in exclusive mode, and not activated for LISTEN, then forward raw data to NFA_RW to send */
   1179         if (  !(nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE)
   1180             &&!(nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_LISTEN_ACTIVE)
   1181             &&(  (nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_T1T)
   1182                ||(nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_T2T)
   1183                ||(nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_T3T)
   1184                ||(nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_ISO_DEP)
   1185                ||(nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_ISO15693)  )  )
   1186         {
   1187             /* if RW is checking presence then it will put into pending queue */
   1188             status = nfa_rw_send_raw_frame ((BT_HDR*)p_data);
   1189         }
   1190         else
   1191         {
   1192             status = NFC_SendData (NFC_RF_CONN_ID, (BT_HDR*) p_data);
   1193             if (status != NFC_STATUS_OK)
   1194             {
   1195                 NFC_SetReassemblyFlag (TRUE);
   1196             }
   1197             /* Already freed or NCI layer will free buffer */
   1198             return FALSE;
   1199         }
   1200     }
   1201 
   1202     if (status == NFC_STATUS_FAILED)
   1203     {
   1204         NFC_SetReassemblyFlag (TRUE);
   1205         /* free the buffer */
   1206         return TRUE;
   1207     }
   1208     else
   1209     {
   1210         /* NCI layer will free buffer */
   1211         return FALSE;
   1212     }
   1213 }
   1214 
   1215 /*******************************************************************************
   1216 **
   1217 ** Function         nfa_dm_set_p2p_listen_tech
   1218 **
   1219 ** Description      Notify change of P2P listen technologies to NFA P2P
   1220 **
   1221 ** Returns          TRUE (message buffer to be freed by caller)
   1222 **
   1223 *******************************************************************************/
   1224 BOOLEAN nfa_dm_set_p2p_listen_tech (tNFA_DM_MSG *p_data)
   1225 {
   1226     NFA_TRACE_DEBUG1 ("nfa_dm_set_p2p_listen_tech ()  tech_mask = %d",
   1227                       p_data->set_p2p_listen_tech.tech_mask);
   1228 
   1229     nfa_p2p_update_listen_tech (p_data->set_p2p_listen_tech.tech_mask);
   1230     nfa_dm_conn_cback_event_notify (NFA_SET_P2P_LISTEN_TECH_EVT, NULL);
   1231 
   1232     return (TRUE);
   1233 }
   1234 
   1235 /*******************************************************************************
   1236 **
   1237 ** Function         nfa_dm_act_start_rf_discovery
   1238 **
   1239 ** Description      Process start RF discovery command
   1240 **
   1241 ** Returns          TRUE (message buffer to be freed by caller)
   1242 **
   1243 *******************************************************************************/
   1244 BOOLEAN nfa_dm_act_start_rf_discovery (tNFA_DM_MSG *p_data)
   1245 {
   1246     tNFA_CONN_EVT_DATA evt_data;
   1247 
   1248     NFA_TRACE_DEBUG0 ("nfa_dm_act_start_rf_discovery ()");
   1249 
   1250     if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_ENABLED)
   1251     {
   1252         evt_data.status = NFA_STATUS_OK;
   1253         nfa_dm_conn_cback_event_notify (NFA_RF_DISCOVERY_STARTED_EVT, &evt_data);
   1254     }
   1255     else if (nfa_dm_cb.disc_cb.disc_state != NFA_DM_RFST_IDLE)
   1256     {
   1257         evt_data.status = NFA_STATUS_SEMANTIC_ERROR;
   1258         nfa_dm_conn_cback_event_notify (NFA_RF_DISCOVERY_STARTED_EVT, &evt_data);
   1259     }
   1260     else
   1261     {
   1262         nfa_dm_cb.disc_cb.disc_flags |= (NFA_DM_DISC_FLAGS_ENABLED|NFA_DM_DISC_FLAGS_NOTIFY);
   1263         nfa_dm_start_rf_discover ();
   1264     }
   1265 
   1266     return (TRUE);
   1267 }
   1268 
   1269 /*******************************************************************************
   1270 **
   1271 ** Function         nfa_dm_act_stop_rf_discovery
   1272 **
   1273 ** Description      Process stop RF discovery command
   1274 **
   1275 ** Returns          TRUE (message buffer to be freed by caller)
   1276 **
   1277 *******************************************************************************/
   1278 BOOLEAN nfa_dm_act_stop_rf_discovery (tNFA_DM_MSG *p_data)
   1279 {
   1280     tNFA_CONN_EVT_DATA evt_data;
   1281 
   1282     NFA_TRACE_DEBUG0 ("nfa_dm_act_stop_rf_discovery ()");
   1283 
   1284     if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_ENABLED) ||
   1285         (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_IDLE) )
   1286     {
   1287         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_ENABLED;
   1288 
   1289         /* if discover command has been sent in IDLE state and waiting for response */
   1290         if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP)
   1291         {
   1292             nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_STOPPING;
   1293         }
   1294         else
   1295         {
   1296             evt_data.status = NFA_STATUS_OK;
   1297             nfa_dm_conn_cback_event_notify (NFA_RF_DISCOVERY_STOPPED_EVT, &evt_data);
   1298         }
   1299     }
   1300     else
   1301     {
   1302         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_ENABLED;
   1303         nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_STOPPING;
   1304 
   1305         if (nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_IDLE) == NFA_STATUS_OK)
   1306         {
   1307             if (nfa_dm_cb.disc_cb.kovio_tle.in_use)
   1308                 nfa_sys_stop_timer (&nfa_dm_cb.disc_cb.kovio_tle);
   1309             nfa_rw_stop_presence_check_timer ();
   1310         }
   1311     }
   1312     return (TRUE);
   1313 }
   1314 
   1315 /*******************************************************************************
   1316 **
   1317 ** Function         nfa_dm_act_set_rf_disc_duration
   1318 **
   1319 ** Description      Set duration for RF discovery
   1320 **
   1321 ** Returns          TRUE (message buffer to be freed by caller)
   1322 **
   1323 *******************************************************************************/
   1324 BOOLEAN nfa_dm_act_set_rf_disc_duration (tNFA_DM_MSG *p_data)
   1325 {
   1326     nfa_dm_cb.disc_cb.disc_duration = p_data->disc_duration.rf_disc_dur_ms;
   1327     return (TRUE);
   1328 }
   1329 
   1330 /*******************************************************************************
   1331 **
   1332 ** Function         nfa_dm_act_select
   1333 **
   1334 ** Description      Process RF select command
   1335 **
   1336 ** Returns          TRUE (message buffer to be freed by caller)
   1337 **
   1338 *******************************************************************************/
   1339 BOOLEAN nfa_dm_act_select (tNFA_DM_MSG *p_data)
   1340 {
   1341     NFA_TRACE_DEBUG0 ("nfa_dm_act_select ()");
   1342 
   1343     nfa_dm_rf_discover_select (p_data->select.rf_disc_id,
   1344                                p_data->select.protocol,
   1345                                p_data->select.rf_interface);
   1346     return (TRUE);
   1347 }
   1348 
   1349 /*******************************************************************************
   1350 **
   1351 ** Function         nfa_dm_act_update_rf_params
   1352 **
   1353 ** Description      Process update RF communication parameters command
   1354 **
   1355 ** Returns          TRUE (message buffer to be freed by caller)
   1356 **
   1357 *******************************************************************************/
   1358 BOOLEAN nfa_dm_act_update_rf_params (tNFA_DM_MSG *p_data)
   1359 {
   1360     tNFA_CONN_EVT_DATA conn_evt;
   1361 
   1362     NFA_TRACE_DEBUG0 ("nfa_dm_act_update_rf_params ()");
   1363 
   1364     if (NFC_UpdateRFCommParams (&p_data->update_rf_params.params) != NFC_STATUS_OK)
   1365     {
   1366         conn_evt.status = NFA_STATUS_FAILED;
   1367         nfa_dm_conn_cback_event_notify (NFA_UPDATE_RF_PARAM_RESULT_EVT, &conn_evt);
   1368     }
   1369 
   1370     return (TRUE);
   1371 }
   1372 
   1373 /*******************************************************************************
   1374 **
   1375 ** Function         nfa_dm_act_disable_timeout
   1376 **
   1377 ** Description      timeout on disable process. Shutdown immediately
   1378 **
   1379 ** Returns          TRUE (message buffer to be freed by caller)
   1380 **
   1381 *******************************************************************************/
   1382 BOOLEAN nfa_dm_act_disable_timeout (tNFA_DM_MSG *p_data)
   1383 {
   1384     tNFA_DM_API_DISABLE disable;
   1385 
   1386     disable.graceful = FALSE;
   1387     nfa_dm_disable ((tNFA_DM_MSG *) &disable);
   1388     return (TRUE);
   1389 }
   1390 
   1391 /*******************************************************************************
   1392 **
   1393 ** Function         nfa_dm_act_conn_cback_notify
   1394 **
   1395 ** Description      Notify app of reader/writer/ndef events
   1396 **
   1397 ** Returns          nothing
   1398 **
   1399 *******************************************************************************/
   1400 void nfa_dm_act_conn_cback_notify (UINT8 event, tNFA_CONN_EVT_DATA *p_data)
   1401 {
   1402     NFA_TRACE_DEBUG1 ("nfa_dm_act_conn_cback_notify (): event:0x%X", event);
   1403 
   1404     /* Notify event using appropriate CONN_CBACK */
   1405     nfa_dm_conn_cback_event_notify (event, p_data);
   1406 
   1407     /* If not in exclusive RF mode, then read NDEF message from tag (if automatic reading is enabled) */
   1408     if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE))
   1409     {
   1410         if (  (event == NFA_NDEF_DETECT_EVT)
   1411             &&(nfa_dm_cb.flags & NFA_DM_FLAGS_AUTO_READING_NDEF)  )
   1412         {
   1413             /* read NDEF message from tag */
   1414             if (p_data->ndef_detect.status == NFA_STATUS_OK)
   1415             {
   1416                 NFA_RwReadNDef ();
   1417             }
   1418             else if (p_data->ndef_detect.status == NFA_STATUS_FAILED)
   1419             {
   1420                 nfa_dm_cb.flags &= ~NFA_DM_FLAGS_AUTO_READING_NDEF;
   1421             }
   1422             /* ignore NFA_STATUS_BUSY */
   1423         }
   1424         else if (  (event == NFA_READ_CPLT_EVT)
   1425                  &&(nfa_dm_cb.flags & NFA_DM_FLAGS_AUTO_READING_NDEF))
   1426         {
   1427             /* reading NDEF message is done */
   1428             nfa_dm_cb.flags &= ~NFA_DM_FLAGS_AUTO_READING_NDEF;
   1429         }
   1430     }
   1431 }
   1432 
   1433 /*******************************************************************************
   1434 **
   1435 ** Function         nfa_dm_act_data_cback
   1436 **
   1437 ** Description      Processing data from RF link
   1438 **
   1439 ** Returns          None
   1440 **
   1441 *******************************************************************************/
   1442 static void nfa_dm_act_data_cback (UINT8 conn_id, tNFC_CONN_EVT event, tNFC_CONN *p_data)
   1443 {
   1444     BT_HDR             *p_msg;
   1445     tNFA_CONN_EVT_DATA evt_data;
   1446 
   1447     NFA_TRACE_DEBUG1 ("nfa_dm_act_data_cback (): event = 0x%X", event);
   1448 
   1449     if (event == NFC_DATA_CEVT)
   1450     {
   1451         p_msg = (BT_HDR *) p_data->data.p_data;
   1452 
   1453         if (p_msg)
   1454         {
   1455             evt_data.data.status = p_data->data.status;
   1456             evt_data.data.p_data = (UINT8 *) (p_msg + 1) + p_msg->offset;
   1457             evt_data.data.len    = p_msg->len;
   1458 
   1459             nfa_dm_conn_cback_event_notify (NFA_DATA_EVT, &evt_data);
   1460 
   1461             GKI_freebuf (p_msg);
   1462         }
   1463         else
   1464         {
   1465             NFA_TRACE_ERROR0 ("nfa_dm_act_data_cback (): received NFC_DATA_CEVT with NULL data pointer");
   1466         }
   1467     }
   1468     else if (event == NFC_DEACTIVATE_CEVT)
   1469     {
   1470         NFC_SetStaticRfCback (NULL);
   1471     }
   1472 }
   1473 
   1474 /*******************************************************************************
   1475 **
   1476 ** Function         nfa_dm_excl_disc_cback
   1477 **
   1478 ** Description      Processing event from discovery callback
   1479 **
   1480 ** Returns          None
   1481 **
   1482 *******************************************************************************/
   1483 static void nfa_dm_excl_disc_cback (tNFA_DM_RF_DISC_EVT event, tNFC_DISCOVER *p_data)
   1484 {
   1485     tNFA_CONN_EVT_DATA evt_data;
   1486 
   1487     NFA_TRACE_DEBUG1 ("nfa_dm_excl_disc_cback (): event:0x%02X", event);
   1488 
   1489     switch (event)
   1490     {
   1491     case NFA_DM_RF_DISC_START_EVT:
   1492         evt_data.status = NFA_STATUS_OK;
   1493         nfa_dm_conn_cback_event_notify (NFA_EXCLUSIVE_RF_CONTROL_STARTED_EVT, &evt_data);
   1494         break;
   1495 
   1496     case NFA_DM_RF_DISC_ACTIVATED_EVT:
   1497         if (nfa_dm_cb.disc_cb.activated_tech_mode == NFC_DISCOVERY_TYPE_POLL_A)
   1498         {
   1499             /* store SEL_RES response */
   1500             nfa_dm_cb.disc_cb.activated_sel_res = p_data->activate.rf_tech_param.param.pa.sel_rsp;
   1501         }
   1502 
   1503         if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_LISTEN_ACTIVE)
   1504         {
   1505             /* Set data callback to receive raw frame */
   1506             NFC_SetStaticRfCback (nfa_dm_act_data_cback);
   1507 
   1508             memset (&(evt_data.activated.params), 0x00, sizeof (tNFA_TAG_PARAMS));
   1509             memcpy (&(evt_data.activated.activate_ntf), &(p_data->activate), sizeof (tNFC_ACTIVATE_DEVT));
   1510 
   1511             nfa_dm_conn_cback_event_notify (NFA_ACTIVATED_EVT, &evt_data);
   1512         }
   1513         else
   1514         {
   1515             /* holding activation notification until sub-module is ready */
   1516             nfa_dm_cb.p_activate_ntf = (UINT8*) GKI_getbuf (sizeof (tNFC_ACTIVATE_DEVT));
   1517 
   1518             if (nfa_dm_cb.p_activate_ntf)
   1519             {
   1520                 memcpy (nfa_dm_cb.p_activate_ntf,
   1521                         &(p_data->activate),
   1522                         sizeof (tNFC_ACTIVATE_DEVT));
   1523 
   1524                 if (  (nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_T1T)
   1525                     ||(nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_T2T)
   1526                     ||(nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_T3T)
   1527                     ||(nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_ISO_DEP)
   1528                     ||(nfa_dm_cb.disc_cb.activated_protocol  == NFA_PROTOCOL_ISO15693)
   1529                     ||(nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_KOVIO)  )
   1530                 {
   1531                     /* Notify NFA tag sub-system */
   1532                     nfa_rw_proc_disc_evt (NFA_DM_RF_DISC_ACTIVATED_EVT, p_data, FALSE);
   1533                 }
   1534                 else /* if NFC-DEP, ISO-DEP with frame interface or others */
   1535                 {
   1536                     /* Set data callback to receive raw frame */
   1537                     NFC_SetStaticRfCback (nfa_dm_act_data_cback);
   1538                     nfa_dm_notify_activation_status (NFA_STATUS_OK, NULL);
   1539                 }
   1540             }
   1541             else
   1542             {
   1543                 /* deactivate and restart RF discovery */
   1544                 nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_DISCOVERY);
   1545             }
   1546         }
   1547         break;
   1548 
   1549     case NFA_DM_RF_DISC_DEACTIVATED_EVT:
   1550 
   1551         /* if deactivated to idle or discovery */
   1552         if (  (p_data->deactivate.type == NFC_DEACTIVATE_TYPE_IDLE)
   1553             ||(p_data->deactivate.type == NFC_DEACTIVATE_TYPE_DISCOVERY)  )
   1554         {
   1555             /* clear stored NFCID/UID/KOVIO bar code */
   1556             nfa_dm_cb.activated_nfcid_len = 0;
   1557         }
   1558 
   1559         if (nfa_dm_cb.disc_cb.activated_protocol != NFC_PROTOCOL_NFC_DEP)
   1560         {
   1561             /* Notify NFA RW sub-systems */
   1562             nfa_rw_proc_disc_evt (NFA_DM_RF_DISC_DEACTIVATED_EVT, NULL, FALSE);
   1563         }
   1564 
   1565         /* if deactivated as sleep mode */
   1566         if (  (p_data->deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP)
   1567             ||(p_data->deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP_AF)  )
   1568         {
   1569             evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_SLEEP;
   1570         }
   1571         else
   1572         {
   1573             evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_IDLE;
   1574         }
   1575 
   1576         /* notify deactivation to upper layer */
   1577         nfa_dm_conn_cback_event_notify (NFA_DEACTIVATED_EVT, &evt_data);
   1578 
   1579         /* clean up SEL_RES response */
   1580         nfa_dm_cb.disc_cb.activated_sel_res = 0;
   1581         break;
   1582 
   1583     default:
   1584         NFA_TRACE_ERROR0 ("Unexpected event");
   1585         break;
   1586     }
   1587 }
   1588 
   1589 /*******************************************************************************
   1590 **
   1591 ** Function         nfa_dm_poll_disc_cback
   1592 **
   1593 ** Description      Processing event from discovery callback
   1594 **
   1595 ** Returns          None
   1596 **
   1597 *******************************************************************************/
   1598 static void nfa_dm_poll_disc_cback (tNFA_DM_RF_DISC_EVT event, tNFC_DISCOVER *p_data)
   1599 {
   1600     tNFA_CONN_EVT_DATA evt_data;
   1601 
   1602     NFA_TRACE_DEBUG1 ("nfa_dm_poll_disc_cback (): event:0x%02X", event);
   1603 
   1604     switch (event)
   1605     {
   1606     case NFA_DM_RF_DISC_START_EVT:
   1607         break;
   1608 
   1609     case NFA_DM_RF_DISC_ACTIVATED_EVT:
   1610 
   1611         if (nfa_dm_cb.disc_cb.activated_tech_mode == NFC_DISCOVERY_TYPE_POLL_A)
   1612         {
   1613             /* store SEL_RES response */
   1614             nfa_dm_cb.disc_cb.activated_sel_res = p_data->activate.rf_tech_param.param.pa.sel_rsp;
   1615         }
   1616 
   1617         /* holding activation notification until sub-module is ready */
   1618         nfa_dm_cb.p_activate_ntf = (UINT8*) GKI_getbuf (sizeof (tNFC_ACTIVATE_DEVT));
   1619 
   1620         if (nfa_dm_cb.p_activate_ntf)
   1621         {
   1622             memcpy (nfa_dm_cb.p_activate_ntf,
   1623                     &(p_data->activate),
   1624                     sizeof (tNFC_ACTIVATE_DEVT));
   1625 
   1626             if (  (nfa_dm_cb.disc_cb.activated_protocol     == NFC_PROTOCOL_NFC_DEP)
   1627                 &&(nfa_dm_cb.disc_cb.activated_rf_interface == NFC_INTERFACE_NFC_DEP)  )
   1628             {
   1629                 if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_P2P_PAUSED))
   1630                 {
   1631                     /* activate LLCP */
   1632                     nfa_p2p_activate_llcp (p_data);
   1633                 }
   1634                 else
   1635                 {
   1636                     NFA_TRACE_DEBUG0 ("P2P is paused");
   1637                     nfa_dm_notify_activation_status (NFA_STATUS_OK, NULL);
   1638                 }
   1639             }
   1640             else if (  (nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_T1T)
   1641                      ||(nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_T2T)
   1642                      ||(nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_T3T)
   1643                      ||(nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_ISO_DEP)
   1644                      ||(nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_15693)
   1645                      ||(nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_KOVIO)  )
   1646             {
   1647                 /* Notify NFA tag sub-system */
   1648                 nfa_rw_proc_disc_evt (NFA_DM_RF_DISC_ACTIVATED_EVT, p_data, TRUE);
   1649             }
   1650             else /* if NFC-DEP/ISO-DEP with frame interface */
   1651             {
   1652                 /* Set data callback to receive raw frame */
   1653                 NFC_SetStaticRfCback (nfa_dm_act_data_cback);
   1654                 nfa_dm_notify_activation_status (NFA_STATUS_OK, NULL);
   1655             }
   1656         }
   1657         else
   1658         {
   1659             /* deactivate and restart RF discovery */
   1660             nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_DISCOVERY);
   1661         }
   1662         break;
   1663 
   1664     case NFA_DM_RF_DISC_DEACTIVATED_EVT:
   1665 
   1666         /* if deactivated to idle or discovery */
   1667         if (  (p_data->deactivate.type == NFC_DEACTIVATE_TYPE_IDLE)
   1668             ||(p_data->deactivate.type == NFC_DEACTIVATE_TYPE_DISCOVERY)  )
   1669         {
   1670             /* clear stored NFCID/UID/KOVIO bar code */
   1671             nfa_dm_cb.activated_nfcid_len = 0;
   1672         }
   1673 
   1674         if (  (nfa_dm_cb.disc_cb.activated_protocol     == NFC_PROTOCOL_NFC_DEP)
   1675             &&(nfa_dm_cb.disc_cb.activated_rf_interface == NFC_INTERFACE_NFC_DEP)  )
   1676         {
   1677             /*
   1678             ** If LLCP link is not deactivated yet,
   1679             ** LLCP will receive deactivation ntf through data callback.
   1680             ** NFA P2P will receive callback event from LLCP.
   1681             */
   1682         }
   1683         else
   1684         {
   1685             /* Notify NFA RW sub-systems */
   1686             nfa_rw_proc_disc_evt (NFA_DM_RF_DISC_DEACTIVATED_EVT, NULL, TRUE);
   1687         }
   1688 
   1689         /* if NFA sent NFA_ACTIVATED_EVT earlier */
   1690         if (nfa_dm_cb.flags & NFA_DM_FLAGS_SEND_DEACTIVATED_EVT)
   1691         {
   1692             nfa_dm_cb.flags &= ~NFA_DM_FLAGS_SEND_DEACTIVATED_EVT;
   1693 
   1694             /* if deactivated as sleep mode */
   1695             if (  (p_data->deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP)
   1696                 ||(p_data->deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP_AF)  )
   1697             {
   1698                 evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_SLEEP;
   1699             }
   1700             else
   1701             {
   1702                 evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_IDLE;
   1703             }
   1704             /* notify deactivation to application */
   1705             nfa_dm_conn_cback_event_notify (NFA_DEACTIVATED_EVT, &evt_data);
   1706         }
   1707 
   1708         /* clean up SEL_RES response */
   1709         nfa_dm_cb.disc_cb.activated_sel_res = 0;
   1710 
   1711         if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_POLLING_ENABLED))
   1712         {
   1713             /* deregister discovery callback from NFA DM Discovery */
   1714             nfa_dm_delete_rf_discover (nfa_dm_cb.poll_disc_handle);
   1715             nfa_dm_cb.poll_disc_handle = NFA_HANDLE_INVALID;
   1716 
   1717             /* this is for disable polling */
   1718             if (nfa_dm_cb.flags & NFA_DM_FLAGS_SEND_POLL_STOP_EVT)
   1719             {
   1720                 nfa_dm_cb.flags &= ~NFA_DM_FLAGS_SEND_POLL_STOP_EVT;
   1721 
   1722                 evt_data.status = NFA_STATUS_OK;
   1723                 nfa_dm_conn_cback_event_notify (NFA_POLL_DISABLED_EVT, &evt_data);
   1724             }
   1725         }
   1726         break;
   1727     }
   1728 }
   1729 
   1730 /*******************************************************************************
   1731 **
   1732 ** Function         nfa_dm_notify_activation_status
   1733 **
   1734 ** Description      Processing activation status from sub-modules
   1735 **
   1736 ** Returns          None
   1737 **
   1738 *******************************************************************************/
   1739 void nfa_dm_notify_activation_status (tNFA_STATUS status, tNFA_TAG_PARAMS *p_params)
   1740 {
   1741     tNFA_CONN_EVT_DATA  evt_data;
   1742     tNFC_RF_TECH_PARAMS *p_tech_params;
   1743     UINT8               *p_nfcid = NULL, nfcid_len;
   1744 
   1745     NFA_TRACE_DEBUG1 ("nfa_dm_notify_activation_status (): status:0x%X", status);
   1746 
   1747     if (!nfa_dm_cb.p_activate_ntf)
   1748     {
   1749         /* this is for NFA P2P listen */
   1750         return;
   1751     }
   1752 
   1753     if (status == NFA_STATUS_OK)
   1754     {
   1755         /* notify NFC link activation */
   1756         memcpy ( &(evt_data.activated.activate_ntf),
   1757                  nfa_dm_cb.p_activate_ntf,
   1758                  sizeof (tNFC_ACTIVATE_DEVT));
   1759 
   1760         p_tech_params = &evt_data.activated.activate_ntf.rf_tech_param;
   1761 
   1762         memset (&(evt_data.activated.params), 0x00, sizeof (tNFA_TAG_PARAMS));
   1763         if (p_params)
   1764         {
   1765             memcpy (&(evt_data.activated.params),
   1766                     p_params,
   1767                     sizeof (tNFA_TAG_PARAMS));
   1768         }
   1769 
   1770         /* get length of NFCID and location */
   1771         if (p_tech_params->mode == NFC_DISCOVERY_TYPE_POLL_A)
   1772         {
   1773             nfcid_len = p_tech_params->param.pa.nfcid1_len;
   1774             p_nfcid   = p_tech_params->param.pa.nfcid1;
   1775         }
   1776         else if (p_tech_params->mode == NFC_DISCOVERY_TYPE_POLL_B)
   1777         {
   1778             nfcid_len = NFC_NFCID0_MAX_LEN;
   1779             p_nfcid   = p_tech_params->param.pb.nfcid0;
   1780         }
   1781         else if (p_tech_params->mode == NFC_DISCOVERY_TYPE_POLL_F)
   1782         {
   1783             nfcid_len = NFC_NFCID2_LEN;
   1784             p_nfcid   = p_tech_params->param.pf.nfcid2;
   1785         }
   1786         else if (p_tech_params->mode == NFC_DISCOVERY_TYPE_POLL_ISO15693)
   1787         {
   1788             nfcid_len = NFC_ISO15693_UID_LEN;
   1789             p_nfcid   = p_tech_params->param.pi93.uid;
   1790         }
   1791         else if (p_tech_params->mode == NFC_DISCOVERY_TYPE_POLL_KOVIO)
   1792         {
   1793             nfcid_len = p_tech_params->param.pk.uid_len;
   1794             p_nfcid   = p_tech_params->param.pk.uid;
   1795         }
   1796         else
   1797         {
   1798             nfcid_len = 0;
   1799         }
   1800 
   1801         /*
   1802         ** If not in exlusive RF mode, and
   1803         **      P2P activation, then push default NDEF message through SNEP
   1804         **      TAG activation, then read NDEF message
   1805         */
   1806         if (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_NFC_DEP)
   1807         {
   1808             /*
   1809             ** Default NDEF message will be put to NFC Forum defualt SNEP server
   1810             ** after receiving NFA_LLCP_ACTIVATED_EVT.
   1811             */
   1812         }
   1813         else if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE))
   1814         {
   1815             /*
   1816             ** if the same tag is activated then do not perform auto NDEF detection.
   1817             ** Application may put a tag into sleep mode and reactivate the same tag.
   1818             */
   1819 
   1820             if (  (p_tech_params->mode != nfa_dm_cb.activated_tech_mode)
   1821                 ||(nfcid_len != nfa_dm_cb.activated_nfcid_len)
   1822                 ||(memcmp (p_nfcid, nfa_dm_cb.activated_nfcid, nfcid_len)))
   1823             {
   1824                 if (  (nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_T1T)
   1825                     ||(nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_T2T)
   1826                     ||(nfa_dm_cb.disc_cb.activated_protocol  == NFC_PROTOCOL_T3T)
   1827                     ||(  (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_ISO_DEP)
   1828                        &&(nfa_dm_cb.disc_cb.activated_rf_interface == NFC_INTERFACE_ISO_DEP)  )
   1829                     ||(nfa_dm_cb.disc_cb.activated_protocol  == NFA_PROTOCOL_ISO15693)  )
   1830                 {
   1831                     if (p_nfa_dm_cfg->auto_detect_ndef)
   1832                     {
   1833                         if (p_nfa_dm_cfg->auto_read_ndef)
   1834                         {
   1835                             nfa_dm_cb.flags |= NFA_DM_FLAGS_AUTO_READING_NDEF;
   1836                         }
   1837                         NFA_RwDetectNDef ();
   1838                     }
   1839                     else if (p_nfa_dm_cfg->auto_read_ndef)
   1840                     {
   1841                         NFA_RwReadNDef ();
   1842                     }
   1843                 }
   1844             }
   1845         }
   1846 
   1847         /* store activated tag information */
   1848         nfa_dm_cb.activated_tech_mode = p_tech_params->mode;
   1849         nfa_dm_cb.activated_nfcid_len = nfcid_len;
   1850         if (nfcid_len)
   1851             memcpy (nfa_dm_cb.activated_nfcid, p_nfcid, nfcid_len);
   1852 
   1853         nfa_dm_cb.flags |= NFA_DM_FLAGS_SEND_DEACTIVATED_EVT;
   1854         if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_CHECKING))
   1855             nfa_dm_conn_cback_event_notify (NFA_ACTIVATED_EVT, &evt_data);
   1856     }
   1857     else
   1858     {
   1859         /* if NFC_DEP, NFA P2P will deactivate */
   1860         if (nfa_dm_cb.disc_cb.activated_protocol != NFC_PROTOCOL_NFC_DEP)
   1861         {
   1862             nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_DISCOVERY);
   1863         }
   1864     }
   1865 
   1866     GKI_freebuf (nfa_dm_cb.p_activate_ntf);
   1867     nfa_dm_cb.p_activate_ntf = NULL;
   1868 }
   1869 
   1870 #if (BT_TRACE_VERBOSE == TRUE)
   1871 /*******************************************************************************
   1872 **
   1873 ** Function         nfa_dm_nfc_revt_2_str
   1874 **
   1875 ** Description      convert nfc revt to string
   1876 **
   1877 *******************************************************************************/
   1878 char *nfa_dm_nfc_revt_2_str (tNFC_RESPONSE_EVT event)
   1879 {
   1880     switch (event) {
   1881     case NFC_ENABLE_REVT:
   1882         return "NFC_ENABLE_REVT";
   1883 
   1884     case NFC_DISABLE_REVT:
   1885         return "NFC_DISABLE_REVT";
   1886 
   1887     case NFC_SET_CONFIG_REVT:
   1888         return "NFC_SET_CONFIG_REVT";
   1889 
   1890     case NFC_GET_CONFIG_REVT:
   1891         return "NFC_GET_CONFIG_REVT";
   1892 
   1893     case NFC_NFCEE_DISCOVER_REVT:
   1894         return "NFC_NFCEE_DISCOVER_REVT";
   1895 
   1896     case NFC_NFCEE_INFO_REVT:
   1897         return "NFC_NFCEE_INFO_REVT";
   1898 
   1899     case NFC_NFCEE_MODE_SET_REVT:
   1900         return "NFC_NFCEE_MODE_SET_REVT";
   1901 
   1902     case NFC_RF_FIELD_REVT:
   1903         return "NFC_RF_FIELD_REVT";
   1904 
   1905     case NFC_EE_ACTION_REVT:
   1906         return "NFC_EE_ACTION_REVT";
   1907 
   1908     case NFC_EE_DISCOVER_REQ_REVT:
   1909         return "NFC_EE_DISCOVER_REQ_REVT";
   1910 
   1911     case NFC_SET_ROUTING_REVT:
   1912         return "NFC_SET_ROUTING_REVT";
   1913 
   1914     case NFC_GET_ROUTING_REVT:
   1915         return "NFC_GET_ROUTING_REVT";
   1916 
   1917     case NFC_GEN_ERROR_REVT:
   1918         return "NFC_GEN_ERROR_REVT";
   1919 
   1920     case NFC_NFCC_RESTART_REVT:
   1921         return "NFC_NFCC_RESTART_REVT";
   1922 
   1923     case NFC_NFCC_TIMEOUT_REVT:
   1924         return "NFC_NFCC_TIMEOUT_REVT";
   1925 
   1926     case NFC_NFCC_TRANSPORT_ERR_REVT:
   1927         return "NFC_NFCC_TRANSPORT_ERR_REVT";
   1928 
   1929     case NFC_NFCC_POWER_OFF_REVT:
   1930         return "NFC_NFCC_POWER_OFF_REVT";
   1931 
   1932     default:
   1933         return "unknown revt";
   1934         break;
   1935     }
   1936 }
   1937 #endif /* BT_VERBOSE */
   1938