码迷,mamicode.com
首页 > 其他好文 > 详细

基于状态机----状态表的小组件

时间:2018-05-11 23:38:43      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:warning   err   print   pst   eve   normal   ptr   init   work   

使用实例
   
static state_trans_t close2ready = {
    WIFI_ST_READY,   // next state
    __open      // action
};
static state_trans_t ready2work = {
    WIFI_ST_WORK,
    __start
};
static state_trans_t ready2close = {
    WIFI_ST_CLOSED,
    __close
};
static state_trans_t work2ready = {
    WIFI_ST_READY,
    __stop
};
static state_trans_t work2close = {
    WIFI_ST_CLOSED,
    __close
};
static state_trans_t ignore = {
    WIFI_ST_CLOSED,
    __close
};


static pstate_trans_t trans_tble[][WIFI_EV_MAX] = {
                  /*EV_OPEN*/  /*EV_START*/  /*EV_STOP*/  /*EV_CLOSE*/ 
/*ST_CLOSED*/   &close2ready,  NULL,          NULL,          NULL,
/*ST_READY*/    NULL,          &ready2work,   NULL,          &ready2close,
/*ST_WORK*/     NULL,          NULL,          &work2ready,   &work2close
};

  

typedef int (*state_action)(int state, int event); typedef struct state_trans{ int next_state; state_action action; } state_trans_t,*pstate_trans_t; typedef struct fsm { int curr_state; int event_max; pstate_trans_t *pptrans_tbl; ak_mutex_t mutex; }fsm_t; int akp_fsm_ctor(fsm_t *this, pstate_trans_t *pptrans_tbl,int event_max); int akp_fsm_dector(fsm_t *this); int akp_fsm_on_event(fsm_t *this, int event); int akp_fsm_get_curr_status(fsm_t *this); //.c int akp_fsm_ctor(fsm_t *this, pstate_trans_t *pptrans_tbl,int event_max) { this->curr_state = 0; this->event_max = event_max; this->pptrans_tbl = pptrans_tbl; ak_thread_mutex_init(&(this->mutex),NULL); } int akp_fsm_dector(fsm_t *this) { this->pptrans_tbl = NULL; ak_thread_mutex_destroy(&(this->mutex)); } int akp_fsm_on_event(fsm_t *this, int event) { int ret = 0; if(NULL == this->pptrans_tbl) { ak_print_error_ex("[state]: pptrans_tbl is null,pl ctor fsm\n"); return -1; } ak_thread_mutex_lock(&(this->mutex)); int state = this->curr_state; //ak_print_normal("[state]: max=%d state=%d event=%d tbl=0x%x\n",this->event_max,state,event,this->pptrans_tbl); pstate_trans_t ptrans = (*(this->pptrans_tbl+(this->event_max*state+event))); //ak_print_normal("ptrans %x\n",ptrans); if(NULL != ptrans) { this->curr_state = ptrans->next_state; ret = ptrans->action(state,event); // must after change state } // else ignore this case else{ ak_print_warning("[fsm]: ignore this case: curr state=%d,event=%d\n",this->curr_state,event); } ak_thread_mutex_unlock(&(this->mutex)); return ret; } int akp_fsm_get_curr_status(fsm_t *this) { return (this->curr_state); }

  

基于状态机----状态表的小组件

标签:warning   err   print   pst   eve   normal   ptr   init   work   

原文地址:https://www.cnblogs.com/mic-chen/p/9026315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!