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