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_Desfire.h
     19  * \brief NFC Ndef Mapping For Desfire Smart Card.
     20  *
     21  * Project: NFC-FRI
     22  *
     23  * $Date: Thu Jul 23 13:45:06 2009 $
     24  * $Author: ing07336 $
     25  * $Revision: 1.4 $
     26  * $Aliases: NFC_FRI1.1_WK930_R30_1,NFC_FRI1.1_WK934_PREP_1,NFC_FRI1.1_WK934_R31_1,NFC_FRI1.1_WK941_PREP1,NFC_FRI1.1_WK941_PREP2,NFC_FRI1.1_WK941_1,NFC_FRI1.1_WK943_R32_1,NFC_FRI1.1_WK949_PREP1,NFC_FRI1.1_WK943_R32_10,NFC_FRI1.1_WK943_R32_13,NFC_FRI1.1_WK943_R32_14,NFC_FRI1.1_WK1007_R33_1,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $
     27  *
     28  */
     29 
     30 #ifndef PHFRINFC_DESFIREMAP_H
     31 #define PHFRINFC_DESFIREMAP_H
     32 
     33 #include <phFriNfc.h>
     34 #if !defined PH_HAL4_ENABLE
     35 #include <phHal4Nfc.h>
     36 #endif
     37 #include <phNfcTypes.h>
     38 #include <phNfcStatus.h>
     39 #include <phFriNfc_NdefMap.h>
     40 
     41 
     42 
     43 /*!
     44  * \name Desfire - Standard constants
     45  *
     46  */
     47 /*@{*/
     48 #define PH_FRINFC_NDEFMAP_DESF_READ_OP                              2  /*!< Desfire Operation Flag is Read */
     49 #define PH_FRINFC_NDEFMAP_DESF_WRITE_OP                             3  /*!< Desfire Operation Flag is Write */
     50 #define PH_FRINFC_NDEFMAP_DESF_NDEF_CHK_OP                          4  /*!< Desfire Operation Flag is Check Ndef */
     51 #define PH_FRINFC_NDEFMAP_DESF_GET_LEN_OP                           5
     52 #define PH_FRINFC_NDEFMAP_DESF_SET_LEN_OP                           6
     53 #define PH_FRINFC_NDEFMAP_DESF_RESP_OFFSET                          2  /*!< Two Status Flag at the end of the
     54                                                                             Receive buffer*/
     55 #define PH_FRINFC_NDEFMAP_DESF_CAPDU_SMARTTAG_PKT_SIZE              12 /*!< Send Length for Smart Tag function*/
     56 #define PH_FRINFC_NDEFMAP_DESF_CAPDU_SELECT_FILE_PKT_SIZE           7  /*!< Send Length for Select File function */
     57 #define PH_FRINFC_NDEFMAP_DESF_CAPDU_READ_BIN_PKT_SIZE              5  /*!< Send Length for Reading a Packet */
     58 
     59 /*!
     60  * \name NDEF Mapping - states of the Finite State machine
     61  *
     62  */
     63 /*@{*/
     64 
     65 #define PH_FRINFC_NDEFMAP_DESF_STATE_SELECT_SMART_TAG               5     /*!< Selection of Smart Tag is going on */
     66 #define PH_FRINFC_NDEFMAP_DESF_STATE_SELECT_FILE                    6     /*!< Selecting a file to read/write */
     67 #define PH_FRINFC_NDEFMAP_DESF_STATE_READ_CAP_CONT                  7     /*!< Reading a capability container */
     68 #define PH_FRINFC_NDEFMAP_DESF_STATE_READ_BIN                       8     /*!< Reading from the card */
     69 #define PH_FRINFC_NDEFMAP_DESF_STATE_UPDATE_BIN_BEGIN               60    /*!< Writing to the card */
     70 #define PH_FRINFC_NDEFMAP_DESF_STATE_UPDATE_BIN_END                 61    /*!< Writing to the card */
     71 
     72 #define PH_FRINFC_NDEFMAP_DESF_STATE_CHK_NDEF                       10    /*!< Check Ndef is in progress */
     73 #define PH_FRINFC_NDEFMAP_DESF_TLV_INDEX                            7     /*!< Specifies the index of TLV Structure */
     74 #define PH_FRINFC_NDEFMAP_DESF_NDEF_CNTRL_TLV                       0x04  /*!< Specifies the NDEF File Cntrl TLV */
     75 #define PH_FRINFC_NDEFMAP_DESF_PROP_CNTRL_TLV                       0x05  /*!< Specifies the Propreitary File Cntrl TLV */
     76 
     77 /* Following Constants are used to navigate the Capability Container(CC)*/
     78 
     79 /*!< Following two indexes represents the CCLEN in CC*/
     80 #define PH_FRINFC_NDEFMAP_DESF_CCLEN_BYTE_FIRST_INDEX               0
     81 #define PH_FRINFC_NDEFMAP_DESF_CCLEN_BYTE_SECOND_INDEX              1
     82 
     83 /*!< Specifies the index of the Mapping Version in CC */
     84 #define PH_FRINFC_NDEFMAP_DESF_VER_INDEX                            2
     85 
     86 /*!< Following two indexes represents the MLe bytes in CC*/
     87 #define PH_FRINFC_NDEFMAP_DESF_MLE_BYTE_FIRST_INDEX                 3
     88 #define PH_FRINFC_NDEFMAP_DESF_MLE_BYTE_SECOND_INDEX                4
     89 
     90 /*!< Following two indexes represents the MLc bytes in CC*/
     91 #define PH_FRINFC_NDEFMAP_DESF_MLC_BYTE_FIRST_INDEX                 5
     92 #define PH_FRINFC_NDEFMAP_DESF_MLC_BYTE_SECOND_INDEX                6
     93 
     94 /*!< Specifies the index of the TLV in CC */
     95 #define PH_FRINFC_NDEFMAP_DESF_TLV_INDEX                            7
     96 
     97 /*!< Specifies the index of the TLV  length in CC */
     98 #define PH_FRINFC_NDEFMAP_DESF_TLV_LEN_INDEX                        8
     99 
    100 /*!< Following two indexes represents the NDEF file identifier in CC*/
    101 #define PH_FRINFC_NDEFMAP_DESF_NDEF_FILEID_BYTE_FIRST_INDEX         9
    102 #define PH_FRINFC_NDEFMAP_DESF_NDEF_FILEID_BYTE_SECOND_INDEX        10
    103 
    104 /*!< Following two indexes represents the NDEF file size in CC */
    105 #define PH_FRINFC_NDEFMAP_DESF_NDEF_FILESZ_BYTE_FIRST_INDEX         11
    106 #define PH_FRINFC_NDEFMAP_DESF_NDEF_FILESZ_BYTE_SECOND_INDEX        12
    107 
    108 /*!< Specifies the index of the NDEF file READ access byte in CC */
    109 #define PH_FRINFC_NDEFMAP_DESF_NDEF_FILERD_ACCESS_INDEX             13
    110 
    111 /*!< Specifies the index of the NDEF file WRITE access byte in CC */
    112 #define PH_FRINFC_NDEFMAP_DESF_NDEF_FILEWR_ACCESS_INDEX             14
    113 
    114 
    115 /* Macros to find Maximum NDEF File Size*/
    116 #define PH_NFCFRI_NDEFMAP_DESF_NDEF_FILE_SIZE                       (NdefMap->DesfireCapContainer.NdefFileSize - 2)
    117 /* Specifies the size of the NLEN Bytes*/
    118 #define PH_FRINFC_NDEFMAP_DESF_NLEN_SIZE_IN_BYTES                    2
    119 
    120 
    121 /* Following constants are used with buffer index's*/
    122 #define PH_FRINFC_NDEFMAP_DESF_SW1_INDEX            0
    123 #define PH_FRINFC_NDEFMAP_DESF_SW2_INDEX            1
    124 
    125 
    126 /* Following constants are used for SW1 SW2 status codes*/
    127 #define PH_FRINFC_NDEFMAP_DESF_RAPDU_SW1_BYTE                    0x90
    128 #define PH_FRINFC_NDEFMAP_DESF_RAPDU_SW2_BYTE                    0x00
    129 
    130 
    131 /* Following constatnts for shift bytes*/
    132 #define PH_FRINFC_NDEFMAP_DESF_SHL8                             8
    133 
    134 
    135 #define PH_FRINFC_DESF_GET_VER_CMD                          0x60
    136 #define PH_FRINFC_DESF_NATIVE_CLASS_BYTE                    0x90
    137 #define PH_FRINFC_DESF_NATIVE_OFFSET_P1                     0x00
    138 #define PH_FRINFC_DESF_NATIVE_OFFSET_P2                     0x00
    139 #define PH_FRINFC_DESF_NATIVE_GETVER_RESP                   0xAF
    140 /*!
    141 * \name NDEF Mapping - states of the Finite State machine
    142 *
    143 */
    144 /*@{*/
    145 
    146 typedef enum
    147 {
    148     PH_FRINFC_DESF_STATE_GET_UID,
    149     PH_FRINFC_DESF_STATE_GET_SW_VERSION,
    150     PH_FRINFC_DESF_STATE_GET_HW_VERSION
    151 
    152 }phFriNfc_eMapDesfireState;
    153 
    154 typedef enum
    155 {
    156     PH_FRINFC_DESF_IDX_0,
    157     PH_FRINFC_DESF_IDX_1,
    158     PH_FRINFC_DESF_IDX_2,
    159     PH_FRINFC_DESF_IDX_3,
    160     PH_FRINFC_DESF_IDX_4,
    161     PH_FRINFC_DESF_IDX_5
    162 
    163 }phFriNfc_eMapDesfireId;
    164 
    165 #define PH_FRINFC_DESF_ISO_NATIVE_WRAPPER() \
    166     do \
    167 {\
    168     NdefMap->SendRecvBuf[PH_FRINFC_DESF_IDX_0] = PH_FRINFC_DESF_NATIVE_CLASS_BYTE;\
    169     NdefMap->SendRecvBuf[PH_FRINFC_DESF_IDX_2] = PH_FRINFC_DESF_NATIVE_OFFSET_P1;\
    170     NdefMap->SendRecvBuf[PH_FRINFC_DESF_IDX_3] = PH_FRINFC_DESF_NATIVE_OFFSET_P2;\
    171     switch(NdefMap->State)\
    172 {\
    173     case PH_FRINFC_DESF_STATE_GET_HW_VERSION :\
    174     case PH_FRINFC_DESF_STATE_GET_SW_VERSION :\
    175     case PH_FRINFC_DESF_STATE_GET_UID :\
    176     if ( NdefMap->State == PH_FRINFC_DESF_STATE_GET_HW_VERSION  )\
    177 {\
    178     NdefMap->SendRecvBuf[PH_FRINFC_DESF_IDX_1] = PH_FRINFC_DESF_GET_VER_CMD;\
    179 }\
    180         else\
    181 {\
    182     NdefMap->SendRecvBuf[PH_FRINFC_DESF_IDX_1] = 0xAF;\
    183 }\
    184     NdefMap->SendRecvBuf[PH_FRINFC_DESF_IDX_4] = 0x00;\
    185     NdefMap->SendLength = PH_FRINFC_DESF_IDX_5;\
    186     break;\
    187     default :\
    188     break;\
    189 }\
    190 } while(0)\
    191 
    192 
    193 
    194 
    195 
    196 /*!
    197  * \brief \copydoc page_ovr Initiates Reading of NDEF information from the Remote Device.
    198  *
    199  * The function initiates the reading of NDEF information from a Remote Device.
    200  * It performs a reset of the state and starts the action (state machine).
    201  * A periodic call of the \ref phFriNfc_NdefMap_Process has to be done once the action
    202  * has been triggered.
    203  *
    204  * \param[in] NdefMap Pointer to a valid instance of the \ref phFriNfc_NdefMap_t structure describing
    205  *                    the component context.
    206  *
    207  * \param[in] PacketData  Pointer to a location that receives the NDEF Packet.
    208  *
    209  * \param[in,out] PacketDataLength Pointer to a variable receiving the length of the NDEF packet.
    210  *
    211  * \param[in] Offset Indicates whether the read operation shall start from the begining of the
    212  *            file/card storage \b or continue from the last offset. The last Offset set is stored
    213  *            within a context variable (must not be modified by the integration).
    214  *            If the caller sets the value to \ref PH_FRINFC_NDEFMAP_SEEK_CUR, the component shall
    215  *            start reading from the last offset set (continue where it has stopped before).
    216  *            If set to \ref PH_FRINFC_NDEFMAP_SEEK_BEGIN, the component shall start reading
    217  *            from the begining of the card (restarted)
    218  *
    219  * \retval NFCSTATUS_PENDING                       The action has been successfully triggered.
    220  * \retval NFCSTATUS_INVALID_DEVICE_REQUEST        If Previous Operation is Write Ndef and Offset
    221  *                                                 is Current then this error is displayed.
    222  * \retval NFCSTATUS_EOF_NDEF_CONTAINER_REACHED              No Space in the File to read.
    223  * \retval NFCSTATUS_MORE_INFORMATION              There are more bytes to read in the card.
    224  * \retval NFCSTATUS_SUCCESS                       Last Byte of the card read.
    225  * \retval NFCSTATUS_INVALID_DEVICE                The device has not been opened or has been disconnected
    226  *                                                 meanwhile.
    227  * \retval NFCSTATUS_CMD_ABORTED                   The caller/driver has aborted the request.
    228  * \retval NFCSTATUS_BUFFER_TOO_SMALL              The buffer provided by the caller is too small.
    229  * \retval NFCSTATUS_RF_TIMEOUT                    No data has been received within the TIMEOUT period.
    230  *
    231  */
    232 
    233 NFCSTATUS phFriNfc_Desfire_RdNdef(  phFriNfc_NdefMap_t  *NdefMap,
    234                                     uint8_t             *PacketData,
    235                                     uint32_t            *PacketDataLength,
    236                                     uint8_t             Offset);
    237 
    238 /*!
    239  * \brief \copydoc page_ovr Initiates Writing of NDEF information to the Remote Device.
    240  *
    241  * The function initiates the writing of NDEF information to a Remote Device.
    242  * It performs a reset of the state and starts the action (state machine).
    243  * A periodic call of the \ref phFriNfc_NdefMap_Process has to be done once the action
    244  * has been triggered.
    245  *
    246  * \param[in] NdefMap Pointer to a valid instance of the \ref phFriNfc_NdefMap_t structure describing
    247  *                    the component context.
    248  *
    249  * \param[in] PacketData  Pointer to a location that holds the prepared NDEF Packet.
    250  *
    251  * \param[in,out] PacketDataLength Variable specifying the length of the prepared NDEF packet.
    252  *
    253  * \param[in] Offset Indicates whether the write operation shall start from the begining of the
    254  *            file/card storage \b or continue from the last offset. The last Offset set is stored
    255  *            within a context variable (must not be modified by the integration).
    256  *            If the caller sets the value to \ref PH_FRINFC_NDEFMAP_SEEK_CUR, the component shall
    257  *            start writing from the last offset set (continue where it has stopped before).
    258  *            If set to \ref PH_FRINFC_NDEFMAP_SEEK_BEGIN, the component shall start writing
    259  *            from the begining of the card (restarted)
    260  *
    261  * \retval NFCSTATUS_PENDING                        The action has been successfully triggered.
    262  * \retval NFCSTATUS_INVALID_DEVICE_REQUEST         If Previous Operation is Write Ndef and Offset
    263  *                                                  is Current then this error is displayed.
    264  * \retval NFCSTATUS_EOF_NDEF_CONTAINER_REACHED               Last byte is written to the card after this
    265  *                                                  no further writing is possible.
    266  * \retval NFCSTATUS_SUCCESS                        Buffer provided by the user is completely written
    267  *                                                  into the card.
    268  * \retval NFCSTATUS_INVALID_DEVICE                 The device has not been opened or has been disconnected
    269  *                                                  meanwhile.
    270  * \retval NFCSTATUS_CMD_ABORTED                    The caller/driver has aborted the request.
    271  * \retval NFCSTATUS_BUFFER_TOO_SMALL               The buffer provided by the caller is too small.
    272  * \retval NFCSTATUS_RF_TIMEOUT                     No data has been received within the TIMEOUT period.
    273  *
    274  */
    275 
    276 NFCSTATUS phFriNfc_Desfire_WrNdef(  phFriNfc_NdefMap_t  *NdefMap,
    277                                     uint8_t             *PacketData,
    278                                     uint32_t            *PacketDataLength,
    279                                     uint8_t             Offset);
    280 
    281 /*!
    282  * \brief \copydoc page_ovr Check whether a particulat Remote Device is NDEF compliant.
    283  *
    284  * The function checks whether the peer device is NDEF compliant.
    285  *
    286  * \param[in] NdefMap Pointer to a valid instance of the \ref phFriNfc_NdefMap_t structure describing
    287  *                    the component context.
    288  *
    289  * \retval NFCSTATUS_PENDING               The action has been successfully triggered.
    290  * \retval NFCSTATUS_INVALID_PARAMETER     At least one parameter of the function is invalid.
    291  * \retval NFCSTATUS_INVALID_DEVICE         The device has not been opened or has been disconnected
    292  *                                          meanwhile.
    293  * \retval NFCSTATUS_CMD_ABORTED            The caller/driver has aborted the request.
    294  * \retval NFCSTATUS_BUFFER_TOO_SMALL       The buffer provided by the caller is too small.
    295  * \retval NFCSTATUS_RF_TIMEOUT             No data has been received within the TIMEOUT period.
    296  *
    297  */
    298 
    299 NFCSTATUS phFriNfc_Desfire_ChkNdef( phFriNfc_NdefMap_t  *NdefMap);
    300 
    301 /*!
    302  * \brief \copydoc page_cb Completion Routine, Processing function, needed to avoid long blocking.
    303  *
    304  * The function call scheme is according to \ref grp_interact. No State reset is performed during operation.
    305  *
    306  * \copydoc pphFriNfc_Cr_t
    307  *
    308  * \note The lower (Overlapped HAL) layer must register a pointer to this function as a Completion
    309  *       Routine in order to be able to notify the component that an I/O has finished and data are
    310  *       ready to be processed.
    311  *
    312  */
    313 
    314 void phFriNfc_Desfire_Process(  void        *Context,
    315                                 NFCSTATUS   Status);
    316 
    317 
    318 #endif /* PHFRINFC_DESFIREMAP_H */
    319 
    320