• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
phFriNfc_NdefMap_Reset(phFriNfc_NdefMap_t * NdefMap,void * LowerDevice,phHal_sRemoteDevInformation_t * psRemoteDevInfo,phHal_sDevInputParam_t * psDevInputParam,uint8_t * TrxBuffer,uint16_t TrxBufferSize,uint8_t * ReceiveBuffer,uint16_t * ReceiveLength,uint16_t * DataCount)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 
phFriNfc_NdefMap_SetCompletionRoutine(phFriNfc_NdefMap_t * NdefMap,uint8_t FunctionID,pphFriNfc_Cr_t CompletionRoutine,void * CompletionRoutineContext)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 
phFriNfc_NdefMap_RdNdef(phFriNfc_NdefMap_t * NdefMap,uint8_t * PacketData,uint32_t * PacketDataLength,uint8_t Offset)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 
phFriNfc_NdefMap_WrNdef(phFriNfc_NdefMap_t * NdefMap,uint8_t * PacketData,uint32_t * PacketDataLength,uint8_t Offset)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
phFriNfc_NdefMap_ConvertToReadOnly(phFriNfc_NdefMap_t * NdefMap)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 
phFriNfc_NdefMap_ChkNdef(phFriNfc_NdefMap_t * NdefMap)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                         (0x01 == sak))
783                 {
784                     /*  The SAK/Sel_Res says the card is of the type
785                     Mifare Standard */
786 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
787                     status = phFriNfc_MifareStdMap_ChkNdef( NdefMap);
788 #else   /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
789                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
790                         NFCSTATUS_INVALID_REMOTE_DEVICE);
791 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
792                 }
793                 else
794                 {
795                     /*  Invalid Mifare UL card, as the remote device
796                     info - opmode says its a Mifare UL card but,
797                     The SAK/Sel_Res is wrong */
798                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
799                         NFCSTATUS_INVALID_REMOTE_DEVICE);
800                 }
801                 break;
802             case phHal_eISO14443_B_PICC:
803                 {
804                     status = phFriNfc_Desfire_ChkNdef(NdefMap);
805                 }
806                 break;
807             case  phHal_eISO14443_A_PICC :
808                 /*  Remote device is Desfire card . Check for Desfire
809                 NDEF compliancy */
810                 if(0x20 == (sak & 0x20))
811                 {
812                     /*  The SAK/Sel_Res says the card is of the type
813                     ISO14443_4A */
814 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
815                     status = phFriNfc_Desfire_ChkNdef(NdefMap);
816 #else   /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
817                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
818                         NFCSTATUS_INVALID_REMOTE_DEVICE);
819 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
820                 }
821                 else
822                 {
823                     /*  Invalid Desfire card, as the remote device
824                     info - opmode says its a desfire card but,
825                     The SAK/Sel_Res is wrong */
826                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
827                         NFCSTATUS_INVALID_REMOTE_DEVICE);
828                 }
829                 break;
830 
831             case phHal_eFelica_PICC:
832 
833                 /*Set the OpMode Type Flag*/
834 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
835 #ifndef PH_HAL4_ENABLE
836                 NdefMap->OpModeType[0] = phHal_eOpModesFelica212;
837                 NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
838 #endif /* #ifndef PH_HAL4_ENABLE */
839                 status = phFriNfc_Felica_ChkNdef(NdefMap);
840 #else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
841                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
842                     NFCSTATUS_INVALID_REMOTE_DEVICE);
843 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/
844 
845                 break;
846 
847 #ifndef PH_HAL4_ENABLE
848 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
849             case phHal_eFelica424:
850                 /*Set the OpMode Ttype Flag*/
851                 NdefMap->OpModeType[0] = phHal_eOpModesFelica424;
852                 NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
853                 status = phFriNfc_Felica_ChkNdef(NdefMap);
854 #else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
855                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
856                     NFCSTATUS_INVALID_REMOTE_DEVICE);
857 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/
858                 break;
859 #endif
860 
861             case phHal_eJewel_PICC :
862                 /*  Remote device is Topaz card . Check for Topaz
863                 NDEF compliancy */
864 #ifdef PH_HAL4_ENABLE
865 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
866             /* Decide on the Header bytes to know the
867                    Type of the Topaz card.Card could be Static or
868                    Dynamic type. These are of type NFFC-NDEF Data Application*/
869                 if ( NdefMap->psRemoteDevInfo->RemoteDevInfo.Jewel_Info.HeaderRom0
870                                 == PH_FRINFC_TOPAZ_HEADROM0_VAL)
871                 {
872 
873                         status = phFriNfc_TopazMap_ChkNdef(NdefMap);
874                 }
875 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
876                 else if( NdefMap->psRemoteDevInfo->RemoteDevInfo.Jewel_Info.HeaderRom0
877                                 == PH_FRINFC_TOPAZ_DYNAMIC_HEADROM0_VAL)
878                 {
879 
880                     status = phFriNfc_TopazDynamicMap_ChkNdef(NdefMap);
881                 }
882 #endif
883                 else
884                 {
885 
886                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
887                            NFCSTATUS_INVALID_REMOTE_DEVICE);
888 
889                 }
890 #endif
891 
892 
893 #else
894                 if(0xC2 == sak)
895                 {
896                     /*  The SAK/Sel_Res says the card is of the type
897                     ISO14443_4A */
898 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
899                     status = phFriNfc_TopazMap_ChkNdef(NdefMap);
900 #else   /* PH_FRINFC_MAP_TOPAZ_DISABLED*/
901                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
902                         NFCSTATUS_INVALID_REMOTE_DEVICE);
903 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED*/
904                 }
905                 else
906                 {
907                     /*  Invalid Topaz card, as the remote device
908                     info - opmode says its a desfire card but,
909                     The SAK/Sel_Res is wrong */
910                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
911                         NFCSTATUS_INVALID_REMOTE_DEVICE);
912                 }
913 #endif
914                 break;
915 
916 #ifdef PHFRINFC_OVRHAL_MOCKUP
917             case phHal_eOpModesMockup :
918                 /*Set the OpMode Ttype Flag*/
919                 NdefMap->OpModeType[0] = phHal_eOpModesMockup;
920                 NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
921                 status = phFriNfc_Mockup_ChkNdef(NdefMap);
922                 break;
923 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
924 
925             default :
926                 /*  Remote device is not recognised.
927                 Probably not NDEF compliant */
928                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
929                     NFCSTATUS_INVALID_REMOTE_DEVICE);
930                 break;
931             }
932         }
933     }
934     return status;
935 }
936 
937 
938 /*!
939 * \brief Completion Routine, Processing function, needed to avoid long blocking.
940 * \note The lower (Overlapped HAL) layer must register a pointer to this function as a Completion
941 *       Routine in order to be able to notify the component that an I/O has finished and data are
942 *       ready to be processed.
943 * This is a main Ndef Map Process api.Internally,depending upon the different
944 * CardTypes,respective mifare/desfire process functions are called.
945 *
946 */
947 
phFriNfc_NdefMap_Process(void * Context,NFCSTATUS Status)948 void phFriNfc_NdefMap_Process(  void        *Context,
949                               NFCSTATUS   Status)
950 {
951 
952     if ( Context != NULL )
953     {
954         phFriNfc_NdefMap_t  *NdefMap = (phFriNfc_NdefMap_t *)Context;
955         /*
956         * 4 Changed
957         *   Description: Opmode replace with RevDevTyp.
958         */
959 
960         switch ( NdefMap->psRemoteDevInfo->RemDevType )
961         {
962         case  phHal_eMifare_PICC :
963         case phHal_eISO14443_3A_PICC:
964 
965             if((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD) ||
966                 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD))
967             {
968 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
969                 /*  Remote device is Mifare Standard card */
970                 phFriNfc_MifareStdMap_Process(NdefMap,Status);
971 #else   /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
972                 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
973                     NFCSTATUS_INVALID_REMOTE_DEVICE);
974 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
975             }
976             else
977             {
978 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
979                 /*  Remote device is Mifare UL card */
980                 phFriNfc_MifareUL_Process(NdefMap,Status);
981 #else   /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
982                 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
983                     NFCSTATUS_INVALID_REMOTE_DEVICE);
984 #endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
985             }
986             break;
987 
988         case phHal_eISO14443_A_PICC :
989 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
990             /*  Remote device is Desfire card */
991             phFriNfc_Desfire_Process(NdefMap, Status);
992 #else   /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
993             Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
994                 NFCSTATUS_INVALID_REMOTE_DEVICE);
995 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
996             break;
997         case phHal_eISO14443_B_PICC:
998             /*  Remote device is Desfire card */
999             phFriNfc_Desfire_Process(NdefMap, Status);
1000             break;
1001 
1002         case phHal_eFelica_PICC :
1003 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1004             /*  Remote device is Felica Smart card */
1005             phFriNfc_Felica_Process(NdefMap, Status);
1006 #else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
1007             Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1008                 NFCSTATUS_INVALID_REMOTE_DEVICE);
1009 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/
1010             break;
1011 
1012         case phHal_eJewel_PICC:
1013 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
1014             if ( NdefMap->CardType == PH_FRINFC_NDEFMAP_TOPAZ_CARD )
1015             {
1016                 /*  Remote device is Topaz Smart card */
1017                 phFriNfc_TopazMap_Process(NdefMap, Status);
1018             }
1019 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
1020             else if ( NdefMap->CardType == PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD )
1021             {
1022                 /*  Remote device is Topaz Smart card */
1023                 phFriNfc_TopazDynamicMap_Process(NdefMap, Status);
1024             }
1025             else
1026             {
1027                 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1028                 NFCSTATUS_INVALID_REMOTE_DEVICE);
1029 
1030             }
1031             break;
1032 #endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED*/
1033 #endif
1034 
1035 
1036 #ifdef PHFRINFC_OVRHAL_MOCKUP
1037         case phHal_eOpModesMockup:
1038             /*  Remote device is Desfire card */
1039             phFriNfc_Mockup_Process(NdefMap, Status);
1040             break;
1041 #endif  /* PHFRINFC_OVRHAL_MOCKUP*/
1042         default :
1043             /*  Remote device opmode not recognised.
1044             Probably not NDEF compliant */
1045             Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1046                 NFCSTATUS_INVALID_REMOTE_DEVICE);
1047             /* set the state back to the Reset_Init state*/
1048             NdefMap->State =  PH_FRINFC_NDEFMAP_STATE_RESET_INIT;
1049 
1050             /* set the completion routine*/
1051             NdefMap->CompletionRoutine[PH_FRINFC_NDEFMAP_CR_INVALID_OPE].
1052                 CompletionRoutine(NdefMap->CompletionRoutine->Context, Status);
1053             break;
1054         }
1055     }
1056     else
1057     {
1058         Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
1059             NFCSTATUS_INVALID_PARAMETER);
1060         /* The control should not come here. As Context itself is NULL ,
1061         Can't call the CR*/
1062     }
1063 }
1064 
1065 #if 0
1066 
1067 NFCSTATUS phFriNfc_ChkAndParseTLV(phFriNfc_NdefMap_t    *NdefMap)
1068 {
1069     NFCSTATUS status = NFCSTATUS_PENDING;
1070 
1071     switch ( NdefMap->CardType )
1072     {
1073 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
1074             case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
1075 
1076 
1077                 break;
1078 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
1079 
1080 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
1081             case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
1082                 status = phFriNfc_Desf_ChkAndParseTLV(NdefMap);
1083                 return (status);
1084 
1085                 break;
1086 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
1087 
1088 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
1089             case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
1090             case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
1091 
1092                 break;
1093 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
1094 
1095 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1096             case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
1097                 ;
1098                 break;
1099 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
1100 
1101             default :
1102                 /*  Unknown card type. Return error */
1103                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
1104                     NFCSTATUS_INVALID_REMOTE_DEVICE);
1105 
1106                 break;
1107     }
1108 
1109     return ( status);
1110 }
1111 #endif
1112 
1113 
1114 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
phFriNfc_DesfCapCont_HReset(phFriNfc_NdefMap_t * NdefMap)1115 static void phFriNfc_DesfCapCont_HReset(phFriNfc_NdefMap_t *NdefMap)
1116 {
1117     /* Initialise/reset the desfire capability contatiner structure variables*/
1118     NdefMap->DesfireCapContainer.DesfVersion = 0;
1119     NdefMap->DesfireCapContainer.NdefMsgFid  = 0;
1120     NdefMap->DesfireCapContainer.NdefFileSize = 0;
1121     NdefMap->DesfireCapContainer.MaxCmdSize  = 0;
1122     NdefMap->DesfireCapContainer.MaxRespSize = 0;
1123     NdefMap->DesfireCapContainer.ReadAccess  = 0;
1124     NdefMap->DesfireCapContainer.WriteAccess = 0;
1125 }
1126 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
1127 
1128 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
phFriNfc_Felica_HReset(phFriNfc_NdefMap_t * NdefMap)1129 static void phFriNfc_Felica_HReset(phFriNfc_NdefMap_t *NdefMap)
1130 {
1131     uint8_t index=0;
1132 
1133     /* Initialise/reset the different felica structure variables*/
1134     /* Reset all the felica Basic staruture variables*/
1135     NdefMap->Felica.CurBlockNo = 0;
1136 
1137     for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_BLOCK_SIZE;index++)
1138     {
1139         NdefMap->Felica.Rd_BytesToCopyBuff[index] = 0;
1140         NdefMap->Felica.Wr_RemainedBytesBuff[index] = 0;
1141     }
1142     NdefMap->Felica.Rd_NoBytesToCopy = 0;
1143     NdefMap->Felica.Wr_BytesRemained = 0;
1144 
1145 
1146     /* Reset all the felica attribute information staruture variables*/
1147     for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_ATTR_NDEF_DATA_LEN;index++)
1148     {
1149         NdefMap->FelicaAttrInfo.LenBytes[index] = 0;
1150     }
1151 
1152     NdefMap->FelicaAttrInfo.Nmaxb = 0;
1153     NdefMap->FelicaAttrInfo.Nbr = 0;
1154     NdefMap->FelicaAttrInfo.Nbw= 0;
1155     NdefMap->FelicaAttrInfo.RdWrFlag = 0;
1156     NdefMap->FelicaAttrInfo.WriteFlag = 0;
1157     NdefMap->Felica.CurrBytesRead=0;
1158 
1159     /* Reset all the felica manufacture details staruture variables*/
1160     for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_MANUF_ID_DATA_LEN;index++)
1161     {
1162         NdefMap->FelicaManufDetails.ManufID[index] = 0;
1163         NdefMap->FelicaManufDetails.ManufParameter[index] = 0;
1164     }
1165     NdefMap->Felica.NoBlocksWritten=0;
1166 }
1167 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
1168 
phFriNfc_NdefMap_EraseNdef(phFriNfc_NdefMap_t * NdefMap)1169 NFCSTATUS phFriNfc_NdefMap_EraseNdef(phFriNfc_NdefMap_t *NdefMap)
1170 {
1171     NFCSTATUS   status = NFCSTATUS_PENDING;
1172 
1173     static uint8_t     PktData[3] = PH_FRINFC_NDEFMAP_EMPTY_NDEF_MSG;
1174     uint8_t     MemOffset = PH_FRINFC_NDEFMAP_SEEK_BEGIN;
1175     static uint32_t    PacketDataLength = sizeof(PktData);
1176 
1177     if (NdefMap == NULL )
1178     {
1179         /*  Invalid input parameter error   */
1180         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
1181     }
1182     else
1183     {
1184         switch ( NdefMap->CardType)
1185         {
1186 #ifdef PHFRINFC_OVRHAL_MOCKUP
1187             case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
1188 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
1189             case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
1190             case  PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
1191 #ifdef DESFIRE_EV1
1192             case  PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1 :
1193 #endif /* #ifdef DESFIRE_EV1 */
1194             case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
1195             case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
1196 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
1197             case  PH_FRINFC_NDEFMAP_TOPAZ_CARD :
1198 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
1199             case  PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
1200 #endif
1201 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
1202             case PH_FRINFC_NDEFMAP_ISO15693_CARD:
1203 #endif
1204 #endif
1205                 /*  Mifare card selected. Call Mifare Write */
1206                 status =  phFriNfc_NdefMap_WrNdef( NdefMap,
1207                     PktData,
1208                     &PacketDataLength,
1209                     MemOffset);
1210                 break;
1211 
1212             case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
1213 
1214 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1215                 /*  Felica card selected. Call to write EMPTY NDEF Msg */
1216                 status =  phFriNfc_Felica_EraseNdef( NdefMap );
1217 #else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
1218                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1219                     NFCSTATUS_INVALID_REMOTE_DEVICE);
1220 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/
1221 
1222                 break;
1223             default :
1224                 /*  Unknown card type. Return error */
1225                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1226                     NFCSTATUS_INVALID_REMOTE_DEVICE);
1227                 break;
1228         }
1229     }
1230     return status;
1231 }
1232 /*  Harsha: Fix for the mantis entry 0000420: NDEF_MAP: Size of NDEF data:
1233 no abstracted way for user to know how many bytes to read/write  */
1234 
1235 /*!
1236 * \brief Helper API, exposed to the user to enable him to know the size
1237 *        of the NDEF data that he can write in to the card.
1238 */
phFriNfc_NdefMap_GetContainerSize(const phFriNfc_NdefMap_t * NdefMap,uint32_t * maxSize,uint32_t * actualSize)1239 NFCSTATUS phFriNfc_NdefMap_GetContainerSize(const phFriNfc_NdefMap_t *NdefMap,uint32_t *maxSize, uint32_t *actualSize)
1240 {
1241     NFCSTATUS   result = NFCSTATUS_SUCCESS;
1242     uint8_t     sect_index = 0;
1243     uint8_t     actual_sect_index = 0;
1244     uint8_t     count_index = 0;
1245 
1246     if( (NdefMap == NULL) || (maxSize == NULL) || (actualSize == NULL))
1247     {
1248         /*  Invalid input parameter error   */
1249         result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
1250     }
1251     else
1252     {
1253         /*  Which card ? */
1254         switch(NdefMap->CardType)
1255         {
1256 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
1257             case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
1258                 /*  Mifare UL card */
1259                 /*  The integration needs to ensure that the checkNdef
1260                 function has been called before calling this function,
1261                 otherwise NdefMap->CardMemSize will be 0 */
1262                 *maxSize = NdefMap->MifareULContainer.RemainingSize;
1263                 /* In Mifare UL card, the actual size is the length field
1264                 value of the TLV */
1265                 *actualSize = NdefMap->TLVStruct.ActualSize;
1266                 break;
1267 #endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
1268 
1269 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
1270             case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
1271 #ifdef DESFIRE_EV1
1272             case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1 :
1273 #endif /* #ifdef DESFIRE_EV1 */
1274                 /*  Desfire card */
1275                 /*  The integration needs to ensure that the checkNdef
1276                 function has been called before calling this function,
1277                 otherwise NdefMap->DesfireCapContainer.NdefFileSize
1278                 will be 0 */
1279                 /* -2 bytes represents the size field bytes*/
1280                 *maxSize = NdefMap->DesfireCapContainer.NdefFileSize - 2;
1281                 /* In Desfire card, the actual size cant be calculated so
1282                 the actual size is given as 0xFFFFFFFF */
1283                 *actualSize = NdefMap->DesfireCapContainer.NdefDataLen;
1284                 break;
1285 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
1286 
1287 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
1288             case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
1289                 /*  Topaz card */
1290                 /*  The integration needs to ensure that the checkNdef
1291                 function has been called before calling this function,
1292                 otherwise NdefMap->CardMemSize will be 0 */
1293                 *maxSize = NdefMap->TopazContainer.RemainingSize;
1294                 /* In Topaz card, the actual size is the length field value of the
1295                 TLV */
1296                 *actualSize = NdefMap->TLVStruct.BytesRemainLinTLV;
1297                 break;
1298 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
1299             case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
1300                 /*  Topaz 512 card */
1301                 /*  The integration needs to ensure that the checkNdef
1302                 function has been called before calling this function,
1303                 otherwise NdefMap->CardMemSize will be 0 */
1304                 *maxSize = NdefMap->TopazContainer.NDEFRWSize;
1305                 /* In Topaz card, the actual size is the length field value of the
1306                 TLV */
1307                 *actualSize = NdefMap->TopazContainer.ActualNDEFMsgSize;
1308                 break;
1309 
1310 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */
1311 #endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
1312 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
1313             case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
1314             case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
1315                 /*  Mifare std card */
1316 
1317                 /*  Max size is the number of NDEF compliant blocks in the card
1318                 multiplied by 16 bytes */
1319 #ifndef PH_HAL4_ENABLE
1320 
1321                 *maxSize = NdefMap->StdMifareContainer.remainingSize;
1322 
1323 #else /* #ifndef PH_HAL4_ENABLE */
1324 
1325                 while ((PH_FRINFC_MIFARESTD_NDEF_COMP ==
1326                         NdefMap->StdMifareContainer.aid[count_index]) &&
1327                         (count_index <
1328                         PH_FRINFC_NDEFMAP_MIFARESTD_TOTALNO_BLK))
1329                 {
1330                     actual_sect_index++;
1331                     count_index++;
1332                 }
1333                 /* Total number of sectors in 1k = 16 (0 to 15, MAD sector number = 0)
1334                     Total number of sectors in 4k = 40 (0 to 39,
1335                         MAD sector number = 0 and 16, After block number 31, each sector
1336                         has 16 blocks)
1337                     Every last block of the sector is the sector header, So the blocks
1338                     that can be read or written in each sector is always
1339                         (number of blocks in each sector - 1)
1340                     No of blocks in the one sector till the sector number 0 to 31
1341                         (total 32 sectors) =
1342                         4 blocks, So blocks that can be read/write = (4 - 1 = 3 blocks)
1343                     No of blocks in the one sector after the sector number 31 to 39 =
1344                         16 blocks, So blocks that can be read/write = (16 - 1 = 15 blocks)
1345                     Each block has 16 bytes
1346                     To calculate number of bytes in the sector, depending on the number
1347                     of blocks multiplied by 16
1348                 */
1349                 if (PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD ==
1350                     NdefMap->CardType)
1351                 {
1352                     if (actual_sect_index > 32)
1353                     {
1354                         sect_index = (actual_sect_index - 32);
1355                         /* Here, 30 is used because block number 0 and 16 are MAD blocks
1356                         which cannot be used for reading and writing data
1357                         3 and 15 are blocks in each sector which can be read/write
1358                         3 indicates the sector is in between (0 and 31)
1359                         15 indicates the sector is in between (32 to 39)
1360                         16 is always number of bytes in each block
1361                         4 is used because each NDEF write has to write using the
1362                             TLV format and T & L takes 4 bytes length and V is the
1363                             input data
1364                         */
1365                         *maxSize = (((30 * (16 * 3)) + (sect_index * (16 * 15))) - 4);
1366                     }
1367                     else if (actual_sect_index <= 16)
1368                     {
1369                         *maxSize = (((actual_sect_index - 1) * (16 * 3)) - 4);
1370                     }
1371                     else
1372                     {
1373                         *maxSize = (((actual_sect_index - 2)  * (16 * 3)) - 4);
1374                     }
1375                 }
1376                 else
1377                 {
1378                     /* Here, 16 is always number of bytes in each block
1379                         3 indicates the sector is in between (0 and 31) */
1380                     if (actual_sect_index > NdefMap->StdMifareContainer.SectorIndex)
1381                     {
1382                         actual_sect_index = NdefMap->StdMifareContainer.SectorIndex;
1383                     }
1384                     *maxSize = (((actual_sect_index - 1) * (16 * 3)) - 4);
1385                 }
1386 
1387 #endif /* #ifndef PH_HAL4_ENABLE */
1388                 *actualSize = NdefMap->TLVStruct.BytesRemainLinTLV;
1389 
1390                 break;
1391 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
1392 
1393 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
1394             case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
1395                 /*  Felica card */
1396 
1397                 *maxSize = NdefMap->FelicaAttrInfo.Nmaxb * 0x10;
1398 
1399                 /* In Felica Card, actual size is calculated using the Length Bytes */
1400                 *actualSize = NdefMap->FelicaAttrInfo.LenBytes[0];
1401                 *actualSize = *actualSize << 16;
1402                 *actualSize += NdefMap->FelicaAttrInfo.LenBytes[1];
1403                 *actualSize = *actualSize << 8;
1404                 *actualSize += NdefMap->FelicaAttrInfo.LenBytes[2];
1405                 break;
1406 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
1407 
1408 #ifndef PH_FRINFC_MAP_ISO15693_DISABLED
1409             case PH_FRINFC_NDEFMAP_ISO15693_CARD:
1410             {
1411 #if 1
1412                 uint16_t                    block_no = 0;
1413                 uint8_t                     byte_no = 0;
1414 
1415                 block_no = (uint16_t)
1416                     ISO15693_GET_VALUE_FIELD_BLOCK_NO (
1417                         NdefMap->ISO15693Container.ndef_tlv_type_blk,
1418                         NdefMap->ISO15693Container.ndef_tlv_type_byte,
1419                         NdefMap->ISO15693Container.actual_ndef_size);
1420                 byte_no = (uint8_t)
1421                     ISO15693_GET_VALUE_FIELD_BYTE_NO (
1422                         NdefMap->ISO15693Container.ndef_tlv_type_blk,
1423                         NdefMap->ISO15693Container.ndef_tlv_type_byte,
1424                         NdefMap->ISO15693Container.actual_ndef_size);
1425 
1426                 *maxSize = (NdefMap->ISO15693Container.max_data_size -
1427                             ((block_no * ISO15693_BYTES_PER_BLOCK) + byte_no));
1428 #else /* #if 1 */
1429                 /* 2 is used to exclude the T and L part of the TLV */
1430                 *maxSize = (NdefMap->ISO15693Container.max_data_size
1431                             - ISO15693_BYTES_PER_BLOCK - 2);
1432 #endif /* #if 1 */
1433                 *actualSize = NdefMap->ISO15693Container.actual_ndef_size;
1434                 break;
1435             }
1436 #endif
1437 
1438 #ifdef PHFRINFC_OVRHAL_MOCKUP
1439             case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
1440                 *maxSize = 0xFFFFFFFF;
1441                 /* In Desfire card, the actual size cant be calculated so
1442                 the actual size is given as 0xFFFFFFFF */
1443                 *actualSize = 0xFFFFFFFF;
1444                 break;
1445 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
1446 
1447             default :
1448                 /*  Unknown card type. Return error */
1449                 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
1450                     NFCSTATUS_INVALID_REMOTE_DEVICE);
1451                 break;
1452         }
1453     }
1454     return(result);
1455 }
1456 
1457 #ifdef PHFRINFC_OVRHAL_MOCKUP
phFriNfc_NdefMap_MockupCardSetter(phFriNfc_NdefMap_t * NdefMap,uint8_t * NdefData,uint32_t NdefActualSize,uint32_t NdefMaxSize,uint32_t CardSize)1458 NFCSTATUS phFriNfc_NdefMap_MockupCardSetter(phFriNfc_NdefMap_t *NdefMap,
1459                                             uint8_t *NdefData,
1460                                             uint32_t NdefActualSize,
1461                                             uint32_t NdefMaxSize,
1462                                             uint32_t CardSize)
1463 {
1464     NFCSTATUS Status = NFCSTATUS_SUCCESS;
1465     // First check all parameters
1466     if((NdefData != NULL) && (NdefMap != NULL))
1467     {
1468         // OK we can set
1469         NdefMap->MochupContainer.NdefData       = NdefData;
1470         NdefMap->MochupContainer.NdefActualSize = NdefActualSize;
1471         NdefMap->MochupContainer.NdefMaxSize    = NdefMaxSize;
1472         NdefMap->MochupContainer.CardSize       = CardSize;
1473         NdefMap->MochupContainer.CurrentBlock   = 0;
1474 
1475     } else
1476     {
1477         Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
1478     }
1479     return Status;
1480 }
1481 
phFriNfc_NdefMap_MockupNDefModeEn(uint8_t * pNdefCompliancy,uint8_t * pCardType,uint8_t Enable)1482 NFCSTATUS phFriNfc_NdefMap_MockupNDefModeEn(uint8_t  *pNdefCompliancy, uint8_t  *pCardType, uint8_t Enable)
1483 {
1484     *pNdefCompliancy = Enable;
1485     *pCardType = PH_FRINFC_NDEFMAP_MOCKUP_CARD;
1486     return NFCSTATUS_SUCCESS;
1487 }
1488 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
1489 
1490 
1491 
1492 
1493 
1494 
1495 
1496 
1497