#include "plt.h" static struct state_t ies1000_states[] = { { SMST_LAUNCH, "launch" }, { SMST_STDBY, "stdby" }, { SMST_STOP, "stop" }, { SMST_READY, "ready" }, { SMST_DHG, "dhg" }, { SMST_CHG, "chg" }, { SMST_OFFGRID, "offgrid" }, { SMST_ERR, "err" }, }; static struct err_t ies1000_errs[] = { {IES1000ERR_NONE, "none"}, // launch IES1000ERR_LAUNCH_COMMERR {IES1000ERR_LAUNCH_COMMERR, "launch, comm err"}, // err {IES1000ERR_ERR_COMMERR, "err, comm err"}, {IES1000ERR_ERR_PWRUP, "err, pwrup"}, // stdby {IES1000ERR_STDBY_COMMERR, "stdby, comm err"}, {IES1000ERR_STDBY_CHK_DEVAPS0_FAIL_AFTER_SET, "stdby, chk aps = 0 fail"}, {IES1000ERR_STDBY_CHK_STOP_FAIL_AFTER_SET, "stdby, chk stop fail"}, {IES1000ERR_STDBY_WAIT_STOP_TIMEOUT, "stdby, wait stop timeout"}, {IES1000ERR_STDBY_WAIT_RUN_TIMEOUT, "stdby, wait run state timeout"}, {IES1000ERR_STDBY_UNKOWN_RUNSTAT, "stdby, unknown run state"}, // stop {IES1000ERR_STOP_COMMERR, "stop, comm err"}, {IES1000ERR_STOP_NONE_STOP_DETECTED, "stop, none stop run state detected"}, {IES1000ERR_STOP_CHK_DEVAPS0_FAIL_AFTER_SET, "stop, chk dev aps = 0 fail"}, {IES1000ERR_STOP_WAIT_IDLE_TIMEOUT, "stop, wait idle timeout"}, {IES1000ERR_STOP_WAIT_RUNMOD_OFFGRID_TIMEOUT, "stop, wait run mod = offgrid timeout"}, // ready {IES1000ERR_READY_COMMERR, "ready, comm err"}, {IES1000ERR_READY_WAIT_STOP_TIMEOUT, "ready, wait stop timeout"}, {IES1000ERR_READY_WAIT_RUN4DHG_TIMEOUT, "ready, wait run state for dhg timeout"}, {IES1000ERR_READY_WAIT_AP4DHG_TIMEOUT, "ready, wait ap for dhg timeout"}, {IES1000ERR_READY_WAIT_RUN4CHG_TIMEOUT, "ready, wait run state for chg timeout"}, {IES1000ERR_READY_WAIT_AP4CHG_TIMEOUT, "ready, wait ap for chg tiemout"}, {IES1000ERR_READY_NONIDLE_DETECTED, "ready, non idle state detected"}, {IES1000ERR_READY_SET_DEVAPS_FAIL, "ready, set dev aps fail"}, {IES1000ERR_READY_SEND_STARTCMD_FAIL, "ready, send startcmd fail"}, {IES1000ERR_READY_WAIT_RUNMOD_OFFGRID_FOR_OFFGRID_TIMEOUT, "ready, wait run mode = offgrid for offgrid timeout"}, {IES1000ERR_READY_WAIT_IDLE_FOR_OFFGRID_TIMEOUT, "ready, wait idle for offgrid timeout"}, {IES1000ERR_READY_WAIT_TARGET_VOLTAGE_FOR_OFFGRID_TIMEOUT, "stop, wait target voltage for offgrid timeout"}, {IES1000ERR_READY_WAIT_RUNMOD_ONGRID_FOR_DHG_TIMEOUT, "ready, wait runmod = ongrid for dhg timeout"}, {IES1000ERR_READY_WAIT_RUNMOD_ONGRID_FOR_CHG_TIMEOUT, "ready, wait runmod = ongrid for chg timeout"}, // dhg {IES1000ERR_DHG_COMMERR, "dhg, comm err"}, {IES1000ERR_DHG_WAIT_DEVAPS0_TIMEOUT, "dhg, wait dev aps = 0 timeout"}, {IES1000ERR_DHG_WAIT_AP0_TIMEOUT, "dhg, wait ap = 0 timeout"}, {IES1000ERR_DHG_NONDHG_DETECTED, "dhg, non-dhg detected"}, {IES1000ERR_DHG_WAIT_DEVAPS0_FOR_READYCMD_TIMEOUT, "dhg, wait devaps=0 for readycmd timeout"}, {IES1000ERR_DHG_WAIT_AP0_FOR_READYCMD_TIMEOUT, "dhg, wait ap=0 for readycmd timeout"}, {IES1000ERR_DHG_WAIT_RUNSTATE_IDLE_FOR_READYCMD_TIMEOUT, "dhg, wait run state=idle for readycmd timeout"}, // chg {IES1000ERR_CHG_COMMERR, "chg, comm err"}, {IES1000ERR_CHG_WAIT_DEVAPS0_TIMEOUT, "chg, wait dev aps = 0 timeout"}, {IES1000ERR_CHG_WAIT_AP0_TIMEOUT, "chg, wait ap = 0 timeout"}, {IES1000ERR_CHG_NONCHG_DETECTED, "chg, non-chg detected"}, {IES1000ERR_CHG_WAIT_DEVAPS0_FOR_READYCMD_TIMEOUT, "chg, wait devaps=0 for readycmd timeout"}, {IES1000ERR_CHG_WAIT_AP0_FOR_READYCMD_TIMEOUT, "chg, wait ap=0 for readycmd timeout"}, {IES1000ERR_CHG_WAIT_RUNSTATE_IDLE_FOR_READYCMD_TIMEOUT, "chg, wait run state=idle for readycmd timeout"}, // offgrid {IES1000ERR_OFFGRID_ERRSTAT_DETECTED, "offgrid, err state detected"}, {IES1000ERR_OFFGRID_NON_DHGCHG_DETECTED, "offgrid, non dhg chg state detected"}, }; int ies1000_sm_init(int idx) { struct statemachine_t* sm = &ies1000[idx].sm; sm_reset_timing(sm, 10, 10); sm->states = ies1000_states; sm->state_nbr = sizeof(ies1000_states)/sizeof(struct state_t); sm->errs = ies1000_errs; sm->err_nbr = sizeof(ies1000_errs)/sizeof(struct err_t); sm_set_state( sm, SMST_LAUNCH, IES1000ERR_NONE ); return 0; } static void ies1000_sm_launch( int idx ) { struct ies1000_t* dev = &ies1000[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)); ies1000_reset_cmd(idx); ies1000_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)); ies1000_reset_cmd(idx); sm_set_step(sm, 10); }else if( sm_get_step(sm) == 10 ){ // wait cmd if( ies1000_get_cmd(idx) == CMD_SM_STDBY){ // stdby cmd log_dbg("%s, idx:%d, state:%s, step:%d, get stdby cmd, reset comm then chk", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_reset_cmd(idx); ies1000_comm_reset(idx); sm_set_step(sm, 20); } }*/else if( sm_get_step(sm) == 20 ){ if( comm_get_state(comm) == COMMST_NORMAL){ log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto stdby", __func__, idx,sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_STDBY, IES1000ERR_NONE); }else{ 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, IES1000ERR_LAUNCH_COMMERR); } } } static void ies1000_sm_err( int idx ) { struct ies1000_t* dev = &ies1000[idx]; struct comm_t* comm = &ies1000[idx].comm; struct statemachine_t* sm = &ies1000[idx].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)); ies1000_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( ies1000_get_cmd(idx) == CMD_SM_STDBY){ // stdby cmd log_dbg("%s, idx:%d, state:%s, step:%d, get stdby cmd, reset comm then chk", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_reset_cmd(idx); ies1000_comm_reset(idx); sm_set_step(sm, 20); }else if( comm_get_state(comm) != COMMST_NORMAL && (ts - ts_last_try > 60000) ){ // 60s log_dbg("%s, idx:%d, state:%s, step:%d,comm:%s,reset comm", __func__, idx, sm_get_szstate(sm), sm_get_step(sm),comm_get_state_str(comm)); ts_last_try = ts; ies1000_comm_reset(idx); } }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 stdby", __func__, idx,sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_STDBY, IES1000ERR_NONE); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, comm err detected, stay err", __func__, idx,sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_ERR_COMMERR); } } } static void ies1000_sm_stdby( int idx ) { struct ies1000_t* dev = &ies1000[idx]; struct comm_t* comm = &ies1000[idx].comm; struct statemachine_t* sm = &ies1000[idx].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, IES1000ERR_STDBY_COMMERR); return; } if( sm_get_step(sm) == 0 ){ /* entry */ ies1000_reset_cmd(idx); sm_set_step(sm, 10); }else if( sm_get_step(sm) == 10 ){ /* wait cmd */ if( ies1000_get_cmd(idx) == CMD_SM_STOP ){ /* stop cmd */ log_dbg("%s, idx:%d, state:%s, step:%d, get stop cmd, chk devaps = 0 ?",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_reset_cmd(idx); sm_set_step(sm, 20); } }else if( sm_get_step(sm) == 20 ){ /* chk devaps = 0 ? */ if( ies1000_get_dev_aps(idx) != 0 ){ log_dbg("%s, idx:%d, state:%s, step:%d, devaps != 0 detected, set it to 0, wait and chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_aps(idx, 0); sm_set_step(sm, 30); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, devaps = 0 detected, chk stop state",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_step(sm, 40); } }else if( sm_get_step(sm) == 30 ){ /* wait and chk devaps = 0 */ if( ies1000_get_dev_aps(idx) == 0 ){ log_dbg("%s, idx:%d, state:%s, step:%d, devaps = 0 detected, chk stop state",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_step(sm, 40); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, chk devaps = 0 fail after set, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_STDBY_CHK_DEVAPS0_FAIL_AFTER_SET); } }else if( sm_get_step(sm) == 40 ){ /* chk runstate = stop ? */ if( IES1000_RUNSTAT_STOP != ies1000_get_runstat(idx) ){ log_dbg("%s, idx:%d, state:%s, step:%d, non-stop run state detected, send stop cmd, wait and chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_stopcmd(idx); sm_set_step(sm, 50); sm_set_count(sm, 0); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, stop run state detected, goto stop", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_STOP, IES1000ERR_NONE); } }else if( sm_get_step(sm) == 50 ){ /* wait run state = stop after stopcmd*/ if( IES1000_RUNSTAT_STOP == ies1000_get_runstat(idx) ){ log_dbg("%s, idx:%d, state:%s, step:%d, stop run state detected, goto stop",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_STOP, IES1000ERR_NONE); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100){ log_dbg("%s, idx:%d, state:%s, step:%d, wait stop run state timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_STDBY_WAIT_STOP_TIMEOUT); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting stop run state, count:%d",__func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } } } static void ies1000_sm_offgrid( int idx ) { struct ies1000_t* dev = &ies1000[idx]; struct comm_t* comm = &ies1000[idx].comm; struct statemachine_t* sm = &ies1000[idx].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, IES1000ERR_STOP_COMMERR); return; } if( sm_get_step(sm) == 0 ){ // entry ies1000_reset_cmd(idx); sm_set_step(sm, 10); }else if( sm_get_step(sm) == 10 ){ // wait cmd and chk if( ies1000_get_cmd(idx) == CMD_SM_READY ){ // ready cmd log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, set dev idlecmd, goto ready",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_reset_cmd(idx); ies1000_set_dev_idlecmd(idx); sm_set_state(sm, SMST_READY, IES1000ERR_NONE); }else if( ies1000_get_errstat(idx) != 0){ log_dbg("%s, idx:%d, state:%s, step:%d, err state detected, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_OFFGRID_ERRSTAT_DETECTED); }else if( ies1000_get_runstat(idx) != IES1000_RUNSTAT_CHG && ies1000_get_runstat(idx) != IES1000_RUNSTAT_DHG){ log_dbg("%s, idx:%d, state:%s, step:%d, non dhg chg run state detected, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_OFFGRID_NON_DHGCHG_DETECTED); }else{ } } } static void ies1000_sm_stop( int idx ) { struct ies1000_t* dev = &ies1000[idx]; struct comm_t* comm = &ies1000[idx].comm; struct statemachine_t* sm = &ies1000[idx].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, IES1000ERR_STOP_COMMERR); return; } if( sm_get_step(sm) == 0 ){ // entry ies1000_reset_cmd(idx); ies1000_set_aps(idx, 0); sm_set_step(sm, 10); }else if( sm_get_step(sm) == 10 ){ // wait cmd and chk if( ies1000_get_cmd(idx) == CMD_SM_STDBY ){ // stdby cmd log_dbg("%s, idx:%d, state:%s, step:%d, get stdby cmd, goto stdby",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_reset_cmd(idx); sm_set_state(sm, SMST_STDBY, IES1000ERR_NONE); }else if( ies1000_get_cmd(idx) == CMD_SM_READY ){ // ready cmd log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, set dev resetcmd, chk dev aps",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_reset_cmd(idx); sm_set_step(sm, 20); ies1000_set_dev_resetcmd(idx); }else{ /* no cmd, do chking */ if( IES1000_RUNSTAT_STOP != ies1000_get_runstat(idx) ){ log_dbg("%s, idx:%d, state:%s, step:%d, run state != stop detected, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_STOP_NONE_STOP_DETECTED); } } }else if( sm_get_step(sm) == 20 ){ /* chk devaps = 0 ? */ if( ies1000_get_dev_aps(idx) != 0 ){ log_dbg("%s, idx:%d, state:%s, step:%d, dev aps != 0 detected, set it to 0, then chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_aps(idx, 0); sm_set_step(sm, 30); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, dev aps = 0 detected, set dev runmod = offgrid, wait and chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_runmod(idx, IES1000_RUNMOD_OFFGRID); sm_set_step(sm, 40); } }else if( sm_get_step(sm) == 30 ){ /* wait and chk devaps = 0 */ if( ies1000_get_dev_aps(idx) == 0 ){ log_dbg("%s, idx:%d, state:%s, step:%d, dev aps = 0 detected",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_step(sm, 40); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, chk dev aps = 0 fail, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_STOP_CHK_DEVAPS0_FAIL_AFTER_SET); } }else if( sm_get_step(sm) == 40 ){ // wait pcs runmod = offgrid if( IES1000_RUNMOD_OFFGRID == ies1000_get_runmod(idx) ){ log_dbg("%s, idx:%d, state:%s, step:%d, chk pcs runmod = offgrid ok, set dev idle, wait and chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_idlecmd(idx); sm_set_step(sm, 41); sm_set_count(sm, 0); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 50 ){ //2.5s sm_set_state(sm, SMST_ERR, IES1000ERR_STOP_WAIT_RUNMOD_OFFGRID_TIMEOUT); log_dbg("%s, idx:%d, state:%s, step:%d, wait run mode = offgrid timeout, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting run mode = offgrid, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 41 ){ // wait pcs idle if( IES1000_RUNSTAT_IDLE == ies1000_get_runstat(idx) ){ log_dbg("%s, idx:%d, state:%s, step:%d, chk pcs idle ok, goto ready",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_READY, IES1000ERR_NONE); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ /* 5s */ sm_set_state(sm, SMST_ERR, IES1000ERR_STOP_WAIT_IDLE_TIMEOUT); log_dbg("%s, idx:%d, state:%s, step:%d, wait idle run state timeout, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting idle run state, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } } } static void ies1000_sm_ready( int idx ) { struct ies1000_t* dev = &ies1000[idx]; struct comm_t* comm = &ies1000[idx].comm; struct statemachine_t* sm = &ies1000[idx].sm; int aps = ies1000_get_aps(idx); int ap = ies1000_get_ap(idx); /* 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, IES1000ERR_READY_COMMERR); return; } 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)); ies1000_reset_cmd(idx); ies1000_reset_aps(idx); sm_set_step(sm, 10); }else if( sm_get_step(sm) == 10 ){ // wait and chk if( ies1000_get_cmd(idx) == CMD_SM_STOP){ // stop cmd log_dbg("%s, idx:%d, state:%s, step:%d, get stop cmd, dev stop cmd sent, wait and chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_reset_cmd(idx); ies1000_set_dev_stopcmd(idx); sm_set_step(sm, 20); sm_set_count(sm, 0); }else if( aps > 0 ){ // new aps, prepare to dhg log_dbg("%s, idx:%d, state:%s, step:%d, aps > 0 detected, %d, set dev run mode = ongrid, wait and chk", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), aps); ies1000_set_dev_runmod(idx, IES1000_RUNMOD_ONGRID); sm_set_step(sm, 30); sm_set_count(sm, 0); }else if( aps < 0 ){ // new aps, prepare to chg log_dbg("%s, idx:%d, state:%s, step:%d, aps < 0 detected, %d, set dev run mode = ongrid, wait and chk", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), aps); ies1000_set_dev_runmod(idx, IES1000_RUNMOD_ONGRID); sm_set_step(sm, 40); sm_set_count(sm, 0); }else if( ies1000_get_cmd(idx) == CMD_SM_OFFGRID ){ // offgrid cmd log_dbg("%s, idx:%d, state:%s, step:%d, get offgrid cmd, set dev run mode = offgrid, wait and chk",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_reset_cmd(idx); ies1000_set_dev_runmod(idx, IES1000_RUNMOD_OFFGRID); sm_set_step(sm, 500); sm_set_count(sm, 0); }else if( ies1000_get_runstat(idx) != IES1000_RUNSTAT_IDLE){ log_dbg("%s, idx:%d, state:%s, step:%d, run state != idle detected, %d, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ies1000_get_runstat(idx)); sm_set_state(sm, SMST_ERR, IES1000ERR_READY_NONIDLE_DETECTED); } }else if( sm_get_step(sm) == 20){ /* wait and chk run state = stop */ if( ies1000_get_runstat(idx) == IES1000_RUNSTAT_STOP ){ log_dbg("%s, idx:%d, state:%s, step:%d, dev run state = stop detected, goto stop", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_STOP, IES1000ERR_NONE); }else{ sm_inc_count(sm); if(sm_get_count(sm) >= 100){ log_dbg("%s, idx:%d, state:%s, step:%d, wait dev run state = stop timeout, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_READY_WAIT_STOP_TIMEOUT); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting dev run state = stop, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 30 ){ // chk run mode = ongrid for dhg if( ies1000_get_runmod(idx) == IES1000_RUNMOD_ONGRID ){ /* chk ok */ log_dbg("%s, idx:%d, state:%s, step:%d, chk run mode = ongrid ok, set dev aps, wait and chk to dhg", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); if( ies1000_set_dev_aps(idx, aps) == 0 ){ log_dbg("%s, idx:%d, state:%s, step:%d, set dev aps ok", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); if( ies1000_set_dev_startcmd(idx) == 0 ){ log_dbg("%s, idx:%d, state:%s, step:%d, set dev startcmd ok, wait and chk run state = dhg", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_step(sm, 31); sm_set_count(sm, 0); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, set dev startcmd fail, stopcmd sent, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_stopcmd(idx); sm_set_state(sm, SMST_ERR, IES1000ERR_READY_SEND_STARTCMD_FAIL); } }else{ log_dbg("%s, idx:%d, state:%s, step:%d, set dev aps fail, stopcmd sent, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_stopcmd(idx); sm_set_state(sm, SMST_ERR, IES1000ERR_READY_SET_DEVAPS_FAIL); } }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 50 ){ log_dbg("%s, idx:%d, state:%s, step:%d, wait run mode = ongrid for dhg timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_READY_WAIT_RUNMOD_ONGRID_FOR_DHG_TIMEOUT); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting run mode = ongrid for dhg, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 31 ){ // chk run state = dhg if( ies1000_get_runstat(idx) == IES1000_RUNSTAT_DHG ){ /* chk ok */ log_dbg("%s, idx:%d, state:%s, step:%d, chk runstate = dhg ok, chk ap", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_step(sm, 32); sm_set_count(sm, 0); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ log_dbg("%s, idx:%d, state:%s, step:%d, wait run state for dhg timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_READY_WAIT_RUN4DHG_TIMEOUT); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting run state for dhg, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 32 ){ // chk ap > 0 if( ap > 0 ){ /* chk ok */ log_dbg("%s, idx:%d, state:%s, step:%d, ap > 0 detected, %d, goto dhg", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap); sm_set_state(sm, SMST_DHG, IES1000ERR_NONE); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ sm_set_state(sm, SMST_ERR, IES1000ERR_READY_WAIT_AP4DHG_TIMEOUT); log_dbg("%s, idx:%d, state:%s, step:%d, wait ap > 0 for dhg timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting ap > 0 for dhg, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 40 ){ // chk run mode = ongrid for chg if( ies1000_get_runmod(idx) == IES1000_RUNMOD_ONGRID ){ /* chk ok */ log_dbg("%s, idx:%d, state:%s, step:%d, run mode = ongrid detected, set dev aps, wait and chk to chg", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); if( ies1000_set_dev_aps(idx, aps) == 0 ){ log_dbg("%s, idx:%d, state:%s, step:%d, set dev aps ok", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); if( ies1000_set_dev_startcmd(idx) == 0 ){ log_dbg("%s, idx:%d, state:%s, step:%d, set dev startcmd ok, wait and chk run state = chg", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_step(sm, 41); sm_set_count(sm, 0); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, set dev startcmd fail, stopcmd sent, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_stopcmd(idx); sm_set_state(sm, SMST_ERR, IES1000ERR_READY_SEND_STARTCMD_FAIL); } }else{ log_dbg("%s, idx:%d, state:%s, step:%d, set dev aps fail, stopcmd sent, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_stopcmd(idx); sm_set_state(sm, SMST_ERR, IES1000ERR_READY_SET_DEVAPS_FAIL); } }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 50 ){ log_dbg("%s, idx:%d, state:%s, step:%d, wait run mode = ongrid for chg timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_READY_WAIT_RUNMOD_ONGRID_FOR_CHG_TIMEOUT); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting run mode = ongrid for chg, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 41 ){ // chk run state = chg if( ies1000_get_runstat(idx) == IES1000_RUNSTAT_CHG ){ // chk ok log_dbg("%s, idx:%d, state:%s, step:%d, dev run state = chg detected, chk ap", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_step(sm, 42); sm_set_count(sm, 0); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ log_dbg("%s, idx:%d, state:%s, step:%d, wait run state for chg timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_READY_WAIT_RUN4CHG_TIMEOUT); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting run state for chg, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 42 ){ // chk ap for chg if( ap < 0 ){ /* chk ok */ log_dbg("%s, idx:%d, state:%s, step:%d, ap = %d < 0 detected, goto chg", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap); sm_set_state(sm, SMST_CHG, IES1000ERR_NONE); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ sm_set_state(sm, SMST_ERR, IES1000ERR_READY_WAIT_AP4CHG_TIMEOUT); log_dbg("%s, idx:%d, state:%s, step:%d, wait ap < 0 for chg timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting ap < 0 for chg, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 500 ){ // wait run mode = offgrid for offgrid if( IES1000_RUNMOD_OFFGRID == ies1000_get_runmod(idx) ){ log_dbg("%s, idx:%d, state:%s, step:%d, chk run mode = offgrid ok, set dev startcmd, wait and chk voltage",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_startcmd(idx); sm_set_step(sm, 501); sm_set_count(sm, 0); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ // 5s sm_set_state(sm, SMST_ERR, IES1000ERR_READY_WAIT_RUNMOD_OFFGRID_FOR_OFFGRID_TIMEOUT); log_dbg("%s, idx:%d, state:%s, step:%d, wait run mode = offgrid timeout, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting run mode = offgrid, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 501 ){ // chk voltage for offgrid if( ies1000_get_ua(idx) > 340.0 && ies1000_get_ua(idx) < 420.0){ log_dbg("%s, idx:%d, state:%s, step:%d, chk voltage ok, %.1f, goto offgrid",__func__, idx, sm_get_szstate(sm), sm_get_step(sm), ies1000_get_ua(idx)); sm_set_state(sm, SMST_OFFGRID, IES1000ERR_NONE); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 300 ){ // 15s log_dbg("%s, idx:%d, state:%s, step:%d, wait voltage to 380 for offgrid timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_READY_WAIT_TARGET_VOLTAGE_FOR_OFFGRID_TIMEOUT); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, wait target voltage for offgrid, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } } } static void ies1000_sm_dhg( int idx ) { struct ies1000_t* dev = &ies1000[idx]; struct comm_t* comm = &ies1000[idx].comm; struct statemachine_t* sm = &ies1000[idx].sm; int aps = ies1000_get_aps(idx); int last_aps = ies1000_get_last_aps(idx); int ap = ies1000_get_ap(idx); /* 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, IES1000ERR_DHG_COMMERR); return; } 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)); sm_set_step(sm, 10); ies1000_reset_bsytikchk(idx); }else if( sm_get_step(sm) == 10 ){ // wait and chk if( ies1000_get_cmd(idx) == CMD_SM_READY ){ // ready cmd log_dbg("%s, idx:%d, state:%s, step:%d, got ready cmd, set dev aps = 0, wait and chk", __func__,idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_reset_cmd(idx); ies1000_set_dev_aps(idx, 0); sm_set_step(sm, 30); sm_set_count(sm, 0); }else if( aps <= 0 ){ // aps <= 0 log_dbg("%s, idx:%d, state:%s, step:%d, aps <= 0 detected, %d, set dev aps = 0, wait and chk", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), aps); ies1000_set_dev_aps(idx, 0); sm_set_step(sm, 30); sm_set_count(sm, 0); }else if( ies1000_is_aps_changed(idx) ){ // new aps, set devaps and chk log_dbg("%s, idx:%d, state:%s, step:%d, new aps detected, %d -> %d, set dev aps", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), last_aps, aps); ies1000_set_aps(idx, aps); // update last aps ies1000_set_dev_aps(idx, aps); }else if( ies1000_is_bsytikchk_timeout(idx) ){ log_dbg("%s, idx:%d, state:%s, step:%d, bsytik timeout detected, set dev aps to 0", __func__,idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_aps(idx, 0); }else if( ies1000_get_runstat(idx) != IES1000_RUNSTAT_DHG ){ log_dbg("%s, idx:%d, state:%s, step:%d, run state != dhg detected, set dev aps to 0, goto err", __func__,idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_aps(idx, 0); sm_set_state(sm, SMST_ERR, IES1000ERR_DHG_NONDHG_DETECTED); } }else if( sm_get_step(sm) == 30 ){ // chk dev aps = 0 for readycmd if( ies1000_get_dev_aps(idx) == 0 ){ log_dbg("%s, idx:%d, state:%s, step:%d, chk dev aps = 0 ok, wait and chk ap", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_step(sm, 31); sm_set_count(sm, 0); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ sm_set_state(sm, SMST_ERR, IES1000ERR_DHG_WAIT_DEVAPS0_FOR_READYCMD_TIMEOUT); log_dbg("%s, idx:%d, state:%s, step:%d, wait dev aps = 0 for readycmd timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting dev aps = 0 for readycmd, count: %d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 31 ){ // chk ap = 0 for readycmd if( ap < 2 && ap > -2 ){ log_dbg("%s, idx:%d, state:%s, step:%d, chk ap ok for readycmd, %d, idlecmd sent, wait and chk run state = idle", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap); ies1000_set_dev_idlecmd(idx); sm_set_step(sm, 32); sm_set_count(sm, 0); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ log_dbg("%s, idx:%d, state:%s, step:%d, wait ap = 0 for readycmd timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_DHG_WAIT_AP0_FOR_READYCMD_TIMEOUT); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting ap = 0 for readycmd, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 32 ){ // wait run state = idle for readycmd if( ies1000_get_runstat(idx) == IES1000_RUNSTAT_IDLE ){ log_dbg("%s, idx:%d, state:%s, step:%d, chk run state = idle ok, goto ready", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_READY, IES1000ERR_NONE); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ log_dbg("%s, idx:%d, state:%s, step:%d, wait run state = idle timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_DHG_WAIT_RUNSTATE_IDLE_FOR_READYCMD_TIMEOUT); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting run state = idle for readycmd, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } } } static void ies1000_sm_chg( int idx ) { struct ies1000_t* dev = &ies1000[idx]; struct comm_t* comm = &ies1000[idx].comm; struct statemachine_t* sm = &ies1000[idx].sm; int aps = ies1000_get_aps(idx); int last_aps = ies1000_get_last_aps(idx); int ap = ies1000_get_ap(idx); /* 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, IES1000ERR_CHG_COMMERR); return; } 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)); sm_set_step(sm, 10); ies1000_reset_bsytikchk(idx); }else if( sm_get_step(sm) == 10 ){ // wait and chk if( ies1000_get_cmd(idx) == CMD_SM_READY ){ log_dbg("%s, idx:%d, state:%s, step:%d, got ready cmd, set dev aps = 0", __func__,idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_reset_cmd(idx); ies1000_set_dev_aps(idx, 0); sm_set_step(sm, 30); sm_set_count(sm, 0); }else if( aps >= 0 ){ // aps => 0 log_dbg("%s, idx:%d, state:%s, step:%d, aps >= 0 detected, %d, set dev aps = 0, wait and chk", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), aps); ies1000_set_dev_aps(idx, 0); sm_set_step(sm, 30); sm_set_count(sm, 0); }else if( ies1000_is_aps_changed(idx) ){ /* new aps, set devaps and chk */ log_dbg("%s, idx:%d, state:%s, step:%d, new aps detected, %d -> %d, set dev aps", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), last_aps, aps); ies1000_set_aps(idx, aps); // update last aps ies1000_set_dev_aps(idx, aps); }else if( ies1000_is_bsytikchk_timeout(idx) ){ log_dbg("%s, idx:%d, state:%s, step:%d, bsytik timeout detected, set dev aps to 0", __func__,idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_aps(idx, 0); }else if( ies1000_get_runstat(idx) != IES1000_RUNSTAT_CHG ){ log_dbg("%s, idx:%d, state:%s, step:%d, run state != chg detected, set dev aps to 0, goto err", __func__,idx, sm_get_szstate(sm), sm_get_step(sm)); ies1000_set_dev_aps(idx, 0); sm_set_state(sm, SMST_ERR, IES1000ERR_CHG_NONCHG_DETECTED); } }else if( sm_get_step(sm) == 30 ){ // chk dev aps = 0 for readycmd if( ies1000_get_dev_aps(idx) == 0 ){ log_dbg("%s, idx:%d, state:%s, step:%d, chk dev aps = 0 ok, wait and chk ap", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_step(sm, 31); sm_set_count(sm, 0); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ sm_set_state(sm, SMST_ERR, IES1000ERR_CHG_WAIT_DEVAPS0_FOR_READYCMD_TIMEOUT); log_dbg("%s, idx:%d, state:%s, step:%d, wait dev aps = 0 for readycmd timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting dev aps = 0 for readycmd, count: %d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 31 ){ // chk ap = 0 for readycmd if( ap < 2 && ap > -2 ){ log_dbg("%s, idx:%d, state:%s, step:%d, chk ap ok for readycmd, %d, idlecmd sent, wait and chk run state = idle", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), ap); ies1000_set_dev_idlecmd(idx); sm_set_step(sm, 32); sm_set_count(sm, 0); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ log_dbg("%s, idx:%d, state:%s, step:%d, wait ap = 0 for readycmd timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_CHG_WAIT_AP0_FOR_READYCMD_TIMEOUT); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting ap = 0 for readycmd, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } }else if( sm_get_step(sm) == 32 ){ // wait run state = idle for readycmd if( ies1000_get_runstat(idx) == IES1000_RUNSTAT_IDLE ){ log_dbg("%s, idx:%d, state:%s, step:%d, chk run state = idle ok, goto ready", __func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_READY, IES1000ERR_NONE); }else{ sm_inc_count(sm); if( sm_get_count(sm) >= 100 ){ log_dbg("%s, idx:%d, state:%s, step:%d, wait run state = idle timeout, goto err",__func__, idx, sm_get_szstate(sm), sm_get_step(sm)); sm_set_state(sm, SMST_ERR, IES1000ERR_CHG_WAIT_RUNSTATE_IDLE_FOR_READYCMD_TIMEOUT); }else{ log_dbg("%s, idx:%d, state:%s, step:%d, waiting run state = idle for readycmd, count:%d", __func__, idx, sm_get_szstate(sm), sm_get_step(sm), sm_get_count(sm)); } } } } void ies1000_sm( int idx ) { struct ies1000_t* dev = &ies1000[idx]; struct statemachine_t* sm = &dev->sm; sm_cal_timing(&dev->sm); switch( sm_get_state( sm ) ){ case SMST_LAUNCH: ies1000_sm_launch( idx ); break; case SMST_STDBY: ies1000_sm_stdby( idx ); break; case SMST_STOP: ies1000_sm_stop( idx ); break; case SMST_OFFGRID: ies1000_sm_offgrid( idx ); break; case SMST_READY: ies1000_sm_ready( idx ); break; case SMST_DHG: ies1000_sm_dhg( idx ); break; case SMST_CHG: ies1000_sm_chg( idx ); break; case SMST_ERR: ies1000_sm_err( idx ); break; default: log_dbg("%s, unknown state, %d",__func__, ies1000_get_state( idx )); break; } }