summaryrefslogtreecommitdiffstats
path: root/src/phFriNfc_NdefMap.c
blob: 7e195f20304cd2e1f2cc643fac51870a559d0a2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
/*
 * Copyright (C) 2010 NXP Semiconductors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*!
* \file  phFriNfcNdefMap.c
* \brief NFC Ndef Mapping For Different Smart Cards.
*
* Project: NFC-FRI
*
* $Date: Mon Dec 13 14:14:12 2010 $
* $Author: ing02260 $
* $Revision: 1.39 $
* $Aliases:  $
*
*/


#include <phFriNfc_NdefMap.h>

#ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
#include <phFriNfc_MifareULMap.h>
#endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/

#ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
#include <phFriNfc_TopazMap.h>
#endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */

#ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
#include <phFriNfc_MifareStdMap.h>
#endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */

#ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
#include <phFriNfc_DesfireMap.h>
#endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */

#ifndef PH_FRINFC_MAP_FELICA_DISABLED
#include <phFriNfc_FelicaMap.h>
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED */

#ifndef PH_FRINFC_MAP_ISO15693_DISABLED
#include <phFriNfc_ISO15693Map.h>
#endif  /* PH_FRINFC_MAP_ISO15693_DISABLED */

#ifdef PHFRINFC_OVRHAL_MOCKUP
#include <phFriNfc_MockupMap.h>
#endif  /* PHFRINFC_OVRHAL_MOCKUP */


#include <phFriNfc_OvrHal.h>

/*! \ingroup grp_file_attributes
*  \name NDEF Mapping
*
* File: \ref phFriNfcNdefMap.c
*
*/
/*@{*/
#define PHFRINFCNDEFMAP_FILEREVISION "$Revision: 1.39 $"
#define PHFRINFCNDEFMAP_FILEALIASES  "$Aliases:  $"
/*@}*/

#ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
/* Desfire capability Container Reset Helper */
static void phFriNfc_DesfCapCont_HReset(phFriNfc_NdefMap_t  *NdefMap);
#endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */

#ifndef PH_FRINFC_MAP_FELICA_DISABLED
/* Felica Smart Tag Reset Helper */
static void phFriNfc_Felica_HReset(phFriNfc_NdefMap_t  *NdefMap);
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED */


/* \note    This function has to be called at the beginning, after creating an
*          instance of \ref phFriNfc_NdefMap_t . Use this function to reset
*          the instance and/or switch to a different underlying device (
*          different NFC device or device mode, or different Remote Device).
*/

NFCSTATUS phFriNfc_NdefMap_Reset(   phFriNfc_NdefMap_t              *NdefMap,
                                 void                            *LowerDevice,
                                 phHal_sRemoteDevInformation_t   *psRemoteDevInfo,
                                 phHal_sDevInputParam_t          *psDevInputParam,
                                 uint8_t                         *TrxBuffer,
                                 uint16_t                        TrxBufferSize,
                                 uint8_t                         *ReceiveBuffer,
                                 uint16_t                        *ReceiveLength,
                                 uint16_t                        *DataCount)
{
    NFCSTATUS   status = NFCSTATUS_SUCCESS;
    uint8_t     index;

    if (    (ReceiveLength == NULL) || (NdefMap == NULL) || (psRemoteDevInfo == NULL) ||
        (TrxBuffer == NULL) || (TrxBufferSize == 0)  || (LowerDevice == NULL) ||
        (*ReceiveLength == 0) || (ReceiveBuffer == NULL) || (DataCount == NULL) ||
        (psDevInputParam == NULL) ||
        (*ReceiveLength < PH_FRINFC_NDEFMAP_MAX_SEND_RECV_BUF_SIZE ))
    {
        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        /* Initialise the state to Init */
        NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RESET_INIT;

        for(index = 0;index<PH_FRINFC_NDEFMAP_CR;index++)
        {
            /* Initialise the NdefMap Completion Routine to Null */
            NdefMap->CompletionRoutine[index].CompletionRoutine = NULL;
            /* Initialise the NdefMap Completion Routine context to Null  */
            NdefMap->CompletionRoutine[index].Context = NULL;
        }

        /* Lower Device(Always Overlapped HAL Struct initialised in application
        is registred in NdefMap Lower Device) */
        NdefMap->LowerDevice = LowerDevice;

        /* Remote Device info received from Manual Device Discovery is registered here */
        NdefMap->psRemoteDevInfo = psRemoteDevInfo;

        /* Transfer Buffer registered */
        NdefMap->ApduBuffer = TrxBuffer;

        /* Set the MaxApduBufferSize */
        NdefMap->ApduBufferSize = TrxBufferSize;

        /* Set APDU Buffer Index */
        NdefMap->ApduBuffIndex = 0;

        /* Register Transfer Buffer Length */
        NdefMap->SendLength = 0;

        /* Register Receive Buffer */
        NdefMap->SendRecvBuf = ReceiveBuffer;

        /* Register Receive Buffer Length */
        NdefMap->SendRecvLength = ReceiveLength;

        /* Register Temporary Receive Buffer Length */
        NdefMap->TempReceiveLength = *ReceiveLength;

        /* Register Data Count variable and set it to zero */
        NdefMap->DataCount = DataCount;
        *NdefMap->DataCount = 0;

        /* Reset the PageOffset */
        NdefMap->Offset = 0;

        /* Reset the NumOfBytesRead*/
        NdefMap->NumOfBytesRead = 0;

        /* Reset the NumOfBytesWritten*/
        NdefMap->NumOfBytesWritten = 0;

        /* Reset the Card Type */
        NdefMap->CardType = 0;

        /* Reset the Memory Card Size*/
        NdefMap->CardMemSize = 0;

        /* Reset the Previous Operation*/
        NdefMap->PrevOperation = 0;

        /* Reset the Desfire Operation Flag*/
        NdefMap->DespOpFlag = 0;

        /* Reset MapCompletion Info*/
        NdefMap->MapCompletionInfo.CompletionRoutine = NULL;
        NdefMap->MapCompletionInfo.Context = NULL;

        /*  Reset the ReadingForWriteOperation flag. */
        NdefMap->ReadingForWriteOperation = 0;  /*  FALSE  */

#ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
        /*Reset Desfire Cap Container elements*/
        phFriNfc_DesfCapCont_HReset(NdefMap);
#endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */

#ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
        /*Reset Mifare Standard Container elements*/
        NdefMap->StdMifareContainer.DevInputParam = psDevInputParam;
        status = phFriNfc_MifareStdMap_H_Reset(NdefMap);
#endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */

#ifndef PH_FRINFC_MAP_FELICA_DISABLED
        /*Reset Felica Tag elements*/
        NdefMap->FelicaPollDetails.DevInputParam = psDevInputParam;
        phFriNfc_Felica_HReset(NdefMap);
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED */

#if !(defined(PH_FRINFC_MAP_TOPAZ_DISABLED ) || defined (PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED ))

        phFriNfc_TopazMap_H_Reset(NdefMap);
#endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED || PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED  */


#ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
        status = phFriNfc_MifareUL_H_Reset(NdefMap);
#endif

#ifdef PHFRINFC_OVRHAL_MOCKUP
        /*Reset Desfire Cap Container elements*/
        phFriNfc_Mockup_H_Reset(NdefMap);
#endif  /* PHFRINFC_OVRHAL_MOCKUP */

        /*
        *  Fix for PR - 0001256
        *  Date- 08-08-08
        */
        NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID;
    }

    return (status);
}

/*!
* Registering the Completion Routine.
*
* This function requires the caller to set a Completion Routine
* which serves as notifier for the upper component.
* NOTE: Please refer the header file for more information.
*
*/

NFCSTATUS phFriNfc_NdefMap_SetCompletionRoutine(phFriNfc_NdefMap_t     *NdefMap,
                                                uint8_t                 FunctionID,
                                                pphFriNfc_Cr_t          CompletionRoutine,
                                                void                   *CompletionRoutineContext)
{
    NFCSTATUS   status = NFCSTATUS_SUCCESS;

    if ( ( NdefMap == NULL ) || (FunctionID >= PH_FRINFC_NDEFMAP_CR) ||
        ( CompletionRoutine == NULL) || (CompletionRoutineContext == NULL))
    {
        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        /* Register the application callback with the NdefMap Completion Routine */
        NdefMap->CompletionRoutine[FunctionID].CompletionRoutine = CompletionRoutine;

        /* Register the application context with the NdefMap Completion Routine context */
        NdefMap->CompletionRoutine[FunctionID].Context = CompletionRoutineContext;
    }

    return status;
}

/*!
* Initiates Reading of NDEF information from the Remote Device.
*
* Remote Peer device may be of type any card. Ex: desfire,felica,jewel
* mifareUL,mifare 1K etc. The function initiates the reading of NDEF
* information from a Remote Device.
*
* This is the main NdefMap read function call.Internally,depending upon
* the CardType,respective mifare/desfire read functions are called.
* In future this can be extended to support any types of card.
*
* It performs a reset of the state and triggers/starts the raed action (state machine).
* A periodic call of the \ref phFriNfcNdefMap_Process has to be done once the action
* has been triggered.
*
* NOTE: Please refer the header file for more information.
*
*/

