blob: ffbd24d9522cda0d67021fe006ad6235244941ee [file] [log] [blame]
nxpandroid64fd68c2015-09-23 16:45:15 +05301/*
nxpandroid34627bd2016-05-27 15:52:30 +05302 * Copyright (C) 2015 The Android Open Source Project
nxpandroid64fd68c2015-09-23 16:45:15 +05303 *
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 */
Suhas Sureshe29f24d2018-04-25 16:33:03 +053016#include <android-base/stringprintf.h>
Suhas Suresh2de4d8f2018-04-26 18:40:59 +053017#include <base/logging.h>
Ganesh Deva20b20a52019-09-04 16:48:11 +053018#include <log/log.h>
nxpandroid64fd68c2015-09-23 16:45:15 +053019
20#include <nfc_api.h>
Nikhil Chhabra9ac26292018-01-10 11:57:21 +053021#include <nfc_int.h>
nxpandroid64fd68c2015-09-23 16:45:15 +053022#include <phFriNfc_MifareStdTimer.h>
Suhas Suresh2de4d8f2018-04-26 18:40:59 +053023#include <phNfcCompId.h>
24#include <phNxpExtns_MifareStd.h>
Suhas Suresh2de4d8f2018-04-26 18:40:59 +053025#include <rw_api.h>
26#include <signal.h>
nxpandroid64fd68c2015-09-23 16:45:15 +053027
Suhas Sureshe29f24d2018-04-25 16:33:03 +053028using android::base::StringPrintf;
29
Suhas Sureshfa0df862018-05-07 11:53:43 +053030extern bool nfc_debug_enabled;
31
Suhas Suresh6a4cb322018-04-26 18:04:09 +053032phNxpExtns_Context_t gphNxpExtns_Context;
33phNciNfc_TransceiveInfo_t tNciTranscvInfo;
34phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt = NULL;
35phFriNfc_NdefMap_t* NdefMap = NULL;
36phLibNfc_NdefInfo_t NdefInfo;
37#if (NXP_EXTNS == TRUE)
nxpandroid64fd68c2015-09-23 16:45:15 +053038pthread_mutex_t SharedDataMutex = PTHREAD_MUTEX_INITIALIZER;
39#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +053040uint8_t current_key[6] = {0};
41phNci_mfc_auth_cmd_t gAuthCmdBuf;
42phFriNfc_MifareStdTimer_t mTimerInfo;
nxpandroid64fd68c2015-09-23 16:45:15 +053043
44STATIC NFCSTATUS phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,
Suhas Suresh6a4cb322018-04-26 18:04:09 +053045 uint8_t* buff, uint16_t* buffSz);
46STATIC NFCSTATUS
47phLibNfc_SendRawCmd(phNfc_sTransceiveInfo_t* pTransceiveInfo,
48 pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
49STATIC NFCSTATUS
50phLibNfc_SendWrt16Cmd(phNfc_sTransceiveInfo_t* pTransceiveInfo,
51 pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
Suhas Suresh2de4d8f2018-04-26 18:40:59 +053052STATIC NFCSTATUS phLibNfc_SendAuthCmd(
53 phNfc_sTransceiveInfo_t* pTransceiveInfo,
54 phNciNfc_TransceiveInfo_t* tNciTranscvInfo) __attribute__((unused));
Suhas Suresh6a4cb322018-04-26 18:04:09 +053055STATIC NFCSTATUS phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,
56 phNfc_sTransceiveInfo_t* pTransceiveInfo,
57 pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
58STATIC NFCSTATUS
59phLibNfc_MifareMap(phNfc_sTransceiveInfo_t* pTransceiveInfo,
60 pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
61STATIC NFCSTATUS
62phLibNfc_ChkAuthCmdMFC(phNfc_sTransceiveInfo_t* pTransceiveInfo, uint8_t* bKey);
63STATIC NFCSTATUS phLibNfc_GetKeyNumberMFC(uint8_t* buffer, uint8_t* bKey);
64STATIC void phLibNfc_CalSectorAddress(uint8_t* Sector_Address);
Suhas Suresh2de4d8f2018-04-26 18:40:59 +053065STATIC NFCSTATUS phNciNfc_MfCreateAuthCmdHdr(
66 phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t bBlockAddr, uint8_t* buff,
67 uint16_t* buffSz);
68STATIC NFCSTATUS phNciNfc_MfCreateXchgDataHdr(
69 phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t* buff, uint16_t* buffSz);
Suhas Suresh6a4cb322018-04-26 18:04:09 +053070STATIC NFCSTATUS
71phLibNfc_SendWrt16CmdPayload(phNfc_sTransceiveInfo_t* pTransceiveInfo,
72 pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
Suhas Suresh2de4d8f2018-04-26 18:40:59 +053073STATIC NFCSTATUS phNciNfc_RecvMfResp(phNciNfc_Buff_t* RspBuffInfo,
74 NFCSTATUS wStatus);
Suhas Suresh6a4cb322018-04-26 18:04:09 +053075STATIC NFCSTATUS nativeNfcExtns_doTransceive(uint8_t* buff, uint16_t buffSz);
76STATIC NFCSTATUS
77phFriNfc_NdefSmtCrd_Reset__(phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt,
78 uint8_t* SendRecvBuffer, uint16_t* SendRecvBuffLen);
Suhas Suresh2de4d8f2018-04-26 18:40:59 +053079STATIC NFCSTATUS phFriNfc_ValidateParams(uint8_t* PacketData,
80 uint32_t* PacketDataLength,
81 uint8_t Offset,
82 phFriNfc_NdefMap_t* pNdefMap,
83 uint8_t bNdefReq);
Suhas Suresh6a4cb322018-04-26 18:04:09 +053084STATIC void Mfc_FormatNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
85STATIC void Mfc_WriteNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
86STATIC void Mfc_ReadNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
87STATIC void Mfc_CheckNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
nxpandroid64fd68c2015-09-23 16:45:15 +053088STATIC void Mfc_CheckNdef_timeoutcb_Routine(union sigval);
89
90/*******************************************************************************
91**
92** Function phNxpExtns_MfcModuleDeInit
93**
94** Description It Deinitializes the Mifare module.
95**
96** Frees all the memory occupied by Mifare module
97**
98** Returns:
99** NFCSTATUS_SUCCESS - if successfully deinitialize
100** NFCSTATUS_FAILED - otherwise
101**
102*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530103NFCSTATUS phNxpExtns_MfcModuleDeInit(void) {
104 NFCSTATUS status = NFCSTATUS_FAILED;
nxpandroid64fd68c2015-09-23 16:45:15 +0530105
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530106 if (NdefMap != NULL) {
107 if (NdefMap->psRemoteDevInfo != NULL) {
108 free(NdefMap->psRemoteDevInfo);
109 NdefMap->psRemoteDevInfo = NULL;
110 }
111 if (NdefMap->SendRecvBuf != NULL) {
112 free(NdefMap->SendRecvBuf);
113 NdefMap->SendRecvBuf = NULL;
114 }
115 if (NdefMap->SendRecvLength != NULL) {
116 free(NdefMap->SendRecvLength);
117 NdefMap->SendRecvLength = NULL;
118 }
119 if (NdefMap->DataCount != NULL) {
120 free(NdefMap->DataCount);
121 NdefMap->DataCount = NULL;
122 }
123 if (NdefMap->pTransceiveInfo != NULL) {
124 if (NdefMap->pTransceiveInfo->sSendData.buffer != NULL) {
125 free(NdefMap->pTransceiveInfo->sSendData.buffer);
126 NdefMap->pTransceiveInfo->sSendData.buffer = NULL;
127 }
128 if (NdefMap->pTransceiveInfo->sRecvData.buffer != NULL) {
129 free(NdefMap->pTransceiveInfo->sRecvData.buffer);
130 NdefMap->pTransceiveInfo->sRecvData.buffer = NULL;
131 }
132 free(NdefMap->pTransceiveInfo);
133 NdefMap->pTransceiveInfo = NULL;
nxpandroid64fd68c2015-09-23 16:45:15 +0530134 }
135
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530136 free(NdefMap);
137 NdefMap = NULL;
138 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530139
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530140 if (tNciTranscvInfo.tSendData.pBuff != NULL) {
141 free(tNciTranscvInfo.tSendData.pBuff);
142 tNciTranscvInfo.tSendData.pBuff = NULL;
143 }
144
145 if (NdefSmtCrdFmt != NULL) {
146 free(NdefSmtCrdFmt);
147 NdefSmtCrdFmt = NULL;
148 }
149#if (NXP_EXTNS == TRUE)
150 pthread_mutex_lock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530151#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530152 if (NULL != NdefInfo.psUpperNdefMsg) {
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530153 free(NdefInfo.psUpperNdefMsg);
154 NdefInfo.psUpperNdefMsg = NULL;
155 }
156#if (NXP_EXTNS == TRUE)
157 pthread_mutex_unlock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530158#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530159 if (NULL != gAuthCmdBuf.pauth_cmd) {
160 if (NULL != gAuthCmdBuf.pauth_cmd->buffer) {
161 free(gAuthCmdBuf.pauth_cmd->buffer);
162 gAuthCmdBuf.pauth_cmd->buffer = NULL;
nxpandroid64fd68c2015-09-23 16:45:15 +0530163 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530164 free(gAuthCmdBuf.pauth_cmd);
165 gAuthCmdBuf.pauth_cmd = NULL;
166 }
167 status = NFCSTATUS_SUCCESS;
168 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +0530169}
170
171/*******************************************************************************
172**
173** Function phNxpExtns_MfcModuleInit
174**
175** Description It Initializes the memroy and global variables related
176** to Mifare module.
177**
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530178** Reset all the global variables and allocate memory for
179*Mifare module
nxpandroid64fd68c2015-09-23 16:45:15 +0530180**
181** Returns:
182** NFCSTATUS_SUCCESS - if successfully deinitialize
183** NFCSTATUS_FAILED - otherwise
184**
185*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530186NFCSTATUS phNxpExtns_MfcModuleInit(void) {
187 NFCSTATUS status = NFCSTATUS_FAILED;
188 gphNxpExtns_Context.writecmdFlag = false;
189 gphNxpExtns_Context.RawWriteCallBack = false;
190 gphNxpExtns_Context.CallBackCtxt = NULL;
191 gphNxpExtns_Context.CallBackMifare = NULL;
192 gphNxpExtns_Context.ExtnsConnect = false;
193 gphNxpExtns_Context.ExtnsDeactivate = false;
194 gphNxpExtns_Context.ExtnsCallBack = false;
nxpandroid64fd68c2015-09-23 16:45:15 +0530195
Suhas Suresh9139dc22018-05-09 15:48:37 +0530196 NdefMap = (phFriNfc_NdefMap_t *)malloc(sizeof(phFriNfc_NdefMap_t));
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530197 if (NULL == NdefMap) {
198 goto clean_and_return;
199 }
200 memset(NdefMap, 0, sizeof(phFriNfc_NdefMap_t));
nxpandroid64fd68c2015-09-23 16:45:15 +0530201
Suhas Suresh9139dc22018-05-09 15:48:37 +0530202 NdefMap->psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t *)malloc(sizeof(phLibNfc_sRemoteDevInformation_t));
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530203 if (NULL == NdefMap->psRemoteDevInfo) {
204 goto clean_and_return;
205 }
206 memset(NdefMap->psRemoteDevInfo, 0, sizeof(phLibNfc_sRemoteDevInformation_t));
nxpandroid64fd68c2015-09-23 16:45:15 +0530207
Suhas Suresh9139dc22018-05-09 15:48:37 +0530208 NdefMap->SendRecvBuf = (uint8_t *)malloc((uint32_t)(MAX_BUFF_SIZE * 2));
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530209 if (NULL == NdefMap->SendRecvBuf) {
210 goto clean_and_return;
211 }
212 memset(NdefMap->SendRecvBuf, 0, (MAX_BUFF_SIZE * 2));
nxpandroid64fd68c2015-09-23 16:45:15 +0530213
Suhas Suresh9139dc22018-05-09 15:48:37 +0530214 NdefMap->SendRecvLength = (uint16_t *)malloc(sizeof(uint16_t));
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530215 if (NULL == NdefMap->SendRecvLength) {
216 goto clean_and_return;
217 }
218 memset(NdefMap->SendRecvLength, 0, sizeof(uint16_t));
nxpandroid64fd68c2015-09-23 16:45:15 +0530219
Suhas Suresh9139dc22018-05-09 15:48:37 +0530220 NdefMap->DataCount = (uint16_t *)malloc(sizeof(uint16_t));
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530221 if (NULL == NdefMap->DataCount) {
222 goto clean_and_return;
223 }
224 memset(NdefMap->DataCount, 0, sizeof(uint16_t));
nxpandroid64fd68c2015-09-23 16:45:15 +0530225
Suhas Suresh9139dc22018-05-09 15:48:37 +0530226 NdefMap->pTransceiveInfo = (phNfc_sTransceiveInfo_t *)malloc(sizeof(phNfc_sTransceiveInfo_t));
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530227 if (NULL == NdefMap->pTransceiveInfo) {
228 goto clean_and_return;
229 }
230 memset(NdefMap->pTransceiveInfo, 0, sizeof(phNfc_sTransceiveInfo_t));
nxpandroid64fd68c2015-09-23 16:45:15 +0530231
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530232 tNciTranscvInfo.tSendData.pBuff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
233 if (NULL == tNciTranscvInfo.tSendData.pBuff) {
234 goto clean_and_return;
235 }
236 memset(tNciTranscvInfo.tSendData.pBuff, 0, MAX_BUFF_SIZE);
nxpandroid64fd68c2015-09-23 16:45:15 +0530237
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530238 NdefMap->pTransceiveInfo->sSendData.buffer =
239 (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
240 if (NdefMap->pTransceiveInfo->sSendData.buffer == NULL) {
241 goto clean_and_return;
242 }
243 memset(NdefMap->pTransceiveInfo->sSendData.buffer, 0, MAX_BUFF_SIZE);
244 NdefMap->pTransceiveInfo->sSendData.length = MAX_BUFF_SIZE;
nxpandroid64fd68c2015-09-23 16:45:15 +0530245
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530246 NdefMap->pTransceiveInfo->sRecvData.buffer = (uint8_t*)malloc(
247 (uint32_t)MAX_BUFF_SIZE); /* size should be same as sRecvData */
248 if (NdefMap->pTransceiveInfo->sRecvData.buffer == NULL) {
249 goto clean_and_return;
250 }
251 memset(NdefMap->pTransceiveInfo->sRecvData.buffer, 0, MAX_BUFF_SIZE);
252 NdefMap->pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
nxpandroid64fd68c2015-09-23 16:45:15 +0530253
Suhas Suresh9139dc22018-05-09 15:48:37 +0530254 NdefSmtCrdFmt = (phFriNfc_sNdefSmtCrdFmt_t *)malloc(sizeof(phFriNfc_sNdefSmtCrdFmt_t));
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530255 if (NdefSmtCrdFmt == NULL) {
256 goto clean_and_return;
257 }
258 memset(NdefSmtCrdFmt, 0, sizeof(phFriNfc_sNdefSmtCrdFmt_t));
259#if (NXP_EXTNS == TRUE)
260 pthread_mutex_lock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530261#endif
Suhas Suresh9139dc22018-05-09 15:48:37 +0530262 NdefInfo.psUpperNdefMsg = (phNfc_sData_t *) malloc(sizeof(phNfc_sData_t));
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530263 if (NULL == NdefInfo.psUpperNdefMsg) {
264 goto clean_and_return;
265 }
266 memset(NdefInfo.psUpperNdefMsg, 0, sizeof(phNfc_sData_t));
267 memset(&gAuthCmdBuf, 0, sizeof(phNci_mfc_auth_cmd_t));
Suhas Suresh9139dc22018-05-09 15:48:37 +0530268 gAuthCmdBuf.pauth_cmd = (phNfc_sData_t *)malloc(sizeof(phNfc_sData_t));
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530269 if (NULL == gAuthCmdBuf.pauth_cmd) {
270 goto clean_and_return;
271 }
Suhas Suresh9139dc22018-05-09 15:48:37 +0530272 gAuthCmdBuf.pauth_cmd->buffer = (uint8_t *)malloc((uint32_t)NCI_MAX_DATA_LEN);
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530273 if (NULL == gAuthCmdBuf.pauth_cmd->buffer) {
274 goto clean_and_return;
275 }
276 status = NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +0530277
278clean_and_return:
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530279#if (NXP_EXTNS == TRUE)
280 pthread_mutex_unlock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530281#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530282 if (status != NFCSTATUS_SUCCESS) {
283 LOG(ERROR) << StringPrintf("CRIT: Memory Allocation failed for MFC!");
284 phNxpExtns_MfcModuleDeInit();
285 }
286 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +0530287}
288
289/*******************************************************************************
290**
291** Function Mfc_CheckNdef
292**
293** Description It triggers NDEF detection for Mifare Classic Tag.
294**
295**
296** Returns NFCSTATUS_SUCCESS - if successfully initiated
297** NFCSTATUS_FAILED - otherwise
298**
299*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530300NFCSTATUS Mfc_CheckNdef(void) {
301 NFCSTATUS status = NFCSTATUS_FAILED;
nxpandroid64fd68c2015-09-23 16:45:15 +0530302
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530303 EXTNS_SetCallBackFlag(false);
304 /* Set Completion Routine for CheckNdef */
305 NdefMap->CompletionRoutine[0].CompletionRoutine =
306 Mfc_CheckNdef_Completion_Routine;
nxpandroid64fd68c2015-09-23 16:45:15 +0530307
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530308 gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
309 gphNxpExtns_Context.CallBackCtxt = NdefMap;
310 status = phFriNfc_MifareStdMap_H_Reset(NdefMap);
311 if (NFCSTATUS_SUCCESS == status) {
312 status = phFriNfc_MifareStdMap_ChkNdef(NdefMap);
313 if (status == NFCSTATUS_PENDING) {
314 status = NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +0530315 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530316 }
317 /*Start a timer for MIFARE Check Ndef response callback handler*/
318 if (NFCSTATUS_SUCCESS == status) {
319 memset(&mTimerInfo, 0, sizeof(mTimerInfo));
320 mTimerInfo.mCb = Mfc_CheckNdef_timeoutcb_Routine;
321 mTimerInfo.mtimeout = (uint32_t)PH_FRINFC_CHECK_NDEF_TIMEOUT;
322 status = phFriNfc_MifareStd_StartTimer(&mTimerInfo);
323 }
324 if (status != NFCSTATUS_SUCCESS) {
325 status = NFCSTATUS_FAILED;
326 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530327
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530328 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +0530329}
330
331/*******************************************************************************
332**
333** Function Mfc_CheckNdef_Completion_Routine
334**
335** Description Notify NDEF detection for Mifare Classic Tag to JNI
336**
337** Upon completion of NDEF detection, a
338** NFA_NDEF_DETECT_EVT will be sent, to notify the application
339** of the NDEF attributes (NDEF total memory size, current
340** size, etc.).
341**
342** Returns: void
343**
344*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530345STATIC void Mfc_CheckNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
346 (void)NdefCtxt;
347 tNFA_CONN_EVT_DATA conn_evt_data;
348 NFCSTATUS timer_status = NFCSTATUS_FAILED;
349 conn_evt_data.ndef_detect.status = status;
350 // stopping checkndef timer if running
351 timer_status = phFriNfc_MifareStd_StopTimer(&mTimerInfo);
352 if (timer_status != NFCSTATUS_SUCCESS) {
353 LOG(ERROR) << StringPrintf("Failed to stop timer");
354 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530355
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530356 if (NFCSTATUS_SUCCESS == status) {
357 /* NDef Tag Detected */
358 conn_evt_data.ndef_detect.protocol = NFC_PROTOCOL_MIFARE;
359 phFrinfc_MifareClassic_GetContainerSize(
360 NdefMap, (uint32_t*)&(conn_evt_data.ndef_detect.max_size),
361 (uint32_t*)&(conn_evt_data.ndef_detect.cur_size));
362 NdefInfo.NdefLength = conn_evt_data.ndef_detect.max_size;
363 /* update local flags */
364 NdefInfo.is_ndef = 1;
365 NdefInfo.NdefActualSize = conn_evt_data.ndef_detect.cur_size;
366 if (PH_NDEFMAP_CARD_STATE_READ_ONLY == NdefMap->CardState) {
Ganesh Deva15e35d42020-02-18 10:40:37 +0530367 DLOG_IF(INFO, nfc_debug_enabled)
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530368 << StringPrintf("Mfc_CheckNdef_Completion_Routine : READ_ONLY_CARD");
369 conn_evt_data.ndef_detect.flags = RW_NDEF_FL_READ_ONLY;
370 } else {
371 conn_evt_data.ndef_detect.flags =
372 RW_NDEF_FL_SUPPORTED | RW_NDEF_FL_FORMATED;
nxpandroid64fd68c2015-09-23 16:45:15 +0530373 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530374 } else {
375 /* NDEF Detection failed for other reasons */
376 conn_evt_data.ndef_detect.cur_size = 0;
377 conn_evt_data.ndef_detect.max_size = 0;
378 conn_evt_data.ndef_detect.flags = RW_NDEF_FL_UNKNOWN;
nxpandroid64fd68c2015-09-23 16:45:15 +0530379
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530380 /* update local flags */
381 NdefInfo.is_ndef = 0;
382 NdefInfo.NdefActualSize = conn_evt_data.ndef_detect.cur_size;
383 }
384 (*gphNxpExtns_Context.p_conn_cback)(NFA_NDEF_DETECT_EVT, &conn_evt_data);
nxpandroid64fd68c2015-09-23 16:45:15 +0530385
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530386 return;
nxpandroid64fd68c2015-09-23 16:45:15 +0530387}
388/*******************************************************************************
389**
390** Function Mfc_ReadNdef_Completion_Routine
391**
392** Description Notify NDEF read completion for Mifare Classic Tag to JNI
393**
394** Upon completion of NDEF read, a
395** NFA_READ_CPLT_EVT will be sent, to notify the application
396** with the NDEF data and status
397**
398** Returns: void
399**
400*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530401STATIC void Mfc_ReadNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
402 (void)NdefCtxt;
403 tNFA_CONN_EVT_DATA conn_evt_data;
404 tNFA_NDEF_EVT_DATA p_data;
nxpandroid64fd68c2015-09-23 16:45:15 +0530405
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530406 conn_evt_data.status = status;
407#if (NXP_EXTNS == TRUE)
408 pthread_mutex_lock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530409#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530410 if (NFCSTATUS_SUCCESS == status) {
411 p_data.ndef_data.len = NdefInfo.psUpperNdefMsg->length;
412 p_data.ndef_data.p_data = NdefInfo.psUpperNdefMsg->buffer;
413 (*gphNxpExtns_Context.p_ndef_cback)(NFA_NDEF_DATA_EVT, &p_data);
414 } else {
415 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530416
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530417 (*gphNxpExtns_Context.p_conn_cback)(NFA_READ_CPLT_EVT, &conn_evt_data);
nxpandroid64fd68c2015-09-23 16:45:15 +0530418
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530419 if (NdefInfo.psUpperNdefMsg->buffer != NULL) {
420 free(NdefInfo.psUpperNdefMsg->buffer);
421 NdefInfo.psUpperNdefMsg->buffer = NULL;
422 }
423#if (NXP_EXTNS == TRUE)
424 pthread_mutex_unlock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530425#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530426 return;
nxpandroid64fd68c2015-09-23 16:45:15 +0530427}
428
429/*******************************************************************************
430**
431** Function Mfc_WriteNdef_Completion_Routine
432**
433** Description Notify NDEF write completion for Mifare Classic Tag to JNI
434**
435** Upon completion of NDEF write, a
436** NFA_WRITE_CPLT_EVT will be sent along with status
437**
438** Returns: void
439**
440*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530441STATIC void Mfc_WriteNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
442 (void)NdefCtxt;
443 tNFA_CONN_EVT_DATA conn_evt_data;
nxpandroid64fd68c2015-09-23 16:45:15 +0530444
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530445 conn_evt_data.status = status;
446 (*gphNxpExtns_Context.p_conn_cback)(NFA_WRITE_CPLT_EVT, &conn_evt_data);
nxpandroid64fd68c2015-09-23 16:45:15 +0530447
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530448 return;
nxpandroid64fd68c2015-09-23 16:45:15 +0530449}
450
451/*******************************************************************************
452**
453** Function Mfc_FormatNdef_Completion_Routine
454**
455** Description Notify NDEF format completion for Mifare Classic Tag to JNI
456**
457** Upon completion of NDEF format, a
458** NFA_FORMAT_CPLT_EVT will be sent along with status
459**
460** Returns: void
461**
462*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530463STATIC void Mfc_FormatNdef_Completion_Routine(void* NdefCtxt,
464 NFCSTATUS status) {
465 (void)NdefCtxt;
466 tNFA_CONN_EVT_DATA conn_evt_data;
nxpandroid64fd68c2015-09-23 16:45:15 +0530467
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530468 conn_evt_data.status = status;
469 (*gphNxpExtns_Context.p_conn_cback)(NFA_FORMAT_CPLT_EVT, &conn_evt_data);
nxpandroid64fd68c2015-09-23 16:45:15 +0530470
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530471 return;
nxpandroid64fd68c2015-09-23 16:45:15 +0530472}
473
474/*******************************************************************************
475**
476** Function phFriNfc_ValidateParams
477**
478** Description This function is a common function which validates NdefRd
479** and NdefWr parameters.
480**
481** Returns NFCSTATUS_SUCCESS - All the params are valid
482** NFCSTATUS_FAILED - otherwise
483**
484*******************************************************************************/
Suhas Suresh2de4d8f2018-04-26 18:40:59 +0530485STATIC NFCSTATUS phFriNfc_ValidateParams(uint8_t* PacketData,
486 uint32_t* PacketDataLength,
487 uint8_t Offset,
488 phFriNfc_NdefMap_t* pNdefMap,
489 uint8_t bNdefReq) {
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530490 if ((pNdefMap == NULL) || (PacketData == NULL) ||
491 (PacketDataLength == NULL)) {
492 return NFCSTATUS_FAILED;
493 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530494
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530495 if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) {
496 return NFCSTATUS_FAILED;
497 }
498
499 if (bNdefReq == PH_FRINFC_NDEF_READ_REQ) {
500 if ((Offset != PH_FRINFC_NDEFMAP_SEEK_CUR) &&
501 (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN)) {
502 return NFCSTATUS_FAILED;
nxpandroid64fd68c2015-09-23 16:45:15 +0530503 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530504 if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED) {
505 pNdefMap->NumOfBytesRead = PacketDataLength;
506 *pNdefMap->NumOfBytesRead = 0;
507 return NFCSTATUS_EOF_NDEF_CONTAINER_REACHED;
nxpandroid64fd68c2015-09-23 16:45:15 +0530508 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530509 if ((pNdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE) &&
510 (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN)) {
511 return NFCSTATUS_FAILED; /* return INVALID_DEVICE_REQUEST */
nxpandroid64fd68c2015-09-23 16:45:15 +0530512 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530513 if (Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) {
514 pNdefMap->ApduBuffIndex = 0;
515 *pNdefMap->DataCount = 0;
516 } else if ((pNdefMap->bPrevReadMode == PH_FRINFC_NDEFMAP_SEEK_BEGIN) ||
517 (pNdefMap->bPrevReadMode == PH_FRINFC_NDEFMAP_SEEK_CUR)) {
518 } else {
519 return NFCSTATUS_FAILED;
nxpandroid64fd68c2015-09-23 16:45:15 +0530520 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530521 } else if (bNdefReq == PH_FRINFC_NDEF_WRITE_REQ) {
522 if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY) {
523 pNdefMap->WrNdefPacketLength = PacketDataLength;
524 *pNdefMap->WrNdefPacketLength = 0x00;
525 return NFCSTATUS_NOT_ALLOWED;
526 }
527 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530528
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530529 return NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +0530530}
531
532/*******************************************************************************
533**
534** Function Mfc_SetRdOnly_Completion_Routine
535**
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530536** Description Notify NDEF read only completion for Mifare Classic Tag to
537*JNI
nxpandroid64fd68c2015-09-23 16:45:15 +0530538**
539** Upon completion of NDEF format, a
540** NFA_SET_TAG_RO_EVT will be sent along with status
541**
542** Returns: void
543**
544*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530545STATIC void Mfc_SetRdOnly_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
546 (void)NdefCtxt;
547 tNFA_CONN_EVT_DATA conn_evt_data;
548 LOG(ERROR) << StringPrintf("%s status = 0x%x", __func__, status);
549 conn_evt_data.status = status;
550 (*gphNxpExtns_Context.p_conn_cback)(NFA_SET_TAG_RO_EVT, &conn_evt_data);
nxpandroid64fd68c2015-09-23 16:45:15 +0530551
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530552 return;
nxpandroid64fd68c2015-09-23 16:45:15 +0530553}
554
555/*******************************************************************************
556**
557** Function Mfc_SetReadOnly
558**
559**
560** Description: It triggers ConvertToReadOnly for Mifare Classic Tag.
561**
562** Returns:
563** NFCSTATUS_SUCCESS if successfully initiated
564** NFCSTATUS_FAILED otherwise
565**
566*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530567NFCSTATUS Mfc_SetReadOnly(uint8_t* secrtkey, uint8_t len) {
Ganesh Deva15e35d42020-02-18 10:40:37 +0530568 DLOG_IF(INFO, nfc_debug_enabled)
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530569 << StringPrintf("%s Entering ", __func__);
570 NFCSTATUS status = NFCSTATUS_FAILED;
571 uint8_t mif_secrete_key[6] = {0};
572 uint8_t id = 0;
573 EXTNS_SetCallBackFlag(false);
574 memcpy(mif_secrete_key, secrtkey, len);
575 gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
576 gphNxpExtns_Context.CallBackCtxt = NdefMap;
577 for (id = 0; id < len; id++) {
578 DLOG_IF(INFO, nfc_debug_enabled)
579 << StringPrintf("secrtkey[%d] = 0x%x", id, secrtkey[id]);
580 DLOG_IF(INFO, nfc_debug_enabled)
581 << StringPrintf("mif_secrete_key[%d] = 0x%x", id, mif_secrete_key[id]);
582 }
583 /* Set Completion Routine for ReadNdef */
584 NdefMap->CompletionRoutine[0].CompletionRoutine =
585 Mfc_SetRdOnly_Completion_Routine;
586 if (NdefInfo.is_ndef == 0) {
587 status = NFCSTATUS_NON_NDEF_COMPLIANT;
588 goto Mfc_SetRdOnly;
589 } else if ((NdefInfo.is_ndef == 1) && (NdefInfo.NdefActualSize == 0)) {
590#if (NXP_EXTNS == TRUE)
591 pthread_mutex_lock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530592#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530593 NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize;
594#if (NXP_EXTNS == TRUE)
595 pthread_mutex_unlock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530596#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530597 status = NFCSTATUS_SUCCESS;
598 goto Mfc_SetRdOnly;
599 } else {
600 status = phFriNfc_MifareStdMap_ConvertToReadOnly(NdefMap, mif_secrete_key);
601 }
602 if (NFCSTATUS_PENDING == status) {
603 status = NFCSTATUS_SUCCESS;
604 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530605
606Mfc_SetRdOnly:
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530607 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +0530608}
609
610/*******************************************************************************
611**
612** Function Mfc_ReadNdef
613**
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530614** Description It triggers receiving of the NDEF message from Mifare
615*Classic Tag.
nxpandroid64fd68c2015-09-23 16:45:15 +0530616**
617**
618** Returns:
619** NFCSTATUS_SUCCESS - if successfully initiated
620** NFCSTATUS_FAILED - otherwise
621**
622*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530623NFCSTATUS Mfc_ReadNdef(void) {
624 NFCSTATUS status = NFCSTATUS_FAILED;
625 uint8_t* PacketData = NULL;
626 uint32_t* PacketDataLength = NULL;
627 phLibNfc_Ndef_EOffset_t Offset;
nxpandroid64fd68c2015-09-23 16:45:15 +0530628
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530629 EXTNS_SetCallBackFlag(false);
nxpandroid64fd68c2015-09-23 16:45:15 +0530630
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530631 Offset = phLibNfc_Ndef_EBegin;
nxpandroid64fd68c2015-09-23 16:45:15 +0530632
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530633 gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
634 gphNxpExtns_Context.CallBackCtxt = NdefMap;
635#if (NXP_EXTNS == TRUE)
636 pthread_mutex_lock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530637#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530638 if (NdefInfo.is_ndef == 0) {
639 status = NFCSTATUS_NON_NDEF_COMPLIANT;
640 goto Mfc_RdNdefEnd;
641 } else if ((NdefInfo.is_ndef == 1) && (NdefInfo.NdefActualSize == 0)) {
642 NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize;
643 status = NFCSTATUS_SUCCESS;
644 goto Mfc_RdNdefEnd;
645 } else {
Suhas Suresh9139dc22018-05-09 15:48:37 +0530646 NdefInfo.psUpperNdefMsg->buffer = (uint8_t *)malloc(NdefInfo.NdefActualSize);
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530647 if (NULL == NdefInfo.psUpperNdefMsg->buffer) {
648 goto Mfc_RdNdefEnd;
nxpandroid64fd68c2015-09-23 16:45:15 +0530649 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530650 NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize;
nxpandroid64fd68c2015-09-23 16:45:15 +0530651
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530652 /* Set Completion Routine for ReadNdef */
653 NdefMap->CompletionRoutine[1].CompletionRoutine =
654 Mfc_ReadNdef_Completion_Routine;
nxf50051cd8d5822019-03-20 15:32:57 +0530655 NdefInfo.NdefContinueRead = (uint8_t)PH_FRINFC_NDEFMAP_SEEK_BEGIN;
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530656 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530657
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530658 PacketData = NdefInfo.psUpperNdefMsg->buffer;
659 PacketDataLength = (uint32_t*)&(NdefInfo.psUpperNdefMsg->length);
660 NdefMap->bCurrReadMode = Offset;
661 status = phFriNfc_ValidateParams(PacketData, PacketDataLength, Offset,
662 NdefMap, PH_FRINFC_NDEF_READ_REQ);
663 if (status != NFCSTATUS_SUCCESS) {
664 goto Mfc_RdNdefEnd;
665 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530666
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530667 status = phFriNfc_MifareStdMap_RdNdef(NdefMap, PacketData, PacketDataLength,
668 Offset);
nxpandroid64fd68c2015-09-23 16:45:15 +0530669
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530670 if (NFCSTATUS_INSUFFICIENT_STORAGE == status) {
671 NdefInfo.psUpperNdefMsg->length = 0x00;
672 status = NFCSTATUS_SUCCESS;
673 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530674
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530675 if (NFCSTATUS_PENDING == status) {
676 status = NFCSTATUS_SUCCESS;
677 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530678
679Mfc_RdNdefEnd:
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530680 if (status != NFCSTATUS_SUCCESS) {
681 if (NULL != NdefInfo.psUpperNdefMsg->buffer) {
682 free(NdefInfo.psUpperNdefMsg->buffer);
683 NdefInfo.psUpperNdefMsg->buffer = NULL;
nxpandroid64fd68c2015-09-23 16:45:15 +0530684 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530685 status = NFCSTATUS_FAILED;
686 }
687#if (NXP_EXTNS == TRUE)
688 pthread_mutex_unlock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530689#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530690 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +0530691}
692/*******************************************************************************
693**
694** Function Mfc_PresenceCheck
695**
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530696** Description It triggers receiving of the NDEF message from Mifare
697*Classic Tag.
nxpandroid64fd68c2015-09-23 16:45:15 +0530698**
699**
700** Returns:
701** NFCSTATUS_SUCCESS - if successfully initiated
702** NFCSTATUS_FAILED - otherwise
703**
704*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530705NFCSTATUS Mfc_PresenceCheck(void) {
706 NFCSTATUS status = NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +0530707
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530708 if (gAuthCmdBuf.auth_status == true) {
709 EXTNS_SetCallBackFlag(false);
710 status = nativeNfcExtns_doTransceive(gAuthCmdBuf.pauth_cmd->buffer,
711 gAuthCmdBuf.pauth_cmd->length);
712 if (status != NFCSTATUS_PENDING) {
713 gAuthCmdBuf.auth_sent = false;
714 status = NFCSTATUS_FAILED;
715 } else {
716 gAuthCmdBuf.auth_sent = true;
717 status = NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +0530718 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530719 } else {
720 status = NFCSTATUS_NOT_ALLOWED;
721 }
Ganesh Deva15e35d42020-02-18 10:40:37 +0530722 DLOG_IF(INFO, nfc_debug_enabled)
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530723 << StringPrintf("%s status = 0x%x", __func__, status);
724 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +0530725}
726/*******************************************************************************
727**
728** Function Mfc_WriteNdef
729**
730** Description It triggers the NDEF data write to Mifare Classic Tag.
731**
732**
733** Returns:
734** NFCSTATUS_SUCCESS - if successfully initiated
735** NFCSTATUS_FAILED - otherwise
736**
737*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530738NFCSTATUS Mfc_WriteNdef(uint8_t* p_data, uint32_t len) {
739 NFCSTATUS status = NFCSTATUS_SUCCESS;
740 uint8_t* PacketData = NULL;
741 uint32_t* PacketDataLength = NULL;
nxpandroid64fd68c2015-09-23 16:45:15 +0530742
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530743 if (p_data == NULL || len == 0) {
744 LOG(ERROR) << StringPrintf("MFC Error: Invalid Parameters to Ndef Write");
745 status = NFCSTATUS_FAILED;
746 goto Mfc_WrNdefEnd;
747 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530748
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530749 EXTNS_SetCallBackFlag(false);
750 gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
751 gphNxpExtns_Context.CallBackCtxt = NdefMap;
752#if (NXP_EXTNS == TRUE)
753 pthread_mutex_lock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530754#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530755 if (NdefInfo.is_ndef == PH_LIBNFC_INTERNAL_CHK_NDEF_NOT_DONE) {
756 status = NFCSTATUS_REJECTED;
757 goto Mfc_WrNdefEnd;
758 } else if (NdefInfo.is_ndef == 0) {
759 status = NFCSTATUS_NON_NDEF_COMPLIANT;
760 goto Mfc_WrNdefEnd;
761 } else if (len > NdefInfo.NdefLength) {
762 status = NFCSTATUS_NOT_ENOUGH_MEMORY;
763 goto Mfc_WrNdefEnd;
764 } else {
765 NdefInfo.psUpperNdefMsg->buffer = p_data;
766 NdefInfo.psUpperNdefMsg->length = len;
767
768 NdefInfo.AppWrLength = len;
769 NdefMap->CompletionRoutine[2].CompletionRoutine =
770 Mfc_WriteNdef_Completion_Routine;
771 if (0 == len) {
772 /* TODO: Erase the Tag */
773 } else {
774 NdefMap->ApduBuffIndex = 0x00;
775 *NdefMap->DataCount = 0x00;
776 PacketData = NdefInfo.psUpperNdefMsg->buffer;
777 PacketDataLength = &(NdefInfo.dwWrLength);
778 NdefMap->WrNdefPacketLength = PacketDataLength;
779 NdefInfo.dwWrLength = len;
780
781 status = phFriNfc_ValidateParams(PacketData, PacketDataLength, 0, NdefMap,
782 PH_FRINFC_NDEF_WRITE_REQ);
783 if (status != NFCSTATUS_SUCCESS) {
nxpandroid64fd68c2015-09-23 16:45:15 +0530784 goto Mfc_WrNdefEnd;
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530785 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530786
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530787 status = phFriNfc_MifareStdMap_WrNdef(
788 NdefMap, PacketData, PacketDataLength, PH_FRINFC_NDEFMAP_SEEK_BEGIN);
nxpandroid64fd68c2015-09-23 16:45:15 +0530789
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530790 if (status == NFCSTATUS_PENDING) {
791 status = NFCSTATUS_SUCCESS;
792 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530793 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530794 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530795
796Mfc_WrNdefEnd:
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530797#if (NXP_EXTNS == TRUE)
798 pthread_mutex_unlock(&SharedDataMutex);
nxpandroid64fd68c2015-09-23 16:45:15 +0530799#endif
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530800 if (status != NFCSTATUS_SUCCESS) {
801 status = NFCSTATUS_FAILED;
802 }
803 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +0530804}
805/*******************************************************************************
806**
807** Function phFriNfc_NdefSmtCrd_Reset__
808**
809** Description This function Resets the component instance to the initial
810** state and initializes the internal variables.
811**
812** Returns NFCSTATUS_SUCCESS
813**
814*******************************************************************************/
Suhas Suresh2de4d8f2018-04-26 18:40:59 +0530815STATIC NFCSTATUS phFriNfc_NdefSmtCrd_Reset__(
816 phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt, uint8_t* SendRecvBuffer,
817 uint16_t* SendRecvBuffLen) {
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530818 // NFCSTATUS status = NFCSTATUS_FAILED; /*commented to
819 // eliminate unused variable warning*/
820 uint8_t index;
nxpandroid64fd68c2015-09-23 16:45:15 +0530821
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530822 /* Initialize the state to Init */
823 NdefSmtCrdFmt->State = PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT;
nxpandroid64fd68c2015-09-23 16:45:15 +0530824
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530825 for (index = 0; index < PH_FRINFC_SMTCRDFMT_CR; index++) {
826 /* Initialize the NdefMap Completion Routine to Null */
827 NdefSmtCrdFmt->CompletionRoutine[index].CompletionRoutine = NULL;
828 /* Initialize the NdefMap Completion Routine context to Null */
829 NdefSmtCrdFmt->CompletionRoutine[index].Context = NULL;
830 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530831
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530832 /* Trx Buffer registered */
833 NdefSmtCrdFmt->SendRecvBuf = SendRecvBuffer;
nxpandroid64fd68c2015-09-23 16:45:15 +0530834
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530835 /* Trx Buffer Size */
836 NdefSmtCrdFmt->SendRecvLength = SendRecvBuffLen;
nxpandroid64fd68c2015-09-23 16:45:15 +0530837
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530838 /* Register Transfer Buffer Length */
839 NdefSmtCrdFmt->SendLength = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +0530840
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530841 /* Initialize the Format status flag*/
842 NdefSmtCrdFmt->FmtProcStatus = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +0530843
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530844 /* Reset the Card Type */
845 NdefSmtCrdFmt->CardType = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +0530846
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530847 /* Reset MapCompletion Info*/
848 NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.CompletionRoutine = NULL;
849 NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.Context = NULL;
nxpandroid64fd68c2015-09-23 16:45:15 +0530850
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530851 phFriNfc_MfStd_Reset(NdefSmtCrdFmt);
nxpandroid64fd68c2015-09-23 16:45:15 +0530852
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530853 return NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +0530854}
855
856/*******************************************************************************
857**
858** Function Mfc_FormatNdef
859**
860** Description It triggers the NDEF format of Mifare Classic Tag.
861**
862**
863** Returns:
864** NFCSTATUS_SUCCESS - if successfully initiated
865** NFCSTATUS_FAILED - otherwise
866**
867*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530868NFCSTATUS Mfc_FormatNdef(uint8_t* secretkey, uint8_t len) {
869 NFCSTATUS status = NFCSTATUS_FAILED;
870 uint8_t mif_std_key[6] = {0};
871 // static uint8_t Index;
872 // /*commented to eliminate unused variable warning*/
873 uint8_t sak = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +0530874
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530875 EXTNS_SetCallBackFlag(false);
nxpandroid64fd68c2015-09-23 16:45:15 +0530876
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530877 memcpy(mif_std_key, secretkey, len);
878 memcpy(current_key, secretkey, len);
nxpandroid64fd68c2015-09-23 16:45:15 +0530879
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530880 if (NULL == NdefSmtCrdFmt || NULL == NdefMap ||
881 NULL == NdefMap->SendRecvBuf) {
882 goto Mfc_FormatEnd;
883 }
884 NdefSmtCrdFmt->pTransceiveInfo = NdefMap->pTransceiveInfo;
nxpandroid64fd68c2015-09-23 16:45:15 +0530885
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530886 gphNxpExtns_Context.CallBackMifare = phFriNfc_MfStd_Process;
887 gphNxpExtns_Context.CallBackCtxt = NdefSmtCrdFmt;
nxpandroid64fd68c2015-09-23 16:45:15 +0530888
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530889 NdefInfo.NdefSendRecvLen = NDEF_SENDRCV_BUF_LEN;
890 phFriNfc_NdefSmtCrd_Reset__(NdefSmtCrdFmt, NdefMap->SendRecvBuf,
891 &(NdefInfo.NdefSendRecvLen));
nxpandroid64fd68c2015-09-23 16:45:15 +0530892
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530893 /* Register Callbacks */
894 NdefSmtCrdFmt->CompletionRoutine[0].CompletionRoutine =
895 Mfc_FormatNdef_Completion_Routine;
896 NdefSmtCrdFmt->CompletionRoutine[1].CompletionRoutine =
897 Mfc_FormatNdef_Completion_Routine;
898 NdefSmtCrdFmt->psRemoteDevInfo = NdefMap->psRemoteDevInfo;
899 sak = NdefSmtCrdFmt->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
nxpandroid64fd68c2015-09-23 16:45:15 +0530900
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530901 if ((0x08 == (sak & 0x18)) || (0x18 == (sak & 0x18)) || (0x01 == sak)) {
902 NdefSmtCrdFmt->CardType = (uint8_t)(
903 ((sak & 0x18) == 0x08)
904 ? PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD
905 : (((sak & 0x19) == 0x19) ? PH_FRINFC_SMTCRDFMT_MFSTD_2K_CRD
906 : PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD));
907 status = phFriNfc_MfStd_Format(NdefSmtCrdFmt, mif_std_key);
908 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530909
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530910 if (NFCSTATUS_PENDING == status) {
911 status = NFCSTATUS_SUCCESS;
912 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530913
914Mfc_FormatEnd:
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530915 if (status != NFCSTATUS_SUCCESS) {
916 status = NFCSTATUS_FAILED;
917 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530918
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530919 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +0530920}
921
922/*******************************************************************************
923**
924** Function phNxNciExtns_MifareStd_Reconnect
925**
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530926** Description This function sends the deactivate command to NFCC for
927*Mifare
nxpandroid64fd68c2015-09-23 16:45:15 +0530928**
929**
930** Returns:
931** NFCSTATUS_PENDING - if successfully initiated
932** NFCSTATUS_FAILED - otherwise
933**
934*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530935NFCSTATUS phNxNciExtns_MifareStd_Reconnect(void) {
936 tNFA_STATUS status;
nxpandroid64fd68c2015-09-23 16:45:15 +0530937
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530938 EXTNS_SetDeactivateFlag(true);
939 if (NFA_STATUS_OK !=
940 (status = NFA_Deactivate(true))) /* deactivate to sleep state */
941 {
942 LOG(ERROR) << StringPrintf("%s: deactivate failed, status = %d", __func__,
943 status);
944 return NFCSTATUS_FAILED;
945 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530946
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530947 return NFCSTATUS_PENDING;
nxpandroid64fd68c2015-09-23 16:45:15 +0530948}
949
950/*******************************************************************************
951**
952** Function Mfc_DeactivateCbackSelect
953**
954** Description This function select the Mifare tag
955**
956**
957** Returns: void
958**
959*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530960void Mfc_DeactivateCbackSelect(void) {
961 tNFA_STATUS status;
nxpandroid64fd68c2015-09-23 16:45:15 +0530962
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530963 EXTNS_SetConnectFlag(true);
964 if (NFA_STATUS_OK !=
965 (status = NFA_Select(0x01, phNciNfc_e_RfProtocolsMifCProtocol,
966 phNciNfc_e_RfInterfacesTagCmd_RF))) {
967 LOG(ERROR) << StringPrintf("%s: NFA_Select failed, status = %d", __func__,
968 status);
969 }
nxpandroid64fd68c2015-09-23 16:45:15 +0530970
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530971 return;
nxpandroid64fd68c2015-09-23 16:45:15 +0530972}
973
974/*******************************************************************************
975**
976** Function Mfc_ActivateCback
977**
978** Description This function invoke the callback when receive the response
979**
980**
981** Returns: void
982**
983**
984*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +0530985void Mfc_ActivateCback(void) {
986 gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt,
987 NFCSTATUS_SUCCESS);
988 return;
nxpandroid64fd68c2015-09-23 16:45:15 +0530989}
990
991/*******************************************************************************
992**
993** Function Mfc_Transceive
994**
995** Description Sends raw frame to Mifare Classic Tag.
996**
997** Returns NFCSTATUS_SUCCESS - if successfully initiated
998** NFCSTATUS_FAILED - otherwise
999**
1000*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301001NFCSTATUS Mfc_Transceive(uint8_t* p_data, uint32_t len) {
1002 NFCSTATUS status = NFCSTATUS_FAILED;
1003 uint8_t i = 0x00;
nxpandroid64fd68c2015-09-23 16:45:15 +05301004
Ganesh Deva389aa822019-09-04 16:48:25 +05301005 if (len == 0) {
1006 android_errorWriteLog(0x534e4554, "132082342");
1007 return status;
1008 }
1009
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301010 gphNxpExtns_Context.RawWriteCallBack = false;
1011 gphNxpExtns_Context.CallBackMifare = NULL;
1012 gphNxpExtns_Context.CallBackCtxt = NdefMap;
nxpandroid64fd68c2015-09-23 16:45:15 +05301013
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301014 EXTNS_SetCallBackFlag(true);
1015 if (p_data[0] == 0x60 || p_data[0] == 0x61) {
Ganesh Deva638d3d12019-09-04 16:48:38 +05301016 if (len < 12) {
1017 android_errorWriteLog(0x534e4554, "125900276");
1018 return status;
1019 }
Suhas Suresh9139dc22018-05-09 15:48:37 +05301020 NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t) p_data[0];
nxpandroid64fd68c2015-09-23 16:45:15 +05301021
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301022 NdefMap->SendRecvBuf[i++] = p_data[1];
nxpandroid64fd68c2015-09-23 16:45:15 +05301023
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301024 NdefMap->SendRecvBuf[i++] = p_data[6]; /* TODO, handle 7 byte UID */
1025 NdefMap->SendRecvBuf[i++] = p_data[7];
1026 NdefMap->SendRecvBuf[i++] = p_data[8];
1027 NdefMap->SendRecvBuf[i++] = p_data[9];
1028 NdefMap->SendRecvBuf[i++] = p_data[10];
1029 NdefMap->SendRecvBuf[i++] = p_data[11];
nxpandroid64fd68c2015-09-23 16:45:15 +05301030
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301031 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1032 NdefMap->SendRecvBuf, NdefMap->SendLength,
1033 NdefMap->SendRecvLength);
1034 } else if (p_data[0] == 0xA0) {
1035 EXTNS_SetCallBackFlag(false);
1036 NdefMap->Cmd.MfCmd = phNfc_eMifareWrite16;
1037 gphNxpExtns_Context.RawWriteCallBack = true;
nxpandroid64fd68c2015-09-23 16:45:15 +05301038
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301039 memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1040 NdefMap->SendLength = len - 1;
1041 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1042 NdefMap->SendRecvBuf, NdefMap->SendLength,
1043 NdefMap->SendRecvLength);
1044 } else if ((p_data[0] == phNfc_eMifareInc) ||
1045 (p_data[0] == phNfc_eMifareDec)) {
1046 EXTNS_SetCallBackFlag(false);
Suhas Suresh9139dc22018-05-09 15:48:37 +05301047 NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t) p_data[0];
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301048 gphNxpExtns_Context.RawWriteCallBack = true;
1049
1050 memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1051 NdefMap->SendLength = len - 1;
1052 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1053 NdefMap->SendRecvBuf, NdefMap->SendLength,
1054 NdefMap->SendRecvLength);
1055 } else if (((p_data[0] == phNfc_eMifareTransfer) ||
1056 (p_data[0] == phNfc_eMifareRestore)) &&
1057 (len == 2)) {
Suhas Suresh9139dc22018-05-09 15:48:37 +05301058 NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t) p_data[0];
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301059 if (p_data[0] == phNfc_eMifareRestore) {
1060 EXTNS_SetCallBackFlag(false);
1061 gphNxpExtns_Context.RawWriteCallBack = true;
1062 memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1063 NdefMap->SendLength = len - 1;
1064 } else {
1065 memcpy(NdefMap->SendRecvBuf, p_data, len);
1066 NdefMap->SendLength = len;
nxpandroid64fd68c2015-09-23 16:45:15 +05301067 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301068 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1069 NdefMap->SendRecvBuf, NdefMap->SendLength,
1070 NdefMap->SendRecvLength);
nxpandroid64fd68c2015-09-23 16:45:15 +05301071
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301072 } else {
1073 NdefMap->Cmd.MfCmd = phNfc_eMifareRaw;
nxpandroid64fd68c2015-09-23 16:45:15 +05301074
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301075 memcpy(NdefMap->SendRecvBuf, p_data, len);
1076 NdefMap->SendLength = len;
1077 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1078 NdefMap->SendRecvBuf, NdefMap->SendLength,
1079 NdefMap->SendRecvLength);
1080 }
1081 if (NFCSTATUS_PENDING == status) {
1082 status = NFCSTATUS_SUCCESS;
1083 } else {
1084 LOG(ERROR) << StringPrintf("ERROR: Mfc_Transceive = 0x%x", status);
1085 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301086
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301087 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +05301088}
1089
1090/*******************************************************************************
1091**
1092** Function nativeNfcExtns_doTransceive
1093**
1094** Description Sends raw frame to BCM stack.
1095**
1096** Returns NFCSTATUS_PENDING - if successfully initiated
1097** NFCSTATUS_FAILED - otherwise
1098**
1099*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301100STATIC NFCSTATUS nativeNfcExtns_doTransceive(uint8_t* buff, uint16_t buffSz) {
1101 NFCSTATUS wStatus = NFCSTATUS_PENDING;
1102 tNFA_STATUS status =
1103 NFA_SendRawFrame(buff, buffSz, NFA_DM_DEFAULT_PRESENCE_CHECK_START_DELAY);
nxpandroid64fd68c2015-09-23 16:45:15 +05301104
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301105 if (status != NFA_STATUS_OK) {
1106 LOG(ERROR) << StringPrintf("%s: fail send; error=%d", __func__, status);
1107 wStatus = NFCSTATUS_FAILED;
1108 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301109
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301110 return wStatus;
nxpandroid64fd68c2015-09-23 16:45:15 +05301111}
1112
1113/*******************************************************************************
1114**
1115** Function phNciNfc_RecvMfResp
1116**
1117** Description This function shall be invoked as part of ReaderMgmt data
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301118** exchange sequence handler on receiving response/data from
1119*NFCC
nxpandroid64fd68c2015-09-23 16:45:15 +05301120**
1121** Returns NFCSTATUS_SUCCESS - Data Reception is successful
1122** NFCSTATUS_FAILED - Data Reception failed
1123**
1124*******************************************************************************/
Suhas Suresh2de4d8f2018-04-26 18:40:59 +05301125STATIC NFCSTATUS phNciNfc_RecvMfResp(phNciNfc_Buff_t* RspBuffInfo,
1126 NFCSTATUS wStatus) {
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301127 NFCSTATUS status = NFCSTATUS_SUCCESS;
1128 uint16_t wPldDataSize = 0;
1129 phNciNfc_ExtnRespId_t RecvdExtnRspId = phNciNfc_e_InvalidRsp;
1130 if (NULL == RspBuffInfo) {
1131 status = NFCSTATUS_FAILED;
1132 } else {
Alisher Alikhodjaev0d7103d2022-03-18 17:13:05 -07001133 if (((PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE) >
1134 RspBuffInfo->wLen) ||
1135 (PH_NCINFC_STATUS_OK != wStatus) || (NULL == (RspBuffInfo->pBuff))) {
nxpandroid64fd68c2015-09-23 16:45:15 +05301136 status = NFCSTATUS_FAILED;
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301137 } else {
1138 RecvdExtnRspId = (phNciNfc_ExtnRespId_t)RspBuffInfo->pBuff[0];
nxpandroid64fd68c2015-09-23 16:45:15 +05301139
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301140 switch (RecvdExtnRspId) {
1141 case phNciNfc_e_MfXchgDataRsp: {
1142 NFCSTATUS writeResponse = NFCSTATUS_SUCCESS;
1143 /* check the status byte */
1144 if ((NFC_GetNCIVersion() == NCI_VERSION_2_0) &&
1145 (((NdefMap->State) == PH_FRINFC_NDEFMAP_STATE_WR_TLV) ||
1146 ((NdefMap->State) == PH_FRINFC_NDEFMAP_STATE_WRITE) ||
1147 ((NdefMap->State) == PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN) ||
1148 ((NdefMap->State) == PH_FRINFC_NDEFMAP_STATE_INIT))) {
1149 uint8_t rspAck = RspBuffInfo->pBuff[RspBuffInfo->wLen - 2];
1150 uint8_t rspAckMask = ((RspBuffInfo->pBuff[RspBuffInfo->wLen - 1]) &
1151 MAX_NUM_VALID_BITS_FOR_ACK);
1152 NCI_CALCULATE_ACK(rspAck, rspAckMask);
1153 writeResponse =
1154 (rspAck == T2T_RSP_ACK) ? NFCSTATUS_SUCCESS : NFC_STATUS_FAILED;
1155 } else {
1156 writeResponse = RspBuffInfo->pBuff[RspBuffInfo->wLen - 1];
1157 }
Nikhil Chhabra9ac26292018-01-10 11:57:21 +05301158
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301159 if (writeResponse == PH_NCINFC_STATUS_OK) {
1160 status = NFCSTATUS_SUCCESS;
1161 uint16_t wRecvDataSz = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +05301162
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301163 /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1164 wPldDataSize = ((RspBuffInfo->wLen) -
nxpandroid64fd68c2015-09-23 16:45:15 +05301165 (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301166 wRecvDataSz = NCI_MAX_DATA_LEN;
nxpandroid64fd68c2015-09-23 16:45:15 +05301167
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301168 /* wPldDataSize = wPldDataSize-1; ==> ignoring the last status byte
1169 * appended with data */
1170 if ((wPldDataSize) <= wRecvDataSz) {
1171 /* Extract the data part from pBuff[2] & fill it to be sent to
1172 * upper layer */
1173 memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[1]),
1174 (wPldDataSize));
1175 /* update the number of bytes received from lower layer,excluding
1176 * the status byte */
1177 *(NdefMap->SendRecvLength) = wPldDataSize;
1178 } else {
1179 // TODO:- Map some status for remaining extra data received to be
1180 // sent back to caller??
1181 status = NFCSTATUS_FAILED;
nxpandroid64fd68c2015-09-23 16:45:15 +05301182 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301183 } else {
1184 status = NFCSTATUS_FAILED;
1185 }
1186 } break;
nxpandroid64fd68c2015-09-23 16:45:15 +05301187
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301188 case phNciNfc_e_MfcAuthRsp: {
1189 /* check the status byte */
1190 if (PH_NCINFC_STATUS_OK == RspBuffInfo->pBuff[1]) {
1191 if (gAuthCmdBuf.auth_sent == true) {
1192 MfcPresenceCheckResult(NFCSTATUS_SUCCESS);
1193 return NFCSTATUS_SUCCESS;
1194 }
1195 gAuthCmdBuf.auth_status = true;
1196 status = NFCSTATUS_SUCCESS;
1197
1198 /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1199 wPldDataSize = ((RspBuffInfo->wLen) -
1200 (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
1201
1202 /* Extract the data part from pBuff[2] & fill it to be sent to upper
1203 * layer */
1204 memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[2]),
1205 wPldDataSize);
1206 /* update the number of bytes received from lower layer,excluding
1207 * the status byte */
1208 *(NdefMap->SendRecvLength) = wPldDataSize;
1209 } else {
1210 if (gAuthCmdBuf.auth_sent == true) {
1211 gAuthCmdBuf.auth_status = false;
1212 MfcPresenceCheckResult(NFCSTATUS_FAILED);
1213 return NFCSTATUS_SUCCESS;
1214 } else {
1215 /* Reset the stored auth command buffer */
1216 memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1217 gAuthCmdBuf.pauth_cmd->length = 0;
1218 gAuthCmdBuf.auth_status = false;
1219 }
1220 status = NFCSTATUS_FAILED;
1221 }
1222 } break;
1223
1224 default: { status = NFCSTATUS_FAILED; } break;
1225 }
1226 }
1227 }
1228
1229 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +05301230}
1231
1232/*******************************************************************************
1233**
1234** Function phLibNfc_SendWrt16CmdPayload
1235**
1236** Description This function map the raw write cmd
1237**
1238** Returns NFCSTATUS_SUCCESS - Command framing done
1239** NFCSTATUS_INVALID_PARAMETER - Otherwise
1240**
1241*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301242STATIC NFCSTATUS
1243phLibNfc_SendWrt16CmdPayload(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1244 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1245 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +05301246
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301247 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1248 (0 != (pTransceiveInfo->sSendData.length))) {
1249 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1250 (pTransceiveInfo->sSendData.length));
1251 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1252 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1253 } else {
1254 wStatus = NFCSTATUS_INVALID_PARAMETER;
1255 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301256
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301257 if (gphNxpExtns_Context.RawWriteCallBack == true) {
1258 EXTNS_SetCallBackFlag(true);
1259 gphNxpExtns_Context.RawWriteCallBack = false;
1260 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301261
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301262 return wStatus;
nxpandroid64fd68c2015-09-23 16:45:15 +05301263}
1264
1265/*******************************************************************************
1266**
1267** Function phLibNfc_SendIncDecCmdPayload
1268**
1269** Description This function prepares the Increment/Decrement Value to be
1270** sent. This is called after sending the Increment/Decrement
1271** command is already sent and successfull
1272**
1273** Returns NFCSTATUS_SUCCESS - Payload framing done
1274** NFCSTATUS_INVALID_PARAMETER - Otherwise
1275**
1276*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301277STATIC NFCSTATUS
1278phLibNfc_SendIncDecCmdPayload(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1279 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1280 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +05301281
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301282 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1283 (0 != (pTransceiveInfo->sSendData.length))) {
1284 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1285 (pTransceiveInfo->sSendData.length));
1286 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1287 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1288 } else {
1289 wStatus = NFCSTATUS_INVALID_PARAMETER;
1290 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301291
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301292 if (gphNxpExtns_Context.RawWriteCallBack == true) {
1293 EXTNS_SetCallBackFlag(true);
1294 gphNxpExtns_Context.RawWriteCallBack = false;
1295 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301296
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301297 return wStatus;
nxpandroid64fd68c2015-09-23 16:45:15 +05301298}
1299
1300/*******************************************************************************
1301**
1302** Function Mfc_RecvPacket
1303**
1304** Description Decodes Mifare Classic Tag Response
1305** This is called from NFA_SendRaw Callback
1306**
1307** Returns:
1308** NFCSTATUS_SUCCESS - if successfully initiated
1309** NFCSTATUS_FAILED - otherwise
1310**
1311*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301312NFCSTATUS Mfc_RecvPacket(uint8_t* buff, uint8_t buffSz) {
1313 NFCSTATUS status = NFCSTATUS_SUCCESS;
1314 phNciNfc_Buff_t RspBuff;
1315 uint8_t* pcmd_buff;
1316 uint16_t buffSize;
nxpandroid64fd68c2015-09-23 16:45:15 +05301317
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301318 RspBuff.pBuff = buff;
1319 RspBuff.wLen = buffSz;
1320 status = phNciNfc_RecvMfResp(&RspBuff, status);
1321 if (true == gAuthCmdBuf.auth_sent) {
1322 DLOG_IF(INFO, nfc_debug_enabled)
1323 << StringPrintf("%s Mfc Check Presence in progress", __func__);
1324 gAuthCmdBuf.auth_sent = false;
nxpandroid64fd68c2015-09-23 16:45:15 +05301325 return status;
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301326 }
1327 if (true == gphNxpExtns_Context.writecmdFlag &&
1328 (NFCSTATUS_SUCCESS == status)) {
1329 pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1330 if (NULL == pcmd_buff) {
1331 return NFCSTATUS_FAILED;
1332 }
1333 buffSize = MAX_BUFF_SIZE;
1334 gphNxpExtns_Context.writecmdFlag = false;
1335 phLibNfc_SendWrt16CmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1336 status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1337 if (NFCSTATUS_PENDING != status) {
1338 LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1339 } else {
1340 status = NFCSTATUS_SUCCESS;
1341 }
1342 if (pcmd_buff != NULL) {
1343 free(pcmd_buff);
1344 pcmd_buff = NULL;
1345 }
1346 } else if (true == gphNxpExtns_Context.incrdecflag &&
1347 (NFCSTATUS_SUCCESS == status)) {
1348 pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1349 if (NULL == pcmd_buff) {
1350 return NFCSTATUS_FAILED;
1351 }
1352 buffSize = MAX_BUFF_SIZE;
1353 gphNxpExtns_Context.incrdecflag = false;
1354 phLibNfc_SendIncDecCmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1355 status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1356 if (NFCSTATUS_PENDING != status) {
1357 LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1358 } else {
1359 status = NFCSTATUS_SUCCESS;
1360 }
1361 gphNxpExtns_Context.incrdecstatusflag = true;
1362 if (pcmd_buff != NULL) {
1363 free(pcmd_buff);
1364 pcmd_buff = NULL;
1365 }
1366
1367 } else {
1368 if (gphNxpExtns_Context.CallBackMifare != NULL) {
1369 if ((gphNxpExtns_Context.incrdecstatusflag == true) && status == 0xB2) {
1370 gphNxpExtns_Context.incrdecstatusflag = false;
1371 status = NFCSTATUS_SUCCESS;
1372 }
1373 gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt,
1374 status);
1375 }
1376 }
1377
1378 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +05301379}
1380
1381/*******************************************************************************
1382**
1383** Function phNciNfc_MfCreateXchgDataHdr
1384**
1385** Description This function builds the payload header for mifare XchgData
1386** request to be sent to NFCC.
1387**
1388** Returns NFCSTATUS_PENDING - Command framing done
1389** NFCSTATUS_FAILED - Otherwise
1390**
1391*******************************************************************************/
1392STATIC
1393NFCSTATUS
1394phNciNfc_MfCreateXchgDataHdr(phNciNfc_TransceiveInfo_t tTranscvInfo,
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301395 uint8_t* buff, uint16_t* buffSz)
nxpandroid64fd68c2015-09-23 16:45:15 +05301396
1397{
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301398 NFCSTATUS status = NFCSTATUS_SUCCESS;
1399 uint8_t i = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +05301400
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301401 buff[i++] = phNciNfc_e_MfRawDataXchgHdr;
1402 memcpy(&buff[i], tTranscvInfo.tSendData.pBuff, tTranscvInfo.tSendData.wLen);
1403 *buffSz = i + tTranscvInfo.tSendData.wLen;
nxpandroid64fd68c2015-09-23 16:45:15 +05301404
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301405 status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
nxpandroid64fd68c2015-09-23 16:45:15 +05301406
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301407 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +05301408}
1409
1410/*******************************************************************************
1411**
1412** Function phNciNfc_MfCreateAuthCmdHdr
1413**
1414** Description This function builds the payload header for mifare
1415** classic Authenticate command to be sent to NFCC.
1416**
1417** Returns NFCSTATUS_PENDING - Command framing done
1418** NFCSTATUS_FAILED - Otherwise
1419**
1420*******************************************************************************/
1421STATIC
1422NFCSTATUS
1423phNciNfc_MfCreateAuthCmdHdr(phNciNfc_TransceiveInfo_t tTranscvInfo,
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301424 uint8_t bBlockAddr, uint8_t* buff,
1425 uint16_t* buffSz) {
1426 NFCSTATUS status = NFCSTATUS_SUCCESS;
1427 // pphNciNfc_RemoteDevInformation_t pActivDev = NULL;
1428 // /*commented to eliminate unused variable warning*/
1429 uint8_t bKey = 0x00;
nxpandroid64fd68c2015-09-23 16:45:15 +05301430
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301431 /*No need to check range of block address*/
1432 /*To check for Authenticate A or Authenticate B type command*/
1433 if (PHNCINFC_AUTHENTICATION_KEYB == tTranscvInfo.tSendData.pBuff[0]) {
1434 bKey = bKey | PHNCINFC_ENABLE_KEY_B;
1435 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301436
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301437 /*TO Do last 4 bits of Key to be set based of firmware implementation*/
1438 /*this value is hardcoded but based on firmware implementation change this
1439 * value*/
1440 bKey = (bKey | PHNCINFC_AUTHENTICATION_KEY);
nxpandroid64fd68c2015-09-23 16:45:15 +05301441
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301442 bKey |= tTranscvInfo.tSendData.pBuff[2];
nxpandroid64fd68c2015-09-23 16:45:15 +05301443
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301444 /*For authentication extension no need to copy tSendData buffer of
1445 * tTranscvInfo */
1446 tTranscvInfo.tSendData.wLen = 0x00;
nxpandroid64fd68c2015-09-23 16:45:15 +05301447
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301448 buff[0] = phNciNfc_e_MfcAuthReq;
1449 buff[1] = bBlockAddr;
1450 buff[2] = bKey;
nxpandroid64fd68c2015-09-23 16:45:15 +05301451
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301452 *buffSz = 0x03;
1453 if (bKey & PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY) {
1454 memcpy(&buff[3], &tTranscvInfo.tSendData.pBuff[3], PHLIBNFC_MFC_AUTHKEYLEN);
1455 *buffSz += PHLIBNFC_MFC_AUTHKEYLEN;
1456 }
1457 /* Store the auth command buffer to use further for presence check */
1458 if (gAuthCmdBuf.pauth_cmd != NULL) {
1459 memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1460 gAuthCmdBuf.pauth_cmd->length = *buffSz;
1461 memcpy(gAuthCmdBuf.pauth_cmd->buffer, buff, *buffSz);
1462 }
1463 status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
nxpandroid64fd68c2015-09-23 16:45:15 +05301464
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301465 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +05301466}
1467
1468/*******************************************************************************
1469**
1470** Function phNciNfc_SendMfReq
1471**
1472** Description This function shall be invoked as part of ReaderMgmt data
1473** exchange sequence handler.
1474** It shall send the request packet to NFCC.
1475**
1476** Returns NFCSTATUS_PENDING - Send request is Pending
1477** NFCSTATUS_FAILED - otherwise
1478**
1479*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301480STATIC NFCSTATUS phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,
1481 uint8_t* buff, uint16_t* buffSz) {
1482 NFCSTATUS status = NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +05301483
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301484 switch (tTranscvInfo.uCmd.T2TCmd) {
1485 case phNciNfc_eT2TRaw: {
1486 status = phNciNfc_MfCreateXchgDataHdr(tTranscvInfo, buff, buffSz);
1487 } break;
1488 case phNciNfc_eT2TAuth: {
1489 status = phNciNfc_MfCreateAuthCmdHdr(tTranscvInfo, (tTranscvInfo.bAddr),
1490 buff, buffSz);
1491 } break;
1492 default: {
1493 status = NFCSTATUS_FAILED;
1494 break;
nxpandroid64fd68c2015-09-23 16:45:15 +05301495 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301496 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301497
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301498 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +05301499}
1500
1501/*******************************************************************************
1502**
1503** Function phLibNfc_CalSectorAddress
1504**
1505** Description This function update the sector address for Mifare classic
1506**
1507** Returns none
1508**
1509*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301510STATIC void phLibNfc_CalSectorAddress(uint8_t* Sector_Address) {
1511 uint8_t BlockNumber = 0x00;
nxpandroid64fd68c2015-09-23 16:45:15 +05301512
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301513 if (NULL != Sector_Address) {
1514 BlockNumber = *Sector_Address;
1515 if (BlockNumber >= PHLIBNFC_MIFARESTD4K_BLK128) {
1516 *Sector_Address = (uint8_t)(PHLIBNFC_MIFARESTD_SECTOR_NO32 +
1517 ((BlockNumber - PHLIBNFC_MIFARESTD4K_BLK128) /
1518 PHLIBNFC_MIFARESTD_BLOCK_BYTES));
1519 } else {
1520 *Sector_Address = BlockNumber / PHLIBNFC_NO_OF_BLKPERSECTOR;
nxpandroid64fd68c2015-09-23 16:45:15 +05301521 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301522 } else {
1523 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301524
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301525 return;
nxpandroid64fd68c2015-09-23 16:45:15 +05301526}
1527
1528/*******************************************************************************
1529**
1530** Function phLibNfc_GetKeyNumberMFC
1531**
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301532** Description This function find key number based on authentication
1533*command
nxpandroid64fd68c2015-09-23 16:45:15 +05301534**
1535** Returns NFCSTATUS_SUCCESS - If found the key number
1536** NFCSTATUS_FAILED - otherwise
1537**
1538*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301539STATIC NFCSTATUS phLibNfc_GetKeyNumberMFC(uint8_t* buffer, uint8_t* bKey) {
1540 int32_t sdwStat = 0X00;
1541 NFCSTATUS wStatus = NFCSTATUS_INVALID_PARAMETER;
nxpandroid64fd68c2015-09-23 16:45:15 +05301542
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301543 uint8_t bIndex = 0x00;
1544 uint8_t bNoOfKeys = 0x00;
nxpandroid64fd68c2015-09-23 16:45:15 +05301545
1546#if PHLIBNFC_NXPETENSION_CONFIGURE_MFKEYS
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301547 uint8_t aMfc_keys[NXP_NUMBER_OF_MFC_KEYS][NXP_MFC_KEY_SIZE] = NXP_MFC_KEYS;
nxpandroid64fd68c2015-09-23 16:45:15 +05301548#else
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301549 uint8_t aMfc_keys[1][1] = {{0x00}};
nxpandroid64fd68c2015-09-23 16:45:15 +05301550#endif
1551
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301552 if (NULL != bKey && NULL != buffer) {
1553 bNoOfKeys = sizeof(aMfc_keys) / NXP_MFC_KEY_SIZE;
1554 /* Traverse through the keys stored to determine whether keys is preloaded
1555 * key */
1556 for (bIndex = 0; bIndex < bNoOfKeys; bIndex++) {
1557 /* Check passed key is NDEF key */
1558 sdwStat = memcmp(&buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD], aMfc_keys[bIndex],
1559 PHLIBNFC_MFC_AUTHKEYLEN);
1560 if (!sdwStat) {
1561 LOG(ERROR) << StringPrintf(
1562 "Mifare : phLibNfc_GetKeyNumberMFC Key found");
1563 *bKey = bIndex;
1564 wStatus = NFCSTATUS_SUCCESS;
1565 break;
1566 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301567 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301568 LOG(ERROR) << StringPrintf(
1569 "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x Key = 0x%x",
1570 wStatus, *bKey);
1571 } else {
1572 wStatus = NFCSTATUS_FAILED;
1573 LOG(ERROR) << StringPrintf(
1574 "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x", wStatus);
1575 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301576
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301577 return wStatus;
nxpandroid64fd68c2015-09-23 16:45:15 +05301578}
1579
1580/*******************************************************************************
1581**
1582** Function phLibNfc_ChkAuthCmdMFC
1583**
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301584** Description This function Check Authentication command send is proper or
1585*not
nxpandroid64fd68c2015-09-23 16:45:15 +05301586**
1587** Returns NFCSTATUS_SUCCESS - Authenticate command is proper
1588** NFCSTATUS_FAILED - otherwise
1589**
1590*******************************************************************************/
Suhas Suresh2de4d8f2018-04-26 18:40:59 +05301591STATIC NFCSTATUS phLibNfc_ChkAuthCmdMFC(
1592 phNfc_sTransceiveInfo_t* pTransceiveInfo, uint8_t* bKey) {
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301593 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +05301594
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301595 if (NULL != pTransceiveInfo && NULL != pTransceiveInfo->sSendData.buffer &&
1596 0 != pTransceiveInfo->sSendData.length && NULL != bKey) {
1597 if ((pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentA ||
1598 pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentB)) {
1599 wStatus =
1600 phLibNfc_GetKeyNumberMFC(pTransceiveInfo->sSendData.buffer, bKey);
1601 } else {
1602 wStatus = NFCSTATUS_FAILED;
nxpandroid64fd68c2015-09-23 16:45:15 +05301603 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301604 } else {
1605 wStatus = NFCSTATUS_FAILED;
1606 }
1607 return wStatus;
nxpandroid64fd68c2015-09-23 16:45:15 +05301608}
1609
1610/*******************************************************************************
1611**
1612** Function phLibNfc_MifareMap
1613**
1614** Description Mifare Mapping Utility function
1615**
1616** Returns NFCSTATUS_SUCCESS - Mapping is proper
1617** NFCSTATUS_INVALID_PARAMETER - otherwise
1618**
1619*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301620STATIC NFCSTATUS
1621phLibNfc_MifareMap(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1622 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1623 NFCSTATUS status = NFCSTATUS_SUCCESS;
1624 uint8_t bBuffIdx = 0;
1625 uint8_t bSectorNumber;
1626 uint8_t bKey = 0;
nxpandroid64fd68c2015-09-23 16:45:15 +05301627
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301628 switch (pTransceiveInfo->cmd.MfCmd) {
1629 case phNfc_eMifareRead16: {
1630 if ((NULL != pTransceiveInfo->sRecvData.buffer) &&
1631 (0 != (pTransceiveInfo->sRecvData.length))) {
1632 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareRead16;
1633 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1634 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1635 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1636 } else {
1637 status = NFCSTATUS_INVALID_PARAMETER;
1638 }
1639 } break;
1640
1641 case phNfc_eMifareWrite16: {
1642 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1643 (0 != (pTransceiveInfo->sSendData.length))) {
1644 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1645 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1646 memcpy(&(pMappedTranscvIf->tSendData.pBuff[bBuffIdx]),
1647 pTransceiveInfo->sSendData.buffer,
1648 (pTransceiveInfo->sSendData.length));
1649 pMappedTranscvIf->tSendData.wLen =
1650 bBuffIdx + pTransceiveInfo->sSendData.length;
1651 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1652 } else {
1653 status = NFCSTATUS_INVALID_PARAMETER;
1654 }
1655 } break;
1656
1657 case phNfc_eMifareAuthentA:
1658 case phNfc_eMifareAuthentB: {
1659 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1660 (0 != (pTransceiveInfo->sSendData.length)) &&
1661 (NULL != pTransceiveInfo->sRecvData.buffer) &&
1662 (0 != (pTransceiveInfo->sRecvData.length))) {
1663 status = phLibNfc_ChkAuthCmdMFC(pTransceiveInfo, &bKey);
1664 if (NFCSTATUS_FAILED != status) {
1665 bSectorNumber = pTransceiveInfo->addr;
1666 phLibNfc_CalSectorAddress(&bSectorNumber);
1667 /*For creating extension command header pTransceiveInfo's MfCmd get
1668 * used*/
1669 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] =
1670 pTransceiveInfo->cmd.MfCmd;
1671 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bSectorNumber;
1672 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TAuth;
1673 pMappedTranscvIf->bAddr = bSectorNumber;
1674 pMappedTranscvIf->bNumBlock = pTransceiveInfo->NumBlock;
1675 if (NFCSTATUS_SUCCESS == status) {
1676 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1677 (pMappedTranscvIf->tSendData.wLen) = (uint16_t)(bBuffIdx);
1678
1679 } else if (NFCSTATUS_INVALID_PARAMETER == status) {
1680 bKey = bKey | PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY;
1681 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1682 memcpy(&pMappedTranscvIf->tSendData.pBuff[bBuffIdx],
1683 &pTransceiveInfo->sSendData
1684 .buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD],
1685 PHLIBNFC_MFC_AUTHKEYLEN);
1686
1687 (pMappedTranscvIf->tSendData.wLen) =
1688 (uint16_t)(bBuffIdx + PHLIBNFC_MFC_AUTHKEYLEN);
1689 status = NFCSTATUS_SUCCESS;
1690 } else {
1691 /* do nothing */
1692 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301693 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301694 } else {
1695 status = NFCSTATUS_INVALID_PARAMETER;
1696 }
1697 } break;
nxpandroid64fd68c2015-09-23 16:45:15 +05301698
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301699 case phNfc_eMifareRaw: {
1700 } break;
nxpandroid64fd68c2015-09-23 16:45:15 +05301701
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301702 default: {
1703 status = NFCSTATUS_INVALID_PARAMETER;
1704 break;
nxpandroid64fd68c2015-09-23 16:45:15 +05301705 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301706 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301707
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301708 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +05301709}
1710
1711/*******************************************************************************
1712**
1713** Function phLibNfc_MapCmds
1714**
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301715** Description This function maps the command request from libnfc level to
1716*nci level
nxpandroid64fd68c2015-09-23 16:45:15 +05301717**
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301718** Returns NFCSTATUS_SUCCESS - Mapping of command is
1719*successful
nxpandroid64fd68c2015-09-23 16:45:15 +05301720** NFCSTATUS_INVALID_PARAMETER - One or more of the supplied
1721** parameters could not be interpreted properly
1722**
1723*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301724STATIC NFCSTATUS phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,
1725 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1726 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1727 NFCSTATUS status = NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +05301728
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301729 if ((NULL == pTransceiveInfo) || (NULL == pMappedTranscvIf)) {
1730 return NFCSTATUS_FAILED;
1731 }
1732 switch (RemDevType) {
1733 case phNciNfc_eMifare1k_PICC:
1734 case phNciNfc_eMifare4k_PICC: {
1735 status = phLibNfc_MifareMap(pTransceiveInfo, pMappedTranscvIf);
1736 break;
nxpandroid64fd68c2015-09-23 16:45:15 +05301737 }
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301738 default: { break; }
1739 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301740
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301741 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +05301742}
1743
1744/*******************************************************************************
1745**
1746** Function phLibNfc_SendAuthCmd
1747**
1748** Description This function Send authentication command to NFCC
1749**
1750** Returns NFCSTATUS_SUCCESS - Parameters are proper
1751** NFCSTATUS_INVALID_PARAMETER - Otherwise
1752**
1753*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301754STATIC NFCSTATUS
1755phLibNfc_SendAuthCmd(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1756 phNciNfc_TransceiveInfo_t* tNciTranscvInfo) {
1757 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
nxpandroid64fd68c2015-09-23 16:45:15 +05301758
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301759 wStatus = phLibNfc_MapCmds(phNciNfc_eMifare1k_PICC, pTransceiveInfo,
1760 tNciTranscvInfo);
nxpandroid64fd68c2015-09-23 16:45:15 +05301761
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301762 return wStatus;
nxpandroid64fd68c2015-09-23 16:45:15 +05301763}
1764
1765/*******************************************************************************
1766**
1767** Function phLibNfc_SendWrt16Cmd
1768**
1769** Description This function maps Mifarewirte16 commands
1770**
1771** Returns NFCSTATUS_SUCCESS - Parameters are mapped
1772** NFCSTATUS_INVALID_PARAMETER - Otherwise
1773**
1774*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301775STATIC NFCSTATUS
1776phLibNfc_SendWrt16Cmd(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1777 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1778 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1779 uint8_t bBuffIdx = 0x00;
nxpandroid64fd68c2015-09-23 16:45:15 +05301780
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301781 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1782 (0 != (pTransceiveInfo->sSendData.length))) {
1783 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1784 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1785 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1786 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1787 } else {
1788 wStatus = NFCSTATUS_INVALID_PARAMETER;
1789 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301790
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301791 return wStatus;
nxpandroid64fd68c2015-09-23 16:45:15 +05301792}
1793
1794/*******************************************************************************
1795**
1796** Function phLibNfc_SendIncDecCmd
1797**
1798** Description This function prepares the Increment/Decrement command
1799** to be sent, increment/decrement value is sent separately
1800**
1801** Returns NFCSTATUS_SUCCESS - Params are mapped
1802** NFCSTATUS_INVALID_PARAMETER - Otherwise
1803**
1804*******************************************************************************/
Suhas Suresh2de4d8f2018-04-26 18:40:59 +05301805STATIC NFCSTATUS phLibNfc_SendIncDecCmd(
1806 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1807 pphNciNfc_TransceiveInfo_t pMappedTranscvIf, uint8_t IncDecCmd) {
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301808 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1809 uint8_t bBuffIdx = 0x00;
nxpandroid64fd68c2015-09-23 16:45:15 +05301810
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301811 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1812 (0 != (pTransceiveInfo->sSendData.length))) {
1813 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = IncDecCmd;
1814 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1815 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1816 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1817 } else {
1818 wStatus = NFCSTATUS_INVALID_PARAMETER;
1819 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301820
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301821 return wStatus;
nxpandroid64fd68c2015-09-23 16:45:15 +05301822}
1823
1824/*******************************************************************************
1825**
1826** Function phLibNfc_SendRawCmd
1827**
1828** Description This function maps Mifare raw command
1829**
1830** Returns NFCSTATUS_SUCCESS - Parameters are mapped
1831** NFCSTATUS_INVALID_PARAMETER - Otherwise
1832**
1833*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301834STATIC NFCSTATUS
1835phLibNfc_SendRawCmd(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1836 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1837 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1838 // uint8_t bBuffIdx = 0x00; /*commented to
1839 // eliminate unused variable warning*/
nxpandroid64fd68c2015-09-23 16:45:15 +05301840
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301841 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1842 (0 != (pTransceiveInfo->sSendData.length))) {
1843 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1844 (pTransceiveInfo->sSendData.length));
1845 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1846 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1847 } else {
1848 wStatus = NFCSTATUS_INVALID_PARAMETER;
1849 }
nxpandroid64fd68c2015-09-23 16:45:15 +05301850
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301851 return wStatus;
nxpandroid64fd68c2015-09-23 16:45:15 +05301852}
1853
1854/*******************************************************************************
1855**
1856** Function phFriNfc_ExtnsTransceive
1857**
1858** Description This function maps Mifare raw command and send it to NFCC
1859**
1860** Returns NFCSTATUS_PENDING - Operation successful
1861** NFCSTATUS_INVALID_PARAMETER - Otherwise
1862**
1863*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301864NFCSTATUS phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1865 phNfc_uCmdList_t Cmd, uint8_t* SendRecvBuf,
nxpandroid64fd68c2015-09-23 16:45:15 +05301866 uint16_t SendLength,
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301867 uint16_t* SendRecvLength) {
1868 (void)SendRecvLength;
1869 NFCSTATUS status = NFCSTATUS_FAILED;
1870 uint8_t* buff = NULL;
1871 uint16_t buffSz = 0;
1872 uint8_t i = 0;
1873 uint32_t length = SendLength;
1874 uint8_t restore_payload[] = {
1875 0x00, 0x00, 0x00, 0x00,
1876 };
nxpandroid64fd68c2015-09-23 16:45:15 +05301877
Ganesh Deva20b20a52019-09-04 16:48:11 +05301878 if (SendLength == 0) {
1879 android_errorWriteLog(0x534e4554, "132083376");
1880 return status;
1881 }
1882
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301883 buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1884 if (NULL == buff) {
nxpandroid64fd68c2015-09-23 16:45:15 +05301885 return status;
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301886 }
1887
1888 pTransceiveInfo->cmd = Cmd;
1889
1890 if ((Cmd.MfCmd == phNfc_eMifareAuthentA) ||
1891 (Cmd.MfCmd == phNfc_eMifareAuthentB)) {
1892 pTransceiveInfo->addr = SendRecvBuf[i++];
1893 pTransceiveInfo->sSendData.buffer[4] = SendRecvBuf[i++];
1894 pTransceiveInfo->sSendData.buffer[5] = SendRecvBuf[i++];
1895 pTransceiveInfo->sSendData.buffer[6] = SendRecvBuf[i++];
1896 pTransceiveInfo->sSendData.buffer[7] = SendRecvBuf[i++];
1897 pTransceiveInfo->sSendData.buffer[8] = SendRecvBuf[i++];
1898 pTransceiveInfo->sSendData.buffer[9] = SendRecvBuf[i++];
1899
1900 pTransceiveInfo->cmd.MfCmd = Cmd.MfCmd;
1901
1902 pTransceiveInfo->sSendData.length = length;
1903 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1904 status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1905 } else if (Cmd.MfCmd == phNfc_eMifareWrite16) {
1906 pTransceiveInfo->addr = SendRecvBuf[i++];
1907 length = SendLength - i;
1908 memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1909 pTransceiveInfo->sSendData.length = length;
1910 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1911
1912 gphNxpExtns_Context.writecmdFlag = true;
1913
1914 status = phLibNfc_SendWrt16Cmd(pTransceiveInfo, &tNciTranscvInfo);
1915 } else if ((Cmd.MfCmd == phNfc_eMifareInc) ||
1916 (Cmd.MfCmd == phNfc_eMifareDec)) {
1917 pTransceiveInfo->addr = SendRecvBuf[i++];
1918 length = SendLength - i;
1919 memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1920 pTransceiveInfo->sSendData.length = length;
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_eMifareRestore) {
1929 pTransceiveInfo->addr = SendRecvBuf[i++];
1930 length = SendLength - i;
1931 memcpy(pTransceiveInfo->sSendData.buffer, &restore_payload[0],
1932 sizeof(restore_payload));
1933 pTransceiveInfo->sSendData.length = length + sizeof(restore_payload);
1934 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1935
1936 gphNxpExtns_Context.incrdecflag = true;
1937
1938 status =
1939 phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd);
1940
1941 } else if ((Cmd.MfCmd == phNfc_eMifareRaw) ||
1942 (Cmd.MfCmd == phNfc_eMifareTransfer)) {
1943 pTransceiveInfo->cmd.MfCmd = (phNfc_eMifareCmdList_t)phNciNfc_eT2TRaw;
1944 memcpy(pTransceiveInfo->sSendData.buffer, SendRecvBuf, length);
1945 pTransceiveInfo->sSendData.length = length;
1946 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1947 status = phLibNfc_SendRawCmd(pTransceiveInfo, &tNciTranscvInfo);
1948 } else {
1949 pTransceiveInfo->addr = SendRecvBuf[i++];
1950 length = SendLength - i;
1951 memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1952 pTransceiveInfo->sSendData.length = length;
1953 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1954 status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1955 }
1956
1957 if (NFCSTATUS_SUCCESS == status) {
1958 status = phNciNfc_SendMfReq(tNciTranscvInfo, buff, &buffSz);
1959 if (NFCSTATUS_PENDING != status) {
1960 LOG(ERROR) << StringPrintf("ERROR : phNciNfc_SendMfReq()");
1961 }
1962 } else {
1963 LOG(ERROR) << StringPrintf(" ERROR : Sending phNciNfc_SendMfReq");
1964 }
1965 if (buff != NULL) {
1966 free(buff);
1967 buff = NULL;
1968 }
1969
1970 return status;
nxpandroid64fd68c2015-09-23 16:45:15 +05301971}
1972/*******************************************************************************
1973**
1974** Function Mfc_CheckNdef_timeoutcb_Routine
1975**
1976** Description Callback for Mifare check ndef
1977**
1978** Returns None
1979**
1980**
1981*******************************************************************************/
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301982STATIC void Mfc_CheckNdef_timeoutcb_Routine(union sigval value) {
1983 LOG(ERROR) << StringPrintf(" Inside Mfc_CheckNdef_timeoutcb_Routine() ");
1984 tNFA_CONN_EVT_DATA conn_evt_data;
1985 /* NDEF Detection failed for other reasons */
1986 conn_evt_data.ndef_detect.status = NFCSTATUS_FAILED;
1987 conn_evt_data.ndef_detect.cur_size = 0;
1988 conn_evt_data.ndef_detect.max_size = 0;
1989 conn_evt_data.ndef_detect.flags = RW_NDEF_FL_UNKNOWN;
nxpandroid64fd68c2015-09-23 16:45:15 +05301990
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301991 /* update local flags */
1992 NdefInfo.is_ndef = 0;
1993 NdefInfo.NdefActualSize = conn_evt_data.ndef_detect.cur_size;
nxpandroid64fd68c2015-09-23 16:45:15 +05301994
Suhas Suresh6a4cb322018-04-26 18:04:09 +05301995 (*gphNxpExtns_Context.p_conn_cback)(NFA_NDEF_DETECT_EVT, &conn_evt_data);
nxpandroid64fd68c2015-09-23 16:45:15 +05301996}