WatchDogFSM.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "WatchDogFSM.h"
  4. #include "GetDevInfoHelper.h"
  5. const int MAX_INIT_TRIES = 3;
  6. ErrorCodeEnum CWatchDogFSM::OnInit()
  7. {
  8. LOG_FUNCTION();
  9. GET_DEV_ENTITY_BASE_POINTER()->InitializeVendorLogSwitch();
  10. ErrorCodeEnum err = Load();
  11. return err;
  12. }
  13. ErrorCodeEnum CWatchDogFSM::OnExit()
  14. {
  15. LOG_FUNCTION();
  16. ErrorCodeEnum errCode = Error_Succeed;
  17. m_hDevHelper.TearDown();
  18. return errCode;
  19. }
  20. void CWatchDogFSM::s0_on_entry()
  21. {
  22. LOG_FUNCTION();
  23. }
  24. void CWatchDogFSM::s0_on_exit()
  25. {
  26. LOG_FUNCTION();
  27. }
  28. unsigned int CWatchDogFSM::s0_on_event(FSMEvent* pEvt)
  29. {
  30. Dbg("s0 evt %d",pEvt->iEvt);
  31. int rt = 0;
  32. switch(pEvt->iEvt)
  33. {
  34. case USER_EVT_STARTWATCH:
  35. {
  36. StartWatchEvent* swe = dynamic_cast<StartWatchEvent*>(pEvt);
  37. StartWatchTask* task = new StartWatchTask(this);
  38. task->ctx = swe->ctx;
  39. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  40. pEvt->SetHandled();
  41. rt = 0;
  42. }
  43. break;
  44. case USER_EVT_QUIT:
  45. pEvt->SetHandled();
  46. rt = 0;
  47. break;
  48. default:
  49. break;
  50. }
  51. return rt;
  52. }
  53. void CWatchDogFSM::s1_on_entry()
  54. {
  55. LOG_FUNCTION();
  56. }
  57. void CWatchDogFSM::s1_on_exit()
  58. {
  59. LOG_FUNCTION();
  60. }
  61. unsigned int CWatchDogFSM::s1_on_event(FSMEvent* evt)
  62. {
  63. LOG_FUNCTION();
  64. int rt = 0;
  65. switch(evt->iEvt)
  66. {
  67. case USER_EVT_STARTWATCH_FINISHED:
  68. {
  69. if (evt->param1 == 0)
  70. rt = 0;
  71. else
  72. rt = 1;
  73. evt->SetHandled();
  74. }
  75. break;
  76. case USER_EVT_QUIT:
  77. evt->SetHandled();
  78. rt = 0;
  79. break;
  80. default:
  81. break;
  82. }
  83. return rt;
  84. }
  85. void CWatchDogFSM::s2_on_entry()
  86. {
  87. LOG_FUNCTION();
  88. }
  89. void CWatchDogFSM::s2_on_exit()
  90. {
  91. }
  92. unsigned int CWatchDogFSM::s2_on_event(FSMEvent* evt)
  93. {
  94. //LOG_FUNCTION();
  95. int rt = 0;
  96. switch(evt->iEvt)
  97. {
  98. case USER_EVT_REFRESH:
  99. {
  100. RefreshEvent* re = dynamic_cast<RefreshEvent*>(evt);
  101. RefreshTask* task = new RefreshTask(this);
  102. task->ctx = re->ctx;
  103. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  104. evt->SetHandled();
  105. rt = 0;
  106. }
  107. break;
  108. case USER_EVT_REFRESH_FINISHED:
  109. {
  110. if (evt->param1 != 0)
  111. {
  112. Dbg("Refresh failed(%d).",evt->param1);
  113. rt = 1;
  114. }
  115. else
  116. rt = 0;
  117. evt->SetHandled();
  118. }
  119. break;
  120. case USER_EVT_STOPWATCH:
  121. {
  122. StopWatchEvent* swe = dynamic_cast<StopWatchEvent*>(evt);
  123. StopWatchTask* task = new StopWatchTask(this);
  124. task->ctx = swe->ctx;
  125. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  126. evt->SetHandled();
  127. rt = 0;
  128. }
  129. break;
  130. case USER_EVT_QUIT:
  131. evt->SetHandled();
  132. break;
  133. default:
  134. break;
  135. }
  136. return rt;
  137. }
  138. void CWatchDogFSM::s3_on_entry()
  139. {
  140. LOG_FUNCTION();
  141. }
  142. void CWatchDogFSM::s3_on_exit()
  143. {
  144. LOG_FUNCTION();
  145. }
  146. unsigned int CWatchDogFSM::s3_on_event(FSMEvent* pEvt)
  147. {
  148. Dbg("s3 evt %d",pEvt->iEvt);
  149. int rt = 0;
  150. switch(pEvt->iEvt)
  151. {
  152. case USER_EVT_STOPWATCH_FINISHED:
  153. if (pEvt->param1 == 0)
  154. rt = 0;
  155. else
  156. rt = 1;
  157. pEvt->SetHandled();
  158. break;
  159. case USER_EVT_QUIT:
  160. pEvt->SetHandled();
  161. break;
  162. default:
  163. break;
  164. }
  165. return rt;
  166. }
  167. void CWatchDogFSM::s4_on_entry()
  168. {
  169. LOG_FUNCTION();
  170. }
  171. void CWatchDogFSM::s4_on_exit()
  172. {
  173. LOG_FUNCTION();
  174. }
  175. unsigned int CWatchDogFSM::s4_on_event(FSMEvent* evt)
  176. {
  177. LOG_FUNCTION();
  178. int rt = 0;
  179. switch(evt->iEvt)
  180. {
  181. case USER_EVT_QUIT:
  182. evt->SetHandled();
  183. break;
  184. default:
  185. break;
  186. }
  187. return rt;
  188. }
  189. ErrorCodeEnum CWatchDogFSM::Load()
  190. {
  191. CSimpleStringA dllName;
  192. auto pEntity = GET_DEV_ENTITY_BASE_POINTER();
  193. auto result = pEntity->ExtractVendorLibFullPath(dllName);
  194. if (result != Error_Succeed) {
  195. return result;
  196. }
  197. HARDWARE_ENTITY_SET_VENDOR_NAME(m_entCode, pEntity->vendorLibInfo.strVendor);
  198. Dbg("vendor library path: %s", (LPCTSTR)dllName);
  199. result = m_hDevHelper.LoadUp(dllName);
  200. if (result != Error_Succeed) {
  201. return result;
  202. }
  203. result = m_hDevHelper->DevOpen();
  204. if (result == Error_Succeed) {
  205. LOG_TRACE("WatchDog open succeed.");
  206. } else {
  207. LOG_WATCHDOG_ERROR_MSG_MACRO(result, DevOpen);
  208. return Error_DevConnFailed;
  209. }
  210. return Error_Succeed;
  211. }
  212. int CWatchDogFSM::StartWatch(SpReqAnsContext<WatchDogService_StartWatch_Req,WatchDogService_StartWatch_Ans>::Pointer ctx)
  213. {
  214. Dbg("fsm starwatch %d,%d", ctx->Req.Delay, ctx->Req.Timeout);
  215. ErrorCodeEnum eErrCode = m_hDevHelper->StartWatch(ctx->Req.Delay, ctx->Req.Timeout);
  216. if (eErrCode != Error_Succeed)
  217. {
  218. LOG_WATCHDOG_ERROR_MSG_MACRO(eErrCode, StartWatch);
  219. if (ctx != NULL)
  220. ctx->Answer(Error_Unexpect);
  221. return 1;
  222. }
  223. ctx->Answer(Error_Succeed);
  224. return 0;
  225. }
  226. int CWatchDogFSM::Refresh(SpReqAnsContext<WatchDogService_Refresh_Req,WatchDogService_Refresh_Ans>::Pointer ctx)
  227. {
  228. ErrorCodeEnum eErrCode = m_hDevHelper->RefreshDog();
  229. if (eErrCode != Error_Succeed)
  230. {
  231. LOG_WATCHDOG_ERROR_MSG_MACRO(eErrCode, RefreshDog);
  232. if (ctx != NULL)
  233. ctx->Answer(Error_Unexpect);
  234. return 1;
  235. }
  236. ctx->Answer(Error_Succeed);
  237. return 0;
  238. }
  239. int CWatchDogFSM::StopWatch(SpReqAnsContext<WatchDogService_StopWatch_Req,WatchDogService_StopWatch_Ans>::Pointer ctx)
  240. {
  241. ErrorCodeEnum eErrCode = m_hDevHelper->StopWatch();
  242. if (eErrCode != Error_Succeed)
  243. {
  244. LOG_WATCHDOG_ERROR_MSG_MACRO(eErrCode, StopWatch);
  245. if (ctx != NULL)
  246. ctx->Answer(Error_Unexpect);
  247. return 1;
  248. }
  249. if (ctx != NULL)
  250. ctx->Answer(Error_Succeed);
  251. return 0;
  252. }