Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. //--------------------------------------------------------------------
  2. // TUCN, Computer Science Department
  3. // Input/Output Systems and Peripheral Devices
  4. //--------------------------------------------------------------------
  5. // http://users.utcluj.ro/~baruch/en/pages/teaching/inputoutput-systems/laboratory.php
  6. //--------------------------------------------------------------------
  7. // File: AppScroll-e.cpp
  8. // Date: 10.10.2015
  9. // Modified: 21.11.2015
  10. //--------------------------------------------------------------------
  11. // IOS application example with vertical scroll bar
  12. //--------------------------------------------------------------------
  13.  
  14. #include <windows.h>
  15. #include "SetupAPI.h"
  16. #include "hidsdi.h"
  17.  
  18. #define NLIN 500 // number of lines in the display window
  19. #define NCOL 240 // number of columns in the display window
  20.  
  21. // Global variables
  22. char szBuffer[NLIN][NCOL]; // buffer for the window contents
  23. int cLine; // number of current line in the display buffer
  24. HANDLE hFile;
  25.  
  26.  
  27. // Declarations of external functions
  28. void DisplWindow(HWND hWnd);
  29. void displayAdditionalInfo(HANDLE hFile);
  30.  
  31. //--------------------------------------------------------------------
  32. // Function AppScroll
  33. //--------------------------------------------------------------------
  34. //
  35. // Function: IOS application example with vertical scroll bar
  36. //
  37. // Parameters: - Handle to the application window
  38. //
  39. // Returns: 0 - Operation completed successfully
  40. // 1 - Error at initializing the Hw driver
  41. //
  42. //--------------------------------------------------------------------
  43.  
  44. void displayIDStringsHID(HWND hWnd)
  45. {
  46. struct _GUID GUID;
  47. HANDLE PnPHandle;
  48. HANDLE hFile;
  49. SP_DEVICE_INTERFACE_DATA interface_data;
  50. PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data;
  51. DWORD last_error;
  52. DWORD RequiredSize;
  53. char cBuffer[256];
  54.  
  55.  
  56. HidD_GetHidGuid(&GUID);
  57. PnPHandle = SetupDiGetClassDevs(&GUID, NULL, NULL,
  58. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  59.  
  60.  
  61. if (PnPHandle == INVALID_HANDLE_VALUE)
  62. {
  63. wsprintf(szBuffer[cLine++], "[ERROR] Comm cannot be established!\n");
  64. return;
  65. }
  66.  
  67. for (int i = 0; i < 20; i++)
  68. {
  69. interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  70. SetupDiEnumDeviceInterfaces(PnPHandle, NULL, &GUID, i, &interface_data);
  71.  
  72. last_error = GetLastError();
  73.  
  74. if (last_error == ERROR_NO_MORE_ITEMS)
  75. {
  76. break;
  77. }
  78.  
  79. SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, NULL, 0, &RequiredSize, NULL);
  80. detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize);
  81. detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  82. BOOLEAN retCode = SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, detail_data,
  83. RequiredSize, &RequiredSize, NULL);
  84.  
  85. if (retCode == FALSE)
  86. {
  87. free(detail_data);
  88. continue;
  89. }
  90.  
  91. hFile = CreateFile(detail_data->DevicePath, 0, //GENERIC_READ | GENERIC_WRITE use for the board
  92. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  93.  
  94. if (INVALID_HANDLE_VALUE == hFile)
  95. {
  96. wsprintf(szBuffer[cLine++], "[ERROR] Error creating file\n");
  97. free(detail_data);
  98. continue;
  99. }
  100.  
  101. HidD_GetProductString(hFile, cBuffer, sizeof(cBuffer));
  102.  
  103. free(detail_data);
  104.  
  105. wsprintf(szBuffer[cLine++], "%ls", cBuffer);
  106. displayAdditionalInfo(hFile);
  107. CloseHandle(hFile);
  108. }
  109.  
  110. SetupDiDestroyDeviceInfoList(PnPHandle);
  111. }
  112.  
  113. boolean ConnectToBoard(HWND hWnd)
  114. {
  115. bool isConnected = false;
  116. struct _GUID GUID;
  117. HANDLE PnPHandle;
  118. SP_DEVICE_INTERFACE_DATA interface_data;
  119. PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data;
  120. DWORD last_error;
  121. DWORD RequiredSize;
  122. char cBuffer[256];
  123.  
  124.  
  125. HidD_GetHidGuid(&GUID);
  126. PnPHandle = SetupDiGetClassDevs(&GUID, NULL, NULL,
  127. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  128.  
  129.  
  130. if (PnPHandle == INVALID_HANDLE_VALUE)
  131. {
  132. wsprintf(szBuffer[cLine++], "[ERROR] Comm cannot be established!\n");
  133. isConnected = false;
  134. }
  135.  
  136. for (int i = 0; i < 20; i++)
  137. {
  138. interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  139. SetupDiEnumDeviceInterfaces(PnPHandle, NULL, &GUID, i, &interface_data);
  140.  
  141. last_error = GetLastError();
  142.  
  143. if (last_error == ERROR_NO_MORE_ITEMS)
  144. {
  145. break;
  146. isConnected = false;
  147. }
  148.  
  149. SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, NULL, 0, &RequiredSize, NULL);
  150. detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize);
  151. detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  152. BOOLEAN retCode = SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, detail_data,
  153. RequiredSize, &RequiredSize, NULL);
  154.  
  155. if (retCode == FALSE)
  156. {
  157. free(detail_data);
  158. isConnected = false;
  159. continue;
  160. }
  161.  
  162. hFile = CreateFile(detail_data->DevicePath, GENERIC_READ | GENERIC_WRITE,
  163. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  164.  
  165. if (INVALID_HANDLE_VALUE == hFile)
  166. {
  167. wsprintf(szBuffer[cLine++], "[ERROR] Error creating file\n");
  168. free(detail_data);
  169. isConnected = false;
  170. continue;
  171. }
  172.  
  173. //step 6
  174. HidD_GetProductString(hFile, cBuffer, sizeof(cBuffer));
  175.  
  176. //step 7
  177. free(detail_data);
  178.  
  179. bool equal = true;
  180. const char board[] = "Keil MCB2140 HID";
  181. for (int i = 0; i < strlen(cBuffer); i++)
  182. {
  183. if (cBuffer[2 * i] != board[i])
  184. {
  185. equal = false;
  186. break;
  187. }
  188. }
  189.  
  190. if (equal)
  191. {
  192. isConnected = true;
  193. }
  194.  
  195. //CloseHandle(hFile);
  196. }
  197.  
  198. SetupDiDestroyDeviceInfoList(PnPHandle);
  199. return isConnected;
  200. }
  201.  
  202. int StatusButtons()
  203. {
  204. BYTE reportBuffer[2] = {0 , 0};
  205. boolean status;
  206. int toReturn = 1;
  207. status = HidD_GetInputReport(hFile, reportBuffer, 2);
  208.  
  209. if (status)
  210. {
  211. wsprintf(szBuffer[cLine++], "%x\n", reportBuffer[1]);
  212. toReturn = 0;
  213. }
  214.  
  215. return toReturn;
  216. }
  217.  
  218.  
  219. void displayAdditionalInfo(HANDLE hFile)
  220. {
  221. HIDD_ATTRIBUTES HidAttributes;
  222. PHIDP_PREPARSED_DATA PreParsedData;
  223. HIDP_CAPS Caps;
  224. NTSTATUS CapsStatus;
  225. boolean statusAttr;
  226. boolean statusPreP;
  227. HidAttributes.Size = sizeof(HIDD_ATTRIBUTES);
  228.  
  229. statusAttr = HidD_GetAttributes(hFile, &HidAttributes);
  230. statusPreP = HidD_GetPreparsedData(hFile, &PreParsedData);
  231. if (statusAttr)
  232. {
  233. wsprintf(szBuffer[cLine++], "ProductID: %x; VendorID: %x; VersionNumber: %x\n", HidAttributes.ProductID, HidAttributes.VendorID, HidAttributes.VersionNumber);
  234. }
  235.  
  236. if (statusPreP)
  237. {
  238. CapsStatus = HidP_GetCaps(PreParsedData, &Caps);
  239. if (CapsStatus == HIDP_STATUS_SUCCESS)
  240. {
  241. wsprintf(szBuffer[cLine++], "UsagePage: %x; UsageID: %x; Length I/P Rep: %x; Length of O/P Rep: %x; Length of Feature Rep: %x", Caps.UsagePage, Caps.Usage, Caps.InputReportByteLength, Caps.OutputReportByteLength, Caps.FeatureReportByteLength);
  242. }
  243. }
  244.  
  245. HidD_FreePreparsedData(PreParsedData);
  246. }
  247.  
  248. int AppScroll(HWND hWnd)
  249. {
  250. int i;
  251.  
  252. char szMes0[] = "Error initializing the Hw driver";
  253. char szMes1[] = "IOS Application";
  254.  
  255. // Erase the display buffer and the window contents
  256. for (i = 0; i < NLIN; i++) {
  257. memset (szBuffer[i], ' ', NCOL);
  258. }
  259. cLine = 1;
  260.  
  261. // Copy the start message into the display buffer and display the message
  262. wsprintf(szBuffer[cLine], szMes1);
  263. cLine += 2;
  264. DisplWindow(hWnd);
  265.  
  266. //--------------------------------------------------------------------
  267. // To be completed with the application's code
  268. //--------------------------------------------------------------------
  269.  
  270.  
  271. displayIDStringsHID(hWnd);
  272.  
  273. /*boolean isConnected;
  274. isConnected = ConnectToBoard(hWnd);
  275.  
  276. if (isConnected)
  277. {
  278. wsprintf(szBuffer[cLine++], "[SUCCESS] Connected to the board! \n");
  279. }
  280. else
  281. {
  282. wsprintf(szBuffer[cLine++], "[ERROR] Could NOT connect to the board! \n");
  283. }
  284.  
  285. while (StatusButtons() == 0)
  286. {
  287. wsprintf(szBuffer[cLine++], "[SUCCESS] \n");
  288. DisplWindow(hWnd);
  289. Sleep(1000);
  290. }*/
  291.  
  292.  
  293. // Display the messages
  294. DisplWindow(hWnd);
  295.  
  296. return 0;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement