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
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
|
/*
* 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 phFriNfc_FelicaMap.c
* \brief This component encapsulates read/write/check ndef/process functionalities,
* for the Felica Smart Card.
*
* Project: NFC-FRI
*
* $Date: Thu May 6 14:01:35 2010 $
* $Author: ing07385 $
* $Revision: 1.10 $
* $Aliases: NFC_FRI1.1_WK1017_R34_4,NFC_FRI1.1_WK1023_R35_1 $
*
*/
#ifndef PH_FRINFC_MAP_FELICA_DISABLED
#include <phNfcTypes.h>
#include <phFriNfc_OvrHal.h>
#include <phFriNfc_FelicaMap.h>
#include <phFriNfc_MapTools.h>
/*! \ingroup grp_file_attributes
* \name NDEF Mapping
*
* File: \ref phFriNfc_FelicaMap.c
*
*/
/*@{*/
#define PHFRINFCNDEFMAP_FILEREVISION "$Revision: 1.10 $"
#define PHFRINFCNDEFMAP_FILEALIASES "$Aliases: NFC_FRI1.1_WK1017_R34_4,NFC_FRI1.1_WK1023_R35_1 $"
/*@}*/
/* Helpers for Read and updating the attribute informations*/
static NFCSTATUS phFriNfc_Felica_HRdAttrInfo(phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_Felica_HUpdateAttrInfo(phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_Felica_HCalCheckSum(const uint8_t *TempBuffer,
uint8_t StartIndex,
uint8_t EndIndex,
uint16_t RecvChkSum);
/* Helpers for Poll Related Operations*/
static NFCSTATUS phFriNfc_Felica_HPollCard( phFriNfc_NdefMap_t *NdefMap,
const uint8_t sysCode[],
uint8_t state);
static NFCSTATUS phFriNfc_Felica_HUpdateManufIdDetails(const phFriNfc_NdefMap_t *NdefMap);
/*Helpers for Reading Operations*/
static NFCSTATUS phFriNfc_Felica_HReadData(phFriNfc_NdefMap_t *NdefMap,uint8_t offset);
static uint16_t phFriNfc_Felica_HGetMaximumBlksToRead(const phFriNfc_NdefMap_t *NdefMap,uint8_t NbcOrNmaxb );
static void phFriNfc_Felica_HAfterRead_CopyDataToBuff(phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_Felica_HSetTransceiveForRead(phFriNfc_NdefMap_t *NdefMap,uint16_t TrxLen,uint8_t Offset);
static uint16_t phFriNfc_Felica_HSetTrxLen(phFriNfc_NdefMap_t *NdefMap,uint16_t Nbc);
static NFCSTATUS phFriNfc_Felica_HChkApduBuff_Size( phFriNfc_NdefMap_t *NdefMap);
/* Helpers for Writing Operations*/
static NFCSTATUS phFriNfc_Felica_HChkAttrBlkForWrOp(phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_Felica_HChkAttrBlkForRdOp(phFriNfc_NdefMap_t *NdefMap,
uint32_t NdefLen);
static NFCSTATUS phFriNfc_Felica_HUpdateAttrBlkForWrOp(phFriNfc_NdefMap_t *NdefMap,uint8_t isStarted);
static NFCSTATUS phFriNfc_Felica_HUpdateData(phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_Felica_HWriteDataBlk(phFriNfc_NdefMap_t *NdefMap);
/* Write Empty NDEF Message*/
static NFCSTATUS phFriNfc_Felica_HWrEmptyMsg(phFriNfc_NdefMap_t *NdefMap);
/*Helpers for common checks*/
static NFCSTATUS phFriNfc_Felica_HCheckManufId(const phFriNfc_NdefMap_t *NdefMap);
static void phFriNfc_Felica_HCrHandler(phFriNfc_NdefMap_t *NdefMap,
uint8_t CrIndex,
NFCSTATUS Status);
static void phFriNfc_Felica_HInitInternalBuf(uint8_t *Buffer);
static int phFriNfc_Felica_MemCompare ( void *s1, void *s2, unsigned int n );
/*!
* \brief returns maximum number of blocks can be read from the Felica Smart Card.
*
* The function is useful in reading of NDEF information from a felica tag.
*/
static uint16_t phFriNfc_Felica_HGetMaximumBlksToRead(const phFriNfc_NdefMap_t *NdefMap, uint8_t NbcOrNmaxb )
{
uint16_t BlksToRead=0;
uint32_t DataLen = 0;
/* This part of the code is useful if we take account of Nbc blks reading*/
if ( NbcOrNmaxb == PH_NFCFRI_NDEFMAP_FELI_NBC )
{
PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0],
NdefMap->FelicaAttrInfo.LenBytes[1],
NdefMap->FelicaAttrInfo.LenBytes[2],
DataLen);
/* Calculate Nbc*/
BlksToRead = (uint16_t) ( ((DataLen % 16) == 0) ? (DataLen >> 4) : ((DataLen >> 4) +1) );
}
else if ( NbcOrNmaxb == PH_NFCFRI_NDEFMAP_FELI_NMAXB)
{
BlksToRead = NdefMap->FelicaAttrInfo.Nmaxb;
}
else
{
/* WARNING !!! code should not reach this point*/
;
}
return (BlksToRead);
}
/*!
* \brief Initiates Reading of NDEF information from the Felica Card.
*
* The function initiates the reading of NDEF information from a Remote Device.
* 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.
*/
NFCSTATUS phFriNfc_Felica_RdNdef( phFriNfc_NdefMap_t *NdefMap,
uint8_t *PacketData,
uint32_t *PacketDataLength,
uint8_t Offset)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint32_t Nbc = 0;
NdefMap->ApduBufferSize = *PacketDataLength;
/*Store the packet data buffer*/
NdefMap->ApduBuffer = PacketData;
NdefMap->NumOfBytesRead = PacketDataLength ;
*NdefMap->NumOfBytesRead = 0;
NdefMap->ApduBuffIndex = 0;
NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE;
NdefMap->Felica.Offset = Offset;
if( ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN )||( NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE))
{
NdefMap->Felica.CurBlockNo = 0;
NdefMap->Felica.OpFlag = PH_FRINFC_NDEFMAP_FELI_RD_ATTR_RD_OP;
NdefMap->Felica.IntermediateCpyFlag = FALSE;
NdefMap->Felica.IntermediateCpyLen = 0;
NdefMap->Felica.Rd_NoBytesToCopy = 0;
NdefMap->Felica.EofCardReachedFlag= FALSE ;
NdefMap->Felica.LastBlkReachedFlag = FALSE;
NdefMap->Felica.CurrBytesRead = 0;
phFriNfc_Felica_HInitInternalBuf(NdefMap->Felica.Rd_BytesToCopyBuff);
/* send request to read attribute information*/
status = phFriNfc_Felica_HRdAttrInfo(NdefMap);
/* handle the error in Transc function*/
if ( (status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_CHK_NDEF,status);
}
}
else
{
Nbc = phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC);
/* Offset = Current, but the read has reached the End of NBC Blocks */
if(( ( Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && (NdefMap->Felica.CurBlockNo == Nbc)) &&
(NdefMap->Felica.EofCardReachedFlag == FELICA_RD_WR_EOF_CARD_REACHED ))
{
status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_EOF_NDEF_CONTAINER_REACHED);
}
else
{
NdefMap->Felica.CurrBytesRead = ((NdefMap->Felica.CurBlockNo * 16)- NdefMap->Felica.Rd_NoBytesToCopy);
status = phFriNfc_Felica_HReadData(NdefMap,NdefMap->Felica.Offset);
}
}
return (status);
}
/*Read Operation Related Helper Routines*/
/*!
* \brief Used in Read Opearation.Sets the Trx Buffer Len calls Transc Cmd.
* After a successful read operation, function does checks the user buffer size
* sets the status flags.
*/
static NFCSTATUS phFriNfc_Felica_HReadData(phFriNfc_NdefMap_t *NdefMap,uint8_t offset)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint16_t Nbc=0,TranscLen=0;
Nbc = phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC);
if( ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) > 0) && (NdefMap->Felica.CurBlockNo < Nbc ))
{
/* if data is present in the internal buffer*/
if (NdefMap->Felica.Rd_NoBytesToCopy > 0 )
{
/* copy data to external buffer*/
phFriNfc_Felica_HAfterRead_CopyDataToBuff(NdefMap);
/*Check the size of user buffer*/
status = phFriNfc_Felica_HChkApduBuff_Size(NdefMap);
if ( (status != NFCSTATUS_SUCCESS) && (NdefMap->Felica.IntermediateRdFlag == TRUE ))
{
/* set the transc len and call transc cmd*/
TranscLen = phFriNfc_Felica_HSetTrxLen(NdefMap,Nbc);
status= phFriNfc_Felica_HSetTransceiveForRead(NdefMap,TranscLen,offset);
}
else
{
/* Nothing to be done , if IntermediateRdFlag is set to zero*/
;
}
}
else
{
/* set the transc len and call transc cmd*/
TranscLen = phFriNfc_Felica_HSetTrxLen(NdefMap,Nbc);
status= phFriNfc_Felica_HSetTransceiveForRead(NdefMap,TranscLen,offset);
}
}
else
{
/* Chk the Buffer size*/
status = phFriNfc_Felica_HChkApduBuff_Size(NdefMap);
if ( (status != NFCSTATUS_SUCCESS) && (NdefMap->Felica.IntermediateRdFlag == TRUE ))
{
TranscLen = phFriNfc_Felica_HSetTrxLen(NdefMap,Nbc);
status= phFriNfc_Felica_HSetTransceiveForRead(NdefMap,TranscLen,offset);
}
}
return (status);
}
/*!
* \brief Used in Read Opearation.Sets the Trx Buffer Len.
*/
static uint16_t phFriNfc_Felica_HSetTrxLen(phFriNfc_NdefMap_t *NdefMap,uint16_t Nbc)
{
uint16_t TranscLen = 0,BlocksToRead=0;
if( ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)% 16) == 0)
{
BlocksToRead = (uint16_t)( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)/16 );
}
else
{
BlocksToRead = (uint16_t)(((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)/16) +1);
}
if ( (BlocksToRead > Nbc) ||( (BlocksToRead) > ( Nbc - NdefMap->Felica.CurBlockNo)) )
{
BlocksToRead = Nbc - NdefMap->Felica.CurBlockNo;
}
if ( BlocksToRead >= NdefMap->FelicaAttrInfo.Nbr)
{
if( NdefMap->FelicaAttrInfo.Nbr < Nbc )
{
TranscLen = NdefMap->FelicaAttrInfo.Nbr*16;
}
else
{
TranscLen = Nbc*16;
NdefMap->Felica.LastBlkReachedFlag =1;
}
}
else
{
if (BlocksToRead <= Nbc )
{
if ( ( BlocksToRead * 16) == ((Nbc *16) - (NdefMap->Felica.CurBlockNo * 16)))
{
NdefMap->Felica.LastBlkReachedFlag =1;
}
TranscLen = BlocksToRead*16;
}
else
{
TranscLen = Nbc*16;
}
}
/* As Cur Blk changes, to remember the exact len what we had set
in the begining of each read operation*/
NdefMap->Felica.TrxLen = TranscLen;
return (TranscLen);
}
/*!
* \brief Used in Read Opearation.After a successful read operation,
* Copies the data to user buffer.
*/
static void phFriNfc_Felica_HAfterRead_CopyDataToBuff(phFriNfc_NdefMap_t *NdefMap)
{
uint8_t ResetFlag = FALSE, ExtrBytesToCpy = FALSE;
uint16_t Nbc=0;
uint32_t DataLen=0;
Nbc = phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC );
PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0],
NdefMap->FelicaAttrInfo.LenBytes[1],
NdefMap->FelicaAttrInfo.LenBytes[2],
DataLen);
/* Internal Buffer has some old read bytes to cpy to user buffer*/
if( NdefMap->Felica.Rd_NoBytesToCopy > 0 )
{
if ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) < NdefMap->Felica.Rd_NoBytesToCopy )
{
NdefMap->Felica.Rd_NoBytesToCopy -= (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
if (NdefMap->Felica.IntermediateCpyFlag == TRUE )
{
/*Copy data from the internal buffer to user buffer*/
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(&(NdefMap->Felica.Rd_BytesToCopyBuff[NdefMap->Felica.IntermediateCpyLen])),
(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex));
/* Store number of bytes copied frm internal buffer to User Buffer */
NdefMap->Felica.IntermediateCpyLen += (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
NdefMap->Felica.IntermediateCpyFlag = 1;
/* check do we reach len bytes any chance*/
PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0],
NdefMap->FelicaAttrInfo.LenBytes[1],
NdefMap->FelicaAttrInfo.LenBytes[2],
DataLen);
/* Internal buffer has zero bytes for copy operation*/
if ( NdefMap->Felica.Rd_NoBytesToCopy == 0)
{
NdefMap->Felica.EofCardReachedFlag =FELICA_RD_WR_EOF_CARD_REACHED;
}
}
else
{
/*Copy data from the internal buffer to apdu buffer*/
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
NdefMap->Felica.Rd_BytesToCopyBuff,
(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex));
}
NdefMap->ApduBuffIndex += (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
}
else if ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) == NdefMap->Felica.Rd_NoBytesToCopy )
{
if ( NdefMap->Felica.IntermediateCpyFlag == TRUE )
{
/*Copy data internal buff to apdubuffer*/
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(&(NdefMap->Felica.Rd_BytesToCopyBuff[NdefMap->Felica.IntermediateCpyLen])),
NdefMap->Felica.Rd_NoBytesToCopy);
}
else
{
/*Copy data internal buff to apdubuffer*/
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
NdefMap->Felica.Rd_BytesToCopyBuff,
NdefMap->Felica.Rd_NoBytesToCopy);
}
/*increment the index,internal buffer len*/
NdefMap->ApduBuffIndex += NdefMap->Felica.Rd_NoBytesToCopy;
NdefMap->Felica.Rd_NoBytesToCopy -= (uint8_t)(NdefMap->ApduBuffIndex);
/* To reset the parameters*/
ResetFlag = TRUE;
}
else
{
/* Extra Bytes to Copy from internal buffer to external buffer*/
if ( NdefMap->Felica.IntermediateCpyFlag == TRUE )
{
/*Copy data internal buff to apdubuffer*/
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(&(NdefMap->Felica.Rd_BytesToCopyBuff[NdefMap->Felica.IntermediateCpyLen])),
NdefMap->Felica.Rd_NoBytesToCopy);
}
else
{
/*Copy data internal buff to apdubuffer*/
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
NdefMap->Felica.Rd_BytesToCopyBuff,
NdefMap->Felica.Rd_NoBytesToCopy);
}
/*increment the index*/
NdefMap->ApduBuffIndex += NdefMap->Felica.Rd_NoBytesToCopy;
/* To reset the parameters*/
ResetFlag = TRUE;
}
}/*End of Internal Buffer has some old read bytes to cpy to user buffer*/
else
{
/* check if last block is reached*/
if ( ((NdefMap->Felica.LastBlkReachedFlag == 1) && (( NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= 16)) )
{
/* greater than 16 but less than the data len size*/
if (( NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= DataLen)
{
NdefMap->Felica.CurrBytesRead = (uint16_t)((DataLen) - (NdefMap->Felica.CurrBytesRead +
NdefMap->ApduBuffIndex));
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(&(NdefMap->SendRecvBuf[13])),
NdefMap->Felica.CurrBytesRead);
NdefMap->ApduBuffIndex += NdefMap->Felica.CurrBytesRead;
if ( NdefMap->ApduBuffIndex == DataLen)
{
ResetFlag = TRUE;
}
}
else
{
/* need to check exact no. of bytes to copy to buffer*/
if( ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) <= NdefMap->Felica.TrxLen )||
((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) <= DataLen ))
{
ExtrBytesToCpy = TRUE;
}
else
{
NdefMap->Felica.Rd_NoBytesToCopy = (uint8_t)(16-(( Nbc * 16) - (DataLen)));
if ( NdefMap->Felica.Rd_NoBytesToCopy > (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex))
{
/*Reduce already copied bytes from the internal buffer*/
NdefMap->Felica.Rd_NoBytesToCopy -= (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
ExtrBytesToCpy = TRUE;
}
else
{
ExtrBytesToCpy = FALSE;
}
}
if ( ExtrBytesToCpy == TRUE )
{
NdefMap->Felica.CurrBytesRead = (uint16_t)((DataLen)- (NdefMap->Felica.CurrBytesRead +
NdefMap->ApduBuffIndex));
if(NdefMap->Felica.CurrBytesRead <
(uint16_t)(NdefMap->ApduBufferSize -
NdefMap->ApduBuffIndex))
{
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(&(NdefMap->SendRecvBuf[13])),
NdefMap->Felica.CurrBytesRead);
}
else
{
(void)memcpy( (&( NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(&( NdefMap->SendRecvBuf[13])),
(uint16_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex));
}
if ( NdefMap->Felica.LastBlkReachedFlag == 1 )
{
NdefMap->Felica.Rd_NoBytesToCopy =
(uint8_t)((NdefMap->Felica.CurrBytesRead >
(uint16_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex))?
(NdefMap->Felica.CurrBytesRead -
(uint16_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)):
0);
ResetFlag = ((NdefMap->Felica.Rd_NoBytesToCopy == 0)?TRUE:FALSE);
}
else
{
NdefMap->Felica.Rd_NoBytesToCopy = (uint8_t)( NdefMap->Felica.TrxLen - (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex));
}
/* Copy remained bytes back into internal buffer*/
(void)memcpy( NdefMap->Felica.Rd_BytesToCopyBuff,
(&(NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_RESP_HEADER_LEN+(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)])),
NdefMap->Felica.Rd_NoBytesToCopy);
/* set the intermediate flag : This flag remembers that there are still X no. bytes remained in
Internal Buffer Ex: User has given only one byte buffer,needs to cpy one byte at a time*/
NdefMap->Felica.IntermediateCpyFlag = TRUE;
NdefMap->ApduBuffIndex += ((NdefMap->Felica.CurrBytesRead <
(uint16_t)(NdefMap->ApduBufferSize -
NdefMap->ApduBuffIndex))?
NdefMap->Felica.CurrBytesRead:
(uint16_t)(NdefMap->ApduBufferSize -
NdefMap->ApduBuffIndex));
}
else
{
/*Copy data from the internal buffer to user buffer*/
(void)memcpy( (&( NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(&( NdefMap->SendRecvBuf[13])),
NdefMap->Felica.Rd_NoBytesToCopy);
NdefMap->ApduBuffIndex += NdefMap->Felica.Rd_NoBytesToCopy;
ResetFlag = TRUE;
}
}
}
else
{
if ((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) < NdefMap->Felica.TrxLen )
{
/* Calculate exactly remained bytes to copy to internal buffer and set it*/
if ( NdefMap->Felica.LastBlkReachedFlag == 1)
{
NdefMap->Felica.Rd_NoBytesToCopy = (uint8_t)(16-(( Nbc * 16) - DataLen));
if ( NdefMap->Felica.Rd_NoBytesToCopy > (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex))
{
/*Reduce already copied bytes from the internal buffer*/
NdefMap->Felica.Rd_NoBytesToCopy -= (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
ExtrBytesToCpy = TRUE;
}
}
else
{
NdefMap->Felica.Rd_NoBytesToCopy = (uint8_t)(NdefMap->Felica.TrxLen - (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex));
ExtrBytesToCpy = TRUE;
}
if ( ExtrBytesToCpy == TRUE )
{
/*Copy the read data from trx buffer to apdu of size apdu*/
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(&(NdefMap->SendRecvBuf[13])),
NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
/*copy bytesToCopy to internal buffer*/
(void)memcpy( NdefMap->Felica.Rd_BytesToCopyBuff,
(&(NdefMap->SendRecvBuf[13+(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)])),
NdefMap->Felica.Rd_NoBytesToCopy);
NdefMap->Felica.IntermediateCpyFlag = TRUE;
NdefMap->ApduBuffIndex += (uint16_t)NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex;
}
else
{
/*Copy data from the internal buffer to user buffer*/
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(&(NdefMap->SendRecvBuf[13])),
NdefMap->Felica.Rd_NoBytesToCopy);
NdefMap->ApduBuffIndex += NdefMap->Felica.Rd_NoBytesToCopy;
ResetFlag = TRUE;
}
if ( DataLen <= (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) )
{
NdefMap->Felica.EofCardReachedFlag =FELICA_RD_WR_EOF_CARD_REACHED;
}
else
{
;
}
}
else if ((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) == NdefMap->Felica.TrxLen )
{
/*Copy exactly remained last bytes to user buffer and increment the index*/
/*13 : 1+12 : 1st byte entire pkt length + 12 bytes to skip manuf details*/
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(&(NdefMap->SendRecvBuf[13])),
(NdefMap->Felica.TrxLen ));
NdefMap->ApduBuffIndex += NdefMap->Felica.TrxLen;
}
else
{
if ((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) > NdefMap->Felica.TrxLen )
{
/*Copy the data to apdu buffer and increment the index */
(void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(&(NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_RESP_HEADER_LEN])),
NdefMap->Felica.TrxLen);
NdefMap->ApduBuffIndex += (uint16_t)NdefMap->Felica.TrxLen;
}
}
}
}
if ( ResetFlag == TRUE )
{
/* reset the internal buffer variables*/
NdefMap->Felica.Rd_NoBytesToCopy =0;
NdefMap->Felica.IntermediateCpyLen =0;
NdefMap->Felica.IntermediateCpyFlag =FALSE;
}
return;
}
/*!
* \brief Used in Read Opearation.After a successful read operation,
Checks the relavent buffer sizes and set the status.Following function is used
when we read the Nmaxb blocks. Retained for future purpose.
*/
static NFCSTATUS phFriNfc_Felica_HChkApduBuff_Size( phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint8_t ResetFlag = FALSE;
uint32_t Nbc = 0;
Nbc = phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC);
/* set status to Success : User Buffer is full and Curblk < nmaxb*/
if ( (( NdefMap->ApduBufferSize-NdefMap->ApduBuffIndex )== 0) &&
(NdefMap->Felica.CurBlockNo < Nbc ))
{
status = PHNFCSTVAL(CID_NFC_NONE,
NFCSTATUS_SUCCESS);
/*Reset the index, internal buffer counters back to zero*/
*NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
NdefMap->ApduBuffIndex = 0;
}/*if( (NdefMap->ApduBufferSize-NdefMap->ApduBuffIndex )== 0 && NdefMap->Felica.CurBlockNo < NdefMap->FelicaAttrInfo.Nmaxb )*/
else
{
if (( ( NdefMap->ApduBufferSize-NdefMap->ApduBuffIndex )== 0) &&
(NdefMap->Felica.CurBlockNo == Nbc ))
{
status = PHNFCSTVAL(CID_NFC_NONE,
NFCSTATUS_SUCCESS);
ResetFlag = ((NdefMap->Felica.Rd_NoBytesToCopy > 0 )?
FALSE:
TRUE);
if( ResetFlag== FALSE)
{
*NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
/*Reset the index, internal buffer counters back to zero*/
NdefMap->ApduBuffIndex = 0;
}
}/*if ((NdefMap->ApduBufferSize-NdefMap->ApduBuffIndex )== 0 && NdefMap->Felica.CurBlockNo == NdefMap->FelicaAttrInfo.Nmaxb )*/
else
{
/* reached reading all the blks available in the card: set EOF flag*/
if ( NdefMap->ApduBuffIndex == (Nbc*16))
{
status = PHNFCSTVAL(CID_NFC_NONE,
NFCSTATUS_SUCCESS);
ResetFlag = TRUE;
}
else
{
if ((NdefMap->ApduBufferSize-NdefMap->ApduBuffIndex )> 0 )
{
if ( NdefMap->Felica.CurBlockNo == Nbc )
{
/* bytes pending in internal buffer , No Space in User Buffer*/
if ( NdefMap->Felica.Rd_NoBytesToCopy > 0)
{
if ( NdefMap->Felica.EofCardReachedFlag == TRUE )
{
status = PHNFCSTVAL(CID_NFC_NONE,
NFCSTATUS_SUCCESS);
*NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
NdefMap->ApduBuffIndex=0;
}
else
{
phFriNfc_Felica_HAfterRead_CopyDataToBuff(NdefMap);
if( NdefMap->Felica.Rd_NoBytesToCopy > 0 )
{
status = PHNFCSTVAL(CID_NFC_NONE,
NFCSTATUS_SUCCESS);
*NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
NdefMap->ApduBuffIndex=0;
}
else
{
/* EOF Card Reached set the internal EOF Flag*/
status = PHNFCSTVAL(CID_NFC_NONE,
NFCSTATUS_SUCCESS);
ResetFlag = TRUE;
}
}
}
/* All bytes from internal buffer are copied and set eof flag*/
else
{
status = PHNFCSTVAL(CID_NFC_NONE,
NFCSTATUS_SUCCESS);
ResetFlag = TRUE;
}
}
else
{
/* This flag is set to ensure that, need of Read Opearation
we completed coying the data from internal buffer to external buffer
left some more bytes,in User bufer so initiate the read operation */
NdefMap->Felica.IntermediateRdFlag = TRUE;
}
}
else
{
status = PHNFCSTVAL(CID_NFC_NONE,
NFCSTATUS_SUCCESS);
}
}
}
if ( ResetFlag == TRUE)
{
*NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
/*Reset the index, internal buffer counters back to zero*/
NdefMap->ApduBuffIndex = 0;
NdefMap->Felica.Rd_NoBytesToCopy=0;
NdefMap->Felica.EofCardReachedFlag=FELICA_RD_WR_EOF_CARD_REACHED;
}
}
return( status);
}
/*!
* \brief Used in Read Opearation.Sets the transceive Command for read.
*/
static NFCSTATUS phFriNfc_Felica_HSetTransceiveForRead(phFriNfc_NdefMap_t *NdefMap,uint16_t TrxLen,uint8_t Offset)
{
NFCSTATUS TrxStatus = NFCSTATUS_PENDING;
uint16_t BufIndex=0,i=0;
/* set the felica cmd */
#ifdef PH_HAL4_ENABLE
NdefMap->Cmd.FelCmd = phHal_eFelica_Raw;
#else
NdefMap->Cmd.FelCmd = phHal_eFelicaCmdListFelicaCmd;
#endif /* #ifdef PH_HAL4_ENABLE */
/*Change the state to Read */
NdefMap->State = PH_NFCFRI_NDEFMAP_FELI_STATE_RD_BLOCK;
/* set the complition routines for the mifare operations */
NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_Felica_Process;
NdefMap->MapCompletionInfo.Context = NdefMap;
/*set the additional informations for the data exchange*/
NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0;
/* pkt len : updated at the end*/
NdefMap->SendRecvBuf[BufIndex] = 0x00;
BufIndex ++;
NdefMap->SendRecvBuf[BufIndex] = 0x06;
BufIndex++;
/* IDm - Manufacturer Id : 8bytes*/
#ifdef PH_HAL4_ENABLE
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(void * )(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm)),
8);
#else
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(void * )(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo212_424.Startup212_424.NFCID2t)),
8);
#endif /* #ifdef PH_HAL4_ENABLE */
BufIndex+=8;
/*Number of Services (n=1 ==> 0x80)*/
NdefMap->SendRecvBuf[BufIndex] = 0x01;
BufIndex++;
/*Service Code List*/
NdefMap->SendRecvBuf[BufIndex] = 0x0B;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00;
BufIndex++;
/*Number of Blocks to read*/
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(TrxLen/16);
BufIndex++;
/* Set the Blk numbers as per the offset set by the user : Block List*/
if ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN )
{
for ( i=0;i<(TrxLen/16);i++)
{
/*1st Service Code list : byte 1*/
NdefMap->SendRecvBuf[BufIndex] = 0x80;
BufIndex++;
/* No. Of Blocks*/
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(i + 1);
BufIndex++;
}
}
else
{
for ( i= 1;i<=(TrxLen/16);i++)
{
/*1st Service Code list : byte 1*/
NdefMap->SendRecvBuf[BufIndex] = 0x80;
BufIndex++;
/* No. Of Blocks*/
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(NdefMap->Felica.CurBlockNo + i);
BufIndex++;
}
}
/* len of entire pkt*/
NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = (uint8_t) BufIndex;
/* Set the Pkt Len*/
NdefMap->SendLength = BufIndex;
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
TrxStatus = phFriNfc_OvrHal_Transceive(NdefMap->LowerDevice,
&NdefMap->MapCompletionInfo,
NdefMap->psRemoteDevInfo,
NdefMap->Cmd,
&NdefMap->psDepAdditionalInfo,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvBuf,
NdefMap->SendRecvLength);
return (TrxStatus);
}
/*!
* \brief Initiates Writing of NDEF information to the Remote Device.
*
* The function initiates the writing of NDEF information to a Remote Device.
* 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.
*/
NFCSTATUS phFriNfc_Felica_WrNdef( phFriNfc_NdefMap_t *NdefMap,
uint8_t *PacketData,
uint32_t *PacketDataLength,
uint8_t Offset)
{
NFCSTATUS status = NFCSTATUS_PENDING;
NdefMap->ApduBufferSize = *PacketDataLength;
/*Store the packet data buffer*/
NdefMap->ApduBuffer = PacketData;
/* To Update the Acutal written bytes to context*/
NdefMap->WrNdefPacketLength = PacketDataLength;
*NdefMap->WrNdefPacketLength = 0;
NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE;
NdefMap->Felica.Offset = Offset;
NdefMap->Felica.OpFlag = PH_FRINFC_NDEFMAP_FELI_WR_ATTR_RD_OP;
status = phFriNfc_Felica_HRdAttrInfo(NdefMap);
/* handle the error in Transc function*/
if ( (status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_CHK_NDEF,status);
}
return (status);
}
/*!
* \brief Initiates Writing of Empty NDEF information to the Remote Device.
*
* The function initiates the writing empty of NDEF information to a Remote Device.
* 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.
*/
NFCSTATUS phFriNfc_Felica_EraseNdef( phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
static uint32_t PktDtLength =0;
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_READ_ONLY )
{
/*Can't write to the card :No Grants */
status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
NFCSTATUS_WRITE_FAILED);
/* set the no. bytes written is zero*/
NdefMap->WrNdefPacketLength = &PktDtLength;
*NdefMap->WrNdefPacketLength = 0;
}
else
{
/* set the Operation*/
NdefMap->Felica.OpFlag = PH_FRINFC_NDEFMAP_FELI_WR_EMPTY_MSG_OP;
status = phFriNfc_Felica_HRdAttrInfo(NdefMap);
}
return (status);
}
/*!
* \brief Used in Write Opearation.
* check the value set for the Write Flag, in first write operation(begin), sets the
* WR flag in attribute blck.
* After a successful write operation, This function sets the WR flag off and updates
* the LEN bytes in attribute Block.
*/
static NFCSTATUS phFriNfc_Felica_HUpdateAttrBlkForWrOp(phFriNfc_NdefMap_t *NdefMap,uint8_t isStarted)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint16_t ChkSum=0,index=0;
uint8_t BufIndex=0, ErrFlag = FALSE;
uint32_t TotNoWrittenBytes=0;
/* Write Operation : Begin/End Check*/
NdefMap->State =
(( isStarted == FELICA_WRITE_STARTED )?
PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_BEGIN:
PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_END);
if( ( NdefMap->State == PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_BEGIN)||
( NdefMap->State == PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_END) )
{
/* Set the Felica Cmd*/
#ifdef PH_HAL4_ENABLE
NdefMap->Cmd.FelCmd = phHal_eFelica_Raw;
#else
NdefMap->Cmd.FelCmd = phHal_eFelicaCmdListFelicaCmd;
#endif /* #ifdef PH_HAL4_ENABLE */
/* 1st byte represents the length of the cmd packet*/
NdefMap->SendRecvBuf[BufIndex] = 0x00;
BufIndex++;
/* Write/Update command code*/
NdefMap->SendRecvBuf[BufIndex] = 0x08;
BufIndex++;
/* IDm - Manufacturer Id : 8bytes*/
#ifdef PH_HAL4_ENABLE
(void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])),
(void*)(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm)),
8);
#else
(void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])),
(void*)(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo212_424.Startup212_424.NFCID2t)),
8);
#endif /* #ifdef PH_HAL4_ENABLE */
BufIndex+=8;
NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Services (n=1 ==> 0x80)*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x09; /* Service Code List*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /* Service Code List*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Blocks to Write*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x80; /* 1st Block Element : byte 1*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /* 1st Block Element : byte 2, block 1*/
BufIndex++;
/* Fill Attribute Blk Information*/
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Version;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Nbr;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Nbw;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)((NdefMap->FelicaAttrInfo.Nmaxb) >> 8);
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)((NdefMap->FelicaAttrInfo.Nmaxb) & (0x00ff));
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/
BufIndex++;
if (isStarted == FELICA_WRITE_STARTED )
{
NdefMap->SendRecvBuf[BufIndex] = 0x0F; /* Write Flag Made On*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.RdWrFlag; /* Read write flag*/
BufIndex++;
/* Len Bytes*/
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.LenBytes[0];
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.LenBytes[1];
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.LenBytes[2];
BufIndex++;
}
else
{
/* Case: Previous Write Operation failed and integration context continues with write
operation with offset set to Current. In this case, if we find Internal Bytes remained in the
felica context is true(>0) and current block number is Zero. Then we shouldn't allow the module
to write the data to card, as this is a invalid case*/
if ( (NdefMap->Felica.Wr_BytesRemained > 0) && (NdefMap->Felica.CurBlockNo == 0))
{
status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_PARAMETER);
ErrFlag = TRUE;
}
else
{
NdefMap->SendRecvBuf[BufIndex] = 0x00; /* Write Flag Made Off*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.RdWrFlag; /* Read write flag*/
BufIndex++;
if ( NdefMap->Felica.Wr_BytesRemained > 0 )
{
TotNoWrittenBytes = ( (NdefMap->Felica.CurBlockNo *16)- (16 - (NdefMap->Felica.Wr_BytesRemained)));
}
else
{
TotNoWrittenBytes = ( NdefMap->Felica.CurBlockNo *16);
}
/* Update Len Bytes*/
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(( TotNoWrittenBytes & 0x00ff0000) >> 16);
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)((TotNoWrittenBytes & 0x0000ff00) >> 8);
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(TotNoWrittenBytes & 0x000000ff);
BufIndex++;
}
}
if ( ErrFlag != TRUE )
{
/* check sum update*/
for ( index = 16 ; index < 30 ; index ++)
{
ChkSum += NdefMap->SendRecvBuf[index];
}
/* fill check sum in command pkt*/
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(ChkSum >> 8);
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = (uint8_t )(ChkSum & 0x00ff);
BufIndex++;
/* update length of the cmd pkt*/
NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex;
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
/* Update the Send Len*/
NdefMap->SendLength = BufIndex;
/*set the completion routines for the desfire card operations*/
NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_NdefMap_Process;
NdefMap->MapCompletionInfo.Context = NdefMap;
/*set the additional informations for the data exchange*/
NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0;
/*Call the Overlapped HAL Transceive function */
status = phFriNfc_OvrHal_Transceive( NdefMap->LowerDevice,
&NdefMap->MapCompletionInfo,
NdefMap->psRemoteDevInfo,
NdefMap->Cmd,
&NdefMap->psDepAdditionalInfo,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvBuf,
NdefMap->SendRecvLength);
}
}
else
{
status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_PARAMETER);
}
return (status);
}
static NFCSTATUS phFriNfc_Felica_HWrEmptyMsg(phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint16_t ChkSum=0,index=0;
uint8_t BufIndex=0;
/* Write Operation : To Erase the present NDEF Data*/
NdefMap->State = PH_NFCFRI_NDEFMAP_FELI_STATE_WR_EMPTY_MSG;
/* Set the Felica Cmd*/
#ifdef PH_HAL4_ENABLE
NdefMap->Cmd.FelCmd = phHal_eFelica_Raw;
#else
NdefMap->Cmd.FelCmd = phHal_eFelicaCmdListFelicaCmd;
#endif /* #ifdef PH_HAL4_ENABLE */
/* 1st byte represents the length of the cmd packet*/
NdefMap->SendRecvBuf[BufIndex] = 0x00;
BufIndex++;
/* Write/Update command code*/
NdefMap->SendRecvBuf[BufIndex] = 0x08;
BufIndex++;
/* IDm - Manufacturer Id : 8bytes*/
#ifdef PH_HAL4_ENABLE
(void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])),
(void*)(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm)),
8);
#else
(void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])),
(void*)(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo212_424.Startup212_424.NFCID2t)),
8);
#endif /* #ifdef PH_HAL4_ENABLE */
BufIndex+=8;
NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Services (n=1 ==> 0x80)*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x09; /* Service Code List*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /* Service Code List*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Blocks to Write*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x80; /* 1st Block Element : byte 1*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /* 1st Block Element : byte 2, block 1*/
BufIndex++;
/* Fill Attribute Blk Information*/
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Version;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Nbr;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Nbw;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)((NdefMap->FelicaAttrInfo.Nmaxb) >> 8);
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)((NdefMap->FelicaAttrInfo.Nmaxb) & (0x00ff));
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.WriteFlag;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.RdWrFlag; /* Read write flag*/
BufIndex++;
/* Len Bytes are set to 0 : Empty Msg*/
NdefMap->SendRecvBuf[BufIndex] = 0x00;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00;
BufIndex++;
/* check sum update*/
for ( index = 16 ; index < 30 ; index ++)
{
ChkSum += NdefMap->SendRecvBuf[index];
}
/* fill check sum in command pkt*/
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(ChkSum >> 8);
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = (uint8_t )(ChkSum & 0x00ff);
BufIndex++;
/* update length of the cmd pkt*/
NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex;
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
/* Update the Send Len*/
NdefMap->SendLength = BufIndex;
/*set the completion routines for the desfire card operations*/
NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_NdefMap_Process;
NdefMap->MapCompletionInfo.Context = NdefMap;
/*set the additional informations for the data exchange*/
NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0;
/*Call the Overlapped HAL Transceive function */
status = phFriNfc_OvrHal_Transceive( NdefMap->LowerDevice,
&NdefMap->MapCompletionInfo,
NdefMap->psRemoteDevInfo,
NdefMap->Cmd,
&NdefMap->psDepAdditionalInfo,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvBuf,
NdefMap->SendRecvLength);
return (status);
}
/*!
* \brief Used in Write Opearation.
* This Function is called after a successful validation and storing of attribution block
* content in to context.
* If the write operation is initiated with begin,function initiates the write operation with
* RdWr flag.
* If the Offset is set to Current, Checks for the EOF card reached status and writes data to
* The Card
*/
static NFCSTATUS phFriNfc_Felica_HChkAttrBlkForWrOp(phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint32_t DataLen=0;
/*check RW Flag Access Rights*/
/* set to read only cannot write*/
if ( NdefMap->FelicaAttrInfo.RdWrFlag == 0x00)
{
status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_DEVICE_REQUEST);
}
else
{
if ( ( NdefMap->Felica.Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) ||
( ( NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_READ_OPE) &&
(NdefMap->Felica.Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ) ))
{
/* check allready written number of bytes and apdu buffer size*/
if (NdefMap->ApduBufferSize > (uint32_t)(NdefMap->FelicaAttrInfo.Nmaxb *16))
{
NdefMap->Felica.EofCardReachedFlag = FELICA_EOF_REACHED_WR_WITH_BEGIN_OFFSET;
}
else
{
NdefMap->Felica.EofCardReachedFlag = FALSE;
}
/* reset the internal variables initiate toupdate the attribute blk*/
NdefMap->Felica.Wr_BytesRemained = 0;
NdefMap->Felica.CurBlockNo = 0;
NdefMap->Felica.NoBlocksWritten = 0;
phFriNfc_Felica_HInitInternalBuf(NdefMap->Felica.Wr_RemainedBytesBuff);
status= phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_STARTED);
}
else
{
if (NdefMap->Felica.Offset == PH_FRINFC_NDEFMAP_SEEK_CUR )
{
/* Calculate the Allready Written No. Of Blocks*/
PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0],
NdefMap->FelicaAttrInfo.LenBytes[1],
NdefMap->FelicaAttrInfo.LenBytes[2],
DataLen);
if (( NdefMap->ApduBufferSize + (DataLen )) >
(uint32_t)( NdefMap->FelicaAttrInfo.Nmaxb *16))
{
if(( DataLen ) == (uint32_t)(NdefMap->FelicaAttrInfo.Nmaxb *16) )
{
status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_EOF_NDEF_CONTAINER_REACHED);
}
else
{
NdefMap->Felica.EofCardReachedFlag =FELICA_EOF_REACHED_WR_WITH_CURR_OFFSET;
NdefMap->ApduBuffIndex =0;
NdefMap->Felica.NoBlocksWritten = 0;
status= phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_STARTED);
}
}
else
{
NdefMap->ApduBuffIndex =0;
NdefMap->Felica.NoBlocksWritten = 0;
status= phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_STARTED);
}
}/*if (NdefMap->Felica.Offset == PH_FRINFC_NDEFMAP_SEEK_CUR )*/
}
}
return (status);
}
/*!
* \brief Used in Read Opearation.
* This Function is called after a successful validation and storing of attribution block
* content in to context.
* While Offset is set to Current, Checks for the EOF card reached status and reads data from
* The Card
*/
static NFCSTATUS phFriNfc_Felica_HChkAttrBlkForRdOp(phFriNfc_NdefMap_t *NdefMap,
uint32_t NdefLen)
{
NFCSTATUS status = NFCSTATUS_PENDING;
/*check WR Flag Access Rights*/
/* set to still writing data state only cannot Read*/
if ( NdefMap->FelicaAttrInfo.WriteFlag == 0x0F )
{
status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_READ_FAILED);
/* As we are not able to continue with reading data
bytes read set to zero*/
*NdefMap->NumOfBytesRead = 0;
}
else
{
status = phFriNfc_MapTool_SetCardState( NdefMap,NdefLen);
if ( status == NFCSTATUS_SUCCESS)
{
/* Read data From the card*/
status = phFriNfc_Felica_HReadData(NdefMap,NdefMap->Felica.Offset);
}
}
return (status);
}
/*!
* \brief Used in Write Opearation.
* This function writes the data in terms of blocks.
* Each write operation supports,minimum Nbw blocks of bytes.
* Also checks for the EOF,>=NBW,<NBW etc
*/
static NFCSTATUS phFriNfc_Felica_HUpdateData(phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint8_t BufIndex=0,
i=0,
BlkNo=0,
PadBytes=0,
CurBlk=1,
NoOfBlks=0,
NbwCheck=0,
TotNoBlks=0;
uint32_t BytesRemainedInCard=0,
BytesRemained=0,
TotNoWrittenBytes=0;
if( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) > 0 )
{
/* Prepare the write cmd pkt for felica*/
/* 1st byte represents the length of the cmd packet*/
NdefMap->SendRecvBuf[BufIndex] = 0x00;
BufIndex++;
/* Write/Update command code*/
NdefMap->SendRecvBuf[BufIndex] = 0x08;
BufIndex++;
/* IDm - Manufacturer Id : 8bytes*/
#ifdef PH_HAL4_ENABLE
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm)),
8);
#else
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo212_424.Startup212_424.NFCID2t)),
8);
#endif /* #ifdef PH_HAL4_ENABLE */
BufIndex+=8;
NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Services (n=1 ==> 0x80)*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x09; /* Service Code List*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /* Service Code List*/
BufIndex++;
if ( NdefMap->Felica.EofCardReachedFlag == FELICA_EOF_REACHED_WR_WITH_BEGIN_OFFSET)
{
/* check for the eof card reached flag.Need to write only mamximum bytes(memory)to card.
Used when, offset set to begin case*/
BytesRemainedInCard= ( (NdefMap->FelicaAttrInfo.Nmaxb*16) - (NdefMap->Felica.CurBlockNo * 16));
}
else
{
/* Offset : Cuurent*/
if ( NdefMap->Felica.EofCardReachedFlag == FELICA_EOF_REACHED_WR_WITH_CURR_OFFSET )
{
/* caculate previously written Ndef blks*/
(void)phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC);
if ( NdefMap->Felica.Wr_BytesRemained )
{
TotNoWrittenBytes = ( (NdefMap->Felica.CurBlockNo *16)- (16 - (NdefMap->Felica.Wr_BytesRemained)));
}
else
{
TotNoWrittenBytes = ( NdefMap->Felica.CurBlockNo *16);
}
/* Determine exactly, how many bytes we can write*/
BytesRemainedInCard = (NdefMap->FelicaAttrInfo.Nmaxb*16 - (TotNoWrittenBytes));
}
}
/* Write Data Pending in the Internal Buffer*/
if(NdefMap->Felica.Wr_BytesRemained > 0)
{
/* update the number of blocks to write with the block list elements*/
/* Total Number of blocks to write*/
NdefMap->SendRecvBuf[BufIndex] = 0;
BufIndex++;
/* Update this Total no. Bloks later*/
NoOfBlks = BufIndex;
/* As we are writing atleast one block*/
TotNoBlks = 1;
/* check do we have some extra bytes to write? in User Buffer*/
if ( NdefMap->ApduBufferSize >(uint32_t) (16 - NdefMap->Felica.Wr_BytesRemained))
{
/* Have we reached EOF?*/
if ( NdefMap->Felica.EofCardReachedFlag )
{
BytesRemained = BytesRemainedInCard;
}
else
{
/* This value tells how many extra bytes we can write other than internal buffer bytes*/
BytesRemained = (uint8_t)NdefMap->ApduBufferSize - (16 - NdefMap->Felica.Wr_BytesRemained);
}
if ( BytesRemained )
{
/* Not reached EOF*/
if (!NdefMap->Felica.EofCardReachedFlag)
{
/* Calculate How many blks we need to write*/
BlkNo =((uint8_t)( BytesRemained )/16);
/* check blocks to write exceeds nbw*/
if ( BlkNo >= NdefMap->FelicaAttrInfo.Nbw )
{
BlkNo = NdefMap->FelicaAttrInfo.Nbw;
/* No. Blks to write are more than Nbw*/
NbwCheck = 1;
}
else
{
if ((( BytesRemained %16) == 0)&& (BlkNo == 0 ))
{
BlkNo=1;
}
}
/* check do we need pad bytes?*/
if( (!NbwCheck && (uint8_t)( BytesRemained)%16) != 0)
{
BlkNo++;
PadBytes = (BlkNo * 16) - (uint8_t)( BytesRemained);
NdefMap->Felica.PadByteFlag = TRUE;
NdefMap->Felica.NoBlocksWritten = BlkNo;
TotNoBlks += BlkNo;
}
else
{
if ( NbwCheck )
{
/* as we have to write only 8 blocks and already we have pad bytes so we have
to strat from previous block*/
TotNoBlks += BlkNo - 1;
NdefMap->Felica.NoBlocksWritten = TotNoBlks-1;
}
else
{
if ( !(BytesRemained - (16 -NdefMap->Felica.Wr_BytesRemained)== 0 ))
{
TotNoBlks += BlkNo;
}
else
{
}
if ( NdefMap->Felica.PadByteFlag )
{
NdefMap->Felica.NoBlocksWritten = TotNoBlks-1;
}
}
}
}
else
{
/* we have reached the eof card & hv bytes to write*/
BlkNo =(uint8_t)(( BytesRemained - ((16 -NdefMap->Felica.Wr_BytesRemained)) )/16);
/* check are we exceeding the NBW limit, while a write?*/
if ( BlkNo >= NdefMap->FelicaAttrInfo.Nbw )
{
BlkNo = NdefMap->FelicaAttrInfo.Nbw;
/* No. Blks to write are more than Nbw*/
NbwCheck = 1;
}
else
{
if ((( BytesRemained %16) == 0)&& (BlkNo == 0 ))
{
BlkNo=1;
}
}
/*check Total how many blocks to write*/
if(((!NbwCheck) &&( BytesRemained- (16 - NdefMap->Felica.Wr_BytesRemained))%16) != 0)
{
BlkNo++;
PadBytes = (BlkNo * 16) - (uint8_t)( BytesRemained);
NdefMap->Felica.PadByteFlag = TRUE;
NdefMap->Felica.NoBlocksWritten = BlkNo;
TotNoBlks += BlkNo;
}
else
{
if ( NbwCheck )
{
/* as we have to write only 8 blocks and already we have pad bytes so we have
to strat from previous last block*/
TotNoBlks += BlkNo - 1;
NdefMap->Felica.NoBlocksWritten = TotNoBlks-1;
}
else
{
/* we need to write only one block ( bytesremanind + internal buffer size = 16)*/
if ( !(BytesRemained - (16 -NdefMap->Felica.Wr_BytesRemained)== 0 ))
{
TotNoBlks += BlkNo;
}
else
{
;/* we are not incrementing the Total no. of blocks to write*/
}
if ( NdefMap->Felica.PadByteFlag )
{
NdefMap->Felica.NoBlocksWritten = TotNoBlks -1;
}
}
}
}
}/*if ( BytesRemained )*/
else
{
; /*Nothing to process here*/
}
}/*if ( NdefMap->ApduBufferSize >(uint32_t) (16 - NdefMap->Felica.Wr_BytesRemained))*/
else
{
/* No new blks to write*/
NdefMap->Felica.NoBlocksWritten = 0;
}
/* Prepare the Blk List for Write Operation*/
/* Block List for NBw : 1st byte : two byte len list: 2nd byte is the block number*/
for ( i=0; i< TotNoBlks; i++)
{
NdefMap->SendRecvBuf[BufIndex] = 0x80;
BufIndex++;
/* remember the previous Blk no and continue from there*/
if ( NdefMap->Felica.PadByteFlag == TRUE )
{
NdefMap->SendRecvBuf[BufIndex] = NdefMap->Felica.CurBlockNo + i;
BufIndex++;
}
else
{
CurBlk = NdefMap->Felica.CurBlockNo +1;
NdefMap->SendRecvBuf[BufIndex] = CurBlk + i;
BufIndex++;
}
}
/* Copy relevant data to Transc buffer*/
if((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= (uint32_t)(16 - NdefMap->Felica.Wr_BytesRemained))
{
/*Copy the Remained bytes from the internal buffer to trxbuffer */
(void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])),
NdefMap->Felica.Wr_RemainedBytesBuff,
NdefMap->Felica.Wr_BytesRemained);
/*Increment the buff index*/
BufIndex += NdefMap->Felica.Wr_BytesRemained;
/*append copy 16-bytesToPad to trxBuffer*/
(void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(16 - NdefMap->Felica.Wr_BytesRemained));
/* Update Number Of Bytes Writtened*/
NdefMap->NumOfBytesWritten = 16 - NdefMap->Felica.Wr_BytesRemained;
/* increment the index*/
BufIndex += 16 - NdefMap->Felica.Wr_BytesRemained;
if ( BytesRemained )
{
if (!NdefMap->Felica.EofCardReachedFlag)
{
/* check nbw limit*/
if ( NbwCheck != 1 )
{
/* Copy Extra Bytes other than the internal buffer bytes*/
(void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->ApduBuffer[(16 - NdefMap->Felica.Wr_BytesRemained)])),
(NdefMap->ApduBufferSize - (16 - NdefMap->Felica.Wr_BytesRemained)));
/* Update Number Of Bytes Writtened*/
NdefMap->NumOfBytesWritten += (uint16_t)(NdefMap->ApduBufferSize - (16 - NdefMap->Felica.Wr_BytesRemained));
BufIndex += (uint8_t)(NdefMap->ApduBufferSize - (16 - NdefMap->Felica.Wr_BytesRemained));
if ( PadBytes )
{
for(i= 0; i< PadBytes; i++)
{
NdefMap->SendRecvBuf[BufIndex] =0x00;
BufIndex++;
}
/* no of bytes remained copy*/
NdefMap->Felica.Wr_BytesRemained = (uint8_t)(16 - PadBytes);
/*copy the data to internal buffer : Bytes remained*/
(void)memcpy( NdefMap->Felica.Wr_RemainedBytesBuff,
(&( NdefMap->ApduBuffer[(NdefMap->ApduBufferSize - NdefMap->Felica.Wr_BytesRemained)])),
( NdefMap->Felica.Wr_BytesRemained));
}
else
{
/* No Bytes in Internal buffer*/
NdefMap->Felica.Wr_BytesRemained = 0;
}
}
else
{
/*Copy Nbw*16 bytes of data to the trx buffer*/
(void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->ApduBuffer[(16 - NdefMap->Felica.Wr_BytesRemained)])),
(NdefMap->FelicaAttrInfo.Nbw - 1) * 16);
/* increment the Buffindex*/
BufIndex += ((NdefMap->FelicaAttrInfo.Nbw - 1 )*16);
NdefMap->Felica.Wr_BytesRemained = 0;
NdefMap->NumOfBytesWritten+= ((NdefMap->FelicaAttrInfo.Nbw -1)*16);
NdefMap->Felica.PadByteFlag =FALSE;
}
}/*if (!NdefMap->Felica.EofCardReachedFlag)*/
else
{
/* check nbw limit*/
if ( NbwCheck != 1 )
{
/* handle EOF card reached case*/
(void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->ApduBuffer[(16 - NdefMap->Felica.Wr_BytesRemained)])),
( BytesRemained - ((16 -NdefMap->Felica.Wr_BytesRemained) )));
/* Update Number Of Bytes Writtened*/
NdefMap->NumOfBytesWritten += (uint16_t)( BytesRemained - (16 -NdefMap->Felica.Wr_BytesRemained));
BufIndex += (uint8_t)( BytesRemained - (16 -NdefMap->Felica.Wr_BytesRemained));
if ( PadBytes )
{
for(i= 0; i< PadBytes; i++)
{
NdefMap->SendRecvBuf[BufIndex] =0x00;
BufIndex++;
}
/*no of bytes remained copy*/
NdefMap->Felica.Wr_BytesRemained = (uint8_t)(16 - PadBytes);
/*copy the data to internal buffer : Bytes remained*/
(void)memcpy(NdefMap->Felica.Wr_RemainedBytesBuff,
(&(NdefMap->ApduBuffer[(NdefMap->ApduBufferSize - NdefMap->Felica.Wr_BytesRemained)])),
(NdefMap->Felica.Wr_BytesRemained));
}
else
{
NdefMap->Felica.Wr_BytesRemained = 0;
}
}
else
{
/*Copy Nbw*16 bytes of data to the trx buffer*/
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->ApduBuffer[(16 - NdefMap->Felica.Wr_BytesRemained)])),
(NdefMap->FelicaAttrInfo.Nbw - 1) * 16);
/* increment the Buffindex*/
BufIndex += ((NdefMap->FelicaAttrInfo.Nbw - 1 )*16);
NdefMap->Felica.Wr_BytesRemained = 0;
NdefMap->NumOfBytesWritten+= ((NdefMap->FelicaAttrInfo.Nbw -1)*16);
NdefMap->Felica.PadByteFlag =FALSE;
}
}
}/*if ( BytesRemained )*/
else
{
NdefMap->Felica.Wr_BytesRemained = 0;
}
/* Update Total No. of blocks writtened*/
NdefMap->SendRecvBuf[NoOfBlks -1 ]=TotNoBlks;
}/*if((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= (uint32_t)(16 - NdefMap->Felica.Wr_BytesRemained))*/
else
{
/*copy the internal buffer data to trx buffer*/
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
NdefMap->Felica.Wr_RemainedBytesBuff,
(NdefMap->Felica.Wr_BytesRemained));
/* increment the index*/
BufIndex+=NdefMap->Felica.Wr_BytesRemained;
/*append the apdusize data to the trx buffer*/
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
NdefMap->ApduBufferSize);
/* Index increment*/
BufIndex+= (uint8_t)NdefMap->ApduBufferSize;
/* Tells how many bytes present in the internal buffer*/
BytesRemained = NdefMap->Felica.Wr_BytesRemained + NdefMap->ApduBufferSize;
PadBytes = (uint8_t)(16-BytesRemained);
/* Pad empty bytes with Zeroes to complete 16 bytes*/
for(i= 0; i< PadBytes; i++)
{
NdefMap->SendRecvBuf[BufIndex] =0x00;
BufIndex++;
}
/* Update Number Of Bytes Writtened*/
NdefMap->NumOfBytesWritten = (uint16_t)NdefMap->ApduBufferSize;
/* Flag set to understand that , we have received less no. of bytes than
present in the internal buffer*/
NdefMap->Felica.IntermediateWrFlag = TRUE;
if ( NdefMap->Felica.PadByteFlag )
{
NdefMap->Felica.NoBlocksWritten = 0;
}
}
NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex;
NdefMap->SendLength = BufIndex;
/* Update Total No. of blocks writtened*/
NdefMap->SendRecvBuf[NoOfBlks -1 ]=TotNoBlks;
}
else
{
/*Fresh write, starting from a new block*/
if ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= (uint32_t)(16* NdefMap->FelicaAttrInfo.Nbw ))
{
/* check for the card size and write Nbw Blks*/
if ( NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo >= NdefMap->FelicaAttrInfo.Nbw)
{
/* update the number of blocks to write with the block list elements*/
/* Total Number of blocks to write*/
NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Nbw;
BufIndex++;
/* Block List for NBw : 1st byte : two byte len list: 2nd byte is the block number*/
for ( i=1; i<= NdefMap->FelicaAttrInfo.Nbw; i++)
{
NdefMap->SendRecvBuf[BufIndex] = 0x80;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->Felica.CurBlockNo + i;
BufIndex++;
}
/*Copy Nbw*16 bytes of data to the trx buffer*/
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
NdefMap->FelicaAttrInfo.Nbw * 16);
/* increment the Buffindex*/
BufIndex += (NdefMap->FelicaAttrInfo.Nbw*16);
/* update the length*/
NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex;
NdefMap->Felica.Wr_BytesRemained = 0;
NdefMap->NumOfBytesWritten = (NdefMap->FelicaAttrInfo.Nbw*16);
NdefMap->Felica.NoBlocksWritten = NdefMap->FelicaAttrInfo.Nbw;
/* update the Send length*/
NdefMap->SendLength = BufIndex;
NdefMap->Felica.PadByteFlag = FALSE;
}/*if ( NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo >= NdefMap->FelicaAttrInfo.Nbw)*/
else
{
/* we need to write less than nbw blks*/
/* update the number of blocks to write with the block list elements*/
/* Total Number of blocks to write*/
NdefMap->SendRecvBuf[BufIndex] = (uint8_t)( NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo);
BufIndex++;
/* Block List for NBw : 1st byte : two byte len list: 2nd byte is the block number*/
for ( i=1; i<= (NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo); i++)
{
NdefMap->SendRecvBuf[BufIndex] = 0x80;
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = NdefMap->Felica.CurBlockNo + i;
BufIndex++;
}
/*Copy Nbw*16 bytes of data to the trx buffer*/
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo)*16);
/* increment the Buffindex*/
BufIndex += (uint8_t)((NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo )*16);
/* update the length*/
NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex;
NdefMap->NumOfBytesWritten = ((NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo)*16);
NdefMap->Felica.NoBlocksWritten = (uint8_t)(NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo);
/* update the Send length*/
NdefMap->SendLength = BufIndex;
NdefMap->Felica.PadByteFlag =FALSE;
NdefMap->Felica.Wr_BytesRemained = 0;
}
}/* if ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= (uint32_t)(16* NdefMap->FelicaAttrInfo.Nbw )) */
else
{
/*chk eof reached*/
if ( NdefMap->Felica.EofCardReachedFlag)
{
BlkNo =((uint8_t)(BytesRemainedInCard )/16);
if(((uint8_t)( BytesRemainedInCard )%16) != 0)
{
BlkNo++;
PadBytes = ((BlkNo * 16) - (uint8_t)(BytesRemainedInCard ));
NdefMap->Felica.PadByteFlag = TRUE;
}
}
else
{
BlkNo =((uint8_t)( NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)/16);
if(((uint8_t)( NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)%16) != 0)
{
BlkNo++;
PadBytes = (BlkNo * 16) - (uint8_t)( NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
NdefMap->Felica.PadByteFlag = TRUE;
}
}
/* update the number of blocks to write with the block list elements*/
/* Total Number of blocks to write*/
NdefMap->SendRecvBuf[BufIndex] = BlkNo;
BufIndex++;
NdefMap->Felica.NoBlocksWritten = BlkNo;
/* Block List for NBw : 1st byte : two byte len list: 2nd byte is the block number*/
for ( i=0; i< BlkNo; i++)
{
NdefMap->SendRecvBuf[BufIndex] = 0x80;
BufIndex++;
{
CurBlk = NdefMap->Felica.CurBlockNo +1;
NdefMap->SendRecvBuf[BufIndex] = CurBlk + i;
BufIndex++;
}
}
if ( NdefMap->Felica.EofCardReachedFlag )
{
/*Copy last data to the trx buffer*/
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
BytesRemainedInCard );
/* increment the bufindex and bytes written*/
BufIndex += (uint8_t )BytesRemainedInCard ;
NdefMap->NumOfBytesWritten = (uint16_t)BytesRemainedInCard ;
}
else
{
/*Copy data to the trx buffer*/
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])),
(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex));
/* increment the bufindex and bytes written*/
BufIndex += (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
NdefMap->NumOfBytesWritten = (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
}
if ( PadBytes )
{
for(i= 0; i< PadBytes; i++)
{
NdefMap->SendRecvBuf[BufIndex] =0x00;
BufIndex++;
}
/*no of bytes remained copy*/
NdefMap->Felica.Wr_BytesRemained = (uint8_t)(16 - PadBytes);
if ( NdefMap->Felica.EofCardReachedFlag )
{
/*copy the data to internal buffer : Bytes remained*/
(void)memcpy(NdefMap->Felica.Wr_RemainedBytesBuff,
(&(NdefMap->ApduBuffer[((BytesRemainedInCard - (BytesRemainedInCard % 16)))])),
( NdefMap->Felica.Wr_BytesRemained));
}
else
{
/*copy the data to internal buffer : Bytes remained*/
(void)memcpy( NdefMap->Felica.Wr_RemainedBytesBuff,
(&(NdefMap->ApduBuffer[((NdefMap->ApduBufferSize - NdefMap->Felica.Wr_BytesRemained))])),
( NdefMap->Felica.Wr_BytesRemained));
}
}/*if ( PadBytes )*/
else
{
NdefMap->Felica.Wr_BytesRemained = 0;
NdefMap->Felica.PadByteFlag = FALSE;
}
/* update the pkt len*/
NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex;
NdefMap->SendLength = BufIndex;
}
}/* else of if ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= (uint32_t)(16* NdefMap->FelicaAttrInfo.Nbw )) */
status = phFriNfc_Felica_HWriteDataBlk(NdefMap);
}
else
{
/*0 represents the write operation ended*/
status = phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_ENDED);
}
return (status);
}
/*!
* \brief Used in Write Opearation.
* This function prepares and sends transcc Cmd Pkt.
*/
static NFCSTATUS phFriNfc_Felica_HWriteDataBlk(phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
/*set the additional informations for the data exchange*/
NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0;
/*Set the ISO14434 command*/
#ifdef PH_HAL4_ENABLE
NdefMap->Cmd.FelCmd = phHal_eFelica_Raw;
#else
NdefMap->Cmd.FelCmd = phHal_eFelicaCmdListFelicaCmd;
#endif /* #ifdef PH_HAL4_ENABLE */
/* set the state*/
NdefMap->State = PH_NFCFRI_NDEFMAP_FELI_STATE_WR_BLOCK;
/* set send receive length*/
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
/*Call the Overlapped HAL Transceive function */
status = phFriNfc_OvrHal_Transceive( NdefMap->LowerDevice,
&NdefMap->MapCompletionInfo,
NdefMap->psRemoteDevInfo,
NdefMap->Cmd,
&NdefMap->psDepAdditionalInfo,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvBuf,
NdefMap->SendRecvLength);
return (status);
}
/*!
* \brief Check whether a particular Remote Device is NDEF compliant.
* The function checks whether the peer device is NDEF compliant.
*/
NFCSTATUS phFriNfc_Felica_ChkNdef( phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint8_t sysCode[2];
/* set the system code for selecting the wild card*/
sysCode[0] = 0x12;
sysCode[1] = 0xFC;
status = phFriNfc_Felica_HPollCard( NdefMap,sysCode,PH_NFCFRI_NDEFMAP_FELI_STATE_SELECT_NDEF_APP);
return (status);
}
/*!
* \brief Check whether a particular Remote Device is NDEF compliant.
* selects the sysCode and then NFC Forum Reference Applications
*/
#ifdef PH_HAL4_ENABLE
static NFCSTATUS phFriNfc_Felica_HPollCard( phFriNfc_NdefMap_t *NdefMap,
const uint8_t sysCode[],
uint8_t state)
{
NFCSTATUS status = NFCSTATUS_PENDING;
/*Format the Poll Packet for selecting the system code passed as parameter */
NdefMap->SendRecvBuf[0] = 0x06;
NdefMap->SendRecvBuf[1] = 0x00;
NdefMap->SendRecvBuf[2] = sysCode[0];
NdefMap->SendRecvBuf[3] = sysCode[1];
NdefMap->SendRecvBuf[4] = 0x01;
NdefMap->SendRecvBuf[5] = 0x03;
NdefMap->SendLength = 6;
/*set the completion routines for the felica card operations*/
NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_Felica_Process;
NdefMap->MapCompletionInfo.Context = NdefMap;
/*Set Ndef State*/
NdefMap->State = state;
/* set the felica cmd */
NdefMap->Cmd.FelCmd = phHal_eFelica_Raw;
/*set the additional informations for the data exchange*/
NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0;
status = phFriNfc_OvrHal_Transceive(NdefMap->LowerDevice,
&NdefMap->MapCompletionInfo,
NdefMap->psRemoteDevInfo,
NdefMap->Cmd,
&NdefMap->psDepAdditionalInfo,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvBuf,
NdefMap->SendRecvLength);
return (status);
}
#endif
#ifndef PH_HAL4_ENABLE
static NFCSTATUS phFriNfc_Felica_HPollCard( phFriNfc_NdefMap_t *NdefMap,
const uint8_t sysCode[],
uint8_t state)
{
NFCSTATUS status = NFCSTATUS_PENDING;
/*Format the Poll Packet for selecting the wild card "0xff 0xff as system code*/
NdefMap->FelicaPollDetails.DevInputParam->FelicaPollPayload[0] = 0x00;
NdefMap->FelicaPollDetails.DevInputParam->FelicaPollPayload[1] = sysCode[0];
NdefMap->FelicaPollDetails.DevInputParam->FelicaPollPayload[2] = sysCode[1];
NdefMap->FelicaPollDetails.DevInputParam->FelicaPollPayload[3] = 0x01;
NdefMap->FelicaPollDetails.DevInputParam->FelicaPollPayload[4] = 0x03;
/* set the length to zero*/
NdefMap->FelicaPollDetails.DevInputParam->GeneralByteLength =0x00;
NdefMap->NoOfDevices = PH_FRINFC_NDEFMAP_FELI_NUM_DEVICE_TO_DETECT;
/*set the completion routines for the felica card operations*/
NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_Felica_Process;
NdefMap->MapCompletionInfo.Context = NdefMap;
/*Set Ndef State*/
NdefMap->State = state;
/* Harsha: This is a special case for felica.
Make a copy of the remote device information and send it for
polling. Return the original remote device information to the
caller. The user does not need the updated results of the poll
that we are going to call now. This is only used for checking
whether the felica card is NDEF compliant or not. */
(void) memcpy( &NdefMap->FelicaPollDetails.psTempRemoteDevInfo,
NdefMap->psRemoteDevInfo,
sizeof(phHal_sRemoteDevInformation_t));
/* Reset the session opened flag */
NdefMap->FelicaPollDetails.psTempRemoteDevInfo.SessionOpened = 0x00;
/*Call the Overlapped HAL POLL function */
status = phFriNfc_OvrHal_Poll( NdefMap->LowerDevice,
&NdefMap->MapCompletionInfo,
NdefMap->OpModeType,
&NdefMap->FelicaPollDetails.psTempRemoteDevInfo,
&NdefMap->NoOfDevices,
NdefMap->FelicaPollDetails.DevInputParam);
return (status);
}
#endif /* #ifndef PH_HAL4_ENABLE */
/*!
* \brief Checks validity of system code sent from the lower device, during poll operation.
*/
static NFCSTATUS phFriNfc_Felica_HUpdateManufIdDetails(const phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
/* Get the details from Poll Response packet */
if (NdefMap->SendRecvLength >= 20)
{
(void)memcpy( (uint8_t *)NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm,
(uint8_t *)&NdefMap->SendRecvBuf[2], 8);
(void)memcpy( (uint8_t *)NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.PMm,
(uint8_t *)&NdefMap->SendRecvBuf[10], 8);
NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode[1] = NdefMap->SendRecvBuf[18];
NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode[0] = NdefMap->SendRecvBuf[19];
NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDmLength = 8;
/* copy the IDm and PMm in Manufacture Details Structure*/
(void)memcpy( (uint8_t *)(NdefMap->FelicaManufDetails.ManufID),
(uint8_t *)NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm,
8);
(void)memcpy( (uint8_t *)(NdefMap->FelicaManufDetails.ManufParameter),
(uint8_t *)NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.PMm,
8);
if((NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode[1] == 0x12)
&& (NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode[0] == 0xFC))
{
status = PHNFCSTVAL(CID_NFC_NONE, NFCSTATUS_SUCCESS);
}
else
{
status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_NO_NDEF_SUPPORT);
}
}
else
{
status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_NO_NDEF_SUPPORT);
}
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.
*/
void phFriNfc_Felica_Process(void *Context,
NFCSTATUS Status)
{
uint8_t CRFlag = FALSE;
uint16_t RecvTxLen = 0,
BytesToRecv = 0,
Nbc = 0;
uint32_t TotNoWrittenBytes = 0,
NDEFLen=0;
/*Set the context to Map Module*/
phFriNfc_NdefMap_t *NdefMap = (phFriNfc_NdefMap_t *)Context;
if ( Status == NFCSTATUS_SUCCESS )
{
switch (NdefMap->State)
{
case PH_NFCFRI_NDEFMAP_FELI_STATE_SELECT_NDEF_APP:
/* check the ndef compliency with the system code reecived in the RemoteDevInfo*/
Status = phFriNfc_Felica_HUpdateManufIdDetails(NdefMap);
if (Status == NFCSTATUS_SUCCESS)
{
/* Mantis ID : 645*/
/* set the operation type to Check ndef type*/
NdefMap->Felica.OpFlag = PH_FRINFC_NDEFMAP_FELI_CHK_NDEF_OP;
Status = phFriNfc_Felica_HRdAttrInfo(NdefMap);
/* handle the error in Transc function*/
if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
CRFlag = TRUE;
}
}
else
{
CRFlag = TRUE;
}
if ( CRFlag == TRUE )
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_CHK_NDEF,Status);
}
break;
case PH_NFCFRI_NDEFMAP_FELI_STATE_RD_ATTR:
/* check for the status flag1 and status flag2for the successful read operation*/
if ( NdefMap->SendRecvBuf[10] == 0x00)
{
/* check the Manuf Id in the receive buffer*/
Status = phFriNfc_Felica_HCheckManufId(NdefMap);
if ( Status == NFCSTATUS_SUCCESS)
{
/* Update the Attribute Information in to the context structure*/
Status = phFriNfc_Felica_HUpdateAttrInfo(NdefMap);
if ( Status == NFCSTATUS_SUCCESS )
{
PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0],
NdefMap->FelicaAttrInfo.LenBytes[1],
NdefMap->FelicaAttrInfo.LenBytes[2],
NDEFLen);
if ( NdefMap->Felica.OpFlag == PH_FRINFC_NDEFMAP_FELI_WR_ATTR_RD_OP )
{
/* Proceed With Write Functinality*/
Status = phFriNfc_Felica_HChkAttrBlkForWrOp(NdefMap);
/* handle the error in Transc function*/
if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status);
}
}
else if( NdefMap->Felica.OpFlag == PH_FRINFC_NDEFMAP_FELI_RD_ATTR_RD_OP )
{
/* Proceed With Read Functinality*/
Status = phFriNfc_Felica_HChkAttrBlkForRdOp(NdefMap,NDEFLen);
if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_RD_NDEF,Status);
}
}
else if( NdefMap->Felica.OpFlag == PH_FRINFC_NDEFMAP_FELI_CHK_NDEF_OP )
{
Status = phFriNfc_MapTool_SetCardState( NdefMap,
NDEFLen);
/* check status value*/
NdefMap->CardType = PH_FRINFC_NDEFMAP_FELICA_SMART_CARD;
/*reset the buffer index*/
NdefMap->ApduBuffIndex = 0;
/* set the Next operation Flag to indicate need of reading attribute information*/
NdefMap->Felica.OpFlag = PH_FRINFC_NDEFMAP_FELI_OP_NONE;
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_CHK_NDEF,Status);
}
else if ( NdefMap->Felica.OpFlag == PH_FRINFC_NDEFMAP_FELI_WR_EMPTY_MSG_OP )
{
/* Proceed With Write Functinality*/
Status = phFriNfc_Felica_HWrEmptyMsg(NdefMap);
/* handle the error in Transc function*/
if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_ERASE_NDEF,Status);
}
}
else
{
/* invalid operation occured*/
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
NFCSTATUS_INVALID_DEVICE_REQUEST);
CRFlag =TRUE ;
}
}
else
{
CRFlag =TRUE ;
}
}
else
{
CRFlag =TRUE ;
}
}
else
{
CRFlag =TRUE;
/*handle the Error case*/
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
NFCSTATUS_READ_FAILED);
}
if ( CRFlag == TRUE )
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_RD_NDEF,Status);
}
break;
case PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_BEGIN:
/* chk the status flags 1 and 2*/
if ( NdefMap->SendRecvBuf[10] == 0x00 )
{
/* Update Data Call*/
Status =phFriNfc_Felica_HUpdateData(NdefMap);
if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status);
}
}
else
{
/*handle the Error case*/
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
NFCSTATUS_WRITE_FAILED);
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status);
}
break;
case PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_END:
/* chk the status flags 1 and 2*/
if ( NdefMap->SendRecvBuf[10] == 0x00)
{
/* Entire Write Operation is complete*/
Status = PHNFCSTVAL(CID_NFC_NONE,\
NFCSTATUS_SUCCESS);
}
else
{
/*handle the Error case*/
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
NFCSTATUS_WRITE_FAILED);
}
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status);
break;
case PH_NFCFRI_NDEFMAP_FELI_STATE_WR_EMPTY_MSG :
/* chk the status flags 1 and 2*/
if ( NdefMap->SendRecvBuf[10] == 0x00)
{
/* Entire Write Operation is complete*/
Status = PHNFCSTVAL(CID_NFC_NONE,\
NFCSTATUS_SUCCESS);
}
else
{
/*handle the Error case*/
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
NFCSTATUS_WRITE_FAILED);
}
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status);
break;
case PH_NFCFRI_NDEFMAP_FELI_STATE_WR_BLOCK :
if(NdefMap->SendRecvBuf[1] == PH_NFCFRI_NDEFMAP_FELI_WR_RESP_BYTE )
{
/* chk the status flags 1 and 2*/
if ( NdefMap->SendRecvBuf[10] == 0x00 )
{
/* This is used when we have bytes less than 16 bytes*/
if ( NdefMap->Felica.IntermediateWrFlag == TRUE )
{
/* after Successful write copy the last writtened bytes back to the
internal buffer*/
(void)memcpy( (&(NdefMap->Felica.Wr_RemainedBytesBuff[NdefMap->Felica.Wr_BytesRemained])),
NdefMap->ApduBuffer,
NdefMap->NumOfBytesWritten);
NdefMap->Felica.Wr_BytesRemained +=
(uint8_t)( NdefMap->NumOfBytesWritten);
/* Increment the Send Buffer index */
NdefMap->ApduBuffIndex +=
NdefMap->NumOfBytesWritten;
*NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex;
NdefMap->Felica.IntermediateWrFlag = FALSE;
/* Call Update Data()*/
Status = phFriNfc_Felica_HUpdateData(NdefMap);
}
else
{
/* update the index and bytes writtened*/
NdefMap->ApduBuffIndex += NdefMap->NumOfBytesWritten;
*NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex;
if ( NdefMap->Felica.EofCardReachedFlag )
{
if ( NdefMap->Felica.CurBlockNo < NdefMap->FelicaAttrInfo.Nmaxb)
{
NdefMap->Felica.CurBlockNo += NdefMap->Felica.NoBlocksWritten;
}
if (( NdefMap->Felica.CurBlockNo == NdefMap->FelicaAttrInfo.Nmaxb) &&
( NdefMap->ApduBuffIndex == (NdefMap->FelicaAttrInfo.Nmaxb*16)))
{
NdefMap->Felica.EofCardReachedFlag = FELICA_RD_WR_EOF_CARD_REACHED ;
/*0 represents the write ended*/
Status = phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_ENDED);
if( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status);
}
}
else
{
PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0],
NdefMap->FelicaAttrInfo.LenBytes[1],
NdefMap->FelicaAttrInfo.LenBytes[2],
TotNoWrittenBytes);
if ( ( NdefMap->Felica.CurBlockNo == NdefMap->FelicaAttrInfo.Nmaxb) &&
((TotNoWrittenBytes + NdefMap->ApduBuffIndex) == (uint32_t)(NdefMap->FelicaAttrInfo.Nmaxb*16)))
{
NdefMap->Felica.EofCardReachedFlag =FELICA_RD_WR_EOF_CARD_REACHED;
/*0 represents the write ended*/
Status = phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_ENDED);
if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status);
}
}
else
{
/* Call Update Data()*/
Status = phFriNfc_Felica_HUpdateData(NdefMap);
if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status);
}
}
}
}/*if ( NdefMap->Felica.EofCardReachedFlag )*/
else
{
NdefMap->Felica.CurBlockNo += NdefMap->Felica.NoBlocksWritten;
/* Call Update Data()*/
Status = phFriNfc_Felica_HUpdateData(NdefMap);
if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status);
}
}
}
}/*if ( NdefMap->SendRecvBuf[10] == 0x00 )*/
else
{
/*handle the Error case*/
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
NFCSTATUS_WRITE_FAILED);
CRFlag = TRUE;
}
}/*if(NdefMap->SendRecvBuf[1] == PH_NFCFRI_NDEFMAP_FELI_WR_RESP_BYTE )*/
else
{
/*return Error "Invalid Write Response Code"*/
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_WRITE_FAILED);
CRFlag = TRUE;
}
if ( CRFlag == TRUE )
{
/* Reset following parameters*/
NdefMap->ApduBuffIndex=0;
NdefMap->Felica.Wr_BytesRemained = 0;
NdefMap->ApduBufferSize = 0;
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status);
}
break;
case PH_NFCFRI_NDEFMAP_FELI_STATE_RD_BLOCK :
/* check the Manuf Id in the receive buffer*/
Status = phFriNfc_Felica_HCheckManufId(NdefMap);
if ( Status == NFCSTATUS_SUCCESS )
{
if(NdefMap->SendRecvBuf[1] == PH_NFCFRI_NDEFMAP_FELI_RD_RESP_BYTE )
{
/* calculate the Nmaxb*/
Nbc = phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC);
/*get Receive length from the card for corss verifications*/
RecvTxLen= phFriNfc_Felica_HSetTrxLen(NdefMap,Nbc);
BytesToRecv = NdefMap->SendRecvBuf[12]*16;
/* chk the status flags 1 */
if ( NdefMap->SendRecvBuf[10] == 0x00)
{
if ( RecvTxLen == BytesToRecv)
{
NdefMap->Felica.CurBlockNo += (uint8_t)(RecvTxLen/16);
phFriNfc_Felica_HAfterRead_CopyDataToBuff(NdefMap);
Status = phFriNfc_Felica_HReadData(NdefMap,PH_FRINFC_NDEFMAP_SEEK_CUR);
/* handle the error in Transc function*/
if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER))
{
CRFlag =TRUE;
}
}
else
{
CRFlag =TRUE;
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_RECEIVE_LENGTH);
/*set the buffer index back to zero*/
NdefMap->ApduBuffIndex = 0;
NdefMap->Felica.Rd_NoBytesToCopy = 0;
}
}
else
{
NdefMap->ApduBuffIndex=0;
/*handle the Error case*/
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
NFCSTATUS_READ_FAILED);
CRFlag =TRUE;
}
}
else
{
CRFlag =TRUE;
NdefMap->ApduBuffIndex=0;
/*return Error "Invalid Read Response Code"*/
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_READ_FAILED);
}
}
else
{
CRFlag =TRUE;
}
if ( CRFlag ==TRUE )
{
/* call respective CR */
phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_RD_NDEF,Status);
}
break;
default:
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_DEVICE_REQUEST);
phFriNfc_Felica_HCrHandler(NdefMap, PH_FRINFC_NDEFMAP_CR_INVALID_OPE, Status);
break;
}
}
else
{
/* Call CR for unknown Error's*/
switch ( NdefMap->State)
{
case PH_FRINFC_NDEFMAP_FELI_STATE_CHK_NDEF :
case PH_NFCFRI_NDEFMAP_FELI_STATE_SELECT_WILD_CARD :
case PH_NFCFRI_NDEFMAP_FELI_STATE_SELECT_NDEF_APP :
case PH_NFCFRI_NDEFMAP_FELI_STATE_RD_ATTR :
phFriNfc_Felica_HCrHandler(NdefMap, PH_FRINFC_NDEFMAP_CR_CHK_NDEF,
Status);
break;
case PH_NFCFRI_NDEFMAP_FELI_STATE_WR_BLOCK :
case PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_BEGIN :
case PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_END :
phFriNfc_Felica_HCrHandler(NdefMap, PH_FRINFC_NDEFMAP_CR_WR_NDEF,
Status);
break;
case PH_NFCFRI_NDEFMAP_FELI_STATE_RD_BLOCK :
phFriNfc_Felica_HCrHandler(NdefMap, PH_FRINFC_NDEFMAP_CR_RD_NDEF,
Status);
break;
default :
/*set the invalid state*/
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
phFriNfc_Felica_HCrHandler(NdefMap, PH_FRINFC_NDEFMAP_CR_INVALID_OPE, Status);
break;
}
}
}
/*!
* \brief Prepares Cmd Pkt for reading attribute Blk information.
*/
static NFCSTATUS phFriNfc_Felica_HRdAttrInfo(phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint8_t BufIndex = 0;
/* Set the Felica Cmd*/
#ifdef PH_HAL4_ENABLE
NdefMap->Cmd.FelCmd = phHal_eFelica_Raw;
#else
NdefMap->Cmd.FelCmd = phHal_eFelicaCmdListFelicaCmd;
#endif /* #ifdef PH_HAL4_ENABLE */
/*set the additional informations for the data exchange*/
NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0;
/* 1st byte represents the length of the cmd packet*/
NdefMap->SendRecvBuf[BufIndex] = 0x00;
BufIndex++;
/* Read/check command code*/
NdefMap->SendRecvBuf[BufIndex] = 0x06;
BufIndex++;
/* IDm - Manufacturer Id : 8bytes*/
#ifdef PH_HAL4_ENABLE
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(void * )&NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm,
8);
#else
(void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])),
(void * )&NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo212_424.Startup212_424.NFCID2t,
8);
#endif /* #ifdef PH_HAL4_ENABLE */
BufIndex+=8;
NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Services (n=1 ==> 0x80)*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x0B; /* Service Code List*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /* Service Code List*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Blocks to read)*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x80; /* 1st Block Element : byte 1*/
BufIndex++;
NdefMap->SendRecvBuf[BufIndex] = 0x00; /* 1st Block Element : byte 2, block 1*/
BufIndex++;
NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex;
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
/* Update the Send Len*/
NdefMap->SendLength = BufIndex;
/* Change the state to PH_NFCFRI_NDEFMAP_FELI_STATE_RD_ATTR */
NdefMap->State = PH_NFCFRI_NDEFMAP_FELI_STATE_RD_ATTR;
/*set the completion routines for the desfire card operations*/
NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_NdefMap_Process;
NdefMap->MapCompletionInfo.Context = NdefMap;
/*Call the Overlapped HAL Transceive function */
status = phFriNfc_OvrHal_Transceive( NdefMap->LowerDevice,
&NdefMap->MapCompletionInfo,
NdefMap->psRemoteDevInfo,
NdefMap->Cmd,
&NdefMap->psDepAdditionalInfo,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvBuf,
NdefMap->SendRecvLength);
return (status);
}
/*!
* \brief Validated manufacturer Details, during the read/write operations.
*/
static NFCSTATUS phFriNfc_Felica_HCheckManufId(const phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint8_t result = 0;
/* check the stored manufacture id with the received manufacture id*/
result = (uint8_t)(phFriNfc_Felica_MemCompare( (void *)(&(NdefMap->SendRecvBuf[2])),
(void *)NdefMap->FelicaManufDetails.ManufID,
8));
if ( result != 0)
{
status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_REMOTE_DEVICE);
}
else
{
status = PHNFCSTVAL(CID_NFC_NONE,NFCSTATUS_SUCCESS);
}
return (status);
}
static NFCSTATUS phFriNfc_Felica_HCalCheckSum(const uint8_t *TempBuffer,
uint8_t StartIndex,
uint8_t EndIndex,
uint16_t RecvChkSum)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
uint16_t CheckSum=0,
BufIndex=0;
for(BufIndex = StartIndex;BufIndex <=EndIndex;BufIndex++)
{
CheckSum += TempBuffer[BufIndex];
}
if( RecvChkSum != CheckSum )
{
Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_FORMAT);
}
return (Result);
}
/*!
* \brief On successful read attribute blk information, this function validates and stores the
* Attribute informations in to the context.
*/
static NFCSTATUS phFriNfc_Felica_HUpdateAttrInfo(phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint8_t CRFlag = FALSE,
Nmaxb1, Nmaxb2 = 0,
ChkSum1 = 0, ChkSum2=0;
uint16_t Nmaxblk = 0,
RecvChkSum=0,
NdefBlk = 0;
uint32_t DataLen =0;
/* Validate T3VNo and NFCDevVNo */
status = phFriNfc_MapTool_ChkSpcVer(NdefMap,
PH_NFCFRI_NDEFMAP_FELI_VERSION_INDEX);
if ( status != NFCSTATUS_SUCCESS )
{
CRFlag = TRUE;
}
else
{
/* get the Nmaxb from the receive buffer*/
Nmaxb1 = NdefMap->SendRecvBuf[16];
Nmaxb2 = NdefMap->SendRecvBuf[17];
Nmaxblk = (((uint16_t)Nmaxb1 << 8) | (Nmaxb2 & 0x00ff));
if ( Nmaxblk != 0 )
{
/* check the Nbr against the Nmaxb*/
if ( NdefMap->SendRecvBuf[14] > Nmaxblk )
{
CRFlag = TRUE;
}
else
{
/*check Nbw > Nmaxb*/
/*check the write flag validity*/
/*check for the RFU bytes validity*/
if ( (NdefMap->SendRecvBuf[15] > Nmaxblk) ||
((NdefMap->SendRecvBuf[22] != 0x00) && (NdefMap->SendRecvBuf[22] !=0x0f ))||
( (NdefMap->SendRecvBuf[23] != 0x00) && (NdefMap->SendRecvBuf[23] !=0x01 ))||
( NdefMap->SendRecvBuf[18] != 0x00) ||
( NdefMap->SendRecvBuf[19] != 0x00) ||
( NdefMap->SendRecvBuf[20] != 0x00) ||
( NdefMap->SendRecvBuf[21] != 0x00))
{
CRFlag = TRUE;
}
else
{
/* check the validity of the actual ndef data len*/
PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES( NdefMap->SendRecvBuf[24],
NdefMap->SendRecvBuf[25],
NdefMap->SendRecvBuf[26],
DataLen);
/* Calculate Nbc*/
NdefBlk = (uint16_t )((( DataLen % 16) == 0 ) ? (DataLen >> 4) : ((DataLen >> 4) +1));
/* check Nbc against Nmaxb*/
if ((NdefBlk > Nmaxblk))
{
CRFlag = TRUE;
}
else
{
/*Store the attribute information in phFriNfc_Felica_AttrInfo*/
NdefMap->FelicaAttrInfo.Version = NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_VERSION_INDEX];
NdefMap->FelicaAttrInfo.Nbr = NdefMap->SendRecvBuf[14];
NdefMap->FelicaAttrInfo.Nbw = NdefMap->SendRecvBuf[15];
NdefMap->FelicaAttrInfo.Nmaxb = Nmaxblk;
NdefMap->FelicaAttrInfo.WriteFlag = NdefMap->SendRecvBuf[22];
NdefMap->FelicaAttrInfo.RdWrFlag = NdefMap->SendRecvBuf[23];
/* Get CheckSum*/
ChkSum1 = NdefMap->SendRecvBuf[27];
ChkSum2 = NdefMap->SendRecvBuf[28];
RecvChkSum = (((uint16_t)ChkSum1 << 8) | (ChkSum2 & 0x00ff));
/* Check the check sum validity?*/
status = phFriNfc_Felica_HCalCheckSum(NdefMap->SendRecvBuf,
PH_NFCFRI_NDEFMAP_FELI_VERSION_INDEX,
26,
RecvChkSum);
if ( status != NFCSTATUS_SUCCESS )
{
CRFlag = TRUE;
}
else
{
/*check RW Flag Access Rights*/
/* set to read only cannot write*/
if ( NdefMap->FelicaAttrInfo.RdWrFlag == 0x00 )
{
NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_ONLY;
}
else if ( NdefMap->FelicaAttrInfo.RdWrFlag == 0x01 ) // additional check for R/W access
{
NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_WRITE;
}
else // otherwise invalid
{
NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID;
}
NdefMap->FelicaAttrInfo.LenBytes[0] = NdefMap->SendRecvBuf[24];
NdefMap->FelicaAttrInfo.LenBytes[1] = NdefMap->SendRecvBuf[25];
NdefMap->FelicaAttrInfo.LenBytes[2] = NdefMap->SendRecvBuf[26];
status = PHNFCSTVAL(CID_NFC_NONE,NFCSTATUS_SUCCESS);
}
}
}
}
}
else
{
CRFlag = TRUE;
}
}
if ( (status == NFCSTATUS_INVALID_FORMAT ) && (CRFlag == TRUE ))
{
NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID;
}
if ( CRFlag == TRUE )
{
/*Return Status Error “ Invalid Format”*/
status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_FORMAT);
}
return (status);
}
/*!
* \brief this shall notify the integration software with respective
* success/error status along with the completion routines.
*/
static void phFriNfc_Felica_HCrHandler(phFriNfc_NdefMap_t *NdefMap,
uint8_t CrIndex,
NFCSTATUS Status)
{
/* set the state back to the Reset_Init state*/
NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RESET_INIT;
/* set the completion routine*/
NdefMap->CompletionRoutine[CrIndex].
CompletionRoutine(NdefMap->CompletionRoutine->Context, Status);
}
/*!
* \brief this shall initialise the internal buffer data to zero.
*/
static void phFriNfc_Felica_HInitInternalBuf(uint8_t *Buffer)
{
uint8_t index=0;
for( index = 0; index< 16 ; index++)
{
Buffer[index] = 0;
}
}
static int phFriNfc_Felica_MemCompare ( void *s1, void *s2, unsigned int n )
{
int8_t diff = 0;
int8_t *char_1 =(int8_t *)s1;
int8_t *char_2 =(int8_t *)s2;
if(NULL == s1 || NULL == s2)
{
PHDBG_CRITICAL_ERROR("NULL pointer passed to memcompare");
}
else
{
for(;((n>0)&&(diff==0));n--,char_1++,char_2++)
{
diff = *char_1 - *char_2;
}
}
return (int)diff;
}
#ifdef UNIT_TEST
#include <phUnitTestNfc_Felica_static.c>
#endif
#endif /* PH_FRINFC_MAP_FELICA_DISABLED */
|