Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <set>
- #include <wtypes.h>
- #include <Winusb.h>
- #include <SetupAPI.h>
- #include <cfgmgr32.h>
- #include <sstream>
- #pragma comment(lib, "Winusb.lib")
- #pragma comment(lib, "setupapi.lib")
- #define DELAY 10
- #define DELAY2 100
- #define REPORT_SIZE 0x5A
- #define RAZER_VID 0x1532
- #define RBP2016_PID 0x0210
- static const GUID GUID_DEVINTERFACE = { 0xDEE824EF, 0x729B, 0x4A0E, 0x9C, 0x14, 0xB7, 0x11, 0x7D, 0x33, 0xA8, 0x17 }; // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DeviceAccess\CapabilityHandlers\{DEE824EF-729B-4A0E-9C14-B7117D33A817} // works for keyboard 1532,0210,03,WINUSB (v10.0.14393.0)
- static const char* COLORARR[6] = {
- "\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF\x00\xFF\xFF\xFF\x00\xFF\xFF\xFF\x00"
- ,"\xFF\xFF\x00\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF\x00\xFF\xFF\xFF\x00\xFF"
- ,"\xFF\x00\xFF\xFF\xFF\x00\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF\x00\xFF\xFF"
- ,"\x00\xFF\xFF\xFF\x00\xFF\xFF\xFF\x00\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF"
- ,"\x00\x00\xFF\x00\xFF\xFF\xFF\x00\xFF\xFF\xFF\x00\xFF\x00\x00\x00\xFF\x00"
- ,"\x00\xFF\x00\x00\x00\xFF\x00\xFF\xFF\xFF\x00\xFF\xFF\xFF\x00\xFF\x00\x00"
- };
- void listAllDevices(unsigned int uiVID) {
- HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, TEXT("USB"), NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);
- if (hDevInfo == INVALID_HANDLE_VALUE)
- printf("SetupDiGetClassDevs failed\n");
- SP_DEVINFO_DATA deviceData = { 0 };
- deviceData.cbSize = sizeof(SP_DEVINFO_DATA);
- for (unsigned int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &deviceData); ++i) {
- unsigned long size;
- CM_Get_Device_ID_Size(&size, deviceData.DevInst, 0);
- char deviceID[MAX_DEVICE_ID_LEN];
- CM_Get_Device_ID(deviceData.DevInst, deviceID, MAX_PATH, 0);
- if (CM_Get_Device_ID(deviceData.DevInst, deviceID, MAX_DEVICE_ID_LEN, 0))
- continue;
- char* vid = strstr(deviceID, "VID_");
- if (!vid || uiVID != strtoul(vid+4, NULL, 16))
- continue;
- printf("found device (%s)\n",deviceID);
- }
- }
- WINUSB_INTERFACE_HANDLE connect_winusb(unsigned int uiVID, unsigned int uiPID) {
- HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE, 0, 0, DIGCF_DEVICEINTERFACE);
- if (hDevInfo == INVALID_HANDLE_VALUE) {
- printf("SetupDiGetClassDevs failed\n");
- return NULL;
- }
- SP_DEVINFO_DATA deviceData = { 0 };
- deviceData.cbSize = sizeof(SP_DEVINFO_DATA);
- for (unsigned int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &deviceData); ++i) {
- char deviceID[MAX_DEVICE_ID_LEN];
- if (CM_Get_Device_ID(deviceData.DevInst, deviceID, MAX_DEVICE_ID_LEN, 0))
- continue;
- char* vid = strstr(deviceID, "VID_");
- if (!vid || uiVID != strtoul(vid+4, NULL, 16))
- continue;
- char* pid = strstr(deviceID, "PID_");
- if (!pid || uiPID != strtoul(pid+4, NULL, 16))
- continue;
- /*char* mi = strstr(deviceID, "MI_");
- if (!mi || uiMI != strtoul(mi+3, NULL, 16))
- continue;*/
- SP_INTERFACE_DEVICE_DATA interfaceData = { 0 };
- interfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
- if (!SetupDiEnumDeviceInterfaces(hDevInfo, &deviceData, &GUID_DEVINTERFACE, 0, &interfaceData))
- continue;
- DWORD dwRequiredSize = 0;
- SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, 0, 0, &dwRequiredSize, 0);
- SP_INTERFACE_DEVICE_DETAIL_DATA *pData = (SP_INTERFACE_DEVICE_DETAIL_DATA *)new unsigned char[dwRequiredSize];
- pData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
- if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, pData, dwRequiredSize, 0, 0)) {
- delete pData;
- continue;
- }
- HANDLE hDevice = CreateFile(pData->DevicePath
- , GENERIC_READ | GENERIC_WRITE
- , FILE_SHARE_READ | FILE_SHARE_WRITE
- , 0
- , OPEN_EXISTING
- , FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED
- , 0);
- if (hDevice == INVALID_HANDLE_VALUE) {
- delete pData;
- continue;
- }
- BOOL bResult;
- WINUSB_INTERFACE_HANDLE hWinUSBHandle;
- bResult = WinUsb_Initialize(hDevice, &hWinUSBHandle);
- if (!bResult)
- continue;
- SetupDiDestroyDeviceInfoList(hDevInfo);
- printf("using device (%s)\n",deviceID);
- return hWinUSBHandle;
- }
- return NULL;
- }
- void command(WINUSB_INTERFACE_HANDLE handle, unsigned char* request) {
- request[88] = 0; // crc
- for(unsigned char i = 2; i < 88; i++)
- request[88] ^= request[i]; // crc
- WINUSB_SETUP_PACKET packet;
- packet.RequestType = 0x21;
- packet.Request = 0x09;
- packet.Value = 0x300;
- packet.Index = 0;
- packet.Length = REPORT_SIZE;
- ULONG cbSent = 0;
- BOOL bResult = WinUsb_ControlTransfer(handle, packet, request, REPORT_SIZE, &cbSent, 0);
- if (!bResult)
- printf("\tfailed\tWinUsb_ControlTransfer\n\n");
- Sleep(DELAY);
- if(cbSent == REPORT_SIZE) {
- unsigned char* response = (unsigned char*)malloc(REPORT_SIZE);
- WINUSB_SETUP_PACKET packet;
- packet.RequestType = 0xA1;
- packet.Request = 0x01;
- packet.Value = 0x300;
- packet.Index = 0;
- packet.Length = REPORT_SIZE;
- ULONG cbSent = 0;
- BOOL bResult = WinUsb_ControlTransfer(handle, packet, response, REPORT_SIZE, &cbSent, 0);
- if (!bResult)
- printf("\tfailed\tWinUsb_ControlTransfer\n\n");
- Sleep(DELAY);
- if(cbSent == REPORT_SIZE) {
- if(response[2] != request[2] || response[5] != request[5] || response[6] != request[6])
- printf("Response doesnt match request\n");
- else
- switch (response[0]) {
- case 0x01:
- printf("Device is busy\n");
- break;
- case 0x02: // success
- break;
- case 0x03:
- printf("Command failed\n");
- break;
- case 0x04:
- printf("Command timed out\n");
- break;
- case 0x05:
- printf("Command not supported\n");
- break;
- default:
- printf("Unknown error response %02x\n", response[0]);
- break;
- }
- } else
- printf("Response failed - usb_control_msg. length: %d\n", cbSent);
- free(response);
- } else
- printf("Request failed - usb_control_msg. length: %d\n", cbSent);
- }
- void command_frame(WINUSB_INTERFACE_HANDLE handle, unsigned char offset) {
- unsigned char* request = (unsigned char*)malloc(REPORT_SIZE);
- for (unsigned char row = 0; row < 6; row++) {
- memset(request, 0, REPORT_SIZE);
- memcpy(&request[0],"\x00\xFF\x00\x00\x00\x46\x03\x0B\xFF\x00\x01\x18",12);
- request[9] = row;
- for (int i = 0; i < 4; i++)
- memcpy(&request[18*i+12], COLORARR[offset % _countof(COLORARR)], 18);
- command(handle, request);
- }
- free(request);
- }
- void command_custom(WINUSB_INTERFACE_HANDLE handle) {
- unsigned char* request = (unsigned char*)malloc(REPORT_SIZE);
- memset(request, 0, REPORT_SIZE);
- //st//td//rmpkts//pt//ds//cc//ci//ars//crc
- memcpy(&request[0],"\x00\xFF\x00\x00\x00\x02\x03\x0A\x05\x01",9);
- command(handle, request);
- free(request);
- }
- void test_custom(unsigned int uiVID, unsigned int uiPID) {
- WINUSB_INTERFACE_HANDLE handle = connect_winusb(uiVID, uiPID);
- if (!handle) {
- printf("No Handle\n");
- return;
- }
- for (int i =0;i < 5;i++){
- for (unsigned char offset = 0; offset < 8; offset++) {
- command_frame(handle,offset);
- command_custom(handle);
- }
- Sleep(DELAY2);
- for (unsigned char offset = 0; offset < 8; offset++) {
- command_frame(handle,offset);
- command_custom(handle);
- }
- Sleep(DELAY2);
- }
- }
- int main(int argc, char **argv) {
- printf("Press enter to start...");
- getc(stdin);
- printf("\n");
- printf("All Razer devices found:\n");
- listAllDevices(RAZER_VID);
- printf("Press enter to test...");
- getc(stdin);
- printf("\n");
- test_custom(RAZER_VID, RBP2016_PID);
- printf("Press enter to exit...");
- getc(stdin);
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement