1 /******************************************************************************
2  *
3  *  Copyright (C) 2011-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 is the main implementation file for the NFA_CE
23  *
24  ******************************************************************************/
25 #include <string.h>
26 #include "nfa_ce_api.h"
27 #include "nfa_sys.h"
28 #include "nfa_ce_int.h"
29 #include "nfa_dm_int.h"
30 #include "nfa_sys_int.h"
31 
32 /* NFA_CE control block */
33 tNFA_CE_CB nfa_ce_cb;
34 
35 /*****************************************************************************
36 ** Constants and types
37 *****************************************************************************/
38 #define NFA_CE_DEFAULT_ISODEP_DISC_MASK (NFA_DM_DISC_MASK_LA_ISO_DEP | NFA_DM_DISC_MASK_LB_ISO_DEP)
39 static void nfa_ce_proc_nfcc_power_mode (UINT8 nfcc_power_mode);
40 
41 static const tNFA_SYS_REG nfa_ce_sys_reg =
42 {
43     NULL,
44     nfa_ce_hdl_event,
45     nfa_ce_sys_disable,
46     nfa_ce_proc_nfcc_power_mode
47 };
48 
49 /* NFA_CE actions */
50 const tNFA_CE_ACTION nfa_ce_action_tbl[] =
51 {
52     nfa_ce_api_cfg_local_tag,   /* NFA_CE_API_CFG_LOCAL_TAG_EVT */
53     nfa_ce_api_reg_listen,      /* NFA_CE_API_REG_LISTEN_EVT    */
54     nfa_ce_api_dereg_listen,    /* NFA_CE_API_DEREG_LISTEN_EVT  */
55     nfa_ce_api_cfg_isodep_tech, /* NFA_CE_API_CFG_ISODEP_TECH_EVT*/
56     nfa_ce_activate_ntf,        /* NFA_CE_ACTIVATE_NTF_EVT      */
57     nfa_ce_deactivate_ntf,      /* NFA_CE_DEACTIVATE_NTF_EVT    */
58 };
59 #define NFA_CE_ACTION_TBL_SIZE  (sizeof (nfa_ce_action_tbl) / sizeof (tNFA_CE_ACTION))
60 
61 /*****************************************************************************
62 ** Local function prototypes
63 *****************************************************************************/
64 #if (BT_TRACE_VERBOSE == TRUE)
65 static char *nfa_ce_evt_2_str (UINT16 event);
66 #endif
67 
68 
69 /*******************************************************************************
70 **
71 ** Function         nfa_ce_init
72 **
73 ** Description      Initialize NFA CE
74 **
75 ** Returns          None
76 **
77 *******************************************************************************/
nfa_ce_init(void)78 void nfa_ce_init (void)
79 {
80     NFA_TRACE_DEBUG0 ("nfa_ce_init ()");
81 
82     /* initialize control block */
83     memset (&nfa_ce_cb, 0, sizeof (tNFA_CE_CB));
84 
85     /* Generate a random NFCID for Type-3 NDEF emulation (Type-3 tag NFCID2 must start with 02:FE) */
86     nfa_ce_t3t_generate_rand_nfcid (nfa_ce_cb.listen_info[NFA_CE_LISTEN_INFO_IDX_NDEF].t3t_nfcid2);
87     nfa_ce_cb.listen_info[NFA_CE_LISTEN_INFO_IDX_NDEF].rf_disc_handle = NFA_HANDLE_INVALID;
88     nfa_ce_cb.isodep_disc_mask  = NFA_CE_DEFAULT_ISODEP_DISC_MASK;
89     nfa_ce_cb.idx_wild_card     = NFA_CE_LISTEN_INFO_IDX_INVALID;
90 
91     /* register message handler on NFA SYS */
92     nfa_sys_register ( NFA_ID_CE,  &nfa_ce_sys_reg);
93 }
94 
95 /*******************************************************************************
96 **
97 ** Function         nfa_ce_sys_disable
98 **
99 ** Description      Clean up ce sub-system
100 **
101 **
102 ** Returns          void
103 **
104 *******************************************************************************/
nfa_ce_sys_disable(void)105 void nfa_ce_sys_disable (void)
106 {
107     tNFA_CE_LISTEN_INFO *p_info;
108     UINT8 xx;
109 
110     NFC_SetStaticRfCback (NULL);
111 
112     /* Free scratch buf if any */
113     nfa_ce_free_scratch_buf ();
114 
115     /* Delete discovery handles */
116     for (xx = 0, p_info = nfa_ce_cb.listen_info; xx < NFA_CE_LISTEN_INFO_MAX; xx++, p_info++)
117     {
118         if ((p_info->flags & NFA_CE_LISTEN_INFO_IN_USE) && (p_info->rf_disc_handle != NFA_HANDLE_INVALID))
119         {
120             nfa_dm_delete_rf_discover (p_info->rf_disc_handle);
121             p_info->rf_disc_handle = NFA_HANDLE_INVALID;
122         }
123     }
124 
125     nfa_sys_deregister (NFA_ID_CE);
126 }
127 
128 /*******************************************************************************
129 **
130 ** Function         nfa_ce_proc_nfcc_power_mode
131 **
132 ** Description      Processing NFCC power mode changes
133 **
134 ** Returns          None
135 **
136 *******************************************************************************/
nfa_ce_proc_nfcc_power_mode(UINT8 nfcc_power_mode)137 static void nfa_ce_proc_nfcc_power_mode (UINT8 nfcc_power_mode)
138 {
139     tNFA_CE_CB *p_cb = &nfa_ce_cb;
140     UINT8 listen_info_idx;
141 
142     NFA_TRACE_DEBUG1 ("nfa_ce_proc_nfcc_power_mode (): nfcc_power_mode=%d",
143                        nfcc_power_mode);
144 
145     /* if NFCC power mode is change to full power */
146     if (nfcc_power_mode == NFA_DM_PWR_MODE_FULL)
147     {
148         nfa_ce_restart_listen_check ();
149     }
150     else
151     {
152         for (listen_info_idx=0; listen_info_idx<NFA_CE_LISTEN_INFO_IDX_INVALID; listen_info_idx++)
153         {
154             /* add RF discovery to DM only if it is not added yet */
155             if (  (p_cb->listen_info[listen_info_idx].flags & NFA_CE_LISTEN_INFO_IN_USE)
156                 &&(p_cb->listen_info[listen_info_idx].rf_disc_handle != NFA_HANDLE_INVALID))
157             {
158                 nfa_dm_delete_rf_discover (p_cb->listen_info[listen_info_idx].rf_disc_handle);
159                 p_cb->listen_info[listen_info_idx].rf_disc_handle = NFA_HANDLE_INVALID;
160             }
161         }
162     }
163 
164     nfa_sys_cback_notify_nfcc_power_mode_proc_complete (NFA_ID_CE);
165 }
166 
167 /*******************************************************************************
168 **
169 ** Function         nfa_ce_hdl_event
170 **
171 ** Description      nfa rw main event handling function.
172 **
173 ** Returns          BOOLEAN
174 **
175 *******************************************************************************/
nfa_ce_hdl_event(BT_HDR * p_msg)176 BOOLEAN nfa_ce_hdl_event (BT_HDR *p_msg)
177 {
178     UINT16 act_idx;
179     BOOLEAN freebuf = TRUE;
180 
181 #if (BT_TRACE_VERBOSE == TRUE)
182     NFA_TRACE_EVENT3 ("nfa_ce_handle_event event: %s (0x%02x), flags: %08x", nfa_ce_evt_2_str (p_msg->event), p_msg->event, nfa_ce_cb.flags);
183 #else
184     NFA_TRACE_EVENT2 ("nfa_ce_handle_event event: 0x%x, flags: %08x",p_msg->event, nfa_ce_cb.flags);
185 #endif
186 
187     /* Get NFA_RW sub-event */
188     if ((act_idx = (p_msg->event & 0x00FF)) < NFA_CE_ACTION_TBL_SIZE)
189     {
190         freebuf = (*nfa_ce_action_tbl[act_idx]) ((tNFA_CE_MSG*) p_msg);
191     }
192 
193     /* if vendor specific event handler is registered */
194     if (nfa_ce_cb.p_vs_evt_hdlr)
195     {
196         (*nfa_ce_cb.p_vs_evt_hdlr) (p_msg);
197     }
198 
199     return freebuf;
200 }
201 
202 #if (BT_TRACE_VERBOSE == TRUE)
203 /*******************************************************************************
204 **
205 ** Function         nfa_ce_evt_2_str
206 **
207 ** Description      convert nfc evt to string
208 **
209 *******************************************************************************/
nfa_ce_evt_2_str(UINT16 event)210 static char *nfa_ce_evt_2_str (UINT16 event)
211 {
212     switch (event)
213     {
214     case NFA_CE_API_CFG_LOCAL_TAG_EVT:
215         return "NFA_CE_API_CFG_LOCAL_TAG_EVT";
216 
217     case NFA_CE_API_REG_LISTEN_EVT:
218         return "NFA_CE_API_REG_LISTEN_EVT";
219 
220     case NFA_CE_API_DEREG_LISTEN_EVT:
221         return "NFA_CE_API_DEREG_LISTEN_EVT";
222 
223     case NFA_CE_API_CFG_ISODEP_TECH_EVT:
224         return "NFA_CE_API_CFG_ISODEP_TECH_EVT";
225 
226     case NFA_CE_ACTIVATE_NTF_EVT:
227         return "NFA_CE_ACTIVATE_NTF_EVT";
228 
229     case NFA_CE_DEACTIVATE_NTF_EVT:
230         return "NFA_CE_DEACTIVATE_NTF_EVT";
231 
232     default:
233         return "Unknown";
234     }
235 }
236 #endif /* BT_TRACE_VERBOSE */
237