36 #define PRODUCT 0x0268    38 #define USB_DIR_IN 0x80    41 void fatal(
char *msg) { perror(msg); exit(1); }
    44   printf(
"Current Bluetooth master: ");
    46   int res = usb_control_msg
    47     (devh, 
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
    48      0x01, 0x03f5, itfnum, (
void*)msg, 
sizeof(msg), 5000);
    49   if ( res < 0 ) { perror(
"USB_REQ_GET_CONFIGURATION"); 
return; }
    50   printf(
"%02x:%02x:%02x:%02x:%02x:%02x\n",
    51          msg[2], msg[3], msg[4], msg[5], msg[6], msg[7]);
    54 void set_master(usb_dev_handle *devh, 
int itfnum, 
int mac[6]) {
    55   printf(
"Setting master bd_addr to %02x:%02x:%02x:%02x:%02x:%02x\n",
    56          mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    57   char msg[8]= { 0x01, 0x00, mac[0],mac[1],mac[2],mac[3],mac[4],mac[5] };
    58   int res = usb_control_msg
    62      0x03f5, itfnum, msg, 
sizeof(msg),
    64   if ( res < 0 ) 
fatal(
"USB_REQ_SET_CONFIGURATION");
    68                     struct usb_config_descriptor *cfg, 
int itfnum) {
    71   usb_dev_handle *devh = usb_open(dev);
    72   if ( ! devh ) 
fatal(
"usb_open");
    74   usb_detach_kernel_driver_np(devh, itfnum);
    76   int res = usb_claim_interface(devh, itfnum);
    77   if ( res < 0 ) 
fatal(
"usb_claim_interface");
    82     if ( sscanf(argv[1], 
"%x:%x:%x:%x:%x:%x",
    83                 &mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]) != 6 ) {
    85       printf(
"usage: %s [<bd_addr of master>]\n", argv[0]);
    89     FILE *
f = popen(
"hcitool dev", 
"r");
    91          fscanf(f, 
"%*s\n%*s %x:%x:%x:%x:%x:%x",
    92                 &mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]) != 6 ) {
    93       printf(
"Unable to retrieve local bd_addr from `hcitool dev`.\n");
    94       printf(
"Please enable Bluetooth or specify an address manually.\n");
   105 int main(
int argc, 
char *argv[]) {  
   108   if ( usb_find_busses() < 0 ) 
fatal(
"usb_find_busses");
   109   if ( usb_find_devices() < 0 ) 
fatal(
"usb_find_devices");
   110   struct usb_bus *busses = usb_get_busses();
   111   if ( ! busses ) 
fatal(
"usb_get_busses");
   116   for ( bus=busses; bus; bus=bus->next ) {
   117     struct usb_device *dev;
   118     for ( dev=bus->devices; dev; dev=dev->next) {
   119       struct usb_config_descriptor *cfg;
   120       for ( cfg = dev->config;
   121             cfg < dev->config + dev->descriptor.bNumConfigurations;
   124         for ( itfnum=0; itfnum<cfg->bNumInterfaces; ++itfnum ) {
   125           struct usb_interface *itf = &cfg->interface[itfnum];
   126           struct usb_interface_descriptor *alt;
   127           for ( alt = itf->altsetting;
   128                 alt < itf->altsetting + itf->num_altsetting;
   130             if ( dev->descriptor.idVendor == 
VENDOR &&
   131                  dev->descriptor.idProduct == 
PRODUCT &&
   132                  alt->bInterfaceClass == 3 ) {
   143     printf(
"No controller found on USB busses. Please connect your joystick via USB.\n");
 void set_master(usb_dev_handle *devh, int itfnum, int mac[6])
int main(int argc, char *argv[])
void show_master(usb_dev_handle *devh, int itfnum)
void process_device(int argc, char **argv, struct usb_device *dev, struct usb_config_descriptor *cfg, int itfnum)