1/******************************************************************************
2 *
3 *  Copyright (C) 2010-2012 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 the common data types shared by Reader/Writer mode
22 *  and Card Emulation.
23 *
24 ******************************************************************************/
25#include "nfc_target.h"
26#include "bt_types.h"
27
28#if (NFC_INCLUDED == TRUE)
29#include "nfc_api.h"
30#include "rw_api.h"
31#include "rw_int.h"
32#include "tags_int.h"
33
34#define T1T_MAX_NUM_OPCODES         9
35#define T1T_STATIC_OPCODES          5
36#define T1T_MAX_TAG_MODELS          2
37
38const tT1T_CMD_RSP_INFO t1t_cmd_rsp_infos[] =
39{
40    /* Note: the order of these commands can not be changed.
41     * If new events are added, add them after T1T_CMD_WRITE_NE8 */
42/*   opcode         cmd_len,  uid_offset,  rsp_len */
43    {T1T_CMD_RID,       7,          3,      6},
44    {T1T_CMD_RALL,      7,          3,      122},
45    {T1T_CMD_READ,      7,          3,      2},
46    {T1T_CMD_WRITE_E,   7,          3,      2},
47    {T1T_CMD_WRITE_NE,  7,          3,      2},
48    {T1T_CMD_RSEG,      14,         10,     129},
49    {T1T_CMD_READ8,     14,         10,     9},
50    {T1T_CMD_WRITE_E8,  14,         10,     9},
51    {T1T_CMD_WRITE_NE8, 14,         10,     9}
52};
53
54const tT1T_INIT_TAG t1t_init_content[] =
55{
56/*  Tag Name            CC3,        is dynamic, ltv[0]  ltv[1]  ltv[2]  mtv[0]  mtv[1]  mtv[2]*/
57    {RW_T1T_IS_TOPAZ96, 0x0E,       FALSE,      {0,      0,      0},      {0,      0,      0}},
58    {RW_T1T_IS_TOPAZ512,0x3F,       TRUE,       {0xF2,   0x30,   0x33},   {0xF0,   0x02,   0x03}}
59};
60
61#define T2T_MAX_NUM_OPCODES         3
62#define T2T_MAX_TAG_MODELS          7
63
64const tT2T_CMD_RSP_INFO t2t_cmd_rsp_infos[] =
65{
66    /* Note: the order of these commands can not be changed.
67     * If new events are added, add them after T2T_CMD_SEC_SEL */
68/*  opcode            cmd_len,   rsp_len, nack_rsp_len */
69    {T2T_CMD_READ,      2,          16,     1},
70    {T2T_CMD_WRITE,     6,          1,      1},
71    {T2T_CMD_SEC_SEL,   2,          1,      1}
72};
73
74const tT2T_INIT_TAG t2t_init_content[] =
75{
76/*  Tag Name        is_multi_v  Ver Block                   Ver No                               Vbitmask   to_calc_cc CC3      OTP     BLPB */
77    {TAG_MIFARE_MID,    TRUE,   T2T_MIFARE_VERSION_BLOCK,   T2T_MIFARE_ULTRALIGHT_VER_NO,        0xFFFF,    FALSE,     0x06,    FALSE,  T2T_DEFAULT_LOCK_BLPB},
78    {TAG_MIFARE_MID,    TRUE,   T2T_MIFARE_VERSION_BLOCK,   T2T_MIFARE_ULTRALIGHT_FAMILY_VER_NO, 0xFFFF,    TRUE,      0x00,    FALSE,  T2T_DEFAULT_LOCK_BLPB},
79    {TAG_KOVIO_MID,     FALSE,  0x00,                       0x00,                                0x0000,    FALSE,     0x1D,    TRUE,   0x04},
80    {TAG_INFINEON_MID,  TRUE,   T2T_INFINEON_VERSION_BLOCK, T2T_INFINEON_MYD_MOVE_LEAN,          0xFFF0,    FALSE,     0x06,    FALSE,  T2T_DEFAULT_LOCK_BLPB},
81    {TAG_INFINEON_MID,  TRUE,   T2T_INFINEON_VERSION_BLOCK, T2T_INFINEON_MYD_MOVE,               0xFFF0,    FALSE,     0x10,    FALSE,  T2T_DEFAULT_LOCK_BLPB},
82    {TAG_BRCM_MID,      TRUE,   T2T_BRCM_VERSION_BLOCK,     T2T_BRCM_STATIC_MEM,                 0xFFFF,    FALSE,     0x06,    FALSE,  T2T_DEFAULT_LOCK_BLPB},
83    {TAG_BRCM_MID,      TRUE,   T2T_BRCM_VERSION_BLOCK,     T2T_BRCM_DYNAMIC_MEM,                0xFFFF,    FALSE,     0x3C,    FALSE,  T2T_DEFAULT_LOCK_BLPB}
84
85};
86
87const UINT8 t4t_v10_ndef_tag_aid[T4T_V10_NDEF_TAG_AID_LEN] = {0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x00};
88const UINT8 t4t_v20_ndef_tag_aid[T4T_V20_NDEF_TAG_AID_LEN] = {0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01};
89
90#if (BT_TRACE_PROTOCOL == TRUE)
91const char * const t1t_cmd_str[] = {
92    "T1T_RID",
93    "T1T_RALL",
94    "T1T_READ",
95    "T1T_WRITE_E",
96    "T1T_WRITE_NE",
97    "T1T_RSEG",
98    "T1T_READ8",
99    "T1T_WRITE_E8",
100    "T1T_WRITE_NE8"
101};
102
103const char * const t2t_cmd_str[] = {
104    "T2T_CMD_READ",
105    "T2T_CMD_WRITE",
106    "T2T_CMD_SEC_SEL"
107};
108#endif
109
110static unsigned int tags_ones32 (register unsigned int x);
111
112/*******************************************************************************
113**
114** Function         t1t_cmd_to_rsp_info
115**
116** Description      This function maps the given opcode to tT1T_CMD_RSP_INFO.
117**
118** Returns          tNFC_STATUS
119**
120*******************************************************************************/
121const tT1T_CMD_RSP_INFO * t1t_cmd_to_rsp_info (UINT8 opcode)
122{
123    const tT1T_CMD_RSP_INFO *p_ret = NULL, *p;
124    int xx;
125
126    for (xx = 0, p=&t1t_cmd_rsp_infos[0]; xx<T1T_MAX_NUM_OPCODES; xx++, p++)
127    {
128        if (opcode == p->opcode)
129        {
130            if ((xx < T1T_STATIC_OPCODES) || (rw_cb.tcb.t1t.hr[0] != T1T_STATIC_HR0))
131                p_ret = p;
132            break;
133        }
134    }
135
136    return p_ret;
137}
138
139
140/*******************************************************************************
141**
142** Function         t1t_tag_init_data
143**
144** Description      This function maps the given opcode to tT1T_INIT_TAG.
145**
146** Returns          tNFC_STATUS
147**
148*******************************************************************************/
149const tT1T_INIT_TAG * t1t_tag_init_data (UINT8 tag_model)
150{
151    const tT1T_INIT_TAG *p_ret = NULL, *p;
152    int xx;
153
154    for (xx = 0, p = &t1t_init_content[0]; xx < T1T_MAX_TAG_MODELS; xx++, p++)
155    {
156        if (tag_model == p->tag_model)
157        {
158            p_ret = p;
159            break;
160        }
161    }
162
163    return p_ret;
164}
165
166/*******************************************************************************
167**
168** Function         t2t_tag_init_data
169**
170** Description      This function maps the given manufacturer id and version to
171**                  tT2T_INIT_TAG.
172**
173** Returns          tNFC_STATUS
174**
175*******************************************************************************/
176const tT2T_INIT_TAG * t2t_tag_init_data (UINT8 manufacturer_id, BOOLEAN b_valid_ver, UINT16 version_no)
177{
178    const tT2T_INIT_TAG *p_ret = NULL, *p;
179    int xx;
180
181    for (xx = 0, p = &t2t_init_content[0]; xx < T2T_MAX_TAG_MODELS; xx++, p++)
182    {
183        if (manufacturer_id == p->manufacturer_id)
184        {
185            if (  (!p->b_multi_version)
186                ||(!b_valid_ver)
187                ||(p->version_no == (version_no & p->version_bmask))  )
188            {
189                p_ret = p;
190                break;
191            }
192        }
193    }
194
195    return p_ret;
196}
197
198/*******************************************************************************
199**
200** Function         t2t_cmd_to_rsp_info
201**
202** Description      This function maps the given opcode to tT2T_CMD_RSP_INFO.
203**
204** Returns          tNFC_STATUS
205**
206*******************************************************************************/
207const tT2T_CMD_RSP_INFO * t2t_cmd_to_rsp_info (UINT8 opcode)
208{
209    const tT2T_CMD_RSP_INFO *p_ret = NULL, *p;
210    int xx;
211
212    for (xx = 0, p = &t2t_cmd_rsp_infos[0]; xx < T2T_MAX_NUM_OPCODES; xx++, p++)
213    {
214        if (opcode == p->opcode)
215        {
216            p_ret = p;
217            break;
218        }
219    }
220
221    return p_ret;
222}
223
224/*******************************************************************************
225**
226** Function         t1t_info_to_evt
227**
228** Description      This function maps the given tT1T_CMD_RSP_INFO to RW/CE event code
229**
230** Returns          RW/CE event code
231**
232*******************************************************************************/
233UINT8 t1t_info_to_evt (const tT1T_CMD_RSP_INFO * p_info)
234{
235    return ((UINT8) (p_info - t1t_cmd_rsp_infos) + RW_T1T_FIRST_EVT);
236}
237
238/*******************************************************************************
239**
240** Function         t2t_info_to_evt
241**
242** Description      This function maps the given tT2T_CMD_RSP_INFO to RW/CE event code
243**
244** Returns          RW/CE event code
245**
246*******************************************************************************/
247UINT8 t2t_info_to_evt (const tT2T_CMD_RSP_INFO * p_info)
248{
249    return ((UINT8) (p_info - t2t_cmd_rsp_infos) + RW_T2T_FIRST_EVT);
250}
251
252#if (BT_TRACE_PROTOCOL == TRUE)
253/*******************************************************************************
254**
255** Function         t1t_info_to_str
256**
257** Description      This function maps the given tT1T_CMD_RSP_INFO to T1T cmd str
258**
259** Returns          T1T cmd str
260**
261*******************************************************************************/
262const char * t1t_info_to_str (const tT1T_CMD_RSP_INFO * p_info)
263{
264    int ind = (int) (p_info - t1t_cmd_rsp_infos);
265    if (ind < T1T_MAX_NUM_OPCODES)
266        return (const char *) t1t_cmd_str[ind];
267    else
268        return "";
269}
270
271/*******************************************************************************
272**
273** Function         t2t_info_to_str
274**
275** Description      This function maps the given tT2T_CMD_RSP_INFO to T2T cmd str
276**
277** Returns          T2T cmd str
278**
279*******************************************************************************/
280const char * t2t_info_to_str (const tT2T_CMD_RSP_INFO * p_info)
281{
282    int ind = (int) (p_info - t2t_cmd_rsp_infos);
283    if (ind < T2T_MAX_NUM_OPCODES)
284        return (const char *) t2t_cmd_str[ind];
285    else
286        return "";
287}
288#endif
289
290/*******************************************************************************
291**
292** Function         tags_pow
293**
294** Description      This function calculates x(base) power of y.
295**
296** Returns          int
297**
298*******************************************************************************/
299int tags_pow (int x, int y)
300{
301    int i, ret = 1;
302    for (i = 0; i < y; i++)
303    {
304        ret *= x;
305    }
306    return ret;
307}
308
309/*******************************************************************************
310**
311** Function         ones32
312**
313** Description      This function returns number of bits set in an unsigned
314**                  integer variable
315**
316** Returns          int
317**
318*******************************************************************************/
319static unsigned int tags_ones32 (register unsigned int x)
320{
321        /* 32-bit recursive reduction using SWAR...
322	   but first step is mapping 2-bit values
323	   into sum of 2 1-bit values in sneaky way
324	*/
325        x -= ((x >> 1) & 0x55555555);
326        x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
327        x = (((x >> 4) + x) & 0x0f0f0f0f);
328        x += (x >> 8);
329        x += (x >> 16);
330        return (x & 0x0000003f);
331}
332
333/*******************************************************************************
334**
335** Function         tags_log2
336**
337** Description      This function calculates log to the base  2.
338**
339** Returns          int
340**
341*******************************************************************************/
342unsigned int tags_log2 (register unsigned int x)
343{
344        x |= (x >> 1);
345        x |= (x >> 2);
346        x |= (x >> 4);
347        x |= (x >> 8);
348        x |= (x >> 16);
349
350        return (tags_ones32 (x) - 1);
351}
352
353#endif /* NFC_INCLUDED == TRUE*/
354