USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
BTD.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
2 
3  This software may be distributed and modified under the terms of the GNU
4  General Public License version 2 (GPL2) as published by the Free Software
5  Foundation and appearing in the file GPL2.TXT included in the packaging of
6  this file. Please note that GPL2 Section 2[b] requires that all works based
7  on this software must also be made publicly available under the terms of
8  the GPL2 ("Copyleft").
9 
10  Contact information
11  -------------------
12 
13  Kristian Lauszus, TKJ Electronics
14  Web : http://www.tkjelectronics.com
15  e-mail : kristianl@tkjelectronics.com
16  */
17 
18 #include "BTD.h"
19 #define DEBUG // Uncomment to print data for debugging
20 //#define EXTRADEBUG // Uncomment to get even more debugging data
21 
22 const uint8_t BTD::BTD_CONTROL_PIPE = 0;
23 const uint8_t BTD::BTD_EVENT_PIPE = 1;
24 const uint8_t BTD::BTD_DATAIN_PIPE = 2;
25 const uint8_t BTD::BTD_DATAOUT_PIPE = 3;
26 
28 pUsb(p), // Pointer to USB class instance - mandatory
29 bAddress(0), // Device address - mandatory
30 bNumEP(1), // If config descriptor needs to be parsed
31 qNextPollTime(0), // Reset NextPollTime
32 bPollEnable(false) // Don't start polling before dongle is connected
33 {
34  for(uint8_t i=0; i<BTD_MAX_ENDPOINTS; i++) {
35  epInfo[i].epAddr = 0;
36  epInfo[i].maxPktSize = (i) ? 0 : 8;
37  epInfo[i].epAttribs = 0;
39  }
40 
41  if (pUsb) // register in USB subsystem
42  pUsb->RegisterDeviceClass(this); //set devConfig[] entry
43 }
44 
45 uint8_t BTD::Init(uint8_t parent, uint8_t port, bool lowspeed) {
46  uint8_t buf[sizeof(USB_DEVICE_DESCRIPTOR)];
47  uint8_t rcode;
48  UsbDevice *p = NULL;
49  EpInfo *oldep_ptr = NULL;
50  uint8_t num_of_conf; // number of configurations
51  uint16_t PID;
52  uint16_t VID;
53 
54  // get memory address of USB device address pool
55  AddressPool &addrPool = pUsb->GetAddressPool();
56 #ifdef EXTRADEBUG
57  Notify(PSTR("\r\nBTD Init"));
58 #endif
59  // check if address has already been assigned to an instance
60  if (bAddress) {
61 #ifdef DEBUG
62  Notify(PSTR("\r\nAddress in use"));
63 #endif
65  }
66 
67  // Get pointer to pseudo device with address 0 assigned
68  p = addrPool.GetUsbDevicePtr(0);
69 
70  if (!p) {
71 #ifdef DEBUG
72  Notify(PSTR("\r\nAddress not found"));
73 #endif
75  }
76 
77  if (!p->epinfo) {
78 #ifdef DEBUG
79  Notify(PSTR("\r\nepinfo is null"));
80 #endif
82  }
83 
84  // Save old pointer to EP_RECORD of address 0
85  oldep_ptr = p->epinfo;
86 
87  // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
88  p->epinfo = epInfo;
89 
90  p->lowspeed = lowspeed;
91 
92  // Get device descriptor
93  rcode = pUsb->getDevDescr(0, 0, sizeof(USB_DEVICE_DESCRIPTOR), (uint8_t*)buf);// Get device descriptor - addr, ep, nbytes, data
94 
95  // Restore p->epinfo
96  p->epinfo = oldep_ptr;
97 
98  if(rcode)
99  goto FailGetDevDescr;
100 
101  // Allocate new address according to device class
102  bAddress = addrPool.AllocAddress(parent, false, port);
103 
104  if (!bAddress)
106 
107  // Extract Max Packet Size from device descriptor
108  epInfo[0].maxPktSize = (uint8_t)((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0;
109 
110  // Assign new address to the device
111  rcode = pUsb->setAddr( 0, 0, bAddress );
112  if (rcode) {
113  p->lowspeed = false;
114  addrPool.FreeAddress(bAddress);
115  bAddress = 0;
116 #ifdef DEBUG
117  Notify(PSTR("\r\nsetAddr: "));
118 #endif
119  PrintHex<uint8_t>(rcode);
120  return rcode;
121  }
122 #ifdef EXTRADEBUG
123  Notify(PSTR("\r\nAddr: "));
124  PrintHex<uint8_t>(bAddress);
125 #endif
126  p->lowspeed = false;
127 
128  //get pointer to assigned address record
129  p = addrPool.GetUsbDevicePtr(bAddress);
130  if (!p)
132 
133  p->lowspeed = lowspeed;
134 
135  // Assign epInfo to epinfo pointer - only EP0 is known
136  rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
137  if (rcode)
138  goto FailSetDevTblEntry;
139  VID = ((USB_DEVICE_DESCRIPTOR*)buf)->idVendor;
140  PID = ((USB_DEVICE_DESCRIPTOR*)buf)->idProduct;
141 
142  if(VID == PS3_VID && (PID == PS3_PID || PID == PS3NAVIGATION_PID || PID == PS3MOVE_PID)) {
143  /* We only need the Control endpoint, so we don't have to initialize the other endpoints of device */
144  rcode = pUsb->setConf(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, 1);
145  if( rcode )
146  goto FailSetConf;
147 
148  if(PID == PS3_PID || PID == PS3NAVIGATION_PID) {
149 #ifdef DEBUG
150  if(PID == PS3_PID)
151  Notify(PSTR("\r\nDualshock 3 Controller Connected"));
152  else // must be a navigation controller
153  Notify(PSTR("\r\nNavigation Controller Connected"));
154 #endif
155  /* Set internal bluetooth address */
156  setBdaddr(my_bdaddr);
157  }
158  else { // must be a Motion controller
159 #ifdef DEBUG
160  Notify(PSTR("\r\nMotion Controller Connected"));
161 #endif
162  setMoveBdaddr(my_bdaddr);
163  }
164  rcode = pUsb->setConf(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, 0); // Reset configuration value
165  pUsb->setAddr(bAddress, 0, 0); // Reset address
166  Release(); // Release device
168  }
169  else {
170  num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations;
171 
172  // check if attached device is a Bluetooth dongle and fill endpoint data structure
173  // first interface in the configuration must have Bluetooth assigned Class/Subclass/Protocol
174  // and 3 endpoints - interrupt-IN, bulk-IN, bulk-OUT,
175  // not necessarily in this order
176  for (uint8_t i=0; i<num_of_conf; i++) {
178  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
179  if(rcode)
180  goto FailGetConfDescr;
181  if(bNumEP >= BTD_MAX_ENDPOINTS) // All endpoints extracted
182  break;
183  }
184 
186  goto FailUnknownDevice;
187 
188  // Assign epInfo to epinfo pointer - this time all 3 endpoins
190  if(rcode)
191  goto FailSetDevTblEntry;
192 
193  delay(200); // Give time for address change
194 
195  // Set Configuration Value
196  rcode = pUsb->setConf(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bConfNum);
197  if(rcode)
198  goto FailSetConf;
199 
200  hci_num_reset_loops = 100; // only loop 100 times before trying to send the hci reset command
201  hci_counter = 0;
202  hci_state = HCI_INIT_STATE;
203  watingForConnection = false;
204  bPollEnable = true;
205 
206 #ifdef DEBUG
207  Notify(PSTR("\r\nBluetooth Dongle Initialized"));
208 #endif
209  }
210  return 0; // Successful configuration
211 
212  /* diagnostic messages */
213 FailGetDevDescr:
214 #ifdef DEBUG
215  Notify(PSTR("\r\ngetDevDescr"));
216 #endif
217  goto Fail;
218 FailSetDevTblEntry:
219 #ifdef DEBUG
220  Notify(PSTR("\r\nsetDevTblEn"));
221 #endif
222  goto Fail;
223 FailGetConfDescr:
224 #ifdef DEBUG
225  Notify(PSTR("\r\ngetConf"));
226 #endif
227  goto Fail;
228 FailSetConf:
229 #ifdef DEBUG
230  Notify(PSTR("\r\nsetConf"));
231 #endif
232  goto Fail;
233 FailUnknownDevice:
234 #ifdef DEBUG
235  Notify(PSTR("\r\nUnknown Device Connected - VID: "));
236  PrintHex<uint16_t>(VID);
237  Notify(PSTR(" PID: "));
238  PrintHex<uint16_t>(PID);
239 #endif
240  pUsb->setAddr(bAddress, 0, 0); // Reset address
242  goto Fail;
243 Fail:
244 #ifdef DEBUG
245  Notify(PSTR("\r\nBTD Init Failed, error code: "));
246  Serial.print(rcode);
247 #endif
248  Release();
249  return rcode;
250 }
251 /* Extracts interrupt-IN, bulk-IN, bulk-OUT endpoint information from config descriptor */
252 void BTD::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) {
253  //ErrorMessage<uint8_t>(PSTR("Conf.Val"),conf);
254  //ErrorMessage<uint8_t>(PSTR("Iface Num"),iface);
255  //ErrorMessage<uint8_t>(PSTR("Alt.Set"),alt);
256 
257  if(alt) // wrong interface - by BT spec, no alt setting
258  return;
259 
260  bConfNum = conf;
261  uint8_t index;
262 
263  if ((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80) // Interrupt In endpoint found
264  index = BTD_EVENT_PIPE;
265 
266  else {
267  if ((pep->bmAttributes & 0x02) == 2) // bulk endpoint found
268  index = ((pep->bEndpointAddress & 0x80) == 0x80) ? BTD_DATAIN_PIPE : BTD_DATAOUT_PIPE;
269  else
270  return;
271  }
272 
273  // Fill the rest of endpoint data structure
274  epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
275  epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize;
276 #ifdef EXTRADEBUG
278 #endif
279  if(pollInterval < pep->bInterval) // Set the polling interval as the largest polling interval obtained from endpoints
280  pollInterval = pep->bInterval;
281  bNumEP++;
282 }
284 #ifdef EXTRADEBUG
285  Notify(PSTR("\r\nEndpoint descriptor:"));
286  Notify(PSTR("\r\nLength:\t\t"));
287  PrintHex<uint8_t>(ep_ptr->bLength);
288  Notify(PSTR("\r\nType:\t\t"));
289  PrintHex<uint8_t>(ep_ptr->bDescriptorType);
290  Notify(PSTR("\r\nAddress:\t"));
291  PrintHex<uint8_t>(ep_ptr->bEndpointAddress);
292  Notify(PSTR("\r\nAttributes:\t"));
293  PrintHex<uint8_t>(ep_ptr->bmAttributes);
294  Notify(PSTR("\r\nMaxPktSize:\t"));
295  PrintHex<uint16_t>(ep_ptr->wMaxPacketSize);
296  Notify(PSTR("\r\nPoll Intrv:\t"));
297  PrintHex<uint8_t>(ep_ptr->bInterval);
298 #endif
299 }
300 
301 /* Performs a cleanup after failed Init() attempt */
302 uint8_t BTD::Release() {
303  for (uint8_t i=0; i<BTD_NUMSERVICES; i++)
304  if (btService[i])
305  btService[i]->Reset(); // Reset all Bluetooth services
307  bAddress = 0;
308  bPollEnable = false;
309  bNumEP = 1; // must have to be reset to 1
310  return 0;
311 }
312 uint8_t BTD::Poll() {
313  if (!bPollEnable)
314  return 0;
315  if (qNextPollTime <= millis()) { // Don't poll if shorter than polling interval
316  qNextPollTime = millis() + pollInterval; // Set new poll time
317  HCI_event_task(); // poll the HCI event pipe
318  ACL_event_task(); // start polling the ACL input pipe too, though discard data until connected
319  }
320  return 0;
321 }
322 
323 void BTD::HCI_event_task() {
324  /* check the event pipe*/
325  uint16_t MAX_BUFFER_SIZE = BULK_MAXPKTSIZE; // Request more than 16 bytes anyway, the inTransfer routine will take care of this
326  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[ BTD_EVENT_PIPE ].epAddr, &MAX_BUFFER_SIZE, hcibuf); // input on endpoint 1
327  if(!rcode || rcode == hrNAK) // Check for errors
328  {
329  switch (hcibuf[0]) //switch on event type
330  {
331  case EV_COMMAND_COMPLETE:
332  if (!hcibuf[5]) { // Check if command succeeded
333  hci_event_flag |= HCI_FLAG_CMD_COMPLETE; // set command complete flag
334  if((hcibuf[3] == 0x01) && (hcibuf[4] == 0x10)) { // parameters from read local version information
335  hci_version = hcibuf[6]; // Used to check if it supports 2.0+EDR - see http://www.bluetooth.org/Technical/AssignedNumbers/hci.htm
336  hci_event_flag |= HCI_FLAG_READ_VERSION;
337  } else if((hcibuf[3] == 0x09) && (hcibuf[4] == 0x10)) { // parameters from read local bluetooth address
338  for (uint8_t i = 0; i < 6; i++)
339  my_bdaddr[i] = hcibuf[6 + i];
340  hci_event_flag |= HCI_FLAG_READ_BDADDR;
341  }
342  }
343  break;
344 
345  case EV_COMMAND_STATUS:
346  if(hcibuf[2]) { // show status on serial if not OK
347 #ifdef DEBUG
348  Notify(PSTR("\r\nHCI Command Failed: "));
349  PrintHex<uint8_t>(hcibuf[2]);
350  Notify(PSTR(" "));
351  PrintHex<uint8_t>(hcibuf[4]);
352  Notify(PSTR(" "));
353  PrintHex<uint8_t>(hcibuf[5]);
354 #endif
355  }
356  break;
357 
358  case EV_INQUIRY_COMPLETE:
359  if(inquiry_counter >= 5) {
360  inquiry_counter = 0;
361 #ifdef DEBUG
362  Notify(PSTR("\r\nCouldn't find Wiimote"));
363 #endif
364  connectToWii = false;
365  pairWithWii = false;
366  hci_state = HCI_SCANNING_STATE;
367  }
368  inquiry_counter++;
369  break;
370 
371  case EV_INQUIRY_RESULT:
372  if (hcibuf[2]) { // Check that there is more than zero responses
373 #ifdef EXTRADEBUG
374  Notify(PSTR("\r\nNumber of responses: "));
375  Serial.print(hcibuf[2]);
376 #endif
377  for(uint8_t i = 0; i < hcibuf[2]; i++) {
378  if((hcibuf[4+8*hcibuf[2]+3*i] == 0x04 && hcibuf[5+8*hcibuf[2]+3*i] == 0x25 && hcibuf[6+8*hcibuf[2]+3*i] == 0x00) || (hcibuf[4+8*hcibuf[2]+3*i] == 0x08 && hcibuf[5+8*hcibuf[2]+3*i] == 0x05 && hcibuf[6+8*hcibuf[2]+3*i] == 0x00)) { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html and http://wiibrew.org/wiki/Wiimote#SDP_information
379  if(hcibuf[4+8*hcibuf[2]+3*i] == 0x08) // Check if it's the new Wiimote with motion plus inside that was detected
380  motionPlusInside = true;
381  else
382  motionPlusInside = false;
383  disc_bdaddr[0] = hcibuf[3+6*i];
384  disc_bdaddr[1] = hcibuf[4+6*i];
385  disc_bdaddr[2] = hcibuf[5+6*i];
386  disc_bdaddr[3] = hcibuf[6+6*i];
387  disc_bdaddr[4] = hcibuf[7+6*i];
388  disc_bdaddr[5] = hcibuf[8+6*i];
389  hci_event_flag |= HCI_FLAG_WII_FOUND;
390  break;
391  }
392 #ifdef EXTRADEBUG
393  else {
394  Notify(PSTR("\r\nClass of device: "));
395  PrintHex<uint8_t>(hcibuf[6+8*hcibuf[2]+3*i]);
396  Notify(PSTR(" "));
397  PrintHex<uint8_t>(hcibuf[5+8*hcibuf[2]+3*i]);
398  Notify(PSTR(" "));
399  PrintHex<uint8_t>(hcibuf[4+8*hcibuf[2]+3*i]);
400  }
401 #endif
402  }
403  }
404  break;
405 
406  case EV_CONNECT_COMPLETE:
407  hci_event_flag |= HCI_FLAG_CONNECT_EVENT;
408  if (!hcibuf[2]) { // check if connected OK
409  hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8); // store the handle for the ACL connection
410  hci_event_flag |= HCI_FLAG_CONN_COMPLETE; // set connection complete flag
411  }
412 #ifdef EXTRADEBUG
413  else {
414  Notify(PSTR("\r\nConnection Failed"));
415  }
416 #endif
417  break;
418 
420  if (!hcibuf[2]) { // check if disconnected OK
421  hci_event_flag |= HCI_FLAG_DISCONN_COMPLETE; // set disconnect command complete flag
422  hci_event_flag &= ~HCI_FLAG_CONN_COMPLETE; // clear connection complete flag
423  }
424  break;
425 
427  if (!hcibuf[2]) { // check if reading is OK
428  for (uint8_t i = 0; i < 30; i++)
429  remote_name[i] = hcibuf[9 + i]; //store first 30 bytes
430  hci_event_flag |= HCI_FLAG_REMOTE_NAME_COMPLETE;
431  }
432  break;
433 
434  case EV_INCOMING_CONNECT:
435  disc_bdaddr[0] = hcibuf[2];
436  disc_bdaddr[1] = hcibuf[3];
437  disc_bdaddr[2] = hcibuf[4];
438  disc_bdaddr[3] = hcibuf[5];
439  disc_bdaddr[4] = hcibuf[6];
440  disc_bdaddr[5] = hcibuf[7];
441  hci_event_flag |= HCI_FLAG_INCOMING_REQUEST;
442  break;
443 
444  case EV_PIN_CODE_REQUEST:
445  if(pairWithWii) {
446 #ifdef DEBUG
447  Notify(PSTR("\r\nPairing with wiimote"));
448 #endif
450  }
451  else if(btdPin != NULL) {
452 #ifdef DEBUG
453  Notify(PSTR("\r\nBluetooth pin is set too: "));
454  Serial.print(btdPin);
455 #endif
457  }
458  else {
459 #ifdef DEBUG
460  Notify(PSTR("\r\nNo pin was set"));
461 #endif
463  }
464  break;
465 
466  case EV_LINK_KEY_REQUEST:
467 #ifdef DEBUG
468  Notify(PSTR("\r\nReceived Key Request"));
469 #endif
471  break;
472 
474  if(pairWithWii && !connectToWii) {
475 #ifdef DEBUG
476  Notify(PSTR("\r\nPairing successful"));
477 #endif
478  connectToWii = true; // Only send the ACL data to the Wii service
479  }
480  break;
481  /* We will just ignore the following events */
482  case EV_NUM_COMPLETE_PKT:
483  case EV_ROLE_CHANGED:
484  case EV_PAGE_SCAN_REP_MODE:
485  case EV_LOOPBACK_COMMAND:
488  case EV_MAX_SLOTS_CHANGE:
493  break;
494 #ifdef EXTRADEBUG
495  default:
496  if(hcibuf[0] != 0x00) {
497  Notify(PSTR("\r\nUnmanaged HCI Event: "));
498  PrintHex<uint8_t>(hcibuf[0]);
499  }
500  break;
501 #endif
502  } // switch
503  HCI_task();
504  }
505 #ifdef EXTRADEBUG
506  else {
507  Notify(PSTR("\r\nHCI event error: "));
508  PrintHex<uint8_t>(rcode);
509  }
510 #endif
511 }
512 
513 /* Poll Bluetooth and print result */
514 void BTD::HCI_task() {
515  switch (hci_state){
516  case HCI_INIT_STATE:
517  hci_counter++;
518  if (hci_counter > hci_num_reset_loops) { // wait until we have looped x times to clear any old events
519  hci_reset();
520  hci_state = HCI_RESET_STATE;
521  hci_counter = 0;
522  }
523  break;
524 
525  case HCI_RESET_STATE:
526  hci_counter++;
527  if (hci_cmd_complete) {
528  hci_counter = 0;
529 #ifdef DEBUG
530  Notify(PSTR("\r\nHCI Reset complete"));
531 #endif
532  hci_state = HCI_BDADDR_STATE;
533  hci_read_bdaddr();
534  }
535  else if (hci_counter > hci_num_reset_loops) {
536  hci_num_reset_loops *= 10;
537  if(hci_num_reset_loops > 2000)
538  hci_num_reset_loops = 2000;
539 #ifdef DEBUG
540  Notify(PSTR("\r\nNo response to HCI Reset"));
541 #endif
542  hci_state = HCI_INIT_STATE;
543  hci_counter = 0;
544  }
545  break;
546 
547  case HCI_BDADDR_STATE:
549 #ifdef DEBUG
550  Notify(PSTR("\r\nLocal Bluetooth Address: "));
551  for(int8_t i = 5; i > 0;i--) {
552  PrintHex<uint8_t>(my_bdaddr[i]);
553  Notify(PSTR(":"));
554  }
555  PrintHex<uint8_t>(my_bdaddr[0]);
556 #endif
558  hci_state = HCI_LOCAL_VERSION_STATE;
559  }
560  break;
561 
562  case HCI_LOCAL_VERSION_STATE: // The local version is used by the PS3BT class
564  if(btdName != NULL) {
566  hci_state = HCI_SET_NAME_STATE;
567  } else
568  hci_state = HCI_CHECK_WII_SERVICE;
569  }
570  break;
571 
572  case HCI_SET_NAME_STATE:
573  if (hci_cmd_complete) {
574 #ifdef DEBUG
575  Notify(PSTR("\r\nThe name is set to: "));
576  Serial.print(btdName);
577 #endif
578  hci_state = HCI_CHECK_WII_SERVICE;
579  }
580  break;
581 
583  if(pairWithWii) { // Check if it should try to connect to a wiimote
584 #ifdef DEBUG
585  Notify(PSTR("\r\nStarting inquiry\r\nPress 1 & 2 on the Wiimote\r\nOr press sync if you are using a Wii U Pro Controller"));
586 #endif
587  hci_inquiry();
588  hci_state = HCI_INQUIRY_STATE;
589  }
590  else
591  hci_state = HCI_SCANNING_STATE; // Don't try to connect to a Wiimote
592  break;
593 
594  case HCI_INQUIRY_STATE:
595  if(hci_wii_found) {
596  hci_inquiry_cancel(); // Stop inquiry
597 #ifdef DEBUG
598  Notify(PSTR("\r\nWiimote found"));
599  Notify(PSTR("\r\nNow just create the instance like so:"));
600  Notify(PSTR("\r\nWII Wii(&Btd);"));
601  Notify(PSTR("\r\nAnd then press any button on the Wiimote"));
602 #endif
603  if(motionPlusInside) {
604  hci_remote_name(); // We need to know the name to distinguish between a Wiimote and a Wii U Pro Controller
605  hci_state = HCI_REMOTE_NAME_STATE;
606  } else
607  hci_state = HCI_CONNECT_WII_STATE;
608  }
609  break;
610 
612  if(hci_cmd_complete) {
613 #ifdef DEBUG
614  Notify(PSTR("\r\nConnecting to Wiimote"));
615 #endif
616  hci_connect();
617  hci_state = HCI_CONNECTED_WII_STATE;
618  }
619  break;
620 
622  if(hci_connect_event) {
624 #ifdef DEBUG
625  Notify(PSTR("\r\nConnected to Wiimote"));
626 #endif
627  hci_authentication_request(); // This will start the pairing with the wiimote
628  hci_state = HCI_SCANNING_STATE;
629  } else {
630 #ifdef DEBUG
631  Notify(PSTR("\r\nTrying to connect one more time..."));
632 #endif
633  hci_connect(); // Try to connect one more time
634  }
635  }
636  break;
637 
638  case HCI_SCANNING_STATE:
639  if(!connectToWii && !pairWithWii) {
640 #ifdef DEBUG
641  Notify(PSTR("\r\nWait For Incoming Connection Request"));
642 #endif
644  watingForConnection = true;
645  hci_state = HCI_CONNECT_IN_STATE;
646  }
647  break;
648 
651  watingForConnection = false;
652 #ifdef DEBUG
653  Notify(PSTR("\r\nIncoming Connection Request"));
654 #endif
655  hci_remote_name();
656  hci_state = HCI_REMOTE_NAME_STATE;
657  } else if (hci_disconnect_complete)
658  hci_state = HCI_DISCONNECT_STATE;
659  break;
660 
663 #ifdef DEBUG
664  Notify(PSTR("\r\nRemote Name: "));
665  for (uint8_t i = 0; i < 30; i++) {
666  if(remote_name[i] == NULL)
667  break;
668  Serial.write(remote_name[i]);
669  }
670 #endif
671  if(strncmp((const char*)remote_name, "Nintendo", 8) == 0) {
672 #ifdef DEBUG
673  Notify(PSTR("\r\nWiimote is connecting"));
674 #endif
675  if(strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-TR", 22) == 0) {
676 #ifdef DEBUG
677  Notify(PSTR(" with Motion Plus Inside"));
678 #endif
679  motionPlusInside = true;
680  }
681  else if(strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-UC", 22) == 0) {
682 #ifdef DEBUG
683  Notify(PSTR(" - Wii U Pro Controller"));
684 #endif
685  motionPlusInside = true;
686  wiiUProController = true;
687  } else {
688  motionPlusInside = false;
689  wiiUProController = false;
690  }
691  incomingWii = true;
692  }
694  hci_state = HCI_CONNECT_WII_STATE;
695  else {
697  hci_state = HCI_CONNECTED_STATE;
698  }
699  }
700  break;
701 
702  case HCI_CONNECTED_STATE:
703  if (hci_connect_complete) {
704 #ifdef DEBUG
705  Notify(PSTR("\r\nConnected to Device: "));
706  for(int8_t i = 5; i>0;i--) {
707  PrintHex<uint8_t>(disc_bdaddr[i]);
708  Notify(PSTR(":"));
709  }
710  PrintHex<uint8_t>(disc_bdaddr[0]);
711 #endif
712  // Clear these flags for a new connection
713  l2capConnectionClaimed = false;
714  sdpConnectionClaimed = false;
715  rfcommConnectionClaimed = false;
716 
717  hci_event_flag = 0;
718  hci_state = HCI_DONE_STATE;
719  }
720  break;
721 
722  case HCI_DONE_STATE:
723  hci_counter++;
724  if (hci_counter > 1000) { // Wait until we have looped 1000 times to make sure that the L2CAP connection has been started
725  hci_counter = 0;
726  hci_state = HCI_SCANNING_STATE;
727  }
728  break;
729 
732 #ifdef DEBUG
733  Notify(PSTR("\r\nHCI Disconnected from Device"));
734 #endif
735  hci_event_flag = 0; // Clear all flags
736 
737  // Reset all buffers
738  for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++)
739  hcibuf[i] = 0;
740  for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++)
741  l2capinbuf[i] = 0;
742 
743  hci_state = HCI_SCANNING_STATE;
744  }
745  break;
746  default:
747  break;
748  }
749 }
750 
751 void BTD::ACL_event_task() {
752  uint16_t MAX_BUFFER_SIZE = BULK_MAXPKTSIZE;
753  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[ BTD_DATAIN_PIPE ].epAddr, &MAX_BUFFER_SIZE, l2capinbuf); // input on endpoint 2
754  if(!rcode) { // Check for errors
755  for (uint8_t i=0; i<BTD_NUMSERVICES; i++)
756  if (btService[i])
757  btService[i]->ACLData(l2capinbuf);
758  }
759 #ifdef EXTRADEBUG
760  else if (rcode != hrNAK) {
761  Notify(PSTR("\r\nACL data in error: "));
762  PrintHex<uint8_t>(rcode);
763  }
764 #endif
765  for (uint8_t i=0; i<BTD_NUMSERVICES; i++)
766  if (btService[i])
767  btService[i]->Run();
768 }
769 
770 /************************************************************/
771 /* HCI Commands */
772 /************************************************************/
773 void BTD::HCI_Command(uint8_t* data, uint16_t nbytes) {
774  hci_event_flag &= ~HCI_FLAG_CMD_COMPLETE;
775  pUsb->ctrlReq(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bmREQ_HCI_OUT, 0x00, 0x00, 0x00 ,0x00, nbytes, nbytes, data, NULL);
776 }
778  hci_event_flag = 0; // Clear all the flags
779  hcibuf[0] = 0x03; // HCI OCF = 3
780  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
781  hcibuf[2] = 0x00;
782  HCI_Command(hcibuf, 3);
783 }
785  hci_event_flag &= ~HCI_FLAG_INCOMING_REQUEST;
786  hcibuf[0] = 0x1A; // HCI OCF = 1A
787  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
788  hcibuf[2] = 0x01; // parameter length = 1
789  if(btdName != NULL)
790  hcibuf[3] = 0x03; // Inquiry Scan enabled. Page Scan enabled.
791  else
792  hcibuf[3] = 0x02; // Inquiry Scan disabled. Page Scan enabled.
793  HCI_Command(hcibuf, 4);
794 }
796  hcibuf[0] = 0x1A; // HCI OCF = 1A
797  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
798  hcibuf[2] = 0x01; // parameter length = 1
799  hcibuf[3] = 0x00; // Inquiry Scan disabled. Page Scan disabled.
800  HCI_Command(hcibuf, 4);
801 }
803  hcibuf[0] = 0x09; // HCI OCF = 9
804  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
805  hcibuf[2] = 0x00;
806  HCI_Command(hcibuf, 3);
807 }
809  hcibuf[0] = 0x01; // HCI OCF = 1
810  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
811  hcibuf[2] = 0x00;
812  HCI_Command(hcibuf, 3);
813 }
815  hci_event_flag &= ~HCI_FLAG_CONN_COMPLETE;
816  hcibuf[0] = 0x09; // HCI OCF = 9
817  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
818  hcibuf[2] = 0x07; // parameter length 7
819  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
820  hcibuf[4] = disc_bdaddr[1];
821  hcibuf[5] = disc_bdaddr[2];
822  hcibuf[6] = disc_bdaddr[3];
823  hcibuf[7] = disc_bdaddr[4];
824  hcibuf[8] = disc_bdaddr[5];
825  hcibuf[9] = 0x00; //switch role to master
826 
827  HCI_Command(hcibuf, 10);
828 }
830  hci_event_flag &= ~HCI_FLAG_REMOTE_NAME_COMPLETE;
831  hcibuf[0] = 0x19; // HCI OCF = 19
832  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
833  hcibuf[2] = 0x0A; // parameter length = 10
834  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
835  hcibuf[4] = disc_bdaddr[1];
836  hcibuf[5] = disc_bdaddr[2];
837  hcibuf[6] = disc_bdaddr[3];
838  hcibuf[7] = disc_bdaddr[4];
839  hcibuf[8] = disc_bdaddr[5];
840  hcibuf[9] = 0x01; //Page Scan Repetition Mode
841  hcibuf[10] = 0x00; //Reserved
842  hcibuf[11] = 0x00; //Clock offset - low byte
843  hcibuf[12] = 0x00; //Clock offset - high byte
844 
845  HCI_Command(hcibuf, 13);
846 }
847 void BTD::hci_set_local_name(const char* name) {
848  hcibuf[0] = 0x13; // HCI OCF = 13
849  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
850  hcibuf[2] = strlen(name)+1; // parameter length = the length of the string + end byte
851  uint8_t i;
852  for(i = 0; i < strlen(name); i++)
853  hcibuf[i+3] = name[i];
854  hcibuf[i+3] = 0x00; // End of string
855 
856  HCI_Command(hcibuf, 4+strlen(name));
857 }
859  hci_event_flag &= ~HCI_FLAG_WII_FOUND;
860  hcibuf[0] = 0x01;
861  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
862  hcibuf[2] = 0x05; // Parameter Total Length = 5
863  hcibuf[3] = 0x33; // LAP: Genera/Unlimited Inquiry Access Code (GIAC = 0x9E8B33) - see https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
864  hcibuf[4] = 0x8B;
865  hcibuf[5] = 0x9E;
866  hcibuf[6] = 0x30; // Inquiry time = 61.44 sec (maximum)
867  hcibuf[7] = 0x0A; // 10 number of responses
868 
869  HCI_Command(hcibuf, 8);
870 }
872  hcibuf[0] = 0x02;
873  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
874  hcibuf[2] = 0x0; // Parameter Total Length = 0
875 
876  HCI_Command(hcibuf, 3);
877 }
879  hci_event_flag &= ~(HCI_FLAG_CONN_COMPLETE | HCI_FLAG_CONNECT_EVENT);
880  hcibuf[0] = 0x05;
881  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
882  hcibuf[2] = 0x0D; // parameter Total Length = 13
883  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
884  hcibuf[4] = disc_bdaddr[1];
885  hcibuf[5] = disc_bdaddr[2];
886  hcibuf[6] = disc_bdaddr[3];
887  hcibuf[7] = disc_bdaddr[4];
888  hcibuf[8] = disc_bdaddr[5];
889  hcibuf[9] = 0x18; // DM1 or DH1 may be used
890  hcibuf[10] = 0xCC; // DM3, DH3, DM5, DH5 may be used
891  hcibuf[11] = 0x01; // Page repetition mode R1
892  hcibuf[12] = 0x00; // Reserved
893  hcibuf[13] = 0x00; // Clock offset
894  hcibuf[14] = 0x00; // Invalid clock offset
895  hcibuf[15] = 0x00; // Do not allow role switch
896 
897  HCI_Command(hcibuf, 16);
898 }
900  hcibuf[0] = 0x0D; // HCI OCF = 0D
901  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
902  hcibuf[2] = 0x17; // parameter length 23
903  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
904  hcibuf[4] = disc_bdaddr[1];
905  hcibuf[5] = disc_bdaddr[2];
906  hcibuf[6] = disc_bdaddr[3];
907  hcibuf[7] = disc_bdaddr[4];
908  hcibuf[8] = disc_bdaddr[5];
910  hcibuf[9] = 6; // Pin length is the length of the bt address
911  hcibuf[10] = disc_bdaddr[0]; // The pin is the Wiimotes bt address backwards
912  hcibuf[11] = disc_bdaddr[1];
913  hcibuf[12] = disc_bdaddr[2];
914  hcibuf[13] = disc_bdaddr[3];
915  hcibuf[14] = disc_bdaddr[4];
916  hcibuf[15] = disc_bdaddr[5];
917  for(uint8_t i = 16; i < 26; i++)
918  hcibuf[i] = 0x00; // The rest should be 0
919  } else if(pairWithWii && wiiUProController) {
920 #ifdef DEBUG
921  Notify(PSTR("\r\nParing with Wii U Pro Controller"));
922 #endif
923  hcibuf[9] = 6; // Pin length is the length of the bt address
924  hcibuf[10] = my_bdaddr[0]; // The pin is the Bluetooth dongles bt address backwards
925  hcibuf[11] = my_bdaddr[1];
926  hcibuf[12] = my_bdaddr[2];
927  hcibuf[13] = my_bdaddr[3];
928  hcibuf[14] = my_bdaddr[4];
929  hcibuf[15] = my_bdaddr[5];
930  for(uint8_t i = 16; i < 26; i++)
931  hcibuf[i] = 0x00; // The rest should be 0
932  } else {
933  hcibuf[9] = strlen(btdPin); // Length of pin
934  uint8_t i;
935  for(i = 0; i < strlen(btdPin); i++) // The maximum size of the pin is 16
936  hcibuf[i+10] = btdPin[i];
937  for(;i < 16; i++)
938  hcibuf[i+10] = 0x00; // The rest should be 0
939  }
940 
941  HCI_Command(hcibuf, 26);
942 }
944  hcibuf[0] = 0x0E; // HCI OCF = 0E
945  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
946  hcibuf[2] = 0x06; // parameter length 6
947  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
948  hcibuf[4] = disc_bdaddr[1];
949  hcibuf[5] = disc_bdaddr[2];
950  hcibuf[6] = disc_bdaddr[3];
951  hcibuf[7] = disc_bdaddr[4];
952  hcibuf[8] = disc_bdaddr[5];
953 
954  HCI_Command(hcibuf, 9);
955 }
957  hcibuf[0] = 0x0C; // HCI OCF = 0C
958  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
959  hcibuf[2] = 0x06; // parameter length 6
960  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
961  hcibuf[4] = disc_bdaddr[1];
962  hcibuf[5] = disc_bdaddr[2];
963  hcibuf[6] = disc_bdaddr[3];
964  hcibuf[7] = disc_bdaddr[4];
965  hcibuf[8] = disc_bdaddr[5];
966 
967  HCI_Command(hcibuf, 9);
968 }
970  hcibuf[0] = 0x11; // HCI OCF = 11
971  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
972  hcibuf[2] = 0x02; // parameter length = 2
973  hcibuf[3] = (uint8_t)(hci_handle & 0xFF);//connection handle - low byte
974  hcibuf[4] = (uint8_t)((hci_handle >> 8) & 0x0F);//connection handle - high byte
975 
976  HCI_Command(hcibuf, 5);
977 }
978 void BTD::hci_disconnect(uint16_t handle) { // This is called by the different services
979  hci_event_flag &= ~HCI_FLAG_DISCONN_COMPLETE;
980  hcibuf[0] = 0x06; // HCI OCF = 6
981  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
982  hcibuf[2] = 0x03; // parameter length = 3
983  hcibuf[3] = (uint8_t)(handle & 0xFF);//connection handle - low byte
984  hcibuf[4] = (uint8_t)((handle >> 8) & 0x0F);//connection handle - high byte
985  hcibuf[5] = 0x13; // reason
986 
987  HCI_Command(hcibuf, 6);
988 }
989 /*******************************************************************
990  * *
991  * HCI ACL Data Packet *
992  * *
993  * buf[0] buf[1] buf[2] buf[3]
994  * 0 4 8 11 12 16 24 31 MSB
995  * .-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
996  * | HCI Handle |PB |BC | Data Total Length | HCI ACL Data Packet
997  * .-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
998  *
999  * buf[4] buf[5] buf[6] buf[7]
1000  * 0 8 16 31 MSB
1001  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1002  * | Length | Channel ID | Basic L2CAP header
1003  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1004  *
1005  * buf[8] buf[9] buf[10] buf[11]
1006  * 0 8 16 31 MSB
1007  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1008  * | Code | Identifier | Length | Control frame (C-frame)
1009  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-. (signaling packet format)
1010  */
1011 /************************************************************/
1012 /* L2CAP Commands */
1013 /************************************************************/
1014 void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh) {
1015  uint8_t buf[8+nbytes];
1016  buf[0] = (uint8_t)(handle & 0xff); // HCI handle with PB,BC flag
1017  buf[1] = (uint8_t)(((handle >> 8) & 0x0f) | 0x20);
1018  buf[2] = (uint8_t)((4 + nbytes) & 0xff); // HCI ACL total data length
1019  buf[3] = (uint8_t)((4 + nbytes) >> 8);
1020  buf[4] = (uint8_t)(nbytes & 0xff); // L2CAP header: Length
1021  buf[5] = (uint8_t)(nbytes >> 8);
1022  buf[6] = channelLow;
1023  buf[7] = channelHigh;
1024 
1025  for (uint16_t i = 0; i < nbytes; i++) // L2CAP C-frame
1026  buf[8 + i] = data[i];
1027 
1028  uint8_t rcode = pUsb->outTransfer(bAddress, epInfo[ BTD_DATAOUT_PIPE ].epAddr, (8 + nbytes), buf);
1029  if(rcode) {
1030  delay(100); // This small delay prevents it from overflowing if it fails
1031 #ifdef DEBUG
1032  Notify(PSTR("\r\nError sending L2CAP message: 0x"));
1033  PrintHex<uint8_t>(rcode);
1034  Notify(PSTR(" - Channel ID: "));
1035  Serial.print(channelHigh);
1036  Notify(PSTR(" "));
1037  Serial.print(channelLow);
1038 #endif
1039  }
1040 }
1041 void BTD::l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm) {
1042  l2capoutbuf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code
1043  l2capoutbuf[1] = rxid; // Identifier
1044  l2capoutbuf[2] = 0x04; // Length
1045  l2capoutbuf[3] = 0x00;
1046  l2capoutbuf[4] = (uint8_t)(psm & 0xff); // PSM
1047  l2capoutbuf[5] = (uint8_t)(psm >> 8);
1048  l2capoutbuf[6] = scid[0]; // Source CID
1049  l2capoutbuf[7] = scid[1];
1050 
1051  L2CAP_Command(handle, l2capoutbuf, 8);
1052 }
1053 void BTD::l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result) {
1054  l2capoutbuf[0] = L2CAP_CMD_CONNECTION_RESPONSE; // Code
1055  l2capoutbuf[1] = rxid; // Identifier
1056  l2capoutbuf[2] = 0x08; // Length
1057  l2capoutbuf[3] = 0x00;
1058  l2capoutbuf[4] = dcid[0]; // Destination CID
1059  l2capoutbuf[5] = dcid[1];
1060  l2capoutbuf[6] = scid[0]; // Source CID
1061  l2capoutbuf[7] = scid[1];
1062  l2capoutbuf[8] = result; // Result: Pending or Success
1063  l2capoutbuf[9] = 0x00;
1064  l2capoutbuf[10] = 0x00; // No further information
1065  l2capoutbuf[11] = 0x00;
1066 
1067  L2CAP_Command(handle, l2capoutbuf, 12);
1068 }
1069 void BTD::l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid) {
1070  l2capoutbuf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code
1071  l2capoutbuf[1] = rxid; // Identifier
1072  l2capoutbuf[2] = 0x08; // Length
1073  l2capoutbuf[3] = 0x00;
1074  l2capoutbuf[4] = dcid[0]; // Destination CID
1075  l2capoutbuf[5] = dcid[1];
1076  l2capoutbuf[6] = 0x00; // Flags
1077  l2capoutbuf[7] = 0x00;
1078  l2capoutbuf[8] = 0x01; // Config Opt: type = MTU (Maximum Transmission Unit) - Hint
1079  l2capoutbuf[9] = 0x02; // Config Opt: length
1080  l2capoutbuf[10] = 0xFF; // MTU
1081  l2capoutbuf[11] = 0xFF;
1082 
1083  L2CAP_Command(handle, l2capoutbuf, 12);
1084 }
1085 void BTD::l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid) {
1086  l2capoutbuf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code
1087  l2capoutbuf[1] = rxid; // Identifier
1088  l2capoutbuf[2] = 0x0A; // Length
1089  l2capoutbuf[3] = 0x00;
1090  l2capoutbuf[4] = scid[0]; // Source CID
1091  l2capoutbuf[5] = scid[1];
1092  l2capoutbuf[6] = 0x00; // Flag
1093  l2capoutbuf[7] = 0x00;
1094  l2capoutbuf[8] = 0x00; // Result
1095  l2capoutbuf[9] = 0x00;
1096  l2capoutbuf[10] = 0x01; // Config
1097  l2capoutbuf[11] = 0x02;
1098  l2capoutbuf[12] = 0xA0;
1099  l2capoutbuf[13] = 0x02;
1100 
1101  L2CAP_Command(handle, l2capoutbuf, 14);
1102 }
1103 void BTD::l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
1104  l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_REQUEST; // Code
1105  l2capoutbuf[1] = rxid; // Identifier
1106  l2capoutbuf[2] = 0x04; // Length
1107  l2capoutbuf[3] = 0x00;
1108  l2capoutbuf[4] = dcid[0];
1109  l2capoutbuf[5] = dcid[1];
1110  l2capoutbuf[6] = scid[0];
1111  l2capoutbuf[7] = scid[1];
1112  L2CAP_Command(handle, l2capoutbuf, 8);
1113 }
1114 void BTD::l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
1115  l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_RESPONSE; // Code
1116  l2capoutbuf[1] = rxid; // Identifier
1117  l2capoutbuf[2] = 0x04; // Length
1118  l2capoutbuf[3] = 0x00;
1119  l2capoutbuf[4] = dcid[0];
1120  l2capoutbuf[5] = dcid[1];
1121  l2capoutbuf[6] = scid[0];
1122  l2capoutbuf[7] = scid[1];
1123  L2CAP_Command(handle, l2capoutbuf, 8);
1124 }
1125 void BTD::l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh) {
1126  l2capoutbuf[0] = L2CAP_CMD_INFORMATION_RESPONSE; // Code
1127  l2capoutbuf[1] = rxid; // Identifier
1128  l2capoutbuf[2] = 0x08; // Length
1129  l2capoutbuf[3] = 0x00;
1130  l2capoutbuf[4] = infoTypeLow;
1131  l2capoutbuf[5] = infoTypeHigh;
1132  l2capoutbuf[6] = 0x00; // Result = success
1133  l2capoutbuf[7] = 0x00; // Result = success
1134  l2capoutbuf[8] = 0x00;
1135  l2capoutbuf[9] = 0x00;
1136  l2capoutbuf[10] = 0x00;
1137  l2capoutbuf[11] = 0x00;
1138  L2CAP_Command(handle, l2capoutbuf, 12);
1139 }
1140 
1141 /* PS3 Commands - only set Bluetooth address is implemented */
1142 void BTD::setBdaddr(uint8_t* BDADDR) {
1143  /* Set the internal bluetooth address */
1144  uint8_t buf[8];
1145  buf[0] = 0x01;
1146  buf[1] = 0x00;
1147  for (uint8_t i = 0; i < 6; i++)
1148  buf[i+2] = BDADDR[5 - i];//Copy into buffer, has to be written reversed
1149 
1150  //bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0xF5), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data)
1151  pUsb->ctrlReq(bAddress,epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
1152 #ifdef DEBUG
1153  Notify(PSTR("\r\nBluetooth Address was set to: "));
1154  for(int8_t i = 5; i > 0; i--) {
1155  PrintHex<uint8_t>(my_bdaddr[i]);
1156  Notify(PSTR(":"));
1157  }
1158  PrintHex<uint8_t>(my_bdaddr[0]);
1159 #endif
1160 }
1161 void BTD::setMoveBdaddr(uint8_t* BDADDR) {
1162  /* Set the internal bluetooth address */
1163  uint8_t buf[11];
1164  buf[0] = 0x05;
1165  buf[7] = 0x10;
1166  buf[8] = 0x01;
1167  buf[9] = 0x02;
1168  buf[10] = 0x12;
1169 
1170  for (uint8_t i = 0; i < 6; i++)
1171  buf[i + 1] = BDADDR[i];
1172 
1173  //bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x05), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data)
1174  pUsb->ctrlReq(bAddress,epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00,11,11, buf, NULL);
1175 #ifdef DEBUG
1176  Notify(PSTR("\r\nBluetooth Address was set to: "));
1177  for(int8_t i = 5; i > 0; i--) {
1178  PrintHex<uint8_t>(my_bdaddr[i]);
1179  Notify(PSTR(":"));
1180  }
1181  PrintHex<uint8_t>(my_bdaddr[0]);
1182 #endif
1183 }