1
0
Fork 0
forked from denis/a811

Something

This commit is contained in:
Denis Manherz 2024-08-29 15:02:48 +02:00
parent 5dfb798abd
commit de6ce3178f
7 changed files with 293 additions and 144 deletions

BIN
conf1.bin Normal file

Binary file not shown.

BIN
conf2.bin Normal file

Binary file not shown.

BIN
person.bin Normal file

Binary file not shown.

View file

@ -2,41 +2,65 @@
#include <iostream> #include <iostream>
#include <QDebug> #include <QDebug>
Conf::Conf(QObject *parent) : QObject(parent) {} Conf::Conf(QObject *parent) : QObject(parent)
QList<QObject *> Conf::deviceList()
{ {
libusb_context *ctx; int r = libusb_init(&m_ctx);
int r = libusb_init(&ctx);
if (r < 0) if (r < 0)
{ {
std::cout << libusb_error_name(r) << std::endl; std::cout << libusb_error_name(r) << std::endl;
exit(1); exit(1);
} }
libusb_device **list; libusb_device **list;
libusb_device *found = NULL; ssize_t cnt = libusb_get_device_list(m_ctx, &list);
ssize_t cnt = libusb_get_device_list(ctx, &list);
QQmlEngine engine;
QList<QObject *> asd;
for (ssize_t i = 0; i < cnt; i++) for (ssize_t i = 0; i < cnt; i++)
{ {
libusb_device *device = list[i]; libusb_device *device = list[i];
found = device; m_device = device;
uint8_t addr = libusb_get_device_address(found); uint8_t addr = libusb_get_device_address(m_device);
uint8_t bus = libusb_get_bus_number(found); uint8_t bus = libusb_get_bus_number(m_device);
libusb_device_descriptor desc; libusb_device_descriptor desc;
libusb_get_device_descriptor(found, &desc); libusb_get_device_descriptor(m_device, &desc);
QString formattedString = QString::asprintf("%04X:%04X", desc.idVendor, desc.idProduct); if (desc.idProduct == 0x2f && desc.idVendor == 0x258a)
{
break;
}
}
libusb_free_device_list(list, 1);
}
QList<QObject *> 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<QObject *> asd;
QQmlComponent component(&engine, QQmlComponent component(&engine,
QUrl::fromLocalFile(QString::fromStdString("/home/denis/repos/a811/src/action.qml"))); QUrl::fromLocalFile(QString::fromStdString("/home/denis/repos/a811/src/action.qml")));
QObject *object = component.create(); 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); asd.append(object);
}
return asd; return asd;
} }
@ -45,3 +69,222 @@ void Conf::setDeviceList(QList<QObject *> deviceList)
{ {
Q_EMIT deviceListChanged(); 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;
}

View file

@ -3,12 +3,18 @@
#include <QObject> #include <QObject>
#include <QtQml> #include <QtQml>
#include <libusb-1.0/libusb.h>
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <QQuickView> #include <QQuickView>
#include <QQmlComponent> #include <QQmlComponent>
#include <QList> #include <QList>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include "structs.h"
#include <libusb-1.0/libusb.h>
#define CONF_FILE "conf.bin"
class Conf : public QObject class Conf : public QObject
{ {
@ -20,12 +26,21 @@ public:
QList<QObject *> deviceList(); QList<QObject *> deviceList();
Q_INVOKABLE void setDeviceList(QList<QObject *> deviceList); Q_INVOKABLE void setDeviceList(QList<QObject *> deviceList);
Q_SIGNAL void deviceListChanged(); 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: Q_SIGNALS:
private: private:
QList<QObject *> m_deviceList; QList<QObject *> m_deviceList;
libusb_device* m_device = NULL;
libusb_context* m_ctx = NULL;
}; };
#endif #endif

View file

@ -17,12 +17,6 @@
int main(int argc, char *argv[]) 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::initTheme(); // this is not available in nixpkgs version of
// KiconTheme // KiconTheme
QApplication app(argc, argv); QApplication app(argc, argv);
@ -42,7 +36,7 @@ int main(int argc, char *argv[])
Conf backend; Conf backend;
qmlRegisterSingletonInstance<Conf>("CustomComponents", 1, 0, "Conf", &backend); qmlRegisterSingletonInstance<Conf>("CustomComponents", 1, 0, "Conf", &backend);
qmlRegisterType<Conf>("CustomComponents", 1, 0, "Test"); //qmlRegisterType<Conf>("CustomComponents", 1, 0, "Test");
engine.rootContext()->setContextObject(new KLocalizedContext(&engine)); engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
engine.loadFromModule("org.kde.tutorial", "Main"); engine.loadFromModule("org.kde.tutorial", "Main");
@ -52,114 +46,6 @@ int main(int argc, char *argv[])
return -1; 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(); return app.exec();
} }

View file

@ -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 dpi_modes; // 0x0b, 0x11-0x15 erste 4 welches ist aktiv? dann welhce für conf??
uint8_t __u4; // 0x0c zero 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 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 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 col_brightness_speed; // 0x46 0-4 Brightness und 0-4 Speed bei colorful 0x(br)(sp)
uint8_t __u8; // 0x47 ??? uint8_t __u8; // 0x47 ???
@ -61,4 +61,9 @@ struct macro {
}; };
struct conf_12 {
conf_1 c1;
conf_2 c2;
};
#endif #endif