summaryrefslogtreecommitdiff
path: root/wl1271/stad/src/AirLink_Managment/SwitchChannel.c
blob: 6831a557088b4cdd6a6ea1e16b08960a5b712794 (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
/*
 * SwitchChannel.c
 *
 * Copyright(c) 1998 - 2010 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 SwitchChannel.c
 *  \brief SwitchChannel module interface
 *
 *  \see SwitchChannelApi.h
 */

/****************************************************************************************************/
/*                                                                                                  */
/*      MODULE:     SwitchChannel.c                                                                         */
/*      PURPOSE:    SwitchChannel module interface.                                                         */
/*                  This module perform SwitchChannel (Dynamic Frequency Selection)                         */
/*                      according to AP command. The object responsibles for switching channel after*/
/*                      the requires time and quieting the channel for the required duration        */
/*                      time.                                                                       */
/****************************************************************************************************/

#define __FILE_ID__  FILE_ID_7
#include "tidef.h"
#include "report.h"
#include "osApi.h"
#include "paramOut.h"
#include "SwitchChannelApi.h"
#include "DataCtrl_Api.h"
#include "regulatoryDomainApi.h"
#include "apConn.h"
#include "siteMgrApi.h"
#include "PowerMgr_API.h"
#include "healthMonitor.h"
#include "TWDriver.h"
#include "DrvMainModules.h"

/* allocation vector */
#define SC_INIT_BIT                     (1)
#define SC_SM_INIT_BIT                  (2)

#define SC_SWITCH_CHANNEL_CMD_LEN               3
#define SC_SWITCH_CHANNEL_MODE_NOT_TX_SUS       0
#define SC_SWITCH_CHANNEL_MODE_TX_SUS           1


#define SC_CHANNEL_INVALID          TI_FALSE
#define SC_CHANNEL_VALID            TI_TRUE

/* Enumerations */

/** state machine states */
typedef enum 
{
    SC_STATE_IDLE           = 0,
    SC_STATE_WAIT_4_CMD     = 1,
    SC_STATE_WAIT_4_SCR     = 2,
    SC_STATE_SC_IN_PROG     = 3,
    SC_STATE_LAST           = 4
} switchChannel_smStates;

/** State machine events */
typedef enum 
{
    SC_EVENT_START          = 0,
    SC_EVENT_STOP           = 1,
    SC_EVENT_SC_CMD         = 2,
    SC_EVENT_SCR_RUN        = 3,
    SC_EVENT_SCR_FAIL       = 4,
    SC_EVENT_SC_CMPLT       = 5,
    SC_EVENT_FW_RESET       = 6,
    SC_EVENT_LAST           = 7
} switchChannel_smEvents;


#define SC_NUM_STATES       SC_STATE_LAST    
#define SC_NUM_EVENTS       SC_EVENT_LAST   


/* Structures */
typedef struct 
{

    /* SwitchChannel parameters that can be configured externally */
    TI_BOOL            dot11SpectrumManagementRequired;

    /* Internal SwitchChannel parameters */
    TI_UINT8                   currentState;
    dot11_CHANNEL_SWITCH_t  curChannelSwitchCmdParams;
    TI_UINT32                   SCRRequestTimestamp;
    TI_UINT8                    currentChannel;
    TI_BOOL                 switchChannelStarted;

#ifdef TI_DBG
    /* switchChannelCmd for debug */
    dot11_CHANNEL_SWITCH_t  debugChannelSwitchCmdParams;
    TI_UINT8                    ignoreCancelSwitchChannelCmd;
#endif
    
    /* SwitchChannel SM */
    fsm_stateMachine_t          *pSwitchChannelSm;

    /* SwitchChannel handles to other objects */                                
    TI_HANDLE       hTWD;
    TI_HANDLE       hSiteMgr;
    TI_HANDLE       hSCR;
    TI_HANDLE       hRegulatoryDomain;
    TI_HANDLE       hPowerMngr;
    TI_HANDLE       hApConn;
    TI_HANDLE       hReport;
    TI_HANDLE       hOs;

} switchChannel_t;




/* External data definitions */

/* Local functions definitions */

/* Global variables */


/********************************************************************************/
/*                      Internal functions prototypes.                          */
/********************************************************************************/


/* SM functions */
static TI_STATUS switchChannel_smStartSwitchChannelCmd(void *pData);
static TI_STATUS switchChannel_smReqSCR_UpdateCmd(void *pData);
static TI_STATUS switchChannel_smSwitchChannelCmplt(void *pData);
static TI_STATUS switchChannel_smFwResetWhileSCInProg(void *pData);
static TI_STATUS switchChannel_smScrFailWhileWait4Scr(void *pData);
static TI_STATUS switchChannel_smNop(void *pData);
static TI_STATUS switchChannel_smUnexpected(void *pData);
static TI_STATUS switchChannel_smStopWhileWait4Cmd(void *pData);
static TI_STATUS switchChannel_smStopWhileWait4Scr(void *pData);
static TI_STATUS switchChannel_smStopWhileSwitchChannelInProg(void *pData);
static TI_STATUS switchChannel_smStart(void *pData);


/* other functions */
static void release_module(switchChannel_t *pSwitchChannel, TI_UINT32 initVec);
static TI_STATUS switchChannel_smEvent(TI_UINT8 *currState, TI_UINT8 event, void* data);
static void switchChannel_zeroDatabase(switchChannel_t *pSwitchChannel);
void switchChannel_SwitchChannelCmdCompleteReturn(TI_HANDLE hSwitchChannel);
void switchChannel_scrStatusCB(TI_HANDLE hSwitchChannel, EScrClientRequestStatus requestStatus, 
                               EScrResourceId eResource, EScePendReason pendReason );
#ifdef TI_DBG
static void switchChannel_recvCmd4Debug(TI_HANDLE hSwitchChannel, dot11_CHANNEL_SWITCH_t *channelSwitch, TI_BOOL BeaconPacket, TI_UINT8 channel);
#endif


/********************************************************************************/
/*                      Interface functions Implementation.                     */
/********************************************************************************/


/************************************************************************
 *                        switchChannel_create                          *
 ************************************************************************/
/**
*
* \b Description: 
*
* This procedure is called by the config manager when the driver is created.
* It creates the SwitchChannel object.
*
* \b ARGS:
*
*  I - hOs - OS context \n
*
* \b RETURNS:
*
*  Handle to the SwitchChannel object.
*
* \sa 
*/
TI_HANDLE switchChannel_create(TI_HANDLE hOs)
{
    switchChannel_t           *pSwitchChannel = NULL;
    TI_UINT32          initVec = 0;
    TI_STATUS       status;

    /* allocating the SwitchChannel object */
    pSwitchChannel = os_memoryAlloc(hOs,sizeof(switchChannel_t));

    if (pSwitchChannel == NULL)
        return NULL;

    initVec |= (1 << SC_INIT_BIT);

    os_memoryZero(hOs, pSwitchChannel, sizeof(switchChannel_t));

    pSwitchChannel->hOs = hOs;

    status = fsm_Create(hOs, &pSwitchChannel->pSwitchChannelSm, SC_NUM_STATES, SC_NUM_EVENTS);
    if (status != TI_OK)
    {
        release_module(pSwitchChannel, initVec);
        WLAN_OS_REPORT(("FATAL ERROR: switchChannel_create(): Error Creating pSwitchChannelSm - Aborting\n"));
        return NULL;
    }
    initVec |= (1 << SC_SM_INIT_BIT);

    return(pSwitchChannel);
}

/************************************************************************
 *                        switchChannel_init                            *
 ************************************************************************/
/**
*
* \b Description: 
*
* This procedure is called by the DrvMain when the driver is initialized.
* It initializes the SwitchChannel object's variables and handlers and creates the SwitchChannel SM.
*
* \b ARGS:
*
*  I - pStadHandles  - The driver modules handles \n
*
* \b RETURNS:
*
*  void
*
* \sa 
*/
void switchChannel_init (TStadHandlesList *pStadHandles)
{
    switchChannel_t *pSwitchChannel = (switchChannel_t *)(pStadHandles->hSwitchChannel);

    /** Roaming State Machine matrix */
    fsm_actionCell_t    switchChannel_SM[SC_NUM_STATES][SC_NUM_EVENTS] =
    {
        /* next state and actions for IDLE state */
        {   {SC_STATE_WAIT_4_CMD, switchChannel_smStart},               /* START        */
            {SC_STATE_IDLE, switchChannel_smNop},                       /* STOP         */ 
            {SC_STATE_IDLE, switchChannel_smNop},                       /* SC_CMD      */
            {SC_STATE_IDLE, switchChannel_smUnexpected},                /* SCR_RUN      */ 
            {SC_STATE_IDLE, switchChannel_smUnexpected},                /* SCR_FAIL     */
            {SC_STATE_IDLE, switchChannel_smUnexpected},                /* SC_CMPLT    */
            {SC_STATE_IDLE, switchChannel_smUnexpected}                 /* FW_RESET     */
        },                                                                              

        /* next state and actions for WAIT_4_CMD state */    
        {   {SC_STATE_WAIT_4_CMD, switchChannel_smNop},                     /* START        */
            {SC_STATE_IDLE, switchChannel_smStopWhileWait4Cmd},             /* STOP         */ 
            {SC_STATE_WAIT_4_SCR, switchChannel_smReqSCR_UpdateCmd},        /* SC_CMD      */
            {SC_STATE_WAIT_4_CMD, switchChannel_smUnexpected},              /* SCR_RUN      */ 
            {SC_STATE_WAIT_4_CMD, switchChannel_smUnexpected},              /* SCR_FAIL     */
            {SC_STATE_WAIT_4_CMD, switchChannel_smUnexpected},              /* SC_CMPLT    */
            {SC_STATE_WAIT_4_CMD, switchChannel_smUnexpected}               /* FW_RESET    */

        },                                                                              

        /* next state and actions for WAIT_4_SCR state */    
        {   {SC_STATE_WAIT_4_SCR, switchChannel_smUnexpected},                  /* START        */
            {SC_STATE_IDLE, switchChannel_smStopWhileWait4Scr},                 /* STOP         */
            {SC_STATE_WAIT_4_SCR, switchChannel_smNop},                         /* SC_CMD      */
            {SC_STATE_SC_IN_PROG, switchChannel_smStartSwitchChannelCmd},       /* SCR_RUN      */
            {SC_STATE_WAIT_4_CMD, switchChannel_smScrFailWhileWait4Scr},        /* SCR_FAIL    */
            {SC_STATE_WAIT_4_SCR, switchChannel_smUnexpected} ,                 /* SC_CMPLT    */
            {SC_STATE_WAIT_4_CMD, switchChannel_smUnexpected}                   /* FW_RESET    */

        },                                                                              

        /* next state and actions for switchChannel_IN_PROG state */
        {   {SC_STATE_SC_IN_PROG, switchChannel_smUnexpected},                   /* START        */
            {SC_STATE_IDLE, switchChannel_smStopWhileSwitchChannelInProg},       /* STOP         */
            {SC_STATE_SC_IN_PROG, switchChannel_smNop},                          /* SC_CMD      */
            {SC_STATE_SC_IN_PROG, switchChannel_smUnexpected},                   /* SCR_RUN      */
            {SC_STATE_WAIT_4_CMD, switchChannel_smUnexpected},                   /* SCR_FAIL    */
            {SC_STATE_WAIT_4_CMD, switchChannel_smSwitchChannelCmplt},           /* SC_CMPLT    */
            {SC_STATE_WAIT_4_CMD, switchChannel_smFwResetWhileSCInProg}          /* FW_RESET    */
        }                                                                             
    }; 

    fsm_Config(pSwitchChannel->pSwitchChannelSm, 
            &switchChannel_SM[0][0], 
            SC_NUM_STATES, 
            SC_NUM_EVENTS, 
            switchChannel_smEvent, pSwitchChannel->hOs);

    /* init handlers */
    pSwitchChannel->hTWD                = pStadHandles->hTWD;
    pSwitchChannel->hSiteMgr            = pStadHandles->hSiteMgr;
    pSwitchChannel->hSCR                = pStadHandles->hSCR;
    pSwitchChannel->hRegulatoryDomain   = pStadHandles->hRegulatoryDomain;
    pSwitchChannel->hApConn             = pStadHandles->hAPConnection;
    pSwitchChannel->hReport             = pStadHandles->hReport;
    pSwitchChannel->hOs                 = pStadHandles->hOs;
}


TI_STATUS switchChannel_SetDefaults (TI_HANDLE hSwitchChannel, SwitchChannelInitParams_t *SwitchChannelInitParams)
{   
    switchChannel_t       *pSwitchChannel = (switchChannel_t *)hSwitchChannel;

    /* init variables */
    pSwitchChannel->dot11SpectrumManagementRequired = SwitchChannelInitParams->dot11SpectrumManagementRequired;
    pSwitchChannel->currentState = SC_STATE_IDLE;
    pSwitchChannel->currentChannel = 0;
    pSwitchChannel->switchChannelStarted = TI_FALSE;

    /* register to SCR */
    scr_registerClientCB(pSwitchChannel->hSCR, SCR_CID_SWITCH_CHANNEL, 
                         switchChannel_scrStatusCB, pSwitchChannel);

    /* register to Switch Channel Complete event in HAL */
    TWD_RegisterEvent (pSwitchChannel->hTWD, 
                       TWD_OWN_EVENT_SWITCH_CHANNEL_CMPLT, 
                       (void *)switchChannel_SwitchChannelCmdCompleteReturn, 
                       pSwitchChannel);

    TWD_EnableEvent (pSwitchChannel->hTWD, TWD_OWN_EVENT_SWITCH_CHANNEL_CMPLT);
#ifdef TI_DBG
    /* for debug */
    pSwitchChannel->debugChannelSwitchCmdParams.hdr[0] = CHANNEL_SWITCH_ANNOUNCEMENT_IE_ID;
    pSwitchChannel->debugChannelSwitchCmdParams.hdr[1] = SC_SWITCH_CHANNEL_CMD_LEN;
    pSwitchChannel->ignoreCancelSwitchChannelCmd = 0;
#endif
    TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_INIT, ".....SwitchChannel configured successfully\n");

    return TI_OK;
}


