mc90hdnc1a_sm.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "plt.h"
  2. static struct state_t mc90hdnc1a_states[] = {
  3. { SMST_LAUNCH, "launch" },
  4. { SMST_READY, "ready" },
  5. { SMST_ERR, "err" },
  6. };
  7. static struct err_t mc90hdnc1a_errs[] = {
  8. { MC90HDNC1AERR_NONE, "none" },
  9. // launch
  10. { MC90HDNC1AERR_LAUNCH_COMMERR, "launch, comm err" },
  11. // err
  12. { MC90HDNC1AERR_ERR_COMMERR, "err comm err" },
  13. // ready
  14. { MC90HDNC1AERR_READY_COMMERR, "ready comm err" },
  15. };
  16. int mc90hdnc1a_sm_init(int idx)
  17. {
  18. struct statemachine_t* sm = &mc90hdnc1a[idx].sm;
  19. sm_reset_timing(sm, 1, 1);
  20. sm->states = mc90hdnc1a_states;
  21. sm->state_nbr = sizeof(mc90hdnc1a_states)/sizeof(struct state_t);
  22. sm->errs = mc90hdnc1a_errs;
  23. sm->err_nbr = sizeof(mc90hdnc1a_errs)/sizeof(struct err_t);
  24. sm_set_state( sm, SMST_LAUNCH, MC90HDNC1AERR_NONE );
  25. return 0;
  26. }
  27. static void mc90hdnc1a_sm_launch( int idx )
  28. {
  29. struct mc90hdnc1a_t* dev = &mc90hdnc1a[idx];
  30. struct comm_t* comm = &dev->comm;
  31. struct statemachine_t* sm = &dev->sm;
  32. if( sm_get_step(sm) == 0 ){ // entry
  33. log_dbg("%s, idx:%d, state:%s, step:%d, entry", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  34. mc90hdnc1a_reset_cmd(idx);
  35. mc90hdnc1a_comm_reset(idx);
  36. sm_set_step(sm, 20);
  37. }
  38. /*
  39. if( sm_get_step(sm) == 0 ){ // entry
  40. log_dbg("%s, idx:%d, state:%s, step:%d, entry", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  41. mc90hdnc1a_reset_cmd(idx);
  42. sm_set_step(sm, 10);
  43. }else if( sm_get_step(sm) == 10){ // wait cmd
  44. if( mc90hdnc1a_get_cmd(idx) == CMD_SM_READY){
  45. log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, try to comm",
  46. __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  47. mc90hdnc1a_reset_cmd(idx);
  48. mc90hdnc1a_comm_reset(idx);
  49. sm_set_step(sm, 20);
  50. }
  51. }*/else if( sm_get_step(sm) == 20){ // chk comm state
  52. if( comm_get_state(comm) == COMMST_NORMAL){
  53. log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto ready",
  54. __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  55. sm_set_state(sm, SMST_READY, MC90HDNC1AERR_NONE);
  56. }else{
  57. log_dbg("%s, idx:%d, state:%s, step:%d, comm err, goto err",
  58. __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  59. sm_set_state(sm, SMST_ERR, MC90HDNC1AERR_LAUNCH_COMMERR);
  60. }
  61. }
  62. }
  63. static void mc90hdnc1a_sm_err( int idx )
  64. {
  65. struct mc90hdnc1a_t* dev = &mc90hdnc1a[idx];
  66. struct comm_t* comm = &dev->comm;
  67. struct statemachine_t* sm = &dev->sm;
  68. static double ts_last_try;
  69. double ts;
  70. if( sm_get_step(sm) == 0 ){ // entry
  71. mc90hdnc1a_reset_cmd(idx);
  72. sm_set_step(sm, 10);
  73. ts_last_try = sm_get_timeofday();
  74. }else if( sm_get_step(sm) == 10){ // wait cmd
  75. ts = sm_get_timeofday();
  76. if( mc90hdnc1a_get_cmd(idx) == CMD_SM_READY){
  77. log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, try to comm",
  78. __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  79. mc90hdnc1a_reset_cmd(idx);
  80. mc90hdnc1a_comm_reset(idx);
  81. sm_set_step(sm, 20);
  82. }else if( ts - ts_last_try > 60000 ){ // 60s
  83. ts_last_try = ts;
  84. mc90hdnc1a_comm_reset(idx);
  85. sm_set_step(sm, 20);
  86. }
  87. }else if( sm_get_step(sm) == 20){ // chk comm state
  88. if( comm_get_state(comm) == COMMST_NORMAL){
  89. log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto ready", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  90. sm_set_state(sm, SMST_READY, MC90HDNC1AERR_NONE);
  91. }else{
  92. log_dbg("%s, idx:%d, state:%s, step:%d, comm err, stay err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  93. sm_set_state(sm, SMST_ERR, MC90HDNC1AERR_ERR_COMMERR);
  94. }
  95. }
  96. }
  97. static void mc90hdnc1a_sm_ready( int idx )
  98. {
  99. struct mc90hdnc1a_t* dev = &mc90hdnc1a[idx];
  100. struct comm_t* comm = &dev->comm;
  101. struct statemachine_t* sm = &dev->sm;
  102. /* chk comm state */
  103. if( comm_get_state(comm) != COMMST_NORMAL){
  104. log_dbg("%s, idx:%d, state:%s, step:%d, comm err detected, goto err",
  105. __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  106. sm_set_state(sm, SMST_ERR, MC90HDNC1AERR_READY_COMMERR);
  107. return;
  108. }
  109. }
  110. void* mc90hdnc1a_sm( int idx )
  111. {
  112. struct mc90hdnc1a_t* dev = &mc90hdnc1a[idx];
  113. struct statemachine_t* sm = &dev->sm;
  114. sm_cal_timing(sm);
  115. switch( sm_get_state(sm) ){
  116. case SMST_LAUNCH:
  117. mc90hdnc1a_sm_launch( idx );
  118. break;
  119. case SMST_READY:
  120. mc90hdnc1a_sm_ready( idx );
  121. break;
  122. case SMST_ERR:
  123. mc90hdnc1a_sm_err( idx );
  124. break;
  125. default:
  126. log_dbg("%s unknown state : %d",__func__, sm_get_state(sm));
  127. break;
  128. }
  129. }