Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
5.9 KiB

  1. /* Copyright 1999 American Power Conversion, All Rights Reserved
  2. *
  3. * Description:
  4. * Implementation of the RUNNING state machine.
  5. *
  6. * Revision History:
  7. * dsmith 31Mar1999 Created
  8. *
  9. */
  10. #include <windows.h>
  11. #include "states.h"
  12. #include "events.h"
  13. #include "run_states.h"
  14. #include "statmach.h"
  15. #include "running_statmach.h"
  16. //////////////////////////////////////////
  17. // Internal prototype definitions
  18. //////////////////////////////////////////
  19. BOOL isRunningEvent(DWORD aState, DWORD anEvent);
  20. DWORD do_running_work(DWORD aCurrentState);
  21. DWORD change_running_state(DWORD aCurrentState, DWORD anEvent);
  22. void exit_running_state(DWORD aCurrentState, DWORD anEvent);
  23. void enter_running_state(DWORD aCurrentState, DWORD anEvent);
  24. //////////////////////////////////////////
  25. // Running State internal variables
  26. //////////////////////////////////////////
  27. BOOL theLogPowerRestoredEvent = FALSE;
  28. /**
  29. * Running_Enter
  30. *
  31. * Description:
  32. * Performs the actions necessary when transitioning into the RUNNING state.
  33. *
  34. * Parameters:
  35. * anEvent The event that caused the transition into this state.
  36. *
  37. * Returns:
  38. * None
  39. */
  40. void Running_Enter(DWORD anEvent){
  41. // Initialize internal variables and enter the RUNNING default sub-state
  42. OnLine_Enter(anEvent, FALSE);
  43. }
  44. /**
  45. * Running_DoWork
  46. *
  47. * Description:
  48. * Change run states based upon events from the UPS. When an event occurs
  49. * that cannot be handled in the run state, then this method exits.
  50. *
  51. * Parameters:
  52. * None
  53. *
  54. * Returns:
  55. * The event that caused the transition from the RUNNING state.
  56. */
  57. DWORD Running_DoWork(){
  58. DWORD event = NO_EVENT;
  59. DWORD new_state = ON_LINE;
  60. DWORD current_state = new_state;
  61. // Perform work in sub-states until an event occurs that cannot be
  62. // handled in the RUNNING state
  63. while (isRunningEvent(current_state, event) && IsStateMachineActive()){
  64. new_state = change_running_state(current_state, event);
  65. current_state = new_state;
  66. event = do_running_work(current_state);
  67. }
  68. return event;
  69. }
  70. /**
  71. * Running_Exit
  72. *
  73. * Description:
  74. * Performs the actions necessary when transitioning from the RUNNING state.
  75. *
  76. * Parameters:
  77. * anEvent The event that caused the transition from the RUNNING state.
  78. *
  79. * Returns:
  80. * None
  81. */
  82. void Running_Exit(DWORD anEvent){
  83. // No work to perform
  84. }
  85. /**
  86. * do_running_work
  87. *
  88. * Description:
  89. * Transfers control to one of the RUNNING sub-states.
  90. *
  91. * Parameters:
  92. * aCurrentState The sub-state to perform the work in.
  93. *
  94. * Returns:
  95. * The event that caused the transition from one of the sub-states
  96. */
  97. DWORD do_running_work(DWORD aCurrentState){
  98. DWORD event = NO_EVENT;
  99. switch (aCurrentState){
  100. case ON_LINE:
  101. event = OnLine_DoWork();
  102. break;
  103. case ON_BATTERY:
  104. event = OnBattery_DoWork();
  105. break;
  106. case NO_COMM:
  107. event = NoComm_DoWork();
  108. break;
  109. default:
  110. break;
  111. }
  112. return event;
  113. }
  114. /**
  115. * isRunningEvent
  116. *
  117. * Description:
  118. * Determines if the current event pertains to the RUNNING state.
  119. *
  120. * Parameters:
  121. * aState The current RUNNING state
  122. * anEvent The current event occuring within the RUNNING state.
  123. *
  124. * Returns:
  125. * TRUE if the current event is applicable to the RUNNING state.
  126. * FALSE for all other events.
  127. */
  128. BOOL isRunningEvent(DWORD aState, DWORD anEvent){
  129. BOOL running_event = FALSE;
  130. // If the state machine has been commanded to exit, then return FALSE
  131. // Otherwise, determine if the event is applicable to the Running state
  132. if (IsStateMachineActive()){
  133. switch (anEvent){
  134. case LOST_COMM:
  135. // If the UPS is on battery, then lost comm will translate into
  136. // a non-RUNNING state event (since the service must now shutdown)
  137. if (aState != ON_BATTERY){
  138. running_event = TRUE;
  139. }
  140. break;
  141. case NO_EVENT:
  142. case POWER_FAILED:
  143. case POWER_RESTORED:
  144. running_event = TRUE;
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. return running_event;
  151. }
  152. /**
  153. * change_running_state
  154. *
  155. * Description:
  156. * Changes the running state based upon the current state and an event.
  157. *
  158. * Parameters:
  159. * anEvent The current event occuring within the RUNNING state.
  160. * aCurrentState The current RUNNING sub-state.
  161. *
  162. * Returns:
  163. * The new RUNNING state.
  164. */
  165. DWORD change_running_state(DWORD aCurrentState, DWORD anEvent){
  166. DWORD new_state;
  167. // Determine new RUNNING sub-state
  168. switch (anEvent){
  169. case LOST_COMM:
  170. new_state = NO_COMM;
  171. break;
  172. case POWER_FAILED:
  173. new_state = ON_BATTERY;
  174. break;
  175. case POWER_RESTORED:
  176. new_state = ON_LINE;
  177. break;
  178. case NO_EVENT:
  179. default:
  180. new_state = aCurrentState;
  181. break;
  182. }
  183. // Close down the old sub-state and enter the new one.
  184. if (new_state != aCurrentState){
  185. exit_running_state(aCurrentState, anEvent);
  186. enter_running_state(new_state, anEvent);
  187. }
  188. return new_state;
  189. }
  190. /**
  191. * exit_running_state
  192. *
  193. * Description:
  194. * Exits the currently executing sub-state.
  195. *
  196. * Parameters:
  197. * anEvent The event that is causing the transition from a sub-state.
  198. * aCurrentState The current RUNNING sub-state.
  199. *
  200. * Returns:
  201. * None
  202. */
  203. void exit_running_state(DWORD aCurrentState, DWORD anEvent){
  204. switch (aCurrentState){
  205. case ON_LINE:
  206. OnLine_Exit(anEvent);
  207. break;
  208. case ON_BATTERY:
  209. OnBattery_Exit(anEvent);
  210. theLogPowerRestoredEvent = TRUE;
  211. break;
  212. case NO_COMM:
  213. NoComm_Exit(anEvent);
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. /**
  220. * enter_running_state
  221. *
  222. * Description:
  223. * Initializes the new RUNNING sub-state.
  224. *
  225. * Parameters:
  226. * anEvent The event that is causing the transition to a sub-state.
  227. * aCurrentState A RUNNING sub-state to enter.
  228. *
  229. * Returns:
  230. * None
  231. */
  232. void enter_running_state(DWORD aCurrentState, DWORD anEvent){
  233. switch (aCurrentState){
  234. case ON_LINE:
  235. OnLine_Enter(anEvent, theLogPowerRestoredEvent);
  236. theLogPowerRestoredEvent = FALSE;
  237. break;
  238. case ON_BATTERY:
  239. OnBattery_Enter(anEvent);
  240. break;
  241. case NO_COMM:
  242. NoComm_Enter(anEvent);
  243. break;
  244. default:
  245. break;
  246. }
  247. }