#include "plt.h" static struct state_t sdcs_states[] = { { SMST_LAUNCH, "launch" }, { SMST_READY, "ready" }, { SMST_ERR, "err" }, }; static struct err_t sdcs_errs[] = { { SDCSERR_NONE, "none" }, // launch { SDCSERR_LAUNCH_COMMERR, "launch, comm err" }, // err { SDCSERR_ERR_COMMERR, "err, comm err" }, // ready { SDCSERR_READY_COMMERR, "ready, comm err" }, }; static void sdcs_sm_launch( int idx ) { struct sdcs_t* dev = &sdcs[idx]; struct comm_t* comm = &dev->comm; struct statemachine_t* sm = &dev->sm; if( sm_get_step(sm) == 0 ){ // entry log_dbg("%s, idx:%d, state:%s, step:%d, entry", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sdcs_reset_cmd(idx); sdcs_comm_reset(idx); sm_set_step(sm, 20); } /* if( sm_get_step(sm) == 0 ){ // entry log_dbg("%s, idx:%d, state:%s, step:%d, entry", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sdcs_reset_cmd(idx); sm_set_step(sm, 10); }else if( sm_get_step(sm) == 10){ // wait cmd if( sdcs_get_cmd(idx) == CMD_SM_READY){ log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, chk comm state", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sdcs_reset_cmd(idx); sdcs_comm_reset(idx); sm_set_step(sm, 20); } }*/else if( sm_get_step(sm) == 20){ // chk comm state if( comm_get_state(comm) == COMMST_NORMAL){ log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto ready", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_READY, SDCSERR_NONE); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, comm err, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, SDCSERR_LAUNCH_COMMERR); } } } static void sdcs_sm_err( int idx ) { struct sdcs_t* dev = &sdcs[idx]; struct comm_t* comm = &dev->comm; struct statemachine_t* sm = &dev->sm; static double ts_last_try; double ts; if( sm_get_step(sm) == 0 ){ // entry log_dbg("%s, idx:%d, state:%s, step:%d, entry", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sdcs_reset_cmd(idx); sm_set_step(sm, 10); ts_last_try = sm_get_timeofday(); }else if( sm_get_step(sm) == 10){ // wait cmd ts = sm_get_timeofday(); if( sdcs_get_cmd(idx) == CMD_SM_READY){ log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, try to comm", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sdcs_reset_cmd(idx); sdcs_comm_reset(idx); sm_set_step(sm, 20); }else if( ts - ts_last_try > 60000 ){ // 60s ts_last_try = ts; sdcs_comm_reset(idx); sm_set_step(sm, 20); } }else if( sm_get_step(sm) == 20){ // chk comm state if( comm_get_state(comm) == COMMST_NORMAL){ log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto ready", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_READY, SDCSERR_NONE); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, comm err, stay err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, SDCSERR_ERR_COMMERR); } } } static void sdcs_sm_ready( int idx ) { struct sdcs_t* dev = &sdcs[idx]; struct comm_t* comm = &dev->comm; struct statemachine_t* sm = &dev->sm; /* chk comm state */ if( comm_get_state(comm) != COMMST_NORMAL){ log_dbg("%s, idx:%d, state:%s, step:%d, comm err detected, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, SDCSERR_READY_COMMERR); return; } } void sdcs_sm( int idx ) { struct sdcs_t* dev = &sdcs[idx]; struct statemachine_t* sm = &dev->sm; sm_cal_timing(sm); switch(sm_get_state( sm )) { case SMST_LAUNCH: sdcs_sm_launch( idx ); break; case SMST_READY: sdcs_sm_ready( idx ); break; case SMST_ERR: sdcs_sm_err( idx ); break; default: log_dbg("%s, never reach here",__func__); break; } } int sdcs_sm_init(int idx) { struct statemachine_t* sm = &sdcs[idx].sm; sm_reset_timing(sm, 1, 1); sm->states = sdcs_states; sm->state_nbr = sizeof(sdcs_states)/sizeof(struct state_t); sm->errs = sdcs_errs; sm->err_nbr = sizeof(sdcs_errs)/sizeof(struct err_t); sm_set_state( sm, SMST_LAUNCH, SDCSERR_NONE ); return 0; }