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.

320 lines
8.4 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. Author:
  6. Ken Reneris
  7. Environment:
  8. console
  9. --*/
  10. //
  11. // set variable to define global variables
  12. //
  13. #include <nt.h>
  14. #include <ntrtl.h>
  15. #include <nturtl.h>
  16. #include <windows.h>
  17. #include <errno.h>
  18. #include <malloc.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <poclass.h>
  22. //
  23. // global handles
  24. //
  25. UCHAR Usage[] = "batt: \n";
  26. HANDLE DriverHandle;
  27. ULONG BatteryTag;
  28. #define RANGE 1
  29. //
  30. // Prototypes
  31. //
  32. BOOLEAN
  33. InitDriver ( CHAR *NamePtr );
  34. int
  35. Nib (
  36. UCHAR c
  37. )
  38. {
  39. if (c >= '0' && c <= '9') {
  40. return c - '0';
  41. }
  42. if (c >= 'A' && c <= 'F') {
  43. return c - 'A' + 10;
  44. }
  45. if (c >= 'a' && c <= 'f') {
  46. return c - 'a' + 10;
  47. }
  48. printf ("Invalid hex value\n");
  49. return 0;
  50. }
  51. int
  52. htoi (
  53. PUCHAR s
  54. )
  55. {
  56. ULONG i;
  57. UCHAR c;
  58. i = Nib(s[0]);
  59. if (s[1]) {
  60. i = i << 4 | Nib(s[1]);
  61. }
  62. return i;
  63. }
  64. VOID
  65. GetBatteryTag (
  66. VOID
  67. )
  68. {
  69. NTSTATUS Status;
  70. IO_STATUS_BLOCK IOSB;
  71. Status = NtDeviceIoControlFile(
  72. DriverHandle,
  73. (HANDLE) NULL, // event
  74. (PIO_APC_ROUTINE) NULL,
  75. (PVOID) NULL,
  76. &IOSB,
  77. IOCTL_BATTERY_QUERY_TAG,
  78. NULL, // input buffer
  79. 0,
  80. &BatteryTag, // output buffer
  81. sizeof (BatteryTag)
  82. );
  83. if (!NT_SUCCESS(Status)) {
  84. printf ("Battery tag not available. Status = %x\n", Status);
  85. BatteryTag = 0xffffffff;
  86. }
  87. }
  88. VOID
  89. GetBatteryInfo (
  90. IN BATTERY_QUERY_INFORMATION_LEVEL Level,
  91. IN PVOID Buffer,
  92. IN ULONG BufferLength
  93. )
  94. {
  95. NTSTATUS Status;
  96. IO_STATUS_BLOCK IOSB;
  97. BATTERY_QUERY_INFORMATION BInfo;
  98. memset (Buffer, 0, BufferLength);
  99. BInfo.BatteryTag = BatteryTag;
  100. BInfo.InformationLevel = Level;
  101. Status = NtDeviceIoControlFile(
  102. DriverHandle,
  103. (HANDLE) NULL, // event
  104. (PIO_APC_ROUTINE) NULL,
  105. (PVOID) NULL,
  106. &IOSB,
  107. IOCTL_BATTERY_QUERY_INFORMATION,
  108. &BInfo, // input buffer
  109. sizeof (BInfo),
  110. Buffer, // output buffer
  111. BufferLength
  112. );
  113. if (!NT_SUCCESS(Status)) {
  114. printf ("Query battery information failed. Level %x. Status = %x\n", Level, Status);
  115. }
  116. }
  117. VOID
  118. GetBatteryStatus (
  119. IN PBATTERY_WAIT_STATUS WaitStatus,
  120. OUT PBATTERY_STATUS BatteryStatus
  121. )
  122. {
  123. NTSTATUS Status;
  124. IO_STATUS_BLOCK IOSB;
  125. memset (BatteryStatus, 0xAB, sizeof(BatteryStatus));
  126. Status = NtDeviceIoControlFile(
  127. DriverHandle,
  128. (HANDLE) NULL, // event
  129. (PIO_APC_ROUTINE) NULL,
  130. (PVOID) NULL,
  131. &IOSB,
  132. IOCTL_BATTERY_QUERY_STATUS,
  133. WaitStatus, // input buffer
  134. sizeof (BATTERY_WAIT_STATUS),
  135. BatteryStatus, // output buffer
  136. sizeof (BATTERY_STATUS)
  137. );
  138. if (!NT_SUCCESS(Status)) {
  139. printf ("Query battery status failed. Status = %x\n", Status);
  140. return ;
  141. }
  142. // dump battery status
  143. printf ("Power State.........: %08x\n", BatteryStatus->PowerState);
  144. printf ("Capacity............: %08x %d\n", BatteryStatus->Capacity, BatteryStatus->Capacity);
  145. printf ("Voltage.............: %08x %d\n", BatteryStatus->Voltage, BatteryStatus->Voltage);
  146. printf ("Current.,,,,,,,,,...: %08x %d\n", BatteryStatus->Current, BatteryStatus->Current);
  147. printf ("\n");
  148. }
  149. int
  150. __cdecl
  151. main(USHORT argc, CHAR **argv)
  152. {
  153. ULONG BattTag;
  154. BATTERY_INFORMATION BInfo;
  155. ULONG BETime;
  156. WCHAR BDeviceName[50];
  157. UCHAR BManDate[50];
  158. WCHAR BManName[50];
  159. ULONG BETemp;
  160. WCHAR BEUID [50];
  161. ULONG BEGran[4];
  162. BATTERY_WAIT_STATUS WStat;
  163. BATTERY_STATUS BStat;
  164. CHAR *NamePtr;
  165. if (argc > 1) {
  166. NamePtr=argv[1];
  167. } else {
  168. NamePtr = "CmBatt";
  169. }
  170. //
  171. // Locate pentium perf driver
  172. //
  173. if (!InitDriver (NamePtr)) {
  174. printf ("CmBatt not found\n");
  175. exit (1);
  176. }
  177. GetBatteryTag ();
  178. printf ("Battery Tag.........: %x\n", BatteryTag);
  179. //
  180. // Get generic info
  181. //
  182. GetBatteryInfo (BatteryInformation, &BInfo, sizeof(BInfo));
  183. GetBatteryInfo (BatteryEstimatedTime, &BETime, sizeof(BETime));
  184. GetBatteryInfo (BatteryDeviceName, BDeviceName, sizeof(BDeviceName));
  185. GetBatteryInfo (BatteryManufactureDate, BManDate, sizeof(BManDate));
  186. GetBatteryInfo (BatteryManufactureName, BManName, sizeof(BManName));
  187. GetBatteryInfo (BatteryTemperature, &BETemp, sizeof(BETemp));
  188. GetBatteryInfo (BatteryUniqueID, BEUID, sizeof(BEUID));
  189. GetBatteryInfo (BatteryGranularityInformation, BEGran, sizeof(BEGran));
  190. // dump it...
  191. printf ("Capabilities........: %08x\n", BInfo.Capabilities);
  192. printf ("Technology..........: %02x\n", BInfo.Technology);
  193. printf ("Chemisttry..........: %4.4s\n", BInfo.Chemistry);
  194. printf ("Designed Capacity...: %08x\n", BInfo.DesignedCapacity);
  195. printf ("FullCharged Capacity: %08x\n", BInfo.FullChargedCapacity);
  196. printf ("Default Alert1......: %08x\n", BInfo.DefaultAlert1);
  197. printf ("Default Alert2......: %08x\n", BInfo.DefaultAlert2);
  198. printf ("Critical Bias.......: %08x\n", BInfo.CriticalBias);
  199. printf ("Cycle Count.........: %08x\n", BInfo.CycleCount);
  200. printf ("Granularity.........: %x %x %x %x\n", BEGran[0], BEGran[1], BEGran[2], BEGran[3]);
  201. printf ("Temapture...........: %08x\n", BETemp);
  202. wprintf (L"Unique ID...........: %s\n", BEUID);
  203. printf ("Estimated Time......: %08x\n", BETime);
  204. wprintf (L"Device Name.........: %s\n", BDeviceName);
  205. printf ("Manufacture Date....: %d %d %d %d\n", BManDate[0], BManDate[1], BManDate[2], BManDate[3]);
  206. wprintf (L"Manufacture Name....: %s\n", BManName);
  207. printf ("\n");
  208. memset (&WStat, 0, sizeof(WStat));
  209. WStat.BatteryTag = BatteryTag;
  210. GetBatteryStatus (&WStat, &BStat);
  211. // Now that we've got the state, let's perform a long term status change request
  212. WStat.PowerState = BStat.PowerState;
  213. WStat.LowCapacity = BStat.Capacity - RANGE;
  214. WStat.HighCapacity = BStat.Capacity + RANGE;
  215. WStat.Timeout = 50000000; // 5 min
  216. GetBatteryStatus (&WStat, &BStat);
  217. }
  218. BOOLEAN
  219. InitDriver (
  220. CHAR *NamePtr
  221. )
  222. {
  223. UNICODE_STRING DriverName;
  224. ANSI_STRING AnsiName;
  225. NTSTATUS status;
  226. OBJECT_ATTRIBUTES ObjA;
  227. IO_STATUS_BLOCK IOSB;
  228. UCHAR strbuf[100];
  229. sprintf (strbuf, "\\Device\\%s",NamePtr);
  230. RtlInitAnsiString(&AnsiName, strbuf);
  231. RtlAnsiStringToUnicodeString(&DriverName, &AnsiName, TRUE);
  232. InitializeObjectAttributes(
  233. &ObjA,
  234. &DriverName,
  235. OBJ_CASE_INSENSITIVE,
  236. 0,
  237. 0 );
  238. status = NtOpenFile (
  239. &DriverHandle, // return handle
  240. SYNCHRONIZE | FILE_READ_DATA | FILE_WRITE_DATA, // desired access
  241. &ObjA, // Object
  242. &IOSB, // io status block
  243. FILE_SHARE_READ | FILE_SHARE_WRITE, // share access
  244. FILE_SYNCHRONOUS_IO_ALERT // open options
  245. );
  246. if (!NT_SUCCESS(status)) {
  247. printf ("Device name %s Error %x\n", strbuf, status);
  248. return FALSE;
  249. } else {
  250. printf ("Opened Device name %s\n", strbuf);
  251. return TRUE;
  252. }
  253. }