rw_main.c revision 85b7e84f6cc61506c94e98844cac9ce50bbbe9dc
1/******************************************************************************
2 *
3 *  Copyright (C) 2009-2013 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#include "nfc_target.h"
28#include "bt_types.h"
29
30#if (NFC_INCLUDED == TRUE)
31#include "nfc_api.h"
32#include "nci_hmsgs.h"
33#include "rw_api.h"
34#include "rw_int.h"
35
36tRW_CB rw_cb;
37/*******************************************************************************
38*******************************************************************************/
39void rw_init (void)
40{
41    memset (&rw_cb, 0, sizeof (tRW_CB));
42    rw_cb.trace_level = NFC_INITIAL_TRACE_LEVEL;
43
44}
45
46#if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
47/*******************************************************************************
48* Internal functions for statistics
49*******************************************************************************/
50/*******************************************************************************
51**
52** Function         rw_main_reset_stats
53**
54** Description      Reset counters for statistics
55**
56** Returns          void
57**
58*******************************************************************************/
59void rw_main_reset_stats (void)
60{
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*******************************************************************************/
76void rw_main_update_tx_stats (UINT32 num_bytes, BOOLEAN is_retry)
77{
78    rw_cb.stats.bytes_sent+=num_bytes;
79    rw_cb.stats.num_ops++;
80
81    if (is_retry)
82        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*******************************************************************************/
94void rw_main_update_fail_stats (void)
95{
96    rw_cb.stats.num_fail++;
97}
98
99/*******************************************************************************
100**
101** Function         rw_main_update_crc_error_stats
102**
103** Description      Increment crc error count
104**
105** Returns          void
106**
107*******************************************************************************/
108void rw_main_update_crc_error_stats (void)
109{
110    rw_cb.stats.num_crc++;
111}
112
113/*******************************************************************************
114**
115** Function         rw_main_update_trans_error_stats
116**
117** Description      Increment trans error count
118**
119** Returns          void
120**
121*******************************************************************************/
122void rw_main_update_trans_error_stats (void)
123{
124    rw_cb.stats.num_trans_err++;
125}
126
127/*******************************************************************************
128**
129** Function         rw_main_update_rx_stats
130**
131** Description      Update stats for rx
132**
133** Returns          void
134**
135*******************************************************************************/
136void rw_main_update_rx_stats (UINT32 num_bytes)
137{
138    rw_cb.stats.bytes_received+=num_bytes;
139}
140
141/*******************************************************************************
142**
143** Function         rw_main_log_stats
144**
145** Description      Dump stats
146**
147** Returns          void
148**
149*******************************************************************************/
150void rw_main_log_stats (void)
151{
152    UINT32 ticks, elapsed_ms;
153
154    ticks = GKI_get_tick_count () - rw_cb.stats.start_tick;
155    elapsed_ms = GKI_TICKS_TO_MS (ticks);
156
157    RW_TRACE_DEBUG5 ("NFC tx stats: cmds:%i, retries:%i, aborted: %i, tx_errs: %i, bytes sent:%i", rw_cb.stats.num_ops, rw_cb.stats.num_retries, rw_cb.stats.num_fail, rw_cb.stats.num_trans_err, rw_cb.stats.bytes_sent);
158    RW_TRACE_DEBUG2 ("    rx stats: rx-crc errors %i, bytes received: %i", rw_cb.stats.num_crc, rw_cb.stats.bytes_received);
159    RW_TRACE_DEBUG1 ("    time activated %i ms", elapsed_ms);
160}
161#endif  /* RW_STATS_INCLUDED */
162
163
164/*******************************************************************************
165**
166** Function         RW_SendRawFrame
167**
168** Description      This function sends a raw frame to the peer device.
169**
170** Returns          tNFC_STATUS
171**
172*******************************************************************************/
173tNFC_STATUS RW_SendRawFrame (UINT8 *p_raw_data, UINT16 data_len)
174{
175    tNFC_STATUS status = NFC_STATUS_FAILED;
176    BT_HDR  *p_data;
177    UINT8   *p;
178
179    if (rw_cb.p_cback)
180    {
181        /* a valid opcode for RW - remove */
182        p_data = (BT_HDR *) GKI_getpoolbuf (NFC_RW_POOL_ID);
183        if (p_data)
184        {
185            p_data->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
186            p = (UINT8 *) (p_data + 1) + p_data->offset;
187            memcpy (p, p_raw_data, data_len);
188            p_data->len = data_len;
189
190            RW_TRACE_EVENT1 ("RW SENT raw frame (0x%x)", data_len);
191            status = NFC_SendData (NFC_RF_CONN_ID, p_data);
192        }
193
194    }
195    return status;
196}
197
198/*******************************************************************************
199**
200** Function         RW_SetActivatedTagType
201**
202** Description      This function selects the tag type for Reader/Writer mode.
203**
204** Returns          tNFC_STATUS
205**
206*******************************************************************************/
207tNFC_STATUS RW_SetActivatedTagType (tNFC_ACTIVATE_DEVT *p_activate_params, tRW_CBACK *p_cback)
208{
209    tNFC_STATUS status = NFC_STATUS_FAILED;
210
211    /* check for null cback here / remove checks from rw_t?t */
212    RW_TRACE_DEBUG3 ("RW_SetActivatedTagType protocol:%d, technology:%d, SAK:%d", p_activate_params->protocol, p_activate_params->rf_tech_param.mode, p_activate_params->rf_tech_param.param.pa.sel_rsp);
213
214    if (p_cback == NULL)
215    {
216        RW_TRACE_ERROR0 ("RW_SetActivatedTagType called with NULL callback");
217        return (NFC_STATUS_FAILED);
218    }
219
220    /* Reset tag-specific area of control block */
221    memset (&rw_cb.tcb, 0, sizeof (tRW_TCB));
222
223#if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
224    /* Reset RW stats */
225    rw_main_reset_stats ();
226#endif  /* RW_STATS_INCLUDED */
227
228    rw_cb.p_cback = p_cback;
229    switch (p_activate_params->protocol)
230    {
231    /* not a tag NFC_PROTOCOL_NFCIP1:   NFCDEP/LLCP - NFC-A or NFC-F */
232    case NFC_PROTOCOL_T1T:    /* Type1Tag    - NFC-A */
233        if (p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_A)
234        {
235            status = rw_t1t_select (p_activate_params->intf_param.intf_param.frame.param,
236                                    p_activate_params->rf_tech_param.param.pa.nfcid1);
237        }
238        break;
239
240    case NFC_PROTOCOL_T2T:   /* Type2Tag    - NFC-A */
241        if (p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_A)
242        {
243            if (p_activate_params->rf_tech_param.param.pa.sel_rsp == NFC_SEL_RES_NFC_FORUM_T2T)
244                status      = rw_t2t_select ();
245        }
246        break;
247
248    case NFC_PROTOCOL_T3T:   /* Type3Tag    - NFC-F */
249        if (p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_F)
250        {
251            status = rw_t3t_select (p_activate_params->rf_tech_param.param.pf.nfcid2,
252                                    p_activate_params->rf_tech_param.param.pf.mrti_check,
253                                    p_activate_params->rf_tech_param.param.pf.mrti_update);
254        }
255        break;
256
257    case NFC_PROTOCOL_ISO_DEP:     /* ISODEP/4A,4B- NFC-A or NFC-B */
258        if (  (p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_B)
259            ||(p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_A)  )
260        {
261            status          = rw_t4t_select ();
262        }
263        break;
264
265    case NFC_PROTOCOL_15693:     /* ISO 15693 */
266        if (p_activate_params->rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_ISO15693)
267        {
268            status          = rw_i93_select (p_activate_params->rf_tech_param.param.pi93.uid);
269        }
270        break;
271    /* TODO set up callback for proprietary protocol */
272
273    default:
274        RW_TRACE_ERROR0 ("RW_SetActivatedTagType Invalid protocol");
275    }
276
277    if (status != NFC_STATUS_OK)
278        rw_cb.p_cback = NULL;
279    return status;
280}
281
282/*******************************************************************************
283**
284** Function         RW_SetTraceLevel
285**
286** Description      This function sets the trace level for Reader/Writer mode.
287**                  If called with a value of 0xFF,
288**                  it simply returns the current trace level.
289**
290** Returns          The new or current trace level
291**
292*******************************************************************************/
293UINT8 RW_SetTraceLevel (UINT8 new_level)
294{
295    if (new_level != 0xFF)
296        rw_cb.trace_level = new_level;
297
298    return (rw_cb.trace_level);
299}
300
301#endif /* NFC_INCLUDED == TRUE */
302