mirror of
https://github.com/MatMoul/g810-led.git
synced 2024-12-23 01:06:11 +00:00
Add error context to failures to open devices
This should address common misconceptions of "No matching or compatible device found" by isolating permission problems from lack of a matching device in the system.
This commit is contained in:
parent
5ee810a520
commit
0998cc370d
@ -20,6 +20,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <cerrno>
|
||||||
|
|
||||||
#if defined(hidapi)
|
#if defined(hidapi)
|
||||||
#include <locale>
|
#include <locale>
|
||||||
@ -200,6 +201,8 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) {
|
|||||||
|
|
||||||
if (! dev) {
|
if (! dev) {
|
||||||
currentDevice.model = KeyboardModel::unknown;
|
currentDevice.model = KeyboardModel::unknown;
|
||||||
|
errno = ENODEV;
|
||||||
|
|
||||||
hid_exit();
|
hid_exit();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -209,6 +212,7 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) {
|
|||||||
|
|
||||||
if(m_hidHandle == 0) {
|
if(m_hidHandle == 0) {
|
||||||
hid_exit();
|
hid_exit();
|
||||||
|
errno = EACCES;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,6 +298,7 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) {
|
|||||||
|
|
||||||
if (currentDevice.model == KeyboardModel::unknown) {
|
if (currentDevice.model == KeyboardModel::unknown) {
|
||||||
libusb_exit(m_ctx);
|
libusb_exit(m_ctx);
|
||||||
|
errno = ENODEV;
|
||||||
m_ctx = NULL;
|
m_ctx = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -303,6 +308,7 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) {
|
|||||||
|
|
||||||
if(m_hidHandle == NULL) {
|
if(m_hidHandle == NULL) {
|
||||||
libusb_exit(m_ctx);
|
libusb_exit(m_ctx);
|
||||||
|
errno = EACCES;
|
||||||
m_ctx = NULL;
|
m_ctx = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -310,6 +316,7 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) {
|
|||||||
if(libusb_kernel_driver_active(m_hidHandle, 1) == 1) {
|
if(libusb_kernel_driver_active(m_hidHandle, 1) == 1) {
|
||||||
if(libusb_detach_kernel_driver(m_hidHandle, 1) != 0) {
|
if(libusb_detach_kernel_driver(m_hidHandle, 1) != 0) {
|
||||||
libusb_exit(m_ctx);
|
libusb_exit(m_ctx);
|
||||||
|
errno = EACCES;
|
||||||
m_ctx = NULL;
|
m_ctx = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -322,6 +329,7 @@ bool LedKeyboard::open(uint16_t vendorID, uint16_t productID, string serial) {
|
|||||||
m_isKernellDetached = false;
|
m_isKernellDetached = false;
|
||||||
}
|
}
|
||||||
libusb_exit(m_ctx);
|
libusb_exit(m_ctx);
|
||||||
|
errno = EACCES;
|
||||||
m_ctx = NULL;
|
m_ctx = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
12
src/main.cpp
12
src/main.cpp
@ -323,7 +323,17 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
//Initialize the device for use
|
//Initialize the device for use
|
||||||
if (!kbd.open(vendorID, productID, serial)) {
|
if (!kbd.open(vendorID, productID, serial)) {
|
||||||
std::cout << "Matching or compatible device not found !" << std::endl;
|
switch (errno)
|
||||||
|
{
|
||||||
|
case ENODEV:
|
||||||
|
std::cout << "Matching or compatible device not found" << std::endl;
|
||||||
|
break;
|
||||||
|
case EACCES:
|
||||||
|
std::cout << "Access denied: Check device access permissions or run as a privileged user (root/sudo)" << std::endl;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::cout << "Unknown error: errno=" << errno << std::endl;
|
||||||
|
}
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user