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.

265 lines
6.9 KiB

  1. /*
  2. *
  3. * NOTES:
  4. *
  5. * REVISIONS:
  6. * pcy29Nov92: New defines from codes.h
  7. * sja08Dec92: Changed #define BATTERY_TYPE to RATED_BATTERY_VOLTAGE
  8. * jod13Jan93: Added eventList to InterpretMessage
  9. * jod28Jan93: Added fixes for support of Data and Decrement Sets
  10. * pcy02Feb93: InterpretSetMessage needs to return a value
  11. * ane03Feb93: Changed BuildPollTransactionGroupMessages to check IsPollSet
  12. * differently
  13. * jod14Feb93: Handle mulit char sets (@ddd, KK, etc)
  14. * pcy16Feb92: Move UPS_STATE_SET define to err.h to avoid conflicts
  15. * pcy16Feb92: Allow gets of UpsState params
  16. * pcy16Feb93: Made battery test results pollable
  17. * pcy21Apr93: OS/2 2.0 FE Merge
  18. * jod05Apr93: Added changes for Deep Discharge
  19. * jod14May93: Added Matrix changes.
  20. * cad10Jun93: Added Mups parms
  21. * cad22Jul93: Fixed up destructor conflicts and omissions
  22. * cad27Aug93: Added MeasureUPS firmware poll param
  23. * cad07Oct93: Plugging Memory Leaks
  24. * pcy08Apr94: Trim size, use static iterators, dead code removal
  25. * mwh01Jun94: port for INTERACTIVE
  26. * jps14Jul94: commented out INCL_NOPMAPI
  27. * ajr15Feb96: Sinix Merge
  28. * cgm23Jul97: BuildStandardSetMessage creates Message(aCode,SET)
  29. *
  30. * v-stebe 29Jul2000 Fixed PREfix errors (bugs #46353-#46355)
  31. */
  32. #define INCL_BASE
  33. #define INCL_NOPM
  34. /* #define INCL_NOPMAPI jwa */
  35. #include "cdefine.h"
  36. extern "C" {
  37. #if (C_OS & C_OS2)
  38. #include <process.h>
  39. #include <os2.h>
  40. #endif
  41. #include <stdlib.h>
  42. #if (!(C_OS & C_INTERACTIVE))
  43. #include <malloc.h>
  44. #endif
  45. #include <string.h>
  46. #include <time.h>
  47. }
  48. #include "_defs.h"
  49. #include "event.h"
  50. #include "codes.h"
  51. #include "message.h"
  52. #include "pollparm.h"
  53. #include "trans.h"
  54. #include "protsimp.h"
  55. #include "err.h"
  56. #if (C_OS & C_UNIX)
  57. #include "utils.h"
  58. #endif
  59. SimpleUpsProtocol:: SimpleUpsProtocol() : Protocol()
  60. {
  61. InitProtocol();
  62. }
  63. SimpleUpsProtocol:: ~SimpleUpsProtocol()
  64. {
  65. }
  66. VOID SimpleUpsProtocol::InitProtocol()
  67. {
  68. char buffer[5];
  69. for(int i=0; i<PROTOSIZE; i++) {
  70. ProtoList[i]=(PollParam*)NULL;
  71. }
  72. ProtoList[UTILITY_LINE_CONDITION] = new SimplePollParam(UTILITY_LINE_CONDITION, _itoa(UTILITY_LINE_CONDITION,buffer,10), 0, POLL);
  73. ProtoList[BATTERY_CONDITION] = new SimplePollParam(BATTERY_CONDITION, _itoa(BATTERY_CONDITION,buffer,10), 0, POLL);
  74. ProtoList[TURN_OFF_UPS] = new SimplePollParam(TURN_OFF_UPS, _itoa(TURN_OFF_UPS,buffer,10), 0, NO_POLL);
  75. ProtoList[TURN_ON_SMART_MODE] = new SimplePollParam(TURN_ON_SMART_MODE, _itoa(TURN_ON_SMART_MODE,buffer,10), 0, NO_POLL);
  76. ProtoList[TURN_OFF_UPS_ON_BATTERY] = new SimplePollParam(TURN_OFF_UPS_ON_BATTERY, _itoa(TURN_OFF_UPS_ON_BATTERY,buffer,10), 0, NO_POLL);
  77. return;
  78. }
  79. INT SimpleUpsProtocol :: BuildMessage(Message*, List* )
  80. {
  81. return 1;
  82. }
  83. INT SimpleUpsProtocol :: BuildPollTransactionGroupMessages(PTransactionGroup aTransactionGroup)
  84. {
  85. int err = ErrNO_ERROR;
  86. PTransactionItem theItem = aTransactionGroup->GetFirstTransactionItem();
  87. while ((theItem != NULL) && (err == ErrNO_ERROR))
  88. {
  89. if (ProtoList[theItem->GetCode()] != (PollParam*)NULL)
  90. {
  91. PollParam* poll_param = ProtoList[theItem->GetCode()];
  92. /********************************************************
  93. * *
  94. * There is no test for POLLABLE in the Simple Protocol *
  95. * *
  96. *********************************************************/
  97. }
  98. else
  99. err = ErrNOT_POLLABLE;
  100. theItem = aTransactionGroup->GetNextTransactionItem();
  101. }
  102. if (err == ErrNO_ERROR)
  103. {
  104. err = BuildTransactionGroupMessages(aTransactionGroup);
  105. }
  106. return err;
  107. }
  108. INT SimpleUpsProtocol :: BuildTransactionGroupMessages(PTransactionGroup aTransactionGroup)
  109. {
  110. int err = ErrNO_ERROR;
  111. PTransactionItem theItem = aTransactionGroup->GetFirstTransactionItem();
  112. while ((theItem != NULL) && (err == ErrNO_ERROR))
  113. {
  114. List* msglist = BuildTransactionMessageList(theItem->GetType(), theItem->GetCode(), theItem->GetValue());
  115. if (msglist)
  116. {
  117. ListIterator iterator(*msglist);
  118. PMessage msg = (PMessage)&(iterator.Current());
  119. while (msg) {
  120. theItem->AddMessage(msg);
  121. PMessage copy_msg = new Message(msg);
  122. aTransactionGroup->AddMessage(copy_msg);
  123. msg = (PMessage)iterator.Next();
  124. }
  125. // don't flush list, we just gave away all the elements
  126. delete msglist;
  127. msglist = NULL;
  128. }
  129. else
  130. {
  131. theItem->SetErrorCode(ErrBUILD_FAILED);
  132. err = ErrBUILD_FAILED;
  133. }
  134. theItem = aTransactionGroup->GetNextTransactionItem();
  135. }
  136. return err;
  137. }
  138. List* SimpleUpsProtocol :: BuildTransactionMessageList(Type aType, INT aCode, CHAR* aValue)
  139. {
  140. List* msglist;
  141. PollParam* poll_param = ProtoList[aCode];
  142. if(poll_param == NULL) {
  143. return (List*)NULL;
  144. }
  145. switch(aType) {
  146. case GET:
  147. msglist = BuildGetMessage(aCode);
  148. break;
  149. case SET:
  150. msglist = BuildStandardSetMessage(aCode, aValue);
  151. break;
  152. }
  153. return msglist;
  154. }
  155. List* SimpleUpsProtocol :: BuildGetMessage(INT aCode)
  156. {
  157. List* msglist = NULL;
  158. PMessage msg = new Message(aCode);
  159. if (msg)
  160. {
  161. PollParam* poll_param = ProtoList[aCode];
  162. PCHAR cmd = poll_param->Query();
  163. msg->setSubmit(cmd);
  164. msg->setTimeout(poll_param->GetTime());
  165. msglist = new List();
  166. msglist->Append(msg);
  167. free(cmd);
  168. }
  169. else {
  170. ;// setError(ErrMESSAGE_CREATE_FAILED);
  171. }
  172. return msglist;
  173. }
  174. List* SimpleUpsProtocol :: BuildStandardSetMessage(INT aCode, CHAR* aValue)
  175. {
  176. List* msglist = new List();
  177. // Really only action type stuff. How will we deal with commands that
  178. // require two letters (S, wait 1 seconds, S). Possibly use
  179. // TURN_OFF_UPS as the code, and set an inter character delay as part of
  180. // protocol message. For now this only handles the simple stuff.
  181. PollParam* poll_param = ProtoList[aCode];
  182. PMessage msg = new Message(aCode,SET);
  183. CHAR submit_string[50];
  184. *submit_string = '\0';
  185. PCHAR cmd = poll_param->Query();
  186. if (cmd != NULL) {
  187. strcpy(submit_string,cmd);
  188. free(cmd);
  189. }
  190. if(poll_param->GetSetType() != SIMPLE_SET) {
  191. if (aValue) {
  192. strcat(submit_string,aValue);
  193. }
  194. }
  195. if (msg != NULL) {
  196. msg->setSubmit(submit_string);
  197. msg->setTimeout(poll_param->GetTime());
  198. msglist->Append(msg);
  199. }
  200. return msglist;
  201. }
  202. INT SimpleUpsProtocol :: InterpretMessage(Message* msg, List* eventList, List* newmsglist)
  203. {
  204. PList tiList = theCurrentTransactionGroup->GetTransactionItemList();
  205. TransactionItem tmpti(GET,msg->getId());
  206. PTransactionItem trans_item = (PTransactionItem)tiList->Find(&tmpti);
  207. if (trans_item) {
  208. trans_item->SetErrorCode(msg->getErrcode());
  209. trans_item->SetValue(msg->getResponse());
  210. }
  211. #if 0
  212. PEvent event = new Event(msg->getId(), msg->getResponse());
  213. // event->setResponse(msg->getResponse());
  214. eventList->Append(event);
  215. #endif
  216. return ErrNO_ERROR;
  217. }