Blame view

tests/test_list_devices.c 944 Bytes
9e490f23   pifou   recuperation des ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <libusb-1.0/libusb.h>
  #include <stdio.h>
  #include <stdlib.h>
  
  // gcc file.c -l usb-1.0
  
  int main(){
  	// init
  	libusb_context *context;
  	int status = libusb_init(&context);
  	if(status != 0){perror("libusb_init"); exit(-1);}
  
  	// devices list
  	libusb_device **devices;
  	ssize_t devices_count = libusb_get_device_list(context, &devices);
  	if(devices_count < 0){perror("libusb_get_device_list");exit(-1);}
  	for(int i=0; i<devices_count; i++){
  		libusb_device *device = devices[i];
  
  		struct libusb_device_descriptor desc;
  		int status = libusb_get_device_descriptor(device, &desc);
  		if(status != 0) continue;
  
  		uint8_t bus = libusb_get_bus_number(device);
  		uint8_t address = libusb_get_device_address(device);
  
  		printf("Device Found @ (Bus:Address) %d:%d\n", bus, address);
  		printf("Vendor ID 0x0%x\n", desc.idVendor);
  		printf("Product ID 0x0%x\n", desc.idProduct);
  	}
  	libusb_free_device_list(devices, 1);
  
  	libusb_exit(context);
  }