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