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.

344 lines
9.2 KiB

  1. /*
  2. *
  3. * NOTES:
  4. *
  5. * REVISIONS:
  6. * rct11Dec92 compiled w/o the TurnOffUpsOnBatterySensor
  7. * SjA15Dec92 Fixed macro problems and now registers for events.
  8. * jod16Dec92: Moved cdefine.h to top of file.
  9. * pcy27Dec92: Parent is now an UpdateObj, not a DeviceController
  10. * pcy27Dec92: Sensors no longer initialized explicitly. Done during construct
  11. * ane11Jan93: Added generation of a RUN_TIME_EXPIRED event
  12. * pcy02Feb93: Scoped declaration of run_time_remaining so compilers dont error
  13. * ane03Feb93: Added destructors, commented out one of the timers being set
  14. * pcy1&Feb93: Set value to RUN_TIME_EXPIRED for RUN_TIME_EXPIRED event
  15. * ajr22Feb93: moved process.h into C_OS2 cond
  16. * cad04Aug93: Changed shutdown handling for status
  17. * cad14Sep93: looking at shutdown_status to set shutdown flag
  18. * pcy20Sep93: Reset theRunTimeRemaining when setting line good
  19. * cad27Sep93: Added enabling battery run time
  20. * cad02Nov93: name fix
  21. * cad15Nov93: Changed how comm lost handled
  22. * cad10Dec93: firmware rev get added
  23. * pcy28Jan94: Handle run time timer different (now in Shutdowner)
  24. * ajr08Feb94: Fixed TIMED_RUN_TIME_REMAINING case
  25. * cad08Jan94: removed run time enabled stuff (assumed by flex shutdown)
  26. * pcy04Mar94: Make sure ups gets shutdown events
  27. * pcy08Apr94: Trim size, use static iterators, dead code removal
  28. * rct03Jun94: Added functionality for tracking xfer cause and time on battery
  29. * jps20jul94: itoa -> ltoa (theUpsState); added some (time_t *)s
  30. * ajr02Aug94: Lets set the line to bad if we are in bad bat state
  31. * ajr02Aug94: moved BACKUPS_FIRMWARE_REV definition to backups.h
  32. * ajr14Feb96: SINIX Merge
  33. * djs16Jul96: Added IS_BACKUPS
  34. * mds29Dec97: Enabled the TurnOffUpsOnBatterySensor to correctly shutdown
  35. * UPS when using a Share-Ups in confirmed mode.
  36. * tjg12Jan98: Undid the changes to TurnOffUpsOnBatterySensor
  37. * clk09Sep98: Reworked LineBad condition in HandleBadBatteryCond to always
  38. * setBatteryBad (fixes SIR 6019).
  39. */
  40. #define INCL_BASE
  41. #define INCL_DOS
  42. #define INCL_NOPM
  43. #include "cdefine.h"
  44. #include "_defs.h"
  45. extern "C" {
  46. #if (C_OS & C_OS2)
  47. #include <os2.h>
  48. #include <netcons.h>
  49. #include <process.h>
  50. #endif
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <time.h>
  54. #include <stdio.h>
  55. #include <ctype.h>
  56. }
  57. #include "err.h"
  58. #include "backups.h"
  59. #include "simpsens.h"
  60. #include "event.h"
  61. #include "codes.h"
  62. #include "dispatch.h"
  63. #include "cfgcodes.h"
  64. #include "cfgmgr.h"
  65. #include "timerman.h"
  66. #include "utils.h" /* ajr 02/22/93 */
  67. _CLASSDEF(DeviceController)
  68. _CLASSDEF(UnsupportedSensor)
  69. //-------------------------------------------------------------------
  70. BackUps :: BackUps(PUpdateObj aDeviceController,
  71. PCommController aCommController)
  72. : Ups(aDeviceController, aCommController),
  73. theRunTimeExpiration(0),
  74. theOnBatteryTimer(0L),
  75. theLastTransferCause(NO_TRANSFERS)
  76. {
  77. theUtilityLineConditionSensor =
  78. new UtilityLineConditionSensor(this, aCommController);
  79. theBatteryConditionSensor =
  80. new BatteryConditionSensor(this, aCommController);
  81. theTurnOffUpsOnBatterySensor =
  82. new TurnOffUpsOnBatterySensor(this, aCommController);
  83. }
  84. BackUps :: ~BackUps()
  85. {
  86. delete theUtilityLineConditionSensor;
  87. theUtilityLineConditionSensor = NULL;
  88. delete theBatteryConditionSensor;
  89. theBatteryConditionSensor = NULL;
  90. delete theTurnOffUpsOnBatterySensor;
  91. theTurnOffUpsOnBatterySensor = NULL;
  92. }
  93. //-------------------------------------------------------------------
  94. INT BackUps :: Update(PEvent anEvent)
  95. {
  96. INT err = ErrNO_ERROR;
  97. switch(anEvent->GetCode()) {
  98. case UTILITY_LINE_CONDITION:
  99. HandleLineConditionEvent(anEvent);
  100. break;
  101. case BATTERY_CONDITION:
  102. HandleBatteryConditionEvent(anEvent);
  103. break;
  104. case ADMIN_SHUTDOWN:
  105. SET_BIT(theUpsState, SHUTDOWN_IN_PROGRESS_BIT);
  106. break;
  107. case CANCEL_SHUTDOWN:
  108. CLEAR_BIT(theUpsState, SHUTDOWN_IN_PROGRESS_BIT);
  109. break;
  110. default:
  111. UpdateObj::Update(anEvent);
  112. break;
  113. }
  114. return err;
  115. }
  116. //-------------------------------------------------------------------
  117. INT BackUps :: Get(INT code, PCHAR aValue)
  118. {
  119. INT err = ErrNO_ERROR;
  120. // This is a quick and dirty implementation. We should probably
  121. // look for the code in Device::theSensorList and then send the
  122. // get to the sensor that matches the code. For now this will do.
  123. // We may have to worry about get codes that dont have sensors if
  124. // we use the above approach. Maybe everything should have a sensor.
  125. CHAR state[10];
  126. switch(code) {
  127. case FIRMWARE_REV:
  128. strcpy(aValue, BACKUPS_FIRMWARE_REV);
  129. break;
  130. case BATTERY_CONDITION:
  131. err = theBatteryConditionSensor->Get(code, aValue);
  132. break;
  133. case UTILITY_LINE_CONDITION:
  134. err = theUtilityLineConditionSensor->Get(code, aValue);
  135. break;
  136. case UPS_STATE:
  137. strcpy(aValue, _ltoa(theUpsState,state,10));
  138. break;
  139. case UPS_MODEL :
  140. strcpy(aValue, "Back-UPS");
  141. break;
  142. case TIMED_RUN_TIME_REMAINING:
  143. err=ErrUNSUPPORTED;
  144. break;
  145. case MAX_BATTERY_RUN_TIME:
  146. _theConfigManager->Get(CFG_UPS_MAX_BATTERY_RUN_TIME, aValue);
  147. break;
  148. case TIME_ON_BATTERY:
  149. if (theOnBatteryTimer == 0)
  150. {
  151. strcpy(aValue, "0");
  152. }
  153. else
  154. {
  155. _ltoa((LONG)((time((time_t *)NULL) - theOnBatteryTimer)), aValue, 10);
  156. }
  157. break;
  158. case IS_BACKUPS:
  159. strcpy(aValue,"Yes");
  160. break;
  161. default:
  162. err = ErrINVALID_CODE;
  163. break;
  164. }
  165. return err;
  166. }
  167. //-------------------------------------------------------------------
  168. INT BackUps :: Set(INT code, PCHAR value)
  169. {
  170. INT err = ErrNO_ERROR;
  171. switch(code)
  172. {
  173. case TURN_OFF_UPS_ON_BATTERY:
  174. theTurnOffUpsOnBatterySensor->Set(code, value);
  175. break;
  176. case MAX_BATTERY_RUN_TIME:
  177. //_theConfigManager->Set(CFG_UPS_MAX_BATTERY_RUN_TIME, value);
  178. break;
  179. default:
  180. err = ErrUNSUPPORTED;
  181. break;
  182. }
  183. return err;
  184. }
  185. //-------------------------------------------------------------------
  186. VOID BackUps :: HandleLineConditionEvent(PEvent anEvent)
  187. {
  188. switch(atoi(anEvent->GetValue()))
  189. {
  190. case LINE_GOOD:
  191. theLastTransferCause = NO_TRANSFERS;
  192. theOnBatteryTimer = 0;
  193. theRunTimeExpiration = 0;
  194. setLineGood();
  195. break;
  196. case LINE_BAD:
  197. theLastTransferCause = BLACKOUT;
  198. theOnBatteryTimer = time((time_t *)NULL);
  199. theRunTimeExpiration = 1;
  200. setLineBad();
  201. break;
  202. }
  203. UpdateObj::Update(anEvent);
  204. }
  205. //-------------------------------------------------------------------
  206. VOID BackUps :: HandleBatteryConditionEvent(PEvent anEvent)
  207. {
  208. INT send_update = TRUE;
  209. switch(atoi(anEvent->GetValue()))
  210. {
  211. case BATTERY_GOOD:
  212. if(!(IS_STATE(UPS_STATE_BATTERY_BAD)))
  213. {
  214. send_update = FALSE;
  215. }
  216. setBatteryGood();
  217. break;
  218. case BATTERY_BAD:
  219. // When we get a bad battery, we always want to call setBadBattery
  220. // because it performs the test for on-line/on-battery. There's
  221. // no need to perform the test here.
  222. setBatteryBad(anEvent);
  223. break;
  224. }
  225. if(send_update)
  226. {
  227. UpdateObj::Update(anEvent);
  228. }
  229. }
  230. //-------------------------------------------------------------------
  231. VOID BackUps :: setLineGood()
  232. {
  233. CLEAR_BIT(theUpsState, LINE_STATUS_BIT);
  234. CLEAR_BIT(theUpsState, LINE_FAIL_PENDING_BIT);
  235. theRunTimeExpiration = FALSE;
  236. }
  237. //-------------------------------------------------------------------
  238. VOID BackUps :: setLineBad()
  239. {
  240. CLEAR_BIT(theUpsState, LINE_FAIL_PENDING_BIT);
  241. SET_BIT(theUpsState, LINE_STATUS_BIT);
  242. theRunTimeExpiration = TRUE;
  243. if (isBatteryBad())
  244. {
  245. Event batteryEvent(BATTERY_CONDITION, LOW_BATTERY);
  246. UpdateObj::Update(&batteryEvent);
  247. }
  248. }
  249. //-------------------------------------------------------------------
  250. VOID BackUps :: setBatteryGood()
  251. {
  252. CLEAR_BIT(theUpsState, BATTERY_STATUS_BIT);
  253. }
  254. //-------------------------------------------------------------------
  255. VOID BackUps :: setBatteryBad(PEvent anEvent)
  256. {
  257. SET_BIT(theUpsState, BATTERY_STATUS_BIT);
  258. if (isOnBattery()) {
  259. anEvent->SetValue(LOW_BATTERY);
  260. } else {
  261. anEvent->SetValue(BATTERY_DISCHARGED);
  262. }
  263. }
  264. //-------------------------------------------------------------------
  265. VOID BackUps :: setLineFailPending()
  266. {
  267. SET_BIT(theUpsState, LINE_FAIL_PENDING_BIT);
  268. }
  269. //-------------------------------------------------------------------
  270. VOID BackUps :: registerForEvents()
  271. {
  272. theUtilityLineConditionSensor->RegisterEvent(UTILITY_LINE_CONDITION, this);
  273. theBatteryConditionSensor->RegisterEvent(BATTERY_CONDITION, this);
  274. theDeviceController->RegisterEvent(ADMIN_SHUTDOWN, this);
  275. theDeviceController->RegisterEvent(CANCEL_SHUTDOWN, this);
  276. theDeviceController->RegisterEvent(SHUTDOWN, this);
  277. }