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.

355 lines
9.8 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 <ntpoapi.h>
  22. #include "..\cmbdrect.h"
  23. //
  24. // Prototypes
  25. //
  26. HANDLE InitDriver ( CHAR *NamePtr );
  27. void Call_UID (HANDLE Driver) {
  28. NTSTATUS Status;
  29. IO_STATUS_BLOCK IOSB;
  30. UINT Data;
  31. Status = NtDeviceIoControlFile(
  32. Driver,
  33. (HANDLE) NULL, // event
  34. (PIO_APC_ROUTINE) NULL,
  35. (PVOID) NULL,
  36. &IOSB,
  37. IOCTL_CMBATT_UID,
  38. (PVOID) NULL, // input buffer
  39. 0,
  40. &Data, // output buffer
  41. sizeof (Data)
  42. );
  43. if (!NT_SUCCESS(Status)) {
  44. printf ("_UID Method failed. Status = 0x%08lx\n", Status);
  45. } else {
  46. printf("_UID returned: 0x%08lx\n", Data);
  47. };
  48. }
  49. void Call_STA (HANDLE Driver) {
  50. NTSTATUS Status;
  51. IO_STATUS_BLOCK IOSB;
  52. ULONG Data;
  53. Status = NtDeviceIoControlFile(
  54. Driver,
  55. (HANDLE) NULL, // event
  56. (PIO_APC_ROUTINE) NULL,
  57. (PVOID) NULL,
  58. &IOSB,
  59. IOCTL_CMBATT_STA,
  60. (PVOID) NULL, // input buffer
  61. 0,
  62. &Data, // output buffer
  63. sizeof (Data)
  64. );
  65. if (!NT_SUCCESS(Status)) {
  66. printf ("_STA Method failed. Status = 0x%08lx\n", Status);
  67. } else {
  68. printf("_STA returned: 0x%08lx\n", Data);
  69. };
  70. }
  71. void Call_PSR (HANDLE Driver) {
  72. NTSTATUS Status;
  73. IO_STATUS_BLOCK IOSB;
  74. ULONG Data;
  75. Status = NtDeviceIoControlFile(
  76. Driver,
  77. (HANDLE) NULL, // event
  78. (PIO_APC_ROUTINE) NULL,
  79. (PVOID) NULL,
  80. &IOSB,
  81. IOCTL_CMBATT_PSR,
  82. (PVOID) NULL, // input buffer
  83. 0,
  84. &Data, // output buffer
  85. sizeof (Data)
  86. );
  87. if (!NT_SUCCESS(Status)) {
  88. printf ("_PSR Method failed. Status = 0x%08lx\n", Status);
  89. } else {
  90. printf("_PSR returned: 0x%08lx\n", Data);
  91. };
  92. }
  93. void Call_BTP (HANDLE Driver) {
  94. NTSTATUS Status;
  95. IO_STATUS_BLOCK IOSB;
  96. ULONG Data;
  97. printf ("Enter the Trip Point value (Hex): ");
  98. scanf ("%x", &Data);
  99. Status = NtDeviceIoControlFile(
  100. Driver,
  101. (HANDLE) NULL, // event
  102. (PIO_APC_ROUTINE) NULL,
  103. (PVOID) NULL,
  104. &IOSB,
  105. IOCTL_CMBATT_BTP,
  106. &Data, // input buffer
  107. sizeof (Data),
  108. (PVOID) NULL, // output buffer
  109. 0
  110. );
  111. if (!NT_SUCCESS(Status)) {
  112. printf ("\n_BTP Method failed. Status = 0x%08lx\n", Status);
  113. printf("\nSet trip point to 0x%08lx failed.\n", Data);
  114. } else {
  115. printf("\nTrip point set to 0x%08lx\n", Data);
  116. };
  117. }
  118. void Call_BIF (HANDLE Driver) {
  119. NTSTATUS Status;
  120. IO_STATUS_BLOCK IOSB;
  121. CM_BIF_BAT_INFO Data;
  122. Status = NtDeviceIoControlFile(
  123. Driver,
  124. (HANDLE) NULL, // event
  125. (PIO_APC_ROUTINE) NULL,
  126. (PVOID) NULL,
  127. &IOSB,
  128. IOCTL_CMBATT_BIF,
  129. (PVOID) NULL, // input buffer
  130. 0,
  131. &Data, // output buffer
  132. sizeof (Data)
  133. );
  134. if (!NT_SUCCESS(Status)) {
  135. printf ("_BIF Metod failed. Status = 0x%08lx\n", Status);
  136. } else {
  137. printf("_BIF returned:\n");
  138. printf(" Power Unit = 0x%08lx\n", Data.PowerUnit);
  139. printf(" Design Capacity = 0x%08lx\n", Data.DesignCapacity);
  140. printf(" Last Full Charge Capacity = 0x%08lx\n", Data.LastFullChargeCapacity);
  141. printf(" Battery Technology = 0x%08lx\n", Data.BatteryTechnology);
  142. printf(" Design Voltage = 0x%08lx\n", Data.DesignVoltage);
  143. printf(" Design Capacity Of Warning = 0x%08lx\n", Data.DesignCapacityOfWarning);
  144. printf(" Design Capacity Of Low = 0x%08lx\n", Data.DesignCapacityOfLow);
  145. printf(" Battery Capacity Granularity 1 (low -> warning) = 0x%08lx\n", Data.BatteryCapacityGran_1);
  146. printf(" Battery Capacity Granularity 2 (warning -> full) = 0x%08lx\n", Data.BatteryCapacityGran_2);
  147. printf(" Model number = \"%s\"\n", Data.ModelNumber);
  148. printf(" Serial number = \"%s\"\n", Data.SerialNumber);
  149. printf(" Battery Type = \"%s\"\n", Data.BatteryType);
  150. printf(" OEM Information = \"%s\"\n", Data.OEMInformation);
  151. };
  152. }
  153. void Call_BST (HANDLE Driver) {
  154. NTSTATUS Status;
  155. IO_STATUS_BLOCK IOSB;
  156. CM_BST_BAT_INFO Data;
  157. Status = NtDeviceIoControlFile(
  158. Driver,
  159. (HANDLE) NULL, // event
  160. (PIO_APC_ROUTINE) NULL,
  161. (PVOID) NULL,
  162. &IOSB,
  163. IOCTL_CMBATT_BST,
  164. (PVOID) NULL, // input buffer
  165. 0,
  166. &Data, // output buffer
  167. sizeof (Data)
  168. );
  169. if (!NT_SUCCESS(Status)) {
  170. printf ("_BST Metod failed. Status = 0x%08lx\n", Status);
  171. } else {
  172. printf("_BST returned:\n");
  173. printf(" Battery Status = 0x%08lx\n", Data.BatteryState);
  174. printf(" Present Rate = 0x%08lx\n", Data.PresentRate);
  175. printf(" Remaining Capacity = 0x%08lx\n", Data.RemainingCapacity);
  176. printf(" Present Voltage = 0x%08lx\n", Data.PresentVoltage);
  177. };
  178. }
  179. int
  180. __cdecl
  181. main(USHORT argc, CHAR **argv)
  182. {
  183. CHAR *NamePtr;
  184. ULONG cmd;
  185. HANDLE DriverHandle;
  186. if (argc > 1) {
  187. NamePtr=argv[1];
  188. } else {
  189. NamePtr = "ControlMethodBattery1";
  190. }
  191. //
  192. // Locate driver
  193. //
  194. if (!(DriverHandle = InitDriver (NamePtr))) {
  195. printf ("CmBatt not found\n");
  196. exit (1);
  197. }
  198. while (1) {
  199. printf("\n\nOptions: (1)_UID (2)_STA (3)_PSR (4)_BTP (5)_BIF (6)_BST\n");
  200. printf( ">>>>>>>> ");
  201. if (scanf ("%d", &cmd) != 1) {
  202. return (0);
  203. printf("\n\n");
  204. };
  205. printf("\n");
  206. switch (cmd) {
  207. case 1 :
  208. Call_UID (DriverHandle);
  209. break;
  210. case 2 :
  211. Call_STA (DriverHandle);
  212. break;
  213. case 3 :
  214. Call_PSR (DriverHandle);
  215. break;
  216. case 4 :
  217. Call_BTP (DriverHandle);
  218. break;
  219. case 5 :
  220. Call_BIF (DriverHandle);
  221. break;
  222. case 6 :
  223. Call_BST (DriverHandle);
  224. break;
  225. }
  226. }
  227. }
  228. HANDLE
  229. InitDriver (
  230. CHAR *NamePtr
  231. )
  232. {
  233. UNICODE_STRING DriverName;
  234. ANSI_STRING AnsiName;
  235. NTSTATUS status;
  236. OBJECT_ATTRIBUTES ObjA;
  237. IO_STATUS_BLOCK IOSB;
  238. UCHAR strbuf[100];
  239. HANDLE DriverHandle;
  240. SYSTEM_POWER_CAPABILITIES powerCapabilities;
  241. if (NT_SUCCESS (NtPowerInformation (SystemPowerCapabilities,
  242. NULL,
  243. 0,
  244. &powerCapabilities,
  245. sizeof (SYSTEM_POWER_CAPABILITIES)
  246. ))) {
  247. printf("NtPowerInformation returned: \n"
  248. " SystemBatteriesPresent = %d\n"
  249. " BatteriesAreShortTerm = %d\n",
  250. powerCapabilities.SystemBatteriesPresent,
  251. powerCapabilities.BatteriesAreShortTerm);
  252. }
  253. sprintf (strbuf, "\\Device\\%s",NamePtr);
  254. RtlInitAnsiString(&AnsiName, strbuf);
  255. RtlAnsiStringToUnicodeString(&DriverName, &AnsiName, TRUE);
  256. InitializeObjectAttributes(
  257. &ObjA,
  258. &DriverName,
  259. OBJ_CASE_INSENSITIVE,
  260. 0,
  261. 0 );
  262. status = NtOpenFile (
  263. &DriverHandle, // return handle
  264. SYNCHRONIZE | FILE_READ_DATA | FILE_WRITE_DATA, // desired access
  265. &ObjA, // Object
  266. &IOSB, // io status block
  267. FILE_SHARE_READ | FILE_SHARE_WRITE, // share access
  268. FILE_SYNCHRONOUS_IO_ALERT // open options
  269. );
  270. if (!NT_SUCCESS(status)) {
  271. printf ("Device name %s Error 0x%08lx\n", strbuf, status);
  272. return NULL;
  273. } else {
  274. printf ("Opened Device name %s\n", strbuf);
  275. return DriverHandle;
  276. }
  277. }