Source code of Windows XP (NT5)
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.

435 lines
14 KiB

  1. /*
  2. *
  3. * REVISIONS:
  4. */
  5. #include "cdefine.h"
  6. extern "C" {
  7. #include <stdlib.h>
  8. }
  9. #include "_defs.h"
  10. #include "servapp.h"
  11. #include "codes.h"
  12. #include "cfgmgr.h"
  13. #include "trans.h"
  14. #include "timerman.h"
  15. #include "sysstate.h"
  16. #include "event.h"
  17. #include "utils.h"
  18. // Retry a failed construction every 3 seconds
  19. //const INT RETRY_CONSTRUCT_DELAY = 3000;
  20. const INT RETRY_CONSTRUCT_DELAY = 3;
  21. // When setting the port name, it might fix comm on its own
  22. // making this == 0 will force a rebuild immediately, on
  23. // the same thread
  24. //
  25. // #define WAIT_FOR_SET_TO_WORK 250 // 1/4 seconds
  26. #define WAIT_FOR_SET_TO_WORK 1 /* 1 second */
  27. // theDeviceControllerInitialized values
  28. #define NEVER_DONE 0
  29. #define IN_PROGRESS 1
  30. #define COMPLETED 2
  31. // This should be initialized in the main module
  32. PServerApplication _theApp = NULL;
  33. //-------------------------------------------------------------------
  34. ServerApplication::ServerApplication() :
  35. theTimerID(0),
  36. theDeviceControllerInitialized(NEVER_DONE),
  37. theForceDeviceRebuildFlag(FALSE),
  38. theDeviceController((PDeviceController)NULL)
  39. {
  40. _theApp = this;
  41. }
  42. //-------------------------------------------------------------------
  43. ServerApplication::~ServerApplication()
  44. {
  45. if (theTimerID) {
  46. if (_theTimerManager) {
  47. _theTimerManager->CancelTimer(theTimerID);
  48. }
  49. theTimerID = 0;
  50. }
  51. }
  52. //-------------------------------------------------------------------
  53. INT ServerApplication::Start()
  54. {
  55. // set the timezone variables
  56. SetTimeZone();
  57. Event start_event(MONITORING_STATUS, MONITORING_STARTED);
  58. Update(&start_event);
  59. CreateDeviceController((PEvent)NULL);
  60. InitializeDeviceController();
  61. return ErrNO_ERROR;
  62. }
  63. //-------------------------------------------------------------------
  64. VOID ServerApplication::Quit()
  65. {
  66. DisableEvents(); //srt28Mar97
  67. // Tell everyone that the service is stopping
  68. Event start_event(MONITORING_STATUS, MONITORING_STOPPED);
  69. Update(&start_event);
  70. // Stop all active threads ... NOTE that they are stopped
  71. // in a specific order. theDataLog is independent and is stopped first,
  72. // theDeviceController will stop the UPS polling. theServerController
  73. // will stop all incoming client requests and current connections. Finally,
  74. // theTimerManager will stop executing timed events.
  75. if (theDeviceController) {
  76. theDeviceController->Stop();
  77. }
  78. if (_theTimerManager) {
  79. _theTimerManager->Stop();
  80. }
  81. // Return to simple signalling
  82. theDeviceController->Set(TURN_OFF_SMART_MODE, NULL);
  83. delete theDeviceController;
  84. theDeviceController = NULL;
  85. delete _theTimerManager;
  86. _theTimerManager = theTimerManager = NULL;
  87. }
  88. //-------------------------------------------------------------------
  89. INT ServerApplication::Set(INT code,const PCHAR value)
  90. {
  91. INT err = ErrNO_ERROR;
  92. switch(code/1000) {
  93. case 0: // Ups
  94. case 1: // Measure ups
  95. if (theDeviceController) {
  96. err = theDeviceController->Set(code,value);
  97. }
  98. else {
  99. err = ErrCOMMUNICATION_LOST;
  100. }
  101. break;
  102. case 2: // Host
  103. if (code == RESET_UPS_COMM_PORT) {
  104. // Set this TRUE. If just setting the .ini file value
  105. // causes the retry to succeed, then the flag will be
  106. // reset and we won't bother forcing a rebuild
  107. // Note that by sending the event on the timer thread,
  108. // we serialize with the retries.
  109. //
  110. theForceDeviceRebuildFlag = TRUE;
  111. Event ev(COMMUNICATION_STATE, UPS_COMM_PORT_CHANGED);
  112. #if (WAIT_FOR_SET_TO_WORK > 0)
  113. _theTimerManager->SetTheTimer(WAIT_FOR_SET_TO_WORK, &ev, this);
  114. #else
  115. Update(&ev);
  116. #endif
  117. }
  118. break;
  119. }
  120. return err;
  121. }
  122. //-------------------------------------------------------------------
  123. INT ServerApplication::Set(PTransactionItem trans)
  124. {
  125. INT err = ErrNO_ERROR;
  126. return err;
  127. }
  128. //-------------------------------------------------------------------
  129. INT ServerApplication::Get(INT code, PCHAR value)
  130. {
  131. INT err = ErrNO_ERROR;
  132. switch(code/1000) {
  133. case 0: // Ups
  134. if (code == SYSTEM_STATE || code == UPS_STATE) {
  135. //
  136. // Really all we want is sytem state. Ups state hangs around
  137. // to be compatible with old software (PowerNet)
  138. //
  139. ULONG system_state = 0;
  140. if (theDeviceController) {
  141. value[0] = 0;
  142. err = theDeviceController->Get(COMMUNICATION_STATE, value);
  143. ULONG comm_state = atol(value);
  144. if (comm_state == COMMUNICATION_ESTABLISHED) {
  145. //
  146. // Only get UPS State if we have Comm
  147. //
  148. value[0] = 0;
  149. err = theDeviceController->Get(UPS_STATE, value);
  150. system_state = atol(value);
  151. //
  152. // Just to make sure
  153. //
  154. CLEAR_BIT(system_state, COMMUNICATIONS_BIT);
  155. CLEAR_BIT(system_state, SHUTDOWN_IN_PROGRESS_BIT);
  156. }
  157. else {
  158. //
  159. // pcy - I don't know why we don't return communication
  160. // lost as an error here, but we do below.
  161. //
  162. SET_BIT(system_state, COMMUNICATIONS_BIT);
  163. }
  164. _ltoa(system_state, value, 10);
  165. }
  166. else {
  167. SET_BIT(system_state, COMMUNICATIONS_BIT);
  168. _ltoa(system_state, value, 10);
  169. err = ErrCOMMUNICATION_LOST;
  170. }
  171. break;
  172. }
  173. case 1: // Measure ups
  174. value[0] = 0;
  175. if (theDeviceController) {
  176. err = theDeviceController->Get(code,value);
  177. }
  178. else {
  179. err = ErrCOMMUNICATION_LOST;
  180. }
  181. break;
  182. case 2: // Host
  183. // printf("Getting from host\n");
  184. value[0] = 0;
  185. break;
  186. }
  187. return err;
  188. }
  189. //-------------------------------------------------------------------
  190. INT ServerApplication::Get(PTransactionItem trans)
  191. {
  192. INT err = ErrNO_ERROR;
  193. return err;
  194. }
  195. //-------------------------------------------------------------------
  196. INT ServerApplication::Update(PEvent anEvent)
  197. {
  198. INT err = ErrNO_ERROR;
  199. int sentEventOn = FALSE;
  200. // If the communication is lost or we change ports, then
  201. // schedule a rebuild of the device controller
  202. if (anEvent->GetCode() == COMMUNICATION_STATE) {
  203. switch(atoi(anEvent->GetValue())) {
  204. case UPS_COMM_PORT_CHANGED:
  205. if (theForceDeviceRebuildFlag) {
  206. err = MainApplication::Update(anEvent);
  207. sentEventOn = TRUE;
  208. theDeviceController->Set(RETRY_CONSTRUCT, "Yes");
  209. }
  210. break;
  211. case COMMUNICATION_ESTABLISHED:
  212. if (theDeviceControllerInitialized == NEVER_DONE) {
  213. err = InitializeDeviceController();
  214. }
  215. theForceDeviceRebuildFlag = FALSE;
  216. // Fall through
  217. }
  218. }
  219. if (!sentEventOn) {
  220. err = MainApplication::Update(anEvent);
  221. }
  222. return err;
  223. }
  224. //-------------------------------------------------------------------
  225. VOID ServerApplication::DisableEvents()
  226. {
  227. if (theDeviceController) {
  228. theDeviceController->UnregisterEvent(UTILITY_LINE_CONDITION,this);
  229. theDeviceController->UnregisterEvent(BATTERY_CONDITION,this);
  230. theDeviceController->UnregisterEvent(RUN_TIME_EXPIRED,this);
  231. theDeviceController->UnregisterEvent(RUN_TIME_REMAINING,this);
  232. theDeviceController->UnregisterEvent(UPS_OFF_PENDING,this);
  233. // Register with the device for Shutdown - this will happen when the
  234. // server is a slave
  235. theDeviceController->UnregisterEvent(SHUTDOWN,this);
  236. theDeviceController->UnregisterEvent(SMART_BOOST_STATE,this);
  237. theDeviceController->UnregisterEvent(SMART_TRIM_STATE,this);
  238. theDeviceController->UnregisterEvent(UPS_LOAD,this);
  239. theDeviceController->UnregisterEvent(COMMUNICATION_STATE,this);
  240. theDeviceController->UnregisterEvent(SELF_TEST_RESULT,this);
  241. theDeviceController->UnregisterEvent(BATTERY_CALIBRATION_CONDITION,this);
  242. theDeviceController->UnregisterEvent(MIN_LINE_VOLTAGE,this);
  243. theDeviceController->UnregisterEvent(MAX_LINE_VOLTAGE,this);
  244. theDeviceController->UnregisterEvent(BYPASS_MODE, this);
  245. theDeviceController->UnregisterEvent(MATRIX_FAN_STATE, this);
  246. theDeviceController->UnregisterEvent(BYPASS_POWER_SUPPLY_CONDITION, this);
  247. theDeviceController->UnregisterEvent(SMART_CELL_SIGNAL_CABLE_STATE, this);
  248. theDeviceController->UnregisterEvent(REDUNDANCY_STATE, this);
  249. theDeviceController->UnregisterEvent(UPS_MODULE_ADDED, this);
  250. theDeviceController->UnregisterEvent(UPS_MODULE_REMOVED, this);
  251. theDeviceController->UnregisterEvent(UPS_MODULE_FAILED, this);
  252. theDeviceController->UnregisterEvent(BATTERY_ADDED, this);
  253. theDeviceController->UnregisterEvent(BATTERY_REMOVED, this);
  254. theDeviceController->UnregisterEvent(IM_OK, this);
  255. theDeviceController->UnregisterEvent(IM_FAILED, this);
  256. theDeviceController->UnregisterEvent(IM_INSTALLATION_STATE, this);
  257. theDeviceController->UnregisterEvent(RIM_INSTALLATION_STATE, this);
  258. theDeviceController->UnregisterEvent(RIM_OK, this);
  259. theDeviceController->UnregisterEvent(RIM_FAILED, this);
  260. theDeviceController->UnregisterEvent(SYSTEM_FAN_STATE, this);
  261. theDeviceController->UnregisterEvent(BYPASS_CONTACTOR_STATE, this);
  262. theDeviceController->UnregisterEvent(INPUT_BREAKER_STATE, this);
  263. theDeviceController->UnregisterEvent(UPS_TEMPERATURE, this);
  264. // MeasureUPS Events
  265. theDeviceController->UnregisterEvent(AMBIENT_TEMPERATURE,this);
  266. theDeviceController->UnregisterEvent(HUMIDITY,this);
  267. theDeviceController->UnregisterEvent(CONTACT_STATE,this);
  268. theDeviceController->UnregisterEvent(IM_STATUS,this);
  269. theDeviceController->UnregisterEvent(RIM_STATUS,this);
  270. theDeviceController->UnregisterEvent(BATTERY_REPLACEMENT_CONDITION,this);
  271. }
  272. }
  273. //-------------------------------------------------------------------
  274. INT ServerApplication::CreateDeviceController (PEvent anEvent)
  275. {
  276. INT err = ErrNO_ERROR;
  277. // printf("Entering CreateDeviceController\n");
  278. // Delete any existing device controller
  279. if (theDeviceController) {
  280. delete theDeviceController;
  281. theDeviceController = (PDeviceController)NULL;
  282. }
  283. // Now create a new device controller
  284. theDeviceController = new DeviceController(this);
  285. theDeviceController->RegisterEvent(UTILITY_LINE_CONDITION,this);
  286. theDeviceController->RegisterEvent(BATTERY_CONDITION,this);
  287. theDeviceController->RegisterEvent(RUN_TIME_EXPIRED,this);
  288. theDeviceController->RegisterEvent(RUN_TIME_REMAINING,this);
  289. theDeviceController->RegisterEvent(UPS_OFF_PENDING,this);
  290. // Register with the device for Shutdown - this will happen when the
  291. // server is a slave
  292. theDeviceController->RegisterEvent(SHUTDOWN,this);
  293. theDeviceController->RegisterEvent(SMART_BOOST_STATE,this);
  294. theDeviceController->RegisterEvent(SMART_TRIM_STATE,this);
  295. theDeviceController->RegisterEvent(UPS_LOAD,this);
  296. theDeviceController->RegisterEvent(COMMUNICATION_STATE,this);
  297. theDeviceController->RegisterEvent(SELF_TEST_RESULT,this);
  298. theDeviceController->RegisterEvent(BATTERY_CALIBRATION_CONDITION,this);
  299. theDeviceController->RegisterEvent(MIN_LINE_VOLTAGE,this);
  300. theDeviceController->RegisterEvent(MAX_LINE_VOLTAGE,this);
  301. theDeviceController->RegisterEvent(BYPASS_MODE, this);
  302. theDeviceController->RegisterEvent(MATRIX_FAN_STATE, this);
  303. theDeviceController->RegisterEvent(BYPASS_POWER_SUPPLY_CONDITION, this);
  304. theDeviceController->RegisterEvent(SMART_CELL_SIGNAL_CABLE_STATE, this);
  305. theDeviceController->RegisterEvent(REDUNDANCY_STATE, this);
  306. theDeviceController->RegisterEvent(UPS_MODULE_ADDED, this);
  307. theDeviceController->RegisterEvent(UPS_MODULE_REMOVED, this);
  308. theDeviceController->RegisterEvent(UPS_MODULE_FAILED, this);
  309. theDeviceController->RegisterEvent(BATTERY_ADDED, this);
  310. theDeviceController->RegisterEvent(BATTERY_REMOVED, this);
  311. theDeviceController->RegisterEvent(IM_STATUS, this);
  312. theDeviceController->RegisterEvent(IM_OK, this);
  313. theDeviceController->RegisterEvent(IM_FAILED, this);
  314. theDeviceController->RegisterEvent(IM_INSTALLATION_STATE, this);
  315. theDeviceController->RegisterEvent(RIM_INSTALLATION_STATE, this);
  316. theDeviceController->RegisterEvent(RIM_STATUS, this);
  317. theDeviceController->RegisterEvent(RIM_OK, this);
  318. theDeviceController->RegisterEvent(RIM_FAILED, this);
  319. theDeviceController->RegisterEvent(SYSTEM_FAN_STATE, this);
  320. theDeviceController->RegisterEvent(BYPASS_CONTACTOR_STATE, this);
  321. theDeviceController->RegisterEvent(INPUT_BREAKER_STATE, this);
  322. theDeviceController->RegisterEvent(UPS_TEMPERATURE, this);
  323. theDeviceController->RegisterEvent(TOTAL_INVERTERS, this);
  324. theDeviceController->RegisterEvent(NUMBER_BAD_INVERTERS, this);
  325. theDeviceController->RegisterEvent(BAD_BATTERY_PACKS, this);
  326. theDeviceController->RegisterEvent(EXTERNAL_BATTERY_PACKS, this);
  327. theDeviceController->RegisterEvent(BATTERY_REPLACEMENT_CONDITION, this);
  328. // MeasureUPS Events
  329. theDeviceController->RegisterEvent(AMBIENT_TEMPERATURE,this);
  330. theDeviceController->RegisterEvent(HUMIDITY,this);
  331. theDeviceController->RegisterEvent(CONTACT_STATE,this);
  332. return err;
  333. }
  334. //-------------------------------------------------------------------
  335. INT ServerApplication::InitializeDeviceController()
  336. {
  337. INT err = theDeviceController->GetObjectStatus();
  338. // Make sure that the object constructed ok
  339. if (err == ErrNO_ERROR) {
  340. theDeviceControllerInitialized = IN_PROGRESS;
  341. err = theDeviceController->Initialize();
  342. if (err == ErrNO_ERROR) {
  343. theDeviceControllerInitialized = COMPLETED;
  344. }
  345. }
  346. return err;
  347. }
  348. //-------------------------------------------------------------------