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