From de6ce3178fc350b214a25df1ccdd5ff2d0a350e5 Mon Sep 17 00:00:00 2001 From: Denis Manherz Date: Thu, 29 Aug 2024 15:02:48 +0200 Subject: [PATCH] Something --- conf1.bin | Bin 0 -> 520 bytes conf2.bin | Bin 0 -> 520 bytes person.bin | Bin 0 -> 519 bytes src/conf.cpp | 287 ++++++++++++++++++++++++++++++++++++++++++++++---- src/conf.h | 27 +++-- src/main.cpp | 116 +------------------- src/structs.h | 7 +- 7 files changed, 293 insertions(+), 144 deletions(-) create mode 100644 conf1.bin create mode 100644 conf2.bin create mode 100644 person.bin diff --git a/conf1.bin b/conf1.bin new file mode 100644 index 0000000000000000000000000000000000000000..ada055817e422a1c9c1a4828de015dae48673198 GIT binary patch literal 520 zcmZ|LJx&5a90l;l07{ICXtESG*x)sm1&pnQ{T5bM+<-j;EzS5vj7Fl7(6U%+;T84} z#&+D9_hQE+v%h`+H<@g+9q>)puVME+C%0%lJ=bc f;fBVy6n8Y2*}eLi=CW8a+ZD||R~@km1Lk literal 0 HcmV?d00001 diff --git a/conf2.bin b/conf2.bin new file mode 100644 index 0000000000000000000000000000000000000000..ae723bd3b6920e7263028718630218dbd3b088a1 GIT binary patch literal 520 zcmd;3VgLg{Mg|5!CLm@3Vh$h{U|>*WNMvvX$_B7Ow%it9S?$OAoD*EfGtBJ4zNMwnW6Fy0uVmXII#KyTo69cX;}3m z8ATTJ092O(l&Fj}6ij_SLlHwNgFeGU tsQLx3AnG?ffzTJGK5sU^Fat!hDncYUpmd5jiz@>U13!Z> zgE)gUR1;R}Kg0k3AUvCa0nGgG#>C_-Cib6!K@cbolmN>E)Rs #include -Conf::Conf(QObject *parent) : QObject(parent) {} -QList Conf::deviceList() +Conf::Conf(QObject *parent) : QObject(parent) { - libusb_context *ctx; - int r = libusb_init(&ctx); + int r = libusb_init(&m_ctx); if (r < 0) { std::cout << libusb_error_name(r) << std::endl; exit(1); } libusb_device **list; - libusb_device *found = NULL; - ssize_t cnt = libusb_get_device_list(ctx, &list); - - QQmlEngine engine; - QList asd; + ssize_t cnt = libusb_get_device_list(m_ctx, &list); for (ssize_t i = 0; i < cnt; i++) { libusb_device *device = list[i]; - found = device; - uint8_t addr = libusb_get_device_address(found); - uint8_t bus = libusb_get_bus_number(found); + m_device = device; + uint8_t addr = libusb_get_device_address(m_device); + uint8_t bus = libusb_get_bus_number(m_device); libusb_device_descriptor desc; - libusb_get_device_descriptor(found, &desc); - QString formattedString = QString::asprintf("%04X:%04X", desc.idVendor, desc.idProduct); - QQmlComponent component(&engine, - QUrl::fromLocalFile(QString::fromStdString("/home/denis/repos/a811/src/action.qml"))); - QObject *object = component.create(); - QQmlProperty::write(object, QString::fromStdString("text"), formattedString); - //object->setProperty("shortcut", QString::fromStdString("StandardKey.Quit")); - //object->setProperty("icon.name", QString::fromStdString("application-exit-symbolic")); - //object->setProperty("onTriggered", QString::fromStdString("showPassiveNotification('Action 2 clicked')")); - asd.append(object); + libusb_get_device_descriptor(m_device, &desc); + if (desc.idProduct == 0x2f && desc.idVendor == 0x258a) + { + break; + } } + libusb_free_device_list(list, 1); +} +QList Conf::deviceList() +{ + conf_1 *conf = (conf_1 *)malloc(sizeof(conf_1)); + conf_2 *conf2 = (conf_2 *)malloc(sizeof(conf_2)); + + // readConfigFromDevice(conf); + readConfigFromFile("conf1.bin", conf); + readConfigFromDevice(conf2); + + // edit data + // conf->light_mode = 0x2; + conf->req_type = 0x92; + conf->st_brightness_colnr = {0x16}; + conf->col_steady[18] = 0x00; + conf->col_steady[19] = 0xff; + conf->col_steady[20] = 0xff; + conf->col_breathing[0] = 0xff; + conf->col_breathing[1] = 0xff; + conf->col_breathing[2] = 0x00; + + writeConfigToDevice(conf); + writeConfigToDevice(conf2); + + writeConfigToFile(conf, "conf1.bin"); + writeConfigToFile(conf2, "conf2.bin"); + + QQmlEngine engine; + + QList asd; + QQmlComponent component(&engine, + QUrl::fromLocalFile(QString::fromStdString("/home/denis/repos/a811/src/action.qml"))); + QObject *object = component.create(); + asd.append(object); return asd; } @@ -44,4 +68,223 @@ QList Conf::deviceList() void Conf::setDeviceList(QList deviceList) { Q_EMIT deviceListChanged(); +} + +ssize_t Conf::writeConfigToDevice(conf_2 *conf) +{ + int r = 0; + libusb_device_handle *handle; + if ((r = libusb_open(m_device, &handle)) < 0) + std::cout << "libusb_open: " << libusb_error_name(r) << std::endl; + + if ((r = libusb_detach_kernel_driver(handle, 1)) < 0) + std::cout << "libusb_detach_kernel_driver: " << libusb_error_name(r) << std::endl; + if ((r = libusb_claim_interface(handle, 1)) < 0) + std::cout << "libusb_claim_interface: " << libusb_error_name(r) << std::endl; + + // conf_1 + if ((r = libusb_control_transfer(handle, 0x21, 0x09, 0x0308, 1, (unsigned char *)conf, 520, 5000)) < 0) + std::cout << "libusb_control_transfer5: " << libusb_error_name(r) << std::endl; + + if ((r = libusb_release_interface(handle, 1)) < 0) + std::cout << "libusb_release_interface: " << libusb_error_name(r) << std::endl; + if ((r = libusb_attach_kernel_driver(handle, 1)) < 0) + std::cout << "libusb_attach_kernel_driver: " << libusb_error_name(r) << std::endl; + + libusb_close(handle); + + return 0; +} + +ssize_t Conf::writeConfigToDevice(conf_1 *conf) +{ + int r = 0; + libusb_device_handle *handle; + if ((r = libusb_open(m_device, &handle)) < 0) + std::cout << "libusb_open: " << libusb_error_name(r) << std::endl; + + if ((r = libusb_detach_kernel_driver(handle, 1)) < 0) + std::cout << "libusb_detach_kernel_driver: " << libusb_error_name(r) << std::endl; + if ((r = libusb_claim_interface(handle, 1)) < 0) + std::cout << "libusb_claim_interface: " << libusb_error_name(r) << std::endl; + + // conf_1 + if ((r = libusb_control_transfer(handle, 0x21, 0x09, 0x0308, 1, (unsigned char *)conf, 520, 5000)) < 0) + std::cout << "libusb_control_transfer5: " << libusb_error_name(r) << std::endl; + + if ((r = libusb_release_interface(handle, 1)) < 0) + std::cout << "libusb_release_interface: " << libusb_error_name(r) << std::endl; + if ((r = libusb_attach_kernel_driver(handle, 1)) < 0) + std::cout << "libusb_attach_kernel_driver: " << libusb_error_name(r) << std::endl; + + libusb_close(handle); + + return 0; +} + +ssize_t Conf::readConfigFromDevice(conf_2 *conf) +{ + unsigned char send_data0[8]; + memset(send_data0, 0, 8 * sizeof(unsigned char)); + send_data0[0] = 0x05; + send_data0[1] = 0x22; // conf 2, wireless + + // get config before write new config + // conf_1 + int r = 0; + libusb_device_handle *handle; + if ((r = libusb_open(m_device, &handle)) < 0) + std::cout << "libusb_open: " << libusb_error_name(r) << std::endl; + + if ((r = libusb_detach_kernel_driver(handle, 1)) < 0) + std::cout << "libusb_detach_kernel_driver: " << libusb_error_name(r) << std::endl; + if ((r = libusb_claim_interface(handle, 1)) < 0) + std::cout << "libusb_claim_interface: " << libusb_error_name(r) << std::endl; + + // conf_2 + if ((r = libusb_control_transfer(handle, 0x21, 0x09, 0x0305, 1, send_data0, 8, 5000)) < 0) + std::cout << "libusb_control_transfer3: " << libusb_error_name(r) << std::endl; + if ((r = libusb_control_transfer(handle, 0xa1, 0x01, 0x0308, 1, (unsigned char *)conf, 520, 5000)) < 0) + std::cout << "libusb_control_transfer4: " << libusb_error_name(r) << std::endl; + + if ((r = libusb_release_interface(handle, 1)) < 0) + std::cout << "libusb_release_interface: " << libusb_error_name(r) << std::endl; + if ((r = libusb_attach_kernel_driver(handle, 1)) < 0) + std::cout << "libusb_attach_kernel_driver: " << libusb_error_name(r) << std::endl; + + libusb_close(handle); + + return 0; +} + +ssize_t Conf::readConfigFromDevice(conf_1 *conf) +{ + unsigned char send_data0[8]; + memset(send_data0, 0, 8 * sizeof(unsigned char)); + send_data0[0] = 0x05; + send_data0[1] = 0x21; // conf 1, wireless + + // get config before write new config + // conf_1 + int r = 0; + libusb_device_handle *handle; + if ((r = libusb_open(m_device, &handle)) < 0) + std::cout << "libusb_open: " << libusb_error_name(r) << std::endl; + + if ((r = libusb_detach_kernel_driver(handle, 1)) < 0) + std::cout << "libusb_detach_kernel_driver: " << libusb_error_name(r) << std::endl; + if ((r = libusb_claim_interface(handle, 1)) < 0) + std::cout << "libusb_claim_interface: " << libusb_error_name(r) << std::endl; + + // get config before write new config + // conf_1 + if ((r = libusb_control_transfer(handle, 0x21, 0x09, 0x0305, 1, send_data0, 8, 5000)) < 0) + std::cout << "libusb_control_transfer1: " << libusb_error_name(r) << std::endl; + if ((r = libusb_control_transfer(handle, 0xa1, 0x01, 0x0308, 1, (unsigned char *)conf, 520, 5000)) < 0) + std::cout << "libusb_control_transfer2: " << libusb_error_name(r) << std::endl; + + if ((r = libusb_release_interface(handle, 1)) < 0) + std::cout << "libusb_release_interface: " << libusb_error_name(r) << std::endl; + if ((r = libusb_attach_kernel_driver(handle, 1)) < 0) + std::cout << "libusb_attach_kernel_driver: " << libusb_error_name(r) << std::endl; + + libusb_close(handle); + + return 0; +} + +ssize_t Conf::writeConfigToFile(conf_1 *conf, const char *filePath) +{ + FILE *outfile; + outfile = fopen(filePath, "wb"); + if (outfile == NULL) + { + fprintf(stderr, "\nError opened file\n"); + exit(1); + } + // write struct to file + int flag = 0; + flag = fwrite(conf, sizeof(struct conf_1), 1, outfile); + + if (flag) + { + printf("Contents of the structure written " + "successfully %d\n", + flag); + } + else + printf("Error Writing to File!\n"); + + fclose(outfile); + + return 0; +} + +ssize_t Conf::writeConfigToFile(conf_2 *conf, const char *filePath) +{ + FILE *outfile; + outfile = fopen(filePath, "wb"); + if (outfile == NULL) + { + fprintf(stderr, "\nError opened file\n"); + exit(1); + } + // write struct to file + int flag = 0; + flag = fwrite(conf, sizeof(struct conf_2), 1, outfile); + + if (flag) + { + printf("Contents of the structure written " + "successfully %d\n", + flag); + } + else + printf("Error Writing to File!\n"); + + fclose(outfile); + + return 0; +} + +ssize_t Conf::readConfigFromFile(const char *filePath, conf_1 *conf) +{ + FILE *infile; + + // Open person.dat for reading + infile = fopen(filePath, "rb"); + if (infile == NULL) + { + fprintf(stderr, "\nError opening file\n"); + exit(1); + } + + // reading to read_struct + fread(conf, sizeof(struct conf_1), 1, infile); + + // close file + fclose(infile); + + return 0; +} + +ssize_t Conf::readConfigFromFile(const char *filePath, conf_2 *conf) +{ + FILE *infile; + + // Open person.dat for reading + infile = fopen(filePath, "rb"); + if (infile == NULL) + { + fprintf(stderr, "\nError opening file\n"); + exit(1); + } + + // reading to read_struct + fread(conf, sizeof(struct conf_2), 1, infile); + + // close file + fclose(infile); + + return 0; } \ No newline at end of file diff --git a/src/conf.h b/src/conf.h index 0953ae3..32aa645 100644 --- a/src/conf.h +++ b/src/conf.h @@ -3,29 +3,44 @@ #include #include -#include #include #include #include #include #include +#include +#include +#include +#include "structs.h" +#include + +#define CONF_FILE "conf.bin" class Conf : public QObject { Q_OBJECT - Q_PROPERTY(QList deviceList READ deviceList WRITE setDeviceList NOTIFY deviceListChanged) + Q_PROPERTY(QList deviceList READ deviceList WRITE setDeviceList NOTIFY deviceListChanged) public: explicit Conf(QObject *parent = nullptr); - QList deviceList(); - Q_INVOKABLE void setDeviceList(QList deviceList); + QList deviceList(); + Q_INVOKABLE void setDeviceList(QList deviceList); Q_SIGNAL void deviceListChanged(); - + 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_SIGNALS: private: - QList m_deviceList; + QList m_deviceList; + libusb_device* m_device = NULL; + libusb_context* m_ctx = NULL; }; #endif diff --git a/src/main.cpp b/src/main.cpp index 6ba7fa9..1ed89db 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,12 +17,6 @@ int main(int argc, char *argv[]) { - - unsigned char data[520]; - unsigned char data2[520]; - unsigned char deviceData[520]; - memset(deviceData, 0, 520 * sizeof(unsigned char)); - KIconTheme::initTheme(); // this is not available in nixpkgs version of // KiconTheme QApplication app(argc, argv); @@ -42,7 +36,7 @@ int main(int argc, char *argv[]) Conf backend; qmlRegisterSingletonInstance("CustomComponents", 1, 0, "Conf", &backend); - qmlRegisterType("CustomComponents", 1, 0, "Test"); + //qmlRegisterType("CustomComponents", 1, 0, "Test"); engine.rootContext()->setContextObject(new KLocalizedContext(&engine)); engine.loadFromModule("org.kde.tutorial", "Main"); @@ -52,114 +46,6 @@ int main(int argc, char *argv[]) return -1; } - libusb_context *ctx; - int r = libusb_init(&ctx); - if (r < 0) - { - std::cout << libusb_error_name(r) << std::endl; - exit(1); - } - // discover devices - libusb_device **list; - libusb_device *found = NULL; - ssize_t cnt = libusb_get_device_list(ctx, &list); - ssize_t i = 0; - std::cout << cnt << "\n"; - - for (i = 0; i < cnt; i++) - { - libusb_device *device = list[i]; - found = device; - uint8_t addr = libusb_get_device_address(found); - uint8_t bus = libusb_get_bus_number(found); - libusb_device_descriptor desc; - libusb_get_device_descriptor(found, &desc); - std::cout << "Bus " << unsigned(bus) << " Device " << unsigned(addr) << ": ID " << std::hex << desc.idVendor << ":" << desc.idProduct << std::dec << std::endl; - if (desc.idProduct == 0x2f && desc.idVendor == 0x258a) - { - // std::cout << std::hex << (int)(desc.bNumConfigurations) << std::endl; - break; - } - } - - libusb_device_handle *handle; - if ((r = libusb_open(found, &handle)) < 0) - std::cout << "libusb_open: " << libusb_error_name(r) << std::endl; - - if ((r = libusb_detach_kernel_driver(handle, 1)) < 0) - std::cout << "libusb_detach_kernel_driver: " << libusb_error_name(r) << std::endl; - if ((r = libusb_claim_interface(handle, 1)) < 0) - std::cout << "libusb_claim_interface: " << libusb_error_name(r) << std::endl; - - unsigned char send_data0[8]; - memset(send_data0, 0, 8 * sizeof(unsigned char)); - send_data0[0] = 0x05; - send_data0[1] = 0x21; - - // get config before write new config - // conf_1 - if ((r = libusb_control_transfer(handle, 0x21, 0x09, 0x0305, 1, send_data0, 8, 5000)) < 0) - std::cout << "libusb_control_transfer1: " << libusb_error_name(r) << std::endl; - if ((r = libusb_control_transfer(handle, 0xa1, 0x01, 0x0308, 1, deviceData, 520, 5000)) < 0) - std::cout << "libusb_control_transfer2: " << libusb_error_name(r) << std::endl; - - // edit data - memcpy(data, deviceData, 520); - conf_1 *_data = (conf_1 *)data; - _data->light_mode = 0x2; - _data->req_type = 0x92; - _data->st_brightness_colnr = {0x16}; - _data->col_steady[18] = 0x00; - _data->col_steady[19] = 0xff; - _data->col_steady[20] = 0xff; - _data->col_breathing[0] = 0xff; - _data->col_breathing[1] = 0xff; - _data->col_breathing[2] = 0x00; - - memset(send_data0, 0, 8 * sizeof(unsigned char)); - send_data0[0] = 0x05; - send_data0[1] = 0x22; - memset(deviceData, 0, 520 * sizeof(unsigned char)); - - // conf_2 - if ((r = libusb_control_transfer(handle, 0x21, 0x09, 0x0305, 1, send_data0, 8, 5000)) < 0) - std::cout << "libusb_control_transfer3: " << libusb_error_name(r) << std::endl; - if ((r = libusb_control_transfer(handle, 0xa1, 0x01, 0x0308, 1, deviceData, 520, 5000)) < 0) - std::cout << "libusb_control_transfer4: " << libusb_error_name(r) << std::endl; - - // edit data - memcpy(data2, deviceData, 520); - conf_2 *_data2 = (conf_2 *)data2; - _data2->side[3] = {0x21, 0x1, 0x6, 0x0}; - _data2->req_type = 0x50; - - // write new conf - // conf_1 - if ((r = libusb_control_transfer(handle, 0x21, 0x09, 0x0308, 1, data, 520, 5000)) < 0) - std::cout << "libusb_control_transfer5: " << libusb_error_name(r) << std::endl; - // conf_2 - if ((r = libusb_control_transfer(handle, 0x21, 0x09, 0x0308, 1, data2, 520, 5000)) < 0) - std::cout << "libusb_control_transfer6: " << libusb_error_name(r) << std::endl; - - // get config after write new config - // conf_1 - memset(send_data0, 0, 8 * sizeof(unsigned char)); - send_data0[0] = 0x05; - send_data0[1] = 0x21; - memset(deviceData, 0, 520 * sizeof(unsigned char)); - if ((r = libusb_control_transfer(handle, 0x21, 0x09, 0x0305, 1, send_data0, 8, 5000)) < 0) - std::cout << "libusb_control_transfer1: " << libusb_error_name(r) << std::endl; - if ((r = libusb_control_transfer(handle, 0xa1, 0x01, 0x0308, 1, deviceData, 520, 5000)) < 0) - std::cout << "libusb_control_transfer2: " << libusb_error_name(r) << std::endl; - - if ((r = libusb_release_interface(handle, 1)) < 0) - std::cout << "libusb_release_interface: " << libusb_error_name(r) << std::endl; - if ((r = libusb_attach_kernel_driver(handle, 1)) < 0) - std::cout << "libusb_attach_kernel_driver: " << libusb_error_name(r) << std::endl; - - libusb_close(handle); - - libusb_free_device_list(list, 1); return app.exec(); } diff --git a/src/structs.h b/src/structs.h index e958b38..91f86dd 100644 --- a/src/structs.h +++ b/src/structs.h @@ -16,7 +16,7 @@ struct conf_1 uint8_t dpi_modes; // 0x0b, 0x11-0x15 erste 4 welches ist aktiv? dann welhce für conf?? uint8_t __u4; // 0x0c zero uint8_t hdpi[10]; // 0x0 0x0d-0x16 jeweils 2 byte most significant byte last, * 50 -> annäherung an dpi -50 - uint8_t __u5[0x44 - 0x17]; // 0x17 - 0x44 ????? zeros + uint8_t __u5[0x44 - 0x16]; // 0x17 - 0x44 ????? zeros uint8_t light_mode; // 0x45 Lighting Mode, 02 Steady, 01 Colorful Streaming, 03 Breathing uint8_t col_brightness_speed; // 0x46 0-4 Brightness und 0-4 Speed bei colorful 0x(br)(sp) uint8_t __u8; // 0x47 ??? @@ -61,4 +61,9 @@ struct macro { }; +struct conf_12 { + conf_1 c1; + conf_2 c2; +}; + #endif \ No newline at end of file