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