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