From 54897d9a5a67b9de25a150b7558fc7726bd4fe9a Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 12 Dec 2016 14:06:45 -0600 Subject: [PATCH] Multiple changes of datatypes being past overflowing constants (most char needed to be changed to unsigned char or uint8_t). Also changed the include style of the to be controlled through the makefile setting --- makefile | 7 +++-- src/classes/Keyboard.cpp | 59 ++++++++++++++++++++++++---------------- src/classes/Keyboard.h | 12 ++++---- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/makefile b/makefile index 6a24c9b..24826e8 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,6 @@ CC=g++ CFLAGS=-Wall -O2 -std=gnu++11 +LIBUSB_INC?=-I/usr/include/libusb-1.0 LDFLAGS=-lusb-1.0 PROGN=g810-led @@ -7,11 +8,11 @@ PROGN=g810-led all: bin/$(PROGN) -bin/$(PROGN): src/main.cpp src/classes/*.cpp +bin/$(PROGN): src/main.cpp src/classes/*.cpp src/classes/*.h @mkdir -p bin - $(CC) -o $@ $(CFLAGS) $^ $(LDFLAGS) + $(CC) $(CFLAGS) $(LIBUSB_INC) -o $@ $^ $(LDFLAGS) -debug: CFLAGS += -g +debug: CFLAGS += -g -Wextra -pedantic debug: bin/$(PROGN) clean: diff --git a/src/classes/Keyboard.cpp b/src/classes/Keyboard.cpp index 5544788..f2400a5 100644 --- a/src/classes/Keyboard.cpp +++ b/src/classes/Keyboard.cpp @@ -21,7 +21,20 @@ bool Keyboard::attach() { int pid = 0; for(ssize_t i = 0; i < cnt; i++) { libusb_device *device = devs[i]; - libusb_device_descriptor desc = {0}; + 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); if (desc.idVendor == 0x046d) { if (desc.idProduct == 0xc331) { pid = desc.idProduct; break; } // G810 spectrum @@ -476,7 +489,7 @@ bool Keyboard::parseColor(std::string color, KeyColors &colors) { return true; } -bool Keyboard::sendDataInternal(unsigned char *data, int data_size) { +bool Keyboard::sendDataInternal(unsigned char *data, uint16_t data_size) { if (m_isAttached == false) return false; int r; if (data_size > 20) r = libusb_control_transfer(dev_handle, 0x21, 0x09, 0x0212, 1, data, data_size, 2000); @@ -589,17 +602,15 @@ bool Keyboard::populateAddressGroupInternal(KeyAddressGroup addressGroup, unsign return true; } -bool Keyboard::setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[], int keyValueCount) { +bool Keyboard::setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[], size_t keyValueCount) { bool retval = false; - unsigned char *data; int data_size; - int maxKeyValueCount = 0; if (addressGroup == KeyAddressGroup::logo) data_size = 20; else data_size = 64; - data = new unsigned char[data_size]; + unsigned char *data = new unsigned char[data_size]; + const size_t maxKeyValueCount = (data_size - 8) / 4; populateAddressGroupInternal(addressGroup, data); - maxKeyValueCount = (data_size - 8) / 4; - for(int i = 0; i < maxKeyValueCount; i++) { + for(size_t i = 0; i < maxKeyValueCount; i++) { if (i < keyValueCount) { data[8 + i * 4 + 0] = keyValues[i].key.id; data[8 + i * 4 + 1] = keyValues[i].colors.red; @@ -671,24 +682,24 @@ bool Keyboard::setKey(Key key, KeyColors colors) { return setKey(keyValue); } -bool Keyboard::setKeys(KeyValue keyValue[], int keyValueCount) { - int maxLogoKeys = 5; - int logoCount = 0; +bool Keyboard::setKeys(KeyValue keyValue[], size_t keyValueCount) { + const size_t maxLogoKeys = 5; + const size_t maxIndicatorsKeys = 25; + const size_t maxMultimediaKeys = 25; + const size_t maxKeys = 200; + const size_t maxGKeys = 25; + size_t logoCount = 0; + size_t indicatorsCount = 0; + size_t multimediaCount = 0; + size_t keysCount = 0; + size_t gkeysCount = 0; KeyValue logo[maxLogoKeys]; - int maxIndicatorsKeys = 25; - int indicatorsCount = 0; KeyValue indicators[maxIndicatorsKeys]; - int maxMultimediaKeys = 25; - int multimediaCount = 0; KeyValue multimedia[maxMultimediaKeys]; - int maxKeys = 200; - int keysCount = 0; KeyValue keys[maxKeys]; - int maxGKeys = 25; - int gkeysCount = 0; KeyValue gkeys[maxGKeys]; - for (int i = 0; i < keyValueCount; i++) { + for (size_t i = 0; i < keyValueCount; i++) { if(keyValue[i].key.addressGroup == KeyAddressGroup::logo && logoCount <= maxLogoKeys) { logo[logoCount] = keyValue[i]; logoCount++; @@ -714,11 +725,11 @@ bool Keyboard::setKeys(KeyValue keyValue[], int keyValueCount) { if (multimediaCount > 0) setKeysInternal(KeyAddressGroup::multimedia, multimedia, multimediaCount); if (keysCount > 0) { - int maxKeyValueCount = 14; - for (int i = 0; i < keysCount; i = i + maxKeyValueCount) { + const size_t maxKeyValueCount = 14; + for (size_t i = 0; i < keysCount; i = i + maxKeyValueCount) { KeyValue keysBlock[maxKeyValueCount]; - int keysBlockCount = 0; - for (int j = 0; j < maxKeyValueCount; j++) { + size_t keysBlockCount = 0; + for (size_t j = 0; j < maxKeyValueCount; j++) { if((i + j) < keysCount ) { keysBlock[j] = keys[i + j]; keysBlockCount++; diff --git a/src/classes/Keyboard.h b/src/classes/Keyboard.h index aa0f60f..a2b563e 100644 --- a/src/classes/Keyboard.h +++ b/src/classes/Keyboard.h @@ -2,7 +2,7 @@ #define DEF_KEYBOARD #include -#include "/usr/include/libusb-1.0/libusb.h" +#include class Keyboard { @@ -32,8 +32,8 @@ class Keyboard { }; enum class KeyGroup { logo, indicators, multimedia, fkeys, modifiers, arrows, numeric, functions, keys, gkeys}; - struct KeyColors { char red; char green; char blue; }; - struct KeyAddress { KeyAddressGroup addressGroup; char id; }; + struct KeyColors { uint8_t red; uint8_t green; uint8_t blue; }; + struct KeyAddress { KeyAddressGroup addressGroup; uint8_t id; }; struct KeyValue { KeyAddress key; KeyColors colors; }; bool isAttached(); @@ -48,7 +48,7 @@ class Keyboard { bool setPowerOnEffect(PowerOnEffect powerOnEffect); bool setKey(KeyValue keyValue); bool setKey(Key key, KeyColors colors); - bool setKeys(KeyValue keyValue[], int keyValueCount); + bool setKeys(KeyValue keyValue[], size_t keyValueCount); bool setAllKeys(KeyColors colors); bool setGroupKeys(KeyGroup keyGroup, KeyColors colors); @@ -63,8 +63,8 @@ class Keyboard { libusb_context *ctx = NULL; bool populateAddressGroupInternal(KeyAddressGroup addressGroup, unsigned char *data); - bool sendDataInternal(unsigned char *data, int data_size); - bool setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[], int keyValueCount); + bool sendDataInternal(unsigned char *data, uint16_t data_size); + bool setKeysInternal(KeyAddressGroup addressGroup, KeyValue keyValues[], size_t keyValueCount); };