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.

336 lines
8.8 KiB

  1. /*
  2. *
  3. * NOTES:
  4. *
  5. * REVISIONS:
  6. * pcy24Nov92: Use apc.h. Remove Popups and event log (now in App).
  7. * SjA11Dec92: Registers events with the theUps now. Doesn't update twice now.
  8. * pcy11Dec92: include smartups.h instead of backups.h
  9. * pcy11Dec92: Initialize theCommController in the constructor
  10. * ane16Dec92: Comment out passing of gets/sets to host object, handled in app now
  11. * ane05Jan93: Added code to support slaves
  12. * ane11Jan93: Register for additional events when a slave
  13. * pcy26Jan93: Construct SmartUps/BackUps based on ini file
  14. * pcy26Jan93: Return construction errors in theObjectStatus
  15. * ane03Feb93: Added state and SetInvalid and state checking
  16. * pcy16Feb93: Get rid of SORRY CHARLIE debug msg
  17. * tje24Feb93: Added Windows support
  18. * jod14May93: Added Matrix changes.
  19. * cad10Jun93: Added MeasureUPS support
  20. * cad15Jul93: Moved add-ons to under smart comm
  21. * cad04Aug93: Fixed up admin shutdown handling
  22. * cad27Aug93: Added handler for is measureups attached get
  23. * cad14Sep93: Handling measureups non-null, but not really there
  24. * cad20Oct93: Better MUPS checking
  25. * cad27Oct93: even better than that
  26. * jod02Nov93: Added CIBC conditional statements
  27. * cad11Nov93: Changed handling of Comm Lost
  28. * cad17Nov93: .. more little fixups
  29. * rct21Dec93: fixed bug in Get()
  30. * pcy08Apr94: Trim size, use static iterators, dead code removal
  31. * ajr22Aug94: Lets not auto-detect mups because of ShareUps problems.
  32. * ajr14Feb96: Sinix merge
  33. * cgm29Feb96: Delete the created UPS if lost comm in first 3-10 sec
  34. * djs22Feb96: Changed to new firmware rev interface
  35. * cgm29Feb96: (NetWare) Override switch
  36. * cgm17Apr96: Delete the ups before commcontroller
  37. * cgm17Apr96: Don't create measureups without valid ups object
  38. * djs17May96: Added DarkStar device
  39. * srt02Apr97: Added fix for potential bug
  40. * tjg02Dec97: Changed darkstar to symmetra
  41. * tjg26Jan98: Added Stop method
  42. * clk13May98: When getting value for IS_SYMMETRA, always get value from UPS
  43. * mholly12May1999: special handling of TURN_OFF_SMART_MODE code
  44. *
  45. * v-stebe 29Jul2000 Fixed PREfix error (bug #112614)
  46. */
  47. #include "cdefine.h"
  48. extern "C" {
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. }
  52. #include "_defs.h"
  53. #include "apc.h"
  54. #include "cdevice.h"
  55. #include "devctrl.h"
  56. #include "ups.h"
  57. #include "err.h"
  58. #include "dispatch.h"
  59. #include "smartups.h"
  60. #include "matrix.h"
  61. #include "codes.h"
  62. #include "cfgmgr.h"
  63. #include "dcomctrl.h"
  64. DeviceController::DeviceController(PMainApplication anApp)
  65. : Controller(),
  66. theApplication(anApp),
  67. slaveEnabled(FALSE),
  68. theUps((PUps)NULL)
  69. {
  70. INT err = ErrNO_ERROR;
  71. theCommController = new DevComContrl(this);
  72. theCommController->RegisterEvent(COMMUNICATION_STATE, this);
  73. theCommController->RegisterEvent(SHUTDOWN,this);
  74. theCommController->RegisterEvent(UPS_OFF_PENDING,this);
  75. theObjectStatus = theCommController->GetObjectStatus();
  76. if(theObjectStatus == ErrNO_ERROR) {
  77. if (theUps) {
  78. theObjectStatus = theUps->GetObjectStatus();
  79. }
  80. }
  81. else {
  82. theUps = (PUps)NULL;
  83. }
  84. theApplication->RegisterEvent(EXIT_THREAD_NOW, this);
  85. }
  86. DeviceController::~DeviceController()
  87. {
  88. theApplication->UnregisterEvent(EXIT_THREAD_NOW, this);
  89. // Must delete theUps object before theCommController
  90. delete theUps;
  91. theUps = (PUps)NULL;
  92. delete theCommController;
  93. theCommController = (PCommController)NULL;
  94. }
  95. INT DeviceController::Initialize()
  96. {
  97. INT err = ErrNO_ERROR;
  98. theCommController->Initialize();
  99. return err;
  100. }
  101. INT DeviceController::CreateUps()
  102. {
  103. INT err = ErrNO_ERROR;
  104. CHAR value[32];
  105. if (!theUps) {
  106. _theConfigManager->Get(CFG_UPS_SIGNALLING_TYPE, value);
  107. if( (_strcmpi(value, "SIMPLE") == 0) || (slaveEnabled == TRUE)) {
  108. theUps = new BackUps(this, theCommController);
  109. }
  110. else {
  111. FirmwareRevSensor theFirmwareRevSensor(((PDevice)NULL), theCommController);
  112. CHAR Is_Ups_A_Symmetra[32];
  113. theFirmwareRevSensor.Get(IS_SYMMETRA,Is_Ups_A_Symmetra);
  114. if (_strcmpi(Is_Ups_A_Symmetra,"Yes") == 0) {
  115. theUps = new Matrix(this, theCommController);
  116. }
  117. else {
  118. CHAR Is_Ups_A_Matrix[32];
  119. theFirmwareRevSensor.Get(IS_MATRIX,Is_Ups_A_Matrix);
  120. if (_strcmpi(Is_Ups_A_Matrix,"Yes") == 0) {
  121. theUps = new Matrix(this, theCommController);
  122. }
  123. else {
  124. theUps = new SmartUps(this, theCommController);
  125. if ((theUps->GetObjectStatus()) == ErrSMART_MODE_FAILED) {
  126. delete theUps;
  127. theUps = (PUps) NULL;
  128. }
  129. }
  130. }
  131. }
  132. if (theUps) {
  133. theUps->Initialize();
  134. theDispatcher->RefreshEventRegistration(theUps, this);
  135. theCommController->GetDevice()->OkToPoll();
  136. }
  137. }
  138. return err;
  139. }
  140. INT DeviceController::Get(INT code, PCHAR aValue)
  141. {
  142. INT err = ErrNO_ERROR;
  143. INT comm = FALSE;
  144. switch(code/1000)
  145. {
  146. case UPS/1000:
  147. if (code == COMMUNICATION_STATE) {
  148. if (theCommController) {
  149. err = theCommController->Get(COMMUNICATION_STATE, aValue);
  150. }
  151. else {
  152. sprintf(aValue, "%d", COMMUNICATION_LOST);
  153. }
  154. }
  155. else if (theUps && theCommController &&
  156. !(theCommController->GetDevice()->HasLostComm())) {
  157. err = theUps->Get(code, aValue);
  158. }
  159. else if (theUps && (code == IS_SYMMETRA)) {
  160. err = theUps->Get(code, aValue);
  161. }
  162. else {
  163. err = ErrINVALID_VALUE;
  164. }
  165. break;
  166. case MEASURE_UPS/1000:
  167. if (code == IS_MEASURE_UPS_ATTACHED) {
  168. strcpy(aValue, "No");
  169. }
  170. else {
  171. err = ErrNO_MEASURE_UPS;
  172. }
  173. break;
  174. case INTERNAL/1000:
  175. if ((code == RETRY_CONSTRUCT) && theCommController) {
  176. err = theCommController->Get(code, aValue);
  177. }
  178. break;
  179. case IS_LINE_FAIL_RUN_TIME_ENABLED/1000:
  180. theApplication->Get(code, aValue);
  181. break;
  182. }
  183. return err;
  184. }
  185. INT DeviceController::Set(INT code, const PCHAR aValue)
  186. {
  187. INT err = ErrNO_ERROR;
  188. switch(code/1000) {
  189. case UPS/1000:
  190. if (theUps && theCommController &&
  191. !(theCommController->GetDevice()->HasLostComm())) {
  192. if (TURN_OFF_SMART_MODE == code) {
  193. err = theCommController->Set(code, aValue);
  194. }
  195. else {
  196. err = theUps->Set(code, aValue);
  197. }
  198. }
  199. break;
  200. case MEASURE_UPS/1000:
  201. err = ErrNO_MEASURE_UPS;
  202. break;
  203. case INTERNAL/1000:
  204. if ((code == RETRY_CONSTRUCT) && theCommController) {
  205. err = theCommController->Set(code, aValue);
  206. }
  207. break;
  208. }
  209. return err;
  210. }
  211. INT DeviceController::Update(PEvent anEvent)
  212. {
  213. switch (anEvent->GetCode()) {
  214. case COMMUNICATION_STATE:
  215. if (atoi(anEvent->GetValue()) == COMMUNICATION_ESTABLISHED) {
  216. CreateUps();
  217. }
  218. else {
  219. if(theUps) {
  220. CHAR val[32];
  221. theUps->Get(UPS_STATE, val);
  222. if(atoi(val) & UPS_STATE_ON_BATTERY) {
  223. anEvent->SetValue(COMMUNICATION_LOST_ON_BATTERY);
  224. }
  225. }
  226. }
  227. break;
  228. }
  229. INT err;
  230. if ( (theCommController->GetDevice()->HasLostComm() == FALSE) ||
  231. (anEvent!=NULL && anEvent->GetCode()==COMMUNICATION_STATE &&
  232. atoi(anEvent->GetValue()) != COMMUNICATION_ESTABLISHED))
  233. {
  234. err = UpdateObj::Update(anEvent);
  235. }
  236. return err;
  237. }
  238. INT DeviceController::RegisterEvent(INT id, UpdateObj* object)
  239. {
  240. INT err = UpdateObj::RegisterEvent(id,object);
  241. if (err == ErrNO_ERROR) {
  242. if (id == SHUTDOWN_STATUS ||
  243. id == ADMIN_SHUTDOWN ||
  244. id == CANCEL_SHUTDOWN) {
  245. theApplication->RegisterEvent(id, this);
  246. }
  247. else {
  248. switch(id/1000) {
  249. case UPS/1000:
  250. if (theUps) {
  251. err = theUps->RegisterEvent(id, object);
  252. }
  253. break;
  254. case MEASURE_UPS/1000:
  255. break;
  256. }
  257. }
  258. }
  259. return err;
  260. }
  261. VOID DeviceController::SetInvalid()
  262. {
  263. }
  264. VOID DeviceController::Stop()
  265. {
  266. if (theCommController) {
  267. theCommController->Stop();
  268. }
  269. }