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 #include <android-base/stringprintf.h>
18 #include <base/logging.h>
19 #include <log/log.h>
20 #include <nfc_api.h>
21 #include <nfc_int.h>
22 #include <phNfcCompId.h>
23 #include <phNxpExtns_MifareStd.h>
24 #include <rw_api.h>
25 
26 using android::base::StringPrintf;
27 
28 extern bool nfc_debug_enabled;
29 
30 phNxpExtns_Context_t gphNxpExtns_Context;
31 phNciNfc_TransceiveInfo_t tNciTranscvInfo;
32 phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt = NULL;
33 phFriNfc_NdefMap_t* NdefMap = NULL;
34 phLibNfc_NdefInfo_t NdefInfo;
35 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
36 pthread_mutex_t SharedDataMutex = PTHREAD_MUTEX_INITIALIZER;
37 #endif
38 uint8_t current_key[PHLIBNFC_MFC_AUTHKEYLEN] = {0};
39 phNci_mfc_auth_cmd_t gAuthCmdBuf;
40 static NFCSTATUS phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,
41                                     uint8_t* buff, uint16_t* buffSz);
42 static NFCSTATUS phLibNfc_SendRawCmd(
43     phNfc_sTransceiveInfo_t* pTransceiveInfo,
44     pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
45 static NFCSTATUS phLibNfc_SendWrt16Cmd(
46     phNfc_sTransceiveInfo_t* pTransceiveInfo,
47     pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
48 static NFCSTATUS phLibNfc_SendAuthCmd(
49     phNfc_sTransceiveInfo_t* pTransceiveInfo,
50     phNciNfc_TransceiveInfo_t* tNciTranscvInfo) __attribute__((unused));
51 static NFCSTATUS phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,
52                                   phNfc_sTransceiveInfo_t* pTransceiveInfo,
53                                   pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
54 static NFCSTATUS phLibNfc_MifareMap(
55     phNfc_sTransceiveInfo_t* pTransceiveInfo,
56     pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
57 static NFCSTATUS phLibNfc_ChkAuthCmdMFC(
58     phNfc_sTransceiveInfo_t* pTransceiveInfo, uint8_t* bKey);
59 static NFCSTATUS phLibNfc_GetKeyNumberMFC(uint8_t* buffer, uint8_t* bKey);
60 static void phLibNfc_CalSectorAddress(uint8_t* Sector_Address);
61 static NFCSTATUS phNciNfc_MfCreateAuthCmdHdr(
62     phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t bBlockAddr, uint8_t* buff,
63     uint16_t* buffSz);
64 static NFCSTATUS phNciNfc_MfCreateXchgDataHdr(
65     phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t* buff, uint16_t* buffSz);
66 static NFCSTATUS phLibNfc_SendWrt16CmdPayload(
67     phNfc_sTransceiveInfo_t* pTransceiveInfo,
68     pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
69 static NFCSTATUS phNciNfc_RecvMfResp(phNciNfc_Buff_t* RspBuffInfo,
70                                      NFCSTATUS wStatus);
71 static NFCSTATUS nativeNfcExtns_doTransceive(uint8_t* buff, uint16_t buffSz);
72 static NFCSTATUS phFriNfc_NdefSmtCrd_Reset__(
73     phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt, uint8_t* SendRecvBuffer,
74     uint16_t* SendRecvBuffLen);
75 static NFCSTATUS phFriNfc_ValidateParams(uint8_t* PacketData,
76                                          uint32_t* PacketDataLength,
77                                          uint8_t Offset,
78                                          phFriNfc_NdefMap_t* pNdefMap,
79                                          uint8_t bNdefReq);
80 static void Mfc_FormatNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
81 static void Mfc_WriteNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
82 static void Mfc_ReadNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
83 static void Mfc_CheckNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
84 
85 /*******************************************************************************
86 **
87 ** Function         phNxpExtns_MfcModuleDeInit
88 **
89 ** Description      It Deinitializes the Mifare module.
90 **
91 **                  Frees all the memory occupied by Mifare module
92 **
93 ** Returns:
94 **                  NFCSTATUS_SUCCESS - if successfully deinitialize
95 **                  NFCSTATUS_FAILED  - otherwise
96 **
97 *******************************************************************************/
phNxpExtns_MfcModuleDeInit(void)98 NFCSTATUS phNxpExtns_MfcModuleDeInit(void) {
99   NFCSTATUS status = NFCSTATUS_FAILED;
100 
101   if (NdefMap != NULL) {
102     if (NdefMap->psRemoteDevInfo != NULL) {
103       free(NdefMap->psRemoteDevInfo);
104       NdefMap->psRemoteDevInfo = NULL;
105     }
106     if (NdefMap->SendRecvBuf != NULL) {
107       free(NdefMap->SendRecvBuf);
108       NdefMap->SendRecvBuf = NULL;
109     }
110     if (NdefMap->SendRecvLength != NULL) {
111       free(NdefMap->SendRecvLength);
112       NdefMap->SendRecvLength = NULL;
113     }
114     if (NdefMap->DataCount != NULL) {
115       free(NdefMap->DataCount);
116       NdefMap->DataCount = NULL;
117     }
118     if (NdefMap->pTransceiveInfo != NULL) {
119       if (NdefMap->pTransceiveInfo->sSendData.buffer != NULL) {
120         free(NdefMap->pTransceiveInfo->sSendData.buffer);
121         NdefMap->pTransceiveInfo->sSendData.buffer = NULL;
122       }
123       if (NdefMap->pTransceiveInfo->sRecvData.buffer != NULL) {
124         free(NdefMap->pTransceiveInfo->sRecvData.buffer);
125         NdefMap->pTransceiveInfo->sRecvData.buffer = NULL;
126       }
127       free(NdefMap->pTransceiveInfo);
128       NdefMap->pTransceiveInfo = NULL;
129     }
130 
131     free(NdefMap);
132     NdefMap = NULL;
133   }
134 
135   if (tNciTranscvInfo.tSendData.pBuff != NULL) {
136     free(tNciTranscvInfo.tSendData.pBuff);
137     tNciTranscvInfo.tSendData.pBuff = NULL;
138   }
139 
140   if (NdefSmtCrdFmt != NULL) {
141     free(NdefSmtCrdFmt);
142     NdefSmtCrdFmt = NULL;
143   }
144 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
145   pthread_mutex_lock(&SharedDataMutex);
146 #endif
147   if (NULL != NdefInfo.psUpperNdefMsg) {
148     free(NdefInfo.psUpperNdefMsg);
149     NdefInfo.psUpperNdefMsg = NULL;
150   }
151 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
152   pthread_mutex_unlock(&SharedDataMutex);
153 #endif
154   if (NULL != gAuthCmdBuf.pauth_cmd) {
155     if (NULL != gAuthCmdBuf.pauth_cmd->buffer) {
156       free(gAuthCmdBuf.pauth_cmd->buffer);
157       gAuthCmdBuf.pauth_cmd->buffer = NULL;
158     }
159     free(gAuthCmdBuf.pauth_cmd);
160     gAuthCmdBuf.pauth_cmd = NULL;
161   }
162   status = NFCSTATUS_SUCCESS;
163   return status;
164 }
165 
166 /*******************************************************************************
167 **
168 ** Function         phNxpExtns_MfcModuleInit
169 **
170 ** Description      It Initializes the memroy and global variables related
171 **                  to Mifare module.
172 **
173 **                  Reset all the global variables and allocate memory for
174 *Mifare module
175 **
176 ** Returns:
177 **                  NFCSTATUS_SUCCESS - if successfully deinitialize
178 **                  NFCSTATUS_FAILED  - otherwise
179 **
180 *******************************************************************************/
phNxpExtns_MfcModuleInit(void)181 NFCSTATUS phNxpExtns_MfcModuleInit(void) {
182   NFCSTATUS status = NFCSTATUS_FAILED;
183   gphNxpExtns_Context.writecmdFlag = false;
184   gphNxpExtns_Context.RawWriteCallBack = false;
185   gphNxpExtns_Context.CallBackCtxt = NULL;
186   gphNxpExtns_Context.CallBackMifare = NULL;
187   gphNxpExtns_Context.ExtnsConnect = false;
188   gphNxpExtns_Context.ExtnsDeactivate = false;
189   gphNxpExtns_Context.ExtnsCallBack = false;
190 
191   NdefMap = (phFriNfc_NdefMap_t*)malloc(sizeof(phFriNfc_NdefMap_t));
192   if (NULL == NdefMap) {
193     goto clean_and_return;
194   }
195   memset(NdefMap, 0, sizeof(phFriNfc_NdefMap_t));
196 
197   NdefMap->psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t*)malloc(
198       sizeof(phLibNfc_sRemoteDevInformation_t));
199   if (NULL == NdefMap->psRemoteDevInfo) {
200     goto clean_and_return;
201   }
202   memset(NdefMap->psRemoteDevInfo, 0, sizeof(phLibNfc_sRemoteDevInformation_t));
203 
204   NdefMap->SendRecvBuf = (uint8_t*)malloc((uint32_t)(MAX_BUFF_SIZE * 2));
205   if (NULL == NdefMap->SendRecvBuf) {
206     goto clean_and_return;
207   }
208   memset(NdefMap->SendRecvBuf, 0, (MAX_BUFF_SIZE * 2));
209 
210   NdefMap->SendRecvLength = (uint16_t*)malloc(sizeof(uint16_t));
211   if (NULL == NdefMap->SendRecvLength) {
212     goto clean_and_return;
213   }
214   memset(NdefMap->SendRecvLength, 0, sizeof(uint16_t));
215 
216   NdefMap->DataCount = (uint16_t*)malloc(sizeof(uint16_t));
217   if (NULL == NdefMap->DataCount) {
218     goto clean_and_return;
219   }
220   memset(NdefMap->DataCount, 0, sizeof(uint16_t));
221 
222   NdefMap->pTransceiveInfo =
223       (phNfc_sTransceiveInfo_t*)malloc(sizeof(phNfc_sTransceiveInfo_t));
224   if (NULL == NdefMap->pTransceiveInfo) {
225     goto clean_and_return;
226   }
227   memset(NdefMap->pTransceiveInfo, 0, sizeof(phNfc_sTransceiveInfo_t));
228 
229   tNciTranscvInfo.tSendData.pBuff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
230   if (NULL == tNciTranscvInfo.tSendData.pBuff) {
231     goto clean_and_return;
232   }
233   memset(tNciTranscvInfo.tSendData.pBuff, 0, MAX_BUFF_SIZE);
234 
235   NdefMap->pTransceiveInfo->sSendData.buffer =
236       (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
237   if (NdefMap->pTransceiveInfo->sSendData.buffer == NULL) {
238     goto clean_and_return;
239   }
240   memset(NdefMap->pTransceiveInfo->sSendData.buffer, 0, MAX_BUFF_SIZE);
241   NdefMap->pTransceiveInfo->sSendData.length = MAX_BUFF_SIZE;
242 
243   NdefMap->pTransceiveInfo->sRecvData.buffer = (uint8_t*)malloc(
244       (uint32_t)MAX_BUFF_SIZE); /* size should be same as sRecvData */
245   if (NdefMap->pTransceiveInfo->sRecvData.buffer == NULL) {
246     goto clean_and_return;
247   }
248   memset(NdefMap->pTransceiveInfo->sRecvData.buffer, 0, MAX_BUFF_SIZE);
249   NdefMap->pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
250 
251   NdefSmtCrdFmt =
252       (phFriNfc_sNdefSmtCrdFmt_t*)malloc(sizeof(phFriNfc_sNdefSmtCrdFmt_t));
253   if (NdefSmtCrdFmt == NULL) {
254     goto clean_and_return;
255   }
256   memset(NdefSmtCrdFmt, 0, sizeof(phFriNfc_sNdefSmtCrdFmt_t));
257 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
258   pthread_mutex_lock(&SharedDataMutex);
259 #endif
260   NdefInfo.psUpperNdefMsg = (phNfc_sData_t*)malloc(sizeof(phNfc_sData_t));
261   if (NULL == NdefInfo.psUpperNdefMsg) {
262     goto clean_and_return;
263   }
264   memset(NdefInfo.psUpperNdefMsg, 0, sizeof(phNfc_sData_t));
265   memset(&gAuthCmdBuf, 0, sizeof(phNci_mfc_auth_cmd_t));
266   gAuthCmdBuf.pauth_cmd = (phNfc_sData_t*)malloc(sizeof(phNfc_sData_t));
267   if (NULL == gAuthCmdBuf.pauth_cmd) {
268     goto clean_and_return;
269   }
270   gAuthCmdBuf.pauth_cmd->buffer = (uint8_t*)malloc((uint32_t)NCI_MAX_DATA_LEN);
271   if (NULL == gAuthCmdBuf.pauth_cmd->buffer) {
272     goto clean_and_return;
273   }
274   status = NFCSTATUS_SUCCESS;
275 
276 clean_and_return:
277 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
278   pthread_mutex_unlock(&SharedDataMutex);
279 #endif
280   if (status != NFCSTATUS_SUCCESS) {
281     LOG(ERROR) << StringPrintf("CRIT: Memory Allocation failed for MFC!");
282     phNxpExtns_MfcModuleDeInit();
283   }
284   return status;
285 }
286 
287 /*******************************************************************************
288 **
289 ** Function         Mfc_CheckNdef
290 **
291 ** Description      It triggers NDEF detection for Mifare Classic Tag.
292 **
293 **
294 ** Returns          NFCSTATUS_SUCCESS - if successfully initiated
295 **                  NFCSTATUS_FAILED  - otherwise
296 **
297 *******************************************************************************/
Mfc_CheckNdef(void)298 NFCSTATUS Mfc_CheckNdef(void) {
299   NFCSTATUS status = NFCSTATUS_FAILED;
300 
301   EXTNS_SetCallBackFlag(false);
302   /* Set Completion Routine for CheckNdef */
303   NdefMap->CompletionRoutine[0].CompletionRoutine =
304       Mfc_CheckNdef_Completion_Routine;
305 
306   gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
307   gphNxpExtns_Context.CallBackCtxt = NdefMap;
308   status = phFriNfc_MifareStdMap_H_Reset(NdefMap);
309   if (NFCSTATUS_SUCCESS == status) {
310     status = phFriNfc_MifareStdMap_ChkNdef(NdefMap);
311     if (status == NFCSTATUS_PENDING) {
312       status = NFCSTATUS_SUCCESS;
313     }
314   }
315   if (status != NFCSTATUS_SUCCESS) {
316     status = NFCSTATUS_FAILED;
317   }
318 
319   return status;
320 }
321 
322 /*******************************************************************************
323 **
324 ** Function         Mfc_CheckNdef_Completion_Routine
325 **
326 ** Description      Notify NDEF detection for Mifare Classic Tag to JNI
327 **
328 **                  Upon completion of NDEF detection, a
329 **                  NFA_NDEF_DETECT_EVT will be sent, to notify the application
330 **                  of the NDEF attributes (NDEF total memory size, current
331 **                  size, etc.).
332 **
333 ** Returns:         void
334 **
335 *******************************************************************************/
Mfc_CheckNdef_Completion_Routine(void * NdefCtxt,NFCSTATUS status)336 static void Mfc_CheckNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
337   (void)NdefCtxt;
338   tNFA_CONN_EVT_DATA conn_evt_data;
339 
340   conn_evt_data.ndef_detect.status = status;
341   if (NFCSTATUS_SUCCESS == status) {
342     /* NDef Tag Detected */
343     conn_evt_data.ndef_detect.protocol = NFC_PROTOCOL_MIFARE;
344     phFrinfc_MifareClassic_GetContainerSize(
345         NdefMap, (uint32_t*)&(conn_evt_data.ndef_detect.max_size),
346         (uint32_t*)&(conn_evt_data.ndef_detect.cur_size));
347     NdefInfo.NdefLength = conn_evt_data.ndef_detect.max_size;
348     /* update local flags */
349     NdefInfo.is_ndef = 1;
350     NdefInfo.NdefActualSize = conn_evt_data.ndef_detect.cur_size;
351     if (PH_NDEFMAP_CARD_STATE_READ_ONLY == NdefMap->CardState) {
352       DLOG_IF(INFO, nfc_debug_enabled)
353           << StringPrintf("Mfc_CheckNdef_Completion_Routine : READ_ONLY_CARD");
354       conn_evt_data.ndef_detect.flags = RW_NDEF_FL_READ_ONLY;
355     } else {
356       conn_evt_data.ndef_detect.flags =
357           RW_NDEF_FL_SUPPORTED | RW_NDEF_FL_FORMATED;
358     }
359   } else {
360     /* NDEF Detection failed for other reasons */
361     conn_evt_data.ndef_detect.cur_size = 0;
362     conn_evt_data.ndef_detect.max_size = 0;
363     conn_evt_data.ndef_detect.flags = RW_NDEF_FL_UNKNOWN;
364 
365     /* update local flags */
366     NdefInfo.is_ndef = 0;
367     NdefInfo.NdefActualSize = conn_evt_data.ndef_detect.cur_size;
368   }
369   (*gphNxpExtns_Context.p_conn_cback)(NFA_NDEF_DETECT_EVT, &conn_evt_data);
370 
371   return;
372 }
373 /*******************************************************************************
374 **
375 ** Function         Mfc_ReadNdef_Completion_Routine
376 **
377 ** Description      Notify NDEF read completion for Mifare Classic Tag to JNI
378 **
379 **                  Upon completion of NDEF read, a
380 **                  NFA_READ_CPLT_EVT will be sent, to notify the application
381 **                  with the NDEF data and status
382 **
383 ** Returns:         void
384 **
385 *******************************************************************************/
Mfc_ReadNdef_Completion_Routine(void * NdefCtxt,NFCSTATUS status)386 static void Mfc_ReadNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
387   (void)NdefCtxt;
388   tNFA_CONN_EVT_DATA conn_evt_data;
389   tNFA_NDEF_EVT_DATA p_data;
390 
391   conn_evt_data.status = status;
392 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
393   pthread_mutex_lock(&SharedDataMutex);
394 #endif
395   if (NFCSTATUS_SUCCESS == status) {
396     p_data.ndef_data.len = NdefInfo.psUpperNdefMsg->length;
397     p_data.ndef_data.p_data = NdefInfo.psUpperNdefMsg->buffer;
398     (*gphNxpExtns_Context.p_ndef_cback)(NFA_NDEF_DATA_EVT, &p_data);
399   } else {
400   }
401 
402   (*gphNxpExtns_Context.p_conn_cback)(NFA_READ_CPLT_EVT, &conn_evt_data);
403 
404   if (NdefInfo.psUpperNdefMsg->buffer != NULL) {
405     free(NdefInfo.psUpperNdefMsg->buffer);
406     NdefInfo.psUpperNdefMsg->buffer = NULL;
407   }
408 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
409   pthread_mutex_unlock(&SharedDataMutex);
410 #endif
411   return;
412 }
413 
414 /*******************************************************************************
415 **
416 ** Function         Mfc_WriteNdef_Completion_Routine
417 **
418 ** Description      Notify NDEF write completion for Mifare Classic Tag to JNI
419 **
420 **                  Upon completion of NDEF write, a
421 **                  NFA_WRITE_CPLT_EVT will be sent along with status
422 **
423 ** Returns:         void
424 **
425 *******************************************************************************/
Mfc_WriteNdef_Completion_Routine(void * NdefCtxt,NFCSTATUS status)426 static void Mfc_WriteNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
427   (void)NdefCtxt;
428   tNFA_CONN_EVT_DATA conn_evt_data;
429 
430   conn_evt_data.status = status;
431   (*gphNxpExtns_Context.p_conn_cback)(NFA_WRITE_CPLT_EVT, &conn_evt_data);
432 
433   return;
434 }
435 
436 /*******************************************************************************
437 **
438 ** Function         Mfc_FormatNdef_Completion_Routine
439 **
440 ** Description      Notify NDEF format completion for Mifare Classic Tag to JNI
441 **
442 **                  Upon completion of NDEF format, a
443 **                  NFA_FORMAT_CPLT_EVT will be sent along with status
444 **
445 ** Returns:         void
446 **
447 *******************************************************************************/
Mfc_FormatNdef_Completion_Routine(void * NdefCtxt,NFCSTATUS status)448 static void Mfc_FormatNdef_Completion_Routine(void* NdefCtxt,
449                                               NFCSTATUS status) {
450   (void)NdefCtxt;
451   tNFA_CONN_EVT_DATA conn_evt_data;
452 
453   conn_evt_data.status = status;
454   (*gphNxpExtns_Context.p_conn_cback)(NFA_FORMAT_CPLT_EVT, &conn_evt_data);
455 
456   return;
457 }
458 
459 /*******************************************************************************
460 **
461 ** Function          phFriNfc_ValidateParams
462 **
463 ** Description      This function is a common function which validates NdefRd
464 **                  and NdefWr parameters.
465 **
466 ** Returns          NFCSTATUS_SUCCESS  - All the params are valid
467 **                  NFCSTATUS_FAILED   - otherwise
468 **
469 *******************************************************************************/
phFriNfc_ValidateParams(uint8_t * PacketData,uint32_t * PacketDataLength,uint8_t Offset,phFriNfc_NdefMap_t * pNdefMap,uint8_t bNdefReq)470 static NFCSTATUS phFriNfc_ValidateParams(uint8_t* PacketData,
471                                          uint32_t* PacketDataLength,
472                                          uint8_t Offset,
473                                          phFriNfc_NdefMap_t* pNdefMap,
474                                          uint8_t bNdefReq) {
475   if ((pNdefMap == NULL) || (PacketData == NULL) ||
476       (PacketDataLength == NULL)) {
477     return NFCSTATUS_FAILED;
478   }
479 
480   if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) {
481     return NFCSTATUS_FAILED;
482   }
483 
484   if (bNdefReq == PH_FRINFC_NDEF_READ_REQ) {
485     if ((Offset != PH_FRINFC_NDEFMAP_SEEK_CUR) &&
486         (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN)) {
487       return NFCSTATUS_FAILED;
488     }
489     if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED) {
490       pNdefMap->NumOfBytesRead = PacketDataLength;
491       *pNdefMap->NumOfBytesRead = 0;
492       return NFCSTATUS_EOF_NDEF_CONTAINER_REACHED;
493     }
494     if ((pNdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE) &&
495         (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN)) {
496       return NFCSTATUS_FAILED; /* return INVALID_DEVICE_REQUEST */
497     }
498     if (Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) {
499       pNdefMap->ApduBuffIndex = 0;
500       *pNdefMap->DataCount = 0;
501     } else if ((pNdefMap->bPrevReadMode == PH_FRINFC_NDEFMAP_SEEK_BEGIN) ||
502                (pNdefMap->bPrevReadMode == PH_FRINFC_NDEFMAP_SEEK_CUR)) {
503     } else {
504       return NFCSTATUS_FAILED;
505     }
506   } else if (bNdefReq == PH_FRINFC_NDEF_WRITE_REQ) {
507     if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY) {
508       pNdefMap->WrNdefPacketLength = PacketDataLength;
509       *pNdefMap->WrNdefPacketLength = 0x00;
510       return NFCSTATUS_NOT_ALLOWED;
511     }
512   }
513 
514   return NFCSTATUS_SUCCESS;
515 }
516 
517 /*******************************************************************************
518 **
519 ** Function         Mfc_SetRdOnly_Completion_Routine
520 **
521 ** Description      Notify NDEF read only completion for Mifare Classic Tag to
522 *JNI
523 **
524 **                  Upon completion of NDEF format, a
525 **                  NFA_SET_TAG_RO_EVT will be sent along with status
526 **
527 ** Returns:         void
528 **
529 *******************************************************************************/
Mfc_SetRdOnly_Completion_Routine(void * NdefCtxt,NFCSTATUS status)530 static void Mfc_SetRdOnly_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
531   (void)NdefCtxt;
532   tNFA_CONN_EVT_DATA conn_evt_data;
533   LOG(ERROR) << StringPrintf("%s status = 0x%x", __func__, status);
534   conn_evt_data.status = status;
535   (*gphNxpExtns_Context.p_conn_cback)(NFA_SET_TAG_RO_EVT, &conn_evt_data);
536 
537   return;
538 }
539 
540 /*******************************************************************************
541 **
542 ** Function        Mfc_SetReadOnly
543 **
544 **
545 ** Description:    It triggers ConvertToReadOnly  for Mifare Classic Tag.
546 **
547 ** Returns:
548 **                  NFCSTATUS_SUCCESS if successfully initiated
549 **                  NFCSTATUS_FAILED otherwise
550 **
551 *******************************************************************************/
Mfc_SetReadOnly(uint8_t * secrtkey,uint8_t len)552 NFCSTATUS Mfc_SetReadOnly(uint8_t* secrtkey, uint8_t len) {
553   DLOG_IF(INFO, nfc_debug_enabled)
554       << StringPrintf("%s Entering ", __func__);
555   NFCSTATUS status = NFCSTATUS_FAILED;
556   uint8_t mif_secrete_key[6] = {0};
557   uint8_t id = 0;
558   EXTNS_SetCallBackFlag(false);
559   memcpy(mif_secrete_key, secrtkey, len);
560   gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
561   gphNxpExtns_Context.CallBackCtxt = NdefMap;
562   for (id = 0; id < len; id++) {
563     DLOG_IF(INFO, nfc_debug_enabled)
564         << StringPrintf("secrtkey[%d] = 0x%x", id, secrtkey[id]);
565     DLOG_IF(INFO, nfc_debug_enabled)
566         << StringPrintf("mif_secrete_key[%d] = 0x%x", id, mif_secrete_key[id]);
567   }
568   /* Set Completion Routine for ReadNdef */
569   NdefMap->CompletionRoutine[0].CompletionRoutine =
570       Mfc_SetRdOnly_Completion_Routine;
571   if (NdefInfo.is_ndef == 0) {
572     status = NFCSTATUS_NON_NDEF_COMPLIANT;
573     goto Mfc_SetRdOnly;
574   } else if ((NdefInfo.is_ndef == 1) && (NdefInfo.NdefActualSize == 0)) {
575 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
576     pthread_mutex_lock(&SharedDataMutex);
577 #endif
578     NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize;
579 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
580     pthread_mutex_unlock(&SharedDataMutex);
581 #endif
582     status = NFCSTATUS_SUCCESS;
583     goto Mfc_SetRdOnly;
584   } else {
585     status = phFriNfc_MifareStdMap_ConvertToReadOnly(NdefMap, mif_secrete_key);
586   }
587   if (NFCSTATUS_PENDING == status) {
588     status = NFCSTATUS_SUCCESS;
589   }
590 
591 Mfc_SetRdOnly:
592   return status;
593 }
594 
595 /*******************************************************************************
596 **
597 ** Function         Mfc_ReadNdef
598 **
599 ** Description      It triggers receiving of the NDEF message from Mifare
600 *Classic Tag.
601 **
602 **
603 ** Returns:
604 **                  NFCSTATUS_SUCCESS - if successfully initiated
605 **                  NFCSTATUS_FAILED  - otherwise
606 **
607 *******************************************************************************/
Mfc_ReadNdef(void)608 NFCSTATUS Mfc_ReadNdef(void) {
609   NFCSTATUS status = NFCSTATUS_FAILED;
610   uint8_t* PacketData = NULL;
611   uint32_t* PacketDataLength = NULL;
612   phLibNfc_Ndef_EOffset_t Offset;
613 
614   EXTNS_SetCallBackFlag(false);
615 
616   Offset = phLibNfc_Ndef_EBegin;
617 
618   gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
619   gphNxpExtns_Context.CallBackCtxt = NdefMap;
620 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
621   pthread_mutex_lock(&SharedDataMutex);
622 #endif
623   if (NdefInfo.is_ndef == 0) {
624     status = NFCSTATUS_NON_NDEF_COMPLIANT;
625     goto Mfc_RdNdefEnd;
626   } else if ((NdefInfo.is_ndef == 1) && (NdefInfo.NdefActualSize == 0)) {
627     NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize;
628     status = NFCSTATUS_SUCCESS;
629     goto Mfc_RdNdefEnd;
630   } else {
631     NdefInfo.psUpperNdefMsg->buffer = (uint8_t*)malloc(NdefInfo.NdefActualSize);
632     if (NULL == NdefInfo.psUpperNdefMsg->buffer) {
633       goto Mfc_RdNdefEnd;
634     }
635     NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize;
636 
637     /* Set Completion Routine for ReadNdef */
638     NdefMap->CompletionRoutine[1].CompletionRoutine =
639         Mfc_ReadNdef_Completion_Routine;
640     NdefInfo.NdefContinueRead = (uint8_t)(PH_FRINFC_NDEFMAP_SEEK_BEGIN);
641   }
642 
643   PacketData = NdefInfo.psUpperNdefMsg->buffer;
644   PacketDataLength = (uint32_t*)&(NdefInfo.psUpperNdefMsg->length);
645   NdefMap->bCurrReadMode = Offset;
646   status = phFriNfc_ValidateParams(PacketData, PacketDataLength, Offset,
647                                    NdefMap, PH_FRINFC_NDEF_READ_REQ);
648   if (status != NFCSTATUS_SUCCESS) {
649     goto Mfc_RdNdefEnd;
650   }
651 
652   status = phFriNfc_MifareStdMap_RdNdef(NdefMap, PacketData, PacketDataLength,
653                                         Offset);
654 
655   if (NFCSTATUS_INSUFFICIENT_STORAGE == status) {
656     NdefInfo.psUpperNdefMsg->length = 0x00;
657     status = NFCSTATUS_SUCCESS;
658   }
659 
660   if (NFCSTATUS_PENDING == status) {
661     status = NFCSTATUS_SUCCESS;
662   }
663 
664 Mfc_RdNdefEnd:
665   if (status != NFCSTATUS_SUCCESS) {
666     if (NULL != NdefInfo.psUpperNdefMsg->buffer) {
667       free(NdefInfo.psUpperNdefMsg->buffer);
668       NdefInfo.psUpperNdefMsg->buffer = NULL;
669     }
670     status = NFCSTATUS_FAILED;
671   }
672 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
673   pthread_mutex_unlock(&SharedDataMutex);
674 #endif
675   return status;
676 }
677 /*******************************************************************************
678 **
679 ** Function         Mfc_PresenceCheck
680 **
681 ** Description      It triggers receiving of the NDEF message from Mifare
682 *Classic Tag.
683 **
684 **
685 ** Returns:
686 **                  NFCSTATUS_SUCCESS - if successfully initiated
687 **                  NFCSTATUS_FAILED  - otherwise
688 **
689 *******************************************************************************/
Mfc_PresenceCheck(void)690 NFCSTATUS Mfc_PresenceCheck(void) {
691   NFCSTATUS status = NFCSTATUS_SUCCESS;
692 
693   if (gAuthCmdBuf.auth_status == true) {
694     EXTNS_SetCallBackFlag(false);
695     status = nativeNfcExtns_doTransceive(gAuthCmdBuf.pauth_cmd->buffer,
696                                          gAuthCmdBuf.pauth_cmd->length);
697     if (status != NFCSTATUS_PENDING) {
698       gAuthCmdBuf.auth_sent = false;
699       status = NFCSTATUS_FAILED;
700     } else {
701       gAuthCmdBuf.auth_sent = true;
702       status = NFCSTATUS_SUCCESS;
703     }
704   } else {
705     status = NFCSTATUS_NOT_ALLOWED;
706   }
707   DLOG_IF(INFO, nfc_debug_enabled)
708       << StringPrintf("%s status = 0x%x", __func__, status);
709   return status;
710 }
711 /*******************************************************************************
712 **
713 ** Function         Mfc_WriteNdef
714 **
715 ** Description      It triggers the NDEF data write to Mifare Classic Tag.
716 **
717 **
718 ** Returns:
719 **                  NFCSTATUS_SUCCESS - if successfully initiated
720 **                  NFCSTATUS_FAILED  - otherwise
721 **
722 *******************************************************************************/
Mfc_WriteNdef(uint8_t * p_data,uint32_t len)723 NFCSTATUS Mfc_WriteNdef(uint8_t* p_data, uint32_t len) {
724   NFCSTATUS status = NFCSTATUS_SUCCESS;
725   uint8_t* PacketData = NULL;
726   uint32_t* PacketDataLength = NULL;
727 
728   if (p_data == NULL || len == 0) {
729     LOG(ERROR) << StringPrintf("MFC Error: Invalid Parameters to Ndef Write");
730     status = NFCSTATUS_FAILED;
731     goto Mfc_WrNdefEnd;
732   }
733 
734   EXTNS_SetCallBackFlag(false);
735   gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
736   gphNxpExtns_Context.CallBackCtxt = NdefMap;
737 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
738   pthread_mutex_lock(&SharedDataMutex);
739 #endif
740   if (NdefInfo.is_ndef == PH_LIBNFC_INTERNAL_CHK_NDEF_NOT_DONE) {
741     status = NFCSTATUS_REJECTED;
742     goto Mfc_WrNdefEnd;
743   } else if (NdefInfo.is_ndef == 0) {
744     status = NFCSTATUS_NON_NDEF_COMPLIANT;
745     goto Mfc_WrNdefEnd;
746   } else if (len > NdefInfo.NdefLength) {
747     status = NFCSTATUS_NOT_ENOUGH_MEMORY;
748     goto Mfc_WrNdefEnd;
749   } else {
750     NdefInfo.psUpperNdefMsg->buffer = p_data;
751     NdefInfo.psUpperNdefMsg->length = len;
752 
753     NdefInfo.AppWrLength = len;
754     NdefMap->CompletionRoutine[2].CompletionRoutine =
755         Mfc_WriteNdef_Completion_Routine;
756     if (0 == len) {
757       /* TODO: Erase the Tag */
758     } else {
759       NdefMap->ApduBuffIndex = 0x00;
760       *NdefMap->DataCount = 0x00;
761       PacketData = NdefInfo.psUpperNdefMsg->buffer;
762       PacketDataLength = &(NdefInfo.dwWrLength);
763       NdefMap->WrNdefPacketLength = PacketDataLength;
764       NdefInfo.dwWrLength = len;
765 
766       status = phFriNfc_ValidateParams(PacketData, PacketDataLength, 0, NdefMap,
767                                        PH_FRINFC_NDEF_WRITE_REQ);
768       if (status != NFCSTATUS_SUCCESS) {
769         goto Mfc_WrNdefEnd;
770       }
771 
772       status = phFriNfc_MifareStdMap_WrNdef(
773           NdefMap, PacketData, PacketDataLength, PH_FRINFC_NDEFMAP_SEEK_BEGIN);
774 
775       if (status == NFCSTATUS_PENDING) {
776         status = NFCSTATUS_SUCCESS;
777       }
778     }
779   }
780 
781 Mfc_WrNdefEnd:
782 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
783   pthread_mutex_unlock(&SharedDataMutex);
784 #endif
785   if (status != NFCSTATUS_SUCCESS) {
786     status = NFCSTATUS_FAILED;
787   }
788   return status;
789 }
790 /*******************************************************************************
791 **
792 ** Function          phFriNfc_NdefSmtCrd_Reset__
793 **
794 ** Description      This function Resets the component instance to the initial
795 **                  state and initializes the internal variables.
796 **
797 ** Returns          NFCSTATUS_SUCCESS
798 **
799 *******************************************************************************/
phFriNfc_NdefSmtCrd_Reset__(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt,uint8_t * SendRecvBuffer,uint16_t * SendRecvBuffLen)800 static NFCSTATUS phFriNfc_NdefSmtCrd_Reset__(
801     phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt, uint8_t* SendRecvBuffer,
802     uint16_t* SendRecvBuffLen) {
803   //    NFCSTATUS status = NFCSTATUS_FAILED;                      /*commented to
804   //    eliminate unused variable warning*/
805   uint8_t index;
806 
807   /* Initialize the state to Init */
808   NdefSmtCrdFmt->State = PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT;
809 
810   for (index = 0; index < PH_FRINFC_SMTCRDFMT_CR; index++) {
811     /* Initialize the NdefMap Completion Routine to Null */
812     NdefSmtCrdFmt->CompletionRoutine[index].CompletionRoutine = NULL;
813     /* Initialize the NdefMap Completion Routine context to Null  */
814     NdefSmtCrdFmt->CompletionRoutine[index].Context = NULL;
815   }
816 
817   /* Trx Buffer registered */
818   NdefSmtCrdFmt->SendRecvBuf = SendRecvBuffer;
819 
820   /* Trx Buffer Size */
821   NdefSmtCrdFmt->SendRecvLength = SendRecvBuffLen;
822 
823   /* Register Transfer Buffer Length */
824   NdefSmtCrdFmt->SendLength = 0;
825 
826   /* Initialize the Format status flag*/
827   NdefSmtCrdFmt->FmtProcStatus = 0;
828 
829   /* Reset the Card Type */
830   NdefSmtCrdFmt->CardType = 0;
831 
832   /* Reset MapCompletion Info*/
833   NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.CompletionRoutine = NULL;
834   NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.Context = NULL;
835 
836   phFriNfc_MfStd_Reset(NdefSmtCrdFmt);
837 
838   return NFCSTATUS_SUCCESS;
839 }
840 
841 /*******************************************************************************
842 **
843 ** Function         Mfc_FormatNdef
844 **
845 ** Description      It triggers the NDEF format of Mifare Classic Tag.
846 **
847 **
848 ** Returns:
849 **                  NFCSTATUS_SUCCESS - if successfully initiated
850 **                  NFCSTATUS_FAILED  - otherwise
851 **
852 *******************************************************************************/
Mfc_FormatNdef(uint8_t * secretkey,uint8_t len)853 NFCSTATUS Mfc_FormatNdef(uint8_t* secretkey, uint8_t len) {
854   NFCSTATUS status = NFCSTATUS_FAILED;
855   uint8_t mif_std_key[PHLIBNFC_MFC_AUTHKEYLEN] = {0};
856   //    static uint8_t   Index;
857   //    /*commented to eliminate unused variable warning*/
858   uint8_t sak = 0;
859 
860   EXTNS_SetCallBackFlag(false);
861 
862   if (len != PHLIBNFC_MFC_AUTHKEYLEN) return NFCSTATUS_FAILED;
863 
864   memcpy(mif_std_key, secretkey, len);
865   memcpy(current_key, secretkey, len);
866 
867   if (NULL == NdefSmtCrdFmt || NULL == NdefMap ||
868       NULL == NdefMap->SendRecvBuf) {
869     goto Mfc_FormatEnd;
870   }
871   NdefSmtCrdFmt->pTransceiveInfo = NdefMap->pTransceiveInfo;
872 
873   gphNxpExtns_Context.CallBackMifare = phFriNfc_MfStd_Process;
874   gphNxpExtns_Context.CallBackCtxt = NdefSmtCrdFmt;
875 
876   NdefInfo.NdefSendRecvLen = NDEF_SENDRCV_BUF_LEN;
877   phFriNfc_NdefSmtCrd_Reset__(NdefSmtCrdFmt, NdefMap->SendRecvBuf,
878                               &(NdefInfo.NdefSendRecvLen));
879 
880   /* Register Callbacks */
881   NdefSmtCrdFmt->CompletionRoutine[0].CompletionRoutine =
882       Mfc_FormatNdef_Completion_Routine;
883   NdefSmtCrdFmt->CompletionRoutine[1].CompletionRoutine =
884       Mfc_FormatNdef_Completion_Routine;
885   NdefSmtCrdFmt->psRemoteDevInfo = NdefMap->psRemoteDevInfo;
886   sak = NdefSmtCrdFmt->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
887 
888   if ((0x08 == (sak & 0x18)) || (0x18 == (sak & 0x18)) || (0x01 == sak)) {
889     NdefSmtCrdFmt->CardType = (uint8_t)(
890         ((sak & 0x18) == 0x08)
891             ? PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD
892             : (((sak & 0x19) == 0x19) ? PH_FRINFC_SMTCRDFMT_MFSTD_2K_CRD
893                                       : PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD));
894     status = phFriNfc_MfStd_Format(NdefSmtCrdFmt, mif_std_key);
895   }
896 
897   if (NFCSTATUS_PENDING == status) {
898     status = NFCSTATUS_SUCCESS;
899   }
900 
901 Mfc_FormatEnd:
902   if (status != NFCSTATUS_SUCCESS) {
903     status = NFCSTATUS_FAILED;
904   }
905 
906   return status;
907 }
908 
909 /*******************************************************************************
910 **
911 ** Function         phNxNciExtns_MifareStd_Reconnect
912 **
913 ** Description      This function sends the deactivate command to NFCC for
914 *Mifare
915 **
916 **
917 ** Returns:
918 **                  NFCSTATUS_PENDING - if successfully initiated
919 **                  NFCSTATUS_FAILED  - otherwise
920 **
921 *******************************************************************************/
phNxNciExtns_MifareStd_Reconnect(void)922 NFCSTATUS phNxNciExtns_MifareStd_Reconnect(void) {
923   tNFA_STATUS status;
924 
925   EXTNS_SetDeactivateFlag(true);
926   if (NFA_STATUS_OK !=
927       (status = NFA_Deactivate(true))) /* deactivate to sleep state */
928   {
929     LOG(ERROR) << StringPrintf("%s: deactivate failed, status = %d", __func__,
930                                status);
931     return NFCSTATUS_FAILED;
932   }
933 
934   return NFCSTATUS_PENDING;
935 }
936 
937 /*******************************************************************************
938 **
939 ** Function         Mfc_DeactivateCbackSelect
940 **
941 ** Description      This function select the Mifare tag
942 **
943 **
944 ** Returns:         void
945 **
946 *******************************************************************************/
Mfc_DeactivateCbackSelect(void)947 void Mfc_DeactivateCbackSelect(void) {
948   tNFA_STATUS status;
949 
950   EXTNS_SetConnectFlag(true);
951   if (NFA_STATUS_OK !=
952       (status = NFA_Select(0x01, phNciNfc_e_RfProtocolsMifCProtocol,
953                            phNciNfc_e_RfInterfacesTagCmd_RF))) {
954     LOG(ERROR) << StringPrintf("%s: NFA_Select failed, status = %d", __func__,
955                                status);
956   }
957 
958   return;
959 }
960 
961 /*******************************************************************************
962 **
963 ** Function         Mfc_ActivateCback
964 **
965 ** Description      This function invoke the callback when receive the response
966 **
967 **
968 ** Returns:         void
969 **
970 **
971 *******************************************************************************/
Mfc_ActivateCback(void)972 void Mfc_ActivateCback(void) {
973   gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt,
974                                      NFCSTATUS_SUCCESS);
975   return;
976 }
977 
978 /*******************************************************************************
979 **
980 ** Function         Mfc_Transceive
981 **
982 ** Description      Sends raw frame to Mifare Classic Tag.
983 **
984 ** Returns          NFCSTATUS_SUCCESS - if successfully initiated
985 **                  NFCSTATUS_FAILED  - otherwise
986 **
987 *******************************************************************************/
Mfc_Transceive(uint8_t * p_data,uint32_t len)988 NFCSTATUS Mfc_Transceive(uint8_t* p_data, uint32_t len) {
989   NFCSTATUS status = NFCSTATUS_FAILED;
990   uint8_t i = 0x00;
991 
992   if (len == 0) {
993     android_errorWriteLog(0x534e4554, "132082342");
994     return status;
995   }
996 
997   gphNxpExtns_Context.RawWriteCallBack = false;
998   gphNxpExtns_Context.CallBackMifare = NULL;
999   gphNxpExtns_Context.CallBackCtxt = NdefMap;
1000 
1001   EXTNS_SetCallBackFlag(true);
1002   if (p_data[0] == 0x60 || p_data[0] == 0x61) {
1003     if (len < 12) {
1004       android_errorWriteLog(0x534e4554, "125900276");
1005       return status;
1006     }
1007     NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)p_data[0];
1008 
1009     NdefMap->SendRecvBuf[i++] = p_data[1];
1010 
1011     NdefMap->SendRecvBuf[i++] = p_data[6]; /* TODO, handle 7 byte UID */
1012     NdefMap->SendRecvBuf[i++] = p_data[7];
1013     NdefMap->SendRecvBuf[i++] = p_data[8];
1014     NdefMap->SendRecvBuf[i++] = p_data[9];
1015     NdefMap->SendRecvBuf[i++] = p_data[10];
1016     NdefMap->SendRecvBuf[i++] = p_data[11];
1017 
1018     status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1019                                       NdefMap->SendRecvBuf, NdefMap->SendLength,
1020                                       NdefMap->SendRecvLength);
1021   } else if (p_data[0] == 0xA0) {
1022     EXTNS_SetCallBackFlag(false);
1023     NdefMap->Cmd.MfCmd = phNfc_eMifareWrite16;
1024     gphNxpExtns_Context.RawWriteCallBack = true;
1025 
1026     memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1027     NdefMap->SendLength = len - 1;
1028     status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1029                                       NdefMap->SendRecvBuf, NdefMap->SendLength,
1030                                       NdefMap->SendRecvLength);
1031   } else if ((p_data[0] == phNfc_eMifareInc) ||
1032              (p_data[0] == phNfc_eMifareDec)) {
1033     EXTNS_SetCallBackFlag(false);
1034     NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)p_data[0];
1035     gphNxpExtns_Context.RawWriteCallBack = true;
1036 
1037     memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1038     NdefMap->SendLength = len - 1;
1039     status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1040                                       NdefMap->SendRecvBuf, NdefMap->SendLength,
1041                                       NdefMap->SendRecvLength);
1042   } else if (((p_data[0] == phNfc_eMifareTransfer) ||
1043               (p_data[0] == phNfc_eMifareRestore)) &&
1044              (len == 2)) {
1045     NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)p_data[0];
1046     if (p_data[0] == phNfc_eMifareRestore) {
1047       EXTNS_SetCallBackFlag(false);
1048       gphNxpExtns_Context.RawWriteCallBack = true;
1049       memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1050       NdefMap->SendLength = len - 1;
1051     } else {
1052       memcpy(NdefMap->SendRecvBuf, p_data, len);
1053       NdefMap->SendLength = len;
1054     }
1055     status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1056                                       NdefMap->SendRecvBuf, NdefMap->SendLength,
1057                                       NdefMap->SendRecvLength);
1058 
1059   } else {
1060     NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)phNfc_eMifareRaw;
1061 
1062     memcpy(NdefMap->SendRecvBuf, p_data, len);
1063     NdefMap->SendLength = len;
1064     status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1065                                       NdefMap->SendRecvBuf, NdefMap->SendLength,
1066                                       NdefMap->SendRecvLength);
1067   }
1068   if (NFCSTATUS_PENDING == status) {
1069     status = NFCSTATUS_SUCCESS;
1070   } else {
1071     LOG(ERROR) << StringPrintf("ERROR: Mfc_Transceive = 0x%x", status);
1072   }
1073 
1074   return status;
1075 }
1076 
1077 /*******************************************************************************
1078 **
1079 ** Function         nativeNfcExtns_doTransceive
1080 **
1081 ** Description      Sends raw frame to BCM stack.
1082 **
1083 ** Returns          NFCSTATUS_PENDING - if successfully initiated
1084 **                  NFCSTATUS_FAILED  - otherwise
1085 **
1086 *******************************************************************************/
nativeNfcExtns_doTransceive(uint8_t * buff,uint16_t buffSz)1087 static NFCSTATUS nativeNfcExtns_doTransceive(uint8_t* buff, uint16_t buffSz) {
1088   NFCSTATUS wStatus = NFCSTATUS_PENDING;
1089   tNFA_STATUS status =
1090       NFA_SendRawFrame(buff, buffSz, NFA_DM_DEFAULT_PRESENCE_CHECK_START_DELAY);
1091 
1092   if (status != NFA_STATUS_OK) {
1093     LOG(ERROR) << StringPrintf("%s: fail send; error=%d", __func__, status);
1094     wStatus = NFCSTATUS_FAILED;
1095   }
1096 
1097   return wStatus;
1098 }
1099 
1100 /*******************************************************************************
1101 **
1102 ** Function          phNciNfc_RecvMfResp
1103 **
1104 ** Description      This function shall be invoked as part of ReaderMgmt data
1105 **                  exchange sequence handler on receiving response/data from
1106 *NFCC
1107 **
1108 ** Returns          NFCSTATUS_SUCCESS - Data Reception is successful
1109 **                  NFCSTATUS_FAILED  - Data Reception failed
1110 **
1111 *******************************************************************************/
phNciNfc_RecvMfResp(phNciNfc_Buff_t * RspBuffInfo,NFCSTATUS wStatus)1112 static NFCSTATUS phNciNfc_RecvMfResp(phNciNfc_Buff_t* RspBuffInfo,
1113                                      NFCSTATUS wStatus) {
1114   NFCSTATUS status = NFCSTATUS_SUCCESS;
1115   uint16_t wPldDataSize = 0;
1116   phNciNfc_ExtnRespId_t RecvdExtnRspId = phNciNfc_e_InvalidRsp;
1117   if (NULL == RspBuffInfo) {
1118     status = NFCSTATUS_FAILED;
1119   } else {
1120     if (((PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE) >
1121          RspBuffInfo->wLen) ||
1122         (PH_NCINFC_STATUS_OK != wStatus) || (NULL == (RspBuffInfo->pBuff))) {
1123       status = NFCSTATUS_FAILED;
1124     } else {
1125       RecvdExtnRspId = (phNciNfc_ExtnRespId_t)RspBuffInfo->pBuff[0];
1126 
1127       switch (RecvdExtnRspId) {
1128         case phNciNfc_e_MfXchgDataRsp: {
1129           NFCSTATUS writeResponse = NFCSTATUS_SUCCESS;
1130           /* check the status byte */
1131           if (NFC_GetNCIVersion() == NCI_VERSION_2_0 &&
1132               (NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WR_TLV ||
1133                NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WRITE ||
1134                NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN ||
1135                NdefMap->State == PH_FRINFC_NDEFMAP_STATE_INIT)) {
1136             uint8_t rspAck = RspBuffInfo->pBuff[RspBuffInfo->wLen - 2];
1137             uint8_t rspAckMask = ((RspBuffInfo->pBuff[RspBuffInfo->wLen - 1]) &
1138                                   MAX_NUM_VALID_BITS_FOR_ACK);
1139             NCI_CALCULATE_ACK(rspAck, rspAckMask);
1140             writeResponse =
1141                 (rspAck == T2T_RSP_ACK) ? NFCSTATUS_SUCCESS : NFC_STATUS_FAILED;
1142           } else {
1143             writeResponse = RspBuffInfo->pBuff[RspBuffInfo->wLen - 1];
1144           }
1145           if (PH_NCINFC_STATUS_OK == writeResponse) {
1146             status = NFCSTATUS_SUCCESS;
1147             uint16_t wRecvDataSz = 0;
1148 
1149             /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1150             wPldDataSize = ((RspBuffInfo->wLen) -
1151                             (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
1152             wRecvDataSz = NCI_MAX_DATA_LEN;
1153 
1154             /* wPldDataSize = wPldDataSize-1; ==> ignoring the last status byte
1155              * appended with data */
1156             if ((wPldDataSize) <= wRecvDataSz) {
1157               /* Extract the data part from pBuff[2] & fill it to be sent to
1158                * upper layer */
1159               memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[1]),
1160                      (wPldDataSize));
1161               /* update the number of bytes received from lower layer,excluding
1162                * the status byte */
1163               *(NdefMap->SendRecvLength) = wPldDataSize;
1164             } else {
1165               // TODO:- Map some status for remaining extra data received to be
1166               // sent back to caller??
1167               status = NFCSTATUS_FAILED;
1168             }
1169           } else {
1170             status = NFCSTATUS_FAILED;
1171           }
1172         } break;
1173 
1174         case phNciNfc_e_MfcAuthRsp: {
1175           /* check the status byte */
1176           if (PH_NCINFC_STATUS_OK == RspBuffInfo->pBuff[1]) {
1177             if (gAuthCmdBuf.auth_sent == true) {
1178               MfcPresenceCheckResult(NFCSTATUS_SUCCESS);
1179               return NFCSTATUS_SUCCESS;
1180             }
1181             gAuthCmdBuf.auth_status = true;
1182             status = NFCSTATUS_SUCCESS;
1183             if ((PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE) >
1184                 RspBuffInfo->wLen) {
1185               android_errorWriteLog(0x534e4554, "126204073");
1186               return NFCSTATUS_FAILED;
1187             }
1188             /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1189             wPldDataSize = ((RspBuffInfo->wLen) -
1190                             (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
1191 
1192             /* Extract the data part from pBuff[2] & fill it to be sent to upper
1193              * layer */
1194             memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[2]),
1195                    wPldDataSize);
1196             /* update the number of bytes received from lower layer,excluding
1197              * the status byte */
1198             *(NdefMap->SendRecvLength) = wPldDataSize;
1199           } else {
1200             if (gAuthCmdBuf.auth_sent == true) {
1201               gAuthCmdBuf.auth_status = false;
1202               MfcPresenceCheckResult(NFCSTATUS_FAILED);
1203               return NFCSTATUS_SUCCESS;
1204             } else {
1205               /* Reset the stored auth command buffer */
1206               memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1207               gAuthCmdBuf.pauth_cmd->length = 0;
1208               gAuthCmdBuf.auth_status = false;
1209             }
1210             status = NFCSTATUS_FAILED;
1211           }
1212         } break;
1213 
1214         default: { status = NFCSTATUS_FAILED; } break;
1215       }
1216     }
1217   }
1218 
1219   return status;
1220 }
1221 
1222 /*******************************************************************************
1223 **
1224 ** Function         phLibNfc_SendWrt16CmdPayload
1225 **
1226 ** Description      This function map the raw write cmd
1227 **
1228 ** Returns          NFCSTATUS_SUCCESS            - Command framing done
1229 **                  NFCSTATUS_INVALID_PARAMETER  - Otherwise
1230 **
1231 *******************************************************************************/
phLibNfc_SendWrt16CmdPayload(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1232 static NFCSTATUS phLibNfc_SendWrt16CmdPayload(
1233     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1234     pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1235   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1236 
1237   if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1238       (0 != (pTransceiveInfo->sSendData.length))) {
1239     memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1240            (pTransceiveInfo->sSendData.length));
1241     pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1242     pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1243   } else {
1244     wStatus = NFCSTATUS_INVALID_PARAMETER;
1245   }
1246 
1247   if (gphNxpExtns_Context.RawWriteCallBack == true) {
1248     EXTNS_SetCallBackFlag(true);
1249     gphNxpExtns_Context.RawWriteCallBack = false;
1250   }
1251 
1252   return wStatus;
1253 }
1254 
1255 /*******************************************************************************
1256 **
1257 ** Function         phLibNfc_SendIncDecCmdPayload
1258 **
1259 ** Description      This function prepares the Increment/Decrement Value to be
1260 **                  sent. This is called after sending the Increment/Decrement
1261 **                  command is already sent and successfull
1262 **
1263 ** Returns          NFCSTATUS_SUCCESS            - Payload framing done
1264 **                  NFCSTATUS_INVALID_PARAMETER  - Otherwise
1265 **
1266 *******************************************************************************/
phLibNfc_SendIncDecCmdPayload(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1267 static NFCSTATUS phLibNfc_SendIncDecCmdPayload(
1268     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1269     pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1270   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1271 
1272   if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1273       (0 != (pTransceiveInfo->sSendData.length))) {
1274     memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1275            (pTransceiveInfo->sSendData.length));
1276     pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1277     pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1278   } else {
1279     wStatus = NFCSTATUS_INVALID_PARAMETER;
1280   }
1281 
1282   if (gphNxpExtns_Context.RawWriteCallBack == true) {
1283     EXTNS_SetCallBackFlag(true);
1284     gphNxpExtns_Context.RawWriteCallBack = false;
1285   }
1286 
1287   return wStatus;
1288 }
1289 
1290 /*******************************************************************************
1291 **
1292 ** Function         Mfc_RecvPacket
1293 **
1294 ** Description      Decodes Mifare Classic Tag Response
1295 **                  This is called from NFA_SendRaw Callback
1296 **
1297 ** Returns:
1298 **                  NFCSTATUS_SUCCESS - if successfully initiated
1299 **                  NFCSTATUS_FAILED  - otherwise
1300 **
1301 *******************************************************************************/
Mfc_RecvPacket(uint8_t * buff,uint8_t buffSz)1302 NFCSTATUS Mfc_RecvPacket(uint8_t* buff, uint8_t buffSz) {
1303   NFCSTATUS status = NFCSTATUS_SUCCESS;
1304   phNciNfc_Buff_t RspBuff;
1305   uint8_t* pcmd_buff;
1306   uint16_t buffSize;
1307 
1308   RspBuff.pBuff = buff;
1309   RspBuff.wLen = buffSz;
1310   status = phNciNfc_RecvMfResp(&RspBuff, status);
1311   if (true == gAuthCmdBuf.auth_sent) {
1312     DLOG_IF(INFO, nfc_debug_enabled)
1313         << StringPrintf("%s Mfc Check Presence in progress", __func__);
1314     gAuthCmdBuf.auth_sent = false;
1315     return status;
1316   }
1317   if (true == gphNxpExtns_Context.writecmdFlag &&
1318       (NFCSTATUS_SUCCESS == status)) {
1319     pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1320     if (NULL == pcmd_buff) {
1321       return NFCSTATUS_FAILED;
1322     }
1323     buffSize = MAX_BUFF_SIZE;
1324     gphNxpExtns_Context.writecmdFlag = false;
1325     phLibNfc_SendWrt16CmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1326     status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1327     if (NFCSTATUS_PENDING != status) {
1328       LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1329     } else {
1330       status = NFCSTATUS_SUCCESS;
1331     }
1332     if (pcmd_buff != NULL) {
1333       free(pcmd_buff);
1334       pcmd_buff = NULL;
1335     }
1336   } else if (true == gphNxpExtns_Context.incrdecflag &&
1337              (NFCSTATUS_SUCCESS == status)) {
1338     pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1339     if (NULL == pcmd_buff) {
1340       return NFCSTATUS_FAILED;
1341     }
1342     buffSize = MAX_BUFF_SIZE;
1343     gphNxpExtns_Context.incrdecflag = false;
1344     phLibNfc_SendIncDecCmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1345     status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1346     if (NFCSTATUS_PENDING != status) {
1347       LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1348     } else {
1349       status = NFCSTATUS_SUCCESS;
1350     }
1351     gphNxpExtns_Context.incrdecstatusflag = true;
1352     if (pcmd_buff != NULL) {
1353       free(pcmd_buff);
1354       pcmd_buff = NULL;
1355     }
1356 
1357   } else {
1358     if (gphNxpExtns_Context.CallBackMifare != NULL) {
1359       if ((gphNxpExtns_Context.incrdecstatusflag == true) && status == 0xB2) {
1360         gphNxpExtns_Context.incrdecstatusflag = false;
1361         status = NFCSTATUS_SUCCESS;
1362       }
1363       gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt,
1364                                          status);
1365     }
1366   }
1367 
1368   return status;
1369 }
1370 
1371 /*******************************************************************************
1372 **
1373 ** Function         phNciNfc_MfCreateXchgDataHdr
1374 **
1375 ** Description      This function builds the payload header for mifare XchgData
1376 **                  request to be sent to NFCC.
1377 **
1378 ** Returns          NFCSTATUS_PENDING            - Command framing done
1379 **                  NFCSTATUS_FAILED             - Otherwise
1380 **
1381 *******************************************************************************/
phNciNfc_MfCreateXchgDataHdr(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t * buff,uint16_t * buffSz)1382 static NFCSTATUS phNciNfc_MfCreateXchgDataHdr(
1383     phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t* buff, uint16_t* buffSz)
1384 
1385 {
1386   NFCSTATUS status = NFCSTATUS_SUCCESS;
1387   uint8_t i = 0;
1388 
1389   buff[i++] = phNciNfc_e_MfRawDataXchgHdr;
1390   memcpy(&buff[i], tTranscvInfo.tSendData.pBuff, tTranscvInfo.tSendData.wLen);
1391   *buffSz = i + tTranscvInfo.tSendData.wLen;
1392 
1393   status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
1394 
1395   return status;
1396 }
1397 
1398 /*******************************************************************************
1399 **
1400 ** Function         phNciNfc_MfCreateAuthCmdHdr
1401 **
1402 ** Description      This function builds the payload header for mifare
1403 **                  classic Authenticate command to be sent to NFCC.
1404 **
1405 ** Returns          NFCSTATUS_PENDING            - Command framing done
1406 **                  NFCSTATUS_FAILED             - Otherwise
1407 **
1408 *******************************************************************************/
phNciNfc_MfCreateAuthCmdHdr(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t bBlockAddr,uint8_t * buff,uint16_t * buffSz)1409 static NFCSTATUS phNciNfc_MfCreateAuthCmdHdr(
1410     phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t bBlockAddr, uint8_t* buff,
1411     uint16_t* buffSz) {
1412   NFCSTATUS status = NFCSTATUS_SUCCESS;
1413   //    pphNciNfc_RemoteDevInformation_t  pActivDev = NULL;
1414   //    /*commented to eliminate unused variable warning*/
1415   uint8_t bKey = 0x00;
1416 
1417   /*No need to check range of block address*/
1418   /*To check for Authenticate A or Authenticate B type command*/
1419   if (PHNCINFC_AUTHENTICATION_KEYB == tTranscvInfo.tSendData.pBuff[0]) {
1420     bKey = bKey | PHNCINFC_ENABLE_KEY_B;
1421   }
1422 
1423   /*TO Do last 4 bits of Key to be set based of firmware implementation*/
1424   /*this value is hardcoded but based on firmware implementation change this
1425    * value*/
1426   bKey = (bKey | PHNCINFC_AUTHENTICATION_KEY);
1427 
1428   bKey |= tTranscvInfo.tSendData.pBuff[2];
1429 
1430   /*For authentication extension no need to copy tSendData buffer of
1431    * tTranscvInfo */
1432   tTranscvInfo.tSendData.wLen = 0x00;
1433 
1434   buff[0] = phNciNfc_e_MfcAuthReq;
1435   buff[1] = bBlockAddr;
1436   buff[2] = bKey;
1437 
1438   *buffSz = 0x03;
1439   if (bKey & PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY) {
1440     memcpy(&buff[3], &tTranscvInfo.tSendData.pBuff[3], PHLIBNFC_MFC_AUTHKEYLEN);
1441     *buffSz += PHLIBNFC_MFC_AUTHKEYLEN;
1442   }
1443   /* Store the auth command buffer to use further for presence check */
1444   if (gAuthCmdBuf.pauth_cmd != NULL) {
1445     memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1446     gAuthCmdBuf.pauth_cmd->length = *buffSz;
1447     memcpy(gAuthCmdBuf.pauth_cmd->buffer, buff, *buffSz);
1448   }
1449   status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
1450 
1451   return status;
1452 }
1453 
1454 /*******************************************************************************
1455 **
1456 ** Function         phNciNfc_SendMfReq
1457 **
1458 ** Description      This function shall be invoked as part of ReaderMgmt data
1459 **                  exchange sequence handler.
1460 **                  It shall send the request packet to NFCC.
1461 **
1462 ** Returns          NFCSTATUS_PENDING  - Send request is Pending
1463 **                  NFCSTATUS_FAILED   - otherwise
1464 **
1465 *******************************************************************************/
phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t * buff,uint16_t * buffSz)1466 static NFCSTATUS phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,
1467                                     uint8_t* buff, uint16_t* buffSz) {
1468   NFCSTATUS status = NFCSTATUS_SUCCESS;
1469 
1470   switch (tTranscvInfo.uCmd.T2TCmd) {
1471     case phNciNfc_eT2TRaw: {
1472       status = phNciNfc_MfCreateXchgDataHdr(tTranscvInfo, buff, buffSz);
1473     } break;
1474     case phNciNfc_eT2TAuth: {
1475       status = phNciNfc_MfCreateAuthCmdHdr(tTranscvInfo, (tTranscvInfo.bAddr),
1476                                            buff, buffSz);
1477     } break;
1478     default: {
1479       status = NFCSTATUS_FAILED;
1480       break;
1481     }
1482   }
1483 
1484   return status;
1485 }
1486 
1487 /*******************************************************************************
1488 **
1489 ** Function         phLibNfc_CalSectorAddress
1490 **
1491 ** Description      This function update the sector address for Mifare classic
1492 **
1493 ** Returns          none
1494 **
1495 *******************************************************************************/
phLibNfc_CalSectorAddress(uint8_t * Sector_Address)1496 static void phLibNfc_CalSectorAddress(uint8_t* Sector_Address) {
1497   uint8_t BlockNumber = 0x00;
1498 
1499   if (NULL != Sector_Address) {
1500     BlockNumber = *Sector_Address;
1501     if (BlockNumber >= PHLIBNFC_MIFARESTD4K_BLK128) {
1502       *Sector_Address = (uint8_t)(PHLIBNFC_MIFARESTD_SECTOR_NO32 +
1503                                   ((BlockNumber - PHLIBNFC_MIFARESTD4K_BLK128) /
1504                                    PHLIBNFC_MIFARESTD_BLOCK_BYTES));
1505     } else {
1506       *Sector_Address = BlockNumber / PHLIBNFC_NO_OF_BLKPERSECTOR;
1507     }
1508   } else {
1509   }
1510 
1511   return;
1512 }
1513 
1514 /*******************************************************************************
1515 **
1516 ** Function         phLibNfc_GetKeyNumberMFC
1517 **
1518 ** Description      This function find key number based on authentication
1519 *command
1520 **
1521 ** Returns          NFCSTATUS_SUCCESS  - If found the key number
1522 **                  NFCSTATUS_FAILED   - otherwise
1523 **
1524 *******************************************************************************/
phLibNfc_GetKeyNumberMFC(uint8_t * buffer,uint8_t * bKey)1525 static NFCSTATUS phLibNfc_GetKeyNumberMFC(uint8_t* buffer, uint8_t* bKey) {
1526   int32_t sdwStat = 0X00;
1527   NFCSTATUS wStatus = NFCSTATUS_INVALID_PARAMETER;
1528 
1529   uint8_t bIndex = 0x00;
1530   uint8_t bNoOfKeys = 0x00;
1531 
1532 #if PHLIBNFC_NXPETENSION_CONFIGURE_MFKEYS
1533   uint8_t aMfc_keys[NXP_NUMBER_OF_MFC_KEYS][NXP_MFC_KEY_SIZE] = NXP_MFC_KEYS;
1534 #else
1535   uint8_t aMfc_keys[1][1] = {{0x00}};
1536 #endif
1537 
1538   if (NULL != bKey && NULL != buffer) {
1539     bNoOfKeys = sizeof(aMfc_keys) / NXP_MFC_KEY_SIZE;
1540     /* Traverse through the keys stored to determine whether keys is preloaded
1541      * key */
1542     for (bIndex = 0; bIndex < bNoOfKeys; bIndex++) {
1543       /* Check passed key is NDEF key */
1544       sdwStat = memcmp(&buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD], aMfc_keys[bIndex],
1545                        PHLIBNFC_MFC_AUTHKEYLEN);
1546       if (!sdwStat) {
1547         LOG(ERROR) << StringPrintf(
1548             "Mifare : phLibNfc_GetKeyNumberMFC Key found");
1549         *bKey = bIndex;
1550         wStatus = NFCSTATUS_SUCCESS;
1551         break;
1552       }
1553     }
1554     LOG(ERROR) << StringPrintf(
1555         "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x Key = 0x%x",
1556         wStatus, *bKey);
1557   } else {
1558     wStatus = NFCSTATUS_FAILED;
1559     LOG(ERROR) << StringPrintf(
1560         "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x", wStatus);
1561   }
1562 
1563   return wStatus;
1564 }
1565 
1566 /*******************************************************************************
1567 **
1568 ** Function         phLibNfc_ChkAuthCmdMFC
1569 **
1570 ** Description      This function Check Authentication command send is proper or
1571 *not
1572 **
1573 ** Returns          NFCSTATUS_SUCCESS  - Authenticate command is proper
1574 **                  NFCSTATUS_FAILED   - otherwise
1575 **
1576 *******************************************************************************/
phLibNfc_ChkAuthCmdMFC(phNfc_sTransceiveInfo_t * pTransceiveInfo,uint8_t * bKey)1577 static NFCSTATUS phLibNfc_ChkAuthCmdMFC(
1578     phNfc_sTransceiveInfo_t* pTransceiveInfo, uint8_t* bKey) {
1579   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1580 
1581   if (NULL != pTransceiveInfo && NULL != pTransceiveInfo->sSendData.buffer &&
1582       0 != pTransceiveInfo->sSendData.length && NULL != bKey) {
1583     if ((pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentA ||
1584          pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentB)) {
1585       wStatus =
1586           phLibNfc_GetKeyNumberMFC(pTransceiveInfo->sSendData.buffer, bKey);
1587     } else {
1588       wStatus = NFCSTATUS_FAILED;
1589     }
1590   } else {
1591     wStatus = NFCSTATUS_FAILED;
1592   }
1593   return wStatus;
1594 }
1595 
1596 /*******************************************************************************
1597 **
1598 ** Function         phLibNfc_MifareMap
1599 **
1600 ** Description      Mifare Mapping Utility function
1601 **
1602 ** Returns          NFCSTATUS_SUCCESS             - Mapping is proper
1603 **                  NFCSTATUS_INVALID_PARAMETER   - otherwise
1604 **
1605 *******************************************************************************/
phLibNfc_MifareMap(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1606 static NFCSTATUS phLibNfc_MifareMap(
1607     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1608     pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1609   NFCSTATUS status = NFCSTATUS_SUCCESS;
1610   uint8_t bBuffIdx = 0;
1611   uint8_t bSectorNumber;
1612   uint8_t bKey = 0;
1613 
1614   switch (pTransceiveInfo->cmd.MfCmd) {
1615     case phNfc_eMifareRead16: {
1616       if ((NULL != pTransceiveInfo->sRecvData.buffer) &&
1617           (0 != (pTransceiveInfo->sRecvData.length))) {
1618         pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareRead16;
1619         pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1620         pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1621         pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1622       } else {
1623         status = NFCSTATUS_INVALID_PARAMETER;
1624       }
1625     } break;
1626 
1627     case phNfc_eMifareWrite16: {
1628       if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1629           (0 != (pTransceiveInfo->sSendData.length))) {
1630         pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1631         pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1632         memcpy(&(pMappedTranscvIf->tSendData.pBuff[bBuffIdx]),
1633                pTransceiveInfo->sSendData.buffer,
1634                (pTransceiveInfo->sSendData.length));
1635         pMappedTranscvIf->tSendData.wLen =
1636             bBuffIdx + pTransceiveInfo->sSendData.length;
1637         pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1638       } else {
1639         status = NFCSTATUS_INVALID_PARAMETER;
1640       }
1641     } break;
1642 
1643     case phNfc_eMifareAuthentA:
1644     case phNfc_eMifareAuthentB: {
1645       if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1646           (0 != (pTransceiveInfo->sSendData.length)) &&
1647           (NULL != pTransceiveInfo->sRecvData.buffer) &&
1648           (0 != (pTransceiveInfo->sRecvData.length))) {
1649         status = phLibNfc_ChkAuthCmdMFC(pTransceiveInfo, &bKey);
1650         if (NFCSTATUS_FAILED != status) {
1651           bSectorNumber = pTransceiveInfo->addr;
1652           phLibNfc_CalSectorAddress(&bSectorNumber);
1653           /*For creating extension command header pTransceiveInfo's MfCmd get
1654            * used*/
1655           pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] =
1656               pTransceiveInfo->cmd.MfCmd;
1657           pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bSectorNumber;
1658           pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TAuth;
1659           pMappedTranscvIf->bAddr = bSectorNumber;
1660           pMappedTranscvIf->bNumBlock = pTransceiveInfo->NumBlock;
1661           if (NFCSTATUS_SUCCESS == status) {
1662             pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1663             (pMappedTranscvIf->tSendData.wLen) = (uint16_t)(bBuffIdx);
1664 
1665           } else if (NFCSTATUS_INVALID_PARAMETER == status) {
1666             bKey = bKey | PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY;
1667             pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1668             memcpy(&pMappedTranscvIf->tSendData.pBuff[bBuffIdx],
1669                    &pTransceiveInfo->sSendData
1670                         .buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD],
1671                    PHLIBNFC_MFC_AUTHKEYLEN);
1672 
1673             (pMappedTranscvIf->tSendData.wLen) =
1674                 (uint16_t)(bBuffIdx + PHLIBNFC_MFC_AUTHKEYLEN);
1675             status = NFCSTATUS_SUCCESS;
1676           } else {
1677             /* do nothing */
1678           }
1679         }
1680       } else {
1681         status = NFCSTATUS_INVALID_PARAMETER;
1682       }
1683     } break;
1684 
1685     case phNfc_eMifareRaw: {
1686     } break;
1687 
1688     default: {
1689       status = NFCSTATUS_INVALID_PARAMETER;
1690       break;
1691     }
1692   }
1693 
1694   return status;
1695 }
1696 
1697 /*******************************************************************************
1698 **
1699 ** Function         phLibNfc_MapCmds
1700 **
1701 ** Description      This function maps the command request from libnfc level to
1702 *nci level
1703 **
1704 ** Returns          NFCSTATUS_SUCCESS           - Mapping of command is
1705 *successful
1706 **                  NFCSTATUS_INVALID_PARAMETER - One or more of the supplied
1707 **                  parameters could not be interpreted properly
1708 **
1709 *******************************************************************************/
phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1710 static NFCSTATUS phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,
1711                                   phNfc_sTransceiveInfo_t* pTransceiveInfo,
1712                                   pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1713   NFCSTATUS status = NFCSTATUS_SUCCESS;
1714 
1715   if ((NULL == pTransceiveInfo) || (NULL == pMappedTranscvIf)) {
1716     return NFCSTATUS_FAILED;
1717   }
1718   switch (RemDevType) {
1719     case phNciNfc_eMifare1k_PICC:
1720     case phNciNfc_eMifare4k_PICC: {
1721       status = phLibNfc_MifareMap(pTransceiveInfo, pMappedTranscvIf);
1722       break;
1723     }
1724     default: { break; }
1725   }
1726 
1727   return status;
1728 }
1729 
1730 /*******************************************************************************
1731 **
1732 ** Function         phLibNfc_SendAuthCmd
1733 **
1734 ** Description      This function Send authentication command to NFCC
1735 **
1736 ** Returns          NFCSTATUS_SUCCESS           - Parameters are proper
1737 **                  NFCSTATUS_INVALID_PARAMETER - Otherwise
1738 **
1739 *******************************************************************************/
phLibNfc_SendAuthCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,phNciNfc_TransceiveInfo_t * tNciTranscvInfo)1740 static NFCSTATUS phLibNfc_SendAuthCmd(
1741     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1742     phNciNfc_TransceiveInfo_t* tNciTranscvInfo) {
1743   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1744 
1745   wStatus = phLibNfc_MapCmds(phNciNfc_eMifare1k_PICC, pTransceiveInfo,
1746                              tNciTranscvInfo);
1747 
1748   return wStatus;
1749 }
1750 
1751 /*******************************************************************************
1752 **
1753 ** Function         phLibNfc_SendWrt16Cmd
1754 **
1755 ** Description      This function maps Mifarewirte16 commands
1756 **
1757 ** Returns          NFCSTATUS_SUCCESS           - Parameters are mapped
1758 **                  NFCSTATUS_INVALID_PARAMETER - Otherwise
1759 **
1760 *******************************************************************************/
phLibNfc_SendWrt16Cmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1761 static NFCSTATUS phLibNfc_SendWrt16Cmd(
1762     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1763     pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1764   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1765   uint8_t bBuffIdx = 0x00;
1766 
1767   if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1768       (0 != (pTransceiveInfo->sSendData.length))) {
1769     pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1770     pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1771     pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1772     pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1773   } else {
1774     wStatus = NFCSTATUS_INVALID_PARAMETER;
1775   }
1776 
1777   return wStatus;
1778 }
1779 
1780 /*******************************************************************************
1781 **
1782 ** Function         phLibNfc_SendIncDecCmd
1783 **
1784 ** Description      This function prepares the Increment/Decrement command
1785 **                  to be sent, increment/decrement value is sent separately
1786 **
1787 ** Returns          NFCSTATUS_SUCCESS           - Params are mapped
1788 **                  NFCSTATUS_INVALID_PARAMETER - Otherwise
1789 **
1790 *******************************************************************************/
phLibNfc_SendIncDecCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf,uint8_t IncDecCmd)1791 static NFCSTATUS phLibNfc_SendIncDecCmd(
1792     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1793     pphNciNfc_TransceiveInfo_t pMappedTranscvIf, uint8_t IncDecCmd) {
1794   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1795   uint8_t bBuffIdx = 0x00;
1796 
1797   if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1798       (0 != (pTransceiveInfo->sSendData.length))) {
1799     pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = IncDecCmd;
1800     pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1801     pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1802     pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1803   } else {
1804     wStatus = NFCSTATUS_INVALID_PARAMETER;
1805   }
1806 
1807   return wStatus;
1808 }
1809 
1810 /*******************************************************************************
1811 **
1812 ** Function         phLibNfc_SendRawCmd
1813 **
1814 ** Description      This function maps Mifare raw command
1815 **
1816 ** Returns          NFCSTATUS_SUCCESS           - Parameters are mapped
1817 **                  NFCSTATUS_INVALID_PARAMETER - Otherwise
1818 **
1819 *******************************************************************************/
phLibNfc_SendRawCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1820 static NFCSTATUS phLibNfc_SendRawCmd(
1821     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1822     pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1823   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1824   //    uint8_t bBuffIdx = 0x00;                                  /*commented to
1825   //    eliminate unused variable warning*/
1826 
1827   if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1828       (0 != (pTransceiveInfo->sSendData.length))) {
1829     memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1830            (pTransceiveInfo->sSendData.length));
1831     pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1832     pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1833   } else {
1834     wStatus = NFCSTATUS_INVALID_PARAMETER;
1835   }
1836 
1837   return wStatus;
1838 }
1839 
1840 /*******************************************************************************
1841 **
1842 ** Function         phFriNfc_ExtnsTransceive
1843 **
1844 ** Description      This function maps Mifare raw command and send it to NFCC
1845 **
1846 ** Returns          NFCSTATUS_PENDING           - Operation successful
1847 **                  NFCSTATUS_INVALID_PARAMETER - Otherwise
1848 **
1849 *******************************************************************************/
phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t * pTransceiveInfo,phNfc_uCmdList_t Cmd,uint8_t * SendRecvBuf,uint16_t SendLength,uint16_t * SendRecvLength)1850 NFCSTATUS phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1851                                    phNfc_uCmdList_t Cmd, uint8_t* SendRecvBuf,
1852                                    uint16_t SendLength,
1853                                    uint16_t* SendRecvLength) {
1854   (void)SendRecvLength;
1855   NFCSTATUS status = NFCSTATUS_FAILED;
1856   uint8_t* buff = NULL;
1857   uint16_t buffSz = 0;
1858   uint8_t i = 0;
1859   uint32_t length = SendLength;
1860   uint8_t restore_payload[] = {
1861       0x00, 0x00, 0x00, 0x00,
1862   };
1863 
1864   if (SendLength == 0) {
1865     android_errorWriteLog(0x534e4554, "132083376");
1866     return status;
1867   }
1868 
1869   buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1870   if (NULL == buff) {
1871     return status;
1872   }
1873 
1874   pTransceiveInfo->cmd = Cmd;
1875 
1876   if ((Cmd.MfCmd == phNfc_eMifareAuthentA) ||
1877       (Cmd.MfCmd == phNfc_eMifareAuthentB)) {
1878     pTransceiveInfo->addr = SendRecvBuf[i++];
1879     pTransceiveInfo->sSendData.buffer[4] = SendRecvBuf[i++];
1880     pTransceiveInfo->sSendData.buffer[5] = SendRecvBuf[i++];
1881     pTransceiveInfo->sSendData.buffer[6] = SendRecvBuf[i++];
1882     pTransceiveInfo->sSendData.buffer[7] = SendRecvBuf[i++];
1883     pTransceiveInfo->sSendData.buffer[8] = SendRecvBuf[i++];
1884     pTransceiveInfo->sSendData.buffer[9] = SendRecvBuf[i++];
1885 
1886     pTransceiveInfo->cmd.MfCmd = Cmd.MfCmd;
1887 
1888     pTransceiveInfo->sSendData.length = length;
1889     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1890     status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1891   } else if (Cmd.MfCmd == phNfc_eMifareWrite16) {
1892     pTransceiveInfo->addr = SendRecvBuf[i++];
1893     length = SendLength - i;
1894     memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1895     pTransceiveInfo->sSendData.length = length;
1896     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1897 
1898     gphNxpExtns_Context.writecmdFlag = true;
1899 
1900     status = phLibNfc_SendWrt16Cmd(pTransceiveInfo, &tNciTranscvInfo);
1901   } else if ((Cmd.MfCmd == phNfc_eMifareInc) ||
1902              (Cmd.MfCmd == phNfc_eMifareDec)) {
1903     pTransceiveInfo->addr = SendRecvBuf[i++];
1904     length = SendLength - i;
1905     memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1906     pTransceiveInfo->sSendData.length = length;
1907     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1908 
1909     gphNxpExtns_Context.incrdecflag = true;
1910 
1911     status =
1912         phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd);
1913 
1914   } else if (Cmd.MfCmd == phNfc_eMifareRestore) {
1915     pTransceiveInfo->addr = SendRecvBuf[i++];
1916     length = SendLength - i;
1917     memcpy(pTransceiveInfo->sSendData.buffer, &restore_payload[0],
1918            sizeof(restore_payload));
1919     pTransceiveInfo->sSendData.length = length + sizeof(restore_payload);
1920     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1921 
1922     gphNxpExtns_Context.incrdecflag = true;
1923 
1924     status =
1925         phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd);
1926 
1927   } else if ((Cmd.MfCmd == phNfc_eMifareRaw) ||
1928              (Cmd.MfCmd == phNfc_eMifareTransfer)) {
1929     pTransceiveInfo->cmd.MfCmd = (phNfc_eMifareCmdList_t)phNciNfc_eT2TRaw;
1930     memcpy(pTransceiveInfo->sSendData.buffer, SendRecvBuf, length);
1931     pTransceiveInfo->sSendData.length = length;
1932     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1933     status = phLibNfc_SendRawCmd(pTransceiveInfo, &tNciTranscvInfo);
1934   } else {
1935     pTransceiveInfo->addr = SendRecvBuf[i++];
1936     length = SendLength - i;
1937     memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1938     pTransceiveInfo->sSendData.length = length;
1939     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1940     status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1941   }
1942 
1943   if (NFCSTATUS_SUCCESS == status) {
1944     status = phNciNfc_SendMfReq(tNciTranscvInfo, buff, &buffSz);
1945     if (NFCSTATUS_PENDING != status) {
1946       LOG(ERROR) << StringPrintf("ERROR : phNciNfc_SendMfReq()");
1947     }
1948   } else {
1949     LOG(ERROR) << StringPrintf(" ERROR : Sending phNciNfc_SendMfReq");
1950   }
1951   if (buff != NULL) {
1952     free(buff);
1953     buff = NULL;
1954   }
1955 
1956   return status;
1957 }
1958