1
0
mirror of https://github.com/MatMoul/g810-led.git synced 2024-12-23 01:06:11 +00:00

Merge pull request #245 from pearsonk/check_perms

Add error context to failures to open devices
This commit is contained in:
MatMoul 2020-11-06 19:16:09 +01:00 committed by GitHub
commit 0f44df71ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -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;
} }

View File

@ -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;
} }