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