/************************************************************************
 *                        switchChannel_stop                            *
 ************************************************************************/
/**
*
* \b Description: 
*
* This procedure is called by the SME when the state is changed from CONNECTED.
* It generates a STOP event to the SwitchChannel SM.
*
* \b ARGS:
*
*  I - hSwitchChannel - SwitchChannel context \n
*
* \b RETURNS:
*
*  TI_OK on success, TI_NOK otherwise
*
* \sa 
*/
TI_STATUS switchChannel_stop(TI_HANDLE hSwitchChannel)
{
    switchChannel_t *pSwitchChannel = (switchChannel_t *)hSwitchChannel;

    pSwitchChannel->switchChannelStarted = TI_FALSE;
    return (switchChannel_smEvent((TI_UINT8*)&pSwitchChannel->currentState, SC_EVENT_STOP, pSwitchChannel));

}

/************************************************************************
 *                        switchChannel_start                           *
 ************************************************************************/
/**
*
* \b Description: 
*
* This procedure is called by the SME when the state is changed to CONNECTED.
* It generates a START event to the SwitchChannel SM.
*
* \b ARGS:
*
*  I - hSwitchChannel - SwitchChannel context \n
*
* \b RETURNS:
*
*  TI_OK on success, TI_NOK otherwise
*
* \sa 
*/
TI_STATUS switchChannel_start(TI_HANDLE hSwitchChannel)
{
    switchChannel_t *pSwitchChannel = (switchChannel_t *)hSwitchChannel;
    pSwitchChannel->switchChannelStarted = TI_TRUE;

    return (switchChannel_smEvent((TI_UINT8*)&pSwitchChannel->currentState, SC_EVENT_START, pSwitchChannel));

}


/************************************************************************
 *                        switchChannel_unload                          *
 ************************************************************************/
/**
*
* \b Description: 
*
* This procedure is called by the config manager when the driver is unloaded.
* It frees any memory allocated and timers.
*
* \b ARGS:
*
*  I - hSwitchChannel - SwitchChannel context \n
*
* \b RETURNS:
*
*  TI_OK on success, TI_NOK otherwise
*
* \sa 
*/
TI_STATUS switchChannel_unload(TI_HANDLE hSwitchChannel)
{
    TI_UINT32              initVec;
    switchChannel_t *pSwitchChannel = (switchChannel_t *)hSwitchChannel;

    if (pSwitchChannel == NULL)
        return TI_OK;

    initVec = 0xff;
    release_module(pSwitchChannel, initVec);

    return TI_OK;
}


/************************************************************************
 *                        switchChannel_recvCmd                         *
 ************************************************************************/
/*DESCRIPTION: This function is called by MLME Parser upon receiving of
                Beacon, Probe Response or Action with Switch Channel command,
                or beacon/
                performs the following:
                -   Initializes the switching channel procedure.
                -   Setting timer to the actual switching time(if needed)
                
INPUT:      hSwitchChannel      - SwitchChannel handle.
            switchMode          - indicates whether to stop transmission
                                    until the scheduled channel switch.
            newChannelNum       - indicates the number of the new channel.
            durationTime        - indicates the time (expressed in ms) until
                                    the scheduled channel switch should accure.

OUTPUT:     None

RETURN:     TI_OK on success, TI_NOK otherwise

************************************************************************/

void switchChannel_recvCmd(TI_HANDLE hSwitchChannel, dot11_CHANNEL_SWITCH_t *channelSwitch, TI_UINT8 channel)
{

    switchChannel_t             *pSwitchChannel = (switchChannel_t *)hSwitchChannel;
    paramInfo_t                 param;

    if (pSwitchChannel==NULL)
    {
        return;
    }

    param.paramType = REGULATORY_DOMAIN_DFS_CHANNELS_RANGE;
    regulatoryDomain_getParam(pSwitchChannel->hRegulatoryDomain, &param);

    if (!pSwitchChannel->dot11SpectrumManagementRequired ||
        (channel < param.content.DFS_ChannelRange.minDFS_channelNum) ||
        (channel > param.content.DFS_ChannelRange.maxDFS_channelNum))
    {   /* Do not parse Switch Channel IE, when SpectrumManagement is disabled,
            or the channel is non-DFS channel */
        return;
    }
#ifdef TI_DBG
    /* for debug purposes only */
    if (pSwitchChannel->ignoreCancelSwitchChannelCmd != 0)
    {
        return;
    }
#endif

    if (channelSwitch == NULL)
    {   /* No SC IE, update regDomain */
        param.paramType = REGULATORY_DOMAIN_UPDATE_CHANNEL_VALIDITY;
        param.content.channel = channel;
        regulatoryDomain_setParam(pSwitchChannel->hRegulatoryDomain, &param);
    } 
    else 
    {   /* SC IE exists */
        TRACE3(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_recvFrame, SwitchChannel cmd was found, channel no=%d, mode=%d, TBTT=%d \n", channelSwitch->channelNumber, channelSwitch->channelSwitchMode, channelSwitch->channelSwitchCount);

        /* Checking channel number validity */
        param.content.channel = channelSwitch->channelNumber;
        param.paramType = REGULATORY_DOMAIN_IS_CHANNEL_SUPPORTED;
        regulatoryDomain_getParam(pSwitchChannel->hRegulatoryDomain,&param);
        if ( ( !param.content.bIsChannelSupprted )   || 
            (channelSwitch->channelSwitchCount == 0) || 
            (channelSwitch->channelSwitchMode == SC_SWITCH_CHANNEL_MODE_TX_SUS))
        {   /* Trigger Roaming, if TX mode is disabled, the new channel number is invalid, 
                or the TBTT count is 0 */
            TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "report Roaming trigger\n");
            if (channelSwitch->channelSwitchMode == SC_SWITCH_CHANNEL_MODE_TX_SUS) 
            {
                param.paramType = REGULATORY_DOMAIN_SET_CHANNEL_VALIDITY;
                param.content.channelValidity.channelNum = channel;
                param.content.channelValidity.channelValidity = TI_FALSE;
                regulatoryDomain_setParam(pSwitchChannel->hRegulatoryDomain, &param);
            }
            
            if (TI_TRUE == pSwitchChannel->switchChannelStarted) 
            {
                   apConn_reportRoamingEvent(pSwitchChannel->hApConn, ROAMING_TRIGGER_SWITCH_CHANNEL, NULL); 
            }
            
        }
        else
        {   /* Invoke Switch Channel command */
            /* update the new SCC params */
            pSwitchChannel->curChannelSwitchCmdParams.channelNumber = channelSwitch->channelNumber;
            pSwitchChannel->curChannelSwitchCmdParams.channelSwitchCount = channelSwitch->channelSwitchCount;
            pSwitchChannel->curChannelSwitchCmdParams.channelSwitchMode = channelSwitch->channelSwitchMode;
            switchChannel_smEvent((TI_UINT8*)&pSwitchChannel->currentState, SC_EVENT_SC_CMD, pSwitchChannel);
        }

    }

}


