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