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.

196 lines
5.5 KiB

  1. /*
  2. *
  3. * NOTES:
  4. *
  5. * REVISIONS:
  6. * ker03DEC92 Initial OS/2 Revision
  7. * pcy14Dec92: Changed READ_ONLY to AREAD_ONLY
  8. * jod05Apr93: Added changes for Deep Discharge
  9. * cad19Jul93: Battery Calibration fixed up
  10. * pcy10Sep93: Fixed to cancel on line fail and other rework
  11. * cad28Sep93: Not generating cancels on double sets
  12. * pcy08Apr94: Trim size, use static iterators, dead code removal
  13. * clk24Jun98: Added Pending Event to Update/Set
  14. * clk29Jul98: Initialized thePendingEvent to NULL (it was causing
  15. * the server to crash when shutting down server)
  16. *
  17. * v-stebe 29Jul2000 Fixed PREfix errors (bugs #46370, #46371)
  18. */
  19. #define INCL_BASE
  20. #define INCL_DOS
  21. #define INCL_NOPM
  22. #include "cdefine.h"
  23. extern "C" {
  24. #if (C_OS & C_OS2)
  25. #include <os2.h>
  26. #endif
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <malloc.h>
  30. #include <string.h>
  31. }
  32. #include "batcalt.h"
  33. #include "cfgmgr.h"
  34. #include "utils.h"
  35. #include "dispatch.h"
  36. #include "comctrl.h"
  37. #include "timerman.h"
  38. //Constructor
  39. BatteryCalibrationTestSensor :: BatteryCalibrationTestSensor(PDevice aParent,
  40. PCommController aCommController)
  41. : StateSensor(aParent,aCommController, BATTERY_CALIBRATION_TEST,AREAD_WRITE),
  42. theCalibrationCondition(0), thePendingEventTimerId(0), thePendingEvent(NULL)
  43. {
  44. storeState(NO_BATTERY_CALIBRATION_IN_PROGRESS);
  45. theCommController->RegisterEvent(BATTERY_CALIBRATION_CONDITION, this);
  46. }
  47. BatteryCalibrationTestSensor :: ~BatteryCalibrationTestSensor()
  48. {
  49. theCommController->UnregisterEvent(BATTERY_CALIBRATION_CONDITION, this);
  50. // if there's a pending event, then we want to cancel it
  51. if (thePendingEventTimerId) {
  52. _theTimerManager->CancelTimer(thePendingEventTimerId);
  53. thePendingEventTimerId = 0;
  54. }
  55. if (thePendingEvent)
  56. {
  57. delete thePendingEvent;
  58. thePendingEvent = NULL;
  59. }
  60. }
  61. INT BatteryCalibrationTestSensor::Validate(INT aCode, const PCHAR aValue)
  62. {
  63. INT retVal = ErrINVALID_VALUE; // easy default
  64. INT the_new_value=atoi(aValue);
  65. INT the_curr_value = atoi(theValue);
  66. if (aCode == theSensorCode) {
  67. if ((the_curr_value == NO_BATTERY_CALIBRATION_IN_PROGRESS) &&
  68. (the_new_value == BATTERY_CALIBRATION_IN_PROGRESS)) {
  69. retVal = ErrNO_ERROR;
  70. }
  71. else {
  72. if((the_curr_value == BATTERY_CALIBRATION_IN_PROGRESS) &&
  73. (the_new_value == NO_BATTERY_CALIBRATION_IN_PROGRESS)) {
  74. retVal = ErrNO_ERROR;
  75. }
  76. }
  77. }
  78. else if (aCode == BATTERY_CALIBRATION_CONDITION) {
  79. retVal = StateSensor::Validate(aCode, aValue);
  80. }
  81. else {
  82. retVal = ErrINVALID_CODE;
  83. }
  84. return retVal;
  85. }
  86. INT BatteryCalibrationTestSensor::Set(const PCHAR aValue)
  87. {
  88. INT action = atoi(aValue);
  89. INT the_current_value = atoi(theValue);
  90. CHAR tmpstring[11];
  91. PEvent cal_event = (PEvent)NULL;
  92. CHAR new_val[16];
  93. INT err = ErrNO_ERROR;
  94. if (action == PERFORM_BATTERY_CALIBRATION) {
  95. sprintf(new_val, "%d", BATTERY_CALIBRATION_IN_PROGRESS);
  96. }
  97. else if (action == CANCEL_BATTERY_CALIBRATION) {
  98. sprintf(new_val, "%d", NO_BATTERY_CALIBRATION_IN_PROGRESS);
  99. }
  100. err = Sensor::Set(new_val);
  101. switch(action) {
  102. case PERFORM_BATTERY_CALIBRATION:
  103. if(err == ErrNO_ERROR) {
  104. cal_event = new Event(BATTERY_CALIBRATION_CONDITION,
  105. BATTERY_CALIBRATION_IN_PROGRESS);
  106. }
  107. else if ((err != ErrNO_STATE_CHANGE) && (err != ErrINVALID_VALUE)) {
  108. cal_event = new Event(BATTERY_CALIBRATION_CONDITION,
  109. BATTERY_CALIBRATION_CANCELLED);
  110. if (cal_event != NULL) {
  111. cal_event->AppendAttribute(FAILURE_CAUSE, (float) err);
  112. }
  113. else {
  114. err = ErrMEMORY;
  115. }
  116. }
  117. break;
  118. case CANCEL_BATTERY_CALIBRATION:
  119. if (!err)
  120. {
  121. // Create a pending event
  122. Event pendingEvent(PENDING_EVENT, 0L);
  123. thePendingEventTimerId = _theTimerManager->SetTheTimer((ULONG)5,
  124. &pendingEvent, this);
  125. cal_event = new Event(BATTERY_CALIBRATION_CONDITION,
  126. BATTERY_CALIBRATION_CANCELLED);
  127. if (cal_event != NULL) {
  128. if (theCalibrationCondition == CANCELLED_LINEFAIL)
  129. cal_event->AppendAttribute(FAILURE_CAUSE,
  130. _itoa(LINE_BAD,tmpstring,10));
  131. theCalibrationCondition = FALSE;
  132. }
  133. else {
  134. err = ErrMEMORY;
  135. }
  136. }
  137. break;
  138. }
  139. if (cal_event)
  140. {
  141. UpdateObj::Update(cal_event);
  142. delete cal_event;
  143. cal_event = NULL;
  144. }
  145. return err;
  146. }
  147. INT BatteryCalibrationTestSensor::Update(PEvent anEvent)
  148. {
  149. INT err = ErrNO_ERROR;
  150. INT the_temp_code=anEvent->GetCode();
  151. PCHAR the_temp_value=anEvent->GetValue();
  152. // Do Sets sent by time delay (scheduled)
  153. //
  154. if (the_temp_code == BATTERY_CALIBRATION_TEST) {
  155. err = Set(the_temp_value);
  156. }
  157. // We want to check to see if the event is the
  158. // pending event set up in the Set.
  159. // This fix was put in to stop multiple events.
  160. if (thePendingEventTimerId) {
  161. if (anEvent->GetCode() == PENDING_EVENT) {
  162. thePendingEventTimerId = 0;
  163. }
  164. }
  165. else {
  166. StateSensor::Update(anEvent);
  167. }
  168. return err;
  169. }