summaryrefslogtreecommitdiff
path: root/InfraStack/OSAgnostic/WiMax/Agents/NDnS/Source/NDnSAgent.c
blob: 94414b8e67a4f66e4f7ca9981a71bc211f334f3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
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
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
/**************************************************************************
 Copyright (c) 2007-2008, Intel Corporation. All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  3. Neither the name of the Intel Corporation nor the names of its
     contributors may be used to endorse or promote products derived from
     this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.

***************************************************************************/
#include "NDnSAgent_Internals.h"
#include "NDnSAgent.h"
#include "NDnS_L4Scanner.h"
#include "NDnS_L4Publisher.h"
#include "NDnSAgent_L4P.h"
#include "NDnSAgent_APDO.h"
#include "NDnSAgent_DB_if.h"

#include "CommonServices.h"
#include "WrappersCommon.h"
#include "L5OpCodes.h"
#include "NDnSAgent_Monitor.h"

#include "wmxSDK_Nds_Internals.h"
#include "wmxSDK_Nds_Utils.h"
#include "wmxSDK_Nds_3.h"
#include "wmxSDK_Nds_L5.h"
#include "wmxSDK_Sup_Defines.h"
#include "wmxSDK_Msc_2.h"
#include "wmxSDK_Coex_1.h"
#include "NDnS_Coex.h"
#include "L3L4Structs.h"

#include "Act.h"
#include "L4ConfigurationManager.h"

#include "NDnSAgent_Utils.h"
#include "wimax_osal_services_cmn.h"
#include "wimax_osal_ip_services.h"
#include "wimax_osal_crypt_services.h"

typedef wmx_Status_t (*DualFlushOp)();

extern tUtilityFunctions *pUtils;
extern wmx_CoexistenceMode_t initialCoexMode;
static wmx_LinkStatus_t g_linkStatus;
// Declarations
void L4Pub_SetDeviceStatusActivationConnect(BOOL activation);
void L4C_Reset(L4CState l4cState, wmx_pSystemStateUpdate systemStateUpdate);
wmx_Status_t NDnSAgent_GetStatistics( UINT32 *bufferLength, UINT8 *buffer );
void L4C_ScheduleTask(L4C_Task task);
void L4C_TaskHandler(UINT32 internalRequestID, void *buffer, UINT32 bufferLength);
wmx_Status_t NDnSAgent_ConnectPhase2(wmx_pConnectInfo_t connectInfo);
wmx_Status_t L4C_StartManualScanPhase2(wmx_ScanType_t scanType, BOOL isInternal);
void AddNAPsBeneathThreshold(UINT32 numOfNAPs, wmx_pNAP_t pNAPs);
wmx_Status_t NDnSAgent_SetConnectModePhase2();
wmx_Status_t NDnSAgent_InvokeDualFlushOp(L4C_Task task, void * paramsBuf, UINT32 bufSize);
void NDnSAgent_OnProgressDualFlush(L4C_Task task, DualFlushOp ExecuteTask, UINT32 phaseNum);

wmx_Status_t L4C_UpdateScanStatus(BOOL isScanning)
{
	//[Oran]
	L4Pub_UpdateData(L4Pub_CbType_ScanStatus, (L4Pub_pCallbackParamsUnion)&isScanning);
	return WMX_ST_OK;
}

wmx_Status_t NDnSAgent_RfOn()
{
	wmx_Status_t status;
	L4CState states[2];

	states[0] = L4C_SINGLE_SCAN;
	states[1] = L4C_AUTO_SCAN;

	status = wmx_InternalRfOn();

	if (status == WMX_ST_OK)
	{
		FSM_WaitForMultipleStates(&g_ndnsContext.fsm, (int*)states, 2, L4C_TIMEOUT);
	}

	return status;
}

wmx_Status_t NDnSAgent_RfOff()
{
	wmx_Status_t status;

	// in case a scan/connect process is taking place, notify device
	if(L4C_GetInitialCoexMode() == WMX_MODE_CM && g_ndnsContext.processStarted)
	{
		L4C_SetProcessStarted(FALSE);
		wmx_CmdProcessEnd();
	}

	status = wmx_InternalRfOff();

	if (status == WMX_ST_OK)
	{
		if (!FSM_WaitForState(&g_ndnsContext.fsm, L4C_RF_OFF, L4C_TIMEOUT))
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_WARNING, "Wait for RfOff state has timed out.");
			return WMX_ST_FAIL;
		}
		else
		{
			ClearAvailableNSPsList();
		}
	}

	return status;
}

// Get the current preferred NSP.
wmx_Status_t GetCurrentPreferredNSPEx(wmx_pNSP_t pNSP, UINT32 *pNumOfCurrentPreferredNSPs)
{
	wmx_Status_t rc = WMX_ST_OK;
	wmx_NSPid_t currentPreferredNspID;

	if (	NULL == pNSP	)
	{
		*pNumOfCurrentPreferredNSPs = 0;
		rc = WMX_ST_WRONG_ARGUMENT;
		goto Finalize;
	}

	rc = L4db_GetCurrentPreferredNsp(&currentPreferredNspID);
	if (rc != WMX_ST_OK)
	{
		goto Finalize;
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "GetCurrentPreferredNSP. nsp ID=%d", currentPreferredNspID);

	if (currentPreferredNspID != L4DB_INVALID_INDEX)
	{
		rc = GetNspInfo(currentPreferredNspID, pNSP);
		if (rc != WMX_ST_OK)
		{
			*pNumOfCurrentPreferredNSPs = 0;
			goto Finalize;
		}

		*pNumOfCurrentPreferredNSPs = 1;
	}
	else
	{
		*pNumOfCurrentPreferredNSPs = 0;
	}

Finalize:
	return rc;
}

// Get the current preferred NSP.
wmx_Status_t GetCurrentPreferredNSP(void *buffer, UINT32 *bufferSize)
{
	wmx_Status_t rc = WMX_ST_OK;
	wmx_NSP_t currentPreferredNsp;
	UINT32 numOfCurrentPreferredNSPs;

	if (	*bufferSize < sizeof(wmx_NSP_t) ||
			NULL == buffer	)
	{
		rc = WMX_ST_WRONG_ARGUMENT;
		goto Finalize;
	}

	rc = GetCurrentPreferredNSPEx(&currentPreferredNsp, &numOfCurrentPreferredNSPs);
	if (rc != WMX_ST_OK)
	{
		*bufferSize = 0;
		goto Finalize;
	}


	if (numOfCurrentPreferredNSPs > 0)
	{
		*(wmx_pNSP_t)buffer = currentPreferredNsp;
		*bufferSize = sizeof(wmx_NSP_t) * numOfCurrentPreferredNSPs;
	}
	else
	{
		// Current preferred NSP was not set
		*bufferSize = 0;
	}

Finalize:
	return rc;
}

// Set a new current preferred NSP or unset the current preferred to none.
wmx_Status_t SetCurrentPreferredNSPEx(wmx_pNSPid_t pCurrentPreferredNspIDs, UINT32 numOfCurrentPreferredNSPs)
{
	wmx_NSPid_t currentPreferredNspID;
	wmx_SystemState_t currentSystemState;
	wmx_Status_t status = WMX_ST_OK;
	wmx_Status_t currentStatus;

	// use bufferSize=0 to unset the current preferred NSP
	if (pCurrentPreferredNspIDs == NULL &&
		numOfCurrentPreferredNSPs != 0)
	{
		status = WMX_ST_WRONG_ARGUMENT;
		goto Finalize;
	}

	// Check that the system state is not connected.
	// This check is done here and not in the DB because internal calls might want to change the current
	// preferred NSP after being connected
	OSAL_enter_critical_section(&g_ndnsContext.lockSystemState);
	currentSystemState = g_ndnsContext.systemState;
	OSAL_exit_critical_section(&g_ndnsContext.lockSystemState);

	if( ( L4C_CONNECTED == FSM_GetState(&g_ndnsContext.fsm) ))
	{
		status = WMX_ST_WRONG_STATE;
		goto Finalize;
	}

	if (numOfCurrentPreferredNSPs != 0)
	{
		currentPreferredNspID = *(wmx_pNSPid_t)pCurrentPreferredNspIDs; // Use only the first one
	}
	else
	{
		currentPreferredNspID = (wmx_NSPid_t)L4DB_INVALID_INDEX;
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "SetCurrentPreferredNSP(IN). curretntPreferredNSP=%d", currentPreferredNspID);
	currentStatus = L4db_SetCurrentPreferredNsp(currentPreferredNspID);

	// Check if there was a successful change to the current preferred NSP
	if (currentStatus == WMX_ST_OK)
	{
		// Raise an indication about changing the current preferred NSP
		if (currentPreferredNspID != L4DB_INVALID_INDEX)
		{
			NDnSAgent_SendCurrentPreferredNSPChanged(1, &currentPreferredNspID); // Support only one current preferred
		}
		else
		{  // Raise size 0 when the current preferred is set to NULL
			NDnSAgent_SendCurrentPreferredNSPChanged(0, &currentPreferredNspID);
		}
	}

Finalize:
	return status;
}

// Set a new current preferred NSP or unset the current preferred to none.
wmx_Status_t SetCurrentPreferredNSP(void *buffer, UINT32 bufferSize)
{
	wmx_Status_t status = WMX_ST_OK;

	// use bufferSize=0 to unset the current preferred NSP
	if (	bufferSize != sizeof(wmx_NSPid_t) &&
			bufferSize != 0)
	{
		status = WMX_ST_WRONG_ARGUMENT;
		goto Finalize;
	}

	SetCurrentPreferredNSPEx((wmx_pNSPid_t)buffer, (UINT32)(bufferSize / sizeof(wmx_NSPid_t)));

Finalize:
	return status;
}

// Enters the ConnectMode to the Buffer, returns an error if buffer is too short.
wmx_Status_t GetConnectMode(void *buffer, UINT32 *bufferSize)
{
	if ( *bufferSize >= sizeof(wmx_UserConnectMode_t) )
	{
		*(wmx_pUserConnectMode_t)buffer = 	L4db_GetConnectMode();

		return WMX_ST_OK;
	}

	return WMX_ST_FAIL;
}

wmx_Status_t NDnSAgent_GetPollingSupported(BOOL *buffer, UINT32 *bufferSize)
{
	BOOL localBool;
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "NDnSAgent_GetPollingSupported: [IN]");
	if ( *bufferSize >= sizeof(BOOL) )
	{
		localBool = 	L4db_GetPollingIntervalSupported();
		*buffer = localBool;
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "NDnSAgent_GetPollingSupported: [OUT-ok]");
		return WMX_ST_OK;
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "NDnSAgent_GetPollingSupported: [OUT-fail]");
	return WMX_ST_FAIL;
}

// Extracts the ConnectMode from the Buffer and set it, returns an error if buffer is too short.
wmx_Status_t SetConnectMode(wmx_pUserConnectMode_t pConnectMode, UINT32 bufferSize)
{
	//wmx_SystemState_t systemState;
	//wmx_NSPid_t currentPreferredNSP;

	wmx_UserConnectMode_t lastConnectMode = L4db_GetConnectMode();
	//wmx_UserConnectMode_t connectMode;
	wmx_Status_t status = WMX_ST_FAIL;

	UNREFERENCED_PARAMETER(bufferSize);
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "SetConnectMode(IN). systemState=%s, pConnectMode = [%d]", NDnSSystemStates(g_ndnsContext.systemState), *pConnectMode);
	// in case a scan/connect process is taking place, notify device
	if(L4C_GetInitialCoexMode() == WMX_MODE_CM && g_ndnsContext.processStarted)
	{
		L4C_SetProcessStarted(FALSE);
		wmx_CmdProcessEnd();
	}

	if (pConnectMode == NULL)
	{
		return WMX_ST_WRONG_ARGUMENT;
	}

	if (lastConnectMode == *pConnectMode)
	{
		return WMX_ST_OK;
	}

	// 	//In case we need to change to AutoConnect - Make sure we have a preferred NSP set.
	// 	if((connectMode == UserSemiManScanAutoConnect || connectMode == UserAutoScanAutoConnect) &&
	// 		(L4db_GetCurrentPreferredNsp(&currentPreferredNSP) != WMX_ST_OK ) )
	// 	{
	// 		return WMX_ST_WRONG_STATE;
	// 	}

	//For all scenarios we need to call StopScan and then Update DB on DualFlash -
	status = NDnSAgent_InvokeDualFlushOp(Task_SetConnectMode, (void *)pConnectMode, sizeof(wmx_pUserConnectMode_t));

	////////////////////////////////
	// 	if ( bufferSize >= sizeof(wmx_UserConnectMode_t) )
	// 	{
	// 		connectMode = *(wmx_pUserConnectMode_t)buffer;
	//
	// 		if (lastConnectMode == connectMode)
	// 		{
	// 			return WMX_ST_OK;
	// 		}
	//
	// 		st = L4db_GetCurrentPreferredNsp(&currentPreferredNSP);
	//
	// 		// Auto connect can not be set when current preferred NSP is not set
	// 		if ((connectMode == UserSemiManScanAutoConnect ||
	// 			connectMode == UserAutoScanAutoConnect) &&
	// 			st != WMX_ST_OK)
	// 		{
	// 			return WMX_ST_WRONG_STATE;
	// 		}
	//
	// 		if( lastConnectMode == UserSemiManScanManConnect && connectMode == UserSemiManScanAutoConnect)
	// 		{
	// 			L4db_SetConnectMode(connectMode);
	// 			return WMX_ST_OK;
	// 		}
	//
	// 		if( lastConnectMode == UserSemiManScanAutoConnect && connectMode == UserSemiManScanManConnect)
	// 		{
	// 			L4db_SetConnectMode(connectMode);
	// 			return WMX_ST_OK;
	// 		}
	//
	// 		// read the current system state
	// 		RWL_LockWrite(&g_ndnsContext.lockSystemState);
	// 		systemState = g_ndnsContext.systemState;
	// 		RWL_UnlockWrite(&g_ndnsContext.lockSystemState);
	//
	// 		L4S_Reset(FALSE);
	// 		OSAL_atomic_exchange(&g_ndnsContext.isResumeScan, FALSE);
	// 		L4db_SetConnectMode(connectMode);
	// 		// in case we move from manual to semi-manual/auto scan - this will allow the auto scan to start
	// 		if (UserManScanManConnect == lastConnectMode)
	// 		{
	// 			OSAL_reset_event(g_ndnsContext.hScanStartedEvent);
	// 		}
	// 		status = L4C_SetControllerMode(systemState, FALSE);
	// 		l4cLastState = FSM_GetState(&g_ndnsContext.fsm);
	// 		if ( (systemState == Ready) )
	// 		{
	// 			// wait for scan processing to end
	// 			if ((OSAL_wait_event(g_ndnsContext.hScanStartedEvent, L4C_TIMEOUT) != WAIT_OBJECT_0) || (g_ndnsContext.scanStatus != WMX_ST_OK))
	// 			{
	// 				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Couldn't start semi-manual/auto scan");
	// 				status = WMX_ST_FAIL;
	// 			}
	// 		}
	// 	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "SetConnectMode(OUT) - status=%d", status);
	return status;
}


// Enters the RoamingMode to the Buffer, returns an error if buffer is too short.
wmx_Status_t GetRoamingMode(void *buffer, UINT32 *bufferSize)
{
	if ( *bufferSize >= sizeof(wmx_UserRoamingMode_t) )
	{
// TODO: Add support when the nsp id is given
//		*(wmx_pUserRoamingMode_t)buffer = 	L4db_GetRoamingMode();
UNREFERENCED_PARAMETER(buffer);

		return WMX_ST_OK;
	}

	return WMX_ST_FAIL;
}

// Extracts the RoamingMode from the Buffer and set it, returns an error if buffer is too short.
wmx_Status_t SetRoamingMode(void *buffer, UINT32 bufferSize)
{
	UNREFERENCED_PARAMETER(buffer);
	if ( bufferSize >= sizeof(wmx_UserRoamingMode_t) )
	{
// TODO: Add support when the nsp id is given
//		L4db_SetRoamingMode(*(wmx_pUserRoamingMode_t)buffer);
//send indication to subscribers
		NDnSAgent_RoamingModeUpdate(*(wmx_pUserRoamingMode_t)(buffer));
		return WMX_ST_OK;
	}

	return WMX_ST_FAIL;
}

wmx_Status_t SetFastReconnectStatus(void *buffer, UINT32 bufferSize)
{
	BOOL fastReconnectStatus;
	wmx_Status_t status = WMX_ST_FAIL;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "SetFastReconnectStatus(IN)");
	if ( bufferSize >= sizeof(BOOL) )
	{
		fastReconnectStatus = *(BOOL*)buffer;

		if (L4db_GetFastReconnectStatus() == fastReconnectStatus)
		{
			status = WMX_ST_OK; // no need to re-assign the same value
		}
		else
		{
			status = L4db_SetFastReconnectStatus(fastReconnectStatus);
			//send indication to subscribers
			NDnSAgent_FastReconnectModeUpdate(*(BOOL *)(buffer));
		}
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "SetFastReconnectStatus(OUT) - status=%d", status);
	return status;
}

// Enters the ConnectMode to the Buffer, returns an error if buffer is too short.
wmx_Status_t GetFastReconnectStatus(void *buffer, UINT32 *bufferSize)
{
	if ( *bufferSize >= sizeof(BOOL) )
	{
		*(BOOL*)buffer = L4db_GetFastReconnectStatus();

		return WMX_ST_OK;
	}

	return WMX_ST_FAIL;
}

// Enters the setPreferredNspOnConnect to the Buffer, returns an error if buffer is too short.
wmx_Status_t GetConnectedAsCurrentPreferredCapabilityStatus(void *buffer, UINT32 *bufferSize)
{
	if ( *bufferSize >= sizeof(BOOL) )
	{
		*(BOOL*)buffer = L4db_GetSetPreferredNspOnConnectState();

		return WMX_ST_OK;
	}

	return WMX_ST_FAIL;
}

wmx_Status_t SetConnectedAsCurrentPreferredCapabilityStatus(void *buffer, UINT32 bufferSize)
{
	BOOL isEnable;
	wmx_Status_t status = WMX_ST_FAIL;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "SetConnectedAsCurrentPreferredCapabilityStatus(IN)");
	if ( bufferSize >= sizeof(BOOL) )
	{
		isEnable = *(BOOL*)buffer;

		status = L4db_SetSetPreferredNspOnConnectState(isEnable);

		//send indication to subscribers
		NDnSAgent_ConnectedAsCurrentPreferredCapabilityStatusUpdate(*(BOOL *)(buffer));
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "SetConnectedAsCurrentPreferredCapabilityStatus(OUT) - status=%d", status);
	return status;
}

wmx_Status_t NDnSAgent_setMSChap_v2Crd( void *buffer, UINT32 bufferSize)
{
	wmx_pSetMSChapParams MSChapParams = (wmx_pSetMSChapParams)buffer;
	wmx_Status_t wmxStatus = WMX_ST_FAIL;
	if ((bufferSize >= sizeof(MSChapParams) && (bufferSize != 0)))
	{
		wmxStatus = L4db_SetEapNode(MSChapParams->nspId, MSChapParams->username, MSChapParams->password);
	}
	if(wmxStatus == WMX_ST_OK)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_setMSChap_v2Crd - before save to DB");
		SaveToDB();
	}
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "NDnSAgent_setMSChap_v2Crd(OUT). status = %d", wmxStatus);
	return wmxStatus;
}

void NDnSAgent_SendConnectCompleted( wmx_ConnectStatus_t connectStatus )
{
	wmx_UserConnectStatus_t userConnectStatus;
	UINT32 msgSize;
	UINT32 sendIndDataSize;
	SendIndData *pSendIndData;
	NDNS_MESSAGE *pIndMsg;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "connect completed callback called");

	wmxNds_ConnectStatusToUserConnectStatus( &userConnectStatus, connectStatus );

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "sending connect completed indication");

	msgSize = sizeof(NDNS_MESSAGE) + sizeof(wmx_UserConnectStatus_t);
	sendIndDataSize = sizeof(SendIndData) + msgSize;
	pSendIndData = (SendIndData *)malloc(sendIndDataSize);
	if (pSendIndData == NULL)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "malloc failed");
		return;
	}

	pIndMsg = (NDNS_MESSAGE *)(pSendIndData->indication_buffer);

	pSendIndData->pSenderFuncs = nds_pFuncs;
	pSendIndData->senderL5Conn = nds_l5ConnID;
	pSendIndData->pSubscribersList = g_ndnsContext.indicatorSubscribersList;
	pSendIndData->indication_id = NDNS_OPCODE_IND_CONNECT_COMPLETED;
	pSendIndData->indication_buffer_size = msgSize;
	pIndMsg->opcode = NDNS_OPCODE_IND_CONNECT_COMPLETED;
	pIndMsg->bufferLength = sizeof(wmx_UserConnectStatus_t);
	*(wmx_pUserConnectStatus_t)pIndMsg->buf = userConnectStatus;

	pUtils->tpfnPostRequest(
		MEDIUM, NDNS_OPCODE_INDICATION_ARRIVED, pSendIndData, sendIndDataSize, &SendIndicationToSubscribers);

	free(pSendIndData);
}

void NDnSAgent_SendCurrentPreferredNSPChanged(UINT32 numOfCurrentPreferred, wmx_pNSPid_t pNspIDs )
{
	UINT32 msgSize;
	UINT32 sendIndDataSize;
	SendIndData *pSendIndData = NULL;
	NDNS_MESSAGE *pIndMsg;
	wmx_pNSP_t pNSPs = NULL;
	int i;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_SendCurrentPreferredNSPChanged (IN)");

	msgSize = sizeof(NDNS_MESSAGE) + sizeof(UINT32) + sizeof(wmx_NSP_t)*numOfCurrentPreferred;
	sendIndDataSize = sizeof(SendIndData) + msgSize;
	pSendIndData = (SendIndData *)malloc(sendIndDataSize);
	if (pSendIndData == NULL)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "NDnSAgent_SendCurrentPreferredNSPChanged malloc pSendIndData failed");
		goto Finalize;
	}

	pNSPs = (wmx_pNSP_t)malloc(numOfCurrentPreferred * sizeof(wmx_NSP_t));
	if (pNSPs == NULL)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "NDnSAgent_SendCurrentPreferredNSPChanged malloc pNSPs failed");
		goto Finalize;
	}

	// Fill the NSPs list
	for (i=0; i < (int)numOfCurrentPreferred; i++)
	{
		GetNspInfo(pNspIDs[i], &pNSPs[i]);
	}

	pIndMsg = (NDNS_MESSAGE *)(pSendIndData->indication_buffer);

	pSendIndData->pSenderFuncs = nds_pFuncs;
	pSendIndData->senderL5Conn = nds_l5ConnID;
	pSendIndData->pSubscribersList = g_ndnsContext.indicatorSubscribersList;
	pSendIndData->indication_id = NDNS_OPCODE_IND_CURRENT_PREFERRED_NSP;
	pSendIndData->indication_buffer_size = msgSize;
	pIndMsg->opcode = NDNS_OPCODE_IND_CURRENT_PREFERRED_NSP;
	pIndMsg->bufferLength = sizeof(UINT32) + sizeof(wmx_NSP_t)*numOfCurrentPreferred;
	*(UINT32*)pIndMsg->buf = numOfCurrentPreferred;
	OSAL_memcpy_s( pIndMsg->buf + sizeof(UINT32), sizeof(wmx_NSP_t) * numOfCurrentPreferred,
		pNSPs, sizeof(wmx_NSP_t) * numOfCurrentPreferred);

	pUtils->tpfnPostRequest(
		MEDIUM, NDNS_OPCODE_INDICATION_ARRIVED, pSendIndData, sendIndDataSize, &SendIndicationToSubscribers);

Finalize:
	if (pSendIndData != NULL)
	{
		free(pSendIndData);
	}
	if (pNSPs != NULL)
	{
		free(pNSPs);
	}
}


L4C_ConnectMode L4C_GetConnectionMode(wmx_UserConnectMode_t userConnectMode)
{
	L4C_ConnectMode connectMode = ConnectMode_Manual;

	switch (userConnectMode)
	{
		case UserSemiManScanManConnect:
		case UserAutoScanManConnect:
		case UserManScanManConnect:
			connectMode = ConnectMode_Manual;
			break;
		case UserSemiManScanAutoConnect:
		case UserAutoScanAutoConnect:
			connectMode = ConnectMode_Auto;
			break;
	}

	return connectMode;
}

L4C_ScanMode L4C_GetScanMode(wmx_UserConnectMode_t userConnectMode)
{
	L4C_ScanMode scanMode = ScanMode_Auto;

	switch (userConnectMode)
	{
		case UserSemiManScanManConnect:
		case UserSemiManScanAutoConnect:
			scanMode = ScanMode_SemiManual;
			break;
		case UserAutoScanManConnect:
		case UserAutoScanAutoConnect:
			scanMode = ScanMode_Auto;
			break;
		case UserManScanManConnect:
			scanMode = ScanMode_Manual;
			break;
	}

	return scanMode;
}

// resume a scan if more chunks are available or if scan cycle is complete and L4C is in auto scan mode
wmx_Status_t L4C_ResumeScan(UINT32 internalRequestID, void *buffer, UINT32 bufferLength)
{
	wmx_Status_t status;
	// choose between semi-manual scan and auto scan
	wmx_ScanType_t scanType = (L4C_GetScanMode(L4db_GetConnectMode()) == ScanMode_Auto) ? SCAN_TYPE_WIDE : SCAN_TYPE_PREFERRED;
	wmx_LinkLossType_t linkLossType;

	UNREFERENCED_PARAMETER(bufferLength);
	UNREFERENCED_PARAMETER(buffer);
	UNREFERENCED_PARAMETER(internalRequestID);

	L4S_EnableScanner(TRUE);
	// continue scanning the next chunk / retry
	if (L4C_AUTO_SCAN == FSM_GetState(&g_ndnsContext.fsm))
	{
		status = L4S_StartScanner(FALSE);
	}
	else
	{
		status = L4S_StartScanner(TRUE);
	}


	// if retryCounter is invalid (=no more retries) we want to schedule a new scan (long link loss)
	if (status == WMX_ST_WRONG_ARGUMENT)
	{	// if we got here it means that we're in auto scan and it's time to move to long link loss
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "L4C_ResumeScan: Move to long link loss");

		if (FSM_GetState(&g_ndnsContext.fsm) != L4C_AUTO_SCAN)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_ResumeScan: Can't move to long link loss since L4C in not in auto scan (wrong state)");
		}
		else
		{
			linkLossType = (g_ndnsContext.linkLossType != LinkLossType_Short) ? LinkLossType_Infinite : LinkLossType_Long;
			status = L4C_StartScan(scanType, linkLossType, TRUE, TRUE);
			// TODO: [Oran] consider use a saved isInternal instead of TRUE
			if ( WMX_ST_OK != status )
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_StartScan Failed!");
				status = WMX_ST_FAIL;
			}

		}
	}

	g_ndnsContext.scanStatus = status;
	OSAL_set_event(g_ndnsContext.hScanStartedEvent);
	return status;
}

wmx_Status_t L4C_StartAutoScan(UINT32 internalRequestID, void *buffer, UINT32 bufferLength)
{
	wmx_Status_t status = WMX_ST_OK;
	BOOL delayStart = FALSE;
	wmx_LinkLossType_t linkLossType = LinkLossType_Short;

	UNREFERENCED_PARAMETER(bufferLength);
	UNREFERENCED_PARAMETER(buffer);
	UNREFERENCED_PARAMETER(internalRequestID);

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "L4C_StartAutoScan(IN)");
	if (FALSE == (BOOL)OSAL_atomic_compareExchange(&g_ndnsContext.isScanProcessingRequired, (long)FALSE, (long)TRUE))
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_StartAutoScan: leftovers of auto scan detected");
		return WMX_ST_FAIL;
	}

	//AgeAvailableNspsList();
	if (TRUE == OSAL_atomic_compareExchange(&g_ndnsContext.isSingleManualScan, (long)FALSE, (long)TRUE))
	{
		delayStart = TRUE;
		linkLossType = (g_ndnsContext.scheduledTaskData.scanInfo.manualScanType == SCAN_TYPE_PREFERRED ? LinkLossType_Short : LinkLossType_Long);
	}

	// if last manual scan type was wide - LinkLossType_Long, else - LinkLossType_Short
	status = L4C_StartScan(SCAN_TYPE_PREFERRED, linkLossType, delayStart, TRUE);
	if ( WMX_ST_OK != status )
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_StartScan Failed!");
		status = WMX_ST_FAIL;
	}

	g_ndnsContext.scanStatus = status;
	OSAL_set_event(g_ndnsContext.hScanStartedEvent);

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "L4C_StartAutoScan(OUT)");
	return status;
}

// configure the L4C to one of the scan modes and start scanning if required
wmx_Status_t L4C_SetScanMode(BOOL startScan)
{
	L4C_ScanMode scanMode = L4C_GetScanMode(L4db_GetConnectMode());
	L4CState prevState = FSM_GetPreviousState(&g_ndnsContext.fsm);

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "L4C_SetScanMode - CurState=%d, PrevState=%d, startScan=%d",
		FSM_GetState(&g_ndnsContext.fsm), prevState, startScan);

	// set scan mode
	switch (scanMode)
	{
		case ScanMode_SemiManual:
		case ScanMode_Auto:
			if ((FSM_SetStateIfNotEqual(&g_ndnsContext.fsm, L4C_AUTO_SCAN, L4C_AUTO_SCAN) ||
				(startScan &&	(prevState == L4C_RF_OFF) ||
								// TODO: Oran - check the consequences of adding "L4C_CONNECTING" / L4C_UNINITIALIZED
								(prevState == L4C_UNINITIALIZED) ||
								(prevState == L4C_CONNECTING))))
			{
				OSAL_atomic_exchange(&g_ndnsContext.isScanProcessingRequired, (long)TRUE);
				pUtils->tpfnPostRequest(MEDIUM, 0, NULL, 0, &L4C_StartAutoScan);
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Scan mode is auto/semi-manual. Post a new scan request.");

			}
			break;
		case ScanMode_Manual:
			g_ndnsContext.scanStatus = WMX_ST_OK;
			OSAL_set_event(g_ndnsContext.hScanStartedEvent);
			OSAL_atomic_exchange(&g_ndnsContext.isSingleManualScan, (long)FALSE); // reset manual scan status
			FSM_SetState(&g_ndnsContext.fsm, L4C_SINGLE_SCAN);
			L4S_EnableScanner(TRUE);
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Scan mode is manual. Do nothing.");
			break;
	}

	return WMX_ST_OK;
}

/*wmx_SystemState_t L4C_UpdateSystemState(wmx_SystemState_t systemState)
{
	OSAL_enter_critical_section(&g_ndnsContext.lockSystemState);
	if ((verifyState) && (g_ndnsContext.systemState == systemState))
	{
		OSAL_exit_critical_section(&g_ndnsContext.lockSystemState);
		return status;
	}
	g_ndnsContext.systemState = systemState;
	OSAL_exit_critical_section(&g_ndnsContext.lockSystemState);
}*/

// returns true if the input system state reason is valid for fast reconnect
BOOL L4C_IsFastReconnectRequired(wmx_pSystemStateUpdate systemStateUpdate)
{
	BOOL res = FALSE;
	wmx_CoexistenceMode_t coexMode = WMX_MODE_XOR;
	UINT32 msgLength = sizeof(wmx_CoexistenceMode_t);
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_IsFastReconnectRequired (IN)");

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "State Reason %d", systemStateUpdate->ReportStateReason);
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "System State %d", systemStateUpdate->SystemState);
	switch(systemStateUpdate->ReportStateReason)
	{
		case StateReasonDisconnectDreg:
		case StateReasonDisconnectAbort:
		case StateReasonDisconnectReset:
		case StateReasonDisconnectSecurity:
		case StateReasonDisconnectProtocolError:
			if (systemStateUpdate->SystemState == Ready)
			{
				res = TRUE;
			}
			break;
		case StateReasonResetHwWdt:
		case StateReasonResetSwWdt:
		case StateReasonResetAssert:
		case StateReasonFWRequestDevReset:
		case StateReasonFWRequestFuncReset:
			if (systemStateUpdate->SystemState == Config)
			{
				res = TRUE;
			}
			break;
		case StateReasonResetCoExistence:
			{
				//fix for bug HSD 2694572
				//While WiMAX is connecting to the network, turning on WiFi using
				//the systray icon does not stop WiMAX from connecting and also
				//shows WiFi radio as on, but no network are found. WiMAX CU shows connection,
				//WiFi shows no networks found. Leaving it like this for approximately
				//2 minutes and WiMAX will turn the radio off and WiFi will see networks again.
				//
				//The solution for this problem for V3 is to have WiMax reset upon a WiFi request (any request),
				//while WiMax is connecting (specifically, when WiMax is in the ETLS process)
				//When L3 is resetting the device it will be sending the coex reason.
				//L4 will shut RF off here and don't do a fast reconnect.

				L4db_SetSwRfState(Off);
				GetCoexistenceMode(&coexMode,&msgLength);
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_IsFastReconnectRequired User call set to FALSE");
				UserCall = FALSE;
				SetCoexistanceMode(&coexMode,msgLength);
				res = FALSE;
				break;
			}
		default:
			res = FALSE;
			break;
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_IsFastReconnectRequired (OUT) res %d", res);
	return res;
}

// handle Fast Reconnect
BOOL L4C_HandleFastReconnect(wmx_pSystemStateUpdate systemStateUpdate)
{
	UINT32 disableFastReconnect = 0;
	BOOL res;
	BOOL isFastReconnectEnabled = FALSE;
	wmx_NSPid_t lastConnectedNSPid;
	wmx_Status_t status = WMX_ST_OK;
	BOOL isHandled = FALSE;
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_HandleFastReconnect (IN)");
	
	// if this system state contains a valid reason for fast reconnect
	if (L4C_IsFastReconnectRequired(systemStateUpdate))
	{
		res = L4Configurations_getDisableFastReconnect(&disableFastReconnect);
		if (res) // if the reg key exists - it overrides the L4DB settings
		{
			if( disableFastReconnect)
			{
				isFastReconnectEnabled = FALSE;
			}
			else
			{
				
				isFastReconnectEnabled = TRUE;
			}
		}
		else // use L4DB settings
		{
			isFastReconnectEnabled = L4db_GetFastReconnectStatus();
		}

		if (!isFastReconnectEnabled) // Fast Reconnect is disabled
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Fast Reconnect is disabled. Skipping reconnection");
		}
		else // fast reconnect is enabled
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Starting fast reconnect. Reason: %s", WMX_STATE_REASON_STR(systemStateUpdate->ReportStateReason));
			// get the recent connected NSP
			status = L4db_GetRecentNsp(&lastConnectedNSPid);
			if(status != WMX_ST_OK)
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_WARNING, "Fast reconnect canceled since the recent NSP list is empty");
			}
			else // we found a recent NSP to connect to so start connecting
			{
				// try to reconnect to the recent connected NSP
				status = NDnSAgent_StartConnect(lastConnectedNSPid, COEX_PRIORITY_CONNECT_LOW, TRUE, FALSE);

				if (status != WMX_ST_OK) // we couldn't auto-connect to any of the provisioned NSPs, so start timer
				{
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Fast reconnect failed");
				}
				else
				{
					isHandled = TRUE; // reconnect started successfully
				}
			}
		}
	}
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_HandleFastReconnect (OUT)");
	return isHandled;
}

BOOL L4C_HandleRobustConnect(wmx_pSystemStateUpdate systemStateUpdate, BOOL isL3Trigger)
{
	UINT32 disableRobustConnect = 0;
	BOOL res;
	wmx_NSPid_t nspId;
	wmx_Status_t status = WMX_ST_OK;
	BOOL isHandled = FALSE;
	BOOL isConnectRequired = FALSE;
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_HandleRobustConnect (IN)");
	UNREFERENCED_PARAMETER(systemStateUpdate);

	//if (systemStateUpdate->ReportStateReason == StateReasonFailToConnectEAPAuth)
	//{
	//	g_ndnsContext.eapContext.isEapFail = TRUE;
	//}
	res = L4Configurations_getDisableRobustConnect(&disableRobustConnect);

	if (g_ndnsContext.eapContext.isStarted && g_ndnsContext.eapContext.isEapFail)
	{
		if (!g_ndnsContext.eapContext.isPerformedFallBack) //If did not get the change to perform fall back - still have to perform trials...
		{
			if (res) // if the reg key exists - it overrides default settings
			{
				if(!disableRobustConnect)
				{
					isConnectRequired = TRUE;
				}
				else
				{
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Robust Connect is disabled. Skipping connection attempt");
				}
			}
			else // default - robust connect is enabled
			{
				isConnectRequired = TRUE;
			}
		}
	}

	if(isL3Trigger)
	{
		if (disableRobustConnect)
		{
			g_ndnsContext.eapContext.isEapFail = FALSE;	// In case we are not in robust connect mode - case not to fallback
		}
		NDnSAgent_SetConfigToSupplicant(Ndns_GetConnectingNSPTemp());
		isHandled = TRUE;
	}
	else
	{
		if (isConnectRequired) // if robust connect conditions are valid
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Starting robust connect sequence");
			// get the recent connected NSP
			nspId = Ndns_GetConnectingNSPTemp();

			// try to connect to the NSP used in the last unsuccessful attempt
			status = NDnSAgent_StartConnect(nspId, g_ndnsContext.tempCoexPriority, TRUE, FALSE);

			if (status != WMX_ST_OK) // we couldn't auto-connect to any of the provisioned NSPs, so start timer
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Robust connect failed");

			}
			else
			{
				isHandled = TRUE; // reconnect started successfully
			}
		}
	}

	g_ndnsContext.eapContext.isEapFail = FALSE; //	A trial is done handling of next trials with new inputs.

	if(!isHandled)
	{
		g_ndnsContext.eapContext.isStarted = FALSE;
		g_ndnsContext.eapContext.numEapRetries = 0;
		g_ndnsContext.eapContext.isEapFail = FALSE;
		g_ndnsContext.eapContext.isPerformedFallBack = FALSE;
	}
	
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_HandleRobustConnect (OUT)");
	return isHandled;
}

void L4C_CallHandleRobustConnect(BOOL isL3Trigger)
{
	wmx_SystemStateUpdate systemStateUpdate;
	OSAL_ZeroMemory(&systemStateUpdate, sizeof(systemStateUpdate));
	L4C_HandleRobustConnect(&systemStateUpdate, isL3Trigger);
}

// handle all the reconnect use cases
BOOL L4C_HandleReconnect(wmx_pSystemStateUpdate systemStateUpdate)
{
	BOOL isHandled = FALSE;
	L4CState l4CState = FSM_GetState(&g_ndnsContext.fsm);
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_HanldeReconnect (IN)");
	if (l4CState == L4C_CONNECTED) // fast reconnect
	{
		isHandled = L4C_HandleFastReconnect(systemStateUpdate);
		// reset the manual scan type (important after scan for reconnect attempt)
		g_ndnsContext.scheduledTaskData.scanInfo.manualScanType = SCAN_TYPE_PREFERRED;
	}
	else if (l4CState == L4C_CONNECTING) // robust connect
	{
		isHandled = L4C_HandleRobustConnect(systemStateUpdate, FALSE);
		// reset the manual scan type (important after scan for reconnect attempt)
		g_ndnsContext.scheduledTaskData.scanInfo.manualScanType = SCAN_TYPE_PREFERRED;
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_HanldeReconnect (OUT)");
	return isHandled;
}

wmx_Status_t L4C_HandleDataPathConnected(wmx_pSystemStateUpdate systemStateUpdate, wmx_SystemState_t previousSystemState)
{
	wmx_NAPid_t tmpNapId;
	wmx_NSPid_t tmpNSPid = (wmx_NSPid_t)L4DB_INVALID_INDEX;
	UINT32 connectTime;
	wmx_Status_t currentPreferredStatus;
	wmx_Status_t nspSt = WMX_ST_OK, napSt = WMX_ST_OK;
	wmx_LinkStatus_t linkStatus;
	wmx_Status_t status;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Handling DataPathConnected system state. Current state=%d, previous state=%d", systemStateUpdate->SystemState, previousSystemState);

	// we update the connection time only if we're not already in DataPathConnected
	if ( systemStateUpdate->SystemState == DataPathConnected &&
		(!(previousSystemState == DataPathConnected || previousSystemState == Idle)))
	{
		//Reset link status structure per new datapath connection
		memset(&g_linkStatus, 0, sizeof(wmx_LinkStatus_t));

		// DataPathConnected after restart
		if (FSM_GetState(&g_ndnsContext.fsm) == L4C_UNINITIALIZED)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "DataPathConnected system state after restart detected");

			nspSt = L4db_GetRecentNsp(&tmpNSPid);
			if (nspSt != WMX_ST_OK)
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Error finding recent NSP info");
			}

			memset(tmpNapId, L4DB_INVALID_INDEX, sizeof(wmx_NAPid_t)); // set the NAP ID to be empty

			if (!L4db_GetRecentNAP(tmpNapId))
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Reading connected BsID from the device");
				status = wmx_GetLinkStatus(&linkStatus);
				if( WMX_ST_OK != status )
				{
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Error reading connected BsID from the device");
				}
				else
				{
					OSAL_memcpy_s(tmpNapId, sizeof(wmx_NAPid_t), linkStatus.bsId, sizeof(wmx_NAPid_t));
				}
				napSt = WMX_ST_FAIL;
			}
		}
		else // move from "connecting" state to "connected" state
		{
			if (initialCoexMode == WMX_MODE_CM)
			{
				// Connection is over. Send ProcessEnd
				wmx_CmdProcessEnd();
			}

			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Using cached values of NSP and NAP IDs from connection attempt");
			// use the cached NSP ID
			tmpNSPid = Ndns_GetConnectingNSPTemp();  // Get the temp connecting NSP and make it the last connected
			// use the cached NAP ID
			Ndns_GetConnectingNAPTemp((wmx_pNAPid_t)&tmpNapId);
		}

		// we have a connect NAP ID but no NSP ID - try to use the NSP ID = NAP ID bit
		if (nspSt != WMX_ST_OK && napSt == WMX_ST_OK)
		{
			//TODO: [Oran]

		}

		L4db_SetConnectedNap(tmpNapId);
		connectTime = OSAL_timeGetTime();
		L4db_SetConnectionTime (connectTime);

		// in case there was no recent NSP info or there was a mismatch between the NAP and NSP info - don't update the NSP info
		if (nspSt == WMX_ST_OK)
		{
			L4db_SetConnectedNsp(tmpNSPid);
			if(TRUE == L4db_GetSetPreferredNspOnConnectState())
			{
				currentPreferredStatus = L4db_SetCurrentPreferredNsp(tmpNSPid);
				// Check if the change was successful
				if (currentPreferredStatus == WMX_ST_OK)
				{
					NDnSAgent_SendCurrentPreferredNSPChanged(1, &tmpNSPid);
				}
			}
		}

		if (CheckIfNspInAvailableList(tmpNSPid) == FALSE)
		{
			wmx_NSP_t nsp;
			wmx_BSid_t bsId;
			wmx_BsIdType_t bsIdType = FULL_NAP; // TODO: check if the BsID type should be retreived (and saved) from the L4DB

			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Connected to NSP ID = %d but it is not contained in the available NSPs list. Synchronizing the available NSPs list", tmpNSPid);

			// the available NSPs list is doesn't contain the currently connected NSP (could be possible when the ND&S is
			// restarted when the device is already connected).
			// update the available NSPs list with that NSP.
			OSAL_ZeroMemory(&nsp, sizeof(wmx_NSP_t));
			if ( GetNspStruct(tmpNSPid, &nsp) != WMX_ST_OK)
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Error getting NSP info for NSP ID = %d. AvailableNSPsList will not be synchronized with the current connected NSP.", tmpNSPid);
				nspSt = WMX_ST_FAIL;
			}
			else
			{
				OSAL_ZeroMemory(&bsId, sizeof(wmx_BSid_t));
				OSAL_memcpy_s(&bsId, sizeof(wmx_NAPid_t), &tmpNapId, sizeof(wmx_NAPid_t));
				if (WMX_ST_OK == AddMultipleNSPsToAvailableNSPsList(1, &nsp, &bsId, &bsIdType))
				{
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NSP ID = %d was added to the available NSPs list. List synchronized", tmpNSPid);
				}
				else
				{
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Error adding NSP ID = %d to the available NSPs list. List will not be synchronized", tmpNSPid);
					nspSt = WMX_ST_FAIL;
				}
			}
		}
		else
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NSP ID=%d is in the available NSPs list. No further handling is required", tmpNSPid);
		}
	}
	g_ndnsContext.scheduledTaskData.scanInfo.manualScanType = SCAN_TYPE_PREFERRED;
	FSM_SetState(&g_ndnsContext.fsm, L4C_CONNECTED);
	return nspSt;
}

// handle system state = Ready
wmx_Status_t L4C_HandleReadyState(wmx_pSystemStateUpdate systemStateUpdate, BOOL *isReset)
{
	BOOL isHandled = FALSE;
	wmx_Status_t status = WMX_ST_OK;
	UINT32 phaseNum = 2, i;

	*isReset = FALSE;

	while (!isHandled)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Executing the current scheduled task: <%s>", L4C_TaskStr(g_ndnsContext.scheduledTask));

		L4db_SetSwRfState(On);
		wmx_InternalRfOn();
		switch(g_ndnsContext.scheduledTask)
		{
		case Task_StartScan:
			// if a connect process was on, this means that it failed
			if (L4C_GetInitialCoexMode() == WMX_MODE_CM &&
				g_ndnsContext.processStarted &&
				FSM_GetState(&g_ndnsContext.fsm) == L4C_CONNECTING)
			{
				L4C_SetProcessStarted(FALSE);
				wmx_CmdProcessEnd();
			}
			// handle all reconnect use cases, if reconnect is required
			isHandled = L4C_HandleReconnect(systemStateUpdate);

			if (!isHandled) // no reconnect attempt / reconnect failed
			{
				if (L4S_READY == L4S_GetScannerState())
				{
					if (OSAL_atomic_compareExchange(&g_ndnsContext.isResumeScan, FALSE, TRUE))
					{
						TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx: Resuming scan");
						// since we are about to begin/resume a scan - send the scan type to the L4 publisher
						L4Pub_UpdateData(L4Pub_CbType_ScanType, (L4Pub_pCallbackParamsUnion)&g_ndnsContext.scheduledTaskData.scanInfo.manualScanType);
						pUtils->tpfnPostRequest(MEDIUM, 0, NULL, 0, &L4C_ResumeScan); // schedule the next scan task
						status = WMX_ST_OK;
					}
					else
					{
						TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx: A new scan is requested. Checking scan mode...");
						// reset the manual scan type (important after scan for reconnect attempt)
						g_ndnsContext.scheduledTaskData.scanInfo.manualScanType = SCAN_TYPE_PREFERRED;
						// since we are about to begin/resume a scan - send the scan type to the L4 publisher
						L4Pub_UpdateData(L4Pub_CbType_ScanType, (L4Pub_pCallbackParamsUnion)&g_ndnsContext.scheduledTaskData.scanInfo.manualScanType);
						status = L4C_SetScanMode(TRUE); // system state is Ready so in auto mode we can start the scan
					}
				}
				else
				{
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "L4C_SetControllerModeEx: SystemState = Ready but L4S is still scanning. Skipping state processing");
				}
				isHandled = TRUE;
			}
			break;

		case Task_StartManualScan:
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Executing secheduled manual scan");
			L4C_ScheduleTask(Task_StartScan); // after the scan session is over - return to normal scan mode
			L4C_UpdateScanStatus(FALSE);
			OSAL_atomic_exchange(&g_ndnsContext.isSingleManualScan, (long)FALSE);
			// in case this is a fast reconnect scan cycle - make sure fast reconnect is allowed
			if ((g_ndnsContext.scheduledTaskData.scanInfo.manualScanType != SCAN_TYPE_RECONNECT) ||
				(L4db_GetFastReconnectStatus() == TRUE))
			{
				L4C_StartManualScanPhase2(g_ndnsContext.scheduledTaskData.scanInfo.manualScanType,
										   g_ndnsContext.scheduledTaskData.scanInfo.isInternal);
				isHandled = TRUE;
			}
			break;

		case Task_Connect:
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Executing secheduled Connect");
			L4C_ScheduleTask(Task_StartScan); // after the connect session is over - return to normal scan mode
			L4C_UpdateScanStatus(FALSE);
			OSAL_atomic_exchange(&g_ndnsContext.isSingleManualScan, (long)FALSE);
			NDnSAgent_ConnectPhase2(&g_ndnsContext.scheduledTaskData.connectInfo);
			isHandled = TRUE;
			break;

		case Task_SetConnectMode:
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Executing secheduled SetConnectMode");
			NDnSAgent_OnProgressDualFlush(Task_SetConnectMode, NDnSAgent_SetConnectModePhase2, phaseNum);
			isHandled = TRUE;
			break;

		case Task_AutoConnect:
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Executing secheduled AutoConnect. forceManualConnect=%d", g_ndnsContext.scheduledTaskData.connectInfo.forceManualConnect);
			L4C_ScheduleTask(Task_StartScan);
			// go over the NSPs and try to connect. Stop on success or when there are no more NSPs.
			for (i = 0; i < g_ndnsContext.scheduledTaskData.connectInfo.numOfNspIDs; ++i)
			{
				status = NDnSAgent_StartConnect(g_ndnsContext.scheduledTaskData.connectInfo.nspIDs[i],
					g_ndnsContext.scheduledTaskData.connectInfo.coexPriority,
					g_ndnsContext.scheduledTaskData.connectInfo.forceManualConnect,
					g_ndnsContext.scheduledTaskData.connectInfo.useOnlyLastChannel);
				if (WMX_ST_OK == status) break;
			}

			if (status != WMX_ST_OK) // we couldn't auto-connect to any of the provisioned NSPs, so start timer
			{
				OSAL_atomic_exchange(&g_ndnsContext.isResumeScan, TRUE);  // continue to scan
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Couldn't auto-connect to any of the detected home NSPs. Resuming scan.");
			}
			else
			{
				isHandled = TRUE; // auto connect started successfully
			}

			break;

		case Task_StopScan:
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx(OUT): Reset/StopScan during system state Ready requested");
			OSAL_atomic_exchange(&g_ndnsContext.isResumeScan, FALSE); // go back to initial state
			NDnSAgent_OnProgressDualFlush(Task_StopScan, NULL, phaseNum);
			L4C_HandleReadyState(systemStateUpdate, isReset);
			//L4C_ScheduleTask(Task_StartScan);
			//L4C_UpdateScanStatus(FALSE);
			break;

		case Task_Reset:
		default:
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx(OUT): Reset/StopScan during system state Ready requested");
			*isReset = TRUE;
			isHandled = TRUE;
			L4C_HandleReadyState(systemStateUpdate, isReset);
			break;
		}
	}
	return status;
}

