Initialize Tizen 2.3
[adaptation/devices/nfc-plugin-nxp.git] / src / phFriNfc_NdefMap.c
1 /*
2  * Copyright (C) 2010 NXP Semiconductors
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
17 /*!
18 * \file  phFriNfcNdefMap.c
19 * \brief NFC Ndef Mapping For Different Smart Cards.
20 *
21 * Project: NFC-FRI
22 *
23 * $Date: Mon Dec 13 14:14:12 2010 $
24 * $Author: ing02260 $
25 * $Revision: 1.39 $
26 * $Aliases:  $
27 *
28 */
29
30
31 #include <phFriNfc_NdefMap.h>
32
33 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
34 #include <phFriNfc_MifareULMap.h>
35 #endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
36
37 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
38 #include <phFriNfc_TopazMap.h>
39 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */
40
41 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
42 #include <phFriNfc_MifareStdMap.h>
43 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
44
45 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
46 #include <phFriNfc_DesfireMap.h>
47 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
48
49 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
50 #include <phFriNfc_FelicaMap.h>
51 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
52
53 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
54 #include <phFriNfc_ISO15693Map.h>
55 #endif  /* PH_FRINFC_MAP_ISO15693_DISABLED */
56
57 #ifdef PHFRINFC_OVRHAL_MOCKUP
58 #include <phFriNfc_MockupMap.h>
59 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
60
61
62 #include <phFriNfc_OvrHal.h>
63
64 /*! \ingroup grp_file_attributes
65 *  \name NDEF Mapping
66 *
67 * File: \ref phFriNfcNdefMap.c
68 *
69 */
70 /*@{*/
71 #define PHFRINFCNDEFMAP_FILEREVISION "$Revision: 1.39 $"
72 #define PHFRINFCNDEFMAP_FILEALIASES  "$Aliases:  $"
73 /*@}*/
74
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 */
79
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 */
84
85
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).
90 */
91
92 NFCSTATUS phFriNfc_NdefMap_Reset(   phFriNfc_NdefMap_t              *NdefMap,
93                                  void                            *LowerDevice,
94                                  phHal_sRemoteDevInformation_t   *psRemoteDevInfo,
95                                  phHal_sDevInputParam_t          *psDevInputParam,
96                                  uint8_t                         *TrxBuffer,
97                                  uint16_t                        TrxBufferSize,
98                                  uint8_t                         *ReceiveBuffer,
99                                  uint16_t                        *ReceiveLength,
100                                  uint16_t                        *DataCount)
101 {
102     NFCSTATUS   status = NFCSTATUS_SUCCESS;
103     uint8_t     index;
104
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 ))
110     {
111         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
112     }
113     else
114     {
115         /* Initialise the state to Init */
116         NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RESET_INIT;
117
118         for(index = 0;index<PH_FRINFC_NDEFMAP_CR;index++)
119         {
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;
124         }
125
126         /* Lower Device(Always Overlapped HAL Struct initialised in application
127         is registred in NdefMap Lower Device) */
128         NdefMap->LowerDevice = LowerDevice;
129
130         /* Remote Device info received from Manual Device Discovery is registered here */
131         NdefMap->psRemoteDevInfo = psRemoteDevInfo;
132
133         /* Transfer Buffer registered */
134         NdefMap->ApduBuffer = TrxBuffer;
135
136         /* Set the MaxApduBufferSize */
137         NdefMap->ApduBufferSize = TrxBufferSize;
138
139         /* Set APDU Buffer Index */
140         NdefMap->ApduBuffIndex = 0;
141
142         /* Register Transfer Buffer Length */
143         NdefMap->SendLength = 0;
144
145         /* Register Receive Buffer */
146         NdefMap->SendRecvBuf = ReceiveBuffer;
147
148         /* Register Receive Buffer Length */
149         NdefMap->SendRecvLength = ReceiveLength;
150
151         /* Register Temporary Receive Buffer Length */
152         NdefMap->TempReceiveLength = *ReceiveLength;
153
154         /* Register Data Count variable and set it to zero */
155         NdefMap->DataCount = DataCount;
156         *NdefMap->DataCount = 0;
157
158         /* Reset the PageOffset */
159         NdefMap->Offset = 0;
160
161         /* Reset the NumOfBytesRead*/
162         NdefMap->NumOfBytesRead = 0;
163
164         /* Reset the NumOfBytesWritten*/
165         NdefMap->NumOfBytesWritten = 0;
166
167         /* Reset the Card Type */
168         NdefMap->CardType = 0;
169
170         /* Reset the Memory Card Size*/
171         NdefMap->CardMemSize = 0;
172
173         /* Reset the Previous Operation*/
174         NdefMap->PrevOperation = 0;
175
176         /* Reset the Desfire Operation Flag*/
177         NdefMap->DespOpFlag = 0;
178
179         /* Reset MapCompletion Info*/
180         NdefMap->MapCompletionInfo.CompletionRoutine = NULL;
181         NdefMap->MapCompletionInfo.Context = NULL;
182
183         /*  Reset the ReadingForWriteOperation flag. */
184         NdefMap->ReadingForWriteOperation = 0;  /*  FALSE  */
185
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 */
190
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 */
196
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 */
202
203 #if !(defined(PH_FRINFC_MAP_TOPAZ_DISABLED ) || defined (PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED ))
204
205         phFriNfc_TopazMap_H_Reset(NdefMap);
206 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED || PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED  */
207
208
209 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
210         status = phFriNfc_MifareUL_H_Reset(NdefMap);
211 #endif
212
213 #ifdef PHFRINFC_OVRHAL_MOCKUP
214         /*Reset Desfire Cap Container elements*/
215         phFriNfc_Mockup_H_Reset(NdefMap);
216 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
217
218         /*
219         *  Fix for PR - 0001256
220         *  Date- 08-08-08
221         */
222         NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID;
223     }
224
225     return (status);
226 }
227
228 /*!
229 * Registering the Completion Routine.
230 *
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.
234 *
235 */
236
237 NFCSTATUS phFriNfc_NdefMap_SetCompletionRoutine(phFriNfc_NdefMap_t     *NdefMap,
238                                                 uint8_t                 FunctionID,
239                                                 pphFriNfc_Cr_t          CompletionRoutine,
240                                                 void                   *CompletionRoutineContext)
241 {
242     NFCSTATUS   status = NFCSTATUS_SUCCESS;
243
244     if ( ( NdefMap == NULL ) || (FunctionID >= PH_FRINFC_NDEFMAP_CR) ||
245         ( CompletionRoutine == NULL) || (CompletionRoutineContext == NULL))
246     {
247         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
248     }
249     else
250     {
251         /* Register the application callback with the NdefMap Completion Routine */
252         NdefMap->CompletionRoutine[FunctionID].CompletionRoutine = CompletionRoutine;
253
254         /* Register the application context with the NdefMap Completion Routine context */
255         NdefMap->CompletionRoutine[FunctionID].Context = CompletionRoutineContext;
256     }
257
258     return status;
259 }
260
261 /*!
262 * Initiates Reading of NDEF information from the Remote Device.
263 *
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.
267 *
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.
271 *
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.
275 *
276 * NOTE: Please refer the header file for more information.
277 *
278 */
279
280 NFCSTATUS phFriNfc_NdefMap_RdNdef(  phFriNfc_NdefMap_t  *NdefMap,
281                                   uint8_t             *PacketData,
282                                   uint32_t            *PacketDataLength,
283                                   uint8_t             Offset)
284 {
285     NFCSTATUS   status = NFCSTATUS_PENDING;
286
287
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 )
296         )
297     {
298         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
299     }
300     else if (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID)
301     {
302         /*  Card is in invalid state, cannot have any read/write
303         operations*/
304         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
305             NFCSTATUS_INVALID_FORMAT);
306     }
307     else if(NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED)
308     {
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);
311
312         NdefMap->NumOfBytesRead = PacketDataLength;
313         *NdefMap->NumOfBytesRead = 0;
314
315
316     }
317     else if ( (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ))
318     {
319         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
320     }
321     else
322     {
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.  */
328
329         if ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN )
330         {
331             NdefMap->ApduBuffIndex = 0;
332             *NdefMap->DataCount = 0;
333         }
334
335         if  ( (NdefMap->CardType == PH_FRINFC_NDEFMAP_ISO14443_4A_CARD) &&
336             (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && (*NdefMap->DataCount == 0 ))
337         {
338
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);
343         }
344         else
345         {
346             switch ( NdefMap->CardType)
347             {
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,
352                         PacketData,
353                         PacketDataLength,
354                         Offset);
355                     break;
356 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
357
358 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
359                 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
360 #ifdef DESFIRE_EV1
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,
365                         PacketData,
366                         PacketDataLength,
367                         Offset);
368                     break;
369 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
370
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,
376                         PacketData,
377                         PacketDataLength,
378                         Offset);
379                     break;
380 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
381
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,
386                         PacketData,
387                         PacketDataLength,
388                         Offset);
389                     break;
390 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
391
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,
396                         PacketData,
397                         PacketDataLength,
398                         Offset);
399                     break;
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,
404                         PacketData,
405                         PacketDataLength,
406                         Offset);
407                     break;
408 #endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
409 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */
410
411 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
412                 case PH_FRINFC_NDEFMAP_ISO15693_CARD:
413                     status =  phFriNfc_ISO15693_RdNdef(  NdefMap,
414                         PacketData,
415                         PacketDataLength,
416                         Offset);
417                     break;
418 #endif /* #ifndef PH_FRINFC_MAP_ISO15693_DISABLED */
419
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,
424                         PacketData,
425                         PacketDataLength,
426                         Offset);
427                     break;
428 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
429
430                 default :
431                     /*  Unknown card type. Return error */
432                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
433                         NFCSTATUS_INVALID_REMOTE_DEVICE);
434
435                     break;
436             }
437         }
438     }
439     return status;
440 }
441
442 /*!
443 * Initiates Writing of NDEF information to the Remote Device.
444 *
445 * The function initiates the writing of NDEF information to a Remote Device
446 *
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.
450 *
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.
454 *
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.
458 *
459 * NOTE: Please refer the header file for more information.
460 *
461 */
462
463
464 NFCSTATUS phFriNfc_NdefMap_WrNdef(  phFriNfc_NdefMap_t  *NdefMap,
465                                   uint8_t             *PacketData,
466                                   uint32_t            *PacketDataLength,
467                                   uint8_t             Offset)
468 {
469     NFCSTATUS   status = NFCSTATUS_PENDING;
470     uint8_t     StatusChk=0;
471
472      if (     (PacketData          == NULL)
473         ||  ( NdefMap             == NULL )
474         /* + Mantis 442 */
475         || ( PacketDataLength    == NULL )
476         /* - Mantis 442 */
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)
481         )
482     {
483         /*  Invalid input parameter error   */
484         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
485     }
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))
489     {
490         /*  Card is in invalid state, cannot have any read/write
491         operations*/
492         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
493             NFCSTATUS_INVALID_FORMAT);
494     }
495
496     else if ( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY )
497
498     {
499         /*Can't write to the card :No Grants */
500         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
501             NFCSTATUS_NOT_ALLOWED);
502
503         /* set the no. bytes written is zero*/
504         NdefMap->WrNdefPacketLength = PacketDataLength;
505         *NdefMap->WrNdefPacketLength = 0;
506
507     }
508     else
509     {
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))
517         {
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;
523         }
524
525         if ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN )
526         {
527             NdefMap->ApduBuffIndex = 0;
528             *NdefMap->DataCount = 0;
529         }
530
531         if ( (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_UL_CARD) ||
532             (NdefMap->CardType == PH_FRINFC_NDEFMAP_ISO14443_4A_CARD))
533         {
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 )))
536             {
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);
541                 StatusChk = 1;
542             }
543         }
544         if(StatusChk != 1)
545         {
546             NdefMap->WrNdefPacketLength =   PacketDataLength;
547             switch ( NdefMap->CardType)
548             {
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,
553                         PacketData,
554                         PacketDataLength,
555                         Offset);
556                     break;
557 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
558
559 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
560                 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
561 #ifdef DESFIRE_EV1
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,
566                         PacketData,
567                         PacketDataLength,
568                         Offset);
569                     break;
570 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
571
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,
577                         PacketData,
578                         PacketDataLength,
579                         Offset);
580                     break;
581 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
582
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,
587                         PacketData,
588                         PacketDataLength,
589                         Offset);
590                     break;
591 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
592
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,
597                         PacketData,
598                         PacketDataLength,
599                         Offset);
600                     break;
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,
605                         PacketData,
606                         PacketDataLength,
607                         Offset);
608                     break;
609 #endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
610 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */
611
612 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
613                 case PH_FRINFC_NDEFMAP_ISO15693_CARD:
614                     status =  phFriNfc_ISO15693_WrNdef(  NdefMap,
615                         PacketData,
616                         PacketDataLength,
617                         Offset);
618                     break;
619 #endif /* #ifndef PH_FRINFC_MAP_ISO15693_DISABLED */
620
621
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,
626                         PacketData,
627                         PacketDataLength,
628                         Offset);
629                     break;
630 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
631                 default :
632                     /*  Unknown card type. Return error */
633                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
634                         NFCSTATUS_INVALID_REMOTE_DEVICE);
635                     break;
636             }
637         }
638     }
639     return status;
640 }
641
642 #ifdef FRINFC_READONLY_NDEF
643
644 NFCSTATUS
645 phFriNfc_NdefMap_ConvertToReadOnly (
646     phFriNfc_NdefMap_t          *NdefMap)
647 {
648     NFCSTATUS   result = NFCSTATUS_PENDING;
649
650
651     /*  Check for ndefmap context and relevant state. Else return error*/
652     if (NULL == NdefMap)
653     {
654         result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
655     }
656     else if ((NdefMap->CompletionRoutine->CompletionRoutine == NULL) 
657         || (NdefMap->CompletionRoutine->Context == NULL))
658     {
659         result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
660     }
661     else
662     {
663         switch (NdefMap->CardType)
664         {
665             case PH_FRINFC_NDEFMAP_TOPAZ_CARD:
666             {
667                 result = phFriNfc_TopazMap_ConvertToReadOnly (NdefMap);
668                 break;
669             }
670
671             case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD:
672             {
673                 result = phFriNfc_TopazDynamicMap_ConvertToReadOnly (NdefMap);
674                 break;
675             }
676
677             case PH_FRINFC_NDEFMAP_ISO15693_CARD:
678             {
679                 result = phFriNfc_ISO15693_ConvertToReadOnly (NdefMap);
680                 break;
681             }
682
683             default:
684             {
685                 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
686                                     NFCSTATUS_INVALID_REMOTE_DEVICE);
687                 break;
688             }
689         }
690     }
691     return result;
692 }
693
694 #endif /* #ifdef FRINFC_READONLY_NDEF */
695
696 /*!
697 * Check whether a particular Remote Device is NDEF compliant.
698 *
699 * The function initiates the ndef compliancy check.
700 *
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
704 * compliancy.
705 *
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.
709 *
710 * NOTE: Please refer the header file for more information.
711 *
712 */
713
714 NFCSTATUS phFriNfc_NdefMap_ChkNdef( phFriNfc_NdefMap_t     *NdefMap)
715 {
716     NFCSTATUS   status = NFCSTATUS_PENDING;
717     uint8_t     sak;
718
719
720     /*  Check for ndefmap context and relevant state. Else return error*/
721     if ( NdefMap == NULL )
722     {
723         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
724     }
725     else
726     {
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 */
730         {
731             status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
732         }
733         else if ( (NdefMap->CompletionRoutine->CompletionRoutine == NULL) || (NdefMap->CompletionRoutine->Context == NULL ))
734         {
735             status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
736         }
737         else
738         {
739             /*
740             * 1.Changed
741             *   CardInfo106 Replace with the ReaderA_Info.
742             */
743
744             sak = NdefMap->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
745
746             /*
747             * 3. Changed
748             *    Description: Opmode replace with RemDevType.
749             */
750
751
752             switch ( NdefMap->psRemoteDevInfo->RemDevType )
753             {
754 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
755             case phHal_eISO15693_PICC:
756             {
757                 status = phFriNfc_ISO15693_ChkNdef (NdefMap);
758                 break;
759             }
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 */
764
765             case phHal_eMifare_PICC:
766             case phHal_eISO14443_3A_PICC:
767                 /*  Remote device is Mifare card . Check for Mifare
768                 NDEF compliancy */
769                 if(0x00 == sak)
770                 {
771                     /*  The SAK/Sel_Res says the card is of the type
772                     Mifare UL */
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*/
779                 }
780                 else if ((0x08 == (sak & 0x18)) ||
781                         (0x18 == (sak & 0x18)))
782                 {
783                     /*  The SAK/Sel_Res says the card is of the type
784                     Mifare Standard */
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*/
791                 }
792                 else
793                 {
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);
799                 }
800                 break;
801             case phHal_eISO14443_B_PICC:
802                 {
803                     status = phFriNfc_Desfire_ChkNdef(NdefMap);
804                 }
805                 break;
806             case  phHal_eISO14443_A_PICC :
807                 /*  Remote device is Desfire card . Check for Desfire
808                 NDEF compliancy */
809                 if(0x20 == (sak & 0x20))
810                 {
811                     /*  The SAK/Sel_Res says the card is of the type
812                     ISO14443_4A */
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*/
819                 }
820                 else
821                 {
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);
827                 }
828                 break;
829
830             case phHal_eFelica_PICC:
831
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*/
843
844                 break;
845
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*/
857                 break;
858 #endif
859
860             case phHal_eJewel_PICC :
861                 /*  Remote device is Topaz card . Check for Topaz
862                 NDEF compliancy */
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)
870                 {
871
872                         status = phFriNfc_TopazMap_ChkNdef(NdefMap);
873                 }
874 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
875                 else if( NdefMap->psRemoteDevInfo->RemoteDevInfo.Jewel_Info.HeaderRom0
876                                 == PH_FRINFC_TOPAZ_DYNAMIC_HEADROM0_VAL)
877                 {
878
879                     status = phFriNfc_TopazDynamicMap_ChkNdef(NdefMap);
880                 }
881 #endif
882                 else
883                 {
884
885                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
886                            NFCSTATUS_INVALID_REMOTE_DEVICE);
887
888                 }
889 #endif
890
891
892 #else
893                 if(0xC2 == sak)
894                 {
895                     /*  The SAK/Sel_Res says the card is of the type
896                     ISO14443_4A */
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*/
903                 }
904                 else
905                 {
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);
911                 }
912 #endif
913                 break;
914
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);
921                 break;
922 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
923
924             default :
925                 /*  Remote device is not recognised.
926                 Probably not NDEF compliant */
927                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
928                     NFCSTATUS_INVALID_REMOTE_DEVICE);
929                 break;
930             }
931         }
932     }
933     return status;
934 }
935
936
937 /*!
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.
944 *
945 */
946
947 void phFriNfc_NdefMap_Process(  void        *Context,
948                               NFCSTATUS   Status)
949 {
950
951     if ( Context != NULL )
952     {
953         phFriNfc_NdefMap_t  *NdefMap = (phFriNfc_NdefMap_t *)Context;
954         /*
955         * 4 Changed
956         *   Description: Opmode replace with RevDevTyp.
957         */
958
959         switch ( NdefMap->psRemoteDevInfo->RemDevType )
960         {
961         case  phHal_eMifare_PICC :
962         case phHal_eISO14443_3A_PICC:
963
964             if((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD) ||
965                 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD))
966             {
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*/
974             }
975             else
976             {
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*/
984             }
985             break;
986
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*/
995             break;
996         case phHal_eISO14443_B_PICC:
997             /*  Remote device is Desfire card */
998             phFriNfc_Desfire_Process(NdefMap, Status);
999             break;
1000
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*/
1009             break;
1010
1011         case phHal_eJewel_PICC:
1012 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
1013             if ( NdefMap->CardType == PH_FRINFC_NDEFMAP_TOPAZ_CARD )
1014             {
1015                 /*  Remote device is Topaz Smart card */
1016                 phFriNfc_TopazMap_Process(NdefMap, Status);
1017             }
1018 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
1019             else if ( NdefMap->CardType == PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD )
1020             {
1021                 /*  Remote device is Topaz Smart card */
1022                 phFriNfc_TopazDynamicMap_Process(NdefMap, Status);
1023             }
1024             else
1025             {
1026                 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1027                 NFCSTATUS_INVALID_REMOTE_DEVICE);
1028
1029             }
1030             break;
1031 #endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED*/
1032 #endif
1033
1034
1035 #ifdef PHFRINFC_OVRHAL_MOCKUP
1036         case phHal_eOpModesMockup:
1037             /*  Remote device is Desfire card */
1038             phFriNfc_Mockup_Process(NdefMap, Status);
1039             break;
1040 #endif  /* PHFRINFC_OVRHAL_MOCKUP*/
1041         default :
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;
1048
1049             /* set the completion routine*/
1050             NdefMap->CompletionRoutine[PH_FRINFC_NDEFMAP_CR_INVALID_OPE].
1051                 CompletionRoutine(NdefMap->CompletionRoutine->Context, Status);
1052             break;
1053         }
1054     }
1055     else
1056     {
1057         Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
1058             NFCSTATUS_INVALID_PARAMETER);
1059         /* The control should not come here. As Context itself is NULL ,
1060         Can't call the CR*/
1061     }
1062 }
1063
1064 #if 0
1065
1066 NFCSTATUS phFriNfc_ChkAndParseTLV(phFriNfc_NdefMap_t    *NdefMap)
1067 {
1068     NFCSTATUS status = NFCSTATUS_PENDING;
1069
1070     switch ( NdefMap->CardType )
1071     {
1072 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
1073             case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
1074
1075
1076                 break;
1077 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
1078
1079 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
1080             case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
1081                 status = phFriNfc_Desf_ChkAndParseTLV(NdefMap);
1082                 return (status);
1083
1084                 break;
1085 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
1086
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 :
1090
1091                 break;
1092 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
1093
1094 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1095             case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
1096                 ;
1097                 break;
1098 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
1099
1100             default :
1101                 /*  Unknown card type. Return error */
1102                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
1103                     NFCSTATUS_INVALID_REMOTE_DEVICE);
1104
1105                 break;
1106     }
1107
1108     return ( status);
1109 }
1110 #endif
1111
1112
1113 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
1114 static void phFriNfc_DesfCapCont_HReset(phFriNfc_NdefMap_t *NdefMap)
1115 {
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;
1124 }
1125 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
1126
1127 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1128 static void phFriNfc_Felica_HReset(phFriNfc_NdefMap_t *NdefMap)
1129 {
1130     uint8_t index=0;
1131
1132     /* Initialise/reset the different felica structure variables*/
1133     /* Reset all the felica Basic staruture variables*/
1134     NdefMap->Felica.CurBlockNo = 0;
1135
1136     for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_BLOCK_SIZE;index++)
1137     {
1138         NdefMap->Felica.Rd_BytesToCopyBuff[index] = 0;
1139         NdefMap->Felica.Wr_RemainedBytesBuff[index] = 0;
1140     }
1141     NdefMap->Felica.Rd_NoBytesToCopy = 0;
1142     NdefMap->Felica.Wr_BytesRemained = 0;
1143
1144
1145     /* Reset all the felica attribute information staruture variables*/
1146     for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_ATTR_NDEF_DATA_LEN;index++)
1147     {
1148         NdefMap->FelicaAttrInfo.LenBytes[index] = 0;
1149     }
1150
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;
1157
1158     /* Reset all the felica manufacture details staruture variables*/
1159     for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_MANUF_ID_DATA_LEN;index++)
1160     {
1161         NdefMap->FelicaManufDetails.ManufID[index] = 0;
1162         NdefMap->FelicaManufDetails.ManufParameter[index] = 0;
1163     }
1164     NdefMap->Felica.NoBlocksWritten=0;
1165 }
1166 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
1167
1168 NFCSTATUS phFriNfc_NdefMap_EraseNdef(phFriNfc_NdefMap_t *NdefMap)
1169 {
1170     NFCSTATUS   status = NFCSTATUS_PENDING;
1171
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);
1175
1176     if (NdefMap == NULL )
1177     {
1178         /*  Invalid input parameter error   */
1179         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
1180     }
1181     else
1182     {
1183         switch ( NdefMap->CardType)
1184         {
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 :
1190 #ifdef DESFIRE_EV1
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 :
1199 #endif
1200 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
1201             case PH_FRINFC_NDEFMAP_ISO15693_CARD:
1202 #endif
1203 #endif
1204                 /*  Mifare card selected. Call Mifare Write */
1205                 status =  phFriNfc_NdefMap_WrNdef( NdefMap,
1206                     PktData,
1207                     &PacketDataLength,
1208                     MemOffset);
1209                 break;
1210
1211             case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
1212
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*/
1220
1221                 break;
1222             default :
1223                 /*  Unknown card type. Return error */
1224                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1225                     NFCSTATUS_INVALID_REMOTE_DEVICE);
1226                 break;
1227         }
1228     }
1229     return status;
1230 }
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  */
1233
1234 /*!
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.
1237 */
1238 NFCSTATUS phFriNfc_NdefMap_GetContainerSize(const phFriNfc_NdefMap_t *NdefMap,uint32_t *maxSize, uint32_t *actualSize)
1239 {
1240     NFCSTATUS   result = NFCSTATUS_SUCCESS;
1241     uint8_t     sect_index = 0;
1242     uint8_t     actual_sect_index = 0;
1243     uint8_t     count_index = 0;
1244
1245     if( (NdefMap == NULL) || (maxSize == NULL) || (actualSize == NULL))
1246     {
1247         /*  Invalid input parameter error   */
1248         result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
1249     }
1250     else
1251     {
1252         /*  Which card ? */
1253         switch(NdefMap->CardType)
1254         {
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
1263                 value of the TLV */
1264                 *actualSize = NdefMap->TLVStruct.ActualSize;
1265                 break;
1266 #endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
1267
1268 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
1269             case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
1270 #ifdef DESFIRE_EV1
1271             case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1 :
1272 #endif /* #ifdef DESFIRE_EV1 */
1273                 /*  Desfire card */
1274                 /*  The integration needs to ensure that the checkNdef
1275                 function has been called before calling this function,
1276                 otherwise NdefMap->DesfireCapContainer.NdefFileSize
1277                 will be 0 */
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;
1283                 break;
1284 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
1285
1286 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
1287             case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
1288                 /*  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
1294                 TLV */
1295                 *actualSize = NdefMap->TLVStruct.BytesRemainLinTLV;
1296                 break;
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
1305                 TLV */
1306                 *actualSize = NdefMap->TopazContainer.ActualNDEFMsgSize;
1307                 break;
1308
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 */
1315
1316                 /*  Max size is the number of NDEF compliant blocks in the card
1317                 multiplied by 16 bytes */
1318 #ifndef PH_HAL4_ENABLE
1319
1320                 *maxSize = NdefMap->StdMifareContainer.remainingSize;
1321
1322 #else /* #ifndef PH_HAL4_ENABLE */
1323
1324                 while ((PH_FRINFC_MIFARESTD_NDEF_COMP ==
1325                         NdefMap->StdMifareContainer.aid[count_index]) &&
1326                         (count_index <
1327                         PH_FRINFC_NDEFMAP_MIFARESTD_TOTALNO_BLK))
1328                 {
1329                     actual_sect_index++;
1330                     count_index++;
1331                 }
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
1335                         has 16 blocks)
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
1347                 */
1348                 if (PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD ==
1349                     NdefMap->CardType)
1350                 {
1351                     if (actual_sect_index > 32)
1352                     {
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
1362                             input data
1363                         */
1364                         *maxSize = (((30 * (16 * 3)) + (sect_index * (16 * 15))) - 4);
1365                     }
1366                     else if (actual_sect_index <= 16)
1367                     {
1368                         *maxSize = (((actual_sect_index - 1) * (16 * 3)) - 4);
1369                     }
1370                     else
1371                     {
1372                         *maxSize = (((actual_sect_index - 2)  * (16 * 3)) - 4);
1373                     }
1374                 }
1375                 else
1376                 {
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)
1380                     {
1381                         actual_sect_index = NdefMap->StdMifareContainer.SectorIndex;
1382                     }
1383                     *maxSize = (((actual_sect_index - 1) * (16 * 3)) - 4);
1384                 }
1385
1386 #endif /* #ifndef PH_HAL4_ENABLE */
1387                 *actualSize = NdefMap->TLVStruct.BytesRemainLinTLV;
1388
1389                 break;
1390 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
1391
1392 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1393             case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
1394                 /*  Felica card */
1395
1396                 *maxSize = NdefMap->FelicaAttrInfo.Nmaxb * 0x10;
1397
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];
1404                 break;
1405 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
1406
1407 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
1408             case PH_FRINFC_NDEFMAP_ISO15693_CARD:
1409             {
1410 #if 1
1411                 uint16_t                    block_no = 0;
1412                 uint8_t                     byte_no = 0;
1413
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);
1419                 byte_no = (uint8_t)
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);
1424
1425                 *maxSize = (NdefMap->ISO15693Container.max_data_size - 
1426                             ((block_no * ISO15693_BYTES_PER_BLOCK) + byte_no));
1427 #else /* #if 1 */
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);
1431 #endif /* #if 1 */
1432                 *actualSize = NdefMap->ISO15693Container.actual_ndef_size;
1433                 break;
1434             }
1435 #endif
1436
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;
1443                 break;
1444 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
1445
1446             default :
1447                 /*  Unknown card type. Return error */
1448                 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
1449                     NFCSTATUS_INVALID_REMOTE_DEVICE);
1450                 break;
1451         }
1452     }
1453     return(result);
1454 }
1455
1456 #ifdef PHFRINFC_OVRHAL_MOCKUP
1457 NFCSTATUS phFriNfc_NdefMap_MockupCardSetter(phFriNfc_NdefMap_t *NdefMap,
1458                                             uint8_t *NdefData,
1459                                             uint32_t NdefActualSize,
1460                                             uint32_t NdefMaxSize,
1461                                             uint32_t CardSize)
1462 {
1463     NFCSTATUS Status = NFCSTATUS_SUCCESS;
1464     // First check all parameters
1465     if((NdefData != NULL) && (NdefMap != NULL))
1466     {
1467         // OK we can set
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;
1473
1474     } else
1475     {
1476         Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
1477     }
1478     return Status;
1479 }
1480
1481 NFCSTATUS phFriNfc_NdefMap_MockupNDefModeEn(uint8_t  *pNdefCompliancy, uint8_t  *pCardType, uint8_t Enable)
1482 {
1483     *pNdefCompliancy = Enable;
1484     *pCardType = PH_FRINFC_NDEFMAP_MOCKUP_CARD;
1485     return NFCSTATUS_SUCCESS;
1486 }
1487 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
1488
1489
1490
1491
1492
1493
1494
1495