cm-kernel /drivers/net/wireless/tiwlan1251/common/src/Management/apConn/apConn.c

Language C Lines 2788
MD5 Hash a29c4f7cede1ef95734795242c7e3f22 Estimated Cost $37,486 (why?)
Repository git://github.com/CyanogenMod/cm-kernel.git View Raw File View Project SPDX
   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
/****************************************************************************
**+-----------------------------------------------------------------------+**
**|                                                                       |**
**| 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;
    pAPConnectio
Back to Top