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 (0x03U)
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   } /* Key used during NDEF format */
80 
81 #ifndef NCI_MAX_DATA_LEN
82 #define NCI_MAX_DATA_LEN 300
83 #endif
84 
85 /*
86  * Request Id for different commands
87  */
88 typedef enum phNciNfc_ExtnReqId {
89   phNciNfc_e_T1TXchgDataReq = 0x10,   /* T1T Raw Data Request from DH */
90   phNciNfc_e_T1TWriteNReq = 0x20,     /* T1T N bytes write request from DH */
91   phNciNfc_e_MfRawDataXchgHdr = 0x10, /* MF Raw Data Request from DH */
92   phNciNfc_e_MfWriteNReq = 0x31,      /* MF N bytes write request from DH */
93   phNciNfc_e_MfReadNReq = 0x32,       /* MF N bytes read request from DH */
94   phNciNfc_e_MfSectorSelReq = 0x33,   /* MF Block select request from DH */
95   phNciNfc_e_MfPlusProxCheckReq =
96       0x28, /* MF + Prox check request for NFCC from DH */
97   phNciNfc_e_MfcAuthReq =
98       0x40,             /* MFC Authentication request for NFCC from DH */
99   phNciNfc_e_InvalidReq /* Invalid ReqId */
100 } phNciNfc_ExtnReqId_t;
101 
102 /*
103  * Response Ids for different command response
104  */
105 typedef enum phNciNfc_ExtnRespId {
106   phNciNfc_e_T1TXchgDataRsp =
107       0x10, /* DH gets Raw data from T1T on successful req */
108   phNciNfc_e_T1TWriteNRsp = 0x20, /* DH gets write status */
109   phNciNfc_e_MfXchgDataRsp =
110       0x10, /* DH gets Raw data from MF on successful req */
111   phNciNfc_e_MfWriteNRsp = 0x31, /* DH gets write status */
112   phNciNfc_e_MfReadNRsp =
113       0x32, /* DH gets N Bytes read from MF, if successful */
114   phNciNfc_e_MfSectorSelRsp = 0x33, /* DH gets the Sector Select cmd status */
115   phNciNfc_e_MfPlusProxCheckRsp =
116       0x29,                     /* DH gets the MF+ Prox Check cmd status */
117   phNciNfc_e_MfcAuthRsp = 0x40, /* DH gets the authenticate cmd status */
118   phNciNfc_e_InvalidRsp         /* Invalid RspId */
119 } phNciNfc_ExtnRespId_t;
120 
121 /* Data buffer */
122 typedef struct phNciNfc_Buff {
123   uint8_t*
124       pBuff; /* pointer to the buffer where received payload shall be stored*/
125   uint16_t wLen; /* Buffer length*/
126 } phNciNfc_Buff_t, *pphNciNfc_Buff_t;
127 
128 /*
129  * Structure for NCI Extension information
130  */
131 typedef struct phNciNfc_ExtnInfo {
132   union {
133     phNciNfc_ExtnReqId_t ExtnReqId;  /* NCI extension reqid sent */
134     phNciNfc_ExtnRespId_t ExtnRspId; /* NCI extension respid received */
135   } ActivExtnId;                     /* Active Req/Rsp Id */
136   uint8_t bParamsNumsPresent; /* Holds number of params available for this
137                                  Req/Rsp Id */
138   uint8_t bParam[2]; /* Req/Res: Param[0] = Param1, Param[1] = Param2 */
139 } phNciNfc_ExtnInfo_t;
140 
141 /*
142  * NDEF related data structures
143  */
144 typedef struct phLibNfc_NdefInfo {
145   uint8_t NdefContinueRead;
146   uint32_t NdefActualSize;
147   uint32_t AppWrLength;
148   phFriNfc_NdefMap_t* psNdefMap;
149   uint16_t NdefSendRecvLen;
150   phNfc_sData_t* psUpperNdefMsg;
151   uint32_t dwWrLength;
152   uint32_t NdefLength;
153   uint8_t is_ndef;
154   phFriNfc_sNdefSmtCrdFmt_t* ndef_fmt;
155 } phLibNfc_NdefInfo_t;
156 
157 /*
158  * NDEF offsets
159  */
160 typedef enum {
161   phLibNfc_Ndef_EBegin = 0x01, /**<  Start from the beginning position */
162   phLibNfc_Ndef_ECurrent       /**<  Start from the current position */
163 } phLibNfc_Ndef_EOffset_t;
164 
165 /*
166  * Extns module status
167  */
168 typedef enum { EXTNS_STATUS_OPEN = 0, EXTNS_STATUS_CLOSE } phNxpExtns_Status;
169 
170 /*
171  * NCI Data
172  */
173 typedef struct nci_data_package {
174   uint16_t len;
175   uint8_t p_data[NCI_MAX_DATA_LEN];
176 } nci_rsp_data_t;
177 
178 /*
179  * Mifare Callback function definition
180  */
181 typedef void (*CallBackMifare_t)(void*, uint16_t);
182 
183 /*
184  * Auth Cmd Data
185  */
186 typedef struct nci_mfc_package {
187   bool_t auth_status;
188   bool_t auth_sent;
189   sem_t semPresenceCheck;
190   pthread_mutex_t syncmutex;
191   NFCSTATUS status;
192   phNfc_sData_t* pauth_cmd;
193 } phNci_mfc_auth_cmd_t;
194 /*
195  * Structure of callback functions from different module.
196  * It includes the status also.
197  */
198 typedef struct phNxpExtns_Context {
199   phNxpExtns_Status Extns_status;
200   tNFA_DM_CBACK* p_dm_cback;
201   tNFA_CONN_CBACK* p_conn_cback;
202   tNFA_NDEF_CBACK* p_ndef_cback;
203   uint8_t writecmdFlag;
204   uint8_t RawWriteCallBack;
205   void* CallBackCtxt;
206   CallBackMifare_t CallBackMifare;
207   bool_t ExtnsConnect;
208   bool_t ExtnsDeactivate;
209   bool_t ExtnsCallBack;
210   uint8_t incrdecflag;
211   uint8_t incrdecstatusflag;
212 } phNxpExtns_Context_t;
213 
214 NFCSTATUS phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t* pTransceiveInfo,
215                                    phNfc_uCmdList_t Cmd, 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