• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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[6] = {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)((phLibNfc_Ndef_EBegin == Offset)
641                                               ? PH_FRINFC_NDEFMAP_SEEK_BEGIN
642                                               : PH_FRINFC_NDEFMAP_SEEK_CUR);
643   }
644 
645   PacketData = NdefInfo.psUpperNdefMsg->buffer;
646   PacketDataLength = (uint32_t*)&(NdefInfo.psUpperNdefMsg->length);
647   NdefMap->bCurrReadMode = Offset;
648   status = phFriNfc_ValidateParams(PacketData, PacketDataLength, Offset,
649                                    NdefMap, PH_FRINFC_NDEF_READ_REQ);
650   if (status != NFCSTATUS_SUCCESS) {
651     goto Mfc_RdNdefEnd;
652   }
653 
654   status = phFriNfc_MifareStdMap_RdNdef(NdefMap, PacketData, PacketDataLength,
655                                         Offset);
656 
657   if (NFCSTATUS_INSUFFICIENT_STORAGE == status) {
658     NdefInfo.psUpperNdefMsg->length = 0x00;
659     status = NFCSTATUS_SUCCESS;
660   }
661 
662   if (NFCSTATUS_PENDING == status) {
663     status = NFCSTATUS_SUCCESS;
664   }
665 
666 Mfc_RdNdefEnd:
667   if (status != NFCSTATUS_SUCCESS) {
668     if (NULL != NdefInfo.psUpperNdefMsg->buffer) {
669       free(NdefInfo.psUpperNdefMsg->buffer);
670       NdefInfo.psUpperNdefMsg->buffer = NULL;
671     }
672     status = NFCSTATUS_FAILED;
673   }
674 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
675   pthread_mutex_unlock(&SharedDataMutex);
676 #endif
677   return status;
678 }
679 /*******************************************************************************
680 **
681 ** Function         Mfc_PresenceCheck
682 **
683 ** Description      It triggers receiving of the NDEF message from Mifare
684 *Classic Tag.
685 **
686 **
687 ** Returns:
688 **                  NFCSTATUS_SUCCESS - if successfully initiated
689 **                  NFCSTATUS_FAILED  - otherwise
690 **
691 *******************************************************************************/
Mfc_PresenceCheck(void)692 NFCSTATUS Mfc_PresenceCheck(void) {
693   NFCSTATUS status = NFCSTATUS_SUCCESS;
694 
695   if (gAuthCmdBuf.auth_status == true) {
696     EXTNS_SetCallBackFlag(false);
697     status = nativeNfcExtns_doTransceive(gAuthCmdBuf.pauth_cmd->buffer,
698                                          gAuthCmdBuf.pauth_cmd->length);
699     if (status != NFCSTATUS_PENDING) {
700       gAuthCmdBuf.auth_sent = false;
701       status = NFCSTATUS_FAILED;
702     } else {
703       gAuthCmdBuf.auth_sent = true;
704       status = NFCSTATUS_SUCCESS;
705     }
706   } else {
707     status = NFCSTATUS_NOT_ALLOWED;
708   }
709   DLOG_IF(INFO, nfc_debug_enabled)
710       << StringPrintf("%s status = 0x%x", __func__, status);
711   return status;
712 }
713 /*******************************************************************************
714 **
715 ** Function         Mfc_WriteNdef
716 **
717 ** Description      It triggers the NDEF data write to Mifare Classic Tag.
718 **
719 **
720 ** Returns:
721 **                  NFCSTATUS_SUCCESS - if successfully initiated
722 **                  NFCSTATUS_FAILED  - otherwise
723 **
724 *******************************************************************************/
Mfc_WriteNdef(uint8_t * p_data,uint32_t len)725 NFCSTATUS Mfc_WriteNdef(uint8_t* p_data, uint32_t len) {
726   NFCSTATUS status = NFCSTATUS_SUCCESS;
727   uint8_t* PacketData = NULL;
728   uint32_t* PacketDataLength = NULL;
729 
730   if (p_data == NULL || len == 0) {
731     LOG(ERROR) << StringPrintf("MFC Error: Invalid Parameters to Ndef Write");
732     status = NFCSTATUS_FAILED;
733     goto Mfc_WrNdefEnd;
734   }
735 
736   EXTNS_SetCallBackFlag(false);
737   gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
738   gphNxpExtns_Context.CallBackCtxt = NdefMap;
739 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
740   pthread_mutex_lock(&SharedDataMutex);
741 #endif
742   if (NdefInfo.is_ndef == PH_LIBNFC_INTERNAL_CHK_NDEF_NOT_DONE) {
743     status = NFCSTATUS_REJECTED;
744     goto Mfc_WrNdefEnd;
745   } else if (NdefInfo.is_ndef == 0) {
746     status = NFCSTATUS_NON_NDEF_COMPLIANT;
747     goto Mfc_WrNdefEnd;
748   } else if (len > NdefInfo.NdefLength) {
749     status = NFCSTATUS_NOT_ENOUGH_MEMORY;
750     goto Mfc_WrNdefEnd;
751   } else {
752     NdefInfo.psUpperNdefMsg->buffer = p_data;
753     NdefInfo.psUpperNdefMsg->length = len;
754 
755     NdefInfo.AppWrLength = len;
756     NdefMap->CompletionRoutine[2].CompletionRoutine =
757         Mfc_WriteNdef_Completion_Routine;
758     if (0 == len) {
759       /* TODO: Erase the Tag */
760     } else {
761       NdefMap->ApduBuffIndex = 0x00;
762       *NdefMap->DataCount = 0x00;
763       PacketData = NdefInfo.psUpperNdefMsg->buffer;
764       PacketDataLength = &(NdefInfo.dwWrLength);
765       NdefMap->WrNdefPacketLength = PacketDataLength;
766       NdefInfo.dwWrLength = len;
767 
768       status = phFriNfc_ValidateParams(PacketData, PacketDataLength, 0, NdefMap,
769                                        PH_FRINFC_NDEF_WRITE_REQ);
770       if (status != NFCSTATUS_SUCCESS) {
771         goto Mfc_WrNdefEnd;
772       }
773 
774       status = phFriNfc_MifareStdMap_WrNdef(
775           NdefMap, PacketData, PacketDataLength, PH_FRINFC_NDEFMAP_SEEK_BEGIN);
776 
777       if (status == NFCSTATUS_PENDING) {
778         status = NFCSTATUS_SUCCESS;
779       }
780     }
781   }
782 
783 Mfc_WrNdefEnd:
784 #if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
785   pthread_mutex_unlock(&SharedDataMutex);
786 #endif
787   if (status != NFCSTATUS_SUCCESS) {
788     status = NFCSTATUS_FAILED;
789   }
790   return status;
791 }
792 /*******************************************************************************
793 **
794 ** Function          phFriNfc_NdefSmtCrd_Reset__
795 **
796 ** Description      This function Resets the component instance to the initial
797 **                  state and initializes the internal variables.
798 **
799 ** Returns          NFCSTATUS_SUCCESS
800 **
801 *******************************************************************************/
phFriNfc_NdefSmtCrd_Reset__(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt,uint8_t * SendRecvBuffer,uint16_t * SendRecvBuffLen)802 static NFCSTATUS phFriNfc_NdefSmtCrd_Reset__(
803     phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt, uint8_t* SendRecvBuffer,
804     uint16_t* SendRecvBuffLen) {
805   //    NFCSTATUS status = NFCSTATUS_FAILED;                      /*commented to
806   //    eliminate unused variable warning*/
807   uint8_t index;
808 
809   /* Initialize the state to Init */
810   NdefSmtCrdFmt->State = PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT;
811 
812   for (index = 0; index < PH_FRINFC_SMTCRDFMT_CR; index++) {
813     /* Initialize the NdefMap Completion Routine to Null */
814     NdefSmtCrdFmt->CompletionRoutine[index].CompletionRoutine = NULL;
815     /* Initialize the NdefMap Completion Routine context to Null  */
816     NdefSmtCrdFmt->CompletionRoutine[index].Context = NULL;
817   }
818 
819   /* Trx Buffer registered */
820   NdefSmtCrdFmt->SendRecvBuf = SendRecvBuffer;
821 
822   /* Trx Buffer Size */
823   NdefSmtCrdFmt->SendRecvLength = SendRecvBuffLen;
824 
825   /* Register Transfer Buffer Length */
826   NdefSmtCrdFmt->SendLength = 0;
827 
828   /* Initialize the Format status flag*/
829   NdefSmtCrdFmt->FmtProcStatus = 0;
830 
831   /* Reset the Card Type */
832   NdefSmtCrdFmt->CardType = 0;
833 
834   /* Reset MapCompletion Info*/
835   NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.CompletionRoutine = NULL;
836   NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.Context = NULL;
837 
838   phFriNfc_MfStd_Reset(NdefSmtCrdFmt);
839 
840   return NFCSTATUS_SUCCESS;
841 }
842 
843 /*******************************************************************************
844 **
845 ** Function         Mfc_FormatNdef
846 **
847 ** Description      It triggers the NDEF format of Mifare Classic Tag.
848 **
849 **
850 ** Returns:
851 **                  NFCSTATUS_SUCCESS - if successfully initiated
852 **                  NFCSTATUS_FAILED  - otherwise
853 **
854 *******************************************************************************/
Mfc_FormatNdef(uint8_t * secretkey,uint8_t len)855 NFCSTATUS Mfc_FormatNdef(uint8_t* secretkey, uint8_t len) {
856   NFCSTATUS status = NFCSTATUS_FAILED;
857   uint8_t mif_std_key[6] = {0};
858   //    static uint8_t   Index;
859   //    /*commented to eliminate unused variable warning*/
860   uint8_t sak = 0;
861 
862   EXTNS_SetCallBackFlag(false);
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 ((0 == (RspBuffInfo->wLen)) || (PH_NCINFC_STATUS_OK != wStatus) ||
1121         (NULL == (RspBuffInfo->pBuff))) {
1122       status = NFCSTATUS_FAILED;
1123     } else {
1124       RecvdExtnRspId = (phNciNfc_ExtnRespId_t)RspBuffInfo->pBuff[0];
1125 
1126       switch (RecvdExtnRspId) {
1127         case phNciNfc_e_MfXchgDataRsp: {
1128           NFCSTATUS writeResponse = NFCSTATUS_SUCCESS;
1129           /* check the status byte */
1130           if (NFC_GetNCIVersion() == NCI_VERSION_2_0 &&
1131               (NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WR_TLV ||
1132                NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WRITE ||
1133                NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN ||
1134                NdefMap->State == PH_FRINFC_NDEFMAP_STATE_INIT)) {
1135             uint8_t rspAck = RspBuffInfo->pBuff[RspBuffInfo->wLen - 2];
1136             uint8_t rspAckMask = ((RspBuffInfo->pBuff[RspBuffInfo->wLen - 1]) &
1137                                   MAX_NUM_VALID_BITS_FOR_ACK);
1138             NCI_CALCULATE_ACK(rspAck, rspAckMask);
1139             writeResponse =
1140                 (rspAck == T2T_RSP_ACK) ? NFCSTATUS_SUCCESS : NFC_STATUS_FAILED;
1141           } else {
1142             writeResponse = RspBuffInfo->pBuff[RspBuffInfo->wLen - 1];
1143           }
1144           if (PH_NCINFC_STATUS_OK == writeResponse) {
1145             status = NFCSTATUS_SUCCESS;
1146             uint16_t wRecvDataSz = 0;
1147 
1148             /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1149             wPldDataSize = ((RspBuffInfo->wLen) -
1150                             (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
1151             wRecvDataSz = NCI_MAX_DATA_LEN;
1152 
1153             /* wPldDataSize = wPldDataSize-1; ==> ignoring the last status byte
1154              * appended with data */
1155             if ((wPldDataSize) <= wRecvDataSz) {
1156               /* Extract the data part from pBuff[2] & fill it to be sent to
1157                * upper layer */
1158               memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[1]),
1159                      (wPldDataSize));
1160               /* update the number of bytes received from lower layer,excluding
1161                * the status byte */
1162               *(NdefMap->SendRecvLength) = wPldDataSize;
1163             } else {
1164               // TODO:- Map some status for remaining extra data received to be
1165               // sent back to caller??
1166               status = NFCSTATUS_FAILED;
1167             }
1168           } else {
1169             status = NFCSTATUS_FAILED;
1170           }
1171         } break;
1172 
1173         case phNciNfc_e_MfcAuthRsp: {
1174           /* check the status byte */
1175           if (PH_NCINFC_STATUS_OK == RspBuffInfo->pBuff[1]) {
1176             if (gAuthCmdBuf.auth_sent == true) {
1177               MfcPresenceCheckResult(NFCSTATUS_SUCCESS);
1178               return NFCSTATUS_SUCCESS;
1179             }
1180             gAuthCmdBuf.auth_status = true;
1181             status = NFCSTATUS_SUCCESS;
1182 
1183             /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1184             wPldDataSize = ((RspBuffInfo->wLen) -
1185                             (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
1186 
1187             /* Extract the data part from pBuff[2] & fill it to be sent to upper
1188              * layer */
1189             memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[2]),
1190                    wPldDataSize);
1191             /* update the number of bytes received from lower layer,excluding
1192              * the status byte */
1193             *(NdefMap->SendRecvLength) = wPldDataSize;
1194           } else {
1195             if (gAuthCmdBuf.auth_sent == true) {
1196               gAuthCmdBuf.auth_status = false;
1197               MfcPresenceCheckResult(NFCSTATUS_FAILED);
1198               return NFCSTATUS_SUCCESS;
1199             } else {
1200               /* Reset the stored auth command buffer */
1201               memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1202               gAuthCmdBuf.pauth_cmd->length = 0;
1203               gAuthCmdBuf.auth_status = false;
1204             }
1205             status = NFCSTATUS_FAILED;
1206           }
1207         } break;
1208 
1209         default: { status = NFCSTATUS_FAILED; } break;
1210       }
1211     }
1212   }
1213 
1214   return status;
1215 }
1216 
1217 /*******************************************************************************
1218 **
1219 ** Function         phLibNfc_SendWrt16CmdPayload
1220 **
1221 ** Description      This function map the raw write cmd
1222 **
1223 ** Returns          NFCSTATUS_SUCCESS            - Command framing done
1224 **                  NFCSTATUS_INVALID_PARAMETER  - Otherwise
1225 **
1226 *******************************************************************************/
phLibNfc_SendWrt16CmdPayload(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1227 static NFCSTATUS phLibNfc_SendWrt16CmdPayload(
1228     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1229     pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1230   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1231 
1232   if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1233       (0 != (pTransceiveInfo->sSendData.length))) {
1234     memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1235            (pTransceiveInfo->sSendData.length));
1236     pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1237     pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1238   } else {
1239     wStatus = NFCSTATUS_INVALID_PARAMETER;
1240   }
1241 
1242   if (gphNxpExtns_Context.RawWriteCallBack == true) {
1243     EXTNS_SetCallBackFlag(true);
1244     gphNxpExtns_Context.RawWriteCallBack = false;
1245   }
1246 
1247   return wStatus;
1248 }
1249 
1250 /*******************************************************************************
1251 **
1252 ** Function         phLibNfc_SendIncDecCmdPayload
1253 **
1254 ** Description      This function prepares the Increment/Decrement Value to be
1255 **                  sent. This is called after sending the Increment/Decrement
1256 **                  command is already sent and successfull
1257 **
1258 ** Returns          NFCSTATUS_SUCCESS            - Payload framing done
1259 **                  NFCSTATUS_INVALID_PARAMETER  - Otherwise
1260 **
1261 *******************************************************************************/
phLibNfc_SendIncDecCmdPayload(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1262 static NFCSTATUS phLibNfc_SendIncDecCmdPayload(
1263     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1264     pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1265   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1266 
1267   if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1268       (0 != (pTransceiveInfo->sSendData.length))) {
1269     memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1270            (pTransceiveInfo->sSendData.length));
1271     pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1272     pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1273   } else {
1274     wStatus = NFCSTATUS_INVALID_PARAMETER;
1275   }
1276 
1277   if (gphNxpExtns_Context.RawWriteCallBack == true) {
1278     EXTNS_SetCallBackFlag(true);
1279     gphNxpExtns_Context.RawWriteCallBack = false;
1280   }
1281 
1282   return wStatus;
1283 }
1284 
1285 /*******************************************************************************
1286 **
1287 ** Function         Mfc_RecvPacket
1288 **
1289 ** Description      Decodes Mifare Classic Tag Response
1290 **                  This is called from NFA_SendRaw Callback
1291 **
1292 ** Returns:
1293 **                  NFCSTATUS_SUCCESS - if successfully initiated
1294 **                  NFCSTATUS_FAILED  - otherwise
1295 **
1296 *******************************************************************************/
Mfc_RecvPacket(uint8_t * buff,uint8_t buffSz)1297 NFCSTATUS Mfc_RecvPacket(uint8_t* buff, uint8_t buffSz) {
1298   NFCSTATUS status = NFCSTATUS_SUCCESS;
1299   phNciNfc_Buff_t RspBuff;
1300   uint8_t* pcmd_buff;
1301   uint16_t buffSize;
1302 
1303   RspBuff.pBuff = buff;
1304   RspBuff.wLen = buffSz;
1305   status = phNciNfc_RecvMfResp(&RspBuff, status);
1306   if (true == gAuthCmdBuf.auth_sent) {
1307     DLOG_IF(INFO, nfc_debug_enabled)
1308         << StringPrintf("%s Mfc Check Presence in progress", __func__);
1309     gAuthCmdBuf.auth_sent = false;
1310     return status;
1311   }
1312   if (true == gphNxpExtns_Context.writecmdFlag &&
1313       (NFCSTATUS_SUCCESS == status)) {
1314     pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1315     if (NULL == pcmd_buff) {
1316       return NFCSTATUS_FAILED;
1317     }
1318     buffSize = MAX_BUFF_SIZE;
1319     gphNxpExtns_Context.writecmdFlag = false;
1320     phLibNfc_SendWrt16CmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1321     status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1322     if (NFCSTATUS_PENDING != status) {
1323       LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1324     } else {
1325       status = NFCSTATUS_SUCCESS;
1326     }
1327     if (pcmd_buff != NULL) {
1328       free(pcmd_buff);
1329       pcmd_buff = NULL;
1330     }
1331   } else if (true == gphNxpExtns_Context.incrdecflag &&
1332              (NFCSTATUS_SUCCESS == status)) {
1333     pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1334     if (NULL == pcmd_buff) {
1335       return NFCSTATUS_FAILED;
1336     }
1337     buffSize = MAX_BUFF_SIZE;
1338     gphNxpExtns_Context.incrdecflag = false;
1339     phLibNfc_SendIncDecCmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1340     status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1341     if (NFCSTATUS_PENDING != status) {
1342       LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1343     } else {
1344       status = NFCSTATUS_SUCCESS;
1345     }
1346     gphNxpExtns_Context.incrdecstatusflag = true;
1347     if (pcmd_buff != NULL) {
1348       free(pcmd_buff);
1349       pcmd_buff = NULL;
1350     }
1351 
1352   } else {
1353     if (gphNxpExtns_Context.CallBackMifare != NULL) {
1354       if ((gphNxpExtns_Context.incrdecstatusflag == true) && status == 0xB2) {
1355         gphNxpExtns_Context.incrdecstatusflag = false;
1356         status = NFCSTATUS_SUCCESS;
1357       }
1358       gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt,
1359                                          status);
1360     }
1361   }
1362 
1363   return status;
1364 }
1365 
1366 /*******************************************************************************
1367 **
1368 ** Function         phNciNfc_MfCreateXchgDataHdr
1369 **
1370 ** Description      This function builds the payload header for mifare XchgData
1371 **                  request to be sent to NFCC.
1372 **
1373 ** Returns          NFCSTATUS_PENDING            - Command framing done
1374 **                  NFCSTATUS_FAILED             - Otherwise
1375 **
1376 *******************************************************************************/
phNciNfc_MfCreateXchgDataHdr(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t * buff,uint16_t * buffSz)1377 static NFCSTATUS phNciNfc_MfCreateXchgDataHdr(
1378     phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t* buff, uint16_t* buffSz)
1379 
1380 {
1381   NFCSTATUS status = NFCSTATUS_SUCCESS;
1382   uint8_t i = 0;
1383 
1384   buff[i++] = phNciNfc_e_MfRawDataXchgHdr;
1385   memcpy(&buff[i], tTranscvInfo.tSendData.pBuff, tTranscvInfo.tSendData.wLen);
1386   *buffSz = i + tTranscvInfo.tSendData.wLen;
1387 
1388   status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
1389 
1390   return status;
1391 }
1392 
1393 /*******************************************************************************
1394 **
1395 ** Function         phNciNfc_MfCreateAuthCmdHdr
1396 **
1397 ** Description      This function builds the payload header for mifare
1398 **                  classic Authenticate command to be sent to NFCC.
1399 **
1400 ** Returns          NFCSTATUS_PENDING            - Command framing done
1401 **                  NFCSTATUS_FAILED             - Otherwise
1402 **
1403 *******************************************************************************/
phNciNfc_MfCreateAuthCmdHdr(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t bBlockAddr,uint8_t * buff,uint16_t * buffSz)1404 static NFCSTATUS phNciNfc_MfCreateAuthCmdHdr(
1405     phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t bBlockAddr, uint8_t* buff,
1406     uint16_t* buffSz) {
1407   NFCSTATUS status = NFCSTATUS_SUCCESS;
1408   //    pphNciNfc_RemoteDevInformation_t  pActivDev = NULL;
1409   //    /*commented to eliminate unused variable warning*/
1410   uint8_t bKey = 0x00;
1411 
1412   /*No need to check range of block address*/
1413   /*To check for Authenticate A or Authenticate B type command*/
1414   if (PHNCINFC_AUTHENTICATION_KEYB == tTranscvInfo.tSendData.pBuff[0]) {
1415     bKey = bKey | PHNCINFC_ENABLE_KEY_B;
1416   }
1417 
1418   /*TO Do last 4 bits of Key to be set based of firmware implementation*/
1419   /*this value is hardcoded but based on firmware implementation change this
1420    * value*/
1421   bKey = (bKey | PHNCINFC_AUTHENTICATION_KEY);
1422 
1423   bKey |= tTranscvInfo.tSendData.pBuff[2];
1424 
1425   /*For authentication extension no need to copy tSendData buffer of
1426    * tTranscvInfo */
1427   tTranscvInfo.tSendData.wLen = 0x00;
1428 
1429   buff[0] = phNciNfc_e_MfcAuthReq;
1430   buff[1] = bBlockAddr;
1431   buff[2] = bKey;
1432 
1433   *buffSz = 0x03;
1434   if (bKey & PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY) {
1435     memcpy(&buff[3], &tTranscvInfo.tSendData.pBuff[3], PHLIBNFC_MFC_AUTHKEYLEN);
1436     *buffSz += PHLIBNFC_MFC_AUTHKEYLEN;
1437   }
1438   /* Store the auth command buffer to use further for presence check */
1439   if (gAuthCmdBuf.pauth_cmd != NULL) {
1440     memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1441     gAuthCmdBuf.pauth_cmd->length = *buffSz;
1442     memcpy(gAuthCmdBuf.pauth_cmd->buffer, buff, *buffSz);
1443   }
1444   status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
1445 
1446   return status;
1447 }
1448 
1449 /*******************************************************************************
1450 **
1451 ** Function         phNciNfc_SendMfReq
1452 **
1453 ** Description      This function shall be invoked as part of ReaderMgmt data
1454 **                  exchange sequence handler.
1455 **                  It shall send the request packet to NFCC.
1456 **
1457 ** Returns          NFCSTATUS_PENDING  - Send request is Pending
1458 **                  NFCSTATUS_FAILED   - otherwise
1459 **
1460 *******************************************************************************/
phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t * buff,uint16_t * buffSz)1461 static NFCSTATUS phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,
1462                                     uint8_t* buff, uint16_t* buffSz) {
1463   NFCSTATUS status = NFCSTATUS_SUCCESS;
1464 
1465   switch (tTranscvInfo.uCmd.T2TCmd) {
1466     case phNciNfc_eT2TRaw: {
1467       status = phNciNfc_MfCreateXchgDataHdr(tTranscvInfo, buff, buffSz);
1468     } break;
1469     case phNciNfc_eT2TAuth: {
1470       status = phNciNfc_MfCreateAuthCmdHdr(tTranscvInfo, (tTranscvInfo.bAddr),
1471                                            buff, buffSz);
1472     } break;
1473     default: {
1474       status = NFCSTATUS_FAILED;
1475       break;
1476     }
1477   }
1478 
1479   return status;
1480 }
1481 
1482 /*******************************************************************************
1483 **
1484 ** Function         phLibNfc_CalSectorAddress
1485 **
1486 ** Description      This function update the sector address for Mifare classic
1487 **
1488 ** Returns          none
1489 **
1490 *******************************************************************************/
phLibNfc_CalSectorAddress(uint8_t * Sector_Address)1491 static void phLibNfc_CalSectorAddress(uint8_t* Sector_Address) {
1492   uint8_t BlockNumber = 0x00;
1493 
1494   if (NULL != Sector_Address) {
1495     BlockNumber = *Sector_Address;
1496     if (BlockNumber >= PHLIBNFC_MIFARESTD4K_BLK128) {
1497       *Sector_Address = (uint8_t)(PHLIBNFC_MIFARESTD_SECTOR_NO32 +
1498                                   ((BlockNumber - PHLIBNFC_MIFARESTD4K_BLK128) /
1499                                    PHLIBNFC_MIFARESTD_BLOCK_BYTES));
1500     } else {
1501       *Sector_Address = BlockNumber / PHLIBNFC_NO_OF_BLKPERSECTOR;
1502     }
1503   } else {
1504   }
1505 
1506   return;
1507 }
1508 
1509 /*******************************************************************************
1510 **
1511 ** Function         phLibNfc_GetKeyNumberMFC
1512 **
1513 ** Description      This function find key number based on authentication
1514 *command
1515 **
1516 ** Returns          NFCSTATUS_SUCCESS  - If found the key number
1517 **                  NFCSTATUS_FAILED   - otherwise
1518 **
1519 *******************************************************************************/
phLibNfc_GetKeyNumberMFC(uint8_t * buffer,uint8_t * bKey)1520 static NFCSTATUS phLibNfc_GetKeyNumberMFC(uint8_t* buffer, uint8_t* bKey) {
1521   int32_t sdwStat = 0X00;
1522   NFCSTATUS wStatus = NFCSTATUS_INVALID_PARAMETER;
1523 
1524   /*Key Configuration
1525     uint8_t NdefKey[PHLIBNFC_MFC_AUTHKEYLEN] = {0xD3,0XF7,0xD3,0XF7,0xD3,0XF7};
1526     uint8_t RawKey[PHLIBNFC_MFC_AUTHKEYLEN] = {0xFF,0XFF,0xFF,0XFF,0xFF,0XFF};
1527     uint8_t MadKey[PHLIBNFC_MFC_AUTHKEYLEN] = {0xA0,0XA1,0xA2,0XA3,0xA4,0XA5};
1528     uint8_t Key[PHLIBNFC_MFC_AUTHKEYLEN] = {0x00,0x00,0x00,0x00,0x00,0x00}; */ /*Key used during ndef format*/
1529 
1530   uint8_t bIndex = 0x00;
1531   uint8_t bNoOfKeys = 0x00;
1532 
1533 #if PHLIBNFC_NXPETENSION_CONFIGURE_MFKEYS
1534   uint8_t aMfc_keys[NXP_NUMBER_OF_MFC_KEYS][NXP_MFC_KEY_SIZE] = NXP_MFC_KEYS;
1535 #else
1536   uint8_t aMfc_keys[1][1] = {{0x00}};
1537 #endif
1538 
1539   if (NULL != bKey && NULL != buffer) {
1540     bNoOfKeys = sizeof(aMfc_keys) / NXP_MFC_KEY_SIZE;
1541     /* Traverse through the keys stored to determine whether keys is preloaded
1542      * key */
1543     for (bIndex = 0; bIndex < bNoOfKeys; bIndex++) {
1544       /* Check passed key is NDEF key */
1545       sdwStat = memcmp(&buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD], aMfc_keys[bIndex],
1546                        PHLIBNFC_MFC_AUTHKEYLEN);
1547       if (!sdwStat) {
1548         LOG(ERROR) << StringPrintf(
1549             "Mifare : phLibNfc_GetKeyNumberMFC Key found");
1550         *bKey = bIndex;
1551         wStatus = NFCSTATUS_SUCCESS;
1552         break;
1553       }
1554     }
1555     LOG(ERROR) << StringPrintf(
1556         "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x Key = 0x%x",
1557         wStatus, *bKey);
1558   } else {
1559     wStatus = NFCSTATUS_FAILED;
1560     LOG(ERROR) << StringPrintf(
1561         "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x", wStatus);
1562   }
1563 
1564   return wStatus;
1565 }
1566 
1567 /*******************************************************************************
1568 **
1569 ** Function         phLibNfc_ChkAuthCmdMFC
1570 **
1571 ** Description      This function Check Authentication command send is proper or
1572 *not
1573 **
1574 ** Returns          NFCSTATUS_SUCCESS  - Authenticate command is proper
1575 **                  NFCSTATUS_FAILED   - otherwise
1576 **
1577 *******************************************************************************/
phLibNfc_ChkAuthCmdMFC(phNfc_sTransceiveInfo_t * pTransceiveInfo,uint8_t * bKey)1578 static NFCSTATUS phLibNfc_ChkAuthCmdMFC(
1579     phNfc_sTransceiveInfo_t* pTransceiveInfo, uint8_t* bKey) {
1580   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1581 
1582   if (NULL != pTransceiveInfo && NULL != pTransceiveInfo->sSendData.buffer &&
1583       0 != pTransceiveInfo->sSendData.length && NULL != bKey) {
1584     if ((pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentA ||
1585          pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentB)) {
1586       wStatus =
1587           phLibNfc_GetKeyNumberMFC(pTransceiveInfo->sSendData.buffer, bKey);
1588     } else {
1589       wStatus = NFCSTATUS_FAILED;
1590     }
1591   } else {
1592     wStatus = NFCSTATUS_FAILED;
1593   }
1594   return wStatus;
1595 }
1596 
1597 /*******************************************************************************
1598 **
1599 ** Function         phLibNfc_MifareMap
1600 **
1601 ** Description      Mifare Mapping Utility function
1602 **
1603 ** Returns          NFCSTATUS_SUCCESS             - Mapping is proper
1604 **                  NFCSTATUS_INVALID_PARAMETER   - otherwise
1605 **
1606 *******************************************************************************/
phLibNfc_MifareMap(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1607 static NFCSTATUS phLibNfc_MifareMap(
1608     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1609     pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1610   NFCSTATUS status = NFCSTATUS_SUCCESS;
1611   uint8_t bBuffIdx = 0;
1612   uint8_t bSectorNumber;
1613   uint8_t bKey = 0;
1614 
1615   switch (pTransceiveInfo->cmd.MfCmd) {
1616     case phNfc_eMifareRead16: {
1617       if ((NULL != pTransceiveInfo->sRecvData.buffer) &&
1618           (0 != (pTransceiveInfo->sRecvData.length))) {
1619         pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareRead16;
1620         pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1621         pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1622         pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1623       } else {
1624         status = NFCSTATUS_INVALID_PARAMETER;
1625       }
1626     } break;
1627 
1628     case phNfc_eMifareWrite16: {
1629       if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1630           (0 != (pTransceiveInfo->sSendData.length))) {
1631         pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1632         pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1633         memcpy(&(pMappedTranscvIf->tSendData.pBuff[bBuffIdx]),
1634                pTransceiveInfo->sSendData.buffer,
1635                (pTransceiveInfo->sSendData.length));
1636         pMappedTranscvIf->tSendData.wLen =
1637             bBuffIdx + pTransceiveInfo->sSendData.length;
1638         pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1639       } else {
1640         status = NFCSTATUS_INVALID_PARAMETER;
1641       }
1642     } break;
1643 
1644     case phNfc_eMifareAuthentA:
1645     case phNfc_eMifareAuthentB: {
1646       if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1647           (0 != (pTransceiveInfo->sSendData.length)) &&
1648           (NULL != pTransceiveInfo->sRecvData.buffer) &&
1649           (0 != (pTransceiveInfo->sRecvData.length))) {
1650         status = phLibNfc_ChkAuthCmdMFC(pTransceiveInfo, &bKey);
1651         if (NFCSTATUS_FAILED != status) {
1652           bSectorNumber = pTransceiveInfo->addr;
1653           phLibNfc_CalSectorAddress(&bSectorNumber);
1654           /*For creating extension command header pTransceiveInfo's MfCmd get
1655            * used*/
1656           pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] =
1657               pTransceiveInfo->cmd.MfCmd;
1658           pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bSectorNumber;
1659           pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TAuth;
1660           pMappedTranscvIf->bAddr = bSectorNumber;
1661           pMappedTranscvIf->bNumBlock = pTransceiveInfo->NumBlock;
1662           if (NFCSTATUS_SUCCESS == status) {
1663             pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1664             (pMappedTranscvIf->tSendData.wLen) = (uint16_t)(bBuffIdx);
1665 
1666           } else if (NFCSTATUS_INVALID_PARAMETER == status) {
1667             bKey = bKey | PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY;
1668             pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1669             memcpy(&pMappedTranscvIf->tSendData.pBuff[bBuffIdx],
1670                    &pTransceiveInfo->sSendData
1671                         .buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD],
1672                    PHLIBNFC_MFC_AUTHKEYLEN);
1673 
1674             (pMappedTranscvIf->tSendData.wLen) =
1675                 (uint16_t)(bBuffIdx + PHLIBNFC_MFC_AUTHKEYLEN);
1676             status = NFCSTATUS_SUCCESS;
1677           } else {
1678             /* do nothing */
1679           }
1680         }
1681       } else {
1682         status = NFCSTATUS_INVALID_PARAMETER;
1683       }
1684     } break;
1685 
1686     case phNfc_eMifareRaw: {
1687     } break;
1688 
1689     default: {
1690       status = NFCSTATUS_INVALID_PARAMETER;
1691       break;
1692     }
1693   }
1694 
1695   return status;
1696 }
1697 
1698 /*******************************************************************************
1699 **
1700 ** Function         phLibNfc_MapCmds
1701 **
1702 ** Description      This function maps the command request from libnfc level to
1703 *nci level
1704 **
1705 ** Returns          NFCSTATUS_SUCCESS           - Mapping of command is
1706 *successful
1707 **                  NFCSTATUS_INVALID_PARAMETER - One or more of the supplied
1708 **                  parameters could not be interpreted properly
1709 **
1710 *******************************************************************************/
phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1711 static NFCSTATUS phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,
1712                                   phNfc_sTransceiveInfo_t* pTransceiveInfo,
1713                                   pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1714   NFCSTATUS status = NFCSTATUS_SUCCESS;
1715 
1716   if ((NULL == pTransceiveInfo) || (NULL == pMappedTranscvIf)) {
1717     return NFCSTATUS_FAILED;
1718   }
1719   switch (RemDevType) {
1720     case phNciNfc_eMifare1k_PICC:
1721     case phNciNfc_eMifare4k_PICC: {
1722       status = phLibNfc_MifareMap(pTransceiveInfo, pMappedTranscvIf);
1723       break;
1724     }
1725     default: { break; }
1726   }
1727 
1728   return status;
1729 }
1730 
1731 /*******************************************************************************
1732 **
1733 ** Function         phLibNfc_SendAuthCmd
1734 **
1735 ** Description      This function Send authentication command to NFCC
1736 **
1737 ** Returns          NFCSTATUS_SUCCESS           - Parameters are proper
1738 **                  NFCSTATUS_INVALID_PARAMETER - Otherwise
1739 **
1740 *******************************************************************************/
phLibNfc_SendAuthCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,phNciNfc_TransceiveInfo_t * tNciTranscvInfo)1741 static NFCSTATUS phLibNfc_SendAuthCmd(
1742     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1743     phNciNfc_TransceiveInfo_t* tNciTranscvInfo) {
1744   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1745 
1746   wStatus = phLibNfc_MapCmds(phNciNfc_eMifare1k_PICC, pTransceiveInfo,
1747                              tNciTranscvInfo);
1748 
1749   return wStatus;
1750 }
1751 
1752 /*******************************************************************************
1753 **
1754 ** Function         phLibNfc_SendWrt16Cmd
1755 **
1756 ** Description      This function maps Mifarewirte16 commands
1757 **
1758 ** Returns          NFCSTATUS_SUCCESS           - Parameters are mapped
1759 **                  NFCSTATUS_INVALID_PARAMETER - Otherwise
1760 **
1761 *******************************************************************************/
phLibNfc_SendWrt16Cmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1762 static NFCSTATUS phLibNfc_SendWrt16Cmd(
1763     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1764     pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1765   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1766   uint8_t bBuffIdx = 0x00;
1767 
1768   if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1769       (0 != (pTransceiveInfo->sSendData.length))) {
1770     pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1771     pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1772     pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1773     pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1774   } else {
1775     wStatus = NFCSTATUS_INVALID_PARAMETER;
1776   }
1777 
1778   return wStatus;
1779 }
1780 
1781 /*******************************************************************************
1782 **
1783 ** Function         phLibNfc_SendIncDecCmd
1784 **
1785 ** Description      This function prepares the Increment/Decrement command
1786 **                  to be sent, increment/decrement value is sent separately
1787 **
1788 ** Returns          NFCSTATUS_SUCCESS           - Params are mapped
1789 **                  NFCSTATUS_INVALID_PARAMETER - Otherwise
1790 **
1791 *******************************************************************************/
phLibNfc_SendIncDecCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf,uint8_t IncDecCmd)1792 static NFCSTATUS phLibNfc_SendIncDecCmd(
1793     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1794     pphNciNfc_TransceiveInfo_t pMappedTranscvIf, uint8_t IncDecCmd) {
1795   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1796   uint8_t bBuffIdx = 0x00;
1797 
1798   if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1799       (0 != (pTransceiveInfo->sSendData.length))) {
1800     pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = IncDecCmd;
1801     pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1802     pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1803     pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1804   } else {
1805     wStatus = NFCSTATUS_INVALID_PARAMETER;
1806   }
1807 
1808   return wStatus;
1809 }
1810 
1811 /*******************************************************************************
1812 **
1813 ** Function         phLibNfc_SendRawCmd
1814 **
1815 ** Description      This function maps Mifare raw command
1816 **
1817 ** Returns          NFCSTATUS_SUCCESS           - Parameters are mapped
1818 **                  NFCSTATUS_INVALID_PARAMETER - Otherwise
1819 **
1820 *******************************************************************************/
phLibNfc_SendRawCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1821 static NFCSTATUS phLibNfc_SendRawCmd(
1822     phNfc_sTransceiveInfo_t* pTransceiveInfo,
1823     pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1824   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1825   //    uint8_t bBuffIdx = 0x00;                                  /*commented to
1826   //    eliminate unused variable warning*/
1827 
1828   if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1829       (0 != (pTransceiveInfo->sSendData.length))) {
1830     memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1831            (pTransceiveInfo->sSendData.length));
1832     pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1833     pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1834   } else {
1835     wStatus = NFCSTATUS_INVALID_PARAMETER;
1836   }
1837 
1838   return wStatus;
1839 }
1840 
1841 /*******************************************************************************
1842 **
1843 ** Function         phFriNfc_ExtnsTransceive
1844 **
1845 ** Description      This function maps Mifare raw command and send it to NFCC
1846 **
1847 ** Returns          NFCSTATUS_PENDING           - Operation successful
1848 **                  NFCSTATUS_INVALID_PARAMETER - Otherwise
1849 **
1850 *******************************************************************************/
phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t * pTransceiveInfo,phNfc_uCmdList_t Cmd,uint8_t * SendRecvBuf,uint16_t SendLength,uint16_t * SendRecvLength)1851 NFCSTATUS phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1852                                    phNfc_uCmdList_t Cmd, uint8_t* SendRecvBuf,
1853                                    uint16_t SendLength,
1854                                    uint16_t* SendRecvLength) {
1855   (void)SendRecvLength;
1856   NFCSTATUS status = NFCSTATUS_FAILED;
1857   uint8_t* buff = NULL;
1858   uint16_t buffSz = 0;
1859   uint8_t i = 0;
1860   uint32_t length = SendLength;
1861   uint8_t restore_payload[] = {
1862       0x00, 0x00, 0x00, 0x00,
1863   };
1864 
1865   if (SendLength == 0) {
1866     android_errorWriteLog(0x534e4554, "132083376");
1867     return status;
1868   }
1869 
1870   buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1871   if (NULL == buff) {
1872     return status;
1873   }
1874 
1875   pTransceiveInfo->cmd = Cmd;
1876 
1877   if ((Cmd.MfCmd == phNfc_eMifareAuthentA) ||
1878       (Cmd.MfCmd == phNfc_eMifareAuthentB)) {
1879     pTransceiveInfo->addr = SendRecvBuf[i++];
1880     pTransceiveInfo->sSendData.buffer[4] = SendRecvBuf[i++];
1881     pTransceiveInfo->sSendData.buffer[5] = SendRecvBuf[i++];
1882     pTransceiveInfo->sSendData.buffer[6] = SendRecvBuf[i++];
1883     pTransceiveInfo->sSendData.buffer[7] = SendRecvBuf[i++];
1884     pTransceiveInfo->sSendData.buffer[8] = SendRecvBuf[i++];
1885     pTransceiveInfo->sSendData.buffer[9] = SendRecvBuf[i++];
1886 
1887     pTransceiveInfo->cmd.MfCmd = Cmd.MfCmd;
1888 
1889     pTransceiveInfo->sSendData.length = length;
1890     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1891     status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1892   } else if (Cmd.MfCmd == phNfc_eMifareWrite16) {
1893     pTransceiveInfo->addr = SendRecvBuf[i++];
1894     length = SendLength - i;
1895     memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1896     pTransceiveInfo->sSendData.length = length;
1897     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1898 
1899     gphNxpExtns_Context.writecmdFlag = true;
1900 
1901     status = phLibNfc_SendWrt16Cmd(pTransceiveInfo, &tNciTranscvInfo);
1902   } else if ((Cmd.MfCmd == phNfc_eMifareInc) ||
1903              (Cmd.MfCmd == phNfc_eMifareDec)) {
1904     pTransceiveInfo->addr = SendRecvBuf[i++];
1905     length = SendLength - i;
1906     memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1907     pTransceiveInfo->sSendData.length = length;
1908     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1909 
1910     gphNxpExtns_Context.incrdecflag = true;
1911 
1912     status =
1913         phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd);
1914 
1915   } else if (Cmd.MfCmd == phNfc_eMifareRestore) {
1916     pTransceiveInfo->addr = SendRecvBuf[i++];
1917     length = SendLength - i;
1918     memcpy(pTransceiveInfo->sSendData.buffer, &restore_payload[0],
1919            sizeof(restore_payload));
1920     pTransceiveInfo->sSendData.length = length + sizeof(restore_payload);
1921     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1922 
1923     gphNxpExtns_Context.incrdecflag = true;
1924 
1925     status =
1926         phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd);
1927 
1928   } else if ((Cmd.MfCmd == phNfc_eMifareRaw) ||
1929              (Cmd.MfCmd == phNfc_eMifareTransfer)) {
1930     pTransceiveInfo->cmd.MfCmd = (phNfc_eMifareCmdList_t)phNciNfc_eT2TRaw;
1931     memcpy(pTransceiveInfo->sSendData.buffer, SendRecvBuf, length);
1932     pTransceiveInfo->sSendData.length = length;
1933     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1934     status = phLibNfc_SendRawCmd(pTransceiveInfo, &tNciTranscvInfo);
1935   } else {
1936     pTransceiveInfo->addr = SendRecvBuf[i++];
1937     length = SendLength - i;
1938     memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1939     pTransceiveInfo->sSendData.length = length;
1940     pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1941     status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1942   }
1943 
1944   if (NFCSTATUS_SUCCESS == status) {
1945     status = phNciNfc_SendMfReq(tNciTranscvInfo, buff, &buffSz);
1946     if (NFCSTATUS_PENDING != status) {
1947       LOG(ERROR) << StringPrintf("ERROR : phNciNfc_SendMfReq()");
1948     }
1949   } else {
1950     LOG(ERROR) << StringPrintf(" ERROR : Sending phNciNfc_SendMfReq");
1951   }
1952   if (buff != NULL) {
1953     free(buff);
1954     buff = NULL;
1955   }
1956 
1957   return status;
1958 }
1959