utils/GenSM.c
changeset 0 10c42ec6c05f
equal deleted inserted replaced
-1:000000000000 0:10c42ec6c05f
       
     1 /*
       
     2  * GenSM.c
       
     3  *
       
     4  * Copyright(c) 1998 - 2010 Texas Instruments. All rights reserved.      
       
     5  * All rights reserved.      
       
     6  * 
       
     7  * This program and the accompanying materials are made available under the 
       
     8  * terms of the Eclipse Public License v1.0 or BSD License which accompanies
       
     9  * this distribution. The Eclipse Public License is available at
       
    10  * http://www.eclipse.org/legal/epl-v10.html and the BSD License is as below.                                   
       
    11  *                                                                       
       
    12  * Redistribution and use in source and binary forms, with or without    
       
    13  * modification, are permitted provided that the following conditions    
       
    14  * are met:                                                              
       
    15  *                                                                       
       
    16  *  * Redistributions of source code must retain the above copyright     
       
    17  *    notice, this list of conditions and the following disclaimer.      
       
    18  *  * Redistributions in binary form must reproduce the above copyright  
       
    19  *    notice, this list of conditions and the following disclaimer in    
       
    20  *    the documentation and/or other materials provided with the         
       
    21  *    distribution.                                                      
       
    22  *  * Neither the name Texas Instruments nor the names of its            
       
    23  *    contributors may be used to endorse or promote products derived    
       
    24  *    from this software without specific prior written permission.      
       
    25  *                                                                       
       
    26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   
       
    27  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     
       
    28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
       
    29  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  
       
    30  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
       
    31  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      
       
    32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
       
    33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
       
    34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   
       
    35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
       
    36  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    37  */
       
    38 
       
    39 /** \file GenSM.c
       
    40  *  \brief Generic state machine implementation
       
    41  *
       
    42  *  \see GenSM.h
       
    43  */
       
    44 
       
    45 
       
    46 #define __FILE_ID__  FILE_ID_128
       
    47 #include "tidef.h"
       
    48 #include "osApi.h"
       
    49 #include "report.h"
       
    50 #include "GenSM.h"
       
    51 
       
    52 /* generic state machine object structure */
       
    53 typedef struct
       
    54 {
       
    55     TI_HANDLE       hOS;               /**< OS handle */ 
       
    56     TI_HANDLE       hReport;           /**< report handle */ 
       
    57     TGenSM_matrix   tMatrix;           /**< next state/action matrix */
       
    58     TI_UINT32       uStateNum;         /**< Number of states in the matrix */
       
    59     TI_UINT32       uEventNum;         /**< Number of events in the matrix */
       
    60     TI_UINT32       uCurrentState;     /**< Current state */
       
    61     TI_UINT32       uEvent;            /**< Last event sent */
       
    62     void            *pData;            /**< Last event data */
       
    63     TI_BOOL         bEventPending;     /**< Event pending indicator */
       
    64     TI_BOOL         bInAction;         /**< Evenet execution indicator */
       
    65     TI_UINT32       uModuleLogIndex;   /**< Module index to use for printouts */
       
    66     TI_INT8         *pGenSMName;       /**< state machine name */
       
    67     TI_INT8         **pStateDesc;      /**< State description strings */
       
    68     TI_INT8         **pEventDesc;      /**< Event description strings */
       
    69 } TGenSM;
       
    70 
       
    71 
       
    72 /** 
       
    73  * \fn     genSM_Create 
       
    74  * \brief  Cerates a generic state machine object
       
    75  * 
       
    76  * Cerates a generic state machine object. Allocates system resources.
       
    77  * 
       
    78  * \note   event/action matrix and debug descriptions are used by reference, and are not copied! 
       
    79  * \param  hOS - handle to the OS object
       
    80  * \return Handle to the generic state machine object
       
    81  * \sa     GenSM_Unload, GenSM_Init, genSM_SetDefaults
       
    82  */
       
    83 TI_HANDLE genSM_Create (TI_HANDLE hOS)
       
    84 {
       
    85     TGenSM      *pGenSM = NULL;
       
    86 
       
    87     /* Allocate object storage */
       
    88     pGenSM = os_memoryAlloc (hOS, sizeof(TGenSM),MemoryNormal);
       
    89     if (NULL != pGenSM)
       
    90     {
       
    91         /* Store OS handle */
       
    92         pGenSM->hOS = hOS;
       
    93     }
       
    94 
       
    95     return (TI_HANDLE)pGenSM;
       
    96 }
       
    97 
       
    98 /** 
       
    99  * \fn     genSM_Unload 
       
   100  * \brief  Unloads a generic state machine object
       
   101  * 
       
   102  * Unloads a generic state machine object. Frees system resources consumed by the object.
       
   103  * 
       
   104  * \param  hGenSM - hanlde to the generic state machine object
       
   105  * \return None
       
   106  * \sa     GenSM_Create 
       
   107  */
       
   108 void genSM_Unload (TI_HANDLE hGenSM)
       
   109 {
       
   110     TGenSM      *pGenSM =       (TGenSM*)hGenSM;
       
   111 
       
   112     /* free the generic state machine object storage */
       
   113     os_memoryFree (pGenSM->hOS, hGenSM, sizeof (TGenSM));
       
   114 }
       
   115 
       
   116 /** 
       
   117  * \fn     genSM_Init 
       
   118  * \brief  Initializes the generic state machine object
       
   119  * 
       
   120  * Initializes the generic state machine object. Store handles to other modules.
       
   121  * 
       
   122  * \param  hGenSM - hanlde to the generic state machine object
       
   123  * \param  hReport - handle to the report module
       
   124  * \return None
       
   125  * \sa     GenSM_Create, genSM_SetDefaults
       
   126  */
       
   127 void genSM_Init (TI_HANDLE hGenSM, TI_HANDLE hReport)
       
   128 {
       
   129     TGenSM      *pGenSM =       (TGenSM*)hGenSM;
       
   130 
       
   131     /* store report handle */
       
   132     pGenSM->hReport = hReport;
       
   133 }
       
   134 
       
   135 /** 
       
   136  * \fn     genSM_SetDefaults 
       
   137  * \brief  Set default values to the generic state machine 
       
   138  * 
       
   139  * Set default values to the generic state machine
       
   140  * 
       
   141  * \note   event/action matrix and debug descriptions are used by reference, and are not copied! 
       
   142  * \param  hGenSM - hanlde to the generic state machine object
       
   143  * \param  uStateNum - number of states
       
   144  * \param  uEventNum - number of events
       
   145  * \param  pMatrix - pointer to the event/actions matrix
       
   146  * \param  uInitialState - the initial state
       
   147  * \param  pGenSMName - a string describing the state machine, for debug prints
       
   148  * \param  pStateDesc - strings describing the state machine states, for debug prints
       
   149  * \param  pEventDesc - strings describing the state machine events, for debug prints
       
   150  * \param  uModuleLogIndex - Log index used by the module using the state machine
       
   151  * \return None
       
   152  * \sa     genSM_Create, genSM_Init
       
   153  */
       
   154 void genSM_SetDefaults (TI_HANDLE hGenSM, TI_UINT32 uStateNum, TI_UINT32 uEventNum,
       
   155                         TGenSM_matrix pMatrix, TI_UINT32 uInitialState, TI_INT8 *pGenSMName, 
       
   156                         TI_INT8 **pStateDesc, TI_INT8 **pEventDesc, TI_UINT32 uModuleLogIndex)
       
   157 {
       
   158     TGenSM      *pGenSM =       (TGenSM*)hGenSM;
       
   159 
       
   160     /* set values */
       
   161     pGenSM->uStateNum       = uStateNum;
       
   162     pGenSM->uEventNum       = uEventNum;
       
   163     pGenSM->tMatrix         = pMatrix;
       
   164     pGenSM->uCurrentState   = uInitialState;
       
   165     pGenSM->pGenSMName      = pGenSMName;
       
   166     pGenSM->pStateDesc      = pStateDesc;
       
   167     pGenSM->pEventDesc      = pEventDesc;
       
   168     pGenSM->uModuleLogIndex = uModuleLogIndex;
       
   169     pGenSM->bEventPending   = TI_FALSE;
       
   170     pGenSM->bInAction       = TI_FALSE;
       
   171 }
       
   172 
       
   173 void genSM_Event (TI_HANDLE hGenSM, TI_UINT32 uEvent, void *pData)
       
   174 {
       
   175     TGenSM              *pGenSM =       (TGenSM*)hGenSM;
       
   176     TI_UINT32           uCurrentState;
       
   177     TGenSM_actionCell   *pCell;
       
   178 
       
   179 #ifdef TI_DBG
       
   180     /* sanity check */
       
   181     if (uEvent >= pGenSM->uEventNum)
       
   182     {
       
   183         TRACE3(pGenSM->hReport, REPORT_SEVERITY_ERROR , ": module: %d received event %d, which is out of events boundry %d\n", pGenSM->uModuleLogIndex, uEvent, pGenSM->uEventNum);
       
   184     }
       
   185     if (TI_TRUE == pGenSM->bEventPending)
       
   186     {
       
   187         TRACE3(pGenSM->hReport, REPORT_SEVERITY_ERROR , ": module: %d received event %d, when event %d is pending execution!\n", pGenSM->uModuleLogIndex, uEvent, pGenSM->uEvent);
       
   188     }
       
   189 #endif
       
   190 
       
   191     /* mark that an event is pending */
       
   192     pGenSM->bEventPending = TI_TRUE;
       
   193 
       
   194     /* save event and data */
       
   195     pGenSM->uEvent = uEvent;
       
   196     pGenSM->pData = pData;
       
   197 
       
   198     /* if an event is currently executing, return (new event will be handled when current event is done) */
       
   199     if (TI_TRUE == pGenSM->bInAction)
       
   200     {
       
   201         TRACE1(pGenSM->hReport, REPORT_SEVERITY_INFORMATION , ": module: %d delaying execution of event \n", pGenSM->uModuleLogIndex);
       
   202         return;
       
   203     }
       
   204 
       
   205     /* execute events, until none is pending */
       
   206     while (TI_TRUE == pGenSM->bEventPending)
       
   207     {
       
   208         /* get the cell pointer for the current state and event */
       
   209         pCell = &(pGenSM->tMatrix[ (pGenSM->uCurrentState * pGenSM->uEventNum) + pGenSM->uEvent ]);
       
   210         
       
   211 
       
   212         /* print state transition information */
       
   213 		TRACE4(pGenSM->hReport, REPORT_SEVERITY_INFORMATION, "genSM_Event: module %d <currentState = %d, event = %d> --> nextState = %d\n", pGenSM->uModuleLogIndex, pGenSM->uCurrentState, uEvent, pCell->uNextState);
       
   214 
       
   215         /* mark that event execution is in place */
       
   216         pGenSM->bInAction = TI_TRUE;
       
   217 
       
   218         /* mark that pending event is being handled */
       
   219         pGenSM->bEventPending = TI_FALSE;
       
   220         
       
   221         /* keep current state */
       
   222         uCurrentState = pGenSM->uCurrentState;
       
   223 
       
   224         /* update current state */
       
   225         pGenSM->uCurrentState = pCell->uNextState;
       
   226 
       
   227         /* run transition function */
       
   228         (*(pCell->fAction)) (pGenSM->pData);
       
   229 
       
   230         /* mark that event execution is complete */
       
   231         pGenSM->bInAction = TI_FALSE;
       
   232     }
       
   233 }
       
   234 
       
   235 /** 
       
   236  * \fn     genSM_GetCurrentState
       
   237  * \brief  retrieves the state machine current state
       
   238  * 
       
   239  * retrieves the state machine current state
       
   240  * 
       
   241  * \param  hGenSM - hanlde to the generic state machine object
       
   242  * \return state machine current state
       
   243  */
       
   244 TI_UINT32 genSM_GetCurrentState (TI_HANDLE hGenSM)
       
   245 {
       
   246     TGenSM              *pGenSM =       (TGenSM*)hGenSM;
       
   247 
       
   248     return pGenSM->uCurrentState;
       
   249 }