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.

402 lines
12 KiB

  1. /*****************************************************************************
  2. *
  3. * $Workfile: Status.cpp $
  4. *
  5. * Copyright (C) 1997 Hewlett-Packard Company.
  6. * Copyright (C) 1997 Microsoft Corporation.
  7. * All rights reserved.
  8. *
  9. * 11311 Chinden Blvd.
  10. * Boise, Idaho 83714
  11. *
  12. *****************************************************************************/
  13. #include "precomp.h"
  14. #include "snmpmgr.h"
  15. #include "stdoids.h"
  16. #include "status.h"
  17. /********************************************************
  18. status notes:
  19. 1. ASYNCH_NETWORK_ERROR handled by calling function,
  20. GetObjectSNMP() located in snmp.c
  21. *********************************************************/
  22. //constants==============================================
  23. #define NA 0
  24. #define OTHER_ALERTS MAX_ASYNCH_STATUS+1
  25. #define WARNING_ALERTS MAX_ASYNCH_STATUS+2
  26. #define CRITICAL_ALERTS MAX_ASYNCH_STATUS+3
  27. //hrPrinterDetectedErrorState Masks
  28. #define LOW_PAPER 0x00000080
  29. #define NO_PAPER 0x00000040
  30. #define LOW_TONER 0x00000020
  31. #define NO_TONER 0x00000010
  32. #define DOOR_OPEN 0x00000008
  33. #define PAPER_JAM 0x00000004
  34. #define OFF_LINE 0x00000002
  35. #define SERVICE_REQUESTED 0x00000001
  36. //subunit status
  37. #define AVAIL_IDLE 0L //available and idle
  38. #define AVAIL_STDBY 2L //available and in standby
  39. #define AVAIL_ACTIVE 4L //available and active
  40. #define AVAIL_BUSY 6L
  41. #define UNAVAIL_ONREQ 1L //unavailable and on-request
  42. #define UNAVAIL_BROKEN 3L //unavailable because broken
  43. #define AVAIL_UNKNOWN 5L
  44. #define NON_CRITICAL_ALERT 8L
  45. #define CRITICAL_ALERT 16L
  46. #define OFF_LINEx 32L
  47. #define TRANS 64L //transitioning to intended state
  48. #define NUM_TRAYS 2
  49. /*************
  50. Printer hrDeviceStatus hrPrinterStatus hrPrinterDetectedErrorState
  51. Status
  52. Normal running(2) idle(3) none set
  53. Busy/ running(2) printing(4)
  54. Temporarily
  55. Unavailable
  56. Non Critical warning(3) idle(3) or could be: lowPaper,
  57. Alert Active printing(4) lowToner, or
  58. serviceRequested
  59. Critical down(5) other(1) could be: jammed,
  60. Alert Active noPaper, noToner,
  61. coverOpen, or
  62. serviceRequested
  63. Unavailable down(5) other(1)
  64. Moving off- warning(3) idle(3) or offline
  65. line printing(4)
  66. Smith, Wright, Hastings, Zilles & Gyllenskog [Page 14]
  67. RFC 1759 Printer MIB March 1995
  68. Off-line down(5) other(1) offline
  69. Moving down(5) warmup(5)
  70. on-line
  71. Standby running(2) other(1)
  72. *************/
  73. //lookup table for basic status
  74. // [device status][printer status]
  75. #define LOOKUP_TABLE_ROWS 5
  76. #define LOOKUP_TABLE_COLS 5
  77. BYTE basicStatusTable[LOOKUP_TABLE_COLS][LOOKUP_TABLE_ROWS] =
  78. {
  79. /*other unknown idle printing warmup*/
  80. /*unknown*/ { NA, NA, NA, NA, NA },
  81. /*running*/ { ASYNCH_POWERSAVE_MODE, NA, ASYNCH_ONLINE, ASYNCH_PRINTING, ASYNCH_WARMUP },
  82. /*warning*/ { NA, NA, WARNING_ALERTS, WARNING_ALERTS, WARNING_ALERTS },
  83. /*testing*/ { OTHER_ALERTS, NA, NA, ASYNCH_PRINTING_TEST_PAGE, NA },
  84. /*down*/ { CRITICAL_ALERTS, NA, NA, NA, ASYNCH_WARMUP }
  85. };
  86. ///////////////////////////////////////////////////////////////////////////////
  87. // StdMibGetPeripheralStatus
  88. // Returns Printer status ( Async Code )
  89. // or ASYNCH_STATUS_UNKNOWN if Printer MIB is not supported on the device
  90. DWORD
  91. StdMibGetPeripheralStatus( const char in *pHost,
  92. const char in *pCommunity,
  93. DWORD in dwDevIndex)
  94. {
  95. DWORD dwRetCode = NO_ERROR;
  96. DWORD errorState;
  97. WORD wLookup = NA;
  98. RFC1157VarBindList variableBindings;
  99. UINT OID_HRMIB_hrDeviceStatus[] = { 1, 3, 6, 1, 2, 1, 25, 3, 2, 1, 5, dwDevIndex};
  100. UINT OID_HRMIB_hrPrinterStatus[] = { 1, 3, 6, 1, 2, 1, 25, 3, 5, 1, 1, dwDevIndex};
  101. UINT OID_HRMIB_hrPrinterDetectedErrorState[] = { 1, 3, 6, 1, 2, 1, 25, 3, 5, 1, 2, dwDevIndex};
  102. AsnObjectIdentifier OT_DEVICE_STATUS[] = { { OID_SIZEOF(OID_HRMIB_hrDeviceStatus), OID_HRMIB_hrDeviceStatus },
  103. { OID_SIZEOF(OID_HRMIB_hrPrinterStatus), OID_HRMIB_hrPrinterStatus },
  104. { OID_SIZEOF(OID_HRMIB_hrPrinterDetectedErrorState), OID_HRMIB_hrPrinterDetectedErrorState },
  105. { 0, 0 } };
  106. // build the variable bindings list
  107. variableBindings.list = NULL;
  108. variableBindings.len = 0;
  109. CSnmpMgr *pSnmpMgr = new CSnmpMgr(pHost, pCommunity, dwDevIndex);
  110. if ( !pSnmpMgr )
  111. {
  112. return ERROR_OUTOFMEMORY;
  113. }
  114. if (pSnmpMgr->GetLastError() != SNMPAPI_NOERROR )
  115. {
  116. delete pSnmpMgr;
  117. return ASYNCH_STATUS_UNKNOWN;
  118. }
  119. dwRetCode = pSnmpMgr->BldVarBindList(OT_DEVICE_STATUS, &variableBindings);
  120. if (dwRetCode != SNMPAPI_NOERROR)
  121. {
  122. SnmpUtilVarBindListFree(&variableBindings);
  123. delete pSnmpMgr;
  124. return ASYNCH_STATUS_UNKNOWN;
  125. }
  126. // get the status objects
  127. dwRetCode = pSnmpMgr->Get(&variableBindings);
  128. if (dwRetCode != NO_ERROR)
  129. {
  130. SnmpUtilVarBindListFree(&variableBindings);
  131. delete pSnmpMgr;
  132. if (dwRetCode == SNMP_ERRORSTATUS_NOSUCHNAME)
  133. dwRetCode = ASYNCH_ONLINE;
  134. else
  135. dwRetCode = ASYNCH_STATUS_UNKNOWN;
  136. return dwRetCode;
  137. }
  138. if(dwRetCode == NO_ERROR)
  139. {
  140. if( (variableBindings.list[0].value.asnValue.number-1 < 0) ||
  141. (variableBindings.list[0].value.asnValue.number-1>=LOOKUP_TABLE_COLS) )
  142. {
  143. wLookup = OTHER_ALERTS;
  144. }
  145. else if( (variableBindings.list[1].value.asnValue.number-1 < 0) ||
  146. (variableBindings.list[1].value.asnValue.number-1 >=LOOKUP_TABLE_ROWS) )
  147. {
  148. wLookup = OTHER_ALERTS;
  149. }
  150. else
  151. {
  152. wLookup = basicStatusTable[variableBindings.list[0].value.asnValue.number-1]
  153. [variableBindings.list[1].value.asnValue.number-1];
  154. }
  155. switch(wLookup)
  156. {
  157. case NA:
  158. dwRetCode = ASYNCH_STATUS_UNKNOWN;
  159. break;
  160. case CRITICAL_ALERTS:
  161. GetBitsFromString((LPSTR)(variableBindings.list[2].value.asnValue.string.stream),
  162. variableBindings.list[2].value.asnValue.string.length, &errorState );
  163. dwRetCode = ProcessCriticalAlerts(errorState);
  164. break;
  165. case WARNING_ALERTS:
  166. GetBitsFromString((LPSTR)(variableBindings.list[2].value.asnValue.string.stream),
  167. variableBindings.list[2].value.asnValue.string.length, &errorState );
  168. dwRetCode = ProcessWarningAlerts(errorState);
  169. break;
  170. case OTHER_ALERTS:
  171. GetBitsFromString((LPSTR)(variableBindings.list[2].value.asnValue.string.stream),
  172. variableBindings.list[2].value.asnValue.string.length, &errorState );
  173. dwRetCode = ProcessOtherAlerts( errorState);
  174. break;
  175. default:
  176. dwRetCode = wLookup;
  177. break;
  178. }
  179. }
  180. else
  181. {
  182. dwRetCode = ASYNCH_STATUS_UNKNOWN;
  183. }
  184. SnmpUtilVarBindListFree(&variableBindings);
  185. delete pSnmpMgr;
  186. return dwRetCode;
  187. } // StdMibGetPeripheralStatus()
  188. ///////////////////////////////////////////////////////////////////////////////
  189. // ProcessCriticalAlerts - determine active critical error
  190. //
  191. // returns the device status for Critical Alerts ( ASYNC_XXXXX )
  192. DWORD
  193. ProcessCriticalAlerts( DWORD errorState )
  194. {
  195. DWORD status = ASYNCH_ONLINE;
  196. if ( errorState & DOOR_OPEN) {
  197. status = ASYNCH_DOOR_OPEN;
  198. }
  199. else if( errorState & NO_TONER) {
  200. status = ASYNCH_TONER_GONE;
  201. }
  202. else if( errorState & NO_PAPER) {
  203. status = ASYNCH_PAPER_OUT;
  204. }
  205. else if( errorState & PAPER_JAM ) {
  206. status = ASYNCH_PAPER_JAM;
  207. }
  208. else if(errorState & SERVICE_REQUESTED) {
  209. status = ASYNCH_PRINTER_ERROR;
  210. }
  211. else if( errorState & OFF_LINE) {
  212. status = ASYNCH_OFFLINE;
  213. }
  214. else
  215. status = ASYNCH_PRINTER_ERROR;
  216. return status;
  217. } // ProcessCriticalAlerts()
  218. ///////////////////////////////////////////////////////////////////////////////
  219. // ProcessWarningAlerts - determine active warning
  220. //
  221. // returns the device status for Critical Alerts ( ASYNC_XXXXX )
  222. DWORD
  223. ProcessWarningAlerts( DWORD errorState )
  224. {
  225. DWORD status = ASYNCH_ONLINE;
  226. if( errorState & LOW_PAPER) {
  227. status = ASYNCH_ONLINE;
  228. }
  229. else if(errorState & LOW_TONER) {
  230. status = ASYNCH_TONER_LOW;
  231. }
  232. else if( errorState & SERVICE_REQUESTED) {
  233. // Changed it from ASYNCH_INTERVENTION; since if hrDeviceStatus = warning,
  234. // the printer can still print even though hrPrinterDetectedErrorState = serviceRequested
  235. //
  236. status = ASYNCH_ONLINE;
  237. }
  238. else if( errorState == 0) {
  239. status = ASYNCH_ONLINE;
  240. }
  241. else {
  242. status = ASYNCH_STATUS_UNKNOWN;
  243. }
  244. return status;
  245. } // ProcessWarningAlerts()
  246. ///////////////////////////////////////////////////////////////////////////////
  247. // ProcessWarningAlerts - determine status for other Alerts
  248. // returns the device status for Critical Alerts ( ASYNC_XXXXX )
  249. DWORD ProcessOtherAlerts( DWORD errorState )
  250. {
  251. DWORD status = ASYNCH_ONLINE;
  252. //
  253. // This is a place holder for future functionality
  254. //
  255. status = ASYNCH_STATUS_UNKNOWN;
  256. return status;
  257. } // ProcessOtherAlerts
  258. ///////////////////////////////////////////////////////////////////////////////
  259. // GetBitsFromString -
  260. // extracts the bin numbers from collection string returned by the get
  261. //
  262. void GetBitsFromString( LPSTR getVal,
  263. DWORD getSiz,
  264. LPDWORD bits)
  265. {
  266. char* ptr = (char*)bits;
  267. *bits = 0;
  268. #if defined(_INTEL) || defined(WINNT)
  269. switch(getSiz)
  270. {
  271. case 1:
  272. ptr[0] = getVal[0];
  273. break;
  274. case 2:
  275. ptr[1] = getVal[0];
  276. ptr[0] = getVal[1];
  277. break;
  278. case 3:
  279. ptr[2] = getVal[0];
  280. ptr[1] = getVal[1];
  281. ptr[0] = getVal[2];
  282. break;
  283. case 4:
  284. ptr[3] = getVal[0];
  285. ptr[2] = getVal[1];
  286. ptr[1] = getVal[2];
  287. ptr[0] = getVal[3];
  288. break;
  289. }
  290. #elif defined(_MOTOROLLA)
  291. switch(getSiz)
  292. {
  293. case 1:
  294. ptr[3] = getVal[0];
  295. break;
  296. case 2:
  297. ptr[2] = getVal[0];
  298. ptr[3] = getVal[1];
  299. break;
  300. case 3:
  301. ptr[1] = getVal[0];
  302. ptr[2] = getVal[1];
  303. ptr[3] = getVal[2];
  304. break;
  305. case 4:
  306. ptr[0] = getVal[0];
  307. ptr[1] = getVal[1];
  308. ptr[2] = getVal[2];
  309. ptr[3] = getVal[3];
  310. break;
  311. }
  312. #else
  313. #error #define a swap method ( _INTEL, _MOTOROLLA )
  314. #endif /* _INTEL, _MOTOROLLA */
  315. } // GetBitsFromString()