dlt645_sm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "plt.h"
  2. #include "dlt645_sm.h"
  3. static struct state_t dlt645_states[] = {
  4. { SMST_LAUNCH, "launch" },
  5. { SMST_READY, "ready" },
  6. { SMST_ERR, "err" },
  7. };
  8. static struct err_t dlt645_errs[] = {
  9. { DLT645ERR_NONE, "none" },
  10. //
  11. { DLT645ERR_ERR_COMMERR, "err comm err" },
  12. // ready
  13. { DLT645ERR_READY_COMMERR, "ready comm err" },
  14. };
  15. static void dlt645_sm_launch( int idx )
  16. {
  17. struct dlt645_t* dev = &dlt645[idx];
  18. struct comm_t* comm = &dev->comm;
  19. struct statemachine_t* sm = &dev->sm;
  20. if(sm_get_step(sm) == 0){ // entry
  21. log_dbg("%s, idx:%d, state:%s, step:%d, entry", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  22. dlt645_reset_cmd(idx);
  23. dlt645_comm_reset(idx);
  24. sm_set_step(sm, 20);
  25. }
  26. /*
  27. if(sm_get_step(sm) == 0){ // entry
  28. log_dbg("%s, idx:%d, state:%s, step:%d, entry", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  29. dlt645_reset_cmd(idx);
  30. sm_set_step(sm, 10);
  31. }else if( sm_get_step(sm) == 10 ){ // wait cmd
  32. if( dlt645_get_cmd(idx) == CMD_SM_READY){ // ready cmd
  33. 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));
  34. dlt645_reset_cmd(idx);
  35. dlt645_comm_reset(idx);
  36. sm_set_step(sm, 20);
  37. }else if( dlt645_get_cmd(idx) == CMD_SM_ACK){
  38. log_dbg("%s, idx:%d, state:%s, step:%d, get ack cmd", __func__,idx, sm_get_szstate(sm), sm_get_step(sm));
  39. dlt645_reset_cmd(idx);
  40. dlt645_read_com_ae(1);
  41. dlt645_read_pos_ae(1);
  42. dlt645_read_neg_ae(1);
  43. dlt645_read_com_ap(1);
  44. dlt645_read_ua(1);
  45. dlt645_read_ub(1);
  46. dlt645_read_uc(1);
  47. dlt645_read_ia(1);
  48. dlt645_read_ib(1);
  49. dlt645_read_ic(1);
  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, DLT645ERR_NONE);
  56. }else{
  57. log_dbg("%s, idx:%d, state:%s, step:%d, comm err, goto err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  58. sm_set_state(sm, SMST_ERR, DLT645ERR_LAUNCH_COMMERR);
  59. }
  60. }
  61. }
  62. static void dlt645_sm_err( int idx )
  63. {
  64. struct dlt645_t* dev = &dlt645[idx];
  65. struct comm_t* comm = &dev->comm;
  66. struct statemachine_t* sm = &dev->sm;
  67. static double ts_last_try;
  68. double ts;
  69. if(sm_get_step(sm) == 0){ // entry
  70. dlt645_reset_cmd(idx);
  71. sm_set_step(sm, 10);
  72. ts_last_try = sm_get_timeofday();
  73. }else if( sm_get_step(sm) == 10 ){ // wait cmd
  74. ts = sm_get_timeofday();
  75. if( dlt645_get_cmd(idx) == CMD_SM_READY){ // ready cmd
  76. log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, try to comm",
  77. __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  78. dlt645_reset_cmd(idx);
  79. dlt645_comm_reset(idx);
  80. sm_set_step(sm, 20);
  81. }else if( dlt645_get_cmd(idx) == CMD_SM_ACK){
  82. log_dbg("%s, idx:%d, state:%s, step:%d, get ack cmd",
  83. __func__,idx, sm_get_szstate(sm), sm_get_step(sm));
  84. dlt645_reset_cmd(idx);
  85. dlt645_read_com_ae(1);
  86. dlt645_read_pos_ae(1);
  87. dlt645_read_neg_ae(1);
  88. dlt645_read_com_ap(1);
  89. dlt645_read_ua(1);
  90. dlt645_read_ub(1);
  91. dlt645_read_uc(1);
  92. dlt645_read_ia(1);
  93. dlt645_read_ib(1);
  94. dlt645_read_ic(1);
  95. }else if( ts - ts_last_try > 60000 ){ // 60s
  96. ts_last_try = ts;
  97. dlt645_comm_reset(idx);
  98. sm_set_step(sm, 20);
  99. }
  100. }else if( sm_get_step(sm) == 20 ){ // chk comm state
  101. if( comm_get_state(comm) == COMMST_NORMAL){
  102. log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto ready", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  103. sm_set_state(sm, SMST_READY, DLT645ERR_NONE);
  104. }else{
  105. log_dbg("%s, idx:%d, state:%s, step:%d, comm err, stay err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  106. sm_set_state(sm, SMST_ERR, DLT645ERR_ERR_COMMERR);
  107. }
  108. }
  109. }
  110. static void dlt645_sm_ready( int idx )
  111. {
  112. struct dlt645_t* dev = &dlt645[idx];
  113. struct comm_t* comm = &dev->comm;
  114. struct statemachine_t* sm = &dev->sm;
  115. /* chk comm state */
  116. if( comm_get_state(comm) != COMMST_NORMAL){
  117. log_dbg("%s, idx:%d, state:%s, step:%d, comm err detected, goto err",
  118. __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
  119. sm_set_state(sm, SMST_ERR, DLT645ERR_READY_COMMERR);
  120. return;
  121. }
  122. }
  123. void dlt645_sm( int idx )
  124. {
  125. struct dlt645_t* dev = &dlt645[idx];
  126. struct statemachine_t* sm = &dev->sm;
  127. sm_cal_timing(sm);
  128. switch(sm_get_state( sm )){
  129. case SMST_LAUNCH:
  130. dlt645_sm_launch( idx );
  131. break;
  132. case SMST_READY:
  133. dlt645_sm_ready( idx );
  134. break;
  135. case SMST_ERR:
  136. dlt645_sm_err( idx );
  137. break;
  138. default:
  139. log_dbg("%s, never reach here",__func__);
  140. break;
  141. }
  142. }
  143. int dlt645_sm_init(int idx)
  144. {
  145. struct statemachine_t* sm = &dlt645[idx].sm;
  146. sm_reset_timing(sm, 100, 100);
  147. sm->states = dlt645_states;
  148. sm->state_nbr = sizeof(dlt645_states)/sizeof(struct state_t);
  149. sm->errs = dlt645_errs;
  150. sm->err_nbr = sizeof(dlt645_errs)/sizeof(struct err_t);
  151. sm_set_state( sm, SMST_LAUNCH, DLT645ERR_NONE );
  152. return 0;
  153. }