attachment:sixpair.c of Sixaxis


Attachment 'sixpair.c'

Download

Toggle line numbers
   1 /*
   2  * sixpair.c version 2007-04-18
   3  * Compile with: gcc -o sixpair sixpair.c -lusb
   4  */
   5 
   6 #include <string.h>
   7 #include <unistd.h>
   8 #include <stdio.h>
   9 #include <usb.h>
  10 
  11 #define VENDOR 0x054c
  12 #define PRODUCT 0x0268
  13 
  14 #define USB_DIR_IN 0x80
  15 #define USB_DIR_OUT 0
  16 
  17 void fatal(char *msg) { perror(msg); exit(1); }
  18 
  19 void show_master(usb_dev_handle *devh, int itfnum) {
  20   printf("Current Bluetooth master: ");
  21   unsigned char msg[8];
  22   int res = usb_control_msg
  23     (devh, USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  24      0x01, 0x03f5, itfnum, (void*)msg, sizeof(msg), 5000);
  25   if ( res < 0 ) { perror("USB_REQ_GET_CONFIGURATION"); return; }
  26   printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  27 	 msg[2], msg[3], msg[4], msg[5], msg[6], msg[7]);
  28 }
  29 
  30 void set_master(usb_dev_handle *devh, int itfnum, int mac[6]) {
  31   printf("Setting master bd_addr to %02x:%02x:%02x:%02x:%02x:%02x\n",
  32 	 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  33   char msg[8]= { 0x01, 0x00, mac[0],mac[1],mac[2],mac[3],mac[4],mac[5] };
  34   int res = usb_control_msg
  35     (devh,
  36      USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  37      0x09,
  38      0x03f5, itfnum, msg, sizeof(msg),
  39      5000);
  40   if ( res < 0 ) fatal("USB_REQ_SET_CONFIGURATION");
  41 }
  42 
  43 void process_device(int argc, char **argv, struct usb_device *dev,
  44 		    struct usb_config_descriptor *cfg, int itfnum) {
  45   int mac[6], have_mac=0;
  46 
  47   usb_dev_handle *devh = usb_open(dev);
  48   if ( ! devh ) fatal("usb_open");
  49 
  50   usb_detach_kernel_driver_np(devh, itfnum);
  51 
  52   int res = usb_claim_interface(devh, itfnum);
  53   if ( res < 0 ) fatal("usb_claim_interface");
  54 
  55   show_master(devh, itfnum);
  56 
  57   if ( argc >= 2 ) {
  58     if ( sscanf(argv[1], "%x:%x:%x:%x:%x:%x",
  59 		&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]) != 6 ) {
  60 
  61       printf("usage: %s [<bd_addr of master>]\n", argv[0]);
  62       exit(1);
  63     }
  64   } else {
  65     FILE *f = popen("hcitool dev", "r");
  66     if ( !f ||
  67 	 fscanf(f, "%*s\n%*s %x:%x:%x:%x:%x:%x",
  68 		&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]) != 6 ) {
  69       printf("Unable to retrieve local bd_addr from `hcitool dev`.\n");
  70       printf("Please enable Bluetooth or specify an address manually.\n");
  71       exit(1);
  72     }
  73     pclose(f);
  74   }
  75     
  76   set_master(devh, itfnum, mac);
  77 
  78   usb_close(devh);
  79 }
  80 
  81 int main(int argc, char *argv[]) {  
  82 
  83   usb_init();
  84   if ( usb_find_busses() < 0 ) fatal("usb_find_busses");
  85   if ( usb_find_devices() < 0 ) fatal("usb_find_devices");
  86   struct usb_bus *busses = usb_get_busses();
  87   if ( ! busses ) fatal("usb_get_busses");
  88 
  89   int found = 0;
  90 
  91   struct usb_bus *bus;
  92   for ( bus=busses; bus; bus=bus->next ) {
  93     struct usb_device *dev;
  94     for ( dev=bus->devices; dev; dev=dev->next) {
  95       struct usb_config_descriptor *cfg;
  96       for ( cfg = dev->config;
  97 	    cfg < dev->config + dev->descriptor.bNumConfigurations;
  98 	    ++cfg ) {
  99 	int itfnum;
 100 	for ( itfnum=0; itfnum<cfg->bNumInterfaces; ++itfnum ) {
 101 	  struct usb_interface *itf = &cfg->interface[itfnum];
 102 	  struct usb_interface_descriptor *alt;
 103 	  for ( alt = itf->altsetting;
 104 		alt < itf->altsetting + itf->num_altsetting;
 105 		++alt ) {
 106 	    if ( dev->descriptor.idVendor == VENDOR &&
 107 		 dev->descriptor.idProduct == PRODUCT &&
 108 		 alt->bInterfaceClass == 3 ) {
 109 	      process_device(argc, argv, dev, cfg, itfnum);
 110 	      ++found;
 111 	    }
 112 	  }
 113 	}
 114       }
 115     }
 116   }
 117 
 118   if ( ! found ) printf("No controller found on USB busses.\n");
 119   return 0;
 120 
 121 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-05-19 16:53:52, 3.3 KB) [[attachment:sixpair.c]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.