// configure the L4C to the correct mode
wmx_Status_t L4C_SetControllerModeEx(wmx_pSystemStateUpdate systemStateUpdate, BOOL verifyState)
{
	wmx_Status_t status = WMX_ST_OK;
	wmx_SystemState_t systemState = systemStateUpdate->SystemState;
	wmx_RfSwitchesStatus_t rfSwitches = systemStateUpdate->rfSwitchesStatus;
	static wmx_RfStatus_t hwRfStatus = On;
	wmx_SystemState_t previousSystemState;
	L4CState l4cLastState = FSM_GetState(&g_ndnsContext.fsm);
	BOOL isReset = FALSE;
	UINT32 disableFastReconnect = 0;
	BOOL res;
	L4Pub_States_t pubStates;
	//char readFromNVM[MAX_ANSWER_SIZE];
	wmx_CoexistenceMode_t CoexMode;
	UINT32 msgLength = sizeof(wmx_CoexistenceMode_t);
	char readFromNVM[MAX_ANSWER_SIZE];
	if (systemStateUpdate->SystemState != UnInitialized)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx(IN): systemStateUpdate=%s, swRfStatus=%d, hwRfStatus=%d",
		      NDnSSystemStates(systemStateUpdate->SystemState), systemStateUpdate->rfSwitchesStatus.swRfStatus,
		systemStateUpdate->rfSwitchesStatus.hwRfStatus);
	}
	else
	{
		ClearAvailableNSPsList();
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx(IN): systemStateUpdate = UnInitialized");
	}

	if (L4C_GetInitialCoexMode() == WMX_MODE_CM &&
		systemStateUpdate->ReportStateReason == StateReasonFailToConnectCoexNoRF)
	{	// true when RF has just been taken by WiFi. Need to send indication to CM
		L4C_SetProcessStarted(FALSE);
		// Need to stop the scan  send notification 
		NDnSAgent_RfTakenIndication();
		// disable "g_l4PublisherContext.isScanning" flag to avoid next time scan or wide scan
		L4Pub_UpdateScanState(FALSE);
	}

	OSAL_enter_critical_section(&g_ndnsContext.lockSystemState);

	if ((verifyState) &&
		(g_ndnsContext.systemState == systemState) &&
		(systemState != RfOff) && // we allow RfOff to track the changes in the RF switches
		(systemState != Connecting)) // we allow Connecting to track changes in the Connection Progress
	{
		OSAL_exit_critical_section(&g_ndnsContext.lockSystemState);
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx(OUT). status=%d", status);
		return status;
	}
	previousSystemState = g_ndnsContext.systemState;
	g_ndnsContext.systemState = systemState;
	OSAL_exit_critical_section(&g_ndnsContext.lockSystemState);

	OSAL_enter_critical_section(&g_ndnsContext.lockSetMode);

	// synchronize with the driver
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx. systemState=%d", systemState);
	switch(systemState)
	{
		case Ready:
			{
				status = L4C_HandleReadyState(systemStateUpdate, &isReset);
				if (isReset)
				{
					OSAL_exit_critical_section(&g_ndnsContext.lockSetMode);
					return WMX_ST_OK;
				}
				break;
			}
		case DataPathConnected:
		case Idle:
			{
				L4C_HandleDataPathConnected(systemStateUpdate, previousSystemState);
				break;
			}
		case Config:
			{
				L4CState currentState = FSM_GetState(&g_ndnsContext.fsm);
				L4CState previousState = FSM_GetPreviousState(&g_ndnsContext.fsm);

				// this a reoccuring assert - cancel the fast reconnect
				if (g_ndnsContext.scheduledTaskData.scanInfo.manualScanType == SCAN_TYPE_RECONNECT)
				{
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Reoccuring assert. Skipping reconnection");
					L4C_ScheduleTask(Task_StartScan);
				}
				// in case of device assert - schedule a manual scan and reconnect attempts
				if ((currentState == L4C_CONNECTED ||
					currentState == L4C_UNINITIALIZED && previousState == L4C_CONNECTED) &&
					L4C_IsFastReconnectRequired(systemStateUpdate) &&
					g_ndnsContext.scheduledTaskData.scanInfo.manualScanType != SCAN_TYPE_RECONNECT)
				{
					res = L4Configurations_getDisableFastReconnect(&disableFastReconnect);
					if ((res && disableFastReconnect) || (L4db_GetFastReconnectStatus() == FALSE)) // Fast Reconnect is disabled
					{
						TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Fast Reconnect (assert) is disabled. Skipping reconnection");
					}
					else
					{
						// this is the first time we get the fast reconnect trigger after the assert
						TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Fast Reconnect (assert) activated. Scheduling reconnection");
						g_ndnsContext.scheduledTaskData.scanInfo.manualScanType = SCAN_TYPE_RECONNECT;
						L4C_ScheduleTask(Task_StartManualScan);
					}
				}
				//// If registry key is set to true update L4db from NVM
				//L4Configurations_getAPDOReadFromNvm(readFromNVM);

				//if (0 == _wcsicmp(DEFAULT_TRUE_LOWER, readFromNVM))
				//{
				//	L4db_UpdateFromNVM();
				//}
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Device in config state, setting coex mode as well !");
			}
		// Fall Down
		case UnInitialized:
		case Initialized:
			{
				L4S_Reset(TRUE); // reset the L4 scanner in a hard manner, meaning don't rely on driver indications
				FSM_SetState(&g_ndnsContext.fsm, L4C_UNINITIALIZED);
				GetCoexistenceMode(&CoexMode, &msgLength);
				UserCall = FALSE;
				SetCoexistanceMode(&CoexMode, msgLength);
				// If registry key is set to true update L4db from NVM
				L4Configurations_getAPDOReadFromNvm(readFromNVM);

			if (0 == OSAL_stricmp(DEFAULT_TRUE_LOWER, readFromNVM))
			{
				L4db_UpdateFromNVM();
			}
			break;
		}
	case RfOff:
	case RfShutDown:
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx : Device in RfShutDown\\RfOff state, checking -->");
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx : if ((FSM_GetState(&g_ndnsContext.fsm) == L4C_UNINITIALIZED) --> [%d] || (systemStateUpdate->rfSwitchesStatus.hwRfStatus == Off) --> [%d] && (systemStateUpdate->rfSwitchesStatus.swRfStatus == Off)) --> [%d]", (FSM_GetState(&g_ndnsContext.fsm) == L4C_UNINITIALIZED), (systemStateUpdate->rfSwitchesStatus.hwRfStatus == Off), (systemStateUpdate->rfSwitchesStatus.swRfStatus == Off));
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx : FSM_GetState(&g_ndnsContext.fsm)==[%d]",FSM_GetState(&g_ndnsContext.fsm));
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx : ((rfSwitches.hwRfStatus == %s && hwRfStatus == %s) && (FSM_GetState(&g_ndnsContext.fsm) == %d)", (rfSwitches.hwRfStatus==On ? "On" : "Off"), (hwRfStatus==On ? "On" : "Off") , FSM_GetState(&g_ndnsContext.fsm));
			if ((rfSwitches.hwRfStatus == On && hwRfStatus == Off) && (FSM_GetState(&g_ndnsContext.fsm) == L4C_UNINITIALIZED || FSM_GetState(&g_ndnsContext.fsm) == L4C_RF_OFF))
			{
				// Set the RF state of the driver based on the L4DB (only during ND&S init)
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx : checking --> L4db_GetSwRfState() == On --> [%d] (Set the RF state of the driver based on the L4DB (only during ND&S init))", (L4db_GetSwRfState() == On));
				if (systemStateUpdate->rfSwitchesStatus.hwRfStatus == On && (L4db_GetSwRfState() == On ))
				{
					wmx_InternalRfOn(); // Maybe the rf is already on, ignore the error in this case.
				}
				status = WMX_ST_OK;
			}
			else if (FSM_GetState(&g_ndnsContext.fsm) == L4C_UNINITIALIZED && rfSwitches.hwRfStatus == On)
			{
				// Set the RF state of the driver based on the L4DB (only during ND&S init)
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx : checking --> L4db_GetSwRfState() == On --> [%d] (Set the RF state of the driver based on the L4DB (only during ND&S init))", (L4db_GetSwRfState() == On));
				if (systemStateUpdate->rfSwitchesStatus.hwRfStatus == On && (L4db_GetSwRfState() == On ))
				{
					wmx_InternalRfOn(); // Maybe the rf is already on, ignore the error in this case.
				}
				status = WMX_ST_OK;
			}
			else
			{
				if (systemState == RfShutDown)
				{
					L4db_SetSwRfState(systemStateUpdate->rfSwitchesStatus.swRfStatus);
				}
				L4S_Reset(TRUE); // reset the L4 scanner in a hard manner, meaning don't rely on driver indicationss	
			}
			hwRfStatus = rfSwitches.hwRfStatus;
			FSM_SetStateIfNotEqual(&g_ndnsContext.fsm, L4C_RF_OFF, L4C_RF_OFF);
			break;
		}
	default:
		{
			break;
		}
	}

	// now, after the state has been aligned, reset any old actions (scan / connect / disconnect)
	L4C_Reset(l4cLastState, systemStateUpdate);

	OSAL_exit_critical_section(&g_ndnsContext.lockSetMode);

	// [Oran]
	//L4Pub_UpdateData(L4Pub_CbType_ScanType, (L4Pub_pCallbackParamsUnion)&g_ndnsContext.scheduledTaskData.scanInfo.manualScanType);
	pubStates.l4cState = FSM_GetState(&g_ndnsContext.fsm);
	pubStates.pSystemStateUpdate = systemStateUpdate;
	L4Pub_UpdateData(L4Pub_CbType_StatesChange, (L4Pub_pCallbackParamsUnion)&pubStates);

	//L4Pub_UpdateDeviceStatus(systemStateUpdate, FSM_GetState(&g_ndnsContext.fsm));
	//////////////////////

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetControllerModeEx(OUT): status=%d", status);
	return status;
}

wmx_Status_t L4C_SetControllerMode(wmx_SystemState_t systemState, BOOL verifyState)
{
	wmx_SystemStateUpdate systemStateUpdate;
	wmx_SystemState_t tempSystemState;
	wmx_Status_t status;

	memset(&systemStateUpdate, 0, sizeof(wmx_SystemStateUpdate));
	systemStateUpdate.SystemState = systemState;

	// update internal data
	status = wmx_GetSystemState( &tempSystemState,
		&systemStateUpdate.ConnectProgress,
		&systemStateUpdate.rfSwitchesStatus );
	if( WMX_ST_OK != status )
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_SetControllerMode: Error getting system state from the device. System state processing will be inaccurate. Status=%d", status);
	}

	if (UnknownState == systemState && WMX_ST_OK == status)
	{
		systemStateUpdate.SystemState = tempSystemState;
	}
	else if (tempSystemState != systemState)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_WARNING, "L4C_SetControllerMode: input system state <> device system state");
	}

	// set the rest of the fields of the system state update struct
	systemStateUpdate.ReportStateReason = StateReasonNormal;

	memset(systemStateUpdate.linkStatus.bsId, 0xFF, sizeof(wmx_BSid_t));

	return L4C_SetControllerModeEx(&systemStateUpdate, verifyState);
}

void L4C_Reset(L4CState l4cState, wmx_pSystemStateUpdate systemStateUpdate)
{
	wmx_SystemState_t systemState = systemStateUpdate->SystemState;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "L4 Controller reset (IN)");

	// if we tried to connect and now we're done (either success or failure)
	if ((l4cState == L4C_CONNECTING) && (FSM_GetState(&g_ndnsContext.fsm) != L4C_CONNECTING))
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Connect done");

		g_ndnsContext.eapContext.isStarted = FALSE;
		g_ndnsContext.eapContext.numEapRetries = 0;
		g_ndnsContext.eapContext.isEapFail = FALSE;
		g_ndnsContext.eapContext.isPerformedFallBack = FALSE;
		L4S_EnableScanner(TRUE);
		OSAL_set_event(g_ndnsContext.connectCompletedEvent); // Connect // TODO: [Oran] remove - stop scan
		OSAL_set_event(g_ndnsContext.dualFlushOpEvent); // Connect
		NDnSAgent_SendConnectCompleted(g_ndnsContext.connectStatus);

	}
	else if ((l4cState == L4C_DISCONNECTING) &&
		(systemState == Ready ||
		systemState == RfOff ||
		systemState == RfShutDown))
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Disconnect completed");
		L4db_SetConnectionTime(DEFAULT_TIME);
		OSAL_set_event(g_ndnsContext.disconnectCompletedEvent); // Disconnect
	}

	if (((g_ndnsContext.scheduledTask == Task_Connect)  ||
		(g_ndnsContext.scheduledTask == Task_StartManualScan) ||
		(g_ndnsContext.scheduledTask == Task_AutoConnect)) &&
		(systemState == UnInitialized ||
		systemState == Config ||
		systemState == Initialized ||
		systemState == RfOff ||
		systemState == RfShutDown))
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Connect done");
		L4S_EnableScanner(TRUE);
		if (g_ndnsContext.scheduledTaskData.scanInfo.manualScanType != SCAN_TYPE_RECONNECT &&
			g_ndnsContext.scheduledTaskData.scanInfo.manualScanType != SCAN_TYPE_AUTO_CONNECT)
		{
			L4C_ScheduleTask(Task_StartScan); // return to normal mode - start scan on next Ready state
		}
		// TODO: [Oran] - check if the "if" condition above should contain also "Task_StopScan" and "Task_SetConnectMode"
		OSAL_set_event(g_ndnsContext.dualFlushOpEvent); // Connect completed event
		OSAL_set_event(g_ndnsContext.connectCompletedEvent); // Connect //TODO: [Oran] remove - stop scan
	}

	if (systemState != Ready)
	{
		if (!L4S_IsScanning())
		{
			OSAL_atomic_exchange(&g_ndnsContext.isSingleManualScan, (long)FALSE);
		}
		OSAL_atomic_exchange(&g_ndnsContext.isResumeScan, (long)FALSE);
	}
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "L4 Controller reset (OUT)");
}

void L4C_ScheduleTask(L4C_Task task)
{
	OSAL_atomic_exchange((LONG *)&g_ndnsContext.scheduledTask, (LONG)task);
}

// Generate a preferred channel plan using the L4Planner and configure the L4Scanner
wmx_Status_t L4C_SetPS(wmx_LinkLossType_t linkLossType, BOOL delayStart, BOOL isInternal)
{
	wmx_CoexPriority_t coexPriority;
	NdsSettings_t settings; // L4db struct
	wmx_ChannelInfo_t mcp[MAX_CHANNELS_NUM];
	UINT32 numChannels = MAX_CHANNELS_NUM;
	int repetitionsCount, delayTime;
	wmx_Status_t rc;
	wmx_Status_t monitorStatus;
	wmx_NSPid_t currentPreferredNspId;
	wmx_ScanPolicy_t scanPolicy;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "L4C_SetPS(IN)");
	L4db_GetNdsSettings(&settings);

	L4db_PrintDiscoveredNAPsList();
	rc = L4P_GenerateMCP(ScanType_PreferredList, mcp, &numChannels);

	if (numChannels < 1)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_SetPS: Couldn't generate master channel plan for preferred scan. No channels found.");
		return WMX_ST_WRONG_ARGUMENT;
	}

	if (rc != WMX_ST_OK || numChannels == MAX_CHANNELS_NUM)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_SetPS: Couldn't generate master channel plan for preferred scan");
		return WMX_ST_WRONG_ARGUMENT;
	}

	// if we got here - then we have a valid MCP
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetPS: Master channel plan generated and contains %d channels", numChannels);


	// AutoScan / SemiManualScan = COEX_PRIORITY_SCAN_LOW, ManualScan = COEX_PRIORITY_SCAN_HIGH
	if (isInternal)
	{
		coexPriority = (settings.connectMode == UserManScanManConnect) ?
						COEX_PRIORITY_SCAN_HIGH : COEX_PRIORITY_SCAN_LOW;
	}
	else // external manual scan command
	{
		coexPriority = COEX_PRIORITY_SCAN_HIGH;
	}

	// configure the correct link loss parameters
	monitorStatus = wmx_PopulateMonitorMCP(mcp,numChannels,linkLossType);
	switch (linkLossType)
	{
		case LinkLossType_Short:
			repetitionsCount = settings.shortLinkLossParams.repetition;
			delayTime = settings.shortLinkLossParams.time;
			break;
		case LinkLossType_Long:
			repetitionsCount = settings.longLinkLossParams.repetition;
			delayTime = settings.longLinkLossParams.time;
			break;
		case LinkLossType_Infinite:
			repetitionsCount = MAXSHORT;
			delayTime = (int)(INFINITE_LINK_LOSS_RATIO * settings.longLinkLossParams.time);
			break;
		default: // short link loss is the default
			repetitionsCount = settings.shortLinkLossParams.repetition;
			delayTime = settings.shortLinkLossParams.time;
			break;
	}

	//setting the flag of the currentPreferredNspId
	rc = L4db_GetCurrentPreferredNsp(&currentPreferredNspId);

	// if the current preferred NSP is set - use FIND FIRST, otherwise use FIND ALL (=out of the box preferred scan)
	if((WMX_ST_OK == rc) && ((wmx_NSPid_t)L4DB_INVALID_INDEX != currentPreferredNspId))
	{
		scanPolicy = SCAN_MODE_FIND_FIRST;
	}
	else
	{
		scanPolicy = SCAN_MODE_FIND_ALL;
	}

	//g_ndnsContext.currentScanType = SCAN_TYPE_PREFERRED;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Link Loss config: Type = %s, Delay = %d sec, Repetitions = %d", wmx_LinkLossType_tStr(linkLossType), delayTime, repetitionsCount);
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "L4C_SetPS(OUT)");
	return L4S_SetPSCfg(mcp,
						numChannels,
						repetitionsCount,
						delayTime,
						scanPolicy,
						coexPriority,
						delayStart);
}

// Generate a wide channel plan using the L4Planner and configure the L4Scanner
wmx_Status_t L4C_SetWS(wmx_LinkLossType_t linkLossType, BOOL delayStart, BOOL isInternal)
{
	wmx_CoexPriority_t coexPriority = COEX_PRIORITY_SCAN_LOW;
	NdsSettings_t settings; // L4db struct
	wmx_ChannelInfo_t mcp[MAX_CHANNELS_NUM];
	UINT32 numChannels = MAX_CHANNELS_NUM;
	int repetitionsCount, delayTime;
	wmx_Status_t rc;
	wmx_Status_t monitorStatus;

	L4db_GetNdsSettings(&settings);
	// TODO: Oran - each channel in the MCP has 0 in the MaxTxPower field
	rc = L4P_GenerateMCP(ScanType_WhileScan, mcp, &numChannels);
	monitorStatus = wmx_PopulateMonitorMCP(mcp,numChannels,linkLossType);

	if (numChannels < 1)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetWS: Couldn't generate master channel plan for wide scan. No channels found.");
		return WMX_ST_WRONG_ARGUMENT;
	}

	if (rc != WMX_ST_OK || numChannels == MAX_CHANNELS_NUM)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_SetWS: Couldn't generate master channel plan for preferred scan");
		return WMX_ST_WRONG_ARGUMENT;
	}

	// if we got here - then we have a valid MCP
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetWS: Master channel plan generated and contains %d channels", numChannels);

	// AutoScan / SemiManualScan = COEX_PRIORITY_SCAN_LOW, ManualScan = COEX_PRIORITY_SCAN_HIGH
	// in the current version only manual wide scan is available
	if (TRUE == OSAL_atomic_compareExchange(&g_ndnsContext.isSingleManualScan, (long)TRUE, (long)TRUE)
		&& !isInternal)
	{
		coexPriority = COEX_PRIORITY_SCAN_HIGH;
	}

	if (linkLossType == LinkLossType_Infinite)
	{
		repetitionsCount = MAXSHORT;
		delayTime = (int)(INFINITE_LINK_LOSS_RATIO * settings.longLinkLossParams.time);
	}
	else
	{
		repetitionsCount = settings.longLinkLossParams.repetition;
		delayTime = settings.longLinkLossParams.time;
	}

	//g_ndnsContext.currentScanType = SCAN_TYPE_WIDE;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Link Loss config: Type = %s, Delay = %d sec, Repetitions = %d", wmx_LinkLossType_tStr(linkLossType), delayTime, repetitionsCount);

	return L4S_SetWhileSCfg(mcp,
							numChannels,
							repetitionsCount,
							delayTime,
							coexPriority,
							delayStart);
}


// Configure the L4Scanner to scan a specially targeted plan of selected channels (for example: in a "scan-connect" sequence)
wmx_Status_t L4C_SetTargetedScan(wmx_LinkLossType_t linkLossType, BOOL delayStart)
{
	UNREFERENCED_PARAMETER(linkLossType);

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "L4C_SetTargetedScan(IN)");

	if (g_ndnsContext.scheduledTaskData.scanInfo.chPlanSize < 1)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_SetTargetedScan: Illegal channel plan detected. No channels found.");
		return WMX_ST_WRONG_ARGUMENT;
	}
	else
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SetTargetedScan: Master channel plan contains %d channels", g_ndnsContext.scheduledTaskData.scanInfo.chPlanSize);
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "L4C_SetTargetedScan(OUT)");

	return L4S_SetPSCfg(g_ndnsContext.scheduledTaskData.scanInfo.chPlan, // master channel plan
		g_ndnsContext.scheduledTaskData.scanInfo.chPlanSize, // channel plan size
		1, // repetition count
		0, // delay time
		SCAN_MODE_FIND_FIRST,
		g_ndnsContext.scheduledTaskData.connectInfo.coexPriority, // For the scan we use the same coex priority as the connect
		delayStart);
}


wmx_Status_t L4C_StartScan(wmx_ScanType_t scanType, wmx_LinkLossType_t linkLossType, BOOL delayStart, BOOL isInternal)
{
	wmx_Status_t status = WMX_ST_OK;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_StartScan(IN): type = <%s>\tlink loss type = <%s>\ttrigger = <%s%s>",
	        wmx_ScanType_tStr(scanType), wmx_LinkLossType_tStr(linkLossType),
		(g_ndnsContext.isSingleManualScan == TRUE ? NDNS_STR_MANUAL : NDNS_STR_AUTOMATIC),
		(delayStart == TRUE ? NDNS_STR_DELAYED : ""));

	g_ndnsContext.linkLossType = linkLossType;
	g_ndnsContext.currentScanType = scanType;

	L4S_EnableScanner(TRUE);
	switch(scanType)
	{
		case SCAN_TYPE_PREFERRED:
		case SCAN_TYPE_RECONNECT:
			status = L4C_SetPS(linkLossType, delayStart, isInternal);
			break;
		case SCAN_TYPE_WIDE:
			status = L4C_SetWS(linkLossType, delayStart, isInternal);
			break;
		case SCAN_TYPE_AUTO_CONNECT:
			status = L4C_SetTargetedScan(linkLossType, delayStart);
			break;
		case SCAN_TYPE_DIRECT:
			// don't read scan settings from the L4DB (overrided)
			break;
	}

	if (status != WMX_ST_OK)
	{
		goto Finalize;
	}

	// send the scan type to the L4 publisher
	L4Pub_UpdateData(L4Pub_CbType_ScanType, (L4Pub_pCallbackParamsUnion)&g_ndnsContext.scheduledTaskData.scanInfo.manualScanType);

	if (L4C_AUTO_SCAN == FSM_GetState(&g_ndnsContext.fsm))
	{
		status = L4S_StartScanner(FALSE);
	}
	else
	{
		status = L4S_StartScanner(TRUE);
	}

Finalize:
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_StartScan(OUT)");
	return status;
}

