123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- #include "plt.h"
- #include "dlt645_sm.h"
- static struct state_t dlt645_states[] = {
- { SMST_LAUNCH, "launch" },
- { SMST_READY, "ready" },
- { SMST_ERR, "err" },
- };
- static struct err_t dlt645_errs[] = {
- { DLT645ERR_NONE, "none" },
- //
- { DLT645ERR_ERR_COMMERR, "err comm err" },
- // ready
- { DLT645ERR_READY_COMMERR, "ready comm err" },
- };
- static void dlt645_sm_launch( int idx )
- {
- struct dlt645_t* dev = &dlt645[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));
- dlt645_reset_cmd(idx);
- dlt645_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));
- dlt645_reset_cmd(idx);
- sm_set_step(sm, 10);
- }else if( sm_get_step(sm) == 10 ){ // wait cmd
- if( dlt645_get_cmd(idx) == CMD_SM_READY){ // ready cmd
- 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));
- dlt645_reset_cmd(idx);
- dlt645_comm_reset(idx);
- sm_set_step(sm, 20);
- }else if( dlt645_get_cmd(idx) == CMD_SM_ACK){
- log_dbg("%s, idx:%d, state:%s, step:%d, get ack cmd", __func__,idx, sm_get_szstate(sm), sm_get_step(sm));
- dlt645_reset_cmd(idx);
- dlt645_read_com_ae(1);
- dlt645_read_pos_ae(1);
- dlt645_read_neg_ae(1);
- dlt645_read_com_ap(1);
- dlt645_read_ua(1);
- dlt645_read_ub(1);
- dlt645_read_uc(1);
- dlt645_read_ia(1);
- dlt645_read_ib(1);
- dlt645_read_ic(1);
- }
- }*/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, DLT645ERR_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, DLT645ERR_LAUNCH_COMMERR);
- }
- }
- }
- static void dlt645_sm_err( int idx )
- {
- struct dlt645_t* dev = &dlt645[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
- dlt645_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( dlt645_get_cmd(idx) == CMD_SM_READY){ // ready cmd
- 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));
- dlt645_reset_cmd(idx);
- dlt645_comm_reset(idx);
- sm_set_step(sm, 20);
- }else if( dlt645_get_cmd(idx) == CMD_SM_ACK){
- log_dbg("%s, idx:%d, state:%s, step:%d, get ack cmd",
- __func__,idx, sm_get_szstate(sm), sm_get_step(sm));
- dlt645_reset_cmd(idx);
- dlt645_read_com_ae(1);
- dlt645_read_pos_ae(1);
- dlt645_read_neg_ae(1);
- dlt645_read_com_ap(1);
- dlt645_read_ua(1);
- dlt645_read_ub(1);
- dlt645_read_uc(1);
- dlt645_read_ia(1);
- dlt645_read_ib(1);
- dlt645_read_ic(1);
- }else if( ts - ts_last_try > 60000 ){ // 60s
- ts_last_try = ts;
- dlt645_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, DLT645ERR_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, DLT645ERR_ERR_COMMERR);
- }
- }
- }
- static void dlt645_sm_ready( int idx )
- {
- struct dlt645_t* dev = &dlt645[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, DLT645ERR_READY_COMMERR);
- return;
- }
- }
- void dlt645_sm( int idx )
- {
- struct dlt645_t* dev = &dlt645[idx];
- struct statemachine_t* sm = &dev->sm;
- sm_cal_timing(sm);
- switch(sm_get_state( sm )){
- case SMST_LAUNCH:
- dlt645_sm_launch( idx );
- break;
- case SMST_READY:
- dlt645_sm_ready( idx );
- break;
- case SMST_ERR:
- dlt645_sm_err( idx );
- break;
- default:
- log_dbg("%s, never reach here",__func__);
- break;
- }
- }
- int dlt645_sm_init(int idx)
- {
- struct statemachine_t* sm = &dlt645[idx].sm;
- sm_reset_timing(sm, 100, 100);
- sm->states = dlt645_states;
- sm->state_nbr = sizeof(dlt645_states)/sizeof(struct state_t);
- sm->errs = dlt645_errs;
- sm->err_nbr = sizeof(dlt645_errs)/sizeof(struct err_t);
- sm_set_state( sm, SMST_LAUNCH, DLT645ERR_NONE );
-
- return 0;
- }
|