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.

219 lines
4.9 KiB

  1. /* DEMOUPS - UPS Minidriver Sample
  2. * Copyright (C) Microsoft Corporation, 2001, All rights reserved.
  3. * Copyright (C) American Power Conversion, 2001, All rights reserved.
  4. *
  5. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  6. * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  7. * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  8. * PURPOSE.
  9. *
  10. * File: demoups.cpp
  11. *
  12. * Author: Stephen Berard
  13. *
  14. * Description:
  15. * Demo UPS Minidriver implementation. This minidriver provides
  16. * a basic framework for a UPS Minidriver.
  17. *
  18. * Revision History:
  19. * 26Jun2001 Created
  20. */
  21. #include <windows.h>
  22. #include "demoups.h"
  23. // Global value used to indicate that the UPS state has changed
  24. HANDLE theStateChangedEvent;
  25. // Global handle to DLL module
  26. HINSTANCE theDLLModuleHandle;
  27. /**
  28. * DllMain
  29. *
  30. * Description:
  31. * This method is called when the DLL is loaded or unloaded.
  32. *
  33. * Parameters:
  34. * aHandle: the DLL module handle
  35. * aReason: flag indicating the reason why the entry point was called
  36. * aReserved: reserved
  37. *
  38. * Returns:
  39. * TRUE
  40. *
  41. */
  42. BOOL APIENTRY DllMain(HINSTANCE aHandle, DWORD aReason, LPVOID aReserved) {
  43. switch (aReason) {
  44. // DLL initialization code goes here
  45. case DLL_PROCESS_ATTACH:
  46. theDLLModuleHandle = aHandle;
  47. DisableThreadLibraryCalls(theDLLModuleHandle);
  48. break;
  49. case DLL_THREAD_ATTACH:
  50. case DLL_THREAD_DETACH:
  51. case DLL_PROCESS_DETACH:
  52. break;
  53. }
  54. return TRUE;
  55. }
  56. /**
  57. * UPSInit
  58. *
  59. * Description:
  60. * Must be the first method called in the interface. This method should do
  61. * any initialization required and obtain the initial UPS state.
  62. *
  63. * Parameters:
  64. * None
  65. *
  66. * Returns:
  67. * UPS_INITOK: successful initialization
  68. * UPS_INITUNKNOWNERROR: failed initialization
  69. *
  70. */
  71. UPSMINIDRIVER_API DWORD UPSInit() {
  72. DWORD init_err = UPS_INITOK;
  73. theStateChangedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  74. if (!theStateChangedEvent) {
  75. init_err = UPS_INITUNKNOWNERROR;
  76. }
  77. return init_err;
  78. }
  79. /**
  80. * UPSStop
  81. *
  82. * Description:
  83. * Stops monitoring of the UPS. This method should perform any necessary
  84. * cleanup.
  85. *
  86. * Parameters:
  87. * None
  88. *
  89. * Returns:
  90. * None
  91. *
  92. */
  93. UPSMINIDRIVER_API void UPSStop(void) {
  94. UPSCancelWait();
  95. if (theStateChangedEvent) {
  96. CloseHandle(theStateChangedEvent);
  97. theStateChangedEvent = NULL;
  98. }
  99. }
  100. /**
  101. * UPSWaitForStateChange
  102. *
  103. * Description:
  104. * Blocks until the state of the UPS differs from the value passed in
  105. * via aState or anInterval milliseconds has expired. If anInterval has
  106. * a value of INFINITE this function will never timeout
  107. *
  108. * Parameters:
  109. * aState: defines the state to wait for a change from,
  110. * possible values:
  111. * UPS_ONLINE
  112. * UPS_ONBATTERY
  113. * UPS_LOWBATTERY
  114. * UPS_NOCOMM
  115. *
  116. * anInterval: timeout in milliseconds, or INFINITE for
  117. * no timeout interval
  118. *
  119. * Returns:
  120. * None
  121. *
  122. */
  123. UPSMINIDRIVER_API void UPSWaitForStateChange(DWORD aState, DWORD anInterval) {
  124. // Wait for the UPS state to change. Typically a separate thread would be
  125. // used to monitor the UPS and then set theStateChangedEvent to indicate
  126. // that the state has changed. In this demo we only report UPS_ONLINE so
  127. // we don't have a monitoring thread.
  128. if (theStateChangedEvent) {
  129. WaitForSingleObject(theStateChangedEvent, anInterval);
  130. }
  131. }
  132. /**
  133. * UPSGetState
  134. *
  135. * Description:
  136. * Returns the current state of the UPS. This demo minidriver always returns
  137. * UPS_ONLINE.
  138. *
  139. * Parameters:
  140. * None
  141. *
  142. * Returns:
  143. * possible values:
  144. * UPS_ONLINE
  145. * UPS_ONBATTERY
  146. * UPS_LOWBATTERY
  147. * UPS_NOCOMM
  148. *
  149. */
  150. UPSMINIDRIVER_API DWORD UPSGetState(void) {
  151. // Determine the UPS state and return it.
  152. // Demo UPS minidriver always returns Online
  153. return UPS_ONLINE;
  154. }
  155. /**
  156. * UPSCancelWait
  157. *
  158. * Description:
  159. * Interrupts a pending calls to UPSWaitForStateChange without regard to
  160. * timout or state change
  161. *
  162. * Parameters:
  163. * None
  164. *
  165. * Returns:
  166. * None
  167. *
  168. */
  169. UPSMINIDRIVER_API void UPSCancelWait(void) {
  170. // Send a signal to interupt anything waiting.
  171. if (theStateChangedEvent) {
  172. SetEvent(theStateChangedEvent);
  173. }
  174. }
  175. /**
  176. * UPSTurnOff
  177. *
  178. * Description:
  179. * Attempts to turn off the outlets on the UPS after the specified delay.
  180. * This demo minidriver ignores this call and simply returns.
  181. *
  182. * Parameters:
  183. * aTurnOffDelay: the minimum amount of time to wait before
  184. * turning off the outlets on the UPS
  185. *
  186. * Returns:
  187. * None
  188. *
  189. */
  190. UPSMINIDRIVER_API void UPSTurnOff(DWORD aTurnOffDelay) {
  191. // Code to power off the UPS goes here
  192. }