wmx_Status_t L4C_StartManualScanPhase2(wmx_ScanType_t scanType, BOOL isInternal)
{
	L4CState fsmState = FSM_GetState(&g_ndnsContext.fsm);

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "L4C_StartManualScanPhase2(IN) - scanType=%s, isInternal=%d",
	      wmx_ScanType_tStr(scanType), isInternal);
	L4S_Reset(TRUE); // reset the L4Scanner

	if (!FSM_SetStateIfNotEqual(&g_ndnsContext.fsm, L4C_SINGLE_SCAN, L4C_SINGLE_SCAN) &&
		(TRUE == OSAL_atomic_compareExchange(&g_ndnsContext.isSingleManualScan, (long)TRUE, (long)TRUE)))
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_StartManualScanPhase2(OUT). Unable to start manual scan since already in single scan state");
		g_ndnsContext.scheduledTaskData.status = WMX_ST_WRONG_STATE;
		FSM_SetStateIfNotEqual(&g_ndnsContext.fsm, fsmState, fsmState); // return to the previous state
		L4C_ScheduleTask(Task_StartScan); // reset the scheduler back to normal scan
		OSAL_atomic_exchange(&g_ndnsContext.isResumeScan, FALSE);
		L4C_SetControllerMode(Ready, FALSE); // trigger the next scan cycle
		goto Finalize;
	}
	OSAL_atomic_exchange(&g_ndnsContext.isSingleManualScan, (long)TRUE);

	// clear the available NSPs list only if the manual scan was commanded from outside of the ND&S
	if (scanType != SCAN_TYPE_AUTO_CONNECT && scanType != SCAN_TYPE_RECONNECT)
	{
		ClearAvailableNSPsList();
	}
	// the link loss type doesn't matter here since when the auto scan will start again - the link loss type
	// will be chosen based on the type of the scan
	g_ndnsContext.scheduledTaskData.status = L4C_StartScan(scanType, LinkLossType_Short, FALSE, isInternal);
	if (g_ndnsContext.scheduledTaskData.status != WMX_ST_OK)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_StartManualScanPhase2(OUT). Unable to start manual scan");
		FSM_SetStateIfNotEqual(&g_ndnsContext.fsm, fsmState, fsmState); // return to the previous state
		L4C_ScheduleTask(Task_StartScan); // reset the scheduler back to normal scan
		OSAL_atomic_exchange(&g_ndnsContext.isResumeScan, FALSE);
		L4C_SetControllerMode(Ready, FALSE); // trigger the next scan cycle
	}

Finalize:
	OSAL_set_event(g_ndnsContext.startManualScanCompletedEvent);

	return g_ndnsContext.scheduledTaskData.status;
}

wmx_Status_t L4C_StartManualScan(wmx_ScanType_t scanType)
{
	L4CState fsmState = FSM_GetState(&g_ndnsContext.fsm);
	wmx_Status_t status = WMX_ST_WRONG_STATE;
	wmx_ScanInfo_t scanInfo;

	// scan requests arriving outside of the ND&S can be only for preferred or wide scan
	if (scanType != SCAN_TYPE_PREFERRED && scanType != SCAN_TYPE_WIDE)
	{
		return WMX_ST_WRONG_ARGUMENT;
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_StartManualScan(IN)");

	// in case a scan/connect process is taking place, notify device
	if(L4C_GetInitialCoexMode() == WMX_MODE_CM && g_ndnsContext.processStarted)
	{
		L4C_SetProcessStarted(FALSE);
		wmx_CmdProcessEnd();
	}

	if (fsmState == L4C_AUTO_SCAN || fsmState == L4C_SINGLE_SCAN)
	{
		L4C_ScheduleTask(Task_Reset); // reset the L4Controller
		L4S_Reset(FALSE); // reset the L4Scanner

		OSAL_reset_event(g_ndnsContext.startManualScanCompletedEvent);

		scanInfo.manualScanType = scanType;
		scanInfo.isInternal = FALSE;

		pUtils->tpfnPostRequest(MEDIUM, Task_StartManualScan, (void*)&scanInfo, sizeof(wmx_ScanInfo_t), &L4C_TaskHandler);

		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Manual scan task was scheduled");

		if (OSAL_wait_event(g_ndnsContext.startManualScanCompletedEvent, L4C_TIMEOUT * 2) != WAIT_OBJECT_0)
		{
			L4C_ScheduleTask(Task_StartScan); // reset the scheduler back to normal scan
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Error starting manual scan. Timeout expired !");
			status = WMX_ST_FAIL;
		}
		else
		{
			status = g_ndnsContext.scheduledTaskData.status;
		}
	}
	/*else if (fsmState == L4C_SINGLE_SCAN)
	{
		OSAL_atomic_exchange(&g_ndnsContext.isSingleManualScan, (long)FALSE);
		ClearAvailableNSPsList();
		status = L4C_StartScan(scanType, LinkLossType_Short, FALSE, FALSE);
		if (status != WMX_ST_OK)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_StartManualScan. Error starting manual scan. status=%d",status);
		}
	}*/
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_StartManualScan(OUT). Status: %d", status);

	return status;
}

wmx_Status_t L4C_StartManualScanDirect(UINT8 *pChannelsListBuffer)
{
	L4CState fsmState = FSM_GetState(&g_ndnsContext.fsm);
	wmx_Status_t status = WMX_ST_WRONG_STATE;
	wmx_ScanType_t scanType = SCAN_TYPE_DIRECT;
	int listSize = 0;
	wmx_pChannelInfo_t pList = NULL;
	UINT32 numChannels = 0;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_StartManualScanDirect(IN)");

	// Copy values from the buffer to each field respectively. (The first 4 bytes of the buffer are the
	// Duplex mode that is not yet supported...)
	OSAL_memcpy_s(&numChannels, sizeof(UINT32), pChannelsListBuffer + sizeof(wmx_DuplexMode_t), sizeof(UINT32));
	OSAL_memcpy_s(&g_ndnsContext.scheduledTaskData.scanInfo.isDirectScanBlocking, sizeof(BOOL), pChannelsListBuffer + sizeof(wmx_DuplexMode_t) + sizeof(UINT32), sizeof(BOOL));
	listSize = sizeof(wmx_ChannelInfo_t) * numChannels;
	pList = (wmx_pChannelInfo_t)malloc(listSize);
	if (NULL == pList)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_StartManualScanDirect(OUT). malloc error. Could not allocate space for pList!");
		return status;
	}
	OSAL_memcpy_s(pList, listSize, pChannelsListBuffer + sizeof(wmx_DuplexMode_t) + sizeof(UINT32) + sizeof(BOOL), listSize);

	if (fsmState == L4C_SINGLE_SCAN)
	{
		status = L4S_SetPSCfg(pList,
							  numChannels,
							  1, // One scan cycle.
							  0, // No retries interval.
							  SCAN_MODE_FIND_ALL, // in direct scan - we want to scan all the channels
							  COEX_PRIORITY_SCAN_HIGH,
							  FALSE);

		if (status != WMX_ST_OK)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_StartManualScan(OUT). Bad status returned from L4S_SetPSCfg. status: %d", status);
		}
		else
		{
			status = L4C_StartScan(scanType, LinkLossType_Short, FALSE, FALSE);
			if (status != WMX_ST_OK)
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_StartManualScan(OUT). Error starting manual scan. status: %d", status);
			}
		}
	}
	else
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_StartManualScan - Error bad state. state: %d", fsmState);
	}

	free(pList);
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_StartManualScanDirect(OUT). status: %d", status);
	return status;
}

// send a scan results indication with all the available NSPs
void L4C_SendAvailableNSPsInd()
{
	UINT32 msgSize;
	UINT32 sendIndDataSize;
	SendIndData *pSendIndData;
	NDNS_MESSAGE *pIndMsg;
	UINT32 totalNumOfNsps;
	wmx_NSP_t nsps[WMX_NSPS_MAX_NUM];

	totalNumOfNsps = WMX_NSPS_MAX_NUM;

	GetAvailableNspsList(&totalNumOfNsps, nsps, TRUE);

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Sending scan update indication with %d NSPs", totalNumOfNsps);
	PrintAvailableNspsList(TRUE, TR_SEV_NOTICE);

	// size contains space for NSPs + space for NSPs number (UINT32) + space for scanProgress percentage (UINT32)
	msgSize = sizeof(NDNS_MESSAGE) + totalNumOfNsps * sizeof(wmx_NSP_t) + 2 * sizeof(UINT32);
	sendIndDataSize = sizeof(SendIndData) + msgSize;
	pSendIndData = (SendIndData *)malloc(sendIndDataSize);
	if (pSendIndData == NULL)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "malloc failed");
		return;
	}

	pIndMsg = (NDNS_MESSAGE *)(pSendIndData->indication_buffer);

	pSendIndData->pSenderFuncs = nds_pFuncs;
	pSendIndData->senderL5Conn = nds_l5ConnID;
	pSendIndData->pSubscribersList = g_ndnsContext.indicatorSubscribersList;
	pSendIndData->indication_id = NDNS_OPCODE_IND_SCAN_UPDATE;
	pSendIndData->indication_buffer_size = msgSize;
	pIndMsg->opcode = NDNS_OPCODE_IND_SCAN_UPDATE;
	pIndMsg->bufferLength = totalNumOfNsps * sizeof(wmx_NSP_t) + 2 * sizeof(UINT32);
	*((UINT32*)(pIndMsg->buf)) = totalNumOfNsps;
	*((UINT32*)(pIndMsg->buf + sizeof(UINT32))) = L4S_GetScanProgress();
	OSAL_memcpy_s( pIndMsg->buf + 2 * sizeof(UINT32), totalNumOfNsps * sizeof(wmx_NSP_t), nsps, totalNumOfNsps * sizeof(wmx_NSP_t) );
		pUtils->tpfnPostRequest(
		MEDIUM, NDNS_OPCODE_INDICATION_ARRIVED, pSendIndData, sendIndDataSize, &SendIndicationToSubscribers);

	free(pSendIndData);
}

void ProcessAutoConnect(wmx_ScanType_t scanType, wmx_NSPid_t nspId, BOOL isMultiChunk)
{
	BOOL isAutoConnect = FALSE, isSaveInfo = FALSE;

	switch(scanType)
	{
		case SCAN_TYPE_PREFERRED: // if the connect mode is auto connect, connect to the current preferred NSP
			if (ConnectMode_Auto == L4C_GetConnectionMode(L4db_GetConnectMode()) &&
				ScanMode_Manual != L4C_GetScanMode(L4db_GetConnectMode()))
				//L4C_AUTO_SCAN == FSM_GetState(&g_ndnsContext.fsm))
			{
				isAutoConnect = TRUE;
				isSaveInfo = TRUE;
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "User auto connect - connect to current preferred NSP scheduled");
			}
			break;
		case SCAN_TYPE_RECONNECT:
			if (L4db_GetFastReconnectStatus())
			{
				isAutoConnect = TRUE;
				isSaveInfo = TRUE;
				isMultiChunk = FALSE;
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Fast reconnect after scan - auto connect to recent NSP scheduled");
			}
			break;
		case SCAN_TYPE_AUTO_CONNECT:
			isAutoConnect = TRUE;
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "\"scan-connect\" sequence - auto connect scheduled. isMultiChunk=%d", isMultiChunk);
			break;
		default:
			break;
	}

	if (isAutoConnect)
	{
		// save the connection info for the next "Ready" system state
		if (isSaveInfo)
		{
			g_ndnsContext.scheduledTaskData.connectInfo.numOfNspIDs = 1;
			g_ndnsContext.scheduledTaskData.connectInfo.coexPriority = COEX_PRIORITY_CONNECT_LOW;
			g_ndnsContext.scheduledTaskData.connectInfo.nspIDs[0] = nspId;
			g_ndnsContext.scheduledTaskData.connectInfo.useOnlyLastChannel = FALSE;
		}

		if (!g_ndnsContext.scheduledTaskData.connectInfo.useOnlyLastChannel)
		{
			g_ndnsContext.scheduledTaskData.connectInfo.forceManualConnect = !isMultiChunk;
			g_ndnsContext.scheduledTaskData.connectInfo.useOnlyLastChannel = isMultiChunk;
		}
		else // in case we've already had an iteration with only the last channel - end the scanning and connect
		{
			// this line will ensure that we won't get back here as part of this connect session
			g_ndnsContext.scheduledTaskData.connectInfo.forceManualConnect = TRUE;
			g_ndnsContext.scheduledTaskData.connectInfo.useOnlyLastChannel = FALSE;
		}

		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Auto connect scheduled. forceManualConnect=%d, useOnlyLastChannel=%d", g_ndnsContext.scheduledTaskData.connectInfo.forceManualConnect, g_ndnsContext.scheduledTaskData.connectInfo.useOnlyLastChannel);


		L4C_ScheduleTask(Task_AutoConnect);
	}
}

BOOL L4C_GetSelectedNSP(wmx_ScanType_t scanType, wmx_pNSPid_t nspId)
{
	BOOL isFound = FALSE;

	// get the selected NSP based on the scan type
	switch(scanType)
	{
	case SCAN_TYPE_RECONNECT:
		// Fast reconnect is done only to the recent connected NSP
		if (L4db_GetRecentNsp(nspId) == WMX_ST_OK) // there are recent NSPs
		{
			isFound = TRUE;
		}
		break;
	case SCAN_TYPE_AUTO_CONNECT:
		// in case of a "scan-connect" cycle - connect only to the saved NSP
		if (g_ndnsContext.scheduledTaskData.connectInfo.numOfNspIDs > 0)
		{
			*nspId = g_ndnsContext.scheduledTaskData.connectInfo.nspIDs[0];
			isFound = TRUE;
		}
		break;
	case SCAN_TYPE_PREFERRED:
		// in preferred scan - search for the current preferred
		if (WMX_ST_OK == L4db_GetCurrentPreferredNsp(nspId) && (wmx_NSPid_t)L4DB_INVALID_INDEX != *nspId)
		{
			isFound = TRUE;
		}
	default:
		break;
	}

	return isFound;
}

// search the selected NSP in the NSPs lists
BOOL L4C_FindSelectedNsp(wmx_pNSP_t homeNsps,
						 UINT32 numOfHomeNSPs,
						 wmx_pAvailableNSP_t unknownNsps,
						 UINT32 numOfUnknownNSPs,
						 wmx_NSPid_t selectedNspId, // the selected NSP id
						 int *selectedIndex,
						 BOOL *isHomeSelected)
{
	BOOL isNspFound = FALSE;
	BOOL isHome = FALSE;
	UINT32 i = 0, j;


	// check if one of the home NSPs is the selected NSP
	for (i = 0; i < numOfHomeNSPs; ++i)
	{
		// for each NSP - go over its IDs
		for (j = 0; j < homeNsps[i].numOfNspIDs; ++j)
		{
			if (homeNsps[i].nspIDs[j] == selectedNspId)
			{
				isNspFound = TRUE;
				isHome = TRUE;
				break;
			}
		}
		if (isNspFound) break;
	}
	if (!isNspFound)
	{
		// check if one of the unknown NSPs is the selected NSP
		for (i = 0; i < numOfUnknownNSPs; ++i)
		{
			if (unknownNsps[i].nspId == selectedNspId)
			{
				isNspFound = TRUE;
				break;
			}
		}
	}

	if (isNspFound)
	{
		*selectedIndex = i;
		*isHomeSelected = isHome;
	}
	return isNspFound;
}

/// Decide which logic to apply for the next scan
/// <returns>True on success
BOOL L4C_ProcessNextScanParams(wmx_ScanType_t scanType,
							 wmx_ScanStatus_t scanStatus,
							 BOOL isNspFound,
							 long *isResumeScan,
							 BOOL *isScanCycleComplete)
{
	BOOL isReconnectRequired = FALSE;
	L4CState l4cState = FSM_GetState(&g_ndnsContext.fsm);
	BOOL res = FALSE;

	// Step 1: error checking
	if (isResumeScan == NULL || isScanCycleComplete == NULL)
	{
		goto Finalize;
	}

	// 0 represents a missing scan complete status
	if ((int)scanStatus == 0)
	{			// the scan of the current chunk is still in progress ->
		goto Finalize; // no scan parameters calculation is required at the moment
	}

	// Step 2: calculate the parameters for the next scan phase
	res = TRUE;
	*isResumeScan = FALSE;
	*isScanCycleComplete = FALSE;

	// This is the first case in which we stop the scan in the middle
	// Found an NSP which is valid for connection
	if (isNspFound)
	{
		*isScanCycleComplete = TRUE;
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Found an NSP which is valid for connection. Ending scan cycle and scheduling the next cycle.");

		if (scanType == SCAN_TYPE_AUTO_CONNECT || scanType == SCAN_TYPE_RECONNECT)
		{
			isReconnectRequired = TRUE;
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "This is an auto connect scan cycle. Scheduling a fresh cycle.");
		}
	}

	if (l4cState == L4C_AUTO_SCAN && !isReconnectRequired)
	{	// in auto scan mode - always resume scan on the next state shift
		*isResumeScan = TRUE;
	}

	if (L4S_AreChunksLeft())
	{	// there are more chunks to scan - so resume scan on the next state shift
		*isResumeScan = TRUE;
	}
	else
	{	// no chunks left = the scan cycle has ended
		*isScanCycleComplete = TRUE;
	}
Finalize:
	return res;
}

// Apply the logic for the next scan
void L4C_ApplyNextScanParams(wmx_ScanType_t scanType,
							 wmx_ScanStatus_t scanStatus,
							 long isResumeScan,
							 BOOL isScanCycleComplete,
							 UINT32 numOfNSPs)
{
	L4C_ConnectMode connectionMode = L4C_GetConnectionMode(L4db_GetConnectMode());

	UNREFERENCED_PARAMETER(numOfNSPs);

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE,
		"L4C_ApplyNextScanParams (IN) - scanType=%s, scanStatus=%d, isResumeScan=%d, isScanCycleComplete=%d, numOfNSPs=%d",
	        wmx_ScanType_tStr(scanType), scanStatus, isResumeScan, isScanCycleComplete, numOfNSPs);
	if (isResumeScan)
	{
		OSAL_atomic_exchange(&g_ndnsContext.isResumeScan, TRUE);
	}
	if (isScanCycleComplete)
	{
		if (initialCoexMode == WMX_MODE_CM)
		{
			wmx_CmdProcessEnd();
		}

		// we don't update (at all) the available NSPs list in 2 cases:
		// 1. when this is a direct scan cycle
		// 2. when this is a "scan-connect" sequence (targeted scan)
		if ((scanType != SCAN_TYPE_AUTO_CONNECT) &&
			(scanType != SCAN_TYPE_DIRECT || g_ndnsContext.scheduledTaskData.scanInfo.isDirectScanBlocking == FALSE))
		{
			//nardimon
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "<---L4db_PrintDiscoveredNAPsList - L4C_ApplyNextScanParams before AgeAvailableNspsList-- >");
			L4db_PrintDiscoveredNAPsList();
			//AgeAvailableNspsList(); // age the list before the next retry
		}
		L4S_EndScanCycle(); // end the scan cycle to allow the next cycle to start from scratch
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_ApplyNextScanParams - before save to DB");
		SaveToDB();

		// send scan results to the user ONLY if in manual connect mode
		// the results will be sent also if no new NSPs were found to allow updating the progress information
		if (/*(numOfNSPs > 0) &&*/ (connectionMode == ConnectMode_Manual) &&
			(scanType != SCAN_TYPE_AUTO_CONNECT) &&
			(scanType != SCAN_TYPE_DIRECT || g_ndnsContext.scheduledTaskData.scanInfo.isDirectScanBlocking == FALSE))
		{
			L4C_SendAvailableNSPsInd();
		}
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Sending scan completed event");
		L4Pub_SendScanCompleted(scanStatus); // send the scan completed event
		L4C_UpdateScanStatus(FALSE); //[Oran]
	}
	else
	{
		// send scan results to the user ONLY if in manual connect mode
		// the results will be sent also if no new NSPs were found to allow updating the progress information
		if (/*(numOfNSPs > 0) &&*/ (connectionMode == ConnectMode_Manual) &&
			(scanType != SCAN_TYPE_AUTO_CONNECT) &&
			(scanType != SCAN_TYPE_DIRECT || g_ndnsContext.scheduledTaskData.scanInfo.isDirectScanBlocking == FALSE))
		{
			L4C_SendAvailableNSPsInd();
		}
	}
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_ApplyNextScanParams (OUT)");
}

void L4C_SaveDetectedChannels(wmx_pNAP_t pNAPs, UINT32 numOfNAPs, L4C_LinkGrade linkGrade)
{
	wmx_ChannelID_t chIds[MAX_CHANNEL_INFO_ID_SIZE];
	UINT32 numOfChIds;
	UINT32 i;
	wmx_Status_t monitorStatus;
	char targetStr[MAX_NAME_STR_LEN];
	DiscoveredNap_t napInfo[WMX_NAPS_MAX_NUM];

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SaveDetectedChannels (IN)");

	// for each NAP - convert channel IDs list to L4DB channel plan (only channels with the specified link grade)
	for (i = 0; i < numOfNAPs; ++i)
	{
		l4db_ConvertNapIdToStr(napInfo[i].napID, targetStr);
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SaveDetectedChannels discovered NAP: NAP-ID=%s", targetStr);
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SaveDetectedChannels : Copying pNAPs[i].numChannels = [%d]",pNAPs[i].numChannels);
		napInfo[i].channelPlan.channelsSize = MAX_CHANNEL_INFO_ID_SIZE;  // Assign the amount of allocated channels
		napInfo[i].changeCount = pNAPs[i].changeCount;
		numOfChIds = MAX_CHANNEL_INFO_ID_SIZE;
		// save only the channels which are above the threshold
		GetChannelIdsByThreshold(linkGrade, &pNAPs[i], chIds, &numOfChIds);
		L4S_ConvertChIdsToChPlan(pNAPs[i], pNAPs[i].numChannels, napInfo[i].channelPlan.channels, &napInfo[i].channelPlan.channelsSize);
		napInfo[i].isLastDetected = TRUE;
		OSAL_memcpy_s(napInfo[i].napID, sizeof(wmx_NAPid_t), pNAPs[i].bsCriteria.bsID, sizeof(wmx_NAPid_t));
		napInfo[i].nspInfoSize = 0; // no 16G NSP info
		memset(&napInfo[i].channelPlan.linkQuality,0x00,(WMX_CHANNELS_MAX_NUM*(sizeof (wmx_LinkQuality_t))));
		OSAL_memcpy_s(&napInfo[i].channelPlan.linkQuality, (pNAPs[i].numChannels*(sizeof (wmx_LinkQuality_t))), pNAPs[i].linkQuality,(pNAPs[i].numChannels*(sizeof (wmx_LinkQuality_t))));
		L4db_SetDiscoveredNapInfo(&napInfo[i], &pNAPs[i]);
		monitorStatus = wmx_PopulateMonitorRecentDiscoverNap(&napInfo[i], pNAPs[i].bsCriteria.bsID);
	}
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_SaveDetectedChannels (OUT)");

}

