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