Home | History | Annotate | Download | only in TrafficMonitor
      1 /****************************************************************************
      2 **+-----------------------------------------------------------------------+**
      3 **|                                                                       |**
      4 **| Copyright(c) 1998 - 2008 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 ****************************************************************************/
     35 /***************************************************************************/
     36 /*                                                                         */
     37 /*              MODULE:         TrafficMonitor.c                           */
     38 /*              PURPOSE:        Traffic Monitor                            */
     39 /*                                                                         */
     40 /***************************************************************************/
     41 #include "TrafficMonitorAPI.h"
     42 #include "TrafficMonitor.h"
     43 #include "DataCtrl_Api.h"
     44 #include "osApi.h"
     45 #include "report.h"
     46 
     47 
     48 /* Percentage of max down events test interval to use in our "traffic down" timer */
     49 #define MIN_INTERVAL_PERCENT 50
     50 
     51 /*#define TRAFF_TEST*/
     52 #ifdef TRAFF_TEST
     53 /*for TEST Function*/
     54 TI_HANDLE TestTrafficMonitor;
     55 TI_HANDLE TestEventTimer;
     56 TI_HANDLE Alert1;
     57 TI_HANDLE Alert2;
     58 TI_HANDLE Alert3;
     59 TI_HANDLE Alert4;
     60 VOID PrintElertStus();
     61 VOID TestEventFunc(TI_HANDLE hTrafficMonitor);
     62 #endif
     63 
     64 
     65 /************************************************************************/
     66 /*           Function prototype                                         */
     67 /************************************************************************/
     68 static VOID TimerMonitor_TimeOut(TI_HANDLE hTrafficMonitor);
     69 static void TrafficMonitor_updateBW(BandWidth_t *pBandWidth, UINT32 uCurrentTS);
     70 static UINT32 TrafficMonitor_calcBW(BandWidth_t *pBandWidth, UINT32 uCurrentTS);
     71 static BOOL isThresholdDown(TrafficAlertElement_t *AlertElement,UINT32 CurrentTime);
     72 static BOOL isThresholdUp(TrafficAlertElement_t *AlertElement , UINT32 CurrentTime);
     73 static VOID SimpleByteAggregation(TI_HANDLE TraffElem,int Count);
     74 static VOID SimpleFrameAggregation(TI_HANDLE TraffElem,int Count);
     75 static TI_HANDLE TrafficMonitor_ExitFunc(TrafficMonitor_t *TrafficMonitor,TI_HANDLE hOs);
     76 static TI_STATUS FindRstElemEntryIndex (TrafficMonitor_t *TrafficMonitor,TrafficAlertElement_t  *TrafficAlertElement,int *Index);
     77 static TI_STATUS TrafficMonitor_SetMask(TrafficMonitor_t *TrafficMonitor,TrafficAlertElement_t *TrafficAlertElement,TraffEvntOptNum_t MaskType);
     78 
     79 static void TrafficMonitor_UpdateDownTrafficTimerState (TI_HANDLE hTrafficMonitor);
     80 static void TrafficMonitor_ChangeDownTimerStatus (TI_HANDLE hTrafficMonitor, UINT32 downEventsFound, UINT32 minIntervalTime);
     81 
     82 /************************************************************************/
     83 /*                      TrafficMonitor_create                           */
     84 /************************************************************************/
     85 TI_HANDLE TrafficMonitor_create(TI_HANDLE hOs)
     86 {
     87     TrafficMonitor_t *TrafficMonitor;
     88 
     89     /* Allocate the data structure TrafficMonitor*/
     90         TrafficMonitor = (TrafficMonitor_t*)os_memoryAlloc(hOs, sizeof(TrafficMonitor_t));
     91         if (TrafficMonitor == NULL)
     92         return NULL;
     93 
     94     os_memoryZero(hOs,TrafficMonitor,sizeof(TrafficMonitor_t));
     95 
     96     TrafficMonitor->hOs = hOs;
     97 
     98     /*Create the base threshold timer that will serve all the down thresholds*/
     99         TrafficMonitor->TrafficMonTimer = os_timerCreate(hOs, TimerMonitor_TimeOut, TrafficMonitor);
    100         if (TrafficMonitor->TrafficMonTimer == NULL)
    101         return TrafficMonitor_ExitFunc(TrafficMonitor,hOs);
    102 
    103     /*Creates the list that will hold all the registered alert requests*/
    104     TrafficMonitor->NotificationRegList = List_create(hOs,MAX_MONITORED_REQ,sizeof(TrafficAlertElement_t));
    105     if (TrafficMonitor->NotificationRegList == NULL)
    106         return TrafficMonitor_ExitFunc(TrafficMonitor,hOs);
    107 
    108     return (TI_HANDLE)TrafficMonitor;
    109 }
    110 
    111 
    112 /************************************************************************/
    113 /*                    TrafficMonitor_ExitFunc                           */
    114 /************************************************************************/
    115 static TI_HANDLE TrafficMonitor_ExitFunc(TrafficMonitor_t *TrafficMonitor,TI_HANDLE hOs)
    116 {
    117     if (TrafficMonitor)
    118     {
    119         if(TrafficMonitor->TrafficMonTimer)
    120           os_timerDestroy(hOs,TrafficMonitor->TrafficMonTimer);
    121         os_memoryFree(hOs, TrafficMonitor, sizeof(TrafficMonitor_t));
    122     }
    123     return NULL;
    124 }
    125 
    126 
    127 
    128 /************************************************************************/
    129 /*                    TrafficMonitor_config                             */
    130 /************************************************************************/
    131 TI_STATUS TrafficMonitor_Init(TI_HANDLE hTrafficMonitor,TI_HANDLE hRxData,TI_HANDLE hTxData)
    132 {
    133     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    134 	UINT32 uCurrTS = os_timeStampMs(TrafficMonitor->hOs);
    135 
    136     if(TrafficMonitor == NULL)
    137         return NOK;
    138 
    139     TrafficMonitor->Active = FALSE;
    140 
    141     TrafficMonitor->hRxData = hRxData;
    142     TrafficMonitor->hTxData = hTxData;
    143 
    144 	/*Init All the bandwidth elements in the system */
    145 	os_memoryZero(TrafficMonitor->hOs,&TrafficMonitor->DirectTxFrameBW,sizeof(BandWidth_t));
    146 	os_memoryZero(TrafficMonitor->hOs,&TrafficMonitor->DirectRxFrameBW,sizeof(BandWidth_t));
    147 	TrafficMonitor->DirectRxFrameBW.auFirstEventsTS[0] = uCurrTS;
    148 	TrafficMonitor->DirectTxFrameBW.auFirstEventsTS[0] = uCurrTS;
    149 
    150     /*Registering to the RX module for notification.*/
    151     TrafficMonitor->RxRegReqHandle = rxData_RegNotif(hRxData,DIRECTED_FRAMES_RECV,
    152                                             TrafficMonitor_Event,TrafficMonitor,RX_TRAFF_MODULE);
    153     if (TrafficMonitor->RxRegReqHandle == NULL)
    154         return NOK;
    155 
    156 
    157     /*Registering to the TX module for notification .*/
    158     TrafficMonitor->TxRegReqHandle = txData_RegNotif(hTxData,DIRECTED_FRAMES_XFER,
    159                                             TrafficMonitor_Event,TrafficMonitor,TX_TRAFF_MODULE);
    160     if (TrafficMonitor->TxRegReqHandle == NULL)
    161         return NOK;
    162 
    163     TrafficMonitor->DownTimerEnabled = FALSE;
    164     TrafficMonitor->trafficDownTestIntervalPercent = MIN_INTERVAL_PERCENT;
    165 
    166 #ifdef TRAFF_TEST
    167     TestTrafficMonitor = TrafficMonitor;
    168     TestEventTimer = os_timerCreate(TrafficMonitor->hOs, TestEventFunc, TrafficMonitor);
    169     os_timerStart(TrafficMonitor->hOs,TestEventTimer,5000,TRUE);
    170 #endif
    171 
    172     return OK;
    173 }
    174 
    175 /************************************************************************/
    176 /*                TrafficMonitor_Start                                  */
    177 /************************************************************************/
    178 TI_STATUS TrafficMonitor_Start(TI_HANDLE hTrafficMonitor)
    179 {
    180     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    181     TrafficAlertElement_t *AlertElement;
    182     UINT32 CurentTime;
    183 
    184 
    185     if(TrafficMonitor == NULL)
    186         return NOK;
    187 
    188     /*starts the bandwidth TIMER*/
    189     if(!TrafficMonitor->Active) /*To prevent double call to timer start*/
    190     {
    191         TrafficMonitor_UpdateDownTrafficTimerState (TrafficMonitor);
    192     }
    193 
    194     AlertElement  = (TrafficAlertElement_t*)List_GetFirst(TrafficMonitor->NotificationRegList);
    195     CurentTime = os_timeStampMs(TrafficMonitor->hOs);
    196 
    197     /* go over all the Down elements and reload the timer*/
    198     while(AlertElement)
    199     {
    200         if(AlertElement->CurrentState != ALERT_WAIT_FOR_RESET)
    201         {
    202             AlertElement->EventCounter = 0;
    203             AlertElement->TimeOut = AlertElement->TimeIntervalMs + CurentTime;
    204         }
    205         AlertElement = (TrafficAlertElement_t*)List_GetNext(TrafficMonitor->NotificationRegList);
    206     }
    207     TrafficMonitor->Active = TRUE;
    208 
    209     return OK;
    210 }
    211 
    212 
    213 
    214 /************************************************************************/
    215 /*              TrafficMonitor_Stop                                     */
    216 /************************************************************************/
    217 TI_STATUS TrafficMonitor_Stop(TI_HANDLE hTrafficMonitor)
    218 {
    219     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    220     TrafficAlertElement_t *AlertElement;
    221 
    222     if(TrafficMonitor == NULL)
    223         return NOK;
    224 
    225     if(TrafficMonitor->Active) /*To prevent double call to timer stop*/
    226     {
    227 
    228         TrafficMonitor->Active = FALSE;
    229 
    230         TrafficMonitor->DownTimerEnabled = FALSE;
    231         os_timerStop(TrafficMonitor->hOs,TrafficMonitor->TrafficMonTimer);
    232 
    233     }
    234 
    235     /* Set all events state to ALERT_OFF to enable them to "kick" again once after TrafficMonitor is started */
    236     AlertElement  = (TrafficAlertElement_t*)List_GetFirst(TrafficMonitor->NotificationRegList);
    237 
    238      while(AlertElement)
    239       {
    240          AlertElement->CurrentState = ALERT_OFF;
    241          AlertElement = (TrafficAlertElement_t*)List_GetNext(TrafficMonitor->NotificationRegList);
    242     }
    243 
    244     return OK;
    245 }
    246 
    247 
    248 
    249 /************************************************************************/
    250 /*                  TrafficMonitor_Destroy                              */
    251 /************************************************************************/
    252 TI_STATUS TrafficMonitor_Destroy(TI_HANDLE hTrafficMonitor)
    253 {
    254     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    255 
    256     if (TrafficMonitor)
    257     {
    258         /*Unregister from the RX/TX module for the required notification*/
    259         txData_UnRegNotif(TrafficMonitor->hTxData,TrafficMonitor->TxRegReqHandle);
    260         rxData_UnRegNotif(TrafficMonitor->hRxData,TrafficMonitor->RxRegReqHandle);
    261 
    262         if(TrafficMonitor->NotificationRegList)
    263           List_Destroy(TrafficMonitor->NotificationRegList);
    264 
    265         if(TrafficMonitor->TrafficMonTimer)
    266           os_timerDestroy(TrafficMonitor->hOs,TrafficMonitor->TrafficMonTimer);
    267 
    268 #ifdef TRAFF_TEST
    269         os_timerDestroy(TrafficMonitor->hOs,TestEventTimer);
    270 #endif
    271         os_memoryFree(TrafficMonitor->hOs, TrafficMonitor, sizeof(TrafficMonitor_t));
    272 
    273         return OK;
    274     }
    275 
    276     return NOK;
    277 }
    278 
    279 
    280 /***********************************************************************
    281  *                        TrafficMonitor_RegEvent
    282  ***********************************************************************
    283 DESCRIPTION: Reg event processing function, Perform the following:
    284 
    285 
    286 INPUT:          hTrafficMonitor -       Traffic Monitor the object.
    287 
    288             TrafficAlertRegParm -       structure which include values to set for
    289                                                 the requested Alert event
    290 
    291             AutoResetCreate - is only relevant to edge alerts.
    292                   If AutoResetCreate flag is set to true then the registration function will create a conjunction reset element automatic
    293                  this reset element will be with the same threshold but opposite in direction
    294 
    295                  If AutoResetCreate flag is set to false then the reset element will be supplied afterward by the user with the function
    296                  TrafficMonitor_SetRstCondition() the alert will not be active till the reset function will be set.
    297 
    298 OUTPUT:
    299 
    300 RETURN:     TrafficAlertElement pointer on success, NULL otherwise
    301 
    302 ************************************************************************/
    303 TI_HANDLE TrafficMonitor_RegEvent(TI_HANDLE hTrafficMonitor,TrafficAlertRegParm_t *TrafficAlertRegParm,BOOL AutoResetCreate)
    304 {
    305     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    306     TrafficAlertElement_t  *TrafficAlertElement;
    307     UINT32 CurentTime ;
    308 
    309     if(TrafficMonitor == NULL)
    310        return NULL;
    311 
    312     CurentTime = os_timeStampMs(TrafficMonitor->hOs);
    313 
    314     /*Gets a TrafficAlertElement_t memory from the list to assign to the registered request*/
    315     TrafficAlertElement = (TrafficAlertElement_t*)List_AllocElement(TrafficMonitor->NotificationRegList);
    316     if (TrafficAlertElement == NULL)
    317     {   /* add print*/
    318                 return NULL  ;
    319     }
    320 
    321     /*Init the alert element with the registered parameters.*/
    322     TrafficAlertElement->CallBack = TrafficAlertRegParm->CallBack;
    323     TrafficAlertElement->Context  = TrafficAlertRegParm->Context;
    324     TrafficAlertElement->Cookie  = TrafficAlertRegParm->Cookie;
    325     TrafficAlertElement->Direction = TrafficAlertRegParm->Direction;
    326     TrafficAlertElement->Threshold = TrafficAlertRegParm->Threshold;
    327     TrafficAlertElement->Trigger = TrafficAlertRegParm->Trigger;
    328     TrafficAlertElement->TimeIntervalMs = TrafficAlertRegParm->TimeIntervalMs;
    329     TrafficAlertElement->TimeOut = CurentTime + TrafficAlertRegParm->TimeIntervalMs;
    330     TrafficAlertElement->EventCounter = 0;
    331     TrafficMonitor_SetMask(TrafficMonitor,TrafficAlertElement,TrafficAlertRegParm->MonitorType);
    332 
    333     TrafficAlertElement->CurrentState = ALERT_OFF;
    334     TrafficAlertElement->AutoCreated = FALSE;
    335     TrafficAlertElement->Enabled = FALSE;
    336     /*In case that this is an Edge alert there is a need for a reset condition element*/
    337     /*corresponding to the Alert request but opposite in the direction.*/
    338     /*Note that the reset condition for this (new) reset element, is the Alert Element it self.*/
    339     if(TrafficAlertElement->Trigger == TRAFF_EDGE)
    340     {
    341         if(AutoResetCreate)
    342         {
    343             /*Gets a TrafficAlertElement_t memory from the list to assign to the reset elemnt*/
    344             TrafficAlertElement->ResetElment[0] = (TrafficAlertElement_t*)List_AllocElement(TrafficMonitor->NotificationRegList);
    345             if( TrafficAlertElement->ResetElment[0] == NULL)
    346             {
    347                 List_FreeElement(TrafficMonitor->NotificationRegList,TrafficAlertElement);
    348                 return NULL;
    349             }
    350 
    351             /*
    352              copy the Traffic Element init params to the reset Elemnt Except for
    353              the direction and the call back that is set to null the CurrentState set to disable.
    354              And the reset condition,that points to the muster alert.
    355              */
    356             os_memoryCopy(TrafficMonitor->hOs,TrafficAlertElement->ResetElment[0],TrafficAlertElement,sizeof(TrafficAlertElement_t));
    357             TrafficAlertElement->ResetElment[0]->CallBack = NULL;
    358             /*opposite in the direction from the TrafficAlertElement->Direction*/
    359             if (TrafficAlertRegParm->Direction == TRAFF_UP)
    360                 TrafficAlertElement->ResetElment[0]->Direction = TRAFF_DOWN;
    361             else
    362                 TrafficAlertElement->ResetElment[0]->Direction = TRAFF_UP;
    363             TrafficAlertElement->ResetElment[0]->CurrentState = ALERT_WAIT_FOR_RESET;
    364             TrafficAlertElement->ResetElment[0]->ResetElment[0] = TrafficAlertElement;
    365             TrafficAlertElement->ResetElment[0]->AutoCreated = TRUE;
    366 
    367             TrafficAlertElement->ResetElment[0]->RstWasAssigned = TRUE;
    368             TrafficAlertElement->RstWasAssigned = TRUE;
    369 
    370         }
    371         else/* The reset element will be supplied afterward by the user in the meanwhile disable the alert till then*/
    372         {
    373             TrafficAlertElement->RstWasAssigned = FALSE;
    374             TrafficAlertElement->CurrentState = ALERT_WAIT_FOR_RESET;
    375         }
    376 
    377     }
    378 
    379     TrafficMonitor_UpdateDownTrafficTimerState (TrafficMonitor);
    380 
    381     return TrafficAlertElement;
    382 }
    383 
    384 
    385 /************************************************************************/
    386 /*                  FindRstElemEntryIndex                               */
    387 /************************************************************************/
    388 /* Gets a TrafficAlertElement_t memory from the list to assign to the reset elemnt
    389  * for internal use
    390  ************************************************************************/
    391 static TI_STATUS FindRstElemEntryIndex (TrafficMonitor_t *TrafficMonitor,TrafficAlertElement_t  *TrafficAlertElement,int *Index)
    392 {
    393     int i;
    394     /*Find an empty Rst element entry*/
    395     for(i=0;(i<MAX_RST_ELMENT_PER_ALERT) && TrafficAlertElement->ResetElment[i];i++);
    396     if(i == MAX_RST_ELMENT_PER_ALERT)
    397         return NOK;
    398     *Index  = i;
    399     return OK;
    400 }
    401 
    402 /************************************************************************/
    403 /*                  TrafficMonitor_SetMask                              */
    404 /************************************************************************/
    405 /*
    406  *      Convert the Mask from the types that declared in the
    407  *  TrafficMonitorAPI to the types that are used in the Rx Tx modules.
    408  *  And update the TX and RX module of the new event req
    409  *  Sets the aggregation function that corresponds to the specific mask type
    410  ************************************************************************/
    411 static TI_STATUS TrafficMonitor_SetMask(TrafficMonitor_t *TrafficMonitor,TrafficAlertElement_t *TrafficAlertElement,TraffEvntOptNum_t MaskType)
    412 {
    413     UINT32 TxMask = 0;
    414     UINT32 RxMask = 0;
    415 
    416    switch(MaskType) {
    417    case TX_RX_DIRECTED_FRAMES:
    418         TxMask = DIRECTED_FRAMES_XFER;
    419         RxMask = DIRECTED_FRAMES_RECV;
    420         TrafficAlertElement->ActionFunc = SimpleFrameAggregation;
    421         break;
    422    case TX_ALL_MSDU_FRAMES:
    423         TxMask = DIRECTED_FRAMES_XFER|MULTICAST_FRAMES_XFER|BROADCAST_FRAMES_XFER;
    424         TrafficAlertElement->ActionFunc = SimpleFrameAggregation;
    425     break;
    426    case RX_ALL_MSDU_FRAMES:
    427         RxMask = DIRECTED_FRAMES_RECV|MULTICAST_FRAMES_RECV|BROADCAST_FRAMES_RECV;
    428         TrafficAlertElement->ActionFunc = SimpleFrameAggregation;
    429         break;
    430    case TX_RX_ALL_MSDU_FRAMES:
    431         TxMask = DIRECTED_FRAMES_XFER|MULTICAST_FRAMES_XFER|BROADCAST_FRAMES_XFER;
    432         RxMask = DIRECTED_FRAMES_RECV|MULTICAST_FRAMES_RECV|BROADCAST_FRAMES_RECV;
    433         TrafficAlertElement->ActionFunc = SimpleFrameAggregation;
    434         break;
    435     case TX_RX_ALL_MSDU_IN_BYTES:
    436         TxMask = DIRECTED_BYTES_XFER|MULTICAST_BYTES_XFER|BROADCAST_BYTES_XFER;
    437         RxMask = DIRECTED_BYTES_RECV|MULTICAST_BYTES_RECV|BROADCAST_BYTES_RECV;
    438         TrafficAlertElement->ActionFunc = SimpleByteAggregation;
    439         break;
    440    case TX_RX_DIRECTED_IN_BYTES:
    441         TxMask = DIRECTED_BYTES_XFER;
    442         RxMask = DIRECTED_BYTES_RECV;
    443         TrafficAlertElement->ActionFunc = SimpleByteAggregation;
    444     break;
    445    case TX_RX_ALL_802_11_DATA_IN_BYTES:
    446         TxMask = DIRECTED_BYTES_XFER | MULTICAST_BYTES_XFER;
    447         RxMask = DIRECTED_BYTES_RECV | MULTICAST_BYTES_RECV;
    448         TrafficAlertElement->ActionFunc = SimpleByteAggregation;
    449     break;
    450    case TX_RX_ALL_802_11_DATA_FRAMES:
    451         TxMask = DIRECTED_FRAMES_XFER | MULTICAST_FRAMES_XFER;
    452         RxMask = DIRECTED_FRAMES_RECV | MULTICAST_FRAMES_RECV;
    453         TrafficAlertElement->ActionFunc = SimpleFrameAggregation;
    454     break;
    455    default:
    456         WLAN_OS_REPORT(("TrafficMonitor_SetMask - unknown parameter: %d\n", MaskType));
    457        return NOK;
    458    }
    459 
    460 
    461    if(RxMask)
    462    {
    463        TrafficAlertElement->MonitorMask[RX_TRAFF_MODULE] = RxMask;
    464        if(rxData_AddToNotifMask(TrafficMonitor->hRxData,TrafficMonitor->RxRegReqHandle,RxMask) == NOK)
    465            return NOK;
    466    }
    467 
    468    if(TxMask)
    469    {
    470        TrafficAlertElement->MonitorMask[TX_TRAFF_MODULE] = TxMask;
    471        if(txData_AddToNotifMask(TrafficMonitor->hTxData,TrafficMonitor->TxRegReqHandle,TxMask) == NOK)
    472            return NOK;
    473    }
    474 
    475    return OK;
    476 }
    477 
    478 
    479 /***********************************************************************
    480  *                        TrafficMonitor_SetRstCondition
    481  ***********************************************************************
    482 DESCRIPTION: Reg event processing function, Perform the following:
    483              Sets the given reset element to the Alert element.
    484              if MutualRst is set, then The operation is done vise versa .
    485 
    486 INPUT:          hTrafficMonitor -       Traffic Monitor the object.
    487 
    488             EventHandle -         Alert event
    489 
    490             ResetEventHandle  Alert Event that will be  used to as the rest for above.
    491 
    492             MutualRst - if the 2 elements are used to reset One another.
    493 
    494 NOTE        If the reset element event condition is the same as the alert element the user
    495             have to check the that threshold is bigger or smaller according to the direction
    496             else it can create a deadlock
    497 
    498 OUTPUT:
    499 
    500 RETURN:     OK on success, NOK otherwise
    501 
    502 ************************************************************************/
    503 TI_STATUS TrafficMonitor_SetRstCondition(TI_HANDLE hTrafficMonitor, TI_HANDLE EventHandle,TI_HANDLE ResetEventHandle,BOOL MutualRst)
    504 {
    505     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    506     TrafficAlertElement_t *TrafficAlertElement = (TrafficAlertElement_t*)EventHandle;
    507     TrafficAlertElement_t *TrafficResetAlertElement = (TrafficAlertElement_t*)ResetEventHandle;
    508     int i,x;
    509     UINT32 CurentTime ;
    510 
    511     if((TrafficMonitor == NULL) || (EventHandle == NULL) || (TrafficResetAlertElement == NULL))
    512         return NOK;
    513 
    514 
    515     CurentTime = os_timeStampMs(TrafficMonitor->hOs);
    516 
    517     /*
    518     Check that validity of the reset condition
    519     1.The reset condition is edge.
    520     2.The direction is opposite from the main alert.
    521     3.The threshold is bigger or smaller according to the direction
    522     This condition is not checked but the user have check it else it can create a deadlock..
    523     */
    524     if((TrafficResetAlertElement->Trigger != TRAFF_EDGE) || (TrafficAlertElement->Trigger != TRAFF_EDGE))
    525         return NOK;
    526     if(TrafficResetAlertElement->Direction == TrafficAlertElement->Direction)
    527         return NOK;
    528 
    529 
    530     /*Find an empty Rst element entry*/
    531     if(FindRstElemEntryIndex(TrafficMonitor,TrafficResetAlertElement,&i) == NOK)
    532         return NOK;
    533 
    534     TrafficResetAlertElement->ResetElment[i] = TrafficAlertElement;
    535 
    536     /*if we know for sure that No Rst Element was assigned
    537     therefore that element was in disable mode and we have to enable it.*/
    538     if (!(TrafficAlertElement->RstWasAssigned))
    539     {
    540         TrafficAlertElement->RstWasAssigned = TRUE;
    541         TrafficAlertElement->CurrentState = ALERT_OFF;
    542         TrafficAlertElement->TimeOut = CurentTime + TrafficAlertElement->TimeIntervalMs;
    543         TrafficAlertElement->EventCounter =0;
    544     }
    545 
    546 
    547     if(MutualRst)
    548     {
    549       /*Find an empty Rst element entry in the TempRstAlertElement*/
    550       if(FindRstElemEntryIndex(TrafficMonitor,TrafficAlertElement,&x) == NOK)
    551       {
    552         /*this clean up is not complete*/
    553         TrafficResetAlertElement->ResetElment[i] = NULL;
    554         return NOK;
    555       }
    556 
    557       TrafficAlertElement->ResetElment[x] = TrafficResetAlertElement;
    558       /*if know for sure that No Rst Element was assigned
    559       therefore that element was in disable mode and we have to enable it.*/
    560       if (!(TrafficResetAlertElement->RstWasAssigned))
    561       {
    562         TrafficResetAlertElement->RstWasAssigned = TRUE;
    563         TrafficResetAlertElement->CurrentState = ALERT_OFF;
    564         TrafficResetAlertElement->TimeOut = CurentTime + TrafficAlertElement->TimeIntervalMs;
    565         TrafficResetAlertElement->EventCounter = 0;
    566       }
    567     }
    568     return OK;
    569 }
    570 
    571 
    572 /************************************************************************/
    573 /*               TrafficMonitor_CleanRelatedRef                         */
    574 /************************************************************************/
    575 VOID TrafficMonitor_CleanRelatedRef(TrafficMonitor_t *TrafficMonitor,TrafficAlertElement_t *TrafficAlertElement)
    576 {
    577 
    578     int i;
    579     TrafficAlertElement_t *AlertElement  = (TrafficAlertElement_t*)List_GetFirst(TrafficMonitor->NotificationRegList);
    580 
    581     /* go over all the Down elements and check for alert ResetElment that ref to TrafficAlertElement*/
    582     while(AlertElement)
    583     {
    584         for(i=0;i<MAX_RST_ELMENT_PER_ALERT;i++)
    585         {
    586             if(AlertElement->ResetElment[i] == TrafficAlertElement)
    587                 AlertElement->ResetElment[i] = NULL;
    588         }
    589         AlertElement = (TrafficAlertElement_t*)List_GetNext(TrafficMonitor->NotificationRegList);
    590     }
    591 }
    592 
    593 
    594 
    595 /************************************************************************/
    596 /*          TrafficMonitor_StopNotif                                   */
    597 /************************************************************************/
    598 VOID TrafficMonitor_StopEventNotif(TI_HANDLE hTrafficMonitor,TI_HANDLE EventHandle)
    599 {
    600     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    601     TrafficAlertElement_t *TrafficAlertElement = (TrafficAlertElement_t*)EventHandle;
    602 
    603     if(TrafficMonitor == NULL)
    604         return ;
    605 
    606     if(TrafficAlertElement == NULL)
    607         return ;
    608 
    609     TrafficAlertElement->Enabled = FALSE;
    610 
    611 	TrafficMonitor_UpdateDownTrafficTimerState (hTrafficMonitor);
    612 
    613 }
    614 
    615 
    616 
    617 /************************************************************************/
    618 /*          TrafficMonitor_StartNotif                                   */
    619 /************************************************************************/
    620 VOID TrafficMonitor_StartEventNotif(TI_HANDLE hTrafficMonitor, TI_HANDLE EventHandle)
    621 {
    622     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    623     TrafficAlertElement_t *TrafficAlertElement = (TrafficAlertElement_t*)EventHandle;
    624 
    625     if(TrafficMonitor == NULL)
    626         return ;
    627 
    628     if(TrafficAlertElement == NULL)
    629         return ;
    630 
    631     TrafficAlertElement->Enabled = TRUE;
    632 
    633 	TrafficMonitor_UpdateDownTrafficTimerState (hTrafficMonitor);
    634 
    635 }
    636 
    637 
    638 
    639 /************************************************************************/
    640 /*          TrafficMonitor_StartNotif                                   */
    641 /************************************************************************/
    642 VOID TrafficMonitor_ResetEvent(TI_HANDLE hTrafficMonitor, TI_HANDLE EventHandle)
    643 {
    644     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    645     TrafficAlertElement_t *TrafficAlertElement = (TrafficAlertElement_t*)EventHandle;
    646 
    647     if(TrafficMonitor == NULL)
    648         return ;
    649 
    650     if(TrafficAlertElement == NULL)
    651         return ;
    652 
    653     TrafficAlertElement->CurrentState = ALERT_OFF;
    654 
    655     TrafficMonitor_UpdateDownTrafficTimerState (TrafficMonitor);
    656 }
    657 
    658 
    659 
    660 /************************************************************************/
    661 /*          TrafficMonitor_UnregEvent                                   */
    662 /************************************************************************/
    663 VOID TrafficMonitor_UnregEvent(TI_HANDLE hTrafficMonitor, TI_HANDLE EventHandle)
    664 {
    665     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    666     TrafficAlertElement_t *TrafficAlertElement = (TrafficAlertElement_t*)EventHandle;
    667 
    668     if(TrafficMonitor == NULL)
    669         return ;
    670 
    671     /*If it was an edge alert then there can be one more alert element to free.*/
    672     /*one is the alert, and the second is the reset element that corresponds to this alert*/
    673     /*if it was  Auto Created*/
    674     if (TrafficAlertElement->ResetElment[0])
    675       if (TrafficAlertElement->ResetElment[0]->AutoCreated)
    676        List_FreeElement(TrafficMonitor->NotificationRegList,TrafficAlertElement->ResetElment);
    677 
    678     TrafficMonitor_CleanRelatedRef(TrafficMonitor,TrafficAlertElement);
    679 
    680     List_FreeElement(TrafficMonitor->NotificationRegList,EventHandle);
    681 
    682     TrafficMonitor_UpdateDownTrafficTimerState (TrafficMonitor);
    683 }
    684 
    685 
    686 
    687 /***********************************************************************
    688  *                        isThresholdUp
    689  ***********************************************************************
    690 DESCRIPTION: Evaluate if alert element as crossed his threshold
    691              if yes it operate the callback registered for this alert and take care of the alert state.
    692                          For alert with UP direction the following algorithm is preformed
    693              If the threshold is passed in the req time interval or less. then
    694              For Level
    695                 The alert mode is changed to ON & the next timeout is set to the next interval.
    696              For Edge
    697                 The alert mode is changed to wait for reset and the reset element is set to off.
    698                 And his timeout is set
    699 
    700 INPUT:
    701             EventHandle -         Alert event
    702             CurrentTime - the current time Time stamp
    703 
    704 OUTPUT:
    705 
    706 RETURN:     If  threshold crossed TRUE else False
    707 
    708 ************************************************************************/
    709 static BOOL isThresholdUp(TrafficAlertElement_t *AlertElement , UINT32 CurrentTime)
    710 {
    711     int i;
    712 
    713     if (AlertElement->TimeOut < CurrentTime)
    714     {
    715         AlertElement->EventCounter = AlertElement->LastCounte;
    716         AlertElement->TimeOut = CurrentTime + AlertElement->TimeIntervalMs;
    717     }
    718 
    719     if (AlertElement->EventCounter > AlertElement->Threshold)
    720     {
    721         AlertElement->EventCounter = 0;
    722         /*Sets the new due time (time out)*/
    723         AlertElement->TimeOut = CurrentTime + AlertElement->TimeIntervalMs;
    724 
    725         /*For Edge alert change the alert status to wait for reset and
    726         The corresponding reset element from wait for reset To off.
    727         That way toggling the two elements*/
    728         if(AlertElement->Trigger == TRAFF_EDGE)
    729         {
    730             AlertElement->CurrentState = ALERT_WAIT_FOR_RESET;
    731             for(i=0;i<MAX_RST_ELMENT_PER_ALERT;i++)
    732             {
    733                 TrafficAlertElement_t *rstElmt = AlertElement->ResetElment[i];
    734                 if(rstElmt != NULL)
    735                     if(rstElmt->CurrentState == ALERT_WAIT_FOR_RESET)
    736                     {
    737                         rstElmt->CurrentState = ALERT_OFF;
    738                         rstElmt->EventCounter = 0;
    739                         rstElmt->TimeOut = CurrentTime + rstElmt->TimeIntervalMs;
    740                     }
    741             }
    742         }
    743         else
    744             AlertElement->CurrentState = ALERT_ON;
    745 
    746         /*Call the callback function*/
    747         if((AlertElement->CallBack != NULL) && AlertElement->Enabled)
    748             AlertElement->CallBack(AlertElement->Context,AlertElement->Cookie);
    749         return TRUE;
    750     }
    751 
    752     return FALSE;
    753 }
    754 
    755 
    756 
    757 /***********************************************************************
    758  *                        isThresholdDown
    759  ***********************************************************************
    760 DESCRIPTION: Evaluate if alert element as crossed his threshold
    761              if yes it operate the callback registered for this alert and take care of the alert state.
    762                          For alert with DOWN direction the following algorithm is preformed
    763              If the threshold is passed (EventCounter < Threshold) in the req time only. then
    764              For Level
    765                The alert mode is changed to ON & the next timeout is set to the next interval.
    766                If the alert condition will still be on.then the next alert will be in the next time interval
    767             For Edge
    768                The alert mode is changed to wait for reset and the reset element is set to off.
    769                And his timeout is set.
    770 
    771 INPUT:
    772             EventHandle -         Alert event
    773             CurrentTime - the current time Time stamp
    774 
    775 OUTPUT:
    776 
    777 RETURN:     If threshold crossed TRUE else False
    778 
    779 ************************************************************************/
    780 static BOOL isThresholdDown(TrafficAlertElement_t *AlertElement , UINT32 CurrentTime)
    781 {
    782     int i;
    783     BOOL returnVal = FALSE;
    784 
    785     /*
    786     if its end of window time.
    787     */
    788     if (AlertElement->TimeOut <= CurrentTime)
    789     {
    790         /*
    791         if there was a down edge event.
    792         */
    793         if (AlertElement->EventCounter <= AlertElement->Threshold)
    794         {
    795             /*For Edge alert change the alert status to wait for reset and
    796             The corresponding reset element from wait for reset To off.
    797             That way toggling the two elements*/
    798             if(AlertElement->Trigger == TRAFF_EDGE)
    799             {
    800                 AlertElement->CurrentState = ALERT_WAIT_FOR_RESET;
    801                 for(i=0;i<MAX_RST_ELMENT_PER_ALERT;i++)
    802                 {
    803                     TrafficAlertElement_t *rstElmt = AlertElement->ResetElment[i];
    804                     if(rstElmt != NULL)
    805                         if(rstElmt->CurrentState == ALERT_WAIT_FOR_RESET)
    806                         {
    807                             rstElmt->CurrentState = ALERT_OFF;
    808                             rstElmt->EventCounter = 0;
    809                             rstElmt->TimeOut = CurrentTime + rstElmt->TimeIntervalMs;
    810                         }
    811                 }
    812             }
    813             else
    814                 AlertElement->CurrentState = ALERT_ON;
    815 
    816             /*Call the callback function*/
    817             if((AlertElement->CallBack != NULL) && AlertElement->Enabled)
    818                 AlertElement->CallBack(AlertElement->Context,AlertElement->Cookie);
    819 
    820             returnVal = TRUE;
    821         }
    822 
    823         /* end of time window - clear the event counter for the new window.*/
    824         AlertElement->EventCounter = 0;
    825         /*Sets the new due time (time out)*/
    826         AlertElement->TimeOut = CurrentTime + AlertElement->TimeIntervalMs;
    827     }
    828     else
    829     {
    830         /*
    831         In case we find out that the alert condition will not Occur for this frame window,
    832         therefor start a new alert examine cycle (the next farme window).
    833         (Not wait till the timeout of this current frame window)
    834         */
    835         if(AlertElement->EventCounter > AlertElement->Threshold)
    836         {
    837             AlertElement->EventCounter = 0;
    838             AlertElement->TimeOut = CurrentTime + AlertElement->TimeIntervalMs;
    839         }
    840     }
    841     return returnVal;
    842 }
    843 
    844 
    845 
    846 /************************************************************************/
    847 /*              TimerMonitor_TimeOut                                    */
    848 /************************************************************************/
    849 /*
    850  *      Timer function that is called for every x time interval
    851  *   That will invoke a process if any down limit as occurred.
    852  *
    853  ************************************************************************/
    854 static VOID TimerMonitor_TimeOut(TI_HANDLE hTrafficMonitor)
    855 {
    856 
    857     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    858     TrafficAlertElement_t *AlertElement;
    859     UINT32 CurentTime;
    860     UINT32 activeTrafDownEventsNum = 0;
    861     UINT32 trafficDownMinTimeout = 0xFFFFFFFF;
    862 
    863     if(TrafficMonitor == NULL)
    864         return;
    865 
    866     AlertElement  = (TrafficAlertElement_t*)List_GetFirst(TrafficMonitor->NotificationRegList);
    867     CurentTime = os_timeStampMs(TrafficMonitor->hOs);
    868 
    869 
    870     /* go over all the Down elements and check for alert */
    871     while(AlertElement)
    872     {
    873         if(AlertElement->CurrentState != ALERT_WAIT_FOR_RESET)
    874         {
    875             if (AlertElement->Direction == TRAFF_DOWN)
    876             {
    877                isThresholdDown(AlertElement,CurentTime);
    878             }
    879         }
    880 
    881          if ((AlertElement->Direction == TRAFF_DOWN) && (AlertElement->Trigger == TRAFF_EDGE) && (AlertElement->CurrentState == ALERT_OFF) && (AlertElement->Enabled == TRUE))
    882          {
    883             /* Increase counter of active traffic down events */
    884             activeTrafDownEventsNum++;
    885 
    886             /* Search for the alert with the most short Interval time - will be used to start timer */
    887             if ((AlertElement->TimeIntervalMs) < (trafficDownMinTimeout))
    888                trafficDownMinTimeout = AlertElement->TimeIntervalMs;
    889          }
    890 
    891         AlertElement = (TrafficAlertElement_t*)List_GetNext(TrafficMonitor->NotificationRegList);
    892     }
    893 
    894    TrafficMonitor_ChangeDownTimerStatus (TrafficMonitor,activeTrafDownEventsNum,trafficDownMinTimeout);
    895 
    896 }
    897 
    898 /***********************************************************************
    899  *                        TrafficMonitor_IsEventOn
    900  ***********************************************************************
    901 DESCRIPTION: Returns the current status of an event element.
    902 
    903 INPUT:      TrafficAlertElement_t
    904 
    905 
    906 OUTPUT:    bool
    907 
    908 RETURN:     True = ON  false = OFF
    909 
    910 ************************************************************************/
    911 BOOL TrafficMonitor_IsEventOn(TI_HANDLE EventHandle)
    912 {
    913     TrafficAlertElement_t *TrafficAlertElement = (TrafficAlertElement_t*)EventHandle;
    914 
    915     if(TrafficAlertElement == NULL)
    916         return FALSE;
    917 
    918 
    919     if (TrafficAlertElement->CurrentState == ALERT_OFF)
    920         return FALSE;
    921     else
    922         return TRUE;
    923 
    924 }
    925 
    926 
    927 
    928 /***********************************************************************
    929  *                        TrafficMonitor_GetFrameBandwidth
    930  ***********************************************************************
    931 DESCRIPTION: Returns the total direct frames in the Rx and Tx per second.
    932 
    933 INPUT:          hTrafficMonitor -       Traffic Monitor the object.
    934 
    935 
    936 OUTPUT:
    937 
    938 RETURN:     Total BW
    939 ************************************************************************/
    940 int TrafficMonitor_GetFrameBandwidth(TI_HANDLE hTrafficMonitor)
    941 {
    942 	TrafficMonitor_t *pTrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
    943 	UINT32 uCurentTS = os_timeStampMs(pTrafficMonitor->hOs);
    944 
    945 	if(pTrafficMonitor == NULL)
    946 	{
    947 		return -1;	/* return error code */
    948 	}
    949 
    950 	/* Calculate BW for Rx & Tx */
    951 	return ( TrafficMonitor_calcBW(&pTrafficMonitor->DirectRxFrameBW, uCurentTS) +
    952 			 TrafficMonitor_calcBW(&pTrafficMonitor->DirectTxFrameBW, uCurentTS) );
    953 }
    954 
    955 /***********************************************************************
    956 *                        TrafficMonitor_updateBW
    957 ***********************************************************************
    958 DESCRIPTION: Upon receiving an event of Tx/Rx (a packet was sent or received), This function is
    959 				called and performs BW calculation.
    960 
    961 INPUT:
    962 				pBandWidth		- BW of Rx or Tx
    963 				uCurrentTS		- current TS of the recent event
    964 
    965 OUTPUT:         pBandWidth		- updated counters and TS
    966 
    967 ************************************************************************/
    968 void TrafficMonitor_updateBW(BandWidth_t *pBandWidth, UINT32 uCurrentTS)
    969 {
    970 	/* Check if we should move to the next window */
    971 	if ( (uCurrentTS - pBandWidth->auFirstEventsTS[pBandWidth->uCurrentWindow]) < (SIZE_OF_WINDOW_MS) )
    972 	{
    973 		pBandWidth->auWindowCounter[pBandWidth->uCurrentWindow]++;
    974 	}
    975 	else	/* next window */
    976 	{
    977 		/* increment current window and mark the first event received */
    978 		pBandWidth->uCurrentWindow = (pBandWidth->uCurrentWindow + 1) & CYCLIC_COUNTER_ELEMENT;
    979 		pBandWidth->auFirstEventsTS[pBandWidth->uCurrentWindow] = uCurrentTS;
    980 		pBandWidth->auWindowCounter[pBandWidth->uCurrentWindow] = 1;
    981 	}
    982 }
    983 /***********************************************************************
    984 *                        TrafficMonitor_calcBW
    985 ***********************************************************************
    986 DESCRIPTION: Returns the total direct frames in Rx or Tx.
    987 			 It is called when outside module request the BW.
    988 			 Calculate band width by summing up the sliding windows.
    989 
    990 INPUT:       pBandWidth		- BW of Rx or Tx
    991 			 uCurrentTS		- current TS
    992 
    993 RETURN:     Total BW
    994 ************************************************************************/
    995 UINT32 TrafficMonitor_calcBW(BandWidth_t *pBandWidth, UINT32 uCurrentTS)
    996 {
    997 	UINT32 uTotalTime = uCurrentTS - pBandWidth->auFirstEventsTS[pBandWidth->uCurrentWindow];
    998 	UINT32 uTotalBW = 0;
    999 	INT32  iter = (INT32)pBandWidth->uCurrentWindow;
   1000 	INT32  iNextIter = (iter - 1) & CYCLIC_COUNTER_ELEMENT;	/* Always one less than i */
   1001 
   1002 	/* As long as the summed windows are less than BW_WINDOW_MS and we didn't loop the whole array */
   1003 	while ( (uTotalTime < BW_WINDOW_MS) && (iNextIter != pBandWidth->uCurrentWindow))
   1004 	{
   1005 		uTotalBW	+= pBandWidth->auWindowCounter[iter];
   1006 		/* add next window time - next loop will check if we exceeded the BW window */
   1007 		uTotalTime   = uCurrentTS - pBandWidth->auFirstEventsTS[iNextIter];
   1008 
   1009 		iter = iNextIter;
   1010 		iNextIter = (iter - 1) & CYCLIC_COUNTER_ELEMENT;
   1011 	} ;
   1012 
   1013 	/*
   1014 	 * Note that if (iNextIter == pBandWidth->uCurrentWindow) than the calculated BW could be up to
   1015 	 * SIZE_OF_WINDOW_MS less than BW_WINDOW_MS
   1016 	 */
   1017 	return uTotalBW;
   1018 }
   1019 
   1020 
   1021 /***********************************************************************
   1022  *                        TrafficMonitor_Event
   1023  ***********************************************************************
   1024 DESCRIPTION: this function is called for every event that was requested from the Tx or Rx
   1025              The function preformes update of the all the relevant Alert in the system
   1026              that corresponds to the event. checks the Alert Status due to this event.
   1027 
   1028 
   1029 
   1030 INPUT:          hTrafficMonitor -       Traffic Monitor the object.
   1031 
   1032             Count - evnet count.
   1033             Mask - the event mask that That triggered this function.
   1034 
   1035             MonitorModuleType Will hold the module type from where this function was called.
   1036 
   1037 OUTPUT:
   1038 
   1039 RETURN:
   1040 
   1041 ************************************************************************/
   1042 VOID TrafficMonitor_Event(TI_HANDLE hTrafficMonitor,int Count,UINT16 Mask,UINT32 MonitorModuleType)
   1043 {
   1044     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
   1045     TrafficAlertElement_t *AlertElement;
   1046     UINT32 activeTrafDownEventsNum = 0;
   1047     UINT32 trafficDownMinTimeout = 0xFFFFFFFF;
   1048 	UINT32 uCurentTS = os_timeStampMs(TrafficMonitor->hOs);
   1049 
   1050     if(TrafficMonitor == NULL)
   1051         return;
   1052 
   1053     if(!TrafficMonitor->Active)
   1054         return;
   1055 
   1056     /* for BW calculation */
   1057     if(MonitorModuleType == RX_TRAFF_MODULE)
   1058     {
   1059         if(Mask & DIRECTED_FRAMES_RECV)
   1060 		{
   1061             TrafficMonitor_updateBW(&TrafficMonitor->DirectRxFrameBW, uCurentTS);
   1062 		}
   1063     }
   1064     else if (MonitorModuleType == TX_TRAFF_MODULE)
   1065     {
   1066         if(Mask & DIRECTED_FRAMES_XFER)
   1067 		{
   1068             TrafficMonitor_updateBW(&TrafficMonitor->DirectTxFrameBW, uCurentTS);
   1069 		}
   1070     }
   1071     else
   1072 	{
   1073         return; /* module type does not exist, error return */
   1074 	}
   1075 
   1076     AlertElement  = (TrafficAlertElement_t*)List_GetFirst(TrafficMonitor->NotificationRegList);
   1077 
   1078     /* go over all the elements and check for alert */
   1079     while(AlertElement)
   1080     {
   1081         if(AlertElement->CurrentState != ALERT_WAIT_FOR_RESET)
   1082         {
   1083             if(AlertElement->MonitorMask[MonitorModuleType] & Mask)
   1084             {
   1085                 AlertElement->ActionFunc(AlertElement,Count);
   1086                 if (AlertElement->Direction == TRAFF_UP)
   1087                 {
   1088                     isThresholdUp(AlertElement, uCurentTS);
   1089                 }
   1090             }
   1091 
   1092             if ((AlertElement->Direction == TRAFF_DOWN) && (AlertElement->Trigger == TRAFF_EDGE) && (AlertElement->CurrentState == ALERT_OFF) && (AlertElement->Enabled == TRUE))
   1093             {
   1094                /* Increase counter of active traffic down events */
   1095                activeTrafDownEventsNum++;
   1096 
   1097                /* Search for the alert with the most short Interval time - will be used to start timer */
   1098                if ((AlertElement->TimeIntervalMs) < (trafficDownMinTimeout))
   1099                   trafficDownMinTimeout = AlertElement->TimeIntervalMs;
   1100             }
   1101 
   1102         }
   1103         AlertElement = (TrafficAlertElement_t*)List_GetNext(TrafficMonitor->NotificationRegList);
   1104     }
   1105 
   1106     TrafficMonitor_ChangeDownTimerStatus (TrafficMonitor,activeTrafDownEventsNum,trafficDownMinTimeout);
   1107 
   1108 }
   1109 
   1110 
   1111 /*
   1112  *      Used as the aggregation function that is used by the alerts for counting the events.
   1113  */
   1114 static VOID SimpleByteAggregation(TI_HANDLE TraffElem,int Count)
   1115 {
   1116     TrafficAlertElement_t *AlertElement = TraffElem;
   1117     AlertElement->EventCounter += Count;
   1118     AlertElement->LastCounte = Count;
   1119 }
   1120 
   1121 
   1122 /*
   1123  *      Used as the aggregation function for frame. (count is not used)
   1124  */
   1125 static VOID SimpleFrameAggregation(TI_HANDLE TraffElem,int Count)
   1126 {
   1127     TrafficAlertElement_t *AlertElement = TraffElem;
   1128     AlertElement->EventCounter++;
   1129     AlertElement->LastCounte = 1;
   1130 }
   1131 
   1132 /*-----------------------------------------------------------------------------
   1133 Routine Name: TrafficMonitor_UpdateDownTrafficTimerState
   1134 Routine Description: called whenever a "down" alert is called, or any other change in the alert list.
   1135                      used to either start or stop the "traffic down" timer.
   1136                      loops through alert list, searches for active traffic down events.
   1137 Arguments:
   1138 Return Value:
   1139 -----------------------------------------------------------------------------*/
   1140 static void TrafficMonitor_UpdateDownTrafficTimerState (TI_HANDLE hTrafficMonitor)
   1141 {
   1142 	TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
   1143     TrafficAlertElement_t *AlertElement;
   1144     UINT32 activeTrafDownEventsNum = 0;
   1145     UINT32 trafficDownMinTimeout = 0xFFFFFFFF;
   1146 
   1147     AlertElement  = (TrafficAlertElement_t*)List_GetFirst(TrafficMonitor->NotificationRegList);
   1148 
   1149     while(AlertElement)
   1150     {
   1151 
   1152       if ((AlertElement->Direction == TRAFF_DOWN) && (AlertElement->Trigger == TRAFF_EDGE) && (AlertElement->CurrentState == ALERT_OFF) && (AlertElement->Enabled == TRUE))
   1153       {
   1154          /* Increase counter of active traffic down events */
   1155          activeTrafDownEventsNum++;
   1156 
   1157          /* Search for the alert with the most short Interval time - will be used to start timer */
   1158          if ((AlertElement->TimeIntervalMs) < (trafficDownMinTimeout))
   1159             trafficDownMinTimeout = AlertElement->TimeIntervalMs;
   1160       }
   1161 
   1162       AlertElement = (TrafficAlertElement_t*)List_GetNext(TrafficMonitor->NotificationRegList);
   1163 
   1164     }
   1165 
   1166     TrafficMonitor_ChangeDownTimerStatus (TrafficMonitor,activeTrafDownEventsNum,trafficDownMinTimeout);
   1167 
   1168 }
   1169 
   1170 /*-----------------------------------------------------------------------------
   1171 Routine Name: TrafficMonitor_ChangeDownTimerStatus
   1172 Routine Description: Start or stop down traffic timer according to number of down events found and minInterval time.
   1173 Arguments:
   1174 Return Value:
   1175 -----------------------------------------------------------------------------*/
   1176 static void TrafficMonitor_ChangeDownTimerStatus (TI_HANDLE hTrafficMonitor, UINT32 downEventsFound, UINT32 minIntervalTime)
   1177 {
   1178 	TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
   1179 
   1180    if ((downEventsFound == 0) && (TrafficMonitor->DownTimerEnabled == TRUE))
   1181     {
   1182       TrafficMonitor->DownTimerEnabled = FALSE;
   1183       os_timerStop(TrafficMonitor->hOs,TrafficMonitor->TrafficMonTimer);
   1184     }
   1185     else if ((downEventsFound > 0) && (TrafficMonitor->DownTimerEnabled == FALSE))
   1186     {
   1187       TrafficMonitor->DownTimerEnabled = TRUE;
   1188       /* Start the timer with user defined percentage of the the minimum interval discovered earlier */
   1189       os_timerStart(TrafficMonitor->hOs,TrafficMonitor->TrafficMonTimer,
   1190          ((minIntervalTime * TrafficMonitor->trafficDownTestIntervalPercent) / 100),TRUE);
   1191 
   1192     }
   1193 }
   1194 
   1195 #ifdef TI_DBG
   1196 
   1197 /*-----------------------------------------------------------------------------
   1198 Routine Name: TrafficMonitor_UpdateActiveEventsCounters
   1199 Routine Description:
   1200 Arguments:
   1201 Return Value:
   1202 -----------------------------------------------------------------------------*/
   1203 void TrafficMonitor_UpdateActiveEventsCounters (TI_HANDLE hTrafficMonitor)
   1204 {
   1205 	TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)hTrafficMonitor;
   1206     TrafficAlertElement_t *AlertElement;
   1207     UINT32 activeTrafDownEventsNum = 0;
   1208 
   1209     AlertElement  = (TrafficAlertElement_t*)List_GetFirst(TrafficMonitor->NotificationRegList);
   1210 
   1211     while(AlertElement)
   1212     {
   1213       if ((AlertElement->Direction == TRAFF_DOWN) && (AlertElement->Trigger == TRAFF_EDGE) && (AlertElement->CurrentState == ALERT_OFF) && (AlertElement->Enabled == TRUE))
   1214       {
   1215          activeTrafDownEventsNum++;
   1216       }
   1217       AlertElement = (TrafficAlertElement_t*)List_GetNext(TrafficMonitor->NotificationRegList);
   1218     }
   1219 
   1220 }
   1221 
   1222 
   1223 #endif
   1224 
   1225 #ifdef TRAFF_TEST
   1226 /*
   1227  *      TEST Function
   1228  */
   1229 VOID func1(TI_HANDLE Context,UINT32 Cookie)
   1230 {
   1231     switch(Cookie) {
   1232     case 1:
   1233                 WLAN_OS_REPORT(("TRAFF - ALERT UP limit - 50 ON"));
   1234         break;
   1235     case 2:
   1236                 WLAN_OS_REPORT(("TRAFF - ALERT UP limit - 30 ON"));
   1237     break;
   1238     case 3:
   1239                 WLAN_OS_REPORT(("TRAFF - ALERT DOWN limit - 25 ON"));
   1240     break;
   1241     case 4:
   1242                 WLAN_OS_REPORT(("TRAFF - ALERT DOWN limit - 10 ON"));
   1243     break;
   1244    }
   1245 
   1246 }
   1247 
   1248 
   1249 VOID PrintElertStus()
   1250 {
   1251     TrafficMonitor_t *TrafficMonitor =(TrafficMonitor_t*)TestTrafficMonitor;
   1252     TrafficAlertElement_t *AlertElement  = (TrafficAlertElement_t*)List_GetFirst(TrafficMonitor->NotificationRegList);
   1253 
   1254     /* go over all the Down elements and check for alert ResetElment that ref to TrafficAlertElement*/
   1255     while(AlertElement)
   1256     {
   1257         if(AlertElement->CurrentState == ALERT_WAIT_FOR_RESET)
   1258             WLAN_OS_REPORT(("TRAFF - ALERT ALERT_WAIT_FOR_RESET"));
   1259         else
   1260             WLAN_OS_REPORT(("TRAFF - ALERT ENABLED"));
   1261 
   1262 
   1263         AlertElement = (TrafficAlertElement_t*)List_GetNext(TrafficMonitor->NotificationRegList);
   1264     }
   1265 }
   1266 
   1267 VOID TestEventFunc(TI_HANDLE hTrafficMonitor)
   1268 {
   1269 
   1270     static flag = TRUE;
   1271     TrafficAlertRegParm_t TrafficAlertRegParm ;
   1272     if(flag)
   1273     {
   1274 
   1275         TrafficAlertRegParm.CallBack = func1;
   1276         TrafficAlertRegParm.Context = NULL ;
   1277         TrafficAlertRegParm.Cookie =  1 ;
   1278         TrafficAlertRegParm.Direction = TRAFF_UP ;
   1279         TrafficAlertRegParm.Trigger = TRAFF_EDGE;
   1280         TrafficAlertRegParm.TimeIntervalMs = 1000;
   1281         TrafficAlertRegParm.Threshold = 50;
   1282         TrafficAlertRegParm.MonitorType = TX_RX_DIRECTED_FRAMES;
   1283         Alert1 = TrafficMonitor_RegEvent(TestTrafficMonitor,&TrafficAlertRegParm,FALSE);
   1284 
   1285         TrafficAlertRegParm.CallBack = func1;
   1286         TrafficAlertRegParm.Context = NULL ;
   1287         TrafficAlertRegParm.Cookie =  2 ;
   1288         TrafficAlertRegParm.Direction = TRAFF_UP ;
   1289         TrafficAlertRegParm.Trigger = TRAFF_EDGE;
   1290         TrafficAlertRegParm.TimeIntervalMs = 1000;
   1291         TrafficAlertRegParm.Threshold = 30;
   1292         TrafficAlertRegParm.MonitorType = TX_RX_DIRECTED_FRAMES;
   1293         Alert2 = TrafficMonitor_RegEvent(TestTrafficMonitor,&TrafficAlertRegParm,FALSE);
   1294 
   1295 
   1296         TrafficAlertRegParm.CallBack = func1;
   1297         TrafficAlertRegParm.Context = NULL ;
   1298         TrafficAlertRegParm.Cookie =  3 ;
   1299         TrafficAlertRegParm.Direction = TRAFF_DOWN ;
   1300         TrafficAlertRegParm.Trigger = TRAFF_EDGE;
   1301         TrafficAlertRegParm.TimeIntervalMs = 1000;
   1302         TrafficAlertRegParm.Threshold = 25;
   1303         TrafficAlertRegParm.MonitorType = TX_RX_DIRECTED_FRAMES;
   1304         Alert3 = TrafficMonitor_RegEvent(TestTrafficMonitor,&TrafficAlertRegParm,FALSE);
   1305 
   1306         TrafficAlertRegParm.CallBack = func1;
   1307         TrafficAlertRegParm.Context = NULL ;
   1308         TrafficAlertRegParm.Cookie =  4 ;
   1309         TrafficAlertRegParm.Direction = TRAFF_DOWN ;
   1310         TrafficAlertRegParm.Trigger = TRAFF_LEVEL;
   1311         TrafficAlertRegParm.TimeIntervalMs = 1000;
   1312         TrafficAlertRegParm.Threshold = 10;
   1313         TrafficAlertRegParm.MonitorType = TX_RX_DIRECTED_FRAMES;
   1314         Alert4 = TrafficMonitor_RegEvent(TestTrafficMonitor,&TrafficAlertRegParm,FALSE);
   1315 
   1316        TrafficMonitor_SetRstCondition(TestTrafficMonitor, Alert1,Alert3,TRUE);
   1317        TrafficMonitor_SetRstCondition(TestTrafficMonitor, Alert2,Alert3,FALSE);
   1318        flag = FALSE;
   1319     }
   1320 
   1321     PrintElertStus();
   1322 
   1323 }
   1324 
   1325 #endif
   1326