// process the detected NAPs and convert them to NSPs
// <return>Number of NSPs</return>
UINT32 L4C_UpdateAvailableNSPsList(wmx_pScanResults_t scanResults,
								 L4C_LinkGrade linkGrade,
								 BOOL *isScanCycleComplete,
								 long *isResumeScan,
								 BOOL *isNspFound,
								 wmx_pNSPid_t nspId)
{
	UINT32 numOfNsps = WMX_NSPS_MAX_NUM;
	wmx_NSP_t nsps[WMX_NSPS_MAX_NUM];
	wmx_BSid_t bsIds[WMX_NSPS_MAX_NUM]; // the bsIds corresponding to the nsps array
	wmx_BsIdType_t bsIdsTypes[WMX_NSPS_MAX_NUM];
	UINT32 numOfFakeNsps = WMX_NSPS_MAX_NUM;
	wmx_AvailableNSP_t fakeNsps[WMX_NSPS_MAX_NUM];
	wmx_NAP_t napsWithNoNSP[WMX_NAPS_MAX_NUM];
	UINT32 numOfNapsWithNoNSP = WMX_NAPS_MAX_NUM;
	wmx_BSid_t selectedBsId;
	wmx_BsIdType_t selectedBsIdType;
	UINT32 selectedNAPindex;
	int nspIndex=0;
	BOOL isHomeSelected = FALSE;
	BOOL isFindFirstMode = FALSE;
	BOOL bFoundNSP = FALSE;

	BOOL isUseUnderThreshold = linkGrade == LinkGrade_BelowThr ? TRUE : FALSE;

	UINT32 showUnknownNSPs = 0;
	UINT i;

	memset(nsps, 0, sizeof(wmx_NSP_t) * WMX_NSPS_MAX_NUM);
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "<---L4db_PrintDiscoveredNAPsList - isUseUnderThreshold == [%d]  -- >", isUseUnderThreshold);
	*isNspFound = FALSE;
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "<---L4db_PrintDiscoveredNAPsList - L4C_UpdateAvailableNSPsList  before NAPs2NSPs-- >");
	L4db_PrintDiscoveredNAPsList();

	// Convert all the NAPs to NSPs, and then only the good signal results.
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "<-- Before NAPs2NSPs isUseUnderThreshold=[%d]  -->", isUseUnderThreshold);
	bFoundNSP = NAPs2NSPs(scanResults->NAPsCount,
							scanResults->NAPs,
							&numOfNsps,
							nsps,
							bsIds,
							bsIdsTypes,
							&numOfNapsWithNoNSP,
							napsWithNoNSP,
							isUseUnderThreshold);

	// NAP to NSP conversion completed successfully
	if (bFoundNSP && (numOfNsps>0 || numOfNapsWithNoNSP>0))
	{
		L4Configurations_getShowUnkownNSPs(&showUnknownNSPs);
		// Create fake NSPs for the NAPs that don't relate to an NSP
		if (numOfNapsWithNoNSP > 0 && showUnknownNSPs /*&& L4db_GetAllowAutoActivate() == TRUE*/)
		{
			CreateFakeAvailableNSPsFromNaps(numOfNapsWithNoNSP, napsWithNoNSP, &numOfFakeNsps, fakeNsps);
		}
		else
		{
			if (numOfNapsWithNoNSP > 0)
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Filtered %d unknown NSPs from scan results", numOfNapsWithNoNSP);
			}
			numOfNapsWithNoNSP = 0; // Don't support option 3 (unmapped NAPs)
			numOfFakeNsps = 0;
		}

		// get the selected NSP (if the NSP is found then we are in a find first scan mode)
		isFindFirstMode = L4C_GetSelectedNSP(g_ndnsContext.currentScanType, nspId);

		if (isFindFirstMode)
		{
			*isNspFound = L4C_FindSelectedNsp(nsps,
				numOfNsps,
				fakeNsps,
				numOfFakeNsps,
				*nspId,
				&nspIndex,
				&isHomeSelected);

			// during Find First mode - add only the selected nsp (if it exists) to the available NSPs list
			// if this is a "scan-connect" sequence - don't update the available NSPs list
			if (*isNspFound && g_ndnsContext.currentScanType != SCAN_TYPE_AUTO_CONNECT)
			{
				if (isHomeSelected)
				{
					AddMultipleNSPsToAvailableNSPsList(1, &nsps[nspIndex], &bsIds[nspIndex], &bsIdsTypes[nspIndex]);
				}
				else
				{
					AddMultipleFakeNSPsToAvailableNSPsList(1, &fakeNsps[nspIndex]);
				}
			}
		}
		else // in Find All mode - add all the detected NSPs
		{
			// Add all the unknown NSPs to the Available NSPs list
			AddMultipleFakeNSPsToAvailableNSPsList(numOfFakeNsps, fakeNsps);
			// Add all the provisioned NSPs to the Available NSPs list
			AddMultipleNSPsToAvailableNSPsList(numOfNsps, nsps, bsIds, bsIdsTypes);
		}

		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Found %d provisioned and %d unknown NSPs. Trying to add them to the available NSPs list", numOfNsps, numOfFakeNsps);
	}
	else // NAP to NSP conversion failed
	{
		numOfNsps = 0;
		numOfFakeNsps = 0;
	}
	L4C_ProcessNextScanParams(g_ndnsContext.currentScanType,
		scanResults->scanStatus,
		*isNspFound,
		isResumeScan,
		isScanCycleComplete);
	// save the ALL the detected channels which are above the threshold
	if (linkGrade == LinkGrade_AboveThr)
	{
		// for each NAP - convert channel IDs list to L4DB channel plan
		L4C_SaveDetectedChannels(scanResults->NAPs, scanResults->NAPsCount, linkGrade);
	}
	else // save SPECIFIC detected channels which are below the threshold
	{
		if (isFindFirstMode)
		{
			if (*isNspFound)
			{
				// find the BsId of the selected NSP
				if (isHomeSelected)
				{
					OSAL_memcpy_s(selectedBsId, sizeof(wmx_BSid_t), &bsIds[nspIndex], sizeof(wmx_BSid_t));
					selectedBsIdType = bsIdsTypes[nspIndex];
				}
				else
				{
					OSAL_memcpy_s(selectedBsId, sizeof(wmx_BSid_t), &fakeNsps[nspIndex].bsId, sizeof(wmx_BSid_t));
					selectedBsIdType = fakeNsps[nspIndex].bsIdType;
				}

				// find the corresponding NAP to the above selected BsId
				for (i = 0; i < scanResults->NAPsCount; ++i)
				{
					if (IsBsIdEqual(scanResults->NAPs[i].bsCriteria.bsID, selectedBsId, selectedBsIdType))
					{
						selectedNAPindex = i;
						break;
					}
				}

				// save the detected channels of the NAP corresponding to the selected NSP
				if (i == scanResults->NAPsCount)
				{
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4C_UpdateAvailableNSPsList: error finding a matching NAP for the selected NSP. Detected channels discarded.");
				}
				else
				{
					if (scanResults->scanStatus != ScanExhausted) 
					{
						L4C_SaveDetectedChannels(&scanResults->NAPs[i], 1, linkGrade);
					}
				}
			}
		}
		else // find all
		{
			if (scanResults->scanStatus != ScanExhausted) 
			{
				L4C_SaveDetectedChannels(scanResults->NAPs, scanResults->NAPsCount, linkGrade);
			}
			// for each NAP - convert channel IDs list to L4DB channel plan
			
		}
	}

	wmx_MonitorSendScanResults(scanResults, nsps, numOfNsps);

	return numOfNsps + numOfFakeNsps;
}

// Add the scan results to the available NSPs list
// and optionally send indication to the user, schedule the next scan and connect to a selected NSP (NAP)
wmx_Status_t L4C_ProcessScanResults(wmx_pScanResults_t scanResults)
{
	wmx_Status_t status;
	UINT32 totalNumOfNSPs = 0;
	wmx_NSPid_t nspId;
	BOOL isNspFound = FALSE;
	BOOL isScanCycleComplete = FALSE;
	long isResumeScan = FALSE;
	BOOL isMultiChunk = FALSE;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_ProcessScanResults(IN)");

	if (g_ndnsContext.currentScanType == SCAN_TYPE_DIRECT && g_ndnsContext.scheduledTaskData.scanInfo.isDirectScanBlocking)
	{
		L4C_ProcessNextScanParams(g_ndnsContext.currentScanType, scanResults->scanStatus, FALSE, &isResumeScan, &isScanCycleComplete);
		L4C_ApplyNextScanParams(g_ndnsContext.currentScanType,
								scanResults->scanStatus,
								isResumeScan,
								isScanCycleComplete,
								0);
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_ProcessScanResults(OUT) - manual scan direct and blocking");
		return WMX_ST_OK;
	}

	// temporarily save the NAPs that have channels beneath the threshold (if exist).
	// When the scan cycle ends - use these NAPs if there are no better results
	if (scanResults->NAPsCount > 0)
	{
		AddNAPsBeneathThreshold(scanResults->NAPsCount, scanResults->NAPs);
	}

	// first process all the results that have good link grade
	totalNumOfNSPs = L4C_UpdateAvailableNSPsList(scanResults, LinkGrade_AboveThr, &isScanCycleComplete, &isResumeScan, &isNspFound, &nspId);

	// If there are no results and no chunks left, try to use the results that are under the threshold (linkGrade == FALSE)
	if (isScanCycleComplete && !isNspFound)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "L4C_ProcessScanResults - Scan complete and no NSP found");
		scanResults->NAPsCount = WMX_NAPS_MAX_NUM;
		// Fetch the "bad" results
		status = GetNapsBeneathThreshold(&scanResults->NAPsCount, scanResults->NAPs);
		if (scanResults->NAPsCount > 0)
		{
			// second phase (end of scan cycle) - process all the results that have bad link grade
			totalNumOfNSPs += L4C_UpdateAvailableNSPsList(scanResults, LinkGrade_BelowThr, &isScanCycleComplete, &isResumeScan, &isNspFound, &nspId);
			// since we got to the end of the scan cycle - the source of the bad results may be != last chunk
			isMultiChunk = TRUE;
		}
	}

	// apply the "end of scan chunk" logic (report scan results to the user, set params for next chunk to scan etc.)
	L4C_ApplyNextScanParams(g_ndnsContext.currentScanType,
							scanResults->scanStatus,
							isResumeScan,
							isScanCycleComplete,
							totalNumOfNSPs);

	// if required - start an auto connect sequence
	if (isNspFound)
	{
		ProcessAutoConnect(g_ndnsContext.currentScanType, nspId, isMultiChunk);
	}

	if (totalNumOfNSPs < 1)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_ProcessScanResults(OUT) - WMX_ST_NSP_ID_NOT_FOUND");
		status = WMX_ST_NSP_ID_NOT_FOUND;
	}
	else
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_ProcessScanResults(OUT)");
		status = WMX_ST_OK;
	}

	return status;
}


