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