NFCSTATUS phFriNfc_NdefMap_RdNdef(  phFriNfc_NdefMap_t  *NdefMap,
                                  uint8_t             *PacketData,
                                  uint32_t            *PacketDataLength,
                                  uint8_t             Offset)
{
    NFCSTATUS   status = NFCSTATUS_PENDING;


    /* check for validity of input parameters*/
    if (( PacketData == NULL )
        || ( NdefMap == NULL )
        || ( PacketDataLength == NULL )
        || ( *PacketDataLength == 0 )
        || ( ( Offset != PH_FRINFC_NDEFMAP_SEEK_CUR) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ))
        || (NdefMap->CompletionRoutine->CompletionRoutine == NULL)
        || (NdefMap->CompletionRoutine->Context == NULL )
        )
    {
        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    }
    else if (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID)
    {
        /*  Card is in invalid state, cannot have any read/write
        operations*/
        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
            NFCSTATUS_INVALID_FORMAT);
    }
    else if(NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED)
    {
        /*  Can't read any data from the card:TLV length is zero*/
        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_EOF_NDEF_CONTAINER_REACHED);

        NdefMap->NumOfBytesRead = PacketDataLength;
        *NdefMap->NumOfBytesRead = 0;


    }
    else if ( (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ))
    {
        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
    }
    else
    {
        /*  Check the offset given by the user
        If the offset is 1 (SEEK_BEGIN), reset everything and start
        reading from the first Page of the card.
        else if offset is 0 (PH_FRINFC_NDEFMAP_SEEK_CUR), continue reading
        No need to reset the parameters.  */

        if ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN )
        {
            NdefMap->ApduBuffIndex = 0;
            *NdefMap->DataCount = 0;
        }

        if  ( (NdefMap->CardType == PH_FRINFC_NDEFMAP_ISO14443_4A_CARD) &&
            (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && (*NdefMap->DataCount == 0 ))
        {

            /*  A READ operation cannot be done if the previuos operation was WRITE
            unless the offset is set to PH_FRINFC_NDEFMAP_SEEK_BEGIN Or
            Read Operation with Offset set to Continue & DataCount set to 0 */
            status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
        }
        else
        {
            switch ( NdefMap->CardType)
            {
#ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
                case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
                    /*  Mifare card selected. Call Mifare read */
                    status = phFriNfc_MifareUL_RdNdef ( NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */

#ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
                case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
#ifdef DESFIRE_EV1
                case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1 :
#endif /* #ifdef DESFIRE_EV1 */
                    /*  Desfire card selected. Call Desfire read */
                    status = phFriNfc_Desfire_RdNdef(   NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */

#ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
                case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
                case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
                    /*  Mifare card selected. Call Mifare read */
                    status = phFriNfc_MifareStdMap_RdNdef ( NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */

#ifndef PH_FRINFC_MAP_FELICA_DISABLED
                case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
                    /*  Desfire card selected. Call Desfire Write */
                    status =  phFriNfc_Felica_RdNdef(  NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED */

#ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
                case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
                    /*  Topaz card selected. Call Topaz read */
                    status =  phFriNfc_TopazMap_RdNdef(  NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
                case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
                    /*  Topaz card selected. Call Topaz read */
                    status =  phFriNfc_TopazDynamicMap_RdNdef(  NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
#endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */

#ifndef PH_FRINFC_MAP_ISO15693_DISABLED
                case PH_FRINFC_NDEFMAP_ISO15693_CARD:
                    status =  phFriNfc_ISO15693_RdNdef(  NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif /* #ifndef PH_FRINFC_MAP_ISO15693_DISABLED */

#ifdef PHFRINFC_OVRHAL_MOCKUP
                case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
                    /*  Mockup card selected. Call Mockup Write */
                    status =  phFriNfc_Mockup_RdNdef(  NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif  /* PHFRINFC_OVRHAL_MOCKUP */

                default :
                    /*  Unknown card type. Return error */
                    status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
                        NFCSTATUS_INVALID_REMOTE_DEVICE);

                    break;
            }
        }
    }
    return status;
}

/*!
* Initiates Writing of NDEF information to the Remote Device.
*
* The function initiates the writing of NDEF information to a Remote Device
*
* Remote Peer device may be of type any card. Ex: desfire,felica,jewel
* mifareUL,mifare 1K etc. The function initiates the reading of NDEF
* information from a Remote Device.
*
* This is a main write api.Internally,depending upon the CardType,
* respective mifare/desfire write apis are called.In future this can be
* extended to support any types of card.
*
* It performs a reset of the state and starts the action (state machine).
* A periodic call of the \ref phFriNfcNdefMap_Process has to be done once
* the action has been triggered.
*
* NOTE: Please refer the header file for more information.
*
*/


NFCSTATUS phFriNfc_NdefMap_WrNdef(  phFriNfc_NdefMap_t  *NdefMap,
                                  uint8_t             *PacketData,
                                  uint32_t            *PacketDataLength,
                                  uint8_t             Offset)
{
    NFCSTATUS   status = NFCSTATUS_PENDING;
    uint8_t     StatusChk=0;

     if (     (PacketData          == NULL)
        ||  ( NdefMap             == NULL )
        /* + Mantis 442 */
        || ( PacketDataLength    == NULL )
        /* - Mantis 442 */
        || ( *PacketDataLength   == 0 )
        || ((Offset != PH_FRINFC_NDEFMAP_SEEK_CUR) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ))
        || (NdefMap->CompletionRoutine->CompletionRoutine == NULL)
        || (NdefMap->CompletionRoutine->Context == NULL)
        )
    {
        /*  Invalid input parameter error   */
        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
    }
    else if (( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) &&
        (PH_FRINFC_NDEFMAP_TOPAZ_CARD != NdefMap->CardType) &&
        (PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD != NdefMap->CardType))
    {
        /*  Card is in invalid state, cannot have any read/write
        operations*/
        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
            NFCSTATUS_INVALID_FORMAT);
    }

    else if ( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY )

    {
        /*Can't write to the card :No Grants */
        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
            NFCSTATUS_NOT_ALLOWED);

        /* set the no. bytes written is zero*/
        NdefMap->WrNdefPacketLength = PacketDataLength;
        *NdefMap->WrNdefPacketLength = 0;

    }
    else
    {
        /*  Check the offset given by the user
        If the offset is 1 (SEEK_BEGIN), reset everything and start
        writing from the first Byte of the card.
        else if offset is 0 (PH_FRINFC_NDEFMAP_SEEK_CUR), continue writing
        No need to reset the parameters.  */
         if (( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) &&
            (PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD == NdefMap->CardType))
        {
            /* If Topaz Dynamic card CC bytes are not valid then also allow writing,
            If card is really good then writing will be done properly and reading can be performed,
            otherwise writing or reading will fail. so, setting card state to
            NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_WRITE */
            NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_WRITE;
        }

        if ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN )
        {
            NdefMap->ApduBuffIndex = 0;
            *NdefMap->DataCount = 0;
        }

        if ( (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_UL_CARD) ||
            (NdefMap->CardType == PH_FRINFC_NDEFMAP_ISO14443_4A_CARD))
        {
            if (( (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_READ_OPE) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN )) ||
                ( (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && (*NdefMap->DataCount == 0 )))
            {
                /*  A WRITE operation cannot be done if the previuos operation was READ
                unless the offset is set to PH_FRINFC_NDEFMAP_SEEK_BEGIN OR
                Write Operation with Offset set to Continue & DataCount set to 0  */
                status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
                StatusChk = 1;
            }
        }
        if(StatusChk != 1)
        {
            NdefMap->WrNdefPacketLength =   PacketDataLength;
            switch ( NdefMap->CardType)
            {
#ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
                case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
                    /*  Mifare card selected. Call Mifare Write */
                    status =  phFriNfc_MifareUL_WrNdef( NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */

#ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
                case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
#ifdef DESFIRE_EV1
                case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1 :
#endif /* #ifdef DESFIRE_EV1 */
                    /*  Desfire card selected. Call Desfire Write */
                    status =  phFriNfc_Desfire_WrNdef(  NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */

#ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
                case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
                case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
                    /*  Mifare card selected. Call Mifare read */
                    status = phFriNfc_MifareStdMap_WrNdef ( NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */

#ifndef PH_FRINFC_MAP_FELICA_DISABLED
                case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
                    /*  Desfire card selected. Call Desfire Write */
                    status =  phFriNfc_Felica_WrNdef(  NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED */

#ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
                case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
                    /*  Topaz card selected. Call Topaz Write */
                    status =  phFriNfc_TopazMap_WrNdef(  NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
                case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
                    /*  Topaz card selected. Call Topaz Write */
                    status =  phFriNfc_TopazDynamicMap_WrNdef(  NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
#endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */

#ifndef PH_FRINFC_MAP_ISO15693_DISABLED
                case PH_FRINFC_NDEFMAP_ISO15693_CARD:
                    status =  phFriNfc_ISO15693_WrNdef(  NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif /* #ifndef PH_FRINFC_MAP_ISO15693_DISABLED */


#ifdef PHFRINFC_OVRHAL_MOCKUP
                case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
                    /*  Mockup card selected. Call Mockup Write */
                    status =  phFriNfc_Mockup_WrNdef(  NdefMap,
                        PacketData,
                        PacketDataLength,
                        Offset);
                    break;
#endif  /* PHFRINFC_OVRHAL_MOCKUP */
                default :
                    /*  Unknown card type. Return error */
                    status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                        NFCSTATUS_INVALID_REMOTE_DEVICE);
                    break;
            }
        }
    }
    return status;
}

#ifdef FRINFC_READONLY_NDEF

NFCSTATUS
phFriNfc_NdefMap_ConvertToReadOnly (
    phFriNfc_NdefMap_t          *NdefMap)
{
    NFCSTATUS   result = NFCSTATUS_PENDING;


    /*  Check for ndefmap context and relevant state. Else return error*/
    if (NULL == NdefMap)
    {
        result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    }
    else if ((NdefMap->CompletionRoutine->CompletionRoutine == NULL) 
        || (NdefMap->CompletionRoutine->Context == NULL))
    {
        result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        switch (NdefMap->CardType)
        {
            case PH_FRINFC_NDEFMAP_TOPAZ_CARD:
            {
                result = phFriNfc_TopazMap_ConvertToReadOnly (NdefMap);
                break;
            }

            case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD:
            {
                result = phFriNfc_TopazDynamicMap_ConvertToReadOnly (NdefMap);
                break;
            }

            case PH_FRINFC_NDEFMAP_ISO15693_CARD:
            {
                result = phFriNfc_ISO15693_ConvertToReadOnly (NdefMap);
                break;
            }

            default:
            {
                result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                                    NFCSTATUS_INVALID_REMOTE_DEVICE);
                break;
            }
        }
    }
    return result;
}

#endif /* #ifdef FRINFC_READONLY_NDEF */

/*!
* Check whether a particular Remote Device is NDEF compliant.
*
* The function initiates the ndef compliancy check.
*
* This is a main check ndef api.Internally,depending upon the different
* opmodes,respective mifare/desfire checkNdef apis are called.
* In future this can be extended to check any types of card ndef
* compliancy.
*
* It performs a reset of the state and starts the action (state machine).
* A periodic call of the \ref phFriNfcNdefMap_Process has to be done once
* the action has been triggered.
*
* NOTE: Please refer the header file for more information.
*
*/

NFCSTATUS phFriNfc_NdefMap_ChkNdef( phFriNfc_NdefMap_t     *NdefMap)
{
    NFCSTATUS   status = NFCSTATUS_PENDING;
    uint8_t     sak;


    /*  Check for ndefmap context and relevant state. Else return error*/
    if ( NdefMap == NULL )
    {
        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        if( (NdefMap->State !=  PH_FRINFC_NDEFMAP_STATE_RESET_INIT) ||
            (NdefMap->psRemoteDevInfo->SessionOpened != 0x01 ) )
            /*  Harsha: If SessionOpened is not 1, this means that connect has not happened */
        {
            status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
        }
        else if ( (NdefMap->CompletionRoutine->CompletionRoutine == NULL) || (NdefMap->CompletionRoutine->Context == NULL ))
        {
            status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
        }
        else
        {
            /*
            * 1.Changed
            *   CardInfo106 Replace with the ReaderA_Info.
            */

            sak = NdefMap->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;

            /*
            * 3. Changed
            *    Description: Opmode replace with RemDevType.
            */


            switch ( NdefMap->psRemoteDevInfo->RemDevType )
            {
#ifndef PH_FRINFC_MAP_ISO15693_DISABLED
            case phHal_eISO15693_PICC:
            {
                status = phFriNfc_ISO15693_ChkNdef (NdefMap);
                break;
            }
#else /* #ifndef PH_FRINFC_MAP_ISO15693_DISABLED */
            status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 
                                NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif /* #ifndef PH_FRINFC_MAP_ISO15693_DISABLED */

            case phHal_eMifare_PICC:
            case phHal_eISO14443_3A_PICC:
                /*  Remote device is Mifare card . Check for Mifare
                NDEF compliancy */
                if(0x00 == sak)
                {
                    /*  The SAK/Sel_Res says the card is of the type
                    Mifare UL */
#ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
                    status = phFriNfc_MifareUL_ChkNdef( NdefMap);
#else   /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
                    status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                        NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
                }
                else if ((0x08 == (sak & 0x18)) ||
                        (0x18 == (sak & 0x18)) ||
                        (0x01 == sak))
                {
                    /*  The SAK/Sel_Res says the card is of the type
                    Mifare Standard */
#ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
                    status = phFriNfc_MifareStdMap_ChkNdef( NdefMap);
#else   /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
                    status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                        NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
                }
                else
                {
                    /*  Invalid Mifare UL card, as the remote device
                    info - opmode says its a Mifare UL card but,
                    The SAK/Sel_Res is wrong */
                    status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                        NFCSTATUS_INVALID_REMOTE_DEVICE);
                }
                break;
            case phHal_eISO14443_B_PICC:
                {
                    status = phFriNfc_Desfire_ChkNdef(NdefMap);
                }
                break;
            case  phHal_eISO14443_A_PICC :
                /*  Remote device is Desfire card . Check for Desfire
                NDEF compliancy */
                if(0x20 == (sak & 0x20))
                {
                    /*  The SAK/Sel_Res says the card is of the type
                    ISO14443_4A */
#ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
                    status = phFriNfc_Desfire_ChkNdef(NdefMap);
#else   /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
                    status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                        NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
                }
                else
                {
                    /*  Invalid Desfire card, as the remote device
                    info - opmode says its a desfire card but,
                    The SAK/Sel_Res is wrong */
                    status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                        NFCSTATUS_INVALID_REMOTE_DEVICE);
                }
                break;

            case phHal_eFelica_PICC:

                /*Set the OpMode Type Flag*/
#ifndef PH_FRINFC_MAP_FELICA_DISABLED
#ifndef PH_HAL4_ENABLE
                NdefMap->OpModeType[0] = phHal_eOpModesFelica212;
                NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
#endif /* #ifndef PH_HAL4_ENABLE */
                status = phFriNfc_Felica_ChkNdef(NdefMap);
#else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
                status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                    NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/

                break;

#ifndef PH_HAL4_ENABLE
#ifndef PH_FRINFC_MAP_FELICA_DISABLED
            case phHal_eFelica424:
                /*Set the OpMode Ttype Flag*/
                NdefMap->OpModeType[0] = phHal_eOpModesFelica424;
                NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
                status = phFriNfc_Felica_ChkNdef(NdefMap);
#else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
                status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                    NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/
                break;
#endif

            case phHal_eJewel_PICC :
                /*  Remote device is Topaz card . Check for Topaz
                NDEF compliancy */
#ifdef PH_HAL4_ENABLE
#ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
            /* Decide on the Header bytes to know the
                   Type of the Topaz card.Card could be Static or
                   Dynamic type. These are of type NFFC-NDEF Data Application*/
                if ( NdefMap->psRemoteDevInfo->RemoteDevInfo.Jewel_Info.HeaderRom0
                                == PH_FRINFC_TOPAZ_HEADROM0_VAL)
                {

                        status = phFriNfc_TopazMap_ChkNdef(NdefMap);
                }
#ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
                else if( NdefMap->psRemoteDevInfo->RemoteDevInfo.Jewel_Info.HeaderRom0
                                == PH_FRINFC_TOPAZ_DYNAMIC_HEADROM0_VAL)
                {

                    status = phFriNfc_TopazDynamicMap_ChkNdef(NdefMap);
                }
#endif
                else
                {

                    status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                           NFCSTATUS_INVALID_REMOTE_DEVICE);

                }
#endif


#else
                if(0xC2 == sak)
                {
                    /*  The SAK/Sel_Res says the card is of the type
                    ISO14443_4A */
#ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
                    status = phFriNfc_TopazMap_ChkNdef(NdefMap);
#else   /* PH_FRINFC_MAP_TOPAZ_DISABLED*/
                    status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                        NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED*/
                }
                else
                {
                    /*  Invalid Topaz card, as the remote device
                    info - opmode says its a desfire card but,
                    The SAK/Sel_Res is wrong */
                    status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                        NFCSTATUS_INVALID_REMOTE_DEVICE);
                }
#endif
                break;

#ifdef PHFRINFC_OVRHAL_MOCKUP
            case phHal_eOpModesMockup :
                /*Set the OpMode Ttype Flag*/
                NdefMap->OpModeType[0] = phHal_eOpModesMockup;
                NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
                status = phFriNfc_Mockup_ChkNdef(NdefMap);
                break;
#endif  /* PHFRINFC_OVRHAL_MOCKUP */

            default :
                /*  Remote device is not recognised.
                Probably not NDEF compliant */
                status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                    NFCSTATUS_INVALID_REMOTE_DEVICE);
                break;
            }
        }
    }
    return status;
}


/*!
* \brief Completion Routine, Processing function, needed to avoid long blocking.
* \note The lower (Overlapped HAL) layer must register a pointer to this function as a Completion
*       Routine in order to be able to notify the component that an I/O has finished and data are
*       ready to be processed.
* This is a main Ndef Map Process api.Internally,depending upon the different
* CardTypes,respective mifare/desfire process functions are called.
*
*/

void phFriNfc_NdefMap_Process(  void        *Context,
                              NFCSTATUS   Status)
{

    if ( Context != NULL )
    {
        phFriNfc_NdefMap_t  *NdefMap = (phFriNfc_NdefMap_t *)Context;
        /*
        * 4 Changed
        *   Description: Opmode replace with RevDevTyp.
        */

        switch ( NdefMap->psRemoteDevInfo->RemDevType )
        {
        case  phHal_eMifare_PICC :
        case phHal_eISO14443_3A_PICC:

            if((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD) ||
                (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD))
            {
#ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
                /*  Remote device is Mifare Standard card */
                phFriNfc_MifareStdMap_Process(NdefMap,Status);
#else   /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
                Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                    NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
            }
            else
            {
#ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
                /*  Remote device is Mifare UL card */
                phFriNfc_MifareUL_Process(NdefMap,Status);
#else   /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
                Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                    NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
            }
            break;

        case phHal_eISO14443_A_PICC :
#ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
            /*  Remote device is Desfire card */
            phFriNfc_Desfire_Process(NdefMap, Status);
#else   /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
            Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
            break;
        case phHal_eISO14443_B_PICC:
            /*  Remote device is Desfire card */
            phFriNfc_Desfire_Process(NdefMap, Status);
            break;

        case phHal_eFelica_PICC :
#ifndef PH_FRINFC_MAP_FELICA_DISABLED
            /*  Remote device is Felica Smart card */
            phFriNfc_Felica_Process(NdefMap, Status);
#else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
            Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/
            break;

        case phHal_eJewel_PICC:
#ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
            if ( NdefMap->CardType == PH_FRINFC_NDEFMAP_TOPAZ_CARD )
            {
                /*  Remote device is Topaz Smart card */
                phFriNfc_TopazMap_Process(NdefMap, Status);
            }
#ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
            else if ( NdefMap->CardType == PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD )
            {
                /*  Remote device is Topaz Smart card */
                phFriNfc_TopazDynamicMap_Process(NdefMap, Status);
            }
            else
            {
                Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                NFCSTATUS_INVALID_REMOTE_DEVICE);

            }
            break;
#endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED*/
#endif


#ifdef PHFRINFC_OVRHAL_MOCKUP
        case phHal_eOpModesMockup:
            /*  Remote device is Desfire card */
            phFriNfc_Mockup_Process(NdefMap, Status);
            break;
#endif  /* PHFRINFC_OVRHAL_MOCKUP*/
        default :
            /*  Remote device opmode not recognised.
            Probably not NDEF compliant */
            Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                NFCSTATUS_INVALID_REMOTE_DEVICE);
            /* set the state back to the Reset_Init state*/
            NdefMap->State =  PH_FRINFC_NDEFMAP_STATE_RESET_INIT;

            /* set the completion routine*/
            NdefMap->CompletionRoutine[PH_FRINFC_NDEFMAP_CR_INVALID_OPE].
                CompletionRoutine(NdefMap->CompletionRoutine->Context, Status);
            break;
        }
    }
    else
    {
        Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
            NFCSTATUS_INVALID_PARAMETER);
        /* The control should not come here. As Context itself is NULL ,
        Can't call the CR*/
    }
}

