summaryrefslogtreecommitdiff
path: root/sta_dk_4_0_4_32/common/src/Management/apConn/apConn.c
blob: d025f8ee353ca2018f7ecbf9924e709996b215a8 (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
/****************************************************************************
**+-----------------------------------------------------------------------+**
**|                                                                       |**
**| Copyright(c) 1998 - 2008 Texas Instruments. All rights reserved.      |**
**| All rights reserved.                                                  |**
**|                                                                       |**
**| Redistribution and use in source and binary forms, with or without    |**
**| modification, are permitted provided that the following conditions    |**
**| are met:                                                              |**
**|                                                                       |**
**|  * Redistributions of source code must retain the above copyright     |**
**|    notice, this list of conditions and the following disclaimer.      |**
**|  * 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.                                                      |**
**|  * Neither the name Texas Instruments 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.  |**
**|                                                                       |**
**+-----------------------------------------------------------------------+**
****************************************************************************/


/** \file apConn.c
 *  \brief AP Connection
 *
 *  \see apConn.h
 */

/****************************************************************************
 *                                                                          *
 *   MODULE:  AP Connection                                                 *
 *   PURPOSE:                                                               *
 *   Roaming ability of eSTA is implemented by Roaming Manager Component and 
 *   described in "Roaming Manager module LLD" document, and by 
 *   AP Connection module. AP Connection module implemented as two sub-modules:
 *   The major one is AP Connection, that is responsible for: 
 *   - providing Roaming Manager with access to other parts of WLAN Driver, 
 *   - implementing low levels of roaming mechanism.
 *   Current BSS sub-module takes care of:
 *   - maintaining database of current AP info,
 *   - providing access to database of current AP info.
 *                                                                          *
 ****************************************************************************/

#include "osApi.h"

#include "paramIn.h"
#include "report.h"
#include "utils.h"

#include "smeSmApi.h"
#include "siteMgrApi.h"
#include "smeApi.h"
#include "PowerMgr_API.h"
#include "TrafficMonitorAPI.h"
#include "RateAdaptation.h"
#include "qosMngr_API.h"
#ifdef EXC_MODULE_INCLUDED
 #include "excMngr.h"
#endif
#include "measurementMgrApi.h"
#include "connApi.h"
#include "EvHandler.h"
#include "apConn.h"
#include "currBss.h"
#include "Ctrl.h"
#include "fsm.h"
#include "scrApi.h"
#include "regulatoryDomainApi.h"

/* Constants and macros */
#ifdef TI_DBG
 #define    AP_CONN_VALIDATE_HANDLE(hAPConnection)  \
    if (hAPConnection == NULL)  \
    {   \
        WLAN_OS_REPORT(("FATAL ERROR: AP Connection context is not initiated\n"));  \
        return NOK; \
    }
#else
 #define    AP_CONN_VALIDATE_HANDLE(hAPConnection)
#endif

#define MAX_ROAMING_TRIGGERS  ROAMING_TRIGGER_LAST

#define UPDATE_SEND_DEAUTH_PACKET_FLAG(roamingEventType) \
            if ((roamingEventType >= ROAMING_TRIGGER_MAX_TX_RETRIES) && \
                (roamingEventType != ROAMING_TRIGGER_SECURITY_ATTACK)) \
            { \
                pAPConnection->sendDeauthPacket = FALSE; \
            } 

/* Init bits */

 
/* Enumerations */

/**
* AP Connection state machine states
*/
typedef enum
{
    AP_CONNECT_STATE_IDLE = 0,          /**< Initial state */
    AP_CONNECT_STATE_WAIT_ROAM,         /**< Connected to AP, waiting for start roaming command */
    AP_CONNECT_STATE_SWITCHING_CHANNEL, /**< Connected to AP, switch channel in progress */
    AP_CONNECT_STATE_WAIT_CONNECT_CMD,  /**< SCR allocated, PS mode entered; wait for cmd from Roam Mngr */
    AP_CONNECT_STATE_PREPARE_HAND_OFF,  /**< Request CCKM for new AP, wait for response */
    AP_CONNECT_STATE_CONNECTING,        /**< Performing Connection to new AP; wait for response from Conn SM */
    AP_CONNECT_STATE_DISCONNECTING,     /**< Wait for completion of current link disconnection */
    AP_CONNECT_STATE_REESTABLISH_VOICE, /**< Wait for completion of voice TSPEC re-negotiation */
    AP_CONNECT_STATE_LAST
} apConn_smStates;


/**
* AP Connection state machine events
*/
typedef enum
{
    AP_CONNECT_EVENT_PREPARE_FOR_ROAMING= 0,/**< Sent by Roam MNGR when roaming event occurs */
    AP_CONNECT_EVENT_FINISHED_OK,           /**< Indicates successful completion of request sent to Conn SM */
    AP_CONNECT_EVENT_FINISHED_NOT_OK,       /**< Indicates unsuccessful completion of request sent to Conn SM */
    AP_CONNECT_EVENT_RETAIN_CURRENT_AP,     /**< Sent by Roam MNGR when it wishes to give-up roaming */
    AP_CONNECT_EVENT_START,                 /**< Sent by SME when first time link to AP is established */
    AP_CONNECT_EVENT_START_ROAM,            /**< Sent by Roam MNGR when it wishes to roam to new AP */
    AP_CONNECT_EVENT_START_SWITCH_CHANNEL,  /**< Sent by Switch channel module when starting switch channel process (tx enabled) */
    AP_CONNECT_EVENT_FINISHED_SWITCH_CH,    /**< Sent by Switch channel module when finishing switch channel process (tx enabled) */
    AP_CONNECT_EVENT_FINISHED_HAND_OVER,    /**< Sent by EXC module when finishing hand-over */
    AP_CONNECT_EVENT_STOP,                  /**< Disconnect current link, send stop indication to other modules */
    AP_CONNECT_EVENT_LAST
} apConn_smEvents;

#define AP_CONNECT_NUM_STATES       AP_CONNECT_STATE_LAST    
#define AP_CONNECT_NUM_EVENTS       AP_CONNECT_EVENT_LAST   


#ifdef REPORT_LOG

/* Global variables */
static char *apConn_stateDesc[AP_CONNECT_NUM_STATES] = {  
        "APC_STATE_IDLE",              
        "APC_STATE_WAIT_ROAM",                                                
        "APC_STATE_SWITCHING_CHANNEL",                                         
        "APC_STATE_WAIT_CONNECT_CMD",                                                  
        "APC_STATE_PREPARE_HAND_OFF",
        "APC_STATE_CONNECTING",
        "APC_STATE_DISCONNECTING",
        "APC_STATE_REESTABLISH_VOICE"
    };
    
static char *apConn_eventDesc[AP_CONNECT_NUM_EVENTS] = {
        "APC_EVENT_PREPARE_FOR_ROAMING", 
        "APC_EVENT_FINISHED_OK",            
        "APC_EVENT_FINISHED_NOT_OK",            
        "APC_EVENT_RETAIN_CURRENT_AP",  
        "APC_EVENT_START",             
        "APC_EVENT_START_ROAM",             
        "APC_EVENT_START_SWITCH_CHANNEL",             
        "APC_EVENT_FINISHED_SWITCH_CHANNEL",             
        "APC_EVENT_FINISHED_HAND_OVER",             
        "APC_EVENT_STOP"                 
    };
                           
#endif


/* Typedefs */

/* Structures */

/**
* AP Connection control block 
* Following structure defines parameters that can be configured externally,
* internal variables, AP Connection state machine and handlers of other modules
* used by AP Connection module
*/
typedef struct _apConn_t
{
    /* AP Connection state machine */
    fsm_stateMachine_t      *apConnSM;          /**< AP Connection module state machine */
    apConn_smStates         currentState;       /**< AP Connection state machine current state */
    
    /* Internal parameters */
    BOOL                    firstAttempt2Roam;  /**< TRUE if still connected to original AP, FALSE otherwise */
    BOOL                    roamingEnabled;     /**< If FALSE, act like if no roaming callback registered. */
    apConn_roamingTrigger_e roamReason;         /**< The most severe and recent reason for roaming */
    APDisconnect_t          APDisconnect;       /**< The AP disconnect trigger extra information */
    bssEntry_t              *newAP;             /**< Stores parameters of roaming candidate */
    apConn_connRequest_e    requestType;        /**< Stores type of roaming request */
    INT8                    rssiThreshold;      /**< Stores recently configured RSSI threshold */
    UINT8                   snrThreshold;       /**< Stores recently configured SNR threshold */
    UINT8                   txFailureThreshold; /**< Stores recently configured consec. no ack threshold */
    UINT8                   lowRateThreshold;   /**< Stores recently configured consec. no ack threshold */
    UINT32                  vsIElength;         /**< Length of vendor specific info-element for assoc req (if defined) */
    char                    *vsIEbuf;           /**< Pointer to vendor specific info-element for assoc req (if defined) */
    BOOL                    isRssiTriggerMaskedOut;
    BOOL                    isSnrTriggerMaskedOut;
    BOOL                    isConsTxFailureMaskedOut;
    BOOL                    islowRateTriggerMaskedOut;
    BOOL                    removeKeys;         /**< Indicates whether keys should be removed after disconnect or not */
    BOOL                    ignoreDeauthReason0;/**< Indicates whether to ignore DeAuth with reason 0, required for Rogue AP test EXC-V2 */
    BOOL                    sendDeauthPacket;   /**< Indicates whether to send DEAUTH packet when discommecting or not */
    BOOL                    voiceTspecConfigured;/**< Shall be set to TRUE before roaming in case the TSPEC is configured */
	BOOL                    videoTspecConfigured;/**< Shall be set to TRUE before roaming in case the TSPEC is configured */
    BOOL                    reNegotiateTSPEC;   /**< Shall be set to TRUE before hand-over if requested by Roaming Manager */
    BOOL                    resetReportedRoamingStatistics; /**< Shall be set to TRUE if starting to measure traffic */
    UINT16                  lastRoamingDelay;
    UINT32                  roamingStartedTimestamp;
    UINT8                   roamingSuccesfulHandoverNum;
    BOOL                    bNonRoamingDisAssocReason; /**< Indicate whether last disconnection was called from outside (SME) */            
    /** Callback functions, registered by Roaming manager */
    apConn_roamMngrCallb_t  roamEventCallb;         /**< roam event triggers */ 
    apConn_roamMngrCallb_t  reportStatusCallb;      /**< connection status events  */
    apConn_roamMngrCallb_t  returnNeighborApsCallb; /**< neighbor APs list update */
    
    /* Handlers of other modules used by AP Connection */
    TI_HANDLE               hOs;
    TI_HANDLE               hReport;
    TI_HANDLE               hCurrBSS;
    TI_HANDLE               hRoamMng;
    TI_HANDLE               hSme;
    TI_HANDLE               hSiteMgr;
    TI_HANDLE               hExcMngr;
    TI_HANDLE               hConnSm;
    TI_HANDLE               hPrivacy;
    TI_HANDLE               hQos;
    TI_HANDLE               hRateAdapt;
    TI_HANDLE               hEvHandler; 
    TI_HANDLE               hScr;   
    TI_HANDLE               hAssoc;
    TI_HANDLE               hRegulatoryDomain;
    
    /* Counters for statistics */
    UINT32                  roamingTriggerEvents[MAX_ROAMING_TRIGGERS];
    UINT32                  roamingSuccesfulHandoverTotalNum;   
    UINT32                  roamingFailedHandoverNum;   
    UINT32                  retainCurrAPNum;
    UINT32                  disconnectFromRoamMngrNum;
    UINT32                  stopFromSmeNum;
} apConn_t;

 
/* Internal functions prototypes */

/* SM functions */
static TI_STATUS apConn_smEvent(apConn_smStates *currState, UINT8 event, void* data);
static TI_STATUS apConn_smNop(void *pData);
static TI_STATUS apConn_smUnexpected(void *pData);
static TI_STATUS apConn_startWaitingForTriggers(void *pData);
static TI_STATUS apConn_connectedToNewAP(void *pData);
static TI_STATUS apConn_configureDriverBeforeRoaming(void *pData);
static TI_STATUS apConn_stopConnection(void *pData);
static TI_STATUS apConn_invokeConnectionToNewAp(void *pData);
static TI_STATUS apConn_reportDisconnected(void *pData);
static TI_STATUS apConn_retainAP(void *pData);
static TI_STATUS apConn_requestCCKM(void *pData);
static TI_STATUS apConn_reportConnFail(void *pData);
static TI_STATUS apConn_swChFinished(void *pData);
static TI_STATUS apConn_handleTspecReneg (void *pData);

/* other functions */
#ifdef EXC_MODULE_INCLUDED
static void apConn_calcNewTsf(apConn_t *hAPConnection, UINT8 *tsfTimeStamp, UINT32 newSiteOsTimeStamp, UINT32 beaconInterval);
#endif
static TI_STATUS apConn_qosMngrReportResultCallb (TI_HANDLE hApConn, trafficAdmRequestStatus_e result);
static void      apConn_reportConnStatusToSME    (apConn_t *pAPConnection);


/* Public functions implementation */

/**
*
* apConn_create
*
* \b Description: 
*
* Create the AP Connection context: 
* allocate memory for internal variables; 
* create state machine.
*
* \b ARGS:
*
*  I   - hOs - OS handler
*  
* \b RETURNS:
*
* Pointer to the AP Connection on success, NULL on failure 
* (unable to allocate memory or other error).
*
* \sa 
*/
TI_HANDLE apConn_create(TI_HANDLE hOs)
{
    TI_STATUS   status;
    apConn_t    *pAPConnection;

    if ((pAPConnection = os_memoryAlloc(hOs, sizeof(apConn_t))) != NULL)
    {
        pAPConnection->hOs = hOs;
    
        status = fsm_Create(hOs, &(pAPConnection->apConnSM), AP_CONNECT_NUM_STATES, AP_CONNECT_NUM_EVENTS);
        if (status == OK) 
        {
            /* Succeeded to create AP Connection module context - return pointer to it */
            return pAPConnection;
        }
        else /* Failed to create state machine */
        {
            WLAN_OS_REPORT(("FATAL ERROR: apConn_create(): Error creating apConnSM - aborting\n"));
            /* Free pre-allocated control block */
            utils_nullMemoryFree(hOs, pAPConnection, sizeof(apConn_t));
            return NULL;
        }
    }
    else /* Failed to allocate control block */
    {
        WLAN_OS_REPORT(("FATAL ERROR: apConn_create(): Error allocating cb - aborting\n"));
        return NULL;
    }
}

/**
*
* apConn_unload
*
* \b Description: 
*
* Finish AP Connection module work:
* release the allocation for state machine and internal variables.
*
* \b ARGS:
*
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_unload(TI_HANDLE hAPConnection)
{
    apConn_t *pAPConnection;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    pAPConnection = (apConn_t *)hAPConnection;

    /* Unload state machine */
    fsm_Unload(pAPConnection->hOs, pAPConnection->apConnSM);

    /* Free pre-allocated control block */
    utils_nullMemoryFree(pAPConnection->hOs, pAPConnection, sizeof(apConn_t));

    return OK;
}

