USB Host Shield 2.0
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 useSimplePairing(false),
33 pUsb(p), // Pointer to USB class instance - mandatory
34 bAddress(0), // Device address - mandatory
35 bNumEP(1), // If config descriptor needs to be parsed
36 qNextPollTime(0), // Reset NextPollTime
37 pollInterval(0),
38 simple_pairing_supported(false),
39 bPollEnable(false) // Don't start polling before dongle is connected
40 {
41  for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++)
42  btService[i] = NULL;
43 
44  Initialize(); // Set all variables, endpoint structs etc. to default values
45 
46  if(pUsb) // Register in USB subsystem
47  pUsb->RegisterDeviceClass(this); // Set devConfig[] entry
48 }
49 
50 uint8_t BTD::ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed) {
51  const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
52  uint8_t buf[constBufSize];
53  USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
54  uint8_t rcode;
55  UsbDevice *p = NULL;
56  EpInfo *oldep_ptr = NULL;
57 
58  Initialize(); // Set all variables, endpoint structs etc. to default values
59 
60  AddressPool &addrPool = pUsb->GetAddressPool(); // Get memory address of USB device address pool
61 #ifdef EXTRADEBUG
62  Notify(PSTR("\r\nBTD ConfigureDevice"), 0x80);
63 #endif
64 
65  if(bAddress) { // Check if address has already been assigned to an instance
66 #ifdef DEBUG_USB_HOST
67  Notify(PSTR("\r\nAddress in use"), 0x80);
68 #endif
70  }
71 
72  p = addrPool.GetUsbDevicePtr(0); // Get pointer to pseudo device with address 0 assigned
73  if(!p) {
74 #ifdef DEBUG_USB_HOST
75  Notify(PSTR("\r\nAddress not found"), 0x80);
76 #endif
78  }
79 
80  if(!p->epinfo) {
81 #ifdef DEBUG_USB_HOST
82  Notify(PSTR("\r\nepinfo is null"), 0x80);
83 #endif
85  }
86 
87  oldep_ptr = p->epinfo; // Save old pointer to EP_RECORD of address 0
88  p->epinfo = epInfo; // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
89  p->lowspeed = lowspeed;
90  rcode = pUsb->getDevDescr(0, 0, constBufSize, (uint8_t*)buf); // Get device descriptor - addr, ep, nbytes, data
91 
92  p->epinfo = oldep_ptr; // Restore p->epinfo
93 
94  if(rcode)
95  goto FailGetDevDescr;
96 
97  bAddress = addrPool.AllocAddress(parent, false, port); // Allocate new address according to device class
98 
99  if(!bAddress) {
100 #ifdef DEBUG_USB_HOST
101  Notify(PSTR("\r\nOut of address space"), 0x80);
102 #endif
104  }
105 
106  if (udd->bDeviceClass == 0x09) // Some dongles have an USB hub inside
107  goto FailHub;
108 
109  epInfo[0].maxPktSize = udd->bMaxPacketSize0; // Extract Max Packet Size from device descriptor
110  epInfo[1].epAddr = udd->bNumConfigurations; // Steal and abuse from epInfo structure to save memory
111 
112  VID = udd->idVendor;
113  PID = udd->idProduct;
114 
116 
117 FailHub:
118 #ifdef DEBUG_USB_HOST
119  Notify(PSTR("\r\nPlease create a hub instance in your code: \"USBHub Hub1(&Usb);\""), 0x80);
120 #endif
121  pUsb->setAddr(bAddress, 0, 0); // Reset address
123  Release();
124  return rcode;
125 
126 FailGetDevDescr:
127 #ifdef DEBUG_USB_HOST
128  NotifyFailGetDevDescr(rcode);
129 #endif
130  if(rcode != hrJERR)
132  Release();
133  return rcode;
134 };
135 
136 uint8_t BTD::Init(uint8_t parent __attribute__((unused)), uint8_t port __attribute__((unused)), bool lowspeed) {
137  uint8_t rcode;
138  uint8_t num_of_conf = epInfo[1].epAddr; // Number of configurations
139  epInfo[1].epAddr = 0;
140 
141  AddressPool &addrPool = pUsb->GetAddressPool();
142 #ifdef EXTRADEBUG
143  Notify(PSTR("\r\nBTD Init"), 0x80);
144 #endif
145  UsbDevice *p = addrPool.GetUsbDevicePtr(bAddress); // Get pointer to assigned address record
146 
147  if(!p) {
148 #ifdef DEBUG_USB_HOST
149  Notify(PSTR("\r\nAddress not found"), 0x80);
150 #endif
152  }
153 
154  delay(300); // Assign new address to the device
155 
156  rcode = pUsb->setAddr(0, 0, bAddress); // Assign new address to the device
157  if(rcode) {
158 #ifdef DEBUG_USB_HOST
159  Notify(PSTR("\r\nsetAddr: "), 0x80);
160  D_PrintHex<uint8_t > (rcode, 0x80);
161 #endif
162  p->lowspeed = false;
163  goto Fail;
164  }
165 #ifdef EXTRADEBUG
166  Notify(PSTR("\r\nAddr: "), 0x80);
167  D_PrintHex<uint8_t > (bAddress, 0x80);
168 #endif
169 
170  p->lowspeed = false;
171 
172  p = addrPool.GetUsbDevicePtr(bAddress); // Get pointer to assigned address record
173  if(!p) {
174 #ifdef DEBUG_USB_HOST
175  Notify(PSTR("\r\nAddress not found"), 0x80);
176 #endif
178  }
179 
180  p->lowspeed = lowspeed;
181 
182  rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo); // Assign epInfo to epinfo pointer - only EP0 is known
183  if(rcode)
184  goto FailSetDevTblEntry;
185 
186  if(VID == PS3_VID && (PID == PS3_PID || PID == PS3NAVIGATION_PID || PID == PS3MOVE_PID)) {
187  delay(100);
188  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
189  if(rcode)
190  goto FailSetConfDescr;
191 
192 #ifdef DEBUG_USB_HOST
193  if(PID == PS3_PID || PID == PS3NAVIGATION_PID) {
194  if(PID == PS3_PID)
195  Notify(PSTR("\r\nDualshock 3 Controller Connected"), 0x80);
196  else // It must be a navigation controller
197  Notify(PSTR("\r\nNavigation Controller Connected"), 0x80);
198  } else // It must be a Motion controller
199  Notify(PSTR("\r\nMotion Controller Connected"), 0x80);
200 #endif
201 
202  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) {
203 #ifdef DEBUG_USB_HOST
204  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);
205 #endif
206  } else {
207  if(PID == PS3_PID || PID == PS3NAVIGATION_PID)
208  setBdaddr(my_bdaddr); // Set internal Bluetooth address
209  else
210  setMoveBdaddr(my_bdaddr); // Set internal Bluetooth address
211 #ifdef DEBUG_USB_HOST
212  Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80);
213  for(int8_t i = 5; i > 0; i--) {
214  D_PrintHex<uint8_t > (my_bdaddr[i], 0x80);
215  Notify(PSTR(":"), 0x80);
216  }
217  D_PrintHex<uint8_t > (my_bdaddr[0], 0x80);
218 #endif
219  }
220 
221  pUsb->setConf(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, 0); // Reset configuration value
222  pUsb->setAddr(bAddress, 0, 0); // Reset address
223  Release(); // Release device
225  } else {
226  // Check if attached device is a Bluetooth dongle and fill endpoint data structure
227  // First interface in the configuration must have Bluetooth assigned Class/Subclass/Protocol
228  // And 3 endpoints - interrupt-IN, bulk-IN, bulk-OUT, not necessarily in this order
229  for(uint8_t i = 0; i < num_of_conf; i++) {
230  if((VID == IOGEAR_GBU521_VID && PID == IOGEAR_GBU521_PID) || (VID == BELKIN_F8T065BF_VID && PID == BELKIN_F8T065BF_PID)) {
231  ConfigDescParser<USB_CLASS_VENDOR_SPECIFIC, WI_SUBCLASS_RF, WI_PROTOCOL_BT, CP_MASK_COMPARE_ALL> confDescrParser(this); // Workaround issue with some dongles
232  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
233  } else {
234  ConfigDescParser<USB_CLASS_WIRELESS_CTRL, WI_SUBCLASS_RF, WI_PROTOCOL_BT, CP_MASK_COMPARE_ALL> confDescrParser(this); // Set class id according to the specification
235  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
236  }
237  if(rcode) // Check error code
238  goto FailGetConfDescr;
239  if(bNumEP >= BTD_MAX_ENDPOINTS) // All endpoints extracted
240  break;
241  }
242 
244  goto FailUnknownDevice;
245 
246  // Assign epInfo to epinfo pointer - this time all 3 endpoins
248  if(rcode)
249  goto FailSetDevTblEntry;
250 
251  // Set Configuration Value
252  rcode = pUsb->setConf(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bConfNum);
253  if(rcode)
254  goto FailSetConfDescr;
255 
256  hci_num_reset_loops = 100; // only loop 100 times before trying to send the hci reset command
257  hci_counter = 0;
258  hci_state = HCI_INIT_STATE;
259  waitingForConnection = false;
260  bPollEnable = true;
261 
262 #ifdef DEBUG_USB_HOST
263  Notify(PSTR("\r\nBluetooth Dongle Initialized"), 0x80);
264 #endif
265  }
266  return 0; // Successful configuration
267 
268  /* Diagnostic messages */
269 FailSetDevTblEntry:
270 #ifdef DEBUG_USB_HOST
272  goto Fail;
273 #endif
274 
275 FailGetConfDescr:
276 #ifdef DEBUG_USB_HOST
278  goto Fail;
279 #endif
280 
281 FailSetConfDescr:
282 #ifdef DEBUG_USB_HOST
284 #endif
285  goto Fail;
286 
287 FailUnknownDevice:
288 #ifdef DEBUG_USB_HOST
289  NotifyFailUnknownDevice(VID, PID);
290 #endif
291  pUsb->setAddr(bAddress, 0, 0); // Reset address
293 Fail:
294 #ifdef DEBUG_USB_HOST
295  Notify(PSTR("\r\nBTD Init Failed, error code: "), 0x80);
296  NotifyFail(rcode);
297 #endif
298  Release();
299  return rcode;
300 }
301 
302 void BTD::Initialize() {
303  uint8_t i;
304  for(i = 0; i < BTD_MAX_ENDPOINTS; i++) {
305  epInfo[i].epAddr = 0;
306  epInfo[i].maxPktSize = (i) ? 0 : 8;
307  epInfo[i].bmSndToggle = 0;
308  epInfo[i].bmRcvToggle = 0;
310  }
311  for(i = 0; i < BTD_NUM_SERVICES; i++) {
312  if(btService[i])
313  btService[i]->Reset(); // Reset all Bluetooth services
314  }
315 
316  connectToWii = false;
317  incomingWii = false;
318  connectToHIDDevice = false;
319  incomingHIDDevice = false;
320  incomingPSController = false;
321  bAddress = 0; // Clear device address
322  bNumEP = 1; // Must have to be reset to 1
323  qNextPollTime = 0; // Reset next poll time
324  pollInterval = 0;
325  bPollEnable = false; // Don't start polling before dongle is connected
326  simple_pairing_supported = false;
327 }
328 
329 /* Extracts interrupt-IN, bulk-IN, bulk-OUT endpoint information from config descriptor */
330 void BTD::EndpointXtract(uint8_t conf, uint8_t iface __attribute__((unused)), uint8_t alt, uint8_t proto __attribute__((unused)), const USB_ENDPOINT_DESCRIPTOR *pep) {
331  //ErrorMessage<uint8_t>(PSTR("Conf.Val"),conf);
332  //ErrorMessage<uint8_t>(PSTR("Iface Num"),iface);
333  //ErrorMessage<uint8_t>(PSTR("Alt.Set"),alt);
334 
335  if(alt) // Wrong interface - by BT spec, no alt setting
336  return;
337 
338  bConfNum = conf;
339  uint8_t index;
340 
341  if((pep->bmAttributes & bmUSB_TRANSFER_TYPE) == USB_TRANSFER_TYPE_INTERRUPT && (pep->bEndpointAddress & 0x80) == 0x80) { // Interrupt In endpoint found
342  index = BTD_EVENT_PIPE;
344  } else if((pep->bmAttributes & bmUSB_TRANSFER_TYPE) == USB_TRANSFER_TYPE_BULK) // Bulk endpoint found
345  index = ((pep->bEndpointAddress & 0x80) == 0x80) ? BTD_DATAIN_PIPE : BTD_DATAOUT_PIPE;
346  else
347  return;
348 
349  // Fill the rest of endpoint data structure
350  epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
351  epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize;
352 #ifdef EXTRADEBUG
354 #endif
355  if(pollInterval < pep->bInterval) // Set the polling interval as the largest polling interval obtained from endpoints
356  pollInterval = pep->bInterval;
357  bNumEP++;
358 }
359 
360 void BTD::PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr __attribute__((unused))) {
361 #ifdef EXTRADEBUG
362  Notify(PSTR("\r\nEndpoint descriptor:"), 0x80);
363  Notify(PSTR("\r\nLength:\t\t"), 0x80);
364  D_PrintHex<uint8_t > (ep_ptr->bLength, 0x80);
365  Notify(PSTR("\r\nType:\t\t"), 0x80);
366  D_PrintHex<uint8_t > (ep_ptr->bDescriptorType, 0x80);
367  Notify(PSTR("\r\nAddress:\t"), 0x80);
368  D_PrintHex<uint8_t > (ep_ptr->bEndpointAddress, 0x80);
369  Notify(PSTR("\r\nAttributes:\t"), 0x80);
370  D_PrintHex<uint8_t > (ep_ptr->bmAttributes, 0x80);
371  Notify(PSTR("\r\nMaxPktSize:\t"), 0x80);
372  D_PrintHex<uint16_t > (ep_ptr->wMaxPacketSize, 0x80);
373  Notify(PSTR("\r\nPoll Intrv:\t"), 0x80);
374  D_PrintHex<uint8_t > (ep_ptr->bInterval, 0x80);
375 #endif
376 }
377 
378 /* Performs a cleanup after failed Init() attempt */
379 uint8_t BTD::Release() {
380  Initialize(); // Set all variables, endpoint structs etc. to default values
382  return 0;
383 }
384 
385 uint8_t BTD::Poll() {
386  if(!bPollEnable)
387  return 0;
388  if((int32_t)((uint32_t)millis() - qNextPollTime) >= 0L) { // Don't poll if shorter than polling interval
389  qNextPollTime = (uint32_t)millis() + pollInterval; // Set new poll time
390  HCI_event_task(); // Poll the HCI event pipe
391  HCI_task(); // HCI state machine
392  ACL_event_task(); // Poll the ACL input pipe too
393  }
394  return 0;
395 }
396 
398  for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++)
399  if(btService[i])
400  btService[i]->disconnect();
401 };
402 
403 void BTD::HCI_event_task() {
404  uint16_t length = BULK_MAXPKTSIZE; // Request more than 16 bytes anyway, the inTransfer routine will take care of this
405  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[ BTD_EVENT_PIPE ].epAddr, &length, hcibuf, pollInterval); // Input on endpoint 1
406 
407  if(!rcode || rcode == hrNAK) { // Check for errors
408  switch(hcibuf[0]) { // Switch on event type
409  case EV_COMMAND_COMPLETE:
410  if(!hcibuf[5]) { // Check if command succeeded
411  hci_set_flag(HCI_FLAG_CMD_COMPLETE); // Set command complete flag
412  if((hcibuf[3] == 0x01) && (hcibuf[4] == 0x10)) { // Parameters from read local version information
413  hci_version = hcibuf[6]; // Used to check if it supports 2.0+EDR - see http://www.bluetooth.org/Technical/AssignedNumbers/hci.htm
414 #ifdef EXTRADEBUG
416  Notify(PSTR("\r\nHCI version: "), 0x80);
417  D_PrintHex<uint8_t > (hci_version, 0x80);
418  }
419 #endif
421  } else if((hcibuf[3] == 0x04) && (hcibuf[4] == 0x10)) { // Parameters from read local extended features
423 #ifdef EXTRADEBUG
424  Notify(PSTR("\r\nPage number: "), 0x80);
425  D_PrintHex<uint8_t > (hcibuf[6], 0x80);
426  Notify(PSTR("\r\nMaximum page number: "), 0x80);
427  D_PrintHex<uint8_t > (hcibuf[7], 0x80);
428  Notify(PSTR("\r\nExtended LMP features:"), 0x80);
429  for(uint8_t i = 0; i < 8; i++) {
430  Notify(PSTR(" "), 0x80);
431  D_PrintHex<uint8_t > (hcibuf[8 + i], 0x80);
432  }
433 #endif
434  if(hcibuf[6] == 0) { // Page 0
435 #ifdef DEBUG_USB_HOST
436  Notify(PSTR("\r\nDongle "), 0x80);
437 #endif
438  if(hcibuf[8 + 6] & (1U << 3)) {
439  simple_pairing_supported = true;
440 #ifdef DEBUG_USB_HOST
441  Notify(PSTR("supports"), 0x80);
442 #endif
443  } else {
444  simple_pairing_supported = false;
445 #ifdef DEBUG_USB_HOST
446  Notify(PSTR("does NOT support"), 0x80);
447 #endif
448  }
449 #ifdef DEBUG_USB_HOST
450  Notify(PSTR(" secure simple pairing (controller support)"), 0x80);
451 #endif
452  } else if(hcibuf[6] == 1) { // Page 1
453 #ifdef DEBUG_USB_HOST
454  Notify(PSTR("\r\nDongle "), 0x80);
455  if(hcibuf[8 + 0] & (1U << 0))
456  Notify(PSTR("supports"), 0x80);
457  else
458  Notify(PSTR("does NOT support"), 0x80);
459  Notify(PSTR(" secure simple pairing (host support)"), 0x80);
460 #endif
461  }
462  }
463 
465  } else if((hcibuf[3] == 0x09) && (hcibuf[4] == 0x10)) { // Parameters from read local bluetooth address
466  for(uint8_t i = 0; i < 6; i++)
467  my_bdaddr[i] = hcibuf[6 + i];
469  }
470  }
471  break;
472 
473  case EV_COMMAND_STATUS:
474  if(hcibuf[2]) { // Show status on serial if not OK
475 #ifdef DEBUG_USB_HOST
476  Notify(PSTR("\r\nHCI Command Failed: "), 0x80);
477  D_PrintHex<uint8_t > (hcibuf[2], 0x80);
478  Notify(PSTR("\r\nNum HCI Command Packets: "), 0x80);
479  D_PrintHex<uint8_t > (hcibuf[3], 0x80);
480  Notify(PSTR("\r\nCommand Opcode: "), 0x80);
481  D_PrintHex<uint8_t > (hcibuf[4], 0x80);
482  Notify(PSTR(" "), 0x80);
483  D_PrintHex<uint8_t > (hcibuf[5], 0x80);
484 #endif
485  }
486  break;
487 
488  case EV_INQUIRY_COMPLETE:
489  if(inquiry_counter >= 5 && (pairWithWii || pairWithHIDDevice)) {
490  inquiry_counter = 0;
491 #ifdef DEBUG_USB_HOST
492  if(pairWithWii)
493  Notify(PSTR("\r\nCouldn't find Wiimote"), 0x80);
494  else
495  Notify(PSTR("\r\nCouldn't find HID device"), 0x80);
496 #endif
497  connectToWii = false;
498  pairWithWii = false;
499  connectToHIDDevice = false;
500  pairWithHIDDevice = false;
501  hci_state = HCI_SCANNING_STATE;
502  }
503  inquiry_counter++;
504  break;
505 
506  case EV_INQUIRY_RESULT:
507  if(hcibuf[2]) { // Check that there is more than zero responses
508 #ifdef EXTRADEBUG
509  Notify(PSTR("\r\nNumber of responses: "), 0x80);
510  Notify(hcibuf[2], 0x80);
511 #endif
512  for(uint8_t i = 0; i < hcibuf[2]; i++) {
513  uint8_t offset = 8 * hcibuf[2] + 3 * i;
514 
515  for(uint8_t j = 0; j < 3; j++)
516  classOfDevice[j] = hcibuf[j + 4 + offset];
517 
518 #ifdef EXTRADEBUG
519  Notify(PSTR("\r\nClass of device: "), 0x80);
520  D_PrintHex<uint8_t > (classOfDevice[2], 0x80);
521  Notify(PSTR(" "), 0x80);
522  D_PrintHex<uint8_t > (classOfDevice[1], 0x80);
523  Notify(PSTR(" "), 0x80);
524  D_PrintHex<uint8_t > (classOfDevice[0], 0x80);
525 #endif
526 
527  if(pairWithWii && classOfDevice[2] == 0x00 && (classOfDevice[1] == 0x05) && (classOfDevice[0] & 0x0C)) { // See http://wiibrew.org/wiki/Wiimote#SDP_information
528  checkRemoteName = true; // Check remote name to distinguish between the different controllers
529 
530  for(uint8_t j = 0; j < 6; j++)
531  disc_bdaddr[j] = hcibuf[j + 3 + 6 * i];
532 
534  break;
535  } else if(pairWithHIDDevice && (classOfDevice[1] & 0x0F) == 0x05 && (classOfDevice[0] & 0xC8)) { // Check if it is a mouse, keyboard or a gamepad - see: http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html
536 #ifdef DEBUG_USB_HOST
537  checkRemoteName = true; // Used to print name in the serial monitor if serial debugging is enabled
538 
539  if(classOfDevice[0] & 0x80)
540  Notify(PSTR("\r\nMouse found"), 0x80);
541  if(classOfDevice[0] & 0x40)
542  Notify(PSTR("\r\nKeyboard found"), 0x80);
543  if(classOfDevice[0] & 0x08)
544  Notify(PSTR("\r\nGamepad found"), 0x80);
545 #endif
546  for(uint8_t j = 0; j < 6; j++)
547  disc_bdaddr[j] = hcibuf[j + 3 + 6 * i];
548 
550  break;
551  }
552  }
553  }
554  break;
555 
556  case EV_CONNECT_COMPLETE:
558  if(!hcibuf[2]) { // Check if connected OK
559 #ifdef EXTRADEBUG
560  Notify(PSTR("\r\nConnection established"), 0x80);
561 #endif
562  hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8); // Store the handle for the ACL connection
563  hci_set_flag(HCI_FLAG_CONNECT_COMPLETE); // Set connection complete flag
564  } else {
565  hci_state = HCI_CHECK_DEVICE_SERVICE;
566 #ifdef DEBUG_USB_HOST
567  Notify(PSTR("\r\nConnection Failed: "), 0x80);
568  D_PrintHex<uint8_t > (hcibuf[2], 0x80);
569 #endif
570  }
571  break;
572 
574  if(!hcibuf[2]) { // Check if disconnected OK
575  hci_set_flag(HCI_FLAG_DISCONNECT_COMPLETE); // Set disconnect command complete flag
576  hci_clear_flag(HCI_FLAG_CONNECT_COMPLETE); // Clear connection complete flag
577  }
578  break;
579 
581  if(!hcibuf[2]) { // Check if reading is OK
582  for(uint8_t i = 0; i < min(sizeof (remote_name), sizeof (hcibuf) - 9); i++) {
583  remote_name[i] = hcibuf[9 + i];
584  if(remote_name[i] == '\0') // End of string
585  break;
586  }
587  // TODO: Always set '\0' in remote name!
589  }
590  break;
591 
592  case EV_INCOMING_CONNECT:
593  for(uint8_t i = 0; i < 6; i++)
594  disc_bdaddr[i] = hcibuf[i + 2];
595 
596  for(uint8_t i = 0; i < 3; i++)
597  classOfDevice[i] = hcibuf[i + 8];
598 
599  if((classOfDevice[1] & 0x0F) == 0x05 && (classOfDevice[0] & 0xC8)) { // Check if it is a mouse, keyboard or a gamepad
600 #ifdef DEBUG_USB_HOST
601  if(classOfDevice[0] & 0x80)
602  Notify(PSTR("\r\nMouse is connecting"), 0x80);
603  if(classOfDevice[0] & 0x40)
604  Notify(PSTR("\r\nKeyboard is connecting"), 0x80);
605  if(classOfDevice[0] & 0x08)
606  Notify(PSTR("\r\nGamepad is connecting"), 0x80);
607 #endif
608  incomingHIDDevice = true;
609  }
610 
611 #ifdef EXTRADEBUG
612  Notify(PSTR("\r\nClass of device: "), 0x80);
613  D_PrintHex<uint8_t > (classOfDevice[2], 0x80);
614  Notify(PSTR(" "), 0x80);
615  D_PrintHex<uint8_t > (classOfDevice[1], 0x80);
616  Notify(PSTR(" "), 0x80);
617  D_PrintHex<uint8_t > (classOfDevice[0], 0x80);
618 #endif
620  break;
621 
622  case EV_PIN_CODE_REQUEST:
623  if(pairWithWii) {
624 #ifdef DEBUG_USB_HOST
625  Notify(PSTR("\r\nPairing with Wiimote"), 0x80);
626 #endif
628  } else if(btdPin != NULL) {
629 #ifdef DEBUG_USB_HOST
630  Notify(PSTR("\r\nBluetooth pin is set too: "), 0x80);
631  NotifyStr(btdPin, 0x80);
632 #endif
634  } else {
635 #ifdef DEBUG_USB_HOST
636  Notify(PSTR("\r\nNo pin was set"), 0x80);
637 #endif
639  }
640  break;
641 
642  case EV_LINK_KEY_REQUEST:
643 #ifdef DEBUG_USB_HOST
644  Notify(PSTR("\r\nReceived Key Request"), 0x80);
645 #endif
647  break;
648 
650  if(!hcibuf[2]) { // Check if pairing was successful
651  if(pairWithWii && !connectToWii) {
652 #ifdef DEBUG_USB_HOST
653  Notify(PSTR("\r\nPairing successful with Wiimote"), 0x80);
654 #endif
655  connectToWii = true; // Used to indicate to the Wii service, that it should connect to this device
656  } else if(pairWithHIDDevice && !connectToHIDDevice) {
657 #ifdef DEBUG_USB_HOST
658  Notify(PSTR("\r\nPairing successful with HID device"), 0x80);
659 #endif
660  connectToHIDDevice = true; // Used to indicate to the BTHID service, that it should connect to this device
661  } else {
662 #ifdef EXTRADEBUG
663  Notify(PSTR("\r\nPairing was successful"), 0x80);
664 #endif
665  }
666  } else {
667 #ifdef DEBUG_USB_HOST
668  Notify(PSTR("\r\nPairing Failed: "), 0x80);
669  D_PrintHex<uint8_t > (hcibuf[2], 0x80);
670 #endif
672  hci_state = HCI_DISCONNECT_STATE;
673  }
674  break;
675 
677 #ifdef DEBUG_USB_HOST
678  Notify(PSTR("\r\nReceived IO Capability Request"), 0x80);
679 #endif
681  break;
682 
684 #ifdef EXTRADEBUG
685  Notify(PSTR("\r\nReceived IO Capability Response: "), 0x80);
686  Notify(PSTR("\r\nIO capability: "), 0x80);
687  D_PrintHex<uint8_t > (hcibuf[8], 0x80);
688  Notify(PSTR("\r\nOOB data present: "), 0x80);
689  D_PrintHex<uint8_t > (hcibuf[9], 0x80);
690  Notify(PSTR("\r\nAuthentication request: "), 0x80);
691  D_PrintHex<uint8_t > (hcibuf[10], 0x80);
692 #endif
693  break;
694 
696 #ifdef DEBUG_USB_HOST
697  Notify(PSTR("\r\nUser confirmation Request"), 0x80);
698 #ifdef EXTRADEBUG
699  Notify(PSTR(": \r\nNumeric value: "), 0x80);
700  for(uint8_t i = 0; i < 4; i++) {
701  Notify(PSTR(" "), 0x80);
702  D_PrintHex<uint8_t > (hcibuf[8 + i], 0x80);
703  }
704 #endif
705 #endif
706  // Simply confirm the connection, as the host has no "NoInputNoOutput" capabilities
708  break;
709 
711 #ifdef EXTRADEBUG
712  if(!hcibuf[2]) { // Check if connected OK
713  Notify(PSTR("\r\nSimple Pairing succeeded"), 0x80);
714  } else {
715  Notify(PSTR("\r\nSimple Pairing failed: "), 0x80);
716  D_PrintHex<uint8_t > (hcibuf[2], 0x80);
717  }
718 #endif
719  break;
720 
721  /* We will just ignore the following events */
722  case EV_MAX_SLOTS_CHANGE:
723  case EV_NUM_COMPLETE_PKT:
724  break;
725  case EV_ROLE_CHANGED:
727  case EV_LOOPBACK_COMMAND:
734 #ifdef EXTRADEBUG
735  if(hcibuf[0] != 0x00) {
736  Notify(PSTR("\r\nIgnore HCI Event: "), 0x80);
737  D_PrintHex<uint8_t > (hcibuf[0], 0x80);
738  }
739 #endif
740  break;
741 #ifdef EXTRADEBUG
742  default:
743  if(hcibuf[0] != 0x00) {
744  Notify(PSTR("\r\nUnmanaged HCI Event: "), 0x80);
745  D_PrintHex<uint8_t > (hcibuf[0], 0x80);
746  Notify(PSTR(", data: "), 0x80);
747  for(uint16_t i = 0; i < hcibuf[1]; i++) {
748  D_PrintHex<uint8_t > (hcibuf[2 + i], 0x80);
749  Notify(PSTR(" "), 0x80);
750  }
751  }
752  break;
753 #endif
754  } // Switch
755  }
756 #ifdef EXTRADEBUG
757  else {
758  Notify(PSTR("\r\nHCI event error: "), 0x80);
759  D_PrintHex<uint8_t > (rcode, 0x80);
760  }
761 #endif
762 }
763 
764 /* Poll Bluetooth and print result */
765 void BTD::HCI_task() {
766  switch(hci_state) {
767  case HCI_INIT_STATE:
768  hci_counter++;
769  if(hci_counter > hci_num_reset_loops) { // wait until we have looped x times to clear any old events
770  hci_reset();
771  hci_state = HCI_RESET_STATE;
772  hci_counter = 0;
773  }
774  break;
775 
776  case HCI_RESET_STATE:
777  hci_counter++;
779  hci_counter = 0;
780 #ifdef DEBUG_USB_HOST
781  Notify(PSTR("\r\nHCI Reset complete"), 0x80);
782 #endif
783  hci_state = HCI_CLASS_STATE;
785  } else if(hci_counter > hci_num_reset_loops) {
786  hci_num_reset_loops *= 10;
787  if(hci_num_reset_loops > 2000)
788  hci_num_reset_loops = 2000;
789 #ifdef DEBUG_USB_HOST
790  Notify(PSTR("\r\nNo response to HCI Reset"), 0x80);
791 #endif
792  hci_state = HCI_INIT_STATE;
793  hci_counter = 0;
794  }
795  break;
796 
797  case HCI_CLASS_STATE:
799 #ifdef DEBUG_USB_HOST
800  Notify(PSTR("\r\nWrite class of device"), 0x80);
801 #endif
802  hci_state = HCI_BDADDR_STATE;
803  hci_read_bdaddr();
804  }
805  break;
806 
807  case HCI_BDADDR_STATE:
809 #ifdef DEBUG_USB_HOST
810  Notify(PSTR("\r\nLocal Bluetooth Address: "), 0x80);
811  for(int8_t i = 5; i > 0; i--) {
812  D_PrintHex<uint8_t > (my_bdaddr[i], 0x80);
813  Notify(PSTR(":"), 0x80);
814  }
815  D_PrintHex<uint8_t > (my_bdaddr[0], 0x80);
816 #endif
818  hci_state = HCI_LOCAL_VERSION_STATE;
819  }
820  break;
821 
822  case HCI_LOCAL_VERSION_STATE: // The local version is used by the PS3BT class
824  if(btdName != NULL) {
826  hci_state = HCI_WRITE_NAME_STATE;
827  } else if(useSimplePairing) {
828  hci_read_local_extended_features(0); // "Requests the normal LMP features as returned by Read_Local_Supported_Features"
829  //hci_read_local_extended_features(1); // Read page 1
831  } else
832  hci_state = HCI_CHECK_DEVICE_SERVICE;
833  }
834  break;
835 
838 #ifdef DEBUG_USB_HOST
839  Notify(PSTR("\r\nThe name was set to: "), 0x80);
840  NotifyStr(btdName, 0x80);
841 #endif
842  if(useSimplePairing) {
843  hci_read_local_extended_features(0); // "Requests the normal LMP features as returned by Read_Local_Supported_Features"
844  //hci_read_local_extended_features(1); // Read page 1
846  } else
847  hci_state = HCI_CHECK_DEVICE_SERVICE;
848  }
849  break;
850 
853  if(simple_pairing_supported) {
855  hci_state = HCI_WRITE_SIMPLE_PAIRING_STATE;
856  } else
857  hci_state = HCI_CHECK_DEVICE_SERVICE;
858  }
859  break;
860 
863 #ifdef DEBUG_USB_HOST
864  Notify(PSTR("\r\nSimple pairing was enabled"), 0x80);
865 #endif
867  hci_state = HCI_SET_EVENT_MASK_STATE;
868  }
869  break;
870 
873 #ifdef DEBUG_USB_HOST
874  Notify(PSTR("\r\nSet event mask completed"), 0x80);
875 #endif
876  hci_state = HCI_CHECK_DEVICE_SERVICE;
877  }
878  break;
879 
881  if(pairWithHIDDevice || pairWithWii) { // Check if it should try to connect to a Wiimote
882 #ifdef DEBUG_USB_HOST
883  if(pairWithWii)
884  Notify(PSTR("\r\nStarting inquiry\r\nPress 1 & 2 on the Wiimote\r\nOr press the SYNC button if you are using a Wii U Pro Controller or a Wii Balance Board"), 0x80);
885  else
886  Notify(PSTR("\r\nPlease enable discovery of your device"), 0x80);
887 #endif
888  hci_inquiry();
889  hci_state = HCI_INQUIRY_STATE;
890  } else
891  hci_state = HCI_SCANNING_STATE; // Don't try to connect to a Wiimote
892  break;
893 
894  case HCI_INQUIRY_STATE:
896  hci_inquiry_cancel(); // Stop inquiry
897 #ifdef DEBUG_USB_HOST
898  if(pairWithWii)
899  Notify(PSTR("\r\nWiimote found"), 0x80);
900  else
901  Notify(PSTR("\r\nHID device found"), 0x80);
902 
903  Notify(PSTR("\r\nNow just create the instance like so:"), 0x80);
904  if(pairWithWii)
905  Notify(PSTR("\r\nWII Wii(&Btd);"), 0x80);
906  else
907  Notify(PSTR("\r\nBTHID bthid(&Btd);"), 0x80);
908 
909  Notify(PSTR("\r\nAnd then press any button on the "), 0x80);
910  if(pairWithWii)
911  Notify(PSTR("Wiimote"), 0x80);
912  else
913  Notify(PSTR("device"), 0x80);
914 #endif
915  if(checkRemoteName) {
916  hci_remote_name(); // We need to know the name to distinguish between the Wiimote, the new Wiimote with Motion Plus inside, a Wii U Pro Controller and a Wii Balance Board
917  hci_state = HCI_REMOTE_NAME_STATE;
918  } else
919  hci_state = HCI_CONNECT_DEVICE_STATE;
920  }
921  break;
922 
925 #ifdef DEBUG_USB_HOST
926  if(pairWithWii)
927  Notify(PSTR("\r\nConnecting to Wiimote"), 0x80);
928  else
929  Notify(PSTR("\r\nConnecting to HID device"), 0x80);
930 #endif
931  checkRemoteName = false;
932  hci_connect();
933  hci_state = HCI_CONNECTED_DEVICE_STATE;
934  }
935  break;
936 
940 #ifdef DEBUG_USB_HOST
941  if(pairWithWii)
942  Notify(PSTR("\r\nConnected to Wiimote"), 0x80);
943  else
944  Notify(PSTR("\r\nConnected to HID device"), 0x80);
945 #endif
946  hci_authentication_request(); // This will start the pairing with the device
947  hci_state = HCI_SCANNING_STATE;
948  } else {
949 #ifdef DEBUG_USB_HOST
950  Notify(PSTR("\r\nTrying to connect one more time..."), 0x80);
951 #endif
952  hci_connect(); // Try to connect one more time
953  }
954  }
955  break;
956 
957  case HCI_SCANNING_STATE:
959 #ifdef DEBUG_USB_HOST
960  Notify(PSTR("\r\nWait For Incoming Connection Request"), 0x80);
961 #endif
963  waitingForConnection = true;
964  hci_state = HCI_CONNECT_IN_STATE;
965  }
966  break;
967 
970  waitingForConnection = false;
971 #ifdef DEBUG_USB_HOST
972  Notify(PSTR("\r\nIncoming Connection Request"), 0x80);
973 #endif
974  hci_remote_name();
975  hci_state = HCI_REMOTE_NAME_STATE;
977  hci_state = HCI_DISCONNECT_STATE;
978  break;
979 
982 #ifdef DEBUG_USB_HOST
983  Notify(PSTR("\r\nRemote Name: "), 0x80);
984  for(uint8_t i = 0; i < strlen(remote_name); i++)
985  Notifyc(remote_name[i], 0x80);
986 #endif
987  if(strncmp((const char*)remote_name, "Nintendo", 8) == 0) {
988  incomingWii = true;
989  motionPlusInside = false;
990  wiiUProController = false;
991  pairWiiUsingSync = false;
992 #ifdef DEBUG_USB_HOST
993  Notify(PSTR("\r\nWiimote is connecting"), 0x80);
994 #endif
995  if(strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-TR", 22) == 0) {
996 #ifdef DEBUG_USB_HOST
997  Notify(PSTR(" with Motion Plus Inside"), 0x80);
998 #endif
999  motionPlusInside = true;
1000  } else if(strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-UC", 22) == 0) {
1001 #ifdef DEBUG_USB_HOST
1002  Notify(PSTR(" - Wii U Pro Controller"), 0x80);
1003 #endif
1004  wiiUProController = motionPlusInside = pairWiiUsingSync = true;
1005  } else if(strncmp((const char*)remote_name, "Nintendo RVL-WBC-01", 19) == 0) {
1006 #ifdef DEBUG_USB_HOST
1007  Notify(PSTR(" - Wii Balance Board"), 0x80);
1008 #endif
1009  pairWiiUsingSync = true;
1010  }
1011  }
1012  if(classOfDevice[2] == 0 && classOfDevice[1] == 0x25 && classOfDevice[0] == 0x08 && strncmp((const char*)remote_name, "Wireless Controller", 19) == 0) {
1013 #ifdef DEBUG_USB_HOST
1014  Notify(PSTR("\r\nPS4/PS5 controller is connecting"), 0x80);
1015 #endif
1016  incomingPSController = true;
1017  }
1018  if((pairWithWii || pairWithHIDDevice) && checkRemoteName)
1019  hci_state = HCI_CONNECT_DEVICE_STATE;
1020  else {
1022  hci_state = HCI_CONNECTED_STATE;
1023  }
1024  }
1025  break;
1026 
1027  case HCI_CONNECTED_STATE:
1029 #ifdef DEBUG_USB_HOST
1030  Notify(PSTR("\r\nConnected to Device: "), 0x80);
1031  for(int8_t i = 5; i > 0; i--) {
1032  D_PrintHex<uint8_t > (disc_bdaddr[i], 0x80);
1033  Notify(PSTR(":"), 0x80);
1034  }
1035  D_PrintHex<uint8_t > (disc_bdaddr[0], 0x80);
1036 #endif
1037  if(incomingPSController)
1038  connectToHIDDevice = true; // We should always connect to the PS4/PS5 controller
1039 
1040  // Clear these flags for a new connection
1041  l2capConnectionClaimed = false;
1042  sdpConnectionClaimed = false;
1043  rfcommConnectionClaimed = false;
1044 
1045  hci_event_flag = 0;
1046  hci_state = HCI_DONE_STATE;
1047  }
1048  break;
1049 
1050  case HCI_DONE_STATE:
1051  hci_counter++;
1052  if(hci_counter > 1000) { // Wait until we have looped 1000 times to make sure that the L2CAP connection has been started
1053  hci_counter = 0;
1054  hci_state = HCI_SCANNING_STATE;
1055  }
1056  break;
1057 
1058  case HCI_DISCONNECT_STATE:
1060 #ifdef DEBUG_USB_HOST
1061  Notify(PSTR("\r\nHCI Disconnected from Device"), 0x80);
1062 #endif
1063  hci_event_flag = 0; // Clear all flags
1064 
1065  // Reset all buffers
1066  memset(hcibuf, 0, BULK_MAXPKTSIZE);
1067  memset(l2capinbuf, 0, BULK_MAXPKTSIZE);
1068 
1069  connectToWii = incomingWii = pairWithWii = false;
1070  connectToHIDDevice = incomingHIDDevice = pairWithHIDDevice = checkRemoteName = false;
1071  incomingPSController = false;
1072 
1073  hci_state = HCI_SCANNING_STATE;
1074  }
1075  break;
1076  default:
1077  break;
1078  }
1079 }
1080 
1081 void BTD::ACL_event_task() {
1082  uint16_t length = BULK_MAXPKTSIZE;
1083  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[ BTD_DATAIN_PIPE ].epAddr, &length, l2capinbuf, pollInterval); // Input on endpoint 2
1084 
1085  if(!rcode) { // Check for errors
1086  if(length > 0) { // Check if any data was read
1087  for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++) {
1088  if(btService[i])
1089  btService[i]->ACLData(l2capinbuf);
1090  }
1091  }
1092  }
1093 #ifdef EXTRADEBUG
1094  else if(rcode != hrNAK) {
1095  Notify(PSTR("\r\nACL data in error: "), 0x80);
1096  D_PrintHex<uint8_t > (rcode, 0x80);
1097  }
1098 #endif
1099  for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++)
1100  if(btService[i])
1101  btService[i]->Run();
1102 }
1103 
1104 /************************************************************/
1105 /* HCI Commands */
1106 
1107 /************************************************************/
1108 void BTD::HCI_Command(uint8_t* data, uint16_t nbytes) {
1110  pUsb->ctrlReq(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL);
1111 }
1112 
1114  hci_event_flag = 0; // Clear all the flags
1115  hcibuf[0] = 0x03; // HCI OCF = 3
1116  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1117  hcibuf[2] = 0x00;
1118 
1119  HCI_Command(hcibuf, 3);
1120 }
1121 
1124  hcibuf[0] = 0x1A; // HCI OCF = 1A
1125  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1126  hcibuf[2] = 0x01; // parameter length = 1
1127  if(btdName != NULL)
1128  hcibuf[3] = 0x03; // Inquiry Scan enabled. Page Scan enabled.
1129  else
1130  hcibuf[3] = 0x02; // Inquiry Scan disabled. Page Scan enabled.
1131 
1132  HCI_Command(hcibuf, 4);
1133 }
1134 
1136  hcibuf[0] = 0x1A; // HCI OCF = 1A
1137  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1138  hcibuf[2] = 0x01; // parameter length = 1
1139  hcibuf[3] = 0x00; // Inquiry Scan disabled. Page Scan disabled.
1140 
1141  HCI_Command(hcibuf, 4);
1142 }
1143 
1146  hcibuf[0] = 0x09; // HCI OCF = 9
1147  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
1148  hcibuf[2] = 0x00;
1149 
1150  HCI_Command(hcibuf, 3);
1151 }
1152 
1155  hcibuf[0] = 0x01; // HCI OCF = 1
1156  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
1157  hcibuf[2] = 0x00;
1158 
1159  HCI_Command(hcibuf, 3);
1160 }
1161 
1162 void BTD::hci_read_local_extended_features(uint8_t page_number) {
1164  hcibuf[0] = 0x04; // HCI OCF = 4
1165  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
1166  hcibuf[2] = 0x01; // parameter length = 1
1167  hcibuf[3] = page_number;
1168 
1169  HCI_Command(hcibuf, 4);
1170 }
1171 
1174  hcibuf[0] = 0x09; // HCI OCF = 9
1175  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1176  hcibuf[2] = 0x07; // parameter length 7
1177  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1178  hcibuf[4] = disc_bdaddr[1];
1179  hcibuf[5] = disc_bdaddr[2];
1180  hcibuf[6] = disc_bdaddr[3];
1181  hcibuf[7] = disc_bdaddr[4];
1182  hcibuf[8] = disc_bdaddr[5];
1183  hcibuf[9] = 0x00; // Switch role to master
1184 
1185  HCI_Command(hcibuf, 10);
1186 }
1187 
1190  hcibuf[0] = 0x19; // HCI OCF = 19
1191  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1192  hcibuf[2] = 0x0A; // parameter length = 10
1193  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1194  hcibuf[4] = disc_bdaddr[1];
1195  hcibuf[5] = disc_bdaddr[2];
1196  hcibuf[6] = disc_bdaddr[3];
1197  hcibuf[7] = disc_bdaddr[4];
1198  hcibuf[8] = disc_bdaddr[5];
1199  hcibuf[9] = 0x01; // Page Scan Repetition Mode
1200  hcibuf[10] = 0x00; // Reserved
1201  hcibuf[11] = 0x00; // Clock offset - low byte
1202  hcibuf[12] = 0x00; // Clock offset - high byte
1203 
1204  HCI_Command(hcibuf, 13);
1205 }
1206 
1207 void BTD::hci_write_local_name(const char* name) {
1208  hcibuf[0] = 0x13; // HCI OCF = 13
1209  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1210  hcibuf[2] = strlen(name) + 1; // parameter length = the length of the string + end byte
1211  uint8_t i;
1212  for(i = 0; i < strlen(name); i++)
1213  hcibuf[i + 3] = name[i];
1214  hcibuf[i + 3] = 0x00; // End of string
1215 
1216  HCI_Command(hcibuf, 4 + strlen(name));
1217 }
1218 
1220  hcibuf[0] = 0x01; // HCI OCF = 01
1221  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1222  hcibuf[2] = 0x08;
1223  // The first 6 bytes are the default of 1FFF FFFF FFFF
1224  // However we need to set bits 48-55 for simple pairing to work
1225  hcibuf[3] = 0xFF;
1226  hcibuf[4] = 0xFF;
1227  hcibuf[5] = 0xFF;
1228  hcibuf[6] = 0xFF;
1229  hcibuf[7] = 0xFF;
1230  hcibuf[8] = 0x1F;
1231  hcibuf[9] = 0xFF; // Enable bits 48-55 used for simple pairing
1232  hcibuf[10] = 0x00;
1233 
1234  HCI_Command(hcibuf, 11);
1235 }
1236 
1238  hcibuf[0] = 0x56; // HCI OCF = 56
1239  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1240  hcibuf[2] = 1; // parameter length = 1
1241  hcibuf[3] = enable ? 1 : 0;
1242 
1243  HCI_Command(hcibuf, 4);
1244 }
1245 
1248  hcibuf[0] = 0x01;
1249  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1250  hcibuf[2] = 0x05; // Parameter Total Length = 5
1251  hcibuf[3] = 0x33; // LAP: Genera/Unlimited Inquiry Access Code (GIAC = 0x9E8B33) - see https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
1252  hcibuf[4] = 0x8B;
1253  hcibuf[5] = 0x9E;
1254  hcibuf[6] = 0x30; // Inquiry time = 61.44 sec (maximum)
1255  hcibuf[7] = 0x0A; // 10 number of responses
1256 
1257  HCI_Command(hcibuf, 8);
1258 }
1259 
1261  hcibuf[0] = 0x02;
1262  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1263  hcibuf[2] = 0x00; // Parameter Total Length = 0
1264 
1265  HCI_Command(hcibuf, 3);
1266 }
1267 
1269  hci_connect(disc_bdaddr); // Use last discovered device
1270 }
1271 
1272 void BTD::hci_connect(uint8_t *bdaddr) {
1274  hcibuf[0] = 0x05; // HCI OCF = 5
1275  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1276  hcibuf[2] = 0x0D; // parameter Total Length = 13
1277  hcibuf[3] = bdaddr[0]; // 6 octet bdaddr (LSB)
1278  hcibuf[4] = bdaddr[1];
1279  hcibuf[5] = bdaddr[2];
1280  hcibuf[6] = bdaddr[3];
1281  hcibuf[7] = bdaddr[4];
1282  hcibuf[8] = bdaddr[5];
1283  hcibuf[9] = 0x18; // DM1 or DH1 may be used
1284  hcibuf[10] = 0xCC; // DM3, DH3, DM5, DH5 may be used
1285  hcibuf[11] = 0x01; // Page repetition mode R1
1286  hcibuf[12] = 0x00; // Reserved
1287  hcibuf[13] = 0x00; // Clock offset
1288  hcibuf[14] = 0x00; // Invalid clock offset
1289  hcibuf[15] = 0x00; // Do not allow role switch
1290 
1291  HCI_Command(hcibuf, 16);
1292 }
1293 
1295  hcibuf[0] = 0x0D; // HCI OCF = 0D
1296  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1297  hcibuf[2] = 0x17; // parameter length 23
1298  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1299  hcibuf[4] = disc_bdaddr[1];
1300  hcibuf[5] = disc_bdaddr[2];
1301  hcibuf[6] = disc_bdaddr[3];
1302  hcibuf[7] = disc_bdaddr[4];
1303  hcibuf[8] = disc_bdaddr[5];
1304  if(pairWithWii) {
1305  hcibuf[9] = 6; // Pin length is the length of the Bluetooth address
1306  if(pairWiiUsingSync) {
1307 #ifdef DEBUG_USB_HOST
1308  Notify(PSTR("\r\nPairing with Wii controller via SYNC"), 0x80);
1309 #endif
1310  for(uint8_t i = 0; i < 6; i++)
1311  hcibuf[10 + i] = my_bdaddr[i]; // The pin is the Bluetooth dongles Bluetooth address backwards
1312  } else {
1313  for(uint8_t i = 0; i < 6; i++)
1314  hcibuf[10 + i] = disc_bdaddr[i]; // The pin is the Wiimote's Bluetooth address backwards
1315  }
1316  for(uint8_t i = 16; i < 26; i++)
1317  hcibuf[i] = 0x00; // The rest should be 0
1318  } else {
1319  hcibuf[9] = strlen(btdPin); // Length of pin
1320  uint8_t i;
1321  for(i = 0; i < strlen(btdPin); i++) // The maximum size of the pin is 16
1322  hcibuf[i + 10] = btdPin[i];
1323  for(; i < 16; i++)
1324  hcibuf[i + 10] = 0x00; // The rest should be 0
1325  }
1326 
1327  HCI_Command(hcibuf, 26);
1328 }
1329 
1331  hcibuf[0] = 0x0E; // HCI OCF = 0E
1332  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1333  hcibuf[2] = 0x06; // parameter length 6
1334  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1335  hcibuf[4] = disc_bdaddr[1];
1336  hcibuf[5] = disc_bdaddr[2];
1337  hcibuf[6] = disc_bdaddr[3];
1338  hcibuf[7] = disc_bdaddr[4];
1339  hcibuf[8] = disc_bdaddr[5];
1340 
1341  HCI_Command(hcibuf, 9);
1342 }
1343 
1345  hcibuf[0] = 0x0C; // HCI OCF = 0C
1346  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1347  hcibuf[2] = 0x06; // parameter length 6
1348  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1349  hcibuf[4] = disc_bdaddr[1];
1350  hcibuf[5] = disc_bdaddr[2];
1351  hcibuf[6] = disc_bdaddr[3];
1352  hcibuf[7] = disc_bdaddr[4];
1353  hcibuf[8] = disc_bdaddr[5];
1354 
1355  HCI_Command(hcibuf, 9);
1356 }
1357 
1359  hcibuf[0] = 0x2B; // HCI OCF = 2B
1360  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1361  hcibuf[2] = 0x09;
1362  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1363  hcibuf[4] = disc_bdaddr[1];
1364  hcibuf[5] = disc_bdaddr[2];
1365  hcibuf[6] = disc_bdaddr[3];
1366  hcibuf[7] = disc_bdaddr[4];
1367  hcibuf[8] = disc_bdaddr[5];
1368  hcibuf[9] = 0x03; // NoInputNoOutput
1369  hcibuf[10] = 0x00; // OOB authentication data not present
1370  hcibuf[11] = 0x00; // MITM Protection Not Required – No Bonding. Numeric comparison with automatic accept allowed
1371 
1372  HCI_Command(hcibuf, 12);
1373 }
1374 
1376  hcibuf[0] = 0x2C; // HCI OCF = 2C
1377  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1378  hcibuf[2] = 0x06; // parameter length 6
1379  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1380  hcibuf[4] = disc_bdaddr[1];
1381  hcibuf[5] = disc_bdaddr[2];
1382  hcibuf[6] = disc_bdaddr[3];
1383  hcibuf[7] = disc_bdaddr[4];
1384  hcibuf[8] = disc_bdaddr[5];
1385 
1386  HCI_Command(hcibuf, 9);
1387 }
1388 
1390  hcibuf[0] = 0x11; // HCI OCF = 11
1391  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1392  hcibuf[2] = 0x02; // parameter length = 2
1393  hcibuf[3] = (uint8_t)(hci_handle & 0xFF); //connection handle - low byte
1394  hcibuf[4] = (uint8_t)((hci_handle >> 8) & 0x0F); //connection handle - high byte
1395 
1396  HCI_Command(hcibuf, 5);
1397 }
1398 
1399 void BTD::hci_disconnect(uint16_t handle) { // This is called by the different services
1401  hcibuf[0] = 0x06; // HCI OCF = 6
1402  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1403  hcibuf[2] = 0x03; // parameter length = 3
1404  hcibuf[3] = (uint8_t)(handle & 0xFF); //connection handle - low byte
1405  hcibuf[4] = (uint8_t)((handle >> 8) & 0x0F); //connection handle - high byte
1406  hcibuf[5] = 0x13; // reason
1407 
1408  HCI_Command(hcibuf, 6);
1409 }
1410 
1411 void BTD::hci_write_class_of_device() { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html
1412  hcibuf[0] = 0x24; // HCI OCF = 24
1413  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1414  hcibuf[2] = 0x03; // parameter length = 3
1415  hcibuf[3] = 0x04; // Robot
1416  hcibuf[4] = 0x08; // Toy
1417  hcibuf[5] = 0x00;
1418 
1419  HCI_Command(hcibuf, 6);
1420 }
1421 /*******************************************************************
1422  * *
1423  * HCI ACL Data Packet *
1424  * *
1425  * buf[0] buf[1] buf[2] buf[3]
1426  * 0 4 8 11 12 16 24 31 MSB
1427  * .-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1428  * | HCI Handle |PB |BC | Data Total Length | HCI ACL Data Packet
1429  * .-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1430  *
1431  * buf[4] buf[5] buf[6] buf[7]
1432  * 0 8 16 31 MSB
1433  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1434  * | Length | Channel ID | Basic L2CAP header
1435  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1436  *
1437  * buf[8] buf[9] buf[10] buf[11]
1438  * 0 8 16 31 MSB
1439  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1440  * | Code | Identifier | Length | Control frame (C-frame)
1441  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-. (signaling packet format)
1442  */
1443 /************************************************************/
1444 /* L2CAP Commands */
1445 
1446 /************************************************************/
1447 void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh) {
1448  uint8_t buf[8 + nbytes];
1449  buf[0] = (uint8_t)(handle & 0xff); // HCI handle with PB,BC flag
1450  buf[1] = (uint8_t)(((handle >> 8) & 0x0f) | 0x20);
1451  buf[2] = (uint8_t)((4 + nbytes) & 0xff); // HCI ACL total data length
1452  buf[3] = (uint8_t)((4 + nbytes) >> 8);
1453  buf[4] = (uint8_t)(nbytes & 0xff); // L2CAP header: Length
1454  buf[5] = (uint8_t)(nbytes >> 8);
1455  buf[6] = channelLow;
1456  buf[7] = channelHigh;
1457 
1458  for(uint16_t i = 0; i < nbytes; i++) // L2CAP C-frame
1459  buf[8 + i] = data[i];
1460 
1461  uint8_t rcode = pUsb->outTransfer(bAddress, epInfo[ BTD_DATAOUT_PIPE ].epAddr, (8 + nbytes), buf);
1462  if(rcode) {
1463  delay(100); // This small delay prevents it from overflowing if it fails
1464 #ifdef DEBUG_USB_HOST
1465  Notify(PSTR("\r\nError sending L2CAP message: 0x"), 0x80);
1466  D_PrintHex<uint8_t > (rcode, 0x80);
1467  Notify(PSTR(" - Channel ID: "), 0x80);
1468  D_PrintHex<uint8_t > (channelHigh, 0x80);
1469  Notify(PSTR(" "), 0x80);
1470  D_PrintHex<uint8_t > (channelLow, 0x80);
1471 #endif
1472  }
1473 }
1474 
1475 void BTD::l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm) {
1476  l2capoutbuf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code
1477  l2capoutbuf[1] = rxid; // Identifier
1478  l2capoutbuf[2] = 0x04; // Length
1479  l2capoutbuf[3] = 0x00;
1480  l2capoutbuf[4] = (uint8_t)(psm & 0xff); // PSM
1481  l2capoutbuf[5] = (uint8_t)(psm >> 8);
1482  l2capoutbuf[6] = scid[0]; // Source CID
1483  l2capoutbuf[7] = scid[1];
1484 
1485  L2CAP_Command(handle, l2capoutbuf, 8);
1486 }
1487 
1488 void BTD::l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result) {
1489  l2capoutbuf[0] = L2CAP_CMD_CONNECTION_RESPONSE; // Code
1490  l2capoutbuf[1] = rxid; // Identifier
1491  l2capoutbuf[2] = 0x08; // Length
1492  l2capoutbuf[3] = 0x00;
1493  l2capoutbuf[4] = dcid[0]; // Destination CID
1494  l2capoutbuf[5] = dcid[1];
1495  l2capoutbuf[6] = scid[0]; // Source CID
1496  l2capoutbuf[7] = scid[1];
1497  l2capoutbuf[8] = result; // Result: Pending or Success
1498  l2capoutbuf[9] = 0x00;
1499  l2capoutbuf[10] = 0x00; // No further information
1500  l2capoutbuf[11] = 0x00;
1501 
1502  L2CAP_Command(handle, l2capoutbuf, 12);
1503 }
1504 
1505 void BTD::l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid) {
1506  l2capoutbuf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code
1507  l2capoutbuf[1] = rxid; // Identifier
1508  l2capoutbuf[2] = 0x08; // Length
1509  l2capoutbuf[3] = 0x00;
1510  l2capoutbuf[4] = dcid[0]; // Destination CID
1511  l2capoutbuf[5] = dcid[1];
1512  l2capoutbuf[6] = 0x00; // Flags
1513  l2capoutbuf[7] = 0x00;
1514  l2capoutbuf[8] = 0x01; // Config Opt: type = MTU (Maximum Transmission Unit) - Hint
1515  l2capoutbuf[9] = 0x02; // Config Opt: length
1516  l2capoutbuf[10] = 0xFF; // MTU
1517  l2capoutbuf[11] = 0xFF;
1518 
1519  L2CAP_Command(handle, l2capoutbuf, 12);
1520 }
1521 
1522 void BTD::l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid) {
1523  l2capoutbuf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code
1524  l2capoutbuf[1] = rxid; // Identifier
1525  l2capoutbuf[2] = 0x0A; // Length
1526  l2capoutbuf[3] = 0x00;
1527  l2capoutbuf[4] = scid[0]; // Source CID
1528  l2capoutbuf[5] = scid[1];
1529  l2capoutbuf[6] = 0x00; // Flag
1530  l2capoutbuf[7] = 0x00;
1531  l2capoutbuf[8] = 0x00; // Result
1532  l2capoutbuf[9] = 0x00;
1533  l2capoutbuf[10] = 0x01; // Config
1534  l2capoutbuf[11] = 0x02;
1535  l2capoutbuf[12] = 0xA0;
1536  l2capoutbuf[13] = 0x02;
1537 
1538  L2CAP_Command(handle, l2capoutbuf, 14);
1539 }
1540 
1541 void BTD::l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
1542  l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_REQUEST; // Code
1543  l2capoutbuf[1] = rxid; // Identifier
1544  l2capoutbuf[2] = 0x04; // Length
1545  l2capoutbuf[3] = 0x00;
1546  l2capoutbuf[4] = dcid[0];
1547  l2capoutbuf[5] = dcid[1];
1548  l2capoutbuf[6] = scid[0];
1549  l2capoutbuf[7] = scid[1];
1550 
1551  L2CAP_Command(handle, l2capoutbuf, 8);
1552 }
1553 
1554 void BTD::l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
1555  l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_RESPONSE; // Code
1556  l2capoutbuf[1] = rxid; // Identifier
1557  l2capoutbuf[2] = 0x04; // Length
1558  l2capoutbuf[3] = 0x00;
1559  l2capoutbuf[4] = dcid[0];
1560  l2capoutbuf[5] = dcid[1];
1561  l2capoutbuf[6] = scid[0];
1562  l2capoutbuf[7] = scid[1];
1563 
1564  L2CAP_Command(handle, l2capoutbuf, 8);
1565 }
1566 
1567 void BTD::l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh) {
1568  l2capoutbuf[0] = L2CAP_CMD_INFORMATION_RESPONSE; // Code
1569  l2capoutbuf[1] = rxid; // Identifier
1570  l2capoutbuf[2] = 0x08; // Length
1571  l2capoutbuf[3] = 0x00;
1572  l2capoutbuf[4] = infoTypeLow;
1573  l2capoutbuf[5] = infoTypeHigh;
1574  l2capoutbuf[6] = 0x00; // Result = success
1575  l2capoutbuf[7] = 0x00; // Result = success
1576  l2capoutbuf[8] = 0x00;
1577  l2capoutbuf[9] = 0x00;
1578  l2capoutbuf[10] = 0x00;
1579  l2capoutbuf[11] = 0x00;
1580 
1581  L2CAP_Command(handle, l2capoutbuf, 12);
1582 }
1583 
1584 /* PS3 Commands - only set Bluetooth address is implemented in this library */
1585 void BTD::setBdaddr(uint8_t* bdaddr) {
1586  /* Set the internal Bluetooth address */
1587  uint8_t buf[8];
1588  buf[0] = 0x01;
1589  buf[1] = 0x00;
1590 
1591  for(uint8_t i = 0; i < 6; i++)
1592  buf[i + 2] = bdaddr[5 - i]; // Copy into buffer, has to be written reversed, so it is MSB first
1593 
1594  // 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
1595  pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
1596 }
1597 
1598 void BTD::setMoveBdaddr(uint8_t* bdaddr) {
1599  /* Set the internal Bluetooth address */
1600  uint8_t buf[11];
1601  buf[0] = 0x05;
1602  buf[7] = 0x10;
1603  buf[8] = 0x01;
1604  buf[9] = 0x02;
1605  buf[10] = 0x12;
1606 
1607  for(uint8_t i = 0; i < 6; i++)
1608  buf[i + 1] = bdaddr[i];
1609 
1610  // 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
1611  pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);
1612 }
BTD::hci_disconnect
void hci_disconnect(uint16_t handle)
Definition: BTD.cpp:1399
USB::outTransfer
uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data)
Definition: Usb.cpp:303
BTD.h
hrNAK
#define hrNAK
Definition: max3421e.h:218
BTD::bConfNum
uint8_t bConfNum
Definition: BTD.h:545
USB_DEVICE_DESCRIPTOR::idVendor
uint16_t idVendor
Definition: usb_ch9.h:113
BTD::hci_read_bdaddr
void hci_read_bdaddr()
Definition: BTD.cpp:1144
EV_INQUIRY_COMPLETE
#define EV_INQUIRY_COMPLETE
Definition: BTD.h:84
BTD::hci_write_scan_disable
void hci_write_scan_disable()
Definition: BTD.cpp:1135
HCI_FLAG_LOCAL_EXTENDED_FEATURES
#define HCI_FLAG_LOCAL_EXTENDED_FEATURES
Definition: BTD.h:76
EV_NUM_COMPLETE_PKT
#define EV_NUM_COMPLETE_PKT
Definition: BTD.h:98
USB_ENDPOINT_DESCRIPTOR::bInterval
uint8_t bInterval
Definition: usb_ch9.h:154
USB_DEVICE_DESCRIPTOR::idProduct
uint16_t idProduct
Definition: usb_ch9.h:114
AddressPool::GetUsbDevicePtr
virtual UsbDevice * GetUsbDevicePtr(uint8_t addr)=0
BTD::BTD_CONTROL_PIPE
static const uint8_t BTD_CONTROL_PIPE
Definition: BTD.h:552
bmREQ_HID_OUT
#define bmREQ_HID_OUT
Definition: usbhid.h:63
BTD::hci_write_local_name
void hci_write_local_name(const char *name)
Definition: BTD.cpp:1207
EV_ROLE_CHANGED
#define EV_ROLE_CHANGED
Definition: BTD.h:97
BTD::L2CAP_Command
void L2CAP_Command(uint16_t handle, uint8_t *data, uint8_t nbytes, uint8_t channelLow=0x01, uint8_t channelHigh=0x00)
Definition: BTD.cpp:1447
EpInfo::bmSndToggle
uint8_t bmSndToggle
Definition: address.h:47
AddressPool
Definition: address.h:90
BTD::hci_accept_connection
void hci_accept_connection()
Definition: BTD.cpp:1172
USB_DEVICE_DESCRIPTOR::bNumConfigurations
uint8_t bNumConfigurations
Definition: usb_ch9.h:119
L2CAP_CMD_CONNECTION_RESPONSE
#define L2CAP_CMD_CONNECTION_RESPONSE
Definition: BTD.h:177
BTD::l2cap_config_request
void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t *dcid)
Definition: BTD.cpp:1505
HCI_CHECK_DEVICE_SERVICE
#define HCI_CHECK_DEVICE_SERVICE
Definition: BTD.h:49
NotifyFail
#define NotifyFail(...)
Definition: message.h:62
UsbDevice::lowspeed
bool lowspeed
Definition: address.h:86
USB_TRANSFER_TYPE_INTERRUPT
#define USB_TRANSFER_TYPE_INTERRUPT
Definition: usb_ch9.h:93
USB::RegisterDeviceClass
uint8_t RegisterDeviceClass(USBDeviceConfig *pdev)
Definition: UsbCore.h:230
AddressPool::AllocAddress
virtual uint8_t AllocAddress(uint8_t parent, bool is_hub=false, uint8_t port=0)=0
USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
#define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
Definition: UsbCore.h:98
BTD::hci_read_local_version_information
void hci_read_local_version_information()
Definition: BTD.cpp:1153
EV_SIMPLE_PAIRING_COMPLETE
#define EV_SIMPLE_PAIRING_COMPLETE
Definition: BTD.h:110
HCI_FLAG_CONNECT_EVENT
#define HCI_FLAG_CONNECT_EVENT
Definition: BTD.h:75
NotifyFailSetDevTblEntry
#define NotifyFailSetDevTblEntry(...)
Definition: message.h:58
BluetoothService::Reset
virtual void Reset()=0
BTD::incomingWii
bool incomingWii
Definition: BTD.h:504
USB::getDevDescr
uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr)
defined(USB_METHODS_INLINE)
Definition: Usb.cpp:801
PS3MOVE_PID
#define PS3MOVE_PID
Definition: BTD.h:28
BTD::epInfo
EpInfo epInfo[BTD_MAX_ENDPOINTS]
Definition: BTD.h:542
USB_ENDPOINT_DESCRIPTOR
Definition: usb_ch9.h:148
HCI_LOCAL_EXTENDED_FEATURES_STATE
#define HCI_LOCAL_EXTENDED_FEATURES_STATE
Definition: BTD.h:62
HCI_FLAG_REMOTE_NAME_COMPLETE
#define HCI_FLAG_REMOTE_NAME_COMPLETE
Definition: BTD.h:70
EV_USER_CONFIRMATION_REQUEST
#define EV_USER_CONFIRMATION_REQUEST
Definition: BTD.h:109
EV_IO_CAPABILITY_REQUEST
#define EV_IO_CAPABILITY_REQUEST
Definition: BTD.h:107
HCI_CLASS_STATE
#define HCI_CLASS_STATE
Definition: BTD.h:45
NotifyFailGetConfDescr
#define NotifyFailGetConfDescr(...)
Definition: message.h:59
BTD_NUM_SERVICES
#define BTD_NUM_SERVICES
Definition: BTD.h:211
HCI_INQUIRY_STATE
#define HCI_INQUIRY_STATE
Definition: BTD.h:51
BTD::bNumEP
uint8_t bNumEP
Definition: BTD.h:547
HCI_FLAG_DISCONNECT_COMPLETE
#define HCI_FLAG_DISCONNECT_COMPLETE
Definition: BTD.h:69
EpInfo::epAddr
uint8_t epAddr
Definition: address.h:40
EV_AUTHENTICATION_COMPLETE
#define EV_AUTHENTICATION_COMPLETE
Definition: BTD.h:89
BluetoothService::ACLData
virtual void ACLData(uint8_t *ACLData)=0
Notify
#define Notify(...)
Definition: message.h:51
BTD::connectToWii
bool connectToWii
Definition: BTD.h:500
HCI_FLAG_CMD_COMPLETE
#define HCI_FLAG_CMD_COMPLETE
Definition: BTD.h:67
IOGEAR_GBU521_PID
#define IOGEAR_GBU521_PID
Definition: BTD.h:32
BTD::hci_write_class_of_device
void hci_write_class_of_device()
Definition: BTD.cpp:1411
EV_REMOTE_NAME_COMPLETE
#define EV_REMOTE_NAME_COMPLETE
Definition: BTD.h:90
bmREQ_HCI_OUT
#define bmREQ_HCI_OUT
Definition: BTD.h:40
EV_IO_CAPABILITY_RESPONSE
#define EV_IO_CAPABILITY_RESPONSE
Definition: BTD.h:108
L2CAP_CMD_DISCONNECT_RESPONSE
#define L2CAP_CMD_DISCONNECT_RESPONSE
Definition: BTD.h:181
NotifyFailGetDevDescr
#define NotifyFailGetDevDescr(...)
Definition: message.h:57
HID_REQUEST_SET_REPORT
#define HID_REQUEST_SET_REPORT
Definition: usbhid.h:72
L2CAP_CMD_DISCONNECT_REQUEST
#define L2CAP_CMD_DISCONNECT_REQUEST
Definition: BTD.h:180
USB_DEVICE_DESCRIPTOR
Definition: usb_ch9.h:105
EpInfo::bmNakPower
uint8_t bmNakPower
Definition: address.h:49
BluetoothService::disconnect
virtual void disconnect()=0
BTD::hci_read_local_extended_features
void hci_read_local_extended_features(uint8_t page_number)
Definition: BTD.cpp:1162
BTD::btdPin
const char * btdPin
Definition: BTD.h:479
HCI_BDADDR_STATE
#define HCI_BDADDR_STATE
Definition: BTD.h:46
BTD::hci_remote_name
void hci_remote_name()
Definition: BTD.cpp:1188
HCI_REMOTE_NAME_STATE
#define HCI_REMOTE_NAME_STATE
Definition: BTD.h:57
EV_COMMAND_STATUS
#define EV_COMMAND_STATUS
Definition: BTD.h:96
PS3_VID
#define PS3_VID
Definition: BTD.h:25
HCI_INIT_STATE
#define HCI_INIT_STATE
Definition: BTD.h:43
BTD::hci_user_confirmation_request_reply
void hci_user_confirmation_request_reply()
Definition: BTD.cpp:1375
BTD::HCI_Command
void HCI_Command(uint8_t *data, uint16_t nbytes)
Definition: BTD.cpp:1108
HCI_CONNECT_IN_STATE
#define HCI_CONNECT_IN_STATE
Definition: BTD.h:56
USB::getConfDescr
uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr)
Definition: Usb.cpp:806
HCI_WRITE_NAME_STATE
#define HCI_WRITE_NAME_STATE
Definition: BTD.h:48
BELKIN_F8T065BF_PID
#define BELKIN_F8T065BF_PID
Definition: BTD.h:34
HCI_WRITE_SIMPLE_PAIRING_STATE
#define HCI_WRITE_SIMPLE_PAIRING_STATE
Definition: BTD.h:63
BTD::motionPlusInside
bool motionPlusInside
Definition: BTD.h:508
BTD::l2cap_config_response
void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t *scid)
Definition: BTD.cpp:1522
HCI_RESET_STATE
#define HCI_RESET_STATE
Definition: BTD.h:44
BTD::BTD
BTD(USB *p)
Definition: BTD.cpp:27
EV_QOS_SETUP_COMPLETE
#define EV_QOS_SETUP_COMPLETE
Definition: BTD.h:94
BTD::hci_pin_code_request_reply
void hci_pin_code_request_reply()
Definition: BTD.cpp:1294
HCI_FLAG_READ_BDADDR
#define HCI_FLAG_READ_BDADDR
Definition: BTD.h:72
USB_DEVICE_DESCRIPTOR::bMaxPacketSize0
uint8_t bMaxPacketSize0
Definition: usb_ch9.h:112
L2CAP_CMD_INFORMATION_RESPONSE
#define L2CAP_CMD_INFORMATION_RESPONSE
Definition: BTD.h:183
HCI_CONNECTED_STATE
#define HCI_CONNECTED_STATE
Definition: BTD.h:58
EV_DISCONNECT_COMPLETE
#define EV_DISCONNECT_COMPLETE
Definition: BTD.h:88
EV_DATA_BUFFER_OVERFLOW
#define EV_DATA_BUFFER_OVERFLOW
Definition: BTD.h:102
USB_NAK_MAX_POWER
#define USB_NAK_MAX_POWER
Definition: address.h:34
BTD::pUsb
USB * pUsb
Definition: BTD.h:538
BTD::BTD_DATAIN_PIPE
static const uint8_t BTD_DATAIN_PIPE
Definition: BTD.h:556
BTD::hci_write_scan_enable
void hci_write_scan_enable()
Definition: BTD.cpp:1122
BTD::useSimplePairing
bool useSimplePairing
Definition: BTD.h:531
Notifyc
#define Notifyc(...)
Definition: message.h:53
BTD::remote_name
char remote_name[30]
Definition: BTD.h:488
EV_LOOPBACK_COMMAND
#define EV_LOOPBACK_COMMAND
Definition: BTD.h:104
bmUSB_TRANSFER_TYPE
#define bmUSB_TRANSFER_TYPE
Definition: usb_ch9.h:94
BTD::disconnect
void disconnect()
Definition: BTD.cpp:397
BTD::BTD_DATAOUT_PIPE
static const uint8_t BTD_DATAOUT_PIPE
Definition: BTD.h:558
EpInfo
Definition: address.h:39
EV_LINK_KEY_NOTIFICATION
#define EV_LINK_KEY_NOTIFICATION
Definition: BTD.h:101
BTD::l2cap_information_response
void l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh)
Definition: BTD.cpp:1567
BTD::hci_reset
void hci_reset()
Definition: BTD.cpp:1113
BTD::connectToHIDDevice
bool connectToHIDDevice
Definition: BTD.h:517
BTD::hci_connect
void hci_connect()
Definition: BTD.cpp:1268
USB_ENDPOINT_DESCRIPTOR::bEndpointAddress
uint8_t bEndpointAddress
Definition: usb_ch9.h:151
USB_NAK_NOWAIT
#define USB_NAK_NOWAIT
Definition: address.h:36
EpInfo::bmRcvToggle
uint8_t bmRcvToggle
Definition: address.h:48
HCI_DONE_STATE
#define HCI_DONE_STATE
Definition: BTD.h:60
BTD::sdpConnectionClaimed
bool sdpConnectionClaimed
Definition: BTD.h:472
BTD::l2cap_disconnection_response
void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid)
Definition: BTD.cpp:1554
USB::ctrlReq
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
BTD::hci_version
uint8_t hci_version
Definition: BTD.h:494
EV_CONNECT_COMPLETE
#define EV_CONNECT_COMPLETE
Definition: BTD.h:86
NotifyFailUnknownDevice
#define NotifyFailUnknownDevice(...)
Definition: message.h:61
EV_INCOMING_CONNECT
#define EV_INCOMING_CONNECT
Definition: BTD.h:87
HCI_DISCONNECT_STATE
#define HCI_DISCONNECT_STATE
Definition: BTD.h:61
BTD::incomingHIDDevice
bool incomingHIDDevice
Definition: BTD.h:521
hrJERR
#define hrJERR
Definition: max3421e.h:227
BTD::l2cap_connection_request
void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t *scid, uint16_t psm)
Definition: BTD.cpp:1475
BTD::qNextPollTime
uint32_t qNextPollTime
Definition: BTD.h:549
EV_INQUIRY_RESULT
#define EV_INQUIRY_RESULT
Definition: BTD.h:85
USB
Definition: UsbCore.h:210
BTD::bAddress
uint8_t bAddress
Definition: BTD.h:540
BTD::rfcommConnectionClaimed
bool rfcommConnectionClaimed
Definition: BTD.h:474
USB::inTransfer
uint8_t inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t *data, uint8_t bInterval=0)
Definition: Usb.cpp:209
BTD::btdName
const char * btdName
Definition: BTD.h:477
BTD::hci_pin_code_negative_request_reply
void hci_pin_code_negative_request_reply()
Definition: BTD.cpp:1330
L
@ L
Definition: controllerEnums.h:179
BTD::PrintEndpointDescriptor
void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR *ep_ptr)
Definition: BTD.cpp:360
hci_clear_flag
#define hci_clear_flag(flag)
Definition: BTD.h:81
USB_ERROR_EPINFO_IS_NULL
#define USB_ERROR_EPINFO_IS_NULL
Definition: UsbCore.h:96
HCI_FLAG_INCOMING_REQUEST
#define HCI_FLAG_INCOMING_REQUEST
Definition: BTD.h:71
BTD::l2cap_disconnection_request
void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid)
Definition: BTD.cpp:1541
PS3NAVIGATION_PID
#define PS3NAVIGATION_PID
Definition: BTD.h:27
UsbDevice
Definition: address.h:82
BTD::pairWithWii
bool pairWithWii
Definition: BTD.h:506
BTD::Init
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: BTD.cpp:136
hci_check_flag
#define hci_check_flag(flag)
Definition: BTD.h:79
BTD_MAX_ENDPOINTS
#define BTD_MAX_ENDPOINTS
Definition: BTD.h:210
EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE
#define EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE
Definition: BTD.h:93
BTD::waitingForConnection
bool waitingForConnection
Definition: BTD.h:468
BTD::hci_link_key_request_negative_reply
void hci_link_key_request_negative_reply()
Definition: BTD.cpp:1344
BTD::hci_inquiry_cancel
void hci_inquiry_cancel()
Definition: BTD.cpp:1260
L2CAP_CMD_CONFIG_RESPONSE
#define L2CAP_CMD_CONFIG_RESPONSE
Definition: BTD.h:179
BTD::EndpointXtract
void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep)
Definition: BTD.cpp:330
HCI_CONNECT_DEVICE_STATE
#define HCI_CONNECT_DEVICE_STATE
Definition: BTD.h:52
BTD::BTD_EVENT_PIPE
static const uint8_t BTD_EVENT_PIPE
Definition: BTD.h:554
USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition: UsbCore.h:95
IOGEAR_GBU521_VID
#define IOGEAR_GBU521_VID
Definition: BTD.h:31
HCI_CONNECTED_DEVICE_STATE
#define HCI_CONNECTED_DEVICE_STATE
Definition: BTD.h:53
BluetoothService::Run
virtual void Run()=0
BTD::my_bdaddr
uint8_t my_bdaddr[6]
Definition: BTD.h:482
BTD::hci_set_event_mask
void hci_set_event_mask()
Definition: BTD.cpp:1219
USB::setConf
uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value)
Definition: Usb.cpp:845
ConfigDescParser
Definition: confdescparser.h:47
BTD::hci_authentication_request
void hci_authentication_request()
Definition: BTD.cpp:1389
BTD::Poll
uint8_t Poll()
Definition: BTD.cpp:385
EV_ENCRYPTION_CHANGE
#define EV_ENCRYPTION_CHANGE
Definition: BTD.h:91
PSTR
#define PSTR(str)
Definition: version_helper.h:54
HCI_LOCAL_VERSION_STATE
#define HCI_LOCAL_VERSION_STATE
Definition: BTD.h:47
BTD::hci_write_simple_pairing_mode
void hci_write_simple_pairing_mode(bool enable)
Definition: BTD.cpp:1237
BTD::Release
uint8_t Release()
Definition: BTD.cpp:379
BTD::hci_io_capability_request_reply
void hci_io_capability_request_reply()
Definition: BTD.cpp:1358
PS3_PID
#define PS3_PID
Definition: BTD.h:26
USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition: UsbCore.h:93
EV_LINK_KEY_REQUEST
#define EV_LINK_KEY_REQUEST
Definition: BTD.h:100
USB_DEVICE_DESCRIPTOR::bDeviceClass
uint8_t bDeviceClass
Definition: usb_ch9.h:109
HCI_FLAG_DEVICE_FOUND
#define HCI_FLAG_DEVICE_FOUND
Definition: BTD.h:74
BTD::disc_bdaddr
uint8_t disc_bdaddr[6]
Definition: BTD.h:486
NotifyFailSetConfDescr
#define NotifyFailSetConfDescr(...)
Definition: message.h:60
BTD::l2capConnectionClaimed
bool l2capConnectionClaimed
Definition: BTD.h:470
BTD::l2cap_connection_response
void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid, uint8_t result)
Definition: BTD.cpp:1488
BTD::pairWithHIDDevice
bool pairWithHIDDevice
Definition: BTD.h:523
EV_COMMAND_COMPLETE
#define EV_COMMAND_COMPLETE
Definition: BTD.h:95
AddressPool::FreeAddress
virtual void FreeAddress(uint8_t addr)=0
HCI_FLAG_CONNECT_COMPLETE
#define HCI_FLAG_CONNECT_COMPLETE
Definition: BTD.h:68
USB_ERROR_FailGetDevDescr
#define USB_ERROR_FailGetDevDescr
Definition: UsbCore.h:102
USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
#define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
Definition: UsbCore.h:90
USB::setAddr
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition: Usb.cpp:836
EpInfo::maxPktSize
uint8_t maxPktSize
Definition: address.h:41
L2CAP_CMD_CONNECTION_REQUEST
#define L2CAP_CMD_CONNECTION_REQUEST
Definition: BTD.h:176
hci_set_flag
#define hci_set_flag(flag)
Definition: BTD.h:80
USB::setEpInfoEntry
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition: Usb.cpp:64
L2CAP_CMD_CONFIG_REQUEST
#define L2CAP_CMD_CONFIG_REQUEST
Definition: BTD.h:178
USB_ENDPOINT_DESCRIPTOR::bmAttributes
uint8_t bmAttributes
Definition: usb_ch9.h:152
EV_PIN_CODE_REQUEST
#define EV_PIN_CODE_REQUEST
Definition: BTD.h:99
BTD::hci_handle
uint16_t hci_handle
Definition: BTD.h:484
EV_CHANGE_CONNECTION_LINK
#define EV_CHANGE_CONNECTION_LINK
Definition: BTD.h:92
BELKIN_F8T065BF_VID
#define BELKIN_F8T065BF_VID
Definition: BTD.h:33
BULK_MAXPKTSIZE
#define BULK_MAXPKTSIZE
Definition: BTD.h:37
HCI_FLAG_READ_VERSION
#define HCI_FLAG_READ_VERSION
Definition: BTD.h:73
HCI_SCANNING_STATE
#define HCI_SCANNING_STATE
Definition: BTD.h:55
HCI_SET_EVENT_MASK_STATE
#define HCI_SET_EVENT_MASK_STATE
Definition: BTD.h:64
USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET
#define USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET
Definition: UsbCore.h:101
EV_PAGE_SCAN_REP_MODE
#define EV_PAGE_SCAN_REP_MODE
Definition: BTD.h:105
NotifyStr
#define NotifyStr(...)
Definition: message.h:52
USB_TRANSFER_TYPE_BULK
#define USB_TRANSFER_TYPE_BULK
Definition: usb_ch9.h:92
EV_MAX_SLOTS_CHANGE
#define EV_MAX_SLOTS_CHANGE
Definition: BTD.h:103
BTD::ConfigureDevice
uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed)
Definition: BTD.cpp:50
USB::GetAddressPool
AddressPool & GetAddressPool()
Definition: UsbCore.h:226
BTD::wiiUProController
bool wiiUProController
Definition: BTD.h:510
USB_ENDPOINT_DESCRIPTOR::wMaxPacketSize
uint16_t wMaxPacketSize
Definition: usb_ch9.h:153
UsbDevice::epinfo
EpInfo * epinfo
Definition: address.h:83
BTD::hci_inquiry
void hci_inquiry()
Definition: BTD.cpp:1246