RemoteControllerFSM.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "RemoteControllerFSM.h"
  2. CRemoteControllerFSM::CRemoteControllerFSM(void)
  3. :m_pConnection(NULL)
  4. {
  5. }
  6. CRemoteControllerFSM::~CRemoteControllerFSM(void)
  7. {
  8. }
  9. ErrorCodeEnum CRemoteControllerFSM::OnInit()
  10. {
  11. return Error_Succeed;
  12. }
  13. ErrorCodeEnum CRemoteControllerFSM::OnExit()
  14. {
  15. return Error_Succeed;
  16. }
  17. void CRemoteControllerFSM::s1_on_entry()
  18. {
  19. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("s1_on_entry")("enter to init state");
  20. auto pFunction = m_pEntity->GetFunction();
  21. pFunction->SetSysVar("RemoteMaintain", "N");
  22. }
  23. void CRemoteControllerFSM::s1_on_exit()
  24. {
  25. }
  26. unsigned int CRemoteControllerFSM::s1_on_event(FSMEvent* event)
  27. {
  28. //MessageBoxA(0,0,0,0);
  29. if (event->iEvt == Event_CreateChannel)
  30. {
  31. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("s1_on_event")("recv Event_CreateChannel");
  32. auto rc = SecureClientConnect();
  33. if (rc == Error_Succeed && m_pConnection->IsConnectionOK())
  34. rc = m_pConnection->SendCreateRemoteChannelReq();
  35. if(rc == Error_Succeed)
  36. LOG_TRACE("send create remote channel succ");
  37. else
  38. LogError(Severity_Low, rc, 0, "send create remote channel fail");
  39. }
  40. return 0;
  41. }
  42. void CRemoteControllerFSM::s2_on_entry()
  43. {
  44. auto pFunction = m_pEntity->GetFunction();
  45. pFunction->SetSysVar("RemoteMaintain", "O");
  46. }
  47. void CRemoteControllerFSM::s2_on_exit()
  48. {
  49. }
  50. unsigned int CRemoteControllerFSM::s2_on_event(FSMEvent* event)
  51. {
  52. if (event->iEvt == Event_ReleaseChannel)
  53. {
  54. SecureClientRelease();
  55. }
  56. return 0;
  57. }
  58. void CRemoteControllerFSM::s3_on_entry()
  59. {
  60. auto pFunction = m_pEntity->GetFunction();
  61. pFunction->SetSysVar("RemoteMaintain", "M");
  62. }
  63. void CRemoteControllerFSM::s3_on_exit()
  64. {
  65. }
  66. unsigned int CRemoteControllerFSM::s3_on_event(FSMEvent* event)
  67. {
  68. if (event->iEvt == Event_ReleaseChannel)
  69. {
  70. SecureClientRelease();
  71. }
  72. return 0;
  73. }
  74. void CRemoteControllerFSM::s4_on_entry()
  75. {
  76. auto pFunction = m_pEntity->GetFunction();
  77. pFunction->SetSysVar("RemoteMaintain", "T");
  78. }
  79. void CRemoteControllerFSM::s4_on_exit()
  80. {
  81. }
  82. unsigned int CRemoteControllerFSM::s4_on_event(FSMEvent* event)
  83. {
  84. if (event->iEvt == Event_ReleaseChannel)
  85. {
  86. SecureClientRelease();
  87. }
  88. return 0;
  89. }
  90. ErrorCodeEnum CRemoteControllerFSM::SecureClientConnect()
  91. {
  92. if (m_pConnection != NULL && m_pConnection->IsConnectionOK())
  93. return Error_Succeed;
  94. SecureClientRelease();
  95. m_pConnection = new CRemoteControllerCnn(m_pEntity, this);
  96. if (m_pConnection->ConnectFromCentralSetting() && m_pConnection->IsConnectionOK())
  97. return Error_Succeed;
  98. SecureClientRelease();
  99. return Error_PeerReject;
  100. }
  101. ErrorCodeEnum CRemoteControllerFSM::SecureClientRelease()
  102. {
  103. if (m_pConnection != NULL)
  104. {
  105. m_pConnection->Close();
  106. m_pConnection->DecRefCount();
  107. m_pConnection = NULL;
  108. }
  109. return Error_Succeed;
  110. }