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.

182 lines
5.3 KiB

  1. #define __HANDLE_C__
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <wtypes.h>
  5. #include "hidsdi.h"
  6. #include "list.h"
  7. #include "hidtest.h"
  8. #include "handle.h"
  9. #include "debug.h"
  10. /*****************************************************************************
  11. /* Miscellaneous definitions
  12. /*****************************************************************************/
  13. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  14. /*****************************************************************************
  15. /* Macro definitions for creating additional device handles
  16. /*****************************************************************************/
  17. #define NUM_ILLEGAL_HANDLES 2
  18. /*****************************************************************************
  19. /* Module specific typedefs
  20. /*****************************************************************************/
  21. typedef enum { HANDLE_STATE_CALLER, HANDLE_STATE_ADDL, HANDLE_STATE_ILLEGAL } HANDLE_STATES;
  22. /*****************************************************************************
  23. /* Module global variable declarations for device handle generation
  24. /*****************************************************************************/
  25. static CHAR String[1024];
  26. static DEVICE_STRING CurrentDeviceName;
  27. static ULONG NumCallerHandles;
  28. static ULONG NumAddlHandles;
  29. static HANDLE CallerHandleArray[MAX_NUM_HANDLES];
  30. static HANDLE AddlHandleArray[MAX_NUM_HANDLES];
  31. static ULONG NextHandleIndex;
  32. static ULONG NextIllegalHandle;
  33. static HANDLE_STATES CurrentHandleState;
  34. static HANDLE IllegalHandleArray[NUM_ILLEGAL_HANDLES] = {
  35. INVALID_HANDLE_VALUE,
  36. NULL
  37. };
  38. /*****************************************************************************
  39. /* External function definitions
  40. /*****************************************************************************/
  41. VOID
  42. HIDTest_InitDeviceHandles(
  43. IN DEVICE_STRING DeviceName,
  44. IN ULONG nAddlHandles,
  45. IN ULONG nCallerHandles,
  46. IN HANDLE *HandleList
  47. )
  48. {
  49. ULONG Index;
  50. /*
  51. // Store all the caller passed in handles
  52. */
  53. NumCallerHandles = MIN(MAX_NUM_HANDLES, nCallerHandles);
  54. for (Index = 0; Index < NumCallerHandles; Index++) {
  55. CallerHandleArray[Index] = *(HandleList+Index);
  56. }
  57. /*
  58. // Open up the number of additional handles that the caller specifies,
  59. // Currently, we will open every other one for OVERLAPPED I/O,
  60. */
  61. NumAddlHandles = MIN(MAX_NUM_HANDLES, nAddlHandles);
  62. for (Index = 0; Index < NumAddlHandles; Index++) {
  63. AddlHandleArray[Index] = CreateFile(DeviceName,
  64. GENERIC_READ | GENERIC_WRITE,
  65. FILE_SHARE_READ | FILE_SHARE_WRITE,
  66. NULL,
  67. OPEN_EXISTING,
  68. (Index % 2) ? FILE_FLAG_OVERLAPPED : 0,
  69. NULL);
  70. if (INVALID_HANDLE_VALUE == AddlHandleArray[Index]) {
  71. ASSERT (0);
  72. NumAddlHandles = Index;
  73. break;
  74. }
  75. }
  76. HIDTest_ResetDeviceHandles();
  77. return;
  78. }
  79. VOID
  80. HIDTest_ResetDeviceHandles(
  81. VOID
  82. )
  83. {
  84. CurrentHandleState = HANDLE_STATE_CALLER;
  85. if (0 == NumCallerHandles) {
  86. if (0 == NumAddlHandles) {
  87. CurrentHandleState = HANDLE_STATE_ILLEGAL;
  88. }
  89. else {
  90. CurrentHandleState = HANDLE_STATE_ADDL;
  91. }
  92. }
  93. NextHandleIndex = 0;
  94. return;
  95. }
  96. BOOL
  97. HIDTest_GetDeviceHandle(
  98. HANDLE *Handle,
  99. BOOL *IsLegal
  100. )
  101. {
  102. BOOL RetVal = TRUE;
  103. switch (CurrentHandleState) {
  104. case HANDLE_STATE_CALLER:
  105. ASSERT (NextHandleIndex < NumCallerHandles);
  106. *Handle = CallerHandleArray[NextHandleIndex++];
  107. *IsLegal = TRUE;
  108. if (NextHandleIndex >= NumCallerHandles) {
  109. NextHandleIndex = 0;
  110. CurrentHandleState = (NumAddlHandles > 0) ? HANDLE_STATE_ADDL :
  111. HANDLE_STATE_ILLEGAL;
  112. }
  113. break;
  114. case HANDLE_STATE_ADDL:
  115. ASSERT (NextHandleIndex < NumAddlHandles);
  116. *Handle = AddlHandleArray[NextHandleIndex++];
  117. *IsLegal = TRUE;
  118. if (NextHandleIndex >= NumAddlHandles) {
  119. NextHandleIndex = 0;
  120. CurrentHandleState = HANDLE_STATE_ILLEGAL;
  121. }
  122. break;
  123. case HANDLE_STATE_ILLEGAL:
  124. if (NextHandleIndex >= NUM_ILLEGAL_HANDLES) {
  125. RetVal = FALSE;
  126. }
  127. else {
  128. *Handle = IllegalHandleArray[NextHandleIndex++];
  129. *IsLegal = FALSE;
  130. }
  131. break;
  132. default:
  133. ASSERT(0);
  134. }
  135. return (RetVal);
  136. }
  137. VOID
  138. HIDTest_CloseDeviceHandles(
  139. VOID
  140. )
  141. {
  142. ULONG Index;
  143. for (Index = 0; Index < NumAddlHandles; Index++) {
  144. CloseHandle(AddlHandleArray[Index]);
  145. }
  146. return;
  147. }