2 * Copyright (C) 2010 NXP Semiconductors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * \file phFriNfcNdefMap.c
19 * \brief NFC Ndef Mapping For Different Smart Cards.
23 * $Date: Mon Dec 13 14:14:12 2010 $
31 #include <phFriNfc_NdefMap.h>
33 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
34 #include <phFriNfc_MifareULMap.h>
35 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
37 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
38 #include <phFriNfc_TopazMap.h>
39 #endif /* PH_FRINFC_MAP_TOPAZ_DISABLED */
41 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
42 #include <phFriNfc_MifareStdMap.h>
43 #endif /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
45 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
46 #include <phFriNfc_DesfireMap.h>
47 #endif /* PH_FRINFC_MAP_DESFIRE_DISABLED */
49 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
50 #include <phFriNfc_FelicaMap.h>
51 #endif /* PH_FRINFC_MAP_FELICA_DISABLED */
53 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
54 #include <phFriNfc_ISO15693Map.h>
55 #endif /* PH_FRINFC_MAP_ISO15693_DISABLED */
57 #ifdef PHFRINFC_OVRHAL_MOCKUP
58 #include <phFriNfc_MockupMap.h>
59 #endif /* PHFRINFC_OVRHAL_MOCKUP */
62 #include <phFriNfc_OvrHal.h>
64 /*! \ingroup grp_file_attributes
67 * File: \ref phFriNfcNdefMap.c
71 #define PHFRINFCNDEFMAP_FILEREVISION "$Revision: 1.39 $"
72 #define PHFRINFCNDEFMAP_FILEALIASES "$Aliases: $"
75 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
76 /* Desfire capability Container Reset Helper */
77 static void phFriNfc_DesfCapCont_HReset(phFriNfc_NdefMap_t *NdefMap);
78 #endif /* PH_FRINFC_MAP_DESFIRE_DISABLED */
80 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
81 /* Felica Smart Tag Reset Helper */
82 static void phFriNfc_Felica_HReset(phFriNfc_NdefMap_t *NdefMap);
83 #endif /* PH_FRINFC_MAP_FELICA_DISABLED */
86 /* \note This function has to be called at the beginning, after creating an
87 * instance of \ref phFriNfc_NdefMap_t . Use this function to reset
88 * the instance and/or switch to a different underlying device (
89 * different NFC device or device mode, or different Remote Device).
92 NFCSTATUS phFriNfc_NdefMap_Reset( phFriNfc_NdefMap_t *NdefMap,
94 phHal_sRemoteDevInformation_t *psRemoteDevInfo,
95 phHal_sDevInputParam_t *psDevInputParam,
97 uint16_t TrxBufferSize,
98 uint8_t *ReceiveBuffer,
99 uint16_t *ReceiveLength,
102 NFCSTATUS status = NFCSTATUS_SUCCESS;
105 if ( (ReceiveLength == NULL) || (NdefMap == NULL) || (psRemoteDevInfo == NULL) ||
106 (TrxBuffer == NULL) || (TrxBufferSize == 0) || (LowerDevice == NULL) ||
107 (*ReceiveLength == 0) || (ReceiveBuffer == NULL) || (DataCount == NULL) ||
108 (psDevInputParam == NULL) ||
109 (*ReceiveLength < PH_FRINFC_NDEFMAP_MAX_SEND_RECV_BUF_SIZE ))
111 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
115 /* Initialise the state to Init */
116 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RESET_INIT;
118 for(index = 0;index<PH_FRINFC_NDEFMAP_CR;index++)
120 /* Initialise the NdefMap Completion Routine to Null */
121 NdefMap->CompletionRoutine[index].CompletionRoutine = NULL;
122 /* Initialise the NdefMap Completion Routine context to Null */
123 NdefMap->CompletionRoutine[index].Context = NULL;
126 /* Lower Device(Always Overlapped HAL Struct initialised in application
127 is registred in NdefMap Lower Device) */
128 NdefMap->LowerDevice = LowerDevice;
130 /* Remote Device info received from Manual Device Discovery is registered here */
131 NdefMap->psRemoteDevInfo = psRemoteDevInfo;
133 /* Transfer Buffer registered */
134 NdefMap->ApduBuffer = TrxBuffer;
136 /* Set the MaxApduBufferSize */
137 NdefMap->ApduBufferSize = TrxBufferSize;
139 /* Set APDU Buffer Index */
140 NdefMap->ApduBuffIndex = 0;
142 /* Register Transfer Buffer Length */
143 NdefMap->SendLength = 0;
145 /* Register Receive Buffer */
146 NdefMap->SendRecvBuf = ReceiveBuffer;
148 /* Register Receive Buffer Length */
149 NdefMap->SendRecvLength = ReceiveLength;
151 /* Register Temporary Receive Buffer Length */
152 NdefMap->TempReceiveLength = *ReceiveLength;
154 /* Register Data Count variable and set it to zero */
155 NdefMap->DataCount = DataCount;
156 *NdefMap->DataCount = 0;
158 /* Reset the PageOffset */
161 /* Reset the NumOfBytesRead*/
162 NdefMap->NumOfBytesRead = 0;
164 /* Reset the NumOfBytesWritten*/
165 NdefMap->NumOfBytesWritten = 0;
167 /* Reset the Card Type */
168 NdefMap->CardType = 0;
170 /* Reset the Memory Card Size*/
171 NdefMap->CardMemSize = 0;
173 /* Reset the Previous Operation*/
174 NdefMap->PrevOperation = 0;
176 /* Reset the Desfire Operation Flag*/
177 NdefMap->DespOpFlag = 0;
179 /* Reset MapCompletion Info*/
180 NdefMap->MapCompletionInfo.CompletionRoutine = NULL;
181 NdefMap->MapCompletionInfo.Context = NULL;
183 /* Reset the ReadingForWriteOperation flag. */
184 NdefMap->ReadingForWriteOperation = 0; /* FALSE */
186 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
187 /*Reset Desfire Cap Container elements*/
188 phFriNfc_DesfCapCont_HReset(NdefMap);
189 #endif /* PH_FRINFC_MAP_DESFIRE_DISABLED */
191 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
192 /*Reset Mifare Standard Container elements*/
193 NdefMap->StdMifareContainer.DevInputParam = psDevInputParam;
194 status = phFriNfc_MifareStdMap_H_Reset(NdefMap);
195 #endif /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
197 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
198 /*Reset Felica Tag elements*/
199 NdefMap->FelicaPollDetails.DevInputParam = psDevInputParam;
200 phFriNfc_Felica_HReset(NdefMap);
201 #endif /* PH_FRINFC_MAP_FELICA_DISABLED */
203 #if !(defined(PH_FRINFC_MAP_TOPAZ_DISABLED ) || defined (PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED ))
205 phFriNfc_TopazMap_H_Reset(NdefMap);
206 #endif /* PH_FRINFC_MAP_TOPAZ_DISABLED || PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
209 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
210 status = phFriNfc_MifareUL_H_Reset(NdefMap);
213 #ifdef PHFRINFC_OVRHAL_MOCKUP
214 /*Reset Desfire Cap Container elements*/
215 phFriNfc_Mockup_H_Reset(NdefMap);
216 #endif /* PHFRINFC_OVRHAL_MOCKUP */
219 * Fix for PR - 0001256
222 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID;
229 * Registering the Completion Routine.
231 * This function requires the caller to set a Completion Routine
232 * which serves as notifier for the upper component.
233 * NOTE: Please refer the header file for more information.
237 NFCSTATUS phFriNfc_NdefMap_SetCompletionRoutine(phFriNfc_NdefMap_t *NdefMap,
239 pphFriNfc_Cr_t CompletionRoutine,
240 void *CompletionRoutineContext)
242 NFCSTATUS status = NFCSTATUS_SUCCESS;
244 if ( ( NdefMap == NULL ) || (FunctionID >= PH_FRINFC_NDEFMAP_CR) ||
245 ( CompletionRoutine == NULL) || (CompletionRoutineContext == NULL))
247 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
251 /* Register the application callback with the NdefMap Completion Routine */
252 NdefMap->CompletionRoutine[FunctionID].CompletionRoutine = CompletionRoutine;
254 /* Register the application context with the NdefMap Completion Routine context */
255 NdefMap->CompletionRoutine[FunctionID].Context = CompletionRoutineContext;
262 * Initiates Reading of NDEF information from the Remote Device.
264 * Remote Peer device may be of type any card. Ex: desfire,felica,jewel
265 * mifareUL,mifare 1K etc. The function initiates the reading of NDEF
266 * information from a Remote Device.
268 * This is the main NdefMap read function call.Internally,depending upon
269 * the CardType,respective mifare/desfire read functions are called.
270 * In future this can be extended to support any types of card.
272 * It performs a reset of the state and triggers/starts the raed action (state machine).
273 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once the action
274 * has been triggered.
276 * NOTE: Please refer the header file for more information.
280 NFCSTATUS phFriNfc_NdefMap_RdNdef( phFriNfc_NdefMap_t *NdefMap,
282 uint32_t *PacketDataLength,
285 NFCSTATUS status = NFCSTATUS_PENDING;
288 /* check for validity of input parameters*/
289 if (( PacketData == NULL )
290 || ( NdefMap == NULL )
291 || ( PacketDataLength == NULL )
292 || ( *PacketDataLength == 0 )
293 || ( ( Offset != PH_FRINFC_NDEFMAP_SEEK_CUR) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ))
294 || (NdefMap->CompletionRoutine->CompletionRoutine == NULL)
295 || (NdefMap->CompletionRoutine->Context == NULL )
298 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
300 else if (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID)
302 /* Card is in invalid state, cannot have any read/write
304 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
305 NFCSTATUS_INVALID_FORMAT);
307 else if(NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED)
309 /* Can't read any data from the card:TLV length is zero*/
310 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_EOF_NDEF_CONTAINER_REACHED);
312 NdefMap->NumOfBytesRead = PacketDataLength;
313 *NdefMap->NumOfBytesRead = 0;
317 else if ( (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ))
319 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
323 /* Check the offset given by the user
324 If the offset is 1 (SEEK_BEGIN), reset everything and start
325 reading from the first Page of the card.
326 else if offset is 0 (PH_FRINFC_NDEFMAP_SEEK_CUR), continue reading
327 No need to reset the parameters. */
329 if ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN )
331 NdefMap->ApduBuffIndex = 0;
332 *NdefMap->DataCount = 0;
335 if ( (NdefMap->CardType == PH_FRINFC_NDEFMAP_ISO14443_4A_CARD) &&
336 (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && (*NdefMap->DataCount == 0 ))
339 /* A READ operation cannot be done if the previuos operation was WRITE
340 unless the offset is set to PH_FRINFC_NDEFMAP_SEEK_BEGIN Or
341 Read Operation with Offset set to Continue & DataCount set to 0 */
342 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
346 switch ( NdefMap->CardType)
348 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
349 case PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
350 /* Mifare card selected. Call Mifare read */
351 status = phFriNfc_MifareUL_RdNdef ( NdefMap,
356 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
358 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
359 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
361 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1 :
362 #endif /* #ifdef DESFIRE_EV1 */
363 /* Desfire card selected. Call Desfire read */
364 status = phFriNfc_Desfire_RdNdef( NdefMap,
369 #endif /* PH_FRINFC_MAP_DESFIRE_DISABLED */
371 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
372 case PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
373 case PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
374 /* Mifare card selected. Call Mifare read */
375 status = phFriNfc_MifareStdMap_RdNdef ( NdefMap,
380 #endif /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
382 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
383 case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
384 /* Desfire card selected. Call Desfire Write */
385 status = phFriNfc_Felica_RdNdef( NdefMap,
390 #endif /* PH_FRINFC_MAP_FELICA_DISABLED */
392 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
393 case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
394 /* Topaz card selected. Call Topaz read */
395 status = phFriNfc_TopazMap_RdNdef( NdefMap,
400 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
401 case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
402 /* Topaz card selected. Call Topaz read */
403 status = phFriNfc_TopazDynamicMap_RdNdef( NdefMap,
408 #endif /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
409 #endif /* PH_FRINFC_MAP_TOPAZ_DISABLED */
411 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
412 case PH_FRINFC_NDEFMAP_ISO15693_CARD:
413 status = phFriNfc_ISO15693_RdNdef( NdefMap,
418 #endif /* #ifndef PH_FRINFC_MAP_ISO15693_DISABLED */
420 #ifdef PHFRINFC_OVRHAL_MOCKUP
421 case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
422 /* Mockup card selected. Call Mockup Write */
423 status = phFriNfc_Mockup_RdNdef( NdefMap,
428 #endif /* PHFRINFC_OVRHAL_MOCKUP */
431 /* Unknown card type. Return error */
432 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
433 NFCSTATUS_INVALID_REMOTE_DEVICE);
443 * Initiates Writing of NDEF information to the Remote Device.
445 * The function initiates the writing of NDEF information to a Remote Device
447 * Remote Peer device may be of type any card. Ex: desfire,felica,jewel
448 * mifareUL,mifare 1K etc. The function initiates the reading of NDEF
449 * information from a Remote Device.
451 * This is a main write api.Internally,depending upon the CardType,
452 * respective mifare/desfire write apis are called.In future this can be
453 * extended to support any types of card.
455 * It performs a reset of the state and starts the action (state machine).
456 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once
457 * the action has been triggered.
459 * NOTE: Please refer the header file for more information.
464 NFCSTATUS phFriNfc_NdefMap_WrNdef( phFriNfc_NdefMap_t *NdefMap,
466 uint32_t *PacketDataLength,
469 NFCSTATUS status = NFCSTATUS_PENDING;
472 if ( (PacketData == NULL)
473 || ( NdefMap == NULL )
475 || ( PacketDataLength == NULL )
477 || ( *PacketDataLength == 0 )
478 || ((Offset != PH_FRINFC_NDEFMAP_SEEK_CUR) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ))
479 || (NdefMap->CompletionRoutine->CompletionRoutine == NULL)
480 || (NdefMap->CompletionRoutine->Context == NULL)
483 /* Invalid input parameter error */
484 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
486 else if (( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) &&
487 (PH_FRINFC_NDEFMAP_TOPAZ_CARD != NdefMap->CardType) &&
488 (PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD != NdefMap->CardType))
490 /* Card is in invalid state, cannot have any read/write
492 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
493 NFCSTATUS_INVALID_FORMAT);
496 else if ( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY )
499 /*Can't write to the card :No Grants */
500 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
501 NFCSTATUS_NOT_ALLOWED);
503 /* set the no. bytes written is zero*/
504 NdefMap->WrNdefPacketLength = PacketDataLength;
505 *NdefMap->WrNdefPacketLength = 0;
510 /* Check the offset given by the user
511 If the offset is 1 (SEEK_BEGIN), reset everything and start
512 writing from the first Byte of the card.
513 else if offset is 0 (PH_FRINFC_NDEFMAP_SEEK_CUR), continue writing
514 No need to reset the parameters. */
515 if (( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) &&
516 (PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD == NdefMap->CardType))
518 /* If Topaz Dynamic card CC bytes are not valid then also allow writing,
519 If card is really good then writing will be done properly and reading can be performed,
520 otherwise writing or reading will fail. so, setting card state to
521 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_WRITE */
522 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_WRITE;
525 if ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN )
527 NdefMap->ApduBuffIndex = 0;
528 *NdefMap->DataCount = 0;
531 if ( (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_UL_CARD) ||
532 (NdefMap->CardType == PH_FRINFC_NDEFMAP_ISO14443_4A_CARD))
534 if (( (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_READ_OPE) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN )) ||
535 ( (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && (*NdefMap->DataCount == 0 )))
537 /* A WRITE operation cannot be done if the previuos operation was READ
538 unless the offset is set to PH_FRINFC_NDEFMAP_SEEK_BEGIN OR
539 Write Operation with Offset set to Continue & DataCount set to 0 */
540 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
546 NdefMap->WrNdefPacketLength = PacketDataLength;
547 switch ( NdefMap->CardType)
549 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
550 case PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
551 /* Mifare card selected. Call Mifare Write */
552 status = phFriNfc_MifareUL_WrNdef( NdefMap,
557 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
559 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
560 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
562 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1 :
563 #endif /* #ifdef DESFIRE_EV1 */
564 /* Desfire card selected. Call Desfire Write */
565 status = phFriNfc_Desfire_WrNdef( NdefMap,
570 #endif /* PH_FRINFC_MAP_DESFIRE_DISABLED */
572 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
573 case PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
574 case PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
575 /* Mifare card selected. Call Mifare read */
576 status = phFriNfc_MifareStdMap_WrNdef ( NdefMap,
581 #endif /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
583 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
584 case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
585 /* Desfire card selected. Call Desfire Write */
586 status = phFriNfc_Felica_WrNdef( NdefMap,
591 #endif /* PH_FRINFC_MAP_FELICA_DISABLED */
593 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
594 case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
595 /* Topaz card selected. Call Topaz Write */
596 status = phFriNfc_TopazMap_WrNdef( NdefMap,
601 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
602 case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
603 /* Topaz card selected. Call Topaz Write */
604 status = phFriNfc_TopazDynamicMap_WrNdef( NdefMap,
609 #endif /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
610 #endif /* PH_FRINFC_MAP_TOPAZ_DISABLED */
612 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
613 case PH_FRINFC_NDEFMAP_ISO15693_CARD:
614 status = phFriNfc_ISO15693_WrNdef( NdefMap,
619 #endif /* #ifndef PH_FRINFC_MAP_ISO15693_DISABLED */
622 #ifdef PHFRINFC_OVRHAL_MOCKUP
623 case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
624 /* Mockup card selected. Call Mockup Write */
625 status = phFriNfc_Mockup_WrNdef( NdefMap,
630 #endif /* PHFRINFC_OVRHAL_MOCKUP */
632 /* Unknown card type. Return error */
633 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
634 NFCSTATUS_INVALID_REMOTE_DEVICE);
642 #ifdef FRINFC_READONLY_NDEF
645 phFriNfc_NdefMap_ConvertToReadOnly (
646 phFriNfc_NdefMap_t *NdefMap)
648 NFCSTATUS result = NFCSTATUS_PENDING;
651 /* Check for ndefmap context and relevant state. Else return error*/
654 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
656 else if ((NdefMap->CompletionRoutine->CompletionRoutine == NULL)
657 || (NdefMap->CompletionRoutine->Context == NULL))
659 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
663 switch (NdefMap->CardType)
665 case PH_FRINFC_NDEFMAP_TOPAZ_CARD:
667 result = phFriNfc_TopazMap_ConvertToReadOnly (NdefMap);
671 case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD:
673 result = phFriNfc_TopazDynamicMap_ConvertToReadOnly (NdefMap);
677 case PH_FRINFC_NDEFMAP_ISO15693_CARD:
679 result = phFriNfc_ISO15693_ConvertToReadOnly (NdefMap);
685 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
686 NFCSTATUS_INVALID_REMOTE_DEVICE);
694 #endif /* #ifdef FRINFC_READONLY_NDEF */
697 * Check whether a particular Remote Device is NDEF compliant.
699 * The function initiates the ndef compliancy check.
701 * This is a main check ndef api.Internally,depending upon the different
702 * opmodes,respective mifare/desfire checkNdef apis are called.
703 * In future this can be extended to check any types of card ndef
706 * It performs a reset of the state and starts the action (state machine).
707 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once
708 * the action has been triggered.
710 * NOTE: Please refer the header file for more information.
714 NFCSTATUS phFriNfc_NdefMap_ChkNdef( phFriNfc_NdefMap_t *NdefMap)
716 NFCSTATUS status = NFCSTATUS_PENDING;
720 /* Check for ndefmap context and relevant state. Else return error*/
721 if ( NdefMap == NULL )
723 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
727 if( (NdefMap->State != PH_FRINFC_NDEFMAP_STATE_RESET_INIT) ||
728 (NdefMap->psRemoteDevInfo->SessionOpened != 0x01 ) )
729 /* Harsha: If SessionOpened is not 1, this means that connect has not happened */
731 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
733 else if ( (NdefMap->CompletionRoutine->CompletionRoutine == NULL) || (NdefMap->CompletionRoutine->Context == NULL ))
735 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
741 * CardInfo106 Replace with the ReaderA_Info.
744 sak = NdefMap->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
748 * Description: Opmode replace with RemDevType.
752 switch ( NdefMap->psRemoteDevInfo->RemDevType )
754 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
755 case phHal_eISO15693_PICC:
757 status = phFriNfc_ISO15693_ChkNdef (NdefMap);
760 #else /* #ifndef PH_FRINFC_MAP_ISO15693_DISABLED */
761 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
762 NFCSTATUS_INVALID_REMOTE_DEVICE);
763 #endif /* #ifndef PH_FRINFC_MAP_ISO15693_DISABLED */
765 case phHal_eMifare_PICC:
766 case phHal_eISO14443_3A_PICC:
767 /* Remote device is Mifare card . Check for Mifare
771 /* The SAK/Sel_Res says the card is of the type
773 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
774 status = phFriNfc_MifareUL_ChkNdef( NdefMap);
775 #else /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
776 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
777 NFCSTATUS_INVALID_REMOTE_DEVICE);
778 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
780 else if ((0x08 == (sak & 0x18)) ||
781 (0x18 == (sak & 0x18)))
783 /* The SAK/Sel_Res says the card is of the type
785 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
786 status = phFriNfc_MifareStdMap_ChkNdef( NdefMap);
787 #else /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
788 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
789 NFCSTATUS_INVALID_REMOTE_DEVICE);
790 #endif /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
794 /* Invalid Mifare UL card, as the remote device
795 info - opmode says its a Mifare UL card but,
796 The SAK/Sel_Res is wrong */
797 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
798 NFCSTATUS_INVALID_REMOTE_DEVICE);
801 case phHal_eISO14443_B_PICC:
803 status = phFriNfc_Desfire_ChkNdef(NdefMap);
806 case phHal_eISO14443_A_PICC :
807 /* Remote device is Desfire card . Check for Desfire
809 if(0x20 == (sak & 0x20))
811 /* The SAK/Sel_Res says the card is of the type
813 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
814 status = phFriNfc_Desfire_ChkNdef(NdefMap);
815 #else /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
816 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
817 NFCSTATUS_INVALID_REMOTE_DEVICE);
818 #endif /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
822 /* Invalid Desfire card, as the remote device
823 info - opmode says its a desfire card but,
824 The SAK/Sel_Res is wrong */
825 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
826 NFCSTATUS_INVALID_REMOTE_DEVICE);
830 case phHal_eFelica_PICC:
832 /*Set the OpMode Type Flag*/
833 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
834 #ifndef PH_HAL4_ENABLE
835 NdefMap->OpModeType[0] = phHal_eOpModesFelica212;
836 NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
837 #endif /* #ifndef PH_HAL4_ENABLE */
838 status = phFriNfc_Felica_ChkNdef(NdefMap);
839 #else /* PH_FRINFC_MAP_FELICA_DISABLED*/
840 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
841 NFCSTATUS_INVALID_REMOTE_DEVICE);
842 #endif /* PH_FRINFC_MAP_FELICA_DISABLED*/
846 #ifndef PH_HAL4_ENABLE
847 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
848 case phHal_eFelica424:
849 /*Set the OpMode Ttype Flag*/
850 NdefMap->OpModeType[0] = phHal_eOpModesFelica424;
851 NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
852 status = phFriNfc_Felica_ChkNdef(NdefMap);
853 #else /* PH_FRINFC_MAP_FELICA_DISABLED*/
854 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
855 NFCSTATUS_INVALID_REMOTE_DEVICE);
856 #endif /* PH_FRINFC_MAP_FELICA_DISABLED*/
860 case phHal_eJewel_PICC :
861 /* Remote device is Topaz card . Check for Topaz
863 #ifdef PH_HAL4_ENABLE
864 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
865 /* Decide on the Header bytes to know the
866 Type of the Topaz card.Card could be Static or
867 Dynamic type. These are of type NFFC-NDEF Data Application*/
868 if ( NdefMap->psRemoteDevInfo->RemoteDevInfo.Jewel_Info.HeaderRom0
869 == PH_FRINFC_TOPAZ_HEADROM0_VAL)
872 status = phFriNfc_TopazMap_ChkNdef(NdefMap);
874 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
875 else if( NdefMap->psRemoteDevInfo->RemoteDevInfo.Jewel_Info.HeaderRom0
876 == PH_FRINFC_TOPAZ_DYNAMIC_HEADROM0_VAL)
879 status = phFriNfc_TopazDynamicMap_ChkNdef(NdefMap);
885 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
886 NFCSTATUS_INVALID_REMOTE_DEVICE);
895 /* The SAK/Sel_Res says the card is of the type
897 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
898 status = phFriNfc_TopazMap_ChkNdef(NdefMap);
899 #else /* PH_FRINFC_MAP_TOPAZ_DISABLED*/
900 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
901 NFCSTATUS_INVALID_REMOTE_DEVICE);
902 #endif /* PH_FRINFC_MAP_TOPAZ_DISABLED*/
906 /* Invalid Topaz card, as the remote device
907 info - opmode says its a desfire card but,
908 The SAK/Sel_Res is wrong */
909 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
910 NFCSTATUS_INVALID_REMOTE_DEVICE);
915 #ifdef PHFRINFC_OVRHAL_MOCKUP
916 case phHal_eOpModesMockup :
917 /*Set the OpMode Ttype Flag*/
918 NdefMap->OpModeType[0] = phHal_eOpModesMockup;
919 NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
920 status = phFriNfc_Mockup_ChkNdef(NdefMap);
922 #endif /* PHFRINFC_OVRHAL_MOCKUP */
925 /* Remote device is not recognised.
926 Probably not NDEF compliant */
927 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
928 NFCSTATUS_INVALID_REMOTE_DEVICE);
938 * \brief Completion Routine, Processing function, needed to avoid long blocking.
939 * \note The lower (Overlapped HAL) layer must register a pointer to this function as a Completion
940 * Routine in order to be able to notify the component that an I/O has finished and data are
941 * ready to be processed.
942 * This is a main Ndef Map Process api.Internally,depending upon the different
943 * CardTypes,respective mifare/desfire process functions are called.
947 void phFriNfc_NdefMap_Process( void *Context,
951 if ( Context != NULL )
953 phFriNfc_NdefMap_t *NdefMap = (phFriNfc_NdefMap_t *)Context;
956 * Description: Opmode replace with RevDevTyp.
959 switch ( NdefMap->psRemoteDevInfo->RemDevType )
961 case phHal_eMifare_PICC :
962 case phHal_eISO14443_3A_PICC:
964 if((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD) ||
965 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD))
967 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
968 /* Remote device is Mifare Standard card */
969 phFriNfc_MifareStdMap_Process(NdefMap,Status);
970 #else /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
971 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
972 NFCSTATUS_INVALID_REMOTE_DEVICE);
973 #endif /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
977 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
978 /* Remote device is Mifare UL card */
979 phFriNfc_MifareUL_Process(NdefMap,Status);
980 #else /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
981 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
982 NFCSTATUS_INVALID_REMOTE_DEVICE);
983 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
987 case phHal_eISO14443_A_PICC :
988 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
989 /* Remote device is Desfire card */
990 phFriNfc_Desfire_Process(NdefMap, Status);
991 #else /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
992 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
993 NFCSTATUS_INVALID_REMOTE_DEVICE);
994 #endif /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
996 case phHal_eISO14443_B_PICC:
997 /* Remote device is Desfire card */
998 phFriNfc_Desfire_Process(NdefMap, Status);
1001 case phHal_eFelica_PICC :
1002 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1003 /* Remote device is Felica Smart card */
1004 phFriNfc_Felica_Process(NdefMap, Status);
1005 #else /* PH_FRINFC_MAP_FELICA_DISABLED*/
1006 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1007 NFCSTATUS_INVALID_REMOTE_DEVICE);
1008 #endif /* PH_FRINFC_MAP_FELICA_DISABLED*/
1011 case phHal_eJewel_PICC:
1012 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
1013 if ( NdefMap->CardType == PH_FRINFC_NDEFMAP_TOPAZ_CARD )
1015 /* Remote device is Topaz Smart card */
1016 phFriNfc_TopazMap_Process(NdefMap, Status);
1018 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
1019 else if ( NdefMap->CardType == PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD )
1021 /* Remote device is Topaz Smart card */
1022 phFriNfc_TopazDynamicMap_Process(NdefMap, Status);
1026 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1027 NFCSTATUS_INVALID_REMOTE_DEVICE);
1031 #endif /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED*/
1035 #ifdef PHFRINFC_OVRHAL_MOCKUP
1036 case phHal_eOpModesMockup:
1037 /* Remote device is Desfire card */
1038 phFriNfc_Mockup_Process(NdefMap, Status);
1040 #endif /* PHFRINFC_OVRHAL_MOCKUP*/
1042 /* Remote device opmode not recognised.
1043 Probably not NDEF compliant */
1044 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1045 NFCSTATUS_INVALID_REMOTE_DEVICE);
1046 /* set the state back to the Reset_Init state*/
1047 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RESET_INIT;
1049 /* set the completion routine*/
1050 NdefMap->CompletionRoutine[PH_FRINFC_NDEFMAP_CR_INVALID_OPE].
1051 CompletionRoutine(NdefMap->CompletionRoutine->Context, Status);
1057 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
1058 NFCSTATUS_INVALID_PARAMETER);
1059 /* The control should not come here. As Context itself is NULL ,
1066 NFCSTATUS phFriNfc_ChkAndParseTLV(phFriNfc_NdefMap_t *NdefMap)
1068 NFCSTATUS status = NFCSTATUS_PENDING;
1070 switch ( NdefMap->CardType )
1072 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
1073 case PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
1077 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
1079 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
1080 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
1081 status = phFriNfc_Desf_ChkAndParseTLV(NdefMap);
1085 #endif /* PH_FRINFC_MAP_DESFIRE_DISABLED */
1087 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
1088 case PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
1089 case PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
1092 #endif /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
1094 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1095 case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
1098 #endif /* PH_FRINFC_MAP_FELICA_DISABLED */
1101 /* Unknown card type. Return error */
1102 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
1103 NFCSTATUS_INVALID_REMOTE_DEVICE);
1113 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
1114 static void phFriNfc_DesfCapCont_HReset(phFriNfc_NdefMap_t *NdefMap)
1116 /* Initialise/reset the desfire capability contatiner structure variables*/
1117 NdefMap->DesfireCapContainer.DesfVersion = 0;
1118 NdefMap->DesfireCapContainer.NdefMsgFid = 0;
1119 NdefMap->DesfireCapContainer.NdefFileSize = 0;
1120 NdefMap->DesfireCapContainer.MaxCmdSize = 0;
1121 NdefMap->DesfireCapContainer.MaxRespSize = 0;
1122 NdefMap->DesfireCapContainer.ReadAccess = 0;
1123 NdefMap->DesfireCapContainer.WriteAccess = 0;
1125 #endif /* PH_FRINFC_MAP_DESFIRE_DISABLED */
1127 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1128 static void phFriNfc_Felica_HReset(phFriNfc_NdefMap_t *NdefMap)
1132 /* Initialise/reset the different felica structure variables*/
1133 /* Reset all the felica Basic staruture variables*/
1134 NdefMap->Felica.CurBlockNo = 0;
1136 for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_BLOCK_SIZE;index++)
1138 NdefMap->Felica.Rd_BytesToCopyBuff[index] = 0;
1139 NdefMap->Felica.Wr_RemainedBytesBuff[index] = 0;
1141 NdefMap->Felica.Rd_NoBytesToCopy = 0;
1142 NdefMap->Felica.Wr_BytesRemained = 0;
1145 /* Reset all the felica attribute information staruture variables*/
1146 for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_ATTR_NDEF_DATA_LEN;index++)
1148 NdefMap->FelicaAttrInfo.LenBytes[index] = 0;
1151 NdefMap->FelicaAttrInfo.Nmaxb = 0;
1152 NdefMap->FelicaAttrInfo.Nbr = 0;
1153 NdefMap->FelicaAttrInfo.Nbw= 0;
1154 NdefMap->FelicaAttrInfo.RdWrFlag = 0;
1155 NdefMap->FelicaAttrInfo.WriteFlag = 0;
1156 NdefMap->Felica.CurrBytesRead=0;
1158 /* Reset all the felica manufacture details staruture variables*/
1159 for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_MANUF_ID_DATA_LEN;index++)
1161 NdefMap->FelicaManufDetails.ManufID[index] = 0;
1162 NdefMap->FelicaManufDetails.ManufParameter[index] = 0;
1164 NdefMap->Felica.NoBlocksWritten=0;
1166 #endif /* PH_FRINFC_MAP_FELICA_DISABLED */
1168 NFCSTATUS phFriNfc_NdefMap_EraseNdef(phFriNfc_NdefMap_t *NdefMap)
1170 NFCSTATUS status = NFCSTATUS_PENDING;
1172 static uint8_t PktData[3] = PH_FRINFC_NDEFMAP_EMPTY_NDEF_MSG;
1173 uint8_t MemOffset = PH_FRINFC_NDEFMAP_SEEK_BEGIN;
1174 uint32_t PacketDataLength = sizeof(PktData);
1176 if (NdefMap == NULL )
1178 /* Invalid input parameter error */
1179 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
1183 switch ( NdefMap->CardType)
1185 #ifdef PHFRINFC_OVRHAL_MOCKUP
1186 case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
1187 #endif /* PHFRINFC_OVRHAL_MOCKUP */
1188 case PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
1189 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
1191 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1 :
1192 #endif /* #ifdef DESFIRE_EV1 */
1193 case PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
1194 case PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
1195 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
1196 case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
1197 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
1198 case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
1200 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
1201 case PH_FRINFC_NDEFMAP_ISO15693_CARD:
1204 /* Mifare card selected. Call Mifare Write */
1205 status = phFriNfc_NdefMap_WrNdef( NdefMap,
1211 case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
1213 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1214 /* Felica card selected. Call to write EMPTY NDEF Msg */
1215 status = phFriNfc_Felica_EraseNdef( NdefMap );
1216 #else /* PH_FRINFC_MAP_FELICA_DISABLED*/
1217 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1218 NFCSTATUS_INVALID_REMOTE_DEVICE);
1219 #endif /* PH_FRINFC_MAP_FELICA_DISABLED*/
1223 /* Unknown card type. Return error */
1224 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1225 NFCSTATUS_INVALID_REMOTE_DEVICE);
1231 /* Harsha: Fix for the mantis entry 0000420: NDEF_MAP: Size of NDEF data:
1232 no abstracted way for user to know how many bytes to read/write */
1235 * \brief Helper API, exposed to the user to enable him to know the size
1236 * of the NDEF data that he can write in to the card.
1238 NFCSTATUS phFriNfc_NdefMap_GetContainerSize(const phFriNfc_NdefMap_t *NdefMap,uint32_t *maxSize, uint32_t *actualSize)
1240 NFCSTATUS result = NFCSTATUS_SUCCESS;
1241 uint8_t sect_index = 0;
1242 uint8_t actual_sect_index = 0;
1243 uint8_t count_index = 0;
1245 if( (NdefMap == NULL) || (maxSize == NULL) || (actualSize == NULL))
1247 /* Invalid input parameter error */
1248 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
1253 switch(NdefMap->CardType)
1255 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
1256 case PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
1257 /* Mifare UL card */
1258 /* The integration needs to ensure that the checkNdef
1259 function has been called before calling this function,
1260 otherwise NdefMap->CardMemSize will be 0 */
1261 *maxSize = NdefMap->MifareULContainer.RemainingSize;
1262 /* In Mifare UL card, the actual size is the length field
1264 *actualSize = NdefMap->TLVStruct.ActualSize;
1266 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
1268 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
1269 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
1271 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1 :
1272 #endif /* #ifdef DESFIRE_EV1 */
1274 /* The integration needs to ensure that the checkNdef
1275 function has been called before calling this function,
1276 otherwise NdefMap->DesfireCapContainer.NdefFileSize
1278 /* -2 bytes represents the size field bytes*/
1279 *maxSize = NdefMap->DesfireCapContainer.NdefFileSize - 2;
1280 /* In Desfire card, the actual size cant be calculated so
1281 the actual size is given as 0xFFFFFFFF */
1282 *actualSize = NdefMap->DesfireCapContainer.NdefDataLen;
1284 #endif /* PH_FRINFC_MAP_DESFIRE_DISABLED */
1286 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
1287 case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
1289 /* The integration needs to ensure that the checkNdef
1290 function has been called before calling this function,
1291 otherwise NdefMap->CardMemSize will be 0 */
1292 *maxSize = NdefMap->TopazContainer.RemainingSize;
1293 /* In Topaz card, the actual size is the length field value of the
1295 *actualSize = NdefMap->TLVStruct.BytesRemainLinTLV;
1297 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
1298 case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
1299 /* Topaz 512 card */
1300 /* The integration needs to ensure that the checkNdef
1301 function has been called before calling this function,
1302 otherwise NdefMap->CardMemSize will be 0 */
1303 *maxSize = NdefMap->TopazContainer.NDEFRWSize;
1304 /* In Topaz card, the actual size is the length field value of the
1306 *actualSize = NdefMap->TopazContainer.ActualNDEFMsgSize;
1309 #endif /* PH_FRINFC_MAP_TOPAZ_DISABLED */
1310 #endif /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
1311 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
1312 case PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
1313 case PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
1314 /* Mifare std card */
1316 /* Max size is the number of NDEF compliant blocks in the card
1317 multiplied by 16 bytes */
1318 #ifndef PH_HAL4_ENABLE
1320 *maxSize = NdefMap->StdMifareContainer.remainingSize;
1322 #else /* #ifndef PH_HAL4_ENABLE */
1324 while ((PH_FRINFC_MIFARESTD_NDEF_COMP ==
1325 NdefMap->StdMifareContainer.aid[count_index]) &&
1327 PH_FRINFC_NDEFMAP_MIFARESTD_TOTALNO_BLK))
1329 actual_sect_index++;
1332 /* Total number of sectors in 1k = 16 (0 to 15, MAD sector number = 0)
1333 Total number of sectors in 4k = 40 (0 to 39,
1334 MAD sector number = 0 and 16, After block number 31, each sector
1336 Every last block of the sector is the sector header, So the blocks
1337 that can be read or written in each sector is always
1338 (number of blocks in each sector - 1)
1339 No of blocks in the one sector till the sector number 0 to 31
1340 (total 32 sectors) =
1341 4 blocks, So blocks that can be read/write = (4 - 1 = 3 blocks)
1342 No of blocks in the one sector after the sector number 31 to 39 =
1343 16 blocks, So blocks that can be read/write = (16 - 1 = 15 blocks)
1344 Each block has 16 bytes
1345 To calculate number of bytes in the sector, depending on the number
1346 of blocks multiplied by 16
1348 if (PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD ==
1351 if (actual_sect_index > 32)
1353 sect_index = (actual_sect_index - 32);
1354 /* Here, 30 is used because block number 0 and 16 are MAD blocks
1355 which cannot be used for reading and writing data
1356 3 and 15 are blocks in each sector which can be read/write
1357 3 indicates the sector is in between (0 and 31)
1358 15 indicates the sector is in between (32 to 39)
1359 16 is always number of bytes in each block
1360 4 is used because each NDEF write has to write using the
1361 TLV format and T & L takes 4 bytes length and V is the
1364 *maxSize = (((30 * (16 * 3)) + (sect_index * (16 * 15))) - 4);
1366 else if (actual_sect_index <= 16)
1368 *maxSize = (((actual_sect_index - 1) * (16 * 3)) - 4);
1372 *maxSize = (((actual_sect_index - 2) * (16 * 3)) - 4);
1377 /* Here, 16 is always number of bytes in each block
1378 3 indicates the sector is in between (0 and 31) */
1379 if (actual_sect_index > NdefMap->StdMifareContainer.SectorIndex)
1381 actual_sect_index = NdefMap->StdMifareContainer.SectorIndex;
1383 *maxSize = (((actual_sect_index - 1) * (16 * 3)) - 4);
1386 #endif /* #ifndef PH_HAL4_ENABLE */
1387 *actualSize = NdefMap->TLVStruct.BytesRemainLinTLV;
1390 #endif /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
1392 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1393 case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
1396 *maxSize = NdefMap->FelicaAttrInfo.Nmaxb * 0x10;
1398 /* In Felica Card, actual size is calculated using the Length Bytes */
1399 *actualSize = NdefMap->FelicaAttrInfo.LenBytes[0];
1400 *actualSize = *actualSize << 16;
1401 *actualSize += NdefMap->FelicaAttrInfo.LenBytes[1];
1402 *actualSize = *actualSize << 8;
1403 *actualSize += NdefMap->FelicaAttrInfo.LenBytes[2];
1405 #endif /* PH_FRINFC_MAP_FELICA_DISABLED */
1407 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
1408 case PH_FRINFC_NDEFMAP_ISO15693_CARD:
1411 uint16_t block_no = 0;
1412 uint8_t byte_no = 0;
1414 block_no = (uint16_t)
1415 ISO15693_GET_VALUE_FIELD_BLOCK_NO (
1416 NdefMap->ISO15693Container.ndef_tlv_type_blk,
1417 NdefMap->ISO15693Container.ndef_tlv_type_byte,
1418 NdefMap->ISO15693Container.actual_ndef_size);
1420 ISO15693_GET_VALUE_FIELD_BYTE_NO (
1421 NdefMap->ISO15693Container.ndef_tlv_type_blk,
1422 NdefMap->ISO15693Container.ndef_tlv_type_byte,
1423 NdefMap->ISO15693Container.actual_ndef_size);
1425 *maxSize = (NdefMap->ISO15693Container.max_data_size -
1426 ((block_no * ISO15693_BYTES_PER_BLOCK) + byte_no));
1428 /* 2 is used to exclude the T and L part of the TLV */
1429 *maxSize = (NdefMap->ISO15693Container.max_data_size
1430 - ISO15693_BYTES_PER_BLOCK - 2);
1432 *actualSize = NdefMap->ISO15693Container.actual_ndef_size;
1437 #ifdef PHFRINFC_OVRHAL_MOCKUP
1438 case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
1439 *maxSize = 0xFFFFFFFF;
1440 /* In Desfire card, the actual size cant be calculated so
1441 the actual size is given as 0xFFFFFFFF */
1442 *actualSize = 0xFFFFFFFF;
1444 #endif /* PHFRINFC_OVRHAL_MOCKUP */
1447 /* Unknown card type. Return error */
1448 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
1449 NFCSTATUS_INVALID_REMOTE_DEVICE);
1456 #ifdef PHFRINFC_OVRHAL_MOCKUP
1457 NFCSTATUS phFriNfc_NdefMap_MockupCardSetter(phFriNfc_NdefMap_t *NdefMap,
1459 uint32_t NdefActualSize,
1460 uint32_t NdefMaxSize,
1463 NFCSTATUS Status = NFCSTATUS_SUCCESS;
1464 // First check all parameters
1465 if((NdefData != NULL) && (NdefMap != NULL))
1468 NdefMap->MochupContainer.NdefData = NdefData;
1469 NdefMap->MochupContainer.NdefActualSize = NdefActualSize;
1470 NdefMap->MochupContainer.NdefMaxSize = NdefMaxSize;
1471 NdefMap->MochupContainer.CardSize = CardSize;
1472 NdefMap->MochupContainer.CurrentBlock = 0;
1476 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
1481 NFCSTATUS phFriNfc_NdefMap_MockupNDefModeEn(uint8_t *pNdefCompliancy, uint8_t *pCardType, uint8_t Enable)
1483 *pNdefCompliancy = Enable;
1484 *pCardType = PH_FRINFC_NDEFMAP_MOCKUP_CARD;
1485 return NFCSTATUS_SUCCESS;
1487 #endif /* PHFRINFC_OVRHAL_MOCKUP */