/**
*
* apConn_config
*
* \b Description: 
*
* Prepare AP Connection module to work: initiate internal variables, start state machine
*
* \b ARGS:
*
*  I   - hCurrAP - Current BSS handle \n
*  I   - hRoamMng - Roaming Manager handle  \n
*  I   - hSme - SME context  \n
*  I   - hPowerMgr - Power Manager context  \n
*  
* \b RETURNS:
*
*  OK on success, NOK on failure.
*
* \sa 
*/
TI_STATUS apConn_config(TI_HANDLE hAPConnection, 
                        TI_HANDLE hReport,
                        TI_HANDLE hCurrAP,
                        TI_HANDLE hRoamMng,
                        TI_HANDLE hSme,
                        TI_HANDLE hSiteMgr,
                        TI_HANDLE hExcMngr,
                        TI_HANDLE hConnSm,
                        TI_HANDLE hPrivacy,
                        TI_HANDLE hQos,
                        TI_HANDLE hCtrl,
                        TI_HANDLE hEvHandler,
                        TI_HANDLE hScr,
                        TI_HANDLE hAssoc,
                        TI_HANDLE hRegulatoryDomain,
                        apConnParams_t *pApConnParams)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;
    TI_STATUS status;
    UINT32  index;

    /** State Machine matrix */
    fsm_actionCell_t apConn_matrix[AP_CONNECT_NUM_STATES][AP_CONNECT_NUM_EVENTS] =
    {
        /* next state and actions for IDLE state */
        {   {AP_CONNECT_STATE_IDLE, apConn_smUnexpected},               /* PREPARE_FOR_ROAMING  */ 
            {AP_CONNECT_STATE_IDLE, apConn_smUnexpected},               /* FINISHED_OK          */
            {AP_CONNECT_STATE_IDLE, apConn_smUnexpected},               /* FINISHED_NOT_OK      */
            {AP_CONNECT_STATE_IDLE, apConn_smUnexpected},               /* RETAIN_CURRENT_AP    */ 
            {AP_CONNECT_STATE_WAIT_ROAM,apConn_startWaitingForTriggers},/* START                */ 
            {AP_CONNECT_STATE_IDLE, apConn_smUnexpected},               /* START_ROAM           */ 
            {AP_CONNECT_STATE_IDLE, apConn_smUnexpected},               /* START_SWITCH_CHANNEL */ 
            {AP_CONNECT_STATE_IDLE, apConn_smNop},                      /* FINISHED_SWITCH_CH   */ 
            {AP_CONNECT_STATE_IDLE, apConn_smNop},                      /* FINISHED_HAND_OVER   */ 
            {AP_CONNECT_STATE_IDLE, apConn_smUnexpected}                /* STOP                 */
        },                                                                              
        /* next state and actions for WAIT_ROAM state */
        {   {AP_CONNECT_STATE_WAIT_CONNECT_CMD,apConn_configureDriverBeforeRoaming},/* PREPARE_FOR_ROAMING  */ 
            {AP_CONNECT_STATE_WAIT_ROAM, apConn_smUnexpected},      /* FINISHED_OK          */
            {AP_CONNECT_STATE_WAIT_ROAM, apConn_smUnexpected},      /* FINISHED_NOT_OK      */
            {AP_CONNECT_STATE_WAIT_ROAM, apConn_smUnexpected},      /* RETAIN_CURRENT_AP    */ 
            {AP_CONNECT_STATE_WAIT_ROAM, apConn_smUnexpected},      /* START                */ 
            {AP_CONNECT_STATE_WAIT_ROAM, apConn_smUnexpected},      /* START_ROAM           */ 
            {AP_CONNECT_STATE_SWITCHING_CHANNEL, apConn_smNop},     /* START_SWITCH_CHANNEL */ 
            {AP_CONNECT_STATE_WAIT_ROAM, apConn_smUnexpected},      /* FINISHED_SWITCH_CH   */ 
            {AP_CONNECT_STATE_WAIT_ROAM, apConn_smUnexpected},      /* FINISHED_HAND_OVER   */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_stopConnection} /* STOP                 */
        },                                                                              
        /* next state and actions for SWITCHING_CHANNEL state */
        {   {AP_CONNECT_STATE_SWITCHING_CHANNEL, apConn_smUnexpected},  /* PREPARE_FOR_ROAMING  */ 
            {AP_CONNECT_STATE_SWITCHING_CHANNEL, apConn_smUnexpected},  /* FINISHED_OK          */
            {AP_CONNECT_STATE_SWITCHING_CHANNEL, apConn_smUnexpected},  /* FINISHED_NOT_OK      */
            {AP_CONNECT_STATE_SWITCHING_CHANNEL, apConn_smUnexpected},  /* RETAIN_CURRENT_AP    */ 
            {AP_CONNECT_STATE_SWITCHING_CHANNEL, apConn_smUnexpected},  /* START                */ 
            {AP_CONNECT_STATE_SWITCHING_CHANNEL, apConn_smUnexpected},  /* START_ROAM           */ 
            {AP_CONNECT_STATE_SWITCHING_CHANNEL, apConn_swChFinished},  /* START_SWITCH_CHANNEL */ 
            {AP_CONNECT_STATE_WAIT_ROAM, apConn_smNop},                 /* FINISHED_SWITCH_CH   */ 
            {AP_CONNECT_STATE_SWITCHING_CHANNEL, apConn_smUnexpected},  /* FINISHED_HAND_OVER   */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_stopConnection}     /* STOP                 */
        },                                                                              
        /* next state and actions for WAIT_CONNECT_CMD state */
        {   {AP_CONNECT_STATE_WAIT_CONNECT_CMD, apConn_smUnexpected},   /* PREPARE_FOR_ROAMING  */ 
            {AP_CONNECT_STATE_WAIT_CONNECT_CMD, apConn_smUnexpected},   /* FINISHED_OK          */
            {AP_CONNECT_STATE_WAIT_CONNECT_CMD, apConn_smUnexpected},   /* FINISHED_NOT_OK      */
            {AP_CONNECT_STATE_WAIT_ROAM, apConn_retainAP},              /* RETAIN_CURRENT_AP    */ 
            {AP_CONNECT_STATE_WAIT_CONNECT_CMD, apConn_smUnexpected},   /* START                */ 
            {AP_CONNECT_STATE_PREPARE_HAND_OFF, apConn_requestCCKM},    /* START_ROAM           */ 
            {AP_CONNECT_STATE_WAIT_CONNECT_CMD, apConn_smUnexpected},   /* START_SWITCH_CHANNEL */ 
            {AP_CONNECT_STATE_WAIT_CONNECT_CMD, apConn_smUnexpected},   /* FINISHED_SWITCH_CH   */ 
            {AP_CONNECT_STATE_WAIT_CONNECT_CMD, apConn_smUnexpected},   /* FINISHED_HAND_OVER   */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_stopConnection}     /* STOP                 */
        },                                                                              
        /* next state and actions for PREPARE_HAND_OFF state */
        {   {AP_CONNECT_STATE_PREPARE_HAND_OFF, apConn_smUnexpected},   /* PREPARE_FOR_ROAMING  */ 
            {AP_CONNECT_STATE_PREPARE_HAND_OFF, apConn_smUnexpected},   /* FINISHED_OK          */
            {AP_CONNECT_STATE_PREPARE_HAND_OFF, apConn_smUnexpected},   /* FINISHED_NOT_OK      */
            {AP_CONNECT_STATE_PREPARE_HAND_OFF, apConn_smUnexpected},   /* RETAIN_CURRENT_AP    */ 
            {AP_CONNECT_STATE_PREPARE_HAND_OFF, apConn_smUnexpected},   /* START                */ 
            {AP_CONNECT_STATE_PREPARE_HAND_OFF, apConn_smUnexpected},   /* START_ROAM           */ 
            {AP_CONNECT_STATE_PREPARE_HAND_OFF, apConn_smUnexpected},   /* START_SWITCH_CHANNEL */ 
            {AP_CONNECT_STATE_PREPARE_HAND_OFF, apConn_smUnexpected},   /* FINISHED_SWITCH_CH   */ 
            {AP_CONNECT_STATE_CONNECTING, apConn_invokeConnectionToNewAp},/* FINISHED_HAND_OVER */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_stopConnection}     /* STOP                 */
        },                                                                              
        /* next state and actions for CONNECTING state */
        {   {AP_CONNECT_STATE_CONNECTING, apConn_smUnexpected},         /* PREPARE_FOR_ROAMING  */ 
            {AP_CONNECT_STATE_REESTABLISH_VOICE,apConn_handleTspecReneg},/* FINISHED_OK             */
            {AP_CONNECT_STATE_WAIT_CONNECT_CMD, apConn_reportConnFail}, /* FINISHED_NOT_OK      */
            {AP_CONNECT_STATE_CONNECTING, apConn_smUnexpected},         /* RETAIN_CURRENT_AP    */ 
            {AP_CONNECT_STATE_CONNECTING, apConn_smUnexpected},         /* START                */ 
            {AP_CONNECT_STATE_CONNECTING, apConn_smUnexpected},         /* START_ROAM           */ 
            {AP_CONNECT_STATE_CONNECTING, apConn_smUnexpected},         /* START_SWITCH_CHANNEL */ 
            {AP_CONNECT_STATE_CONNECTING, apConn_smUnexpected},         /* FINISHED_SWITCH_CH   */ 
            {AP_CONNECT_STATE_CONNECTING, apConn_smUnexpected},         /* FINISHED_HAND_OVER   */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_stopConnection}     /* STOP                 */
        },                                                                              
        /* next state and actions for DISCONNECTING state */
        {   {AP_CONNECT_STATE_DISCONNECTING, apConn_smNop},         /* PREPARE_FOR_ROAMING  */ 
            {AP_CONNECT_STATE_IDLE, apConn_reportDisconnected},     /* FINISHED_OK          */
            {AP_CONNECT_STATE_IDLE, apConn_reportDisconnected},     /* FINISHED_NOT_OK      */
            {AP_CONNECT_STATE_DISCONNECTING, apConn_smNop},         /* RETAIN_CURRENT_AP    */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_smUnexpected},  /* START                */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_smNop},         /* START_ROAM           */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_smNop},         /* START_SWITCH_CHANNEL */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_smNop},         /* FINISHED_SWITCH_CH   */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_smNop},         /* FINISHED_HAND_OVER   */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_smNop},         /* STOP                 */
        },                                                                             
        /* next state and actions for REESTABLISH_VOICE state */
        {   {AP_CONNECT_STATE_REESTABLISH_VOICE, apConn_smUnexpected},  /* PREPARE_FOR_ROAMING  */ 
            {AP_CONNECT_STATE_WAIT_ROAM,apConn_connectedToNewAP},       /* FINISHED_OK          */
            {AP_CONNECT_STATE_WAIT_CONNECT_CMD, apConn_reportConnFail}, /* FINISHED_NOT_OK      */
            {AP_CONNECT_STATE_REESTABLISH_VOICE, apConn_smUnexpected},  /* RETAIN_CURRENT_AP    */ 
            {AP_CONNECT_STATE_REESTABLISH_VOICE, apConn_smUnexpected},  /* START                */ 
            {AP_CONNECT_STATE_REESTABLISH_VOICE, apConn_smUnexpected},  /* START_ROAM           */ 
            {AP_CONNECT_STATE_REESTABLISH_VOICE, apConn_smUnexpected},  /* START_SWITCH_CHANNEL */ 
            {AP_CONNECT_STATE_REESTABLISH_VOICE, apConn_smUnexpected},  /* FINISHED_SWITCH_CH   */ 
            {AP_CONNECT_STATE_REESTABLISH_VOICE, apConn_smUnexpected},  /* FINISHED_HAND_OVER   */ 
            {AP_CONNECT_STATE_DISCONNECTING, apConn_stopConnection}     /* STOP                 */
        }                                                                             
    }; 

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    status = fsm_Config(pAPConnection->apConnSM, 
                        (fsm_Matrix_t)&apConn_matrix[0][0], 
                        AP_CONNECT_NUM_STATES, 
                        AP_CONNECT_NUM_EVENTS, 
                        (fsm_eventActivation_t)apConn_smEvent, 
                        pAPConnection->hOs);


    if (status == OK)
    {
        pAPConnection->currentState = AP_CONNECT_STATE_IDLE;
        pAPConnection->firstAttempt2Roam = TRUE;
        pAPConnection->hReport = hReport;
        pAPConnection->hCurrBSS = hCurrAP;
        pAPConnection->hRoamMng = hRoamMng;
        pAPConnection->hSme = hSme;
        pAPConnection->hSiteMgr = hSiteMgr;
        pAPConnection->hExcMngr = hExcMngr;
        pAPConnection->hConnSm = hConnSm;
        pAPConnection->hPrivacy = hPrivacy;
        pAPConnection->hQos = hQos;
        pAPConnection->hRateAdapt = ((ctrlData_t *)hCtrl)->pRateAdaptation;
        pAPConnection->hEvHandler = hEvHandler;
        pAPConnection->hScr = hScr;
        pAPConnection->hAssoc = hAssoc;
        pAPConnection->hRegulatoryDomain = hRegulatoryDomain;

        pAPConnection->roamingEnabled = TRUE;
        pAPConnection->reportStatusCallb = NULL;
        pAPConnection->roamEventCallb = NULL;
        pAPConnection->returnNeighborApsCallb = NULL;
        pAPConnection->ignoreDeauthReason0 = pApConnParams->ignoreDeauthReason0;

        for (index=ROAMING_TRIGGER_NONE; index<ROAMING_TRIGGER_LAST; index++)
        {
            pAPConnection->roamingTriggerEvents[index] = 0;
        }
        pAPConnection->roamingSuccesfulHandoverNum = 0;
        pAPConnection->roamingSuccesfulHandoverTotalNum = 0;
        pAPConnection->roamingFailedHandoverNum = 0;
        pAPConnection->retainCurrAPNum = 0;
        pAPConnection->disconnectFromRoamMngrNum = 0;
        pAPConnection->stopFromSmeNum = 0;
        pAPConnection->txFailureThreshold = NO_ACK_DEFAULT_THRESHOLD;
        pAPConnection->lowRateThreshold = LOW_RATE_DEFAULT_THRESHOLD;
        pAPConnection->rssiThreshold = RSSI_DEFAULT_THRESHOLD;
        pAPConnection->snrThreshold = SNR_DEFAULT_THRESHOLD;
        pAPConnection->vsIElength = 0;
        pAPConnection->isRssiTriggerMaskedOut = FALSE;
        pAPConnection->isSnrTriggerMaskedOut = TRUE;
        pAPConnection->islowRateTriggerMaskedOut = FALSE;
        pAPConnection->isConsTxFailureMaskedOut = FALSE;
        pAPConnection->removeKeys = TRUE;
        pAPConnection->sendDeauthPacket = TRUE;       /* Default behavior is radio On - send DISASSOC frame */
        pAPConnection->voiceTspecConfigured = FALSE;
		pAPConnection->videoTspecConfigured = FALSE;
        pAPConnection->resetReportedRoamingStatistics = FALSE;
        pAPConnection->reNegotiateTSPEC = FALSE;
        pAPConnection->bNonRoamingDisAssocReason = FALSE;

        pAPConnection->roamingStartedTimestamp = 0;
        pAPConnection->lastRoamingDelay = 0;
        pAPConnection->roamingSuccesfulHandoverNum = 0;

        return OK;
    }
    else/* Unable to configure State Machine */
    {
        WLAN_REPORT_ERROR(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, ("FATAL ERROR: apConn_config(): Failed to configure sm\n"));
        return status;
    }
}


