• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2010-2013 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 
20 /******************************************************************************
21  *
22  *  This is the main implementation file for the NFA device manager.
23  *
24  ******************************************************************************/
25 
26 #include <string.h>
27 #include "nfa_api.h"
28 #include "nfa_sys.h"
29 #include "nfa_dm_int.h"
30 #include "nfa_sys_int.h"
31 
32 
33 /*****************************************************************************
34 ** Constants and types
35 *****************************************************************************/
36 static const tNFA_SYS_REG nfa_dm_sys_reg =
37 {
38     nfa_dm_sys_enable,
39     nfa_dm_evt_hdlr,
40     nfa_dm_sys_disable,
41     nfa_dm_proc_nfcc_power_mode
42 };
43 
44 
45 tNFA_DM_CB  nfa_dm_cb = {FALSE};
46 
47 
48 #define NFA_DM_NUM_ACTIONS  (NFA_DM_MAX_EVT & 0x00ff)
49 
50 /* type for action functions */
51 typedef BOOLEAN (*tNFA_DM_ACTION) (tNFA_DM_MSG *p_data);
52 
53 /* action function list */
54 const tNFA_DM_ACTION nfa_dm_action[] =
55 {
56     /* device manager local device API events */
57     nfa_dm_enable,                      /* NFA_DM_API_ENABLE_EVT                */
58     nfa_dm_disable,                     /* NFA_DM_API_DISABLE_EVT               */
59     nfa_dm_set_config,                  /* NFA_DM_API_SET_CONFIG_EVT            */
60     nfa_dm_get_config,                  /* NFA_DM_API_GET_CONFIG_EVT            */
61     nfa_dm_act_request_excl_rf_ctrl,    /* NFA_DM_API_REQUEST_EXCL_RF_CTRL_EVT  */
62     nfa_dm_act_release_excl_rf_ctrl,    /* NFA_DM_API_RELEASE_EXCL_RF_CTRL_EVT  */
63     nfa_dm_act_enable_polling,          /* NFA_DM_API_ENABLE_POLLING_EVT        */
64     nfa_dm_act_disable_polling,         /* NFA_DM_API_DISABLE_POLLING_EVT       */
65     nfa_dm_act_send_raw_frame,          /* NFA_DM_API_RAW_FRAME_EVT             */
66     nfa_dm_set_p2p_listen_tech,         /* NFA_DM_API_SET_P2P_LISTEN_TECH_EVT   */
67     nfa_dm_act_start_rf_discovery,      /* NFA_DM_API_START_RF_DISCOVERY_EVT    */
68     nfa_dm_act_stop_rf_discovery,       /* NFA_DM_API_STOP_RF_DISCOVERY_EVT     */
69     nfa_dm_act_set_rf_disc_duration,    /* NFA_DM_API_SET_RF_DISC_DURATION_EVT  */
70     nfa_dm_act_select,                  /* NFA_DM_API_SELECT_EVT                */
71     nfa_dm_act_update_rf_params,        /* NFA_DM_API_UPDATE_RF_PARAMS_EVT      */
72     nfa_dm_act_deactivate,              /* NFA_DM_API_DEACTIVATE_EVT            */
73     nfa_dm_act_power_off_sleep,         /* NFA_DM_API_POWER_OFF_SLEEP_EVT       */
74     nfa_dm_ndef_reg_hdlr,               /* NFA_DM_API_REG_NDEF_HDLR_EVT         */
75     nfa_dm_ndef_dereg_hdlr,             /* NFA_DM_API_DEREG_NDEF_HDLR_EVT       */
76     nfa_dm_act_reg_vsc,                 /* NFA_DM_API_REG_VSC_EVT               */
77     nfa_dm_act_send_vsc,                /* NFA_DM_API_SEND_VSC_EVT              */
78     nfa_dm_act_disable_timeout          /* NFA_DM_TIMEOUT_DISABLE_EVT           */
79 };
80 
81 /*****************************************************************************
82 ** Local function prototypes
83 *****************************************************************************/
84 #if (BT_TRACE_VERBOSE == TRUE)
85 static char *nfa_dm_evt_2_str (UINT16 event);
86 #endif
87 /*******************************************************************************
88 **
89 ** Function         nfa_dm_init
90 **
91 ** Description      Initialises the NFC device manager
92 **
93 ** Returns          void
94 **
95 *******************************************************************************/
nfa_dm_init(void)96 void nfa_dm_init (void)
97 {
98     NFA_TRACE_DEBUG0 ("nfa_dm_init ()");
99     memset (&nfa_dm_cb, 0, sizeof (tNFA_DM_CB));
100     nfa_dm_cb.poll_disc_handle = NFA_HANDLE_INVALID;
101     nfa_dm_cb.disc_cb.disc_duration = NFA_DM_DISC_DURATION_POLL;
102     nfa_dm_cb.nfcc_pwr_mode    = NFA_DM_PWR_MODE_FULL;
103 
104     /* register message handler on NFA SYS */
105     nfa_sys_register (NFA_ID_DM, &nfa_dm_sys_reg);
106 }
107 
108 /*******************************************************************************
109 **
110 ** Function         nfa_dm_evt_hdlr
111 **
112 ** Description      Event handling function for DM
113 **
114 **
115 ** Returns          void
116 **
117 *******************************************************************************/
nfa_dm_evt_hdlr(BT_HDR * p_msg)118 BOOLEAN nfa_dm_evt_hdlr (BT_HDR *p_msg)
119 {
120     BOOLEAN freebuf = TRUE;
121     UINT16  event = p_msg->event & 0x00ff;
122 
123 #if (BT_TRACE_VERBOSE == TRUE)
124     NFA_TRACE_EVENT2 ("nfa_dm_evt_hdlr event: %s (0x%02x)", nfa_dm_evt_2_str (event), event);
125 #else
126     NFA_TRACE_EVENT1 ("nfa_dm_evt_hdlr event: 0x%x", event);
127 #endif
128 
129     /* execute action functions */
130     if (event < NFA_DM_NUM_ACTIONS)
131     {
132         freebuf = (*nfa_dm_action[event]) ((tNFA_DM_MSG*) p_msg);
133     }
134     return freebuf;
135 }
136 
137 /*******************************************************************************
138 **
139 ** Function         nfa_dm_sys_disable
140 **
141 ** Description      This function is called after all subsystems have been disabled.
142 **
143 ** Returns          void
144 **
145 *******************************************************************************/
nfa_dm_sys_disable(void)146 void nfa_dm_sys_disable (void)
147 {
148     /* Disable the DM sub-system */
149     /* If discovery state is not IDLE or DEACTIVATED and graceful disable, */
150     /* then we need to deactivate link or stop discovery                   */
151 
152     if (nfa_sys_is_graceful_disable ())
153     {
154         if (  (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_IDLE)
155             &&((nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_DISABLING) == 0)  )
156         {
157             /* discovery is not started */
158             nfa_dm_disable_complete ();
159         }
160         else
161         {
162             /* probably waiting to be disabled */
163             NFA_TRACE_WARNING2 ("DM disc_state state = %d disc_flags:0x%x", nfa_dm_cb.disc_cb.disc_state, nfa_dm_cb.disc_cb.disc_flags);
164         }
165 
166     }
167     else
168     {
169         nfa_dm_disable_complete ();
170     }
171 }
172 
173 /*******************************************************************************
174 **
175 ** Function         nfa_dm_is_protocol_supported
176 **
177 ** Description      Check if protocol is supported by RW module
178 **
179 ** Returns          TRUE if protocol is supported by NFA
180 **
181 *******************************************************************************/
nfa_dm_is_protocol_supported(tNFC_PROTOCOL protocol,UINT8 sel_res)182 BOOLEAN nfa_dm_is_protocol_supported (tNFC_PROTOCOL protocol, UINT8 sel_res)
183 {
184     return (  (protocol == NFC_PROTOCOL_T1T)
185             ||((protocol == NFC_PROTOCOL_T2T) && (sel_res == NFC_SEL_RES_NFC_FORUM_T2T))
186             ||(protocol == NFC_PROTOCOL_T3T)
187             ||(protocol == NFC_PROTOCOL_ISO_DEP)
188             ||(protocol == NFC_PROTOCOL_NFC_DEP)
189             ||(protocol == NFC_PROTOCOL_15693)  );
190 }
191 /*******************************************************************************
192 **
193 ** Function         nfa_dm_is_active
194 **
195 ** Description      check if all modules of NFA is done with enable process and
196 **                  NFA is not restoring NFCC.
197 **
198 ** Returns          TRUE, if NFA_DM_ENABLE_EVT is reported and it is not restoring NFCC
199 **
200 *******************************************************************************/
nfa_dm_is_active(void)201 BOOLEAN nfa_dm_is_active (void)
202 {
203     if (  (nfa_dm_cb.flags  & NFA_DM_FLAGS_DM_IS_ACTIVE)
204         &&((nfa_dm_cb.flags & (NFA_DM_FLAGS_ENABLE_EVT_PEND | NFA_DM_FLAGS_NFCC_IS_RESTORING)) == 0)  )
205     {
206         return TRUE;
207     }
208     else
209         return FALSE;
210 }
211 /*******************************************************************************
212 **
213 ** Function         nfa_dm_check_set_config
214 **
215 ** Description      Update config parameters only if it's different from NFCC
216 **
217 **
218 ** Returns          tNFA_STATUS
219 **
220 *******************************************************************************/
nfa_dm_check_set_config(UINT8 tlv_list_len,UINT8 * p_tlv_list,BOOLEAN app_init)221 tNFA_STATUS nfa_dm_check_set_config (UINT8 tlv_list_len, UINT8 *p_tlv_list, BOOLEAN app_init)
222 {
223     UINT8 type, len, *p_value, *p_stored, max_len;
224     UINT8 xx = 0, updated_len = 0, *p_cur_len;
225     BOOLEAN update;
226     tNFC_STATUS nfc_status;
227     UINT32 cur_bit;
228 
229     NFA_TRACE_DEBUG0 ("nfa_dm_check_set_config ()");
230 
231     /* We only allow 32 pending SET_CONFIGs */
232     if (nfa_dm_cb.setcfg_pending_num >= NFA_DM_SETCONFIG_PENDING_MAX)
233     {
234         NFA_TRACE_ERROR0 ("nfa_dm_check_set_config () error: pending number of SET_CONFIG exceeded");
235         return NFA_STATUS_FAILED;
236     }
237 
238     while (tlv_list_len - xx >= 2) /* at least type and len */
239     {
240         update  = FALSE;
241         type    = *(p_tlv_list + xx);
242         len     = *(p_tlv_list + xx + 1);
243         p_value = p_tlv_list + xx + 2;
244         p_cur_len = NULL;
245 
246         switch (type)
247         {
248         case NFC_PMID_TOTAL_DURATION:
249             p_stored = nfa_dm_cb.params.total_duration;
250             max_len  = NCI_PARAM_LEN_TOTAL_DURATION;
251             break;
252 
253         /*
254         **  Listen A Configuration
255         */
256         case NFC_PMID_LA_BIT_FRAME_SDD:
257             p_stored  = nfa_dm_cb.params.la_bit_frame_sdd;
258             max_len   = NCI_PARAM_LEN_LA_BIT_FRAME_SDD;
259             p_cur_len = &nfa_dm_cb.params.la_bit_frame_sdd_len;
260             break;
261         case NFC_PMID_LA_PLATFORM_CONFIG:
262             p_stored  = nfa_dm_cb.params.la_platform_config;
263             max_len   = NCI_PARAM_LEN_LA_PLATFORM_CONFIG;
264             p_cur_len = &nfa_dm_cb.params.la_platform_config_len;
265             break;
266         case NFC_PMID_LA_SEL_INFO:
267             p_stored  = nfa_dm_cb.params.la_sel_info;
268             max_len   = NCI_PARAM_LEN_LA_SEL_INFO;
269             p_cur_len = &nfa_dm_cb.params.la_sel_info_len;
270             break;
271         case NFC_PMID_LA_NFCID1:
272             p_stored  = nfa_dm_cb.params.la_nfcid1;
273             max_len   = NCI_NFCID1_MAX_LEN;
274             p_cur_len = &nfa_dm_cb.params.la_nfcid1_len;
275             break;
276         case NFC_PMID_LA_HIST_BY:
277             p_stored  = nfa_dm_cb.params.la_hist_by;
278             max_len   = NCI_MAX_HIS_BYTES_LEN;
279             p_cur_len = &nfa_dm_cb.params.la_hist_by_len;
280             break;
281 
282         /*
283         **  Listen B Configuration
284         */
285         case NFC_PMID_LB_SENSB_INFO:
286             p_stored  = nfa_dm_cb.params.lb_sensb_info;
287             max_len   = NCI_PARAM_LEN_LB_SENSB_INFO;
288             p_cur_len = &nfa_dm_cb.params.lb_sensb_info_len;
289             break;
290         case NFC_PMID_LB_NFCID0:
291             p_stored  = nfa_dm_cb.params.lb_nfcid0;
292             max_len   = NCI_PARAM_LEN_LB_NFCID0;
293             p_cur_len = &nfa_dm_cb.params.lb_nfcid0_len;
294             break;
295         case NFC_PMID_LB_APPDATA:
296             p_stored  = nfa_dm_cb.params.lb_appdata;
297             max_len   = NCI_PARAM_LEN_LB_APPDATA;
298             p_cur_len = &nfa_dm_cb.params.lb_appdata_len;
299             break;
300         case NFC_PMID_LB_ADC_FO:
301             p_stored  = nfa_dm_cb.params.lb_adc_fo;
302             max_len   = NCI_PARAM_LEN_LB_ADC_FO;
303             p_cur_len = &nfa_dm_cb.params.lb_adc_fo_len;
304             break;
305         case NFC_PMID_LB_H_INFO:
306             p_stored  = nfa_dm_cb.params.lb_h_info;
307             max_len   = NCI_MAX_ATTRIB_LEN;
308             p_cur_len = &nfa_dm_cb.params.lb_h_info_len;
309             break;
310 
311         /*
312         **  Listen F Configuration
313         */
314         case NFC_PMID_LF_PROTOCOL:
315             p_stored  = nfa_dm_cb.params.lf_protocol;
316             max_len   = NCI_PARAM_LEN_LF_PROTOCOL;
317             p_cur_len = &nfa_dm_cb.params.lf_protocol_len;
318             break;
319         case NFC_PMID_LF_T3T_FLAGS2:
320             p_stored  = nfa_dm_cb.params.lf_t3t_flags2;
321             max_len   = NCI_PARAM_LEN_LF_T3T_FLAGS2;
322             p_cur_len = &nfa_dm_cb.params.lf_t3t_flags2_len;
323             break;
324         case NFC_PMID_LF_T3T_PMM:
325             p_stored = nfa_dm_cb.params.lf_t3t_pmm;
326             max_len  = NCI_PARAM_LEN_LF_T3T_PMM;
327             break;
328 
329         /*
330         **  ISO-DEP and NFC-DEP Configuration
331         */
332         case NFC_PMID_FWI:
333             p_stored = nfa_dm_cb.params.fwi;
334             max_len  = NCI_PARAM_LEN_FWI;
335             break;
336         case NFC_PMID_WT:
337             p_stored = nfa_dm_cb.params.wt;
338             max_len  = NCI_PARAM_LEN_WT;
339             break;
340         case NFC_PMID_ATR_REQ_GEN_BYTES:
341             p_stored  = nfa_dm_cb.params.atr_req_gen_bytes;
342             max_len   = NCI_MAX_GEN_BYTES_LEN;
343             p_cur_len = &nfa_dm_cb.params.atr_req_gen_bytes_len;
344             break;
345         case NFC_PMID_ATR_RES_GEN_BYTES:
346             p_stored  = nfa_dm_cb.params.atr_res_gen_bytes;
347             max_len   = NCI_MAX_GEN_BYTES_LEN;
348             p_cur_len = &nfa_dm_cb.params.atr_res_gen_bytes_len;
349             break;
350         default:
351             /*
352             **  Listen F Configuration
353             */
354             if ((type >= NFC_PMID_LF_T3T_ID1) && (type < NFC_PMID_LF_T3T_ID1 + NFA_CE_LISTEN_INFO_MAX))
355             {
356                 p_stored = nfa_dm_cb.params.lf_t3t_id[type - NFC_PMID_LF_T3T_ID1];
357                 max_len  = NCI_PARAM_LEN_LF_T3T_ID;
358             }
359             else
360             {
361                 /* we don't stored this config items */
362                 update   = TRUE;
363                 p_stored = NULL;
364             }
365             break;
366         }
367 
368         if ((p_stored)&&(len <= max_len))
369         {
370             if (p_cur_len)
371             {
372                 if (*p_cur_len != len)
373                 {
374                     *p_cur_len = len;
375                     update = TRUE;
376                 }
377                 else if (memcmp (p_value, p_stored, len))
378                 {
379                     update = TRUE;
380                 }
381             }
382             else if (len == max_len)  /* fixed length */
383             {
384                 if (memcmp (p_value, p_stored, len))
385                 {
386                     update = TRUE;
387                 }
388             }
389         }
390 
391         if (update)
392         {
393             /* we don't store this type */
394             if (p_stored)
395             {
396                 memcpy (p_stored, p_value, len);
397             }
398 
399             /* If need to change TLV in the original list. (Do not modify list if app_init) */
400             if ((updated_len != xx) && (!app_init))
401             {
402                 memcpy (p_tlv_list + updated_len, p_tlv_list + xx, (len + 2));
403             }
404             updated_len += (len + 2);
405         }
406         xx += len + 2;  /* move to next TLV */
407     }
408 
409     /* If any TVLs to update, or if the SetConfig was initiated by the application, then send the SET_CONFIG command */
410     if (updated_len || app_init)
411     {
412         if ((nfc_status = NFC_SetConfig (updated_len, p_tlv_list)) == NFC_STATUS_OK)
413         {
414             /* Keep track of whether we will need to notify NFA_DM_SET_CONFIG_EVT on NFC_SET_CONFIG_REVT */
415 
416             /* Get the next available bit offset for this setconfig (based on how many SetConfigs are outstanding) */
417             cur_bit = (UINT32) (1 << nfa_dm_cb.setcfg_pending_num);
418 
419             /* If setconfig is due to NFA_SetConfig: then set the bit (NFA_DM_SET_CONFIG_EVT needed on NFC_SET_CONFIG_REVT) */
420             if (app_init)
421             {
422                 nfa_dm_cb.setcfg_pending_mask |= cur_bit;
423             }
424             /* Otherwise setconfig is internal: clear the bit (NFA_DM_SET_CONFIG_EVT not needed on NFC_SET_CONFIG_REVT) */
425             else
426             {
427                 nfa_dm_cb.setcfg_pending_mask &= ~cur_bit;
428             }
429 
430             /* Increment setcfg_pending counter */
431             nfa_dm_cb.setcfg_pending_num++;
432         }
433         return (nfc_status);
434 
435     }
436     else
437     {
438         return NFA_STATUS_OK;
439     }
440 }
441 
442 #if (BT_TRACE_VERBOSE == TRUE)
443 /*******************************************************************************
444 **
445 ** Function         nfa_dm_nfc_revt_2_str
446 **
447 ** Description      convert nfc revt to string
448 **
449 *******************************************************************************/
nfa_dm_evt_2_str(UINT16 event)450 static char *nfa_dm_evt_2_str (UINT16 event)
451 {
452     switch (NFA_SYS_EVT_START (NFA_ID_DM) | event)
453     {
454     case NFA_DM_API_ENABLE_EVT:
455         return "NFA_DM_API_ENABLE_EVT";
456 
457     case NFA_DM_API_DISABLE_EVT:
458         return "NFA_DM_API_DISABLE_EVT";
459 
460     case NFA_DM_API_SET_CONFIG_EVT:
461         return "NFA_DM_API_SET_CONFIG_EVT";
462 
463     case NFA_DM_API_GET_CONFIG_EVT:
464         return "NFA_DM_API_GET_CONFIG_EVT";
465 
466     case NFA_DM_API_REQUEST_EXCL_RF_CTRL_EVT:
467         return "NFA_DM_API_REQUEST_EXCL_RF_CTRL_EVT";
468 
469     case NFA_DM_API_RELEASE_EXCL_RF_CTRL_EVT:
470         return "NFA_DM_API_RELEASE_EXCL_RF_CTRL_EVT";
471 
472     case NFA_DM_API_ENABLE_POLLING_EVT:
473         return "NFA_DM_API_ENABLE_POLLING_EVT";
474 
475     case NFA_DM_API_DISABLE_POLLING_EVT:
476         return "NFA_DM_API_DISABLE_POLLING_EVT";
477 
478     case NFA_DM_API_RAW_FRAME_EVT:
479         return "NFA_DM_API_RAW_FRAME_EVT";
480 
481     case NFA_DM_API_SET_P2P_LISTEN_TECH_EVT:
482         return "NFA_DM_API_SET_P2P_LISTEN_TECH_EVT";
483 
484     case NFA_DM_API_START_RF_DISCOVERY_EVT:
485         return "NFA_DM_API_START_RF_DISCOVERY_EVT";
486 
487     case NFA_DM_API_STOP_RF_DISCOVERY_EVT:
488         return "NFA_DM_API_STOP_RF_DISCOVERY_EVT";
489 
490     case NFA_DM_API_SET_RF_DISC_DURATION_EVT:
491         return "NFA_DM_API_SET_RF_DISC_DURATION_EVT";
492 
493     case NFA_DM_API_SELECT_EVT:
494         return "NFA_DM_API_SELECT_EVT";
495 
496     case NFA_DM_API_UPDATE_RF_PARAMS_EVT:
497         return "NFA_DM_API_UPDATE_RF_PARAMS_EVT";
498 
499     case NFA_DM_API_DEACTIVATE_EVT:
500         return "NFA_DM_API_DEACTIVATE_EVT";
501 
502     case NFA_DM_API_POWER_OFF_SLEEP_EVT:
503         return "NFA_DM_API_POWER_OFF_SLEEP_EVT";
504 
505     case NFA_DM_API_REG_NDEF_HDLR_EVT:
506         return "NFA_DM_API_REG_NDEF_HDLR_EVT";
507 
508     case NFA_DM_API_DEREG_NDEF_HDLR_EVT:
509         return "NFA_DM_API_DEREG_NDEF_HDLR_EVT";
510 
511     case NFA_DM_TIMEOUT_DISABLE_EVT:
512         return "NFA_DM_TIMEOUT_DISABLE_EVT";
513 
514     }
515 
516     return "Unknown or Vendor Specific";
517 }
518 #endif /* BT_TRACE_VERBOSE */
519