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