Home | History | Annotate | Download | only in dm
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2010-2013 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 discovery
     23  *  function.
     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_p2p_int.h"
     31 #include "nfa_sys_int.h"
     32 #if (NFC_NFCEE_INCLUDED == TRUE)
     33 #include "nfa_ee_api.h"
     34 #include "nfa_ee_int.h"
     35 #endif
     36 #include "nfa_rw_int.h"
     37 
     38 /*
     39 **  static functions
     40 */
     41 
     42 static UINT8 nfa_dm_get_rf_discover_config (tNFA_DM_DISC_TECH_PROTO_MASK dm_disc_mask,
     43                                             tNFC_DISCOVER_PARAMS disc_params[],
     44                                             UINT8 max_params);
     45 static tNFA_STATUS nfa_dm_set_rf_listen_mode_config (tNFA_DM_DISC_TECH_PROTO_MASK tech_proto_mask);
     46 static void nfa_dm_set_rf_listen_mode_raw_config (tNFA_DM_DISC_TECH_PROTO_MASK *p_disc_mask);
     47 static tNFA_DM_DISC_TECH_PROTO_MASK nfa_dm_disc_get_disc_mask (tNFC_RF_TECH_N_MODE tech_n_mode,
     48                                                                tNFC_PROTOCOL       protocol);
     49 static void nfa_dm_notify_discovery (tNFA_DM_RF_DISC_DATA *p_data);
     50 static tNFA_STATUS nfa_dm_disc_notify_activation (tNFC_DISCOVER *p_data);
     51 static void nfa_dm_disc_notify_deactivation (tNFA_DM_RF_DISC_SM_EVENT sm_event, tNFC_DISCOVER *p_data);
     52 static void nfa_dm_disc_data_cback (UINT8 conn_id, tNFC_CONN_EVT event, tNFC_CONN *p_data);
     53 
     54 #if (BT_TRACE_VERBOSE == TRUE)
     55 static char *nfa_dm_disc_state_2_str (UINT8 state);
     56 static char *nfa_dm_disc_event_2_str (UINT8 event);
     57 #endif
     58 
     59 
     60 /*******************************************************************************
     61 **
     62 ** Function         nfa_dm_get_rf_discover_config
     63 **
     64 ** Description      Build RF discovery configurations from tNFA_DM_DISC_TECH_PROTO_MASK
     65 **
     66 ** Returns          number of RF discovery configurations
     67 **
     68 *******************************************************************************/
     69 static UINT8 nfa_dm_get_rf_discover_config (tNFA_DM_DISC_TECH_PROTO_MASK dm_disc_mask,
     70                                             tNFC_DISCOVER_PARAMS         disc_params[],
     71                                             UINT8 max_params)
     72 {
     73     UINT8 num_params = 0;
     74 
     75     /* Check polling A */
     76     if (dm_disc_mask & ( NFA_DM_DISC_MASK_PA_T1T
     77                         |NFA_DM_DISC_MASK_PA_T2T
     78                         |NFA_DM_DISC_MASK_PA_ISO_DEP
     79                         |NFA_DM_DISC_MASK_PA_NFC_DEP
     80                         |NFA_DM_DISC_MASK_P_LEGACY) )
     81     {
     82         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_POLL_A;
     83         disc_params[num_params].frequency = 1;
     84         num_params++;
     85 
     86         if (num_params >= max_params)
     87             return num_params;
     88     }
     89 
     90     /* Check polling B */
     91     if (dm_disc_mask & NFA_DM_DISC_MASK_PB_ISO_DEP)
     92     {
     93         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_POLL_B;
     94         disc_params[num_params].frequency = 1;
     95         num_params++;
     96 
     97         if (num_params >= max_params)
     98             return num_params;
     99     }
    100 
    101     /* Check polling F */
    102     if (dm_disc_mask & ( NFA_DM_DISC_MASK_PF_T3T
    103                         |NFA_DM_DISC_MASK_PF_NFC_DEP) )
    104     {
    105         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_POLL_F;
    106         disc_params[num_params].frequency = 1;
    107         num_params++;
    108 
    109         if (num_params >= max_params)
    110             return num_params;
    111     }
    112 
    113     /* Check polling A Active mode  */
    114     if (dm_disc_mask & NFA_DM_DISC_MASK_PAA_NFC_DEP)
    115     {
    116         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_POLL_A_ACTIVE;
    117         disc_params[num_params].frequency = 1;
    118         num_params++;
    119 
    120         if (num_params >= max_params)
    121             return num_params;
    122     }
    123 
    124     /* Check polling F Active mode  */
    125     if (dm_disc_mask & NFA_DM_DISC_MASK_PFA_NFC_DEP)
    126     {
    127         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_POLL_F_ACTIVE;
    128         disc_params[num_params].frequency = 1;
    129         num_params++;
    130 
    131         if (num_params >= max_params)
    132             return num_params;
    133     }
    134 
    135     /* Check listening A */
    136     if (dm_disc_mask & ( NFA_DM_DISC_MASK_LA_T1T
    137                         |NFA_DM_DISC_MASK_LA_T2T
    138                         |NFA_DM_DISC_MASK_LA_ISO_DEP
    139                         |NFA_DM_DISC_MASK_LA_NFC_DEP) )
    140     {
    141         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_LISTEN_A;
    142         disc_params[num_params].frequency = 1;
    143         num_params++;
    144 
    145         if (num_params >= max_params)
    146             return num_params;
    147     }
    148 
    149     /* Check listening B */
    150     if (dm_disc_mask & NFA_DM_DISC_MASK_LB_ISO_DEP)
    151     {
    152         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_LISTEN_B;
    153         disc_params[num_params].frequency = 1;
    154         num_params++;
    155 
    156         if (num_params >= max_params)
    157             return num_params;
    158     }
    159 
    160     /* Check listening F */
    161     if (dm_disc_mask & ( NFA_DM_DISC_MASK_LF_T3T
    162                         |NFA_DM_DISC_MASK_LF_NFC_DEP) )
    163     {
    164         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_LISTEN_F;
    165         disc_params[num_params].frequency = 1;
    166         num_params++;
    167 
    168         if (num_params >= max_params)
    169             return num_params;
    170     }
    171 
    172     /* Check listening A Active mode */
    173     if (dm_disc_mask & NFA_DM_DISC_MASK_LAA_NFC_DEP)
    174     {
    175         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_LISTEN_A_ACTIVE;
    176         disc_params[num_params].frequency = 1;
    177         num_params++;
    178 
    179         if (num_params >= max_params)
    180             return num_params;
    181     }
    182 
    183     /* Check listening F Active mode */
    184     if (dm_disc_mask & NFA_DM_DISC_MASK_LFA_NFC_DEP)
    185     {
    186         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_LISTEN_F_ACTIVE;
    187         disc_params[num_params].frequency = 1;
    188         num_params++;
    189 
    190         if (num_params >= max_params)
    191             return num_params;
    192     }
    193 
    194     /* Check polling ISO 15693 */
    195     if (dm_disc_mask & NFA_DM_DISC_MASK_P_ISO15693)
    196     {
    197         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_POLL_ISO15693;
    198         disc_params[num_params].frequency = 1;
    199         num_params++;
    200 
    201         if (num_params >= max_params)
    202             return num_params;
    203     }
    204 
    205     /* Check polling B' */
    206     if (dm_disc_mask & NFA_DM_DISC_MASK_P_B_PRIME)
    207     {
    208         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_POLL_B_PRIME;
    209         disc_params[num_params].frequency = 1;
    210         num_params++;
    211 
    212         if (num_params >= max_params)
    213             return num_params;
    214     }
    215 
    216     /* Check polling KOVIO */
    217     if (dm_disc_mask & NFA_DM_DISC_MASK_P_KOVIO)
    218     {
    219         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_POLL_KOVIO;
    220         disc_params[num_params].frequency = 1;
    221         num_params++;
    222 
    223         if (num_params >= max_params)
    224             return num_params;
    225     }
    226 
    227     /* Check listening ISO 15693 */
    228     if (dm_disc_mask & NFA_DM_DISC_MASK_L_ISO15693)
    229     {
    230         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_LISTEN_ISO15693;
    231         disc_params[num_params].frequency = 1;
    232         num_params++;
    233 
    234         if (num_params >= max_params)
    235             return num_params;
    236     }
    237 
    238     /* Check listening B' */
    239     if (dm_disc_mask & NFA_DM_DISC_MASK_L_B_PRIME)
    240     {
    241         disc_params[num_params].type      = NFC_DISCOVERY_TYPE_LISTEN_B_PRIME;
    242         disc_params[num_params].frequency = 1;
    243         num_params++;
    244 
    245         if (num_params >= max_params)
    246             return num_params;
    247     }
    248 
    249     return num_params;
    250 }
    251 
    252 /*******************************************************************************
    253 **
    254 ** Function         nfa_dm_set_rf_listen_mode_config
    255 **
    256 ** Description      Update listening protocol to NFCC
    257 **
    258 ** Returns          NFA_STATUS_OK if success
    259 **
    260 *******************************************************************************/
    261 static tNFA_STATUS nfa_dm_set_rf_listen_mode_config (tNFA_DM_DISC_TECH_PROTO_MASK tech_proto_mask)
    262 {
    263     UINT8 params[40], *p;
    264     UINT8 platform  = 0;
    265     UINT8 sens_info = 0;
    266 
    267     NFA_TRACE_DEBUG1 ("nfa_dm_set_rf_listen_mode_config () tech_proto_mask = 0x%08X",
    268                        tech_proto_mask);
    269 
    270     /*
    271     ** T1T listen     LA_PROT 0x80, LA_SENS_RES byte1:0x00 byte2:0x0C
    272     ** T2T listen     LA_PROT 0x00
    273     ** T3T listen     No bit for T3T in LF_PROT (CE T3T set listen parameters, system code, NFCID2, etc.)
    274     ** ISO-DEP listen LA_PROT 0x01, LB_PROT 0x01
    275     ** NFC-DEP listen LA_PROT 0x02, LF_PROT 0x02
    276     */
    277 
    278     if (tech_proto_mask & NFA_DM_DISC_MASK_LA_T1T)
    279     {
    280         platform = NCI_PARAM_PLATFORM_T1T;
    281     }
    282     else if (tech_proto_mask & NFA_DM_DISC_MASK_LA_T2T)
    283     {
    284         /* platform = 0 and sens_info = 0 */
    285     }
    286     else
    287     {
    288         if (tech_proto_mask & NFA_DM_DISC_MASK_LA_ISO_DEP)
    289         {
    290             sens_info |= NCI_PARAM_SEL_INFO_ISODEP;
    291         }
    292 
    293         if (tech_proto_mask & NFA_DM_DISC_MASK_LA_NFC_DEP)
    294         {
    295             sens_info |= NCI_PARAM_SEL_INFO_NFCDEP;
    296         }
    297     }
    298 
    299     p = params;
    300 
    301     /*
    302     ** for Listen A
    303     **
    304     ** Set ATQA 0x0C00 for T1T listen
    305     ** If the ATQA values are 0x0000, then the FW will use 0x0400
    306     ** which works for ISODEP, T2T and NFCDEP.
    307     */
    308     if (nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_A] == NFA_DM_DISC_HOST_ID_DH)
    309     {
    310         UINT8_TO_STREAM (p, NFC_PMID_LA_BIT_FRAME_SDD);
    311         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LA_BIT_FRAME_SDD);
    312         UINT8_TO_STREAM (p, 0x04);
    313         UINT8_TO_STREAM (p, NFC_PMID_LA_PLATFORM_CONFIG);
    314         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LA_PLATFORM_CONFIG);
    315         UINT8_TO_STREAM (p, platform);
    316         UINT8_TO_STREAM (p, NFC_PMID_LA_SEL_INFO);
    317         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LA_SEL_INFO);
    318         UINT8_TO_STREAM (p, sens_info);
    319     }
    320     else /* Let NFCC use UICC configuration by configuring with length = 0 */
    321     {
    322         UINT8_TO_STREAM (p, NFC_PMID_LA_BIT_FRAME_SDD);
    323         UINT8_TO_STREAM (p, 0);
    324         UINT8_TO_STREAM (p, NFC_PMID_LA_PLATFORM_CONFIG);
    325         UINT8_TO_STREAM (p, 0);
    326         UINT8_TO_STREAM (p, NFC_PMID_LA_SEL_INFO);
    327         UINT8_TO_STREAM (p, 0);
    328         UINT8_TO_STREAM (p, NFC_PMID_LA_NFCID1);
    329         UINT8_TO_STREAM (p, 0);
    330         UINT8_TO_STREAM (p, NFC_PMID_LA_HIST_BY);
    331         UINT8_TO_STREAM (p, 0);
    332     }
    333 
    334     /* for Listen B */
    335     if (nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_B] == NFA_DM_DISC_HOST_ID_DH)
    336     {
    337         UINT8_TO_STREAM (p, NFC_PMID_LB_SENSB_INFO);
    338         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LB_SENSB_INFO);
    339         if (tech_proto_mask & NFA_DM_DISC_MASK_LB_ISO_DEP)
    340         {
    341             UINT8_TO_STREAM (p, NCI_LISTEN_PROTOCOL_ISO_DEP);
    342         }
    343         else
    344         {
    345             UINT8_TO_STREAM (p,  0x00);
    346         }
    347     }
    348     else /* Let NFCC use UICC configuration by configuring with length = 0 */
    349     {
    350         UINT8_TO_STREAM (p, NFC_PMID_LB_SENSB_INFO);
    351         UINT8_TO_STREAM (p, 0);
    352         UINT8_TO_STREAM (p, NFC_PMID_LB_NFCID0);
    353         UINT8_TO_STREAM (p, 0);
    354         UINT8_TO_STREAM (p, NFC_PMID_LB_APPDATA);
    355         UINT8_TO_STREAM (p, 0);
    356         UINT8_TO_STREAM (p, NFC_PMID_LB_ADC_FO);
    357         UINT8_TO_STREAM (p, 0);
    358         UINT8_TO_STREAM (p, NFC_PMID_LB_H_INFO);
    359         UINT8_TO_STREAM (p, 0);
    360     }
    361 
    362     /* for Listen F */
    363     if (nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_F] == NFA_DM_DISC_HOST_ID_DH)
    364     {
    365         UINT8_TO_STREAM (p, NFC_PMID_LF_PROTOCOL);
    366         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LF_PROTOCOL);
    367         if (tech_proto_mask & NFA_DM_DISC_MASK_LF_NFC_DEP)
    368         {
    369             UINT8_TO_STREAM (p, NCI_LISTEN_PROTOCOL_NFC_DEP);
    370         }
    371         else
    372         {
    373             UINT8_TO_STREAM (p, 0x00);
    374         }
    375     }
    376     else
    377     {
    378         /* If DH is not listening on T3T, let NFCC use UICC configuration by configuring with length = 0 */
    379         if ((tech_proto_mask & NFA_DM_DISC_MASK_LF_T3T) == 0)
    380         {
    381             UINT8_TO_STREAM (p, NFC_PMID_LF_PROTOCOL);
    382             UINT8_TO_STREAM (p, 0);
    383             UINT8_TO_STREAM (p, NFC_PMID_LF_T3T_FLAGS2);
    384             UINT8_TO_STREAM (p, 0);
    385         }
    386     }
    387 
    388     if (p > params)
    389     {
    390         nfa_dm_check_set_config ((UINT8) (p - params), params, FALSE);
    391     }
    392 
    393     return NFA_STATUS_OK;
    394 }
    395 
    396 /*******************************************************************************
    397 **
    398 ** Function         nfa_dm_set_total_duration
    399 **
    400 ** Description      Update total duration to NFCC
    401 **
    402 ** Returns          void
    403 **
    404 *******************************************************************************/
    405 static void nfa_dm_set_total_duration (void)
    406 {
    407     UINT8 params[10], *p;
    408 
    409     NFA_TRACE_DEBUG0 ("nfa_dm_set_total_duration ()");
    410 
    411     p = params;
    412 
    413     /* for total duration */
    414     UINT8_TO_STREAM (p, NFC_PMID_TOTAL_DURATION);
    415     UINT8_TO_STREAM (p, NCI_PARAM_LEN_TOTAL_DURATION);
    416     UINT16_TO_STREAM (p, nfa_dm_cb.disc_cb.disc_duration);
    417 
    418     if (p > params)
    419     {
    420         nfa_dm_check_set_config ((UINT8) (p - params), params, FALSE);
    421     }
    422 }
    423 
    424 /*******************************************************************************
    425 **
    426 ** Function         nfa_dm_set_rf_listen_mode_raw_config
    427 **
    428 ** Description      Set raw listen parameters
    429 **
    430 ** Returns          void
    431 **
    432 *******************************************************************************/
    433 static void nfa_dm_set_rf_listen_mode_raw_config (tNFA_DM_DISC_TECH_PROTO_MASK *p_disc_mask)
    434 {
    435     tNFA_DM_DISC_TECH_PROTO_MASK disc_mask = 0;
    436     tNFA_LISTEN_CFG  *p_cfg = &nfa_dm_cb.disc_cb.excl_listen_config;
    437     UINT8 params[250], *p, xx;
    438 
    439     NFA_TRACE_DEBUG0 ("nfa_dm_set_rf_listen_mode_raw_config ()");
    440 
    441     /*
    442     ** Discovery Configuration Parameters for Listen A
    443     */
    444     if (  (nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_A] == NFA_DM_DISC_HOST_ID_DH)
    445         &&(p_cfg->la_enable)  )
    446     {
    447         p = params;
    448 
    449         UINT8_TO_STREAM (p, NFC_PMID_LA_BIT_FRAME_SDD);
    450         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LA_BIT_FRAME_SDD);
    451         UINT8_TO_STREAM (p, p_cfg->la_bit_frame_sdd);
    452 
    453         UINT8_TO_STREAM (p, NFC_PMID_LA_PLATFORM_CONFIG);
    454         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LA_PLATFORM_CONFIG);
    455         UINT8_TO_STREAM (p, p_cfg->la_platform_config);
    456 
    457         UINT8_TO_STREAM (p, NFC_PMID_LA_SEL_INFO);
    458         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LA_SEL_INFO);
    459         UINT8_TO_STREAM (p, p_cfg->la_sel_info);
    460 
    461         if (p_cfg->la_platform_config == NCI_PARAM_PLATFORM_T1T)
    462         {
    463             disc_mask |= NFA_DM_DISC_MASK_LA_T1T;
    464         }
    465         else
    466         {
    467             /* If T4T or NFCDEP */
    468             if (p_cfg->la_sel_info & NCI_PARAM_SEL_INFO_ISODEP)
    469             {
    470                 disc_mask |= NFA_DM_DISC_MASK_LA_ISO_DEP;
    471             }
    472 
    473             if (p_cfg->la_sel_info & NCI_PARAM_SEL_INFO_NFCDEP)
    474             {
    475                 disc_mask |= NFA_DM_DISC_MASK_LA_NFC_DEP;
    476             }
    477 
    478             /* If neither, T4T nor NFCDEP, then its T2T */
    479             if (disc_mask == 0)
    480             {
    481                 disc_mask |= NFA_DM_DISC_MASK_LA_T2T;
    482             }
    483         }
    484 
    485         UINT8_TO_STREAM (p, NFC_PMID_LA_NFCID1);
    486         UINT8_TO_STREAM (p, p_cfg->la_nfcid1_len);
    487         ARRAY_TO_STREAM (p, p_cfg->la_nfcid1, p_cfg->la_nfcid1_len);
    488 
    489         nfa_dm_check_set_config ((UINT8) (p - params), params, FALSE);
    490     }
    491 
    492     /*
    493     ** Discovery Configuration Parameters for Listen B
    494     */
    495     if (  (nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_B] == NFA_DM_DISC_HOST_ID_DH)
    496         &&(p_cfg->lb_enable)  )
    497     {
    498         p = params;
    499 
    500         UINT8_TO_STREAM (p, NFC_PMID_LB_SENSB_INFO);
    501         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LB_SENSB_INFO);
    502         UINT8_TO_STREAM (p, p_cfg->lb_sensb_info);
    503 
    504         UINT8_TO_STREAM (p, NFC_PMID_LB_NFCID0);
    505         UINT8_TO_STREAM (p, p_cfg->lb_nfcid0_len);
    506         ARRAY_TO_STREAM (p, p_cfg->lb_nfcid0, p_cfg->lb_nfcid0_len);
    507 
    508         UINT8_TO_STREAM (p, NFC_PMID_LB_APPDATA);
    509         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LB_APPDATA);
    510         ARRAY_TO_STREAM (p, p_cfg->lb_app_data, NCI_PARAM_LEN_LB_APPDATA);
    511 
    512         UINT8_TO_STREAM (p, NFC_PMID_LB_SFGI);
    513         UINT8_TO_STREAM (p, 1);
    514         UINT8_TO_STREAM (p, p_cfg->lb_adc_fo);
    515 
    516         UINT8_TO_STREAM (p, NFC_PMID_LB_ADC_FO);
    517         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LB_ADC_FO);
    518         UINT8_TO_STREAM (p, p_cfg->lb_adc_fo);
    519 
    520         nfa_dm_check_set_config ((UINT8) (p - params), params, FALSE);
    521 
    522         if (p_cfg->lb_sensb_info & NCI_LISTEN_PROTOCOL_ISO_DEP)
    523         {
    524             disc_mask |= NFA_DM_DISC_MASK_LB_ISO_DEP;
    525         }
    526     }
    527 
    528     /*
    529     ** Discovery Configuration Parameters for Listen F
    530     */
    531     if (  (nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_F] == NFA_DM_DISC_HOST_ID_DH)
    532         &&(p_cfg->lf_enable)  )
    533     {
    534         p = params;
    535 
    536         UINT8_TO_STREAM (p, NFC_PMID_LF_CON_BITR_F);
    537         UINT8_TO_STREAM (p, 1);
    538         UINT8_TO_STREAM (p, p_cfg->lf_con_bitr_f);
    539 
    540         UINT8_TO_STREAM (p, NFC_PMID_LF_PROTOCOL);
    541         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LF_PROTOCOL);
    542         UINT8_TO_STREAM (p, p_cfg->lf_protocol_type);
    543 
    544         UINT8_TO_STREAM (p, NFC_PMID_LF_T3T_FLAGS2);
    545         UINT8_TO_STREAM (p, NCI_PARAM_LEN_LF_T3T_FLAGS2);
    546         UINT16_TO_STREAM(p, p_cfg->lf_t3t_flags);
    547 
    548         /* if the bit at position X is set to 0, SC/NFCID2 with index X shall be ignored */
    549         for (xx = 0; xx < NFA_LF_MAX_SC_NFCID2; xx++)
    550         {
    551             if ((p_cfg->lf_t3t_flags & (0x0001 << xx)) != 0x0000)
    552             {
    553                 UINT8_TO_STREAM (p, NFC_PMID_LF_T3T_ID1 + xx);
    554                 UINT8_TO_STREAM (p, NCI_SYSTEMCODE_LEN + NCI_NFCID2_LEN);
    555                 ARRAY_TO_STREAM (p, p_cfg->lf_t3t_identifier[xx], NCI_SYSTEMCODE_LEN + NCI_NFCID2_LEN);
    556             }
    557         }
    558 
    559         UINT8_TO_STREAM (p,  NFC_PMID_LF_T3T_PMM);
    560         UINT8_TO_STREAM (p,  NCI_PARAM_LEN_LF_T3T_PMM);
    561         ARRAY_TO_STREAM (p,  p_cfg->lf_t3t_pmm, NCI_PARAM_LEN_LF_T3T_PMM);
    562 
    563         nfa_dm_check_set_config ((UINT8) (p - params), params, FALSE);
    564 
    565         if (p_cfg->lf_t3t_flags != NCI_LF_T3T_FLAGS2_ALL_DISABLED)
    566         {
    567             disc_mask |= NFA_DM_DISC_MASK_LF_T3T;
    568         }
    569         if (p_cfg->lf_protocol_type & NCI_LISTEN_PROTOCOL_NFC_DEP)
    570         {
    571             disc_mask |= NFA_DM_DISC_MASK_LF_NFC_DEP;
    572         }
    573     }
    574 
    575     /*
    576     ** Discovery Configuration Parameters for Listen ISO-DEP
    577     */
    578     if ((disc_mask & (NFA_DM_DISC_MASK_LA_ISO_DEP|NFA_DM_DISC_MASK_LB_ISO_DEP))
    579       &&(p_cfg->li_enable))
    580     {
    581         p = params;
    582 
    583         UINT8_TO_STREAM (p, NFC_PMID_FWI);
    584         UINT8_TO_STREAM (p, NCI_PARAM_LEN_FWI);
    585         UINT8_TO_STREAM (p, p_cfg->li_fwi);
    586 
    587         if (disc_mask & NFA_DM_DISC_MASK_LA_ISO_DEP)
    588         {
    589             UINT8_TO_STREAM (p, NFC_PMID_LA_HIST_BY);
    590             UINT8_TO_STREAM (p, p_cfg->la_hist_bytes_len);
    591             ARRAY_TO_STREAM (p, p_cfg->la_hist_bytes, p_cfg->la_hist_bytes_len);
    592         }
    593 
    594         if (disc_mask & NFA_DM_DISC_MASK_LB_ISO_DEP)
    595         {
    596             UINT8_TO_STREAM (p, NFC_PMID_LB_H_INFO);
    597             UINT8_TO_STREAM (p, p_cfg->lb_h_info_resp_len);
    598             ARRAY_TO_STREAM (p, p_cfg->lb_h_info_resp, p_cfg->lb_h_info_resp_len);
    599         }
    600 
    601         nfa_dm_check_set_config ((UINT8) (p - params), params, FALSE);
    602     }
    603 
    604     /*
    605     ** Discovery Configuration Parameters for Listen NFC-DEP
    606     */
    607     if (  (disc_mask & (NFA_DM_DISC_MASK_LA_NFC_DEP|NFA_DM_DISC_MASK_LF_NFC_DEP))
    608         &&(p_cfg->ln_enable))
    609     {
    610         p = params;
    611 
    612         UINT8_TO_STREAM (p, NFC_PMID_WT);
    613         UINT8_TO_STREAM (p, NCI_PARAM_LEN_WT);
    614         UINT8_TO_STREAM (p, p_cfg->ln_wt);
    615 
    616         UINT8_TO_STREAM (p, NFC_PMID_ATR_RES_GEN_BYTES);
    617         UINT8_TO_STREAM (p, p_cfg->ln_atr_res_gen_bytes_len);
    618         ARRAY_TO_STREAM (p, p_cfg->ln_atr_res_gen_bytes, p_cfg->ln_atr_res_gen_bytes_len);
    619 
    620         UINT8_TO_STREAM (p, NFC_PMID_ATR_RSP_CONFIG);
    621         UINT8_TO_STREAM (p, 1);
    622         UINT8_TO_STREAM (p, p_cfg->ln_atr_res_config);
    623 
    624         nfa_dm_check_set_config ((UINT8) (p - params), params, FALSE);
    625     }
    626 
    627     *p_disc_mask = disc_mask;
    628 
    629     NFA_TRACE_DEBUG1 ("nfa_dm_set_rf_listen_mode_raw_config () disc_mask = 0x%x", disc_mask);
    630 }
    631 
    632 /*******************************************************************************
    633 **
    634 ** Function         nfa_dm_disc_get_disc_mask
    635 **
    636 ** Description      Convert RF technology, mode and protocol to bit mask
    637 **
    638 ** Returns          tNFA_DM_DISC_TECH_PROTO_MASK
    639 **
    640 *******************************************************************************/
    641 static tNFA_DM_DISC_TECH_PROTO_MASK nfa_dm_disc_get_disc_mask (tNFC_RF_TECH_N_MODE tech_n_mode,
    642                                                                tNFC_PROTOCOL       protocol)
    643 {
    644     /* Set initial disc_mask to legacy poll or listen */
    645     tNFA_DM_DISC_TECH_PROTO_MASK disc_mask = ((tech_n_mode & 0x80) ? NFA_DM_DISC_MASK_L_LEGACY : NFA_DM_DISC_MASK_P_LEGACY);
    646 
    647     switch (tech_n_mode)
    648     {
    649     case NFC_DISCOVERY_TYPE_POLL_A:
    650         switch (protocol)
    651         {
    652         case NFC_PROTOCOL_T1T:
    653             disc_mask = NFA_DM_DISC_MASK_PA_T1T;
    654             break;
    655         case NFC_PROTOCOL_T2T:
    656             disc_mask = NFA_DM_DISC_MASK_PA_T2T;
    657             break;
    658         case NFC_PROTOCOL_ISO_DEP:
    659             disc_mask = NFA_DM_DISC_MASK_PA_ISO_DEP;
    660             break;
    661         case NFC_PROTOCOL_NFC_DEP:
    662             disc_mask = NFA_DM_DISC_MASK_PA_NFC_DEP;
    663             break;
    664         }
    665         break;
    666     case NFC_DISCOVERY_TYPE_POLL_B:
    667         if (protocol == NFC_PROTOCOL_ISO_DEP)
    668             disc_mask = NFA_DM_DISC_MASK_PB_ISO_DEP;
    669         break;
    670     case NFC_DISCOVERY_TYPE_POLL_F:
    671         if (protocol == NFC_PROTOCOL_T3T)
    672             disc_mask = NFA_DM_DISC_MASK_PF_T3T;
    673         else if (protocol == NFC_PROTOCOL_NFC_DEP)
    674             disc_mask = NFA_DM_DISC_MASK_PF_NFC_DEP;
    675         break;
    676     case NFC_DISCOVERY_TYPE_POLL_ISO15693:
    677         disc_mask = NFA_DM_DISC_MASK_P_ISO15693;
    678         break;
    679     case NFC_DISCOVERY_TYPE_POLL_B_PRIME:
    680         disc_mask = NFA_DM_DISC_MASK_P_B_PRIME;
    681         break;
    682     case NFC_DISCOVERY_TYPE_POLL_KOVIO:
    683         disc_mask = NFA_DM_DISC_MASK_P_KOVIO;
    684         break;
    685     case NFC_DISCOVERY_TYPE_POLL_A_ACTIVE:
    686         disc_mask = NFA_DM_DISC_MASK_PAA_NFC_DEP;
    687         break;
    688     case NFC_DISCOVERY_TYPE_POLL_F_ACTIVE:
    689         disc_mask = NFA_DM_DISC_MASK_PFA_NFC_DEP;
    690         break;
    691 
    692     case NFC_DISCOVERY_TYPE_LISTEN_A:
    693         switch (protocol)
    694         {
    695         case NFC_PROTOCOL_T1T:
    696             disc_mask = NFA_DM_DISC_MASK_LA_T1T;
    697             break;
    698         case NFC_PROTOCOL_T2T:
    699             disc_mask = NFA_DM_DISC_MASK_LA_T2T;
    700             break;
    701         case NFC_PROTOCOL_ISO_DEP:
    702             disc_mask = NFA_DM_DISC_MASK_LA_ISO_DEP;
    703             break;
    704         case NFC_PROTOCOL_NFC_DEP:
    705             disc_mask = NFA_DM_DISC_MASK_LA_NFC_DEP;
    706             break;
    707         }
    708         break;
    709     case NFC_DISCOVERY_TYPE_LISTEN_B:
    710         if (protocol == NFC_PROTOCOL_ISO_DEP)
    711             disc_mask = NFA_DM_DISC_MASK_LB_ISO_DEP;
    712         break;
    713     case NFC_DISCOVERY_TYPE_LISTEN_F:
    714         if (protocol == NFC_PROTOCOL_T3T)
    715             disc_mask = NFA_DM_DISC_MASK_LF_T3T;
    716         else if (protocol == NFC_PROTOCOL_NFC_DEP)
    717             disc_mask = NFA_DM_DISC_MASK_LF_NFC_DEP;
    718         break;
    719     case NFC_DISCOVERY_TYPE_LISTEN_ISO15693:
    720         disc_mask = NFA_DM_DISC_MASK_L_ISO15693;
    721         break;
    722     case NFC_DISCOVERY_TYPE_LISTEN_B_PRIME:
    723         disc_mask = NFA_DM_DISC_MASK_L_B_PRIME;
    724         break;
    725     case NFC_DISCOVERY_TYPE_LISTEN_A_ACTIVE:
    726         disc_mask = NFA_DM_DISC_MASK_LAA_NFC_DEP;
    727         break;
    728     case NFC_DISCOVERY_TYPE_LISTEN_F_ACTIVE:
    729         disc_mask = NFA_DM_DISC_MASK_LFA_NFC_DEP;
    730         break;
    731     }
    732 
    733     NFA_TRACE_DEBUG3 ("nfa_dm_disc_get_disc_mask (): tech_n_mode:0x%X, protocol:0x%X, disc_mask:0x%X",
    734                        tech_n_mode, protocol, disc_mask);
    735     return (disc_mask);
    736 }
    737 
    738 /*******************************************************************************
    739 **
    740 ** Function         nfa_dm_disc_discovery_cback
    741 **
    742 ** Description      Discovery callback event from NFC
    743 **
    744 ** Returns          void
    745 **
    746 *******************************************************************************/
    747 static void nfa_dm_disc_discovery_cback (tNFC_DISCOVER_EVT event, tNFC_DISCOVER *p_data)
    748 {
    749     tNFA_DM_RF_DISC_SM_EVENT dm_disc_event = NFA_DM_DISC_SM_MAX_EVENT;
    750 
    751     NFA_TRACE_DEBUG1 ("nfa_dm_disc_discovery_cback (): event:0x%X", event);
    752 
    753     switch (event)
    754     {
    755     case NFC_START_DEVT:
    756         dm_disc_event = NFA_DM_RF_DISCOVER_RSP;
    757         break;
    758     case NFC_RESULT_DEVT:
    759         dm_disc_event = NFA_DM_RF_DISCOVER_NTF;
    760         break;
    761     case NFC_SELECT_DEVT:
    762         dm_disc_event = NFA_DM_RF_DISCOVER_SELECT_RSP;
    763         break;
    764     case NFC_ACTIVATE_DEVT:
    765         dm_disc_event = NFA_DM_RF_INTF_ACTIVATED_NTF;
    766         break;
    767     case NFC_DEACTIVATE_DEVT:
    768         if (p_data->deactivate.is_ntf)
    769             dm_disc_event = NFA_DM_RF_DEACTIVATE_NTF;
    770         else
    771             dm_disc_event = NFA_DM_RF_DEACTIVATE_RSP;
    772         break;
    773     default:
    774         NFA_TRACE_ERROR0 ("Unexpected event");
    775         return;
    776     }
    777 
    778     nfa_dm_disc_sm_execute (dm_disc_event, (tNFA_DM_RF_DISC_DATA *) p_data);
    779 }
    780 
    781 /*******************************************************************************
    782 **
    783 ** Function         nfa_dm_disc_notify_started
    784 **
    785 ** Description      Report NFA_EXCLUSIVE_RF_CONTROL_STARTED_EVT or
    786 **                  NFA_RF_DISCOVERY_STARTED_EVT, if needed
    787 **
    788 ** Returns          void
    789 **
    790 *******************************************************************************/
    791 static void nfa_dm_disc_notify_started (tNFA_STATUS status)
    792 {
    793     tNFA_CONN_EVT_DATA      evt_data;
    794 
    795     if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_NOTIFY)
    796     {
    797         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_NOTIFY;
    798 
    799         evt_data.status = status;
    800 
    801         if (nfa_dm_cb.disc_cb.excl_disc_entry.in_use)
    802             nfa_dm_conn_cback_event_notify (NFA_EXCLUSIVE_RF_CONTROL_STARTED_EVT, &evt_data);
    803         else
    804             nfa_dm_conn_cback_event_notify (NFA_RF_DISCOVERY_STARTED_EVT, &evt_data);
    805     }
    806 }
    807 
    808 /*******************************************************************************
    809 **
    810 ** Function         nfa_dm_disc_conn_event_notify
    811 **
    812 ** Description      Notify application of CONN_CBACK event, using appropriate
    813 **                  callback
    814 **
    815 ** Returns          nothing
    816 **
    817 *******************************************************************************/
    818 void nfa_dm_disc_conn_event_notify (UINT8 event, tNFA_STATUS status)
    819 {
    820     tNFA_CONN_EVT_DATA      evt_data;
    821 
    822     if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_NOTIFY)
    823     {
    824         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_NOTIFY;
    825         evt_data.status               = status;
    826 
    827         if (nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE)
    828         {
    829             /* Use exclusive RF mode callback */
    830             if (nfa_dm_cb.p_excl_conn_cback)
    831                 (*nfa_dm_cb.p_excl_conn_cback) (event, &evt_data);
    832         }
    833         else
    834         {
    835             (*nfa_dm_cb.p_conn_cback) (event, &evt_data);
    836         }
    837     }
    838 }
    839 
    840 /*******************************************************************************
    841 **
    842 ** Function         nfa_dm_disc_force_to_idle
    843 **
    844 ** Description      Force NFCC to idle state while waiting for deactivation NTF
    845 **
    846 ** Returns          tNFC_STATUS
    847 **
    848 *******************************************************************************/
    849 static tNFC_STATUS nfa_dm_disc_force_to_idle (void)
    850 {
    851     tNFC_STATUS status = NFC_STATUS_SEMANTIC_ERROR;
    852 
    853     NFA_TRACE_DEBUG1 ("nfa_dm_disc_force_to_idle() disc_flags = 0x%x", nfa_dm_cb.disc_cb.disc_flags);
    854 
    855     if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_NTF) /* do not execute more than one */
    856     {
    857         nfa_dm_cb.disc_cb.disc_flags &= ~(NFA_DM_DISC_FLAGS_W4_NTF);
    858         nfa_dm_cb.disc_cb.disc_flags |= (NFA_DM_DISC_FLAGS_W4_RSP);
    859         nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
    860         status = NFC_Deactivate (NFC_DEACTIVATE_TYPE_IDLE);
    861     }
    862 
    863     return (status);
    864 }
    865 
    866 /*******************************************************************************
    867 **
    868 ** Function         nfa_dm_disc_deact_ntf_timeout_cback
    869 **
    870 ** Description      Timeout while waiting for deactivation NTF
    871 **
    872 ** Returns          void
    873 **
    874 *******************************************************************************/
    875 static void nfa_dm_disc_deact_ntf_timeout_cback (TIMER_LIST_ENT *p_tle)
    876 {
    877     NFA_TRACE_ERROR0 ("nfa_dm_disc_deact_ntf_timeout_cback()");
    878 
    879     nfa_dm_disc_force_to_idle();
    880 }
    881 
    882 /*******************************************************************************
    883 **
    884 ** Function         nfa_dm_send_deactivate_cmd
    885 **
    886 ** Description      Send deactivate command to NFCC, if needed.
    887 **
    888 ** Returns          NFC_STATUS_OK             - deactivate cmd is sent
    889 **                  NCI_STATUS_FAILED         - no buffers
    890 **                  NFC_STATUS_SEMANTIC_ERROR - this function does not attempt
    891 **                                              to send deactivate cmd
    892 **
    893 *******************************************************************************/
    894 static tNFC_STATUS nfa_dm_send_deactivate_cmd (tNFC_DEACT_TYPE deactivate_type)
    895 {
    896     tNFC_STATUS status = NFC_STATUS_SEMANTIC_ERROR;
    897     tNFA_DM_DISC_FLAGS w4_flags = nfa_dm_cb.disc_cb.disc_flags & (NFA_DM_DISC_FLAGS_W4_RSP|NFA_DM_DISC_FLAGS_W4_NTF);
    898 
    899     if (!w4_flags)
    900     {
    901         /* if deactivate CMD was not sent to NFCC */
    902         nfa_dm_cb.disc_cb.disc_flags |= (NFA_DM_DISC_FLAGS_W4_RSP|NFA_DM_DISC_FLAGS_W4_NTF);
    903 
    904         status = NFC_Deactivate (deactivate_type);
    905 
    906         if (!nfa_dm_cb.disc_cb.tle.in_use)
    907         {
    908             nfa_dm_cb.disc_cb.tle.p_cback = (TIMER_CBACK *)nfa_dm_disc_deact_ntf_timeout_cback;
    909             nfa_sys_start_timer (&nfa_dm_cb.disc_cb.tle, 0, NFA_DM_DISC_TIMEOUT_W4_DEACT_NTF);
    910         }
    911     }
    912     else
    913     {
    914         if (deactivate_type == NFC_DEACTIVATE_TYPE_SLEEP)
    915         {
    916             status = NFC_STATUS_SEMANTIC_ERROR;
    917         }
    918         else if (nfa_dm_cb.disc_cb.tle.in_use)
    919         {
    920             status = NFC_STATUS_OK;
    921         }
    922         else
    923         {
    924             status = nfa_dm_disc_force_to_idle ();
    925         }
    926     }
    927 
    928     return status;
    929 }
    930 
    931 /*******************************************************************************
    932 **
    933 ** Function         nfa_dm_start_rf_discover
    934 **
    935 ** Description      Start RF discovery
    936 **
    937 ** Returns          void
    938 **
    939 *******************************************************************************/
    940 void nfa_dm_start_rf_discover (void)
    941 {
    942     tNFC_DISCOVER_PARAMS    disc_params[NFA_DM_MAX_DISC_PARAMS];
    943     tNFA_DM_DISC_TECH_PROTO_MASK dm_disc_mask = 0, poll_mask, listen_mask;
    944     UINT8                   num_params, xx;
    945 
    946     NFA_TRACE_DEBUG0 ("nfa_dm_start_rf_discover ()");
    947     /* Make sure that RF discovery was enabled, or some app has exclusive control */
    948     if (  (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_ENABLED))
    949         &&(nfa_dm_cb.disc_cb.excl_disc_entry.in_use == FALSE)  )
    950     {
    951         return;
    952     }
    953 
    954     /* get listen mode routing table for technology */
    955     nfa_ee_get_tech_route (NFA_EE_PWR_STATE_ON, nfa_dm_cb.disc_cb.listen_RT);
    956 
    957     if (nfa_dm_cb.disc_cb.excl_disc_entry.in_use)
    958     {
    959         nfa_dm_set_rf_listen_mode_raw_config (&dm_disc_mask);
    960         dm_disc_mask |= (nfa_dm_cb.disc_cb.excl_disc_entry.requested_disc_mask & NFA_DM_DISC_MASK_POLL);
    961         nfa_dm_cb.disc_cb.excl_disc_entry.selected_disc_mask = dm_disc_mask;
    962     }
    963     else
    964     {
    965         /* Collect RF discovery request from sub-modules */
    966         for (xx = 0; xx < NFA_DM_DISC_NUM_ENTRIES; xx++)
    967         {
    968             if (nfa_dm_cb.disc_cb.entry[xx].in_use)
    969             {
    970                 poll_mask = (nfa_dm_cb.disc_cb.entry[xx].requested_disc_mask & NFA_DM_DISC_MASK_POLL);
    971 
    972                 /* clear poll mode technolgies and protocols which are already used by others */
    973                 poll_mask &= ~(dm_disc_mask & NFA_DM_DISC_MASK_POLL);
    974 
    975                 listen_mask = 0;
    976 
    977                 /*
    978                 ** add listen mode technolgies and protocols if host ID is matched to listen mode routing table
    979                 */
    980 
    981                 /* NFC-A */
    982                 if (nfa_dm_cb.disc_cb.entry[xx].host_id == nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_A])
    983                 {
    984                     listen_mask |= nfa_dm_cb.disc_cb.entry[xx].requested_disc_mask
    985                                    & ( NFA_DM_DISC_MASK_LA_T1T
    986                                       |NFA_DM_DISC_MASK_LA_T2T
    987                                       |NFA_DM_DISC_MASK_LA_ISO_DEP
    988                                       |NFA_DM_DISC_MASK_LA_NFC_DEP
    989                                       |NFA_DM_DISC_MASK_LAA_NFC_DEP );
    990                 }
    991                 else
    992                 {
    993                     /* host can listen ISO-DEP based on AID routing */
    994                     listen_mask |= (nfa_dm_cb.disc_cb.entry[xx].requested_disc_mask  & NFA_DM_DISC_MASK_LA_ISO_DEP);
    995 
    996                     /* host can listen NFC-DEP based on protocol routing */
    997                     listen_mask |= (nfa_dm_cb.disc_cb.entry[xx].requested_disc_mask  & NFA_DM_DISC_MASK_LA_NFC_DEP);
    998                     listen_mask |= (nfa_dm_cb.disc_cb.entry[xx].requested_disc_mask  & NFA_DM_DISC_MASK_LAA_NFC_DEP);
    999                 }
   1000 
   1001                 /* NFC-B */
   1002                 /* multiple hosts can listen ISO-DEP based on AID routing */
   1003                 listen_mask |= nfa_dm_cb.disc_cb.entry[xx].requested_disc_mask
   1004                                & NFA_DM_DISC_MASK_LB_ISO_DEP;
   1005 
   1006                 /* NFC-F */
   1007                 if (nfa_dm_cb.disc_cb.entry[xx].host_id == nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_F])
   1008                 {
   1009                     listen_mask |= nfa_dm_cb.disc_cb.entry[xx].requested_disc_mask
   1010                                    & ( NFA_DM_DISC_MASK_LF_T3T
   1011                                       |NFA_DM_DISC_MASK_LF_NFC_DEP
   1012                                       |NFA_DM_DISC_MASK_LFA_NFC_DEP );
   1013                 }
   1014                 else
   1015                 {
   1016                     /* NFCC can listen T3T based on NFCID routing */
   1017                     listen_mask |= (nfa_dm_cb.disc_cb.entry[xx].requested_disc_mask  & NFA_DM_DISC_MASK_LF_T3T);
   1018                 }
   1019 
   1020                 /* NFC-B Prime */
   1021                 if (nfa_dm_cb.disc_cb.entry[xx].host_id == nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_BP])
   1022                 {
   1023                     listen_mask |= nfa_dm_cb.disc_cb.entry[xx].requested_disc_mask
   1024                                    & NFA_DM_DISC_MASK_L_B_PRIME;
   1025                 }
   1026 
   1027                 /*
   1028                 ** clear listen mode technolgies and protocols which are already used by others
   1029                 */
   1030 
   1031                 /* Check if other modules are listening T1T or T2T */
   1032                 if (dm_disc_mask & (NFA_DM_DISC_MASK_LA_T1T|NFA_DM_DISC_MASK_LA_T2T))
   1033                 {
   1034                     listen_mask &= ~( NFA_DM_DISC_MASK_LA_T1T
   1035                                      |NFA_DM_DISC_MASK_LA_T2T
   1036                                      |NFA_DM_DISC_MASK_LA_ISO_DEP
   1037                                      |NFA_DM_DISC_MASK_LA_NFC_DEP );
   1038                 }
   1039 
   1040                 /* T1T/T2T has priority on NFC-A */
   1041                 if (  (dm_disc_mask & (NFA_DM_DISC_MASK_LA_ISO_DEP|NFA_DM_DISC_MASK_LA_NFC_DEP))
   1042                     &&(listen_mask & (NFA_DM_DISC_MASK_LA_T1T|NFA_DM_DISC_MASK_LA_T2T)))
   1043                 {
   1044                     dm_disc_mask &= ~( NFA_DM_DISC_MASK_LA_ISO_DEP
   1045                                       |NFA_DM_DISC_MASK_LA_NFC_DEP );
   1046                 }
   1047 
   1048                 /* Don't remove ISO-DEP because multiple hosts can listen ISO-DEP based on AID routing */
   1049 
   1050                 /* Check if other modules are listening NFC-DEP */
   1051                 if (dm_disc_mask & (NFA_DM_DISC_MASK_LA_NFC_DEP | NFA_DM_DISC_MASK_LAA_NFC_DEP))
   1052                 {
   1053                     listen_mask &= ~( NFA_DM_DISC_MASK_LA_NFC_DEP
   1054                                      |NFA_DM_DISC_MASK_LAA_NFC_DEP );
   1055                 }
   1056 
   1057                 nfa_dm_cb.disc_cb.entry[xx].selected_disc_mask = poll_mask | listen_mask;
   1058 
   1059                 NFA_TRACE_DEBUG2 ("nfa_dm_cb.disc_cb.entry[%d].selected_disc_mask = 0x%x",
   1060                                    xx, nfa_dm_cb.disc_cb.entry[xx].selected_disc_mask);
   1061 
   1062                 dm_disc_mask |= nfa_dm_cb.disc_cb.entry[xx].selected_disc_mask;
   1063             }
   1064         }
   1065 
   1066         /* Let P2P set GEN bytes for LLCP to NFCC */
   1067         if (dm_disc_mask & ( NFA_DM_DISC_MASK_PA_NFC_DEP
   1068                             |NFA_DM_DISC_MASK_PF_NFC_DEP
   1069                             |NFA_DM_DISC_MASK_LA_NFC_DEP
   1070                             |NFA_DM_DISC_MASK_LF_NFC_DEP
   1071                             |NFA_DM_DISC_MASK_PAA_NFC_DEP
   1072                             |NFA_DM_DISC_MASK_PFA_NFC_DEP
   1073                             |NFA_DM_DISC_MASK_LAA_NFC_DEP
   1074                             |NFA_DM_DISC_MASK_LFA_NFC_DEP ))
   1075         {
   1076             nfa_p2p_set_config (dm_disc_mask);
   1077         }
   1078     }
   1079 
   1080     NFA_TRACE_DEBUG1 ("dm_disc_mask = 0x%x", dm_disc_mask);
   1081 
   1082     /* Get Discovery Technology parameters */
   1083     num_params = nfa_dm_get_rf_discover_config (dm_disc_mask, disc_params, NFA_DM_MAX_DISC_PARAMS);
   1084 
   1085     if (num_params)
   1086     {
   1087         /*
   1088         ** NFCC will abort programming personality slots if not available.
   1089         ** NFCC programs the personality slots in the following order of RF technologies:
   1090         **      NFC-A, NFC-B, NFC-BP, NFC-I93
   1091         */
   1092 
   1093         /* if this is not for exclusive control */
   1094         if (!nfa_dm_cb.disc_cb.excl_disc_entry.in_use)
   1095         {
   1096             /* update listening protocols in each NFC technology */
   1097             nfa_dm_set_rf_listen_mode_config (dm_disc_mask);
   1098         }
   1099 
   1100         /* Set polling duty cycle */
   1101         nfa_dm_set_total_duration ();
   1102         nfa_dm_cb.disc_cb.dm_disc_mask = dm_disc_mask;
   1103 
   1104         NFC_DiscoveryStart (num_params, disc_params, nfa_dm_disc_discovery_cback);
   1105         /* set flag about waiting for response in IDLE state */
   1106         nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_W4_RSP;
   1107 
   1108         /* register callback to get interface error NTF */
   1109         NFC_SetStaticRfCback (nfa_dm_disc_data_cback);
   1110     }
   1111     else
   1112     {
   1113         /* RF discovery is started but there is no valid technology or protocol to discover */
   1114         nfa_dm_disc_notify_started (NFA_STATUS_OK);
   1115     }
   1116 
   1117     /* reset hanlde of activated sub-module */
   1118     nfa_dm_cb.disc_cb.activated_handle = NFA_HANDLE_INVALID;
   1119 }
   1120 
   1121 /*******************************************************************************
   1122 **
   1123 ** Function         nfa_dm_notify_discovery
   1124 **
   1125 ** Description      Send RF discovery notification to upper layer
   1126 **
   1127 ** Returns          void
   1128 **
   1129 *******************************************************************************/
   1130 static void nfa_dm_notify_discovery (tNFA_DM_RF_DISC_DATA *p_data)
   1131 {
   1132     tNFA_CONN_EVT_DATA conn_evt;
   1133 
   1134     /* let application select a device */
   1135     conn_evt.disc_result.status = NFA_STATUS_OK;
   1136     memcpy (&(conn_evt.disc_result.discovery_ntf),
   1137             &(p_data->nfc_discover.result),
   1138             sizeof (tNFC_RESULT_DEVT));
   1139 
   1140     nfa_dm_conn_cback_event_notify (NFA_DISC_RESULT_EVT, &conn_evt);
   1141 }
   1142 
   1143 /*******************************************************************************
   1144 **
   1145 ** Function         nfa_dm_disc_notify_activation
   1146 **
   1147 ** Description      Send RF activation notification to sub-module
   1148 **
   1149 ** Returns          NFA_STATUS_OK if success
   1150 **
   1151 *******************************************************************************/
   1152 static tNFA_STATUS nfa_dm_disc_notify_activation (tNFC_DISCOVER *p_data)
   1153 {
   1154     UINT8   xx, host_id_in_LRT;
   1155     UINT8   iso_dep_t3t__listen = NFA_DM_DISC_NUM_ENTRIES;
   1156 
   1157     tNFC_RF_TECH_N_MODE tech_n_mode = p_data->activate.rf_tech_param.mode;
   1158     tNFC_PROTOCOL       protocol    = p_data->activate.protocol;
   1159 
   1160     tNFA_DM_DISC_TECH_PROTO_MASK activated_disc_mask;
   1161 
   1162     NFA_TRACE_DEBUG2 ("nfa_dm_disc_notify_activation (): tech_n_mode:0x%X, proto:0x%X",
   1163                        tech_n_mode, protocol);
   1164 
   1165     if (nfa_dm_cb.disc_cb.excl_disc_entry.in_use)
   1166     {
   1167         nfa_dm_cb.disc_cb.activated_tech_mode    = tech_n_mode;
   1168         nfa_dm_cb.disc_cb.activated_rf_disc_id   = p_data->activate.rf_disc_id;
   1169         nfa_dm_cb.disc_cb.activated_rf_interface = p_data->activate.intf_param.type;
   1170         nfa_dm_cb.disc_cb.activated_protocol     = protocol;
   1171         nfa_dm_cb.disc_cb.activated_handle       = NFA_HANDLE_INVALID;
   1172 
   1173         if (nfa_dm_cb.disc_cb.excl_disc_entry.p_disc_cback)
   1174             (*(nfa_dm_cb.disc_cb.excl_disc_entry.p_disc_cback)) (NFA_DM_RF_DISC_ACTIVATED_EVT, p_data);
   1175 
   1176         return (NFA_STATUS_OK);
   1177     }
   1178 
   1179     /* if this is NFCEE direct RF interface, notify activation to whoever listening UICC */
   1180     if (p_data->activate.intf_param.type == NFC_INTERFACE_EE_DIRECT_RF)
   1181     {
   1182         for (xx = 0; xx < NFA_DM_DISC_NUM_ENTRIES; xx++)
   1183         {
   1184             if (  (nfa_dm_cb.disc_cb.entry[xx].in_use)
   1185                 &&(nfa_dm_cb.disc_cb.entry[xx].host_id != NFA_DM_DISC_HOST_ID_DH))
   1186             {
   1187                 nfa_dm_cb.disc_cb.activated_rf_disc_id   = p_data->activate.rf_disc_id;
   1188                 nfa_dm_cb.disc_cb.activated_rf_interface = p_data->activate.intf_param.type;
   1189                 nfa_dm_cb.disc_cb.activated_protocol     = NFC_PROTOCOL_UNKNOWN;
   1190                 nfa_dm_cb.disc_cb.activated_handle       = xx;
   1191 
   1192                 NFA_TRACE_DEBUG2 ("activated_rf_interface:0x%x, activated_handle: 0x%x",
   1193                                    nfa_dm_cb.disc_cb.activated_rf_interface,
   1194                                    nfa_dm_cb.disc_cb.activated_handle);
   1195 
   1196                 if (nfa_dm_cb.disc_cb.entry[xx].p_disc_cback)
   1197                     (*(nfa_dm_cb.disc_cb.entry[xx].p_disc_cback)) (NFA_DM_RF_DISC_ACTIVATED_EVT, p_data);
   1198 
   1199                 return (NFA_STATUS_OK);
   1200             }
   1201         }
   1202         return (NFA_STATUS_FAILED);
   1203     }
   1204 
   1205     /* get bit mask of technolgies/mode and protocol */
   1206     activated_disc_mask = nfa_dm_disc_get_disc_mask (tech_n_mode, protocol);
   1207 
   1208     /* get host ID of technology from listen mode routing table */
   1209     if (tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_A)
   1210     {
   1211         host_id_in_LRT = nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_A];
   1212     }
   1213     else if (tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_B)
   1214     {
   1215         host_id_in_LRT = nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_B];
   1216     }
   1217     else if (tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_F)
   1218     {
   1219         host_id_in_LRT = nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_F];
   1220     }
   1221     else if (tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_B_PRIME)
   1222     {
   1223         host_id_in_LRT = nfa_dm_cb.disc_cb.listen_RT[NFA_DM_DISC_LRT_NFC_BP];
   1224     }
   1225     else    /* DH only */
   1226     {
   1227         host_id_in_LRT = NFA_DM_DISC_HOST_ID_DH;
   1228     }
   1229 
   1230     if (protocol == NFC_PROTOCOL_NFC_DEP)
   1231     {
   1232         /* Force NFC-DEP to the host */
   1233         host_id_in_LRT = NFA_DM_DISC_HOST_ID_DH;
   1234     }
   1235 
   1236     for (xx = 0; xx < NFA_DM_DISC_NUM_ENTRIES; xx++)
   1237     {
   1238         /* if any matching NFC technology and protocol */
   1239         if (nfa_dm_cb.disc_cb.entry[xx].in_use)
   1240         {
   1241             if (nfa_dm_cb.disc_cb.entry[xx].host_id == host_id_in_LRT)
   1242             {
   1243                 if (nfa_dm_cb.disc_cb.entry[xx].selected_disc_mask & activated_disc_mask)
   1244                     break;
   1245             }
   1246             else
   1247             {
   1248                 /* check ISO-DEP listening even if host in LRT is not matched */
   1249                 if (protocol == NFC_PROTOCOL_ISO_DEP)
   1250                 {
   1251                     if (  (tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_A)
   1252                         &&(nfa_dm_cb.disc_cb.entry[xx].selected_disc_mask & NFA_DM_DISC_MASK_LA_ISO_DEP))
   1253                     {
   1254                         iso_dep_t3t__listen = xx;
   1255                     }
   1256                     else if (  (tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_B)
   1257                              &&(nfa_dm_cb.disc_cb.entry[xx].selected_disc_mask & NFA_DM_DISC_MASK_LB_ISO_DEP))
   1258                     {
   1259                         iso_dep_t3t__listen = xx;
   1260                     }
   1261                 }
   1262                 /* check T3T listening even if host in LRT is not matched */
   1263                 else if (protocol == NFC_PROTOCOL_T3T)
   1264                 {
   1265                     if (  (tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_F)
   1266                         &&(nfa_dm_cb.disc_cb.entry[xx].selected_disc_mask & NFA_DM_DISC_MASK_LF_T3T))
   1267                     {
   1268                         iso_dep_t3t__listen = xx;
   1269                     }
   1270                 }
   1271             }
   1272         }
   1273     }
   1274 
   1275     if (xx >= NFA_DM_DISC_NUM_ENTRIES)
   1276     {
   1277         /* if any ISO-DEP or T3T listening even if host in LRT is not matched */
   1278         xx = iso_dep_t3t__listen;
   1279     }
   1280 
   1281     if (xx < NFA_DM_DISC_NUM_ENTRIES)
   1282     {
   1283         nfa_dm_cb.disc_cb.activated_tech_mode    = tech_n_mode;
   1284         nfa_dm_cb.disc_cb.activated_rf_disc_id   = p_data->activate.rf_disc_id;
   1285         nfa_dm_cb.disc_cb.activated_rf_interface = p_data->activate.intf_param.type;
   1286         nfa_dm_cb.disc_cb.activated_protocol     = protocol;
   1287         nfa_dm_cb.disc_cb.activated_handle       = xx;
   1288 
   1289         NFA_TRACE_DEBUG2 ("activated_protocol:0x%x, activated_handle: 0x%x",
   1290                            nfa_dm_cb.disc_cb.activated_protocol,
   1291                            nfa_dm_cb.disc_cb.activated_handle);
   1292 
   1293         if (nfa_dm_cb.disc_cb.entry[xx].p_disc_cback)
   1294             (*(nfa_dm_cb.disc_cb.entry[xx].p_disc_cback)) (NFA_DM_RF_DISC_ACTIVATED_EVT, p_data);
   1295 
   1296         return (NFA_STATUS_OK);
   1297     }
   1298     else
   1299     {
   1300         nfa_dm_cb.disc_cb.activated_protocol = NFA_PROTOCOL_INVALID;
   1301         nfa_dm_cb.disc_cb.activated_handle   = NFA_HANDLE_INVALID;
   1302         return (NFA_STATUS_FAILED);
   1303     }
   1304 }
   1305 
   1306 /*******************************************************************************
   1307 **
   1308 ** Function         nfa_dm_disc_notify_deactivation
   1309 **
   1310 ** Description      Send deactivation notification to sub-module
   1311 **
   1312 ** Returns          None
   1313 **
   1314 *******************************************************************************/
   1315 static void nfa_dm_disc_notify_deactivation (tNFA_DM_RF_DISC_SM_EVENT sm_event,
   1316                                              tNFC_DISCOVER *p_data)
   1317 {
   1318     tNFA_HANDLE         xx;
   1319     tNFA_CONN_EVT_DATA  evt_data;
   1320     tNFC_DISCOVER       disc_data;
   1321 
   1322     NFA_TRACE_DEBUG1 ("nfa_dm_disc_notify_deactivation (): activated_handle=%d",
   1323                        nfa_dm_cb.disc_cb.activated_handle);
   1324 
   1325     if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_CHECKING)
   1326     {
   1327         NFA_TRACE_DEBUG0 ("nfa_dm_disc_notify_deactivation (): for presence check");
   1328         return;
   1329     }
   1330 
   1331     if (sm_event == NFA_DM_RF_DEACTIVATE_RSP)
   1332     {
   1333         /*
   1334         ** Activation has been aborted by upper layer in NFA_DM_RFST_W4_ALL_DISCOVERIES or NFA_DM_RFST_W4_HOST_SELECT
   1335         ** Deactivation by upper layer or RF link loss in NFA_DM_RFST_LISTEN_SLEEP
   1336         ** No sub-module is activated at this state.
   1337         */
   1338 
   1339         if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_LISTEN_SLEEP)
   1340         {
   1341             if (nfa_dm_cb.disc_cb.excl_disc_entry.in_use)
   1342             {
   1343                 if (nfa_dm_cb.disc_cb.excl_disc_entry.p_disc_cback)
   1344                 {
   1345                     disc_data.deactivate.type = NFA_DEACTIVATE_TYPE_IDLE;
   1346                     (*(nfa_dm_cb.disc_cb.excl_disc_entry.p_disc_cback)) (NFA_DM_RF_DISC_DEACTIVATED_EVT, &disc_data);
   1347                 }
   1348             }
   1349             else
   1350             {
   1351                 /* let each sub-module handle deactivation */
   1352                 for (xx = 0; xx < NFA_DM_DISC_NUM_ENTRIES; xx++)
   1353                 {
   1354                     if (  (nfa_dm_cb.disc_cb.entry[xx].in_use)
   1355                         &&(nfa_dm_cb.disc_cb.entry[xx].selected_disc_mask & NFA_DM_DISC_MASK_LISTEN)  )
   1356                     {
   1357                         disc_data.deactivate.type = NFA_DEACTIVATE_TYPE_IDLE;
   1358                         (*(nfa_dm_cb.disc_cb.entry[xx].p_disc_cback)) (NFA_DM_RF_DISC_DEACTIVATED_EVT, &disc_data);
   1359                     }
   1360                 }
   1361             }
   1362         }
   1363         else if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_STOPPING))
   1364         {
   1365             evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_IDLE;
   1366             /* notify deactivation to application */
   1367             nfa_dm_conn_cback_event_notify (NFA_DEACTIVATED_EVT, &evt_data);
   1368         }
   1369     }
   1370     else
   1371     {
   1372         /* notify event to activated module */
   1373         if (nfa_dm_cb.disc_cb.excl_disc_entry.in_use)
   1374         {
   1375             if (nfa_dm_cb.disc_cb.excl_disc_entry.p_disc_cback)
   1376             {
   1377                 disc_data.deactivate.type = NFA_DEACTIVATE_TYPE_IDLE;
   1378                 (*(nfa_dm_cb.disc_cb.excl_disc_entry.p_disc_cback)) (NFA_DM_RF_DISC_DEACTIVATED_EVT, p_data);
   1379             }
   1380         }
   1381         else
   1382         {
   1383             xx = nfa_dm_cb.disc_cb.activated_handle;
   1384 
   1385             if ((xx < NFA_DM_DISC_NUM_ENTRIES) && (nfa_dm_cb.disc_cb.entry[xx].in_use))
   1386             {
   1387                 if (nfa_dm_cb.disc_cb.entry[xx].p_disc_cback)
   1388                     (*(nfa_dm_cb.disc_cb.entry[xx].p_disc_cback)) (NFA_DM_RF_DISC_DEACTIVATED_EVT, p_data);
   1389             }
   1390         }
   1391     }
   1392 
   1393     /* clear activated information */
   1394     nfa_dm_cb.disc_cb.activated_tech_mode    = 0;
   1395     nfa_dm_cb.disc_cb.activated_rf_disc_id   = 0;
   1396     nfa_dm_cb.disc_cb.activated_rf_interface = 0;
   1397     nfa_dm_cb.disc_cb.activated_protocol     = NFA_PROTOCOL_INVALID;
   1398     nfa_dm_cb.disc_cb.activated_handle       = NFA_HANDLE_INVALID;
   1399 }
   1400 
   1401 /*******************************************************************************
   1402 **
   1403 ** Function         nfa_dm_disc_presence_check
   1404 **
   1405 ** Description      Perform legacy presence check (put tag to sleep, then
   1406 **                  wake it up to see if tag is present)
   1407 **
   1408 ** Returns          TRUE if operation started
   1409 **
   1410 *******************************************************************************/
   1411 tNFC_STATUS nfa_dm_disc_presence_check (void)
   1412 {
   1413     tNFC_STATUS status = NFC_STATUS_FAILED;
   1414 
   1415     if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_POLL_ACTIVE)
   1416     {
   1417         /* Deactivate to sleep mode */
   1418         status = nfa_dm_send_deactivate_cmd(NFC_DEACTIVATE_TYPE_SLEEP);
   1419         if (status == NFC_STATUS_OK)
   1420         {
   1421             /* deactivate to sleep is sent on behave of presence check.
   1422              * set the presence check information in control block */
   1423             nfa_dm_cb.disc_cb.disc_flags          |= NFA_DM_DISC_FLAGS_CHECKING;
   1424             nfa_dm_cb.presence_check_deact_pending = FALSE;
   1425         }
   1426     }
   1427 
   1428     return (status);
   1429 }
   1430 
   1431 /*******************************************************************************
   1432 **
   1433 ** Function         nfa_dm_disc_end_presence_check
   1434 **
   1435 ** Description      Perform legacy presence check (put tag to sleep, then
   1436 **                  wake it up to see if tag is present)
   1437 **
   1438 ** Returns          TRUE if operation started
   1439 **
   1440 *******************************************************************************/
   1441 static void nfa_dm_disc_end_presence_check (tNFC_STATUS status)
   1442 {
   1443     if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_CHECKING)
   1444     {
   1445         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_CHECKING;
   1446 
   1447         /* notify RW module that presence checking is finished */
   1448         nfa_rw_handle_presence_check_rsp (status);
   1449 
   1450         if (nfa_dm_cb.presence_check_deact_pending)
   1451         {
   1452             nfa_dm_cb.presence_check_deact_pending = FALSE;
   1453             nfa_dm_disc_sm_execute (NFA_DM_RF_DEACTIVATE_CMD,
   1454                                    (tNFA_DM_RF_DISC_DATA *) &nfa_dm_cb.presence_check_deact_type);
   1455         }
   1456     }
   1457 }
   1458 
   1459 /*******************************************************************************
   1460 **
   1461 ** Function         nfa_dm_disc_data_cback
   1462 **
   1463 ** Description      Monitoring interface error through data callback
   1464 **
   1465 ** Returns          void
   1466 **
   1467 *******************************************************************************/
   1468 static void nfa_dm_disc_data_cback (UINT8 conn_id, tNFC_CONN_EVT event, tNFC_CONN *p_data)
   1469 {
   1470     NFA_TRACE_DEBUG0 ("nfa_dm_disc_data_cback ()");
   1471 
   1472     /* if selection failed */
   1473     if (event == NFC_ERROR_CEVT)
   1474     {
   1475         nfa_dm_disc_sm_execute (NFA_DM_CORE_INTF_ERROR_NTF, NULL);
   1476     }
   1477     else if (event == NFC_DATA_CEVT)
   1478     {
   1479         GKI_freebuf (p_data->data.p_data);
   1480     }
   1481 }
   1482 
   1483 /*******************************************************************************
   1484 **
   1485 ** Function         nfa_dm_disc_new_state
   1486 **
   1487 ** Description      Processing discovery events in NFA_DM_RFST_IDLE state
   1488 **
   1489 ** Returns          void
   1490 **
   1491 *******************************************************************************/
   1492 void nfa_dm_disc_new_state (tNFA_DM_RF_DISC_STATE new_state)
   1493 {
   1494     tNFA_CONN_EVT_DATA      evt_data;
   1495     tNFA_DM_RF_DISC_STATE   old_state = nfa_dm_cb.disc_cb.disc_state;
   1496 
   1497 #if (BT_TRACE_VERBOSE == TRUE)
   1498     NFA_TRACE_DEBUG5 ("nfa_dm_disc_new_state (): old_state: %s (%d), new_state: %s (%d) disc_flags: 0x%x",
   1499                        nfa_dm_disc_state_2_str (nfa_dm_cb.disc_cb.disc_state), nfa_dm_cb.disc_cb.disc_state,
   1500                        nfa_dm_disc_state_2_str (new_state), new_state, nfa_dm_cb.disc_cb.disc_flags);
   1501 #else
   1502     NFA_TRACE_DEBUG3 ("nfa_dm_disc_new_state(): old_state: %d, new_state: %d disc_flags: 0x%x",
   1503                        nfa_dm_cb.disc_cb.disc_state, new_state, nfa_dm_cb.disc_cb.disc_flags);
   1504 #endif
   1505     nfa_dm_cb.disc_cb.disc_state = new_state;
   1506     if (  (new_state == NFA_DM_RFST_IDLE)
   1507         &&(!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP))  ) /* not error recovering */
   1508     {
   1509         if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_STOPPING)
   1510         {
   1511             nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_STOPPING;
   1512 
   1513             /* if exclusive RF control is stopping */
   1514             if (nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE)
   1515             {
   1516                 if (old_state > NFA_DM_RFST_DISCOVERY)
   1517                 {
   1518                     /* notify deactivation to application */
   1519                     evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_IDLE;
   1520                     nfa_dm_conn_cback_event_notify (NFA_DEACTIVATED_EVT, &evt_data);
   1521                 }
   1522 
   1523                 nfa_dm_rel_excl_rf_control_and_notify ();
   1524             }
   1525             else
   1526             {
   1527                 evt_data.status = NFA_STATUS_OK;
   1528                 nfa_dm_conn_cback_event_notify (NFA_RF_DISCOVERY_STOPPED_EVT, &evt_data);
   1529             }
   1530         }
   1531         if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_DISABLING)
   1532         {
   1533             nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_DISABLING;
   1534             nfa_sys_check_disabled ();
   1535         }
   1536     }
   1537 }
   1538 
   1539 /*******************************************************************************
   1540 **
   1541 ** Function         nfa_dm_disc_sm_idle
   1542 **
   1543 ** Description      Processing discovery events in NFA_DM_RFST_IDLE state
   1544 **
   1545 ** Returns          void
   1546 **
   1547 *******************************************************************************/
   1548 static void nfa_dm_disc_sm_idle (tNFA_DM_RF_DISC_SM_EVENT event,
   1549                                  tNFA_DM_RF_DISC_DATA *p_data)
   1550 {
   1551     UINT8              xx;
   1552 
   1553     switch (event)
   1554     {
   1555     case NFA_DM_RF_DISCOVER_CMD:
   1556         nfa_dm_start_rf_discover ();
   1557         break;
   1558 
   1559     case NFA_DM_RF_DISCOVER_RSP:
   1560         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_RSP;
   1561 
   1562         if (p_data->nfc_discover.status == NFC_STATUS_OK)
   1563         {
   1564             nfa_dm_disc_new_state (NFA_DM_RFST_DISCOVERY);
   1565 
   1566             /* if RF discovery was stopped while waiting for response */
   1567             if (nfa_dm_cb.disc_cb.disc_flags & (NFA_DM_DISC_FLAGS_STOPPING|NFA_DM_DISC_FLAGS_DISABLING))
   1568             {
   1569                 /* stop discovery */
   1570                 nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_W4_RSP;
   1571                 NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   1572                 break;
   1573             }
   1574 
   1575             if (nfa_dm_cb.disc_cb.excl_disc_entry.in_use)
   1576             {
   1577                 if (nfa_dm_cb.disc_cb.excl_disc_entry.disc_flags & NFA_DM_DISC_FLAGS_NOTIFY)
   1578                 {
   1579                     nfa_dm_cb.disc_cb.excl_disc_entry.disc_flags &= ~NFA_DM_DISC_FLAGS_NOTIFY;
   1580 
   1581                     if (nfa_dm_cb.disc_cb.excl_disc_entry.p_disc_cback)
   1582                         (*(nfa_dm_cb.disc_cb.excl_disc_entry.p_disc_cback)) (NFA_DM_RF_DISC_START_EVT, (tNFC_DISCOVER*) p_data);
   1583                 }
   1584             }
   1585             else
   1586             {
   1587                 /* notify event to each module which is waiting for start */
   1588                 for (xx = 0; xx < NFA_DM_DISC_NUM_ENTRIES; xx++)
   1589                 {
   1590                     /* if registered module is waiting for starting discovery */
   1591                     if (  (nfa_dm_cb.disc_cb.entry[xx].in_use)
   1592                         &&(nfa_dm_cb.disc_cb.dm_disc_mask & nfa_dm_cb.disc_cb.entry[xx].selected_disc_mask)
   1593                         &&(nfa_dm_cb.disc_cb.entry[xx].disc_flags & NFA_DM_DISC_FLAGS_NOTIFY)  )
   1594                     {
   1595                         nfa_dm_cb.disc_cb.entry[xx].disc_flags &= ~NFA_DM_DISC_FLAGS_NOTIFY;
   1596 
   1597                         if (nfa_dm_cb.disc_cb.entry[xx].p_disc_cback)
   1598                             (*(nfa_dm_cb.disc_cb.entry[xx].p_disc_cback)) (NFA_DM_RF_DISC_START_EVT, (tNFC_DISCOVER*) p_data);
   1599                     }
   1600                 }
   1601 
   1602             }
   1603             nfa_dm_disc_notify_started (p_data->nfc_discover.status);
   1604         }
   1605         else
   1606         {
   1607             /* in rare case that the discovery states of NFCC and DH mismatch and NFCC rejects Discover Cmd
   1608              * deactivate idle and then start disvocery when got deactivate rsp */
   1609             nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_W4_RSP;
   1610             NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   1611         }
   1612         break;
   1613 
   1614     case NFA_DM_RF_DEACTIVATE_RSP:
   1615         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_RSP;
   1616 
   1617         /* if NFCC goes to idle successfully */
   1618         if (p_data->nfc_discover.status == NFC_STATUS_OK)
   1619         {
   1620             /* if DH forced to go idle while waiting for deactivation NTF */
   1621             if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_NTF))
   1622             {
   1623                 nfa_dm_disc_notify_deactivation (NFA_DM_RF_DEACTIVATE_NTF, &(p_data->nfc_discover));
   1624 
   1625                 /* check any pending flags like NFA_DM_DISC_FLAGS_STOPPING or NFA_DM_DISC_FLAGS_DISABLING */
   1626                 nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   1627                 /* check if need to restart discovery after resync discovery state with NFCC */
   1628                 nfa_dm_start_rf_discover ();
   1629             }
   1630             /* Otherwise, deactivating when getting unexpected activation */
   1631         }
   1632         /* Otherwise, wait for deactivation NTF */
   1633         break;
   1634 
   1635     case NFA_DM_RF_DEACTIVATE_NTF:
   1636         /* if NFCC sent this after NFCC had rejected deactivate CMD to idle while deactivating */
   1637         if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_NTF))
   1638         {
   1639             if (p_data->nfc_discover.deactivate.type == NFC_DEACTIVATE_TYPE_DISCOVERY)
   1640             {
   1641                 /* stop discovery */
   1642                 nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_W4_RSP;
   1643                 NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   1644             }
   1645             else
   1646             {
   1647                 nfa_dm_disc_notify_deactivation (NFA_DM_RF_DEACTIVATE_NTF, &(p_data->nfc_discover));
   1648                 /* check any pending flags like NFA_DM_DISC_FLAGS_STOPPING or NFA_DM_DISC_FLAGS_DISABLING */
   1649                 nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   1650                 /* check if need to restart discovery after resync discovery state with NFCC */
   1651                 nfa_dm_start_rf_discover ();
   1652             }
   1653         }
   1654         /* Otherwise, deactivated when received unexpected activation in idle state */
   1655         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_NTF;
   1656         break;
   1657 
   1658     case NFA_DM_RF_INTF_ACTIVATED_NTF:
   1659         /* unexpected activation, deactivate to idle */
   1660         nfa_dm_cb.disc_cb.disc_flags |= (NFA_DM_DISC_FLAGS_W4_RSP|NFA_DM_DISC_FLAGS_W4_NTF);
   1661         NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   1662         break;
   1663 
   1664     case NFA_DM_LP_LISTEN_CMD:
   1665         nfa_dm_disc_new_state (NFA_DM_RFST_LP_LISTEN);
   1666         break;
   1667 
   1668     default:
   1669         NFA_TRACE_ERROR0 ("nfa_dm_disc_sm_idle (): Unexpected discovery event");
   1670         break;
   1671     }
   1672 }
   1673 
   1674 /*******************************************************************************
   1675 **
   1676 ** Function         nfa_dm_disc_sm_discovery
   1677 **
   1678 ** Description      Processing discovery events in NFA_DM_RFST_DISCOVERY state
   1679 **
   1680 ** Returns          void
   1681 **
   1682 *******************************************************************************/
   1683 static void nfa_dm_disc_sm_discovery (tNFA_DM_RF_DISC_SM_EVENT event,
   1684                                       tNFA_DM_RF_DISC_DATA *p_data)
   1685 {
   1686     switch (event)
   1687     {
   1688     case NFA_DM_RF_DEACTIVATE_CMD:
   1689         /* if deactivate CMD was not sent to NFCC */
   1690         if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP))
   1691         {
   1692             nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_W4_RSP;
   1693             NFC_Deactivate (p_data->deactivate_type);
   1694         }
   1695         break;
   1696     case NFA_DM_RF_DEACTIVATE_RSP:
   1697         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_RSP;
   1698 
   1699         /* if it's not race condition between deactivate CMD and activate NTF */
   1700         if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_NTF))
   1701         {
   1702             /* do not notify deactivated to idle in RF discovery state
   1703             ** because it is internal or stopping RF discovery
   1704             */
   1705 
   1706             /* there was no activation while waiting for deactivation RSP */
   1707             nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   1708             nfa_dm_start_rf_discover ();
   1709         }
   1710         break;
   1711     case NFA_DM_RF_DISCOVER_NTF:
   1712         nfa_dm_disc_new_state (NFA_DM_RFST_W4_ALL_DISCOVERIES);
   1713         nfa_dm_notify_discovery (p_data);
   1714         break;
   1715     case NFA_DM_RF_INTF_ACTIVATED_NTF:
   1716         if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP)
   1717         {
   1718             NFA_TRACE_DEBUG0 ("RF Activated while waiting for deactivation RSP");
   1719             /* it's race condition. DH has to wait for deactivation NTF */
   1720             nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_W4_NTF;
   1721         }
   1722         else
   1723         {
   1724             if (p_data->nfc_discover.activate.intf_param.type == NFC_INTERFACE_EE_DIRECT_RF)
   1725             {
   1726                 nfa_dm_disc_new_state (NFA_DM_RFST_LISTEN_ACTIVE);
   1727             }
   1728             else if (p_data->nfc_discover.activate.rf_tech_param.mode & 0x80)
   1729             {
   1730                 /* Listen mode */
   1731                 nfa_dm_disc_new_state (NFA_DM_RFST_LISTEN_ACTIVE);
   1732             }
   1733             else
   1734             {
   1735                 /* Poll mode */
   1736                 nfa_dm_disc_new_state (NFA_DM_RFST_POLL_ACTIVE);
   1737             }
   1738 
   1739             if (nfa_dm_disc_notify_activation (&(p_data->nfc_discover)) == NFA_STATUS_FAILED)
   1740             {
   1741                 NFA_TRACE_DEBUG0 ("Not matched, restart discovery after receiving deactivate ntf");
   1742 
   1743                 /* after receiving deactivate event, restart discovery */
   1744                 nfa_dm_cb.disc_cb.disc_flags |= (NFA_DM_DISC_FLAGS_W4_RSP|NFA_DM_DISC_FLAGS_W4_NTF);
   1745                 NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   1746             }
   1747         }
   1748         break;
   1749 
   1750     case NFA_DM_RF_DEACTIVATE_NTF:
   1751         /* if there was race condition between deactivate CMD and activate NTF */
   1752         if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_NTF)
   1753         {
   1754             /* race condition is resolved */
   1755             nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_NTF;
   1756 
   1757             if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP))
   1758             {
   1759                 /* do not notify deactivated to idle in RF discovery state
   1760                 ** because it is internal or stopping RF discovery
   1761                 */
   1762 
   1763                 nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   1764                 nfa_dm_start_rf_discover ();
   1765             }
   1766         }
   1767         break;
   1768     case NFA_DM_LP_LISTEN_CMD:
   1769         break;
   1770     case NFA_DM_CORE_INTF_ERROR_NTF:
   1771         break;
   1772     default:
   1773         NFA_TRACE_ERROR0 ("nfa_dm_disc_sm_discovery (): Unexpected discovery event");
   1774         break;
   1775     }
   1776 }
   1777 
   1778 /*******************************************************************************
   1779 **
   1780 ** Function         nfa_dm_disc_sm_w4_all_discoveries
   1781 **
   1782 ** Description      Processing discovery events in NFA_DM_RFST_W4_ALL_DISCOVERIES state
   1783 **
   1784 ** Returns          void
   1785 **
   1786 *******************************************************************************/
   1787 static void nfa_dm_disc_sm_w4_all_discoveries (tNFA_DM_RF_DISC_SM_EVENT event,
   1788                                                tNFA_DM_RF_DISC_DATA *p_data)
   1789 {
   1790     switch (event)
   1791     {
   1792     case NFA_DM_RF_DEACTIVATE_CMD:
   1793         /* if deactivate CMD was not sent to NFCC */
   1794         if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP))
   1795         {
   1796             nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_W4_RSP;
   1797             /* only IDLE mode is allowed */
   1798             NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   1799         }
   1800         break;
   1801     case NFA_DM_RF_DEACTIVATE_RSP:
   1802         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_RSP;
   1803         /* notify exiting from w4 all discoverie state */
   1804         nfa_dm_disc_notify_deactivation (NFA_DM_RF_DEACTIVATE_RSP, &(p_data->nfc_discover));
   1805 
   1806         nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   1807         nfa_dm_start_rf_discover ();
   1808         break;
   1809     case NFA_DM_RF_DISCOVER_NTF:
   1810         /* if deactivate CMD is already sent then ignore discover NTF */
   1811         if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP))
   1812         {
   1813             /* Notification Type = NCI_DISCOVER_NTF_LAST or NCI_DISCOVER_NTF_LAST_ABORT */
   1814             if (p_data->nfc_discover.result.more != NCI_DISCOVER_NTF_MORE)
   1815             {
   1816                 nfa_dm_disc_new_state (NFA_DM_RFST_W4_HOST_SELECT);
   1817             }
   1818             nfa_dm_notify_discovery (p_data);
   1819         }
   1820         break;
   1821     case NFA_DM_RF_INTF_ACTIVATED_NTF:
   1822         /*
   1823         ** This is only for ISO15693.
   1824         ** FW sends activation NTF when all responses are received from tags without host selecting.
   1825         */
   1826         nfa_dm_disc_new_state (NFA_DM_RFST_POLL_ACTIVE);
   1827 
   1828         if (nfa_dm_disc_notify_activation (&(p_data->nfc_discover)) == NFA_STATUS_FAILED)
   1829         {
   1830             NFA_TRACE_DEBUG0 ("Not matched, restart discovery after receiving deactivate ntf");
   1831 
   1832             /* after receiving deactivate event, restart discovery */
   1833             NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   1834         }
   1835         break;
   1836     default:
   1837         NFA_TRACE_ERROR0 ("nfa_dm_disc_sm_w4_all_discoveries (): Unexpected discovery event");
   1838         break;
   1839     }
   1840 }
   1841 
   1842 /*******************************************************************************
   1843 **
   1844 ** Function         nfa_dm_disc_sm_w4_host_select
   1845 **
   1846 ** Description      Processing discovery events in NFA_DM_RFST_W4_HOST_SELECT state
   1847 **
   1848 ** Returns          void
   1849 **
   1850 *******************************************************************************/
   1851 static void nfa_dm_disc_sm_w4_host_select (tNFA_DM_RF_DISC_SM_EVENT event,
   1852                                            tNFA_DM_RF_DISC_DATA *p_data)
   1853 {
   1854     tNFA_CONN_EVT_DATA conn_evt;
   1855     tNFA_DM_DISC_FLAGS  old_pres_check_flag = (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_CHECKING);
   1856     BOOLEAN             pres_check_event = FALSE;
   1857     BOOLEAN             pres_check_event_processed = FALSE;
   1858 
   1859     switch (event)
   1860     {
   1861     case NFA_DM_RF_DISCOVER_SELECT_CMD:
   1862         /* if not waiting to deactivate */
   1863         if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP))
   1864         {
   1865             NFC_DiscoverySelect (p_data->select.rf_disc_id,
   1866                                  p_data->select.protocol,
   1867                                  p_data->select.rf_interface);
   1868         }
   1869         else
   1870         {
   1871             nfa_dm_disc_conn_event_notify (NFA_SELECT_RESULT_EVT, NFA_STATUS_FAILED);
   1872         }
   1873         break;
   1874 
   1875     case NFA_DM_RF_DISCOVER_SELECT_RSP:
   1876         pres_check_event = TRUE;
   1877         /* notify application status of selection */
   1878         if (p_data->nfc_discover.status == NFC_STATUS_OK)
   1879         {
   1880             pres_check_event_processed = TRUE;
   1881             conn_evt.status = NFA_STATUS_OK;
   1882             /* register callback to get interface error NTF */
   1883             NFC_SetStaticRfCback (nfa_dm_disc_data_cback);
   1884         }
   1885         else
   1886             conn_evt.status = NFA_STATUS_FAILED;
   1887 
   1888         if (!old_pres_check_flag)
   1889         {
   1890             nfa_dm_disc_conn_event_notify (NFA_SELECT_RESULT_EVT, p_data->nfc_discover.status);
   1891         }
   1892         break;
   1893     case NFA_DM_RF_INTF_ACTIVATED_NTF:
   1894         nfa_dm_disc_new_state (NFA_DM_RFST_POLL_ACTIVE);
   1895         if (old_pres_check_flag)
   1896         {
   1897             /* Handle presence check success: notify RW module of presence of tag; if deactivation is pending then deactivate  */
   1898             nfa_dm_disc_end_presence_check (NFC_STATUS_OK);
   1899         }
   1900 
   1901         else if (nfa_dm_disc_notify_activation (&(p_data->nfc_discover)) == NFA_STATUS_FAILED)
   1902         {
   1903             NFA_TRACE_DEBUG0 ("Not matched, restart discovery after receiving deactivate ntf");
   1904 
   1905             /* after receiving deactivate event, restart discovery */
   1906             NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   1907         }
   1908         break;
   1909     case NFA_DM_RF_DEACTIVATE_CMD:
   1910         if (old_pres_check_flag)
   1911         {
   1912             nfa_dm_cb.presence_check_deact_pending = TRUE;
   1913             nfa_dm_cb.presence_check_deact_type    = p_data->deactivate_type;
   1914         }
   1915         /* if deactivate CMD was not sent to NFCC */
   1916         else if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP))
   1917         {
   1918             nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_W4_RSP;
   1919             /* only IDLE mode is allowed */
   1920             NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   1921         }
   1922         break;
   1923     case NFA_DM_RF_DEACTIVATE_RSP:
   1924         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_RSP;
   1925         /* notify exiting from host select state */
   1926         nfa_dm_disc_notify_deactivation (NFA_DM_RF_DEACTIVATE_RSP, &(p_data->nfc_discover));
   1927 
   1928         nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   1929         nfa_dm_start_rf_discover ();
   1930         break;
   1931 
   1932     case NFA_DM_CORE_INTF_ERROR_NTF:
   1933         pres_check_event    = TRUE;
   1934         if (!old_pres_check_flag)
   1935         {
   1936             /* target activation failed, upper layer may deactivate or select again */
   1937             conn_evt.status = NFA_STATUS_FAILED;
   1938             nfa_dm_conn_cback_event_notify (NFA_SELECT_RESULT_EVT, &conn_evt);
   1939         }
   1940         break;
   1941     default:
   1942         NFA_TRACE_ERROR0 ("nfa_dm_disc_sm_w4_host_select (): Unexpected discovery event");
   1943         break;
   1944     }
   1945 
   1946     if (old_pres_check_flag && pres_check_event && !pres_check_event_processed)
   1947     {
   1948         /* performing presence check for unknow protocol and exception conditions happened
   1949          * clear presence check information and report failure */
   1950         nfa_dm_disc_end_presence_check (NFC_STATUS_FAILED);
   1951     }
   1952 }
   1953 
   1954 /*******************************************************************************
   1955 **
   1956 ** Function         nfa_dm_disc_sm_poll_active
   1957 **
   1958 ** Description      Processing discovery events in NFA_DM_RFST_POLL_ACTIVE state
   1959 **
   1960 ** Returns          void
   1961 **
   1962 *******************************************************************************/
   1963 static void nfa_dm_disc_sm_poll_active (tNFA_DM_RF_DISC_SM_EVENT event,
   1964                                         tNFA_DM_RF_DISC_DATA *p_data)
   1965 {
   1966     tNFC_STATUS status;
   1967     tNFA_DM_DISC_FLAGS  old_pres_check_flag = (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_CHECKING);
   1968     BOOLEAN             pres_check_event = FALSE;
   1969     BOOLEAN             pres_check_event_processed = FALSE;
   1970     tNFC_DEACTIVATE_DEVT deact;
   1971 
   1972     switch (event)
   1973     {
   1974     case NFA_DM_RF_DEACTIVATE_CMD:
   1975         if (old_pres_check_flag)
   1976         {
   1977             /* presence check is already enabled when deactivate cmd is requested,
   1978              * keep the information in control block to issue it later */
   1979             nfa_dm_cb.presence_check_deact_pending = TRUE;
   1980             nfa_dm_cb.presence_check_deact_type    = p_data->deactivate_type;
   1981         }
   1982         else
   1983         {
   1984             status = nfa_dm_send_deactivate_cmd(p_data->deactivate_type);
   1985         }
   1986 
   1987         break;
   1988     case NFA_DM_RF_DEACTIVATE_RSP:
   1989         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_RSP;
   1990         /* register callback to get interface error NTF */
   1991         NFC_SetStaticRfCback (nfa_dm_disc_data_cback);
   1992 
   1993         if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_NTF))
   1994         {
   1995             /* it's race condition. received deactivate NTF before receiving RSP */
   1996 
   1997             deact.status = NFC_STATUS_OK;
   1998             deact.type   = NFC_DEACTIVATE_TYPE_IDLE;
   1999             deact.is_ntf = TRUE;
   2000             nfa_dm_disc_notify_deactivation (NFA_DM_RF_DEACTIVATE_NTF, (tNFC_DISCOVER*)&deact);
   2001 
   2002             /* NFCC is in IDLE state */
   2003             nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   2004             nfa_dm_start_rf_discover ();
   2005         }
   2006         break;
   2007     case NFA_DM_RF_DEACTIVATE_NTF:
   2008         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_NTF;
   2009 
   2010         nfa_sys_stop_timer (&nfa_dm_cb.disc_cb.tle);
   2011 
   2012         if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP)
   2013         {
   2014             /* it's race condition. received deactivate NTF before receiving RSP */
   2015             /* notify deactivation after receiving deactivate RSP */
   2016             NFA_TRACE_DEBUG0 ("Rx deactivate NTF while waiting for deactivate RSP");
   2017             break;
   2018         }
   2019 
   2020         pres_check_event    = TRUE;
   2021 
   2022         nfa_dm_disc_notify_deactivation (NFA_DM_RF_DEACTIVATE_NTF, &(p_data->nfc_discover));
   2023 
   2024         if (  (p_data->nfc_discover.deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP)
   2025             ||(p_data->nfc_discover.deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP_AF)  )
   2026         {
   2027             nfa_dm_disc_new_state (NFA_DM_RFST_W4_HOST_SELECT);
   2028             if (old_pres_check_flag)
   2029             {
   2030                 pres_check_event_processed  = TRUE;
   2031                 /* process pending deactivate request */
   2032                 if (nfa_dm_cb.presence_check_deact_pending)
   2033                 {
   2034                     /* notify RW module that presence checking is finished */
   2035                     /* if deactivation is pending then deactivate  */
   2036                     nfa_dm_disc_end_presence_check (NFC_STATUS_OK);
   2037 
   2038                     /* Notify NFA RW sub-systems because NFA_DM_RF_DEACTIVATE_RSP will not call this function */
   2039                     nfa_rw_proc_disc_evt (NFA_DM_RF_DISC_DEACTIVATED_EVT, NULL, TRUE);
   2040                 }
   2041                 else
   2042                 {
   2043                     /* Successfully went to sleep mode for presence check */
   2044                     /* Now wake up the tag to see if it is present */
   2045                     NFC_DiscoverySelect (nfa_dm_cb.disc_cb.activated_rf_disc_id,
   2046                                          nfa_dm_cb.disc_cb.activated_protocol,
   2047                                          nfa_dm_cb.disc_cb.activated_rf_interface);
   2048                 }
   2049 
   2050             }
   2051         }
   2052         else if (p_data->nfc_discover.deactivate.type == NFC_DEACTIVATE_TYPE_IDLE)
   2053         {
   2054             nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   2055             nfa_dm_start_rf_discover ();
   2056         }
   2057         else if (p_data->nfc_discover.deactivate.type == NFC_DEACTIVATE_TYPE_DISCOVERY)
   2058         {
   2059             nfa_dm_disc_new_state (NFA_DM_RFST_DISCOVERY);
   2060             if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_STOPPING)
   2061             {
   2062                 /* stop discovery */
   2063                 NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   2064             }
   2065         }
   2066         break;
   2067 
   2068     case NFA_DM_CORE_INTF_ERROR_NTF:
   2069         pres_check_event    = TRUE;
   2070         NFC_Deactivate (NFC_DEACTIVATE_TYPE_DISCOVERY);
   2071         break;
   2072 
   2073     default:
   2074         NFA_TRACE_ERROR0 ("nfa_dm_disc_sm_poll_active (): Unexpected discovery event");
   2075         break;
   2076     }
   2077 
   2078     if (old_pres_check_flag && pres_check_event && !pres_check_event_processed)
   2079     {
   2080         /* performing presence check for unknow protocol and exception conditions happened
   2081          * clear presence check information and report failure */
   2082         nfa_dm_disc_end_presence_check (NFC_STATUS_FAILED);
   2083     }
   2084 }
   2085 
   2086 /*******************************************************************************
   2087 **
   2088 ** Function         nfa_dm_disc_sm_listen_active
   2089 **
   2090 ** Description      Processing discovery events in NFA_DM_RFST_LISTEN_ACTIVE state
   2091 **
   2092 ** Returns          void
   2093 **
   2094 *******************************************************************************/
   2095 static void nfa_dm_disc_sm_listen_active (tNFA_DM_RF_DISC_SM_EVENT event,
   2096                                           tNFA_DM_RF_DISC_DATA     *p_data)
   2097 {
   2098     tNFC_DEACTIVATE_DEVT deact;
   2099 
   2100     switch (event)
   2101     {
   2102     case NFA_DM_RF_DEACTIVATE_CMD:
   2103         nfa_dm_send_deactivate_cmd(p_data->deactivate_type);
   2104         break;
   2105     case NFA_DM_RF_DEACTIVATE_RSP:
   2106         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_RSP;
   2107         if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_NTF))
   2108         {
   2109             /* it's race condition. received deactivate NTF before receiving RSP */
   2110 
   2111             deact.status = NFC_STATUS_OK;
   2112             deact.type   = NFC_DEACTIVATE_TYPE_IDLE;
   2113             deact.is_ntf = TRUE;
   2114             nfa_dm_disc_notify_deactivation (NFA_DM_RF_DEACTIVATE_NTF, (tNFC_DISCOVER*)&deact);
   2115 
   2116             /* NFCC is in IDLE state */
   2117             nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   2118             nfa_dm_start_rf_discover ();
   2119         }
   2120         break;
   2121     case NFA_DM_RF_DEACTIVATE_NTF:
   2122         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_NTF;
   2123 
   2124         nfa_sys_stop_timer (&nfa_dm_cb.disc_cb.tle);
   2125 
   2126         if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP)
   2127         {
   2128             /* it's race condition. received deactivate NTF before receiving RSP */
   2129             /* notify deactivation after receiving deactivate RSP */
   2130             NFA_TRACE_DEBUG0 ("Rx deactivate NTF while waiting for deactivate RSP");
   2131         }
   2132         else
   2133         {
   2134             nfa_dm_disc_notify_deactivation (NFA_DM_RF_DEACTIVATE_NTF, &(p_data->nfc_discover));
   2135 
   2136             if (p_data->nfc_discover.deactivate.type == NFC_DEACTIVATE_TYPE_IDLE)
   2137             {
   2138                 nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   2139                 nfa_dm_start_rf_discover ();
   2140             }
   2141             else if (  (p_data->nfc_discover.deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP)
   2142                      ||(p_data->nfc_discover.deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP_AF)  )
   2143             {
   2144                 nfa_dm_disc_new_state (NFA_DM_RFST_LISTEN_SLEEP);
   2145             }
   2146             else if (p_data->nfc_discover.deactivate.type == NFC_DEACTIVATE_TYPE_DISCOVERY)
   2147             {
   2148                 /* Discovery */
   2149                 nfa_dm_disc_new_state (NFA_DM_RFST_DISCOVERY);
   2150                 if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_STOPPING)
   2151                 {
   2152                     /* stop discovery */
   2153                     NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   2154                 }
   2155             }
   2156         }
   2157         break;
   2158 
   2159     case NFA_DM_CORE_INTF_ERROR_NTF:
   2160         break;
   2161     default:
   2162         NFA_TRACE_ERROR0 ("nfa_dm_disc_sm_listen_active (): Unexpected discovery event");
   2163         break;
   2164     }
   2165 }
   2166 
   2167 /*******************************************************************************
   2168 **
   2169 ** Function         nfa_dm_disc_sm_listen_sleep
   2170 **
   2171 ** Description      Processing discovery events in NFA_DM_RFST_LISTEN_SLEEP state
   2172 **
   2173 ** Returns          void
   2174 **
   2175 *******************************************************************************/
   2176 static void nfa_dm_disc_sm_listen_sleep (tNFA_DM_RF_DISC_SM_EVENT event,
   2177                                          tNFA_DM_RF_DISC_DATA *p_data)
   2178 {
   2179     switch (event)
   2180     {
   2181     case NFA_DM_RF_DEACTIVATE_CMD:
   2182         nfa_dm_send_deactivate_cmd (p_data->deactivate_type);
   2183 
   2184         /* if deactivate type is not discovery then NFCC will not sent deactivation NTF */
   2185         if (p_data->deactivate_type != NFA_DEACTIVATE_TYPE_DISCOVERY)
   2186         {
   2187             nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_NTF;
   2188             nfa_sys_stop_timer (&nfa_dm_cb.disc_cb.tle);
   2189         }
   2190         break;
   2191     case NFA_DM_RF_DEACTIVATE_RSP:
   2192         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_RSP;
   2193         /* if deactivate type in CMD was IDLE */
   2194         if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_NTF))
   2195         {
   2196             nfa_dm_disc_notify_deactivation (NFA_DM_RF_DEACTIVATE_RSP, &(p_data->nfc_discover));
   2197 
   2198             nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   2199             nfa_dm_start_rf_discover ();
   2200         }
   2201         break;
   2202     case NFA_DM_RF_DEACTIVATE_NTF:
   2203         /* clear both W4_RSP and W4_NTF because of race condition between deactivat CMD and link loss */
   2204         nfa_dm_cb.disc_cb.disc_flags &= ~(NFA_DM_DISC_FLAGS_W4_RSP|NFA_DM_DISC_FLAGS_W4_NTF);
   2205         nfa_sys_stop_timer (&nfa_dm_cb.disc_cb.tle);
   2206 
   2207         /* there is no active protocol in this state, so broadcast to all by using NFA_DM_RF_DEACTIVATE_RSP */
   2208         nfa_dm_disc_notify_deactivation (NFA_DM_RF_DEACTIVATE_RSP, &(p_data->nfc_discover));
   2209 
   2210         if (p_data->nfc_discover.deactivate.type == NFC_DEACTIVATE_TYPE_IDLE)
   2211         {
   2212             nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   2213             nfa_dm_start_rf_discover ();
   2214         }
   2215         else if (p_data->nfc_discover.deactivate.type == NFA_DEACTIVATE_TYPE_DISCOVERY)
   2216         {
   2217             nfa_dm_disc_new_state (NFA_DM_RFST_DISCOVERY);
   2218         }
   2219         else
   2220         {
   2221             NFA_TRACE_ERROR0 ("Unexpected deactivation type");
   2222             nfa_dm_disc_new_state (NFA_DM_RFST_IDLE);
   2223             nfa_dm_start_rf_discover ();
   2224         }
   2225         break;
   2226     case NFA_DM_RF_INTF_ACTIVATED_NTF:
   2227         nfa_dm_disc_new_state (NFA_DM_RFST_LISTEN_ACTIVE);
   2228         if (nfa_dm_disc_notify_activation (&(p_data->nfc_discover)) == NFA_STATUS_FAILED)
   2229         {
   2230             NFA_TRACE_DEBUG0 ("Not matched, restart discovery after receiving deactivate ntf");
   2231 
   2232             /* after receiving deactivate event, restart discovery */
   2233             NFC_Deactivate (NFA_DEACTIVATE_TYPE_IDLE);
   2234         }
   2235         break;
   2236     default:
   2237         NFA_TRACE_ERROR0 ("nfa_dm_disc_sm_listen_sleep (): Unexpected discovery event");
   2238         break;
   2239     }
   2240 }
   2241 
   2242 /*******************************************************************************
   2243 **
   2244 ** Function         nfa_dm_disc_sm_lp_listen
   2245 **
   2246 ** Description      Processing discovery events in NFA_DM_RFST_LP_LISTEN state
   2247 **
   2248 ** Returns          void
   2249 **
   2250 *******************************************************************************/
   2251 static void nfa_dm_disc_sm_lp_listen (tNFA_DM_RF_DISC_SM_EVENT event,
   2252                                            tNFA_DM_RF_DISC_DATA *p_data)
   2253 {
   2254     switch (event)
   2255     {
   2256     case NFA_DM_RF_INTF_ACTIVATED_NTF:
   2257         nfa_dm_disc_new_state (NFA_DM_RFST_LP_ACTIVE);
   2258         nfa_dm_disc_notify_activation (&(p_data->nfc_discover));
   2259         break;
   2260 
   2261     default:
   2262         NFA_TRACE_ERROR0 ("nfa_dm_disc_sm_lp_listen (): Unexpected discovery event");
   2263         break;
   2264     }
   2265 }
   2266 
   2267 /*******************************************************************************
   2268 **
   2269 ** Function         nfa_dm_disc_sm_lp_active
   2270 **
   2271 ** Description      Processing discovery events in NFA_DM_RFST_LP_ACTIVE state
   2272 **
   2273 ** Returns          void
   2274 **
   2275 *******************************************************************************/
   2276 static void nfa_dm_disc_sm_lp_active (tNFA_DM_RF_DISC_SM_EVENT event,
   2277                                            tNFA_DM_RF_DISC_DATA *p_data)
   2278 {
   2279     switch (event)
   2280     {
   2281     case NFA_DM_RF_DEACTIVATE_NTF:
   2282         nfa_dm_disc_new_state (NFA_DM_RFST_LP_LISTEN);
   2283         nfa_dm_disc_notify_deactivation (NFA_DM_RF_DEACTIVATE_NTF, &(p_data->nfc_discover));
   2284         break;
   2285     default:
   2286         NFA_TRACE_ERROR0 ("nfa_dm_disc_sm_lp_active (): Unexpected discovery event");
   2287         break;
   2288     }
   2289 }
   2290 
   2291 /*******************************************************************************
   2292 **
   2293 ** Function         nfa_dm_disc_sm_execute
   2294 **
   2295 ** Description      Processing discovery related events
   2296 **
   2297 ** Returns          void
   2298 **
   2299 *******************************************************************************/
   2300 void nfa_dm_disc_sm_execute (tNFA_DM_RF_DISC_SM_EVENT event, tNFA_DM_RF_DISC_DATA *p_data)
   2301 {
   2302 #if (BT_TRACE_VERBOSE == TRUE)
   2303     NFA_TRACE_DEBUG5 ("nfa_dm_disc_sm_execute (): state: %s (%d), event: %s(%d) disc_flags: 0x%x",
   2304                        nfa_dm_disc_state_2_str (nfa_dm_cb.disc_cb.disc_state), nfa_dm_cb.disc_cb.disc_state,
   2305                        nfa_dm_disc_event_2_str (event), event, nfa_dm_cb.disc_cb.disc_flags);
   2306 #else
   2307     NFA_TRACE_DEBUG3 ("nfa_dm_disc_sm_execute(): state: %d, event:%d disc_flags: 0x%x",
   2308                        nfa_dm_cb.disc_cb.disc_state, event, nfa_dm_cb.disc_cb.disc_flags);
   2309 #endif
   2310 
   2311     switch (nfa_dm_cb.disc_cb.disc_state)
   2312     {
   2313     /*  RF Discovery State - Idle */
   2314     case NFA_DM_RFST_IDLE:
   2315         nfa_dm_disc_sm_idle (event, p_data);
   2316         break;
   2317 
   2318     /* RF Discovery State - Discovery */
   2319     case NFA_DM_RFST_DISCOVERY:
   2320         nfa_dm_disc_sm_discovery (event, p_data);
   2321         break;
   2322 
   2323     /*RF Discovery State - Wait for all discoveries */
   2324     case NFA_DM_RFST_W4_ALL_DISCOVERIES:
   2325         nfa_dm_disc_sm_w4_all_discoveries (event, p_data);
   2326         break;
   2327 
   2328     /* RF Discovery State - Wait for host selection */
   2329     case NFA_DM_RFST_W4_HOST_SELECT:
   2330         nfa_dm_disc_sm_w4_host_select (event, p_data);
   2331         break;
   2332 
   2333     /* RF Discovery State - Poll mode activated */
   2334     case NFA_DM_RFST_POLL_ACTIVE:
   2335         nfa_dm_disc_sm_poll_active (event, p_data);
   2336         break;
   2337 
   2338     /* RF Discovery State - listen mode activated */
   2339     case NFA_DM_RFST_LISTEN_ACTIVE:
   2340         nfa_dm_disc_sm_listen_active (event, p_data);
   2341         break;
   2342 
   2343     /* RF Discovery State - listen mode sleep */
   2344     case NFA_DM_RFST_LISTEN_SLEEP:
   2345         nfa_dm_disc_sm_listen_sleep (event, p_data);
   2346         break;
   2347 
   2348     /* Listening in Low Power mode    */
   2349     case NFA_DM_RFST_LP_LISTEN:
   2350         nfa_dm_disc_sm_lp_listen (event, p_data);
   2351         break;
   2352 
   2353     /* Activated in Low Power mode    */
   2354     case NFA_DM_RFST_LP_ACTIVE:
   2355         nfa_dm_disc_sm_lp_active (event, p_data);
   2356         break;
   2357     }
   2358 #if (BT_TRACE_VERBOSE == TRUE)
   2359     NFA_TRACE_DEBUG3 ("nfa_dm_disc_sm_execute (): new state: %s (%d), disc_flags: 0x%x",
   2360                        nfa_dm_disc_state_2_str (nfa_dm_cb.disc_cb.disc_state), nfa_dm_cb.disc_cb.disc_state,
   2361                        nfa_dm_cb.disc_cb.disc_flags);
   2362 #else
   2363     NFA_TRACE_DEBUG2 ("nfa_dm_disc_sm_execute(): new state: %d,  disc_flags: 0x%x",
   2364                        nfa_dm_cb.disc_cb.disc_state, nfa_dm_cb.disc_cb.disc_flags);
   2365 #endif
   2366 }
   2367 
   2368 /*******************************************************************************
   2369 **
   2370 ** Function         nfa_dm_add_rf_discover
   2371 **
   2372 ** Description      Add discovery configuration and callback function
   2373 **
   2374 ** Returns          valid handle if success
   2375 **
   2376 *******************************************************************************/
   2377 tNFA_HANDLE nfa_dm_add_rf_discover (tNFA_DM_DISC_TECH_PROTO_MASK disc_mask,
   2378                                     tNFA_DM_DISC_HOST_ID         host_id,
   2379                                     tNFA_DISCOVER_CBACK         *p_disc_cback)
   2380 {
   2381     UINT8       xx;
   2382 
   2383     NFA_TRACE_DEBUG1 ("nfa_dm_add_rf_discover () disc_mask=0x%x", disc_mask);
   2384 
   2385     for (xx = 0; xx < NFA_DM_DISC_NUM_ENTRIES; xx++)
   2386     {
   2387         if (!nfa_dm_cb.disc_cb.entry[xx].in_use)
   2388         {
   2389             nfa_dm_cb.disc_cb.entry[xx].in_use              = TRUE;
   2390             nfa_dm_cb.disc_cb.entry[xx].requested_disc_mask = disc_mask;
   2391             nfa_dm_cb.disc_cb.entry[xx].host_id             = host_id;
   2392             nfa_dm_cb.disc_cb.entry[xx].p_disc_cback        = p_disc_cback;
   2393             nfa_dm_cb.disc_cb.entry[xx].disc_flags          = NFA_DM_DISC_FLAGS_NOTIFY;
   2394             return xx;
   2395         }
   2396     }
   2397 
   2398     return NFA_HANDLE_INVALID;
   2399 }
   2400 
   2401 /*******************************************************************************
   2402 **
   2403 ** Function         nfa_dm_start_excl_discovery
   2404 **
   2405 ** Description      Start exclusive RF discovery
   2406 **
   2407 ** Returns          void
   2408 **
   2409 *******************************************************************************/
   2410 void nfa_dm_start_excl_discovery (tNFA_TECHNOLOGY_MASK poll_tech_mask,
   2411                                   tNFA_LISTEN_CFG *p_listen_cfg,
   2412                                   tNFA_DISCOVER_CBACK  *p_disc_cback)
   2413 {
   2414     tNFA_DM_DISC_TECH_PROTO_MASK poll_disc_mask = 0;
   2415 
   2416     NFA_TRACE_DEBUG0 ("nfa_dm_start_excl_discovery ()");
   2417 
   2418     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_A)
   2419     {
   2420         poll_disc_mask |= NFA_DM_DISC_MASK_PA_T1T;
   2421         poll_disc_mask |= NFA_DM_DISC_MASK_PA_T2T;
   2422         poll_disc_mask |= NFA_DM_DISC_MASK_PA_ISO_DEP;
   2423         poll_disc_mask |= NFA_DM_DISC_MASK_PA_NFC_DEP;
   2424         poll_disc_mask |= NFA_DM_DISC_MASK_P_LEGACY;
   2425     }
   2426     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_A_ACTIVE)
   2427     {
   2428         poll_disc_mask |= NFA_DM_DISC_MASK_PAA_NFC_DEP;
   2429     }
   2430     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_B)
   2431     {
   2432         poll_disc_mask |= NFA_DM_DISC_MASK_PB_ISO_DEP;
   2433     }
   2434     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_F)
   2435     {
   2436         poll_disc_mask |= NFA_DM_DISC_MASK_PF_T3T;
   2437         poll_disc_mask |= NFA_DM_DISC_MASK_PF_NFC_DEP;
   2438     }
   2439     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_F_ACTIVE)
   2440     {
   2441         poll_disc_mask |= NFA_DM_DISC_MASK_PFA_NFC_DEP;
   2442     }
   2443     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_ISO15693)
   2444     {
   2445         poll_disc_mask |= NFA_DM_DISC_MASK_P_ISO15693;
   2446     }
   2447     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_B_PRIME)
   2448     {
   2449         poll_disc_mask |= NFA_DM_DISC_MASK_P_B_PRIME;
   2450     }
   2451     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_KOVIO)
   2452     {
   2453         poll_disc_mask |= NFA_DM_DISC_MASK_P_KOVIO;
   2454     }
   2455 
   2456     nfa_dm_cb.disc_cb.excl_disc_entry.in_use              = TRUE;
   2457     nfa_dm_cb.disc_cb.excl_disc_entry.requested_disc_mask = poll_disc_mask;
   2458     nfa_dm_cb.disc_cb.excl_disc_entry.host_id             = NFA_DM_DISC_HOST_ID_DH;
   2459     nfa_dm_cb.disc_cb.excl_disc_entry.p_disc_cback        = p_disc_cback;
   2460     nfa_dm_cb.disc_cb.excl_disc_entry.disc_flags          = NFA_DM_DISC_FLAGS_NOTIFY;
   2461 
   2462     memcpy (&nfa_dm_cb.disc_cb.excl_listen_config, p_listen_cfg, sizeof (tNFA_LISTEN_CFG));
   2463 
   2464     nfa_dm_disc_sm_execute (NFA_DM_RF_DISCOVER_CMD, NULL);
   2465 }
   2466 
   2467 /*******************************************************************************
   2468 **
   2469 ** Function         nfa_dm_stop_excl_discovery
   2470 **
   2471 ** Description      Stop exclusive RF discovery
   2472 **
   2473 ** Returns          void
   2474 **
   2475 *******************************************************************************/
   2476 void nfa_dm_stop_excl_discovery (void)
   2477 {
   2478     NFA_TRACE_DEBUG0 ("nfa_dm_stop_excl_discovery ()");
   2479 
   2480     nfa_dm_cb.disc_cb.excl_disc_entry.in_use       = FALSE;
   2481     nfa_dm_cb.disc_cb.excl_disc_entry.p_disc_cback = NULL;
   2482 }
   2483 
   2484 /*******************************************************************************
   2485 **
   2486 ** Function         nfa_dm_delete_rf_discover
   2487 **
   2488 ** Description      Remove discovery configuration and callback function
   2489 **
   2490 ** Returns          void
   2491 **
   2492 *******************************************************************************/
   2493 void nfa_dm_delete_rf_discover (tNFA_HANDLE handle)
   2494 {
   2495     NFA_TRACE_DEBUG1 ("nfa_dm_delete_rf_discover () handle=0x%x", handle);
   2496 
   2497     if (handle < NFA_DM_DISC_NUM_ENTRIES)
   2498     {
   2499         nfa_dm_cb.disc_cb.entry[handle].in_use = FALSE;
   2500     }
   2501     else
   2502     {
   2503         NFA_TRACE_ERROR0 ("Invalid discovery handle");
   2504     }
   2505 }
   2506 
   2507 /*******************************************************************************
   2508 **
   2509 ** Function         nfa_dm_rf_discover_select
   2510 **
   2511 ** Description      Select target, protocol and RF interface
   2512 **
   2513 ** Returns          void
   2514 **
   2515 *******************************************************************************/
   2516 void nfa_dm_rf_discover_select (UINT8             rf_disc_id,
   2517                                        tNFA_NFC_PROTOCOL protocol,
   2518                                        tNFA_INTF_TYPE    rf_interface)
   2519 {
   2520     tNFA_DM_DISC_SELECT_PARAMS select_params;
   2521     tNFA_CONN_EVT_DATA conn_evt;
   2522 
   2523     NFA_TRACE_DEBUG3 ("nfa_dm_disc_select () rf_disc_id:0x%X, protocol:0x%X, rf_interface:0x%X",
   2524                        rf_disc_id, protocol, rf_interface);
   2525 
   2526     if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_W4_HOST_SELECT)
   2527     {
   2528         /* state is OK: notify the status when the response is received from NFCC */
   2529         select_params.rf_disc_id   = rf_disc_id;
   2530         select_params.protocol     = protocol;
   2531         select_params.rf_interface = rf_interface;
   2532 
   2533         nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_NOTIFY;
   2534         nfa_dm_disc_sm_execute (NFA_DM_RF_DISCOVER_SELECT_CMD, (tNFA_DM_RF_DISC_DATA *) &select_params);
   2535     }
   2536     else
   2537     {
   2538         /* Wrong state: notify failed status right away */
   2539         conn_evt.status = NFA_STATUS_FAILED;
   2540         nfa_dm_conn_cback_event_notify (NFA_SELECT_RESULT_EVT, &conn_evt);
   2541     }
   2542 }
   2543 
   2544 /*******************************************************************************
   2545 **
   2546 ** Function         nfa_dm_rf_deactivate
   2547 **
   2548 ** Description      Deactivate NFC link
   2549 **
   2550 ** Returns          NFA_STATUS_OK if success
   2551 **
   2552 *******************************************************************************/
   2553 tNFA_STATUS nfa_dm_rf_deactivate (tNFA_DEACTIVATE_TYPE deactivate_type)
   2554 {
   2555     NFA_TRACE_DEBUG1 ("nfa_dm_rf_deactivate () deactivate_type:0x%X", deactivate_type);
   2556 
   2557     if (deactivate_type == NFA_DEACTIVATE_TYPE_SLEEP)
   2558     {
   2559         if (nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_NFC_DEP)
   2560             deactivate_type = NFC_DEACTIVATE_TYPE_SLEEP_AF;
   2561         else
   2562             deactivate_type = NFC_DEACTIVATE_TYPE_SLEEP;
   2563     }
   2564 
   2565     if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_IDLE)
   2566     {
   2567         return NFA_STATUS_FAILED;
   2568     }
   2569     else
   2570     {
   2571         nfa_dm_disc_sm_execute (NFA_DM_RF_DEACTIVATE_CMD, (tNFA_DM_RF_DISC_DATA *) &deactivate_type);
   2572         return NFA_STATUS_OK;
   2573     }
   2574 }
   2575 
   2576 #if (BT_TRACE_VERBOSE == TRUE)
   2577 /*******************************************************************************
   2578 **
   2579 ** Function         nfa_dm_disc_state_2_str
   2580 **
   2581 ** Description      convert nfc discovery state to string
   2582 **
   2583 *******************************************************************************/
   2584 static char *nfa_dm_disc_state_2_str (UINT8 state)
   2585 {
   2586     switch (state)
   2587     {
   2588     case NFA_DM_RFST_IDLE:
   2589         return "IDLE";
   2590 
   2591     case NFA_DM_RFST_DISCOVERY:
   2592         return "DISCOVERY";
   2593 
   2594     case NFA_DM_RFST_W4_ALL_DISCOVERIES:
   2595         return "W4_ALL_DISCOVERIES";
   2596 
   2597     case NFA_DM_RFST_W4_HOST_SELECT:
   2598         return "W4_HOST_SELECT";
   2599 
   2600     case NFA_DM_RFST_POLL_ACTIVE:
   2601         return "POLL_ACTIVE";
   2602 
   2603     case NFA_DM_RFST_LISTEN_ACTIVE:
   2604         return "LISTEN_ACTIVE";
   2605 
   2606     case NFA_DM_RFST_LISTEN_SLEEP:
   2607         return "LISTEN_SLEEP";
   2608 
   2609     case NFA_DM_RFST_LP_LISTEN:
   2610         return "LP_LISTEN";
   2611 
   2612     case NFA_DM_RFST_LP_ACTIVE:
   2613         return "LP_ACTIVE";
   2614     }
   2615     return "Unknown";
   2616 }
   2617 
   2618 /*******************************************************************************
   2619 **
   2620 ** Function         nfa_dm_disc_event_2_str
   2621 **
   2622 ** Description      convert nfc discovery RSP/NTF to string
   2623 **
   2624 *******************************************************************************/
   2625 static char *nfa_dm_disc_event_2_str (UINT8 event)
   2626 {
   2627     switch (event)
   2628     {
   2629     case NFA_DM_RF_DISCOVER_CMD:
   2630         return "DISCOVER_CMD";
   2631 
   2632     case NFA_DM_RF_DISCOVER_RSP:
   2633         return "DISCOVER_RSP";
   2634 
   2635     case NFA_DM_RF_DISCOVER_NTF:
   2636         return "DISCOVER_NTF";
   2637 
   2638     case NFA_DM_RF_DISCOVER_SELECT_CMD:
   2639         return "SELECT_CMD";
   2640 
   2641     case NFA_DM_RF_DISCOVER_SELECT_RSP:
   2642         return "SELECT_RSP";
   2643 
   2644     case NFA_DM_RF_INTF_ACTIVATED_NTF:
   2645         return "ACTIVATED_NTF";
   2646 
   2647     case NFA_DM_RF_DEACTIVATE_CMD:
   2648         return "DEACTIVATE_CMD";
   2649 
   2650     case NFA_DM_RF_DEACTIVATE_RSP:
   2651         return "DEACTIVATE_RSP";
   2652 
   2653     case NFA_DM_RF_DEACTIVATE_NTF:
   2654         return "DEACTIVATE_NTF";
   2655 
   2656     case NFA_DM_LP_LISTEN_CMD:
   2657         return "NFA_DM_LP_LISTEN_CMD";
   2658 
   2659     case NFA_DM_CORE_INTF_ERROR_NTF:
   2660         return "INTF_ERROR_NTF";
   2661 
   2662     }
   2663     return "Unknown";
   2664 }
   2665 #endif /* BT_TRACE_VERBOSE */
   2666