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.

406 lines
11 KiB

  1. #define UNICODE
  2. #include <nt.h>
  3. #include <ntrtl.h>
  4. #include <nturtl.h>
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <ctype.h>
  9. #include <wchar.h>
  10. #define NT
  11. //#include <tdi.h>
  12. //#include <winsock2.h>
  13. //#include <wsahelp.h>
  14. //#include <tdistat.h>
  15. //#include <tdiinfo.h>
  16. //#include <llinfo.h>
  17. #include <irioctl.h>
  18. //#include <irda.h>
  19. //#include <irlmp.h>
  20. #define DBG_OUTPUT_DEBUGGER 1
  21. #define DBG_OUTPUT_BUFFER 2
  22. #define DBG_NDIS 0x00000002 // keep in sync with test\irdakdx
  23. #define DBG_TIMER 0x00000004
  24. #define DBG_IRMAC 0x00000008
  25. #define DBG_IRLAP 0x00000010
  26. #define DBG_IRLAPLOG 0x00000020
  27. #define DBG_RXFRAME 0x00000040
  28. #define DBG_TXFRAME 0x00000080
  29. #define DBG_IRLMP 0x00000100
  30. #define DBG_IRLMP_CONN 0x00000200
  31. #define DBG_IRLMP_CRED 0x00000400
  32. #define DBG_IRLMP_IAS 0x00000800
  33. #define DBG_DISCOVERY 0x00001000
  34. #define DBG_PRINT 0x00002000
  35. #define DBG_ADDR 0x00004000
  36. #define DBG_REF 0x00010000
  37. #define DBG_TDI 0x00020000
  38. #define DBG_TDI_IRP 0x00040000
  39. #define DBG_ALLOC 0x10000000
  40. #define DBG_FUNCTION 0x20000000
  41. #define DBG_WARN 0x40000000
  42. #define DBG_ERROR 0x80000000
  43. #define IRDA_DEVICE_NAME TEXT("\\Device\\IrDA")
  44. #define IRWAN_DEVICE_NAME TEXT("\\Device\\IrWAN")
  45. OBJECT_ATTRIBUTES ObjAttr;
  46. UNICODE_STRING DeviceName;
  47. HANDLE DeviceHandle;
  48. UINT i;
  49. HANDLE hFile = 0;
  50. BOOLEAN ConsoleOutput = TRUE;
  51. DWORD KBThreadId;
  52. UINT Dbgs[2];
  53. UINT *pDbgSettings = Dbgs;
  54. UINT *pDbgOutput = Dbgs+1;
  55. HANDLE hMsgsEvent;
  56. CRITICAL_SECTION Cs;
  57. int State;
  58. #define ST_RUNNING 0
  59. #define ST_SETTING 1
  60. #define ST_DONE 2
  61. char Buf[2048];
  62. #define ONOFF(bit) (bit & *pDbgSettings ? "On ": "Off")
  63. void
  64. DispCurrentSettings()
  65. {
  66. NTSTATUS Status;
  67. IO_STATUS_BLOCK IoStatusBlock;
  68. Status = NtDeviceIoControlFile(
  69. DeviceHandle, // HANDLE FileHandle
  70. NULL, // HANDLE Event OPTIONAL
  71. NULL, // PIO_APC_ROUTINE ApcRoutine
  72. NULL, // PVOID ApcContext
  73. &IoStatusBlock, // PIO_STATUS_BLOCK IoStatusBlock
  74. IOCTL_IRDA_GET_DBG_SETTINGS, // ULONG IoControlCode
  75. NULL, // PVOID InputBuffer
  76. 0, // ULONG InputBufferLength
  77. Dbgs, // PVOID OutputBuffer
  78. sizeof(Dbgs)); // ULONG OutputBufferLength
  79. if (!NT_SUCCESS(Status))
  80. {
  81. printf("Ioctl failed %x\n", Status);
  82. return;
  83. }
  84. printf("\nCurrent settings:\n");
  85. printf(" A. RXFRAME...:%s B. TXFRAME...:%s\n",
  86. ONOFF(DBG_RXFRAME), ONOFF(DBG_TXFRAME));
  87. printf(" C. MAC.......:%s D. NDIS......:%s\n",
  88. ONOFF(DBG_IRMAC), ONOFF(DBG_NDIS));
  89. printf(" E. LAPLOG....:%s F. LAP.......:%s\n",
  90. ONOFF(DBG_IRLAPLOG), ONOFF(DBG_IRLAP));
  91. printf(" G. LMP.......:%s H. LMP_CONN..:%s\n",
  92. ONOFF(DBG_IRLMP), ONOFF(DBG_IRLMP_CONN));
  93. printf(" I. LMP_CREDIT:%s J. LMP_IAS:...%s\n",
  94. ONOFF(DBG_IRLMP_CRED), ONOFF(DBG_IRLMP_IAS));
  95. printf(" K. TDI.......:%s L. TDI_IRP...:%s\n",
  96. ONOFF(DBG_TDI), ONOFF(DBG_TDI_IRP));
  97. printf(" M. WARN......:%s N. ERROR.....:%s\n",
  98. ONOFF(DBG_WARN), ONOFF(DBG_ERROR));
  99. printf(" Output:\n");
  100. printf(" O. Debugger..:%s\n",
  101. *pDbgOutput & DBG_OUTPUT_DEBUGGER? "On" : "Off");
  102. printf(" P. Console...:%s\n\n",
  103. *pDbgOutput & DBG_OUTPUT_BUFFER? "On" : "Off");
  104. printf(" <Enter> to continue\n");
  105. return;
  106. }
  107. ULONG WINAPI
  108. KBThread(LPVOID pvarg)
  109. {
  110. NTSTATUS Status;
  111. IO_STATUS_BLOCK IoStatusBlock;
  112. int Key;
  113. while (1)
  114. {
  115. Key = _getch();
  116. if (Key == 'q' || Key =='Q')
  117. {
  118. EnterCriticalSection(&Cs);
  119. State = ST_DONE;
  120. LeaveCriticalSection(&Cs);
  121. SetEvent(hMsgsEvent);
  122. return 0;
  123. }
  124. if (State != ST_SETTING && Key != 27)
  125. {
  126. continue;
  127. }
  128. switch (Key)
  129. {
  130. case 13:
  131. EnterCriticalSection(&Cs);
  132. State = ST_RUNNING;
  133. printf("running\n");
  134. LeaveCriticalSection(&Cs);
  135. continue;;
  136. case 27:
  137. EnterCriticalSection(&Cs);
  138. if (State != ST_SETTING)
  139. {
  140. State = ST_SETTING;
  141. DispCurrentSettings();
  142. }
  143. LeaveCriticalSection(&Cs);
  144. continue;
  145. case 'a':
  146. case 'A':
  147. *pDbgSettings ^= DBG_RXFRAME;
  148. break;
  149. case 'b':
  150. case 'B':
  151. *pDbgSettings ^= DBG_TXFRAME;
  152. break;
  153. case 'c':
  154. case 'C':
  155. *pDbgSettings ^= DBG_IRMAC;
  156. break;
  157. case 'd':
  158. case 'D':
  159. *pDbgSettings ^= DBG_NDIS;
  160. break;
  161. case 'e':
  162. case 'E':
  163. *pDbgSettings ^= DBG_IRLAPLOG;
  164. break;
  165. case 'f':
  166. case 'F':
  167. *pDbgSettings ^= DBG_IRLAP;
  168. break;
  169. case 'g':
  170. case 'G':
  171. *pDbgSettings ^= DBG_IRLMP;
  172. break;
  173. case 'h':
  174. case 'H':
  175. *pDbgSettings ^= DBG_IRLMP_CONN;
  176. break;
  177. case 'i':
  178. case 'I':
  179. *pDbgSettings ^= DBG_IRLMP_CRED;
  180. break;
  181. case 'j':
  182. case 'J':
  183. *pDbgSettings ^= DBG_IRLMP_IAS;
  184. break;
  185. case 'k':
  186. case 'K':
  187. *pDbgSettings ^= DBG_TDI;
  188. break;
  189. case 'l':
  190. case 'L':
  191. *pDbgSettings ^= DBG_TDI_IRP;
  192. break;
  193. case 'm':
  194. case 'M':
  195. *pDbgSettings ^= DBG_WARN;
  196. break;
  197. case 'n':
  198. case 'N':
  199. *pDbgSettings ^= DBG_ERROR;
  200. break;
  201. case 'o':
  202. case 'O':
  203. *pDbgOutput ^= DBG_OUTPUT_DEBUGGER;
  204. break;
  205. case 'p':
  206. case 'P':
  207. *pDbgOutput ^= DBG_OUTPUT_BUFFER;
  208. break;
  209. default:
  210. continue;
  211. }
  212. Status = NtDeviceIoControlFile(
  213. DeviceHandle, // HANDLE FileHandle
  214. NULL, // HANDLE Event OPTIONAL
  215. NULL, // PIO_APC_ROUTINE ApcRoutine
  216. NULL, // PVOID ApcContext
  217. &IoStatusBlock, // PIO_STATUS_BLOCK IoStatusBlock
  218. IOCTL_IRDA_SET_DBG_SETTINGS, // ULONG IoControlCode
  219. Dbgs, // PVOID InputBuffer
  220. sizeof(Dbgs), // ULONG InputBufferLength
  221. NULL, // PVOID OutputBuffer
  222. 0); // ULONG OutputBufferLength
  223. DispCurrentSettings();
  224. }
  225. return 0;
  226. }
  227. _cdecl main(int argc, char *argv[])
  228. {
  229. NTSTATUS Status;
  230. IO_STATUS_BLOCK IoStatusBlock;
  231. /*
  232. if (argc > 1)
  233. {
  234. hFile = CreateFile(argv[1],
  235. GENERIC_WRITE,
  236. 0,
  237. NULL,
  238. CREATE_ALWAYS,
  239. FILE_ATTRIBUTE_NORMAL,
  240. NULL);
  241. if (hFile == INVALID_HANDLE_VALUE)
  242. {
  243. printf("Couldn't open file %s\n", argv[1]);
  244. return 1;
  245. }
  246. }
  247. if (argc == 3)
  248. ConsoleOutput = FALSE;
  249. */
  250. InitializeCriticalSection(&Cs);
  251. State = ST_RUNNING;
  252. hMsgsEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  253. RtlInitUnicodeString(&DeviceName, IRDA_DEVICE_NAME);
  254. // RtlInitUnicodeString(&DeviceName, IRWAN_DEVICE_NAME);
  255. InitializeObjectAttributes(
  256. &ObjAttr,
  257. &DeviceName,
  258. OBJ_CASE_INSENSITIVE,
  259. NULL,
  260. NULL);
  261. Status = NtCreateFile(
  262. &DeviceHandle, // PHANDLE FileHandle
  263. GENERIC_READ | GENERIC_WRITE, // ACCESS_MASK DesiredAccess
  264. &ObjAttr, // POBJECT_ATTRIBUTES ObjAttr
  265. &IoStatusBlock, // PIO_STATUS_BLOCK IoStatusBlock
  266. NULL, // PLARGE_INTEGER AllocationSize
  267. FILE_ATTRIBUTE_NORMAL, // ULONG FileAttributes
  268. FILE_SHARE_DELETE | FILE_SHARE_READ |
  269. FILE_SHARE_WRITE, // ULONG ShareAccess
  270. FILE_OPEN_IF, // ULONG CreateDisposition
  271. 0, // ULONG CreateOptions
  272. NULL, // PVOID EaBuffer
  273. 0); // ULONG EaLength
  274. if (!NT_SUCCESS(Status))
  275. {
  276. printf("failed to open irda.sys\n");
  277. return 1;
  278. }
  279. CreateThread(NULL, 0, KBThread, 0, 0, &KBThreadId);
  280. printf("<Esc> to enter settings mode, <q> to quit\n");
  281. while (1)
  282. {
  283. Status = NtDeviceIoControlFile(
  284. DeviceHandle, // HANDLE FileHandle
  285. hMsgsEvent, // HANDLE Event OPTIONAL
  286. NULL, // PIO_APC_ROUTINE ApcRoutine
  287. Buf, // PVOID ApcContext
  288. &IoStatusBlock, // PIO_STATUS_BLOCK IoStatusBlock
  289. IOCTL_IRDA_GET_DBG_MSGS, // ULONG IoControlCode
  290. NULL, // PVOID InputBuffer
  291. 0, // ULONG InputBufferLength
  292. Buf, // PVOID OutputBuffer
  293. sizeof(Buf)); // ULONG OutputBufferLength
  294. if (Status != STATUS_PENDING && Status != STATUS_SUCCESS)
  295. {
  296. printf("ioctl failed %X\n", Status);
  297. break;
  298. }
  299. if (Status == STATUS_PENDING)
  300. {
  301. WaitForSingleObject(hMsgsEvent, INFINITE);
  302. EnterCriticalSection(&Cs);
  303. if (State == ST_DONE)
  304. {
  305. return 0;
  306. }
  307. else
  308. {
  309. LeaveCriticalSection(&Cs);
  310. }
  311. }
  312. if (IoStatusBlock.Information >= 2048)
  313. {
  314. printf("wow, too big\n");
  315. break;
  316. }
  317. if (ConsoleOutput && State == ST_RUNNING)
  318. fwrite(Buf, IoStatusBlock.Information, 1, stdout);
  319. /*
  320. if (hFile)
  321. fwrite(Buf, 1, IoStatusBlock.Information, stdout);
  322. */
  323. }
  324. NtClose(DeviceHandle);
  325. return 0;
  326. }