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