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.

295 lines
9.6 KiB

  1. /*
  2. *
  3. * NOTES:
  4. *
  5. * REVISIONS:
  6. * ker23NOV92 Initial OS/2 Revision
  7. * pcy07Dec92: DeepGet no longer needs aCode
  8. * pcy11Dec92: Rework
  9. * pcy16Feb93: Have DeepGet return the value it gets event if error
  10. * cad07Oct93: Plugging Memory Leaks
  11. * ajr09Dec93: Added some error condition alerts through _theErrorLogger
  12. * cad16Dec93: fixed lookupsensorname
  13. * rct06Jan94: fixed sensor IsaCode stuff & lookupSensorName()
  14. * cad04Mar94: fix for mups sensor isa translation
  15. * pcy08Apr94: Trim size, use static iterators, dead code removal
  16. * pcy13Apr94: Use automatic variables decrease dynamic mem allocation
  17. * awm27Oct97: Added performance monitor offset to sensor class, and initialized
  18. * counters here
  19. * dma16Dec97: Changed DeepGet implementation to make an Update call.
  20. * awm14Jan98: Removed performance monitor offset to sensor class
  21. * clk11Feb98: Added DeepGetWithoutUpdate function
  22. * mholly25Sep98 : change the buffer size in DeepGet & DeepGetWithoutUpdate
  23. * to MAX_UPS_LINK_RESPONSE_LENGTH - now set at 256, granted
  24. * an arbitrary value, but it was set at 32, a value that was
  25. * too small to hold the allowed return of ^A
  26. */
  27. #include "cdefine.h"
  28. #define INCL_BASE
  29. #define INCL_DOS
  30. #define INCL_NOPM
  31. extern "C" {
  32. #if (C_OS & C_OS2)
  33. #include <os2.h>
  34. #endif
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <malloc.h>
  38. #include <string.h>
  39. }
  40. #include "apc.h"
  41. #include "sensor.h"
  42. #include "utils.h"
  43. #include "comctrl.h"
  44. #include "event.h"
  45. #include "errlogr.h"
  46. #include "isa.h"
  47. #define MAX_UPS_LINK_RESPONSE_LENGTH 256
  48. //Constructor
  49. Sensor :: Sensor(PDevice aParent,
  50. PCommController aCommController,
  51. INT aSensorCode,
  52. ACCESSTYPE anACCESSTYPE)
  53. : UpdateObj()
  54. {
  55. theDevice = aParent;
  56. theCommController = aCommController;
  57. theSensorCode = aSensorCode;
  58. readOnly = anACCESSTYPE;
  59. theValue = (PCHAR)NULL;
  60. }
  61. //Destructor
  62. Sensor :: ~Sensor()
  63. {
  64. if(theValue)
  65. free(theValue);
  66. }
  67. //Member Functions
  68. INT Sensor::Get(PCHAR aValue)
  69. {
  70. INT err = ErrNO_ERROR;
  71. if(theValue) {
  72. strcpy(aValue,theValue);
  73. }
  74. else {
  75. err = ErrNO_VALUE;
  76. }
  77. return err;
  78. }
  79. //Default behavior is to only support gets and sets on your own code
  80. // this does a deep get to the UPS, but will not update the sensor value
  81. INT Sensor::DeepGetWithoutUpdate(PCHAR aValue)
  82. {
  83. CHAR the_temp_value[MAX_UPS_LINK_RESPONSE_LENGTH] = {NULL};
  84. INT err = ErrNO_ERROR;
  85. if ((err = theCommController->Get(theSensorCode, the_temp_value)) == ErrNO_ERROR) {
  86. if(aValue) {
  87. strcpy(aValue, the_temp_value);
  88. }
  89. }
  90. return err;
  91. }
  92. INT Sensor::DeepGet(PCHAR aValue)
  93. {
  94. CHAR the_temp_value[MAX_UPS_LINK_RESPONSE_LENGTH];
  95. strcpy(the_temp_value,"");
  96. INT err = ErrNO_ERROR;
  97. if((err = theCommController->Get(theSensorCode, the_temp_value)) == ErrNO_ERROR) {
  98. if((err = Validate(theSensorCode, the_temp_value)) == ErrNO_ERROR) {
  99. storeValue(the_temp_value);
  100. PEvent sensor_event = new Event(theSensorCode, theValue);
  101. err = UpdateObj::Update(sensor_event);
  102. delete sensor_event;
  103. sensor_event = NULL;
  104. }
  105. else {
  106. #ifdef AJR_DEBUG
  107. CHAR tmp_error[80];
  108. sprintf(tmp_error,"Sensor::Validate fails with %d\0",err);
  109. _theErrorLogger->LogError(tmp_error,__FILE__,__LINE__);
  110. #endif
  111. }
  112. } else {
  113. #ifdef AJR_DEBUG
  114. CHAR tmp_error[80];
  115. sprintf(tmp_error,"Sensor::DeepGet theommController->Get fails with %d\0",err);
  116. _theErrorLogger->LogError(tmp_error,__FILE__,__LINE__);
  117. #endif
  118. }
  119. if(aValue) {
  120. strcpy(aValue, the_temp_value);
  121. }
  122. return err;
  123. }
  124. INT
  125. Sensor::storeValue(const PCHAR aValue)
  126. {
  127. if(aValue) {
  128. UtilStoreString(theValue, aValue);
  129. }
  130. return ErrNO_ERROR;
  131. }
  132. INT Sensor::Get(INT aCode, PCHAR aValue)
  133. {
  134. if(aCode == theSensorCode)
  135. return Get(aValue);
  136. else
  137. return ErrINVALID_CODE;
  138. }
  139. INT Sensor::Set(const PCHAR aValue)
  140. {
  141. INT err = ErrNO_ERROR;
  142. if(readOnly == AREAD_ONLY) {
  143. err = ErrREAD_ONLY;
  144. }
  145. else {
  146. if(aValue) {
  147. if((err = Validate(theSensorCode, aValue)) == ErrNO_ERROR) {
  148. if((err = theCommController->Set(theSensorCode, aValue)) == ErrNO_ERROR) {
  149. storeValue(aValue);
  150. }
  151. }
  152. }
  153. }
  154. return err;
  155. }
  156. INT Sensor::Set(INT aCode, const PCHAR aValue)
  157. {
  158. INT err = ErrNO_ERROR;
  159. if(aCode == theSensorCode)
  160. err = Set(aValue);
  161. else
  162. err = ErrINVALID_CODE;
  163. return err;
  164. }
  165. INT Sensor::Update(PEvent anEvent)
  166. {
  167. INT err = ErrNO_ERROR;
  168. INT the_temp_code;
  169. PCHAR the_temp_value;
  170. the_temp_code=anEvent->GetCode();
  171. the_temp_value=anEvent->GetValue();
  172. if((err = Validate(the_temp_code, the_temp_value)) == ErrNO_ERROR) {
  173. storeValue(the_temp_value);
  174. err = UpdateObj::Update(anEvent);
  175. }
  176. return err;
  177. }
  178. typedef struct IsaCode {
  179. INT isa_code;
  180. PCHAR sensor_name;
  181. } ISACODE;
  182. static ISACODE isa_codes[] = {
  183. { ABNORMALCONDITIONSENSOR, "AbnormalConditionSensor" },
  184. { ALARMDELAYSENSOR, "AlarmDelaySensor" },
  185. { BATTERYCALIBRATIONTESTSENSOR, "BatteryCalibrationTestSensor" },
  186. { BATTERYCAPACITYSENSOR, "BatteryCapacitySensor" },
  187. { BATTERYCONDITIONSENSOR, "BatteryConditionSensor" },
  188. { BATTERYREPLACEMENTDATESENSOR, "BatteryReplacementDateSensor" },
  189. { BATTERYVOLTAGESENSOR, "BatteryVoltageSensor" },
  190. { COMMUNICATIONSTATESENSOR, "CommunicationStateSensor" },
  191. { COPYRIGHTSENSOR, "CopyrightSensor" },
  192. { DIPSWITCHPOSITIONSENSOR, "DipSwitchPositionSensor" },
  193. { FIRMWAREREVSENSOR, "FirmwareRevSensor" },
  194. { OUTPUTFREQUENCYSENSOR, "FrequencySensor" },
  195. { HIGHTRANSFERVOLTAGESENSOR, "HighTransferVoltageSensor" },
  196. { LINEVOLTAGESENSOR, "LineVoltageSensor" },
  197. { LIGHTSTESTSENSOR, "LightsTestSensor" },
  198. { LOWBATTERYDURATIONSENSOR, "LowBatteryDurationSensor" },
  199. { LOWTRANSFERVOLTAGESENSOR, "LowTransferVoltageSensor" },
  200. { MANUFACTUREDATESENSOR, "ManufactureDateSensor" },
  201. { MAXLINEVOLTAGESENSOR, "MaxLineVoltageSensor" },
  202. { MINLINEVOLTAGESENSOR, "MinLineVoltageSensor" },
  203. { MINRETURNCAPACITYSENSOR, "MinReturnCapacitySensor" },
  204. { OUTPUTVOLTAGESENSOR, "OutputVoltageSensor" },
  205. { OVERLOADSENSOR, "OverloadSensor" },
  206. { RATEDBATTERYVOLTAGESENSOR, "RatedBatteryVoltageSensor" },
  207. { REPLACEBATTERYSENSOR, "ReplaceBatterySensor" },
  208. { RATEDLINEVOLTAGESENSOR, "RatedLineVoltageSensor" },
  209. { RATEDOUTPUTVOLTAGESENSOR, "RatedOutputVoltageSensor" },
  210. { RUNTIMEREMAININGSENSOR, "RunTimeRemainingSensor" },
  211. { SELFTESTRESULTSENSOR, "SelfTestResultSensor" },
  212. { SELFTESTSCHEDULESENSOR, "SelfTestScheduleSensor" },
  213. { SHUTDOWNDELAYSENSOR, "ShutdownDelaySensor" },
  214. { SMARTBOOSTSENSOR, "SmartBoostSensor" },
  215. // unused { TURNOFFUPSIMMEDIATELYSENSOR, "TurnOffUpsImmediatelySensor" },
  216. { TURNOFFWITHDELAYSENSOR, "TurnOffWithDelaySensor" },
  217. { TRANSFERCAUSESENSOR, "TransferCauseSensor" },
  218. { TRIPREGISTERSENSOR, "TripRegisterSensor" },
  219. { TURNONDELAYSENSOR, "TurnOnDelaySensor" },
  220. { TURNOFFUPSONBATTERYSENSOR, "TurnOffUpsOnBatterySensor" },
  221. { UTILITYLINECONDITIONSENSOR, "UtilityLineConditionSensor" },
  222. { UNSUPPORTEDSENSOR, "UnsupportedSensor" },
  223. { PUTUPSTOSLEEPSENSOR, "PutUpsToSleepSensor" },
  224. { UPSBATTERYTYPESENSOR, "UpsBatteryTypeSensor" },
  225. { UPSIDSENSOR, "UpsIdSensor" },
  226. { UPSLOADSENSOR, "UpsLoadSensor" },
  227. { UPSSENSITIVITYSENSOR, "UpsSensitivitySensor" },
  228. { UPSSERIALNUMBERSENSOR, "UpsSerialNumberSensor" },
  229. { UPSSIMULATEPOWERFAILSENSOR, "UpsSimulatePowerFailSensor" },
  230. { UPSTEMPERATURESENSOR, "UpsTemperatureSensor" },
  231. { TURNOFFDELAYSENSOR, "TurnOffDelaySensor" },
  232. { SELFTESTSENSOR, "SelfTestSensor" },
  233. { AMBIENTTEMPERATURESENSOR, "AmbientTemperatureSensor" },
  234. { HUMIDITYSENSOR, "HumiditySensor" },
  235. { CONTACTSENSOR, "ContactSensor" },
  236. { NUMBERBATTERYPACKSSENSOR, "NumberBatteryPacksSensor" },
  237. { NUMBERBADBATTERIESSENSOR, "NumberBadBatteriesSensor" },
  238. { STATEREGISTERSENSOR, "StateRegisterSensor" },
  239. { FANFAILURESENSOR, "FanFailureSensor" },
  240. { BATTERYCHARGERSENSOR, "BatteryChargerSensor" },
  241. { OVERTEMPFAULTSENSOR, "OverTempFaultSensor" },
  242. { BYPASSMODESENSOR, "BypassModeSensor" },
  243. { BYPASSRELAYFAILEDSENSOR, "BypassRelayFailedSensor" },
  244. { BYPASSPOWERSUPPLYFAULTSENSOR, "BypassPowerSupplyFaultSensor" },
  245. { MUPSFIRMWAREREVSENSOR, "MupsFirmwareRevSensor" },
  246. { BATTERYRUNTIMESENSOR, "BatteryRunTimeSensor" },
  247. { PANELPASSWORDSENSOR, "FrontPanelPasswordSensor" },
  248. { RUNTIMEAFTERLOWBATTERYSENSOR, "RunTimeAfterLowBatterySensor" },
  249. { 0, "NoMatch" }
  250. };
  251. PCHAR Sensor::lookupSensorName(INT anIsaCode)
  252. {
  253. ISACODE * code = isa_codes;
  254. while(code->isa_code != 0)
  255. {
  256. if(code->isa_code == anIsaCode)
  257. {
  258. break;
  259. }
  260. code++;
  261. }
  262. return code->sensor_name;
  263. }