123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829 |
- #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;
- }
- }
|