Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include <stdio.h>
- #include <stdlib.h>
- #include <libusb-1.0/libusb.h>
- #define IDVENDOR 0x275d;
- #define IDPRODUCT 0x0ba6;
- /*
- *
- */
- int main(int argc, char** argv) {
- int product =(int) 0x0ba6;
- int vendor = (int) 0x275d;
- int numInterface = 0;
- int endAdress = (int)0x81;
- char* data = malloc(5);
- int flagInit = libusb_init(NULL); //инициализация
- if(flagInit != 0 ){ //?
- printf("%d \n",libusb_init);
- exit(1);
- }
- libusb_device ** list; //объяв список устройств
- int count = libusb_get_device_list(NULL, &list);// заносим в список устройст и считаем их
- printf("%d",count);
- struct libusb_device_descriptor deviceDesk;//создаем дискриптор одного устройства
- int num = 0;
- for(int i= 0; i<count; i++){
- int err = libusb_get_device_descriptor(list[i],&deviceDesk);//берется дескриптор из каждого дев
- if(err != 0){ printf("Ошибка получения дескриптора устройства\n"); exit(1); }
- if(deviceDesk.idProduct==product && deviceDesk.idVendor == vendor){
- num = i;// сохраняемэлемент
- i=count;
- }
- // printf(" \n0x%x , 0x%x \n",deviceDesk.idProduct,deviceDesk.idVendor);
- }
- printf(" \n0x%x , 0x%x \n",deviceDesk.idProduct,deviceDesk.idVendor);
- libusb_device_handle *dev_handle;//создаем ссылку на структуру доступа
- int err = libusb_open(list[num], &dev_handle);//получение структуры доступа
- if(err != 0){ printf("Ошибка открытия устройства. Код: %i\n", err); exit(1); }
- libusb_free_device_list(list,0);//очищаем список устройств
- libusb_device *device = libusb_get_device(dev_handle); //создаем структуру устройства через стр-ру доступа
- //Конфигурация
- struct libusb_config_descriptor *configDsc;
- int flagReadConfig = libusb_get_active_config_descriptor(device, &configDsc);// получаем дескриптор конфиг
- if(flagReadConfig!=0){
- printf("Error:%d \n",flagReadConfig);
- exit(1);
- }
- //printf("VID %x PID %x\n", );
- printf("MaxPower: %d mA \n",configDsc->MaxPower*2);
- err = libusb_get_device_descriptor(dev, &desc);
- if (err < 0){
- exit(1);
- }else{
- unsigned char vend[100];
- unsigned char product[100];
- int vLen = libusb_get_string_descriptor_ascii(dev_handle, desc.iManufacturer,vend,100);
- int pLen = libusb_get_string_descriptor_ascii(dev_handle, desc.iProduct,product,100);
- hexDump(vend, vLen);
- hexDump(product, pLen);
- }
- //ЗАДАНИЕ2
- if(libusb_kernel_driver_active(dev_handle,numInterface) == 1)// проверка
- { //отсоед устр-во от ядра по структуре доступа
- int err = libusb_detach_kernel_driver(dev_handle,numInterface);//отключаем от ядра
- //printf("Err : %d", err);
- if(err !=0)
- {
- printf("Error detach: %d",err);
- exit(err);
- }
- }
- if(libusb_claim_interface(dev_handle,numInterface)!= 0){
- printf("Устройство не захвачено \n");
- exit(1);
- }
- printf("Устройство захвачено \n");
- int absolWheel = 0; //абсолют смещ
- do{
- int *transferred;//количество перед бат
- int dWheel = 0;//относ
- err =libusb_interrupt_transfer(dev_handle,endAdress,data,5,&transferred,0);//функция передачи
- if(err==0){
- dWheel = data[4] & 0xf;
- dWheel = dWheel > 7 ? dWheel - 0x10 : dWheel;
- }
- else{
- printf("Erorr: %d \n",err);
- }
- // printf("\n %d \n", dWheel);
- absolWheel+=dWheel;
- printf("%d \n",absolWheel);
- }
- while(data[0]!=0x02);
- // printf("%d \n",absolWheel);
- if(libusb_release_interface (dev_handle, numInterface)!=0){
- printf("Устройство не освобождено \n");
- exit(1);
- }
- printf("Устройство освобождено \n");
- err = libusb_attach_kernel_driver(dev_handle,numInterface);//подключаем к ядру
- if(err !=0)
- {
- printf("Error attach: %d",err);
- exit(err);
- }
- libusb_close (dev_handle);
- libusb_exit(NULL);
- return (EXIT_SUCCESS);
- }
- void hexDump (void *addr, int len) {
- int i;
- unsigned char buff[17];
- unsigned char *pc = (unsigned char*)addr;
- if (len == 0) {
- printf(" ZERO LENGTH\n");
- return;
- }
- if (len < 0) {
- printf(" NEGATIVE LENGTH: %i\n",len);
- return;
- }
- for (i = 0; i < len; i++) {
- if ((i % 16) == 0) {
- if (i != 0)
- printf (" %s\n", buff);
- printf (" %04x ", i);
- }
- printf (" %02x", pc[i]);
- if ((pc[i] < 0x20) || (pc[i] > 0x7e))
- buff[i % 16] = '.';
- else
- buff[i % 16] = pc[i];
- buff[(i % 16) + 1] = '\0';
- }
- while ((i % 16) != 0) {
- printf (" ");
- i++;
- }
- printf (" %s\n", buff);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement