Home | History | Annotate | Download | only in Ctrl_Interface
      1 /*
      2  * EvHandler.c
      3  *
      4  * Copyright(c) 1998 - 2010 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 #define __FILE_ID__  FILE_ID_50
     35 #include "IPCKernelApi.h"
     36 #include "EvHandler.h"
     37 #include "osApi.h"
     38 #include "osDebug.h"
     39 
     40 #ifndef _WINDOWS
     41 #include "windows_types.h"
     42 #else
     43 #include <windows.h>
     44 #endif /*_WINDOWS*/
     45 
     46 #ifdef EV_HANDLER_DEBUG
     47 TI_HANDLE ghEvHandler; /* for debug, remove later*/
     48 #endif
     49 
     50 /* ************************** Upper Interface **********************************/
     51 
     52 TI_HANDLE EvHandler_Create (TI_HANDLE hOs)
     53 {
     54     TEvHandlerObj *pEvHandler;
     55 
     56     PRINT(DBG_INIT_LOUD, (" EvHandlerInit\n"));
     57     pEvHandler = os_memoryAlloc(hOs,sizeof(TEvHandlerObj));
     58 
     59     if(NULL == pEvHandler)
     60     {
     61         PRINT(DBG_INIT_LOUD, ("EvHandler_Create() - Allocation failed! Returning NULL.\n"));
     62         return NULL;
     63     }
     64 
     65     os_memoryZero(hOs,pEvHandler,sizeof(TEvHandlerObj));
     66 
     67     #ifdef EV_HANDLER_DEBUG
     68     ghEvHandler= pEvHandler;
     69       PRINTF(DBG_INIT_VERY_LOUD, ("EvHandlerInit: ghEvHandler set to %08X\n", ghEvHandler));
     70     #endif
     71 
     72     pEvHandler->hOs = hOs;
     73 
     74     pEvHandler->LastUMEventType = 0xFFFFFFFF;
     75 
     76     return (TI_HANDLE) pEvHandler;
     77 }
     78 
     79 TI_UINT32 EvHandlerUnload (TI_HANDLE hEvHandler)
     80 {
     81 
     82     TEvHandlerObj *pEvHandler;
     83 
     84     PRINT(DBG_INIT_LOUD, (" ev_handler_unLoad\n"));
     85     pEvHandler = (TEvHandlerObj *)hEvHandler;
     86 
     87     os_memoryFree(pEvHandler->hOs,pEvHandler,sizeof(TEvHandlerObj));
     88 
     89     return TI_OK;
     90 }
     91 
     92 
     93 TI_UINT32 EvHandlerRegisterEvent(TI_HANDLE hEvHandler, TI_UINT8* pData, TI_UINT32 Length)
     94 {
     95     TEvHandlerObj *pEvHandler;
     96     IPC_EVENT_PARAMS *pEvParams;
     97     TI_UINT32 ModuleIndex;
     98 
     99     if( (hEvHandler==NULL) || (pData == NULL)){
    100         PRINT(DBG_INIT_ERROR, "EvHandler:EvHandlerRegisterEvent Bad Handle passed \n");
    101         return TI_NOK;
    102     }
    103 
    104 #ifdef EV_HANDLER_DEBUG
    105     if (ghEvHandler !=  hEvHandler)
    106     {
    107         return TI_NOK;
    108     }
    109 #endif
    110 
    111     pEvHandler  = (TEvHandlerObj *)hEvHandler;
    112     pEvParams = (IPC_EVENT_PARAMS *)pData;
    113 
    114     PRINTF(DBG_INIT_LOUD, (" EvHandlerRegisterEvent EventType = %d \n",pEvParams->uEventType));
    115 
    116     /* used to be: if ( sizeof(IPC_EVENT_PARAMS) != Length)
    117        relaxed size checking (okay if output buffer is larger)  */
    118     if (sizeof(IPC_EVENT_PARAMS) > Length)
    119     {
    120         PRINTF(DBG_INIT_ERROR, (" EvHandlerRegisterEvent Error sizeof(IPC_EVENT_PARAMS) != Length,"
    121                                "%d != %d \n",sizeof(IPC_EVENT_PARAMS), (int)Length));
    122         return (TI_UINT32)STATUS_INVALID_PARAMETER;
    123     }
    124 
    125     if (pEvParams->uEventType >= IPC_EVENT_MAX){
    126         PRINTF(DBG_INIT_ERROR, (" EvHandlerRegisterEvent Error - Invalid Event Type = %d \n",
    127               pEvParams->uEventType));
    128         return (TI_UINT32)STATUS_INVALID_PARAMETER;
    129     }
    130 
    131     ModuleIndex = 0;
    132 
    133     while ((ModuleIndex < MAX_REGISTERED_MODULES) &&
    134        (pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex].uEventID != NULL))
    135     {
    136         ModuleIndex++;
    137     }
    138 
    139     if (ModuleIndex == MAX_REGISTERED_MODULES)
    140     {
    141         PRINTF(DBG_INIT_WARNING, (" EvHandlerRegisterEvent %d "
    142                                   "Registration queue full or event already registered!\n",
    143                                   pEvParams->uEventType));
    144         return (TI_UINT32)STATUS_INVALID_PARAMETER;
    145     }
    146 
    147     os_memoryCopy(pEvHandler->hOs,(TI_UINT8*)&pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex],
    148                     (TI_UINT8*)pEvParams,Length);
    149 
    150     pEvParams->uEventID = (TI_HANDLE)&pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex];
    151 
    152     pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex].uEventID = pEvParams->uEventID;
    153 
    154     PRINT(DBG_INIT_LOUD, " EvHandlerRegisterEvent Out \n");
    155     return STATUS_SUCCESS;
    156 
    157 }
    158 
    159 
    160 TI_UINT32 EvHandlerUnRegisterEvent(TI_HANDLE hEvHandler, TI_HANDLE uEventID)
    161 {
    162     TEvHandlerObj *pEvHandler;
    163     IPC_EVENT_PARAMS *pEvParams;
    164     TI_UINT32 ModuleIndex;
    165 
    166 #ifdef EV_HANDLER_DEBUG
    167     if (ghEvHandler !=  hEvHandler )
    168     {
    169           return TI_NOK;
    170     }
    171 #endif
    172 
    173     if (uEventID == NULL)
    174     {
    175         return TI_NOK;
    176     }
    177 
    178     pEvHandler = (TEvHandlerObj *)hEvHandler;
    179     pEvParams  = (IPC_EVENT_PARAMS *)uEventID;
    180 
    181     PRINTF(DBG_INIT_LOUD, (" EvHandlerUnRegisterEvent EventType = %d \n",pEvParams->uEventType));
    182 
    183     if( pEvParams->uEventType >= IPC_EVENT_MAX){
    184         PRINTF(DBG_INIT_ERROR, (" EvHandlerRegisterEvent Error Event Type = %d \n",
    185               pEvParams->uEventType));
    186         return (TI_UINT32)STATUS_INVALID_PARAMETER;
    187     }
    188 
    189     ModuleIndex = 0;
    190 
    191     while ((ModuleIndex < MAX_REGISTERED_MODULES) &&
    192            (pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex].uEventID != pEvParams->uEventID))
    193     {
    194         ModuleIndex++;
    195     }
    196 
    197     if (ModuleIndex == MAX_REGISTERED_MODULES)
    198     {
    199         PRINTF(DBG_INIT_ERROR, (" EvHandlerUnRegisterEvent %d "
    200                                "Registration queue doesn't hold this event!\n",
    201                                pEvParams->uEventType ));
    202         return (TI_UINT32)STATUS_INVALID_PARAMETER;
    203     }
    204     pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex].uEventID = NULL;
    205 
    206     return STATUS_SUCCESS;
    207 }
    208 
    209 /* ************************** Upper Interface End*********************************/
    210 
    211 /* ************************** Bottom Interface **********************************/
    212 
    213 TI_UINT32 EvHandlerSendEvent(TI_HANDLE hEvHandler, TI_UINT32 EvType, TI_UINT8* pData, TI_UINT32 Length)
    214 {
    215     TEvHandlerObj *pEvHandler;
    216     IPC_EV_DATA*         pNewEvent;
    217     TI_UINT32            TailIndex=0;
    218     TI_UINT32            ModuleIndex=0;
    219 
    220     PRINTF(DBG_INIT_LOUD, (" EvHandlerSendEvent %d  \n", EvType));
    221 
    222     if(hEvHandler == NULL){
    223         PRINT(DBG_INIT_ERROR, "EvHandlerSendEvent Bad Handle passed \n");
    224         return TI_NOK;
    225     }
    226 
    227 #ifdef EV_HANDLER_DEBUG
    228     if (ghEvHandler !=  hEvHandler)
    229     {
    230         return TI_NOK;
    231     }
    232 #endif
    233 
    234     pEvHandler  = (TEvHandlerObj *)hEvHandler;
    235 
    236     TailIndex   = pEvHandler->SendEventArray.TailIndex;
    237 
    238     while ((ModuleIndex < MAX_REGISTERED_MODULES) && (EvType <= IPC_EVENT_MAX))
    239     {
    240         if (pEvHandler->RegistrationArray[EvType][ModuleIndex].uEventID != NULL )
    241         {
    242             if(pEvHandler->SendEventArray.Counter == MAX_SEND_EVENTS)
    243             {
    244                 PRINT(DBG_INIT_ERROR, " EvHandlerSendEvent Array Full u Fool! \n");
    245                 return TI_NOK;
    246             }
    247 
    248             pNewEvent = &pEvHandler->SendEventArray.Array[TailIndex];
    249 
    250             /* copy the event parameters and data to the events queue*/
    251             os_memoryCopy(pEvHandler->hOs,(TI_UINT8*)&pNewEvent->EvParams,
    252                             (TI_UINT8*)&pEvHandler->RegistrationArray[EvType][ModuleIndex],
    253                             sizeof(IPC_EVENT_PARAMS));
    254 
    255             os_memoryZero(pEvHandler->hOs,(TI_UINT8*)pNewEvent->uBuffer, sizeof(pNewEvent->uBuffer));
    256 
    257             os_memoryCopy(pEvHandler->hOs,
    258                           (TI_UINT8*)pNewEvent->uBuffer,
    259                           (TI_UINT8*)pData,
    260                           Length);
    261 
    262             pNewEvent->uBufferSize = Length;
    263 
    264             if(pNewEvent->EvParams.uDeliveryType ==  DELIVERY_PUSH)
    265             {
    266                     PRINTF(DBG_INIT_LOUD, (" EvHandlerSendEvent %d to OS \n", EvType));
    267                     PRINTF(DBG_INIT_LOUD, ("EvHandlerSendEvent Matching OS Registered event found at EvType = %d,"
    268                                           "ModuleIndex = %d  \n", EvType, ModuleIndex));
    269                     IPC_EventSend (pEvHandler->hOs,(TI_UINT8*)pNewEvent,sizeof(IPC_EV_DATA));
    270             }
    271             else {
    272 
    273                 pEvHandler->LastUMEventType = EvType;
    274                 pEvHandler->SendEventArray.TailIndex = (TailIndex+1) % MAX_SEND_EVENTS;
    275                 pEvHandler->SendEventArray.Counter++;
    276                 TailIndex   = pEvHandler->SendEventArray.TailIndex;
    277                 PRINTF(DBG_INIT_LOUD, (" EvHandlerSendEvent %d to User Mode \n", EvType));
    278                 PRINTF(DBG_INIT_LOUD, ("EvHandlerSendEvent Matching User Mode Registered event found at EvType = %d,"
    279                                        "ModuleIndex = %d  \n", EvType, ModuleIndex));
    280                 if (pEvHandler->SendEventArray.Counter == 1)
    281                 {
    282                     IPC_EventSend (pEvHandler->hOs,NULL,0);
    283                 }
    284             }
    285         } /* end if*/
    286 
    287        ModuleIndex++;
    288 
    289    } /* end of while*/
    290 
    291     return TI_OK;
    292 }
    293 
    294  /* ************************** Bottom Interface End **********************************/
    295