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.

245 lines
6.8 KiB

  1. /*
  2. *
  3. * NOTES:
  4. *
  5. * REVISIONS:
  6. * ker07DEC92: Initial OS/2 revision
  7. * ker14DEC92: fleshed out the methods
  8. * pcy17Dec92: Set should not use const PCHAR
  9. * pcy26Jan93: Added SetEepromAccess()
  10. * pcy02Feb93: Made Set() return value for all cases
  11. * cad03Sep93: Fixed problem where date not being cached, but cache used
  12. * cad07Oct93: Plugging Memory Leaks
  13. * cad11Nov93: Making sure all timers are cancelled on destruction
  14. * cad04Mar94: fixes for access code problem
  15. * pcy13Apr94: Use automatic variables decrease dynamic mem allocation
  16. * djs22Feb96: changed to new firmware rev interface
  17. * cgm16Apr96: Add unregister to destructor
  18. * ntf30Jul97: Added code after strncpy to cater for situation where
  19. * no '\0' added.
  20. * ntf30Jul97: Added 1 to the size of theReplaceDate and theAgeLimit as, well,
  21. * theReplaceDate anyway, was being copied to by a strncpy which
  22. * said its length excluding room for NULL char was
  23. * REPLACE_DATE_MAX which was actuall its size including NULL.
  24. * tjg02Mar98: Added theReplaceBatterySensor->DeepGet() in Reinitialize()
  25. */
  26. #include "cdefine.h"
  27. #define INCL_BASE
  28. #define INCL_DOS
  29. #define INCL_NOPM
  30. extern "C" {
  31. #if (C_OS & C_OS2)
  32. #include <os2.h>
  33. #endif
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <malloc.h>
  37. #include <string.h>
  38. }
  39. #include "_defs.h"
  40. #include "apc.h"
  41. #include "battmgr.h"
  42. #include "battrep.h"
  43. #include "replbatt.h"
  44. #include "cfgmgr.h"
  45. #include "unssens.h"
  46. #include "timerman.h"
  47. #include "eeprom.h"
  48. #include "smartups.h"
  49. #if (C_OS & C_UNIX)
  50. #include "utils.h"
  51. #endif
  52. #define REPLACE_DATE_MAX 32
  53. BatteryReplacementManager::BatteryReplacementManager(PUpdateObj aParent,
  54. PCommController aCommController,
  55. PFirmwareRevSensor aFirmwareRevSensor)
  56. :Device(aParent, aCommController), theTimerId(0)
  57. {
  58. theReplaceDate=new CHAR[REPLACE_DATE_MAX+1];
  59. theAgeLimit=new CHAR[REPLACE_DATE_MAX+1];
  60. theParent=aParent;
  61. theCommController=aCommController;
  62. CHAR Battery_Replacement_Date_Capable[32];
  63. aFirmwareRevSensor->Get(IS_BATTERY_DATE,Battery_Replacement_Date_Capable);
  64. if (_strcmpi(Battery_Replacement_Date_Capable,"No") == 0)
  65. {
  66. theBatteryReplacementDateSensor = &_theUnsupportedSensor;
  67. _theConfigManager->Get(CFG_BATTERY_REPLACEMENT_DATE, theReplaceDate);
  68. }
  69. else {
  70. theBatteryReplacementDateSensor=new BatteryReplacementDateSensor(this, theCommController);
  71. theBatteryReplacementDateSensor->Get(BATTERY_REPLACEMENT_DATE, theReplaceDate);
  72. }
  73. _theConfigManager->Get(CFG_BATTERY_AGE_LIMIT, theAgeLimit);
  74. theReplaceBatterySensor = new ReplaceBatterySensor(this, theCommController);
  75. theReplaceBatterySensor->RegisterEvent(BATTERY_REPLACEMENT_CONDITION, this);
  76. }
  77. BatteryReplacementManager::~BatteryReplacementManager()
  78. {
  79. if (theTimerId) {
  80. _theTimerManager->CancelTimer(theTimerId);
  81. theTimerId = 0;
  82. }
  83. if(theBatteryReplacementDateSensor &&
  84. (theBatteryReplacementDateSensor != &_theUnsupportedSensor)) {
  85. delete theBatteryReplacementDateSensor;
  86. theBatteryReplacementDateSensor = NULL;
  87. }
  88. if (theReplaceBatterySensor){
  89. theReplaceBatterySensor->UnregisterEvent(BATTERY_REPLACEMENT_CONDITION, this);
  90. delete theReplaceBatterySensor;
  91. theReplaceBatterySensor = NULL;
  92. }
  93. if (theReplaceDate) {
  94. delete[] theReplaceDate;
  95. theReplaceDate = NULL;
  96. }
  97. if (theAgeLimit) {
  98. delete[] theAgeLimit;
  99. theAgeLimit = NULL;
  100. }
  101. }
  102. INT BatteryReplacementManager::Get(INT aCode, PCHAR aValue)
  103. {
  104. INT err = ErrNO_ERROR;
  105. switch(aCode)
  106. {
  107. case BATTERY_REPLACEMENT_DATE:
  108. strcpy(aValue, theReplaceDate);
  109. break;
  110. case BATTERY_AGE_LIMIT:
  111. strcpy(aValue, theAgeLimit);
  112. break;
  113. case LIGHTS_TEST:
  114. err = theParent->Get(aCode, aValue);
  115. break;
  116. default:
  117. err =ErrINVALID_CODE;
  118. break;
  119. }
  120. return err;
  121. }
  122. INT BatteryReplacementManager::Set(INT aCode, const PCHAR aValue)
  123. {
  124. INT err = ErrNO_ERROR;
  125. switch(aCode) {
  126. case BATTERY_REPLACEMENT_DATE:
  127. strncpy(theReplaceDate, aValue, REPLACE_DATE_MAX);
  128. //ntf30Jul97: Added next line in case theReplaceDate was full.
  129. *(theReplaceDate + REPLACE_DATE_MAX) = '\0';
  130. theBatteryReplacementDateSensor->Set(aCode, theReplaceDate);
  131. //err = _theConfigManager->Set(CFG_BATTERY_REPLACEMENT_DATE,
  132. // theReplaceDate);
  133. break;
  134. case BATTERY_AGE_LIMIT:
  135. //err = _theConfigManager->Set(aCode, aValue);
  136. break;
  137. default:
  138. err = ErrINVALID_CODE;
  139. break;
  140. }
  141. return err;
  142. }
  143. INT BatteryReplacementManager::Update(PEvent ev)
  144. {
  145. if ((ev->GetCode() == BATTERY_REPLACEMENT_CONDITION) &&
  146. (atoi(ev->GetValue()) == BATTERY_NEEDS_REPLACING)) {
  147. theTimerId = 0;
  148. }
  149. return Device::Update(ev);
  150. }
  151. INT BatteryReplacementManager::SetReplacementTimer(void)
  152. {
  153. //Get Date Battery Was Replaced/Installed
  154. CHAR the_temp_string[REPLACE_DATE_MAX];
  155. strcpy(the_temp_string, theReplaceDate);
  156. the_temp_string[2]='\0';
  157. the_temp_string[5]='\0';
  158. INT the_birth_month=atoi(the_temp_string);
  159. PCHAR the_day_pointer=&(the_temp_string[3]);
  160. INT the_birth_day=atoi(the_day_pointer);
  161. PCHAR the_year_pointer=&(the_temp_string[6]);
  162. INT the_birth_year=atoi(the_year_pointer);
  163. //Get Lifespan of Battery
  164. INT the_battery_lifespan=atoi(theAgeLimit);
  165. //Calculate the month of death
  166. INT the_month=the_birth_month+=(the_battery_lifespan%12);
  167. INT the_year=the_birth_year+=(the_battery_lifespan/12);
  168. if(the_year>=100)
  169. the_year-=100;
  170. //Create a DateTimeObj with that date
  171. DateTimeObj my_date_time_obj(the_year, the_month, the_birth_day, 0L,
  172. 0L, 0L);
  173. //Create the Event
  174. Event the_timer_event(BATTERY_REPLACEMENT_CONDITION,
  175. BATTERY_NEEDS_REPLACING);
  176. //Send it to the Timer manager and forget it
  177. theTimerId = _theTimerManager->SetTheTimer(&my_date_time_obj,
  178. &the_timer_event, this);
  179. return TRUE;
  180. }
  181. VOID
  182. BatteryReplacementManager :: SetEepromAccess(INT anAccessCode)
  183. {
  184. ((PEepromChoiceSensor)theBatteryReplacementDateSensor)->SetEepromAccess(anAccessCode);
  185. }
  186. VOID BatteryReplacementManager :: GetAllowedValue(INT code, CHAR *aValue)
  187. {
  188. ((PSmartUps)theParent)->GetAllowedValue(code, aValue);
  189. }
  190. VOID BatteryReplacementManager::Reinitialize()
  191. {
  192. theBatteryReplacementDateSensor->DeepGet();
  193. theReplaceBatterySensor->DeepGet();
  194. }