From e8c678a18166b6d6c8f1381c29d421cb6e4bdb41 Mon Sep 17 00:00:00 2001 From: MatMoul Date: Sun, 22 Jan 2017 00:47:38 +0100 Subject: [PATCH] Improve code --- makefile | 8 ++- src/classes/Keyboard.cpp | 106 +++++++++++++++++---------------------- 2 files changed, 52 insertions(+), 62 deletions(-) diff --git a/makefile b/makefile index 46fd1cc..5140ca1 100644 --- a/makefile +++ b/makefile @@ -42,8 +42,12 @@ install: $(DESTDIR)/etc/$(PROGN)/samples \ $(DESTDIR)$(SYSTEMDDIR)/system && \ cp sample_profiles/* $(DESTDIR)/etc/$(PROGN)/samples && \ - test -s $(DESTDIR)/etc/$(PROGN)/profile || cp $(DESTDIR)/etc/$(PROGN)/samples/group_keys $(DESTDIR)/etc/$(PROGN)/profile && \ - test -s $(DESTDIR)/etc/$(PROGN)/reboot || cp $(DESTDIR)/etc/$(PROGN)/samples/all_off $(DESTDIR)/etc/$(PROGN)/reboot && \ + test -s $(DESTDIR)/etc/$(PROGN)/profile || \ + cp $(DESTDIR)/etc/$(PROGN)/samples/group_keys $(DESTDIR)/etc/$(PROGN)/profile + @test -s /usr/bin/systemd-run && \ + test -s $(DESTDIR)/etc/$(PROGN)/reboot || \ + cp $(DESTDIR)/etc/$(PROGN)/samples/all_off $(DESTDIR)/etc/$(PROGN)/reboot + @test -s /usr/bin/systemd-run && \ cp systemd/$(PROGN).service $(DESTDIR)$(SYSTEMDDIR)/system && \ cp systemd/$(PROGN)-reboot.service $(DESTDIR)$(SYSTEMDDIR)/system && \ systemctl daemon-reload && \ diff --git a/src/classes/Keyboard.cpp b/src/classes/Keyboard.cpp index 808ddd5..5ad9740 100644 --- a/src/classes/Keyboard.cpp +++ b/src/classes/Keyboard.cpp @@ -47,48 +47,35 @@ bool LedKeyboard::listKeyboards() { hid_free_enumeration(devs); hid_exit(); + return true; + #elif defined(libusb) libusb_context *ctx = NULL; if(libusb_init(&m_ctx) < 0) return false; libusb_device **devs; ssize_t cnt = libusb_get_device_list(ctx, &devs); - if(cnt >= 0) { - for(ssize_t i = 0; i < cnt; i++) { - libusb_device *device = devs[i]; - libusb_device_descriptor desc = { - 0, // bLength - 0, // bDescriptorType - 0, // bcdUSB - 0, // bDeviceClass - 0, // bDeviceSubClass - 0, // bDeviceProtocol - 0, // bMaxPacketSize0 - 0, // idVendor - 0, // idProduct - 0, // bcdDevice - 0, // iManufacturer - 0, // iProduct - 0, // iSerialNumber - 0 // bNumConfigurations - }; - libusb_get_device_descriptor(device, &desc); - for (int i=0; i<(int)SuportedKeyboards.size(); i++) { - if (desc.idVendor == SuportedKeyboards[i][0]) { - if (desc.idProduct == SuportedKeyboards[i][1]) { - cout<<"0x"<= 0) { for(ssize_t i = 0; i < cnt; i++) { libusb_device *device = devs[i]; - libusb_device_descriptor desc = { - 0, // bLength - 0, // bDescriptorType - 0, // bcdUSB - 0, // bDeviceClass - 0, // bDeviceSubClass - 0, // bDeviceProtocol - 0, // bMaxPacketSize0 - 0, // idVendor - 0, // idProduct - 0, // bcdDevice - 0, // iManufacturer - 0, // iProduct - 0, // iSerialNumber - 0 // bNumConfigurations - }; + libusb_device_descriptor desc; libusb_get_device_descriptor(device, &desc); for (int i=0; i<(int)SuportedKeyboards.size(); i++) { if (desc.idVendor == SuportedKeyboards[i][0]) { @@ -173,9 +149,7 @@ bool LedKeyboard::open() { } } } - if (m_keyboardModel != KeyboardModel::unknown) break; - } libusb_free_device_list(devs, 1); } @@ -189,14 +163,12 @@ bool LedKeyboard::open() { } m_hidHandle = libusb_open_device_with_vid_pid(m_ctx, m_vendorID, m_productID); - if(m_hidHandle == 0) { libusb_exit(m_ctx); m_ctx = NULL; return false; } - if(libusb_kernel_driver_active(m_hidHandle, 1) == 1) { if(libusb_detach_kernel_driver(m_hidHandle, 1) != 0) { libusb_exit(m_ctx); @@ -205,6 +177,7 @@ bool LedKeyboard::open() { } m_isKernellDetached = true; } + if(libusb_claim_interface(m_hidHandle, 1) < 0) { if(m_isKernellDetached==true) { libusb_attach_kernel_driver(m_hidHandle, 1); @@ -214,10 +187,13 @@ bool LedKeyboard::open() { m_ctx = NULL; return false; } + + m_isOpen = true; + return true; + #endif - m_isOpen = true; - return true; + return false; } bool LedKeyboard::close() { @@ -228,6 +204,8 @@ bool LedKeyboard::close() { hid_close(m_hidHandle); m_hidHandle = 0; hid_exit(); + return true; + #elif defined(libusb) if(libusb_release_interface(m_hidHandle, 1) != 0) return false; if(m_isKernellDetached==true) { @@ -238,9 +216,10 @@ bool LedKeyboard::close() { m_hidHandle = NULL; libusb_exit(m_ctx); m_ctx = NULL; + return true; #endif - return true; + return false; } @@ -531,19 +510,26 @@ bool LedKeyboard::sendDataInternal(byte_buffer_t &data) { byte_buffer_t data2; data2.resize(21, 0x00); hid_read_timeout(m_hidHandle, const_cast(data2.data()), data2.size(), 0); + return true; #elif defined(libusb) - int r; - if (data.size() > 20) r = libusb_control_transfer(m_hidHandle, 0x21, 0x09, 0x0212, 1, const_cast(data.data()), data.size(), 2000); - else r = libusb_control_transfer(m_hidHandle, 0x21, 0x09, 0x0211, 1, const_cast(data.data()), data.size(), 2000); + if (data.size() > 20) { + if(libusb_control_transfer(m_hidHandle, 0x21, 0x09, 0x0212, 1, + const_cast(data.data()), data.size(), 2000) < 0) + return false; + } else { + if(libusb_control_transfer(m_hidHandle, 0x21, 0x09, 0x0211, 1, + const_cast(data.data()), data.size(), 2000) < 0) + return false; + } usleep(1000); - if (r < 0) return false; unsigned char buffer[64]; int len = 0; - r = libusb_interrupt_transfer(m_hidHandle, 0x82, buffer, sizeof(buffer), &len, 1); + libusb_interrupt_transfer(m_hidHandle, 0x82, buffer, sizeof(buffer), &len, 1); + return true; #endif } - return true; + return false; } LedKeyboard::byte_buffer_t LedKeyboard::getKeyGroupAddress(LedKeyboard::KeyAddressGroup keyAddressGroup) {