1/*
2 * PowerSrvSM.c
3 *
4 * Copyright(c) 1998 - 2009 Texas Instruments. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 *  * Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *  * Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in
15 *    the documentation and/or other materials provided with the
16 *    distribution.
17 *  * Neither the name Texas Instruments nor the names of its
18 *    contributors may be used to endorse or promote products derived
19 *    from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file PowerSrvSM.c
35 *  \brief This is the PowerSrvSM module implementation.
36 *  \author Assaf Azulay
37 *  \date 19-OCT-2005
38 */
39
40/****************************************************************************
41 *                                                                          *
42 *   MODULE:  PowerSrvSM                                                    *
43 *   PURPOSE: PowerSrvSM Module implementation.                             *
44 *                                                                          *
45 ****************************************************************************/
46
47#define __FILE_ID__  FILE_ID_114
48#include "tidef.h"
49#include "osApi.h"
50#include "timer.h"
51#include "fsm.h"
52#include "report.h"
53#include "TWDriver.h"
54#include "PowerSrvSM.h"
55#include "CmdBld.h"
56
57
58/*****************************************************************************
59 **         Defines                                                         **
60 *****************************************************************************/
61
62
63/*****************************************************************************
64 **         structs                                                         **
65 *****************************************************************************/
66
67
68/*****************************************************************************
69 **         Private Function prototypes                                     **
70 *****************************************************************************/
71
72static TI_STATUS powerSrvSmSMEvent(TI_UINT8* pCurrentState,
73                                   TI_UINT8 event,
74                                   TI_HANDLE hPowerSrvSM);
75static TI_STATUS powerSrvSmDoUpdateRequest(TI_HANDLE hPowerSrvSM);
76static TI_STATUS powerSrvSmDoEnterPowerSave(TI_HANDLE hPowerSrvSM);
77static TI_STATUS powerSrvSmDoExitPowerSave(TI_HANDLE hPowerSrvSM);
78static TI_STATUS powerSrvSmDoPending(TI_HANDLE hPowerSrvSM);
79static TI_STATUS powerSrvSmDoAllready(TI_HANDLE hPowerSrvSM);
80static TI_STATUS powerSrvSMActionUnexpected(TI_HANDLE hPowerSrvSM);
81static TI_STATUS powerSrvSMSendMBXConfiguration(TI_HANDLE hPowerSrvSM, TI_BOOL PS_disableEnable);
82static void      powerSrvSMTimerExpired (TI_HANDLE hPowerSrvSM, TI_BOOL bTwdInitOccured);
83
84/***************************************************************************************
85 **         Public Function prototypes                                      **
86 ****************************************************************************************/
87
88
89/****************************************************************************************
90 *                        powerSrvSM_create                                                         *
91 ****************************************************************************************
92DESCRIPTION: Power Server SM module creation function, called by the Power Server create in creation phase
93                performs the following:
94                -   Allocate the Power Server SM handle
95                -   Creates the fsm.
96
97INPUT:          - hOs - Handle to OS
98
99
100OUTPUT:
101
102RETURN:     Handle to the Power Server SM module on success, NULL otherwise
103****************************************************************************************/
104TI_HANDLE powerSrvSM_create(TI_HANDLE hOsHandle)
105{
106    PowerSrvSM_t *pPowerSrvSM = NULL;
107    fsm_stateMachine_t *pFsm = NULL;
108    TI_STATUS status;
109
110    pPowerSrvSM = (PowerSrvSM_t*) os_memoryAlloc (hOsHandle, sizeof(PowerSrvSM_t));
111    if ( pPowerSrvSM == NULL )
112    {
113        WLAN_OS_REPORT(("%s(%d) - Memory Allocation Error!\n",__FUNCTION__,__LINE__));
114        return NULL;
115    }
116
117    os_memoryZero (hOsHandle, pPowerSrvSM, sizeof(PowerSrvSM_t));
118
119    pPowerSrvSM->hOS = hOsHandle;
120
121    /* create the generic state-machine */
122    status = fsm_Create(hOsHandle,
123                        &pFsm,
124                        (TI_UINT8)POWER_SRV_SM_STATE_NUM,
125                        (TI_UINT8)POWER_SRV_SM_EVENT_NUM);
126    if ( status != TI_OK )
127    {
128        WLAN_OS_REPORT(("%s(%d) - Error in create FSM!\n",__FUNCTION__,__LINE__));
129        powerSrvSM_destroy(pPowerSrvSM);
130        return NULL;
131    }
132
133    pPowerSrvSM->hFSM = (TI_HANDLE)pFsm;
134
135    return pPowerSrvSM;
136}
137
138
139/****************************************************************************************
140 *                        powerSrvSM_destroy                                                            *
141 ****************************************************************************************
142DESCRIPTION: Power Server SM module destroy function,
143                -   delete Power Server SM allocation
144
145
146INPUT:          - hPowerSrvSM - Handle to the Power Server  SM
147
148
149OUTPUT:
150
151RETURN:    TI_STATUS - TI_OK on success else TI_NOK.
152****************************************************************************************/
153TI_STATUS powerSrvSM_destroy(TI_HANDLE hPowerSrvSM)
154{
155    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
156
157    TI_HANDLE osHandle = pPowerSrvSM->hOS;
158
159    /* free the timer */
160    if (pPowerSrvSM->hPwrSrvSmTimer)
161    {
162        tmr_DestroyTimer (pPowerSrvSM->hPwrSrvSmTimer);
163    }
164
165    /* free the generic SM */
166    if ( pPowerSrvSM->hFSM != NULL )
167    {
168        fsm_Unload(osHandle, (fsm_stateMachine_t*)pPowerSrvSM->hFSM);
169    }
170
171    /* free the Power Save SRV object */
172    os_memoryFree(osHandle , pPowerSrvSM , sizeof(PowerSrvSM_t));
173
174    return TI_OK;
175}
176
177
178/****************************************************************************************
179*                        powerSrvSM_init                                                           *
180****************************************************************************************
181DESCRIPTION: Power Server SM module initialize function, called by the Power Server init in configure phase
182               performs the following:
183               -   init the Stet machine states.
184               -   set Active as start state.
185
186INPUT:     - hPowerSrvSM       - handle to the PowerSrvSM object.
187           - hReport           - handle to the Report object.
188           - hCmdBld           - handle to the Command Builder object.
189           - hTimer            - handle to the Timer module object.
190
191OUTPUT:
192RETURN:    TI_STATUS - TI_OK on success else TI_NOK.
193****************************************************************************************/
194TI_STATUS powerSrvSM_init (TI_HANDLE hPowerSrvSM,
195                           TI_HANDLE hReport,
196                           TI_HANDLE hCmdBld,
197                           TI_HANDLE hTimer)
198{
199    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
200
201    fsm_actionCell_t smMatrix[POWER_SRV_SM_STATE_NUM][POWER_SRV_SM_EVENT_NUM] =
202    {
203        /*
204        next state and transition action for POWER_SRV_STATE_ACTIVE state
205        */
206        {
207            /* POWER_SRV_EVENT_REQUEST_ACTIVE */
208            {POWER_SRV_STATE_ACTIVE             , powerSrvSmDoAllready},
209
210            /* POWER_SRV_EVENT_REQUEST_PS */
211            {POWER_SRV_STATE_PEND_PS                , powerSrvSmDoEnterPowerSave},
212
213            /* POWER_SRV_EVENT_SUCCESS */
214            {POWER_SRV_STATE_ACTIVE                 , powerSrvSMActionUnexpected},
215
216            /* POWER_SRV_EVENT_FAIL */
217            {POWER_SRV_STATE_ACTIVE                 , powerSrvSMActionUnexpected}
218
219        },
220
221        /*
222        next state and transition action for POWER_SRV_STATE_PEND_PS state
223        */
224        {
225            /* POWER_SRV_EVENT_REQUEST_ACTIVE */
226            {POWER_SRV_STATE_PEND_PS            , powerSrvSmDoPending},
227
228            /* POWER_SRV_EVENT_REQUEST_PS */
229            {POWER_SRV_STATE_PEND_PS        , powerSrvSmDoPending},
230
231            /* POWER_SRV_EVENT_SUCCESS */
232            {POWER_SRV_STATE_PS                 , powerSrvSmDoUpdateRequest},
233
234            /* POWER_SRV_EVENT_FAIL */
235            {POWER_SRV_STATE_ACTIVE             , powerSrvSmDoUpdateRequest}
236
237        },
238        /*
239        next state and transition action for POWER_SRV_STATE_PS state
240        */
241        {
242            /* POWER_SRV_EVENT_REQUEST_ACTIVE */
243            {POWER_SRV_STATE_PEND_ACTIVE        , powerSrvSmDoExitPowerSave},
244
245            /* POWER_SRV_EVENT_REQUEST_PS */
246            {POWER_SRV_STATE_PS                 , powerSrvSmDoAllready},
247
248            /* POWER_SRV_EVENT_SUCCESS */
249            {POWER_SRV_STATE_PS                 , powerSrvSMActionUnexpected},
250
251            /* POWER_SRV_EVENT_FAIL */
252            {POWER_SRV_STATE_PS                 , powerSrvSMActionUnexpected}
253
254        },
255        /*
256        next state and transition action for POWER_SRV_STATE_PEND_ACTIVE state
257        */
258        {
259            /* POWER_SRV_EVENT_REQUEST_ACTIVE */
260            {POWER_SRV_STATE_PEND_ACTIVE            , powerSrvSmDoPending},
261
262            /* POWER_SRV_EVENT_REQUEST_PS */
263            {POWER_SRV_STATE_PEND_ACTIVE        , powerSrvSmDoPending},
264
265            /* POWER_SRV_EVENT_SUCCESS */
266            {POWER_SRV_STATE_ACTIVE             , powerSrvSmDoUpdateRequest},
267
268            /* POWER_SRV_EVENT_FAIL */
269            {POWER_SRV_STATE_ERROR_ACTIVE       , powerSrvSmDoUpdateRequest}
270
271        },
272        /*
273        next state and transition action for POWER_SRV_STATE_ERROR_ACTIVE state
274        */
275        {
276            /* POWER_SRV_EVENT_REQUEST_ACTIVE */
277            {POWER_SRV_STATE_PEND_ACTIVE            , powerSrvSmDoExitPowerSave},
278
279            /* POWER_SRV_EVENT_REQUEST_PS */
280            {POWER_SRV_STATE_PEND_PS        , powerSrvSmDoEnterPowerSave},
281
282            /* POWER_SRV_EVENT_SUCCESS */
283            {POWER_SRV_STATE_ERROR_ACTIVE       , powerSrvSMActionUnexpected},
284
285            /* POWER_SRV_EVENT_FAIL */
286            {POWER_SRV_STATE_ERROR_ACTIVE       , powerSrvSMActionUnexpected}
287
288        },
289
290    };
291
292    pPowerSrvSM->hReport = hReport;
293    pPowerSrvSM->hCmdBld = hCmdBld;
294    pPowerSrvSM->hTimer  = hTimer;
295
296    /* create the timer */
297    pPowerSrvSM->hPwrSrvSmTimer = tmr_CreateTimer (pPowerSrvSM->hTimer);
298	if (pPowerSrvSM->hPwrSrvSmTimer == NULL)
299	{
300        TRACE0(pPowerSrvSM->hReport, REPORT_SEVERITY_ERROR, "powerSrvSM_init(): Failed to create hPwrSrvSmTimer!\n");
301		return TI_NOK;
302	}
303
304    fsm_Config(pPowerSrvSM->hFSM,
305               (fsm_Matrix_t)smMatrix,
306               POWER_SRV_SM_STATE_NUM,
307               POWER_SRV_SM_EVENT_NUM,
308               powerSrvSmSMEvent,
309               pPowerSrvSM->hOS);
310
311    /*
312    the PowerSrvSM start in active mode (POWER_SRV_STATE_ACTIVE)
313    the PowerSrvSM::currentState must be sync with the PowerSrv::desiredPowerModeProfile (POWER_MODE_ACTIVE).
314    */
315    pPowerSrvSM->currentState = POWER_SRV_STATE_ACTIVE;
316
317
318    /*
319    Null packet rate : 2,5.5 M
320    Probe Request : Not PBCC modulation, Long Preamble */
321    pPowerSrvSM->NullPktRateModulation= (DRV_RATE_MASK_1_BARKER | DRV_RATE_MASK_2_BARKER);
322
323    TRACE0(pPowerSrvSM->hReport, REPORT_SEVERITY_INIT, "PowerSrvSM Initialized\n");
324
325    return TI_OK;
326}
327
328/****************************************************************************************
329*                        powerSrvSM_config                                                         *
330****************************************************************************************
331DESCRIPTION: Power Server SM module configuration function, called by the Power Server init in configure phase
332               performs the following:
333               -   init the Stet machine states.
334               -   set Active as start state.
335
336INPUT:      - hPowerSrvSM       - handle to the PowerSrvSM object.
337           - pPowerSrvInitParams   - the Power Server initialize parameters.
338
339OUTPUT:
340RETURN:    TI_STATUS - TI_OK on success else TI_NOK.
341****************************************************************************************/
342TI_STATUS powerSrvSM_config(TI_HANDLE hPowerSrvSM,
343                            TPowerSrvInitParams *pPowerSrvInitParams)
344{
345    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
346
347    /*
348    init PowerMgmtConfigration
349    */
350    pPowerSrvSM->hangOverPeriod =   pPowerSrvInitParams->hangOverPeriod;
351    pPowerSrvSM->numNullPktRetries =    pPowerSrvInitParams->numNullPktRetries;
352
353    return TI_OK;
354}
355/****************************************************************************************
356 *                        powerSrvSM_SMApi                                                           *
357 *****************************************************************************************
358DESCRIPTION: This function triggers events from the outside of the module into the state machine.
359
360
361INPUT:      - hPowerSrvSM                   - handle to the PowerSrvSM object.
362            - theSMEvent                    - event from TWD.
363
364
365OUTPUT:
366RETURN:    TI_STATUS TI_OK / PENDING / TI_NOK
367****************************************************************************************/
368TI_STATUS powerSrvSM_SMApi(TI_HANDLE hPowerSrvSM,
369                           PowerSrvSMEvents_e theSMEvent)
370{
371    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
372    TI_STATUS status;
373
374    switch ( theSMEvent )
375    {
376    case POWER_SRV_EVENT_REQUEST_ACTIVE :
377    case POWER_SRV_EVENT_REQUEST_PS :
378    case POWER_SRV_EVENT_FAIL :
379    case POWER_SRV_EVENT_SUCCESS :
380
381        TRACE1(pPowerSrvSM->hReport, REPORT_SEVERITY_INFORMATION, "powerSrvSM_SMApi(%d) called - legal input parameter.\n",theSMEvent);
382        break;
383
384    default:
385        TRACE1(pPowerSrvSM->hReport, REPORT_SEVERITY_WARNING, "powerSrvSM_SMApi(%d) called,                            input parameter is illegal.",theSMEvent);
386        return TI_NOK;
387    }
388
389
390    status = powerSrvSmSMEvent((TI_UINT8*)&pPowerSrvSM->currentState,
391                               (TI_UINT8)theSMEvent,
392                               hPowerSrvSM);
393
394    return status;
395}
396
397
398/****************************************************************************************
399 *                        powerSrvSm_setSmRequest                                                    *
400 *****************************************************************************************
401DESCRIPTION: This function sets the current SM working request.
402
403INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
404            -powerSrvRequest_t*                 - pointer to the correct request in the Power server.
405
406OUTPUT:
407RETURN:    TI_STATUS -  TI_OK
408****************************************************************************************/
409TI_STATUS powerSrvSm_setSmRequest(TI_HANDLE hPowerSrvSM,powerSrvRequest_t* pSmRequest)
410{
411    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
412    pPowerSrvSM->pSmRequest = pSmRequest;
413    return TI_OK;
414}
415
416
417/****************************************************************************************
418 *                        powerSrvSM_getCurrentState                                                         *
419 *****************************************************************************************
420DESCRIPTION: This function returns the current state of the SM.
421
422
423INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
424
425
426OUTPUT:
427RETURN:    PowerSrvSMStates_e current state
428****************************************************************************************/
429PowerSrvSMStates_e powerSrvSM_getCurrentState(TI_HANDLE hPowerSrvSM)
430{
431    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
432
433    return pPowerSrvSM->currentState;
434}
435
436/****************************************************************************************
437 *                        powerSrvSM_setRateModulation                                               *
438 *****************************************************************************************
439DESCRIPTION: This function sets the Rate Modulation
440
441
442INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
443            - rateModulation                        - desired rate
444
445OUTPUT:
446RETURN:      void
447****************************************************************************************/
448
449void powerSrvSM_setRateModulation(TI_HANDLE hPowerSrvSM, TI_UINT16 rateModulation)
450{
451    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
452    pPowerSrvSM->NullPktRateModulation= rateModulation;
453}
454
455/****************************************************************************************
456 *                        powerSrvSM_getRateModulation                                               *
457 *****************************************************************************************
458DESCRIPTION: This function sets the Rate Modulation
459
460
461INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
462
463OUTPUT:
464RETURN:      -  desired rate
465****************************************************************************************/
466
467TI_UINT32 powerSrvSM_getRateModulation(TI_HANDLE hPowerSrvSM)
468{
469    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
470    return pPowerSrvSM->NullPktRateModulation;
471}
472
473/****************************************************************************************
474 *                        powerSrvSM_printObject                                                         *
475 *****************************************************************************************
476DESCRIPTION: This function prints the SM object
477
478
479INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
480
481
482OUTPUT:
483RETURN:   void
484****************************************************************************************/
485void powerSrvSM_printObject(TI_HANDLE hPowerSrvSM)
486{
487    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
488    char *pString;
489
490    WLAN_OS_REPORT(("\n+++++ powerSrvSM_printObject +++++\n"));
491    WLAN_OS_REPORT(("Handle to the CmdBld is 0x%08X\n", pPowerSrvSM->hCmdBld));
492    WLAN_OS_REPORT(("Handle to the OS is 0x%08X\n", pPowerSrvSM->hOS));
493    WLAN_OS_REPORT(("Handle to the Report is 0x%08X\n", pPowerSrvSM->hReport));
494    WLAN_OS_REPORT(("Handle to the FSM is 0x%08X\n", pPowerSrvSM->hFSM));
495
496    switch ( pPowerSrvSM->currentState )
497    {
498    case POWER_SRV_STATE_ACTIVE:
499        pString = "POWER_SRV_STATE_ACTIVE";
500        break;
501
502    case POWER_SRV_STATE_PEND_PS:
503        pString = "POWER_SRV_STATE_PEND_PS";
504        break;
505
506    case POWER_SRV_STATE_PS:
507        pString = "POWER_SRV_STATE_PS";
508        break;
509
510    case POWER_SRV_STATE_PEND_ACTIVE:
511        pString = "POWER_SRV_STATE_PEND_ACTIVE";
512        break;
513
514    case POWER_SRV_STATE_ERROR_ACTIVE:
515        pString = "POWER_SRV_STATE_ERROR_ACTIVE";
516        break;
517
518
519    default:
520        pString = "UNKWON PARAMETER";
521        break;
522    }
523    WLAN_OS_REPORT(("The current state of the state machine is %s (=%d)\n",
524                    pString,
525                    pPowerSrvSM->currentState));
526
527}
528
529
530
531
532/*****************************************************************************
533 **         Private Function prototypes                                                             **
534 *****************************************************************************/
535
536
537
538
539
540
541/****************************************************************************************
542 *                        powerSrvSmDoEnterPowerSave                                                 *
543 *****************************************************************************************
544DESCRIPTION: This function is an action of the state machine to move from active state to PS
545
546INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
547
548OUTPUT:
549RETURN:    TI_STATUS - TI_OK / TI_NOK
550****************************************************************************************/
551
552static TI_STATUS powerSrvSmDoEnterPowerSave(TI_HANDLE hPowerSrvSM)
553{
554    TI_STATUS status;
555    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
556    pPowerSrvSM->pSmRequest->requestState = RUNNING_REQUEST;
557    status = powerSrvSMSendMBXConfiguration(hPowerSrvSM, TI_TRUE);
558    return status;
559}
560
561
562/****************************************************************************************
563 *                        powerSrvSmDoExitPowerSave                                              *
564 *****************************************************************************************
565DESCRIPTION: This function is an action of the state machine to move from PS state to Active
566
567INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
568
569OUTPUT:
570RETURN:    TI_STATUS - TI_OK / TI_NOK
571****************************************************************************************/
572static TI_STATUS powerSrvSmDoExitPowerSave(TI_HANDLE hPowerSrvSM)
573{
574    TI_STATUS status;
575    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
576    pPowerSrvSM->pSmRequest->requestState = RUNNING_REQUEST;
577    status = powerSrvSMSendMBXConfiguration(hPowerSrvSM, TI_FALSE);
578    return status;
579}
580
581
582/****************************************************************************************
583 *                        powerSrvSmDoUpdateRequest                                                  *
584 *****************************************************************************************
585DESCRIPTION: This function is an action of the state machine to update a request when the SM
586              is already in the requested state is already
587
588INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
589
590OUTPUT:
591RETURN:    TI_STATUS - TI_OK / TI_NOK
592****************************************************************************************/
593
594static TI_STATUS powerSrvSmDoUpdateRequest(TI_HANDLE hPowerSrvSM)
595{
596    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
597
598    /* request has completed - stop the guard timer */
599    tmr_StopTimer (pPowerSrvSM->hPwrSrvSmTimer);
600
601    /*powerSrv_SetRequestState  will update the correct request (acording to the current active request)*/
602    if ( pPowerSrvSM->pSmRequest->requestState == RUNNING_REQUEST )
603    {
604        pPowerSrvSM->pSmRequest->requestState = HANDLED_REQUEST;
605    }
606
607    return TI_OK;
608}
609
610
611/****************************************************************************************
612 *                        powerSrvSmDoPending                                                        *
613 *****************************************************************************************
614DESCRIPTION: This function is an action of the state machine returns Pending in case that there is a request
615              waiting to be finished (already sent to FW)
616
617INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
618
619OUTPUT:
620RETURN:    TI_STATUS - PENDING
621****************************************************************************************/
622
623static TI_STATUS powerSrvSmDoPending(TI_HANDLE hPowerSrvSM)
624{
625    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
626
627    /*powerSrv_SetRequestState will check the mode and will update the correct request (Driver of user)*/
628    pPowerSrvSM->pSmRequest->requestState = PENDING_REQUEST;
629    return POWER_SAVE_802_11_PENDING;
630}
631
632
633
634/****************************************************************************************
635 *                        powerSrvSmDoAllready                                                       *
636 *****************************************************************************************
637DESCRIPTION: This function is an action of the state machine stays in the same state since it the requested
638              one in the request
639
640INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
641
642OUTPUT:
643RETURN:    TI_STATUS - TI_OK
644****************************************************************************************/
645static TI_STATUS powerSrvSmDoAllready(TI_HANDLE hPowerSrvSM)
646{
647    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
648
649    /*powerSrv_SetRequestState will check the mode and will update the correct request (Driver of user)*/
650    pPowerSrvSM->pSmRequest->requestState = HANDLED_REQUEST;
651    return POWER_SAVE_802_11_IS_CURRENT;
652}
653
654
655/****************************************************************************************
656 *                        powerSrvSMActionUnexpected                                                 *
657 *****************************************************************************************
658DESCRIPTION: This function is an action of the state machine stays in the same state and return that action
659              was not expected
660
661INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
662
663OUTPUT:
664RETURN:    TI_STATUS - TI_OK
665****************************************************************************************/
666static TI_STATUS powerSrvSMActionUnexpected(TI_HANDLE hPowerSrvSM)
667{
668#ifdef TI_DBG
669    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
670
671    TRACE0(pPowerSrvSM->hReport, REPORT_SEVERITY_ERROR, "called: powerSrvSMActionUnexpected");
672#endif /* TI_DBG */
673
674    return TI_OK;
675}
676
677
678/****************************************************************************************
679 *                        powerSrvSmSMEvent                                                      *
680 *****************************************************************************************
681DESCRIPTION: This function is the manager of the state macine. its move the state machine
682              from one state to the other depend on the receive event, and call to the appropriate
683              action (function) for the move between the states.
684
685INPUT:      - pCurrentState
686            - event
687            - hPowerSrvSM                       - handle to the PowerSrvSM object.
688
689OUTPUT:
690RETURN:    TI_STATUS
691****************************************************************************************/
692static TI_STATUS powerSrvSmSMEvent(TI_UINT8* pCurrentState,
693                                   TI_UINT8 event,
694                                   TI_HANDLE hPowerSrvSM)
695{
696    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
697    TI_STATUS status = TI_OK;
698    TI_UINT8 nextState;
699
700    status = fsm_GetNextState((fsm_stateMachine_t*)pPowerSrvSM->hFSM,
701                              (TI_UINT8)pPowerSrvSM->currentState,
702                              event,
703                              &nextState);
704    if ( status != TI_OK )
705    {
706        TRACE0(pPowerSrvSM->hReport, REPORT_SEVERITY_SM, "PowerSrvSM - State machine error, failed getting next state\n");
707        return(status);
708    }
709
710
711	TRACE3( pPowerSrvSM->hReport, REPORT_SEVERITY_INFORMATION, "powerSrvSmSMEvent: <currentState = %d, event = %d> --> nextState = %d\n", *pCurrentState, event, nextState);
712
713    status = fsm_Event(pPowerSrvSM->hFSM,
714                       pCurrentState,
715                       event,
716                       (void*)pPowerSrvSM);
717
718    return status;
719}
720
721
722/****************************************************************************************
723*                        powerSrvSMSendMBXConfiguration                                             *
724*****************************************************************************************
725DESCRIPTION: This function send configuration of the power save option that holds in the command
726                mailbox inner sturcture.
727
728INPUT:      - hPowerSrvSM                       - handle to the PowerSrvSM object.
729           - PS_disableEnable                      - true = PS , false = active
730
731OUTPUT:
732RETURN:    TI_STATUS
733****************************************************************************************/
734static TI_STATUS    powerSrvSMSendMBXConfiguration(TI_HANDLE hPowerSrvSM, TI_BOOL PS_disableEnable)
735{
736    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
737    TPowerSaveParams powerSaveParams;
738    TI_STATUS status;
739
740    /*setting the params for the Hal*/
741    powerSaveParams.hangOverPeriod          = pPowerSrvSM->hangOverPeriod;
742    powerSaveParams.numNullPktRetries       = pPowerSrvSM->numNullPktRetries;
743    powerSaveParams.NullPktRateModulation   = pPowerSrvSM->NullPktRateModulation;
744    powerSaveParams.needToSendNullData      = pPowerSrvSM->pSmRequest->sendNullDataOnExit;
745    powerSaveParams.ps802_11Enable          = PS_disableEnable;
746
747    /* start the FW guard timer, which is used to protect from FW stuck */
748    tmr_StartTimer (pPowerSrvSM->hPwrSrvSmTimer,
749                    powerSrvSMTimerExpired,
750                    (TI_HANDLE)pPowerSrvSM,
751                    POWER_SAVE_GUARD_TIME_MS,
752                    TI_FALSE);
753
754    /* that command should be sent to FW just in case we moved from Active to one of the PS modes
755     * and vice versa, it shoul not be sent when moving between different PS modes */
756    status = cmdBld_CmdSetPsMode (pPowerSrvSM->hCmdBld,
757                                  &powerSaveParams,
758                                  (void *)pPowerSrvSM->pSmRequest->powerSaveCmdResponseCB,
759                                  (pPowerSrvSM->pSmRequest->powerSaveCmdResponseCB == NULL) ? NULL : pPowerSrvSM->pSmRequest->powerSaveCBObject);
760
761    if ( status != TI_OK )
762    {
763        TRACE0(pPowerSrvSM->hReport, REPORT_SEVERITY_ERROR, "Error in configuring Power Manager paramters!\n");
764    }
765
766    return status;
767}
768
769/****************************************************************************************
770*                               powerSrvSMTimerExpired                                  *
771*****************************************************************************************
772DESCRIPTION: This function is called upon timer expiry - when the FW has not returned
773             a response within the defined tme (50 ms)
774
775INPUT:      hPowerSrvSM     - handle to the PowerSrvSM object.
776            bTwdInitOccured - Indicates if TWDriver recovery occured since timer started
777
778OUTPUT:    None
779
780RETURN:    None
781****************************************************************************************/
782static void powerSrvSMTimerExpired (TI_HANDLE hPowerSrvSM, TI_BOOL bTwdInitOccured)
783{
784    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
785
786    /* Print an error message */
787    TRACE0(pPowerSrvSM->hReport, REPORT_SEVERITY_ERROR, "PS guard timer expired!\n");
788
789    /* Call the error notification callback (triggering recovery) */
790    pPowerSrvSM->failureEventCB( pPowerSrvSM->hFailureEventObj ,POWER_SAVE_FAILURE );
791}
792
793/****************************************************************************************
794 *                        powerSrvRegisterFailureEventCB                                                    *
795 ****************************************************************************************
796DESCRIPTION: Registers a failure event callback for PS SM error notifications.
797
798
799INPUT:      - hPowerSrv         - handle to the PowerSrv object.
800            - failureEventCB    - the failure event callback function.\n
801            - hFailureEventObj - handle to the object passed to the failure event callback function.
802
803OUTPUT:
804RETURN:    void.
805****************************************************************************************/
806void powerSrvSM_RegisterFailureEventCB( TI_HANDLE hPowerSrvSM,
807                                        void *failureEventCB, TI_HANDLE hFailureEventObj )
808{
809    PowerSrvSM_t *pPowerSrvSM = (PowerSrvSM_t*)hPowerSrvSM;
810
811    pPowerSrvSM->failureEventCB = (TFailureEventCb)failureEventCB;
812    pPowerSrvSM->hFailureEventObj = hFailureEventObj;
813}
814
815