00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <string.h>
00031 #include <unistd.h>
00032 #include <stdio.h>
00033 #include <usb.h>
00034
00035 #define VENDOR 0x054c
00036 #define PRODUCT 0x0268
00037
00038 #define USB_DIR_IN 0x80
00039 #define USB_DIR_OUT 0
00040
00041 void fatal(char *msg) { perror(msg); exit(1); }
00042
00043 void show_master(usb_dev_handle *devh, int itfnum) {
00044 printf("Current Bluetooth master: ");
00045 unsigned char msg[8];
00046 int res = usb_control_msg
00047 (devh, USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
00048 0x01, 0x03f5, itfnum, (void*)msg, sizeof(msg), 5000);
00049 if ( res < 0 ) { perror("USB_REQ_GET_CONFIGURATION"); return; }
00050 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
00051 msg[2], msg[3], msg[4], msg[5], msg[6], msg[7]);
00052 }
00053
00054 void set_master(usb_dev_handle *devh, int itfnum, int mac[6]) {
00055 printf("Setting master bd_addr to %02x:%02x:%02x:%02x:%02x:%02x\n",
00056 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
00057 char msg[8]= { 0x01, 0x00, mac[0],mac[1],mac[2],mac[3],mac[4],mac[5] };
00058 int res = usb_control_msg
00059 (devh,
00060 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
00061 0x09,
00062 0x03f5, itfnum, msg, sizeof(msg),
00063 5000);
00064 if ( res < 0 ) fatal("USB_REQ_SET_CONFIGURATION");
00065 }
00066
00067 void process_device(int argc, char **argv, struct usb_device *dev,
00068 struct usb_config_descriptor *cfg, int itfnum) {
00069 int mac[6];
00070
00071 usb_dev_handle *devh = usb_open(dev);
00072 if ( ! devh ) fatal("usb_open");
00073
00074 usb_detach_kernel_driver_np(devh, itfnum);
00075
00076 int res = usb_claim_interface(devh, itfnum);
00077 if ( res < 0 ) fatal("usb_claim_interface");
00078
00079 show_master(devh, itfnum);
00080
00081 if ( argc >= 2 ) {
00082 if ( sscanf(argv[1], "%x:%x:%x:%x:%x:%x",
00083 &mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]) != 6 ) {
00084
00085 printf("usage: %s [<bd_addr of master>]\n", argv[0]);
00086 exit(1);
00087 }
00088 } else {
00089 FILE *f = popen("hcitool dev", "r");
00090 if ( !f ||
00091 fscanf(f, "%*s\n%*s %x:%x:%x:%x:%x:%x",
00092 &mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]) != 6 ) {
00093 printf("Unable to retrieve local bd_addr from `hcitool dev`.\n");
00094 printf("Please enable Bluetooth or specify an address manually.\n");
00095 exit(1);
00096 }
00097 pclose(f);
00098 }
00099
00100 set_master(devh, itfnum, mac);
00101
00102 usb_close(devh);
00103 }
00104
00105 int main(int argc, char *argv[]) {
00106
00107 usb_init();
00108 if ( usb_find_busses() < 0 ) fatal("usb_find_busses");
00109 if ( usb_find_devices() < 0 ) fatal("usb_find_devices");
00110 struct usb_bus *busses = usb_get_busses();
00111 if ( ! busses ) fatal("usb_get_busses");
00112
00113 int found = 0;
00114
00115 struct usb_bus *bus;
00116 for ( bus=busses; bus; bus=bus->next ) {
00117 struct usb_device *dev;
00118 for ( dev=bus->devices; dev; dev=dev->next) {
00119 struct usb_config_descriptor *cfg;
00120 for ( cfg = dev->config;
00121 cfg < dev->config + dev->descriptor.bNumConfigurations;
00122 ++cfg ) {
00123 int itfnum;
00124 for ( itfnum=0; itfnum<cfg->bNumInterfaces; ++itfnum ) {
00125 struct usb_interface *itf = &cfg->interface[itfnum];
00126 struct usb_interface_descriptor *alt;
00127 for ( alt = itf->altsetting;
00128 alt < itf->altsetting + itf->num_altsetting;
00129 ++alt ) {
00130 if ( dev->descriptor.idVendor == VENDOR &&
00131 dev->descriptor.idProduct == PRODUCT &&
00132 alt->bInterfaceClass == 3 ) {
00133 process_device(argc, argv, dev, cfg, itfnum);
00134 ++found;
00135 }
00136 }
00137 }
00138 }
00139 }
00140 }
00141
00142 if ( ! found ) {
00143 printf("No controller found on USB busses. Please connect your joystick via USB.\n");
00144 return 1;
00145 }
00146
00147 return 0;
00148
00149 }
00150