Home | History | Annotate | Download | only in mifare
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      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 #ifndef _PHNXPEXTNS_MFCRF_H_
     18 #define _PHNXPEXTNS_MFCRF_H_
     19 
     20 #include <phFriNfc_MifStdFormat.h>
     21 #include <phFriNfc_MifareStdMap.h>
     22 #include <phNciNfcTypes.h>
     23 #include <phNfcTypes.h>
     24 #include <phNxpExtns.h>
     25 #include <pthread.h>
     26 #include <semaphore.h>
     27 
     28 extern uint8_t current_key[];
     29 /* Enable this macro to set key configuration for mifare classic Tag */
     30 #define PHLIBNFC_NXPETENSION_CONFIGURE_MFKEYS 1
     31 
     32 #define MAX_BUFF_SIZE (512)
     33 
     34 #define PHLIBNFC_MIFARESTD4K_BLK128 128   /*Block number 128 for Mifare 4k */
     35 #define PHLIBNFC_MIFARESTD_SECTOR_NO32 32 /* Sector 32 for Mifare 4K*/
     36 #define PHLIBNFC_MIFARESTD_BLOCK_BYTES \
     37   16 /* Bytes per block after block 32 for Mifare 4K*/
     38 
     39 #define PHLIBNFC_NO_OF_BLKPERSECTOR         \
     40   (0x04) /* Number of blocks per sector for \
     41           * Mifare Clsssic Tag*/
     42 
     43 #define PHLIBNFC_MFCUIDLEN_INAUTHCMD \
     44   0x04                               /* UID length in Authentication command */
     45 #define PHLIBNFC_MFC_AUTHKEYLEN 0x06 /* Authentication key length */
     46 
     47 #define PHNCINFC_AUTHENTICATION_KEY                 \
     48   (0x00U) /* Authentication key passed in extension \
     49              command header of authentication command */
     50 #define PHNCINFC_AUTHENTICATION_KEYB (0x61U) /* Authentication Key B */
     51 #define PHNCINFC_ENABLE_KEY_B (0x80U)        /* Enable Key B */
     52 #define PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY \
     53   (0x10) /* MIFARE Classic use Embedded Key*/
     54 
     55 #define PH_NCINFC_STATUS_OK (0x0000) /* Status OK */
     56 #define PHNCINFC_EXTNID_SIZE (0x01U) /* Size of Mifare Extension Req/Rsp Id */
     57 #define PHNCINFC_EXTNSTATUS_SIZE \
     58   (0x01U) /* Size of Mifare Extension Resp Status Byte */
     59 
     60 #define PH_NCINFC_EXTN_INVALID_PARAM_VAL \
     61   (0xFFU) /* Initial value of Req/Resp Param/Status */
     62 
     63 #define PH_FRINFC_NDEF_READ_REQ (0x00U)  /* NDEF Read Request */
     64 #define PH_FRINFC_NDEF_WRITE_REQ (0x01U) /* NDEF Write Request */
     65 
     66 #define PH_LIBNFC_INTERNAL_CHK_NDEF_NOT_DONE \
     67   (0x02U) /* Status for check NDEF not done */
     68 
     69 #define NDEF_SENDRCV_BUF_LEN 252U /* Send receive buffer length */
     70 
     71 #define NXP_NUMBER_OF_MFC_KEYS (0x04U)
     72 #define NXP_MFC_KEY_SIZE (0x06U)
     73 
     74 #define NXP_MFC_KEYS                            \
     75   {                                             \
     76     {0xA0, 0XA1, 0xA2, 0XA3, 0xA4, 0XA5},       \
     77         {0xD3, 0XF7, 0xD3, 0XF7, 0xD3, 0XF7},   \
     78         {0xFF, 0XFF, 0xFF, 0XFF, 0xFF, 0XFF}, { \
     79       0x00, 0x00, 0x00, 0x00, 0x00, 0x00        \
     80     }                                           \
     81   } /* Key used during NDEF format */
     82 
     83 #ifndef NCI_MAX_DATA_LEN
     84 #define NCI_MAX_DATA_LEN 300
     85 #endif
     86 
     87 /*
     88  * Request Id for different commands
     89  */
     90 typedef enum phNciNfc_ExtnReqId {
     91   phNciNfc_e_T1TXchgDataReq = 0x10,   /* T1T Raw Data Request from DH */
     92   phNciNfc_e_T1TWriteNReq = 0x20,     /* T1T N bytes write request from DH */
     93   phNciNfc_e_MfRawDataXchgHdr = 0x10, /* MF Raw Data Request from DH */
     94   phNciNfc_e_MfWriteNReq = 0x31,      /* MF N bytes write request from DH */
     95   phNciNfc_e_MfReadNReq = 0x32,       /* MF N bytes read request from DH */
     96   phNciNfc_e_MfSectorSelReq = 0x33,   /* MF Block select request from DH */
     97   phNciNfc_e_MfPlusProxCheckReq =
     98       0x28, /* MF + Prox check request for NFCC from DH */
     99   phNciNfc_e_MfcAuthReq =
    100       0x40,             /* MFC Authentication request for NFCC from DH */
    101   phNciNfc_e_InvalidReq /* Invalid ReqId */
    102 } phNciNfc_ExtnReqId_t;
    103 
    104 /*
    105  * Response Ids for different command response
    106  */
    107 typedef enum phNciNfc_ExtnRespId {
    108   phNciNfc_e_T1TXchgDataRsp =
    109       0x10, /* DH gets Raw data from T1T on successful req */
    110   phNciNfc_e_T1TWriteNRsp = 0x20, /* DH gets write status */
    111   phNciNfc_e_MfXchgDataRsp =
    112       0x10, /* DH gets Raw data from MF on successful req */
    113   phNciNfc_e_MfWriteNRsp = 0x31, /* DH gets write status */
    114   phNciNfc_e_MfReadNRsp =
    115       0x32, /* DH gets N Bytes read from MF, if successful */
    116   phNciNfc_e_MfSectorSelRsp = 0x33, /* DH gets the Sector Select cmd status */
    117   phNciNfc_e_MfPlusProxCheckRsp =
    118       0x29,                     /* DH gets the MF+ Prox Check cmd status */
    119   phNciNfc_e_MfcAuthRsp = 0x40, /* DH gets the authenticate cmd status */
    120   phNciNfc_e_InvalidRsp         /* Invalid RspId */
    121 } phNciNfc_ExtnRespId_t;
    122 
    123 /* Data buffer */
    124 typedef struct phNciNfc_Buff {
    125   uint8_t*
    126       pBuff; /* pointer to the buffer where received payload shall be stored*/
    127   uint16_t wLen; /* Buffer length*/
    128 } phNciNfc_Buff_t, *pphNciNfc_Buff_t;
    129 
    130 /*
    131  * Structure for NCI Extension information
    132  */
    133 typedef struct phNciNfc_ExtnInfo {
    134   union {
    135     phNciNfc_ExtnReqId_t ExtnReqId;  /* NCI extension reqid sent */
    136     phNciNfc_ExtnRespId_t ExtnRspId; /* NCI extension respid received */
    137   } ActivExtnId;                     /* Active Req/Rsp Id */
    138   uint8_t bParamsNumsPresent; /* Holds number of params available for this
    139                                  Req/Rsp Id */
    140   uint8_t bParam[2]; /* Req/Res: Param[0] = Param1, Param[1] = Param2 */
    141 } phNciNfc_ExtnInfo_t;
    142 
    143 /*
    144  * NDEF related data structures
    145  */
    146 typedef struct phLibNfc_NdefInfo {
    147   uint8_t NdefContinueRead;
    148   uint32_t NdefActualSize;
    149   uint32_t AppWrLength;
    150   phFriNfc_NdefMap_t* psNdefMap;
    151   uint16_t NdefSendRecvLen;
    152   phNfc_sData_t* psUpperNdefMsg;
    153   uint32_t dwWrLength;
    154   uint32_t NdefLength;
    155   uint8_t is_ndef;
    156   phFriNfc_sNdefSmtCrdFmt_t* ndef_fmt;
    157 } phLibNfc_NdefInfo_t;
    158 
    159 /*
    160  * NDEF offsets
    161  */
    162 typedef enum {
    163   phLibNfc_Ndef_EBegin = 0x01, /**<  Start from the beginning position */
    164   phLibNfc_Ndef_ECurrent       /**<  Start from the current position */
    165 } phLibNfc_Ndef_EOffset_t;
    166 
    167 /*
    168  * Extns module status
    169  */
    170 typedef enum { EXTNS_STATUS_OPEN = 0, EXTNS_STATUS_CLOSE } phNxpExtns_Status;
    171 
    172 /*
    173  * NCI Data
    174  */
    175 typedef struct nci_data_package {
    176   uint16_t len;
    177   uint8_t p_data[NCI_MAX_DATA_LEN];
    178 } nci_rsp_data_t;
    179 
    180 /*
    181  * Mifare Callback function definition
    182  */
    183 typedef void (*CallBackMifare_t)(void*, uint16_t);
    184 
    185 /*
    186  * Auth Cmd Data
    187  */
    188 typedef struct nci_mfc_package {
    189   bool_t auth_status;
    190   bool_t auth_sent;
    191   sem_t semPresenceCheck;
    192   pthread_mutex_t syncmutex;
    193   NFCSTATUS status;
    194   phNfc_sData_t* pauth_cmd;
    195 } phNci_mfc_auth_cmd_t;
    196 /*
    197  * Structure of callback functions from different module.
    198  * It includes the status also.
    199  */
    200 typedef struct phNxpExtns_Context {
    201   phNxpExtns_Status Extns_status;
    202   tNFA_DM_CBACK* p_dm_cback;
    203   tNFA_CONN_CBACK* p_conn_cback;
    204   tNFA_NDEF_CBACK* p_ndef_cback;
    205   uint8_t writecmdFlag;
    206   uint8_t RawWriteCallBack;
    207   void* CallBackCtxt;
    208   CallBackMifare_t CallBackMifare;
    209   bool_t ExtnsConnect;
    210   bool_t ExtnsDeactivate;
    211   bool_t ExtnsCallBack;
    212   uint8_t incrdecflag;
    213   uint8_t incrdecstatusflag;
    214 } phNxpExtns_Context_t;
    215 
    216 NFCSTATUS phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t* pTransceiveInfo,
    217                                    phNfc_uCmdList_t Cmd, uint8_t* SendRecvBuf,
    218                                    uint16_t SendLength,
    219                                    uint16_t* SendRecvLength);
    220 NFCSTATUS phNxpExtns_MfcModuleInit(void);
    221 NFCSTATUS phNxpExtns_MfcModuleDeInit(void);
    222 NFCSTATUS Mfc_WriteNdef(uint8_t* p_data, uint32_t len);
    223 NFCSTATUS Mfc_CheckNdef(void);
    224 NFCSTATUS Mfc_ReadNdef(void);
    225 NFCSTATUS Mfc_FormatNdef(uint8_t* secretkey, uint8_t len);
    226 NFCSTATUS Mfc_Transceive(uint8_t* p_data, uint32_t len);
    227 NFCSTATUS Mfc_SetReadOnly(uint8_t* secrtkey, uint8_t len);
    228 void Mfc_DeactivateCbackSelect(void);
    229 void Mfc_ActivateCback(void);
    230 NFCSTATUS Mfc_RecvPacket(uint8_t* buff, uint8_t buffSz);
    231 NFCSTATUS phNxNciExtns_MifareStd_Reconnect(void);
    232 NFCSTATUS Mfc_PresenceCheck(void);
    233 
    234 #endif /* _PHNXPEXTNS_MFCRF_H_ */
    235