void NDnSAgent_MessagesHandler(	L5_CONNECTION Conn,
							   L5_TARGET_ID nOriginID,

							   UINT32 dwSentMessageID,
							   void  *pvSentBuffer,
							   UINT32 cbSentBufferSize,

							   UINT32 *pdwResponseMessageID,
							   void  *pvResponseBuffer,
							   UINT32 *cbResponseBufferSize,

							   void *pvUserContext,
							   void *pvReserved )
{
	wmxApdo_FunctionParamStruct functionParamStruct;
	wmx_NSPid_t currentPreferredNspID = (wmx_NSPid_t)L4DB_INVALID_INDEX;
	wmx_pContactInfoParams params;
	wmx_Status_t status = WMX_ST_OK;

	if(pdwResponseMessageID == NULL)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "INTERNAL_PROBLEM: Illegal message. pdwResponseMessageID == NULL");
		nds_pFuncs->pfnSendErrorReport(L5_TARGET_NDNS, INTERNAL_PROBLEM, __FUNCTION__, __LINE__);
		return;
	}

	UNREFERENCED_PARAMETER(pvReserved);
	UNREFERENCED_PARAMETER(pvUserContext);
	UNREFERENCED_PARAMETER(cbSentBufferSize);
	UNREFERENCED_PARAMETER(Conn);


	switch (dwSentMessageID)
	{
		case NDNS_OPCODE_REGISTER_INDICATION:
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: NDNS_OPCODE_REGISTER_INDICATION");
			*pdwResponseMessageID = NDnSAgent_RegisterIndicator(nOriginID, *(UINT32*)pvSentBuffer);
			break;

		case NDNS_OPCODE_UNREGISTER_INDICATION:
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: NDNS_OPCODE_UNREGISTER_INDICATION");
			*pdwResponseMessageID = NDnSAgent_UnRegisterIndicator(nOriginID, *(UINT32*)pvSentBuffer);
			break;

		case NDNS_OPCODE_COMMAND:
			// Commands for manual and auto mode
			switch (*(UINT32*)pvSentBuffer)
			{
				case NDNS_OPCODE_RF_ON:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got RF On command");
					*pdwResponseMessageID = NDnSAgent_RfOn();
					return;
				case NDNS_OPCODE_RF_OFF:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got RF Off command");
					*pdwResponseMessageID = NDnSAgent_RfOff();
					return;
				default:

					//TODO: Oran - check implementation - update based on fsm states
					// In case of auto connect mode only the above commands are allowed
					/*if (UserSemiManScanAutoConnect == L4db_GetConnectMode() || UserAutoScanAutoConnect == L4db_GetConnectMode())
					{
						TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "connect mode is auto - illegal command");
						*pdwResponseMessageID = WMX_ST_ILLEGAL_OPERATION;
						return;
					}*/
					break;
			}

			// Commands for manual mode only
			switch (*(UINT32*)pvSentBuffer)
			{
				case NDNS_OPCODE_START_SCAN:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Start Scan command");

					*pdwResponseMessageID = L4C_StartManualScan(*(wmx_pScanType_t)(((pNDNS_MESSAGE)pvSentBuffer)->buf));

					break;
				case NDNS_OPCODE_START_SCAN_DIRECT:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Start Scan Direct command");
					*pdwResponseMessageID = L4C_StartManualScanDirect((UINT8 *)(((pNDNS_MESSAGE)pvSentBuffer)->buf));
					break;

				case NDNS_OPCODE_STOP_SCAN:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Stop Scan command");
					*pdwResponseMessageID = NDnSAgent_StopScan();
					break;

				case NDNS_OPCODE_CONNECT:
					if( (NULL == cbResponseBufferSize) ||
						(NULL == pvResponseBuffer) )
					{
						TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "one of the response out arguments is null");
						*pdwResponseMessageID = WMX_ST_ILLEGAL_OPERATION;
						break;
					}

					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Connect command");
					*pdwResponseMessageID = NDnSAgent_Connect(
						*(wmx_pConnectParams)(((pNDNS_MESSAGE)pvSentBuffer)->buf), COEX_PRIORITY_CONNECT_HIGH, (wmx_pUserConnectStatus_t)pvResponseBuffer );
					break;

				case NDNS_OPCODE_DISCONNECT:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Disconnect command");
					*pdwResponseMessageID = NDnSAgent_Disconnect();
					break;

				default:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "INTERNAL_PROBLEM: Unsupported NDNS_OPCODE, *(UINT32*)pvSentBuffer==%d", *(UINT32*)pvSentBuffer);
					*pdwResponseMessageID = WMX_ST_ILLEGAL_OPERATION;
					nds_pFuncs->pfnSendErrorReport(L5_TARGET_NDNS, INTERNAL_PROBLEM, __FUNCTION__, __LINE__);
					break;
			}
			break;

		case NDNS_OPCODE_GET:
			if( (NULL == cbResponseBufferSize) ||
				(NULL == pvResponseBuffer) )
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "one of the response out arguments is null");
				*pdwResponseMessageID = WMX_ST_ILLEGAL_OPERATION;
				break;
			}

			switch(*(UINT32*)pvSentBuffer)
			{

				case NDNS_OPCODE_GET_CONNECTED_NSP:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_Connected_NSPs command");
					*pdwResponseMessageID = NDnSAgent_GetConnectedNSP( (UINT32*)cbResponseBufferSize, pvResponseBuffer );
					break;

				case NDNS_OPCODE_GET_LAST_KNOWN_NSP_IDS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_Last_Known_NSPs command");
					*pdwResponseMessageID = NDnSAgent_GetLastKnownNspIDs( (UINT32*)cbResponseBufferSize, pvResponseBuffer );
					break;

				case NDNS_OPCODE_GET_SLA:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_SLA command");
					*pdwResponseMessageID = NDnSAgent_GetSLA( ((pNDNS_MESSAGE)pvSentBuffer)->bufferLength, (UINT8*)((pNDNS_MESSAGE)pvSentBuffer)->buf, *(UINT32*)cbResponseBufferSize, pvResponseBuffer );
					break;
				case NDNS_OPCODE_GET_LINK_STATUS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_Link_Status command");
					*pdwResponseMessageID = NDnSAgent_GetLinkStatus((UINT32*)cbResponseBufferSize, pvResponseBuffer );
					break;
				case NDNS_OPCODE_GET_STATISTICS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_Statistics command");
					*pdwResponseMessageID = NDnSAgent_GetStatistics((UINT32*)cbResponseBufferSize, pvResponseBuffer );
					break;
				case NDNS_OPCODE_GET_CONNECTION_MODE:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_Connect_Mode command");
					*pdwResponseMessageID = GetConnectMode(pvResponseBuffer, cbResponseBufferSize);
					break;

				case NDNS_OPCODE_GET_ROAMING_MODE:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_Roaming_Mode command");
					*pdwResponseMessageID = GetRoamingMode(pvResponseBuffer, cbResponseBufferSize);
					break;

				case NDNS_OPCODE_GET_DEVICE_STATUS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "get device status");
					*pdwResponseMessageID = L4Pub_GetDeviceStatus(pvResponseBuffer, (UINT32*)cbResponseBufferSize);
					break;

				case NDNS_OPCODE_GET_PACKAGE_INFO:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "get package info");
					*pdwResponseMessageID = GetPackageInfo(pvResponseBuffer, (UINT32*)cbResponseBufferSize);
					break;

				case NDNS_OPCODE_GET_WMF_COMPLIANCE_VER:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "get installation info");
					*pdwResponseMessageID = GetWMFComplianceInfo(pvResponseBuffer, *(UINT32*)cbResponseBufferSize);
					break;

				case NDNS_OPCODE_GET_INSTALLATION_INFO:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "get installation info");
					*pdwResponseMessageID = GetInstallationInfo(pvResponseBuffer, (UINT32*)cbResponseBufferSize);
					break;

				case NDNS_OPCODE_GET_COEXISTANCE_MODE:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_Coexistence_Mode command");
					*pdwResponseMessageID = GetCoexistenceMode(pvResponseBuffer, cbResponseBufferSize);
					break;

				case NDNS_OPCODE_GET_FAST_RECONNECT_STATUS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got GetFastReconnectStatus command");
					*pdwResponseMessageID = GetFastReconnectStatus(pvResponseBuffer, cbResponseBufferSize);
					break;
				case NDNS_OPCODE_GET_CURRENT_PREFERRED_NSP:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got NDNS_OPCODE_GET_CURRENT_PREFERRED_NSP command");
					*pdwResponseMessageID = GetCurrentPreferredNSP((wmx_pNSP_t)pvResponseBuffer, cbResponseBufferSize);
					break;

				case NDNS_OPCODE_GET_CONNECTED_AS_CURRENT_PREFERRED_CAPABILITY_STATUS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got GetConnectedAsCurrentPreferredCapabilityStatus");
					*pdwResponseMessageID = GetConnectedAsCurrentPreferredCapabilityStatus(pvResponseBuffer, cbResponseBufferSize);
					break;
				case NDNS_OPCODE_GET_PNM_REQUESTED_CHANNEL:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "get PNM Requested Channel");
					*pdwResponseMessageID = L4db_GetPNM(pvResponseBuffer, (UINT32*)cbResponseBufferSize);
					break;
				case NDNS_OPCODE_GET_IS_PNM_COMM_DEV_USING_CHANNEL:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "get Is Channel aquired by the device");
					*pdwResponseMessageID = L4C_GetIsPNMCommDevUsingChannel(((pNDNS_MESSAGE)pvSentBuffer)->buf, &((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;

				default:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "INTERNAL_PROBLEM: Unsupported NDNS_OPCODE, *(UINT32*)pvSentBuffer==%d", *(UINT32*)pvSentBuffer);
					*pdwResponseMessageID = WMX_ST_ILLEGAL_OPERATION;
					nds_pFuncs->pfnSendErrorReport(L5_TARGET_NDNS, INTERNAL_PROBLEM, __FUNCTION__, __LINE__);
					break;
			}
			break;
		case NDNS_OPCODE_SET:
			switch(((pNDNS_MESSAGE)pvSentBuffer)->opcode)
			{
				case NDNS_OPCODE_SET_CONNECTION_MODE:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Set_Connection_Mode command");
					*pdwResponseMessageID = SetConnectMode((wmx_pUserConnectMode_t)(((pNDNS_MESSAGE)pvSentBuffer)->buf), ((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;

				case NDNS_OPCODE_SET_ROAMING_MODE:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Set_Roaming_Mode command");
					*pdwResponseMessageID = SetRoamingMode(((pNDNS_MESSAGE)pvSentBuffer)->buf, ((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;

				case NDNS_OPCODE_SET_MSCHAP_V2_CRD:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Set_MSChap_V2Crd command");
					*pdwResponseMessageID = NDnSAgent_setMSChap_v2Crd(((pNDNS_MESSAGE)pvSentBuffer)->buf, ((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;

				case NDNS_OPCODE_SET_COEXISTANCE_MODE:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Set_Coex_Mode command");
					*pdwResponseMessageID = SetCoexistanceMode(((pNDNS_MESSAGE)pvSentBuffer)->buf, ((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;

				case NDNS_OPCODE_SET_FAST_RECONNECT_STATUS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got SetFastReconnectStatus command");
					*pdwResponseMessageID = SetFastReconnectStatus(((pNDNS_MESSAGE)pvSentBuffer)->buf, ((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;
				case NDNS_OPCODE_SET_CURRENT_PREFERRED_NSP:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got NDNS_OPCODE_SET_CURRENT_PREFERRED_NSP command");
					*pdwResponseMessageID = SetCurrentPreferredNSP(((pNDNS_MESSAGE)pvSentBuffer)->buf, ((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;


				case NDNS_OPCODE_SET_CONNECTED_AS_CURRENT_PREFERRED_CAPABILITY_STATUS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got SetConnectedAsCurrentPreferredCapabilityStatus command");
					*pdwResponseMessageID = SetConnectedAsCurrentPreferredCapabilityStatus(((pNDNS_MESSAGE)pvSentBuffer)->buf, ((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;

				default:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "INTERNAL_PROBLEM: Unsupported NDNS_OPCODE, ((pNDNS_MESSAGE)pvSentBuffer)->opcode==%d", ((pNDNS_MESSAGE)pvSentBuffer)->opcode);
					*pdwResponseMessageID = WMX_ST_ILLEGAL_OPERATION;
					nds_pFuncs->pfnSendErrorReport(L5_TARGET_NDNS, INTERNAL_PROBLEM, __FUNCTION__, __LINE__);
					break;
			}
			break;
		case NDNS_OPCODE_APDO:
			switch(((pNDNS_MESSAGE)pvSentBuffer)->opcode)
			{
				case NDNS_OPCODE_GET_POLLING_SUPPORTED:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_DevCap command");
					*pdwResponseMessageID = NDnSAgent_GetPollingSupported(pvResponseBuffer,cbResponseBufferSize);
					break;
				case NDNS_OPCODE_GET_APDO_OPERATOR_CONFIGURATIONS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got wmx_GetApdoOperatorConfigurations command");
					*pdwResponseMessageID = GetApdoOperatorsConfigurations( pvResponseBuffer);
					break;
				case NDNS_OPCODE_GET_CONNECTED_NSP_FOR_APDO:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got GET_CONNECTED_NSP_FOR_APDO command");
					*pdwResponseMessageID = GetConnectedNSPForAPDO( cbResponseBufferSize, pvResponseBuffer );
					break;
				case NDNS_OPCODE_GET_BEK:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got wmx_GetBEK command");
					*pdwResponseMessageID = GetBEK( pvResponseBuffer);
					break;
				case NDNS_OPCODE_GET_APDO_INFO:
					//TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, L"NDnSAgent_MessagesHandler: got Get_APDO_Info command");
					*pdwResponseMessageID = GetAPDOinfo(((pNDNS_MESSAGE)pvSentBuffer)->buf, (UINT32*)&((pNDNS_MESSAGE)pvSentBuffer)->bufferLength, pvResponseBuffer);
			//		*cbResponseBufferSize = (UINT32*)&((pNDNS_MESSAGE)pvSentBuffer)->bufferLength; // TODO: Set the real size of the return buffer
					break;
				case NDNS_OPCODE_ADD_APDO_INFO:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Add_APDO_Info command");
					*pdwResponseMessageID = AddAPDOinfo(((pNDNS_MESSAGE)pvSentBuffer)->buf, (UINT32*)&((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;
				case NDNS_OPCODE_UPDATE_APDO_INFO:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Update_APDO_Info command");
					*pdwResponseMessageID = UpdateAPDOinfo(((pNDNS_MESSAGE)pvSentBuffer)->buf, (UINT32*)&((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;
				case NDNS_OPCODE_DELETE_APDO_INFO:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Delete_APDO_Info command");
					*pdwResponseMessageID = DeleteAPDOinfo(((pNDNS_MESSAGE)pvSentBuffer)->buf, (UINT32*)&((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;
				case NDNS_OPCODE_EXECUTE_APDO_INFO:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "execute APDO info");
					*pdwResponseMessageID = ExecuteAPDOinfo(((pNDNS_MESSAGE)pvSentBuffer)->buf, (UINT32*)&((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;
				case NDNS_OPCODE_GET_APDO_LINK_STATUS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Get APDO Link Status");
					*pdwResponseMessageID = GetApdoLinkStatus(((pNDNS_MESSAGE)pvSentBuffer)->buf, (UINT32*)&((pNDNS_MESSAGE)pvSentBuffer)->bufferLength, pvResponseBuffer);
					break;
				case NDNS_OPCODE_GET_APDO_DHCP_HOST_NAME:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Get APDO DHCP host name");
					*pdwResponseMessageID = GetHostName(pvResponseBuffer, ((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;
				case NDNS_OPCODE_APDO_SET_PACKAGE_PATH:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Set package path");
					*pdwResponseMessageID = APDOSetPackagePath(((pNDNS_MESSAGE)pvSentBuffer)->buf, (UINT32*)&((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;
				case NDNS_OPCODE_GET_NSP_CONTACT_INFO_BY_NAME:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "get nsp contact info by name");
					params = (wmx_pContactInfoParams)((pNDNS_MESSAGE)pvSentBuffer)->buf;
					*pdwResponseMessageID = GetNSPContactInfoByName(params->nspName, pvResponseBuffer, cbResponseBufferSize);
					break;
				case NDNS_OPCODE_GET_CONTACT_INFORMATION:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "get contact info");
					*pdwResponseMessageID = GetContactInfo(*(wmx_pNSPid_t)((pNDNS_MESSAGE)pvSentBuffer)->buf, pvResponseBuffer, cbResponseBufferSize);
					break;
				case NDNS_OPCODE_DEPROVISION_NSP:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Deprovision_NSP command");

					status = WMX_ST_OK;

					// If we are in state different then RF-OFF return fail
					if (g_ndnsContext.systemState != RfOff)
					{
						TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Deprovision NSP not possible when system state=[%d].", g_ndnsContext.systemState);
						status = WMX_ST_FAIL;
					}
					else
					{
						// Check if the deprovisioned NSP is the current preferred NSP
						if ((WMX_ST_OK == L4db_GetCurrentPreferredNsp(&currentPreferredNspID)) &&
							(currentPreferredNspID == *(wmx_pNSPid_t)(((pNDNS_MESSAGE)pvSentBuffer)->buf)))
						{
							currentPreferredNspID = (wmx_NSPid_t)L4DB_INVALID_INDEX;
							status = SetCurrentPreferredNSPEx(&currentPreferredNspID, 1);
						}
					}
					if (status == WMX_ST_OK)
					{
						*pdwResponseMessageID = L4db_DeProvisionNsp(*(wmx_pNSPid_t)(((pNDNS_MESSAGE)pvSentBuffer)->buf));
					}
					else // In case the current preferred NSP could not be changed
					{
						*pdwResponseMessageID = status;
					}
					break;
				case NDNS_OPCODE_RESTORE_BACKUP_PROVISIONING_DATABASE:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "restore backup provisioning database");
					*pdwResponseMessageID = RestoreBckProvisioningDB();
					break;
				case NDNS_OPCODE_RESTORE_FACTORY_SETTINGS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "restore factory settings");
					*pdwResponseMessageID = RestoreFactorySettings();
					break;
				case NDNS_OPCODE_SET_PACKAGE_UPDATE_STATE:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got Set_Package_Update_State command");
					*pdwResponseMessageID = NDnSAgent_ApdoSetPackageUpdateState(*(wmx_pPackageUpdateState_t)(((pNDNS_MESSAGE)pvSentBuffer)->buf));
					break;
				case NDNS_OPCODE_GET_UNPROVISIONED_NSP_IDS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_Unprovisioned_NSP_IDs command");
					*pdwResponseMessageID = GetUnprovisionedNspIDs((UINT32*)cbResponseBufferSize, (wmx_pNSPid_t)pvResponseBuffer);
					break;
				case NDNS_OPCODE_GET_PROVISIONED_NSP_IDS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_Provisioned_NSP_IDs command");
					*pdwResponseMessageID = GetProvisionedNspIDs((UINT32*)cbResponseBufferSize, (wmx_pNSPid_t)pvResponseBuffer);
					break;
				case NDNS_OPCODE_GET_NSP_INFO:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got Get_NSP_Info command");
					*pdwResponseMessageID = GetNspInfo(*(wmx_NSPid_t*)((pNDNS_MESSAGE)pvSentBuffer)->buf, (wmx_pNSP_t)pvResponseBuffer);
					break;
				case NDNS_OPCODE_GET_SERVICE_PROVIDER_LOCK_STATUS:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_MessagesHandler: got GET_SERVICE_PROVIDER_LOCK_STATUS command");
					*pdwResponseMessageID = GetServiceProviderLockStatus(pvResponseBuffer, cbResponseBufferSize);
					break;
				case NDNS_OPCODE_SET_SERVICE_PROVIDER_UNLOCK:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_MessagesHandler: got SET_SERVICE_PROVIDER_UNLOCK command");
					*pdwResponseMessageID = SetServiceProviderUnlock(((pNDNS_MESSAGE)pvSentBuffer)->buf, ((pNDNS_MESSAGE)pvSentBuffer)->bufferLength);
					break;
				case NDNS_OPCODE_APDO_TYPE:
					functionParamStruct = *(wmxApdo_pFunctionParamStruct)((pNDNS_MESSAGE)pvSentBuffer)->buf;

					switch (functionParamStruct.FunctionType)
					{
						case APDO_ApdoSessionStatus:
							TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "set APDO session status");
							HandleApdoSessionStatus(functionParamStruct.FuncParamUnion.ApdoSessionStatusStruct.sessionStatus);
							break;
						case APDO_AlertNotification:
							TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "alert notification");
							NDnSAgent_AlertNotification((wmxApdo_pFunctionParamStruct)((pNDNS_MESSAGE)pvSentBuffer)->buf);
							break;
						case APDO_StoreProvisioningClientCfgDataBlobStruct:
							TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Store provisioning client cfg data blob");
							break;
						case APDO_GetProvisioningClientCfgDataBlobStruct:
							TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Get provisioning client cfg data blob");
							break;
						case APDO_FumoUpdate:
							TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Update FUMO downloads");
							HandleFumoUpdate(functionParamStruct.FuncParamUnion.fumoUpdateContent);
							break;
						default:
							break;
					}

					*pdwResponseMessageID = WMX_ST_OK;
				//	*pdwResponseMessageID = SetAPDOSessionStatus(*(wmx_ApdoSessionStatus_t*)((pNDNS_MESSAGE)pvSentBuffer)->buf);
					break;
				default:
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_WARNING, "Unsupported NDNS_OPCODE");
					*pdwResponseMessageID = WMX_ST_ILLEGAL_OPERATION;
					ASSERT();
					break;
			}
			break;
		default: //Unsupported NDNS_OPCODE
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "INTERNAL_PROBLEM: Unsupported NDNS_OPCODE, dwSentMessageID==%d", dwSentMessageID);
			*pdwResponseMessageID = WMX_ST_ILLEGAL_OPERATION;
			nds_pFuncs->pfnSendErrorReport(L5_TARGET_NDNS, INTERNAL_PROBLEM, __FUNCTION__, __LINE__);
			break;
	}

	if (*pdwResponseMessageID != WMX_ST_OK)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Error in response message = %s", wmxGetStatusStr(*pdwResponseMessageID));
	}
}


// TODO: Oran - remove
// Extracts the requested NSP from the nspBuffer, finds the appropriate SLA and enters it to the slaBuffer
wmx_Status_t NDnSAgent_GetSLA( UINT32 nspIDBufferLength, UINT8 *nspIDBuffer, UINT32 slaBufferLength, UINT8 *slaBuffer )
{
	wmx_Status_t status = WMX_ST_DATA_UNAVAILABLE;

	if ( (nspIDBufferLength != sizeof(wmx_NSPid_t)) || (slaBufferLength != sizeof(wmx_SLA_t)) )
	{
		return WMX_ST_ILLEGAL_OPERATION;
	}
	UNREFERENCED_PARAMETER(slaBuffer);
	UNREFERENCED_PARAMETER(nspIDBuffer);

// TODO: SLA is not supported by the DB
//	status = NdsDB_GetSLA(*(wmx_pNSPid_t)nspIDBuffer, (wmx_pSLA_t)slaBuffer);

	return status;
}

// Get the last found NAPs (from the last scan). Translate the NAPs to NSP IDs and return them in a buffer
// Assums that NSP ID doesn't appear twice in two different NSPs. Return one ID of each NSP.
wmx_Status_t NDnSAgent_GetLastKnownNspIDs( UINT32 *bufferLength, UINT8 *buffer )
{
	wmx_Status_t status;
	errno_t err;
	UINT32 returnedNumberOfNSP = 0;
	UINT32 numOfNSPs = WMX_NSPS_MAX_NUM;
	wmx_NSP_t pNSPs[WMX_NSPS_MAX_NUM];
	UINT32 i;
	BOOL roamingDisabled = FALSE;

	// This API can't be called during provisioning
	if (Ndns_GetProvisioningStarted() == TRUE)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Failed NDnSAgent_GetLastKnownNspIDs - called during provisioning.");
		return WMX_ST_FAIL;
	}

	//UINT32 numOfNAPs = WMX_NAPS_MAX_NUM;
	//wmx_NAP_t pNAPs[WMX_NAPS_MAX_NUM];

// TODO: Add support when nsp id is given
//	if (L4db_GetRoamingMode() == UserDisableRoaming)
	{
		roamingDisabled = FALSE;
	}

	GetAvailableNspsList(&numOfNSPs, pNSPs, TRUE);
	TRACE(TR_MOD_NDNS_WRAPPER, TR_SEV_INFO, "GetLastKnownNspIDs - num of NSPs fetched = %d", numOfNSPs);
	PrintAvailableNspsList(TRUE, TR_SEV_INFO);

	//// TODO: make sure that there is no limit on the NAPs size
	//wmx_GetLastKnownNAPs(&numOfNAPs, pNAPs);
	//if( NAPs2NSPs( numOfNAPs, pNAPs, &numOfNSPs, pNSPs ) == FALSE )
	//{
	//	return WMX_ST_FAIL;
	//}

	// Make sure that the buffer is in a correct size
	if ( numOfNSPs <= (*bufferLength / sizeof(wmx_NSPid_t)) )
	{
		status = WMX_ST_OK;
	}
	else
	{
		numOfNSPs = (*bufferLength / sizeof(wmx_NSPid_t));
		status = WMX_ST_RESPONSE_BUFFER_TOO_SHORT;
	}

	// Run over the NSPs and copy the IDs to the result buffer
	for (i = 0; i < numOfNSPs; ++i)
	{
		// If roaming is disabled return only the home NSPs
		if (pNSPs[i].isHomeNSP == FALSE && roamingDisabled == TRUE) continue;

		// Make sure that the found NSP has an ID
		if (pNSPs[i].numOfNspIDs <= 0)
		{
			continue;
		}


		err = OSAL_memcpy_s( buffer + sizeof(UINT32) + returnedNumberOfNSP*sizeof(wmx_NSPid_t), sizeof(wmx_NSPid_t), &(pNSPs[i].nspIDs[0]), sizeof(wmx_NSPid_t) );

		if( err != 0 )
		{
			status = WMX_ST_FAIL;
		}
		returnedNumberOfNSP++;
	}

	*bufferLength = sizeof(UINT32) + (returnedNumberOfNSP * sizeof(wmx_NSPid_t));

	// The number of NSPs is returned as a UIN32 in the first 4 bytes of the buffer
	*(UINT32*)buffer = returnedNumberOfNSP;

	return status;
}


wmx_Status_t NDnSAgent_GetLinkStatus( UINT32 *bufferLength, UINT8 *buffer )
{
	wmx_Status_t status=WMX_ST_OK;
	wmx_pUserLinkStatus_t userLinkStatus;
	//static wmx_LinkStatus_t linkStatus;
	static BOOL fIsLastTimeWasIdle = FALSE;
	BOOL fIsCacheState;

	if (*bufferLength < sizeof(wmx_UserLinkStatus_t))
	{
		return WMX_ST_BUFFER_TOO_SHORT;
	}

	userLinkStatus = (wmx_pUserLinkStatus_t)buffer;

	if (FSM_GetState(&g_ndnsContext.fsm) != L4C_CONNECTED)
	{
		return WMX_ST_WRONG_STATE;
	}

	fIsCacheState = (g_ndnsContext.systemState == Idle);

	TRACE(TR_MOD_NDNS_WRAPPER, TR_SEV_DEBUG, "NDnSAgent_GetLinkStatus - device status = %s", NDnSSystemStates(g_ndnsContext.systemState>=UnknownState?UnknownState:g_ndnsContext.systemState));

	//get the information also for cases the AppSrv reset while in DataPathConnected
	if( !fIsCacheState ||
		(0 == g_linkStatus.rssi && 0 == g_linkStatus.cinr))
	{
		TRACE(TR_MOD_NDNS_WRAPPER, TR_SEV_DEBUG, "NDnSAgent_GetLinkStatus - Data From device");
		status = wmx_GetLinkStatus(&g_linkStatus);

	}
	else
	{
		TRACE(TR_MOD_NDNS_WRAPPER, TR_SEV_DEBUG, "NDnSAgent_GetLinkStatus - Data From Cache");
	}

	userLinkStatus->cinr = g_linkStatus.cinr;
	userLinkStatus->downLinkSpeed = g_linkStatus.downLinkSpeed;
	userLinkStatus->upLinkSpeed = g_linkStatus.upLinkSpeed;
	userLinkStatus->frequency = g_linkStatus.frequency;

	userLinkStatus->linkQuality = g_linkStatus.linkQuality;
	OSAL_memcpy_s(userLinkStatus->bsId, sizeof(wmx_BSid_t), g_linkStatus.bsId, sizeof(wmx_BSid_t));
	//L4db_GetRecentNsp(&userLinkStatus.nspId);
	userLinkStatus->power = g_linkStatus.power;
	userLinkStatus->rssi = g_linkStatus.rssi;
	userLinkStatus->time = wmxNds_GetConnectionDuration();

	*bufferLength = sizeof(wmx_UserLinkStatus_t);

	return status;
}

wmx_Status_t NDnSAgent_GetStatistics( UINT32 *bufferLength, UINT8 *buffer )
{
	wmx_Status_t status=WMX_ST_OK;
	//wmx_UserLinkStatus_t userLinkStatus;
	wmx_UserStatistics_t userStatistics;
	BOOL fIsCacheState;
	static wmx_Statistics_t Statistics={0};

	static BOOL fIsLastTimeWasIdle = FALSE;


	if (*bufferLength < sizeof(wmx_UserStatistics_t))
	{
		return WMX_ST_BUFFER_TOO_SHORT;
	}


	fIsCacheState = ((g_ndnsContext.systemState == Idle)  ||
		(g_ndnsContext.systemState == Ready)  ||
		(g_ndnsContext.systemState == RfOff));

	if( !fIsCacheState)
	{
		status = wmx_GetStatistics(&Statistics);
	}
	else
	{
		TRACE(TR_MOD_NDNS_WRAPPER, TR_SEV_DEBUG, "NDnSAgent_GetStatistics - Data From Cache");
	}

	userStatistics.timeStamp      = Statistics.timeStamp;
	userStatistics.TotalRxBytes   = Statistics.TotalRxBytes;
	userStatistics.TotalRxPackets = Statistics.TotalRxPackets;
	userStatistics.TotalTxBytes   = Statistics.TotalTxBytes;
	userStatistics.TotalTxPackets = Statistics.TotalTxPackets ;

	OSAL_memcpy_s(buffer, *bufferLength, &userStatistics, sizeof(wmx_UserStatistics_t));
	*bufferLength = sizeof(wmx_UserStatistics_t);

	return status;
}

// Copies the connected NSP to the buffer, if not connected returns WMX_ST_DATA_UNAVAILABLE
wmx_Status_t NDnSAgent_GetConnectedNSP( UINT32 *bufferLength, UINT8 *buffer)
{
	// Default is to block during provisioining
	return NDnSAgent_GetConnectedNSPEx(bufferLength, buffer, TRUE);
}

// Copies the connected NSP to the buffer, if not connected returns WMX_ST_DATA_UNAVAILABLE
wmx_Status_t NDnSAgent_GetConnectedNSPEx( UINT32 *bufferLength, UINT8 *buffer, BOOL blockOnProvisioning )
{
	wmx_NSPid_t lastConnectedNSPid;
	errno_t err;
	wmx_Status_t status;
	wmx_NSP_t nsp;
	UINT32 bufferLen = sizeof(wmx_UserLinkStatus_t);
	wmx_SystemState_t currentSystemState;
	wmx_UserLinkStatus_t userLinkStatus;

	OSAL_enter_critical_section(&g_ndnsContext.lockSystemState);
	currentSystemState = g_ndnsContext.systemState;
	OSAL_exit_critical_section(&g_ndnsContext.lockSystemState);

	if (FSM_GetState(&g_ndnsContext.fsm) != L4C_CONNECTED)
	{
		return WMX_ST_DATA_UNAVAILABLE;
	}

	if ( *bufferLength < sizeof(wmx_NSP_t) )
	{
		return WMX_ST_RESPONSE_BUFFER_TOO_SHORT;
	}

	if (blockOnProvisioning)
	{		
		// This API can't be called during provisioning
		if (Ndns_GetProvisioningStarted() == TRUE)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Failed NDnSAgent_GetConnectedNSP - called during provisioning.");
			return WMX_ST_FAIL;
		}
	}
	else
	{
		TRACE(TR_MOD_APDO_AGENT, TR_SEV_INFO, "NDnSAgent_GetConnectedNSP - called during provisioning - but allowed anyway.");
	}

	status = L4db_GetRecentNsp(&lastConnectedNSPid);
	// if the device is connected to an unknown network, status should be OK but last connectedNSP = -1
	if((-1 == lastConnectedNSPid) && (WMX_ST_OK == status))
	{
		// change the lastConnectedNSPid to an illegal NSPID in order to ensure that GetNspInfo will  
		// handle the WMX_ST_DATA_UNAVAILABLE case.
		lastConnectedNSPid = ILLEGAL_NSP_ID;
		
		/*OSAL_ZeroMemory(nsp.nspName, sizeof(nsp.nspName));
		OSAL_memcpy_s(nsp.nspName, sizeof(nsp.nspName), "Unrecognized Operator", sizeof("Unrecognized Operator"));*/
	}
		
	status = GetNspInfo(lastConnectedNSPid, &nsp);
	if( status != WMX_ST_OK )
	{
		return status;
	}

	status = NDnSAgent_GetLinkStatus(&bufferLen,(UINT8*)&userLinkStatus);
	nsp.linkQuality = userLinkStatus.linkQuality;
	nsp.bestRssi = userLinkStatus.rssi;
	nsp.bestCINR = userLinkStatus.cinr;

	*bufferLength = sizeof(wmx_NSP_t);
	err = OSAL_memcpy_s( buffer, sizeof(wmx_NSP_t), &nsp, sizeof(wmx_NSP_t) );
	if( err != 0 )
	{
		return WMX_ST_FAIL;
	}

	return WMX_ST_OK;
}


// Tries to Stop an A-Sync Scan, returns the status of the operation
wmx_Status_t NDnSAgent_StopScan()
{
	wmx_Status_t status;

	// StopScan can be issued only when in single scan state
	//if (FSM_GetState(&g_ndnsContext.fsm) == L4C_SINGLE_SCAN)
	//{
	//	return L4S_StopScanner();
	//}

	// in case a scan/connect process is taking place, notify device
	if(L4C_GetInitialCoexMode() == WMX_MODE_CM && g_ndnsContext.processStarted)
	{
		L4C_SetProcessStarted(FALSE);
		wmx_CmdProcessEnd();
	}

	status = NDnSAgent_InvokeDualFlushOp(Task_StopScan, NULL, 0);

	if (status != WMX_ST_OK)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR,
			"Stop Scan failed. NDnSAgent_InvokeDualFlushOp returned: '%s'",
			wmxGetStatusStr(status));
	}

	L4Pub_SendScanCompleted(ScanStopped);
	return status;
}

void L4C_TaskHandler(UINT32 internalRequestID, void *buffer, UINT32 bufferLength)
{
	wmx_SystemState_t systemState;
	wmx_pConnectInfo_t connectInfo;
	wmx_pScanInfo_t scanInfo;
	UINT32 phaseNum = 1;

	UNREFERENCED_PARAMETER(bufferLength);

	// get the current system state
	OSAL_enter_critical_section(&g_ndnsContext.lockSystemState);
		 systemState = g_ndnsContext.systemState;
	OSAL_exit_critical_section(&g_ndnsContext.lockSystemState);

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_TaskHandler(IN): Got '%s' task", L4C_TaskStr(internalRequestID));

	switch (internalRequestID)
	{
		case Task_StartManualScan:
			scanInfo = (wmx_pScanInfo_t)buffer;
			g_ndnsContext.scheduledTaskData.scanInfo.manualScanType = scanInfo->manualScanType;
			g_ndnsContext.scheduledTaskData.scanInfo.isInternal = scanInfo->isInternal;
			// if we are already in Ready state and there is no scan in progress - we can execute the manual scan task
			// (even if we are in Ready state - there could be a pending Scanning state indication from a previous scan)
			if ((systemState == Ready) && (!L4S_IsScanning()))
			{
				// in case the scanner is active (and NOT scanning) - it means that
				// it is sleeping (LinkLoss / Standby state) - so we need to reset it
				if (L4S_READY != L4S_GetScannerState())
				{
					L4S_Reset(TRUE);
				}
				L4C_ScheduleTask(Task_StartScan); // after the scan session is over - return to normal scan mode
				L4C_UpdateScanStatus(FALSE);
				OSAL_atomic_exchange(&g_ndnsContext.isSingleManualScan, (long)FALSE);
				L4C_StartManualScanPhase2(g_ndnsContext.scheduledTaskData.scanInfo.manualScanType,
										  g_ndnsContext.scheduledTaskData.scanInfo.isInternal);
			}
			else // we are still not in Ready state - schedule the manual scan task to be executed
			{	 // only when getting to Ready state
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG,
					"State is not ready or scanner is stil scanning: system state=%s, scanner state=%s",
				        NDnSSystemStates(systemState),
					ScannerStateStr(L4S_GetScannerState()));
				L4C_ScheduleTask(Task_StartManualScan); // start Connect on the next Ready state
			}
			break;
		case Task_Connect:
			connectInfo = (wmx_pConnectInfo_t)buffer;

			// if we are already in Ready state and there is no scan in progress - we can execute the connect task
			// (even if we are in Ready state - there could be a pending Scanning state indication from a previous scan)
			if ((systemState == Ready) && (!L4S_IsScanning()))
			{
				// in case the scanner is active (and NOT scanning) - it means that
				// it is sleeping (LinkLoss / Standby state) - so we need to reset it
				if (L4S_READY != L4S_GetScannerState())
				{
					L4S_Reset(TRUE);
				}
				L4C_ScheduleTask(Task_StartScan); // after the connect session is over - return to normal scan mode
				L4C_UpdateScanStatus(FALSE);
				OSAL_atomic_exchange(&g_ndnsContext.isSingleManualScan, (long)FALSE);
				NDnSAgent_ConnectPhase2(connectInfo);
			}
			else // we are still not in Ready state - schedule the Connect task to be executed
			{	 // only when getting to Ready state
				OSAL_memcpy_s(&g_ndnsContext.scheduledTaskData.connectInfo, sizeof(g_ndnsContext.scheduledTaskData.connectInfo), connectInfo, sizeof(wmx_ConnectInfo_t));
				L4C_ScheduleTask(Task_Connect); // start Connect on the next Ready state
			}
			break;
		case Task_StopScan:
			{
				if (FSM_GetState(&g_ndnsContext.fsm) == L4C_SINGLE_SCAN)
				{
					//Nothing to do in this case, just complete the
					//process of stopping scan by calling NDnSAgent_OnProgressDualFlush()
					NDnSAgent_OnProgressDualFlush(Task_StopScan, NULL, phaseNum);
				}
				break;
			}
		case Task_SetConnectMode:
			{
				L4CState state = FSM_GetState(&g_ndnsContext.fsm);
				g_ndnsContext.scheduledTaskData.connectMode = *(wmx_pUserConnectMode_t)buffer;
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "Task_SetConnectMode : g_ndnsContext.scheduledTaskData.connectMode = [%d]", g_ndnsContext.scheduledTaskData.connectMode);
				if (state == L4C_SINGLE_SCAN || state == L4C_AUTO_SCAN)
				{
					NDnSAgent_OnProgressDualFlush(Task_SetConnectMode, NDnSAgent_SetConnectModePhase2, phaseNum);
				}
				else // we are not in scanning state (RF OFF or Uninitialized...)
				{
					NDnSAgent_SetConnectModePhase2();
					g_ndnsContext.scheduledTaskData.status = WMX_ST_OK;
					L4C_ScheduleTask(Task_StartScan); // after the session is over - return to normal scan mode
					//Set Event to release the waiting thread
					OSAL_set_event(g_ndnsContext.dualFlushOpEvent);
				}
				break;
			}
		case Task_StartScan:
		case Task_Reset:
		default:
			break;
	}
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "L4C_TaskHandler(OUT)");
}

//
//For all cases we need to stop scan and then update the L4DB with the new connect or scan modes.
//In case we're switching between ManConnect to AutoConnect - we need to start connecting we current preferred NSP is available
//In all other cases (e.g. - In case we switch between AutoScan to SemiAutoScan or vice versa)
//we reset the scanning process (i.e. we stop scan and reschedule Task_StartScan)
wmx_Status_t NDnSAgent_SetConnectModePhase2()
{
	UINT32 totalNumOfNsps = WMX_NSPS_MAX_NUM, numCPNs, i, j;
	wmx_NSP_t currentPreferredNsp, availableNsps[WMX_NSPS_MAX_NUM] = {0} ;
	wmx_UserConnectMode_t connectMode = g_ndnsContext.scheduledTaskData.connectMode ;
	wmx_UserConnectMode_t lastConnectMode = L4db_GetConnectMode();
	wmx_SystemState_t systemState;
	wmx_Status_t status = WMX_ST_FAIL;
	wmx_SystemStateUpdate systemStateUpdate;
	BOOL isReset = FALSE;

	systemStateUpdate.SystemState = g_ndnsContext.systemState;
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_SetConnectModePhase2(IN)");

	//Saving to L4DB - In case of any failure return status
	status = L4db_SetConnectMode(connectMode);

	//send CB indication
	NDnSAgent_ConnectionModeUpdate(connectMode);

	if (status == WMX_ST_OK)
	{
		if( (lastConnectMode != UserSemiManScanAutoConnect && lastConnectMode != UserAutoScanAutoConnect) &&
			(connectMode == UserSemiManScanAutoConnect || connectMode == UserAutoScanAutoConnect) )
		{
			//In case we switch from ManConnect to AutoConnect - start connect
			//(This is a special case where we override the StartScan task set in NDnSAgent_OnProgressDualFlush)
			//In any other case of switching connect (auto to man),
			//or (any to any) scan mode - leave the scheduled StartScan from NDnSAgent_OnProgressDualFlush
			//
			//first see if have a CPN...
			if(GetCurrentPreferredNSPEx(&currentPreferredNsp, &numCPNs ) == WMX_ST_OK && numCPNs > 0)
			{
				//If we have a CPN, search for it in AvailabeNSPsList.
				//If found, set params of the next scheduled task, and schedule AutoConnect
				if (GetAvailableNspsList(&totalNumOfNsps, availableNsps, FALSE) == WMX_ST_OK)
				{
					for (i = 0; i < totalNumOfNsps; i++)
					{
						for (j = 0; j < currentPreferredNsp.numOfNspIDs; j++)
						{
							if (currentPreferredNsp.nspIDs[j] == availableNsps[i].nspIDs[0])
							{
								g_ndnsContext.scheduledTaskData.connectInfo.numOfNspIDs = 1;
								g_ndnsContext.scheduledTaskData.connectInfo.coexPriority = COEX_PRIORITY_CONNECT_LOW;
								g_ndnsContext.scheduledTaskData.connectInfo.forceManualConnect = TRUE;
								g_ndnsContext.scheduledTaskData.connectInfo.nspIDs[0] = currentPreferredNsp.nspIDs[j];

								TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_SetConnectModePhase2 - scheduling the task '%s' for the next Ready state", L4C_TaskStr(Task_AutoConnect));
								//NDnSAgent_OnProgressDualFlush(Task_AutoConnect, NULL);
								L4C_ScheduleTask(Task_AutoConnect);
								OSAL_enter_critical_section(&g_ndnsContext.lockSystemState);
								systemState = g_ndnsContext.systemState;
								OSAL_exit_critical_section(&g_ndnsContext.lockSystemState);
								L4C_SetControllerMode(systemState, FALSE);
								status = WMX_ST_OK;
							}
						}
					}
				}
			}
			else
			{
				//if no CPN is available return WMX_ST_WRONG_STATE
				status = WMX_ST_WRONG_STATE;
				// 				g_ndnsContext.scheduledTaskData.status = ;
				// 				//release the waiting thread... (NDnSAgent_InvokeDualFlushOp())
				// 				OSAL_set_event(g_ndnsContext.dualFlushOpEvent);
			}
		}
		else
		{
			if (g_ndnsContext.systemState == Ready)
			{
				status = L4C_HandleReadyState(&systemStateUpdate, &isReset);
			}
		}
		// 		else
		// 		{
		// 			//In any other case of switching connect (auto to man),
		// 			// or (any to any) scan mode - schedule StartScan
		// 			//NDnSAgent_OnProgressDualFlush(Task_StartScan, NDnSAgent_GenerateFictiveReadyState);
		// 			L4C_ScheduleTask(Task_StartScan);
		// 		}
	}
	// 	else
	// 	{
	// 		//In case of any failure
	// 		status = status;
	// 	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_SetConnectModePhase2(OUT)");
	return status;
}


wmx_Status_t NDnSAgent_ConnectPhase2(wmx_pConnectInfo_t connectInfo)
{
	L4CState fsmState = FSM_GetState(&g_ndnsContext.fsm);
	wmx_Status_t status;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_ConnectPhase2(IN)");

	if (!FSM_SetStateIfNotEqual(&g_ndnsContext.fsm, L4C_CONNECTING, L4C_CONNECTING))
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "NDnSAgent_ConnectPhase2(OUT).ERROR FSM_SetStateIfNotEqual returned FALSE. ");
		return WMX_ST_WRONG_STATE;
	}

	// reset the L4Scanner in a hard manner since we are now using the messenger thread and
	// the L4Scanner won't be able to get L4 indications for successful reset
	L4S_Reset(TRUE);

	if (L4C_GetInitialCoexMode() == WMX_MODE_CM)
	{
		status = wmx_CmdProcessStart(COEX_STATE_CONNECTING);
		if (status != WMX_ST_OK)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_Connect: ProcessStart failed status=%d", status);
			NDnSAgent_RfTakenIndication();
			return status;
		}
		L4C_SetProcessStarted(TRUE);
	}

	status = NDnSAgent_StartConnect( connectInfo->nspIDs[0], connectInfo->coexPriority, connectInfo->forceManualConnect, connectInfo->useOnlyLastChannel );
	// after manual connect attempt (even unsuccessful one) - start the next scan cycle from scratch (no resume)
	OSAL_atomic_exchange(&g_ndnsContext.isResumeScan, FALSE);
	if( status != WMX_ST_OK ) // start another scan cycle
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "NDnSAgent_ConnectPhase2(OUT).ERROR NDnSAgent_StartConnect returned %d. ", status);
		FSM_SetStateIfNotEqual(&g_ndnsContext.fsm, fsmState, fsmState); // return to the previous state
		L4C_ScheduleTask(Task_StartScan); // reset the scheduler back to normal scan
		L4C_SetControllerMode(Ready, FALSE); // trigger the next scan cycle
	}

	L4S_EnableScanner(TRUE);
	g_ndnsContext.scheduledTaskData.status = status;
	OSAL_set_event(g_ndnsContext.connectCompletedEvent); // Connect

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_ConnectPhase2(OUT)");

	return status;
}

// Tries to Connect to specified Target, returns the status of the operation
wmx_Status_t NDnSAgent_StartConnect( wmx_NSPid_t nspID, wmx_CoexPriority_t coexPriority, BOOL forceManualConnect, BOOL useOnlyLastChannel )
{
	wmx_Status_t status, monitorStatus;
	wmx_BSSelectionCriteria_t bsCriteria;
	wmx_ConnectFlags_t connectFlags;
	wmx_AvailableNSP_t availableNSP;
	L4db_Nap_t naps[WMX_NAPS_MAX_NUM];
	UINT32 napsSize = WMX_NAPS_MAX_NUM;
	DiscoveredNap_t discoveredNAP;
	wmx_ChannelInfo_t chPlan[WMX_CHANNELS_MAX_NUM];
	UINT numChannels = WMX_CHANNELS_MAX_NUM;
	wmx_ConnectionMode_t connectionMode = CONNECTION_MODE_MANUAL;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Trying to connect - NSP ID = %d, forcing manual connect = %s", nspID, forceManualConnect == FALSE ? "False" : "True");

	FSM_SetStateIfNotEqual(&g_ndnsContext.fsm, L4C_CONNECTING, L4C_CONNECTING);

	// Save the NSP that we try to connect. When the indication of a successful connection arrives save it as the last connected
	Ndns_SetConnectingNSPTemp(nspID);
	g_ndnsContext.tempCoexPriority = coexPriority;

	if (GetAvailableNspNode(nspID, &availableNSP) == FALSE)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "NSP ID = %d could not be found in the available NSPs list. Aborting connect.", nspID);
		status = WMX_ST_WRONG_ARGUMENT;
		goto Finalize;
	}

	status = L4db_GetNapsInfo(nspID, (pL4db_Nap_t)naps, &napsSize);

	if (status != WMX_ST_OK && status != WMX_ST_DATA_UNAVAILABLE)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Error getting provisioned data for NSP ID = %d. Aborting connect.", nspID);
		goto Finalize;
	}

	// update the BS ID based on the available NSPs list entry
	OSAL_memcpy_s(naps[0].napID, sizeof(wmx_NAPid_t), availableNSP.bsId, sizeof(wmx_NAPid_t));

	// this NSP is not provisioned, so we'll use NapID=NspID
	if (napsSize < 1)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Couldn't find provisioned data for NSP ID = %d.", nspID);
		if (L4DB_GetNon16gDetectedNapInfo(naps[0].napID, &discoveredNAP, TRUE) != WMX_ST_OK)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Error getting discovered data for NAP ID = %d.", nspID);
			status = WMX_ST_FAIL;
			goto Finalize;

		}
		// copy the discovered channel plan
		OSAL_memcpy_s(&naps[0].discoveredChannelPlan, sizeof(naps[0].discoveredChannelPlan), &discoveredNAP.channelPlan, sizeof(discoveredNAP.channelPlan));
		napsSize = 1;
	}

	// convert the detected channels for the selected NSP from L4DB type to ND&S type
	wmxNds_DBChannelInfoToWmxChannelInfo(chPlan, &numChannels, naps[0].discoveredChannelPlan.channels, naps[0].discoveredChannelPlan.channelsSize);
		monitorStatus = wmx_PopulateMonitorSelectedChannel(chPlan, numChannels);

	// if we got here - it means that the L4Scanner has been stopped and is now in Ready state
	if ((!forceManualConnect) && (!L4S_AreChannelsInLastChunk(chPlan, numChannels)))
	{
			// the required channels for the selected NAP are not in the last scanned chunk -
			// so schedule a "scan-connect" sequence
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Required channels are not in the last chunk. Scheduling \"scan-connect\" sequence for NSP ID = %d", nspID);

			// save scheduled scan info
			if (useOnlyLastChannel)
			{
				UINT32 chId = L4db_GetFirstChannelWithFullPreambles(naps[0].discoveredChannelPlan.channels, naps[0].discoveredChannelPlan.channelsSize);
				if (chId == naps[0].discoveredChannelPlan.channelsSize) // there is no channel with full preambles
				{	// in case there are no full channels - use the first partial channel
					g_ndnsContext.scheduledTaskData.scanInfo.chPlanSize = 1;
					wmxNds_DBChannelInfoToWmxChannelInfo(chPlan, &numChannels, naps[0].discoveredChannelPlan.channels, 1);
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_WARNING, "Couldn't find a detected channel with full preambles. Using the last detected channel with SINGLE premable to connect.");
				}
				else
				{	// use the first full channel
					///Nardimon
					///If the first full preamble is not the same as the last discovered channel then we will send
					/// both of the channels with full preambles since the channel with not full preamble that was last
					/// discovered is from NBR_ADV
					if (naps[0].discoveredChannelPlan.channels[chId].firstFreq != naps[0].discoveredChannelPlan.channels[0].firstFreq)
					{
						// use the first full channel
						g_ndnsContext.scheduledTaskData.scanInfo.chPlanSize = 1;
						wmxNds_DBChannelInfoToWmxChannelInfo(chPlan, &numChannels, naps[0].discoveredChannelPlan.channels , 1);
						//// use the first full channel - THIS CODE IS REDUNDANT (AMIR & AVISHAI)
						//g_ndnsContext.scheduledTaskData.scanInfo.chPlanSize += 1;
						//wmxNds_DBChannelInfoToWmxChannelInfo(chPlan, &numChannels, naps[0].discoveredChannelPlan.channels + chId, 1);
					}
					else
					{
						// use the first full channel
						g_ndnsContext.scheduledTaskData.scanInfo.chPlanSize = 1;
						wmxNds_DBChannelInfoToWmxChannelInfo(chPlan, &numChannels, naps[0].discoveredChannelPlan.channels + chId, 1);
					TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Using the last detected channel with FULL premables to connect.");
					}
				}
				OSAL_memcpy_s(g_ndnsContext.scheduledTaskData.scanInfo.chPlan, sizeof(g_ndnsContext.scheduledTaskData.scanInfo.chPlan), chPlan, sizeof(chPlan));

			}
			else
			{
				// generate preferred channel plan for the requested NSP (neighbors first [if exist] and then full preambles)
				g_ndnsContext.scheduledTaskData.scanInfo.chPlanSize = WMX_CHANNELS_MAX_NUM;
				status = L4P_GenerateNspMCP(nspID, g_ndnsContext.scheduledTaskData.scanInfo.chPlan, &g_ndnsContext.scheduledTaskData.scanInfo.chPlanSize);
				// if we couldn't find any valid profile for this nsp (provisioned or not) - use the last detected
				if (status != WMX_ST_OK)
				{
					OSAL_memcpy_s(g_ndnsContext.scheduledTaskData.scanInfo.chPlan, sizeof(g_ndnsContext.scheduledTaskData.scanInfo.chPlan), chPlan, sizeof(chPlan));
					g_ndnsContext.scheduledTaskData.scanInfo.chPlanSize = numChannels;
				}
			}

			g_ndnsContext.scheduledTaskData.scanInfo.manualScanType = SCAN_TYPE_AUTO_CONNECT;

			// save scheduled connect info
			g_ndnsContext.scheduledTaskData.connectInfo.nspIDs[0] = nspID;
			g_ndnsContext.scheduledTaskData.connectInfo.numOfNspIDs = 1;
			g_ndnsContext.scheduledTaskData.connectInfo.coexPriority = coexPriority;
			// after the scheduled scan cycle - the detected channels for the selected NAP are in the last chunk
			g_ndnsContext.scheduledTaskData.connectInfo.forceManualConnect = TRUE;
			g_ndnsContext.scheduledTaskData.connectInfo.useOnlyLastChannel = useOnlyLastChannel;

			L4S_EnableScanner(TRUE); // enable the L4 Scanner to allow scanning
			// shift back to a scanning state
			FSM_SetState(&g_ndnsContext.fsm, FSM_GetPreviousState(&g_ndnsContext.fsm));
			L4C_ScheduleTask(Task_StartManualScan);

			// since we are already in "Ready" system state - generate an artificial "Ready" indication
			status = L4C_SetControllerMode(Ready, FALSE);
			return status;
	}
	else
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Trying to connect to NAP ID [%02x][%02x][%02x] (%02x%02x%02x)", naps[0].napID[0],
												naps[0].napID[1], naps[0].napID[2],
												naps[0].napID[0],
												naps[0].napID[1],
												naps[0].napID[2]);

		// now that we actually connect, we can configure the supplicant
		if( TRUE == g_ndnsContext.isSupplicantStarted )
		{
			status = wmx_EnableSupplicant(TRUE); // enable the Supplicant
			if ( WMX_ST_OK != status )
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Error enabling supplicant");
				goto Finalize;
			}
		}

		status = NDnSAgent_SetConfigToSupplicant( nspID );
		if ( WMX_ST_OK != status )
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Supplicant configuration error");
			goto Finalize;
		}

		Ndns_SetConnectingNAPTemp(naps[0].napID);
		NAP2BSCriteria( naps[0].napID, availableNSP.bsIdType, &bsCriteria );

		connectFlags.requestRealm = REQUEST_REALM_DONT_REQUEST;
		connectFlags.visitedNspId = 0;
		connectFlags.SIQ = 0;
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "trying to connect - bscriteria = %d, bscriteria mask = %d", bsCriteria.bsID, bsCriteria.bsIDmask);

		status = wmx_CmdStartConnect( bsCriteria, thCriteria, &connectFlags, coexPriority, connectionMode );
		if (status != WMX_ST_OK)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Connection attempt failed. status=%d", status);
			goto Finalize;
		}
	}

Finalize:
	if (status != WMX_ST_OK)
	{
		g_ndnsContext.eapContext.isStarted = FALSE;
		g_ndnsContext.eapContext.numEapRetries = 0;
		g_ndnsContext.eapContext.isEapFail = FALSE;
		g_ndnsContext.eapContext.isPerformedFallBack = FALSE;
		FSM_SetState(&g_ndnsContext.fsm, FSM_GetPreviousState(&g_ndnsContext.fsm));
	}

	return status;
}


// Tries to Connect to specified Target, returns the status of the operation
wmx_Status_t NDnSAgent_Connect( wmx_ConnectParams connectParams, wmx_CoexPriority_t coexPriority, wmx_pUserConnectStatus_t pUserConnectStatus )
{
	wmx_Status_t status = WMX_ST_OK;
	L4CState fsmState = FSM_GetState(&g_ndnsContext.fsm);
	//wmx_NSPid_t nspID = connectParams.nspID;
	UINT8 messageBuffer[sizeof(wmx_ConnectInfo_t)];
	wmx_ConnectInfo_t connectInfo;

	UNREFERENCED_PARAMETER(pUserConnectStatus);

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_Connect(IN)");
	// TODO: Oran - for future version - pass all the parameters in connectParams to StartConnect and use them.

	if (fsmState != L4C_SINGLE_SCAN && fsmState != L4C_AUTO_SCAN)
	{	// TODO: Oran - check also the state of the L4Scanner (and stop the scanning, if necessary)
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "NDnSAgent_Connect(OUT). ERROR WMX_ST_WRONG_STATE. fsmState=%d",fsmState);
		return WMX_ST_WRONG_STATE;
	}

	// Make sure that the NSP is in the available NSPs list
	if (CheckIfNspInAvailableList(connectParams.nspID) == FALSE)
	{

		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "NDnSAgent_Connect(OUT). NSP ID = %d was not found in available NSPs list", connectParams.nspID);
		PrintAvailableNspsList(FALSE, TR_SEV_NOTICE);

		return WMX_ST_WRONG_ARGUMENT;
	}

	// in case a scan process is taking place, notify device
	if(L4C_GetInitialCoexMode() == WMX_MODE_CM && g_ndnsContext.processStarted)
	{
		L4C_SetProcessStarted(FALSE);
		wmx_CmdProcessEnd();
	}

	L4C_ScheduleTask(Task_Reset); // reset the L4Controller
	L4S_EnableScanner(FALSE);
	L4S_Reset(FALSE); // reset the L4Scanner

	OSAL_reset_event(g_ndnsContext.connectCompletedEvent);

	connectInfo.nspIDs[0] = connectParams.nspID;
	connectInfo.numOfNspIDs = 1;
	connectInfo.coexPriority = coexPriority;
	connectInfo.forceManualConnect = FALSE;
	connectInfo.useOnlyLastChannel = FALSE;

	OSAL_memcpy_s(messageBuffer, sizeof(connectInfo), &connectInfo, sizeof(connectInfo));

	g_ndnsContext.scheduledTaskData.status = WMX_ST_FAIL;

	pUtils->tpfnPostRequest(MEDIUM, Task_Connect, (void*)messageBuffer, sizeof(wmx_ConnectInfo_t), &L4C_TaskHandler);

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Connect task was scheduled for the next system state Ready");

	if (OSAL_wait_event(g_ndnsContext.connectCompletedEvent, L4C_TIMEOUT * 2) != WAIT_OBJECT_0)
	{
		L4C_ScheduleTask(Task_StartScan); // reset the scheduler back to normal scan
		L4S_EnableScanner(TRUE);
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Error connecting to NSP ID = %d. Timeout expired !", connectParams.nspID);
		status = WMX_ST_FAIL;
	}
	else
	{
		status = g_ndnsContext.scheduledTaskData.status;
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_Connect(OUT) - status=%d", status);
	return status;
}


// Tries to Disconnect, returns the status of the operation
wmx_Status_t NDnSAgent_Disconnect()
{
	wmx_Status_t status;

	if (!FSM_SetStateIfEqual(&g_ndnsContext.fsm, L4C_CONNECTED, L4C_DISCONNECTING))
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "NDnSANDnSAgent_Disconnect failed to set state");
		return WMX_ST_WRONG_STATE;
	}

	// TODO: Oran - V3.0 phase2 new
	//status = NDnSAgent_StartDisconnect();
	OSAL_reset_event(g_ndnsContext.disconnectCompletedEvent); // Oran - new
	status = wmx_CmdDisconnect();

	if (status != WMX_ST_OK)
	{
		// change the state back the "CONNECTED" ONLY if we havn't got a new system state that caused us to reset the "DISCONNECTING" state
		FSM_SetStateIfEqual(&g_ndnsContext.fsm, L4C_DISCONNECTING, L4C_CONNECTED);
		return status;
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "waiting for disconnect complete event");
	if(OSAL_wait_event(g_ndnsContext.disconnectCompletedEvent, LONG_OPERATIONS_TIMEOUT) != WAIT_OBJECT_0)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "missing disconnect completed event");
		return WMX_ST_FAIL;
	}

	return WMX_ST_OK;
}

