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 phFriNfc_SmtCrdFmt.c
19 * \brief This component encapsulates different smart and simple tag formatting functionalities,
20 * for the mapping layer.
24 * $Date: Mon Dec 13 14:14:13 2010 $
31 #ifndef PH_FRINFC_CARD_FORMAT_DISABLED
33 #include <phNfcTypes.h>
34 #include <phFriNfc_OvrHal.h>
35 #include <phFriNfc_SmtCrdFmt.h>
37 #include <phFriNfc_TopazFormat.h>
38 #endif /* #ifdef DISABLE_FORMAT */
39 #include <phFriNfc_MifULFormat.h>
40 #include <phFriNfc_DesfireFormat.h>
41 #include <phFriNfc_MifStdFormat.h>
42 #ifndef PH_FRINFC_FMT_ISO15693_DISABLED
43 #include <phFriNfc_ISO15693Format.h>
44 #endif /* #ifndef PH_FRINFC_FMT_ISO15693_DISABLED */
47 /*! \ingroup grp_file_attributes
50 * File: \ref phFriNfc_CardFormatFunctions.c
60 void phFriNfc_SmtCrdFmt_HCrHandler(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt,
63 /* set the state back to the Reset_Init state*/
64 NdefSmtCrdFmt->State = PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT;
66 /* set the completion routine*/
67 NdefSmtCrdFmt->CompletionRoutine[PH_FRINFC_SMTCRDFMT_CR_FORMAT].
68 CompletionRoutine(NdefSmtCrdFmt->CompletionRoutine->Context, Status);
72 * \brief Used to Reset the context variables , before the actual smart card formatting
76 NFCSTATUS phFriNfc_NdefSmtCrd_Reset(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt,
78 phHal_sRemoteDevInformation_t *psRemoteDevInfo,
79 phHal_sDevInputParam_t *psDevInputParam,
80 uint8_t *SendRecvBuffer,
81 uint16_t *SendRecvBuffLen)
83 NFCSTATUS result = NFCSTATUS_SUCCESS;
86 if ( (SendRecvBuffLen == NULL) || (NdefSmtCrdFmt == NULL) || (psRemoteDevInfo == NULL) ||
87 (SendRecvBuffer == NULL) || (LowerDevice == NULL) ||
88 (*SendRecvBuffLen == 0) || (psDevInputParam == NULL) ||
89 (*SendRecvBuffLen < PH_FRINFC_SMTCRDFMT_MAX_SEND_RECV_BUF_SIZE) )
91 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT, NFCSTATUS_INVALID_PARAMETER);
95 /* Initialise the state to Init */
96 NdefSmtCrdFmt->State = PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT;
98 for(index = 0;index<PH_FRINFC_SMTCRDFMT_CR;index++)
100 /* Initialise the NdefMap Completion Routine to Null */
101 NdefSmtCrdFmt->CompletionRoutine[index].CompletionRoutine = NULL;
102 /* Initialise the NdefMap Completion Routine context to Null */
103 NdefSmtCrdFmt->CompletionRoutine[index].Context = NULL;
106 /* Lower Device(Always Overlapped HAL Struct initialised in application
107 is registred in NdefMap Lower Device) */
108 NdefSmtCrdFmt->LowerDevice = LowerDevice;
110 /* Remote Device info received from Manual Device Discovery is registered here */
111 NdefSmtCrdFmt->psRemoteDevInfo = psRemoteDevInfo;
113 /* Trx Buffer registered */
114 NdefSmtCrdFmt->SendRecvBuf = SendRecvBuffer;
116 /* Trx Buffer Size */
117 NdefSmtCrdFmt->SendRecvLength = SendRecvBuffLen;
119 /* Register Transfer Buffer Length */
120 NdefSmtCrdFmt->SendLength = 0;
122 /* Initialise the Format status flag*/
123 NdefSmtCrdFmt->FmtProcStatus = 0;
125 /* Reset the Card Type */
126 NdefSmtCrdFmt->CardType = 0;
128 /* Reset MapCompletion Info*/
129 NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.CompletionRoutine = NULL;
130 NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.Context = NULL;
132 #ifndef PH_FRINFC_FMT_TOPAZ_DISABLED
133 phFriNfc_Topaz_Reset(NdefSmtCrdFmt);
135 #endif /* PH_FRINFC_FMT_TOPAZ_DISABLED */
137 #ifndef PH_FRINFC_FMT_DESFIRE_DISABLED
138 /*Reset Desfire Cap Container elements*/
139 phFriNfc_Desfire_Reset(NdefSmtCrdFmt);
140 #endif /* PH_FRINFC_FMT_DESFIRE_DISABLED */
142 #ifndef PH_FRINFC_FMT_MIFARESTD_DISABLED
143 /*Reset Mifare Standard Container elements*/
144 NdefSmtCrdFmt->AddInfo.MfStdInfo.DevInputParam = psDevInputParam;
145 phFriNfc_MfStd_Reset(NdefSmtCrdFmt);
146 #endif /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
148 #ifndef PH_FRINFC_FMT_MIFAREUL_DISABLED
149 phFriNfc_MfUL_Reset(NdefSmtCrdFmt);
150 #endif /* #ifndef PH_FRINFC_FMT_MIFAREUL_DISABLED */
152 #ifndef PH_FRINFC_FMT_ISO15693_DISABLED
153 phFriNfc_ISO15693_FmtReset (NdefSmtCrdFmt);
154 #endif /* #ifndef PH_FRINFC_FMT_ISO15693_DISABLED */
156 #ifdef PHFRINFC_OVRHAL_MOCKUP
157 /*Reset Desfire Cap Container elements*/
158 // phFriNfc_Mockup_H_Reset(NdefSmtCrdFmt);
159 #endif /* PHFRINFC_OVRHAL_MOCKUP */
167 * \brief Completion Routine initialisation
170 NFCSTATUS phFriNfc_NdefSmtCrd_SetCR(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt,
172 pphFriNfc_Cr_t CompletionRoutine,
173 void *CompletionRoutineContext)
175 NFCSTATUS status = NFCSTATUS_SUCCESS;
177 if ((NdefSmtCrdFmt == NULL) || (FunctionID >= PH_FRINFC_SMTCRDFMT_CR) ||
178 (CompletionRoutine == NULL) || (CompletionRoutineContext == NULL))
180 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT, NFCSTATUS_INVALID_PARAMETER);
184 /* Register the application callback with the NdefMap Completion Routine */
185 NdefSmtCrdFmt->CompletionRoutine[FunctionID].CompletionRoutine = CompletionRoutine;
187 /* Register the application context with the NdefMap Completion Routine context */
188 NdefSmtCrdFmt->CompletionRoutine[FunctionID].Context = CompletionRoutineContext;
194 #ifdef FRINFC_READONLY_NDEF
197 phFriNfc_NdefSmtCrd_ConvertToReadOnly (
198 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
200 NFCSTATUS result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
201 NFCSTATUS_INVALID_PARAMETER);
204 if((NdefSmtCrdFmt != NULL)
205 && (NdefSmtCrdFmt->CompletionRoutine->CompletionRoutine != NULL)
206 && (NdefSmtCrdFmt->CompletionRoutine->Context != NULL))
208 sak = NdefSmtCrdFmt->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
209 switch (NdefSmtCrdFmt->psRemoteDevInfo->RemDevType)
211 case phHal_eMifare_PICC:
215 result = phFriNfc_MfUL_ConvertToReadOnly (NdefSmtCrdFmt);
219 /* MIFARE classic 1k/4k is not supported */
220 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
221 NFCSTATUS_INVALID_REMOTE_DEVICE);
226 case phHal_eISO14443_A_PICC:
228 result = phFriNfc_Desfire_ConvertToReadOnly (NdefSmtCrdFmt);
234 /* Remote device is not recognised.
235 Probably not NDEF compliant */
236 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
237 NFCSTATUS_INVALID_REMOTE_DEVICE);
245 #endif /* #ifdef FRINFC_READONLY_NDEF */
249 * \brief Used to format the different smart cards.
252 NFCSTATUS phFriNfc_NdefSmtCrd_Format( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt, const uint8_t *ScrtKeyB )
254 /* Component ID needs to be changed */
255 NFCSTATUS Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
256 NFCSTATUS_INVALID_PARAMETER);
259 /* Check for the correct context structure */
260 if((NdefSmtCrdFmt != NULL) &&
261 (NdefSmtCrdFmt->CompletionRoutine->CompletionRoutine != NULL) &&
262 (NdefSmtCrdFmt->CompletionRoutine->Context != NULL))
264 #ifdef PH_HAL4_ENABLE
265 /* SAK (Select response) */
266 sak = NdefSmtCrdFmt->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
268 /* Depending on the Opmodes, call the respective card functions */
269 switch ( NdefSmtCrdFmt->psRemoteDevInfo->RemDevType )
271 /* SAK (Select response) */
272 sak = NdefSmtCrdFmt->psRemoteDevInfo->RemoteDevInfo.CardInfo106.
275 /* Depending on the Opmodes, call the respective card functions */
276 switch ( NdefSmtCrdFmt->psRemoteDevInfo->OpMode )
277 #endif /* #ifdef PH_HAL4_ENABLE */
279 #ifdef PH_HAL4_ENABLE
280 case phHal_eMifare_PICC :
282 case phHal_eOpModesMifare :
283 #endif /* #ifdef PH_HAL4_ENABLE */
284 /* Remote device is Mifare card . Check for Mifare
288 #ifndef PH_FRINFC_FMT_MIFAREUL_DISABLED
289 /* The SAK/Sel_Res says the card is of the type
291 NdefSmtCrdFmt->CardType = PH_FRINFC_SMTCRDFMT_MIFARE_UL_CARD;
292 if (NdefSmtCrdFmt->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.UidLength == 7 &&
293 NdefSmtCrdFmt->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Uid[0] == 0x04)
296 Result = phFriNfc_MfUL_Format( NdefSmtCrdFmt);
300 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
301 NFCSTATUS_INVALID_REMOTE_DEVICE);
304 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
305 NFCSTATUS_INVALID_REMOTE_DEVICE);
306 #endif /* #ifndef PH_FRINFC_FMT_MIFAREUL_DISABLED */
308 else if((0x08 == (sak & 0x18)) ||
309 (0x18 == (sak & 0x18)))
311 #ifndef PH_FRINFC_FMT_MIFARESTD_DISABLED
312 NdefSmtCrdFmt->CardType = (uint8_t)
313 (((sak & 0x18) == 0x08)?
314 PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD:
315 PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD);
317 /* The SAK/Sel_Res says the card is of the type
319 Result = phFriNfc_MfStd_Format( NdefSmtCrdFmt, ScrtKeyB);
321 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
322 NFCSTATUS_INVALID_REMOTE_DEVICE);
323 #endif /* #ifndef PH_FRINFC_FMT_MIFARESTD_DISABLED */
327 /* Invalid Mifare card, as the remote device
328 info - opmode says its a Mifare card but,
329 The SAK/Sel_Res is wrong */
330 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
331 NFCSTATUS_INVALID_REMOTE_DEVICE);
334 #ifdef PH_HAL4_ENABLE
335 case phHal_eISO14443_A_PICC :
337 case phHal_eOpModesISO14443_4A :
338 #endif /* #ifdef PH_HAL4_ENABLE */
339 /* Remote device is Desfire card . Check for Desfire
341 if(0x20 == (sak & 0xFF))
343 #ifndef PH_FRINFC_FMT_DESFIRE_DISABLED
344 NdefSmtCrdFmt->CardType = PH_FRINFC_SMTCRDFMT_ISO14443_4A_CARD;
345 /* The SAK/Sel_Res says the card is of the type
348 Result = phFriNfc_Desfire_Format(NdefSmtCrdFmt);
350 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
351 NFCSTATUS_INVALID_REMOTE_DEVICE);
352 #endif /* #ifndef PH_FRINFC_FMT_DESFIRE_DISABLED */
356 /* Invalid Desfire card, as the remote device
357 info - opmode says its a desfire card but,
358 The SAK/Sel_Res is wrong */
359 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
360 NFCSTATUS_INVALID_REMOTE_DEVICE);
363 #ifdef PH_HAL4_ENABLE
364 case phHal_eJewel_PICC :
366 case phHal_eOpModesJewel :
367 #endif /* #ifdef PH_HAL4_ENABLE */
368 /* Remote device is Topaz card . Check for Topaz
372 #ifndef PH_FRINFC_FMT_TOPAZ_DISABLED
373 NdefSmtCrdFmt->CardType = PH_FRINFC_SMTCRDFMT_TOPAZ_CARD;
374 /* The SAK/Sel_Res says the card is of the type
376 Result = phFriNfc_Topaz_Format(NdefSmtCrdFmt);
378 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
379 NFCSTATUS_INVALID_REMOTE_DEVICE);
380 #endif /* #ifndef PH_FRINFC_FMT_TOPAZ_DISABLED */
385 /* Invalid Topaz card, as the remote device
386 info - opmode says its a desfire card but,
387 The SAK/Sel_Res is wrong */
388 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
389 NFCSTATUS_INVALID_REMOTE_DEVICE);
393 #ifdef PHFRINFC_OVRHAL_MOCKUP
394 case phHal_eOpModesMockup :
395 /*Set the OpMode Ttype Flag*/
396 NdefSmtCrdFmt->OpModeType[0] = phHal_eOpModesMockup;
397 NdefSmtCrdFmt->OpModeType[1] = phHal_eOpModesArrayTerminator;
398 //Result = phFriNfc_Mockup_ChkNdef(NdefSmtCrdFmt);
400 #endif /* PHFRINFC_OVRHAL_MOCKUP */
402 #ifndef PH_FRINFC_FMT_ISO15693_DISABLED
403 case phHal_eISO15693_PICC:
405 Result = phFriNfc_ISO15693_Format (NdefSmtCrdFmt);
408 #endif /* #ifndef PH_FRINFC_FMT_ISO15693_DISABLED */
410 /* Remote device is not recognised.
411 Probably not NDEF compliant */
412 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
413 NFCSTATUS_INVALID_REMOTE_DEVICE);
420 * \brief Handles different request and responses from the integration layer.
423 void phFriNfc_NdefSmtCrd_Process(void *Context,
426 if ( Context != NULL )
428 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt = (phFriNfc_sNdefSmtCrdFmt_t *)Context;
429 #ifdef PH_HAL4_ENABLE
430 switch ( NdefSmtCrdFmt->psRemoteDevInfo->RemDevType )
432 switch ( NdefSmtCrdFmt->psRemoteDevInfo->OpMode )
433 #endif /* #ifdef PH_HAL4_ENABLE */
435 #ifdef PH_HAL4_ENABLE
436 case phHal_eMifare_PICC :
438 case phHal_eOpModesMifare :
439 #endif /* #ifdef PH_HAL4_ENABLE */
440 if((NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD) ||
441 (NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD))
443 #ifndef PH_FRINFC_FMT_MIFARESTD_DISABLED
444 /* Remote device is Mifare Standard card */
445 phFriNfc_MfStd_Process(NdefSmtCrdFmt,Status);
447 #else /* PH_FRINFC_FMT_MIFARESTD_DISABLED*/
448 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
449 NFCSTATUS_INVALID_REMOTE_DEVICE);
450 #endif /* PH_FRINFC_FMT_MIFARESTD_DISABLED*/
454 #ifndef PH_FRINFC_FMT_MIFAREUL_DISABLED
455 /* Remote device is Mifare UL card */
456 phFriNfc_MfUL_Process(NdefSmtCrdFmt,Status);
457 #else /* PH_FRINFC_FMT_MIFAREUL_DISABLED*/
458 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
459 NFCSTATUS_INVALID_REMOTE_DEVICE);
460 #endif /* PH_FRINFC_FMT_MIFAREUL_DISABLED*/
464 #ifdef PH_HAL4_ENABLE
465 case phHal_eISO14443_A_PICC :
467 case phHal_eOpModesISO14443_4A :
468 #endif /* #ifdef PH_HAL4_ENABLE */
469 #ifndef PH_FRINFC_FMT_DESFIRE_DISABLED
470 /* Remote device is Desfire card */
471 phFriNfc_Desf_Process(NdefSmtCrdFmt, Status);
472 #else /* PH_FRINFC_FMT_DESFIRE_DISABLED*/
473 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
474 NFCSTATUS_INVALID_REMOTE_DEVICE);
475 #endif /* PH_FRINFC_FMT_DESFIRE_DISABLED*/
477 #ifdef PH_HAL4_ENABLE
478 case phHal_eJewel_PICC :
480 case phHal_eOpModesJewel:
481 #endif /* #ifdef PH_HAL4_ENABLE */
482 #ifndef PH_FRINFC_FMT_TOPAZ_DISABLED
483 /* Remote device is Topaz Smart card */
484 phFriNfc_Topaz_Process(NdefSmtCrdFmt, Status);
485 #else /* PH_FRINFC_FMT_TOPAZ_DISABLED*/
486 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
487 NFCSTATUS_INVALID_REMOTE_DEVICE);
488 #endif /* PH_FRINFC_FMT_TOPAZ_DISABLED*/
491 #ifndef PH_FRINFC_FMT_ISO15693_DISABLED
492 case phHal_eISO15693_PICC :
494 phFriNfc_ISO15693_FmtProcess (NdefSmtCrdFmt, Status);
497 #endif /* #ifndef PH_FRINFC_FMT_ISO15693_DISABLED */
499 #ifdef PHFRINFC_OVRHAL_MOCKUP
500 case phHal_eOpModesMockup:
501 /* Remote device is Desfire card */
502 //phFriNfc_Mockup_Process(NdefSmtCrdFmt, Status);
504 #endif /* PHFRINFC_OVRHAL_MOCKUP*/
506 /* Remote device opmode not recognised.
507 Probably not NDEF compliant */
508 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
509 NFCSTATUS_INVALID_REMOTE_DEVICE);
510 /* set the state back to the Reset_Init state*/
511 NdefSmtCrdFmt->State = PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT;
513 /* set the completion routine*/
514 NdefSmtCrdFmt->CompletionRoutine[PH_FRINFC_SMTCRDFMT_CR_INVALID_OPE].
515 CompletionRoutine(NdefSmtCrdFmt->CompletionRoutine->Context, Status);
521 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,\
522 NFCSTATUS_INVALID_PARAMETER);
523 /* The control should not come here. As Context itself is NULL ,
528 #endif /* PH_FRINFC_CARD_FORMAT_DISABLED */