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  phFriNfc_MifareStdMap.c
19 * \brief NFC Ndef Mapping For Remote Devices.
20 *
21 * Project: NFC-FRI
22 *
23 * $Date: Thu May  6 10:01:55 2010 $
24 * $Author: ing02260 $
25 * $Revision: 1.22 $
26 * $Aliases: NFC_FRI1.1_WK1017_R34_4,NFC_FRI1.1_WK1023_R35_1 $
27 *
28 */
29
30#ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
31
32#include <phFriNfc_MifareStdMap.h>
33#include <phFriNfc_OvrHal.h>
34
35#ifndef PH_HAL4_ENABLE
36#include <phFriNfc_OvrHalCmd.h>
37#endif
38
39#include <phFriNfc_MapTools.h>
40
41/*! \ingroup grp_file_attributes
42 *  \name NDEF Mapping
43 *
44 * File: \ref phFriNfcNdefMap.c
45 *
46 */
47/*@{*/
48#define PHFRINFCMIFARESTDMAP_FILEREVISION "$Revision: 1.22 $"
49#define PHFRINFCMIFARESTDMAP_FILEALIASES  "$Aliases: NFC_FRI1.1_WK1017_R34_4,NFC_FRI1.1_WK1023_R35_1 $"
50/*@}*/
51/*@}*/
52
53/*!
54 * \name Mifare Standard Mapping - Helper Functions
55 *
56 */
57/*@{*/
58
59/*!
60 * \brief \copydoc page_ovr Helper function for Mifare Std. This function Reads
61 *  a block from the card.
62 */
63static NFCSTATUS phFriNfc_MifStd_H_RdABlock(phFriNfc_NdefMap_t *NdefMap);
64
65/*!
66 * \brief \copydoc page_ovr Helper function for Mifare Std. This function writes
67 *  into a block of the card.
68 */
69static NFCSTATUS phFriNfc_MifStd_H_WrABlock(phFriNfc_NdefMap_t *NdefMap);
70
71/*!
72 * \brief \copydoc page_ovr Helper function for Mifare Std. This function authenticates
73 *  one sector at a time.
74 */
75static NFCSTATUS phFriNfc_MifStd_H_AuthSector(phFriNfc_NdefMap_t *NdefMap);
76
77/*!
78 * \brief \copydoc page_ovr Helper function for Mifare 4k Check Ndef to
79 *  get the next AID blocks
80 */
81static NFCSTATUS phFriNfc_MifStd4k_H_CheckNdef(phFriNfc_NdefMap_t *NdefMap);
82
83/*!
84 * \brief \copydoc page_ovr Helper function for Check Ndef to store the AIDs
85 */
86static void phFriNfc_MifStd_H_fillAIDarray(phFriNfc_NdefMap_t *NdefMap);
87
88/*!
89 * \brief \copydoc page_ovr Helper function to get the Sector from the current block
90 */
91static uint8_t phFriNfc_MifStd_H_GetSect(uint8_t  BlockNumber);
92
93/*!
94 * \brief \copydoc page_ovr Helper function to check the Ndef compliance of the
95 *  current block, if the block is not Ndef Compliant, increment the block till
96 *  the next Ndef compliant block using the Get Sector Helper function
97 */
98static NFCSTATUS phFriNfc_MifStd_H_BlkChk(phFriNfc_NdefMap_t *NdefMap);
99
100/*!
101 * \brief \copydoc page_ovr Helper function to read the access bits of each sector
102 */
103static NFCSTATUS phFriNfc_MifStd_H_RdAcsBit(phFriNfc_NdefMap_t *NdefMap);
104
105/*!
106 * \brief \copydoc page_ovr Helper function to check the access bits of each sector
107 */
108static NFCSTATUS phFriNfc_MifStd_H_ChkAcsBit(phFriNfc_NdefMap_t *NdefMap);
109
110/*!
111 * \brief \copydoc page_ovr Helper function for read access bits, depending
112 * on the read/write/check ndef function called
113 */
114static NFCSTATUS phFriNfc_MifStd_H_ChkRdWr(phFriNfc_NdefMap_t *NdefMap);
115
116/*!
117 * \brief \copydoc page_ovr Helper function for check ndef to check the
118 * ndef compliant sectors
119 */
120static void phFriNfc_MifStd_H_ChkNdefCmpltSects(phFriNfc_NdefMap_t *NdefMap);
121
122#if 0
123/*!
124 * \brief \copydoc page_ovr Helper function for read ndef to check the
125 * the length L of the ndef TLV
126 */
127static uint8_t phFriNfc_MifStd_H_ChkNdefLen(phFriNfc_NdefMap_t *NdefMap,
128                                        uint8_t             NDEFlength,
129                                        uint8_t             *CRFlag);
130
131/*!
132 * \brief \copydoc page_ovr Helper function for read ndef to copy the
133 * card data to the user buffer
134 */
135static NFCSTATUS phFriNfc_MifStd_H_RdNdefTLV(phFriNfc_NdefMap_t *NdefMap,
136                                         uint8_t            *Temp16Bytes);
137
138#endif
139
140/*!
141 * \brief \copydoc page_ovr Helper function for read ndef to process the
142 * remaining bytes of length (L) in the TLV
143 */
144static NFCSTATUS phFriNfc_MifStd_H_RemainTLV(phFriNfc_NdefMap_t *NdefMap,
145                                         uint8_t            *Flag,
146                                         uint8_t            *Temp16Bytes);
147
148/*!
149 * \brief \copydoc page_ovr Helper function for read ndef to process the
150 * internal bytes
151 */
152static NFCSTATUS phFriNfc_MifStd_H_ChkIntLen(phFriNfc_NdefMap_t *NdefMap);
153
154/*!
155 * \brief \copydoc page_ovr Helper function for read ndef to check the
156 * internal bytes without ndef tlv flag
157 */
158static NFCSTATUS phFriNfc_MifStd_H_IntLenWioutNdef(phFriNfc_NdefMap_t  *NdefMap,
159                                            uint8_t             *Flag,
160                                            uint8_t             *TempintBytes);
161#if 0
162/*!
163 * \brief \copydoc page_ovr Helper function for read ndef to check the
164 * internal bytes without ndef tlv flag
165 */
166static NFCSTATUS phFriNfc_MifStd_H_IntLenWithNdef(phFriNfc_NdefMap_t *NdefMap,
167                                            uint8_t             *TempintBytes);
168#endif
169/*!
170 * \brief \copydoc page_ovr Helper function for write ndef to add the TLV
171 * structure
172 */
173static uint8_t phFriNfc_MifStd_H_UpdateTLV(phFriNfc_NdefMap_t *NdefMap);
174
175/*!
176 * \brief \copydoc page_ovr Helper function for write ndef to write the Length TLV
177 *
178 */
179static NFCSTATUS phFriNfc_MifStd_H_WriteNdefLen(phFriNfc_NdefMap_t *NdefMap);
180#if 0
181/*!
182 * \brief \copydoc page_ovr Helper function for check ndef to check the ndef
183 * compliant blocks is 0
184 */
185static NFCSTATUS phFriNfc_MifStd_H_ChkNdefCmpltBlocks(phFriNfc_NdefMap_t   *NdefMap);
186#endif
187/*!
188 * \brief \copydoc page_ovr Helper function to set the authentication flag
189 * for the ndef TLV block
190 */
191static void phFriNfc_MifStd_H_SetNdefBlkAuth(phFriNfc_NdefMap_t   *NdefMap);
192
193/*!
194 * \brief \copydoc page_ovr Helper function to reset ndef TLV values. This is
195 * used when the offset is BEGIN
196 */
197static void phFriNfc_MifStd_H_RdWrReset(phFriNfc_NdefMap_t   *NdefMap);
198
199/*!
200 * \brief \copydoc page_ovr Helper function to read the first ndef compliant block to
201 * change the length
202 */
203static NFCSTATUS phFriNfc_MifStd_H_RdtoWrNdefLen(phFriNfc_NdefMap_t *NdefMap);
204
205/*!
206 * \brief \copydoc page_ovr Helper function to get the actual length of card
207 *
208 */
209static NFCSTATUS phFriNfc_MifStd_H_GetActCardLen(phFriNfc_NdefMap_t *NdefMap);
210
211/*!
212 * \brief \copydoc page_ovr Helper function to check all the TLVs
213 *
214 */
215static NFCSTATUS phFriNfc_MifStd_H_ChkTLVs(phFriNfc_NdefMap_t *NdefMap,
216                                           uint8_t            *CRFlag);
217
218/*!
219 * \brief \copydoc page_ovr Helper function to get the next TLV
220 *
221 */
222static NFCSTATUS phFriNfc_MifStd_H_GetNxtTLV(phFriNfc_NdefMap_t *NdefMap,
223                                       uint16_t             *TempLength,
224                                       uint8_t              *TL4bytesFlag);
225
226/*!
227 * \brief \copydoc page_ovr Helper function to know whether the read
228 *  16 bytes are parsed completely
229 */
230static NFCSTATUS phFriNfc_MifStd_H_Chk16Bytes(phFriNfc_NdefMap_t   *NdefMap,
231                                       uint16_t             TempLength);
232
233/*!
234 * \brief \copydoc page_ovr Helper function to know whether the read
235 *  16 bytes are parsed completely
236 */
237static NFCSTATUS phFriNfc_MifStd_H_ChkRemainTLVs(phFriNfc_NdefMap_t *NdefMap,
238                                          uint8_t            *CRFlag,
239                                          uint8_t            *NDEFFlag);
240
241/*!
242 * \brief \copydoc page_ovr Helper function to call the Completion Routine
243 *
244 */
245static void phFriNfc_MifStd_H_Complete(phFriNfc_NdefMap_t  *NdefMap,
246                                 NFCSTATUS            Result);
247
248/*!
249 * \brief \copydoc page_ovr Helper function to get the Mifare 1k Sector Trailer
250 *
251 */
252static void phFriNfc_MifStd_H_Get1kStTrail(phFriNfc_NdefMap_t  *NdefMap);
253
254/*!
255 * \brief \copydoc page_ovr Helper function to get the Mifare 4k Sector Trailer
256 *
257 */
258static void phFriNfc_MifStd_H_Get4kStTrail(phFriNfc_NdefMap_t  *NdefMap);
259
260/*!
261 * \brief \copydoc page_ovr Helper function to process the check ndef call
262 *
263 */
264static NFCSTATUS phFriNfc_MifStd_H_ProChkNdef(phFriNfc_NdefMap_t        *NdefMap);
265
266/*!
267 * \brief \copydoc page_ovr Helper function to process the authentication of a sector
268 *
269 */
270static NFCSTATUS phFriNfc_MifStd_H_ProAuth(phFriNfc_NdefMap_t       *NdefMap);
271
272/*!
273 * \brief \copydoc page_ovr Helper function to read 16 bytes from a specifed block no.
274 *
275 */
276static NFCSTATUS phFriNfc_MifStd_H_Rd16Bytes(phFriNfc_NdefMap_t     *NdefMap,
277                                              uint8_t               BlockNo);
278
279/*!
280 * \brief \copydoc page_ovr Helper function to process access bits of the
281 * sector trailer
282 */
283static NFCSTATUS phFriNfc_MifStd_H_ProAcsBits(phFriNfc_NdefMap_t    *NdefMap);
284
285/*!
286 * \brief \copydoc page_ovr Helper function to check the GPB bytes
287 */
288static NFCSTATUS phFriNfc_MifStd_H_GPBChk(phFriNfc_NdefMap_t        *NdefMap);
289
290/*!
291 * \brief \copydoc page_ovr Helper function to check for the different
292 * status value in the process because of proprietary forum sector
293 */
294static NFCSTATUS phFriNfc_MifStd_H_ProStatNotValid(phFriNfc_NdefMap_t    *NdefMap,
295                                                   NFCSTATUS             status);
296
297/*!
298 * \brief \copydoc page_ovr Helper function to read the NDEF TLV block
299 */
300static NFCSTATUS phFriNfc_MifStd_H_RdBeforeWr(phFriNfc_NdefMap_t        *NdefMap);
301
302/*!
303 * \brief \copydoc page_ovr Helper function to process the NDEF TLV block
304 *  read bytes to start write from the NDEF TLV
305 */
306static NFCSTATUS phFriNfc_MifStd_H_ProBytesToWr(phFriNfc_NdefMap_t        *NdefMap);
307
308/*!
309 * \brief \copydoc page_ovr Helper function to fill the send buffer to write
310 */
311static NFCSTATUS phFriNfc_MifStd_H_fillSendBuf(phFriNfc_NdefMap_t        *NdefMap,
312                                               uint8_t                   Length);
313
314/*!
315 * \brief \copydoc page_ovr Helper function to write 16 bytes in a block
316 */
317static NFCSTATUS phFriNfc_MifStd_H_WrTLV(phFriNfc_NdefMap_t        *NdefMap);
318
319/*!
320 * \brief \copydoc page_ovr Helper function to process the write TLV bytes in a block
321 */
322static NFCSTATUS phFriNfc_MifStd_H_ProWrTLV(phFriNfc_NdefMap_t        *NdefMap);
323
324/*!
325 * \brief \copydoc page_ovr Helper function to update the remaining TLV
326 */
327static uint8_t phFriNfc_MifStd_H_UpdRemTLV(phFriNfc_NdefMap_t        *NdefMap);
328
329/*!
330 * \brief \copydoc page_ovr Helper function to update the length field if more than one
331 * NULL TLVs exists before of the NDEF TLV
332 */
333static void phFriNfc_MifStd_H_fillTLV1(phFriNfc_NdefMap_t        *NdefMap);
334
335/*!
336 * \brief \copydoc page_ovr Helper function to update the length field if more than one
337 * NULL TLVs does not exists before the TLV
338 */
339static void phFriNfc_MifStd_H_fillTLV2(phFriNfc_NdefMap_t        *NdefMap);
340
341/*!
342 * \brief \copydoc page_ovr Helper function to increment/decrement the ndef tlv block
343 * and read the block
344 */
345static NFCSTATUS phFriNfc_MifStd_H_CallWrNdefLen(phFriNfc_NdefMap_t        *NdefMap);
346
347/*!
348 * \brief \copydoc page_ovr Helper function to check the current block is valid or not
349 * if not valid decrement the current block till the valid block
350 */
351static NFCSTATUS phFriNfc_MifStd_H_BlkChk_1(phFriNfc_NdefMap_t        *NdefMap);
352
353/*!
354 * \brief \copydoc page_ovr Helper function update the length of the TLV if NULL TLVs
355 * greater than or equal to 2
356 */
357static void phFriNfc_MifStd_H_fillTLV1_1(phFriNfc_NdefMap_t        *NdefMap);
358
359/*!
360 * \brief \copydoc page_ovr Helper function update the length of the TLV if NULL TLVs
361 * less than 2
362 */
363static void phFriNfc_MifStd_H_fillTLV2_1(phFriNfc_NdefMap_t        *NdefMap);
364
365/*!
366 * \brief \copydoc page_ovr Helper function to read the TLV block
367 */
368static NFCSTATUS phFriNfc_MifStd_H_RdTLV(phFriNfc_NdefMap_t        *NdefMap);
369
370/*!
371 * \brief \copydoc page_ovr Helper function to process the read TLV block
372 */
373static NFCSTATUS phFriNfc_MifStd_H_ProRdTLV(phFriNfc_NdefMap_t        *NdefMap);
374
375/*!
376 * \brief \copydoc page_ovr Helper function to write the terminator TLV
377 */
378static NFCSTATUS phFriNfc_MifStd_H_WrTermTLV(phFriNfc_NdefMap_t   *NdefMap);
379
380/*!
381 * \brief \copydoc page_ovr Helper function to process the write a block function
382 */
383static NFCSTATUS phFriNfc_MifStd_H_ProWrABlock(phFriNfc_NdefMap_t   *NdefMap);
384
385#ifndef PH_HAL4_ENABLE
386/*!
387 * \brief \copydoc page_ovr Helper function to call poll function after the
388 * authentication has failed
389 */
390static NFCSTATUS phFriNfc_MifStd_H_CallPoll(phFriNfc_NdefMap_t   *NdefMap);
391#endif
392
393/*!
394 * \brief \copydoc page_ovr Helper function to call connect function after the
395 * authentication has failed
396 */
397static NFCSTATUS phFriNfc_MifStd_H_CallConnect(phFriNfc_NdefMap_t   *NdefMap);
398
399/*!
400 * \brief \copydoc page_ovr Helper function to call disconnect function after the
401 * authentication has failed
402 */
403static NFCSTATUS phFriNfc_MifStd_H_CallDisCon(phFriNfc_NdefMap_t   *NdefMap);
404
405/*!
406 * \brief \copydoc page_ovr Helper function to call know the ndef compliant block using
407 */
408static void phFriNfc_MifStd1k_H_BlkChk(phFriNfc_NdefMap_t   *NdefMap,
409                                    uint8_t              SectorID,
410                                    uint8_t              *callbreak);
411
412/*@}*/
413/**
414 * \name Mifare Standard Mapping - Constants.
415 *
416 */
417/*@{*/
418/* As per the spec, the value of this macro
419   PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT1 shall be 0xA0 */
420#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT1        0xA0 /**< \internal Authenticate Command for MAD Sector */
421
422/* As per the spec, the value of this macro
423   PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT2 shall be 0xA1 */
424#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT2        0xA1 /**< \internal Authenticate Command for MAD Sector */
425
426/* As per the spec, the value of this macro
427   PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT3 shall be 0xA2 */
428#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT3        0xA2 /**< \internal Authenticate Command for MAD Sector */
429
430/* As per the spec, the value of this macro
431   PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT4 shall be 0xA3 */
432#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT4        0xA3 /**< \internal Authenticate Command for MAD Sector */
433
434/* As per the spec, the value of this macro
435   PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT5 shall be 0xA4 */
436#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT5        0xA4 /**< \internal Authenticate Command for MAD Sector */
437
438/* As per the spec, the value of this macro
439   PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT6 shall be 0xA5 */
440#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT6        0xA5 /**< \internal Authenticate Command for MAD Sector */
441
442/* As per the spec, the value of this macro
443   PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1 shall be 0xD3 */
444#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1       0xD3 /**< \internal Authenticate Command for NDEF Sectors 1 */
445
446/* As per the spec, the value of this macro
447   PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2 shall be 0xF7 */
448#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2       0xF7 /**< \internal Authenticate Command for NDEF Sectors 2 */
449#define PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL2         0x03 /**< \internal Ndef Compliant command 1 */
450#define PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL1         0xE1 /**< \internal Ndef Compliant command 2 */
451
452/* Enable access bits check for the MAD sector
453#define ENABLE_ACS_BIT_CHK_FOR_MAD */
454/*@}*/
455
456NFCSTATUS phFriNfc_MifareStdMap_H_Reset(phFriNfc_NdefMap_t        *NdefMap)
457{
458    NFCSTATUS   status = NFCSTATUS_SUCCESS;
459    uint8_t     index = PH_FRINFC_MIFARESTD_VAL0;
460    if ( NdefMap == NULL)
461    {
462        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
463    }
464    else
465    {
466        /* Current Block stores the present block accessed in the card */
467        NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_VAL0;
468
469        for(index = PH_FRINFC_MIFARESTD_VAL0; index <
470            PH_FRINFC_NDEFMAP_MIFARESTD_ST15_BYTES; index++)
471        {
472            /* internal buffer to store the odd bytes of length < 15 */
473            NdefMap->StdMifareContainer.internalBuf[index] = PH_FRINFC_MIFARESTD_VAL0;
474        }
475        /* odd bytes length stored in the internal buffer */
476        NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
477
478        NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INITIALIZED;
479
480        /* Flag to get that last few bytes are taken from the user buffer */
481        NdefMap->StdMifareContainer.RemainingBufFlag = PH_FRINFC_MIFARESTD_FLAG0;
482
483        /* Flag to find that the read/write operation has reached the end of the card.
484            Further reading/writing is not possible */
485        NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
486
487        /* Flag to get that last few bytes are taken from the internal buffer */
488        NdefMap->StdMifareContainer.internalBufFlag = PH_FRINFC_MIFARESTD_FLAG0;
489
490        /* Authentication Flag for every sector */
491        NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
492
493        /* Used in Check Ndef for storing the sector ID */
494        NdefMap->StdMifareContainer.SectorIndex = PH_FRINFC_MIFARESTD_VAL0;
495
496        NdefMap->StdMifareContainer.NdefBlocks = PH_FRINFC_MIFARESTD_VAL0;
497
498        NdefMap->StdMifareContainer.NoOfNdefCompBlocks = PH_FRINFC_MIFARESTD_VAL0;
499
500        NdefMap->StdMifareContainer.ReadAcsBitFlag = PH_FRINFC_MIFARESTD_FLAG0;
501
502        NdefMap->StdMifareContainer.remSizeUpdFlag = PH_FRINFC_MIFARESTD_FLAG0;
503
504        NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_MIFARESTD_VAL0;
505
506        NdefMap->TLVStruct.prevLenByteValue = PH_FRINFC_MIFARESTD_VAL0;
507
508        NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0;
509
510        NdefMap->TLVStruct.NdefTLVBlock = PH_FRINFC_MIFARESTD_VAL0;
511
512        NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL0;
513
514        NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
515
516        NdefMap->StdMifareContainer.remainingSize = PH_FRINFC_MIFARESTD_VAL0;
517
518        NdefMap->StdMifareContainer.ReadNdefFlag = PH_FRINFC_MIFARESTD_FLAG0;
519
520        NdefMap->StdMifareContainer.WrNdefFlag = PH_FRINFC_MIFARESTD_FLAG0;
521
522        NdefMap->StdMifareContainer.ChkNdefFlag = PH_FRINFC_MIFARESTD_FLAG0;
523
524        NdefMap->StdMifareContainer.aidCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
525
526        NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0;
527
528        NdefMap->StdMifareContainer.ProprforumSectFlag = PH_FRINFC_MIFARESTD_PROP_1ST_CONFIG;
529
530        NdefMap->StdMifareContainer.ReadCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
531
532        NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0;
533
534        NdefMap->StdMifareContainer.WrLength = PH_FRINFC_MIFARESTD_VAL1;
535
536        NdefMap->StdMifareContainer.ChkNdefCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
537    }
538
539    return status;
540}
541
542/*!
543 * \brief Check whether a particular Remote Device is NDEF compliant.
544 *
545 * The function checks whether the peer device is NDEF compliant.
546 *
547 * \param[in] NdefMap Pointer to a valid instance of the \ref phFriNfc_NdefMap_t
548 *                    structure describing the component context.
549 *
550 * \retval  NFCSTATUS_PENDING   The action has been successfully triggered.
551 * \retval  Others              An error has occurred.
552 *
553 */
554
555NFCSTATUS phFriNfc_MifareStdMap_ChkNdef( phFriNfc_NdefMap_t     *NdefMap)
556{
557    NFCSTATUS                   status =    NFCSTATUS_PENDING;
558    uint8_t                     atq,
559                                sak;
560
561    if ( NdefMap == NULL)
562    {
563        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
564    }
565    else
566    {
567        /* set the data for additional data exchange*/
568        NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
569        NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0;
570        NdefMap->psDepAdditionalInfo.NAD = 0;
571        NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_CHECK_OPE;
572        NdefMap->StdMifareContainer.CRIndex = PH_FRINFC_NDEFMAP_CR_CHK_NDEF;
573
574        /* Get the Select Response and Sense Response to get
575            the exact Card Type either Mifare 1k or 4k */
576#ifndef PH_HAL4_ENABLE
577        sak = NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo106.
578                        Startup106.SelRes;
579        atq = NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo106.
580                        Startup106.SensRes[0];
581#else
582        sak = NdefMap->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
583        atq = NdefMap->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AtqA[0];
584        PHNFC_UNUSED_VARIABLE(atq);
585#endif
586
587        if (0x08 == (sak & 0x18))
588        {
589            /* Total Number of Blocks in Mifare 1k Card */
590            NdefMap->StdMifareContainer.NoOfNdefCompBlocks =
591                                        PH_FRINFC_NDEFMAP_MIFARESTD_1KNDEF_COMPBLOCK;
592            NdefMap->StdMifareContainer.remainingSize =
593                                                ((NdefMap->CardType == PH_FRINFC_MIFARESTD_VAL0)?
594                                                (PH_FRINFC_NDEFMAP_MIFARESTD_1KNDEF_COMPBLOCK *
595                                                PH_FRINFC_MIFARESTD_BLOCK_BYTES):
596                                                NdefMap->StdMifareContainer.remainingSize);
597            NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD;
598        }
599        else
600        {
601             /* Total Number of Blocks in Mifare 4k Card */
602            NdefMap->StdMifareContainer.NoOfNdefCompBlocks =
603                                        PH_FRINFC_NDEFMAP_MIFARESTD_4KNDEF_COMPBLOCK;
604            NdefMap->StdMifareContainer.remainingSize =
605                                            ((NdefMap->CardType == PH_FRINFC_MIFARESTD_VAL0)?
606                                            (PH_FRINFC_NDEFMAP_MIFARESTD_4KNDEF_COMPBLOCK *
607                                            PH_FRINFC_MIFARESTD_BLOCK_BYTES):
608                                            NdefMap->StdMifareContainer.remainingSize);
609            NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD;
610        }
611
612
613        /*  phFriNfc_MifareStdMap_ChkNdef should be called only
614            when currentBlock is 0 OR 64,65 and 66 (for Mifare 4k).
615            Otherwise return error */
616        /* and also Check the Authentication Flag */
617        if((NdefMap->StdMifareContainer.currentBlock != 0) &&
618            (NdefMap->StdMifareContainer.currentBlock != 1) &&
619            (NdefMap->StdMifareContainer.currentBlock != 2) &&
620            (NdefMap->StdMifareContainer.currentBlock != 64) &&
621            (NdefMap->StdMifareContainer.currentBlock != 65) &&
622            (NdefMap->StdMifareContainer.currentBlock != 66) )
623        {
624            status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
625        }
626        else if( NdefMap->StdMifareContainer.AuthDone == 0)
627        {
628            /*  Block 0 contains Manufacturer information and
629                also other informaton. So go for block 1 which
630                contains AIDs. Authenticating any of the block
631                in a sector, Authenticates the whole sector */
632            if(NdefMap->StdMifareContainer.currentBlock == 0)
633            {
634                NdefMap->StdMifareContainer.currentBlock = 1;
635            }
636
637            status = phFriNfc_MifStd_H_AuthSector(NdefMap);
638        }
639        else
640        {
641            /*  Mifare 1k, sak = 0x08 atq = 0x04
642                Mifare 4k, sak = 0x38 atq = 0x02 */
643            if ((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD) ||
644                (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD))
645            {
646                /* Change the state to Check Ndef Compliant */
647                NdefMap->State = PH_FRINFC_NDEFMAP_STATE_CHK_NDEF_COMP;
648                NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_CHECK_OPE;
649                NdefMap->StdMifareContainer.ChkNdefFlag = PH_FRINFC_MIFARESTD_FLAG1;
650
651                NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process;
652                NdefMap->MapCompletionInfo.Context = NdefMap;
653
654#ifndef PH_HAL4_ENABLE
655                NdefMap->Cmd.MfCmd = phHal_eMifareCmdListMifareRead;
656#else
657                NdefMap->Cmd.MfCmd = phHal_eMifareRead;
658#endif
659                *NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
660                NdefMap->SendRecvBuf[0] = NdefMap->StdMifareContainer.currentBlock;
661                NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_READ;
662
663                /* Call the Overlapped HAL Transceive function */
664                status = phFriNfc_OvrHal_Transceive(    NdefMap->LowerDevice,
665                                                        &NdefMap->MapCompletionInfo,
666                                                        NdefMap->psRemoteDevInfo,
667                                                        NdefMap->Cmd,
668                                                        &NdefMap->psDepAdditionalInfo,
669                                                        NdefMap->SendRecvBuf,
670                                                        NdefMap->SendLength,
671                                                        NdefMap->SendRecvBuf,
672                                                        NdefMap->SendRecvLength);
673            }
674            else
675            {
676                /* Since we have decided temporarily not to go
677                    for any new error codes we are using
678                    NFCSTATUS_INVALID_PARAMETER even though it is not
679                    the relevant error code here TBD */
680                status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
681            }
682        }
683    }
684    return status;
685}
686
687
688/*!
689 * \brief Initiates Reading of NDEF information from the Remote Device.
690 *
691 * The function initiates the reading of NDEF information from a Remote Device.
692 * It performs a reset of the state and starts the action (state machine).
693 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once the action
694 * has been triggered.
695 */
696NFCSTATUS phFriNfc_MifareStdMap_RdNdef( phFriNfc_NdefMap_t                  *NdefMap,
697                                        uint8_t                             *PacketData,
698                                        uint32_t                            *PacketDataLength,
699                                        uint8_t                             Offset)
700{
701    NFCSTATUS               status =    NFCSTATUS_PENDING;
702
703    NdefMap->ApduBufferSize = *PacketDataLength;
704    NdefMap->NumOfBytesRead = PacketDataLength;
705    *NdefMap->NumOfBytesRead = 0;
706    NdefMap->ApduBuffIndex = 0;
707    NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE;
708    NdefMap->StdMifareContainer.CRIndex = PH_FRINFC_NDEFMAP_CR_RD_NDEF;
709
710    if((NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID)
711        || (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED))
712    {
713        /* Card state  is not correct */
714        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
715                            NFCSTATUS_INVALID_PARAMETER);
716    }
717    else
718    {
719        if( (Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) || ( NdefMap->PrevOperation ==
720            PH_FRINFC_NDEFMAP_WRITE_OPE))
721        {
722            phFriNfc_MifStd_H_RdWrReset(NdefMap);
723            NdefMap->StdMifareContainer.ReadNdefFlag = PH_FRINFC_MIFARESTD_FLAG1;
724            NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
725        }
726        /* Offset = Current, but the read has reached the End of Card */
727        if( (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) &&
728            (NdefMap->StdMifareContainer.ReadWriteCompleteFlag ==
729            PH_FRINFC_MIFARESTD_FLAG1))
730        {
731            status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_EOF_NDEF_CONTAINER_REACHED);
732        }
733        else
734        {
735            NdefMap->Offset = (((Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN) &&
736                                ( NdefMap->PrevOperation ==
737                                PH_FRINFC_NDEFMAP_WRITE_OPE))?
738                                PH_FRINFC_NDEFMAP_SEEK_BEGIN:
739                                Offset);
740            status = phFriNfc_MifStd_H_BlkChk(NdefMap);
741            if(status == NFCSTATUS_SUCCESS)
742            {
743                NdefMap->ApduBuffer = PacketData;
744
745                /* Read Operation in Progress */
746                NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
747
748                /* Check Authentication Flag */
749                status =
750                ((NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)?
751                    phFriNfc_MifStd_H_RdABlock(NdefMap):
752                    phFriNfc_MifStd_H_AuthSector(NdefMap));
753            }
754        }
755    }
756    return status;
757}
758
759/*!
760 * \brief Initiates Writing of NDEF information to the Remote Device.
761 *
762 * The function initiates the writing of NDEF information to a Remote Device.
763 * It performs a reset of the state and starts the action (state machine).
764 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once the action
765 * has been triggered.
766 */
767NFCSTATUS phFriNfc_MifareStdMap_WrNdef( phFriNfc_NdefMap_t     *NdefMap,
768                                        uint8_t                 *PacketData,
769                                        uint32_t                *PacketDataLength,
770                                        uint8_t                 Offset)
771{
772    NFCSTATUS                   status =    NFCSTATUS_PENDING;
773
774    NdefMap->ApduBuffer = PacketData;
775    NdefMap->ApduBufferSize = *PacketDataLength;
776    NdefMap->ApduBuffIndex = PH_FRINFC_MIFARESTD_VAL0;
777    NdefMap->WrNdefPacketLength = PacketDataLength;
778    *NdefMap->WrNdefPacketLength = PH_FRINFC_MIFARESTD_VAL0;
779    NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE;
780    NdefMap->StdMifareContainer.CRIndex = PH_FRINFC_NDEFMAP_CR_WR_NDEF;
781
782    if((NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID)
783        || (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY))
784    {
785        /* Card state  is not correct */
786        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
787                            NFCSTATUS_INVALID_PARAMETER);
788    }
789    else
790    {
791        if( (Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) ||
792        (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_READ_OPE))
793        {
794            NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
795            NdefMap->StdMifareContainer.RdBeforeWrFlag = PH_FRINFC_MIFARESTD_FLAG1;
796            NdefMap->StdMifareContainer.WrNdefFlag = PH_FRINFC_MIFARESTD_FLAG1;
797            NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
798            NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG0;
799            NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
800            NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
801            NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL0;
802            NdefMap->TLVStruct.NdefTLVAuthFlag = PH_FRINFC_MIFARESTD_FLAG0;
803            NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0;
804            NdefMap->StdMifareContainer.remainingSize =
805                        (NdefMap->StdMifareContainer.NoOfNdefCompBlocks *
806                        PH_FRINFC_MIFARESTD_BLOCK_BYTES);
807            NdefMap->StdMifareContainer.currentBlock =
808                                            PH_FRINFC_MIFARESTD_BLK4;
809            NdefMap->StdMifareContainer.NdefBlocks = PH_FRINFC_MIFARESTD_VAL1;
810            NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0;
811            /* This macro is added, to be compliant with the previous HAL 2.0
812                For HAL 2.0, polling is done before writing data to the mifare
813                std (if the offset is BEGIN), because if an error is reported
814                during read or write and again write is called, the PN531 state is
815                unchanged (so write will fail), to bring the PN531 to the correct
816                state, polling is done.
817                Changed on 13th Jan 2009
818            */
819#ifdef PH_HAL4_ENABLE
820            NdefMap->StdMifareContainer.PollFlag = PH_FRINFC_MIFARESTD_FLAG0;
821#else
822            NdefMap->StdMifareContainer.PollFlag = PH_FRINFC_MIFARESTD_FLAG1;
823#endif /* #ifdef PH_HAL4_ENABLE */
824            NdefMap->StdMifareContainer.WrLength = PH_FRINFC_MIFARESTD_VAL0;
825            NdefMap->StdMifareContainer.FirstWriteFlag = PH_FRINFC_MIFARESTD_FLAG1;
826        }
827
828        if(((Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) &&
829            (NdefMap->StdMifareContainer.ReadWriteCompleteFlag ==
830            PH_FRINFC_MIFARESTD_FLAG1)) || ((NdefMap->StdMifareContainer.PollFlag ==
831            PH_FRINFC_MIFARESTD_FLAG1) && (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR)))
832        {
833            /* Offset = Current, but the read has reached the End of Card */
834            status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
835                                NFCSTATUS_EOF_NDEF_CONTAINER_REACHED);
836        }
837        else
838        {
839            NdefMap->Offset = (((Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN) &&
840                            ( NdefMap->PrevOperation ==
841                            PH_FRINFC_NDEFMAP_READ_OPE))?
842                            PH_FRINFC_NDEFMAP_SEEK_BEGIN:
843                            Offset);
844            NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
845            status = phFriNfc_MifStd_H_BlkChk(NdefMap);
846            NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
847            if(status == NFCSTATUS_SUCCESS)
848            {
849                if(NdefMap->StdMifareContainer.PollFlag ==
850                    PH_FRINFC_MIFARESTD_FLAG1)
851                {
852                    /* if poll flag is set then call disconnect because the authentication
853                        has failed so reactivation of card is required */
854                    status = phFriNfc_MifStd_H_CallDisCon(NdefMap);
855                }
856                /* Check Authentication Flag */
857                else if(NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)
858                {
859                    status =  ((NdefMap->Offset ==
860                                PH_FRINFC_NDEFMAP_SEEK_BEGIN)?
861                                phFriNfc_MifStd_H_RdBeforeWr(NdefMap):
862                                phFriNfc_MifStd_H_WrABlock(NdefMap));
863                }
864                else
865                {
866                    status = phFriNfc_MifStd_H_AuthSector(NdefMap);
867                }
868            }
869        }
870    }
871    return status;
872}
873
874
875/*!
876 * \brief Completion Routine, Processing function, needed to avoid long blocking.
877 * \note The lower (Overlapped HAL) layer must register a pointer to this function as a Completion
878 *       Routine in order to be able to notify the component that an I/O has finished and data are
879 *       ready to be processed.
880 *
881 */
882
883void phFriNfc_MifareStdMap_Process( void       *Context,
884                                    NFCSTATUS   Status)
885{
886
887    phFriNfc_NdefMap_t      *NdefMap;
888
889    uint8_t                 NDEFFlag = 0,
890                            CRFlag = 0,
891                            Temp16Bytes = 0,
892                            i = 0;
893
894
895    NdefMap = (phFriNfc_NdefMap_t *)Context;
896
897
898    if((Status & PHNFCSTBLOWER) == (NFCSTATUS_SUCCESS & PHNFCSTBLOWER))
899    {
900        switch(NdefMap->State)
901        {
902            case PH_FRINFC_NDEFMAP_STATE_CHK_NDEF_COMP:
903                Status = phFriNfc_MifStd_H_ProChkNdef(NdefMap);
904                CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
905                            PH_FRINFC_MIFARESTD_FLAG1:
906                            PH_FRINFC_MIFARESTD_FLAG0);
907                break;
908
909            case PH_FRINFC_NDEFMAP_STATE_READ:
910
911                /* Receive Length for read shall always be equal to 16 */
912                if((*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ) &&
913                    (NdefMap->ApduBuffIndex < (uint16_t)NdefMap->ApduBufferSize))
914                {
915                    Temp16Bytes = PH_FRINFC_MIFARESTD_VAL0;
916                    NDEFFlag = (uint8_t)PH_FRINFC_MIFARESTD_FLAG1;
917                    if(NdefMap->TLVStruct.BytesRemainLinTLV != 0)
918                    {
919                        NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
920                        CRFlag = PH_FRINFC_MIFARESTD_FLAG0;
921                        /* To read the remaining length (L) in TLV */
922                        Status = phFriNfc_MifStd_H_RemainTLV(NdefMap, &NDEFFlag, &Temp16Bytes);
923                        CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
924                                            PH_FRINFC_MIFARESTD_FLAG1:
925                                            PH_FRINFC_MIFARESTD_FLAG0);
926                    }
927
928                    /* check the NDEFFlag is set. if this is not set, then
929                        in the above RemainTLV function all the 16 bytes has been
930                        read */
931#if 0
932                    if((NDEFFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
933                        (NdefMap->TLVStruct.NdefTLVFoundFlag ==
934                        PH_FRINFC_MIFARESTD_FLAG1))
935                    {
936                        /* if the block is NDEF TLV then get data from here */
937                        Status = phFriNfc_MifStd_H_RdNdefTLV(NdefMap, &Temp16Bytes);
938                    }
939#endif
940                }
941                else
942                {
943                    Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_RECEIVE_LENGTH);
944                    CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
945                }
946                break;
947
948            case PH_FRINFC_NDEFMAP_STATE_WRITE:
949                Status = phFriNfc_MifStd_H_ProWrABlock(NdefMap);
950                CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
951                                    PH_FRINFC_MIFARESTD_FLAG1:
952                                    PH_FRINFC_MIFARESTD_FLAG0);
953
954                /* Call Completion Routine if CR Flag is Set to 1 */
955                if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
956                {
957                    *NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex;
958                }
959                break;
960
961            case PH_FRINFC_NDEFMAP_STATE_AUTH:
962                NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0;
963                Status = phFriNfc_MifStd_H_ProAuth(NdefMap);
964                CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
965                                    PH_FRINFC_MIFARESTD_FLAG1:
966                                    PH_FRINFC_MIFARESTD_FLAG0);
967                break;
968
969            case PH_FRINFC_NDEFMAP_STATE_RD_ACS_BIT:
970                Status = phFriNfc_MifStd_H_ProAcsBits(NdefMap);
971                CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
972                                    PH_FRINFC_MIFARESTD_FLAG1:
973                                    PH_FRINFC_MIFARESTD_FLAG0);
974                break;
975
976            case PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN:
977                if(NdefMap->StdMifareContainer.RdAfterWrFlag ==
978                    PH_FRINFC_MIFARESTD_FLAG1)
979                {
980                    Status = phFriNfc_MifStd_H_CallWrNdefLen(NdefMap);
981                    CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
982                                PH_FRINFC_MIFARESTD_FLAG1:
983                                PH_FRINFC_MIFARESTD_FLAG0);
984                }
985                else
986                {
987                    /* Check this */
988                    if(NdefMap->StdMifareContainer.TempBlockNo ==
989                        NdefMap->StdMifareContainer.currentBlock)
990                    {
991                       (void)memcpy( NdefMap->StdMifareContainer.internalBuf,
992                                NdefMap->StdMifareContainer.Buffer,
993                                NdefMap->StdMifareContainer.internalLength);
994                    }
995                    *NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex;
996                    NdefMap->StdMifareContainer.currentBlock =
997                                    NdefMap->StdMifareContainer.TempBlockNo;
998                    NdefMap->CardState = (uint8_t)((NdefMap->CardState ==
999                                        PH_NDEFMAP_CARD_STATE_INITIALIZED)?
1000                                        PH_NDEFMAP_CARD_STATE_READ_WRITE:
1001                                        NdefMap->CardState);
1002                    CRFlag = (uint8_t)PH_FRINFC_MIFARESTD_FLAG1;
1003                }
1004                /*NdefMap->StdMifareContainer.remainingSize -=
1005                (((NdefMap->ApduBufferSize) > (PH_FRINFC_MIFARESTD_NDEFTLV_L -
1006                                            PH_FRINFC_MIFARESTD_VAL1))?
1007                                            ((uint16_t)(*NdefMap->WrNdefPacketLength +
1008                                            PH_FRINFC_MIFARESTD_VAL4)):
1009                                            ((uint16_t)(*NdefMap->WrNdefPacketLength +
1010                                            PH_FRINFC_MIFARESTD_VAL2)));*/
1011                break;
1012
1013            case PH_FRINFC_NDEFMAP_STATE_RD_TO_WR_NDEF_LEN:
1014                CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
1015                Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1016                                    NFCSTATUS_INVALID_DEVICE_REQUEST);
1017                if(*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ)
1018                {
1019                    /* Size of NdefMap->SendRecvBuf is set by phLibNfc_Gen_NdefMapReset to PH_LIBNFC_GEN_MAX_BUFFER */
1020                    /* We don't have to check memory here */
1021                    for (i = PH_FRINFC_MIFARESTD_BYTES_READ; i > 0; i--)
1022                    {
1023                        NdefMap->SendRecvBuf[i] = NdefMap->SendRecvBuf[i-1];
1024                    }
1025                    NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
1026                                NdefMap->StdMifareContainer.currentBlock;
1027                    Status = phFriNfc_MifStd_H_WriteNdefLen(NdefMap);
1028                    CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
1029                                PH_FRINFC_MIFARESTD_FLAG1:
1030                                PH_FRINFC_MIFARESTD_FLAG0);
1031                }
1032                break;
1033
1034            case PH_FRINFC_NDEFMAP_STATE_GET_ACT_CARDSIZE:
1035                NDEFFlag = PH_FRINFC_MIFARESTD_FLAG1;
1036                if(NdefMap->TLVStruct.NoLbytesinTLV > PH_FRINFC_MIFARESTD_VAL0)
1037                {
1038                    NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
1039                    Status = phFriNfc_MifStd_H_ChkRemainTLVs(NdefMap, &CRFlag, &NDEFFlag);
1040                    NdefMap->TLVStruct.NoLbytesinTLV =
1041                                    PH_FRINFC_MIFARESTD_VAL0;
1042                }
1043                if((NDEFFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
1044                    (CRFlag != PH_FRINFC_MIFARESTD_FLAG1))
1045                {
1046                    Status = phFriNfc_MifStd_H_ChkTLVs(NdefMap, &CRFlag);
1047                }
1048                if(((NdefMap->StdMifareContainer.ReadNdefFlag ==
1049                    PH_FRINFC_MIFARESTD_FLAG1) ||
1050                    (NdefMap->StdMifareContainer.WrNdefFlag ==
1051                    PH_FRINFC_MIFARESTD_FLAG1))&&
1052                    (Status != NFCSTATUS_PENDING))
1053                {
1054                    NdefMap->StdMifareContainer.NFCforumSectFlag =
1055                                            PH_FRINFC_MIFARESTD_FLAG1;
1056                    CRFlag = PH_FRINFC_MIFARESTD_FLAG0;
1057                    /* if the card state has changed to initialised and
1058                     read ndef is called then error is returned */
1059                    if(((NdefMap->StdMifareContainer.WrNdefFlag ==
1060                        PH_FRINFC_MIFARESTD_FLAG1) &&
1061                        (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY)) ||
1062                        ((NdefMap->StdMifareContainer.ReadNdefFlag ==
1063                            PH_FRINFC_MIFARESTD_FLAG1) &&
1064                        (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED)))
1065                    {
1066                        Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1067                                            NFCSTATUS_NO_NDEF_SUPPORT);
1068                    }
1069                    if(NdefMap->StdMifareContainer.AuthDone ==
1070                        PH_FRINFC_MIFARESTD_FLAG0)
1071                    {
1072                        Status = phFriNfc_MifStd_H_AuthSector(NdefMap);
1073                    }
1074                    else
1075                    {
1076                        Status = ((NdefMap->StdMifareContainer.ReadNdefFlag ==
1077                                    PH_FRINFC_MIFARESTD_FLAG1)?
1078                                    phFriNfc_MifStd_H_RdTLV(NdefMap):
1079                                    phFriNfc_MifStd_H_RdBeforeWr(NdefMap));
1080                    }
1081                    NdefMap->StdMifareContainer.ReadNdefFlag =
1082                                            PH_FRINFC_MIFARESTD_FLAG0;
1083                    NdefMap->StdMifareContainer.WrNdefFlag =
1084                                            PH_FRINFC_MIFARESTD_FLAG0;
1085
1086                }
1087                if(NdefMap->StdMifareContainer.ChkNdefFlag ==
1088                    PH_FRINFC_MIFARESTD_FLAG1)
1089                {
1090                    CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
1091                            PH_FRINFC_MIFARESTD_FLAG1:
1092                            PH_FRINFC_MIFARESTD_FLAG0);
1093                }
1094                break;
1095
1096            case PH_FRINFC_NDEFMAP_STATE_RD_BEF_WR:
1097                /* Read flag says that already part of TLV has been written */
1098                Status = phFriNfc_MifStd_H_ProBytesToWr(NdefMap);
1099                 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
1100                                    PH_FRINFC_MIFARESTD_FLAG1:
1101                                    PH_FRINFC_MIFARESTD_FLAG0);
1102                break;
1103
1104            case PH_FRINFC_NDEFMAP_STATE_WR_TLV:
1105                Status = phFriNfc_MifStd_H_ProWrTLV(NdefMap);
1106                CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
1107                                    PH_FRINFC_MIFARESTD_FLAG1:
1108                                    PH_FRINFC_MIFARESTD_FLAG0);
1109                break;
1110
1111            case PH_FRINFC_NDEFMAP_STATE_RD_TLV:
1112                Status = phFriNfc_MifStd_H_ProRdTLV(NdefMap);
1113                CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
1114                                    PH_FRINFC_MIFARESTD_FLAG1:
1115                                    PH_FRINFC_MIFARESTD_FLAG0);
1116                break;
1117
1118            case PH_FRINFC_NDEFMAP_STATE_TERM_TLV:
1119                phFriNfc_MifStd_H_SetNdefBlkAuth(NdefMap);
1120                NdefMap->StdMifareContainer.currentBlock =
1121                                NdefMap->TLVStruct.NdefTLVBlock;
1122                Status = phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap);
1123                CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
1124                                    PH_FRINFC_MIFARESTD_FLAG1:
1125                                    PH_FRINFC_MIFARESTD_FLAG0);
1126                break;
1127
1128            case PH_FRINFC_NDEFMAP_STATE_DISCONNECT:
1129                NdefMap->StdMifareContainer.PollFlag = PH_FRINFC_MIFARESTD_FLAG0;
1130
1131#ifndef PH_HAL4_ENABLE
1132                Status = phFriNfc_MifStd_H_CallPoll(NdefMap);
1133                CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
1134                                    PH_FRINFC_MIFARESTD_FLAG1:
1135                                    PH_FRINFC_MIFARESTD_FLAG0);
1136                break;
1137
1138            case PH_FRINFC_NDEFMAP_STATE_POLL:
1139#endif
1140                Status = phFriNfc_MifStd_H_CallConnect(NdefMap);
1141                CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
1142                                    PH_FRINFC_MIFARESTD_FLAG1:
1143                                    PH_FRINFC_MIFARESTD_FLAG0);
1144                break;
1145
1146            case PH_FRINFC_NDEFMAP_STATE_CONNECT:
1147                if(NdefMap->StdMifareContainer.FirstReadFlag == PH_FRINFC_MIFARESTD_FLAG1)
1148                {
1149                    NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0;
1150                    Status = phFriNfc_MifStd_H_AuthSector(NdefMap);
1151                }
1152                else
1153                {
1154                    Status = ((((NdefMap->Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) &&
1155                        (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE)) ||
1156                        (NdefMap->StdMifareContainer.WrLength >
1157                        PH_FRINFC_MIFARESTD_VAL0))?
1158                        phFriNfc_MifStd_H_ProStatNotValid(NdefMap, Status):
1159                        phFriNfc_MifStd_H_AuthSector(NdefMap));
1160                }
1161                CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
1162                                    PH_FRINFC_MIFARESTD_FLAG1:
1163                                    PH_FRINFC_MIFARESTD_FLAG0);
1164                break;
1165
1166            default:
1167                Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1168                                    NFCSTATUS_INVALID_DEVICE_REQUEST);
1169                CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
1170                break;
1171        }
1172    }
1173    else if(NdefMap->State == PH_FRINFC_NDEFMAP_STATE_AUTH)
1174    {
1175        NdefMap->StdMifareContainer.PollFlag = PH_FRINFC_MIFARESTD_FLAG1;
1176        if(NdefMap->StdMifareContainer.FirstWriteFlag ==
1177            PH_FRINFC_MIFARESTD_FLAG1)
1178        {
1179            NdefMap->StdMifareContainer.FirstWriteFlag =
1180                                            PH_FRINFC_MIFARESTD_FLAG0;
1181            NdefMap->StdMifareContainer.WrLength =
1182                ((NdefMap->StdMifareContainer.NFCforumSectFlag ==
1183                PH_FRINFC_MIFARESTD_FLAG0)?
1184                PH_FRINFC_MIFARESTD_VAL1:
1185                NdefMap->StdMifareContainer.WrLength);
1186        }
1187        /*if(NdefMap->StdMifareContainer.WrLength != PH_FRINFC_MIFARESTD_VAL0)
1188        {
1189            Status = NFCSTATUS_SUCCESS;
1190            NdefMap->StdMifareContainer.ReadCompleteFlag =
1191                                    PH_FRINFC_MIFARESTD_FLAG1;
1192            *NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex;
1193            CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
1194        }
1195        else*/ if(NdefMap->StdMifareContainer.WrLength == PH_FRINFC_MIFARESTD_VAL0)
1196        {
1197            Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1198                                NFCSTATUS_EOF_NDEF_CONTAINER_REACHED);
1199            CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
1200        }
1201        else
1202        {
1203            /* Authentication has failed */
1204            Status = phFriNfc_MifStd_H_CallDisCon(NdefMap);
1205            CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)?
1206                        PH_FRINFC_MIFARESTD_FLAG1:
1207                        PH_FRINFC_MIFARESTD_FLAG0);
1208        }
1209    }
1210    else
1211    {
1212        Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1213                            NFCSTATUS_INVALID_DEVICE_REQUEST);
1214        CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
1215    }
1216     /* Call Completion Routine if CR Flag is Set to 1 */
1217    if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
1218    {
1219        phFriNfc_MifStd_H_Complete(NdefMap, Status);
1220    }
1221}
1222
1223static NFCSTATUS phFriNfc_MifStd_H_RdABlock(phFriNfc_NdefMap_t *NdefMap)
1224{
1225    NFCSTATUS               status =    NFCSTATUS_PENDING;
1226
1227    /* set the data for additional data exchange*/
1228    NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
1229    NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0;
1230    NdefMap->psDepAdditionalInfo.NAD = 0;
1231
1232    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_READ;
1233    NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE;
1234    NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process;
1235    NdefMap->MapCompletionInfo.Context = NdefMap;
1236
1237    if( NdefMap->ApduBuffIndex <
1238        (uint16_t)NdefMap->ApduBufferSize)
1239    {
1240
1241        if(NdefMap->StdMifareContainer.internalLength > PH_FRINFC_MIFARESTD_VAL0)
1242        {
1243            status = phFriNfc_MifStd_H_ChkIntLen(NdefMap);
1244        }/* internal Length Check */
1245        else
1246        {
1247            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
1248                        NdefMap->StdMifareContainer.currentBlock;
1249            NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_READ;
1250            *NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
1251
1252#ifndef PH_HAL4_ENABLE
1253            NdefMap->Cmd.MfCmd = phHal_eMifareCmdListMifareRead;
1254#else
1255            NdefMap->Cmd.MfCmd = phHal_eMifareRead;
1256#endif
1257            /* Call the Overlapped HAL Transceive function */
1258            status = phFriNfc_OvrHal_Transceive(    NdefMap->LowerDevice,
1259                                                    &NdefMap->MapCompletionInfo,
1260                                                    NdefMap->psRemoteDevInfo,
1261                                                    NdefMap->Cmd,
1262                                                    &NdefMap->psDepAdditionalInfo,
1263                                                    NdefMap->SendRecvBuf,
1264                                                    NdefMap->SendLength,
1265                                                    NdefMap->SendRecvBuf,
1266                                                    NdefMap->SendRecvLength);
1267        }
1268    }
1269    else
1270    {
1271        /* Check for the Card Size */
1272        if((((NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
1273            NdefMap->StdMifareContainer.NdefBlocks) *
1274            PH_FRINFC_MIFARESTD_BYTES_READ) == 0) ||
1275            (NdefMap->ApduBufferSize == NdefMap->ApduBuffIndex))
1276        {
1277            NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
1278                (uint8_t)((((NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
1279                NdefMap->StdMifareContainer.NdefBlocks) *
1280                PH_FRINFC_MIFARESTD_BYTES_READ) == 0)?
1281                PH_FRINFC_MIFARESTD_FLAG1:
1282                PH_FRINFC_MIFARESTD_FLAG0);
1283            *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
1284            status = PHNFCSTVAL(CID_NFC_NONE,
1285                                NFCSTATUS_SUCCESS);
1286        }
1287        else
1288        {
1289            /*Error: The control should not ideally come here.
1290                Return Error.*/
1291#ifndef PH_HAL4_ENABLE
1292            status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_CMD_ABORTED);
1293#else
1294            status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_FAILED);
1295#endif
1296        }
1297    }
1298    return status;
1299}
1300
1301
1302static NFCSTATUS phFriNfc_MifStd_H_WrABlock(phFriNfc_NdefMap_t *NdefMap)
1303{
1304    NFCSTATUS               status =    NFCSTATUS_PENDING;
1305
1306    uint16_t                    RemainingBytes = 0,
1307                                BytesRemained = 0,
1308                                index = 0;
1309
1310    uint8_t                     Temp16Bytes = 0;
1311
1312    /* set the data for additional data exchange*/
1313    NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
1314    NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0;
1315    NdefMap->psDepAdditionalInfo.NAD = 0;
1316    NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process;
1317    NdefMap->MapCompletionInfo.Context = NdefMap;
1318    NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE;
1319
1320    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_WRITE;
1321
1322    /* User Buffer Check */
1323    if( NdefMap->ApduBuffIndex <
1324        (uint16_t)NdefMap->ApduBufferSize)
1325    {
1326        RemainingBytes = (((uint16_t)(NdefMap->ApduBufferSize -
1327                         NdefMap->ApduBuffIndex) <
1328                        NdefMap->StdMifareContainer.remainingSize )?
1329                        (uint16_t)(NdefMap->ApduBufferSize -
1330                        NdefMap->ApduBuffIndex):
1331                        NdefMap->StdMifareContainer.remainingSize);
1332
1333        NdefMap->SendRecvBuf[0] = NdefMap->StdMifareContainer.currentBlock;
1334        Temp16Bytes += PH_FRINFC_MIFARESTD_INC_1;
1335        /* Check for internal bytes */
1336
1337        if(NdefMap->StdMifareContainer.internalLength > 0)
1338        {
1339            /* copy the bytes previously written in the internal Buffer */
1340            (void)memcpy(&( NdefMap->SendRecvBuf[Temp16Bytes]),
1341                    NdefMap->StdMifareContainer.internalBuf,
1342                    NdefMap->StdMifareContainer.internalLength);
1343
1344            Temp16Bytes += (uint8_t)(NdefMap->StdMifareContainer.internalLength);
1345            if(RemainingBytes >= (  MIFARE_MAX_SEND_BUF_TO_WRITE -
1346                                    Temp16Bytes))
1347            {
1348                /* Copy the Remaining bytes from the user buffer to make the send
1349                    data and length = 16 */
1350                (void)memcpy(&(NdefMap->SendRecvBuf[Temp16Bytes]),
1351                        NdefMap->ApduBuffer,
1352                        (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes));
1353
1354                NdefMap->NumOfBytesWritten =
1355                    (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes);
1356                Temp16Bytes += (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes);
1357                *NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1);
1358            }
1359            else
1360            {
1361                (void)memcpy(&(NdefMap->SendRecvBuf[Temp16Bytes]),
1362                        NdefMap->ApduBuffer,
1363                        RemainingBytes);
1364
1365                NdefMap->StdMifareContainer.internalBufFlag = PH_FRINFC_MIFARESTD_FLAG1;
1366                NdefMap->NumOfBytesWritten = RemainingBytes;
1367                Temp16Bytes += (uint8_t)(RemainingBytes);
1368                *NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1);
1369
1370                BytesRemained = (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes);
1371                /* Pad empty bytes with Zeroes to complete 16 bytes*/
1372                for(index = 0; index < BytesRemained; index++)
1373                {
1374                    NdefMap->SendRecvBuf[(Temp16Bytes + index)] =
1375                                (uint8_t)((index == PH_FRINFC_MIFARESTD_VAL0)?
1376                                    PH_FRINFC_MIFARESTD_TERMTLV_T:
1377                                    PH_FRINFC_MIFARESTD_NULLTLV_T);
1378                    NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
1379                }
1380                Temp16Bytes += (uint8_t)(BytesRemained);
1381            }
1382        }
1383        else
1384        {
1385            if(RemainingBytes >= (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes))
1386            {
1387                /* Bytes left to write < 16
1388                    copy remaining bytes */
1389                (void)memcpy( &(NdefMap->SendRecvBuf[
1390                        Temp16Bytes]),
1391                        &(NdefMap->ApduBuffer[
1392                        NdefMap->ApduBuffIndex]),
1393                        (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes));
1394
1395                NdefMap->NumOfBytesWritten = (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes);
1396                Temp16Bytes += (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes);
1397                *NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1);
1398            }
1399            else
1400            {
1401                /* Bytes left to write < 16
1402                    copy remaining bytes */
1403                (void)memcpy(&(NdefMap->SendRecvBuf[
1404                        Temp16Bytes]),
1405                        &(NdefMap->ApduBuffer[
1406                        NdefMap->ApduBuffIndex]),
1407                        RemainingBytes);
1408
1409                NdefMap->StdMifareContainer.RemainingBufFlag = PH_FRINFC_MIFARESTD_FLAG1;
1410                NdefMap->NumOfBytesWritten = RemainingBytes;
1411                Temp16Bytes += (uint8_t)(RemainingBytes);
1412                *NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1);
1413
1414
1415                /* Pad empty bytes with Zeroes to complete 16 bytes*/
1416                for(index = Temp16Bytes; index < MIFARE_MAX_SEND_BUF_TO_WRITE; index++)
1417                {
1418                    NdefMap->SendRecvBuf[index] = (uint8_t)((index ==
1419                                    Temp16Bytes)?
1420                                    PH_FRINFC_MIFARESTD_TERMTLV_T:
1421                                    PH_FRINFC_MIFARESTD_NULLTLV_T);
1422
1423                    NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
1424                }
1425            }
1426        }
1427        /* Buffer to store 16 bytes which is writing to the present block */
1428        (void)memcpy( NdefMap->StdMifareContainer.Buffer,
1429                &(NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_INC_1]),
1430                PH_FRINFC_MIFARESTD_BLOCK_BYTES);
1431
1432        /* Write from here */
1433        NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_WRITE;
1434#ifndef PH_HAL4_ENABLE
1435        NdefMap->Cmd.MfCmd = phHal_eMifareCmdListMifareWrite16;
1436#else
1437        NdefMap->Cmd.MfCmd = phHal_eMifareWrite16;
1438#endif
1439        *NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
1440        /* Call the Overlapped HAL Transceive function */
1441        status = phFriNfc_OvrHal_Transceive(    NdefMap->LowerDevice,
1442                                                &NdefMap->MapCompletionInfo,
1443                                                NdefMap->psRemoteDevInfo,
1444                                                NdefMap->Cmd,
1445                                                &NdefMap->psDepAdditionalInfo,
1446                                                NdefMap->SendRecvBuf,
1447                                                NdefMap->SendLength,
1448                                                NdefMap->SendRecvBuf,
1449                                                NdefMap->SendRecvLength);
1450    }
1451    else/* Check User Buffer */
1452    {
1453        if(NdefMap->StdMifareContainer.NdefBlocks >
1454             NdefMap->StdMifareContainer.NoOfNdefCompBlocks)
1455        {
1456            NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
1457                PH_FRINFC_MIFARESTD_FLAG1;
1458            status = PHNFCSTVAL(CID_NFC_NONE,
1459                                NFCSTATUS_SUCCESS);
1460        }
1461        else if( NdefMap->ApduBuffIndex ==
1462            (uint16_t)NdefMap->ApduBufferSize)
1463        {
1464            status = PHNFCSTVAL(CID_NFC_NONE,
1465                                NFCSTATUS_SUCCESS);
1466        }
1467        else
1468        {
1469            /*Error: The control should not ideally come here.
1470              Return Error.*/
1471#ifndef PH_HAL4_ENABLE
1472            status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_CMD_ABORTED);
1473#else
1474            status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_FAILED);
1475#endif
1476        }
1477    }
1478    return status;
1479}
1480
1481static NFCSTATUS phFriNfc_MifStd_H_AuthSector(phFriNfc_NdefMap_t *NdefMap)
1482{
1483    NFCSTATUS                   status = NFCSTATUS_PENDING;
1484
1485    /* set the data for additional data exchange*/
1486    NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
1487    NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0;
1488    NdefMap->psDepAdditionalInfo.NAD = 0;
1489    NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process;
1490    NdefMap->MapCompletionInfo.Context = NdefMap;
1491
1492    *NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
1493    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_AUTH;
1494
1495    /* Authenticate */
1496#ifndef PH_HAL4_ENABLE
1497    NdefMap->Cmd.MfCmd = phHal_eMifareCmdListMifareAuthentA;
1498#else
1499    NdefMap->Cmd.MfCmd = phHal_eMifareAuthentA;
1500#endif
1501    NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
1502                            ((NdefMap->TLVStruct.NdefTLVAuthFlag ==
1503                                PH_FRINFC_MIFARESTD_FLAG1)?
1504                                NdefMap->TLVStruct.NdefTLVBlock:
1505                                NdefMap->StdMifareContainer.currentBlock);
1506
1507    /* if MAD blocks then authentication key is
1508        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 else
1509        0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 */
1510    if(( (NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK0) &&
1511        (NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK1) &&
1512        (NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK2) &&
1513        (NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK64) &&
1514        (NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK65) &&
1515        (NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK66)) ||
1516        (NdefMap->TLVStruct.NdefTLVAuthFlag ==
1517                                (uint8_t)PH_FRINFC_MIFARESTD_FLAG1))
1518    {
1519        NdefMap->SendRecvBuf[1] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */
1520        NdefMap->SendRecvBuf[2] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */
1521        NdefMap->SendRecvBuf[3] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */
1522        NdefMap->SendRecvBuf[4] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */
1523        NdefMap->SendRecvBuf[5] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */
1524        NdefMap->SendRecvBuf[6] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */
1525    }
1526    else
1527    {
1528        NdefMap->SendRecvBuf[1] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT1; /* 0xA0 */
1529        NdefMap->SendRecvBuf[2] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT2; /* 0xA1 */
1530        NdefMap->SendRecvBuf[3] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT3; /* 0xA2 */
1531        NdefMap->SendRecvBuf[4] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT4; /* 0xA3 */
1532        NdefMap->SendRecvBuf[5] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT5; /* 0xA4 */
1533        NdefMap->SendRecvBuf[6] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT6; /* 0xA5 */
1534    }
1535
1536    NdefMap->SendLength = MIFARE_AUTHENTICATE_CMD_LENGTH;
1537    *NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
1538    /* Call the Overlapped HAL Transceive function */
1539    status = phFriNfc_OvrHal_Transceive(    NdefMap->LowerDevice,
1540                                            &NdefMap->MapCompletionInfo,
1541                                            NdefMap->psRemoteDevInfo,
1542                                            NdefMap->Cmd,
1543                                            &NdefMap->psDepAdditionalInfo,
1544                                            NdefMap->SendRecvBuf,
1545                                            NdefMap->SendLength,
1546                                            NdefMap->SendRecvBuf,
1547                                            NdefMap->SendRecvLength);
1548
1549    return status;
1550}
1551
1552static void phFriNfc_MifStd_H_Complete(phFriNfc_NdefMap_t  *NdefMap,
1553                                        NFCSTATUS            Result)
1554{
1555    /* set the state back to the Reset_Init state*/
1556    NdefMap->State =  PH_FRINFC_NDEFMAP_STATE_RESET_INIT;
1557
1558    /* set the completion routine*/
1559    NdefMap->CompletionRoutine[NdefMap->StdMifareContainer.CRIndex].
1560        CompletionRoutine(NdefMap->CompletionRoutine->Context, Result);
1561}
1562
1563static NFCSTATUS phFriNfc_MifStd4k_H_CheckNdef(phFriNfc_NdefMap_t *NdefMap)
1564{
1565    NFCSTATUS Result = NFCSTATUS_SUCCESS;
1566
1567    /* Get the AID Block */
1568    if(NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK2)
1569    {
1570        NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_MAD_BLK64;
1571        NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
1572    }
1573    else if(NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64)
1574    {
1575        NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_MAD_BLK65;
1576    }
1577    else
1578    {
1579        NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_MAD_BLK66;
1580    }
1581
1582    Result = phFriNfc_MifareStdMap_ChkNdef(NdefMap);
1583
1584    return Result;
1585
1586}
1587
1588static void phFriNfc_MifStd_H_fillAIDarray(phFriNfc_NdefMap_t *NdefMap)
1589{
1590    uint8_t     byteindex = 0;
1591
1592    if( (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK1) ||
1593        (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64))
1594    {
1595        /* The First Two Bytes in Receive Buffer
1596            are CRC bytes so it is not copied
1597            instead, 0 is copied in AID[0] & AID[1] */
1598        NdefMap->StdMifareContainer.aid[NdefMap->StdMifareContainer.SectorIndex] =
1599                                    PH_FRINFC_MIFARESTD_NDEF_COMP;
1600        NdefMap->StdMifareContainer.SectorIndex++;
1601        byteindex = 2;
1602    }
1603
1604    while(byteindex < PH_FRINFC_MIFARESTD_BYTES_READ)
1605    {
1606        if((NdefMap->SendRecvBuf[byteindex] == PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL2) &&
1607            (NdefMap->SendRecvBuf[(byteindex + 1)] == PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL1))
1608        {
1609            /* This flag is set when a NFC forum sector is found in a
1610                MAD block for the first time*/
1611            NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG1;
1612            NdefMap->StdMifareContainer.aid[NdefMap->StdMifareContainer.SectorIndex] =
1613                                        PH_FRINFC_MIFARESTD_NDEF_COMP;
1614            NdefMap->StdMifareContainer.SectorIndex++;
1615        }
1616        else
1617        {
1618            NdefMap->StdMifareContainer.aid[NdefMap->StdMifareContainer.SectorIndex] =
1619                                    PH_FRINFC_MIFARESTD_NON_NDEF_COMP;
1620            NdefMap->StdMifareContainer.SectorIndex++;
1621            /* AID complete flag is set when a non NFC forum sector is found in a
1622                MAD block after the NFC forum sector. After setting this, all other
1623                values are ignored and are NOT NDEF compliant */
1624            NdefMap->StdMifareContainer.aidCompleteFlag =
1625                ((NdefMap->StdMifareContainer.NFCforumSectFlag ==
1626                    PH_FRINFC_MIFARESTD_FLAG1)?
1627                    PH_FRINFC_MIFARESTD_FLAG1:
1628                    PH_FRINFC_MIFARESTD_FLAG0);
1629
1630            NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0;
1631            if(NdefMap->StdMifareContainer.aidCompleteFlag == PH_FRINFC_MIFARESTD_FLAG1)
1632            {
1633                break;
1634            }
1635        }
1636        byteindex += 2;
1637    }
1638
1639    /* If "aidCompleteFlag" is set then the remaining sectors are made NOT
1640        NDEF compliant */
1641    if((NdefMap->StdMifareContainer.aidCompleteFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
1642        (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD))
1643    {
1644        /* for Mifare 1k there are 16 sectors, till this number all sectors
1645            are made NOT NDEF compliant */
1646        for(byteindex = NdefMap->StdMifareContainer.SectorIndex;
1647            byteindex < PH_FRINFC_MIFARESTD1K_TOTAL_SECTOR;
1648            byteindex++)
1649        {
1650             NdefMap->StdMifareContainer.aid[byteindex] =
1651                                    PH_FRINFC_MIFARESTD_NON_NDEF_COMP;
1652        }
1653    }
1654    else
1655    {
1656        /* for Mifare 4k there are 40 sectors, till this number all sectors
1657            are made NOT NDEF compliant */
1658        if((NdefMap->StdMifareContainer.aidCompleteFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
1659            (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD))
1660        {
1661            for(byteindex = NdefMap->StdMifareContainer.SectorIndex;
1662                byteindex < PH_FRINFC_MIFARESTD4K_TOTAL_SECTOR;
1663                byteindex++)
1664            {
1665                NdefMap->StdMifareContainer.aid[byteindex] =
1666                                        PH_FRINFC_MIFARESTD_NON_NDEF_COMP;
1667            }
1668        }
1669    }
1670}
1671
1672static NFCSTATUS phFriNfc_MifStd_H_BlkChk(phFriNfc_NdefMap_t *NdefMap)
1673{
1674    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
1675    uint8_t     SectorID = 0, callbreak = 0;
1676
1677    for(;;)
1678    {
1679        /* Get a Sector ID for the Current Block */
1680        SectorID = phFriNfc_MifStd_H_GetSect(NdefMap->StdMifareContainer.currentBlock);
1681        /* Check the card Type 1k or 4k */
1682        /* enter if Mifare 1k card. For Mifare 4k go to else */
1683        if(NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD)
1684        {
1685            /* if Sector Id > 15 No Sectors to write */
1686            if(SectorID > 15)
1687            {
1688                SectorID = phFriNfc_MifStd_H_GetSect(NdefMap->StdMifareContainer.currentBlock);
1689                /*Error: No Ndef Compliant Sectors present.*/
1690                Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1691                                    NFCSTATUS_INVALID_PARAMETER);
1692                callbreak = 1;
1693            }
1694            else
1695            {
1696                phFriNfc_MifStd1k_H_BlkChk(NdefMap, SectorID, &callbreak);
1697            }
1698        } /* End of if */ /* End of Mifare 1k check */
1699        else /* Mifare 4k check starts here */
1700        {
1701            /* Sector > 39 no ndef compliant sectors found*/
1702            if(SectorID > PH_FRINFC_MIFARESTD_SECTOR_NO39)
1703            {
1704                /*Error: No Ndef Compliant Sectors present.*/
1705                Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
1706                                    NFCSTATUS_INVALID_PARAMETER);
1707                callbreak = 1;
1708            }
1709            else if(NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64)
1710            {
1711                NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4;
1712            }
1713            else if(SectorID < PH_FRINFC_MIFARESTD_SECTOR_NO32) /* sector < 32 contains 4 blocks in each sector */
1714            {
1715                /* If the block checked is 63, the 3 blocks after this
1716                    are AID(MAD) blocks so its need to be skipped */
1717                if(NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK63)
1718                {
1719                    NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4;
1720                }
1721                else
1722                {
1723                    phFriNfc_MifStd1k_H_BlkChk(NdefMap, SectorID, &callbreak);
1724                }
1725            }
1726            else
1727            {
1728                /* every last block of a sector needs to be skipped */
1729                if(((NdefMap->StdMifareContainer.currentBlock + 1) %
1730                    PH_FRINFC_MIFARESTD_BLOCK_BYTES) == 0)
1731                {
1732                    NdefMap->StdMifareContainer.currentBlock++;
1733                }
1734                else
1735                {
1736                    if(NdefMap->StdMifareContainer.aid[SectorID] ==
1737                            PH_FRINFC_MIFARESTD_NDEF_COMP)
1738                    {
1739                        /* Check whether the block is first block of a (next)new sector and
1740                            also check if it is first block then internal length is zero
1741                            or not. Because once Authentication is done for the sector again
1742                            we should not authenticate it again */
1743                        /* In this case 32 sectors contains 4 blocks and next remaining 8 sectors
1744                            contains 16 blocks that is why (32 * 4) + (sectorID - 32) *16*/
1745                        if((NdefMap->StdMifareContainer.currentBlock ==
1746                            ((PH_FRINFC_MIFARESTD_SECTOR_NO32 * PH_FRINFC_MIFARESTD_BLK4) +
1747                            ((SectorID - PH_FRINFC_MIFARESTD_SECTOR_NO32) * PH_FRINFC_MIFARESTD_BLOCK_BYTES))) &&
1748                            (NdefMap->StdMifareContainer.internalLength == 0))
1749                        {
1750                            NdefMap->StdMifareContainer.AuthDone = 0;
1751                        }
1752                        callbreak = 1;
1753                    }
1754                    else
1755                    {
1756                        NdefMap->StdMifareContainer.currentBlock += 16;
1757                    }
1758                }
1759            }
1760        }
1761        if(callbreak == 1)
1762        {
1763            break;
1764        }
1765    }
1766
1767    return Result;
1768}
1769
1770static uint8_t phFriNfc_MifStd_H_GetSect(uint8_t  BlockNumber)
1771{
1772    uint8_t SectorID = 0;
1773
1774    if(BlockNumber >= PH_FRINFC_MIFARESTD4K_BLK128)
1775    {
1776        SectorID = (uint8_t)(PH_FRINFC_MIFARESTD_SECTOR_NO32 +
1777                    ((BlockNumber - PH_FRINFC_MIFARESTD4K_BLK128)/
1778                    PH_FRINFC_MIFARESTD_BLOCK_BYTES));
1779    }
1780    else
1781    {
1782        SectorID = (BlockNumber/PH_FRINFC_MIFARESTD_BLK4);
1783    }
1784    return SectorID;
1785}
1786
1787static NFCSTATUS phFriNfc_MifStd_H_RdAcsBit(phFriNfc_NdefMap_t *NdefMap)
1788{
1789    NFCSTATUS Result = NFCSTATUS_SUCCESS;
1790    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_ACS_BIT;
1791
1792    if(NdefMap->StdMifareContainer.ReadAcsBitFlag == PH_FRINFC_MIFARESTD_FLAG1)
1793    {
1794        /* Get the sector trailer */
1795        ((NdefMap->StdMifareContainer.currentBlock > 127)?
1796            phFriNfc_MifStd_H_Get4kStTrail(NdefMap):
1797            phFriNfc_MifStd_H_Get1kStTrail(NdefMap));
1798    }
1799    else
1800    {
1801        /* Give the current block to read */
1802        NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
1803                        NdefMap->StdMifareContainer.currentBlock;
1804    }
1805
1806    Result = phFriNfc_MifStd_H_Rd16Bytes(NdefMap,
1807                        NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0]);
1808
1809    return Result;
1810}
1811
1812static NFCSTATUS phFriNfc_MifStd_H_ChkAcsBit(phFriNfc_NdefMap_t *NdefMap)
1813{
1814    NFCSTATUS Result = NFCSTATUS_SUCCESS;
1815
1816    /* Blocks from 0 to 3 and from 64 to 67(MAD blocks) */
1817    if((NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK0) ||
1818        (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK1) ||
1819        (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK2) ||
1820        (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK3) ||
1821        (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64) ||
1822        (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK65) ||
1823        (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK66) )
1824    {
1825        /* Access bits check removed for the MAD blocks */
1826#ifdef ENABLE_ACS_BIT_CHK_FOR_MAD
1827
1828        if(((NdefMap->SendRecvBuf[
1829            PH_FRINFC_MIFARESTD_VAL6] &
1830            PH_FRINFC_MIFARESTD_MASK_FF) ==
1831            PH_FRINFC_MIFARESTD_MADSECT_ACS_BYTE6) &&
1832            ((NdefMap->SendRecvBuf[
1833            PH_FRINFC_MIFARESTD_VAL7] &
1834            PH_FRINFC_MIFARESTD_MASK_FF) ==
1835            PH_FRINFC_MIFARESTD_MADSECT_ACS_BYTE7) &&
1836            ((NdefMap->SendRecvBuf[
1837            PH_FRINFC_MIFARESTD_VAL8] &
1838            PH_FRINFC_MIFARESTD_MASK_FF) ==
1839            PH_FRINFC_MIFARESTD_ACS_BYTE8))
1840        {
1841            NdefMap->StdMifareContainer.WriteFlag =
1842                PH_FRINFC_MIFARESTD_FLAG0;
1843            NdefMap->StdMifareContainer.ReadFlag =
1844                PH_FRINFC_MIFARESTD_FLAG1;
1845        }
1846        else
1847        {
1848            NdefMap->StdMifareContainer.WriteFlag =
1849                PH_FRINFC_MIFARESTD_FLAG0;
1850            NdefMap->StdMifareContainer.ReadFlag =
1851                PH_FRINFC_MIFARESTD_FLAG0;
1852        }
1853
1854#else /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */
1855
1856        NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INITIALIZED;
1857
1858#endif /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */
1859    }
1860    else
1861    {
1862        /* Check for Access bytes 6, 7 and 8 value =
1863            0x7F, 0x07, 0x88 NFC forum sectors*/
1864        if(((NdefMap->SendRecvBuf[
1865            PH_FRINFC_MIFARESTD_VAL6] &
1866            PH_FRINFC_MIFARESTD_MASK_FF) ==
1867            PH_FRINFC_MIFARESTD_NFCSECT_ACS_BYTE6) &&
1868            ((NdefMap->SendRecvBuf[
1869            PH_FRINFC_MIFARESTD_VAL7] &
1870            PH_FRINFC_MIFARESTD_MASK_FF) ==
1871            PH_FRINFC_MIFARESTD_NFCSECT_ACS_BYTE7) &&
1872            ((NdefMap->SendRecvBuf[
1873            PH_FRINFC_MIFARESTD_VAL8] &
1874            PH_FRINFC_MIFARESTD_MASK_FF) ==
1875            PH_FRINFC_MIFARESTD_ACS_BYTE8))
1876        {
1877            NdefMap->StdMifareContainer.WriteFlag =
1878                PH_FRINFC_MIFARESTD_FLAG1;
1879            NdefMap->StdMifareContainer.ReadFlag =
1880                PH_FRINFC_MIFARESTD_FLAG1;
1881        }
1882        else if(((NdefMap->SendRecvBuf[
1883            PH_FRINFC_MIFARESTD_VAL6] &
1884            PH_FRINFC_MIFARESTD_MASK_FF) ==
1885            PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE6) &&
1886            ((NdefMap->SendRecvBuf[
1887            PH_FRINFC_MIFARESTD_VAL7] &
1888            PH_FRINFC_MIFARESTD_MASK_FF) ==
1889            PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE7) &&
1890            ((NdefMap->SendRecvBuf[
1891            PH_FRINFC_MIFARESTD_VAL8] &
1892            PH_FRINFC_MIFARESTD_MASK_FF) ==
1893            PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE8))
1894        {
1895            /* Read Only state */
1896            /* Check for Access bytes 6, 7 and 8 value =
1897                0x55, 0xAD, 0x2A NFC forum Sectors*/
1898            NdefMap->StdMifareContainer.WriteFlag =
1899                PH_FRINFC_MIFARESTD_FLAG0;
1900            NdefMap->StdMifareContainer.ReadFlag =
1901                PH_FRINFC_MIFARESTD_FLAG1;
1902        }
1903        else
1904        {
1905            NdefMap->StdMifareContainer.WriteFlag =
1906                PH_FRINFC_MIFARESTD_FLAG0;
1907            NdefMap->StdMifareContainer.ReadFlag =
1908                PH_FRINFC_MIFARESTD_FLAG0;
1909        }
1910
1911#ifdef ENABLE_ACS_BIT_CHK_FOR_MAD
1912
1913        /* Do nothing */
1914
1915#else /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */
1916
1917        Result = phFriNfc_MifStd_H_GPBChk(NdefMap);
1918
1919#endif /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */
1920
1921    }
1922
1923#ifdef ENABLE_ACS_BIT_CHK_FOR_MAD
1924
1925    Result = phFriNfc_MifStd_H_GPBChk(NdefMap);
1926
1927#endif /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */
1928
1929    return Result;
1930}
1931
1932static NFCSTATUS phFriNfc_MifStd_H_ChkRdWr(phFriNfc_NdefMap_t *NdefMap)
1933{
1934    NFCSTATUS Result = NFCSTATUS_SUCCESS;
1935    switch(NdefMap->PrevOperation)
1936    {
1937        case PH_FRINFC_NDEFMAP_CHECK_OPE:
1938            if(NdefMap->CardState ==
1939                PH_NDEFMAP_CARD_STATE_INVALID)
1940            {
1941                    /* No permission to read */
1942                Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
1943                                    NFCSTATUS_READ_FAILED);
1944            }
1945            else if((NdefMap->StdMifareContainer.currentBlock > 3) &&
1946                (NdefMap->StdMifareContainer.ChkNdefCompleteFlag ==
1947                                    PH_FRINFC_MIFARESTD_FLAG1) &&
1948                (NdefMap->StdMifareContainer.currentBlock !=
1949                            PH_FRINFC_MIFARESTD_MAD_BLK65) &&
1950                (NdefMap->StdMifareContainer.currentBlock !=
1951                            PH_FRINFC_MIFARESTD_MAD_BLK66))
1952            {
1953               /* NdefMap->StdMifareContainer.currentBlock =
1954                    ((NdefMap->StdMifareContainer.ReadCompleteFlag ==
1955                        PH_FRINFC_MIFARESTD_FLAG1)?
1956                        NdefMap->StdMifareContainer.currentBlock:
1957                        (NdefMap->StdMifareContainer.currentBlock +
1958                        PH_FRINFC_MIFARESTD_VAL4));
1959                NdefMap->StdMifareContainer.currentBlock =
1960                    ((NdefMap->StdMifareContainer.currentBlock ==
1961                        PH_FRINFC_MIFARESTD_MAD_BLK64)?
1962                    (NdefMap->StdMifareContainer.currentBlock +
1963                    PH_FRINFC_MIFARESTD_VAL4):
1964                    NdefMap->StdMifareContainer.currentBlock);*/
1965
1966                    Result = ((NdefMap->StdMifareContainer.ReadAcsBitFlag ==
1967                                PH_FRINFC_MIFARESTD_FLAG0)?
1968                                phFriNfc_MifStd_H_RdAcsBit(NdefMap):
1969                                phFriNfc_MifStd_H_AuthSector(NdefMap));
1970            }
1971            else
1972            {
1973                Result = phFriNfc_MifareStdMap_ChkNdef(NdefMap);
1974            }
1975            break;
1976
1977        case PH_FRINFC_NDEFMAP_READ_OPE:
1978            if(NdefMap->CardState ==
1979                PH_NDEFMAP_CARD_STATE_INVALID)
1980            {
1981                /* No permission to Read */
1982                Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
1983                                    NFCSTATUS_READ_FAILED);
1984            }
1985            else if(NdefMap->StdMifareContainer.ReadNdefFlag ==
1986                    PH_FRINFC_MIFARESTD_FLAG1)
1987            {
1988                Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap);
1989            }
1990            else
1991            {
1992                Result = phFriNfc_MifStd_H_RdABlock(NdefMap);
1993            }
1994            break;
1995
1996        case PH_FRINFC_NDEFMAP_WRITE_OPE:
1997            if((NdefMap->CardState ==
1998                PH_NDEFMAP_CARD_STATE_INVALID) ||
1999                (NdefMap->CardState ==
2000                PH_NDEFMAP_CARD_STATE_READ_ONLY))
2001            {
2002                /* No permission to Read */
2003                Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
2004                                    NFCSTATUS_WRITE_FAILED);
2005            }
2006            else if(NdefMap->StdMifareContainer.WrNdefFlag ==
2007                    PH_FRINFC_MIFARESTD_FLAG1)
2008            {
2009                Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap);
2010            }
2011            else if(NdefMap->StdMifareContainer.RdBeforeWrFlag ==
2012                    PH_FRINFC_MIFARESTD_FLAG1)
2013            {
2014                /*NdefMap->StdMifareContainer.ReadFlag =
2015                                PH_FRINFC_MIFARESTD_FLAG0;*/
2016                Result = phFriNfc_MifStd_H_RdBeforeWr(NdefMap);
2017            }
2018            else if(NdefMap->StdMifareContainer.RdAfterWrFlag ==
2019                    PH_FRINFC_MIFARESTD_FLAG1)
2020            {
2021                Result = phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap);
2022            }
2023            else
2024            {
2025                Result = (((NdefMap->TLVStruct.NdefTLVBlock ==
2026                            NdefMap->StdMifareContainer.currentBlock) &&
2027                            (NdefMap->Offset ==
2028                            PH_FRINFC_NDEFMAP_SEEK_BEGIN))?
2029                            phFriNfc_MifStd_H_RdBeforeWr(NdefMap):
2030                            phFriNfc_MifStd_H_WrABlock(NdefMap));
2031            }
2032            break;
2033
2034        case PH_FRINFC_NDEFMAP_GET_ACTSIZE_OPE:
2035            Result = ((NdefMap->StdMifareContainer.ReadFlag ==
2036                        PH_FRINFC_MIFARESTD_FLAG0)?
2037                        (PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
2038                        NFCSTATUS_READ_FAILED)):
2039                        phFriNfc_MifStd_H_GetActCardLen(NdefMap));
2040            break;
2041
2042        default:
2043            /* Operation is not correct */
2044            Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
2045                                NFCSTATUS_INVALID_PARAMETER);
2046
2047            break;
2048    }
2049    return Result;
2050}
2051
2052static void phFriNfc_MifStd_H_ChkNdefCmpltSects(phFriNfc_NdefMap_t *NdefMap)
2053{
2054    uint8_t index = 0;
2055    if(NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD)
2056    {
2057        for(index = 0; index < PH_FRINFC_MIFARESTD4K_TOTAL_SECTOR; index++)
2058        {
2059            /* For Mifare 4k, Block 0 to 31 contains 4 blocks */
2060            /* sector 0 and 15 are aid blocks */
2061            if(index != PH_FRINFC_MIFARESTD_SECTOR_NO16)
2062            {
2063                if(( (index < 32) && (index != PH_FRINFC_MIFARESTD_SECTOR_NO0))
2064                && (NdefMap->StdMifareContainer.aid[index] ==
2065                            PH_FRINFC_MIFARESTD_NON_NDEF_COMP))
2066                {
2067                    /* Only 3 blocks can be read/written till sector 31 */
2068                    NdefMap->StdMifareContainer.NoOfNdefCompBlocks -=
2069                                                PH_FRINFC_MIFARESTD_MAD_BLK3;
2070
2071                }
2072                else
2073                {
2074                    /* For Mifare 4k, Block 32 to 39 contains 16 blocks */
2075                    if(NdefMap->StdMifareContainer.aid[index] ==
2076                        PH_FRINFC_MIFARESTD_NON_NDEF_COMP)
2077                    {
2078                        /* Only 15 blocks can be read/written from sector 31 */
2079                        NdefMap->StdMifareContainer.NoOfNdefCompBlocks -=
2080                                                PH_FRINFC_MIFARESTD_BLK15;
2081                    }
2082                }
2083            }
2084        } /* For index > 40 */
2085    }
2086    else
2087    {
2088        for(index = PH_FRINFC_MIFARESTD_SECTOR_NO1;
2089            index < PH_FRINFC_MIFARESTD_SECTOR_NO16; index++)
2090        {
2091            if(NdefMap->StdMifareContainer.aid[index] ==
2092                PH_FRINFC_MIFARESTD_NON_NDEF_COMP)
2093            {
2094                /*  Only three blocks can be read/written in
2095                    a sector. So if a sector is non-ndef
2096                    compliant, decrement 3 */
2097                NdefMap->StdMifareContainer.NoOfNdefCompBlocks -=
2098                                        PH_FRINFC_MIFARESTD_MAD_BLK3;
2099            }
2100        }
2101    }
2102}
2103
2104#if 0
2105static uint8_t phFriNfc_MifStd_H_ChkNdefLen(phFriNfc_NdefMap_t *NdefMap,
2106                                        uint8_t             NDEFlength,
2107                                        uint8_t             *CRFlag)
2108{
2109    uint8_t     NoLengthBytesinTLV = 0,
2110                TempBuffer[16] = {0};
2111    uint16_t    TempLength = 0;
2112
2113    TempLength = ((NdefMap->StdMifareContainer.internalLength > 0)?
2114                NdefMap->StdMifareContainer.internalLength :
2115                PH_FRINFC_MIFARESTD_BYTES_READ);
2116
2117    if(NdefMap->StdMifareContainer.internalLength > 0)
2118    {
2119        (void)memcpy( TempBuffer,
2120                NdefMap->StdMifareContainer.internalBuf,
2121                TempLength);
2122    }
2123    else
2124    {
2125        (void)memcpy( TempBuffer,
2126                NdefMap->SendRecvBuf,
2127                TempLength);
2128    }
2129
2130    /* if number of bytes for length (L) in TLV is greater than 0*/
2131    if(NdefMap->TLVStruct.NoLbytesinTLV != PH_FRINFC_MIFARESTD_VAL0)
2132    {
2133        switch(NdefMap->TLVStruct.NoLbytesinTLV)
2134        {
2135            /* there are two bytes remaining in the length (L) of TLV */
2136            case PH_FRINFC_MIFARESTD_VAL2:
2137                NoLengthBytesinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES2;
2138                /* Get the value of the length (L) of the TLV */
2139                NdefMap->TLVStruct.BytesRemainLinTLV = *TempBuffer;
2140                NdefMap->TLVStruct.BytesRemainLinTLV =
2141                        ((NdefMap->TLVStruct.BytesRemainLinTLV
2142                        << PH_FRINFC_MIFARESTD_LEFTSHIFT8) + (TempBuffer[
2143                        PH_FRINFC_MIFARESTD_INC_1]));
2144
2145                *CRFlag = (uint8_t)((NdefMap->TLVStruct.BytesRemainLinTLV <
2146                            PH_FRINFC_MIFARESTD_NDEFTLV_L)?
2147                            PH_FRINFC_MIFARESTD_FLAG1 :
2148                            PH_FRINFC_MIFARESTD_FLAG0);
2149                NdefMap->TLVStruct.BytesRemainLinTLV = ((*CRFlag ==
2150                                    PH_FRINFC_MIFARESTD_FLAG1)?
2151                                    PH_FRINFC_MIFARESTD_VAL0:
2152                                    NdefMap->TLVStruct.BytesRemainLinTLV);
2153                break;
2154
2155            /* there is only one byte remaining in the length (L) of TLV */
2156            case PH_FRINFC_MIFARESTD_VAL1:
2157                NoLengthBytesinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES1;
2158                /* Get the value of the length (L) of the TLV */
2159                NdefMap->TLVStruct.BytesRemainLinTLV =
2160                            NdefMap->TLVStruct.prevLenByteValue;
2161
2162                NdefMap->TLVStruct.BytesRemainLinTLV =
2163                        ((NdefMap->TLVStruct.BytesRemainLinTLV
2164                        << PH_FRINFC_MIFARESTD_LEFTSHIFT8) + (*TempBuffer));
2165                *CRFlag = (uint8_t)((NdefMap->TLVStruct.BytesRemainLinTLV <
2166                            PH_FRINFC_MIFARESTD_NDEFTLV_L)?
2167                            PH_FRINFC_MIFARESTD_FLAG1 :
2168                            PH_FRINFC_MIFARESTD_FLAG0);
2169
2170                NdefMap->TLVStruct.BytesRemainLinTLV = ((*CRFlag ==
2171                                    PH_FRINFC_MIFARESTD_FLAG1)?
2172                                    PH_FRINFC_MIFARESTD_VAL0:
2173                                    NdefMap->TLVStruct.BytesRemainLinTLV);
2174                break;
2175
2176            default:
2177                break;
2178        }
2179        NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0;
2180        NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2181        NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2182    }
2183    else if(TempBuffer[NDEFlength] <
2184                PH_FRINFC_MIFARESTD_NDEFTLV_L)
2185    {
2186        NoLengthBytesinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES1;
2187        /* Get the value of the length (L) of the TLV */
2188        NdefMap->TLVStruct.BytesRemainLinTLV =
2189                                TempBuffer[NDEFlength];
2190
2191        /* if NDEFLength has reached 16 bytes, then the next coming byte is not of type in TLV  */
2192        NdefMap->TLVStruct.TcheckedinTLVFlag = (((NDEFlength + PH_FRINFC_MIFARESTD_INC_1)
2193                                                    == PH_FRINFC_MIFARESTD_BYTES_READ)?
2194                                                    PH_FRINFC_MIFARESTD_FLAG1 :
2195                                                    PH_FRINFC_MIFARESTD_FLAG0);
2196        /* if NDEFLength has reached 16 bytes, then the next coming byte is not of length in TLV */
2197        NdefMap->TLVStruct.LcheckedinTLVFlag = (((NDEFlength + PH_FRINFC_MIFARESTD_INC_1)
2198                                                        == PH_FRINFC_MIFARESTD_BYTES_READ)?
2199                                                        PH_FRINFC_MIFARESTD_FLAG1 :
2200                                                        PH_FRINFC_MIFARESTD_FLAG0);
2201    }
2202    else
2203    {
2204        if((NDEFlength + PH_FRINFC_MIFARESTD_INC_1)
2205            == PH_FRINFC_MIFARESTD_BYTES_READ)
2206        {
2207            NoLengthBytesinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES1;
2208            /* Number of bytes left for length (L) field in TLV */
2209            NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_MIFARESTD_VAL2;
2210            /* if NDEFLength has reached 16 bytes, then the next coming byte is not of type in TLV  */
2211            NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
2212            NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2213        }
2214        else if((NDEFlength + PH_FRINFC_MIFARESTD_INC_2)
2215                == PH_FRINFC_MIFARESTD_BYTES_READ)
2216        {
2217            NoLengthBytesinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES2;
2218            /* Number of bytes left for length (L) field in TLV */
2219            NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_MIFARESTD_VAL1;
2220            /* if NDEFLength has reached 16 bytes, then the next byte is not of type in TLV  */
2221            NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
2222            NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2223            /* copy the value of the length field */
2224            NdefMap->TLVStruct.prevLenByteValue =
2225                NdefMap->SendRecvBuf[(NDEFlength + PH_FRINFC_MIFARESTD_INC_1)];
2226        }
2227        else
2228        {
2229            /* if NDEFLength has reached 16 bytes, then the next byte is not of type in TLV  */
2230            NdefMap->TLVStruct.TcheckedinTLVFlag = (((NDEFlength + PH_FRINFC_MIFARESTD_INC_3)
2231                                                        == PH_FRINFC_MIFARESTD_BYTES_READ)?
2232                                                        PH_FRINFC_MIFARESTD_FLAG1 :
2233                                                        PH_FRINFC_MIFARESTD_FLAG0);
2234
2235            /* if NDEFLength has reached 16 bytes, then the next byte is not of length in TLV */
2236            NdefMap->TLVStruct.LcheckedinTLVFlag = (((NDEFlength + PH_FRINFC_MIFARESTD_INC_1)
2237                                                            == PH_FRINFC_MIFARESTD_BYTES_READ)?
2238                                                            PH_FRINFC_MIFARESTD_FLAG1 :
2239                                                            PH_FRINFC_MIFARESTD_FLAG0);
2240
2241            /* Number of bytes left for length (L) field in TLV */
2242            NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_MIFARESTD_VAL0;
2243
2244            NoLengthBytesinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES3;
2245            /* Get the value of the length (L) of the TLV */
2246            NdefMap->TLVStruct.BytesRemainLinTLV =
2247                                            TempBuffer[(NDEFlength +
2248                                            PH_FRINFC_MIFARESTD_INC_1)];
2249
2250            NdefMap->TLVStruct.BytesRemainLinTLV =
2251                    ((NdefMap->TLVStruct.BytesRemainLinTLV
2252                    << PH_FRINFC_MIFARESTD_LEFTSHIFT8) + TempBuffer[
2253                    (NDEFlength +PH_FRINFC_MIFARESTD_INC_2)]);
2254
2255            *CRFlag = (uint8_t)((NdefMap->TLVStruct.BytesRemainLinTLV <
2256                            PH_FRINFC_MIFARESTD_NDEFTLV_L)?
2257                            PH_FRINFC_MIFARESTD_FLAG1 :
2258                            PH_FRINFC_MIFARESTD_FLAG0);
2259        }
2260    }
2261    return NoLengthBytesinTLV;
2262}
2263#endif
2264#if 0
2265static NFCSTATUS phFriNfc_MifStd_H_RdNdefTLV(phFriNfc_NdefMap_t *NdefMap,
2266                                         uint8_t            *Temp16Bytes)
2267{
2268    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
2269    uint8_t     NDEFlength = 0,
2270                RdFlag = 0,
2271                CRFlag = 0;
2272    uint16_t    RemainingBytes = 0;
2273
2274    while(PH_FRINFC_MIFARESTD_FLAG1 == NdefMap->TLVStruct.NdefTLVFoundFlag)
2275    {
2276        RemainingBytes = ((uint16_t)NdefMap->ApduBufferSize -
2277                            NdefMap->ApduBuffIndex);
2278        if((((  NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
2279            NdefMap->StdMifareContainer.NdefBlocks) *
2280            PH_FRINFC_MIFARESTD_BLOCK_BYTES) +
2281            PH_FRINFC_MIFARESTD_BLOCK_BYTES) <
2282            RemainingBytes)
2283        {
2284            /* If the user Buffer is greater than the Card Size
2285                set LastBlockFlag = 1. This Flag is used to read bytes
2286                till the end of the card only */
2287            RemainingBytes = ((( NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
2288                                NdefMap->StdMifareContainer.NdefBlocks) * 16) +
2289                                NdefMap->StdMifareContainer.internalLength);
2290
2291        }
2292        /* Ndef TLV flag to 0 */
2293        NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
2294
2295        if((NdefMap->TLVStruct.TcheckedinTLVFlag != PH_FRINFC_MIFARESTD_FLAG1) &&
2296            (NdefMap->TLVStruct.LcheckedinTLVFlag != PH_FRINFC_MIFARESTD_FLAG1) &&
2297            (NdefMap->TLVStruct.NoLbytesinTLV != PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0))
2298        {
2299            CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
2300            break;
2301        }
2302
2303        /* if type (T) of TLV flag is not set then only the below check is valid
2304            because, for eg. assume, type (T) of TLV flag is set then this means
2305            that it is already checked from the previous 16 bytes data (T is the
2306            last byte of 16 bytes */
2307        if(NdefMap->TLVStruct.TcheckedinTLVFlag != PH_FRINFC_MIFARESTD_FLAG1)
2308        {
2309            CRFlag = ((NdefMap->SendRecvBuf[(*Temp16Bytes)]!=
2310                        PH_FRINFC_MIFARESTD_NDEFTLV_T)?
2311                        PH_FRINFC_MIFARESTD_FLAG1:
2312                        PH_FRINFC_MIFARESTD_FLAG0);
2313
2314            if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
2315            {
2316                NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
2317                                    PH_FRINFC_MIFARESTD_FLAG1;
2318                RdFlag = PH_FRINFC_MIFARESTD_FLAG1;
2319                break;
2320            }
2321            /* skip Type T of the TLV */
2322            *Temp16Bytes += PH_FRINFC_MIFARESTD_INC_1;
2323        }
2324        else
2325        {
2326            NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2327        }
2328
2329        if((*Temp16Bytes + PH_FRINFC_MIFARESTD_VAL1) == PH_FRINFC_MIFARESTD_BYTES_READ)
2330        {
2331            NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
2332            NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
2333            /* 16 bytes completed */
2334            NdefMap->StdMifareContainer.currentBlock++;
2335            NdefMap->StdMifareContainer.NdefBlocks++;
2336            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
2337            if(Result == NFCSTATUS_SUCCESS)
2338            {
2339                if(NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)
2340                {
2341                    Result = phFriNfc_MifStd_H_RdABlock(NdefMap);
2342                }
2343                else
2344                {
2345                    Result = phFriNfc_MifStd_H_AuthSector(NdefMap);
2346                }
2347            }
2348            break;
2349        }
2350        /* skip number of bytes taken by L in TLV, if L is already read */
2351        if(NdefMap->TLVStruct.LcheckedinTLVFlag != PH_FRINFC_MIFARESTD_FLAG1)
2352        {
2353            /*CRFlag = (((*(NdefMap->SendRecvBuf + *Temp16Bytes) ==
2354                        PH_FRINFC_MIFARESTD_NDEFTLV_L0) &&
2355                        (NdefMap->TLVStruct.NoLbytesinTLV ==
2356                        PH_FRINFC_MIFARESTD_VAL0))?
2357                        PH_FRINFC_MIFARESTD_FLAG1:
2358                        PH_FRINFC_MIFARESTD_FLAG0);*/
2359
2360            NDEFlength = *Temp16Bytes;
2361            *Temp16Bytes += phFriNfc_MifStd_H_ChkNdefLen(NdefMap,
2362                                                            NDEFlength,
2363                                                            &CRFlag);
2364            if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
2365            {
2366                NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
2367                                    PH_FRINFC_MIFARESTD_FLAG1;
2368                break;
2369            }
2370
2371        }
2372        else
2373        {
2374            NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2375        }
2376
2377        if(*Temp16Bytes == PH_FRINFC_MIFARESTD_BYTES_READ)
2378        {
2379            NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
2380            NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
2381            /* 16 bytes completed */
2382            NdefMap->StdMifareContainer.currentBlock++;
2383            NdefMap->StdMifareContainer.NdefBlocks++;
2384            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
2385            if(Result == NFCSTATUS_SUCCESS)
2386            {
2387                if(NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)
2388                {
2389                    Result = phFriNfc_MifStd_H_RdABlock(NdefMap);
2390                }
2391                else
2392                {
2393                    Result = phFriNfc_MifStd_H_AuthSector(NdefMap);
2394                }
2395            }
2396            break;
2397        }
2398
2399        if((NdefMap->TLVStruct.BytesRemainLinTLV <=
2400            (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes)) &&
2401            (RemainingBytes <=
2402                NdefMap->TLVStruct.BytesRemainLinTLV))
2403        {
2404            /* Copy data to user buffer */
2405            (void)memcpy(&(NdefMap->ApduBuffer[
2406                    NdefMap->ApduBuffIndex]),
2407                    &(NdefMap->SendRecvBuf[
2408                    (*Temp16Bytes)]),
2409                    RemainingBytes);
2410            NdefMap->ApduBuffIndex += RemainingBytes;
2411            *Temp16Bytes += ((uint8_t)RemainingBytes);
2412            NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
2413
2414            if(NdefMap->TLVStruct.BytesRemainLinTLV == 0)
2415            {
2416                NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
2417                NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2418                NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2419            }
2420            else
2421            {
2422                /* copy the bytes to internal buffer that are read,
2423                    but not used for the user buffer */
2424                (void)memcpy(NdefMap->StdMifareContainer.internalBuf,
2425                    &(NdefMap->SendRecvBuf[(*Temp16Bytes)]),
2426                    (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes));
2427
2428                /* TempLength of the internal buffer */
2429                NdefMap->StdMifareContainer.internalLength =
2430                            (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes);
2431            }
2432            CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
2433            break;
2434        }
2435        else if((NdefMap->TLVStruct.BytesRemainLinTLV <=
2436            (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes)) &&
2437            (RemainingBytes >
2438                NdefMap->TLVStruct.BytesRemainLinTLV))
2439        {
2440            /* Copy data to user buffer */
2441            (void)memcpy(&(NdefMap->ApduBuffer[
2442                    NdefMap->ApduBuffIndex]),
2443                    &(NdefMap->SendRecvBuf[
2444                    (*Temp16Bytes)]),
2445                    NdefMap->TLVStruct.BytesRemainLinTLV);
2446            /* increment the user buffer index */
2447            NdefMap->ApduBuffIndex += NdefMap->TLVStruct.BytesRemainLinTLV;
2448            *Temp16Bytes += ((uint8_t)NdefMap->TLVStruct.BytesRemainLinTLV);
2449            NdefMap->TLVStruct.BytesRemainLinTLV = 0;
2450            NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
2451            NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2452            NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2453
2454            /* check for 16 bytes of data been used */
2455            if(*Temp16Bytes == PH_FRINFC_MIFARESTD_BYTES_READ)
2456            {
2457                /* 16 bytes completed */
2458                NdefMap->StdMifareContainer.currentBlock++;
2459                NdefMap->StdMifareContainer.NdefBlocks++;
2460                Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
2461                if(Result == NFCSTATUS_SUCCESS)
2462                {
2463                    if(NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)
2464                    {
2465                        Result = phFriNfc_MifStd_H_RdABlock(NdefMap);
2466                    }
2467                    else
2468                    {
2469                        Result = phFriNfc_MifStd_H_AuthSector(NdefMap);
2470                    }
2471                }
2472                break;
2473            }
2474        }
2475        else if((NdefMap->TLVStruct.BytesRemainLinTLV >
2476            (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes)) &&
2477            (RemainingBytes <=
2478                (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes)))
2479        {
2480            /* Copy data to user buffer */
2481            (void)memcpy(&(NdefMap->ApduBuffer[
2482                    NdefMap->ApduBuffIndex]),
2483                    &(NdefMap->SendRecvBuf[
2484                    (*Temp16Bytes)]),
2485                    RemainingBytes);
2486            NdefMap->ApduBuffIndex += RemainingBytes;
2487            NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
2488            if(RemainingBytes == (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes))
2489            {
2490                NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
2491                    (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
2492                    (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0))?
2493                    PH_FRINFC_MIFARESTD_FLAG1:
2494                    PH_FRINFC_MIFARESTD_FLAG0);
2495
2496                /* 16 bytes completed */
2497                NdefMap->StdMifareContainer.currentBlock++;
2498                NdefMap->StdMifareContainer.NdefBlocks++;
2499            }
2500            else
2501            {
2502                /* copy the bytes to internal buffer, that are read,
2503                    but not used for the user buffer */
2504                (void)memcpy(NdefMap->StdMifareContainer.internalBuf,
2505                    &(NdefMap->SendRecvBuf[((*Temp16Bytes) + RemainingBytes)]),
2506                    (PH_FRINFC_MIFARESTD_BYTES_READ -
2507                    (*Temp16Bytes + RemainingBytes)));
2508
2509                /* Length of the internal buffer */
2510                NdefMap->StdMifareContainer.internalLength =
2511                            (PH_FRINFC_MIFARESTD_BYTES_READ -
2512                            (*Temp16Bytes + RemainingBytes));
2513            }
2514            *Temp16Bytes += ((uint8_t)RemainingBytes);
2515            CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
2516            break;
2517        }
2518        else
2519        {
2520            if((NdefMap->TLVStruct.BytesRemainLinTLV >
2521            (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes)) &&
2522            (RemainingBytes >
2523                (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes)))
2524            {
2525                /* Copy data to user buffer */
2526                (void)memcpy(&(NdefMap->ApduBuffer[
2527                        NdefMap->ApduBuffIndex]),
2528                        &(NdefMap->SendRecvBuf[
2529                        (*Temp16Bytes)]),
2530                        (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes));
2531                NdefMap->ApduBuffIndex += (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes);
2532                NdefMap->TLVStruct.BytesRemainLinTLV -=
2533                            (PH_FRINFC_MIFARESTD_BYTES_READ - *Temp16Bytes);
2534                *Temp16Bytes = PH_FRINFC_MIFARESTD_BYTES_READ;
2535
2536
2537                /* 16 bytes completed */
2538                NdefMap->StdMifareContainer.currentBlock++;
2539                NdefMap->StdMifareContainer.NdefBlocks++;
2540                Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
2541                if(Result == NFCSTATUS_SUCCESS)
2542                {
2543                    Result = ((NdefMap->StdMifareContainer.AuthDone ==
2544                                PH_FRINFC_MIFARESTD_FLAG1)?
2545                                phFriNfc_MifStd_H_RdABlock(NdefMap):
2546                                phFriNfc_MifStd_H_AuthSector(NdefMap));
2547                }
2548                break;
2549            }
2550        }
2551    }
2552
2553    if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
2554    {
2555        Result = (((RdFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
2556                    (NdefMap->ApduBuffIndex == PH_FRINFC_MIFARESTD_VAL0))?
2557                    (PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
2558                                NFCSTATUS_EOF_CARD_REACHED)):
2559                    Result);
2560        *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
2561        phFriNfc_MifStd_H_Complete(NdefMap, Result);
2562    }
2563    return Result;
2564}
2565#endif
2566
2567static NFCSTATUS phFriNfc_MifStd_H_RemainTLV(phFriNfc_NdefMap_t *NdefMap,
2568                                         uint8_t            *Flag,
2569                                         uint8_t            *Temp16Bytes)
2570{
2571    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
2572    uint8_t     CRFlag = 0;
2573    uint16_t    RemainingBytes = 0;
2574
2575    RemainingBytes = ((uint16_t)NdefMap->ApduBufferSize -
2576                            NdefMap->ApduBuffIndex);
2577
2578    if(NdefMap->StdMifareContainer.remainingSize <
2579        RemainingBytes)
2580    {
2581        /* If the user Buffer is greater than the Card Size
2582            set LastBlockFlag = 1. This Flag is used to read bytes
2583            till the end of the card only */
2584        RemainingBytes = NdefMap->StdMifareContainer.remainingSize;
2585    }
2586
2587    /* Remaining Bytes of length (L) in TLV <=  16 */
2588    if((NdefMap->TLVStruct.BytesRemainLinTLV <=
2589        (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) &&
2590        (RemainingBytes <= NdefMap->TLVStruct.BytesRemainLinTLV))
2591    {
2592        /* Copy data to user buffer */
2593        (void)memcpy(&(NdefMap->ApduBuffer[
2594                NdefMap->ApduBuffIndex]),
2595                &(NdefMap->SendRecvBuf[
2596                (*Temp16Bytes)]),
2597                RemainingBytes);
2598        NdefMap->ApduBuffIndex += RemainingBytes;
2599        NdefMap->StdMifareContainer.remainingSize -= RemainingBytes;
2600
2601        /* copy the bytes to internal buffer, that are read,
2602                        but not used for the user buffer */
2603        if(RemainingBytes != NdefMap->TLVStruct.BytesRemainLinTLV)
2604        {
2605            (void)memcpy( NdefMap->StdMifareContainer.internalBuf,
2606#ifdef PH_HAL4_ENABLE
2607                    &(NdefMap->SendRecvBuf[((*Temp16Bytes) + RemainingBytes)]),
2608#else
2609                    &(NdefMap->SendRecvBuf[RemainingBytes]),
2610#endif /* #ifdef PH_HAL4_ENABLE */
2611                    ((PH_FRINFC_MIFARESTD_BYTES_READ -
2612                    (*Temp16Bytes)) - RemainingBytes));
2613
2614            /* internal buffer length */
2615            NdefMap->StdMifareContainer.internalLength =
2616                    ((PH_FRINFC_MIFARESTD_BYTES_READ -
2617                    (*Temp16Bytes)) - RemainingBytes);
2618        }
2619        *Temp16Bytes += ((uint8_t)RemainingBytes);
2620        /* Remaining Bytes of length value in TLV */
2621        NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
2622
2623        if(NdefMap->StdMifareContainer.internalLength == PH_FRINFC_MIFARESTD_VAL0)
2624        {
2625            NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
2626            (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
2627            (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0))?
2628            PH_FRINFC_MIFARESTD_FLAG1:
2629            PH_FRINFC_MIFARESTD_FLAG0);
2630
2631            /* internal length bytes completed */
2632            NdefMap->StdMifareContainer.currentBlock++;
2633            NdefMap->StdMifareContainer.NdefBlocks++;
2634        }
2635
2636        if(NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)
2637        {
2638            /* Remaining Bytes of length (L) in TLV is Zero means that the next
2639             coming bytes are containing type (T), length (L) in TLV */
2640            NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
2641            NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2642            NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2643        }
2644        /* call completion routine */
2645        CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
2646        *Flag = PH_FRINFC_MIFARESTD_FLAG0;
2647    }
2648    else if((NdefMap->TLVStruct.BytesRemainLinTLV <=
2649            (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) &&
2650            (RemainingBytes > NdefMap->TLVStruct.BytesRemainLinTLV))
2651    {
2652        /* Copy data to user buffer */
2653        (void)memcpy(&(NdefMap->ApduBuffer[
2654                NdefMap->ApduBuffIndex]),
2655                &(NdefMap->SendRecvBuf[
2656                (*Temp16Bytes)]),
2657                NdefMap->TLVStruct.BytesRemainLinTLV);
2658        NdefMap->ApduBuffIndex += NdefMap->TLVStruct.BytesRemainLinTLV;
2659        NdefMap->StdMifareContainer.remainingSize -=
2660                                NdefMap->TLVStruct.BytesRemainLinTLV;
2661        NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
2662        *Temp16Bytes += ((uint8_t)NdefMap->TLVStruct.BytesRemainLinTLV);
2663        NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0;
2664
2665        *Flag = PH_FRINFC_MIFARESTD_FLAG1;
2666
2667        NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2668        NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2669        /* 16 bytes completed */
2670        if(NdefMap->TLVStruct.BytesRemainLinTLV ==
2671            PH_FRINFC_MIFARESTD_BYTES_READ)
2672        {
2673            *Flag = PH_FRINFC_MIFARESTD_FLAG0;
2674            NdefMap->TLVStruct.BytesRemainLinTLV =
2675                            PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0;
2676            NdefMap->StdMifareContainer.currentBlock++;
2677            NdefMap->StdMifareContainer.NdefBlocks++;
2678            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
2679            if(Result == NFCSTATUS_SUCCESS)
2680            {
2681                if(NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)
2682                {
2683                    Result = phFriNfc_MifStd_H_RdABlock(NdefMap);
2684                }
2685                else
2686                {
2687                    Result = phFriNfc_MifStd_H_AuthSector(NdefMap);
2688                }
2689            }
2690        }
2691        else
2692        {
2693            NdefMap->TLVStruct.BytesRemainLinTLV =
2694                            PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0;
2695            /* The read operation has finished. so, completion routine
2696            can be called. set the Completion routine(CR) flag */
2697            CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
2698        }
2699    }
2700    else if((NdefMap->TLVStruct.BytesRemainLinTLV >
2701            (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) &&
2702            (RemainingBytes <= (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))))
2703    {
2704        /* Copy data to user buffer */
2705        (void)memcpy(&(NdefMap->ApduBuffer[
2706                NdefMap->ApduBuffIndex]),
2707                &(NdefMap->SendRecvBuf[
2708                (*Temp16Bytes)]),
2709                RemainingBytes);
2710        NdefMap->ApduBuffIndex += RemainingBytes;
2711        NdefMap->StdMifareContainer.remainingSize -= RemainingBytes;
2712
2713        /* Remaining Bytes of length (L) in TLV */
2714        NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
2715        /*
2716            The below macro PH_HAL4_ENABLE is added because,
2717            there is an error with the following sequence
2718            Read begin (for ex : with 10 bytes)
2719            Read current (for ex : with 10 bytes). Here error buffer
2720            is returned
2721            Reason : During first read, internal buffer is updated
2722                    with the wrong buffer values
2723            Actually, if we read the first block (means TLV block), it
2724            contains even the TLV structure. so to copy the actual buffer,
2725            the TLV shall be excluded
2726        */
2727#ifdef PH_HAL4_ENABLE
2728        /* copy the bytes to internal buffer, that are read,
2729                        but not used for the user buffer */
2730        (void)memcpy( NdefMap->StdMifareContainer.internalBuf,
2731                &(NdefMap->SendRecvBuf[(RemainingBytes + (*Temp16Bytes))]),
2732                ((PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))
2733                - RemainingBytes));
2734
2735        /* internal buffer length */
2736        NdefMap->StdMifareContainer.internalLength =
2737                ((PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)) - RemainingBytes);
2738#else
2739        /* copy the bytes to internal buffer, that are read,
2740                        but not used for the user buffer */
2741        (void)memcpy( NdefMap->StdMifareContainer.internalBuf,
2742                &(NdefMap->SendRecvBuf[RemainingBytes]),
2743                ((PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))
2744                - RemainingBytes));
2745
2746        /* internal buffer length */
2747        NdefMap->StdMifareContainer.internalLength =
2748                ((PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)) - RemainingBytes);
2749#endif /* #ifdef PH_HAL4_ENABLE */
2750
2751        if(RemainingBytes == (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)))
2752        {
2753            NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
2754                (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
2755                (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0))?
2756                PH_FRINFC_MIFARESTD_FLAG1:
2757                PH_FRINFC_MIFARESTD_FLAG0);
2758
2759            /* internal length bytes completed */
2760            NdefMap->StdMifareContainer.currentBlock++;
2761            NdefMap->StdMifareContainer.NdefBlocks++;
2762        }
2763        *Temp16Bytes += ((uint8_t)RemainingBytes);
2764        NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
2765        CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
2766        *Flag = PH_FRINFC_MIFARESTD_FLAG0;
2767    }
2768    else
2769    {
2770        if((NdefMap->TLVStruct.BytesRemainLinTLV >
2771            (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) &&
2772            (RemainingBytes > (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))))
2773        {
2774            *Flag = PH_FRINFC_MIFARESTD_FLAG0;
2775            /* Copy data to user buffer */
2776            (void)memcpy(&(NdefMap->ApduBuffer
2777                    [NdefMap->ApduBuffIndex]),
2778                    &(NdefMap->SendRecvBuf[
2779                    (*Temp16Bytes)]),
2780                    (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)));
2781            NdefMap->ApduBuffIndex += (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes));
2782            NdefMap->StdMifareContainer.remainingSize -=
2783                                    (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes));
2784            NdefMap->TLVStruct.BytesRemainLinTLV -= (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes));
2785            *Temp16Bytes += (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes));
2786            if(NdefMap->TLVStruct.BytesRemainLinTLV != PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0)
2787            {
2788                NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
2789            }
2790            /* 16 bytes completed */
2791            NdefMap->StdMifareContainer.currentBlock++;
2792            NdefMap->StdMifareContainer.NdefBlocks++;
2793            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
2794            if(Result == NFCSTATUS_SUCCESS)
2795            {
2796                Result = ((NdefMap->StdMifareContainer.AuthDone ==
2797                        PH_FRINFC_MIFARESTD_FLAG1)?
2798                        phFriNfc_MifStd_H_RdABlock(NdefMap):
2799                        phFriNfc_MifStd_H_AuthSector(NdefMap));
2800            }
2801        }
2802    }
2803
2804    if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
2805    {
2806        *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
2807        NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
2808            (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
2809            (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0))?
2810            PH_FRINFC_MIFARESTD_FLAG1:
2811            PH_FRINFC_MIFARESTD_FLAG0);
2812    }
2813
2814    return Result;
2815}
2816
2817static NFCSTATUS phFriNfc_MifStd_H_ChkIntLen(phFriNfc_NdefMap_t *NdefMap)
2818{
2819    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
2820    uint8_t     NDEFFlag = PH_FRINFC_MIFARESTD_FLAG1;
2821    uint8_t     TempintBytes = 0;
2822
2823    if(NdefMap->TLVStruct.BytesRemainLinTLV != 0)
2824    {
2825        NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
2826        /* To read the remaining length (L) in TLV */
2827        Result = phFriNfc_MifStd_H_IntLenWioutNdef(NdefMap, &NDEFFlag, &TempintBytes);
2828    }
2829    NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
2830    /* check the NDEFFlag is set. if this is not set, then
2831        in the above RemainTLV function all the 16 bytes has been
2832        read */
2833#if 0
2834    if((NDEFFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
2835        (NdefMap->TLVStruct.NdefTLVFoundFlag ==
2836        PH_FRINFC_MIFARESTD_FLAG1))
2837    {
2838        /* if the block is NDEF TLV then get data from here */
2839        Result = phFriNfc_MifStd_H_IntLenWithNdef(NdefMap, &TempintBytes);
2840    }
2841#endif
2842    return Result;
2843}
2844
2845static NFCSTATUS phFriNfc_MifStd_H_IntLenWioutNdef(phFriNfc_NdefMap_t *NdefMap,
2846                                            uint8_t            *Flag,
2847                                            uint8_t            *TempintBytes)
2848{
2849    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
2850    uint8_t     CRFlag = 0;
2851    uint16_t    RemainingBytes = 0;
2852
2853    RemainingBytes = ((uint16_t)NdefMap->ApduBufferSize -
2854                            NdefMap->ApduBuffIndex);
2855
2856    if(NdefMap->StdMifareContainer.remainingSize <
2857        RemainingBytes)
2858    {
2859        /* If the user Buffer is greater than the Card Size
2860            set LastBlockFlag = 1. This Flag is used to read bytes
2861            till the end of the card only */
2862        RemainingBytes = NdefMap->StdMifareContainer.remainingSize;
2863    }
2864
2865    /* Remaining Bytes of length (L) in TLV <=  internal length */
2866    if((NdefMap->TLVStruct.BytesRemainLinTLV <=
2867        NdefMap->StdMifareContainer.internalLength) &&
2868        (RemainingBytes <= NdefMap->TLVStruct.BytesRemainLinTLV))
2869    {
2870        (void)memcpy(&(NdefMap->ApduBuffer[
2871                NdefMap->ApduBuffIndex]),
2872                &(NdefMap->StdMifareContainer.internalBuf[
2873                (*TempintBytes)]),
2874                RemainingBytes);
2875        NdefMap->ApduBuffIndex += RemainingBytes;
2876        NdefMap->StdMifareContainer.remainingSize -= RemainingBytes;
2877        *TempintBytes += ((uint8_t)RemainingBytes);
2878
2879        /* copy the bytes to internal buffer, that are read,
2880                        but not used for the user buffer */
2881        (void)memcpy( NdefMap->StdMifareContainer.internalBuf,
2882                &(NdefMap->StdMifareContainer.internalBuf[RemainingBytes]),
2883                (NdefMap->StdMifareContainer.internalLength - RemainingBytes));
2884
2885        /* internal buffer length */
2886        NdefMap->StdMifareContainer.internalLength -= RemainingBytes;
2887
2888        NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
2889        if(NdefMap->StdMifareContainer.internalLength == PH_FRINFC_MIFARESTD_VAL0)
2890        {
2891           NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
2892            (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
2893            (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0))?
2894            PH_FRINFC_MIFARESTD_FLAG1:
2895            PH_FRINFC_MIFARESTD_FLAG0);
2896
2897            /* internal length bytes completed */
2898            NdefMap->StdMifareContainer.currentBlock++;
2899            NdefMap->StdMifareContainer.NdefBlocks++;
2900        }
2901
2902        /* Remaining Bytes of length value in TLV */
2903        if(NdefMap->TLVStruct.BytesRemainLinTLV == 0)
2904        {
2905            /* Remaining Bytes of length (L) in TLV is Zero means that the next
2906             coming bytes are containing type (T), length (L) in TLV */
2907            NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
2908            NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2909            NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2910        }
2911        /* call completion routine */
2912        CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
2913        *Flag = PH_FRINFC_MIFARESTD_FLAG0;
2914    }
2915    else if((NdefMap->TLVStruct.BytesRemainLinTLV <=
2916            NdefMap->StdMifareContainer.internalLength) &&
2917            (RemainingBytes > NdefMap->TLVStruct.BytesRemainLinTLV))
2918    {
2919        (void)memcpy(&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex]),
2920                &(NdefMap->StdMifareContainer.internalBuf[(*TempintBytes)]),
2921                NdefMap->TLVStruct.BytesRemainLinTLV);
2922
2923        NdefMap->ApduBuffIndex += NdefMap->TLVStruct.BytesRemainLinTLV;
2924        NdefMap->StdMifareContainer.remainingSize -=
2925                    NdefMap->TLVStruct.BytesRemainLinTLV;
2926        NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
2927
2928        *TempintBytes += ((uint8_t)NdefMap->TLVStruct.BytesRemainLinTLV);
2929        *Flag = PH_FRINFC_MIFARESTD_FLAG1;
2930
2931        NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2932        NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
2933
2934        NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0;
2935        NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
2936        (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
2937            (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0))?
2938            PH_FRINFC_MIFARESTD_FLAG1:
2939            PH_FRINFC_MIFARESTD_FLAG0);
2940#ifdef PH_HAL4_ENABLE
2941
2942        if (PH_FRINFC_MIFARESTD_FLAG1 ==
2943            NdefMap->StdMifareContainer.ReadWriteCompleteFlag)
2944        {
2945            CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
2946        }
2947
2948#endif /* #ifdef PH_HAL4_ENABLE */
2949
2950        if( NdefMap->TLVStruct.BytesRemainLinTLV ==
2951            NdefMap->StdMifareContainer.internalLength)
2952        {
2953            /* Remaining Bytes in Length (L) field of TLV is 0 */
2954            NdefMap->TLVStruct.BytesRemainLinTLV =
2955                            PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0;
2956            NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
2957            *Flag = PH_FRINFC_MIFARESTD_FLAG0;
2958            /* internal length bytes completed */
2959            NdefMap->StdMifareContainer.currentBlock++;
2960            NdefMap->StdMifareContainer.NdefBlocks++;
2961            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
2962            if(Result == NFCSTATUS_SUCCESS)
2963            {
2964                Result =
2965                ((NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)?
2966                phFriNfc_MifStd_H_RdABlock(NdefMap):
2967                phFriNfc_MifStd_H_AuthSector(NdefMap));
2968            }
2969        }
2970        else
2971        {
2972            /* Remaining Bytes in Length (L) field of TLV is 0 */
2973            NdefMap->TLVStruct.BytesRemainLinTLV =
2974                            PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0;
2975            *Flag = PH_FRINFC_MIFARESTD_FLAG1;
2976        }
2977    }
2978    else if((NdefMap->TLVStruct.BytesRemainLinTLV >
2979            NdefMap->StdMifareContainer.internalLength) &&
2980            (RemainingBytes <= NdefMap->StdMifareContainer.internalLength))
2981    {
2982        (void)memcpy(&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex]),
2983                &(NdefMap->StdMifareContainer.internalBuf[(*TempintBytes)]),
2984                RemainingBytes);
2985
2986        NdefMap->ApduBuffIndex += RemainingBytes;
2987        NdefMap->StdMifareContainer.remainingSize -=
2988                            RemainingBytes;
2989        *TempintBytes += ((uint8_t)RemainingBytes);
2990        /* Remaining Bytes of length (L) in TLV */
2991        NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
2992
2993        /* copy the bytes to internal buffer, that are read,
2994                        but not used for the user buffer */
2995        (void)memcpy( NdefMap->StdMifareContainer.internalBuf,
2996                &(NdefMap->StdMifareContainer.internalBuf[RemainingBytes]),
2997                (NdefMap->StdMifareContainer.internalLength - RemainingBytes));
2998
2999        /* internal buffer length */
3000        NdefMap->StdMifareContainer.internalLength -= RemainingBytes;
3001        if(NdefMap->StdMifareContainer.internalLength == PH_FRINFC_MIFARESTD_VAL0)
3002        {
3003            NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
3004            (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
3005            (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0))?
3006            PH_FRINFC_MIFARESTD_FLAG1:
3007            PH_FRINFC_MIFARESTD_FLAG0);
3008
3009            /* internal length bytes completed */
3010            NdefMap->StdMifareContainer.currentBlock++;
3011            NdefMap->StdMifareContainer.NdefBlocks++;
3012        }
3013
3014        NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
3015        CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
3016        *Flag = PH_FRINFC_MIFARESTD_FLAG0;
3017    }
3018    else
3019    {
3020        if((NdefMap->TLVStruct.BytesRemainLinTLV >
3021            NdefMap->StdMifareContainer.internalLength) &&
3022            (RemainingBytes > NdefMap->StdMifareContainer.internalLength))
3023        {
3024            (void)memcpy(&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex]),
3025                    &(NdefMap->StdMifareContainer.internalBuf[(*TempintBytes)]),
3026                    NdefMap->StdMifareContainer.internalLength);
3027            *Flag = PH_FRINFC_MIFARESTD_FLAG0;
3028            NdefMap->ApduBuffIndex += NdefMap->StdMifareContainer.internalLength;
3029            NdefMap->StdMifareContainer.remainingSize -=
3030                                NdefMap->StdMifareContainer.internalLength;
3031            NdefMap->TLVStruct.BytesRemainLinTLV -= NdefMap->StdMifareContainer.internalLength;
3032
3033            if(NdefMap->TLVStruct.BytesRemainLinTLV != PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0)
3034            {
3035                NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
3036            }
3037
3038            NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
3039            /* internal length bytes completed */
3040            NdefMap->StdMifareContainer.currentBlock++;
3041            NdefMap->StdMifareContainer.NdefBlocks++;
3042            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
3043            if(Result == NFCSTATUS_SUCCESS)
3044            {
3045                Result = ((NdefMap->StdMifareContainer.AuthDone ==
3046                            PH_FRINFC_MIFARESTD_FLAG1)?
3047                            phFriNfc_MifStd_H_RdABlock(NdefMap):
3048                            phFriNfc_MifStd_H_AuthSector(NdefMap));
3049            }
3050        }
3051    }
3052
3053    if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
3054    {
3055        NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
3056        (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
3057            (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0))?
3058            PH_FRINFC_MIFARESTD_FLAG1:
3059            PH_FRINFC_MIFARESTD_FLAG0);
3060        *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
3061    }
3062    return Result;
3063}
3064#if 0
3065static NFCSTATUS phFriNfc_MifStd_H_IntLenWithNdef(phFriNfc_NdefMap_t *NdefMap,
3066                                            uint8_t             *TempintBytes)
3067{
3068    NFCSTATUS       Result = NFCSTATUS_SUCCESS;
3069    uint16_t        RemainingBytes = 0;
3070    uint8_t         CRFlag =  PH_FRINFC_MIFARESTD_FLAG0,
3071                    RdFlag = PH_FRINFC_MIFARESTD_FLAG0,
3072                    NDEFlength = 0;
3073
3074    RemainingBytes = (uint16_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
3075    while(PH_FRINFC_MIFARESTD_FLAG1 == NdefMap->TLVStruct.NdefTLVFoundFlag)
3076    {
3077        NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
3078        RemainingBytes = ((uint16_t)NdefMap->ApduBufferSize -
3079                        NdefMap->ApduBuffIndex);
3080        if((((  NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
3081            NdefMap->StdMifareContainer.NdefBlocks) *
3082            PH_FRINFC_MIFARESTD_BLOCK_BYTES) +
3083            (NdefMap->StdMifareContainer.internalLength -
3084            *TempintBytes)) <
3085            RemainingBytes)
3086        {
3087            /* If the user Buffer is greater than the Card Size
3088                set LastBlockFlag = 1. This Flag is used to read bytes
3089                till the end of the card only */
3090            RemainingBytes = ((( NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
3091                                NdefMap->StdMifareContainer.NdefBlocks) * 16) +
3092                                (NdefMap->StdMifareContainer.internalLength -
3093                                *TempintBytes));
3094
3095        }
3096
3097        if((NdefMap->TLVStruct.TcheckedinTLVFlag != PH_FRINFC_MIFARESTD_FLAG1) &&
3098            (NdefMap->TLVStruct.LcheckedinTLVFlag != PH_FRINFC_MIFARESTD_FLAG1) &&
3099            (NdefMap->TLVStruct.NoLbytesinTLV != PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0))
3100        {
3101            CRFlag = 1;
3102            break;
3103        }
3104
3105        /* if type (T) of TLV flag is not set then only the below check is valid
3106            because, for eg. assume, type (T) of TLV flag is set then this means
3107            that it is already checked from the previous internal bytes data (T is the
3108            last byte of internal bytes */
3109        if(NdefMap->TLVStruct.TcheckedinTLVFlag != PH_FRINFC_MIFARESTD_FLAG1)
3110        {
3111            CRFlag =(uint8_t) ((NdefMap->StdMifareContainer.internalBuf[(*TempintBytes)]!=
3112                    PH_FRINFC_MIFARESTD_NDEFTLV_T)?
3113                    PH_FRINFC_MIFARESTD_FLAG1:
3114                    PH_FRINFC_MIFARESTD_FLAG0);
3115
3116            if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
3117            {
3118                NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
3119                                    PH_FRINFC_MIFARESTD_FLAG1;
3120                RdFlag = PH_FRINFC_MIFARESTD_FLAG1;
3121                break;
3122            }
3123            /* skip Type T of the TLV */
3124            *TempintBytes += PH_FRINFC_MIFARESTD_INC_1;
3125        }
3126        else
3127        {
3128            NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
3129        }
3130
3131        if(*TempintBytes == NdefMap->StdMifareContainer.internalLength)
3132        {
3133            NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
3134            NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
3135            NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
3136            /* 16 bytes completed */
3137            NdefMap->StdMifareContainer.currentBlock++;
3138            NdefMap->StdMifareContainer.NdefBlocks++;
3139            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
3140            if(Result == NFCSTATUS_SUCCESS)
3141            {
3142                Result =
3143                    ((NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)?
3144                    phFriNfc_MifStd_H_RdABlock(NdefMap):
3145                    phFriNfc_MifStd_H_AuthSector(NdefMap));
3146            }
3147            break;
3148        }
3149
3150        /* skip number of bytes taken by L in TLV, if L is already read */
3151        if(NdefMap->TLVStruct.LcheckedinTLVFlag != PH_FRINFC_MIFARESTD_FLAG1)
3152        {
3153            CRFlag = (uint8_t)((NdefMap->StdMifareContainer.internalBuf[(*TempintBytes)]==
3154                    PH_FRINFC_MIFARESTD_NDEFTLV_L0)?
3155                    PH_FRINFC_MIFARESTD_FLAG1:
3156                    PH_FRINFC_MIFARESTD_FLAG0);
3157
3158            if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
3159            {
3160                NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
3161                                    PH_FRINFC_MIFARESTD_FLAG1;
3162                RdFlag = PH_FRINFC_MIFARESTD_FLAG1;
3163                break;
3164            }
3165            NDEFlength = *TempintBytes;
3166            *TempintBytes += phFriNfc_MifStd_H_ChkNdefLen(NdefMap,
3167                                                            NDEFlength,
3168                                                            &CRFlag);
3169            if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
3170            {
3171                NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
3172                                    PH_FRINFC_MIFARESTD_FLAG1;
3173                RdFlag = PH_FRINFC_MIFARESTD_FLAG1;
3174                break;
3175            }
3176        }
3177        else
3178        {
3179            NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
3180        }
3181
3182        if(*TempintBytes == NdefMap->StdMifareContainer.internalLength)
3183        {
3184            NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
3185            NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
3186            NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
3187            /* 16 bytes completed */
3188            NdefMap->StdMifareContainer.currentBlock++;
3189            NdefMap->StdMifareContainer.NdefBlocks++;
3190            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
3191            if(Result == NFCSTATUS_SUCCESS)
3192            {
3193                Result = ((NdefMap->StdMifareContainer.AuthDone ==
3194                        PH_FRINFC_MIFARESTD_FLAG1)?
3195                        phFriNfc_MifStd_H_RdABlock(NdefMap):
3196                        phFriNfc_MifStd_H_AuthSector(NdefMap));
3197            }
3198            break;
3199        }
3200
3201        if( (NdefMap->TLVStruct.BytesRemainLinTLV <=
3202            (NdefMap->StdMifareContainer.internalLength - *TempintBytes)) &&
3203            (RemainingBytes <= NdefMap->TLVStruct.BytesRemainLinTLV))
3204        {
3205            /* Copy the internal bytes to the user buffer */
3206            (void)memcpy(&(NdefMap->ApduBuffer[
3207                    NdefMap->ApduBuffIndex]),
3208                    &(NdefMap->StdMifareContainer.internalBuf[
3209                    (*TempintBytes)]),
3210                    RemainingBytes);
3211
3212            NdefMap->ApduBuffIndex += RemainingBytes;
3213            *TempintBytes += ((uint8_t)RemainingBytes);
3214            NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
3215            if(NdefMap->TLVStruct.BytesRemainLinTLV == 0)
3216            {
3217                NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
3218            }
3219            /* Update the internal length */
3220            (void)memcpy( NdefMap->StdMifareContainer.internalBuf,
3221                    &(NdefMap->StdMifareContainer.internalBuf[
3222                    (*TempintBytes)]),
3223                    RemainingBytes);
3224            NdefMap->StdMifareContainer.internalLength -= (*TempintBytes +
3225                                                        RemainingBytes);
3226
3227            CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
3228            break;
3229        }
3230        else if((NdefMap->TLVStruct.BytesRemainLinTLV <=
3231                (NdefMap->StdMifareContainer.internalLength - *TempintBytes)) &&
3232                (RemainingBytes > NdefMap->TLVStruct.BytesRemainLinTLV))
3233        {
3234            /* Copy data to user buffer */
3235            (void)memcpy(&(NdefMap->ApduBuffer[
3236                    NdefMap->ApduBuffIndex]),
3237                    &(NdefMap->StdMifareContainer.internalBuf[
3238                    (*TempintBytes)]),
3239                    NdefMap->TLVStruct.BytesRemainLinTLV);
3240
3241            /* increment the user buffer index */
3242            NdefMap->ApduBuffIndex += NdefMap->TLVStruct.BytesRemainLinTLV;
3243            *TempintBytes += ((uint8_t)NdefMap->TLVStruct.BytesRemainLinTLV);
3244            NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0;
3245            NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
3246
3247            /* check for internal length bytes of data been used */
3248            if(*TempintBytes == NdefMap->StdMifareContainer.internalLength)
3249            {
3250                /* 16 bytes completed */
3251                NdefMap->StdMifareContainer.currentBlock++;
3252                NdefMap->StdMifareContainer.NdefBlocks++;
3253                Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
3254                if(Result == NFCSTATUS_SUCCESS)
3255                {
3256                    Result = ((NdefMap->StdMifareContainer.AuthDone ==
3257                            PH_FRINFC_MIFARESTD_FLAG1)?
3258                            phFriNfc_MifStd_H_RdABlock(NdefMap):
3259                            phFriNfc_MifStd_H_AuthSector(NdefMap));
3260                }
3261                break;
3262            }
3263        }
3264        else if((NdefMap->TLVStruct.BytesRemainLinTLV >
3265                (NdefMap->StdMifareContainer.internalLength - *TempintBytes)) &&
3266                (RemainingBytes <= (NdefMap->StdMifareContainer.internalLength -
3267                *TempintBytes)))
3268        {
3269            /* Copy data to user buffer */
3270            (void)memcpy(&(NdefMap->ApduBuffer[
3271                    NdefMap->ApduBuffIndex]),
3272                    &(NdefMap->StdMifareContainer.internalBuf[
3273                    (*TempintBytes)]),
3274                    RemainingBytes);
3275            NdefMap->ApduBuffIndex += RemainingBytes;
3276            *TempintBytes += ((uint8_t)RemainingBytes);
3277            NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
3278
3279            /* copy the bytes to internal buffer, that are read,
3280                but not used for the user buffer */
3281            (void)memcpy(NdefMap->StdMifareContainer.internalBuf,
3282                &(NdefMap->StdMifareContainer.internalBuf[(*TempintBytes)]),
3283                (NdefMap->StdMifareContainer.internalLength - *TempintBytes));
3284
3285            /* Length of the internal buffer */
3286            NdefMap->StdMifareContainer.internalLength -= *TempintBytes;
3287            CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
3288            break;
3289        }
3290        else
3291        {
3292            if((NdefMap->TLVStruct.BytesRemainLinTLV >
3293                (NdefMap->StdMifareContainer.internalLength - *TempintBytes)) &&
3294                (RemainingBytes > (NdefMap->StdMifareContainer.internalLength -
3295                *TempintBytes)))
3296            {
3297                /* Copy data to user buffer */
3298                (void)memcpy(&(NdefMap->ApduBuffer[
3299                        NdefMap->ApduBuffIndex]),
3300                        &(NdefMap->StdMifareContainer.internalBuf[
3301                        (*TempintBytes)]),
3302                        (NdefMap->StdMifareContainer.internalLength -
3303                        *TempintBytes));
3304                NdefMap->ApduBuffIndex += (NdefMap->StdMifareContainer.internalLength -
3305                                            *TempintBytes);
3306                NdefMap->TLVStruct.BytesRemainLinTLV -=
3307                            (NdefMap->StdMifareContainer.internalLength - *TempintBytes);
3308                *TempintBytes += (uint8_t)(NdefMap->StdMifareContainer.internalLength -
3309                                            *TempintBytes);
3310
3311                NdefMap->StdMifareContainer.internalLength -= *TempintBytes;
3312
3313                /* 16 bytes completed */
3314                NdefMap->StdMifareContainer.currentBlock++;
3315                NdefMap->StdMifareContainer.NdefBlocks++;
3316                Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
3317                if(Result == NFCSTATUS_SUCCESS)
3318                {
3319                    Result = ((NdefMap->StdMifareContainer.AuthDone ==
3320                                PH_FRINFC_MIFARESTD_FLAG1)?
3321                                phFriNfc_MifStd_H_RdABlock(NdefMap):
3322                                phFriNfc_MifStd_H_AuthSector(NdefMap));
3323                }
3324                break;
3325            }
3326        }
3327    }
3328
3329    if((((  NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
3330        NdefMap->StdMifareContainer.NdefBlocks) *
3331        PH_FRINFC_MIFARESTD_BLOCK_BYTES) +
3332        NdefMap->StdMifareContainer.internalLength) ==
3333        PH_FRINFC_MIFARESTD_VAL0)
3334    {
3335        NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
3336                        PH_FRINFC_MIFARESTD_FLAG1;
3337    }
3338
3339    if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
3340    {
3341        Result = (((RdFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
3342                    (NdefMap->ApduBuffIndex == PH_FRINFC_MIFARESTD_VAL0))?
3343                    (PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
3344                                NFCSTATUS_EOF_NDEF_CONTAINER_REACHED)):
3345                    Result);
3346
3347        *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
3348    }
3349
3350    return Result;
3351}
3352#endif
3353
3354static NFCSTATUS phFriNfc_MifStd_H_WriteNdefLen(phFriNfc_NdefMap_t *NdefMap)
3355{
3356    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
3357    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN;
3358
3359    /* If Current block = Ndef TLV block then the starting point
3360        is writing from type of TLV
3361        Else */
3362
3363    if(NdefMap->StdMifareContainer.currentBlock ==
3364                                NdefMap->TLVStruct.NdefTLVBlock)
3365    {
3366
3367        if(NdefMap->TLVStruct.NULLTLVCount >=
3368            PH_FRINFC_MIFARESTD_VAL2)
3369        {
3370            phFriNfc_MifStd_H_fillTLV1(NdefMap);
3371        }
3372        else
3373        {
3374            phFriNfc_MifStd_H_fillTLV2(NdefMap);
3375        }
3376    }
3377    else
3378    {
3379        if(NdefMap->TLVStruct.NULLTLVCount >=
3380            PH_FRINFC_MIFARESTD_VAL2)
3381        {
3382            phFriNfc_MifStd_H_fillTLV1_1(NdefMap);
3383        }
3384        else
3385        {
3386            phFriNfc_MifStd_H_fillTLV2_1(NdefMap);
3387        }
3388    }
3389
3390    (void)memcpy( NdefMap->StdMifareContainer.Buffer,
3391            &(NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL1]),
3392            PH_FRINFC_MIFARESTD_BYTES_READ);
3393
3394
3395    /* Write from here */
3396    NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_WRITE;
3397
3398#ifndef PH_HAL4_ENABLE
3399    NdefMap->Cmd.MfCmd = phHal_eMifareCmdListMifareWrite16;
3400#else
3401    NdefMap->Cmd.MfCmd = phHal_eMifareWrite16;
3402#endif
3403
3404    *NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
3405    /* Call the Overlapped HAL Transceive function */
3406    Result = phFriNfc_OvrHal_Transceive(    NdefMap->LowerDevice,
3407                                            &NdefMap->MapCompletionInfo,
3408                                            NdefMap->psRemoteDevInfo,
3409                                            NdefMap->Cmd,
3410                                            &NdefMap->psDepAdditionalInfo,
3411                                            NdefMap->SendRecvBuf,
3412                                            NdefMap->SendLength,
3413                                            NdefMap->SendRecvBuf,
3414                                            NdefMap->SendRecvLength);
3415
3416    return Result;
3417}
3418#if 0
3419static NFCSTATUS phFriNfc_MifStd_H_ChkNdefCmpltBlocks(phFriNfc_NdefMap_t   *NdefMap)
3420{
3421    NFCSTATUS   Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
3422                                    NFCSTATUS_INVALID_PARAMETER);
3423    /*  If NoOfNdefCompBlocks is found to be zero,
3424        should we warn the user */
3425    if((NdefMap->StdMifareContainer.NoOfNdefCompBlocks !=
3426        PH_FRINFC_MIFARESTD_VAL0) &&
3427        (NdefMap->StdMifareContainer.NoOfNdefCompBlocks <=
3428        PH_FRINFC_MIFARESTD4K_MAX_BLOCKS))
3429    {
3430        /* Reading/writing to the card can start only
3431        from the block 4 */
3432        NdefMap->StdMifareContainer.currentBlock = 4;
3433        NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
3434        Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
3435        if((Result == NFCSTATUS_SUCCESS) &&
3436            (NdefMap->StdMifareContainer.AuthDone ==
3437            PH_FRINFC_MIFARESTD_FLAG1))
3438        {
3439            NdefMap->StdMifareContainer.ReadAcsBitFlag = 0;
3440            Result = phFriNfc_MifStd_H_RdAcsBit(NdefMap);
3441        }
3442        else
3443        {
3444            Result = phFriNfc_MifStd_H_AuthSector(NdefMap);
3445        }
3446    }
3447    return Result;
3448}
3449#endif
3450
3451static void phFriNfc_MifStd_H_RdWrReset(phFriNfc_NdefMap_t   *NdefMap)
3452{
3453    NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_BLK4;
3454    NdefMap->StdMifareContainer.NdefBlocks = PH_FRINFC_MIFARESTD_VAL1;
3455    NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0;
3456    NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
3457    NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
3458    NdefMap->TLVStruct.NdefTLVAuthFlag = PH_FRINFC_MIFARESTD_FLAG0;
3459    NdefMap->TLVStruct.NdefTLVBlock = PH_FRINFC_MIFARESTD_MAD_BLK0;
3460    NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL0;
3461    NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_MIFARESTD_VAL0;
3462    NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
3463    NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
3464    NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
3465    NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0;
3466    NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG1;
3467    NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
3468    NdefMap->StdMifareContainer.remainingSize = (uint16_t)
3469                        (NdefMap->StdMifareContainer.NoOfNdefCompBlocks *
3470                            PH_FRINFC_MIFARESTD_BLOCK_BYTES);
3471    NdefMap->StdMifareContainer.WrLength = PH_FRINFC_MIFARESTD_VAL1;
3472
3473}
3474
3475static NFCSTATUS phFriNfc_MifStd_H_RdtoWrNdefLen(phFriNfc_NdefMap_t *NdefMap)
3476{
3477    NFCSTATUS Result = NFCSTATUS_SUCCESS;
3478    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_TO_WR_NDEF_LEN;
3479
3480    if(NdefMap->TLVStruct.NdefTLVAuthFlag == PH_FRINFC_MIFARESTD_FLAG1)
3481    {
3482        NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
3483        Result = phFriNfc_MifStd_H_AuthSector(NdefMap);
3484    }
3485    else
3486    {
3487        NdefMap->SendRecvBuf[0] = NdefMap->StdMifareContainer.currentBlock;
3488        NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_READ;
3489        *NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
3490
3491#ifndef PH_HAL4_ENABLE
3492        NdefMap->Cmd.MfCmd = phHal_eMifareCmdListMifareRead;
3493#else
3494        NdefMap->Cmd.MfCmd = phHal_eMifareRead;
3495#endif
3496        /* Call the Overlapped HAL Transceive function */
3497        Result = phFriNfc_OvrHal_Transceive(    NdefMap->LowerDevice,
3498                                                &NdefMap->MapCompletionInfo,
3499                                                NdefMap->psRemoteDevInfo,
3500                                                NdefMap->Cmd,
3501                                                &NdefMap->psDepAdditionalInfo,
3502                                                NdefMap->SendRecvBuf,
3503                                                NdefMap->SendLength,
3504                                                NdefMap->SendRecvBuf,
3505                                                NdefMap->SendRecvLength);
3506    }
3507
3508    return Result;
3509}
3510
3511static void phFriNfc_MifStd_H_SetNdefBlkAuth(phFriNfc_NdefMap_t   *NdefMap)
3512{
3513    NdefMap->TLVStruct.NdefTLVAuthFlag =
3514            ((phFriNfc_MifStd_H_GetSect(NdefMap->TLVStruct.NdefTLVBlock)
3515            == phFriNfc_MifStd_H_GetSect(NdefMap->StdMifareContainer.currentBlock))?
3516            PH_FRINFC_MIFARESTD_FLAG0:
3517            PH_FRINFC_MIFARESTD_FLAG1);
3518}
3519
3520static NFCSTATUS phFriNfc_MifStd_H_GetActCardLen(phFriNfc_NdefMap_t *NdefMap)
3521{
3522    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
3523    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_GET_ACT_CARDSIZE;
3524    NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_GET_ACTSIZE_OPE;
3525
3526    Result = ((NdefMap->StdMifareContainer.AuthDone ==
3527            PH_FRINFC_MIFARESTD_FLAG0)?
3528            phFriNfc_MifStd_H_AuthSector(NdefMap):
3529            phFriNfc_MifStd_H_Rd16Bytes(NdefMap,
3530                NdefMap->StdMifareContainer.currentBlock));
3531
3532    return Result;
3533}
3534
3535static NFCSTATUS phFriNfc_MifStd_H_ChkTLVs(phFriNfc_NdefMap_t *NdefMap,
3536                                           uint8_t            *CRFlag)
3537{
3538    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
3539    uint16_t        TempLength = PH_FRINFC_MIFARESTD_VAL0,
3540                    ShiftLength = PH_FRINFC_MIFARESTD_VAL0;
3541    uint8_t         TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0;
3542    NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_GET_ACTSIZE_OPE;
3543    TempLength = NdefMap->TLVStruct.NdefTLVByte;
3544    for(;;)
3545    {
3546#ifdef PH_HAL4_ENABLE
3547        if((NdefMap->SendRecvBuf[TempLength] != PH_FRINFC_MIFARESTD_TERMTLV_T) &&
3548            (NdefMap->SendRecvBuf[TempLength] != PH_FRINFC_MIFARESTD_NULLTLV_T) &&
3549            (NdefMap->SendRecvBuf[TempLength] != PH_FRINFC_MIFARESTD_NDEFTLV_T) &&
3550            (FALSE == NdefMap->TLVStruct.NdefTLVFoundFlag))
3551        {
3552            Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
3553                                NFCSTATUS_NO_NDEF_SUPPORT);
3554            NdefMap->TLVStruct.BytesRemainLinTLV = 0;
3555            NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID;
3556            *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
3557            break;
3558
3559        }
3560        else
3561#endif /* #ifdef PH_HAL4_ENABLE */
3562        if((NdefMap->SendRecvBuf[TempLength] != PH_FRINFC_MIFARESTD_TERMTLV_T) &&
3563            (NdefMap->SendRecvBuf[TempLength] != PH_FRINFC_MIFARESTD_NULLTLV_T))
3564        {
3565            if(NdefMap->SendRecvBuf[TempLength] == PH_FRINFC_MIFARESTD_NDEFTLV_T)
3566            {
3567                NdefMap->TLVStruct.NdefTLVBlock =
3568                            NdefMap->StdMifareContainer.currentBlock;
3569                NdefMap->TLVStruct.NdefTLVByte = (uint8_t)TempLength;
3570                NdefMap->TLVStruct.NdefTLVFoundFlag =
3571                            ((NdefMap->SendRecvBuf[TempLength] ==
3572                                PH_FRINFC_MIFARESTD_NDEFTLV_T)?
3573                                PH_FRINFC_MIFARESTD_FLAG1:
3574                                PH_FRINFC_MIFARESTD_FLAG0);
3575
3576                NdefMap->TLVStruct.NULLTLVCount = ((NdefMap->TLVStruct.NULLTLVCount
3577                                            == PH_FRINFC_MIFARESTD_VAL1)?
3578                                            PH_FRINFC_MIFARESTD_VAL0:
3579                                            NdefMap->TLVStruct.NULLTLVCount);
3580            }
3581            else
3582            {
3583                NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
3584            }
3585
3586            TempLength++;
3587            if(TempLength == PH_FRINFC_MIFARESTD_BYTES_READ)
3588            {
3589                NdefMap->TLVStruct.TcheckedinTLVFlag =
3590                                        PH_FRINFC_MIFARESTD_FLAG1;
3591                NdefMap->TLVStruct.NoLbytesinTLV =
3592                                    PH_FRINFC_MIFARESTD_VAL3;
3593            }
3594            Result = phFriNfc_MifStd_H_Chk16Bytes(  NdefMap,
3595                                                    TempLength);
3596            if(Result != NFCSTATUS_SUCCESS)
3597            {
3598                *CRFlag = (uint8_t)((Result == NFCSTATUS_PENDING)?
3599                            PH_FRINFC_MIFARESTD_FLAG0:
3600                            PH_FRINFC_MIFARESTD_FLAG1);
3601                break;
3602            }
3603
3604            if(((((  NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
3605                        NdefMap->StdMifareContainer.NdefBlocks) *
3606                        PH_FRINFC_MIFARESTD_BLOCK_BYTES) +
3607                        (PH_FRINFC_MIFARESTD_BLOCK_BYTES -
3608                        TempLength)) <
3609                        NdefMap->SendRecvBuf[TempLength]) &&
3610                        ((NdefMap->SendRecvBuf[TempLength] <
3611                        PH_FRINFC_MIFARESTD_NDEFTLV_L) &&
3612                        (NdefMap->TLVStruct.NdefTLVFoundFlag !=
3613                        PH_FRINFC_MIFARESTD_VAL1)))
3614            {
3615                /* Result = Error */
3616                Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
3617                                NFCSTATUS_INVALID_PARAMETER);
3618                *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
3619                break;
3620            }
3621
3622            if(((((  NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
3623                        NdefMap->StdMifareContainer.NdefBlocks) *
3624                        PH_FRINFC_MIFARESTD_BLOCK_BYTES) +
3625                        (PH_FRINFC_MIFARESTD_BLOCK_BYTES -
3626                        TempLength)) <
3627                        NdefMap->SendRecvBuf[TempLength]) &&
3628                        ((NdefMap->SendRecvBuf[TempLength] ==
3629                        PH_FRINFC_MIFARESTD_VAL0) &&
3630                        (NdefMap->TLVStruct.NdefTLVFoundFlag ==
3631                        PH_FRINFC_MIFARESTD_VAL1)))
3632            {
3633                /* Result = Error */
3634                Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
3635                                NFCSTATUS_INVALID_PARAMETER);
3636                *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
3637                break;
3638            }
3639
3640            if((NdefMap->TLVStruct.NdefTLVFoundFlag ==
3641                PH_FRINFC_MIFARESTD_FLAG1) &&
3642                (NdefMap->SendRecvBuf[TempLength] <
3643                 PH_FRINFC_MIFARESTD_NDEFTLV_L))
3644            {
3645                Result = phFriNfc_MapTool_SetCardState(NdefMap,
3646                                    NdefMap->SendRecvBuf[TempLength]);
3647                NdefMap->TLVStruct.BytesRemainLinTLV =
3648                                    NdefMap->SendRecvBuf[TempLength];
3649                NdefMap->StdMifareContainer.remainingSize -=
3650                                            PH_FRINFC_MIFARESTD_VAL2;
3651                /* This flag is set */
3652                NdefMap->StdMifareContainer.remSizeUpdFlag =
3653                    (uint8_t)((NdefMap->TLVStruct.NULLTLVCount >=
3654                    PH_FRINFC_MIFARESTD_VAL2)?
3655                    PH_FRINFC_MIFARESTD_FLAG0:
3656                    PH_FRINFC_MIFARESTD_FLAG1);
3657
3658                *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
3659                break;
3660            }
3661
3662            NdefMap->StdMifareContainer.remainingSize -=
3663                    ((  NdefMap->SendRecvBuf[TempLength] <
3664                            PH_FRINFC_MIFARESTD_NDEFTLV_L)?
3665                            (NdefMap->SendRecvBuf[TempLength]
3666                            + PH_FRINFC_MIFARESTD_VAL2):
3667                            PH_FRINFC_MIFARESTD_VAL0);
3668
3669            if(( NdefMap->SendRecvBuf[TempLength] ==
3670                        PH_FRINFC_MIFARESTD_VAL0))
3671            {
3672                Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
3673                            NFCSTATUS_INVALID_PARAMETER);
3674                *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
3675                break;
3676            }
3677
3678            TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0;
3679            /* get the next TLV after the proprietary TLV */
3680            Result =
3681                ((NdefMap->SendRecvBuf[TempLength] <
3682                    PH_FRINFC_MIFARESTD_NDEFTLV_L)?
3683                    phFriNfc_MifStd_H_GetNxtTLV(NdefMap, &TempLength, &TL4bytesFlag):
3684                    NFCSTATUS_PENDING);
3685
3686            if((TempLength >= PH_FRINFC_MIFARESTD_BYTES_READ) &&
3687                (Result == NFCSTATUS_SUCCESS))
3688            {
3689                NdefMap->TLVStruct.TcheckedinTLVFlag =
3690                                        PH_FRINFC_MIFARESTD_FLAG0;
3691                NdefMap->TLVStruct.NoLbytesinTLV =
3692                                    PH_FRINFC_MIFARESTD_VAL0;
3693
3694                Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap);
3695                *CRFlag = (uint8_t)((Result != NFCSTATUS_PENDING)?
3696                            PH_FRINFC_MIFARESTD_FLAG1:
3697                            PH_FRINFC_MIFARESTD_FLAG0);
3698                break;
3699            }
3700            else
3701            {
3702                if(Result == NFCSTATUS_PENDING)
3703                {
3704                    TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG1;
3705                    Result = ((NdefMap->SendRecvBuf[TempLength] ==
3706                            PH_FRINFC_MIFARESTD_NDEFTLV_L) ?
3707                            NFCSTATUS_SUCCESS:
3708                            (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
3709                                NFCSTATUS_INVALID_PARAMETER)));
3710
3711                    if(Result != NFCSTATUS_SUCCESS)
3712                    {
3713                        *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
3714                        break;
3715                    }
3716                    NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
3717                    TempLength++;
3718                    /* Check 0xFF */
3719                    if(TempLength == PH_FRINFC_MIFARESTD_BYTES_READ)
3720                    {
3721                        NdefMap->TLVStruct.TcheckedinTLVFlag =
3722                                                PH_FRINFC_MIFARESTD_FLAG1;
3723                        NdefMap->TLVStruct.NoLbytesinTLV =
3724                                            PH_FRINFC_MIFARESTD_VAL2;
3725                    }
3726                    Result = phFriNfc_MifStd_H_Chk16Bytes(  NdefMap,
3727                                                            TempLength);
3728                    if(Result != NFCSTATUS_SUCCESS)
3729                    {
3730                        break;
3731                    }
3732
3733                    ShiftLength = NdefMap->SendRecvBuf[TempLength];
3734                    TempLength++;
3735                    if(TempLength == PH_FRINFC_MIFARESTD_BYTES_READ)
3736                    {
3737                        NdefMap->TLVStruct.TcheckedinTLVFlag =
3738                                                PH_FRINFC_MIFARESTD_FLAG1;
3739                        NdefMap->TLVStruct.NoLbytesinTLV =
3740                                            PH_FRINFC_MIFARESTD_VAL1;
3741                        NdefMap->TLVStruct.prevLenByteValue =
3742                                    NdefMap->SendRecvBuf[(TempLength -
3743                                                PH_FRINFC_MIFARESTD_VAL1)];
3744                    }
3745                    Result = phFriNfc_MifStd_H_Chk16Bytes(  NdefMap,
3746                                                            TempLength);
3747                    if(Result != NFCSTATUS_SUCCESS)
3748                    {
3749                        break;
3750                    }
3751
3752
3753                    if((((  NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
3754                        NdefMap->StdMifareContainer.NdefBlocks) *
3755                        PH_FRINFC_MIFARESTD_BLOCK_BYTES) +
3756                        (PH_FRINFC_MIFARESTD_BLOCK_BYTES -
3757                        TempLength)) <
3758                        (( ShiftLength
3759                        << 8) + NdefMap->SendRecvBuf[TempLength]))
3760                    {
3761                        Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
3762                                NFCSTATUS_INVALID_REMOTE_DEVICE);
3763
3764                        break;
3765                    }
3766
3767                    if(NdefMap->TLVStruct.NdefTLVFoundFlag ==
3768                        PH_FRINFC_MIFARESTD_FLAG1)
3769                    {
3770                        ShiftLength = (( ShiftLength<< 8) +
3771                                    NdefMap->SendRecvBuf[TempLength]);
3772                        NdefMap->TLVStruct.BytesRemainLinTLV = ShiftLength;
3773                        Result = phFriNfc_MapTool_SetCardState(NdefMap,
3774                                                                ShiftLength);
3775                        NdefMap->StdMifareContainer.remainingSize -=
3776                                            PH_FRINFC_MIFARESTD_VAL4;
3777                        *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
3778                        break;
3779                    }
3780
3781                    NdefMap->StdMifareContainer.remainingSize -=
3782                                ((ShiftLength<< 8) +
3783                                NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL1)]);
3784                    TempLength++;
3785
3786                    /* get the next TLV after the proprietary TLV */
3787                    Result = phFriNfc_MifStd_H_GetNxtTLV(NdefMap, &TempLength, &TL4bytesFlag);
3788
3789                    if((TempLength >= PH_FRINFC_MIFARESTD_BYTES_READ) &&
3790                        (Result == NFCSTATUS_SUCCESS))
3791                    {
3792                        NdefMap->TLVStruct.TcheckedinTLVFlag =
3793                                                PH_FRINFC_MIFARESTD_FLAG0;
3794                        NdefMap->TLVStruct.NoLbytesinTLV =
3795                                            PH_FRINFC_MIFARESTD_VAL0;
3796                        Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap);
3797
3798                        break;
3799                    }
3800                    break;
3801                }
3802            }
3803        }
3804        else if((NdefMap->SendRecvBuf[TempLength] == PH_FRINFC_MIFARESTD_TERMTLV_T) &&
3805                (NdefMap->TLVStruct.NdefTLVFoundFlag == PH_FRINFC_MIFARESTD_FLAG0))
3806        {
3807            Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
3808                                NFCSTATUS_INVALID_PARAMETER);
3809            NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
3810                                        PH_FRINFC_MIFARESTD_FLAG1;
3811            break;
3812
3813        }
3814        else if(NdefMap->SendRecvBuf[TempLength] == PH_FRINFC_MIFARESTD_NULLTLV_T)
3815        {
3816            TempLength++;
3817            NdefMap->TLVStruct.NULLTLVCount += PH_FRINFC_MIFARESTD_VAL1;
3818            ShiftLength = NdefMap->SendRecvBuf[(TempLength - PH_FRINFC_MIFARESTD_VAL1)];
3819            NdefMap->StdMifareContainer.remainingSize -= PH_FRINFC_MIFARESTD_VAL1;
3820            if(NdefMap->StdMifareContainer.remainingSize <
3821                    (( ShiftLength << 8) + NdefMap->SendRecvBuf[TempLength]))
3822            {
3823                Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
3824                                NFCSTATUS_INVALID_REMOTE_DEVICE);
3825                break;
3826            }
3827            Result = phFriNfc_MifStd_H_Chk16Bytes(  NdefMap,
3828                                                    TempLength);
3829            if(Result != NFCSTATUS_SUCCESS)
3830            {
3831                NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL0;
3832                break;
3833            }
3834        }
3835        else
3836        {
3837            if((NdefMap->SendRecvBuf[TempLength] == PH_FRINFC_MIFARESTD_TERMTLV_T) &&
3838                (NdefMap->TLVStruct.NdefTLVFoundFlag == PH_FRINFC_MIFARESTD_FLAG1))
3839            {
3840                TempLength++;
3841                Result = NFCSTATUS_SUCCESS;
3842                NdefMap->StdMifareContainer.remainingSize -=
3843                                                PH_FRINFC_MIFARESTD_VAL1;
3844            }
3845        }
3846    }
3847
3848    if(NdefMap->TLVStruct.BytesRemainLinTLV >
3849        NdefMap->StdMifareContainer.remainingSize)
3850    {
3851        Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
3852                                NFCSTATUS_INVALID_FORMAT);
3853    }
3854    else
3855    {
3856        if(NdefMap->StdMifareContainer.remainingSize ==
3857                PH_FRINFC_MIFARESTD_VAL0)
3858        {
3859            Result = ((NdefMap->TLVStruct.NdefTLVFoundFlag ==
3860                        PH_FRINFC_MIFARESTD_FLAG1)?
3861                        NFCSTATUS_SUCCESS:
3862                        (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
3863                                    NFCSTATUS_INVALID_PARAMETER)));
3864        }
3865    }
3866    return Result;
3867}
3868
3869static NFCSTATUS phFriNfc_MifStd_H_GetNxtTLV(phFriNfc_NdefMap_t    *NdefMap,
3870                                      uint16_t              *TempLength,
3871                                       uint8_t              *TL4bytesFlag)
3872{
3873    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
3874    uint16_t    LengthRemaining = PH_FRINFC_MIFARESTD_VAL0,
3875                TempLen = PH_FRINFC_MIFARESTD_VAL0,
3876                ShiftLength = PH_FRINFC_MIFARESTD_VAL0;
3877
3878    TempLen = (*TempLength);
3879    LengthRemaining = (PH_FRINFC_MIFARESTD_BYTES_READ -
3880                        (TempLen + PH_FRINFC_MIFARESTD_VAL1));
3881
3882    if(*TL4bytesFlag == PH_FRINFC_MIFARESTD_FLAG0)
3883    {
3884        (*TempLength) += (NdefMap->SendRecvBuf[TempLen] +
3885                            PH_FRINFC_MIFARESTD_VAL1);
3886
3887        if(NdefMap->TLVStruct.NdefTLVFoundFlag == PH_FRINFC_MIFARESTD_FLAG0)
3888        {
3889            LengthRemaining =
3890                (((*TempLength) < PH_FRINFC_MIFARESTD_BYTES_READ)?
3891                PH_FRINFC_MIFARESTD_VAL0:
3892                (NdefMap->SendRecvBuf[TempLen] -
3893                LengthRemaining));
3894        }
3895        else
3896        {
3897            LengthRemaining =
3898                (((*TempLength) < PH_FRINFC_MIFARESTD_BYTES_READ)?
3899                PH_FRINFC_MIFARESTD_VAL0:
3900                (NdefMap->SendRecvBuf[TempLen] -
3901                LengthRemaining));
3902        }
3903    }
3904    else
3905    {
3906        *TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0;
3907        if(NdefMap->TLVStruct.NoLbytesinTLV ==
3908            PH_FRINFC_MIFARESTD_VAL1)
3909        {
3910            ShiftLength = NdefMap->TLVStruct.prevLenByteValue;
3911            (*TempLength) += ((ShiftLength
3912                                << 8) + NdefMap->SendRecvBuf[TempLen] +
3913                            PH_FRINFC_MIFARESTD_VAL1);
3914
3915            LengthRemaining =
3916                (((ShiftLength
3917                << 8) + NdefMap->SendRecvBuf[TempLen]) -
3918                LengthRemaining);
3919        }
3920        else
3921        {
3922            ShiftLength = NdefMap->SendRecvBuf[(TempLen - PH_FRINFC_MIFARESTD_VAL1)];
3923            (*TempLength) += ((ShiftLength
3924                                << 8) + NdefMap->SendRecvBuf[TempLen] +
3925                            PH_FRINFC_MIFARESTD_VAL1);
3926
3927            LengthRemaining =
3928                (((ShiftLength
3929                    << 8) + NdefMap->SendRecvBuf[TempLen]) -
3930                    LengthRemaining);
3931        }
3932    }
3933
3934    NdefMap->TLVStruct.NdefTLVByte =
3935        (uint8_t)(((*TempLength) < PH_FRINFC_MIFARESTD_BYTES_READ)?
3936        (*TempLength):
3937        (LengthRemaining % PH_FRINFC_MIFARESTD_BYTES_READ));
3938
3939    while(LengthRemaining != PH_FRINFC_MIFARESTD_VAL0)
3940    {
3941        NdefMap->StdMifareContainer.currentBlock++;
3942        NdefMap->StdMifareContainer.NdefBlocks++;
3943        Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
3944        LengthRemaining -=
3945            ((LengthRemaining <= PH_FRINFC_MIFARESTD_BYTES_READ)?
3946            LengthRemaining:
3947            PH_FRINFC_MIFARESTD_BYTES_READ);
3948    }
3949
3950    if(NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL0)
3951    {
3952        NdefMap->StdMifareContainer.currentBlock++;
3953        NdefMap->StdMifareContainer.NdefBlocks++;
3954        Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
3955    }
3956
3957    return Result;
3958}
3959
3960static NFCSTATUS phFriNfc_MifStd_H_Chk16Bytes(phFriNfc_NdefMap_t   *NdefMap,
3961                                       uint16_t             TempLength)
3962{
3963    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
3964    if(TempLength == PH_FRINFC_MIFARESTD_BYTES_READ)
3965    {
3966        NdefMap->StdMifareContainer.currentBlock++;
3967        NdefMap->StdMifareContainer.NdefBlocks++;
3968        Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
3969
3970        Result =
3971            ((NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)?
3972            phFriNfc_MifStd_H_GetActCardLen(NdefMap):
3973            phFriNfc_MifStd_H_AuthSector(NdefMap));
3974    }
3975    return Result;
3976}
3977
3978static NFCSTATUS phFriNfc_MifStd_H_ChkRemainTLVs(phFriNfc_NdefMap_t *NdefMap,
3979                                          uint8_t            *CRFlag,
3980                                          uint8_t            *NDEFFlag)
3981{
3982    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
3983    uint16_t    TempLength = PH_FRINFC_MIFARESTD_VAL0,
3984                ShiftLength = PH_FRINFC_MIFARESTD_VAL0;
3985    uint8_t     TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0;
3986
3987    switch(NdefMap->TLVStruct.NoLbytesinTLV)
3988    {
3989        case PH_FRINFC_MIFARESTD_VAL3:
3990            /* if TLV is found then set card state */
3991            Result = ((NdefMap->TLVStruct.NdefTLVFoundFlag ==
3992                        PH_FRINFC_MIFARESTD_FLAG1)?
3993                        phFriNfc_MapTool_SetCardState(NdefMap,
3994                        NdefMap->SendRecvBuf[TempLength]):
3995                        Result);
3996
3997            /* Check the length field is less than or
3998                equal to 0xFF if yes enter below statement
3999                else enter else if*/
4000            if((NdefMap->SendRecvBuf[TempLength] <
4001                PH_FRINFC_MIFARESTD_NDEFTLV_L) &&
4002                (Result == NFCSTATUS_SUCCESS))
4003            {
4004                NdefMap->StdMifareContainer.remainingSize -=
4005                                PH_FRINFC_MIFARESTD_VAL2;
4006
4007                Result = ((NdefMap->SendRecvBuf[TempLength] >
4008                            NdefMap->StdMifareContainer.remainingSize)?
4009                            (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
4010                            NFCSTATUS_INVALID_FORMAT)):
4011                            Result);
4012                TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0;
4013                if((NdefMap->TLVStruct.NdefTLVFoundFlag ==
4014                    PH_FRINFC_MIFARESTD_FLAG1) &&
4015                    (Result == NFCSTATUS_SUCCESS))
4016                {
4017                    NdefMap->TLVStruct.BytesRemainLinTLV =
4018                    NdefMap->SendRecvBuf[TempLength];
4019                    *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
4020
4021                }
4022                else if(Result == NFCSTATUS_SUCCESS)
4023                {
4024                    TempLength++;
4025                    Result = phFriNfc_MifStd_H_GetNxtTLV(NdefMap,
4026                                    &TempLength, &TL4bytesFlag);
4027
4028                    NdefMap->StdMifareContainer.remainingSize -=
4029                                    NdefMap->SendRecvBuf[TempLength];
4030                    if((TempLength >= PH_FRINFC_MIFARESTD_BYTES_READ) &&
4031                        (*CRFlag == PH_FRINFC_MIFARESTD_FLAG0))
4032                    {
4033                        *NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
4034                        Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap);
4035                    }
4036                }
4037
4038                else
4039                {
4040                    /* do nothing */
4041                }
4042            }
4043            else if((NdefMap->SendRecvBuf[TempLength] ==
4044                    PH_FRINFC_MIFARESTD_NDEFTLV_L) &&
4045                    (Result == NFCSTATUS_SUCCESS))
4046            {
4047                TempLength++;
4048                NdefMap->StdMifareContainer.remainingSize -=
4049                                PH_FRINFC_MIFARESTD_VAL4;
4050                TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0;
4051                Result = (((((uint16_t)NdefMap->SendRecvBuf[TempLength] << 8) +
4052                                NdefMap->SendRecvBuf[(TempLength +
4053                                PH_FRINFC_MIFARESTD_VAL1)]) >
4054                                NdefMap->StdMifareContainer.remainingSize)?
4055                                (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
4056                                NFCSTATUS_INVALID_FORMAT)):
4057                                Result);
4058                if((NdefMap->TLVStruct.NdefTLVFoundFlag ==
4059                    PH_FRINFC_MIFARESTD_FLAG1) &&
4060                    (Result == NFCSTATUS_SUCCESS))
4061                {
4062                    NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
4063                    NdefMap->TLVStruct.BytesRemainLinTLV =
4064                                (((uint16_t)NdefMap->SendRecvBuf[TempLength] << 8) +
4065                                NdefMap->SendRecvBuf[(TempLength +
4066                                PH_FRINFC_MIFARESTD_VAL1)]);
4067                    *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
4068                }
4069                else if(Result == NFCSTATUS_SUCCESS)
4070                {
4071                    TempLength++;
4072
4073                    Result = phFriNfc_MifStd_H_GetNxtTLV(NdefMap,
4074                                &TempLength, &TL4bytesFlag);
4075                    NdefMap->StdMifareContainer.remainingSize -=
4076                                (((uint16_t)NdefMap->SendRecvBuf[TempLength] << 8) +
4077                                NdefMap->SendRecvBuf[(TempLength +
4078                                PH_FRINFC_MIFARESTD_VAL1)]);
4079
4080                    *NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
4081                    Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap);
4082                }
4083                else
4084                {
4085                    /* do nothing */
4086                    *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
4087                }
4088            }
4089            else
4090            {
4091                /* Result = Error */
4092                Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
4093                                NFCSTATUS_INVALID_FORMAT);
4094                *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
4095            }
4096            break;
4097
4098        case PH_FRINFC_MIFARESTD_VAL2:
4099        case PH_FRINFC_MIFARESTD_VAL1:
4100            ShiftLength = ((NdefMap->TLVStruct.NoLbytesinTLV ==
4101                            PH_FRINFC_MIFARESTD_VAL1)?
4102                            ((NdefMap->TLVStruct.prevLenByteValue << 8) +
4103                            NdefMap->SendRecvBuf[TempLength]):
4104                            (((uint16_t)NdefMap->SendRecvBuf[TempLength] << 8) +
4105                            NdefMap->SendRecvBuf[(TempLength +
4106                            PH_FRINFC_MIFARESTD_VAL1)]));
4107            if((((  NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
4108                    NdefMap->StdMifareContainer.NdefBlocks) *
4109                    PH_FRINFC_MIFARESTD_BLOCK_BYTES) +
4110                    (PH_FRINFC_MIFARESTD_BLOCK_BYTES -
4111                    TempLength)) <
4112                    ShiftLength)
4113            {
4114                /* Result = Error */
4115                Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
4116                                NFCSTATUS_INVALID_PARAMETER);
4117                *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
4118            }
4119            else
4120            {
4121                NdefMap->StdMifareContainer.remainingSize -=
4122                                    PH_FRINFC_MIFARESTD_VAL2;
4123                if(NdefMap->TLVStruct.NdefTLVFoundFlag ==
4124                        PH_FRINFC_MIFARESTD_FLAG1)
4125                {
4126                    NdefMap->TLVStruct.BytesRemainLinTLV = ShiftLength;
4127                    if(NdefMap->TLVStruct.BytesRemainLinTLV >
4128                        NdefMap->StdMifareContainer.remainingSize)
4129                    {
4130                        Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
4131                                                NFCSTATUS_INVALID_FORMAT);
4132                    }
4133                    *CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
4134                    *NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
4135                }
4136                else
4137                {
4138                    NdefMap->StdMifareContainer.remainingSize -=
4139                                        ShiftLength;
4140                    *CRFlag = PH_FRINFC_MIFARESTD_FLAG0;
4141                    TempLength += PH_FRINFC_MIFARESTD_VAL2;
4142                    TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG1;
4143                    Result = ((NdefMap->TLVStruct.NdefTLVFoundFlag == PH_FRINFC_MIFARESTD_FLAG1)?
4144                                NFCSTATUS_SUCCESS:
4145                                phFriNfc_MifStd_H_GetNxtTLV(NdefMap, &TempLength, &TL4bytesFlag));
4146
4147                    *NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
4148                    Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap);
4149                }
4150            }
4151            break;
4152
4153        default:
4154            break;
4155    }
4156    return Result;
4157}
4158
4159static void phFriNfc_MifStd_H_Get1kStTrail(phFriNfc_NdefMap_t  *NdefMap)
4160{
4161    switch((NdefMap->StdMifareContainer.currentBlock % 4))
4162    {
4163        case PH_FRINFC_MIFARESTD_VAL0:
4164            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4165                            (NdefMap->StdMifareContainer.currentBlock +
4166                                        PH_FRINFC_MIFARESTD_MAD_BLK3);
4167            break;
4168
4169        case PH_FRINFC_MIFARESTD_VAL1:
4170            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4171                        (NdefMap->StdMifareContainer.currentBlock +
4172                                        PH_FRINFC_MIFARESTD_MAD_BLK2);
4173            break;
4174
4175        case PH_FRINFC_MIFARESTD_VAL2:
4176            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4177                        (NdefMap->StdMifareContainer.currentBlock +
4178                                        PH_FRINFC_MIFARESTD_MAD_BLK1);
4179            break;
4180
4181        default:
4182            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4183                        NdefMap->StdMifareContainer.currentBlock;
4184            break;
4185    }
4186}
4187
4188static void phFriNfc_MifStd_H_Get4kStTrail(phFriNfc_NdefMap_t  *NdefMap)
4189{
4190    switch((NdefMap->StdMifareContainer.currentBlock % 16))
4191    {
4192        case PH_FRINFC_MIFARESTD_MAD_BLK0:
4193            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4194                            (NdefMap->StdMifareContainer.currentBlock +
4195                                        PH_FRINFC_MIFARESTD_BLK15);
4196            break;
4197
4198        case PH_FRINFC_MIFARESTD_MAD_BLK1:
4199            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4200                        (NdefMap->StdMifareContainer.currentBlock +
4201                                        PH_FRINFC_MIFARESTD_BLK14);
4202            break;
4203
4204        case PH_FRINFC_MIFARESTD_MAD_BLK2:
4205            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4206                        (NdefMap->StdMifareContainer.currentBlock +
4207                                        PH_FRINFC_MIFARESTD_BLK13);
4208            break;
4209
4210        case PH_FRINFC_MIFARESTD_MAD_BLK3:
4211            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4212                        (NdefMap->StdMifareContainer.currentBlock +
4213                                        PH_FRINFC_MIFARESTD_BLK12);
4214            break;
4215
4216        case PH_FRINFC_MIFARESTD_BLK4:
4217            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4218                        (NdefMap->StdMifareContainer.currentBlock +
4219                                        PH_FRINFC_MIFARESTD_BLK11);
4220            break;
4221
4222        case PH_FRINFC_MIFARESTD_BLK5:
4223            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4224                        (NdefMap->StdMifareContainer.currentBlock +
4225                                        PH_FRINFC_MIFARESTD_BLK10);
4226            break;
4227
4228        case PH_FRINFC_MIFARESTD_BLK6:
4229            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4230                        (NdefMap->StdMifareContainer.currentBlock +
4231                                        PH_FRINFC_MIFARESTD_BLK9);
4232            break;
4233
4234        case PH_FRINFC_MIFARESTD_BLK7:
4235            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4236                        (NdefMap->StdMifareContainer.currentBlock +
4237                                        PH_FRINFC_MIFARESTD_BLK8);
4238            break;
4239
4240        case PH_FRINFC_MIFARESTD_BLK8:
4241            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4242                        (NdefMap->StdMifareContainer.currentBlock +
4243                                        PH_FRINFC_MIFARESTD_BLK7);
4244            break;
4245
4246        case PH_FRINFC_MIFARESTD_BLK9:
4247            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4248                        (NdefMap->StdMifareContainer.currentBlock +
4249                                        PH_FRINFC_MIFARESTD_BLK6);
4250            break;
4251
4252        case PH_FRINFC_MIFARESTD_BLK10:
4253            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4254                        (NdefMap->StdMifareContainer.currentBlock +
4255                                        PH_FRINFC_MIFARESTD_BLK5);
4256            break;
4257
4258        case PH_FRINFC_MIFARESTD_BLK11:
4259            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4260                        (NdefMap->StdMifareContainer.currentBlock +
4261                                        PH_FRINFC_MIFARESTD_BLK4);
4262            break;
4263
4264        case PH_FRINFC_MIFARESTD_BLK12:
4265            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4266                        (NdefMap->StdMifareContainer.currentBlock +
4267                                        PH_FRINFC_MIFARESTD_MAD_BLK3);
4268            break;
4269
4270        case PH_FRINFC_MIFARESTD_BLK13:
4271            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4272                        (NdefMap->StdMifareContainer.currentBlock +
4273                                        PH_FRINFC_MIFARESTD_MAD_BLK2);
4274            break;
4275
4276        case PH_FRINFC_MIFARESTD_BLK14:
4277            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4278                        (NdefMap->StdMifareContainer.currentBlock +
4279                                        PH_FRINFC_MIFARESTD_MAD_BLK1);
4280            break;
4281
4282        default:
4283            NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4284                        NdefMap->StdMifareContainer.currentBlock;
4285            break;
4286    }
4287}
4288
4289static NFCSTATUS phFriNfc_MifStd_H_ProChkNdef(phFriNfc_NdefMap_t        *NdefMap)
4290{
4291    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
4292    /* Copy remaining bytes into the AID array
4293        from Receive Buffer till array number 7 in aid */
4294    if(NdefMap->StdMifareContainer.currentBlock ==
4295        PH_FRINFC_MIFARESTD_VAL1)
4296    {
4297        /* Helper Function to Store AID Information */
4298        phFriNfc_MifStd_H_fillAIDarray(NdefMap);
4299
4300        NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_VAL2;
4301        /* read remaining AIDs from block number 2 */
4302        Result = ((NdefMap->StdMifareContainer.aidCompleteFlag ==
4303                PH_FRINFC_MIFARESTD_FLAG1)?
4304                Result:
4305                phFriNfc_MifareStdMap_ChkNdef( NdefMap));
4306    }
4307    else if(((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD) &&
4308            (NdefMap->StdMifareContainer.currentBlock ==
4309            PH_FRINFC_MIFARESTD_MAD_BLK2)) || (
4310            (NdefMap->StdMifareContainer.currentBlock ==
4311            PH_FRINFC_MIFARESTD_MAD_BLK66) &&
4312            (NdefMap->CardType ==
4313            PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD)))
4314    {
4315        /* Helper Function to Store AID Information */
4316        phFriNfc_MifStd_H_fillAIDarray(NdefMap);
4317
4318        NdefMap->StdMifareContainer.aidCompleteFlag =
4319                        PH_FRINFC_MIFARESTD_FLAG1;
4320    }/* Mifare 1k and Mifare 4k end Check */
4321    else if((NdefMap->StdMifareContainer.currentBlock >
4322            PH_FRINFC_MIFARESTD_VAL1) &&
4323            (NdefMap->CardType ==
4324            PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD))
4325    {
4326        phFriNfc_MifStd_H_fillAIDarray(NdefMap);
4327        /* read remaining AIDs from block number 2 */
4328        /* Mifare 4k Helper Function */
4329        Result = ((NdefMap->StdMifareContainer.aidCompleteFlag ==
4330                    PH_FRINFC_MIFARESTD_FLAG1)?
4331                    Result:
4332                    phFriNfc_MifStd4k_H_CheckNdef(NdefMap));
4333    } /* Card Type 4k Check */
4334    else
4335    {
4336        /* Since we have decided temporarily not to go
4337            for any new error codes we are using
4338            NFCSTATUS_INVALID_PARAMETER even though it is not
4339            the relevant error code here TBD */
4340        Result = PHNFCSTVAL(    CID_FRI_NFC_NDEF_MAP,
4341                                NFCSTATUS_INVALID_PARAMETER);
4342    }
4343
4344    if(NdefMap->StdMifareContainer.aidCompleteFlag ==
4345                        PH_FRINFC_MIFARESTD_FLAG1)
4346    {
4347        NdefMap->StdMifareContainer.ChkNdefCompleteFlag =
4348                        PH_FRINFC_MIFARESTD_FLAG1;
4349        /*  The check for NDEF compliant information is now over for
4350            the Mifare 1K card.
4351            Update(decrement) the NoOfNdefCompBlocks as much required,
4352            depending on the NDEF compliant information found */
4353        /* Check the Sectors are Ndef Compliant */
4354        phFriNfc_MifStd_H_ChkNdefCmpltSects(NdefMap);
4355        if((NdefMap->StdMifareContainer.NoOfNdefCompBlocks == 0) ||
4356            (NdefMap->StdMifareContainer.NoOfNdefCompBlocks > 255))
4357        {
4358            Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
4359                                NFCSTATUS_NO_NDEF_SUPPORT);
4360        }
4361        else
4362        {
4363            NdefMap->StdMifareContainer.aidCompleteFlag =
4364                            PH_FRINFC_MIFARESTD_FLAG0;
4365            NdefMap->StdMifareContainer.NFCforumSectFlag =
4366                                    PH_FRINFC_MIFARESTD_FLAG0;
4367            NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_BLK4;
4368            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
4369            Result = ((Result != NFCSTATUS_SUCCESS)?
4370                        Result:phFriNfc_MifStd_H_AuthSector(NdefMap));
4371        }
4372
4373    }
4374    return Result;
4375}
4376
4377static NFCSTATUS phFriNfc_MifStd_H_ProAuth(phFriNfc_NdefMap_t       *NdefMap)
4378{
4379    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
4380    if(NdefMap->TLVStruct.NdefTLVAuthFlag ==
4381       PH_FRINFC_MIFARESTD_FLAG1)
4382    {
4383        NdefMap->TLVStruct.NdefTLVAuthFlag =
4384                                    PH_FRINFC_MIFARESTD_FLAG0;
4385        NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG1;
4386        Result = phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap);
4387    }
4388    else
4389    {
4390        NdefMap->StdMifareContainer.AuthDone = 1;
4391        NdefMap->StdMifareContainer.ReadAcsBitFlag = 1;
4392        Result = phFriNfc_MifStd_H_RdAcsBit(NdefMap);
4393    }
4394    return Result;
4395}
4396
4397static NFCSTATUS phFriNfc_MifStd_H_Rd16Bytes(phFriNfc_NdefMap_t     *NdefMap,
4398                                              uint8_t               BlockNo)
4399{
4400    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
4401    NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = BlockNo;
4402    NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_READ;
4403    *NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
4404#ifndef PH_HAL4_ENABLE
4405    NdefMap->Cmd.MfCmd = phHal_eMifareCmdListMifareRead;
4406#else
4407    NdefMap->Cmd.MfCmd = phHal_eMifareRead;
4408#endif
4409    NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process;
4410    NdefMap->MapCompletionInfo.Context = NdefMap;
4411    /* Call the Overlapped HAL Transceive function */
4412    Result = phFriNfc_OvrHal_Transceive(    NdefMap->LowerDevice,
4413                                            &NdefMap->MapCompletionInfo,
4414                                            NdefMap->psRemoteDevInfo,
4415                                            NdefMap->Cmd,
4416                                            &NdefMap->psDepAdditionalInfo,
4417                                            NdefMap->SendRecvBuf,
4418                                            NdefMap->SendLength,
4419                                            NdefMap->SendRecvBuf,
4420                                            NdefMap->SendRecvLength);
4421    return Result;
4422}
4423
4424static NFCSTATUS phFriNfc_MifStd_H_ProAcsBits(phFriNfc_NdefMap_t        *NdefMap)
4425{
4426    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
4427    uint8_t     CRFlag = PH_FRINFC_MIFARESTD_FLAG0;
4428    if(*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ)
4429    {
4430        if(NdefMap->StdMifareContainer.ReadAcsBitFlag ==
4431            PH_FRINFC_MIFARESTD_FLAG1)
4432        {
4433            /* check for the correct access bits */
4434            Result = phFriNfc_MifStd_H_ChkAcsBit(NdefMap);
4435
4436            if((NdefMap->StdMifareContainer.ChkNdefFlag ==
4437                PH_FRINFC_MIFARESTD_FLAG1) &&
4438                (Result == NFCSTATUS_SUCCESS))
4439            {
4440                if(NdefMap->CardState ==
4441                    PH_NDEFMAP_CARD_STATE_INVALID)
4442                {
4443                    NdefMap->StdMifareContainer.NoOfNdefCompBlocks =
4444                    ((NdefMap->StdMifareContainer.currentBlock >=
4445                    PH_FRINFC_MIFARESTD4K_BLK128)?
4446                    (NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
4447                                    PH_FRINFC_MIFARESTD_BLK15):
4448                    (NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
4449                                    PH_FRINFC_MIFARESTD_MAD_BLK3));
4450
4451                    NdefMap->StdMifareContainer.ProprforumSectFlag =
4452                        ((NdefMap->StdMifareContainer.NFCforumSectFlag ==
4453                        PH_FRINFC_MIFARESTD_FLAG1)?
4454                        PH_FRINFC_MIFARESTD_PROP_2ND_CONFIG:
4455                        PH_FRINFC_MIFARESTD_PROP_3RD_CONFIG);
4456
4457                    Result = phFriNfc_MifStd_H_ProStatNotValid(NdefMap, Result);
4458                }
4459                else
4460                {
4461                    NdefMap->StdMifareContainer.NFCforumSectFlag =
4462                        (((NdefMap->StdMifareContainer.currentBlock == 64) &&
4463                        (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD))?
4464                        NdefMap->StdMifareContainer.NFCforumSectFlag:
4465                                            PH_FRINFC_MIFARESTD_FLAG1);
4466                }
4467
4468                if(NdefMap->StdMifareContainer.ProprforumSectFlag !=
4469                    PH_FRINFC_MIFARESTD_PROP_2ND_CONFIG)
4470                {
4471                    NdefMap->StdMifareContainer.ReadAcsBitFlag = PH_FRINFC_MIFARESTD_FLAG0;
4472                                /* ((NdefMap->StdMifareContainer.ReadCompleteFlag ==
4473                                        PH_FRINFC_MIFARESTD_FLAG1)?
4474                                        PH_FRINFC_MIFARESTD_FLAG0:
4475                                        PH_FRINFC_MIFARESTD_FLAG1);*/
4476
4477                    NdefMap->StdMifareContainer.ReadCompleteFlag =
4478                    (uint8_t)((((((NdefMap->StdMifareContainer.currentBlock +
4479                        PH_FRINFC_MIFARESTD_VAL4) >=
4480                        PH_FRINFC_MIFARESTD1K_MAX_BLK) &&
4481                        (NdefMap->CardType ==
4482                        PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD)) &&
4483                        (NdefMap->StdMifareContainer.ReadCompleteFlag ==
4484                        PH_FRINFC_MIFARESTD_FLAG0)) ||
4485                        (NdefMap->StdMifareContainer.ReadCompleteFlag ==
4486                        PH_FRINFC_MIFARESTD_FLAG1))?
4487                        PH_FRINFC_MIFARESTD_FLAG1:
4488                        PH_FRINFC_MIFARESTD_FLAG0);
4489
4490                    NdefMap->StdMifareContainer.ReadCompleteFlag =
4491                    (uint8_t)((((((uint16_t)(NdefMap->StdMifareContainer.currentBlock +
4492                        PH_FRINFC_MIFARESTD_VAL4) >=
4493                        PH_FRINFC_MIFARESTD4K_MAX_BLK) &&
4494                        (NdefMap->CardType ==
4495                        PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD)) &&
4496                        (NdefMap->StdMifareContainer.ReadCompleteFlag ==
4497                        PH_FRINFC_MIFARESTD_FLAG0)) ||
4498                        (NdefMap->StdMifareContainer.ReadCompleteFlag ==
4499                        PH_FRINFC_MIFARESTD_FLAG1))?
4500                        PH_FRINFC_MIFARESTD_FLAG1:
4501                        PH_FRINFC_MIFARESTD_FLAG0);
4502
4503                    NdefMap->StdMifareContainer.currentBlock =
4504                        ((NdefMap->StdMifareContainer.ReadCompleteFlag ==
4505                            PH_FRINFC_MIFARESTD_FLAG1)?
4506                            PH_FRINFC_MIFARESTD_BLK4:
4507                            NdefMap->StdMifareContainer.currentBlock);
4508
4509                    Result =
4510                    ((NdefMap->StdMifareContainer.ReadCompleteFlag ==
4511                            PH_FRINFC_MIFARESTD_FLAG1)?
4512                            phFriNfc_MifStd_H_BlkChk(NdefMap):
4513                            Result);
4514                }
4515            }
4516
4517            Result = ((Result != NFCSTATUS_SUCCESS)?
4518                        Result:
4519                        phFriNfc_MifStd_H_ChkRdWr(NdefMap));
4520        }
4521        else
4522        {
4523            NdefMap->StdMifareContainer.ChkNdefFlag =
4524                PH_FRINFC_MIFARESTD_FLAG0;
4525            /* Here its required to read the entire card to know the */
4526            /* Get exact ndef size of the card */
4527            Result = phFriNfc_MifStd_H_ChkTLVs(NdefMap, &CRFlag);
4528        }
4529    }
4530    else
4531    {
4532        /* Since we have decided temporarily not to go
4533            for any new error codes we are using
4534            NFCSTATUS_INVALID_PARAMETER even though it is not
4535            the relevant error code here TBD */
4536        Result = PHNFCSTVAL(    CID_FRI_NFC_NDEF_MAP,
4537                                NFCSTATUS_INVALID_PARAMETER);
4538    }
4539
4540    return Result;
4541}
4542
4543static NFCSTATUS phFriNfc_MifStd_H_GPBChk(phFriNfc_NdefMap_t        *NdefMap)
4544{
4545    NFCSTATUS Result = NFCSTATUS_SUCCESS;
4546
4547    /* Spec version needs to be checked every time */
4548    Result = phFriNfc_MapTool_ChkSpcVer(NdefMap, PH_FRINFC_MIFARESTD_VAL9);
4549
4550    /* Check rhe read and write access field
4551        in GPB is 00b
4552        bit 0 and 1 for write access check
4553        bit 2 and 3 for read access check */
4554    if(Result == NFCSTATUS_SUCCESS)
4555    {
4556        if(((NdefMap->SendRecvBuf[
4557            PH_FRINFC_MIFARESTD_VAL9] &
4558            PH_FRINFC_MIFARESTD_MASK_GPB_WR) ==
4559            PH_FRINFC_MIFARESTD_GPB_RD_WR_VAL) &&
4560            ((NdefMap->SendRecvBuf[
4561            PH_FRINFC_MIFARESTD_VAL9] &
4562            PH_FRINFC_MIFARESTD_MASK_GPB_RD) ==
4563            PH_FRINFC_MIFARESTD_GPB_RD_WR_VAL))
4564        {
4565            NdefMap->CardState = (((NdefMap->StdMifareContainer.ChkNdefFlag ==
4566                                    PH_FRINFC_MIFARESTD_FLAG1) ||
4567                                    (NdefMap->StdMifareContainer.ReadNdefFlag ==
4568                                    PH_FRINFC_MIFARESTD_FLAG1) ||
4569                                    (NdefMap->StdMifareContainer.WrNdefFlag ==
4570                                    PH_FRINFC_MIFARESTD_FLAG1))?
4571                                    PH_NDEFMAP_CARD_STATE_INITIALIZED:
4572                                    PH_NDEFMAP_CARD_STATE_READ_WRITE);
4573        }
4574        else if(((NdefMap->SendRecvBuf[
4575            PH_FRINFC_MIFARESTD_VAL9] &
4576            PH_FRINFC_MIFARESTD_MASK_GPB_WR) !=
4577            PH_FRINFC_MIFARESTD_GPB_RD_WR_VAL) &&
4578            ((NdefMap->SendRecvBuf[
4579            PH_FRINFC_MIFARESTD_VAL9] &
4580            PH_FRINFC_MIFARESTD_MASK_GPB_RD) ==
4581            PH_FRINFC_MIFARESTD_GPB_RD_WR_VAL))
4582        {
4583            /* write access not given
4584            only read access check */
4585            NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_ONLY;
4586        }
4587        else
4588        {
4589            NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID;
4590        }
4591    }
4592    return Result;
4593}
4594
4595static NFCSTATUS phFriNfc_MifStd_H_ProStatNotValid(phFriNfc_NdefMap_t        *NdefMap,
4596                                                   NFCSTATUS                 status)
4597{
4598    NFCSTATUS Result = status;
4599    /* if NFC forum sector is not found before the proprietary one then
4600        authenticate the next sector
4601        Else it is a error*/
4602    if(NdefMap->StdMifareContainer.NFCforumSectFlag ==
4603        PH_FRINFC_MIFARESTD_FLAG0)
4604    {
4605        NdefMap->StdMifareContainer.ProprforumSectFlag =
4606                        PH_FRINFC_MIFARESTD_PROP_3RD_CONFIG;
4607        if(NdefMap->StdMifareContainer.currentBlock <
4608            PH_FRINFC_MIFARESTD4K_BLK128)
4609        {
4610#ifdef PH_HAL4_ENABLE
4611            /* Fix for the disovery problem,
4612                if 1st sector is invalid then ignore the remaining sectors and
4613                send an error if the card is mifare 1k,
4614                if the card is mifare 4k, then update the block number to 67 and
4615                continue.
4616                Even if the authentication of that block fails then send error */
4617            if(((NdefMap->StdMifareContainer.currentBlock <
4618                PH_FRINFC_MIFARESTD_BLK4) &&
4619                (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD)) ||
4620                ((NdefMap->StdMifareContainer.currentBlock <=
4621                PH_FRINFC_MIFARESTD_MAD_BLK67) &&
4622                (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD)))
4623            {
4624                Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
4625                                    NFCSTATUS_NO_NDEF_SUPPORT);
4626            }
4627            else if((NdefMap->StdMifareContainer.currentBlock <
4628                PH_FRINFC_MIFARESTD_BLK4) &&
4629                (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD))
4630            {
4631                Result = NFCSTATUS_SUCCESS;
4632                NdefMap->StdMifareContainer.currentBlock =
4633                            PH_FRINFC_MIFARESTD_MAD_BLK67;
4634            }
4635            else
4636#endif /* #ifdef PH_HAL4_ENABLE */
4637            if(((NdefMap->StdMifareContainer.currentBlock +
4638                PH_FRINFC_MIFARESTD_BLK4) >
4639                PH_FRINFC_MIFARESTD1K_MAX_BLK) &&
4640                (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD))
4641            {
4642                Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
4643                            NFCSTATUS_NO_NDEF_SUPPORT);
4644            }
4645            else
4646            {
4647                NdefMap->StdMifareContainer.remainingSize -=
4648                        (PH_FRINFC_MIFARESTD_MAD_BLK3 * PH_FRINFC_MIFARESTD_BLOCK_BYTES);
4649                NdefMap->StdMifareContainer.currentBlock +=
4650                                PH_FRINFC_MIFARESTD_BLK4;
4651                Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
4652            }
4653        }
4654        else if((NdefMap->StdMifareContainer.currentBlock +
4655                PH_FRINFC_MIFARESTD_BLK15) >
4656                PH_FRINFC_MIFARESTD4K_MAX_BLK)
4657        {
4658            Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
4659                        NFCSTATUS_NO_NDEF_SUPPORT);
4660        }
4661        else
4662        {
4663            NdefMap->StdMifareContainer.remainingSize -=
4664                        (PH_FRINFC_MIFARESTD_BLK15 * PH_FRINFC_MIFARESTD_BLOCK_BYTES);
4665            NdefMap->StdMifareContainer.currentBlock +=
4666                                    PH_FRINFC_MIFARESTD_BLOCK_BYTES;
4667            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
4668        }
4669        Result = ((Result != NFCSTATUS_SUCCESS)?
4670                (PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
4671                        NFCSTATUS_NO_NDEF_SUPPORT)):
4672                phFriNfc_MifStd_H_AuthSector(NdefMap));
4673    }
4674    else if((NdefMap->StdMifareContainer.ProprforumSectFlag ==
4675            PH_FRINFC_MIFARESTD_PROP_3RD_CONFIG) &&
4676            (NdefMap->StdMifareContainer.NFCforumSectFlag ==
4677            PH_FRINFC_MIFARESTD_FLAG1))
4678    {
4679        /*  if the proprietary forum sector are found before
4680            NFC forum sector then again a proprietary
4681            forum sector are found after the NFC forum
4682            sector */
4683        Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
4684                            NFCSTATUS_NO_NDEF_SUPPORT);
4685    }
4686    else
4687    {
4688        NdefMap->StdMifareContainer.ProprforumSectFlag =
4689                        PH_FRINFC_MIFARESTD_PROP_2ND_CONFIG;
4690        switch(NdefMap->PrevOperation)
4691        {
4692        case PH_FRINFC_NDEFMAP_CHECK_OPE:
4693        case PH_FRINFC_NDEFMAP_GET_ACTSIZE_OPE:
4694            Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
4695                        NFCSTATUS_NO_NDEF_SUPPORT);
4696            break;
4697
4698        case PH_FRINFC_NDEFMAP_READ_OPE:
4699            if((NdefMap->TLVStruct.NdefTLVFoundFlag ==
4700                PH_FRINFC_MIFARESTD_FLAG1) &&
4701                (NdefMap->TLVStruct.NoLbytesinTLV ==
4702                PH_FRINFC_MIFARESTD_VAL0))
4703            {
4704                *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
4705                Result = NFCSTATUS_SUCCESS;
4706            }
4707            else
4708            {
4709                Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
4710                        NFCSTATUS_NO_NDEF_SUPPORT);
4711            }
4712            break;
4713
4714        case PH_FRINFC_NDEFMAP_WRITE_OPE:
4715        default:
4716            /* This means the further write is not possible,
4717                EOF_NDEF_CONTAINER_REACHED */
4718            NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
4719                                PH_FRINFC_MIFARESTD_FLAG1;
4720            /* Write the length to the L field in the TLV */
4721            NdefMap->StdMifareContainer.TempBlockNo =
4722                        NdefMap->StdMifareContainer.currentBlock;
4723            phFriNfc_MifStd_H_SetNdefBlkAuth(NdefMap);
4724            NdefMap->StdMifareContainer.currentBlock =
4725                            NdefMap->TLVStruct.NdefTLVBlock;
4726            Result = phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap);
4727            break;
4728        }
4729    }
4730    return Result;
4731}
4732
4733static NFCSTATUS phFriNfc_MifStd_H_RdBeforeWr(phFriNfc_NdefMap_t        *NdefMap)
4734{
4735    NFCSTATUS Result = NFCSTATUS_SUCCESS;
4736    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_BEF_WR;
4737    NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE;
4738
4739    Result = phFriNfc_MifStd_H_Rd16Bytes(NdefMap,
4740                    NdefMap->StdMifareContainer.currentBlock);
4741    return Result;
4742}
4743
4744static NFCSTATUS phFriNfc_MifStd_H_ProBytesToWr(phFriNfc_NdefMap_t        *NdefMap)
4745{
4746    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
4747    uint8_t     TempLength = PH_FRINFC_MIFARESTD_VAL0;
4748
4749    if(*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ)
4750    {
4751        (void)memcpy(&NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL1],
4752                    NdefMap->SendRecvBuf,
4753                    PH_FRINFC_MIFARESTD_BLOCK_BYTES);
4754
4755        /* Write to Ndef TLV Block */
4756        NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4757                            NdefMap->StdMifareContainer.currentBlock;
4758
4759        TempLength = ((NdefMap->StdMifareContainer.currentBlock ==
4760                        NdefMap->TLVStruct.NdefTLVBlock)?
4761                        phFriNfc_MifStd_H_UpdateTLV(NdefMap):
4762                        phFriNfc_MifStd_H_UpdRemTLV(NdefMap));
4763
4764        NdefMap->StdMifareContainer.remainingSize -=
4765            ((NdefMap->StdMifareContainer.remSizeUpdFlag ==
4766            PH_FRINFC_MIFARESTD_FLAG1)?
4767            PH_FRINFC_MIFARESTD_VAL2:
4768            PH_FRINFC_MIFARESTD_VAL0);
4769
4770        NdefMap->StdMifareContainer.remSizeUpdFlag = PH_FRINFC_MIFARESTD_FLAG0;
4771        NdefMap->State = PH_FRINFC_NDEFMAP_STATE_WR_TLV;
4772        Result = ((TempLength == PH_FRINFC_MIFARESTD_BLOCK_BYTES)?
4773                    phFriNfc_MifStd_H_WrTLV(NdefMap):
4774                    phFriNfc_MifStd_H_fillSendBuf(NdefMap, TempLength));
4775    }
4776    else
4777    {
4778        Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP,
4779                                NFCSTATUS_READ_FAILED);
4780    }
4781
4782    return Result;
4783}
4784
4785static uint8_t phFriNfc_MifStd_H_UpdateTLV(phFriNfc_NdefMap_t *NdefMap)
4786{
4787    uint8_t     TempLength = PH_FRINFC_MIFARESTD_VAL0;
4788    TempLength = (uint8_t)(NdefMap->TLVStruct.NdefTLVByte + PH_FRINFC_MIFARESTD_VAL1);
4789    /* Creating TLV */
4790    if(NdefMap->TLVStruct.NULLTLVCount >= 2)
4791    {
4792        if((PH_FRINFC_MIFARESTD_BYTES_READ - TempLength) ==
4793            PH_FRINFC_MIFARESTD_VAL0)
4794        {
4795            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
4796        }
4797        else
4798        {
4799            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
4800            TempLength++;
4801            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L0;
4802        }
4803    }
4804    else
4805    {
4806        switch((PH_FRINFC_MIFARESTD_BYTES_READ -
4807                TempLength))
4808        {
4809        case PH_FRINFC_MIFARESTD_VAL0:
4810            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
4811        break;
4812
4813        case PH_FRINFC_MIFARESTD_VAL1:
4814            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
4815            TempLength++;
4816            NdefMap->TLVStruct.prevLenByteValue =
4817                                    (uint16_t)((NdefMap->SendRecvBuf[TempLength] >=
4818                                    PH_FRINFC_MIFARESTD_NDEFTLV_L)?
4819                                    PH_FRINFC_MIFARESTD_VAL0:
4820                                    NdefMap->SendRecvBuf[TempLength]);
4821            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
4822        break;
4823
4824        case PH_FRINFC_MIFARESTD_VAL2:
4825            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
4826            TempLength++;
4827            NdefMap->TLVStruct.prevLenByteValue =
4828                                    (uint16_t)((NdefMap->SendRecvBuf[TempLength] >=
4829                                    PH_FRINFC_MIFARESTD_NDEFTLV_L)?
4830                                    NdefMap->SendRecvBuf[(TempLength +
4831                                                PH_FRINFC_MIFARESTD_VAL1)]:
4832                                    NdefMap->SendRecvBuf[TempLength]);
4833            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
4834            TempLength++;
4835            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
4836        break;
4837
4838        default:
4839            NdefMap->TLVStruct.prevLenByteValue =
4840                            NdefMap->SendRecvBuf[TempLength];
4841            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
4842            TempLength++;
4843            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
4844            TempLength++;
4845            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
4846            TempLength++;
4847            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L0;
4848        break;
4849        }
4850    }
4851
4852    return TempLength;
4853}
4854
4855static NFCSTATUS phFriNfc_MifStd_H_fillSendBuf(phFriNfc_NdefMap_t        *NdefMap,
4856                                               uint8_t                   Length)
4857{
4858    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
4859    uint16_t    RemainingBytes = PH_FRINFC_MIFARESTD_VAL0,
4860                BytesToWrite = PH_FRINFC_MIFARESTD_VAL0;
4861    uint8_t     index = PH_FRINFC_MIFARESTD_VAL0;
4862
4863    Length = (Length + PH_FRINFC_MIFARESTD_VAL1);
4864
4865    RemainingBytes = (uint16_t)((NdefMap->StdMifareContainer.remainingSize
4866                        < (uint16_t)(NdefMap->ApduBufferSize -
4867                        NdefMap->ApduBuffIndex))?
4868                        NdefMap->StdMifareContainer.remainingSize:
4869                        (NdefMap->ApduBufferSize -
4870                        NdefMap->ApduBuffIndex));
4871
4872    NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] =
4873                            NdefMap->StdMifareContainer.currentBlock;
4874        /* Get the number of bytes that can be written after copying
4875        the internal buffer */
4876    BytesToWrite = ((RemainingBytes <
4877                    ((PH_FRINFC_MIFARESTD_WR_A_BLK - Length) -
4878                    NdefMap->StdMifareContainer.internalLength))?
4879                    RemainingBytes:
4880                    ((PH_FRINFC_MIFARESTD_WR_A_BLK - Length) -
4881                    NdefMap->StdMifareContainer.internalLength));
4882
4883    if(NdefMap->StdMifareContainer.internalLength >
4884        PH_FRINFC_MIFARESTD_VAL0)
4885    {
4886        /* copy the internal buffer to the send buffer */
4887        (void)memcpy(&(NdefMap->SendRecvBuf[
4888                    Length]),
4889                    NdefMap->StdMifareContainer.internalBuf,
4890                    NdefMap->StdMifareContainer.internalLength);
4891    }
4892
4893    /* Copy Bytes to write in the send buffer */
4894    (void)memcpy(&(NdefMap->SendRecvBuf[
4895                (Length +
4896                NdefMap->StdMifareContainer.internalLength)]),
4897                &(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex]),
4898                BytesToWrite);
4899
4900    /* update number of bytes written from the user buffer */
4901    NdefMap->NumOfBytesWritten = BytesToWrite;
4902
4903    /* check the exact number of bytes written to a block including the
4904        internal length */
4905    *NdefMap->DataCount =
4906            ((BytesToWrite + NdefMap->StdMifareContainer.internalLength
4907            + Length) - PH_FRINFC_MIFARESTD_VAL1);
4908
4909    /* if total bytes to write in the card is less than 4 bytes then
4910    pad zeroes till 4 bytes */
4911    if((BytesToWrite + NdefMap->StdMifareContainer.internalLength +
4912        Length) < PH_FRINFC_MIFARESTD_WR_A_BLK)
4913    {
4914        for(index = (uint8_t)(BytesToWrite +
4915                    NdefMap->StdMifareContainer.internalLength +
4916                    Length);
4917            index < PH_FRINFC_MIFARESTD_WR_A_BLK;
4918            index++)
4919            {
4920                NdefMap->SendRecvBuf[index] = (uint8_t)((index ==
4921                                    (BytesToWrite + Length +
4922                                    NdefMap->StdMifareContainer.internalLength))?
4923                                    PH_FRINFC_MIFARESTD_TERMTLV_T:
4924                                    PH_FRINFC_MIFARESTD_NULLTLV_T);
4925
4926                NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
4927            }
4928    }
4929#ifdef PH_HAL4_ENABLE
4930
4931    NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
4932
4933#endif /* #ifdef PH_HAL4_ENABLE */
4934
4935    /* A temporary buffer to hold four bytes of data that is
4936        written to the card */
4937    (void)memcpy(NdefMap->StdMifareContainer.Buffer,
4938                &(NdefMap->SendRecvBuf[
4939                PH_FRINFC_MIFARESTD_VAL1]),
4940                PH_FRINFC_MIFARESTD_BLOCK_BYTES);
4941
4942    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_WR_TLV;
4943    Result = phFriNfc_MifStd_H_WrTLV(NdefMap);
4944    return Result;
4945}
4946
4947static NFCSTATUS phFriNfc_MifStd_H_WrTLV(phFriNfc_NdefMap_t        *NdefMap)
4948{
4949    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
4950
4951    /* set the data for additional data exchange*/
4952    NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
4953    NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0;
4954    NdefMap->psDepAdditionalInfo.NAD = 0;
4955    NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process;
4956    NdefMap->MapCompletionInfo.Context = NdefMap;
4957    /* Write from here */
4958    NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_WRITE;
4959
4960#ifndef PH_HAL4_ENABLE
4961    NdefMap->Cmd.MfCmd = phHal_eMifareCmdListMifareWrite16;
4962#else
4963    NdefMap->Cmd.MfCmd = phHal_eMifareWrite16;
4964#endif
4965
4966    *NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
4967    /* Call the Overlapped HAL Transceive function */
4968    Result = phFriNfc_OvrHal_Transceive(    NdefMap->LowerDevice,
4969                                            &NdefMap->MapCompletionInfo,
4970                                            NdefMap->psRemoteDevInfo,
4971                                            NdefMap->Cmd,
4972                                            &NdefMap->psDepAdditionalInfo,
4973                                            NdefMap->SendRecvBuf,
4974                                            NdefMap->SendLength,
4975                                            NdefMap->SendRecvBuf,
4976                                            NdefMap->SendRecvLength);
4977
4978
4979    return Result;
4980}
4981
4982static NFCSTATUS phFriNfc_MifStd_H_ProWrTLV(phFriNfc_NdefMap_t        *NdefMap)
4983{
4984    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
4985    /* Check that if complete TLV has been written in the
4986        card if yes enter the below check or go to else*/
4987    if(((((PH_FRINFC_MIFARESTD_BLOCK_BYTES -
4988            NdefMap->TLVStruct.NdefTLVByte) ==
4989            PH_FRINFC_MIFARESTD_VAL1) &&
4990            (NdefMap->TLVStruct.NULLTLVCount >=
4991            PH_FRINFC_MIFARESTD_VAL2)) ||
4992            (((PH_FRINFC_MIFARESTD_BLOCK_BYTES -
4993            NdefMap->TLVStruct.NdefTLVByte) <=
4994            PH_FRINFC_MIFARESTD_VAL3) &&
4995            (NdefMap->TLVStruct.NULLTLVCount ==
4996            PH_FRINFC_MIFARESTD_VAL0))) &&
4997            (NdefMap->StdMifareContainer.currentBlock ==
4998            NdefMap->TLVStruct.NdefTLVBlock))
4999    {
5000        /* increment the block and chekc the block is in the same sector
5001            using the block check function */
5002        NdefMap->StdMifareContainer.RdBeforeWrFlag = PH_FRINFC_MIFARESTD_FLAG1;
5003        NdefMap->StdMifareContainer.currentBlock++;
5004        NdefMap->StdMifareContainer.NdefBlocks++;
5005        Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
5006        if(Result == NFCSTATUS_SUCCESS)
5007        {
5008            Result = ((NdefMap->StdMifareContainer.AuthDone ==
5009                        PH_FRINFC_MIFARESTD_FLAG0)?
5010                        phFriNfc_MifStd_H_AuthSector(NdefMap):
5011                        phFriNfc_MifStd_H_RdBeforeWr(NdefMap));
5012        }
5013    }
5014    else
5015    {
5016        NdefMap->StdMifareContainer.RdBeforeWrFlag = PH_FRINFC_MIFARESTD_FLAG0;
5017        if(NdefMap->ApduBuffIndex <
5018                    (uint16_t)NdefMap->ApduBufferSize)
5019        {
5020            if(*NdefMap->DataCount < PH_FRINFC_MIFARESTD_BLOCK_BYTES)
5021            {
5022                /* Write complete, so next byte shall be */
5023                NdefMap->StdMifareContainer.internalLength =
5024                    *NdefMap->DataCount;
5025
5026                /* Copy bytes less than 16 to internal buffer
5027                    for the next write this can be used */
5028                (void)memcpy( NdefMap->StdMifareContainer.internalBuf,
5029                        NdefMap->StdMifareContainer.Buffer,
5030                        NdefMap->StdMifareContainer.internalLength);
5031            }
5032
5033            /* Increment the Send Buffer index */
5034             NdefMap->ApduBuffIndex += NdefMap->NumOfBytesWritten;
5035
5036             NdefMap->StdMifareContainer.remainingSize -=
5037                                        NdefMap->NumOfBytesWritten;
5038
5039             /* Check for the End of Card */
5040            if((NdefMap->StdMifareContainer.remainingSize  ==
5041                PH_FRINFC_MIFARESTD_VAL0) ||
5042                (NdefMap->ApduBuffIndex == NdefMap->ApduBufferSize))
5043            {
5044                NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
5045                (uint8_t)((NdefMap->StdMifareContainer.remainingSize  == 0)?
5046                    PH_FRINFC_MIFARESTD_FLAG1:PH_FRINFC_MIFARESTD_FLAG0);
5047
5048                if(NdefMap->StdMifareContainer.internalLength ==
5049                    PH_FRINFC_MIFARESTD_VAL0)
5050                {
5051                    NdefMap->StdMifareContainer.currentBlock++;
5052                    /* Mifare 4k Card, After 128th Block
5053                    each sector = 16 blocks in Mifare 4k */
5054                    Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
5055                    NdefMap->StdMifareContainer.NdefBlocks++;
5056                }
5057
5058                NdefMap->TLVStruct.SetTermTLVFlag =
5059                    (uint8_t)(((NdefMap->StdMifareContainer.remainingSize ==
5060                            PH_FRINFC_MIFARESTD_VAL0) ||
5061                            (NdefMap->TLVStruct.SetTermTLVFlag ==
5062                            PH_FRINFC_MIFARESTD_FLAG1))?
5063                            PH_FRINFC_MIFARESTD_FLAG1:
5064                            PH_FRINFC_MIFARESTD_FLAG0);
5065
5066            }
5067            else
5068            {
5069                NdefMap->StdMifareContainer.currentBlock++;
5070                /* Mifare 4k Card, After 128th Block
5071                each sector = 16 blocks in Mifare 4k */
5072                Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
5073                if(Result == NFCSTATUS_SUCCESS)
5074                {
5075                    NdefMap->StdMifareContainer.NdefBlocks++;
5076                    Result = ((NdefMap->StdMifareContainer.AuthDone ==
5077                                PH_FRINFC_MIFARESTD_FLAG1)?
5078                                phFriNfc_MifStd_H_WrABlock(NdefMap):
5079                                phFriNfc_MifStd_H_AuthSector(NdefMap));
5080                }
5081            }
5082        }
5083    }
5084
5085    if((Result == NFCSTATUS_SUCCESS) &&
5086        (NdefMap->TLVStruct.SetTermTLVFlag !=
5087        PH_FRINFC_MIFARESTD_FLAG1) &&
5088        (NdefMap->StdMifareContainer.remainingSize >
5089        PH_FRINFC_MIFARESTD_VAL0))
5090    {
5091        Result = phFriNfc_MifStd_H_WrTermTLV(NdefMap);
5092    }
5093    else
5094    {
5095        if((Result == NFCSTATUS_SUCCESS) &&
5096            (NdefMap->TLVStruct.SetTermTLVFlag ==
5097            PH_FRINFC_MIFARESTD_FLAG1))
5098        {
5099            /* Write the length to the L field in the TLV */
5100            NdefMap->StdMifareContainer.TempBlockNo =
5101                        NdefMap->StdMifareContainer.currentBlock;
5102            phFriNfc_MifStd_H_SetNdefBlkAuth(NdefMap);
5103            NdefMap->StdMifareContainer.currentBlock =
5104                            NdefMap->TLVStruct.NdefTLVBlock;
5105            Result = phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap);
5106        }
5107    }
5108    return Result;
5109}
5110
5111static uint8_t phFriNfc_MifStd_H_UpdRemTLV(phFriNfc_NdefMap_t        *NdefMap)
5112{
5113    uint8_t     TempLength = PH_FRINFC_MIFARESTD_VAL1;
5114    if(NdefMap->TLVStruct.NULLTLVCount >=
5115        PH_FRINFC_MIFARESTD_VAL2)
5116    {
5117        NdefMap->TLVStruct.prevLenByteValue = NdefMap->SendRecvBuf[TempLength];
5118        NdefMap->SendRecvBuf[TempLength] =
5119                        PH_FRINFC_MIFARESTD_NDEFTLV_L0;
5120    }
5121    else
5122    {
5123        switch((PH_FRINFC_MIFARESTD_BLOCK_BYTES -
5124            NdefMap->TLVStruct.NdefTLVByte))
5125        {
5126        case PH_FRINFC_MIFARESTD_VAL1:
5127            NdefMap->TLVStruct.prevLenByteValue =
5128                    (((NdefMap->SendRecvBuf[TempLength] ==
5129                    PH_FRINFC_MIFARESTD_NDEFTLV_L))?
5130                    (((uint16_t)NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL1)]
5131                    << PH_FRINFC_MIFARESTD_LEFTSHIFT8) +
5132                    NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL2)]):
5133                    NdefMap->SendRecvBuf[TempLength]);
5134            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
5135            TempLength++;
5136            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
5137            TempLength++;
5138            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L0;
5139            break;
5140
5141        case PH_FRINFC_MIFARESTD_VAL2:
5142            NdefMap->TLVStruct.prevLenByteValue =
5143                    (((NdefMap->SendRecvBuf[TempLength] ==
5144                        PH_FRINFC_MIFARESTD_NDEFTLV_L))?
5145                    (((uint16_t)NdefMap->SendRecvBuf[TempLength] <<
5146                    PH_FRINFC_MIFARESTD_LEFTSHIFT8) +
5147                    NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL1)]):
5148                    NdefMap->SendRecvBuf[TempLength]);
5149            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
5150            TempLength++;
5151            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L0;
5152            break;
5153
5154        case PH_FRINFC_MIFARESTD_VAL3:
5155        default:
5156            NdefMap->TLVStruct.prevLenByteValue =
5157                                            ((NdefMap->TLVStruct.prevLenByteValue <<
5158                                            PH_FRINFC_MIFARESTD_LEFTSHIFT8)
5159                                            + NdefMap->SendRecvBuf[TempLength]);
5160            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L0;
5161            break;
5162        }
5163    }
5164    return TempLength;
5165}
5166
5167static void phFriNfc_MifStd_H_fillTLV1(phFriNfc_NdefMap_t        *NdefMap)
5168{
5169    uint8_t     TempLength = (uint8_t)(NdefMap->TLVStruct.NdefTLVByte +
5170                            PH_FRINFC_MIFARESTD_VAL1);
5171
5172    NdefMap->TLVStruct.prevLenByteValue =
5173                    ((NdefMap->Offset == PH_FRINFC_NDEFMAP_SEEK_CUR)?
5174                    (NdefMap->TLVStruct.prevLenByteValue +
5175                    NdefMap->ApduBuffIndex):
5176                    NdefMap->ApduBuffIndex);
5177
5178    NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG1;
5179    switch(NdefMap->TLVStruct.NdefTLVByte)
5180    {
5181    case PH_FRINFC_MIFARESTD_VAL0:
5182        if(NdefMap->TLVStruct.prevLenByteValue >=
5183            PH_FRINFC_MIFARESTD_NDEFTLV_L)
5184        {
5185            NdefMap->SendRecvBuf[TempLength] =
5186                            (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >>
5187                            PH_FRINFC_MIFARESTD_RIGHTSHIFT8);
5188            NdefMap->SendRecvBuf[(TempLength +
5189                                PH_FRINFC_MIFARESTD_VAL1)] =
5190                            (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5191        }
5192        else
5193        {
5194            NdefMap->SendRecvBuf[TempLength] =
5195                            PH_FRINFC_MIFARESTD_NDEFTLV_T;
5196            NdefMap->SendRecvBuf[(TempLength +
5197                                PH_FRINFC_MIFARESTD_VAL1)] =
5198                            (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5199
5200            NdefMap->StdMifareContainer.RdAfterWrFlag =
5201                                PH_FRINFC_MIFARESTD_FLAG0;
5202        }
5203        break;
5204
5205    case PH_FRINFC_MIFARESTD_VAL1:
5206        if(NdefMap->TLVStruct.prevLenByteValue >=
5207            PH_FRINFC_MIFARESTD_NDEFTLV_L)
5208        {
5209            NdefMap->SendRecvBuf[TempLength - PH_FRINFC_MIFARESTD_VAL1] =
5210                            PH_FRINFC_MIFARESTD_NDEFTLV_L;
5211            NdefMap->SendRecvBuf[TempLength] =
5212                            (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >>
5213                            PH_FRINFC_MIFARESTD_RIGHTSHIFT8);
5214            NdefMap->SendRecvBuf[(TempLength +
5215                                PH_FRINFC_MIFARESTD_VAL1)] =
5216                            (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5217        }
5218        else
5219        {
5220            NdefMap->SendRecvBuf[TempLength] =
5221                            PH_FRINFC_MIFARESTD_NDEFTLV_T;
5222            NdefMap->SendRecvBuf[(TempLength +
5223                                PH_FRINFC_MIFARESTD_VAL1)] =
5224                            (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5225            NdefMap->StdMifareContainer.RdAfterWrFlag =
5226                                PH_FRINFC_MIFARESTD_FLAG0;
5227        }
5228        break;
5229
5230    case PH_FRINFC_MIFARESTD_VAL15:
5231        /* if "Type" of TLV present at byte 15 */
5232        if(NdefMap->TLVStruct.prevLenByteValue >=
5233            PH_FRINFC_MIFARESTD_NDEFTLV_L)
5234        {
5235            /* Update the null TLV, ndef TLV block and ndef TLV byte */
5236            NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
5237            NdefMap->TLVStruct.NdefTLVBlock =
5238                                NdefMap->StdMifareContainer.currentBlock;
5239            NdefMap->TLVStruct.NdefTLVByte =
5240                                (TempLength - PH_FRINFC_MIFARESTD_VAL3);
5241
5242            NdefMap->SendRecvBuf[(TempLength - PH_FRINFC_MIFARESTD_VAL2)] =
5243                            PH_FRINFC_MIFARESTD_NDEFTLV_T;
5244            NdefMap->SendRecvBuf[(TempLength - PH_FRINFC_MIFARESTD_VAL1)] =
5245                            PH_FRINFC_MIFARESTD_NDEFTLV_L;
5246            NdefMap->SendRecvBuf[TempLength] =
5247                            (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >>
5248                            PH_FRINFC_MIFARESTD_RIGHTSHIFT8);
5249        }
5250        else
5251        {
5252            NdefMap->SendRecvBuf[TempLength] =
5253                            PH_FRINFC_MIFARESTD_NDEFTLV_T;
5254        }
5255        break;
5256
5257    default:
5258        /* Already the TLV is present so just append the length field */
5259        if(NdefMap->TLVStruct.prevLenByteValue >=
5260            PH_FRINFC_MIFARESTD_NDEFTLV_L)
5261        {
5262            /* Update the null TLV, ndef TLV block and ndef TLV byte */
5263            NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
5264            NdefMap->TLVStruct.NdefTLVBlock =
5265                                NdefMap->StdMifareContainer.currentBlock;
5266            NdefMap->TLVStruct.NdefTLVByte =
5267                                (TempLength - PH_FRINFC_MIFARESTD_VAL3);
5268
5269            NdefMap->SendRecvBuf[(TempLength - PH_FRINFC_MIFARESTD_VAL2)] =
5270                                (uint8_t)PH_FRINFC_MIFARESTD_NDEFTLV_T;
5271            NdefMap->SendRecvBuf[(TempLength - PH_FRINFC_MIFARESTD_VAL1)] =
5272                                (uint8_t)PH_FRINFC_MIFARESTD_NDEFTLV_L;
5273            NdefMap->SendRecvBuf[TempLength] =
5274                                (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >>
5275                                PH_FRINFC_MIFARESTD_RIGHTSHIFT8);
5276            NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL1)] =
5277                                (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5278        }
5279        else
5280        {
5281            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
5282            NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL1)] =
5283                                (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5284        }
5285        NdefMap->StdMifareContainer.RdAfterWrFlag =
5286                                PH_FRINFC_MIFARESTD_FLAG0;
5287        break;
5288    }
5289}
5290
5291static void phFriNfc_MifStd_H_fillTLV2(phFriNfc_NdefMap_t        *NdefMap)
5292{
5293    uint8_t     TempLength = (uint8_t)(NdefMap->TLVStruct.NdefTLVByte +
5294                                PH_FRINFC_MIFARESTD_VAL1);
5295
5296    NdefMap->TLVStruct.prevLenByteValue = ((NdefMap->Offset ==
5297                                    PH_FRINFC_NDEFMAP_SEEK_CUR)?
5298                                    (NdefMap->TLVStruct.prevLenByteValue +
5299                                    NdefMap->ApduBuffIndex):
5300                                    NdefMap->ApduBuffIndex);
5301    NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG1;
5302    switch(NdefMap->TLVStruct.NdefTLVByte)
5303    {
5304    case PH_FRINFC_MIFARESTD_VAL13:
5305        if(NdefMap->TLVStruct.prevLenByteValue >=
5306            PH_FRINFC_MIFARESTD_NDEFTLV_L)
5307        {
5308            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
5309            TempLength++;
5310            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L;
5311            TempLength++;
5312            NdefMap->SendRecvBuf[TempLength] = (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >>
5313                                                PH_FRINFC_MIFARESTD_RIGHTSHIFT8);
5314        }
5315        else
5316        {
5317            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
5318            TempLength++;
5319            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
5320            TempLength++;
5321
5322            /* Update the null TLV, ndef TLV block and ndef TLV byte */
5323            NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL2;
5324            NdefMap->TLVStruct.NdefTLVBlock =
5325                                NdefMap->StdMifareContainer.currentBlock;
5326            NdefMap->TLVStruct.NdefTLVByte =
5327                                (TempLength - PH_FRINFC_MIFARESTD_VAL1);
5328
5329            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
5330        }
5331        break;
5332
5333    case PH_FRINFC_MIFARESTD_VAL14:
5334        if(NdefMap->TLVStruct.prevLenByteValue >=
5335            PH_FRINFC_MIFARESTD_NDEFTLV_L)
5336        {
5337            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
5338            TempLength++;
5339            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L;
5340        }
5341        else
5342        {
5343            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
5344            TempLength++;
5345            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
5346        }
5347        break;
5348
5349    case PH_FRINFC_MIFARESTD_VAL15:
5350        if(NdefMap->TLVStruct.prevLenByteValue >=
5351            PH_FRINFC_MIFARESTD_NDEFTLV_L)
5352        {
5353            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
5354        }
5355        else
5356        {
5357            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
5358        }
5359        break;
5360
5361    default:
5362        if(NdefMap->TLVStruct.prevLenByteValue >=
5363            PH_FRINFC_MIFARESTD_NDEFTLV_L)
5364        {
5365            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
5366            TempLength++;
5367            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L;
5368            TempLength++;
5369            NdefMap->SendRecvBuf[TempLength] = (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >>
5370                                                PH_FRINFC_MIFARESTD_RIGHTSHIFT8);
5371            TempLength++;
5372            NdefMap->SendRecvBuf[TempLength] =
5373                                (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5374        }
5375        else
5376        {
5377            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
5378            TempLength++;
5379            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T;
5380            TempLength++;
5381
5382            /* Update the null TLV, ndef TLV block and ndef TLV byte */
5383            NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL2;
5384            NdefMap->TLVStruct.NdefTLVBlock =
5385                                NdefMap->StdMifareContainer.currentBlock;
5386            NdefMap->TLVStruct.NdefTLVByte =
5387                                (TempLength - PH_FRINFC_MIFARESTD_VAL1);
5388
5389            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
5390            TempLength++;
5391            NdefMap->SendRecvBuf[TempLength] =
5392                                (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5393        }
5394        NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG0;
5395        break;
5396    }
5397}
5398
5399static NFCSTATUS phFriNfc_MifStd_H_CallWrNdefLen(phFriNfc_NdefMap_t        *NdefMap)
5400{
5401    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
5402    if(NdefMap->TLVStruct.NULLTLVCount >= PH_FRINFC_MIFARESTD_VAL2)
5403    {
5404        if((NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL0) ||
5405            (NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL1) )
5406        {
5407            /* In this case, current block is decremented because the
5408                NULL TLVs are in the previous block */
5409            NdefMap->StdMifareContainer.currentBlock--;
5410            Result = phFriNfc_MifStd_H_BlkChk_1(NdefMap);
5411        }
5412        else
5413        {
5414            /* case NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL15:
5415              Current block is incremented to update the remaining TLV
5416                structure */
5417            NdefMap->StdMifareContainer.currentBlock++;
5418            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
5419        }
5420    }
5421    else
5422    {
5423        if((NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL13) ||
5424            (NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL14) ||
5425            (NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL15))
5426        {
5427            /* Current block is incremented to update the remaining TLV
5428                structure */
5429            NdefMap->StdMifareContainer.currentBlock++;
5430            Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
5431        }
5432    }
5433
5434    Result = ((Result == NFCSTATUS_SUCCESS)?
5435            phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap):
5436            Result);
5437    return Result;
5438}
5439
5440static NFCSTATUS phFriNfc_MifStd_H_BlkChk_1(phFriNfc_NdefMap_t        *NdefMap)
5441{
5442    /* This function is to check the current block is in the authenticated sector and
5443        also check the block does not match with the sector trailer block. If there is a
5444        match then decrement the block and say the caller that the authentication
5445        is required */
5446    NFCSTATUS   Result  = NFCSTATUS_SUCCESS;
5447    uint8_t     SectorID = PH_FRINFC_MIFARESTD_VAL0;
5448
5449    /* Get a Sector ID for the Current Block */
5450    SectorID = phFriNfc_MifStd_H_GetSect(NdefMap->StdMifareContainer.currentBlock);
5451
5452    /* Check the sector id is valid or not and if valid then check the
5453        current block is greater than 128 */
5454    if((NdefMap->StdMifareContainer.aid[SectorID] ==
5455        PH_FRINFC_MIFARESTD_NDEF_COMP) &&
5456        (((SectorID <= PH_FRINFC_MIFARESTD_VAL15) &&
5457        (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD)) ||
5458        ((SectorID <= PH_FRINFC_MIFARESTD_SECTOR_NO39) &&
5459        (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD))))
5460    {
5461        if(NdefMap->StdMifareContainer.currentBlock > 128)
5462        {
5463            NdefMap->TLVStruct.NdefTLVAuthFlag =
5464            ((((NdefMap->StdMifareContainer.currentBlock +
5465                PH_FRINFC_MIFARESTD_VAL1) %
5466                PH_FRINFC_MIFARESTD_MAD_BLK16) ==
5467                PH_FRINFC_MIFARESTD_VAL0)?
5468                PH_FRINFC_MIFARESTD_FLAG1:
5469                PH_FRINFC_MIFARESTD_FLAG0);
5470
5471                NdefMap->StdMifareContainer.currentBlock -=
5472            ((((NdefMap->StdMifareContainer.currentBlock +
5473                PH_FRINFC_MIFARESTD_VAL1) %
5474                PH_FRINFC_MIFARESTD_MAD_BLK16) ==
5475                PH_FRINFC_MIFARESTD_VAL0)?
5476                PH_FRINFC_MIFARESTD_VAL1:
5477                PH_FRINFC_MIFARESTD_VAL0);
5478
5479        }
5480        else
5481        {
5482            NdefMap->TLVStruct.NdefTLVAuthFlag =
5483            ((((NdefMap->StdMifareContainer.currentBlock +
5484                PH_FRINFC_MIFARESTD_VAL1) %
5485                PH_FRINFC_MIFARESTD_BLK4) ==
5486                PH_FRINFC_MIFARESTD_VAL0)?
5487                PH_FRINFC_MIFARESTD_FLAG1:
5488                PH_FRINFC_MIFARESTD_FLAG0);
5489
5490            NdefMap->StdMifareContainer.currentBlock -=
5491            ((((NdefMap->StdMifareContainer.currentBlock +
5492                PH_FRINFC_MIFARESTD_VAL1) %
5493                PH_FRINFC_MIFARESTD_BLK4) ==
5494                PH_FRINFC_MIFARESTD_VAL1)?
5495                PH_FRINFC_MIFARESTD_VAL1:
5496                PH_FRINFC_MIFARESTD_VAL0);
5497
5498        }
5499    }
5500    else
5501    {
5502        /*Error: No Ndef Compliant Sectors present.*/
5503        Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
5504                            NFCSTATUS_INVALID_PARAMETER);
5505    }
5506
5507    return Result;
5508}
5509
5510static void phFriNfc_MifStd_H_fillTLV1_1(phFriNfc_NdefMap_t        *NdefMap)
5511{
5512    switch(NdefMap->TLVStruct.NdefTLVByte)
5513    {
5514    case PH_FRINFC_MIFARESTD_VAL0:
5515        /* In the first write ndef length procedure, the
5516            length is updated, in this case T and L = 0xFF of TLV are
5517            updated */
5518        NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
5519        NdefMap->TLVStruct.NdefTLVBlock =
5520                            NdefMap->StdMifareContainer.currentBlock;
5521        NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL14;
5522
5523        NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL15] =
5524                                PH_FRINFC_MIFARESTD_NDEFTLV_T;
5525        NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL16] =
5526                                PH_FRINFC_MIFARESTD_NDEFTLV_L;
5527        break;
5528
5529    case PH_FRINFC_MIFARESTD_VAL1:
5530        /* In the first write ndef length procedure, the
5531            length is updated, in this case T of TLV is
5532            updated */
5533        NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
5534        NdefMap->TLVStruct.NdefTLVBlock =
5535                            NdefMap->StdMifareContainer.currentBlock;
5536        NdefMap->TLVStruct.NdefTLVByte =
5537                            PH_FRINFC_MIFARESTD_VAL15;
5538        NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL16] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
5539        break;
5540
5541    case PH_FRINFC_MIFARESTD_VAL15:
5542    default:
5543        /* In the first ndef write length, part of the L field or only T
5544            (if update length is less than 255) is updated */
5545        NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL1] =
5546                                    (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5547        break;
5548    }
5549    NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG0;
5550}
5551
5552static void phFriNfc_MifStd_H_fillTLV2_1(phFriNfc_NdefMap_t        *NdefMap)
5553{
5554    uint8_t     TempLength = PH_FRINFC_MIFARESTD_VAL1;
5555    switch(NdefMap->TLVStruct.NdefTLVByte)
5556    {
5557    case PH_FRINFC_MIFARESTD_VAL13:
5558        /* In last write ndef length, part of length (L) field of TLV
5559            is updated now */
5560        NdefMap->SendRecvBuf[TempLength] =
5561                    (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5562        break;
5563
5564    case PH_FRINFC_MIFARESTD_VAL14:
5565        /* In last write ndef length, part of length (L) field of TLV
5566                is updated now */
5567        if(NdefMap->TLVStruct.prevLenByteValue >=
5568                    PH_FRINFC_MIFARESTD_NDEFTLV_L)
5569        {
5570            NdefMap->SendRecvBuf[TempLength] =
5571                            (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >>
5572                            PH_FRINFC_MIFARESTD_RIGHTSHIFT8);
5573            TempLength++;
5574            NdefMap->SendRecvBuf[TempLength] =
5575                            (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5576        }
5577        else
5578        {
5579            NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL2;
5580            NdefMap->TLVStruct.NdefTLVBlock =
5581                                NdefMap->StdMifareContainer.currentBlock;
5582            NdefMap->TLVStruct.NdefTLVByte =
5583                                (TempLength - PH_FRINFC_MIFARESTD_VAL1);
5584            NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T;
5585            TempLength++;
5586            NdefMap->SendRecvBuf[TempLength] =
5587                                        (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5588        }
5589        break;
5590
5591    case PH_FRINFC_MIFARESTD_VAL15:
5592    default:
5593        if(NdefMap->TLVStruct.prevLenByteValue >=
5594            PH_FRINFC_MIFARESTD_NDEFTLV_L)
5595        {
5596            /* In last write ndef length, only T of TLV is updated and
5597                length (L) field of TLV is updated now */
5598            NdefMap->SendRecvBuf[TempLength] =
5599                            PH_FRINFC_MIFARESTD_NDEFTLV_L;
5600            TempLength++;
5601            NdefMap->SendRecvBuf[TempLength] =
5602                            (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >>
5603                            PH_FRINFC_MIFARESTD_RIGHTSHIFT8);
5604            TempLength++;
5605            NdefMap->SendRecvBuf[TempLength] =
5606                            (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5607        }
5608        else
5609        {
5610            NdefMap->SendRecvBuf[TempLength] =
5611                            PH_FRINFC_MIFARESTD_NULLTLV_T;
5612            TempLength++;
5613            NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL2;
5614            NdefMap->TLVStruct.NdefTLVBlock =
5615                                NdefMap->StdMifareContainer.currentBlock;
5616            NdefMap->TLVStruct.NdefTLVByte =
5617                                (TempLength - PH_FRINFC_MIFARESTD_VAL1);
5618            NdefMap->SendRecvBuf[TempLength] =
5619                            PH_FRINFC_MIFARESTD_NDEFTLV_T;
5620            TempLength++;
5621            NdefMap->SendRecvBuf[TempLength] =
5622                        (uint8_t)NdefMap->TLVStruct.prevLenByteValue;
5623        }
5624        break;
5625    }
5626    NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG0;
5627}
5628
5629static NFCSTATUS phFriNfc_MifStd_H_RdTLV(phFriNfc_NdefMap_t        *NdefMap)
5630{
5631    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
5632    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_TLV;
5633    NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE;
5634
5635    Result = phFriNfc_MifStd_H_Rd16Bytes(NdefMap,
5636                NdefMap->StdMifareContainer.currentBlock);
5637    return Result;
5638}
5639
5640static NFCSTATUS phFriNfc_MifStd_H_ProRdTLV(phFriNfc_NdefMap_t        *NdefMap)
5641{
5642    NFCSTATUS   Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
5643                                    NFCSTATUS_INVALID_DEVICE_REQUEST);
5644    uint8_t     TempLength = PH_FRINFC_MIFARESTD_VAL0,
5645                NDEFFlag = PH_FRINFC_MIFARESTD_FLAG1;
5646
5647    /*TempLength = (uint8_t)(((NdefMap->TLVStruct.NULLTLVCount >=
5648                PH_FRINFC_MIFARESTD_VAL2) &&
5649                (NdefMap->TLVStruct.BytesRemainLinTLV > 0xFE))?
5650                ((NdefMap->TLVStruct.NdefTLVByte +
5651                PH_FRINFC_MIFARESTD_VAL2)%
5652                PH_FRINFC_MIFARESTD_VAL16):
5653                ((NdefMap->TLVStruct.NdefTLVByte +
5654                PH_FRINFC_MIFARESTD_VAL4)%
5655                PH_FRINFC_MIFARESTD_VAL16));*/
5656
5657    TempLength = (uint8_t)((NdefMap->TLVStruct.BytesRemainLinTLV <= 0xFE)?
5658                        ((NdefMap->TLVStruct.NdefTLVByte +
5659                        PH_FRINFC_MIFARESTD_VAL2)%
5660                        PH_FRINFC_MIFARESTD_VAL16):
5661                        ((NdefMap->TLVStruct.NdefTLVByte +
5662                        PH_FRINFC_MIFARESTD_VAL4)%
5663                        PH_FRINFC_MIFARESTD_VAL16));
5664
5665    if((*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ) &&
5666        (NdefMap->ApduBuffIndex < NdefMap->ApduBufferSize))
5667    {
5668        if(NdefMap->TLVStruct.BytesRemainLinTLV != 0)
5669        {
5670            NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
5671            /* To read the remaining length (L) in TLV */
5672            Result = phFriNfc_MifStd_H_RemainTLV(NdefMap, &NDEFFlag, &TempLength);
5673        }
5674    }
5675    return Result;
5676}
5677
5678static NFCSTATUS phFriNfc_MifStd_H_WrTermTLV(phFriNfc_NdefMap_t   *NdefMap)
5679{
5680    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
5681    uint8_t     index = PH_FRINFC_MIFARESTD_VAL0;
5682
5683    /* Change the state to check ndef compliancy */
5684    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_TERM_TLV;
5685
5686    NdefMap->SendRecvBuf[index] =
5687                        NdefMap->StdMifareContainer.currentBlock;
5688    index++;
5689    NdefMap->SendRecvBuf[index] = PH_FRINFC_MIFARESTD_TERMTLV_T;
5690    index++;
5691
5692    while(index < PH_FRINFC_MIFARESTD_WR_A_BLK)
5693    {
5694        NdefMap->SendRecvBuf[index] = PH_FRINFC_MIFARESTD_NULLTLV_T;
5695        index++;
5696    }
5697
5698    NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
5699
5700    Result = phFriNfc_MifStd_H_WrTLV(NdefMap);
5701
5702    return Result;
5703}
5704
5705
5706static NFCSTATUS phFriNfc_MifStd_H_ProWrABlock(phFriNfc_NdefMap_t   *NdefMap)
5707{
5708    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
5709
5710    NdefMap->StdMifareContainer.WrLength = PH_FRINFC_MIFARESTD_VAL0;
5711    if(NdefMap->ApduBuffIndex <
5712        (uint16_t)NdefMap->ApduBufferSize)
5713    {
5714        /* Remaining bytes to write < 16 */
5715        if(NdefMap->StdMifareContainer.RemainingBufFlag ==
5716            PH_FRINFC_MIFARESTD_FLAG1)
5717        {
5718            /* Write complete, so next byte shall be */
5719            NdefMap->StdMifareContainer.internalLength =
5720                *NdefMap->DataCount;
5721
5722            /* Copy bytes less than 16 to internal buffer
5723                for the next write this can be used */
5724            (void)memcpy( NdefMap->StdMifareContainer.internalBuf,
5725                    NdefMap->StdMifareContainer.Buffer,
5726                    NdefMap->StdMifareContainer.internalLength);
5727
5728            /* Increment the Send Buffer index */
5729            NdefMap->ApduBuffIndex += NdefMap->NumOfBytesWritten;
5730
5731            NdefMap->StdMifareContainer.remainingSize -=
5732                    NdefMap->NumOfBytesWritten;
5733
5734            NdefMap->StdMifareContainer.RemainingBufFlag = PH_FRINFC_MIFARESTD_VAL0;
5735            /* Check for the End of Card */
5736                NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
5737                    (uint8_t)((NdefMap->StdMifareContainer.remainingSize ==
5738                    PH_FRINFC_MIFARESTD_VAL0)?
5739                    PH_FRINFC_MIFARESTD_FLAG1:
5740                    PH_FRINFC_MIFARESTD_FLAG0);
5741
5742            NdefMap->TLVStruct.SetTermTLVFlag =
5743                    (uint8_t)(((NdefMap->StdMifareContainer.remainingSize ==
5744                            PH_FRINFC_MIFARESTD_VAL0) ||
5745                            (NdefMap->TLVStruct.SetTermTLVFlag ==
5746                            PH_FRINFC_MIFARESTD_FLAG1))?
5747                            PH_FRINFC_MIFARESTD_FLAG1:
5748                            PH_FRINFC_MIFARESTD_FLAG0);
5749
5750        } /* internal Buffer > Send Buffer */
5751        else if(NdefMap->StdMifareContainer.internalBufFlag ==
5752                PH_FRINFC_MIFARESTD_FLAG1)
5753        {
5754            (void)memcpy(NdefMap->StdMifareContainer.internalBuf,
5755                    NdefMap->StdMifareContainer.Buffer,
5756                    *NdefMap->DataCount);
5757
5758            NdefMap->StdMifareContainer.internalLength =
5759                                    *NdefMap->DataCount;
5760
5761            /* Increment the Send Buffer index */
5762            NdefMap->ApduBuffIndex +=
5763                    NdefMap->NumOfBytesWritten;
5764
5765            NdefMap->StdMifareContainer.remainingSize -=
5766                    NdefMap->NumOfBytesWritten;
5767
5768            NdefMap->StdMifareContainer.internalBufFlag =
5769                                PH_FRINFC_MIFARESTD_FLAG0;
5770            /* Check for the End of Card */
5771            NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
5772                (uint8_t)(((NdefMap->StdMifareContainer.remainingSize ==
5773                    PH_FRINFC_MIFARESTD_VAL0) &&
5774                    (NdefMap->StdMifareContainer.internalLength ==
5775                    PH_FRINFC_MIFARESTD_VAL0))?
5776                    PH_FRINFC_MIFARESTD_FLAG1:
5777                    PH_FRINFC_MIFARESTD_FLAG0);
5778
5779            NdefMap->TLVStruct.SetTermTLVFlag =
5780                    (uint8_t)(((NdefMap->StdMifareContainer.remainingSize ==
5781                            PH_FRINFC_MIFARESTD_VAL0) ||
5782                            (NdefMap->TLVStruct.SetTermTLVFlag ==
5783                            PH_FRINFC_MIFARESTD_FLAG1))?
5784                            PH_FRINFC_MIFARESTD_FLAG1:
5785                            PH_FRINFC_MIFARESTD_FLAG0);
5786        }
5787        else
5788        {
5789            NdefMap->StdMifareContainer.internalLength = 0;
5790            /* Increment the Send Buffer index */
5791            NdefMap->ApduBuffIndex +=
5792                    NdefMap->NumOfBytesWritten;
5793            NdefMap->StdMifareContainer.remainingSize -=
5794                    NdefMap->NumOfBytesWritten;
5795
5796            /* Check for the End of Card */
5797            if((NdefMap->StdMifareContainer.remainingSize ==
5798                    PH_FRINFC_MIFARESTD_VAL0) ||
5799                    (NdefMap->ApduBuffIndex == NdefMap->ApduBufferSize))
5800            {
5801                NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
5802                (uint8_t)((NdefMap->StdMifareContainer.remainingSize == 0)?
5803                    PH_FRINFC_MIFARESTD_FLAG1:PH_FRINFC_MIFARESTD_FLAG0);
5804
5805                if(NdefMap->StdMifareContainer.internalLength ==
5806                    PH_FRINFC_MIFARESTD_VAL0)
5807                {
5808                    NdefMap->StdMifareContainer.currentBlock++;
5809                    /* Mifare 4k Card, After 128th Block
5810                    each sector = 16 blocks in Mifare 4k */
5811                    Result = ((NdefMap->StdMifareContainer.remainingSize == 0)?
5812                                Result:
5813                                phFriNfc_MifStd_H_BlkChk(NdefMap));
5814                    NdefMap->StdMifareContainer.NdefBlocks++;
5815                }
5816                NdefMap->TLVStruct.SetTermTLVFlag =
5817                    (uint8_t)(((NdefMap->StdMifareContainer.remainingSize ==
5818                            PH_FRINFC_MIFARESTD_VAL0) ||
5819                            (NdefMap->TLVStruct.SetTermTLVFlag ==
5820                            PH_FRINFC_MIFARESTD_FLAG1))?
5821                            PH_FRINFC_MIFARESTD_FLAG1:
5822                            PH_FRINFC_MIFARESTD_FLAG0);
5823            }
5824            else
5825            {
5826                NdefMap->StdMifareContainer.currentBlock++;
5827                NdefMap->StdMifareContainer.WrLength =
5828                    (uint16_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
5829                /* Mifare 4k Card, After 128th Block
5830                each sector = 16 blocks in Mifare 4k */
5831                Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
5832                if(Result == NFCSTATUS_SUCCESS)
5833                {
5834                    NdefMap->StdMifareContainer.NdefBlocks++;
5835                    Result = ((NdefMap->StdMifareContainer.AuthDone ==
5836                                PH_FRINFC_MIFARESTD_FLAG1)?
5837                                phFriNfc_MifStd_H_WrABlock(NdefMap):
5838                                phFriNfc_MifStd_H_AuthSector(NdefMap));
5839                }
5840            }
5841        }
5842    }
5843    else
5844    {
5845        Result = PHNFCSTVAL(    CID_FRI_NFC_NDEF_MAP,
5846                                NFCSTATUS_INVALID_DEVICE_REQUEST);
5847    }
5848
5849    if((Result == NFCSTATUS_SUCCESS) &&
5850        (NdefMap->TLVStruct.SetTermTLVFlag !=
5851        PH_FRINFC_MIFARESTD_FLAG1) &&
5852        (NdefMap->StdMifareContainer.remainingSize >
5853        PH_FRINFC_MIFARESTD_VAL0))
5854    {
5855        Result = phFriNfc_MifStd_H_WrTermTLV(NdefMap);
5856    }
5857    else
5858    {
5859        if((Result == NFCSTATUS_SUCCESS) &&
5860            (NdefMap->TLVStruct.SetTermTLVFlag ==
5861            PH_FRINFC_MIFARESTD_FLAG1))
5862        {
5863            /* Write the length to the L field in the TLV */
5864            NdefMap->StdMifareContainer.TempBlockNo =
5865                        NdefMap->StdMifareContainer.currentBlock;
5866            phFriNfc_MifStd_H_SetNdefBlkAuth(NdefMap);
5867            NdefMap->StdMifareContainer.currentBlock =
5868                            NdefMap->TLVStruct.NdefTLVBlock;
5869            Result = phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap);
5870        }
5871    }
5872    return Result;
5873}
5874
5875static NFCSTATUS phFriNfc_MifStd_H_CallDisCon(phFriNfc_NdefMap_t   *NdefMap)
5876{
5877    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
5878    /*Set Ndef State*/
5879    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_DISCONNECT;
5880    NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process;
5881    NdefMap->MapCompletionInfo.Context = NdefMap;
5882
5883#ifndef PH_HAL4_ENABLE
5884    /*Call the Overlapped HAL POLL function */
5885    Result =  phFriNfc_OvrHal_Disconnect( NdefMap->LowerDevice,
5886                                          &NdefMap->MapCompletionInfo,
5887                                          NdefMap->psRemoteDevInfo);
5888#else
5889    /*Call the Overlapped HAL Reconnect function */
5890    Result =  phFriNfc_OvrHal_Reconnect( NdefMap->LowerDevice,
5891                                          &NdefMap->MapCompletionInfo,
5892                                          NdefMap->psRemoteDevInfo);
5893#endif
5894
5895    return Result;
5896}
5897
5898#ifndef PH_HAL4_ENABLE
5899static NFCSTATUS phFriNfc_MifStd_H_CallPoll(phFriNfc_NdefMap_t   *NdefMap)
5900{
5901    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
5902    /*Set Ndef State*/
5903    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_POLL;
5904    /* Opmodes */
5905    NdefMap->OpModeType[PH_FRINFC_MIFARESTD_VAL0] = phHal_eOpModesMifare;
5906    NdefMap->OpModeType[PH_FRINFC_MIFARESTD_VAL1] = phHal_eOpModesArrayTerminator;
5907
5908    /* Number of devices to poll */
5909    NdefMap->NoOfDevices = PH_FRINFC_MIFARESTD_VAL1;
5910
5911    /*Call the Overlapped HAL POLL function */
5912    Result =  phFriNfc_OvrHal_Poll( NdefMap->LowerDevice,
5913                                    &NdefMap->MapCompletionInfo,
5914                                    NdefMap->OpModeType,
5915                                    NdefMap->psRemoteDevInfo,
5916                                    &NdefMap->NoOfDevices,
5917                                    NdefMap->StdMifareContainer.DevInputParam);
5918    return Result;
5919}
5920#endif
5921
5922static NFCSTATUS phFriNfc_MifStd_H_CallConnect(phFriNfc_NdefMap_t   *NdefMap)
5923{
5924    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
5925    /*Set Ndef State*/
5926    NdefMap->State = PH_FRINFC_NDEFMAP_STATE_CONNECT;
5927
5928    /*Call the Overlapped HAL POLL function */
5929    Result =  phFriNfc_OvrHal_Connect(  NdefMap->LowerDevice,
5930                                        &NdefMap->MapCompletionInfo,
5931                                        NdefMap->psRemoteDevInfo,
5932                                        NdefMap->StdMifareContainer.DevInputParam);
5933    return Result;
5934}
5935
5936static void phFriNfc_MifStd1k_H_BlkChk(phFriNfc_NdefMap_t   *NdefMap,
5937                                    uint8_t              SectorID,
5938                                    uint8_t              *callbreak)
5939{
5940    /* every last block of a sector needs to be skipped */
5941    if(((NdefMap->StdMifareContainer.currentBlock + PH_FRINFC_MIFARESTD_INC_1) %
5942        PH_FRINFC_MIFARESTD_BLK4) == 0)
5943    {
5944        NdefMap->StdMifareContainer.currentBlock++;
5945    }
5946    else
5947    {
5948        if(NdefMap->StdMifareContainer.aid[SectorID] ==
5949            PH_FRINFC_MIFARESTD_NDEF_COMP)
5950        {
5951            /* Check whether the block is first block of a (next)new sector and
5952            also check if it is first block then internal length is zero
5953            or not. Because once Authentication is done for the sector again
5954            we should not authenticate it again */
5955            if((NdefMap->StdMifareContainer.currentBlock ==
5956                (SectorID * PH_FRINFC_MIFARESTD_BLK4)) &&
5957                (NdefMap->StdMifareContainer.internalLength == 0))
5958            {
5959                NdefMap->StdMifareContainer.AuthDone = 0;
5960            }
5961            *callbreak = 1;
5962        }
5963        else
5964        {
5965            NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4;
5966        }
5967    }
5968}
5969
5970#ifdef UNIT_TEST
5971#include <phUnitTestNfc_MifareStd_static.c>
5972#endif
5973
5974#endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
5975