1 /******************************************************************************
2 *
3 * Copyright (C) 2009-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 file contains functions that interface with the NFC NCI transport.
22 * On the receive side, it routes events to the appropriate handler
23 * (callback). On the transmit side, it manages the command transmission.
24 *
25 ******************************************************************************/
26 #include <android-base/logging.h>
27 #include <android-base/stringprintf.h>
28 #include <log/log.h>
29 #include <string.h>
30
31 #include "bt_types.h"
32 #include "nci_hmsgs.h"
33 #include "nfc_api.h"
34 #include "nfc_int.h"
35 #include "nfc_target.h"
36 #include "rw_api.h"
37 #include "rw_int.h"
38
39 using android::base::StringPrintf;
40
41 tRW_CB rw_cb;
42
43 /*******************************************************************************
44 *******************************************************************************/
rw_init(void)45 void rw_init(void) { memset(&rw_cb, 0, sizeof(tRW_CB)); }
46
47 #if (RW_STATS_INCLUDED == TRUE)
48 /*******************************************************************************
49 * Internal functions for statistics
50 *******************************************************************************/
51 /*******************************************************************************
52 **
53 ** Function rw_main_reset_stats
54 **
55 ** Description Reset counters for statistics
56 **
57 ** Returns void
58 **
59 *******************************************************************************/
rw_main_reset_stats(void)60 void rw_main_reset_stats(void) {
61 memset(&rw_cb.stats, 0, sizeof(tRW_STATS));
62
63 /* Get current tick count */
64 rw_cb.stats.start_tick = GKI_get_tick_count();
65 }
66
67 /*******************************************************************************
68 **
69 ** Function rw_main_update_tx_stats
70 **
71 ** Description Update stats for tx
72 **
73 ** Returns void
74 **
75 *******************************************************************************/
rw_main_update_tx_stats(uint32_t num_bytes,bool is_retry)76 void rw_main_update_tx_stats(uint32_t num_bytes, bool is_retry) {
77 rw_cb.stats.bytes_sent += num_bytes;
78 rw_cb.stats.num_ops++;
79
80 if (is_retry) rw_cb.stats.num_retries++;
81 }
82
83 /*******************************************************************************
84 **
85 ** Function rw_main_update_fail_stats
86 **
87 ** Description Increment failure count
88 **
89 ** Returns void
90 **
91 *******************************************************************************/
rw_main_update_fail_stats(void)92 void rw_main_update_fail_stats(void) { rw_cb.stats.num_fail++; }
93
94 /*******************************************************************************
95 **
96 ** Function rw_main_update_crc_error_stats
97 **
98 ** Description Increment crc error count
99 **
100 ** Returns void
101 **
102 *******************************************************************************/
rw_main_update_crc_error_stats(void)103 void rw_main_update_crc_error_stats(void) { rw_cb.stats.num_crc++; }
104
105 /*******************************************************************************
106 **
107 ** Function rw_main_update_trans_error_stats
108 **
109 ** Description Increment trans error count
110 **
111 ** Returns void
112 **
113 *******************************************************************************/
rw_main_update_trans_error_stats(void)114 void rw_main_update_trans_error_stats(void) { rw_cb.stats.num_trans_err++; }
115
116 /*******************************************************************************
117 **
118 ** Function rw_main_update_rx_stats
119 **
120 ** Description Update stats for rx
121 **
122 ** Returns void
123 **
124 *******************************************************************************/
rw_main_update_rx_stats(uint32_t num_bytes)125 void rw_main_update_rx_stats(uint32_t num_bytes) {
126 rw_cb.stats.bytes_received += num_bytes;
127 }
128
129 /*******************************************************************************
130 **
131 ** Function rw_main_log_stats
132 **
133 ** Description Dump stats
134 **
135 ** Returns void
136 **
137 *******************************************************************************/
rw_main_log_stats(void)138 void rw_main_log_stats(void) {
139 uint32_t ticks, elapsed_ms;
140
141 ticks = GKI_get_tick_count() - rw_cb.stats.start_tick;
142 elapsed_ms = GKI_TICKS_TO_MS(ticks);
143
144 LOG(VERBOSE) << StringPrintf(
145 "%s: tx stats: cmds=%i, retries=%i, aborted=%i, tx_errs=%i, bytes "
146 "sent=%i",
147 __func__, rw_cb.stats.num_ops, rw_cb.stats.num_retries,
148 rw_cb.stats.num_fail, rw_cb.stats.num_trans_err, rw_cb.stats.bytes_sent);
149 LOG(VERBOSE) << StringPrintf(
150 "%s: rx stats: rx-crc errors %i, bytes received=%i", __func__,
151 rw_cb.stats.num_crc, rw_cb.stats.bytes_received);
152 LOG(VERBOSE) << StringPrintf("%s: time activated %i ms", __func__,
153 elapsed_ms);
154 }
155 #endif /* RW_STATS_INCLUDED */
156
157 /*******************************************************************************
158 **
159 ** Function RW_SendRawFrame
160 **
161 ** Description This function sends a raw frame to the peer device.
162 **
163 ** Returns tNFC_STATUS
164 **
165 *******************************************************************************/
RW_SendRawFrame(uint8_t * p_raw_data,uint16_t data_len)166 tNFC_STATUS RW_SendRawFrame(uint8_t* p_raw_data, uint16_t data_len) {
167 tNFC_STATUS status = NFC_STATUS_FAILED;
168 NFC_HDR* p_data;
169 uint8_t* p;
170
171 if (rw_cb.p_cback) {
172 if (data_len > GKI_get_pool_bufsize(NFC_RW_POOL_ID) - NCI_MSG_OFFSET_SIZE -
173 NCI_DATA_HDR_SIZE - 1) {
174 android_errorWriteLog(0x534e4554, "157650117");
175 return NFC_STATUS_FAILED;
176 }
177 /* a valid opcode for RW - remove */
178 p_data = (NFC_HDR*)GKI_getpoolbuf(NFC_RW_POOL_ID);
179 if (p_data) {
180 p_data->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
181 p = (uint8_t*)(p_data + 1) + p_data->offset;
182 memcpy(p, p_raw_data, data_len);
183 p_data->len = data_len;
184
185 LOG(VERBOSE) << StringPrintf("%s: RW SENT raw frame (0x%x)", __func__,
186 data_len);
187 status = NFC_SendData(NFC_RF_CONN_ID, p_data);
188 }
189 }
190 return status;
191 }
192
193 /*******************************************************************************
194 **
195 ** Function RW_SetActivatedTagType
196 **
197 ** Description This function selects the tag type for Reader/Writer mode.
198 **
199 ** Returns tNFC_STATUS
200 **
201 *******************************************************************************/
RW_SetActivatedTagType(tNFC_ACTIVATE_DEVT * p_activate_params,tRW_CBACK * p_cback)202 tNFC_STATUS RW_SetActivatedTagType(tNFC_ACTIVATE_DEVT* p_activate_params,
203 tRW_CBACK* p_cback) {
204 tNFC_STATUS status = NFC_STATUS_FAILED;
205
206 /* check for null cback here / remove checks from rw_t?t */
207 LOG(VERBOSE) << StringPrintf(
208 "%s: protocol=%x, technology=%x, SAK=%x", __func__,
209 p_activate_params->protocol, p_activate_params->rf_tech_param.mode,
210 p_activate_params->rf_tech_param.param.pa.sel_rsp);
211
212 if (p_cback == nullptr) {
213 LOG(ERROR) << StringPrintf("%s: called with NULL callback", __func__);
214 return (NFC_STATUS_FAILED);
215 }
216
217 switch (rw_cb.tcb_type) {
218 case RW_CB_TYPE_T1T: {
219 nfc_stop_quick_timer(&rw_cb.tcb.t1t.timer);
220 break;
221 }
222 case RW_CB_TYPE_T2T: {
223 nfc_stop_quick_timer(&rw_cb.tcb.t2t.t2_timer);
224 break;
225 }
226 case RW_CB_TYPE_T3T: {
227 nfc_stop_quick_timer(&rw_cb.tcb.t3t.timer);
228 nfc_stop_quick_timer(&rw_cb.tcb.t3t.poll_timer);
229 break;
230 }
231 case RW_CB_TYPE_T4T: {
232 nfc_stop_quick_timer(&rw_cb.tcb.t4t.timer);
233 break;
234 }
235 case RW_CB_TYPE_T5T: {
236 nfc_stop_quick_timer(&rw_cb.tcb.i93.timer);
237 break;
238 }
239 case RW_CB_TYPE_MIFARE: {
240 nfc_stop_quick_timer(&rw_cb.tcb.mfc.timer);
241 nfc_stop_quick_timer(&rw_cb.tcb.mfc.mfc_timer);
242 break;
243 }
244 case RW_CB_TYPE_UNKNOWN: {
245 break;
246 }
247 }
248
249 /* Reset tag-specific area of control block */
250 memset(&rw_cb.tcb, 0, sizeof(tRW_TCB));
251
252 #if (RW_STATS_INCLUDED == TRUE)
253 /* Reset RW stats */
254 rw_main_reset_stats();
255 #endif /* RW_STATS_INCLUDED */
256
257 rw_cb.p_cback = p_cback;
258 /* not a tag NFC_PROTOCOL_NFCIP1: NFCDEP/LLCP - NFC-A or NFC-F */
259 if (NFC_PROTOCOL_T1T == p_activate_params->protocol) {
260 /* Type1Tag - NFC-A */
261 if (p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_A) {
262 rw_cb.tcb_type = RW_CB_TYPE_T1T;
263 status = rw_t1t_select(p_activate_params->rf_tech_param.param.pa.hr,
264 p_activate_params->rf_tech_param.param.pa.nfcid1);
265 }
266 } else if (NFC_PROTOCOL_T2T == p_activate_params->protocol) {
267 /* Type2Tag - NFC-A */
268 if (p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_A) {
269 rw_cb.tcb_type = RW_CB_TYPE_T2T;
270 if (p_activate_params->rf_tech_param.param.pa.sel_rsp ==
271 NFC_SEL_RES_NFC_FORUM_T2T)
272 status = rw_t2t_select();
273 }
274 } else if (NFC_PROTOCOL_T3T == p_activate_params->protocol) {
275 /* Type3Tag - NFC-F */
276 if (p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_F) {
277 rw_cb.tcb_type = RW_CB_TYPE_T3T;
278 status =
279 rw_t3t_select(p_activate_params->rf_tech_param.param.pf.nfcid2,
280 p_activate_params->rf_tech_param.param.pf.mrti_check,
281 p_activate_params->rf_tech_param.param.pf.mrti_update);
282 }
283 } else if (NFC_PROTOCOL_ISO_DEP == p_activate_params->protocol) {
284 /* ISODEP/4A,4B- NFC-A or NFC-B */
285 if ((p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_B) ||
286 (p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_A)) {
287 rw_cb.tcb_type = RW_CB_TYPE_T4T;
288 status = rw_t4t_select();
289 }
290 } else if (NFC_PROTOCOL_T5T == p_activate_params->protocol) {
291 /* T5T */
292 if (p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_V) {
293 rw_cb.tcb_type = RW_CB_TYPE_T5T;
294 status = rw_i93_select(p_activate_params->rf_tech_param.param.pi93.uid);
295 }
296 } else if (NFC_PROTOCOL_MIFARE == p_activate_params->protocol) {
297 /* Mifare Classic*/
298 if (p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_A) {
299 rw_cb.tcb_type = RW_CB_TYPE_MIFARE;
300 status = rw_mfc_select(
301 p_activate_params->rf_tech_param.param.pa.sel_rsp,
302 p_activate_params->rf_tech_param.param.pa.nfcid1 +
303 p_activate_params->rf_tech_param.param.pa.nfcid1_len - 4);
304 }
305 } else if ((NFC_PROTOCOL_UNKNOWN == p_activate_params->protocol) &&
306 (p_activate_params->rf_tech_param.mode ==
307 NFC_DISCOVERY_TYPE_POLL_B)) {
308 status = rw_ci_select();
309 } else {
310 rw_cb.tcb_type = RW_CB_TYPE_UNKNOWN;
311 LOG(ERROR) << StringPrintf("%s: Invalid protocol", __func__);
312 }
313
314 if (status != NFC_STATUS_OK) rw_cb.p_cback = nullptr;
315 return status;
316 }
317
318 /*******************************************************************************
319 **
320 ** Function RW_SetT4tNfceeInfo
321 **
322 ** Description This function selects the T4t Nfcee for Reader/Writer mode.
323 **
324 ** Returns tNFC_STATUS
325 **
326 *******************************************************************************/
RW_SetT4tNfceeInfo(tRW_CBACK * p_cback,uint8_t conn_id)327 tNFC_STATUS RW_SetT4tNfceeInfo(tRW_CBACK* p_cback, uint8_t conn_id) {
328 tNFC_STATUS status = NFC_STATUS_FAILED;
329 /* Reset tag-specific area of control block */
330 LOG(ERROR) << StringPrintf("%s: conn_id=%d ", __func__, conn_id);
331
332 memset(&rw_cb.tcb, 0, sizeof(tRW_TCB));
333
334 if (p_cback != NULL) {
335 rw_cb.p_cback = p_cback;
336 status = RW_T4tNfceeInitCb();
337 if (status != NFC_STATUS_OK) {
338 rw_cb.p_cback = NULL;
339 }
340 } else {
341 rw_cb.p_cback = NULL;
342 }
343 return status;
344 }
345