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