#if 0

NFCSTATUS phFriNfc_ChkAndParseTLV(phFriNfc_NdefMap_t    *NdefMap)
{
    NFCSTATUS status = NFCSTATUS_PENDING;

    switch ( NdefMap->CardType )
    {
#ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
            case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :


                break;
#endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */

#ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
            case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
                status = phFriNfc_Desf_ChkAndParseTLV(NdefMap);
                return (status);

                break;
#endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */

#ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
            case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
            case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :

                break;
#endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */

#ifndef PH_FRINFC_MAP_FELICA_DISABLED
            case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
                ;
                break;
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED */

            default :
                /*  Unknown card type. Return error */
                status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
                    NFCSTATUS_INVALID_REMOTE_DEVICE);

                break;
    }

    return ( status);
}
#endif


#ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
static void phFriNfc_DesfCapCont_HReset(phFriNfc_NdefMap_t *NdefMap)
{
    /* Initialise/reset the desfire capability contatiner structure variables*/
    NdefMap->DesfireCapContainer.DesfVersion = 0;
    NdefMap->DesfireCapContainer.NdefMsgFid  = 0;
    NdefMap->DesfireCapContainer.NdefFileSize = 0;
    NdefMap->DesfireCapContainer.MaxCmdSize  = 0;
    NdefMap->DesfireCapContainer.MaxRespSize = 0;
    NdefMap->DesfireCapContainer.ReadAccess  = 0;
    NdefMap->DesfireCapContainer.WriteAccess = 0;
}
#endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */

#ifndef PH_FRINFC_MAP_FELICA_DISABLED
static void phFriNfc_Felica_HReset(phFriNfc_NdefMap_t *NdefMap)
{
    uint8_t index=0;

    /* Initialise/reset the different felica structure variables*/
    /* Reset all the felica Basic staruture variables*/
    NdefMap->Felica.CurBlockNo = 0;

    for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_BLOCK_SIZE;index++)
    {
        NdefMap->Felica.Rd_BytesToCopyBuff[index] = 0;
        NdefMap->Felica.Wr_RemainedBytesBuff[index] = 0;
    }
    NdefMap->Felica.Rd_NoBytesToCopy = 0;
    NdefMap->Felica.Wr_BytesRemained = 0;


    /* Reset all the felica attribute information staruture variables*/
    for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_ATTR_NDEF_DATA_LEN;index++)
    {
        NdefMap->FelicaAttrInfo.LenBytes[index] = 0;
    }

    NdefMap->FelicaAttrInfo.Nmaxb = 0;
    NdefMap->FelicaAttrInfo.Nbr = 0;
    NdefMap->FelicaAttrInfo.Nbw= 0;
    NdefMap->FelicaAttrInfo.RdWrFlag = 0;
    NdefMap->FelicaAttrInfo.WriteFlag = 0;
    NdefMap->Felica.CurrBytesRead=0;

    /* Reset all the felica manufacture details staruture variables*/
    for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_MANUF_ID_DATA_LEN;index++)
    {
        NdefMap->FelicaManufDetails.ManufID[index] = 0;
        NdefMap->FelicaManufDetails.ManufParameter[index] = 0;
    }
    NdefMap->Felica.NoBlocksWritten=0;
}
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED */

NFCSTATUS phFriNfc_NdefMap_EraseNdef(phFriNfc_NdefMap_t *NdefMap)
{
    NFCSTATUS   status = NFCSTATUS_PENDING;

    static uint8_t     PktData[3] = PH_FRINFC_NDEFMAP_EMPTY_NDEF_MSG;
    uint8_t     MemOffset = PH_FRINFC_NDEFMAP_SEEK_BEGIN;
    static uint32_t    PacketDataLength = sizeof(PktData);

    if (NdefMap == NULL )
    {
        /*  Invalid input parameter error   */
        status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        switch ( NdefMap->CardType)
        {
#ifdef PHFRINFC_OVRHAL_MOCKUP
            case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
#endif  /* PHFRINFC_OVRHAL_MOCKUP */
            case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
            case  PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
#ifdef DESFIRE_EV1
            case  PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1 :
#endif /* #ifdef DESFIRE_EV1 */
            case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
            case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
#ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
            case  PH_FRINFC_NDEFMAP_TOPAZ_CARD :
#ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
            case  PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
#endif
#ifndef PH_FRINFC_MAP_ISO15693_DISABLED
            case PH_FRINFC_NDEFMAP_ISO15693_CARD:
#endif
#endif
                /*  Mifare card selected. Call Mifare Write */
                status =  phFriNfc_NdefMap_WrNdef( NdefMap,
                    PktData,
                    &PacketDataLength,
                    MemOffset);
                break;

            case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :

#ifndef PH_FRINFC_MAP_FELICA_DISABLED
                /*  Felica card selected. Call to write EMPTY NDEF Msg */
                status =  phFriNfc_Felica_EraseNdef( NdefMap );
#else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
                status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                    NFCSTATUS_INVALID_REMOTE_DEVICE);
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/

                break;
            default :
                /*  Unknown card type. Return error */
                status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
                    NFCSTATUS_INVALID_REMOTE_DEVICE);
                break;
        }
    }
    return status;
}
/*  Harsha: Fix for the mantis entry 0000420: NDEF_MAP: Size of NDEF data:
no abstracted way for user to know how many bytes to read/write  */

