From f3615c5adffc281e2890ed832872d207d524c4d1 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 3 Apr 2024 15:27:38 +0200 Subject: [PATCH] add better err msg --- src/battery.c | 17 ++++++++++------- src/helper.c | 15 +++++++++++---- src/main.c | 2 +- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/battery.c b/src/battery.c index 1baa669..1ab51fe 100644 --- a/src/battery.c +++ b/src/battery.c @@ -17,11 +17,12 @@ *************************************************************************** */ int get_battery_level(const char *device_path, bool *is_charging) { - HANDLE hDevice = CreateFile(device_path, GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, - OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); - if (hDevice == INVALID_HANDLE_VALUE) { - fprintf_s(stderr, "Failed to open device.\n"); + HANDLE hDevice; + if ((hDevice = CreateFile(device_path, GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, + OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL)) == + INVALID_HANDLE_VALUE) { + fprintf_s(stderr, "[%lu] Failed to open device.\n", GetLastError()); return -1; } @@ -33,7 +34,8 @@ int get_battery_level(const char *device_path, bool *is_charging) { // Send the report 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); return -1; @@ -41,7 +43,8 @@ int get_battery_level(const char *device_path, bool *is_charging) { // Read the response 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); return -1; diff --git a/src/helper.c b/src/helper.c index 045d272..0b0003b 100644 --- a/src/helper.c +++ b/src/helper.c @@ -6,7 +6,7 @@ #include #include -#define VID 0x1532 +#define VID_RAZER 0x1532 void find_device_path(int pid, char *device_path) { GUID hidGuid; @@ -15,7 +15,9 @@ void find_device_path(int pid, char *device_path) { HDEVINFO hDevInfo = SetupDiGetClassDevs( &hidGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 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; } @@ -24,7 +26,7 @@ void find_device_path(int pid, char *device_path) { for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &hidGuid, i, &deviceInterfaceData); - i++) { + ++i) { DWORD requiredSize; SetupDiGetDeviceInterfaceDetail(hDevInfo, &deviceInterfaceData, NULL, 0, &requiredSize, NULL); @@ -35,6 +37,7 @@ void find_device_path(int pid, char *device_path) { fprintf_s( stderr, "Failed to allocated memory for PSP_DEVICE_INTERFACE_DETAIL_DATA.\n"); + return; } detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); @@ -44,21 +47,25 @@ void find_device_path(int pid, char *device_path) { HANDLE hDevice = CreateFile( detailData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + if (hDevice != INVALID_HANDLE_VALUE) { // Get device attributes HIDD_ATTRIBUTES attributes; attributes.Size = sizeof(HIDD_ATTRIBUTES); if (HidD_GetAttributes(hDevice, &attributes)) { // Check VID and PID - if (attributes.VendorID == VID && attributes.ProductID == pid) { + if (attributes.VendorID == VID_RAZER && attributes.ProductID == pid) { strcpy(device_path, detailData->DevicePath); + CloseHandle(hDevice); break; } } + CloseHandle(hDevice); } } + free(detailData); } diff --git a/src/main.c b/src/main.c index 0eeed27..8f846fe 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ int main(void) { char device_path[2048]; - int min_len = 10; + unsigned long long min_len = 10; // Try wired find_device_path(USB_DEVICE_ID_RAZER_VIPER_ULTIMATE_WIRED, device_path);