Initialize Tizen 2.3
[adaptation/devices/nfc-plugin-nxp.git] / src / phFriNfc_MapTools.c
1 /*
2  * Copyright (C) 2010 NXP Semiconductors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*!
18  * \file  phFriNfc_MapTools.c
19  * \brief NFC Ndef Internal Mapping File.
20  *
21  * Project: NFC-FRI
22  *
23  * $Date: Fri Oct 15 13:50:54 2010 $
24  * $Author: ing02260 $
25  * $Revision: 1.6 $
26  * $Aliases:  $
27  *
28  */
29
30 #include <phFriNfc_NdefMap.h>
31 #include <phFriNfc_MapTools.h>
32
33 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
34 #include <phFriNfc_MifareULMap.h>
35 #endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
36
37 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
38 #include <phFriNfc_MifareStdMap.h>
39 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
40
41 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
42 #include <phFriNfc_DesfireMap.h>
43 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
44
45 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
46 #include <phFriNfc_FelicaMap.h>
47 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
48
49 #include <phFriNfc_OvrHal.h>
50
51 /*! \ingroup grp_file_attributes
52  *  \name NDEF Mapping
53  *
54  * File: \ref phFriNfc_MapTools.c
55  *       This file has functions which are used common across all the
56  *       typ1/type2/type3/type4 tags.
57  *
58  */
59 /*@{*/
60 #define PHFRINFCNDEFMAP_FILEREVISION "$Revision: 1.6 $"
61 #define PHFRINFCNDEFMAP_FILEALIASES  "$Aliases:  $"
62 /*@}*/
63
64 NFCSTATUS phFriNfc_MapTool_SetCardState(phFriNfc_NdefMap_t  *NdefMap,
65                                         uint32_t            Length)
66 {
67     NFCSTATUS   Result = NFCSTATUS_SUCCESS;
68     if(Length == PH_FRINFC_NDEFMAP_MFUL_VAL0)
69     {
70         /* As the NDEF LEN / TLV Len is Zero, irrespective of any state the card
71            shall be set to INITIALIZED STATE*/
72         NdefMap->CardState =(uint8_t) (((NdefMap->CardState ==
73                                 PH_NDEFMAP_CARD_STATE_READ_ONLY) ||
74                                 (NdefMap->CardState ==
75                                 PH_NDEFMAP_CARD_STATE_INVALID))?
76                                 PH_NDEFMAP_CARD_STATE_INVALID:
77                                 PH_NDEFMAP_CARD_STATE_INITIALIZED);
78     }
79     else
80     {
81         switch(NdefMap->CardState)
82         {
83             case PH_NDEFMAP_CARD_STATE_INITIALIZED:
84                 NdefMap->CardState =(uint8_t) ((NdefMap->CardState ==
85                     PH_NDEFMAP_CARD_STATE_INVALID)?
86                     NdefMap->CardState:
87                     PH_NDEFMAP_CARD_STATE_READ_WRITE);
88             break;
89
90             case PH_NDEFMAP_CARD_STATE_READ_ONLY:
91                 NdefMap->CardState = (uint8_t)((NdefMap->CardState ==
92                     PH_NDEFMAP_CARD_STATE_INVALID)?
93                     NdefMap->CardState:
94                     PH_NDEFMAP_CARD_STATE_READ_ONLY);
95             break;
96
97             case PH_NDEFMAP_CARD_STATE_READ_WRITE:
98                 NdefMap->CardState = (uint8_t)((NdefMap->CardState ==
99                     PH_NDEFMAP_CARD_STATE_INVALID)?
100                     NdefMap->CardState:
101                     PH_NDEFMAP_CARD_STATE_READ_WRITE);
102             break;
103
104             default:
105                 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID;
106                 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
107                             NFCSTATUS_NO_NDEF_SUPPORT);
108             break;
109         }
110     }
111     Result = ((NdefMap->CardState ==
112                 PH_NDEFMAP_CARD_STATE_INVALID)?
113                 PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
114                 NFCSTATUS_NO_NDEF_SUPPORT):
115                 Result);
116     return Result;
117 }
118
119 /*  To check mapping spec version */
120
121 NFCSTATUS   phFriNfc_MapTool_ChkSpcVer( const phFriNfc_NdefMap_t  *NdefMap,
122                                         uint8_t             VersionIndex)
123 {
124     NFCSTATUS status = NFCSTATUS_SUCCESS;
125
126     uint8_t TagVerNo = NdefMap->SendRecvBuf[VersionIndex];
127
128     if ( TagVerNo == 0 )
129     {
130         /*Return Status Error \93 Invalid Format\94*/
131         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_FORMAT);
132     }
133     else
134     {
135         switch (NdefMap->CardType)
136         {
137             case PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD:
138             case PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD:
139             {
140                 /* calculate the major and minor version number of Mifare std version number */
141                 status = (( (( PH_NFCFRI_MFSTDMAP_NFCDEV_MAJOR_VER_NUM ==
142                                 PH_NFCFRI_MFSTDMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
143                             ( PH_NFCFRI_MFSTDMAP_NFCDEV_MINOR_VER_NUM ==
144                                 PH_NFCFRI_MFSTDMAP_GET_MINOR_TAG_VERNO(TagVerNo))) ||
145                             (( PH_NFCFRI_MFSTDMAP_NFCDEV_MAJOR_VER_NUM ==
146                                 PH_NFCFRI_MFSTDMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
147                             ( PH_NFCFRI_MFSTDMAP_NFCDEV_MINOR_VER_NUM <
148                                 PH_NFCFRI_MFSTDMAP_GET_MINOR_TAG_VERNO(TagVerNo) )))?
149                         NFCSTATUS_SUCCESS:
150                         PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
151                                     NFCSTATUS_INVALID_FORMAT));
152                 break;
153             }
154
155 #ifdef DESFIRE_EV1
156             case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1:
157             {
158                 /* calculate the major and minor version number of T3VerNo */
159                 if( (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM_2 ==
160                         PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
161                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MINOR_VER_NUM ==
162                         PH_NFCFRI_NDEFMAP_GET_MINOR_TAG_VERNO(TagVerNo))) ||
163                     (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM_2 ==
164                         PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
165                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MINOR_VER_NUM <
166                         PH_NFCFRI_NDEFMAP_GET_MINOR_TAG_VERNO(TagVerNo) )))
167                 {
168                     status = PHNFCSTVAL(CID_NFC_NONE,NFCSTATUS_SUCCESS);
169                 }
170                 else
171                 {
172                     if (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM_2 <
173                             PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo) ) ||
174                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM_2 >
175                             PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo)))
176                     {
177                         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_FORMAT);
178                     }
179                 }
180                 break;
181             }
182 #endif /* #ifdef DESFIRE_EV1 */
183
184             default:
185             {
186                 /* calculate the major and minor version number of T3VerNo */
187                 if( (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM ==
188                         PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
189                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MINOR_VER_NUM ==
190                         PH_NFCFRI_NDEFMAP_GET_MINOR_TAG_VERNO(TagVerNo))) ||
191                     (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM ==
192                         PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
193                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MINOR_VER_NUM <
194                         PH_NFCFRI_NDEFMAP_GET_MINOR_TAG_VERNO(TagVerNo) )))
195                 {
196                     status = PHNFCSTVAL(CID_NFC_NONE,NFCSTATUS_SUCCESS);
197                 }
198                 else
199                 {
200                     if (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM <
201                             PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo) ) ||
202                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM >
203                             PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo)))
204                     {
205                         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_FORMAT);
206                     }
207                 }
208                 break;
209             }
210
211
212         }
213     }
214
215     return (status);
216 }