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