wmx_Status_t NDnSAgent_GenerateNAI(wmx_EapType_t eapMethod, char* id, char* provisionedPseudoId,
								   char* realm, BOOL isHome, wmx_pNAI_t nai, UINT32 naiSize)
{
	wmx_Status_t st;
	unsigned char macAddress[BM_GetDeviceInfo_List_CURRENT_DeviceDetails_MacAddress_BYTES_SIZE];
	wmx_ModelType_t modelType;
	wmx_Manufacturer_t manufacturerId;
	wmx_SerialNumber_t serialNumber;
	wmx_NvmVersion_t nvmVersion;
	wmx_Nonce_t nonce;
	wmx_ODMInfo_t odmInfo;
	wmx_DevicePID_t pid;
	wmx_ApdoOperatorConfigurations_t operatorConfigurations[MAX_NUM_OF_CONFIGURATIONS];
	int naiCounter;
	int numberOfNaiCounterDigits, remainder;
	char buff[BUFF_SIZE];
	size_t	tempLength = 0;
	BOOL res;
    char answer[MAX_ANSWER_SIZE];
	char macHex[BM_GetDeviceInfo_List_CURRENT_DeviceDetails_MacAddress_BYTES_SIZE*2 + 1];
	UINT32 noSm = FALSE;

	char * naiHex;
	naiHex = malloc ((sizeof(BYTE))*NUMBER_OF_GENERATED_CHARS_FOR_PSEUDO_NAI);
	if (naiHex == NULL){
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR	, "Could not allocate naiHex !!!");
		return WMX_ST_FAIL;
	}

	res = L4Configurations_getNoSM(&noSm);

	if((!res) && (!isHome) && ((WMX_EAP_TLS == eapMethod) || (WMX_EAP_TTLS == eapMethod)))
	{
		//concatenate the provisioning decoration
		OSAL_strcpy_s(nai, naiSize, PROVISIONING_NAI_DECORATION);
		L4Pub_SetDeviceStatusActivationConnect(TRUE);
	}
	else
	{
		OSAL_strcpy_s(nai, naiSize, "\0");
		L4Pub_SetDeviceStatusActivationConnect(FALSE);
	}

	res = L4Configurations_getCustomDecoration(buff);

	if ((res) && ((WMX_EAP_TLS == eapMethod) || (WMX_EAP_TTLS == eapMethod)))
	{
		OSAL_strcat_s(nai, naiSize, buff);
	}

	//regData = OSAL_ConfigController_GetStrValue(OSAL_KEY_ENTRY_PIPECONFIG, OSAL_KEY_ENTRY_DO_NOT_USE_PSEUDO_NAI, answer, MAX_ANSWER_SIZE);
	if(WMX_EAP_TTLS == eapMethod)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG	, "NDnSAgent_GenerateNAI : Trying GetApdoOperatorsConfigurations...");
		st = GetApdoOperatorsConfigurations(operatorConfigurations);

		if (st == WMX_ST_FAIL)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "NDnSAgent_GenerateNAI : GetApdoOperatorsConfigurations == WMX_ST_FAIL, Using default behavior which is do not disable pseudo nai !");
		}
		else
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "NDnSAgent_GenerateNAI : GetApdoOperatorsConfigurations != WMX_ST_FAIL, Using pseudoNAI == [%s].", (operatorConfigurations[0].disablePseudoNAI == TRUE ? "True" : "False"));
		}


		if (operatorConfigurations[0].disablePseudoNAI)
		{
			if('\0' != provisionedPseudoId[0])
			{
				OSAL_strcat_s(nai, naiSize, provisionedPseudoId);
			}
			else // generate the incremental NAI
			{
				naiCounter = L4db_GetNaiCounter();
				OSAL_itoa_s(naiCounter, buff, BUFF_SIZE, 10);
				OSAL_strcat_s(nai, naiSize, buff);
				naiCounter++;
				L4db_SetNaiCounter(naiCounter);
			}
		}
		else
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Trying GeneratePseudoNAI...");
			st = GeneratePseudoNAI(naiHex,NUMBER_OF_GENERATED_CHARS_FOR_PSEUDO_NAI);
			if (WMX_ST_OK != st)
			{
				free(naiHex);
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "GeneratePseudoNAI returned %d", st);
				return WMX_ST_FAIL;
			}
			naiCounter = L4db_GetNaiCounter();
			remainder = (naiCounter % 10);
			if (remainder == 0)
			{
				naiCounter = L4db_GetNaiCounter();
				naiCounter++;
				L4db_SetNaiCounter(naiCounter);
			}
			numberOfNaiCounterDigits =(int) log10(naiCounter) + 1;
			naiHex[(NUMBER_OF_GENERATED_CHARS_FOR_PSEUDO_NAI-(numberOfNaiCounterDigits+1))] = '\0';
			OSAL_strcat_s(nai, naiSize, naiHex);
			OSAL_itoa_s(naiCounter, buff, BUFF_SIZE, 10);
			OSAL_strcat_s(nai, naiSize, buff);
			naiCounter++;
			L4db_SetNaiCounter(naiCounter);
			if (naiHex)
			{
				free(naiHex);
				naiHex = NULL;
			}
		}
	}
	else if((WMX_EAP_TLS == eapMethod) && ('\0' == id[0]))//check if we should get the mac address or not
	{
		st = wmx_GetDeviceDetailsEx(macAddress, &modelType, &manufacturerId, &serialNumber, (wmx_pNvmVersion_t)&nvmVersion, (wmx_pODMInfo_t)&odmInfo, (wmx_pNonce_t)&nonce, (wmx_pDevicePID_t)&pid);

		if(WMX_ST_OK != st)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "wmx_GetDeviceDetails returned %d", st);
			return st;
		}

		NDnSAgent_HexEncode(macAddress, macHex, BM_GetDeviceInfo_List_CURRENT_DeviceDetails_MacAddress_BYTES_SIZE);

		tempLength = OSAL_strnlen(nai, MAX_STRING_VALIDATE);
		OSAL_strcat_s(nai, naiSize, (char*)macHex);
	}
	else
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "NDnSAgent_GenerateNAI: concatenate the given user identity ");
		//concatenate the given user identity - this patch is temp!!
		OSAL_strcat_s(nai, naiSize, id);
	}

	res = L4Configurations_getNoRealmInNAI(answer);
	if ( TRUE == res )
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "NDnSAgent_GenerateNAI: NoRealmInNAI patch no adding NAI");
		return WMX_ST_OK;
	}

	if('\0' != realm[0])
	{
		//concatenate the @
		OSAL_strcat_s(nai, naiSize, ADDRESS_STR);
		OSAL_strcat_s(nai, naiSize, realm);
	}
	else if(WMX_PLAIN_MSCHAPv2 != eapMethod) // concatenate "wimax.com" when in TLS/TTLS/AKA
	{
		//concatenate the @
		//OSAL_strcat_s(nai, naiSize, ADDRESS_STR);
		//OSAL_strcat_s(nai, naiSize, DEFAULT_REALM);
	}
	/*else
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "NDnSAgent_GenerateNAI: no realm or wrong eap method");
		return WMX_ST_FAIL;
	}*/

	return WMX_ST_OK;
}

// TTLS -> TLS fall-back support
BOOL NDnSAgent_HandleTTLSFallback(pEAP_t ttlsInfo)
{
	BOOL isTLS = FALSE;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_HandleTTLSFallback: isStarted = (%d) isEapFail = (%d)",g_ndnsContext.eapContext.isStarted, g_ndnsContext.eapContext.isEapFail);

	if (ttlsInfo->EAPMethod == WMX_EAP_TTLS)
	{
		if (g_ndnsContext.eapContext.isStarted && g_ndnsContext.eapContext.isEapFail)
		{
			switch(g_ndnsContext.eapContext.numEapRetries)
			{
			case 1:		// Fallback to TLS
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_HandleTTLSFallback: Tallback to TLS");
				// no more TTLS retries left. Move to TLS
				ttlsInfo->Password[0] = '\0';
				ttlsInfo->EAPMethod = WMX_EAP_TLS;
				isTLS = TRUE;
				g_ndnsContext.eapContext.numEapRetries = 0;
				g_ndnsContext.eapContext.isPerformedFallBack = TRUE;
				break;
			case 0:		// Run out of trials - Restarting...
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_WARNING, "NDnSAgent_HandleTTLSFallback: Ran out of trials - starting over TTLS-->TTLS-->TTLS-->TLS-->");
				g_ndnsContext.eapContext.numEapRetries = EAP_NUM_RETRIES;
			    break;
			default:	// This case assumes that numEapRetries > 1
				g_ndnsContext.eapContext.numEapRetries--;
			    break;
			}
		}
		else // start an EAP TTLS session. Assign number of TTLS retries before fall-back to TLS
		{
			g_ndnsContext.eapContext.isStarted = TRUE;
			g_ndnsContext.eapContext.numEapRetries = EAP_NUM_RETRIES;
			g_ndnsContext.eapContext.isEapFail = FALSE;
			g_ndnsContext.eapContext.isPerformedFallBack = FALSE;
		}
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_HandleTTLSFallback: g_ndnsContext.eapContext.numEapRetries = [%d]",g_ndnsContext.eapContext.numEapRetries);
	}
	else
	{
		g_ndnsContext.eapContext.isStarted = FALSE;
	}

	return isTLS;
}

// Set Config to Supplicant.
wmx_Status_t NDnSAgent_SetConfigToSupplicant( wmx_NSPid_t nspID )
{
	wmx_Status_t status;
	pProvisionedNsp_t provisionedNSP=NULL;
	pSubscriberInfo_t pPrimarySubsInfo = NULL;
	BOOL IsNewPrimarySubsInfoAlloc = FALSE;
	pEAP_t mainEap, encapsedEap = NULL;
	DeviceCertificates_t deviceCertificates;
	char buff[MAX_REGISTRY_ANSWER];
	wmx_EapMathod_t eapMethod, encapsedEapMethod;
	char eapTypeStr[MAX_NAME_STR_LEN];
	char identity[WMX_SUBSCRIBER_ID_MAX_SIZE + WMX_NSP_REALM_MAX_SIZE];
	UINT32 tagName;


	// This will reset both contexts:
	// 1. The config context that is saved for the supplicant
	// 2. The supplicant context itself that is saved by the SupplicantAgent.
	wmx_ResetSupplicant();
	provisionedNSP = (pProvisionedNsp_t)malloc(sizeof(ProvisionedNsp_t));
	if (provisionedNSP == NULL)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "malloc failed");
		return WMX_ST_FAIL;
	}

	status = L4db_GetProvisionedNsp(nspID, provisionedNSP);

	if (status == WMX_ST_DATA_UNAVAILABLE)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "SetConfigToSupplicant: nspID not found");

		pPrimarySubsInfo = (pSubscriberInfo_t)malloc(sizeof(SubscriberInfo_t));
		if (pPrimarySubsInfo == NULL)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "malloc failed");
			goto SupFinalize;
		}

		IsNewPrimarySubsInfoAlloc = TRUE;

		memset(&pPrimarySubsInfo->eapInfo[0], 0, sizeof(pPrimarySubsInfo->eapInfo[0]));
		pPrimarySubsInfo->activated = FALSE;
		pPrimarySubsInfo->eapInfoSize = 1;
		pPrimarySubsInfo->eapInfo[0].encaps = (UINT32)L4DB_INVALID_INDEX;
		pPrimarySubsInfo->eapInfo[0].EAPMethod = WMX_EAP_TLS;
		OSAL_strcpy_s(pPrimarySubsInfo->eapInfo[0].id, sizeof(pPrimarySubsInfo->eapInfo[0].id), "\0");
	}
	else if (status != WMX_ST_OK)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Error in NDnSAgent_SetConfigToSupplicant");
		goto SupFinalize;
	}
	else
	{
		pPrimarySubsInfo = &(provisionedNSP->subscriptionParams.primary);
	}

	status = L4db_GetDeviceCertificates(&deviceCertificates);
	if (status == WMX_ST_DATA_UNAVAILABLE)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: no certificates found");
		goto SupFinalize;
	}

	// sync between inner and outer EAP methods
	if (pPrimarySubsInfo->eapInfoSize > 1) // we've got a tunnel... Yeh !
	{
		OSAL_sscanf_s(pPrimarySubsInfo->eapInfo[1].tagName, "%d", &tagName);
		// The encaps value should be a pointer to the index of the entry this entry is encapsulated in
		if (pPrimarySubsInfo->eapInfo[0].encaps == tagName)
		{
			encapsedEap = &pPrimarySubsInfo->eapInfo[0]; // inner
			mainEap = &pPrimarySubsInfo->eapInfo[1]; // outer
		}
		// This is the case that should be accordiing to the OTA standard
		else
		{
			OSAL_sscanf_s(pPrimarySubsInfo->eapInfo[0].tagName, "%d", &tagName);
			if (pPrimarySubsInfo->eapInfo[1].encaps == tagName)
			{
				encapsedEap = &pPrimarySubsInfo->eapInfo[1]; // inner
				mainEap = &pPrimarySubsInfo->eapInfo[0]; // outer
			}
			else
			{
				mainEap = &pPrimarySubsInfo->eapInfo[0]; // no sync is possible, take the first one
				encapsedEap = &pPrimarySubsInfo->eapInfo[1];
				// TODO: Oran - report error
				encapsedEap->encaps = (UINT32)L4DB_INVALID_INDEX; // make sure we won't try to access the encapsed node
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant - error in encaps definition.");
			}
		}

		// check if we have TTLS->TLS fall-back
		if (mainEap && NDnSAgent_HandleTTLSFallback(mainEap))
		{	// override TTLS session with TLS
			encapsedEap = mainEap;
			encapsedEap->encaps = (UINT32)L4DB_INVALID_INDEX;
		}
	}
	else // no tunnel, boring...
	{
		mainEap = &pPrimarySubsInfo->eapInfo[0];
		mainEap->encaps = (UINT32)L4DB_INVALID_INDEX; // make sure we won't try to access the encapsed node
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "SetConfigToSupplicant started");

	// Make sure that the Eap method is provisioned
	if (mainEap->EAPMethod == L4DB_INVALID_INDEX)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: No provisioned EAP method");
		status = WMX_ST_CONFIG_ERROR;
		goto SupFinalize;
	}

	l4db_ConvertToEapTypeStr(mainEap->EAPMethod, eapTypeStr);
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "SetConfigToSupplicant: EAP method - %s", eapTypeStr);

	eapMethod = wmx_EapTypeToDS_EapType(mainEap->EAPMethod);
	if (eapMethod == SWC_EAP_TYPE_NONE)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: invalid EAP method");
		status = WMX_ST_CONFIG_ERROR;
		goto SupFinalize;
	}

	// set eap method (phase 1)
	status = wmx_SetEapMethod(eapMethod);
	if (WMX_ST_OK != status)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: SetEapMethod (encapsulating) failed");
		goto SupFinalize;
	}

	if ((encapsedEap == NULL) || (encapsedEap->encaps == L4DB_INVALID_INDEX)) // no encapsulating EAP method
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "SetConfigToSupplicant: Setting supplicant according main EAP entry ");

		// NAI formatting
		status = NDnSAgent_GenerateNAI(mainEap->EAPMethod, mainEap->id, mainEap->provisionedPseudoId,
			mainEap->realm, pPrimarySubsInfo->activated, identity, (UINT32)sizeof(identity));

		if (status != WMX_ST_OK)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: failed to generate encapsulating NAI");
			goto SupFinalize;
		}

		status = wmx_SetIdentity((wmx_pNAI_t)identity);

		if (WMX_ST_OK != status)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: SetIdentity failed");
			goto SupFinalize;
		}
		wmx_PopulateNAIMonitor((wmx_pNAI_t)identity);

		if (mainEap->Password[0] != '\0')
		{
			status = wmx_SetPassword((wmx_pUserPassword_t)mainEap->Password);
			if (WMX_ST_OK != status)
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: SetPassword failed");
				goto SupFinalize;
			}
		}
	}
	else
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_DEBUG, "SetConfigToSupplicant: Setting supplicant according tunnel EAP entry ");

		// Make sure that the Eap method is provisioned for the encaps eap
		if ((encapsedEap->vendorId != EAP_MSCHAPV2_VENDOR_ID) || (encapsedEap->vendorType != EAP_MSCHAPV2_VENDOR_TYPE))
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: problem in provisioned EAP method tunnel");
			status = WMX_ST_CONFIG_ERROR;
			goto SupFinalize;
		}

		encapsedEapMethod = SWC_PHASE2_METHOD_MSCHAPV2;

		// first set the phase 2 method
		status = wmx_SetPhase2Method(encapsedEapMethod); // set the provisioned eap method as the phase 2 method
		if (WMX_ST_OK != status)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: wmx_SetPhase2Method failed");
			goto SupFinalize;
		}

		// NAI formatting
		status = NDnSAgent_GenerateNAI(mainEap->EAPMethod, mainEap->id, mainEap->provisionedPseudoId,
			mainEap->realm, pPrimarySubsInfo->activated, identity, (UINT32)sizeof(identity));

		if (status != WMX_ST_OK)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: failed to generate encapsulating NAI");
			goto SupFinalize;
		}

		status = wmx_SetAnonymousIdentity((wmx_pNAI_t)identity);
		if (WMX_ST_OK != status)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: wmx_SetAnonymousIdentity failed");
			goto SupFinalize;
		}

		// NAI formatting
		status = NDnSAgent_GenerateNAI(encapsedEap->EAPMethod, encapsedEap->id, encapsedEap->provisionedPseudoId,
			encapsedEap->realm, pPrimarySubsInfo->activated, identity, (UINT32)sizeof(identity));

		if (status != WMX_ST_OK)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: failed to generate encapsulating NAI");
			goto SupFinalize;
		}

		status = wmx_SetIdentity((wmx_pNAI_t)identity);

		if (WMX_ST_OK != status)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: SetIdentity failed");
			goto SupFinalize;
		}
		wmx_PopulateNAIMonitor((wmx_pNAI_t)identity);

		if (encapsedEap->Password[0] != '\0')
		{
			status = wmx_SetPassword((wmx_pUserPassword_t)encapsedEap->Password);
			if (WMX_ST_OK != status)
			{
				TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: SetPassword failed");
				goto SupFinalize;
			}
		}
	}

	// now set the certificates
	if (deviceCertificates.rootCert[0] == '\0')
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: missing root cert");
		status = WMX_ST_CONFIG_ERROR;
		goto SupFinalize;
	}

	OSAL_BuildFullPath(deviceCertificates.rootCert, buff, MAX_REGISTRY_ANSWER -1);
	status = wmx_SetTlsCaCert((wmx_pCertificatePath_t)buff);
	if (WMX_ST_OK != status)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: SetTlsCaCert failed");
		goto SupFinalize;
	}

	if (deviceCertificates.clientCert[0] != '\0')
	{
		OSAL_BuildFullPath(deviceCertificates.clientCert, buff, MAX_REGISTRY_ANSWER -1);
		status = wmx_SetTlsClientCert((wmx_pCertificatePath_t)buff);
		if (WMX_ST_OK != status)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: SetTlsClientCert failed");
			goto SupFinalize;
		}
	}

	if (deviceCertificates.privateKey[0] != '\0')
	{
		OSAL_BuildFullPath(deviceCertificates.privateKey, buff, MAX_REGISTRY_ANSWER -1);
		status = wmx_SetTlsPrivateKey((wmx_pCertificatePath_t)buff);
		if (WMX_ST_OK != status)
		{
			TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "SetConfigToSupplicant: SetTlsPrivateKey failed");
			goto SupFinalize;
		}
	}


	if (mainEap->EAPMethod == WMX_EAP_AKA)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_WARNING, "SetConfigToSupplicant: AKA method is not supported");

		// TODO: unmark the next lines when .Pin will be supported in the Credentials struct. Erase the two lines above

		//if (eap->Pin[0] == '\0')
		//{
		//	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_WARNING, "SetConfigToSupplicant: Pin is empty");
		//	return WMX_ST_CONFIG_ERROR;
		//}
		//status = wmx_SetPin((wmx_pPin_t)pCredentials->Pin);
		//if (WMX_ST_OK != status)
		//{
		//	return status;
		//}
	}

SupFinalize:

	if(provisionedNSP)
		if (provisionedNSP != NULL){
			free(provisionedNSP);
			provisionedNSP=NULL;
		}

	if (IsNewPrimarySubsInfoAlloc == TRUE)
	{
		if(pPrimarySubsInfo)
			if (pPrimarySubsInfo != NULL){
				free(pPrimarySubsInfo);
				pPrimarySubsInfo = NULL;
			}
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "SetConfigToSupplicant finished successfully");

	return status;
}

wmx_Status_t NDnSAgent_InvokeDualFlushOp(L4C_Task task, void * paramsBuf, UINT32 bufSize)
{
	wmx_Status_t status = WMX_ST_OK;
	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_InvokeDualFlushOp(IN)");

	L4C_ScheduleTask(Task_Reset); // reset the L4Controller
	L4S_EnableScanner(FALSE);
	L4S_Reset(FALSE); // reset the L4Scanner

	OSAL_reset_event(g_ndnsContext.dualFlushOpEvent);

	g_ndnsContext.scheduledTaskData.status = WMX_ST_FAIL;

	Messenger_PostRequest(MEDIUM, task, paramsBuf, bufSize, &L4C_TaskHandler);

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "Task '%s' was scheduled for the L4C_TaskHandler", L4C_TaskStr(task));

	if (OSAL_wait_event(g_ndnsContext.dualFlushOpEvent, L4C_TIMEOUT * 2) != WAIT_OBJECT_0)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "Timout - task '%s' execution is too long (more than %d sec)", L4C_TaskStr(task), (L4C_TIMEOUT * 2 / 1000));
		L4C_ScheduleTask(Task_StartScan); // reset the scheduler back to normal scan
		L4S_EnableScanner(TRUE);
		status = WMX_ST_FAIL;
	}
	else
	{
		status = g_ndnsContext.scheduledTaskData.status;
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_InvokeDualFlushOp(OUT). Status = %s", wmxGetStatusStr(status));
	return status;
}

void NDnSAgent_OnProgressDualFlush(L4C_Task task, DualFlushOp ExecuteTask, UINT32 phaseNum)
{
	wmx_SystemState_t systemState;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_OnProgressDualFlush(IN) - task: '%s'", L4C_TaskStr(task));
	// get the current system state
	OSAL_enter_critical_section(&g_ndnsContext.lockSystemState);
	systemState = g_ndnsContext.systemState;
	OSAL_exit_critical_section(&g_ndnsContext.lockSystemState);

	if (phaseNum > 1)
	{
		// reset the L4Scanner in a hard manner since this is an adavanced phase of dual flush
		// (we are now using the messenger thread and the L4Scanner won't be able to get L4 indications
		// for successful reset)
		L4S_Reset(TRUE);
	}

	// if we are already in Ready state and there is no scan in progress - we can execute the connect task
	// (even if we are in Ready state - there could be a pending Scanning state indication from a previous scan)
	if ((systemState == Ready) && (!L4S_IsScanning()))
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_OnProgressDualFlush - executing the task: '%s'", L4C_TaskStr(task));
		// in case the scanner is active (and NOT scanning) - it means that
		// it is sleeping (LinkLoss / Standby state) - so we need to reset it
		if (L4S_READY != L4S_GetScannerState())
		{
			L4S_Reset(TRUE);
		}

		L4C_ScheduleTask(Task_StartScan); // after the session is over - return to normal scan mode
		L4C_UpdateScanStatus(FALSE);

		//Call The Task function if we have any
		if (ExecuteTask != NULL)
		{
			g_ndnsContext.scheduledTaskData.status = ExecuteTask();
		}
		else
		{
			g_ndnsContext.scheduledTaskData.status = WMX_ST_OK;
		}

		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_OnProgressDualFlush - task: '%s' executed", L4C_TaskStr(task));
		//Set Event to release the thread that is waiting for the answer (NDnSAgent_InvokeDualFlushOp())
		OSAL_set_event(g_ndnsContext.dualFlushOpEvent);
	}
	else
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_OnProgressDualFlush - scheduling the task '%s' for the next Ready state (current state=%s)", L4C_TaskStr(task), NDnSSystemStates(systemState));
		// we are still not in Ready state - schedule the Connect task to be executed
		// only when getting to Ready state
		L4C_ScheduleTask(task); // start the task on the next Ready state
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "NDnSAgent_OnProgressDualFlush(OUT)");
}


wmx_Status_t GetInstallationInfo(void *buffer, UINT32 *bufferSize)
{
	wmx_InstallationInfo_t installInfo;
	wmx_Status_t st;

	// validate return buffer size
	if (*bufferSize >= sizeof(wmx_InstallationInfo_t))
	{
		memset(&installInfo, 0, sizeof(wmx_InstallationInfo_t));

		st = L4db_GetInstallationInfo(installInfo.swVersion, MAX_INSTALLAION_VERSION_SIZE);

		if (st != WMX_ST_OK)
		{
			return st;
		}

		*(wmx_pInstallationInfo_t)buffer = installInfo;

		return WMX_ST_OK;
	}

	return WMX_ST_FAIL;
}

//Generates a string of 16 random chars.
wmx_Status_t GeneratePseudoNAI (char *pseudoNai, int buffSize){

	BOOL st;
	static char *lastString;

	if (pseudoNai == NULL || buffSize == 0) {
		return WMX_ST_FAIL;
	}

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "Trying OSAL_CreateRandomBit...");
	st = OSAL_CreateRandomBit(pseudoNai, buffSize);
	if (st != TRUE)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "OSAL_CreateRandomBit returned FAIL !");
		return WMX_ST_FAIL;
	}

	//pseudoNai[NUMBER_OF_GENERATED_CHARS_FOR_PSEUDO_NAI] = '\0';



	return WMX_ST_OK;

}

wmx_CoexistenceMode_t L4C_GetInitialCoexMode(void)
{
	return initialCoexMode;
}

void L4C_SetProcessStarted(BOOL isProcessStarted)
{
	g_ndnsContext.processStarted = isProcessStarted;
}

wmx_Status_t GetWMFComplianceInfo(void *buffer, UINT32 bufferSize)
{
	wmx_Status_t st;

	st = L4db_WMFComplianceVersion((char *)buffer, bufferSize);
	// validate L4db_WMFComplianceVersion function
	if(WMX_ST_OK == st)
	{
		return WMX_ST_OK;
	}

	return WMX_ST_FAIL;
}
wmx_Status_t L4C_HandleSystemStateUpdate(wmx_pSystemStateUpdate systemStateUpdate)
{
	BOOL channelUpdated = FALSE;
	// Set Control Mode:
	L4C_SetControllerModeEx(systemStateUpdate, TRUE);

	// Update Platform Noise Mitigation:
	L4db_PNMUpdate(systemStateUpdate, &channelUpdated);
	if (channelUpdated)
	{
		L4C_NotifyPNMChange();
	}
	return WMX_ST_OK;
}


wmx_Status_t L4C_NotifyPNMChange(void)
{
	UINT32 sendIndDataSize;
	SendIndData *pSendIndData;
	NDNS_MESSAGE *pIndMsg;
	UINT32 msgSize;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_NOTICE, "PNM_SendNotificationEventToPNMService: (IN)");

	msgSize = sizeof(NDNS_MESSAGE);
	sendIndDataSize = sizeof(SendIndData) + msgSize;
	pSendIndData = (SendIndData *)malloc(sendIndDataSize);
	if (pSendIndData == NULL)
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_CRITICAL, "malloc failed");
	}

	pIndMsg = (NDNS_MESSAGE *)(pSendIndData->indication_buffer);

	pSendIndData->pSenderFuncs = nds_pFuncs;
	pSendIndData->senderL5Conn = nds_l5ConnID;
	pSendIndData->pSubscribersList = g_ndnsContext.indicatorSubscribersList;
	pSendIndData->indication_id = NDNS_OPCODE_IND_PNM_NOTIFICATION;
	pSendIndData->indication_buffer_size = msgSize;
	pIndMsg->opcode = NDNS_OPCODE_IND_PNM_NOTIFICATION;
	pIndMsg->bufferLength = 0;

	TRACE(TR_MOD_NDNS_AGENT, TR_SEV_INFO, "PNM_SendNotificationEventToPNMService pSubscribersList=%d, indication_id=%d, "
		, pSendIndData->pSubscribersList, pSendIndData->indication_id);
	pUtils->tpfnPostRequest(
		MEDIUM, NDNS_OPCODE_INDICATION_ARRIVED, pSendIndData, sendIndDataSize, &SendIndicationToSubscribers);
	if (pSendIndData != NULL)
		free(pSendIndData);
	return WMX_ST_OK;
}




//	Platform Noise Mitigation
wmx_Status_t L4C_GetIsPNMCommDevUsingChannel(void *buffer, UINT32 *bufferSize)
{

	wmx_Status_t result = WMX_ST_OK;	// By default we are using the channel
	wmx_Status_t TMPstatus;
	PPNM_COMMDEV_CHANNEL_INFO givenPNMInfo;
	pnm_t innerPNMInfo;
	UINT32 innerBufferSize[1];
	*innerBufferSize = sizeof(pnm_t);
	UNREFERENCED_PARAMETER(bufferSize);

	givenPNMInfo = (PPNM_COMMDEV_CHANNEL_INFO)buffer;

	TMPstatus = L4db_GetPNM(&innerPNMInfo, innerBufferSize);

	if (TMPstatus == WMX_ST_OK)
	{
		// The case where we are not using the NPM:
		// 1. We are not connected:
		if (innerPNMInfo.m_dwConnectStatus == PNM_STATUS_NOT_CONNECTED)
		{
			result = WMX_ST_FAIL;
		}

		// 2. We are using a different channel:
		if (innerPNMInfo.m_dwCenterFrequency != givenPNMInfo->m_dwCenterFrequency ||		// Different center frequency or
			innerPNMInfo.m_dwFrequencySpread != givenPNMInfo->m_dwFrequencySpread)		// Different bandwidth
		{
			result = WMX_ST_FAIL;
		}


	}
	else
	{
		TRACE(TR_MOD_NDNS_AGENT, TR_SEV_ERR, "L4Pub_GetIsPNMCommDevUsingChannel: Could not get PNM info");
	}
	return result;
}