/**
*
* apConn_isPsRequiredBeforeScan
*
* \b Description: 
*
* verify if the PS required before scan according if roaming triger is part of ROAMING_TRIGGER_LOW_QUALITY_GROUP 
*
* \b ARGS:
*
*  I   - hAPConnection - pointer to module\n
*
* \b RETURNS:
*
*  TRUE or FALSE.
*
* \sa 
*/
BOOLEAN apConn_isPsRequiredBeforeScan(TI_HANDLE hAPConnection)
{
    apConn_t * pAPConnection = (apConn_t *) hAPConnection;

	/* check if part of ROAMING_TRIGGER_LOW_QUALITY_GROUP */
	if (pAPConnection->roamReason <= ROAMING_TRIGGER_MAX_TX_RETRIES)
		return TRUE;
	else
		return FALSE;
}

/**
*
* apConn_ConnCompleteInd
*
* \b Description: 
*
* Inform AP Connection about successful / unsuccessful completion 
* of link establishing 
*
* \b ARGS:
*
*  I   - result - OK if successfully connected, NOK otherwise  \n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_ConnCompleteInd(TI_HANDLE hAPConnection, mgmtStatus_e status, UINT32 uStatusCode)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    if (status == STATUS_SUCCESSFUL) 
    {
        apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_OK, pAPConnection);
    }
    else
    {
        apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_NOT_OK, pAPConnection);
    }
    return OK;
}


/**
*
* apConn_getRoamThresholds
*
* \b Description: 
*
* Used to query for the current roaming thresholds configuration
*
* \b ARGS:
*
*  I   - hAPConnection - pointer to module\n
*  O   - pParam - pointer to buffer to store the thresholds\n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_getRoamThresholds(TI_HANDLE hAPConnection, roamingMngrThresholdsConfig_t *pParam)
{
    apConn_t * pAPConnection = (apConn_t *) hAPConnection;

    pParam->lowRssiThreshold = pAPConnection->rssiThreshold;
    pParam->lowSnrThreshold = pAPConnection->snrThreshold;
    pParam->txRateThreshold = pAPConnection->lowRateThreshold;
    pParam->dataRetryThreshold = pAPConnection->txFailureThreshold;

    currBSS_getRoamingParams(pAPConnection->hCurrBSS, 
                             &pParam->numExpectedTbttForBSSLoss, 
                             &pParam->lowQualityForBackgroungScanCondition, 
                             &pParam->normalQualityForBackgroungScanCondition,
                             &pParam->rssiFilterWeight,
                             &pParam->snrFilterWeight);

    return OK;
}


/**
*
* apConn_setRoamThresholds
*
* \b Description: 
*
* Used to configure roaming thresholds
*
* \b ARGS:
*
*  I   - pParam - pointer to type and details of configuration request\n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_setRoamThresholds(TI_HANDLE hAPConnection, roamingMngrThresholdsConfig_t *pParam)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    /* If low quality trigger threshold is set to 0 - this is the request to ignore this trigger */
    /* Otherwise store it */
    if (pParam->lowRssiThreshold == (INT8)AP_CONNECT_TRIGGER_IGNORED) 
    {
        pAPConnection->isRssiTriggerMaskedOut = TRUE;
        pParam->lowRssiThreshold = pAPConnection->rssiThreshold;
    }
    else
    {
        pAPConnection->isRssiTriggerMaskedOut = FALSE;
        pAPConnection->rssiThreshold = pParam->lowRssiThreshold;
    }

    if (pParam->txRateThreshold == AP_CONNECT_TRIGGER_IGNORED) 
    {
        pAPConnection->islowRateTriggerMaskedOut = TRUE;
        pParam->txRateThreshold = pAPConnection->lowRateThreshold;
    }
    else
    {
        pAPConnection->islowRateTriggerMaskedOut = FALSE;
        pAPConnection->lowRateThreshold = pParam->txRateThreshold;
    }

    if (pParam->dataRetryThreshold == AP_CONNECT_TRIGGER_IGNORED) 
    {
        pAPConnection->isConsTxFailureMaskedOut = TRUE;
        pParam->dataRetryThreshold = pAPConnection->txFailureThreshold;
    }
    else
    {
        pAPConnection->isConsTxFailureMaskedOut = FALSE;
        pAPConnection->txFailureThreshold = pParam->dataRetryThreshold;
    }

    if (pParam->rssiFilterWeight == (UINT8)AP_CONNECT_TRIGGER_IGNORED) 
    {
        pParam->rssiFilterWeight = RSSI_DEFAULT_WEIGHT;
    }

    if (pParam->snrFilterWeight == (UINT8)AP_CONNECT_TRIGGER_IGNORED) 
    {
        pParam->snrFilterWeight = SNR_DEFAULT_WEIGHT;
    }

    pAPConnection->isSnrTriggerMaskedOut = FALSE;
    pAPConnection->snrThreshold = pParam->lowSnrThreshold;

    currBSS_updateRoamingTriggers(pAPConnection->hCurrBSS, pParam);

    rateAdaptation_configLowRateThrsh(pAPConnection->hRateAdapt,pParam->txRateThreshold);

    return OK;
}

