ems_sm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "ems.h"
  2. #include "plt.h"
  3. static struct state_t ems_states[] = {
  4. {SMST_LAUNCH, "launch"},
  5. {SMST_IDLE, "idle"},
  6. {SMST_RUN, "run"},
  7. {SMST_ERR, "err"},
  8. };
  9. static struct err_t ems_errs[] = {
  10. {EMSERR_NONE, "none"},
  11. // launch
  12. {EMSERR_LAUNCH_INIT_PLT_FAIL, "launch, init plt fail"},
  13. {EMSERR_LAUNCH_INIT_CHAN_FAIL, "launch, init chan fail"},
  14. {EMSERR_LAUNCH_INIT_MAC_FAIL, "launch, init mac fail"},
  15. {EMSERR_LAUNCH_INIT_SHM_FAIL, "launch, init shm fail"},
  16. {EMSERR_LAUNCH_INIT_LOG_FAIL, "launch, init log fail"},
  17. {EMSERR_LAUNCH_INIT_BKD_FAIL, "launch, init bkd fail"},
  18. {EMSERR_LAUNCH_INIT_CFGDB_FAIL, "launch, init cfgdb fail"},
  19. {EMSERR_LAUNCH_INIT_PROJDB_FAIL, "launch, init proj db fail"},
  20. {EMSERR_LAUNCH_INIT_ESS_FAIL, "launch, init ess fail"},
  21. {EMSERR_LAUNCH_INIT_SNAP_FAIL, "launch, init snap fail"},
  22. {EMSERR_LAUNCH_INIT_EVENT_FAIL, "launch, init event fail"},
  23. {EMSERR_LAUNCH_INIT_CLOUD_FAIL, "launch, init cloud fail"},
  24. {EMSERR_LAUNCH_INIT_TB_FAIL, "launch, init tb fail"},
  25. {EMSERR_LAUNCH_START_AUX_FAIL, "launch, init aux fail"},
  26. {EMSERR_LAUNCH_LOAD_PARAMS_FAIL, "launch, load params fail"},
  27. {EMSERR_LAUNCH_SET_TIMESEG_FAIL, "launch, set timeseg fail"},
  28. {EMSERR_LAUNCH_INIT_ESTATS_FAIL, "launch, init estats fail"},
  29. {EMSERR_LAUNCH_INIT_CTN_CAL_FAIL, "launch, init cal fail"},
  30. };
  31. static void ems_sm_launch()
  32. {
  33. struct ems_t *dev = &ems;
  34. struct statemachine_t *sm = &dev->sm;
  35. if (sm_get_step(sm) == 0)
  36. { // entry
  37. syslog(LOG_INFO, "%s, state:%s, step:%d, entry", __func__,
  38. sm_get_szstate(sm), sm_get_step(sm));
  39. sm_set_step(sm, 5);
  40. }
  41. else if (sm_get_step(sm) == 5)
  42. { // do init
  43. if (plt_init() != 0)
  44. {
  45. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_PLT_FAIL);
  46. }
  47. else if (shm_init() != 0)
  48. {
  49. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_SHM_FAIL);
  50. }
  51. else if (log_init() != 0)
  52. {
  53. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_LOG_FAIL);
  54. } /*else if( bkds_init()!= 0 ){
  55. sm_set_state(sm,SMST_ERR, EMSERR_LAUNCH_INIT_BKD_FAIL);
  56. }*/
  57. else if (plt_init_cfgdb() != 0)
  58. { //
  59. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_CFGDB_FAIL);
  60. }
  61. else if (plt_init_projdb() != 0)
  62. { //
  63. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_PROJDB_FAIL);
  64. }
  65. else if (chan_init() != 0)
  66. {
  67. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_CHAN_FAIL);
  68. }
  69. else if (mac_init() != 0)
  70. {
  71. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_MAC_FAIL);
  72. }
  73. else if (cloud_init() != 0)
  74. {
  75. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_CLOUD_FAIL);
  76. }
  77. else if (tb_init() != 0)
  78. {
  79. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_TB_FAIL);
  80. }
  81. else if (ess_init() != 0)
  82. {
  83. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_ESS_FAIL);
  84. }
  85. else if (/*snap_init()!= */ 0)
  86. {
  87. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_SNAP_FAIL);
  88. }
  89. else if (ev_init() != 0)
  90. {
  91. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_EVENT_FAIL);
  92. }
  93. else if (ems_start_aux() != 0)
  94. { // ems_start_aux()!= 0
  95. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_START_AUX_FAIL);
  96. }
  97. else if (ems_set_timeseg(1) != 0)
  98. { //
  99. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_SET_TIMESEG_FAIL);
  100. }
  101. else if (estats_init() != 0)
  102. { //
  103. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_ESTATS_FAIL);
  104. }
  105. else if (ems_load_params() != 0)
  106. {
  107. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_LOAD_PARAMS_FAIL);
  108. }
  109. else if (ctn_cal_init() != 0)
  110. {
  111. sm_set_state(sm, SMST_ERR, EMSERR_LAUNCH_INIT_CTN_CAL_FAIL);
  112. }
  113. else
  114. {
  115. sm_set_state(sm, SMST_IDLE, EMSERR_NONE);
  116. }
  117. }
  118. }
  119. static void ems_sm_err()
  120. {
  121. struct ems_t *dev = &ems;
  122. struct statemachine_t *sm = &dev->sm;
  123. if (sm_get_step(sm) == 0)
  124. { // entry
  125. log_dbg("%s, state:%s, step:%d, entry", __func__, sm_get_szstate(sm),
  126. sm_get_step(sm));
  127. sm_set_step(sm, 5);
  128. }
  129. else if (sm_get_step(sm) == 5)
  130. {
  131. ;
  132. }
  133. }
  134. static void ems_reset_reg()
  135. {
  136. struct ems_t *dev = &ems;
  137. dev->pow_reg.timer = dev->pow_reg.intv;
  138. }
  139. static void ems_sm_idle()
  140. {
  141. struct ems_t *dev = &ems;
  142. struct statemachine_t *sm = &dev->sm;
  143. if (sm_get_step(sm) == 0)
  144. { // entry
  145. log_dbg("%s, state:%s, step:%d, entry", __func__, sm_get_szstate(sm),
  146. sm_get_step(sm));
  147. sm_set_step(sm, 10);
  148. }
  149. else if (sm_get_step(sm) == 10)
  150. {
  151. if (ems_get_cmd() == CMD_SM_RUN)
  152. {
  153. ems_reset_cmd();
  154. if (ems_get_mode() != EMSMOD_NONE)
  155. {
  156. log_dbg("%s, state:%s, step:%d, get run cmd, mode:%s, goto run",
  157. __func__, sm_get_szstate(sm), sm_get_step(sm),
  158. ems_get_modestr());
  159. ems_reset_reg();
  160. sm_set_state(sm, SMST_RUN, EMSERR_NONE);
  161. }
  162. else
  163. {
  164. log_dbg("%s, state:%s, step:%d, get run cmd, mode:%s, stay idle",
  165. __func__, sm_get_szstate(sm), sm_get_step(sm),
  166. ems_get_modestr());
  167. }
  168. }
  169. }
  170. }
  171. static void ems_sm_run()
  172. {
  173. struct ems_t *dev = &ems;
  174. struct statemachine_t *sm = &dev->sm;
  175. if (sm_get_step(sm) == 0)
  176. { // entry
  177. log_dbg("%s, state:%s, step:%d, entry", __func__, sm_get_szstate(sm),
  178. sm_get_step(sm));
  179. sm_set_step(sm, 10);
  180. }
  181. else if (sm_get_step(sm) == 10)
  182. {
  183. if (ems_get_cmd() == CMD_SM_IDLE)
  184. { // idle cmd
  185. log_dbg("%s, state:%s, step:%d, get idle cmd, goto idle", __func__,
  186. sm_get_szstate(sm), sm_get_step(sm));
  187. sm_set_state(sm, SMST_IDLE, EMSERR_NONE);
  188. }
  189. else
  190. {
  191. if (ems_get_mode() == EMSMOD_PCURV)
  192. {
  193. ems_exe_pcurv();
  194. }
  195. else if (ems_get_mode() == EMSMOD_PDEM)
  196. {
  197. ems_exe_pdem();
  198. }
  199. }
  200. }
  201. }
  202. void ems_sm()
  203. {
  204. struct ems_t *dev = &ems;
  205. struct statemachine_t *sm = &dev->sm;
  206. sm_cal_timing(sm);
  207. switch (sm_get_state(sm))
  208. {
  209. case SMST_LAUNCH:
  210. ems_sm_launch();
  211. break;
  212. case SMST_IDLE:
  213. ems_sm_idle();
  214. break;
  215. case SMST_RUN:
  216. ems_sm_run();
  217. break;
  218. case SMST_ERR:
  219. ems_sm_err();
  220. break;
  221. default:
  222. break;
  223. }
  224. }
  225. int ems_sm_init()
  226. {
  227. struct statemachine_t *sm = &ems.sm;
  228. sm_reset_timing(sm, 100, 100);
  229. sm->states = ems_states;
  230. sm->state_nbr = sizeof(ems_states) / sizeof(struct state_t);
  231. sm->errs = ems_errs;
  232. sm->err_nbr = sizeof(ems_errs) / sizeof(struct err_t);
  233. sm_set_state(sm, SMST_LAUNCH, EMSERR_NONE);
  234. return 0;
  235. }