add better err msg
This commit is contained in:
parent
83bfcdbd54
commit
f3615c5adf
3 changed files with 22 additions and 12 deletions
|
@ -17,11 +17,12 @@
|
||||||
*************************************************************************** */
|
*************************************************************************** */
|
||||||
|
|
||||||
int get_battery_level(const char *device_path, bool *is_charging) {
|
int get_battery_level(const char *device_path, bool *is_charging) {
|
||||||
HANDLE hDevice = CreateFile(device_path, GENERIC_READ | GENERIC_WRITE,
|
HANDLE hDevice;
|
||||||
|
if ((hDevice = CreateFile(device_path, GENERIC_READ | GENERIC_WRITE,
|
||||||
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
|
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
|
||||||
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL)) ==
|
||||||
if (hDevice == INVALID_HANDLE_VALUE) {
|
INVALID_HANDLE_VALUE) {
|
||||||
fprintf_s(stderr, "Failed to open device.\n");
|
fprintf_s(stderr, "[%lu] Failed to open device.\n", GetLastError());
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -33,7 +34,8 @@ int get_battery_level(const char *device_path, bool *is_charging) {
|
||||||
|
|
||||||
// Send the report
|
// Send the report
|
||||||
if (!HidD_SetFeature(hDevice, report, RAZER_REPORT_LEN)) {
|
if (!HidD_SetFeature(hDevice, report, RAZER_REPORT_LEN)) {
|
||||||
fprintf_s(stderr, "Failed to send report to device.\n");
|
fprintf_s(stderr, "[%lu] Failed to send report to device.\n",
|
||||||
|
GetLastError());
|
||||||
|
|
||||||
CloseHandle(hDevice);
|
CloseHandle(hDevice);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -41,7 +43,8 @@ int get_battery_level(const char *device_path, bool *is_charging) {
|
||||||
|
|
||||||
// Read the response
|
// Read the response
|
||||||
if (!HidD_GetFeature(hDevice, report, RAZER_REPORT_LEN)) {
|
if (!HidD_GetFeature(hDevice, report, RAZER_REPORT_LEN)) {
|
||||||
fprintf_s(stderr, "Failed to read report from device.\n");
|
fprintf_s(stderr, "[%lu] Failed to read report from device.\n",
|
||||||
|
GetLastError());
|
||||||
|
|
||||||
CloseHandle(hDevice);
|
CloseHandle(hDevice);
|
||||||
return -1;
|
return -1;
|
||||||
|
|
15
src/helper.c
15
src/helper.c
|
@ -6,7 +6,7 @@
|
||||||
#include <setupapi.h>
|
#include <setupapi.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define VID 0x1532
|
#define VID_RAZER 0x1532
|
||||||
|
|
||||||
void find_device_path(int pid, char *device_path) {
|
void find_device_path(int pid, char *device_path) {
|
||||||
GUID hidGuid;
|
GUID hidGuid;
|
||||||
|
@ -15,7 +15,9 @@ void find_device_path(int pid, char *device_path) {
|
||||||
HDEVINFO hDevInfo = SetupDiGetClassDevs(
|
HDEVINFO hDevInfo = SetupDiGetClassDevs(
|
||||||
&hidGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
&hidGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
||||||
if (hDevInfo == INVALID_HANDLE_VALUE) {
|
if (hDevInfo == INVALID_HANDLE_VALUE) {
|
||||||
fprintf_s(stderr, "Failed to get device information.\n");
|
fprintf_s(stderr, "[%lu] Failed to get any device information\n",
|
||||||
|
GetLastError());
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +26,7 @@ void find_device_path(int pid, char *device_path) {
|
||||||
|
|
||||||
for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &hidGuid, i,
|
for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &hidGuid, i,
|
||||||
&deviceInterfaceData);
|
&deviceInterfaceData);
|
||||||
i++) {
|
++i) {
|
||||||
DWORD requiredSize;
|
DWORD requiredSize;
|
||||||
SetupDiGetDeviceInterfaceDetail(hDevInfo, &deviceInterfaceData, NULL, 0,
|
SetupDiGetDeviceInterfaceDetail(hDevInfo, &deviceInterfaceData, NULL, 0,
|
||||||
&requiredSize, NULL);
|
&requiredSize, NULL);
|
||||||
|
@ -35,6 +37,7 @@ void find_device_path(int pid, char *device_path) {
|
||||||
fprintf_s(
|
fprintf_s(
|
||||||
stderr,
|
stderr,
|
||||||
"Failed to allocated memory for PSP_DEVICE_INTERFACE_DETAIL_DATA.\n");
|
"Failed to allocated memory for PSP_DEVICE_INTERFACE_DETAIL_DATA.\n");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
|
detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
|
||||||
|
@ -44,21 +47,25 @@ void find_device_path(int pid, char *device_path) {
|
||||||
HANDLE hDevice = CreateFile(
|
HANDLE hDevice = CreateFile(
|
||||||
detailData->DevicePath, GENERIC_READ | GENERIC_WRITE,
|
detailData->DevicePath, GENERIC_READ | GENERIC_WRITE,
|
||||||
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
|
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
|
||||||
|
|
||||||
if (hDevice != INVALID_HANDLE_VALUE) {
|
if (hDevice != INVALID_HANDLE_VALUE) {
|
||||||
// Get device attributes
|
// Get device attributes
|
||||||
HIDD_ATTRIBUTES attributes;
|
HIDD_ATTRIBUTES attributes;
|
||||||
attributes.Size = sizeof(HIDD_ATTRIBUTES);
|
attributes.Size = sizeof(HIDD_ATTRIBUTES);
|
||||||
if (HidD_GetAttributes(hDevice, &attributes)) {
|
if (HidD_GetAttributes(hDevice, &attributes)) {
|
||||||
// Check VID and PID
|
// Check VID and PID
|
||||||
if (attributes.VendorID == VID && attributes.ProductID == pid) {
|
if (attributes.VendorID == VID_RAZER && attributes.ProductID == pid) {
|
||||||
strcpy(device_path, detailData->DevicePath);
|
strcpy(device_path, detailData->DevicePath);
|
||||||
|
|
||||||
CloseHandle(hDevice);
|
CloseHandle(hDevice);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CloseHandle(hDevice);
|
CloseHandle(hDevice);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(detailData);
|
free(detailData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
char device_path[2048];
|
char device_path[2048];
|
||||||
int min_len = 10;
|
unsigned long long min_len = 10;
|
||||||
|
|
||||||
// Try wired
|
// Try wired
|
||||||
find_device_path(USB_DEVICE_ID_RAZER_VIPER_ULTIMATE_WIRED, device_path);
|
find_device_path(USB_DEVICE_ID_RAZER_VIPER_ULTIMATE_WIRED, device_path);
|
||||||
|
|
Loading…
Reference in a new issue