/**
*
* apConn_registerRoamMngrCallb
*
* \b Description: 
*
* Used to store method of Roaming Manager in the internal database of AP Connection Module.
* If this function was never called, any roaming event would cause disconnect.
*
* \b ARGS:
*
*  I   - callbackType - type of callback \n
*  I   - callbackFunction - pointer to callback \n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_registerRoamMngrCallb(TI_HANDLE hAPConnection, 
                                       apConn_roamMngrCallb_t roamEventCallb,
                                       apConn_roamMngrCallb_t reportStatusCallb,
                                       apConn_roamMngrCallb_t returnNeighborApsCallb)
{
    apConn_t *pAPConnection;
    apConn_connStatus_t reportStatus;
    paramInfo_t param;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    pAPConnection = (apConn_t *)hAPConnection;

    pAPConnection->roamEventCallb = roamEventCallb;
    pAPConnection->reportStatusCallb = reportStatusCallb;
    if ((pAPConnection->roamingEnabled) && (pAPConnection->currentState != AP_CONNECT_STATE_IDLE))
    {
        param.paramType   = ASSOC_ASSOCIATION_RESP_PARAM;

        assoc_getParam(pAPConnection->hAssoc, &param);
        reportStatus.dataBuf = (char *)(param.content.applicationConfigBuffer.buffer);
        reportStatus.dataBufLength = param.content.applicationConfigBuffer.bufferSize;

        reportStatus.status = CONN_STATUS_CONNECTED;
        reportStatusCallb(pAPConnection->hRoamMng, &reportStatus);  
    }
    ((apConn_t *)hAPConnection)->returnNeighborApsCallb = returnNeighborApsCallb;

    return OK;
}


/**
*
* apConn_unregisterRoamMngrCallb
*
* \b Description: 
*
* Used to Erase methods of Roaming Manager from the internal database of AP Connection
* module. From the moment this function was called, any roaming event would cause disconnect.
*
* \b ARGS:
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_unregisterRoamMngrCallb(TI_HANDLE hAPConnection)
{
    apConn_t *pAPConnection;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

        pAPConnection = (apConn_t *)hAPConnection;

        pAPConnection->roamEventCallb = NULL;
        pAPConnection->reportStatusCallb = NULL;
        pAPConnection->returnNeighborApsCallb = NULL;

    if ((pAPConnection->currentState != AP_CONNECT_STATE_IDLE) && (pAPConnection->currentState != AP_CONNECT_STATE_WAIT_ROAM))
    {
        /* Roaming Manager is unregistering it's callbacks in the middle of roaming - disconnect */
        apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_STOP, pAPConnection);
    }
    return OK;
}


/**
*
* apConn_disconnect
*
* \b Description: 
*
* Roaming Manager calls this function when it fails to find candidate for roaming from
* one hand, and the connection with current AP is not possible from another.
*
* \b ARGS:
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_disconnect(TI_HANDLE hAPConnection)
{
    apConn_t *pAPConnection;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    pAPConnection = (apConn_t *)hAPConnection;

    UPDATE_SEND_DEAUTH_PACKET_FLAG(pAPConnection->roamReason);
    apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_STOP, pAPConnection);
    pAPConnection->disconnectFromRoamMngrNum++;

    return OK;
}


/**
*
* apConn_getStaCapabilities
*
* \b Description: 
*
* Roaming Manager calls this function during selection of new candidate for roaming.
* Returned are local Station capabilities (IEs) of quality and privacy, needed to
* evaluate AP sites as suitable for roaming.
*
* \b ARGS:
*
*  O   - ie_list - station capabilities \n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_getStaCapabilities(TI_HANDLE hAPConnection,
                                    apConn_staCapabilities_t *ie_list)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;
    apConn_staCapabilities_t *pList;
    paramInfo_t param;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

        pList = ie_list;

    /* Get authentication suite type */
    param.paramType = RSN_EXT_AUTHENTICATION_MODE;
    rsn_getParam(pAPConnection->hPrivacy, &param);      

    switch (param.content.rsnExtAuthneticationMode)
    {
        case RSN_EXT_AUTH_MODE_OPEN:
            pList->authMode = os802_11AuthModeOpen;
            break;
        case RSN_EXT_AUTH_MODE_SHARED_KEY:
            pList->authMode = os802_11AuthModeShared;
            break;
        case RSN_EXT_AUTH_MODE_AUTO_SWITCH:
            pList->authMode = os802_11AuthModeAutoSwitch;
            break;
        case RSN_EXT_AUTH_MODE_WPA:
            pList->authMode = os802_11AuthModeWPA;
            break;
        case RSN_EXT_AUTH_MODE_WPAPSK:
            pList->authMode = os802_11AuthModeWPAPSK;
            break;
        case RSN_EXT_AUTH_MODE_WPANONE:
            pList->authMode = os802_11AuthModeWPANone;
            break;
        case RSN_EXT_AUTH_MODE_WPA2:
            pList->authMode = os802_11AuthModeWPA2;
            break;
        case RSN_EXT_AUTH_MODE_WPA2PSK:
            pList->authMode = os802_11AuthModeWPA2PSK;
            break;
        default:
            pList->authMode = os802_11AuthModeOpen;
            break;
    }

    /* Get encryption type */
    param.paramType = RSN_ENCRYPTION_STATUS_PARAM;
    rsn_getParam(pAPConnection->hPrivacy, &param);

    switch (param.content.rsnEncryptionStatus)
    {
        case RSN_CIPHER_NONE:
            pList->encryptionType = OS_ENCRYPTION_TYPE_NONE;
            break;
        case RSN_CIPHER_WEP:
        case RSN_CIPHER_WEP104:
            pList->encryptionType = OS_ENCRYPTION_TYPE_WEP;
            break;
        case RSN_CIPHER_TKIP:
        case RSN_CIPHER_CKIP:
            pList->encryptionType = OS_ENCRYPTION_TYPE_TKIP;
            break;
        case RSN_CIPHER_AES_WRAP:
        case RSN_CIPHER_AES_CCMP:
            pList->encryptionType = OS_ENCRYPTION_TYPE_AES;
            break;
        default:
            pList->encryptionType = OS_ENCRYPTION_TYPE_NONE;
            break;
    }

    /* Get supported rates */
    param.paramType = SITE_MGR_DESIRED_SUPPORTED_RATE_SET_PARAM;
    siteMgr_getParam(pAPConnection->hSiteMgr, &param);
    os_memoryCopy(pAPConnection->hOs, (void *)param.content.siteMgrDesiredSupportedRateSet.ratesString, pList->rateMask, sizeof(OS_802_11_RATES_EX));
    
    /* Get mode: 2.4G, 5G or Dual */
    param.paramType = SITE_MGR_DESIRED_DOT11_MODE_PARAM;
    siteMgr_getParam(pAPConnection->hSiteMgr, &param);
    pList->networkType = (OS_802_11_NETWORK_TYPE)param.content.siteMgrDot11Mode;
    switch(param.content.siteMgrDot11Mode) 
    {
        case DOT11_B_MODE:
            pList->networkType = os802_11DS;
            break;
        case DOT11_A_MODE:
            pList->networkType = os802_11OFDM5;
            break;
        case DOT11_G_MODE:
            pList->networkType = os802_11OFDM24;
            break;
        case DOT11_DUAL_MODE:
            pList->networkType = os802_11Automode;
            break;
        default:
            pList->networkType = os802_11DS;
            break;
    }


    /* Get EXC status */
#ifdef EXC_MODULE_INCLUDED
    param.paramType = EXC_ENABLED;
    excMngr_getParam(pAPConnection->hExcMngr, &param);
    pList->excEnabled = (param.content.excEnabled==EXC_MODE_ENABLED)? TRUE : FALSE;
#else
    pList->excEnabled = FALSE;
#endif

    /* Get QoS type */
    param.paramType = QOS_MNGR_ACTIVE_PROTOCOL;
    qosMngr_getParams(pAPConnection->hQos, &param);
    pList->qosEnabled = (param.content.qosSiteProtocol==NONE_QOS)? FALSE : TRUE;

    pList->regDomain = REG_DOMAIN_FIXED;
    /* Get regulatory domain type */
    param.paramType = REGULATORY_DOMAIN_MANAGEMENT_CAPABILITY_ENABLED_PARAM;
    regulatoryDomain_getParam(pAPConnection->hRegulatoryDomain, &param);
    if (param.content.spectrumManagementEnabled)
    {   /* 802.11h is enabled (802.11h includes 802.11d) */
        pList->regDomain = REG_DOMAIN_80211H;
    }
    else 
    {
    param.paramType = REGULATORY_DOMAIN_ENABLED_PARAM;
    regulatoryDomain_getParam(pAPConnection->hRegulatoryDomain, &param);
        if (param.content.regulatoryDomainEnabled)
        {   /* 802.11d is enabled */
            pList->regDomain = REG_DOMAIN_80211D;
        }
    }
    return OK;
}


/**
*
* apConn_connectToAP
*
* \b Description: 
*
* Roaming Manager calls this function when it has completed the process of selection
* of new AP candidate for roaming. In addition, Roaming manager informs AP Connection
* module whether this is new AP or the current one, and whether to perform full
* roaming procedure or just to retain to current AP. 
*
* \b ARGS:
*
*  I   - requestType - Connect to new AP, current AP (retain to current AP or re-connect) \n
*  I   - newAP - Pointer to parameters list of AP candidate for roaming \n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_connectToAP(TI_HANDLE hAPConnection,
                             bssEntry_t *newAP,
                             apConn_connRequest_t *request,
                             BOOL reNegotiateTspec)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    pAPConnection->requestType = request->requestType;

    switch (request->requestType) 
    {
        case AP_CONNECT_RETAIN_CURR_AP:
            apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_RETAIN_CURRENT_AP, pAPConnection);
            break;

        case AP_CONNECT_FULL_TO_AP:
            pAPConnection->removeKeys = TRUE;
            pAPConnection->newAP = newAP;
            pAPConnection->roamingFailedHandoverNum++;
            pAPConnection->reNegotiateTSPEC = reNegotiateTspec;
            apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_START_ROAM, pAPConnection);
            break;

        case AP_CONNECT_FAST_TO_AP:
        case AP_CONNECT_RECONNECT_CURR_AP:
            pAPConnection->removeKeys = FALSE;
            pAPConnection->newAP = newAP;
            pAPConnection->roamingFailedHandoverNum++;
            pAPConnection->reNegotiateTSPEC = reNegotiateTspec;
            apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_START_ROAM, pAPConnection);
            break;

        default:
            break;
    }

    /* If there is vendor specific IE to attach to Assoc req, store it now */
    if (request->dataBufLength > 0)
    {
        pAPConnection->vsIEbuf = request->dataBuf;
        pAPConnection->vsIElength = request->dataBufLength;
    }

    return OK;
}


/**
*
* apConn_getBSSParams
*
* \b Description: 
*
* Roaming Manager calls this function in order to evaluate the quality of new
* AP candidates for roaming against the quality of current AP.
* The function returns parameters of current AP.
*
* \b ARGS:
*
*  O   - currentAP - Pointer to parameters list of current AP \n
*
* \b RETURNS:
*
*  Pointer to current BSS parameters database if successful, NULL otherwise.
*
* \sa 
*/
bssEntry_t *apConn_getBSSParams(TI_HANDLE hAPConnection)
{
#ifdef TI_DBG
    if (hAPConnection == NULL) /* Failed to allocate control block */
    {
        WLAN_OS_REPORT(("FATAL ERROR: apConn_create(): Error allocating cb - aborting\n"));
        return NULL;
    }
#endif
        
    return currBSS_getBssInfo(((apConn_t *)hAPConnection)->hCurrBSS);
}


/**
 *
 * apConn_isSiteBanned
 *
 * \b Description: 
 *
 * Roaming Manager calls this function during selection of new candidate for roaming.
 * Only APs which are not marked as banned are allowed to be selected.
 *
 * \b ARGS:
 *
 *  O   - givenAp - Pointer to BSSID to check if banned \n
 *
 * \b RETURNS:
 *
 *  TRUE if banned, FALSE otherwise.
 *
 * \sa 
 */
BOOL apConn_isSiteBanned(TI_HANDLE hAPConnection, macAddress_t * givenAp)
{
    apConn_t * pAPConnection = (apConn_t *) hAPConnection;

    return rsn_isSiteBanned(pAPConnection->hPrivacy, *givenAp);
}


/**
*
* apConn_getPreAuthAPStatus
*
* \b Description: 
*
* Roaming Manager calls this function during selection of new candidate for roaming.
* Among all APs with good quality, those who were pre-authenticated are preferred. 
*
* \b ARGS:
*
*  O   - givenAp - Pointer to BSSID to check the pre-authenticated status for \n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
BOOL apConn_getPreAuthAPStatus(TI_HANDLE hAPConnection,
                              macAddress_t *givenAp)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;
    paramInfoPartial_t   param;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    param.paramType = RSN_PRE_AUTH_STATUS;
    os_memoryCopy(pAPConnection->hOs, (void *)param.content.rsnApMac.addr, (void *)givenAp->addr, MAC_ADDR_LEN);
    rsn_getParamPartial(pAPConnection->hPrivacy, &param);

    return param.content.rsnPreAuthStatus;
}


/**
*
* apConn_preAuthenticate
*
* \b Description: 
*
* Roaming Manager calls this function periodically in order to update the list
* of pre-authenticated APs. The list is managed by WLAN driver and required
* by Roaming Manager during selection of roaming candidate process.  
*
* \b ARGS:
*
*  I   - listAPs - List of APs to pre-authenticate \n
*  I   - sizeOfList - Size of list of APs to pre-authenticate \n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_preAuthenticate(TI_HANDLE hAPConnection, bssList_t *listAPs, UINT8 listAPs_numOfEntries)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;
    bssidList4PreAuth_t apList;
    UINT32              listIndex, apListIndex;
    bssEntry_t          *pCurrentAP;
    UINT8               *pRsnIEs;

#ifdef TI_DBG
    if ((hAPConnection == NULL) || (listAPs == NULL))
    {
        WLAN_OS_REPORT(("FATAL ERROR: AP Connection context is not initiated\n"));
        return NOK;
    }
        
        WLAN_REPORT_INFORMATION(pAPConnection->hReport, SITE_MGR_MODULE_LOG, ("apConn_reserveResources \n"));
#endif

	if (listAPs_numOfEntries != 0) {
		for (listIndex=0, apListIndex=0; listIndex<listAPs->numOfEntries; listIndex++)
		{
			os_memoryCopy(pAPConnection->hOs, &(apList.bssidList[apListIndex].bssId), 
						  (void *)listAPs->BSSList[listIndex].BSSID.addr, MAC_ADDR_LEN);
	
			/* search in the buffer pointer to the beginning of the
				RSN IE according to the IE ID */
			if (!parseIeBuffer(pAPConnection->hOs, listAPs->BSSList[listIndex].pBuffer, listAPs->BSSList[listIndex].bufferLength, RSN_IE_ID, &pRsnIEs, NULL, 0))
			{
				WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
									  ("apConn_preAuthenticate, no RSN IE was found \n")); 
				WLAN_REPORT_HEX_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
									  listAPs->BSSList[listIndex].pBuffer, listAPs->BSSList[listIndex].bufferLength); 
	
				continue;
			}
	
			apList.bssidList[apListIndex].pRsnIEs = (dot11_RSN_t*)pRsnIEs;
			apList.bssidList[apListIndex].rsnIeLen = apList.bssidList[apListIndex].pRsnIEs->hdr.eleLen+2;
			apListIndex++;
		}
	}
        else
        {
           listIndex=0;
           apListIndex=0;
         }

    /* Start pre-auth after any Conn succ (including first), 
    and not only when a New BSSID was added, in order to save/refresh 
    PMKID of the current AP.*/
    {
        /* Add the current BSSID to the list */
        pCurrentAP = apConn_getBSSParams(pAPConnection);
        os_memoryCopy(pAPConnection->hOs, &(apList.bssidList[apListIndex].bssId), 
                      (void *)pCurrentAP->BSSID.addr, MAC_ADDR_LEN);
        /* search in the buffer pointer to the beginning of the
            RSN IE according to the IE ID */

        if (!parseIeBuffer(pAPConnection->hOs, pCurrentAP->pBuffer, pCurrentAP->bufferLength, RSN_IE_ID, &pRsnIEs, NULL, 0))
        {
            WLAN_REPORT_ERROR(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
                                  ("apConn_preAuthenticate, no RSN IE was found in the current BSS, BSSID=0x%x-0x%x-0x%x-0x%x-0x%x-0x%x \n",
                                   pCurrentAP->BSSID.addr[0], pCurrentAP->BSSID.addr[1], pCurrentAP->BSSID.addr[2], 
                                   pCurrentAP->BSSID.addr[3], pCurrentAP->BSSID.addr[4], pCurrentAP->BSSID.addr[5])); 
            HexDumpData(pCurrentAP->pBuffer, pCurrentAP->bufferLength);
            apList.bssidList[apListIndex].pRsnIEs = NULL;
            apList.bssidList[apListIndex].rsnIeLen = 0;
        }
        else
        {
            apList.bssidList[apListIndex].pRsnIEs = (dot11_RSN_t*)pRsnIEs;
            apList.bssidList[apListIndex].rsnIeLen = apList.bssidList[apListIndex].pRsnIEs->hdr.eleLen+2;
        }
        apList.NumOfItems = apListIndex+1;
        rsn_startPreAuth(pAPConnection->hPrivacy, &apList);
    }
    return OK;
}


/**
*
* apConn_prepareToRoaming
*
* \b Description: 
*
* Roaming Manager calls this function when roaming event is received and Roaming Manager
* wishes to start the roaming process, thus want to prevent scan and measurement in the
* system. The function will return OK if the allocation is performed, PEND if the
* allocation is started, NOK in case the allocation is not possible.
* In case of successful allocation, Roaming Manager will be informed via callback about
* the end of the allocation.   
*
* \b ARGS:
*
*  I   - reason - the reason for roaming \n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_prepareToRoaming(TI_HANDLE hAPConnection, apConn_roamingTrigger_e reason)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    pAPConnection->roamReason = reason;
    
    return apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_PREPARE_FOR_ROAMING, pAPConnection);
}

/**
*
* apConn_indicateSwitchChannelInProgress
*
* \b Description: 
*
* This function is called when switch channel process is started; it will trigger 
* AP Connection state machine from 'Wait for roaming start' to 'Switch channel in progress'
* state.
*
* \b ARGS:
*
*  I   - reason - the reason for roaming \n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_indicateSwitchChannelInProgress(TI_HANDLE hAPConnection)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_START_SWITCH_CHANNEL, pAPConnection);
    return OK;
}


/**
*
* apConn_indicateSwitchChannelFinished
*
* \b Description: 
*
* This function is called when switch channel process is finished
*
* \b ARGS:
*
*  I   - reason - the reason for roaming \n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_indicateSwitchChannelFinished(TI_HANDLE hAPConnection)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_SWITCH_CH, pAPConnection);

    return OK;
}


/**
*
* apConn_start
*
* \b Description: 
*
* Called by SME module when new connection has been successfully established (first time connection)
*
* \b ARGS:
*
*  I   - isValidBSS - if FALSE, no roaming shall be performed, disconnect upon any roaming event; 
*                   other parameters of current AP can be received from Current BSS module
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_start(TI_HANDLE hAPConnection, BOOLEAN roamingEnabled)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    pAPConnection->roamingEnabled = roamingEnabled;

    apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_START, pAPConnection);
    return OK;
}


/**
*
* apConn_stop
*
* \b Description: 
*
* Called by SME module when current connection must be taken down
* (due to driver download, connection failure or any other reason)
*
* \b ARGS:
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_stop(TI_HANDLE hAPConnection, BOOLEAN removeKeys, BOOLEAN immediateShutdown)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;

    pAPConnection->stopFromSmeNum++;
    pAPConnection->removeKeys = removeKeys;
    pAPConnection->sendDeauthPacket = !(immediateShutdown); /* If immediateShutdown is TRUE, no need to send DISASSOC frame - used during unload process */
    pAPConnection->reNegotiateTSPEC = FALSE;
    pAPConnection->voiceTspecConfigured = FALSE;
	pAPConnection->videoTspecConfigured = FALSE;

    /* Mark that the connection is stopped due to reason outside the scope of this module */
    pAPConnection->bNonRoamingDisAssocReason = TRUE;

    apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_STOP, pAPConnection);
    return OK;
}

/**
*
* apConn_reportRoamingEventDisconnect
*
* \b Description: 
*
* Called by Roaming Manager to inform of Disconnect
*   uStatusCode - status code of deauth/disassoc packet
*   bDeAuthenticate - Whether this packet is DeAuth ( if
*                     DisAssoc than FALSE)
*
* \b ARGS:
*
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_reportRoamingEventDisconnect(TI_HANDLE hAPConnection,UINT16 uStatusCode,BOOLEAN  bDeAuthenticate )
{
	apConn_t *pAPConnection = (apConn_t *)hAPConnection;
	roamingEventData_u RoamingEventData;

	RoamingEventData.APDisconnect.uStatusCode = uStatusCode ; /* status code of deauth/disassoc packet */
	RoamingEventData.APDisconnect.bDeAuthenticate = bDeAuthenticate; /* TRUE state that it is DeAuth packet */
	apConn_reportRoamingEvent(pAPConnection, ROAMING_TRIGGER_AP_DISCONNECT, &RoamingEventData);
	return( TI_OK );	
}
            

/**
*
* apConn_reportRoamingEvent
*
* \b Description: 
*
* Called when one of roaming events occur
*
* \b ARGS:
*
*  I   - roamingEventType   
*  I   - pRoamingEventData - in case of 'Tx rate' event, or AP disconnect
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_reportRoamingEvent(TI_HANDLE hAPConnection,
                                    apConn_roamingTrigger_e roamingEventType,
                                    roamingEventData_u *pRoamingEventData)
{
    apConn_t    *pAPConnection = (apConn_t *)hAPConnection; 
    paramInfo_t  param;  /* parameter for retrieving BSSID */

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
                          ("apConn_reportRoamingEvent, type=%d, cur_state=%d, roamingEnabled=%d, roamEventCallb=%p  \n", 
                           roamingEventType, pAPConnection->currentState,
                           pAPConnection->roamingEnabled, pAPConnection->roamEventCallb));

    /* 1. Check if this is Rogue AP test case */
    if (roamingEventType == ROAMING_TRIGGER_AP_DISCONNECT)
    {   
        if (pRoamingEventData != NULL)
        {   /* Save the disconnect reason for future use */
            pAPConnection->APDisconnect.uStatusCode     = pRoamingEventData->APDisconnect.uStatusCode;
            pAPConnection->APDisconnect.bDeAuthenticate = pRoamingEventData->APDisconnect.bDeAuthenticate;
        }
        if ((pAPConnection->ignoreDeauthReason0) && (pRoamingEventData!=NULL) &&
               (pAPConnection->APDisconnect.uStatusCode == 0))
        {   /* This is required for Rogue AP test,
                When Rogue AP due to invalid User name, deauth with reason 0 arrives before the Rogue AP,
                and this EXC test fails.*/
            WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
                  ("apConn_reportRoamingEvent, Ignore DeAuth with reason 0 \n"));
            return OK;
        }
        WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
              ("apConn_reportRoamingEvent, DeAuth with reason %d \n", pAPConnection->APDisconnect.uStatusCode));

        if (pAPConnection->APDisconnect.uStatusCode == STATUS_CODE_802_1X_AUTHENTICATION_FAILED)
        {
          #ifdef EXC_MODULE_INCLUDED
            TI_STATUS    status;

            /* Raise the EAP-Failure as event */
            status = excMngr_rogueApDetected (pAPConnection->hExcMngr, RSN_AUTH_STATUS_CHALLENGE_FROM_AP_FAILED);
          #endif

            
            /* Remove AP from candidate list for a specified amount of time */
            param.paramType = SITE_MGR_CURRENT_BSSID_PARAM;
            siteMgr_getParam(pAPConnection->hSiteMgr, &param);
            
            WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG,
                 ("current station is banned from the roaming candidates list for %d Ms\n",
                  RSN_AUTH_FAILURE_TIMEOUT));

            rsn_banSite(pAPConnection->hPrivacy, param.content.siteMgrDesiredBSSID, RSN_SITE_BAN_LEVEL_FULL, RSN_AUTH_FAILURE_TIMEOUT);
        }

    }

    /* 2. Check if received trigger is masked out */
    if (((pAPConnection->isConsTxFailureMaskedOut) && (roamingEventType == ROAMING_TRIGGER_MAX_TX_RETRIES)) ||
        ((pAPConnection->isRssiTriggerMaskedOut)   && (roamingEventType == ROAMING_TRIGGER_LOW_QUALITY)) ||
        ((pAPConnection->isSnrTriggerMaskedOut)    && (roamingEventType == ROAMING_TRIGGER_LOW_SNR)) ||
        ((pAPConnection->islowRateTriggerMaskedOut)&& (roamingEventType == ROAMING_TRIGGER_LOW_TX_RATE)))
    {
        WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, ("apConn_reportRoamingEvent, trigger ignored \n"));
        return OK;
    }

    /* 3.  Valid trigger received: */
    /* 3a. Update statistics */
    pAPConnection->roamingTriggerEvents[roamingEventType]++;

    /* 3b. Store the most severe trigger */
    if (pAPConnection->roamReason < roamingEventType)
    {
        pAPConnection->roamReason = roamingEventType;
    }

    /* 3c. Check if Roaming Manager is available */
    if (((!pAPConnection->roamingEnabled) || (pAPConnection->roamEventCallb == NULL) ||
          (pAPConnection->currentState == AP_CONNECT_STATE_IDLE))
        && (roamingEventType > ROAMING_TRIGGER_LOW_QUALITY)
        && (roamingEventType != ROAMING_TRIGGER_MAX_TX_RETRIES)) 
        /* 
         * The last condition was added so that MAX_TX_RETRIES would not cause a disconnect.
         * This is done because the current default value for max TX retries is good enough for 
         * roaming (i.e. it indicates a low quality AP), but would cause unnecessary disconnects
         * if used when roaming is disabled
         */
    {
        /* 'Any SSID' configured, meaning Roaming Manager is not allowed to perform roaming, 
           or Roaming Manager is not registered for roaming events;
           unless this is trigger to change parameters of background scan, disconnect the link */
        WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, ("Disconnecting link due to roaming event: ev = %d\n", roamingEventType));

        /*  Handle IBSS case TBD to remove 
            Handle also the case where A first connection is in progress, and 
            de-auth arrived. */
        if (pAPConnection->currentState == AP_CONNECT_STATE_IDLE)
        {
            apConn_reportConnStatusToSME(pAPConnection);
        }
        else
        {
            /* Infra-structure BSS case - disconnect the link */
            if (roamingEventType >= ROAMING_TRIGGER_AP_DISCONNECT)
            {
                pAPConnection->removeKeys = TRUE;
            }
            else
            {
                pAPConnection->removeKeys = FALSE;
            }
            UPDATE_SEND_DEAUTH_PACKET_FLAG(roamingEventType);
            apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_STOP, pAPConnection);
        }
        return OK;
    }

    /* 4. Check if we are in the middle of switching channel */
    if (pAPConnection->currentState == AP_CONNECT_STATE_SWITCHING_CHANNEL)
    {
        /* Trigger received in the middle of switch channel, continue without reporting Roaming Manager */
        WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, ("Roaming event during switch channel: ev = %d\n", roamingEventType));
        return OK;
    }

    /* 5. Report Roaming Manager */
    if ((pAPConnection->roamingEnabled == TRUE) && (pAPConnection->roamEventCallb != NULL))
    {
        WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, ("Roaming event raised: ev = %d\n", roamingEventType));
        if (roamingEventType == ROAMING_TRIGGER_LOW_QUALITY) 
        {
            EvHandlerSendEvent(pAPConnection->hEvHandler, IPC_EVENT_LOW_RSSI, NULL,0);
        }
        /* Report to Roaming Manager */
        pAPConnection->roamEventCallb(pAPConnection->hRoamMng, &roamingEventType);
    }

    return OK;
}


/**
*
* apConn_RoamHandoffFinished
*
* \b Description: 
*
* Called when EXC module receives response from the supplicant or recognizes 
* timeout while waiting for the response 
*
* \b ARGS:
*
* \b RETURNS:
*
* \sa 
*/
void apConn_RoamHandoffFinished(TI_HANDLE hAPConnection)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;

#ifdef TI_DBG
    if (hAPConnection == NULL) /* Failed to allocate control block */
    {
        WLAN_OS_REPORT(("FATAL ERROR: apConn_create(): Error allocating cb - aborting\n"));
        return;
    }
#endif

    apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_HAND_OVER, pAPConnection);
}


/**
*
* apConn_DisconnCompleteInd
*
* \b Description: 
*
* DISASSOCIATE Packet was sent - proceed with stopping the module
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_DisconnCompleteInd(TI_HANDLE hAPConnection, mgmtStatus_e status, UINT32 uStatusCode)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;

    AP_CONN_VALIDATE_HANDLE(hAPConnection);

    apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_OK, pAPConnection);

    return OK;
}


/**
*
* apConn_updateNeighborAPsList
*
* \b Description: 
*
* Called by EXC Manager when Priority APs are found  
*
* \b ARGS:
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
void apConn_updateNeighborAPsList(TI_HANDLE hAPConnection, neighborAPList_t *pListOfpriorityAps)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;
    
    if (pAPConnection->returnNeighborApsCallb != NULL )
    {
        pAPConnection->returnNeighborApsCallb(pAPConnection->hRoamMng, pListOfpriorityAps);
    }
}


/**
*
* apConn_getRoamingStatistics
*
* \b Description: 
*
* Called from Measurement EXC sub-module when preparing TSM report to the AP. 
*
* \b ARGS: AP Connection handle
*
* \b RETURNS:
*
*  total number of successful roams
*  delay of the latest successful roam
*
* \sa 
*/
void apConn_getRoamingStatistics(TI_HANDLE hAPConnection, UINT8 *roamingCount, UINT16 *roamingDelay)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;
    
    /* Get (and clear) total number of successful roams */
    *roamingCount = pAPConnection->roamingSuccesfulHandoverNum;
    pAPConnection->roamingSuccesfulHandoverNum = 0;

    /* Get delay of the latest roam */
    *roamingDelay = pAPConnection->lastRoamingDelay;
    pAPConnection->lastRoamingDelay = 0;
}




/**
*
* apConn_resetRoamingStatistics
*
* \b Description: 
*
* Called from Measurement EXC sub-module in order to re-start roaming statistics. 
*
* \b ARGS: AP Connection handle
*
* \b RETURNS:
*
*  total number of successful roams
*  delay of the latest successful roam
*
* \sa 
*/
void apConn_resetRoamingStatistics(TI_HANDLE hAPConnection)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;
    
    pAPConnection->resetReportedRoamingStatistics = TRUE;
    pAPConnection->roamingSuccesfulHandoverNum = 0;
    pAPConnection->lastRoamingDelay = 0;
}


/**
*
* apConn_stopRoamingStatistics
*
* \b Description: 
*
* Called from Measurement CCX sub-module in order to stop roaming statistics. 
*
* \b ARGS: AP Connection handle
*
* \b RETURNS:
*
* \sa 
*/
void apConn_stopRoamingStatistics(TI_HANDLE hAPConnection)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;
    
    pAPConnection->resetReportedRoamingStatistics = FALSE;
}


/**
*
* apConn_printStatistics
*
* \b Description: 
*
* Called by Site Manager when request to print statistics is requested from CLI  
*
* \b ARGS: AP Connection handle
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
void apConn_printStatistics(TI_HANDLE hAPConnection)
{   
    WLAN_OS_REPORT(("-------------- Roaming Statistics ---------------\n\n"));
    WLAN_OS_REPORT(("- Low TX rate = %d\n",     ((apConn_t *)hAPConnection)->roamingTriggerEvents[ROAMING_TRIGGER_LOW_TX_RATE]));
    WLAN_OS_REPORT(("- Low SNR = %d\n",         ((apConn_t *)hAPConnection)->roamingTriggerEvents[ROAMING_TRIGGER_LOW_SNR]));
    WLAN_OS_REPORT(("- Low Quality = %d\n",     ((apConn_t *)hAPConnection)->roamingTriggerEvents[ROAMING_TRIGGER_LOW_QUALITY]));
    WLAN_OS_REPORT(("- MAX TX retries = %d\n",  ((apConn_t *)hAPConnection)->roamingTriggerEvents[ROAMING_TRIGGER_MAX_TX_RETRIES]));
    WLAN_OS_REPORT(("- BSS Loss TX = %d\n",     ((apConn_t *)hAPConnection)->roamingTriggerEvents[ROAMING_TRIGGER_BSS_LOSS]));
    WLAN_OS_REPORT(("- Switch Channel = %d\n",  ((apConn_t *)hAPConnection)->roamingTriggerEvents[ROAMING_TRIGGER_SWITCH_CHANNEL]));
    WLAN_OS_REPORT(("- AP Disconnect = %d\n",   ((apConn_t *)hAPConnection)->roamingTriggerEvents[ROAMING_TRIGGER_AP_DISCONNECT]));
    WLAN_OS_REPORT(("- SEC attack = %d\n",      ((apConn_t *)hAPConnection)->roamingTriggerEvents[ROAMING_TRIGGER_SECURITY_ATTACK]));
    WLAN_OS_REPORT(("\n"));
    WLAN_OS_REPORT(("- Succesfull Total roaming = %d\n",                  ((apConn_t *)hAPConnection)->roamingSuccesfulHandoverTotalNum));
    WLAN_OS_REPORT(("- Unsuccesfull roaming = %d\n",                ((apConn_t *)hAPConnection)->roamingFailedHandoverNum));
    WLAN_OS_REPORT(("- Giving up roaming = %d\n",                   ((apConn_t *)hAPConnection)->retainCurrAPNum));
    WLAN_OS_REPORT(("- Disconnect cmd from roaming manager = %d\n", ((apConn_t *)hAPConnection)->disconnectFromRoamMngrNum));
    WLAN_OS_REPORT(("- Disconnect cmd from SME = %d\n",             ((apConn_t *)hAPConnection)->stopFromSmeNum));
    WLAN_OS_REPORT(("\n"));
}



/**
*
* apConn_getVendorSpecificIE
*
* \b Description: 
*
* Called by Association SM when request to associate is built and sent to AP;
* returns request updated with vendor specific info-element
*
* \b ARGS: 
*
*  I   - hAPConnection - AP Connection handle\n
*  O   - pRequest - pointer to request buffer\n
*  O   - len - size of returned IE\n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* \sa 
*/
TI_STATUS apConn_getVendorSpecificIE(TI_HANDLE hAPConnection, UINT8 *pRequest, UINT32 *len)
{
    apConn_t *pAPConnection = (apConn_t *)hAPConnection;

    if (pAPConnection->vsIElength > 0)
    {
        *len = pAPConnection->vsIElength;
        os_memoryCopy(pAPConnection->hOs, pRequest, pAPConnection->vsIEbuf, pAPConnection->vsIElength);
    }
    else
    {
        *len = 0;
    }
    return OK;
}


/* Internal functions implementation */


/**
*
* apConn_smEvent
*
* \b Description: 
*
* AP Connection state machine transition function
*
* \b ARGS:
*
*  I/O - currentState - current state in the state machine\n
*  I   - event - specific event for the state machine\n
*  I   - pData - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_smEvent(apConn_smStates *currState, UINT8 event, void* data)
{
    TI_STATUS   status;
    UINT8       nextState;
    apConn_t    *pAPConnection;

    pAPConnection = (apConn_t *)data;
    status = fsm_GetNextState(pAPConnection->apConnSM, (UINT8)*currState, event, &nextState);
    if (status == OK)
    {
        WLAN_REPORT_SM(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
                                  ("<%s, %s> --> %s\n\n",
                                   apConn_stateDesc[*currState],
                                   apConn_eventDesc[event],
                                   apConn_stateDesc[nextState]));

        status = fsm_Event(pAPConnection->apConnSM, (UINT8 *)currState, event, pAPConnection);
        return status;
    }
    else
    {
        WLAN_REPORT_ERROR(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, ("apConn_smEvent, fsm_GetNextState error\n"));
        return status;
    }
}


/**
*
* apConn_smNop - Do nothing
*
* \b Description: 
*
* Do nothing in the SM.
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context \n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* 
*/
static TI_STATUS apConn_smNop(void *pData)
{
        WLAN_REPORT_INFORMATION((((apConn_t *)pData)->hReport), ROAMING_MANAGER_MODULE_LOG, ("apConn_smNop\n"));
        return OK;
    }


/**
*
* apConn_smUnexpected - Unexpected event
*
* \b Description: 
*
* Unexpected event in the SM.
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context \n
*
* \b RETURNS:
*
*  OK if successful, NOK otherwise.
*
* 
*/
static TI_STATUS apConn_smUnexpected(void *pData)
{
    WLAN_REPORT_INFORMATION(((apConn_t *)pData)->hReport, ROAMING_MANAGER_MODULE_LOG, ("apConn_smUnexpected\n"));
    return NOK;
}


/**
*
* apConn_startWaitingForTriggers
*
* \b Description: 
*
* SME informs AP Connection module about successfull link establishment; start wiating for roaming triggers
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context \n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_startWaitingForTriggers(void *pData)
{
    apConn_t    *pAPConnection;
    apConn_connStatus_t reportStatus;  
    paramInfoPartial_t param;

    pAPConnection = (apConn_t *)pData;
    
    if ((pAPConnection->roamingEnabled) && (pAPConnection->reportStatusCallb != NULL))
    {
        param.paramType   = ASSOC_ASSOCIATION_RESP_PARAM;

        assoc_getParamPartial(pAPConnection->hAssoc, &param);
        reportStatus.dataBuf = (char *)(param.content.applicationConfigBuffer.buffer);
        reportStatus.dataBufLength = param.content.applicationConfigBuffer.bufferSize;

        reportStatus.status = CONN_STATUS_CONNECTED;
        pAPConnection->reportStatusCallb(pAPConnection->hRoamMng, &reportStatus);   
    }
    pAPConnection->firstAttempt2Roam = TRUE;
    pAPConnection->roamReason = ROAMING_TRIGGER_NONE;
    pAPConnection->removeKeys = TRUE;
    pAPConnection->sendDeauthPacket = TRUE;
    pAPConnection->reNegotiateTSPEC = FALSE;
    pAPConnection->voiceTspecConfigured = FALSE;
	pAPConnection->videoTspecConfigured = FALSE;
    return OK;
}


/**
*
* apConn_connectedToNewAP
*
* \b Description: 
*
* After roaming was requested, Connection SM informs AP Connection module about
* successful link re-establishment; start waiting for roaming triggers
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context \n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_connectedToNewAP(void *pData)
{
    apConn_t    *pAPConnection;
    apConn_connStatus_t reportStatus; 
    paramInfoPartial_t param;

    pAPConnection = (apConn_t *)pData;
    
    /* Configure SCR group back to connection */
    scr_setGroup (pAPConnection->hScr, SCR_GID_CONNECTED);

    /* Erase vendor specific info-element if was defined for last AP Assoc request */
    pAPConnection->vsIElength = 0;

    /* TBD Notify Curr BSS module about update of current AP database */

    if (pAPConnection->roamingFailedHandoverNum>0)
    {
        pAPConnection->roamingFailedHandoverNum--;
    }
    /* Report Roaming Manager */
    if (pAPConnection->reportStatusCallb != NULL)
    {
        param.paramType   = ASSOC_ASSOCIATION_RESP_PARAM;

        assoc_getParamPartial(pAPConnection->hAssoc, &param);
        reportStatus.dataBuf = (char *)(param.content.applicationConfigBuffer.buffer);
        reportStatus.dataBufLength = param.content.applicationConfigBuffer.bufferSize;

        reportStatus.status = CONN_STATUS_HANDOVER_SUCCESS;

        pAPConnection->reportStatusCallb(pAPConnection->hRoamMng, &reportStatus);   
    }
    pAPConnection->firstAttempt2Roam = TRUE;
    pAPConnection->roamReason = ROAMING_TRIGGER_NONE;
    pAPConnection->roamingSuccesfulHandoverTotalNum++;
    pAPConnection->removeKeys = TRUE;
    pAPConnection->sendDeauthPacket = TRUE;
    pAPConnection->reNegotiateTSPEC = FALSE;
    pAPConnection->voiceTspecConfigured = FALSE;
	pAPConnection->videoTspecConfigured = FALSE;

    
    if (!pAPConnection->resetReportedRoamingStatistics)
    {
        pAPConnection->roamingSuccesfulHandoverNum++;
        pAPConnection->lastRoamingDelay = 
            (UINT16)os_timeStampMs(pAPConnection->hOs) - pAPConnection->roamingStartedTimestamp;          
    }
    else
    {
        pAPConnection->resetReportedRoamingStatistics = FALSE;
    }

    /* Raise event of Roaming Completion */
    WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG,
                            ("EvHandlerSendEvent -ROAMING_COMPLETE\n" ));
    EvHandlerSendEvent(pAPConnection->hEvHandler, IPC_EVENT_ROAMING_COMPLETE, NULL, 0);

    return OK;
}


/**
*
* apConn_stopConnection
*
* \b Description: 
*
* Stop required before roaming was started
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_stopConnection(void *pData)
{
    apConn_t *pAPConnection;
    disConnType_e disConnType;
    pAPConnection = (apConn_t *)pData;
    
    WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
                    ("apConn_stopConnection, calls conn_stop, removeKeys=%d, sendDeauthPacket=%d \n", pAPConnection->removeKeys, pAPConnection->sendDeauthPacket));
    
    /* Erase vendor specific info-element if was defined for last AP Assoc request */
    pAPConnection->vsIElength = 0;
    
    /* In case AP connection was stopped by SME, and radioOn is false, meaning immidiate shutdown is required without disassoc frame */
    /* Otherwise, ask for normal disconnection with disassoc frame */
    disConnType = (pAPConnection->sendDeauthPacket == TRUE) ? DISCONN_TYPE_DEAUTH : DISCONN_TYPE_IMMEDIATE;
    
    /* Stop Connection state machine - always immediate TBD */
    conn_stop(pAPConnection->hConnSm, 
              disConnType,
              STATUS_UNSPECIFIED,
              FALSE,   /* pAPConnection->removeKeys - for Roaming, do not remove the keys */              
              apConn_DisconnCompleteInd,
              pAPConnection);
    
    return OK;
}


/**
*
* apConn_reportDisconnected
*
* \b Description: 
*
* Moving from 'Disconnecting' state to 'Idle':
*   RoamMgr.status("not-connected")
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_reportDisconnected(void *pData)
{
    apConn_t    *pAPConnection;
    apConn_connStatus_t reportStatus; 

    pAPConnection = (apConn_t *)pData;

    if (pAPConnection->reportStatusCallb != NULL) 
    {
        reportStatus.status = CONN_STATUS_NOT_CONNECTED;
        pAPConnection->reportStatusCallb(pAPConnection->hRoamMng, &reportStatus);   
    }
    
    pAPConnection->firstAttempt2Roam = TRUE;

    /* Notify SME */
    apConn_reportConnStatusToSME(pAPConnection);
    
    return OK;
}


/**
*
* apConn_retainAP
*
* \b Description: 
*
* Roaming Manager gives up on roaming.
* Moving from 'Wait for connection command' back to 'Wait for roam started.
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_retainAP(void *data)
{
    apConn_t    *pAPConnection;
    apConn_connStatus_t reportStatus; 
    paramInfo_t param;

    pAPConnection = (apConn_t *)data;

    /* Configure SCR group back to connection */
    scr_setGroup (pAPConnection->hScr, SCR_GID_CONNECTED);

    /* Report Roaming Manager */
    if (pAPConnection->reportStatusCallb != NULL) 
    {
        param.paramType   = ASSOC_ASSOCIATION_RESP_PARAM;

        assoc_getParam(pAPConnection->hAssoc, &param);
        reportStatus.dataBuf = (char *)(param.content.applicationConfigBuffer.buffer);
        reportStatus.dataBufLength = param.content.applicationConfigBuffer.bufferSize;

        reportStatus.status = CONN_STATUS_HANDOVER_SUCCESS;

        pAPConnection->reportStatusCallb(pAPConnection->hRoamMng, &reportStatus);   
    }
    pAPConnection->retainCurrAPNum++;

    pAPConnection->roamReason = ROAMING_TRIGGER_NONE;
    pAPConnection->removeKeys = TRUE;
    pAPConnection->sendDeauthPacket = TRUE;
    pAPConnection->reNegotiateTSPEC = FALSE;
    pAPConnection->voiceTspecConfigured = FALSE;
	pAPConnection->videoTspecConfigured = FALSE;

    return OK;
}


/**
*
* apConn_requestCCKM
*
* \b Description: 
*
* Roaming Manager requests to roaming.
* Get CCKM (prepare hand-off).
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_requestCCKM(void *data)
{
    apConn_t    *pAPConnection;
    TI_STATUS   status;

        pAPConnection = (apConn_t *)data;
    
#ifdef EXC_MODULE_INCLUDED
        /* Send request to EXC module */
    apConn_calcNewTsf(pAPConnection, (UINT8 *)&(pAPConnection->newAP->lastRxTSF), pAPConnection->newAP->lastRxHostTimestamp, pAPConnection->newAP->beaconInterval);
    status = excMngr_startCckm(pAPConnection->hExcMngr, &(pAPConnection->newAP->BSSID), (UINT8 *)&(pAPConnection->newAP->lastRxTSF));
#else
        status  = OK;
        apConn_RoamHandoffFinished(pAPConnection);
#endif
        return status;
    }


#ifdef EXC_MODULE_INCLUDED
/**
*
* calcNewTsfTimestamp - Calculates the TSF
*
* \b Description: 
*
* Calculates the TSF according to the delta of the TSF from the last Beacon/Probe Resp and the current time.
*
* \b ARGS:
*
*  I   - hRoamingMngr - pointer to the roamingMngr SM context  \n
*  I/O - tsfTimeStamp - the TSF field in the site entry of the roaming candidate AP
*  I   - newSiteOsTimeStamp - the TS field in the site entry of the roaming candidate AP
*
* \b RETURNS:
*
*  Nothing.
*
* 
*/
static void apConn_calcNewTsf(apConn_t *hAPConnection, UINT8 *tsfTimeStamp, UINT32 newSiteOsTimeStamp, UINT32 beaconInterval)
{
    apConn_t    *pAPConnection = hAPConnection;
    UINT32      osTimeStamp = os_timeStampMs(pAPConnection->hOs);
    UINT32      deltaTimeStamp; 
    UINT32      tsfLsdw,tsfMsdw, newOsTimeStamp; 
    UINT32      remainder;
    UINT8       newTsfTimestamp[TIME_STAMP_LEN];

    /* get the delta TS between the TS of the last Beacon/ProbeResp-from the site table
    and the current TS */
    deltaTimeStamp = osTimeStamp - newSiteOsTimeStamp;
    tsfLsdw = *((UINT32*)&tsfTimeStamp[0]); 
    tsfMsdw = *((UINT32*)&tsfTimeStamp[4]);
    
    WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
                            (" TSF time LSDW reversed=%x, TSF time MSDW reversed=%x\n", 
                             tsfLsdw, tsfMsdw));

    deltaTimeStamp = deltaTimeStamp*1000;/* from mSec to uSec*/
    /* Before adding, save remainder */
    remainder = tsfTimeStamp[3] + ((deltaTimeStamp & 0xff000000) >> 24);

    /* The LS DW of the TS is the TSF taken from the last Beacon/Probe Resp
        + the delta TS from the time the Beacon/Probe Resp arrive till now. */
    newOsTimeStamp = deltaTimeStamp+tsfLsdw;

    /* substracting one beacon interval */
    newOsTimeStamp -= (beaconInterval * 1024); /* im usec */

    /* save just for debug printout */
    deltaTimeStamp +=osTimeStamp; /* uMsec */
    /* update the LSB of the TSF */
    newTsfTimestamp[0] = newOsTimeStamp & 0x000000ff;
    newTsfTimestamp[1] = (newOsTimeStamp & 0x0000ff00) >> 8;
    newTsfTimestamp[2] = (newOsTimeStamp & 0x00ff0000) >> 16;
    newTsfTimestamp[3] = (newOsTimeStamp & 0xff000000) >> 24;

    /* increase the MSB in case of overflow */
    if (remainder > 0xff)
    {
        tsfMsdw++;
        
    }
    /* update the MSB of the TSF */
    newTsfTimestamp[4] = tsfMsdw & 0x000000ff;
    newTsfTimestamp[5] = (tsfMsdw & 0x0000ff00) >> 8;
    newTsfTimestamp[6] = (tsfMsdw & 0x00ff0000) >> 16;
    newTsfTimestamp[7] = (tsfMsdw & 0xff000000) >> 24;

    WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
                            (" NEW TSF time: reversedTsfTimeStamp= 0x%x, New deltaTimeStamp= 0x%x, \n remainder=0x%x, new tsfTimeStamp=%x-%x-%x-%x-%x-%x-%x-%x\n",  
                             newOsTimeStamp, deltaTimeStamp, remainder,
                             newTsfTimestamp[0], newTsfTimestamp[1], newTsfTimestamp[2], newTsfTimestamp[3], 
                             newTsfTimestamp[4], newTsfTimestamp[5], newTsfTimestamp[6], newTsfTimestamp[7]));

    os_memoryCopy(pAPConnection->hOs, tsfTimeStamp, newTsfTimestamp, TIME_STAMP_LEN);
}
#endif


