1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-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_RW
23  *
24  ******************************************************************************/
25 #include <string.h>
26 #include "nfa_rw_api.h"
27 #include "nfa_sys.h"
28 #include "nfa_rw_int.h"
29 #include "nfa_dm_int.h"
30 #include "nfa_sys_int.h"
31 
32 /* NFA_RW control block */
33 tNFA_RW_CB nfa_rw_cb;
34 
35 /*****************************************************************************
36 ** Constants and types
37 *****************************************************************************/
38 static const tNFA_SYS_REG nfa_rw_sys_reg =
39 {
40     NULL,
41     nfa_rw_handle_event,
42     nfa_rw_sys_disable,
43     NULL
44 };
45 
46 /* NFA_RW actions */
47 const tNFA_RW_ACTION nfa_rw_action_tbl[] =
48 {
49     nfa_rw_handle_op_req,           /* NFA_RW_OP_REQUEST_EVT            */
50     nfa_rw_activate_ntf,            /* NFA_RW_ACTIVATE_NTF_EVT          */
51     nfa_rw_deactivate_ntf,          /* NFA_RW_DEACTIVATE_NTF_EVT        */
52     nfa_rw_presence_check_tick,     /* NFA_RW_PRESENCE_CHECK_TICK_EVT   */
53     nfa_rw_presence_check_timeout   /* NFA_RW_PRESENCE_CHECK_TIMEOUT_EVT*/
54 };
55 
56 
57 /*****************************************************************************
58 ** Local function prototypes
59 *****************************************************************************/
60 #if (BT_TRACE_VERBOSE == TRUE)
61 static char *nfa_rw_evt_2_str (UINT16 event);
62 #endif
63 
64 /*******************************************************************************
65 **
66 ** Function         nfa_rw_init
67 **
68 ** Description      Initialize NFA RW
69 **
70 ** Returns          None
71 **
72 *******************************************************************************/
nfa_rw_init(void)73 void nfa_rw_init (void)
74 {
75     NFA_TRACE_DEBUG0 ("nfa_rw_init ()");
76 
77     /* initialize control block */
78     memset (&nfa_rw_cb, 0, sizeof (tNFA_RW_CB));
79 
80     /* register message handler on NFA SYS */
81     nfa_sys_register (NFA_ID_RW,  &nfa_rw_sys_reg);
82 }
83 
84 /*******************************************************************************
85 **
86 ** Function         nfa_rw_sys_disable
87 **
88 ** Description      Clean up rw sub-system
89 **
90 **
91 ** Returns          void
92 **
93 *******************************************************************************/
nfa_rw_sys_disable(void)94 void nfa_rw_sys_disable (void)
95 {
96     /* Return to idle */
97     NFC_SetStaticRfCback (NULL);
98 
99     /* Stop presence check timer (if started) */
100     nfa_rw_stop_presence_check_timer ();
101 
102     /* Free scratch buffer if any */
103     nfa_rw_free_ndef_rx_buf ();
104 
105     /* Free pending command if any */
106     if (nfa_rw_cb.p_pending_msg)
107     {
108         GKI_freebuf (nfa_rw_cb.p_pending_msg);
109         nfa_rw_cb.p_pending_msg = NULL;
110     }
111 
112     nfa_sys_deregister (NFA_ID_RW);
113 }
114 
115 /*******************************************************************************
116 **
117 ** Function         nfa_rw_proc_disc_evt
118 **
119 ** Description      Called by nfa_dm to handle ACTIVATED/DEACTIVATED  events
120 **
121 ** Returns          void
122 **
123 *******************************************************************************/
nfa_rw_proc_disc_evt(tNFA_DM_RF_DISC_EVT event,tNFC_DISCOVER * p_data,BOOLEAN excl_rf_not_active)124 void nfa_rw_proc_disc_evt (tNFA_DM_RF_DISC_EVT event, tNFC_DISCOVER *p_data, BOOLEAN excl_rf_not_active)
125 {
126     tNFA_RW_MSG msg;
127 
128     switch (event)
129     {
130     case NFA_DM_RF_DISC_ACTIVATED_EVT:
131         msg.hdr.event                       = NFA_RW_ACTIVATE_NTF_EVT;
132         msg.activate_ntf.p_activate_params  = &p_data->activate;
133         msg.activate_ntf.excl_rf_not_active = excl_rf_not_active;
134 
135         nfa_rw_handle_event ((BT_HDR *) &msg);
136         break;
137 
138     case NFA_DM_RF_DISC_DEACTIVATED_EVT:
139         msg.hdr.event = NFA_RW_DEACTIVATE_NTF_EVT;
140 
141         nfa_rw_handle_event ((BT_HDR *) &msg);
142         break;
143 
144     default:
145         break;
146     }
147 }
148 
149 /*******************************************************************************
150 **
151 ** Function         nfa_rw_send_raw_frame
152 **
153 ** Description      Called by nfa_dm to send raw frame
154 **
155 ** Returns          tNFA_STATUS
156 **
157 *******************************************************************************/
nfa_rw_send_raw_frame(BT_HDR * p_data)158 tNFA_STATUS nfa_rw_send_raw_frame (BT_HDR *p_data)
159 {
160     tNFA_RW_MSG *p_msg;
161 
162     if ((p_msg = (tNFA_RW_MSG *) GKI_getbuf ((UINT16) sizeof(tNFA_RW_MSG))) != NULL)
163     {
164         p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
165         p_msg->op_req.op = NFA_RW_OP_SEND_RAW_FRAME;
166 
167         p_msg->op_req.params.send_raw_frame.p_data = p_data;
168 
169         if (nfa_rw_handle_event ((BT_HDR *) p_msg))
170             GKI_freebuf (p_msg);
171 
172         return (NFA_STATUS_OK);
173     }
174     return NFA_STATUS_FAILED;
175 }
176 
177 /*******************************************************************************
178 **
179 ** Function         nfa_rw_handle_event
180 **
181 ** Description      nfa rw main event handling function.
182 **
183 ** Returns          TRUE if caller should free p_msg buffer
184 **
185 *******************************************************************************/
nfa_rw_handle_event(BT_HDR * p_msg)186 BOOLEAN nfa_rw_handle_event(BT_HDR *p_msg)
187 {
188     UINT16 act_idx;
189 
190 #if (BT_TRACE_VERBOSE == TRUE)
191     NFA_TRACE_EVENT3 ("nfa_rw_handle_event event: %s (0x%02x), flags: %08x", nfa_rw_evt_2_str (p_msg->event), p_msg->event, nfa_rw_cb.flags);
192 #else
193     NFA_TRACE_EVENT2 ("nfa_rw_handle_event event: 0x%x, flags: %08x",p_msg->event, nfa_rw_cb.flags);
194 #endif
195 
196     /* Get NFA_RW sub-event */
197     if ((act_idx = (p_msg->event & 0x00FF)) < (NFA_RW_MAX_EVT & 0xFF))
198     {
199         return (*nfa_rw_action_tbl[act_idx]) ( (tNFA_RW_MSG*) p_msg);
200     }
201     else
202     {
203         NFA_TRACE_ERROR1 ("nfa_rw_handle_event: unhandled event 0x%02X", p_msg->event);
204         return TRUE;
205     }
206 }
207 
208 
209 #if (BT_TRACE_VERBOSE == TRUE)
210 /*******************************************************************************
211 **
212 ** Function         nfa_rw_evt_2_str
213 **
214 ** Description      convert nfa_rw evt to string
215 **
216 *******************************************************************************/
nfa_rw_evt_2_str(UINT16 event)217 static char *nfa_rw_evt_2_str (UINT16 event)
218 {
219     switch (event)
220     {
221     case NFA_RW_OP_REQUEST_EVT:
222         return "NFA_RW_OP_REQUEST_EVT";
223 
224     case NFA_RW_ACTIVATE_NTF_EVT:
225         return "NFA_RW_ACTIVATE_NTF_EVT";
226 
227     case NFA_RW_DEACTIVATE_NTF_EVT:
228         return "NFA_RW_DEACTIVATE_NTF_EVT";
229 
230     case NFA_RW_PRESENCE_CHECK_TICK_EVT:
231         return "NFA_RW_PRESENCE_CHECK_TICK_EVT";
232 
233     case NFA_RW_PRESENCE_CHECK_TIMEOUT_EVT:
234         return "NFA_RW_PRESENCE_CHECK_TIMEOUT_EVT";
235 
236     default:
237         return "Unknown";
238     }
239 }
240 #endif /* BT_TRACE_VERBOSE */
241