• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *  This is the main implementation file for the NFA_RW
22  *
23  ******************************************************************************/
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
26 #include <string.h>
27 
28 #include "nfa_dm_int.h"
29 #include "nfa_rw_api.h"
30 #include "nfa_rw_int.h"
31 #include "rw_int.h"
32 
33 using android::base::StringPrintf;
34 
35 /* NFA_RW control block */
36 tNFA_RW_CB nfa_rw_cb;
37 
38 /*****************************************************************************
39 ** Constants and types
40 *****************************************************************************/
41 static const tNFA_SYS_REG nfa_rw_sys_reg = {nullptr, nfa_rw_handle_event,
42                                             nfa_rw_sys_disable, nullptr};
43 
44 /* NFA_RW actions */
45 const tNFA_RW_ACTION nfa_rw_action_tbl[] = {
46     nfa_rw_handle_op_req,         /* NFA_RW_OP_REQUEST_EVT            */
47     nfa_rw_activate_ntf,          /* NFA_RW_ACTIVATE_NTF_EVT          */
48     nfa_rw_deactivate_ntf,        /* NFA_RW_DEACTIVATE_NTF_EVT        */
49     nfa_rw_presence_check_tick,   /* NFA_RW_PRESENCE_CHECK_TICK_EVT   */
50     nfa_rw_presence_check_timeout /* NFA_RW_PRESENCE_CHECK_TIMEOUT_EVT*/
51 };
52 
53 /*****************************************************************************
54 ** Local function prototypes
55 *****************************************************************************/
56 static std::string nfa_rw_evt_2_str(uint16_t event);
57 
58 /*******************************************************************************
59 **
60 ** Function         nfa_rw_init
61 **
62 ** Description      Initialize NFA RW
63 **
64 ** Returns          None
65 **
66 *******************************************************************************/
nfa_rw_init(void)67 void nfa_rw_init(void) {
68   LOG(VERBOSE) << __func__;
69 
70   /* initialize control block */
71   memset(&nfa_rw_cb, 0, sizeof(tNFA_RW_CB));
72 
73   /* register message handler on NFA SYS */
74   nfa_sys_register(NFA_ID_RW, &nfa_rw_sys_reg);
75 }
76 
77 /*******************************************************************************
78 **
79 ** Function         nfa_rw_sys_disable
80 **
81 ** Description      Clean up rw sub-system
82 **
83 **
84 ** Returns          void
85 **
86 *******************************************************************************/
nfa_rw_sys_disable(void)87 void nfa_rw_sys_disable(void) {
88   tRW_T1T_CB* p_t1t;
89   tRW_T2T_CB* p_t2t;
90   tRW_T3T_CB* p_t3t;
91   tRW_I93_CB* p_i93;
92   tRW_MFC_CB* p_mfc;
93 
94   LOG(VERBOSE) << __func__;
95 
96   switch (rw_cb.tcb_type) {
97     case RW_CB_TYPE_T1T:
98       p_t1t = &rw_cb.tcb.t1t;
99       if (p_t1t->p_cur_cmd_buf != NULL) {
100         GKI_freebuf(p_t1t->p_cur_cmd_buf);
101         p_t1t->p_cur_cmd_buf = NULL;
102       }
103       break;
104     case RW_CB_TYPE_T2T:
105       p_t2t = &rw_cb.tcb.t2t;
106       if (p_t2t->p_cur_cmd_buf != NULL) {
107         GKI_freebuf(p_t2t->p_cur_cmd_buf);
108         p_t2t->p_cur_cmd_buf = NULL;
109       }
110       if (p_t2t->p_sec_cmd_buf != NULL) {
111         GKI_freebuf(p_t2t->p_sec_cmd_buf);
112         p_t2t->p_sec_cmd_buf = NULL;
113       }
114       break;
115     case RW_CB_TYPE_T3T:
116       p_t3t = &rw_cb.tcb.t3t;
117       if (p_t3t->p_cur_cmd_buf != NULL) {
118         GKI_freebuf(p_t3t->p_cur_cmd_buf);
119         p_t3t->p_cur_cmd_buf = NULL;
120       }
121       break;
122     case RW_CB_TYPE_T4T: /* do nothing */
123       break;
124     case RW_CB_TYPE_T5T:
125       p_i93 = &rw_cb.tcb.i93;
126       if (p_i93->p_retry_cmd != NULL) {
127         GKI_freebuf(p_i93->p_retry_cmd);
128         p_i93->p_retry_cmd = NULL;
129       }
130       break;
131     case RW_CB_TYPE_MIFARE:
132       p_mfc = &rw_cb.tcb.mfc;
133       if (p_mfc->p_cur_cmd_buf != NULL) {
134         GKI_freebuf(p_mfc->p_cur_cmd_buf);
135         p_mfc->p_cur_cmd_buf = NULL;
136       }
137       break;
138     default: /* do nothing */
139       break;
140   }
141   rw_cb.tcb_type = RW_CB_TYPE_UNKNOWN;
142 
143   /* Return to idle */
144   NFC_SetStaticRfCback(nullptr);
145 
146   /* Stop presence check timer (if started) */
147   nfa_rw_stop_presence_check_timer();
148 
149   /* Free scratch buffer if any */
150   nfa_rw_free_ndef_rx_buf();
151 
152   /* Free pending command if any */
153   if (nfa_rw_cb.p_pending_msg) {
154     GKI_freebuf(nfa_rw_cb.p_pending_msg);
155     nfa_rw_cb.p_pending_msg = nullptr;
156   }
157 
158   nfa_sys_deregister(NFA_ID_RW);
159 }
160 
161 /*******************************************************************************
162 **
163 ** Function         nfa_rw_proc_disc_evt
164 **
165 ** Description      Called by nfa_dm to handle ACTIVATED/DEACTIVATED  events
166 **
167 ** Returns          void
168 **
169 *******************************************************************************/
nfa_rw_proc_disc_evt(tNFA_DM_RF_DISC_EVT event,tNFC_DISCOVER * p_data,bool excl_rf_not_active)170 void nfa_rw_proc_disc_evt(tNFA_DM_RF_DISC_EVT event, tNFC_DISCOVER* p_data,
171                           bool excl_rf_not_active) {
172   tNFA_RW_MSG msg;
173 
174   switch (event) {
175     case NFA_DM_RF_DISC_ACTIVATED_EVT:
176       msg.hdr.event = NFA_RW_ACTIVATE_NTF_EVT;
177       msg.activate_ntf.p_activate_params = &p_data->activate;
178       msg.activate_ntf.excl_rf_not_active = excl_rf_not_active;
179 
180       nfa_rw_handle_event((NFC_HDR*)&msg);
181       break;
182 
183     case NFA_DM_RF_DISC_DEACTIVATED_EVT:
184       msg.hdr.event = NFA_RW_DEACTIVATE_NTF_EVT;
185 
186       nfa_rw_handle_event((NFC_HDR*)&msg);
187       break;
188 
189     default:
190       break;
191   }
192 }
193 
194 /*******************************************************************************
195 **
196 ** Function         nfa_rw_send_raw_frame
197 **
198 ** Description      Called by nfa_dm to send raw frame
199 **
200 ** Returns          tNFA_STATUS
201 **
202 *******************************************************************************/
nfa_rw_send_raw_frame(NFC_HDR * p_data)203 tNFA_STATUS nfa_rw_send_raw_frame(NFC_HDR* p_data) {
204   if (!gki_utils) {
205     gki_utils = new GkiUtils();
206   }
207   tNFA_RW_MSG* p_msg;
208 
209   p_msg = (tNFA_RW_MSG*)GKI_getbuf((uint16_t)sizeof(tNFA_RW_MSG));
210   if (p_msg != nullptr) {
211     p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
212     p_msg->op_req.op = NFA_RW_OP_SEND_RAW_FRAME;
213 
214     p_msg->op_req.params.send_raw_frame.p_data = p_data;
215 
216     if (nfa_rw_handle_event((NFC_HDR*)p_msg)) gki_utils->freebuf(p_msg);
217 
218     return (NFA_STATUS_OK);
219   }
220   return NFA_STATUS_FAILED;
221 }
222 
223 /*******************************************************************************
224 **
225 ** Function         nfa_rw_handle_event
226 **
227 ** Description      nfa rw main event handling function.
228 **
229 ** Returns          TRUE if caller should free p_msg buffer
230 **
231 *******************************************************************************/
nfa_rw_handle_event(NFC_HDR * p_msg)232 bool nfa_rw_handle_event(NFC_HDR* p_msg) {
233   uint16_t act_idx;
234 
235   LOG(VERBOSE) << StringPrintf("%s: event=%s (0x%02x), flags=%08x", __func__,
236                                nfa_rw_evt_2_str(p_msg->event).c_str(),
237                                p_msg->event, nfa_rw_cb.flags);
238 
239   /* Get NFA_RW sub-event */
240   act_idx = (p_msg->event & 0x00FF);
241   if (act_idx < (NFA_RW_MAX_EVT & 0xFF)) {
242     return (*nfa_rw_action_tbl[act_idx])((tNFA_RW_MSG*)p_msg);
243   } else {
244     LOG(ERROR) << StringPrintf("%s: unhandled event 0x%02X", __func__,
245                                p_msg->event);
246     return true;
247   }
248 }
249 
250 /*******************************************************************************
251 **
252 ** Function         nfa_rw_evt_2_str
253 **
254 ** Description      convert nfa_rw evt to string
255 **
256 *******************************************************************************/
nfa_rw_evt_2_str(uint16_t event)257 static std::string nfa_rw_evt_2_str(uint16_t event) {
258   switch (event) {
259     case NFA_RW_OP_REQUEST_EVT:
260       return "NFA_RW_OP_REQUEST_EVT";
261     case NFA_RW_ACTIVATE_NTF_EVT:
262       return "NFA_RW_ACTIVATE_NTF_EVT";
263     case NFA_RW_DEACTIVATE_NTF_EVT:
264       return "NFA_RW_DEACTIVATE_NTF_EVT";
265     case NFA_RW_PRESENCE_CHECK_TICK_EVT:
266       return "NFA_RW_PRESENCE_CHECK_TICK_EVT";
267     case NFA_RW_PRESENCE_CHECK_TIMEOUT_EVT:
268       return "NFA_RW_PRESENCE_CHECK_TIMEOUT_EVT";
269     default:
270       return "Unknown";
271   }
272 }
273