📄 hamaro_fsm.c
字号:
/* hamaro_fsm.c */
/*+++ *******************************************************************\
*
* Abstract:
*
* Hamaro FSM framework implementation file.
*
* Created: 07/15/2005
*
*
* Copyright and Disclaimer:
*
* ---------------------------------------------------------------
* This software is provided "AS IS" without warranty of any kind,
* either expressed or implied, including but not limited to the
* implied warranties of noninfringement, merchantability and/or
* fitness for a particular purpose.
* ---------------------------------------------------------------
*
* Author: Amar Puttur
*
* Module Revision Id:
*
* $Header:hamaro_fsm.c, 1, 7/15/2005 6:49:57 PM, Amarnath Puttur$
*
* Copyright (c) 2005 Conexant Systems, Inc.
* All rights reserved.
*
\******************************************************************* ---*/
#include "hamaro_fsm.h"
#if HAMARO_SCAN_THE_SKY
#include "hamaro_sts.h"
#if HAMARO_WIN_DEBUG
#define STSDBG_WR_INT_LN(str, val) (WINDBG_WriteIntegerLn((char*)(str), val))
#define STSDBG_WR_INT(str, val) (WINDBG_WriteInteger((char*)(str), val))
#define STSDBG_WR_HEX(str, val) (WINDBG_WriteHex((char*)(str), val))
#define STSDBG_WR_UNSIGNED_LN(str, val) (WINDBG_WriteUnsignedLn((char*)(str), val))
#define STSDBG_WR_UNSIGNED(str, val) (WINDBG_WriteUnsigned((char*)(str), val))
#define STSDBG_WR_STR(str, nl) (WINDBG_WriteString((char*)(str), nl))
#define STSDBG_WR_ELAPSED_TIME() (WINDBG_FileWriteDuration())
#define STSDBG_START_TIMER() (WINDBG_SetTimer()) /* used for time measurements */
#else // HAMARO_WIN_DEBUG
#define STSDBG_WR_INT_LN(str, val) /* Expands to nothing */
#define STSDBG_WR_INT(str, val) /* Expands to nothing */
#define STSDBG_WR_UNSIGNED_LN(str, val) /* Expands to nothing */
#define STSDBG_WR_UNSIGNED(str, val) /* Expands to nothing */
#define STSDBG_WR_STR(str, nl) /* Expands to nothing */
#define STSDBG_WR_ELAPSED_TIME() /* Expands to nothing */
#define STSDBG_START_TIMER() /* Expands to nothing */
#endif /* HAMARO_WIN_DEBUG */
/* Common state for all FSMs */
STATE idle_state = {0, {0, 0}, 0, 0, 0};
#if HAMARO_INCLUDE_DEBUG
static FSM_STATE_SEQUENCE_DATA fsm_sequence_data[STS_MAX_STATE_SEQUENCES+1];
/* Array indices - used to manage the data points and array limits */
static unsigned char sequence_index;
#endif /* HAMARO_INCLUDE_DEBUG */
/*****************************************************************************/
/* FSM_Init() */
/* */
/*****************************************************************************/
void
FSM_Init(FSM *fsm)
{
fsm->p_prev = &idle_state;
fsm->p_curr = &idle_state;
fsm->time_counter = 0;
}
/*****************************************************************************/
/* FSM_Poll() */
/* */
/*****************************************************************************/
void
FSM_Poll(FSM *fsm)
{
unsigned long tmp;
CRITERIA *curr_criterion;
/* update state coding */
tmp = *fsm->p_state_code;
tmp &= ~(fsm->state_code_mask);
tmp |= (fsm->p_curr->coding && fsm->state_code_mask);
*fsm->p_state_code = tmp;
/* Optimization: Exit state machine immediately in case it's not started yet. */
if (fsm->p_curr == &idle_state)
{
return;
}
if (fsm->p_prev != fsm->p_curr) /* state transition */
{
DBG_UPDATE_STATE_SEQUENCE(fsm->p_curr->coding);
fsm->time_counter = 0;
if (1 == fsm->p_curr->counter_type)
{
fsm->last_time_value = 0; /* TBD: Get system clock ticks in msec; */
}
if (fsm->p_curr->enter_task) /* execute the enter task once */
{
fsm->p_curr->enter_task();/*当前状态任务*/
}
fsm->p_prev = fsm->p_curr;
}
if (fsm->p_curr->regular_task) /* regular task run every poll *//*若状态机的状态未发生变化,则每次POLL都是在当前状态*/
{
fsm->p_curr->regular_task();/*常规任务*/
}
/* start executing criterion and corresponding exit tasks one by one */
curr_criterion = &fsm->p_curr->criteria[0];
while(curr_criterion->condition) /* criterion exist/valid */
{
if (curr_criterion->condition(fsm->time_counter)) /* criterion returns True */
{ /* execute the exit task to transit to some other state */
fsm->p_curr = curr_criterion->exit_task();
break;
}
++curr_criterion;
}
/* keep accumulating timer ticks (delta) */
/* This is optionally implemented when state transition criterions start using timeouts */
if (1 == fsm->p_curr->counter_type)
{
tmp = 0; /* TBD : Get system clock ticks in msec; */
fsm->time_counter += (tmp >= fsm->last_time_value ?
(unsigned long)(tmp-fsm->last_time_value) :
(unsigned long)0xFFFF-( unsigned long)(fsm->last_time_value-tmp));
fsm->last_time_value = tmp;
}
}
/*****************************************************************************/
/* FSM_CheckState() */
/* Checks if the given FSM is in the requested state */
/*****************************************************************************/
BOOL
FSM_CheckState(FSM *fsm, unsigned long state)
{
if (fsm == 0)
{
return (False);
}
if (fsm->p_curr->coding == state)
{
return (True);
}
else
{
return (False);
}
}
/*****************************************************************************/
/* D E B U G S E C T I O N */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -