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