/************************************************************************
 *                        switchChannel_powerSaveStatusReturn           *
 ************************************************************************/
/**
*
* \b Description: 
*
* This procedure is called when power save status is returned
*
* \b ARGS:
*
*  I/O - hSwitchChannel - SwitchChannel context \n
*
* \b RETURNS:
*
*  TI_OK on success, TI_NOK otherwise.
*
* \sa 
*/
void switchChannel_SwitchChannelCmdCompleteReturn(TI_HANDLE hSwitchChannel)
{
    switchChannel_t         *pSwitchChannel = (switchChannel_t*)hSwitchChannel;

    if (pSwitchChannel == NULL)
    {
        return;
    }
    TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_SwitchChannelCmdCompleteReturn \n");

    switchChannel_smEvent((TI_UINT8*)&pSwitchChannel->currentState, SC_EVENT_SC_CMPLT, pSwitchChannel);

}

/************************************************************************
 *                        switchChannel_enableDisableSpectrumMngmt          *
 ************************************************************************/
/**
*
* \b Description: 
*
* This procedure enables or disables the spectrum management
*
* \b ARGS:
*
*  I - hSwitchChannel - SwitchChannel context \n
*  I - enableDisable - TI_TRUE - Enable, TI_FALSE - Disable
*
* \b RETURNS:
*
*  TI_OK on success, TI_NOK otherwise.
*
* \sa 
*/
void switchChannel_enableDisableSpectrumMngmt(TI_HANDLE hSwitchChannel, TI_BOOL enableDisable)
{
    switchChannel_t         *pSwitchChannel = (switchChannel_t*)hSwitchChannel;


    if (hSwitchChannel == NULL)
    {
        return;
    }
    TRACE1(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_enableDisableSpectrumMngmt, enableDisable=%d \n", enableDisable);

    pSwitchChannel->dot11SpectrumManagementRequired = enableDisable;

    if (enableDisable)
    {   /* Enable SC, if it was started invoke start event.
            otherwise, wait for a start event */
        if (pSwitchChannel->switchChannelStarted)
        {
            switchChannel_smEvent((TI_UINT8*)&pSwitchChannel->currentState, SC_EVENT_START, pSwitchChannel);
        }
    }
    else
    {   /* Disable SC */
        switchChannel_smEvent((TI_UINT8*)&pSwitchChannel->currentState, SC_EVENT_STOP, pSwitchChannel);
    }

}



/************************************************************************
 *                        SwitchChannel internal functions              *
 ************************************************************************/

/************************************************************************
 *                        switchChannel_smEvent                         *
 ************************************************************************/
/**
*
* \b Description: 
*
* SwitchChannel 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 - Data for state machine action function\n
*
* \b RETURNS:
*
*  TI_OK on success, TI_NOK otherwise.
*
* \sa 
*/
static TI_STATUS switchChannel_smEvent(TI_UINT8 *currState, TI_UINT8 event, void* data)
{
    TI_STATUS       status;
    TI_UINT8           nextState;
    switchChannel_t           *pSwitchChannel = (switchChannel_t*)data;


    status = fsm_GetNextState(pSwitchChannel->pSwitchChannelSm, *currState, event, &nextState);
    if (status != TI_OK)
    {
        TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_ERROR, "switchChannel_smEvent, fsm_GetNextState error\n");
        return(TI_NOK);
    }

	TRACE3(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_smEvent: <currentState = %d, event = %d> --> nextState = %d\n", *currState, event, nextState);

	status = fsm_Event(pSwitchChannel->pSwitchChannelSm, currState, event, (void *)pSwitchChannel);

    if (status != TI_OK)
    {
        TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_ERROR, "switchChannel_smEvent fsm_Event error\n");
		TRACE3(pSwitchChannel->hReport, REPORT_SEVERITY_ERROR, "switchChannel_smEvent: <currentState = %d, event = %d> --> nextState = %d\n", *currState, event, nextState);
    }
    return status;

}


/************************************************************************
 *                        switchChannel_smStart                         *
 ************************************************************************/
