sixpair.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, 2008 pascal@pabr.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <string.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <usb.h>
34 
35 #define VENDOR 0x054c
36 #define PRODUCT 0x0268
37 
38 #define USB_DIR_IN 0x80
39 #define USB_DIR_OUT 0
40 
41 void fatal(char *msg) { perror(msg); exit(1); }
42 
43 void show_master(usb_dev_handle *devh, int itfnum) {
44  printf("Current Bluetooth master: ");
45  unsigned char msg[8];
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]);
52 }
53 
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
59  (devh,
60  USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
61  0x09,
62  0x03f5, itfnum, msg, sizeof(msg),
63  5000);
64  if ( res < 0 ) fatal("USB_REQ_SET_CONFIGURATION");
65 }
66 
67 void process_device(int argc, char **argv, struct usb_device *dev,
68  struct usb_config_descriptor *cfg, int itfnum) {
69  int mac[6];
70 
71  usb_dev_handle *devh = usb_open(dev);
72  if ( ! devh ) fatal("usb_open");
73 
74  usb_detach_kernel_driver_np(devh, itfnum);
75 
76  int res = usb_claim_interface(devh, itfnum);
77  if ( res < 0 ) fatal("usb_claim_interface");
78 
79  show_master(devh, itfnum);
80 
81  if ( argc >= 2 ) {
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 ) {
84 
85  printf("usage: %s [<bd_addr of master>]\n", argv[0]);
86  exit(1);
87  }
88  } else {
89  FILE *f = popen("hcitool dev", "r");
90  if ( !f ||
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");
95  exit(1);
96  }
97  pclose(f);
98  }
99 
100  set_master(devh, itfnum, mac);
101 
102  usb_close(devh);
103 }
104 
105 int main(int argc, char *argv[]) {
106 
107  usb_init();
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");
112 
113  int found = 0;
114 
115  struct usb_bus *bus;
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;
122  ++cfg ) {
123  int itfnum;
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;
129  ++alt ) {
130  if ( dev->descriptor.idVendor == VENDOR &&
131  dev->descriptor.idProduct == PRODUCT &&
132  alt->bInterfaceClass == 3 ) {
133  process_device(argc, argv, dev, cfg, itfnum);
134  ++found;
135  }
136  }
137  }
138  }
139  }
140  }
141 
142  if ( ! found ) {
143  printf("No controller found on USB busses. Please connect your joystick via USB.\n");
144  return 1;
145  }
146 
147  return 0;
148 
149 }
150 
void set_master(usb_dev_handle *devh, int itfnum, int mac[6])
Definition: sixpair.c:54
#define USB_DIR_IN
Definition: sixpair.c:38
f
void fatal(char *msg)
Definition: sixpair.c:41
int main(int argc, char *argv[])
Definition: sixpair.c:105
#define USB_DIR_OUT
Definition: sixpair.c:39
void show_master(usb_dev_handle *devh, int itfnum)
Definition: sixpair.c:43
void process_device(int argc, char **argv, struct usb_device *dev, struct usb_config_descriptor *cfg, int itfnum)
Definition: sixpair.c:67
#define PRODUCT
Definition: sixpair.c:36
#define VENDOR
Definition: sixpair.c:35


ps3joy
Author(s): Blaise Gassend, pascal@pabr.org, Melonee Wise
autogenerated on Mon Jun 10 2019 13:42:41