/**
*
* apConn_invokeConnectionToNewAp
*
* \b Description: 
*
* Got CCKM (hand-off), start re-connection to another AP
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_invokeConnectionToNewAp(void *data)
{
    apConn_t    *pAPConnection;
    connType_e  connType;
    paramInfoPartial_t param;
    UINT8   staPrivacySupported, apPrivacySupported;
    BOOL    renegotiateTspec = FALSE;
    
    pAPConnection = (apConn_t *)data;

    pAPConnection->roamingStartedTimestamp = os_timeStampMs(pAPConnection->hOs);

    /* Check privacy compatibility */
    param.paramType = RSN_ENCRYPTION_STATUS_PARAM;
    rsn_getParamPartial(pAPConnection->hPrivacy, &param);

    staPrivacySupported = (param.content.rsnEncryptionStatus == RSN_CIPHER_NONE) ? FALSE : TRUE;
    apPrivacySupported  = ((pAPConnection->newAP->capabilities >> CAP_PRIVACY_SHIFT) & CAP_PRIVACY_MASK) ? TRUE : FALSE;

    if (staPrivacySupported != apPrivacySupported)
    {
        param.paramType = RSN_MIXED_MODE;
        rsn_getParamPartial(pAPConnection->hPrivacy, &param);

        if (apPrivacySupported ||
            (!param.content.rsnMixedMode && staPrivacySupported))
        {
            WLAN_REPORT_WARNING(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG,
                                ("%s: failed privacy comparison %d vs. %d\n", __FUNCTION__, staPrivacySupported, apPrivacySupported));
            return (apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_NOT_OK, pAPConnection));
        }
    }

    /* Update data info of desired AP; in case of first attempt to roam,
       store previous primary site info */
    if (siteMgr_overwritePrimarySite(pAPConnection->hSiteMgr, pAPConnection->newAP, pAPConnection->firstAttempt2Roam) != OK)
    {
        WLAN_REPORT_WARNING(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG,
                            ("%s: failed to ovewrite Primary Site\n", __FUNCTION__));
        return (apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_NOT_OK, pAPConnection));
    }

    /* Update re-associate parameter of MLME */
    if (pAPConnection->requestType == AP_CONNECT_FAST_TO_AP)
    {
        connType = CONN_TYPE_ROAM;
    }
    else
    {
        connType = CONN_TYPE_FIRST_CONN;
    }

#ifdef EXC_MODULE_INCLUDED
    /* Check the need in TSPEC re-negotiation */
    if ( (pAPConnection->voiceTspecConfigured || pAPConnection->videoTspecConfigured)
		 && pAPConnection->reNegotiateTSPEC )
    {
        /* If the candidate AP is at least EXCver4 AP, try to re-negotiate TSPECs */
        if (excMngr_parseExcVer(pAPConnection->hExcMngr, 
                                pAPConnection->newAP->pBuffer, 
                                pAPConnection->newAP->bufferLength) >= 4)
        {
            renegotiateTspec = TRUE;
        }
    }
#endif

    WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
                            ("%s: calls conn_start, removeKeys=%d, renegotiateTSPEC=%d\n", __FUNCTION__,
                             pAPConnection->removeKeys, renegotiateTspec));

    /* Start Connection state machine */
    return conn_start(pAPConnection->hConnSm, 
                      connType,
                      apConn_ConnCompleteInd,
                      pAPConnection,
                      pAPConnection->removeKeys,
                      renegotiateTspec);
}


/**
*
* apConn_reportConnFail
*
* \b Description: 
*
* Got 'Failed' indication from Connection state machine - inform Roaming Manager Module
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_reportConnFail(void *data)
{
    apConn_t *pAPConnection;
    apConn_connStatus_t reportStatus; 
    paramInfoPartial_t param;

    pAPConnection = (apConn_t *)data;

    pAPConnection->firstAttempt2Roam = FALSE;
    pAPConnection->resetReportedRoamingStatistics = FALSE;

    /* Erase vendor specific info-element if was defined for last AP Assoc request */
    pAPConnection->vsIElength = 0;

    /* Report to Roaming Manager */
    if (pAPConnection->reportStatusCallb != NULL)
    {
        param.paramType   = ASSOC_ASSOCIATION_RESP_PARAM;

        assoc_getParamPartial(pAPConnection->hAssoc, &param);
        reportStatus.dataBuf = (char *)(param.content.applicationConfigBuffer.buffer);
        reportStatus.dataBufLength = param.content.applicationConfigBuffer.bufferSize;

        reportStatus.status = CONN_STATUS_HANDOVER_FAILURE;

        pAPConnection->reportStatusCallb(pAPConnection->hRoamMng, &reportStatus);   
    }

    return OK;
}


/**
*
* apConn_configureSCR
*
* \b Description: 
*
* Got 'Failed' indication from Connection state machine - inform Roaming Manager Module
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_configureDriverBeforeRoaming(void *pData)
{
    apConn_t    *pAPConnection = (apConn_t*)pData;
    paramInfoPartial_t param;
    
    /* Configure SCR group of allowed clients according to 'Roaming' rule */
    scr_setGroup (pAPConnection->hScr, SCR_GID_ROAMING);
    param.paramType = QOS_MNGR_VOICE_RE_NEGOTIATE_TSPEC;
    qosMngr_getParamsPatial(pAPConnection->hQos, &param);
    pAPConnection->voiceTspecConfigured = param.content.TspecConfigure.voiceTspecConfigure;  
	pAPConnection->videoTspecConfigured = param.content.TspecConfigure.videoTspecConfigure;  
    pAPConnection->resetReportedRoamingStatistics = FALSE;
    return OK;
}


/**
*
* apConn_swChFinished
*
* \b Description: 
*
* Switch channel completed; if there were roaming Manager triggers meanwhile, 
* inform Roaming Manager Module
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_swChFinished(void *pData)
{
    apConn_t *pAPConnection = (apConn_t *)pData;

    /* Inform Current BSS module */
    currBSS_restartRssiCounting(pAPConnection->hCurrBSS);

    /* If there are unreported roaming triggers of 'No AP' type, 
       report them now to roaming manager */
    if (pAPConnection->roamReason >= ROAMING_TRIGGER_MAX_TX_RETRIES)
    {
        if ((pAPConnection->roamingEnabled == TRUE) && 
            (pAPConnection->roamEventCallb != NULL))
        {
            /* Report to Roaming Manager */
            pAPConnection->roamEventCallb(pAPConnection->hRoamMng, &pAPConnection->roamReason);
        }
    }
    else
    {
        pAPConnection->roamReason = ROAMING_TRIGGER_NONE;
    }

    return OK;
}


/**
*
* apConn_handleTspecReneg
*
* \b Description: 
*
* This function will be called when moving from CONNECTING state to 
* START_TSPEC_RENEGOTIATION state. It checks if TSPEC re-negotiation was requested 
* by roaming manager, if the TSPEC for voice was defined by user application, 
* if the re-negotiation was performed during hand-over. 
* If so, it will trigger moving to WAIT_ROAM state, otherwise it will start 
* TSPEC negotiation, staying in the REESTABLISHING_VOICE state and waiting 
* for results.
*
* \b ARGS:
*
*  I   - pData - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_handleTspecReneg (void *pData)
{
    apConn_t *pAPConnection = (apConn_t *)pData;
    paramInfo_t param;

    if (pAPConnection->voiceTspecConfigured && pAPConnection->reNegotiateTSPEC)
    {
        param.paramType = QOS_MNGR_VOICE_RE_NEGOTIATE_TSPEC;
        qosMngr_getParams(pAPConnection->hQos, &param);

        if (param.content.TspecConfigure.voiceTspecConfigure == TRUE)
        {
            /* TSPEC is already configured, move to CONNECTED */
            return apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_OK, pAPConnection);
        }
        else
        {
            param.paramType = QOS_MNGR_RESEND_TSPEC_REQUEST;
            param.content.qosRenegotiateTspecRequest.callback = (void *)apConn_qosMngrReportResultCallb;
            param.content.qosRenegotiateTspecRequest.handler = pData; 

            if (qosMngr_setParams(pAPConnection->hQos, &param) != OK)
            {
                /* Re-negotiation of TSPEC cannot be performed */
                return apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_NOT_OK, pAPConnection);
            }
            return OK;
        }
    }
    else
    {
        /* No need to re-negotiate TSPEC, move to CONNECTED */
        return apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_OK, pAPConnection);
    }
}


/**
*
* apConn_qosMngrReportResultCallb
*
* \b Description: 
*
* This function will be transferred to QoS manager upon request to start negotiation
* of the TSPEC for voice and signaling, and will be called when the voice TSPEC
* renegotiation is completed. The function will generate FINISHED_OK or 
* FINISHED_NOK events to the state machine of AP Connection, triggering change of 
* the current state.
*
* \b ARGS:
*
*  I   - hApConn - pointer to AP Connection context\n
*  I   - result - returned by Traffic admission control\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static TI_STATUS apConn_qosMngrReportResultCallb (TI_HANDLE hApConn, trafficAdmRequestStatus_e result)
{
    apConn_t *pAPConnection = (apConn_t *)hApConn;

    AP_CONN_VALIDATE_HANDLE(hApConn);

    if (result == STATUS_TRAFFIC_ADM_REQUEST_ACCEPT) 
    {
        apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_OK, pAPConnection);
    }
    else
    {
        apConn_smEvent(&(pAPConnection->currentState), AP_CONNECT_EVENT_FINISHED_NOT_OK, pAPConnection);
    }
    return OK;
}

/**
*
* apConn_reportConnStatusToSME
*
* \b Description: 
*
* Sends report to SME regarding the connection status
*
* \b ARGS:
*
*  I   - pAPConnection  - pointer to AP Connection context\n
*
* \b RETURNS:
*
*  OK on success, NOK otherwise.
*
* \sa 
*/
static void apConn_reportConnStatusToSME (apConn_t *pAPConnection)
{
    WLAN_REPORT_INFORMATION(pAPConnection->hReport, ROAMING_MANAGER_MODULE_LOG, 
        ("%s roamingTrigger = %d, APDisconnectStatusCode = %d, bNonRoamingDisAssocReason = %d\n",
        __FUNCTION__, pAPConnection->roamReason, pAPConnection->APDisconnect.uStatusCode, 
        pAPConnection->bNonRoamingDisAssocReason));

    /* Check if an outside reason caused the disconnection. */
    if (pAPConnection->bNonRoamingDisAssocReason)
    {
        pAPConnection->bNonRoamingDisAssocReason = FALSE;
        smeSm_reportConnStatus(pAPConnection->hSme, STATUS_UNSPECIFIED, 0);
    }
    /* DisAssociation happened due to roaming trigger */
    else if (pAPConnection->roamReason == ROAMING_TRIGGER_AP_DISCONNECT)
    {   /* AP disconnect is a special case of the status delivered to SME */
        mgmtStatus_e mgmtStatus = ( pAPConnection->APDisconnect.bDeAuthenticate ? STATUS_AP_DEAUTHENTICATE : STATUS_AP_DISASSOCIATE );
        smeSm_reportConnStatus(pAPConnection->hSme, mgmtStatus, pAPConnection->APDisconnect.uStatusCode);
    } 
    else    /* Finally, just send the last roaming trigger */
    {   
        smeSm_reportConnStatus(pAPConnection->hSme, STATUS_ROAMING_TRIGGER, (UINT32)pAPConnection->roamReason);
    }
}