diff --git a/hexdump_analysis/Untitled-1.py b/hexdump_analysis/Untitled-1.py new file mode 100644 index 0000000..bf8460b --- /dev/null +++ b/hexdump_analysis/Untitled-1.py @@ -0,0 +1,48 @@ + + +conf_12 check: + +class datacheck: + type : (type?, [0,...,3], {2, 7, 19}, x) + length + funcptr + + datachekc(type, typedef, length, ptr): + funcpotr = ptr + + check(*data): + actdata = funcpotr(data) + switch(type): + case a: return actdata > typedef[0] + case b: return (actdata > typedef[0]) && (actdata < typedef[1]) + + +map [pos] -> datacheck(0, [0,7], 16, (data) => return (data << 4) | (data >> 4)) +map [pos+16] -> datacheck(0, [0,7], 16, (data) => return (data << 4) | (data >> 4)) + +map [pos] -> datacheck(1, [0], 8*16, (data) => uint8_t x = 0; for (int i = 0; i < 16; ++i) x | data[i]; return !x;) + + +func a(pos): + return map[pos] + +pos not per byte but bit + + +a(pos).check + + +confch3ck(): + isok = True + extralen = 0 + chk; + for(int i = 0; i < (int)sizeof(conf_12) * 8;): + chk = map[i] + res = chk.check(&(((unsigned char *)conf_12)[i])) + i+=chk.length; + + if not res: + isok = False + break; + if chk.type == set; + print("is fucked at byte x / bit y with length chk.length it is not part of set [chk.typedef]") \ No newline at end of file diff --git a/src/a811.cpp b/src/a811.cpp index 9fc5c85..6f3f0e4 100644 --- a/src/a811.cpp +++ b/src/a811.cpp @@ -357,6 +357,107 @@ _r_button A811::getButton(int id) return _r_button(); } +ssize_t A811::readMacrosFromDevice(int cnt) +{ + if (cnt > 255) + { + std::cout << "No more than 255 macros on device" << std::endl; + return ssize_t(1); + } + + _macros = (macro **)malloc(cnt * sizeof(macro *)); + + _connection->open(); + _connection->keepConnection(true); + + for (int i = 1; i <= cnt; i++) + { + // get macro + // ist das bissl kacke weil immer neuer handle und detach und attach hmm + _macros[i - 1] = (macro *)malloc(sizeof(macro)); + readMacroFromDevice(_macros[i - 1], i); + } + + _connection->keepConnection(false); + _connection->close(); + + // check if any macros are read or if any empty macros are read + // maybe just read all possible macros + // or this function is actually completely useless just keep track of the macros in the gui code + return ssize_t(0); +} + +ssize_t A811::readMacroFromDevice(macro *_macro, int id) +{ + if (id > (uint8_t)255) + { + std::cout << "No more than 255 macros on device" << std::endl; + return ssize_t(0); + } + + memset(_macro, 0, 520 * sizeof(unsigned char)); + + unsigned char _data[8] = {0x05, 0x31, (uint8_t)id, 0, 0, 0, 0, 0}; + + ssize_t r; + CHK(_connection->open()); + CHK(_connection->setReport(0x0305, (unsigned char *)_data, 8)); + CHK(_connection->getReport(0x0308, (unsigned char *)_macro, 520)); + CHK(_connection->close()); + + // check if empty macro, if valid macro + return ssize_t(0); +} + +ssize_t A811::writeMacroToDevice(macro *p_macro) +{ + // conf_1 + p_macro->set_report_req[0] = 0x80; + p_macro->set_report_req[1] = 0x30; + p_macro->set_report_req[2] = 0x02; + + ssize_t r; + CHK(_connection->open()); + CHK(_connection->setReport(0x0308, (unsigned char *)p_macro, 520)); + CHK(_connection->close()); + + // check if macro is valid + return ssize_t(0); +} + +bool A811::isIdle() +{ + unsigned char conf[8]; + memset(conf, 0, 8); + unsigned char _data[8] = {0x05, 0x80, 0, 0, 0, 0, 0, 0}; + + ssize_t r; + CHK(_connection->open()); + CHK(_connection->setReport(0x0305, _data, 8)); + CHK(_connection->getReport(0x0305, (unsigned char *)conf, 8)); + CHK(_connection->close()); + + if (conf[2] == 0x0 && conf[3] == 0x1) + return true; + else if (conf[2] == 0x1 && conf[3] == 0x1) + return false; + + logError("ERROR: Reading idle status.", __FILE__, __LINE__, __func__); + return false; +} + +ssize_t A811::restoreConfig() +{ + unsigned char _data[8] = {0x05, 0x40, 0x01, 0, 0, 0, 0, 0}; + + ssize_t r; + CHK(_connection->open()); + CHK(_connection->setReport(0x0305, (unsigned char *)_data, 8)); + CHK(_connection->close()); + + return ssize_t(0); +} + ssize_t A811::readConfigFromDevice(CONF_TYPE t_conf) { unsigned char _data[8] = {0x05, 0x21, 0, 0, 0, 0, 0, 0}; diff --git a/src/a811.h b/src/a811.h index 6c78643..106b09e 100644 --- a/src/a811.h +++ b/src/a811.h @@ -173,7 +173,7 @@ public: * @param mode The mode for which to set the DPI and color values. * @param color RGB * @param dpi DPI value 50-26000 - * @param active 1-5 how many DPI_MODE should be active. + * @param active 1-5 how many DPI_MODE should be active. * * @return ssize_t Returns the status of the operation, where 0 indicates * success and a negative value indicates failure. @@ -230,6 +230,7 @@ public: * success and a negative value indicates failure. */ ssize_t setMacroButton(int, uint8_t, uint8_t, uint8_t); + /** * @brief Assign a simple keyboard key w/o modifier to a button. * @@ -255,6 +256,65 @@ public: */ _r_button getButton(int); + /** + * @brief Read macros from the mouse. + * + * Read a specific number of macros from the mouse. + * + * @param macros where the macros should be saved. + * @param cnt how many macros should be read. + * + * @return ssize_t Returns the status of the operation, where 0 indicates + * success and a negative value indicates failure. + */ + ssize_t readMacrosFromDevice(int); + + /** + * @brief Read a single macro from the mouse. + * + * Read a specific macro from the mouse. + * + * @param macro where the macro should be saved. + * @param id id of the macro. + * + * @return ssize_t Returns the status of the operation, where 0 indicates + * success and a negative value indicates failure. + */ + ssize_t readMacroFromDevice(macro *, int); + + /** + * @brief Write a single macro to the mouse. + * + * Read a specific macro from the mouse. + * + * @param macro the macro to be written. + * + * @return ssize_t Returns the status of the operation, where 0 indicates + * success and a negative value indicates failure. + */ + ssize_t writeMacroToDevice(macro *); + + /** + * @brief Get the idle status of the mouse. + * + * Get the idle status of the mouse. + * + * @return bool returns true if idle, false else + */ + // the error logging here is kinda weird + bool isIdle(); + + // should be writing a custom thingy so i know what the restored config looks like + /** + * @brief Apply standard config. + * + * Write a standard configuration to the mouse if something went wrong. + * + * @return Returns the status of the operation, where 0 indicates + * success and a negative value indicates failure. + */ + ssize_t restoreConfig(); + ssize_t readConfigFromDevice(CONF_TYPE); ssize_t writeConfigToDevice(CONF_TYPE); diff --git a/src/conf.cpp b/src/conf.cpp index d49978d..89de91a 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -17,7 +17,7 @@ Conf::Conf(QObject *parent) : QObject(parent) memset(m_conf1, 0, sizeof(conf_1)); memset(m_conf2, 0, sizeof(conf_2)); - m_mouse = new A811(); + connection = &(Connection::getInstance()); if (connection->findDevice()) @@ -25,10 +25,7 @@ Conf::Conf(QObject *parent) : QObject(parent) std::cerr << "ERROR: device not found" << std::endl; exit(1); } - - // read initial config from device - readConfigFromDevice(m_conf1); - readConfigFromDevice(m_conf2); + m_mouse = new A811(); } ssize_t Conf::appendMacroToFile(macro *macro, const char *filePath) @@ -111,102 +108,77 @@ ssize_t Conf::deleteMacroFromFile(macro *_macro, const char *filePath) void Conf::testFunc() { - /*macro **macros; - readMacrosFromDevice(macros, 4); - for (int i = 0; i < 4; i++) - { - appendMacroToFile(macros[i], "macrofileappend"); - }*/ - A811 *asd = new A811(); - asd->readConfigFromDevice(CONF_TYPE::BATTERY); - asd->readConfigFromDevice(CONF_TYPE::LIGHT); - asd->readConfigFromDevice(CONF_TYPE::KEY); + + m_mouse->readConfigFromDevice(CONF_TYPE::BATTERY); + m_mouse->readConfigFromDevice(CONF_TYPE::LIGHT); + m_mouse->readConfigFromDevice(CONF_TYPE::KEY); - asd->setLightMode(LIGHT_MODE::BREATHING, 1, 4); - asd->writeConfigToDevice(CONF_TYPE::LIGHT); - asd->setUSBPollingRate(POLLING_RATE::P_1000); + m_mouse->setLightMode(LIGHT_MODE::BREATHING, 1, 4); + m_mouse->writeConfigToDevice(CONF_TYPE::LIGHT); + m_mouse->setUSBPollingRate(POLLING_RATE::P_1000); - std::cout << "UBS Polling rate: " << (int)asd->getUSBPollingRate() << std::endl; - std::cout << "BATTERYSTATUS: " << (int)asd->getBatteryStatus() << std::endl; - std::cout << "LIGHTMODE: " << (int)asd->getLightMode() << std::endl; + std::cout << "UBS Polling rate: " << (int)m_mouse->getUSBPollingRate() << std::endl; + std::cout << "BATTERYSTATUS: " << (int)m_mouse->getBatteryStatus() << std::endl; + std::cout << "LIGHTMODE: " << (int)m_mouse->getLightMode() << std::endl; - asd->setLightMode(LIGHT_MODE::COLORFUL_STREAMING, 1, 2); - asd->writeConfigToDevice(CONF_TYPE::LIGHT); + m_mouse->setLightMode(LIGHT_MODE::COLORFUL_STREAMING, 1, 2); + m_mouse->writeConfigToDevice(CONF_TYPE::LIGHT); sleep(2); - asd->setMultimediaButton(2, "media_volume_up"); + m_mouse->setMultimediaButton(2, "media_volume_up"); // does not work for some reason - asd->setMultimediaButton(6, "dpi-cycle"); - asd->setButton(7, "Volume_Up", 0); - asd->setButton(8, "b", 2); - asd->setButton(9, "c", 4); - asd->setButton(10, "d", 8); - asd->setButton(11, "e", 3); - asd->setButton(12, "f", 5); - asd->setButton(13, "g", 7); - asd->setButton(14, "h", 9); + m_mouse->setMultimediaButton(6, "dpi-cycle"); + m_mouse->setButton(7, "Volume_Up", 0); + m_mouse->setButton(8, "b", 2); + m_mouse->setButton(9, "c", 4); + m_mouse->setButton(10, "d", 8); + m_mouse->setButton(11, "e", 3); + m_mouse->setButton(12, "f", 5); + m_mouse->setButton(13, "g", 7); + m_mouse->setButton(14, "h", 9); // dont know if every keycode is working int bid = 0; - std::cout << "Button " << bid << asd->getButton(bid).name << std::endl; + std::cout << "Button " << bid << m_mouse->getButton(bid).name << std::endl; - asd->writeConfigToDevice(CONF_TYPE::KEY); - std::cout << asd->getButton(3).name << std::endl; + m_mouse->writeConfigToDevice(CONF_TYPE::KEY); + std::cout << m_mouse->getButton(3).name << std::endl; - asd->setLightMode(LIGHT_MODE::BREATHING, 4, 4); + m_mouse->setLightMode(LIGHT_MODE::BREATHING, 4, 4); unsigned char *cols; - cols = asd->getLightModeColors(LIGHT_MODE::BREATHING); + cols = m_mouse->getLightModeColors(LIGHT_MODE::BREATHING); for (int i = 0; i < 21; i++) { std::cout << cols[i]; } std::cout << std::endl; - asd->getLightModeColors(LIGHT_MODE::COLORFUL_STREAMING); + m_mouse->getLightModeColors(LIGHT_MODE::COLORFUL_STREAMING); unsigned char cols2[21] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00}; - asd->setLightModeColors(LIGHT_MODE::BREATHING, cols2); + m_mouse->setLightModeColors(LIGHT_MODE::BREATHING, cols2); unsigned char cols3[3] = {0xff, 0x00, 0x00}; - asd->setDPI(DPI_MODE::DPI_1, cols3, 800, 5); + m_mouse->setDPI(DPI_MODE::DPI_1, cols3, 800, 5); cols3[0] = 0x0; cols3[1] = 0xff; - asd->setDPI(DPI_MODE::DPI_2, cols3, 1000, 5); + m_mouse->setDPI(DPI_MODE::DPI_2, cols3, 1000, 5); cols3[1] = 0x0; cols3[2] = 0xff; - asd->setDPI(DPI_MODE::DPI_3, cols3, 1200, 5); + m_mouse->setDPI(DPI_MODE::DPI_3, cols3, 1200, 5); cols3[0] = 0xff; cols3[2] = 0xff; - asd->setDPI(DPI_MODE::DPI_4, cols3, 1400, 5); + m_mouse->setDPI(DPI_MODE::DPI_4, cols3, 1400, 5); cols3[0] = 0xff; cols3[1] = 0xff; cols3[2] = 0xff; - asd->setDPI(DPI_MODE::DPI_5, cols3, 1500, 5); - // asd->writeConfigToDevice(CONF_TYPE::KEY); - std::cout << "DPI at this: " << asd->getDPI(DPI_MODE::DPI_1) << std::endl; + m_mouse->setDPI(DPI_MODE::DPI_5, cols3, 1500, 5); + // m_mouse->writeConfigToDevice(CONF_TYPE::KEY); + std::cout << "DPI at this: " << m_mouse->getDPI(DPI_MODE::DPI_1) << std::endl; - std::cout << "Battery capacity:" << std::dec << asd->getBatteryCapacity() << std::endl; - asd->writeConfigToDevice(CONF_TYPE::LIGHT); -} + std::cout << "Battery capacity:" << std::dec << m_mouse->getBatteryCapacity() << std::endl; + m_mouse->writeConfigToDevice(CONF_TYPE::LIGHT); -int Conf::lightMode() { return m_conf1->light_mode; } - -void Conf::setLightMode(int mode) -{ - m_conf1->light_mode = (unsigned char)mode; - std::cout << (int)m_conf1->light_mode << std::endl; - Q_EMIT lightModeChanged(); -} - -ssize_t Conf::restoreConfigs() -{ - unsigned char _data[8] = {0x05, 0x40, 0x01, 0, 0, 0, 0, 0}; - - ssize_t r; - CHK(connection->open()); - CHK(connection->setReport(0x0305, (unsigned char *)_data, 8)); - CHK(connection->close()); - - return 0; + m_mouse->readMacrosFromDevice(4); } ssize_t Conf::writeConfigToFile(const char *filePath) @@ -247,154 +219,6 @@ ssize_t Conf::writeConfigToFile(const char *filePath) return 0; } -ssize_t Conf::writeConfigToDevice(conf_2 *conf) -{ - ssize_t r; - CHK(connection->open()); - conf->req_type = 0x50; - CHK(connection->setReport(0x0308, (unsigned char *)conf, 520)); - CHK(connection->close()); - - return 0; -} - -ssize_t Conf::writeConfigToDevice(conf_1 *conf) -{ - ssize_t r; - CHK(connection->open()); - conf->req_type = 0x92; - CHK(connection->setReport(0x0308, (unsigned char *)conf, 520)); - CHK(connection->close()); - - return 0; -} - -ssize_t Conf::readConfigFromDevice(conf_2 *conf) -{ - unsigned char _data[8] = {0x05, 0x22, 0, 0, 0, 0, 0, 0}; - /*if(!connection->isWireless()) - _data[1] = 0x12;*/ - - ssize_t r; - CHK(connection->open()); - CHK(connection->setReport(0x0305, (unsigned char *)_data, 8)); - CHK(connection->getReport(0x0308, (unsigned char *)conf, 520)); - CHK(connection->close()); - - return 0; -} - -ssize_t Conf::readMacroFromDevice(macro *_macro, int id) -{ - if (id > (uint8_t)255) - { - std::cout << "No more than 255 macros on device" << std::endl; - return -1; - } - - memset(_macro, 0, 520 * sizeof(unsigned char)); - - unsigned char _data[8] = {0x05, 0x31, (uint8_t)id, 0, 0, 0, 0, 0}; - - ssize_t r; - CHK(connection->open()); - CHK(connection->setReport(0x0305, (unsigned char *)_data, 8)); - CHK(connection->getReport(0x0308, (unsigned char *)_macro, 520)); - CHK(connection->close()); - - std::cout << std::hex << (int)_macro->macro_nr << "\t"; - std::cout << std::endl; - - return 0; -} - -ssize_t Conf::writeMacroToDevice(macro *_macro) -{ - - // conf_1 - _macro->set_report_req[0] = 0x80; - _macro->set_report_req[1] = 0x30; - _macro->set_report_req[2] = 0x02; - ssize_t r; - CHK(connection->open()); - CHK(connection->setReport(0x0308, (unsigned char *)_macro, 520)); - CHK(connection->close()); - - return 0; -} - -ssize_t Conf::readMacrosFromDevice(macro **¯os, int cnt) -{ - if (cnt > 255) - { - std::cout << "No more than 255 macros on device" << std::endl; - return -1; - } - - macros = (macro **)malloc(cnt * sizeof(macro *)); - - connection->open(); - connection->keepConnection(true); - - for (int i = 1; i <= cnt; i++) - { - // get macro - // ist das bissl kacke weil immer neuer handle und detach und attach hmm - macros[i - 1] = (macro *)malloc(sizeof(macro)); - readMacroFromDevice(macros[i - 1], i); - } - - connection->keepConnection(false); - connection->close(); - - return 0; -} - -ssize_t Conf::readAssignedMacrosFromDevice() -{ - // check all the keys for macro mod - int macroCnt = 0; - for (int i = 0; i < 8; i++) - { - if (m_conf2->side[i].pref == 0x70 || - (m_conf2->mouse_buttons[i].pref == 0x70 && i < 6)) - macroCnt++; - } - std::cout << macroCnt << std::endl; - - unsigned char conf[520]; - memset(conf, 0, 520 * sizeof(unsigned char)); - - unsigned char _data[8] = {0x05, 0x31, 1, 0, 0, 0, 0, 0}; - - ssize_t r; - CHK(connection->open()); - for (int i = 1; i <= macroCnt; i++) - { - _data[2] = i; - CHK(connection->setReport(0x0305, _data, 8)); - CHK(connection->getReport(0x0308, (unsigned char *)conf, 520)); - } - CHK(connection->close()); - - return 0; -} - -ssize_t Conf::readConfigFromDevice(conf_1 *conf) -{ - unsigned char _data[8] = {0x05, 0x21, 0, 0, 0, 0, 0, 0}; - /*if(!connection->isWireless()) - _data[1] = 0x11;*/ - - ssize_t r; - CHK(connection->open()); - CHK(connection->setReport(0x0305, _data, 8)); - CHK(connection->getReport(0x0308, (unsigned char *)conf, 520)); - CHK(connection->close()); - - return ssize_t(0); -} - ssize_t Conf::writeConfigToFile() { QString path = QFileDialog::getSaveFileName(nullptr, i18n("Save File As")); @@ -492,40 +316,6 @@ ssize_t Conf::readConfigFromFile(const char *filePath) return 0; } -ssize_t Conf::getIdleStatus() -{ - unsigned char conf[8] = { - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - }; - unsigned char _data[8] = {0x05, 0x80, 0, 0, 0, 0, 0, 0}; - /*if(!connection->isWireless()) - _data[1] = 0x11;*/ - - ssize_t r; - CHK(connection->open()); - CHK(connection->setReport(0x0305, _data, 8)); - CHK(connection->getReport(0x0305, (unsigned char *)conf, 8)); - CHK(connection->close()); - - std::cout << "IDLE STATUS --------------------------" << std::endl; - for (int i = 0; i < 8; i++) - { - std::cout << std::hex << (int)conf[i] << "\t"; - } - std::cout << "---------------" << std::endl; - - return ssize_t(0); - - return ssize_t(); -} - ssize_t Conf::readConfigFromFile() { QFileDialog dialog(nullptr); diff --git a/src/conf.h b/src/conf.h index b0fcb58..05736d5 100644 --- a/src/conf.h +++ b/src/conf.h @@ -21,39 +21,20 @@ class Conf : public QObject { Q_OBJECT - Q_PROPERTY(int lightMode READ lightMode WRITE setLightMode NOTIFY lightModeChanged) public: explicit Conf(QObject *parent = nullptr); - int lightMode(); - Q_INVOKABLE void setLightMode(int mode); - Q_SIGNAL void lightModeChanged(); - - Q_INVOKABLE ssize_t restoreConfigs(); - Q_INVOKABLE ssize_t writeConfigToFile(); ssize_t writeConfigToFile(const char *filePath); Q_INVOKABLE ssize_t readConfigFromFile(); ssize_t readConfigFromFile(const char *filePath); - ssize_t getIdleStatus(); - // all the config should be in a single file ssize_t writeConfigToFile(conf_1 *conf, const char *filePath); ssize_t writeConfigToFile(conf_2 *conf, const char *filePath); ssize_t readConfigFromFile(const char *filePath, conf_1 *conf); ssize_t readConfigFromFile(const char *filePath, conf_2 *conf); - ssize_t writeConfigToDevice(conf_1 *conf); - ssize_t writeConfigToDevice(conf_2 *conf); - ssize_t readConfigFromDevice(conf_1 *conf); - ssize_t readConfigFromDevice(conf_2 *conf); - - Q_INVOKABLE ssize_t readAssignedMacrosFromDevice(); - - Q_INVOKABLE ssize_t readMacrosFromDevice(macro **¯os, int cnt); - Q_INVOKABLE ssize_t readMacroFromDevice(macro *_macro, int id); - Q_INVOKABLE ssize_t writeMacroToDevice(macro *_macro); Q_INVOKABLE ssize_t appendMacroToFile(macro *_macro, const char *filePath); Q_INVOKABLE ssize_t deleteMacroFromFile(macro *_macro, const char *filePath);