/*!
* \brief Helper API, exposed to the user to enable him to know the size
*        of the NDEF data that he can write in to the card.
*/
NFCSTATUS phFriNfc_NdefMap_GetContainerSize(const phFriNfc_NdefMap_t *NdefMap,uint32_t *maxSize, uint32_t *actualSize)
{
    NFCSTATUS   result = NFCSTATUS_SUCCESS;
    uint8_t     sect_index = 0;
    uint8_t     actual_sect_index = 0;
    uint8_t     count_index = 0;

    if( (NdefMap == NULL) || (maxSize == NULL) || (actualSize == NULL))
    {
        /*  Invalid input parameter error   */
        result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        /*  Which card ? */
        switch(NdefMap->CardType)
        {
#ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
            case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
                /*  Mifare UL card */
                /*  The integration needs to ensure that the checkNdef
                function has been called before calling this function,
                otherwise NdefMap->CardMemSize will be 0 */
                *maxSize = NdefMap->MifareULContainer.RemainingSize;
                /* In Mifare UL card, the actual size is the length field
                value of the TLV */
                *actualSize = NdefMap->TLVStruct.ActualSize;
                break;
#endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED */

#ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
            case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
#ifdef DESFIRE_EV1
            case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD_EV1 :
#endif /* #ifdef DESFIRE_EV1 */
                /*  Desfire card */
                /*  The integration needs to ensure that the checkNdef
                function has been called before calling this function,
                otherwise NdefMap->DesfireCapContainer.NdefFileSize
                will be 0 */
                /* -2 bytes represents the size field bytes*/
                *maxSize = NdefMap->DesfireCapContainer.NdefFileSize - 2;
                /* In Desfire card, the actual size cant be calculated so
                the actual size is given as 0xFFFFFFFF */
                *actualSize = NdefMap->DesfireCapContainer.NdefDataLen;
                break;
#endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */

#ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
            case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
                /*  Topaz card */
                /*  The integration needs to ensure that the checkNdef
                function has been called before calling this function,
                otherwise NdefMap->CardMemSize will be 0 */
                *maxSize = NdefMap->TopazContainer.RemainingSize;
                /* In Topaz card, the actual size is the length field value of the
                TLV */
                *actualSize = NdefMap->TLVStruct.BytesRemainLinTLV;
                break;
#ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
            case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
                /*  Topaz 512 card */
                /*  The integration needs to ensure that the checkNdef
                function has been called before calling this function,
                otherwise NdefMap->CardMemSize will be 0 */
                *maxSize = NdefMap->TopazContainer.NDEFRWSize;
                /* In Topaz card, the actual size is the length field value of the
                TLV */
                *actualSize = NdefMap->TopazContainer.ActualNDEFMsgSize;
                break;

#endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */
#endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
#ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
            case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
            case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
                /*  Mifare std card */

                /*  Max size is the number of NDEF compliant blocks in the card
                multiplied by 16 bytes */
#ifndef PH_HAL4_ENABLE

                *maxSize = NdefMap->StdMifareContainer.remainingSize;

#else /* #ifndef PH_HAL4_ENABLE */

                while ((PH_FRINFC_MIFARESTD_NDEF_COMP ==
                        NdefMap->StdMifareContainer.aid[count_index]) &&
                        (count_index <
                        PH_FRINFC_NDEFMAP_MIFARESTD_TOTALNO_BLK))
                {
                    actual_sect_index++;
                    count_index++;
                }
                /* Total number of sectors in 1k = 16 (0 to 15, MAD sector number = 0)
                    Total number of sectors in 4k = 40 (0 to 39,
                        MAD sector number = 0 and 16, After block number 31, each sector
                        has 16 blocks)
                    Every last block of the sector is the sector header, So the blocks
                    that can be read or written in each sector is always
                        (number of blocks in each sector - 1)
                    No of blocks in the one sector till the sector number 0 to 31
                        (total 32 sectors) =
                        4 blocks, So blocks that can be read/write = (4 - 1 = 3 blocks)
                    No of blocks in the one sector after the sector number 31 to 39 =
                        16 blocks, So blocks that can be read/write = (16 - 1 = 15 blocks)
                    Each block has 16 bytes
                    To calculate number of bytes in the sector, depending on the number
                    of blocks multiplied by 16
                */
                if (PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD ==
                    NdefMap->CardType)
                {
                    if (actual_sect_index > 32)
                    {
                        sect_index = (actual_sect_index - 32);
                        /* Here, 30 is used because block number 0 and 16 are MAD blocks
                        which cannot be used for reading and writing data
                        3 and 15 are blocks in each sector which can be read/write
                        3 indicates the sector is in between (0 and 31)
                        15 indicates the sector is in between (32 to 39)
                        16 is always number of bytes in each block
                        4 is used because each NDEF write has to write using the
                            TLV format and T & L takes 4 bytes length and V is the
                            input data
                        */
                        *maxSize = (((30 * (16 * 3)) + (sect_index * (16 * 15))) - 4);
                    }
                    else if (actual_sect_index <= 16)
                    {
                        *maxSize = (((actual_sect_index - 1) * (16 * 3)) - 4);
                    }
                    else
                    {
                        *maxSize = (((actual_sect_index - 2)  * (16 * 3)) - 4);
                    }
                }
                else
                {
                    /* Here, 16 is always number of bytes in each block
                        3 indicates the sector is in between (0 and 31) */
                    if (actual_sect_index > NdefMap->StdMifareContainer.SectorIndex)
                    {
                        actual_sect_index = NdefMap->StdMifareContainer.SectorIndex;
                    }
                    *maxSize = (((actual_sect_index - 1) * (16 * 3)) - 4);
                }

#endif /* #ifndef PH_HAL4_ENABLE */
                *actualSize = NdefMap->TLVStruct.BytesRemainLinTLV;

                break;
#endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */

#ifndef PH_FRINFC_MAP_FELICA_DISABLED
            case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
                /*  Felica card */

                *maxSize = NdefMap->FelicaAttrInfo.Nmaxb * 0x10;

                /* In Felica Card, actual size is calculated using the Length Bytes */
                *actualSize = NdefMap->FelicaAttrInfo.LenBytes[0];
                *actualSize = *actualSize << 16;
                *actualSize += NdefMap->FelicaAttrInfo.LenBytes[1];
                *actualSize = *actualSize << 8;
                *actualSize += NdefMap->FelicaAttrInfo.LenBytes[2];
                break;
#endif  /* PH_FRINFC_MAP_FELICA_DISABLED */

#ifndef PH_FRINFC_MAP_ISO15693_DISABLED
            case PH_FRINFC_NDEFMAP_ISO15693_CARD:
            {
#if 1
                uint16_t                    block_no = 0;
                uint8_t                     byte_no = 0;

                block_no = (uint16_t)
                    ISO15693_GET_VALUE_FIELD_BLOCK_NO (
                        NdefMap->ISO15693Container.ndef_tlv_type_blk, 
                        NdefMap->ISO15693Container.ndef_tlv_type_byte, 
                        NdefMap->ISO15693Container.actual_ndef_size);
                byte_no = (uint8_t)
                    ISO15693_GET_VALUE_FIELD_BYTE_NO (
                        NdefMap->ISO15693Container.ndef_tlv_type_blk, 
                        NdefMap->ISO15693Container.ndef_tlv_type_byte, 
                        NdefMap->ISO15693Container.actual_ndef_size);

                *maxSize = (NdefMap->ISO15693Container.max_data_size - 
                            ((block_no * ISO15693_BYTES_PER_BLOCK) + byte_no));
#else /* #if 1 */
                /* 2 is used to exclude the T and L part of the TLV */
                *maxSize = (NdefMap->ISO15693Container.max_data_size
                            - ISO15693_BYTES_PER_BLOCK - 2);
#endif /* #if 1 */
                *actualSize = NdefMap->ISO15693Container.actual_ndef_size;
                break;
            }
#endif

#ifdef PHFRINFC_OVRHAL_MOCKUP
            case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
                *maxSize = 0xFFFFFFFF;
                /* In Desfire card, the actual size cant be calculated so
                the actual size is given as 0xFFFFFFFF */
                *actualSize = 0xFFFFFFFF;
                break;
#endif  /* PHFRINFC_OVRHAL_MOCKUP */

            default :
                /*  Unknown card type. Return error */
                result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
                    NFCSTATUS_INVALID_REMOTE_DEVICE);
                break;
        }
    }
    return(result);
}

