Home | History | Annotate | Download | only in src
      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                 if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD ||
    103                     NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD)
    104                 {
    105                     if(NdefMap->StdMifareContainer.ReadOnlySectorIndex &&
    106                        NdefMap->StdMifareContainer.SectorTrailerBlockNo == NdefMap->StdMifareContainer.currentBlock )
    107                     {
    108                         NdefMap->CardState = (uint8_t)((NdefMap->CardState ==
    109                                                         PH_NDEFMAP_CARD_STATE_INVALID)?
    110                                                         NdefMap->CardState:
    111                                                         PH_NDEFMAP_CARD_STATE_READ_ONLY);
    112                     }
    113                 }
    114             break;
    115 
    116             default:
    117                 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID;
    118                 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    119                             NFCSTATUS_NO_NDEF_SUPPORT);
    120             break;
    121         }
    122     }
    123     Result = ((NdefMap->CardState ==
    124                 PH_NDEFMAP_CARD_STATE_INVALID)?
    125                 PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    126                 NFCSTATUS_NO_NDEF_SUPPORT):
    127                 Result);
    128     return Result;
    129 }
    130 
    131 /*  To check mapping spec version */
    132 
    133 NFCSTATUS   phFriNfc_MapTool_ChkSpcVer( const phFriNfc_NdefMap_t  *NdefMap,
    134                                         uint8_t             VersionIndex)
    135 {
    136     NFCSTATUS status = NFCSTATUS_SUCCESS;
    137 
    138     uint8_t TagVerNo = NdefMap->SendRecvBuf[VersionIndex];
    139 
    140     if ( TagVerNo == 0 )
    141     {
    142         /*Return Status Error  Invalid Format*/
    143         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_FORMAT);
    144     }
    145     else
    146     {
    147         switch (NdefMap->CardType)
    148         {
    149             case PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD:
    150             case PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD:
    151             {
    152                 /* calculate the major and minor version number of Mifare std version number */
    153                 status = (( (( PH_NFCFRI_MFSTDMAP_NFCDEV_MAJOR_VER_NUM ==
    154                                 PH_NFCFRI_MFSTDMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
    155                             ( PH_NFCFRI_MFSTDMAP_NFCDEV_MINOR_VER_NUM ==
    156                                 PH_NFCFRI_MFSTDMAP_GET_MINOR_TAG_VERNO(TagVerNo))) ||
    157                             (( PH_NFCFRI_MFSTDMAP_NFCDEV_MAJOR_VER_NUM ==
    158                                 PH_NFCFRI_MFSTDMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
    159                             ( PH_NFCFRI_MFSTDMAP_NFCDEV_MINOR_VER_NUM <
    160                                 PH_NFCFRI_MFSTDMAP_GET_MINOR_TAG_VERNO(TagVerNo) )))?
    161                         NFCSTATUS_SUCCESS:
    162                         PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    163                                     NFCSTATUS_INVALID_FORMAT));
    164                 break;
    165             }
    166 
    167 #ifdef DESFIRE_EV1
    168             case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1:
    169             {
    170                 /* calculate the major and minor version number of T3VerNo */
    171                 if( (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM_2 ==
    172                         PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
    173                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MINOR_VER_NUM ==
    174                         PH_NFCFRI_NDEFMAP_GET_MINOR_TAG_VERNO(TagVerNo))) ||
    175                     (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM_2 ==
    176                         PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
    177                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MINOR_VER_NUM <
    178                         PH_NFCFRI_NDEFMAP_GET_MINOR_TAG_VERNO(TagVerNo) )))
    179                 {
    180                     status = PHNFCSTVAL(CID_NFC_NONE,NFCSTATUS_SUCCESS);
    181                 }
    182                 else
    183                 {
    184                     if (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM_2 <
    185                             PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo) ) ||
    186                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM_2 >
    187                             PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo)))
    188                     {
    189                         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_FORMAT);
    190                     }
    191                 }
    192                 break;
    193             }
    194 #endif /* #ifdef DESFIRE_EV1 */
    195 
    196             default:
    197             {
    198                 /* calculate the major and minor version number of T3VerNo */
    199                 if( (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM ==
    200                         PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
    201                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MINOR_VER_NUM ==
    202                         PH_NFCFRI_NDEFMAP_GET_MINOR_TAG_VERNO(TagVerNo))) ||
    203                     (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM ==
    204                         PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&&
    205                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MINOR_VER_NUM <
    206                         PH_NFCFRI_NDEFMAP_GET_MINOR_TAG_VERNO(TagVerNo) )))
    207                 {
    208                     status = PHNFCSTVAL(CID_NFC_NONE,NFCSTATUS_SUCCESS);
    209                 }
    210                 else
    211                 {
    212                     if (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM <
    213                             PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo) ) ||
    214                     ( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM >
    215                             PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo)))
    216                     {
    217                         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_FORMAT);
    218                     }
    219                 }
    220                 break;
    221             }
    222 
    223 
    224         }
    225     }
    226 
    227     return (status);
    228 }
    229