/**
*
*
* \b Description: 
*
* This function is called when the station becomes connected.
 * update the current channel.
*
* \b ARGS:
*
*  I   - pData - pointer to the SwitchChannel SM context  \n
*
* \b RETURNS:
*
*  TI_OK if successful, TI_NOK otherwise.
*
* 
*************************************************************************/
static TI_STATUS switchChannel_smStart(void *pData)
{
    switchChannel_t                       *pSwitchChannel = (switchChannel_t*)pData;
    paramInfo_t                 param;

    if (pSwitchChannel == NULL)
    {
        return TI_NOK;
    }
    
    /* get the current channel number */
    param.paramType = SITE_MGR_CURRENT_CHANNEL_PARAM;
    siteMgr_getParam(pSwitchChannel->hSiteMgr, &param);
    pSwitchChannel->currentChannel = param.content.siteMgrCurrentChannel;
    
    TRACE1(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_smStart, channelNo=%d\n", pSwitchChannel->currentChannel);
    return TI_OK;

}

/************************************************************************
 *                        switchChannel_smReqSCR_UpdateCmd              *
 ************************************************************************/
/**
*
*
* \b Description: 
*
* Update the Switch Channel command parameters.
 * Request SCR and wait for SCR return.
 * if tx status suspend
 *  update regulatory Domain
 *  update tx
 *  start periodic timer 
 
*
* \b ARGS:
*
*  I   - pData - pointer to the SwitchChannel SM context  \n
*
* \b RETURNS:
*
*  TI_OK if successful, TI_NOK otherwise.
*
* 
*************************************************************************/
static TI_STATUS switchChannel_smReqSCR_UpdateCmd(void *pData)
{
    switchChannel_t             *pSwitchChannel = (switchChannel_t*)pData;
    EScrClientRequestStatus     scrStatus;
    EScePendReason              scrPendReason;

    if (pSwitchChannel == NULL)
    {
        return TI_NOK;
    }
    TRACE3(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_smReqSCR_UpdateCmd, channelNo=%d, TBTT = %d, Mode = %d\n", pSwitchChannel->curChannelSwitchCmdParams.channelNumber, pSwitchChannel->curChannelSwitchCmdParams.channelSwitchCount, pSwitchChannel->curChannelSwitchCmdParams.channelSwitchMode);


    /* Save the TS when requesting SCR */
    pSwitchChannel->SCRRequestTimestamp = os_timeStampMs(pSwitchChannel->hOs); 
    
    scrStatus = scr_clientRequest(pSwitchChannel->hSCR, SCR_CID_SWITCH_CHANNEL,
                                  SCR_RESOURCE_SERVING_CHANNEL, &scrPendReason);
    if ((scrStatus != SCR_CRS_RUN) && (scrStatus != SCR_CRS_PEND))
    {   
        TRACE1(pSwitchChannel->hReport, REPORT_SEVERITY_ERROR, "switchChannel_smReqSCR_UpdateCmd():Abort the switch channel, request Roaming, scrStatus=%d\n", scrStatus);
        return (switchChannel_smEvent((TI_UINT8*)&pSwitchChannel->currentState, SC_EVENT_SCR_FAIL, pSwitchChannel));

    }
    if (scrStatus == SCR_CRS_RUN)
    {
        switchChannel_scrStatusCB(pSwitchChannel, scrStatus, SCR_RESOURCE_SERVING_CHANNEL, scrPendReason);
    }
    else if ((scrPendReason==SCR_PR_OTHER_CLIENT_RUNNING) ||
         (scrPendReason==SCR_PR_DIFFERENT_GROUP_RUNNING) )
    {   /* No use to wait for the SCR, invoke FAIL */
        return (switchChannel_smEvent((TI_UINT8*)&pSwitchChannel->currentState, SC_EVENT_SCR_FAIL, pSwitchChannel));
    }
    /* wait for the SCR callback function to be called */
    return TI_OK;

}


/************************************************************************
 *                        switchChannel_scrStatusCB                     *
 ************************************************************************/
/**
*
*
* \b Description: 
*
* This function is called by the SCR when:
 * the resource is reserved for the SwitchChannel - SCR_CRS_RUN
 * recovery occurred - SCR_CRS_ABORT
 * other = ERROR
*
* \b ARGS:
*
*  I   - hSwitchChannel - pointer to the SwitchChannel SM context  \n
*  I   - requestStatus - the SCR request status  \n
*  I   - eResource - the resource for which the CB is issued  \n
*  I   - pendReason - The SCR pend status in case of pend reply  \n
*
* \b RETURNS:
*
*  None.
*
* 
*************************************************************************/
void switchChannel_scrStatusCB(TI_HANDLE hSwitchChannel, EScrClientRequestStatus requestStatus, 
                               EScrResourceId eResource, EScePendReason pendReason )
{
    switchChannel_t             *pSwitchChannel = (switchChannel_t*)hSwitchChannel;
    switchChannel_smEvents      scEvent;

    if (pSwitchChannel == NULL)
    {
        return;
    }

    switch (requestStatus)
    {
    case SCR_CRS_RUN: 
        scEvent = SC_EVENT_SCR_RUN;
        break;
    case SCR_CRS_FW_RESET:
        scEvent = SC_EVENT_FW_RESET;
        break;
    case SCR_CRS_PEND:
        scEvent = SC_EVENT_SCR_FAIL;
        break;
    case SCR_CRS_ABORT: 
    default:
        TRACE2(pSwitchChannel->hReport, REPORT_SEVERITY_ERROR, "switchChannel_scrStatusCB scrStatus = %d, pendReason=%d\n", requestStatus, pendReason);
        scEvent = SC_EVENT_SCR_FAIL;
        break;
    }

    switchChannel_smEvent((TI_UINT8*)&pSwitchChannel->currentState, scEvent, pSwitchChannel);

}



/************************************************************************
 *                        switchChannel_smStartSwitchChannelCmd         *
 ************************************************************************/
/**
*
*
* \b Description: 
*
* This function is called once SwitchChannel command was received and the SCR
 * request returned with reason RUN. 
 * In this case perform the following:
 *  Set CMD to FW


*
* \b ARGS:
*
*  I   - pData - pointer to the SwitchChannel SM context  \n
*
* \b RETURNS:
*
*  TI_OK if successful, TI_NOK otherwise.
*
* 
*************************************************************************/
static TI_STATUS switchChannel_smStartSwitchChannelCmd(void *pData)
{
    switchChannel_t         *pSwitchChannel = (switchChannel_t *)pData;
    TSwitchChannelParams    pSwitchChannelCmd;
    TI_UINT32                   switchChannelTimeDuration;
    paramInfo_t             param;

    if (pSwitchChannel == NULL)
    {
        return TI_NOK;
    }

    param.paramType = SITE_MGR_BEACON_INTERVAL_PARAM;
    siteMgr_getParam(pSwitchChannel->hSiteMgr, &param);

    switchChannelTimeDuration = pSwitchChannel->curChannelSwitchCmdParams.channelSwitchCount * param.content.beaconInterval * 1024 / 1000;
    if (  (switchChannelTimeDuration!=0) &&
        ((os_timeStampMs(pSwitchChannel->hOs) - pSwitchChannel->SCRRequestTimestamp) >= switchChannelTimeDuration ))
    {   /* There's no time to perfrom the SCC, set the Count to 1 */
        pSwitchChannel->curChannelSwitchCmdParams.channelSwitchCount = 1;
    }

    apConn_indicateSwitchChannelInProgress(pSwitchChannel->hApConn);

    pSwitchChannelCmd.channelNumber = pSwitchChannel->curChannelSwitchCmdParams.channelNumber;
    pSwitchChannelCmd.switchTime    = pSwitchChannel->curChannelSwitchCmdParams.channelSwitchCount;
    pSwitchChannelCmd.txFlag        = pSwitchChannel->curChannelSwitchCmdParams.channelSwitchMode;
    pSwitchChannelCmd.flush         = 0;
    TWD_CmdSwitchChannel (pSwitchChannel->hTWD, &pSwitchChannelCmd);
    
    TRACE4(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "TWD_CmdSwitchChannel:Set the cmd in HAL. Params:\n channelNumber=%d, switchTime=%d, txFlag=%d, flush=%d \n", pSwitchChannelCmd.channelNumber, pSwitchChannelCmd.switchTime, pSwitchChannelCmd.txFlag, pSwitchChannelCmd.flush);

    return TI_OK;

}

/************************************************************************
 *    switchChannel_smFwResetWhileSCInProg          *
 ************************************************************************/
/**
*
*
* \b Description: 
*
* This function is called when Switch Channel command is cancelled. 
 * In this case update TX nad regulatory Domain adn HAL.
 * Release the SCR and exit PS.
*
* \b ARGS:
*
*  I   - pData - pointer to the SwitchChannel SM context  \n
*
* \b RETURNS:
*
*  TI_OK if successful, TI_NOK otherwise.
*
* 
*************************************************************************/
static TI_STATUS switchChannel_smFwResetWhileSCInProg(void *pData)
{
    switchChannel_t      *pSwitchChannel = (switchChannel_t *)pData;
    paramInfo_t          param;

    if (pSwitchChannel == NULL)
    {
        return TI_NOK;
    }
    TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_smFwResetWhileSCInProg \n");

    /* Update new current channel */
    param.paramType = SITE_MGR_CURRENT_CHANNEL_PARAM;
    param.content.siteMgrCurrentChannel = pSwitchChannel->curChannelSwitchCmdParams.channelNumber;
    siteMgr_setParam(pSwitchChannel->hSiteMgr, &param);

    apConn_indicateSwitchChannelFinished(pSwitchChannel->hApConn);

    switchChannel_zeroDatabase(pSwitchChannel);

    /* release the SCR */
    scr_clientComplete(pSwitchChannel->hSCR, SCR_CID_SWITCH_CHANNEL, SCR_RESOURCE_SERVING_CHANNEL);
    
    return TI_OK;

}


/************************************************************************
 *                        switchChannel_smSwitchChannelCmplt            *
 ************************************************************************/
/**
*
*
* \b Description: 
*
* This function is called when SwitchChannel command completed in FW.
 * In this case release SCR and update current channel.
 * If TX was sus, it will be enabled only after first Beacon is recieved.
 * Exit PS.
*
* \b ARGS:
*
*  I   - pData - pointer to the SwitchChannel SM context  \n
*
* \b RETURNS:
*
*  TI_OK if successful, TI_NOK otherwise.
*
* 
*************************************************************************/
static TI_STATUS switchChannel_smSwitchChannelCmplt(void *pData)
{
    switchChannel_t             *pSwitchChannel = (switchChannel_t *)pData;
    paramInfo_t     param;
    
    if (pSwitchChannel == NULL)
    {
        return TI_NOK;
    }
    
    /* Update new current channel */
    param.paramType = SITE_MGR_CURRENT_CHANNEL_PARAM;
    param.content.siteMgrCurrentChannel = pSwitchChannel->curChannelSwitchCmdParams.channelNumber;
    siteMgr_setParam(pSwitchChannel->hSiteMgr, &param);

    TRACE1(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_smSwitchChannelCmplt, new channelNum = %d\n", pSwitchChannel->currentChannel);

    apConn_indicateSwitchChannelFinished(pSwitchChannel->hApConn);
    switchChannel_zeroDatabase(pSwitchChannel);

    /* release the SCR */
    scr_clientComplete(pSwitchChannel->hSCR, SCR_CID_SWITCH_CHANNEL, SCR_RESOURCE_SERVING_CHANNEL);

    return TI_OK;

}

 

/************************************************************************
 *                        switchChannel_smScrFailWhileWait4Scr          *
 ************************************************************************/
/**
*
*
* \b Description: 
*
* This function is called when recovery occurred, while waiting for SCR due
 * to previous switch channel command. 
 * Exit PS
 * Release SCR. 
*
* \b ARGS:
*
*  I   - pData - pointer to the SwitchChannel SM context  \n
*
* \b RETURNS:
*
*  TI_OK if successful, TI_NOK otherwise.
*
* 
*************************************************************************/
static TI_STATUS switchChannel_smScrFailWhileWait4Scr(void *pData)
{
    switchChannel_t                       *pSwitchChannel = (switchChannel_t*)pData;

    if (pSwitchChannel == NULL)
    {
        return TI_NOK;
    }

    TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_smScrFailWhileWait4Scr\n");

    switchChannel_zeroDatabase(pSwitchChannel);

    /* release the SCR is not required !!! */
    scr_clientComplete(pSwitchChannel->hSCR, SCR_CID_SWITCH_CHANNEL, SCR_RESOURCE_SERVING_CHANNEL);

    return TI_OK;
}

/************************************************************************
 *                        switchChannel_smStopWhileWait4Cmd             *
 ************************************************************************/
/**
*
*
* \b Description: 
*
* This function is called when the station becomes Disconnected and the current
* state is Wait4Cmd. In this case perfrom:
 * Stop the timer
 * Enable TX if it was disabled
 * Zero the current command parameters
 * Stop the timer
*
* \b ARGS:
*
*  I   - pData - pointer to the SwitchChannel SM context  \n
*
* \b RETURNS:
*
*  TI_OK if successful, TI_NOK otherwise.
*
* 
*************************************************************************/
static TI_STATUS switchChannel_smStopWhileWait4Cmd(void *pData)
{
    switchChannel_t                       *pSwitchChannel = (switchChannel_t*)pData;

    if (pSwitchChannel == NULL)
    {
        return TI_NOK;
    }

    TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_smStopWhileWait4Cmd\n");

    switchChannel_zeroDatabase(pSwitchChannel);
    
    return TI_OK;
}

/************************************************************************
 *                        switchChannel_smStopWhileWait4Scr                 *
 ************************************************************************/
/**
*
*
* \b Description: 
*
* This function is called when the station becomes Disconnected and the current
* state is Wait4Scr. In this case perfrom:
 * Stop the timer
 * Enable TX if it was disabled
 * Zero the current command parameters
 * Complete SCR
*
* \b ARGS:
*
*  I   - pData - pointer to the SwitchChannel SM context  \n
*
* \b RETURNS:
*
*  TI_OK if successful, TI_NOK otherwise.
*
* 
*************************************************************************/
static TI_STATUS switchChannel_smStopWhileWait4Scr(void *pData)
{
    switchChannel_t                       *pSwitchChannel = (switchChannel_t*)pData;

    if (pSwitchChannel == NULL)
    {
        return TI_NOK;
    }

    TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_smStopWhileWait4Scr\n");


    switchChannel_zeroDatabase(pSwitchChannel);
    
    /* release the SCR */
    scr_clientComplete(pSwitchChannel->hSCR, SCR_CID_SWITCH_CHANNEL, SCR_RESOURCE_SERVING_CHANNEL);

    return TI_OK;
}

/************************************************************************
 *         switchChannel_smStopWhileSwitchChannelInProg                 *
 ************************************************************************/
/**
*
*
* \b Description: 
*
* This function is called when the station becomes Disconnected and the current
* state is SwitchChannelInProg. In this case perfrom:
 * Stop the timer
 * Enable TX if it was disabled
 * Zero the current command parameters
 * resume self test
 * Complete SCR
 * Exit PS
*
* \b ARGS:
*
*  I   - pData - pointer to the SwitchChannel SM context  \n
*
* \b RETURNS:
*
*  TI_OK if successful, TI_NOK otherwise.
*
* 
*************************************************************************/
static TI_STATUS switchChannel_smStopWhileSwitchChannelInProg(void *pData)
{
    switchChannel_t                       *pSwitchChannel = (switchChannel_t*)pData;

    if (pSwitchChannel == NULL)
    {
        return TI_NOK;
    }

    TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_smStopWhileSwitchChannelInProg\n");

    /* Exit PS */
    /*PowerMgr_exitFromDriverMode(pSwitchChannel->hPowerMngr, "SwitchChannel");*/

    apConn_indicateSwitchChannelFinished(pSwitchChannel->hApConn);

    TWD_CmdSwitchChannelCancel (pSwitchChannel->hTWD, pSwitchChannel->currentChannel);
    switchChannel_zeroDatabase(pSwitchChannel);
    
    /* release the SCR */
    scr_clientComplete(pSwitchChannel->hSCR, SCR_CID_SWITCH_CHANNEL, SCR_RESOURCE_SERVING_CHANNEL);

    return TI_OK;
}




/************************************************************************
 *                        switchChannel_zeroDatabase                    *
 ************************************************************************/
/**
*
*
* \b Description: 
*
* This function is called when the SwitchChannel internal database should be zero.
 * The following parameters are zerod:
 * SwitchChannelChannelRange - the timestamps and validity state of channels
 * curChannelSwitchCmdParams - the current switch channel command parameters
*
* \b ARGS:
*
*  I   - pSwitchChannel - pointer to the SwitchChannel SM context  \n
*  I   - channelNum - channel number  \n
*  I   - timestamp - required timestamp  \n
*
* \b RETURNS:
*
*  None.
*
* 
*************************************************************************/
static void switchChannel_zeroDatabase(switchChannel_t *pSwitchChannel)
{

    TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_zeroDatabase\n");


    pSwitchChannel->curChannelSwitchCmdParams.channelNumber = 0;
    pSwitchChannel->curChannelSwitchCmdParams.channelSwitchCount = 0;
    pSwitchChannel->curChannelSwitchCmdParams.channelSwitchMode = SC_SWITCH_CHANNEL_MODE_NOT_TX_SUS;
    pSwitchChannel->currentChannel = 0;

}

/***********************************************************************
 *                        release_module                               
 ***********************************************************************/
/* 
DESCRIPTION:    Called by the destroy function or by the create function (on failure)
                Go over the vector, for each bit that is set, release the corresponding module.
                                                                                                   
INPUT:      pSwitchChannel  -   SwitchChannel pointer.
            initVec -   Vector that contains a bit set for each module thah had been initiualized

OUTPUT:     

RETURN:     TI_OK on success, TI_NOK otherwise

************************************************************************/
static void release_module(switchChannel_t *pSwitchChannel, TI_UINT32 initVec)
{
    if (pSwitchChannel == NULL)
    {
        return;
    }
    if (initVec & (1 << SC_SM_INIT_BIT))
    {
        fsm_Unload(pSwitchChannel->hOs, pSwitchChannel->pSwitchChannelSm);
    }

    if (initVec & (1 << SC_INIT_BIT))
    {
        os_memoryFree(pSwitchChannel->hOs, pSwitchChannel, sizeof(switchChannel_t));
    }

    initVec = 0;
}


/**
*
* switchChannel_smNop - Do nothing
*
* \b Description: 
*
* Do nothing in the SM.
*
* \b ARGS:
*
*  I   - pData - pointer to the SwitchChannel SM context  \n
*
* \b RETURNS:
*
*  TI_OK if successful, TI_NOK otherwise.
*
* 
*/
static TI_STATUS switchChannel_smNop(void *pData)
{
    switchChannel_t       *pSwitchChannel;

    pSwitchChannel = (switchChannel_t*)pData;
    if (pSwitchChannel == NULL)
    {
        return TI_NOK;
    }
    TRACE0(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, " switchChannel_smNop\n");

    return TI_OK;
}

/**
*
* switchChannel_smUnexpected - Unexpected event
*
* \b Description: 
*
* Unexpected event in the SM.
*
* \b ARGS:
*
*  I   - pData - pointer to the SwitchChannel SM context  \n
*
* \b RETURNS:
*
*  TI_OK if successful, TI_NOK otherwise.
*
* 
*/
static TI_STATUS switchChannel_smUnexpected(void *pData)
{
    switchChannel_t       *pSwitchChannel;

    pSwitchChannel = (switchChannel_t*)pData;
    if (pSwitchChannel == NULL)
    {
        return TI_NOK;
    }
    TRACE1(pSwitchChannel->hReport, REPORT_SEVERITY_ERROR, " switchChannel_smUnexpected, state = %d\n", pSwitchChannel->currentState);

    return TI_NOK;
}

/*******************************************************************************
***********                         Debug functions                  ***********               
*******************************************************************************/
#ifdef TI_DBG

/************************************************************************
 *                        switchChannel_recvCmd                         *
 ************************************************************************/
/*DESCRIPTION: This function is called by MLME Parser upon receiving of
                Beacon, Probe Response or Action with Switch Channel command,
                or beacon/
                performs the following:
                -   Initializes the switching channel procedure.
                -   Setting timer to the actual switching time(if needed)
                
INPUT:      hSwitchChannel              -   SwitchChannel handle.
            switchMode          - indicates whether to stop transmission
                                until the scheduled channel switch.
            newChannelNum       - indicates the number of the new channel.
            durationTime        - indicates the time (expressed in ms) until
                                the scheduled channel switch should accure.

OUTPUT:     None

RETURN:     TI_OK on success, TI_NOK otherwise

************************************************************************/

static void switchChannel_recvCmd4Debug(TI_HANDLE hSwitchChannel, dot11_CHANNEL_SWITCH_t *channelSwitch, TI_BOOL BeaconPacket, TI_UINT8 channel)
{

    switchChannel_t             *pSwitchChannel = (switchChannel_t *)hSwitchChannel;
    paramInfo_t                 param;

    if (pSwitchChannel==NULL)
    {
        return;
    }


    /* The following is for debug purposes only
        It will be operated when the Switch Channel cmd is opertated from debug CLI */
    if (pSwitchChannel->ignoreCancelSwitchChannelCmd != 0)
    {
        if (pSwitchChannel->ignoreCancelSwitchChannelCmd == 1)
        {
            /* update the new SCC params */
            pSwitchChannel->curChannelSwitchCmdParams.channelNumber = channelSwitch->channelNumber;
            pSwitchChannel->curChannelSwitchCmdParams.channelSwitchCount = channelSwitch->channelSwitchCount;
            pSwitchChannel->curChannelSwitchCmdParams.channelSwitchMode = channelSwitch->channelSwitchMode;
            
            pSwitchChannel->ignoreCancelSwitchChannelCmd = 2;
        }
        else if (channelSwitch->channelSwitchCount>0)
        {
            channelSwitch->channelSwitchCount --;
        }
        else
        {   
            pSwitchChannel->ignoreCancelSwitchChannelCmd = 0;
        }


        /* search in the buffer pointer to the beginning of the
            Switch Cahnnel Announcement IE according to the IE ID */
        
        /* SC IE exists on the serving channel */
        TRACE3(pSwitchChannel->hReport, REPORT_SEVERITY_INFORMATION, "switchChannel_recvFrame, SwitchChannel cmd was found, channel no=%d, mode=%d, TBTT=%d \n", channelSwitch->channelNumber, channelSwitch->channelSwitchMode, channelSwitch->channelSwitchCount);

        /* Checking channel number validity */
        param.content.channel = channelSwitch->channelNumber;
        param.paramType = REGULATORY_DOMAIN_IS_CHANNEL_SUPPORTED;
        regulatoryDomain_getParam(pSwitchChannel->hRegulatoryDomain,&param);
        if (( !param.content.bIsChannelSupprted ) || 
            (channelSwitch->channelSwitchCount == 0) || 
            (channelSwitch->channelSwitchMode == SC_SWITCH_CHANNEL_MODE_TX_SUS))
        {   /* Trigger Roaming, if TX mode is disabled, the new channel number is invalid, 
                or the TBTT count is 0 */
            apConn_reportRoamingEvent(pSwitchChannel->hApConn, ROAMING_TRIGGER_SWITCH_CHANNEL, NULL);
        }
        else
        {   /* Invoke Switch Channel command */
            /* update the new SCC params */
            pSwitchChannel->curChannelSwitchCmdParams.channelNumber = channelSwitch->channelNumber;
            pSwitchChannel->curChannelSwitchCmdParams.channelSwitchCount = channelSwitch->channelSwitchCount;
            pSwitchChannel->curChannelSwitchCmdParams.channelSwitchMode = channelSwitch->channelSwitchMode;
            switchChannel_smEvent((TI_UINT8*)&pSwitchChannel->currentState, SC_EVENT_SC_CMD, pSwitchChannel);
        }
    }

}

void switchChannelDebug_setCmdParams(TI_HANDLE hSwitchChannel, SC_switchChannelCmdParam_e switchChannelCmdParam, TI_UINT8 param)
{
    switchChannel_t       *pSwitchChannel = (switchChannel_t*)hSwitchChannel;

    if (pSwitchChannel == NULL)
    {
        return;
    }
    
    switch (switchChannelCmdParam)
    {
    case SC_SWITCH_CHANNEL_NUM:
        WLAN_OS_REPORT(("SwitchChannelDebug_setSwitchChannelCmdParams, channelNum=%d \n ", param));
        pSwitchChannel->debugChannelSwitchCmdParams.channelNumber = param;
        break;
    case SC_SWITCH_CHANNEL_TBTT:
        WLAN_OS_REPORT(("SwitchChannelDebug_setSwitchChannelCmdParams, channelSwitchCount=%d \n ", param));
        pSwitchChannel->debugChannelSwitchCmdParams.channelSwitchCount = param;
        break;
    case SC_SWITCH_CHANNEL_MODE:
        WLAN_OS_REPORT(("SwitchChannelDebug_setSwitchChannelCmdParams, channelSwitchMode=%d \n ", param));
        pSwitchChannel->debugChannelSwitchCmdParams.channelSwitchMode = param;
        break;
    default:
        WLAN_OS_REPORT(("ERROR: SwitchChannelDebug_setSwitchChannelCmdParams, wrong switchChannelCmdParam=%d \n ",
                        switchChannelCmdParam));
            break;
    }

}
void switchChannelDebug_SwitchChannelCmdTest(TI_HANDLE hSwitchChannel, TI_BOOL BeaconPacket)
{
    switchChannel_t       *pSwitchChannel = (switchChannel_t*)hSwitchChannel;

    if (pSwitchChannel == NULL)
    {
        return;
    }
    
    WLAN_OS_REPORT(("SwitchChannelDebug_SwitchChannelCmdTest, BeaconPacket=%d \n cmd params: channelNumber=%d, channelSwitchCount=%d, channelSwitchMode=%d \n",
                    BeaconPacket,
                    pSwitchChannel->debugChannelSwitchCmdParams.channelNumber,
                    pSwitchChannel->debugChannelSwitchCmdParams.channelSwitchCount,
                    pSwitchChannel->debugChannelSwitchCmdParams.channelSwitchMode));


    pSwitchChannel->ignoreCancelSwitchChannelCmd= 1;
    switchChannel_recvCmd4Debug(hSwitchChannel, &pSwitchChannel->debugChannelSwitchCmdParams, BeaconPacket, pSwitchChannel->currentChannel);
}

void switchChannelDebug_CancelSwitchChannelCmdTest(TI_HANDLE hSwitchChannel, TI_BOOL BeaconPacket)
{

    switchChannel_t       *pSwitchChannel = (switchChannel_t*)hSwitchChannel;

    if (pSwitchChannel == NULL)
    {
        return;
    }
    
    WLAN_OS_REPORT(("SwitchChannelDebug_SwitchChannelCmdTest, BeaconPacket=%d \n",BeaconPacket));

    pSwitchChannel->ignoreCancelSwitchChannelCmd= 0;
    switchChannel_recvCmd4Debug(hSwitchChannel, NULL, BeaconPacket, pSwitchChannel->currentChannel);
}


void switchChannelDebug_printStatus(TI_HANDLE hSwitchChannel)
{
    switchChannel_t       *pSwitchChannel = (switchChannel_t*)hSwitchChannel;

    if (pSwitchChannel == NULL)
    {
        return;
    }
    
    WLAN_OS_REPORT(("SwitchChannel debug params are: channelNumber=%d, channelSwitchCount=%d , channelSwitchMode=%d \n", 
                    pSwitchChannel->debugChannelSwitchCmdParams.channelNumber, 
                    pSwitchChannel->debugChannelSwitchCmdParams.channelSwitchCount,
                    pSwitchChannel->debugChannelSwitchCmdParams.channelSwitchMode));

    WLAN_OS_REPORT(("SwitchChannel state=%d, currentChannel=%d \n", pSwitchChannel->currentState, pSwitchChannel->currentChannel));


}

void switchChannelDebug_setChannelValidity(TI_HANDLE hSwitchChannel, TI_UINT8 channelNum, TI_BOOL validity)
{
    paramInfo_t param;

    switchChannel_t       *pSwitchChannel = (switchChannel_t*)hSwitchChannel;

    if (pSwitchChannel == NULL)
    {
        return;
    }

    param.paramType = REGULATORY_DOMAIN_SET_CHANNEL_VALIDITY;
    param.content.channelValidity.channelNum = channelNum;
    param.content.channelValidity.channelValidity = validity;
    regulatoryDomain_setParam(pSwitchChannel->hRegulatoryDomain, &param);

}

#endif  /* TI_DBG */