#ifdef PHFRINFC_OVRHAL_MOCKUP
NFCSTATUS phFriNfc_NdefMap_MockupCardSetter(phFriNfc_NdefMap_t *NdefMap,
                                            uint8_t *NdefData,
                                            uint32_t NdefActualSize,
                                            uint32_t NdefMaxSize,
                                            uint32_t CardSize)
{
    NFCSTATUS Status = NFCSTATUS_SUCCESS;
    // First check all parameters
    if((NdefData != NULL) && (NdefMap != NULL))
    {
        // OK we can set
        NdefMap->MochupContainer.NdefData       = NdefData;
        NdefMap->MochupContainer.NdefActualSize = NdefActualSize;
        NdefMap->MochupContainer.NdefMaxSize    = NdefMaxSize;
        NdefMap->MochupContainer.CardSize       = CardSize;
        NdefMap->MochupContainer.CurrentBlock   = 0;

    } else
    {
        Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    }
    return Status;
}

NFCSTATUS phFriNfc_NdefMap_MockupNDefModeEn(uint8_t  *pNdefCompliancy, uint8_t  *pCardType, uint8_t Enable)
{
    *pNdefCompliancy = Enable;
    *pCardType = PH_FRINFC_NDEFMAP_MOCKUP_CARD;
    return NFCSTATUS_SUCCESS;
}
#endif  /* PHFRINFC_OVRHAL_MOCKUP */