Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------------------------------------------------------
- // TUCN, Computer Science Department
- // Input/Output Systems and Peripheral Devices
- //--------------------------------------------------------------------
- // http://users.utcluj.ro/~baruch/en/pages/teaching/inputoutput-systems/laboratory.php
- //--------------------------------------------------------------------
- // File: AppScroll-e.cpp
- // Date: 10.10.2015
- // Modified: 21.11.2015
- //--------------------------------------------------------------------
- // IOS application example with vertical scroll bar
- //--------------------------------------------------------------------
- #include <windows.h>
- #include "SetupAPI.h"
- #include "hidsdi.h"
- #define NLIN 500 // number of lines in the display window
- #define NCOL 240 // number of columns in the display window
- // Global variables
- char szBuffer[NLIN][NCOL]; // buffer for the window contents
- int cLine; // number of current line in the display buffer
- HANDLE hFile;
- // Declarations of external functions
- void DisplWindow(HWND hWnd);
- void displayAdditionalInfo(HANDLE hFile);
- //--------------------------------------------------------------------
- // Function AppScroll
- //--------------------------------------------------------------------
- //
- // Function: IOS application example with vertical scroll bar
- //
- // Parameters: - Handle to the application window
- //
- // Returns: 0 - Operation completed successfully
- // 1 - Error at initializing the Hw driver
- //
- //--------------------------------------------------------------------
- void displayIDStringsHID(HWND hWnd)
- {
- struct _GUID GUID;
- HANDLE PnPHandle;
- HANDLE hFile;
- SP_DEVICE_INTERFACE_DATA interface_data;
- PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data;
- DWORD last_error;
- DWORD RequiredSize;
- char cBuffer[256];
- HidD_GetHidGuid(&GUID);
- PnPHandle = SetupDiGetClassDevs(&GUID, NULL, NULL,
- DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
- if (PnPHandle == INVALID_HANDLE_VALUE)
- {
- wsprintf(szBuffer[cLine++], "[ERROR] Comm cannot be established!\n");
- return;
- }
- for (int i = 0; i < 20; i++)
- {
- interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
- SetupDiEnumDeviceInterfaces(PnPHandle, NULL, &GUID, i, &interface_data);
- last_error = GetLastError();
- if (last_error == ERROR_NO_MORE_ITEMS)
- {
- break;
- }
- SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, NULL, 0, &RequiredSize, NULL);
- detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize);
- detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
- BOOLEAN retCode = SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, detail_data,
- RequiredSize, &RequiredSize, NULL);
- if (retCode == FALSE)
- {
- free(detail_data);
- continue;
- }
- hFile = CreateFile(detail_data->DevicePath, 0, //GENERIC_READ | GENERIC_WRITE use for the board
- FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
- if (INVALID_HANDLE_VALUE == hFile)
- {
- wsprintf(szBuffer[cLine++], "[ERROR] Error creating file\n");
- free(detail_data);
- continue;
- }
- HidD_GetProductString(hFile, cBuffer, sizeof(cBuffer));
- free(detail_data);
- wsprintf(szBuffer[cLine++], "%ls", cBuffer);
- displayAdditionalInfo(hFile);
- CloseHandle(hFile);
- }
- SetupDiDestroyDeviceInfoList(PnPHandle);
- }
- boolean ConnectToBoard(HWND hWnd)
- {
- bool isConnected = false;
- struct _GUID GUID;
- HANDLE PnPHandle;
- SP_DEVICE_INTERFACE_DATA interface_data;
- PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data;
- DWORD last_error;
- DWORD RequiredSize;
- char cBuffer[256];
- HidD_GetHidGuid(&GUID);
- PnPHandle = SetupDiGetClassDevs(&GUID, NULL, NULL,
- DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
- if (PnPHandle == INVALID_HANDLE_VALUE)
- {
- wsprintf(szBuffer[cLine++], "[ERROR] Comm cannot be established!\n");
- isConnected = false;
- }
- for (int i = 0; i < 20; i++)
- {
- interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
- SetupDiEnumDeviceInterfaces(PnPHandle, NULL, &GUID, i, &interface_data);
- last_error = GetLastError();
- if (last_error == ERROR_NO_MORE_ITEMS)
- {
- break;
- isConnected = false;
- }
- SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, NULL, 0, &RequiredSize, NULL);
- detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize);
- detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
- BOOLEAN retCode = SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, detail_data,
- RequiredSize, &RequiredSize, NULL);
- if (retCode == FALSE)
- {
- free(detail_data);
- isConnected = false;
- continue;
- }
- hFile = CreateFile(detail_data->DevicePath, GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
- if (INVALID_HANDLE_VALUE == hFile)
- {
- wsprintf(szBuffer[cLine++], "[ERROR] Error creating file\n");
- free(detail_data);
- isConnected = false;
- continue;
- }
- //step 6
- HidD_GetProductString(hFile, cBuffer, sizeof(cBuffer));
- //step 7
- free(detail_data);
- bool equal = true;
- const char board[] = "Keil MCB2140 HID";
- for (int i = 0; i < strlen(cBuffer); i++)
- {
- if (cBuffer[2 * i] != board[i])
- {
- equal = false;
- break;
- }
- }
- if (equal)
- {
- isConnected = true;
- }
- //CloseHandle(hFile);
- }
- SetupDiDestroyDeviceInfoList(PnPHandle);
- return isConnected;
- }
- int StatusButtons()
- {
- BYTE reportBuffer[2] = {0 , 0};
- boolean status;
- int toReturn = 1;
- status = HidD_GetInputReport(hFile, reportBuffer, 2);
- if (status)
- {
- wsprintf(szBuffer[cLine++], "%x\n", reportBuffer[1]);
- toReturn = 0;
- }
- return toReturn;
- }
- void displayAdditionalInfo(HANDLE hFile)
- {
- HIDD_ATTRIBUTES HidAttributes;
- PHIDP_PREPARSED_DATA PreParsedData;
- HIDP_CAPS Caps;
- NTSTATUS CapsStatus;
- boolean statusAttr;
- boolean statusPreP;
- HidAttributes.Size = sizeof(HIDD_ATTRIBUTES);
- statusAttr = HidD_GetAttributes(hFile, &HidAttributes);
- statusPreP = HidD_GetPreparsedData(hFile, &PreParsedData);
- if (statusAttr)
- {
- wsprintf(szBuffer[cLine++], "ProductID: %x; VendorID: %x; VersionNumber: %x\n", HidAttributes.ProductID, HidAttributes.VendorID, HidAttributes.VersionNumber);
- }
- if (statusPreP)
- {
- CapsStatus = HidP_GetCaps(PreParsedData, &Caps);
- if (CapsStatus == HIDP_STATUS_SUCCESS)
- {
- 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);
- }
- }
- HidD_FreePreparsedData(PreParsedData);
- }
- int AppScroll(HWND hWnd)
- {
- int i;
- char szMes0[] = "Error initializing the Hw driver";
- char szMes1[] = "IOS Application";
- // Erase the display buffer and the window contents
- for (i = 0; i < NLIN; i++) {
- memset (szBuffer[i], ' ', NCOL);
- }
- cLine = 1;
- // Copy the start message into the display buffer and display the message
- wsprintf(szBuffer[cLine], szMes1);
- cLine += 2;
- DisplWindow(hWnd);
- //--------------------------------------------------------------------
- // To be completed with the application's code
- //--------------------------------------------------------------------
- displayIDStringsHID(hWnd);
- /*boolean isConnected;
- isConnected = ConnectToBoard(hWnd);
- if (isConnected)
- {
- wsprintf(szBuffer[cLine++], "[SUCCESS] Connected to the board! \n");
- }
- else
- {
- wsprintf(szBuffer[cLine++], "[ERROR] Could NOT connect to the board! \n");
- }
- while (StatusButtons() == 0)
- {
- wsprintf(szBuffer[cLine++], "[SUCCESS] \n");
- DisplWindow(hWnd);
- Sleep(1000);
- }*/
- // Display the messages
- DisplWindow(hWnd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement