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