Advertisement
Guest User

custom rbp2016 test

a guest
Mar 1st, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. #include <set>
  2. #include <wtypes.h>
  3. #include <Winusb.h>
  4. #include <SetupAPI.h>
  5. #include <cfgmgr32.h>
  6. #include <sstream>
  7.  
  8. #pragma comment(lib, "Winusb.lib")
  9. #pragma comment(lib, "setupapi.lib")
  10.  
  11. #define DELAY 10
  12. #define DELAY2 100
  13. #define REPORT_SIZE 0x5A
  14. #define RAZER_VID 0x1532
  15. #define RBP2016_PID 0x0210
  16.  
  17. 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)
  18.  
  19. static const char* COLORARR[6] = {
  20. "\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF\x00\xFF\xFF\xFF\x00\xFF\xFF\xFF\x00"
  21. ,"\xFF\xFF\x00\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF\x00\xFF\xFF\xFF\x00\xFF"
  22. ,"\xFF\x00\xFF\xFF\xFF\x00\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF\x00\xFF\xFF"
  23. ,"\x00\xFF\xFF\xFF\x00\xFF\xFF\xFF\x00\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF"
  24. ,"\x00\x00\xFF\x00\xFF\xFF\xFF\x00\xFF\xFF\xFF\x00\xFF\x00\x00\x00\xFF\x00"
  25. ,"\x00\xFF\x00\x00\x00\xFF\x00\xFF\xFF\xFF\x00\xFF\xFF\xFF\x00\xFF\x00\x00"
  26. };
  27.  
  28. void listAllDevices(unsigned int uiVID) {
  29. HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, TEXT("USB"), NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);
  30. if (hDevInfo == INVALID_HANDLE_VALUE)
  31. printf("SetupDiGetClassDevs failed\n");
  32. SP_DEVINFO_DATA deviceData = { 0 };
  33. deviceData.cbSize = sizeof(SP_DEVINFO_DATA);
  34. for (unsigned int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &deviceData); ++i) {
  35. unsigned long size;
  36. CM_Get_Device_ID_Size(&size, deviceData.DevInst, 0);
  37. char deviceID[MAX_DEVICE_ID_LEN];
  38. CM_Get_Device_ID(deviceData.DevInst, deviceID, MAX_PATH, 0);
  39. if (CM_Get_Device_ID(deviceData.DevInst, deviceID, MAX_DEVICE_ID_LEN, 0))
  40. continue;
  41. char* vid = strstr(deviceID, "VID_");
  42. if (!vid || uiVID != strtoul(vid+4, NULL, 16))
  43. continue;
  44. printf("found device (%s)\n",deviceID);
  45. }
  46. }
  47.  
  48.  
  49. WINUSB_INTERFACE_HANDLE connect_winusb(unsigned int uiVID, unsigned int uiPID) {
  50. HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE, 0, 0, DIGCF_DEVICEINTERFACE);
  51. if (hDevInfo == INVALID_HANDLE_VALUE) {
  52. printf("SetupDiGetClassDevs failed\n");
  53. return NULL;
  54. }
  55. SP_DEVINFO_DATA deviceData = { 0 };
  56. deviceData.cbSize = sizeof(SP_DEVINFO_DATA);
  57. for (unsigned int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &deviceData); ++i) {
  58. char deviceID[MAX_DEVICE_ID_LEN];
  59. if (CM_Get_Device_ID(deviceData.DevInst, deviceID, MAX_DEVICE_ID_LEN, 0))
  60. continue;
  61.  
  62. char* vid = strstr(deviceID, "VID_");
  63. if (!vid || uiVID != strtoul(vid+4, NULL, 16))
  64. continue;
  65.  
  66. char* pid = strstr(deviceID, "PID_");
  67. if (!pid || uiPID != strtoul(pid+4, NULL, 16))
  68. continue;
  69.  
  70. /*char* mi = strstr(deviceID, "MI_");
  71. if (!mi || uiMI != strtoul(mi+3, NULL, 16))
  72. continue;*/
  73.  
  74. SP_INTERFACE_DEVICE_DATA interfaceData = { 0 };
  75. interfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
  76. if (!SetupDiEnumDeviceInterfaces(hDevInfo, &deviceData, &GUID_DEVINTERFACE, 0, &interfaceData))
  77. continue;
  78.  
  79. DWORD dwRequiredSize = 0;
  80. SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, 0, 0, &dwRequiredSize, 0);
  81. SP_INTERFACE_DEVICE_DETAIL_DATA *pData = (SP_INTERFACE_DEVICE_DETAIL_DATA *)new unsigned char[dwRequiredSize];
  82. pData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
  83. if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, pData, dwRequiredSize, 0, 0)) {
  84. delete pData;
  85. continue;
  86. }
  87.  
  88. HANDLE hDevice = CreateFile(pData->DevicePath
  89. , GENERIC_READ | GENERIC_WRITE
  90. , FILE_SHARE_READ | FILE_SHARE_WRITE
  91. , 0
  92. , OPEN_EXISTING
  93. , FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED
  94. , 0);
  95. if (hDevice == INVALID_HANDLE_VALUE) {
  96. delete pData;
  97. continue;
  98. }
  99.  
  100. BOOL bResult;
  101. WINUSB_INTERFACE_HANDLE hWinUSBHandle;
  102. bResult = WinUsb_Initialize(hDevice, &hWinUSBHandle);
  103. if (!bResult)
  104. continue;
  105.  
  106. SetupDiDestroyDeviceInfoList(hDevInfo);
  107. printf("using device (%s)\n",deviceID);
  108. return hWinUSBHandle;
  109. }
  110. return NULL;
  111. }
  112.  
  113.  
  114. void command(WINUSB_INTERFACE_HANDLE handle, unsigned char* request) {
  115. request[88] = 0; // crc
  116. for(unsigned char i = 2; i < 88; i++)
  117. request[88] ^= request[i]; // crc
  118. WINUSB_SETUP_PACKET packet;
  119. packet.RequestType = 0x21;
  120. packet.Request = 0x09;
  121. packet.Value = 0x300;
  122. packet.Index = 0;
  123. packet.Length = REPORT_SIZE;
  124. ULONG cbSent = 0;
  125. BOOL bResult = WinUsb_ControlTransfer(handle, packet, request, REPORT_SIZE, &cbSent, 0);
  126. if (!bResult)
  127. printf("\tfailed\tWinUsb_ControlTransfer\n\n");
  128. Sleep(DELAY);
  129. if(cbSent == REPORT_SIZE) {
  130. unsigned char* response = (unsigned char*)malloc(REPORT_SIZE);
  131. WINUSB_SETUP_PACKET packet;
  132. packet.RequestType = 0xA1;
  133. packet.Request = 0x01;
  134. packet.Value = 0x300;
  135. packet.Index = 0;
  136. packet.Length = REPORT_SIZE;
  137. ULONG cbSent = 0;
  138. BOOL bResult = WinUsb_ControlTransfer(handle, packet, response, REPORT_SIZE, &cbSent, 0);
  139. if (!bResult)
  140. printf("\tfailed\tWinUsb_ControlTransfer\n\n");
  141. Sleep(DELAY);
  142. if(cbSent == REPORT_SIZE) {
  143. if(response[2] != request[2] || response[5] != request[5] || response[6] != request[6])
  144. printf("Response doesnt match request\n");
  145. else
  146. switch (response[0]) {
  147. case 0x01:
  148. printf("Device is busy\n");
  149. break;
  150. case 0x02: // success
  151. break;
  152. case 0x03:
  153. printf("Command failed\n");
  154. break;
  155. case 0x04:
  156. printf("Command timed out\n");
  157. break;
  158. case 0x05:
  159. printf("Command not supported\n");
  160. break;
  161. default:
  162. printf("Unknown error response %02x\n", response[0]);
  163. break;
  164. }
  165. } else
  166. printf("Response failed - usb_control_msg. length: %d\n", cbSent);
  167. free(response);
  168. } else
  169. printf("Request failed - usb_control_msg. length: %d\n", cbSent);
  170. }
  171.  
  172. void command_frame(WINUSB_INTERFACE_HANDLE handle, unsigned char offset) {
  173. unsigned char* request = (unsigned char*)malloc(REPORT_SIZE);
  174. for (unsigned char row = 0; row < 6; row++) {
  175. memset(request, 0, REPORT_SIZE);
  176. memcpy(&request[0],"\x00\xFF\x00\x00\x00\x46\x03\x0B\xFF\x00\x01\x18",12);
  177. request[9] = row;
  178. for (int i = 0; i < 4; i++)
  179. memcpy(&request[18*i+12], COLORARR[offset % _countof(COLORARR)], 18);
  180. command(handle, request);
  181. }
  182. free(request);
  183. }
  184.  
  185. void command_custom(WINUSB_INTERFACE_HANDLE handle) {
  186. unsigned char* request = (unsigned char*)malloc(REPORT_SIZE);
  187. memset(request, 0, REPORT_SIZE);
  188. //st//td//rmpkts//pt//ds//cc//ci//ars//crc
  189. memcpy(&request[0],"\x00\xFF\x00\x00\x00\x02\x03\x0A\x05\x01",9);
  190. command(handle, request);
  191. free(request);
  192. }
  193.  
  194. void test_custom(unsigned int uiVID, unsigned int uiPID) {
  195. WINUSB_INTERFACE_HANDLE handle = connect_winusb(uiVID, uiPID);
  196. if (!handle) {
  197. printf("No Handle\n");
  198. return;
  199. }
  200. for (int i =0;i < 5;i++){
  201. for (unsigned char offset = 0; offset < 8; offset++) {
  202. command_frame(handle,offset);
  203. command_custom(handle);
  204. }
  205. Sleep(DELAY2);
  206. for (unsigned char offset = 0; offset < 8; offset++) {
  207. command_frame(handle,offset);
  208. command_custom(handle);
  209. }
  210. Sleep(DELAY2);
  211. }
  212. }
  213.  
  214. int main(int argc, char **argv) {
  215. printf("Press enter to start...");
  216. getc(stdin);
  217. printf("\n");
  218. printf("All Razer devices found:\n");
  219. listAllDevices(RAZER_VID);
  220.  
  221. printf("Press enter to test...");
  222. getc(stdin);
  223. printf("\n");
  224. test_custom(RAZER_VID, RBP2016_PID);
  225.  
  226. printf("Press enter to exit...");
  227. getc(stdin);
  228. printf("\n");
  229.  
  230. return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement