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.

240 lines
5.9 KiB

  1. /*
  2. *
  3. * NOTES:
  4. *
  5. * REVISIONS:
  6. * pcy08Apr94: Trim size, use static iterators, dead code removal
  7. * pcy13Apr94: Use automatic variables decrease dynamic mem allocation
  8. * djs22Feb96: Added internal, external, and total packs
  9. * cgm12Apr96: Add destructor with unregister
  10. * poc28Sep96: Fixed SIR 4363.
  11. * djs29May97: Added update method for Symmetra events
  12. */
  13. #define INCL_BASE
  14. #define INCL_DOS
  15. #define INCL_NOPM
  16. #include "cdefine.h"
  17. extern "C" {
  18. #if (C_OS & C_OS2)
  19. #include <os2.h>
  20. #endif
  21. #include <stdlib.h>
  22. }
  23. #include "batpacks.h"
  24. #include "comctrl.h"
  25. #include "sensor.h"
  26. #include "event.h"
  27. #include "dispatch.h"
  28. #include "utils.h"
  29. NumberBatteryPacksSensor :: NumberBatteryPacksSensor(PDevice aParent,
  30. PCommController aCommController, PFirmwareRevSensor aFirmwareRev)
  31. :EepromSensor(aParent, aCommController, EXTERNAL_BATTERY_PACKS)
  32. {
  33. theFirmwareRev = aFirmwareRev;
  34. CHAR external_packs[32];
  35. // theValue of this sensor is the number of external battery packs.
  36. // Internal battery packs will be stored locally by this sensor.
  37. theFirmwareRev->Get(EXTERNAL_BATTERY_PACKS,external_packs);
  38. if (_strcmpi(external_packs,"YES") == 0)
  39. {
  40. theCommController->RegisterEvent(theSensorCode, this);
  41. }
  42. CHAR External_Battery_Packs_Changeable[32];
  43. theFirmwareRev->Get(EXTERNAL_PACKS_CHANGEABLE,External_Battery_Packs_Changeable);
  44. if (_strcmpi(External_Battery_Packs_Changeable, "Yes") ==0) {
  45. readOnly = AREAD_WRITE;
  46. setInitialValue();
  47. }
  48. CHAR Internal_Packs[32];
  49. theFirmwareRev->Get(INTERNAL_BATTERY_PACKS,Internal_Packs);
  50. theNumber_Of_Internal_Packs = atoi(Internal_Packs);
  51. // Disable validation checking until the sensor value
  52. // is initialized
  53. theSensorIsInitialized = 0;
  54. if (!theFirmwareRev->IsBackUpsPro()) {
  55. DeepGet();
  56. }
  57. }
  58. INT NumberBatteryPacksSensor::Get(INT aCode, PCHAR aValue)
  59. {
  60. INT err = ErrNO_ERROR;
  61. switch(aCode)
  62. {
  63. case EXTERNAL_BATTERY_PACKS:
  64. err = Sensor::Get(aValue);
  65. if (strlen(aValue) > 2) {
  66. aValue[0] = aValue[1];
  67. aValue[1] = aValue[2];
  68. aValue[2] = aValue[3]; // copy null terminator
  69. }
  70. break;
  71. case INTERNAL_BATTERY_PACKS:
  72. _itoa(theNumber_Of_Internal_Packs,aValue,10);
  73. break;
  74. case TOTAL_BATTERY_PACKS:
  75. {
  76. CHAR External_Packs[32];
  77. err = Sensor::Get(External_Packs);
  78. INT Total_Batteries;
  79. Total_Batteries = theNumber_Of_Internal_Packs +
  80. atoi(External_Packs);
  81. _itoa(Total_Batteries,aValue,10);
  82. }
  83. break;
  84. default:
  85. err = Sensor::Set(aCode, aValue);
  86. break;
  87. }
  88. return err;
  89. }
  90. INT NumberBatteryPacksSensor::Set(INT aCode, const PCHAR aValue)
  91. {
  92. INT err = ErrNO_ERROR;
  93. switch(aCode)
  94. {
  95. case EXTERNAL_BATTERY_PACKS:
  96. {
  97. // Left pad number of battery packs with zeros
  98. // Default: set number of external battery packs to zero
  99. if (strlen(aValue) == 0) {
  100. aValue[3] = NULL;
  101. aValue[2] = '0';
  102. aValue[1] = '0';
  103. }
  104. if (strlen(aValue) == 1) {
  105. aValue[3] = aValue[1]; // copy null terminator
  106. aValue[2] = aValue[0];
  107. aValue[1] = '0';
  108. }
  109. if (strlen(aValue) == 2) {
  110. aValue[3] = aValue[2]; // copy null terminator
  111. aValue[2] = aValue[1];
  112. aValue[1] = aValue[0];
  113. }
  114. aValue[0] = '0';
  115. err = Sensor::Set(aValue);
  116. }
  117. break;
  118. default:
  119. err = Sensor::Set(aCode, aValue);
  120. break;
  121. }
  122. return err;
  123. }
  124. INT NumberBatteryPacksSensor::Set(const PCHAR aValue)
  125. {
  126. return (Set(EXTERNAL_BATTERY_PACKS, aValue));
  127. }
  128. INT NumberBatteryPacksSensor::Update(PEvent anEvent)
  129. {
  130. INT err = ErrNO_ERROR;
  131. switch (anEvent->GetCode()) {
  132. case EXTERNAL_BATTERY_PACKS:
  133. err = storeValue(anEvent->GetValue());
  134. break;
  135. default:
  136. err = EepromSensor::Update(anEvent);
  137. break;
  138. }
  139. return err;
  140. }
  141. INT NumberBatteryPacksSensor::storeValue(const PCHAR aValue)
  142. {
  143. CHAR Ups_Is_A_Matrix[32];
  144. theFirmwareRev ->Get(IS_MATRIX,Ups_Is_A_Matrix);
  145. if (_strcmpi(Ups_Is_A_Matrix,"Yes") ==0) {
  146. //
  147. // Initialize curr_num so if theValue is not set (should only be first
  148. // time thru) we will generate a CHECK_CABLE event
  149. //
  150. INT curr_num = -1;
  151. if(theValue) {
  152. curr_num = atoi(theValue);
  153. }
  154. INT new_count = atoi(aValue);
  155. if(new_count == 0 && curr_num != 0) {
  156. // create Check signal cable Event
  157. Event tmp(SMART_CELL_SIGNAL_CABLE_STATE, CHECK_CABLE);
  158. UpdateObj::Update(&tmp);
  159. }
  160. if(new_count != 0 && curr_num == 0) {
  161. // create Ignore battery good event. The UPS sends a battery good
  162. // when plugging back in the cable. Another UPSLink-ism.
  163. Event tmp(SMART_CELL_SIGNAL_CABLE_STATE, IGNORE_BATTERY_GOOD);
  164. UpdateObj::Update(&tmp);
  165. }
  166. }
  167. // Check for an new battery and generate an event
  168. if (theSensorIsInitialized)
  169. {
  170. if (strcmp(aValue, theValue) > 0)
  171. {
  172. PEvent added_event = new Event(BATTERY_ADDED, "");
  173. UpdateObj::Update(added_event);
  174. delete added_event;
  175. added_event = NULL;
  176. }
  177. // Check for a battery removal and generate an event
  178. if (strcmp(aValue, theValue) < 0)
  179. {
  180. PEvent removed_event = new Event(BATTERY_REMOVED, "");
  181. UpdateObj::Update(removed_event);
  182. delete removed_event;
  183. removed_event = NULL;
  184. }
  185. }
  186. else
  187. {
  188. theSensorIsInitialized = 1;
  189. }
  190. Sensor::storeValue(aValue);
  191. return ErrNO_ERROR;
  192. }
  193. NumberBatteryPacksSensor :: ~NumberBatteryPacksSensor()
  194. {
  195. theCommController->UnregisterEvent(theSensorCode, this);
  196. }