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:
508  if(hcibuf[2]) { // Check that there is more than zero responses
509 #ifdef EXTRADEBUG
510  Notify(PSTR("\r\nNumber of responses: "), 0x80);
511  Notify(hcibuf[2], 0x80); // This will always be 1 for an extended inquiry result
512 #endif
513  for(uint8_t i = 0; i < hcibuf[2]; i++) {
514  uint8_t classOfDevice_offset;
515  if(hcibuf[0] == EV_INQUIRY_RESULT)
516  classOfDevice_offset = 9 * hcibuf[2]; // 6-byte bd_addr, 1 byte page_scan_repetition_mode, 2 byte reserved
517  else
518  classOfDevice_offset = 8 * hcibuf[2]; // 6-byte bd_addr, 1 byte page_scan_repetition_mode, 1 byte reserved
519 
520  for(uint8_t j = 0; j < 3; j++)
521  classOfDevice[j] = hcibuf[3 + classOfDevice_offset + 3 * i + j];
522 
523 #ifdef EXTRADEBUG
524  Notify(PSTR("\r\nClass of device: "), 0x80);
525  D_PrintHex<uint8_t > (classOfDevice[2], 0x80);
526  Notify(PSTR(" "), 0x80);
527  D_PrintHex<uint8_t > (classOfDevice[1], 0x80);
528  Notify(PSTR(" "), 0x80);
529  D_PrintHex<uint8_t > (classOfDevice[0], 0x80);
530 #endif
531 
532  if(pairWithWii && (classOfDevice[2] == 0x00) && ((classOfDevice[1] & 0x0F) == 0x05) && (classOfDevice[0] & 0x0C)) { // See http://wiibrew.org/wiki/Wiimote#SDP_information
533  checkRemoteName = true; // Check remote name to distinguish between the different controllers
534 
535  for(uint8_t j = 0; j < 6; j++)
536  disc_bdaddr[j] = hcibuf[3 + 6 * i + j];
537 
539  break;
540  } 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
541 #ifdef DEBUG_USB_HOST
542  checkRemoteName = true; // Used to print name in the serial monitor if serial debugging is enabled
543 
544  if(classOfDevice[0] & 0x80)
545  Notify(PSTR("\r\nMouse found"), 0x80);
546  if(classOfDevice[0] & 0x40)
547  Notify(PSTR("\r\nKeyboard found"), 0x80);
548  if(classOfDevice[0] & 0x08)
549  Notify(PSTR("\r\nGamepad found"), 0x80);
550 #endif
551  for(uint8_t j = 0; j < 6; j++)
552  disc_bdaddr[j] = hcibuf[3 + 6 * i + j];
553 
555  break;
556  }
557  }
558  }
559  break;
560 
561  case EV_CONNECT_COMPLETE:
563  if(!hcibuf[2]) { // Check if connected OK
564 #ifdef EXTRADEBUG
565  Notify(PSTR("\r\nConnection established"), 0x80);
566 #endif
567  hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8); // Store the handle for the ACL connection
568  hci_set_flag(HCI_FLAG_CONNECT_COMPLETE); // Set connection complete flag
569  } else {
570  hci_state = HCI_CHECK_DEVICE_SERVICE;
571 #ifdef DEBUG_USB_HOST
572  Notify(PSTR("\r\nConnection Failed: "), 0x80);
573  D_PrintHex<uint8_t > (hcibuf[2], 0x80);
574 #endif
575  }
576  break;
577 
579  if(!hcibuf[2]) { // Check if disconnected OK
580  hci_set_flag(HCI_FLAG_DISCONNECT_COMPLETE); // Set disconnect command complete flag
581  hci_clear_flag(HCI_FLAG_CONNECT_COMPLETE); // Clear connection complete flag
582  }
583  break;
584 
586  if(!hcibuf[2]) { // Check if reading is OK
587  for(uint8_t i = 0; i < min(sizeof (remote_name), sizeof (hcibuf) - 9); i++) {
588  remote_name[i] = hcibuf[9 + i];
589  if(remote_name[i] == '\0') // End of string
590  break;
591  }
592  // TODO: Always set '\0' in remote name!
594  }
595  break;
596 
597  case EV_INCOMING_CONNECT:
598  for(uint8_t i = 0; i < 6; i++)
599  disc_bdaddr[i] = hcibuf[i + 2];
600 
601  for(uint8_t i = 0; i < 3; i++)
602  classOfDevice[i] = hcibuf[i + 8];
603 
604  if(((classOfDevice[1] & 0x0F) == 0x05) && (classOfDevice[0] & 0xC8)) { // Check if it is a mouse, keyboard or a gamepad
605 #ifdef DEBUG_USB_HOST
606  if(classOfDevice[0] & 0x80)
607  Notify(PSTR("\r\nMouse is connecting"), 0x80);
608  if(classOfDevice[0] & 0x40)
609  Notify(PSTR("\r\nKeyboard is connecting"), 0x80);
610  if(classOfDevice[0] & 0x08)
611  Notify(PSTR("\r\nGamepad is connecting"), 0x80);
612 #endif
613  incomingHIDDevice = true;
614  }
615 
616 #ifdef EXTRADEBUG
617  Notify(PSTR("\r\nClass of device: "), 0x80);
618  D_PrintHex<uint8_t > (classOfDevice[2], 0x80);
619  Notify(PSTR(" "), 0x80);
620  D_PrintHex<uint8_t > (classOfDevice[1], 0x80);
621  Notify(PSTR(" "), 0x80);
622  D_PrintHex<uint8_t > (classOfDevice[0], 0x80);
623 #endif
625  break;
626 
627  case EV_PIN_CODE_REQUEST:
628  if(pairWithWii) {
629 #ifdef DEBUG_USB_HOST
630  Notify(PSTR("\r\nPairing with Wiimote"), 0x80);
631 #endif
633  } else if(btdPin != NULL) {
634 #ifdef DEBUG_USB_HOST
635  Notify(PSTR("\r\nBluetooth pin is set too: "), 0x80);
636  NotifyStr(btdPin, 0x80);
637 #endif
639  } else {
640 #ifdef DEBUG_USB_HOST
641  Notify(PSTR("\r\nNo pin was set"), 0x80);
642 #endif
644  }
645  break;
646 
647  case EV_LINK_KEY_REQUEST:
648 #ifdef DEBUG_USB_HOST
649  Notify(PSTR("\r\nReceived Key Request"), 0x80);
650 #endif
652  break;
653 
655  if(!hcibuf[2]) { // Check if pairing was successful
656  if(pairWithWii && !connectToWii) {
657 #ifdef DEBUG_USB_HOST
658  Notify(PSTR("\r\nPairing successful with Wiimote"), 0x80);
659 #endif
660  connectToWii = true; // Used to indicate to the Wii service, that it should connect to this device
661  } else if(pairWithHIDDevice && !connectToHIDDevice) {
662 #ifdef DEBUG_USB_HOST
663  Notify(PSTR("\r\nPairing successful with HID device"), 0x80);
664 #endif
665  connectToHIDDevice = true; // Used to indicate to the BTHID service, that it should connect to this device
666  } else {
667 #ifdef EXTRADEBUG
668  Notify(PSTR("\r\nPairing was successful"), 0x80);
669 #endif
670  }
671  } else {
672 #ifdef DEBUG_USB_HOST
673  Notify(PSTR("\r\nPairing Failed: "), 0x80);
674  D_PrintHex<uint8_t > (hcibuf[2], 0x80);
675 #endif
677  hci_state = HCI_DISCONNECT_STATE;
678  }
679  break;
680 
682 #ifdef DEBUG_USB_HOST
683  Notify(PSTR("\r\nReceived IO Capability Request"), 0x80);
684 #endif
686  break;
687 
689 #ifdef EXTRADEBUG
690  Notify(PSTR("\r\nReceived IO Capability Response: "), 0x80);
691  Notify(PSTR("\r\nIO capability: "), 0x80);
692  D_PrintHex<uint8_t > (hcibuf[8], 0x80);
693  Notify(PSTR("\r\nOOB data present: "), 0x80);
694  D_PrintHex<uint8_t > (hcibuf[9], 0x80);
695  Notify(PSTR("\r\nAuthentication request: "), 0x80);
696  D_PrintHex<uint8_t > (hcibuf[10], 0x80);
697 #endif
698  break;
699 
701 #ifdef DEBUG_USB_HOST
702  Notify(PSTR("\r\nUser confirmation Request"), 0x80);
703 #ifdef EXTRADEBUG
704  Notify(PSTR(": \r\nNumeric value: "), 0x80);
705  for(uint8_t i = 0; i < 4; i++) {
706  Notify(PSTR(" "), 0x80);
707  D_PrintHex<uint8_t > (hcibuf[8 + i], 0x80);
708  }
709 #endif
710 #endif
711  // Simply confirm the connection, as the host has no "NoInputNoOutput" capabilities
713  break;
714 
716 #ifdef EXTRADEBUG
717  if(!hcibuf[2]) { // Check if connected OK
718  Notify(PSTR("\r\nSimple Pairing succeeded"), 0x80);
719  } else {
720  Notify(PSTR("\r\nSimple Pairing failed: "), 0x80);
721  D_PrintHex<uint8_t > (hcibuf[2], 0x80);
722  }
723 #endif
724  break;
725 
726  /* We will just ignore the following events */
727  case EV_MAX_SLOTS_CHANGE:
728  case EV_NUM_COMPLETE_PKT:
729  break;
730  case EV_ROLE_CHANGED:
732  case EV_LOOPBACK_COMMAND:
739 #ifdef EXTRADEBUG
740  if(hcibuf[0] != 0x00) {
741  Notify(PSTR("\r\nIgnore HCI Event: "), 0x80);
742  D_PrintHex<uint8_t > (hcibuf[0], 0x80);
743  }
744 #endif
745  break;
746 #ifdef EXTRADEBUG
747  default:
748  if(hcibuf[0] != 0x00) {
749  Notify(PSTR("\r\nUnmanaged HCI Event: "), 0x80);
750  D_PrintHex<uint8_t > (hcibuf[0], 0x80);
751  Notify(PSTR(", data: "), 0x80);
752  for(uint16_t i = 0; i < hcibuf[1]; i++) {
753  D_PrintHex<uint8_t > (hcibuf[2 + i], 0x80);
754  Notify(PSTR(" "), 0x80);
755  }
756  }
757  break;
758 #endif
759  } // Switch
760  }
761 #ifdef EXTRADEBUG
762  else {
763  Notify(PSTR("\r\nHCI event error: "), 0x80);
764  D_PrintHex<uint8_t > (rcode, 0x80);
765  }
766 #endif
767 }
768 
769 /* Poll Bluetooth and print result */
770 void BTD::HCI_task() {
771  switch(hci_state) {
772  case HCI_INIT_STATE:
773  hci_counter++;
774  if(hci_counter > hci_num_reset_loops) { // wait until we have looped x times to clear any old events
775  hci_reset();
776  hci_state = HCI_RESET_STATE;
777  hci_counter = 0;
778  }
779  break;
780 
781  case HCI_RESET_STATE:
782  hci_counter++;
784  hci_counter = 0;
785 #ifdef DEBUG_USB_HOST
786  Notify(PSTR("\r\nHCI Reset complete"), 0x80);
787 #endif
788  hci_state = HCI_CLASS_STATE;
790  } else if(hci_counter > hci_num_reset_loops) {
791  hci_num_reset_loops *= 10;
792  if(hci_num_reset_loops > 2000)
793  hci_num_reset_loops = 2000;
794 #ifdef DEBUG_USB_HOST
795  Notify(PSTR("\r\nNo response to HCI Reset"), 0x80);
796 #endif
797  hci_state = HCI_INIT_STATE;
798  hci_counter = 0;
799  }
800  break;
801 
802  case HCI_CLASS_STATE:
804 #ifdef DEBUG_USB_HOST
805  Notify(PSTR("\r\nWrite class of device"), 0x80);
806 #endif
807  hci_state = HCI_BDADDR_STATE;
808  hci_read_bdaddr();
809  }
810  break;
811 
812  case HCI_BDADDR_STATE:
814 #ifdef DEBUG_USB_HOST
815  Notify(PSTR("\r\nLocal Bluetooth Address: "), 0x80);
816  for(int8_t i = 5; i > 0; i--) {
817  D_PrintHex<uint8_t > (my_bdaddr[i], 0x80);
818  Notify(PSTR(":"), 0x80);
819  }
820  D_PrintHex<uint8_t > (my_bdaddr[0], 0x80);
821 #endif
823  hci_state = HCI_LOCAL_VERSION_STATE;
824  }
825  break;
826 
827  case HCI_LOCAL_VERSION_STATE: // The local version is used by the PS3BT class
829  if(btdName != NULL) {
831  hci_state = HCI_WRITE_NAME_STATE;
832  } else if(useSimplePairing) {
833  hci_read_local_extended_features(0); // "Requests the normal LMP features as returned by Read_Local_Supported_Features"
834  //hci_read_local_extended_features(1); // Read page 1
836  } else
837  hci_state = HCI_CHECK_DEVICE_SERVICE;
838  }
839  break;
840 
843 #ifdef DEBUG_USB_HOST
844  Notify(PSTR("\r\nThe name was set to: "), 0x80);
845  NotifyStr(btdName, 0x80);
846 #endif
847  if(useSimplePairing) {
848  hci_read_local_extended_features(0); // "Requests the normal LMP features as returned by Read_Local_Supported_Features"
849  //hci_read_local_extended_features(1); // Read page 1
851  } else
852  hci_state = HCI_CHECK_DEVICE_SERVICE;
853  }
854  break;
855 
858  if(simple_pairing_supported) {
860  hci_state = HCI_WRITE_SIMPLE_PAIRING_STATE;
861  } else
862  hci_state = HCI_CHECK_DEVICE_SERVICE;
863  }
864  break;
865 
868 #ifdef DEBUG_USB_HOST
869  Notify(PSTR("\r\nSimple pairing was enabled"), 0x80);
870 #endif
872  hci_state = HCI_SET_EVENT_MASK_STATE;
873  }
874  break;
875 
878 #ifdef DEBUG_USB_HOST
879  Notify(PSTR("\r\nSet event mask completed"), 0x80);
880 #endif
881  hci_state = HCI_CHECK_DEVICE_SERVICE;
882  }
883  break;
884 
886  if(pairWithHIDDevice || pairWithWii) { // Check if it should try to connect to a Wiimote
887 #ifdef DEBUG_USB_HOST
888  if(pairWithWii)
889  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);
890  else
891  Notify(PSTR("\r\nPlease enable discovery of your device"), 0x80);
892 #endif
893  hci_inquiry();
894  hci_state = HCI_INQUIRY_STATE;
895  } else
896  hci_state = HCI_SCANNING_STATE; // Don't try to connect to a Wiimote
897  break;
898 
899  case HCI_INQUIRY_STATE:
901  hci_inquiry_cancel(); // Stop inquiry
902 #ifdef DEBUG_USB_HOST
903  if(pairWithWii)
904  Notify(PSTR("\r\nWiimote found"), 0x80);
905  else
906  Notify(PSTR("\r\nHID device found"), 0x80);
907 
908  Notify(PSTR("\r\nNow just create the instance like so:"), 0x80);
909  if(pairWithWii)
910  Notify(PSTR("\r\nWII Wii(&Btd);"), 0x80);
911  else
912  Notify(PSTR("\r\nBTHID bthid(&Btd);"), 0x80);
913 
914  Notify(PSTR("\r\nAnd then press any button on the "), 0x80);
915  if(pairWithWii)
916  Notify(PSTR("Wiimote"), 0x80);
917  else
918  Notify(PSTR("device"), 0x80);
919 #endif
920  if(checkRemoteName) {
921  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
922  hci_state = HCI_REMOTE_NAME_STATE;
923  } else
924  hci_state = HCI_CONNECT_DEVICE_STATE;
925  }
926  break;
927 
930 #ifdef DEBUG_USB_HOST
931  if(pairWithWii)
932  Notify(PSTR("\r\nConnecting to Wiimote"), 0x80);
933  else
934  Notify(PSTR("\r\nConnecting to HID device"), 0x80);
935 #endif
936  checkRemoteName = false;
937  hci_connect();
938  hci_state = HCI_CONNECTED_DEVICE_STATE;
939  }
940  break;
941 
945 #ifdef DEBUG_USB_HOST
946  if(pairWithWii)
947  Notify(PSTR("\r\nConnected to Wiimote"), 0x80);
948  else
949  Notify(PSTR("\r\nConnected to HID device"), 0x80);
950 #endif
951  hci_authentication_request(); // This will start the pairing with the device
952  hci_state = HCI_SCANNING_STATE;
953  } else {
954 #ifdef DEBUG_USB_HOST
955  Notify(PSTR("\r\nTrying to connect one more time..."), 0x80);
956 #endif
957  hci_connect(); // Try to connect one more time
958  }
959  }
960  break;
961 
962  case HCI_SCANNING_STATE:
964 #ifdef DEBUG_USB_HOST
965  Notify(PSTR("\r\nWait For Incoming Connection Request"), 0x80);
966 #endif
968  waitingForConnection = true;
969  hci_state = HCI_CONNECT_IN_STATE;
970  }
971  break;
972 
975  waitingForConnection = false;
976 #ifdef DEBUG_USB_HOST
977  Notify(PSTR("\r\nIncoming Connection Request"), 0x80);
978 #endif
979  hci_remote_name();
980  hci_state = HCI_REMOTE_NAME_STATE;
982  hci_state = HCI_DISCONNECT_STATE;
983  break;
984 
987 #ifdef DEBUG_USB_HOST
988  Notify(PSTR("\r\nRemote Name: "), 0x80);
989  for(uint8_t i = 0; i < strlen(remote_name); i++)
990  Notifyc(remote_name[i], 0x80);
991 #endif
992  if(strncmp((const char*)remote_name, "Nintendo", 8) == 0) {
993  incomingWii = true;
994  motionPlusInside = false;
995  wiiUProController = false;
996  pairWiiUsingSync = false;
997 #ifdef DEBUG_USB_HOST
998  Notify(PSTR("\r\nWiimote is connecting"), 0x80);
999 #endif
1000  if(strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-TR", 22) == 0) {
1001 #ifdef DEBUG_USB_HOST
1002  Notify(PSTR(" with Motion Plus Inside"), 0x80);
1003 #endif
1004  motionPlusInside = true;
1005  } else if(strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-UC", 22) == 0) {
1006 #ifdef DEBUG_USB_HOST
1007  Notify(PSTR(" - Wii U Pro Controller"), 0x80);
1008 #endif
1009  wiiUProController = motionPlusInside = pairWiiUsingSync = true;
1010  } else if(strncmp((const char*)remote_name, "Nintendo RVL-WBC-01", 19) == 0) {
1011 #ifdef DEBUG_USB_HOST
1012  Notify(PSTR(" - Wii Balance Board"), 0x80);
1013 #endif
1014  pairWiiUsingSync = true;
1015  }
1016  }
1017  if(classOfDevice[2] == 0 && classOfDevice[1] == 0x25 && classOfDevice[0] == 0x08 && strncmp((const char*)remote_name, "Wireless Controller", 19) == 0) {
1018 #ifdef DEBUG_USB_HOST
1019  Notify(PSTR("\r\nPS4/PS5 controller is connecting"), 0x80);
1020 #endif
1021  incomingPSController = true;
1022  }
1023  if((pairWithWii || pairWithHIDDevice) && checkRemoteName)
1024  hci_state = HCI_CONNECT_DEVICE_STATE;
1025  else {
1027  hci_state = HCI_CONNECTED_STATE;
1028  }
1029  }
1030  break;
1031 
1032  case HCI_CONNECTED_STATE:
1034 #ifdef DEBUG_USB_HOST
1035  Notify(PSTR("\r\nConnected to Device: "), 0x80);
1036  for(int8_t i = 5; i > 0; i--) {
1037  D_PrintHex<uint8_t > (disc_bdaddr[i], 0x80);
1038  Notify(PSTR(":"), 0x80);
1039  }
1040  D_PrintHex<uint8_t > (disc_bdaddr[0], 0x80);
1041 #endif
1042  if(incomingPSController)
1043  connectToHIDDevice = true; // We should always connect to the PS4/PS5 controller
1044 
1045  // Clear these flags for a new connection
1046  l2capConnectionClaimed = false;
1047  sdpConnectionClaimed = false;
1048  rfcommConnectionClaimed = false;
1049 
1050  hci_event_flag = 0;
1051  hci_state = HCI_DONE_STATE;
1052  }
1053  break;
1054 
1055  case HCI_DONE_STATE:
1056  hci_counter++;
1057  if(hci_counter > 1000) { // Wait until we have looped 1000 times to make sure that the L2CAP connection has been started
1058  hci_counter = 0;
1059  hci_state = HCI_SCANNING_STATE;
1060  }
1061  break;
1062 
1063  case HCI_DISCONNECT_STATE:
1065 #ifdef DEBUG_USB_HOST
1066  Notify(PSTR("\r\nHCI Disconnected from Device"), 0x80);
1067 #endif
1068  hci_event_flag = 0; // Clear all flags
1069 
1070  // Reset all buffers
1071  memset(hcibuf, 0, BULK_MAXPKTSIZE);
1072  memset(l2capinbuf, 0, BULK_MAXPKTSIZE);
1073 
1074  connectToWii = incomingWii = pairWithWii = false;
1075  connectToHIDDevice = incomingHIDDevice = pairWithHIDDevice = checkRemoteName = false;
1076  incomingPSController = false;
1077 
1078  hci_state = HCI_SCANNING_STATE;
1079  }
1080  break;
1081  default:
1082  break;
1083  }
1084 }
1085 
1086 void BTD::ACL_event_task() {
1087  uint16_t length = BULK_MAXPKTSIZE;
1088  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[ BTD_DATAIN_PIPE ].epAddr, &length, l2capinbuf, pollInterval); // Input on endpoint 2
1089 
1090  if(!rcode) { // Check for errors
1091  if(length > 0) { // Check if any data was read
1092  for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++) {
1093  if(btService[i])
1094  btService[i]->ACLData(l2capinbuf);
1095  }
1096  }
1097  }
1098 #ifdef EXTRADEBUG
1099  else if(rcode != hrNAK) {
1100  Notify(PSTR("\r\nACL data in error: "), 0x80);
1101  D_PrintHex<uint8_t > (rcode, 0x80);
1102  }
1103 #endif
1104  for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++)
1105  if(btService[i])
1106  btService[i]->Run();
1107 }
1108 
1109 /************************************************************/
1110 /* HCI Commands */
1111 
1112 /************************************************************/
1113 void BTD::HCI_Command(uint8_t* data, uint16_t nbytes) {
1115  pUsb->ctrlReq(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL);
1116 }
1117 
1119  hci_event_flag = 0; // Clear all the flags
1120  hcibuf[0] = 0x03; // HCI OCF = 3
1121  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1122  hcibuf[2] = 0x00;
1123 
1124  HCI_Command(hcibuf, 3);
1125 }
1126 
1129  hcibuf[0] = 0x1A; // HCI OCF = 1A
1130  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1131  hcibuf[2] = 0x01; // parameter length = 1
1132  if(btdName != NULL)
1133  hcibuf[3] = 0x03; // Inquiry Scan enabled. Page Scan enabled.
1134  else
1135  hcibuf[3] = 0x02; // Inquiry Scan disabled. Page Scan enabled.
1136 
1137  HCI_Command(hcibuf, 4);
1138 }
1139 
1141  hcibuf[0] = 0x1A; // HCI OCF = 1A
1142  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1143  hcibuf[2] = 0x01; // parameter length = 1
1144  hcibuf[3] = 0x00; // Inquiry Scan disabled. Page Scan disabled.
1145 
1146  HCI_Command(hcibuf, 4);
1147 }
1148 
1151  hcibuf[0] = 0x09; // HCI OCF = 9
1152  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
1153  hcibuf[2] = 0x00;
1154 
1155  HCI_Command(hcibuf, 3);
1156 }
1157 
1160  hcibuf[0] = 0x01; // HCI OCF = 1
1161  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
1162  hcibuf[2] = 0x00;
1163 
1164  HCI_Command(hcibuf, 3);
1165 }
1166 
1167 void BTD::hci_read_local_extended_features(uint8_t page_number) {
1169  hcibuf[0] = 0x04; // HCI OCF = 4
1170  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
1171  hcibuf[2] = 0x01; // parameter length = 1
1172  hcibuf[3] = page_number;
1173 
1174  HCI_Command(hcibuf, 4);
1175 }
1176 
1179  hcibuf[0] = 0x09; // HCI OCF = 9
1180  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1181  hcibuf[2] = 0x07; // parameter length 7
1182  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1183  hcibuf[4] = disc_bdaddr[1];
1184  hcibuf[5] = disc_bdaddr[2];
1185  hcibuf[6] = disc_bdaddr[3];
1186  hcibuf[7] = disc_bdaddr[4];
1187  hcibuf[8] = disc_bdaddr[5];
1188  hcibuf[9] = 0x00; // Switch role to master
1189 
1190  HCI_Command(hcibuf, 10);
1191 }
1192 
1195  hcibuf[0] = 0x19; // HCI OCF = 19
1196  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1197  hcibuf[2] = 0x0A; // parameter length = 10
1198  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1199  hcibuf[4] = disc_bdaddr[1];
1200  hcibuf[5] = disc_bdaddr[2];
1201  hcibuf[6] = disc_bdaddr[3];
1202  hcibuf[7] = disc_bdaddr[4];
1203  hcibuf[8] = disc_bdaddr[5];
1204  hcibuf[9] = 0x01; // Page Scan Repetition Mode
1205  hcibuf[10] = 0x00; // Reserved
1206  hcibuf[11] = 0x00; // Clock offset - low byte
1207  hcibuf[12] = 0x00; // Clock offset - high byte
1208 
1209  HCI_Command(hcibuf, 13);
1210 }
1211 
1212 void BTD::hci_write_local_name(const char* name) {
1213  hcibuf[0] = 0x13; // HCI OCF = 13
1214  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1215  hcibuf[2] = strlen(name) + 1; // parameter length = the length of the string + end byte
1216  uint8_t i;
1217  for(i = 0; i < strlen(name); i++)
1218  hcibuf[i + 3] = name[i];
1219  hcibuf[i + 3] = 0x00; // End of string
1220 
1221  HCI_Command(hcibuf, 4 + strlen(name));
1222 }
1223 
1225  hcibuf[0] = 0x01; // HCI OCF = 01
1226  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1227  hcibuf[2] = 0x08;
1228  // The first 6 bytes are the default of 1FFF FFFF FFFF
1229  // However we need to set bits 48-55 for simple pairing to work
1230  hcibuf[3] = 0xFF;
1231  hcibuf[4] = 0xFF;
1232  hcibuf[5] = 0xFF;
1233  hcibuf[6] = 0xFF;
1234  hcibuf[7] = 0xFF;
1235  hcibuf[8] = 0x1F;
1236  hcibuf[9] = 0xFF; // Enable bits 48-55 used for simple pairing
1237  hcibuf[10] = 0x00;
1238 
1239  HCI_Command(hcibuf, 11);
1240 }
1241 
1243  hcibuf[0] = 0x56; // HCI OCF = 56
1244  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1245  hcibuf[2] = 1; // parameter length = 1
1246  hcibuf[3] = enable ? 1 : 0;
1247 
1248  HCI_Command(hcibuf, 4);
1249 }
1250 
1253  hcibuf[0] = 0x01;
1254  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1255  hcibuf[2] = 0x05; // Parameter Total Length = 5
1256  hcibuf[3] = 0x33; // LAP: Genera/Unlimited Inquiry Access Code (GIAC = 0x9E8B33) - see https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
1257  hcibuf[4] = 0x8B;
1258  hcibuf[5] = 0x9E;
1259  hcibuf[6] = 0x30; // Inquiry time = 61.44 sec (maximum)
1260  hcibuf[7] = 0x0A; // 10 number of responses
1261 
1262  HCI_Command(hcibuf, 8);
1263 }
1264 
1266  hcibuf[0] = 0x02;
1267  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1268  hcibuf[2] = 0x00; // Parameter Total Length = 0
1269 
1270  HCI_Command(hcibuf, 3);
1271 }
1272 
1274  hci_connect(disc_bdaddr); // Use last discovered device
1275 }
1276 
1277 void BTD::hci_connect(uint8_t *bdaddr) {
1279  hcibuf[0] = 0x05; // HCI OCF = 5
1280  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1281  hcibuf[2] = 0x0D; // parameter Total Length = 13
1282  hcibuf[3] = bdaddr[0]; // 6 octet bdaddr (LSB)
1283  hcibuf[4] = bdaddr[1];
1284  hcibuf[5] = bdaddr[2];
1285  hcibuf[6] = bdaddr[3];
1286  hcibuf[7] = bdaddr[4];
1287  hcibuf[8] = bdaddr[5];
1288  hcibuf[9] = 0x18; // DM1 or DH1 may be used
1289  hcibuf[10] = 0xCC; // DM3, DH3, DM5, DH5 may be used
1290  hcibuf[11] = 0x01; // Page repetition mode R1
1291  hcibuf[12] = 0x00; // Reserved
1292  hcibuf[13] = 0x00; // Clock offset
1293  hcibuf[14] = 0x00; // Invalid clock offset
1294  hcibuf[15] = 0x00; // Do not allow role switch
1295 
1296  HCI_Command(hcibuf, 16);
1297 }
1298 
1300  hcibuf[0] = 0x0D; // HCI OCF = 0D
1301  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1302  hcibuf[2] = 0x17; // parameter length 23
1303  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1304  hcibuf[4] = disc_bdaddr[1];
1305  hcibuf[5] = disc_bdaddr[2];
1306  hcibuf[6] = disc_bdaddr[3];
1307  hcibuf[7] = disc_bdaddr[4];
1308  hcibuf[8] = disc_bdaddr[5];
1309  if(pairWithWii) {
1310  hcibuf[9] = 6; // Pin length is the length of the Bluetooth address
1311  if(pairWiiUsingSync) {
1312 #ifdef DEBUG_USB_HOST
1313  Notify(PSTR("\r\nPairing with Wii controller via SYNC"), 0x80);
1314 #endif
1315  for(uint8_t i = 0; i < 6; i++)
1316  hcibuf[10 + i] = my_bdaddr[i]; // The pin is the Bluetooth dongles Bluetooth address backwards
1317  } else {
1318  for(uint8_t i = 0; i < 6; i++)
1319  hcibuf[10 + i] = disc_bdaddr[i]; // The pin is the Wiimote's Bluetooth address backwards
1320  }
1321  for(uint8_t i = 16; i < 26; i++)
1322  hcibuf[i] = 0x00; // The rest should be 0
1323  } else {
1324  hcibuf[9] = strlen(btdPin); // Length of pin
1325  uint8_t i;
1326  for(i = 0; i < strlen(btdPin); i++) // The maximum size of the pin is 16
1327  hcibuf[i + 10] = btdPin[i];
1328  for(; i < 16; i++)
1329  hcibuf[i + 10] = 0x00; // The rest should be 0
1330  }
1331 
1332  HCI_Command(hcibuf, 26);
1333 }
1334 
1336  hcibuf[0] = 0x0E; // HCI OCF = 0E
1337  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1338  hcibuf[2] = 0x06; // parameter length 6
1339  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1340  hcibuf[4] = disc_bdaddr[1];
1341  hcibuf[5] = disc_bdaddr[2];
1342  hcibuf[6] = disc_bdaddr[3];
1343  hcibuf[7] = disc_bdaddr[4];
1344  hcibuf[8] = disc_bdaddr[5];
1345 
1346  HCI_Command(hcibuf, 9);
1347 }
1348 
1350  hcibuf[0] = 0x0C; // HCI OCF = 0C
1351  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1352  hcibuf[2] = 0x06; // parameter length 6
1353  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1354  hcibuf[4] = disc_bdaddr[1];
1355  hcibuf[5] = disc_bdaddr[2];
1356  hcibuf[6] = disc_bdaddr[3];
1357  hcibuf[7] = disc_bdaddr[4];
1358  hcibuf[8] = disc_bdaddr[5];
1359 
1360  HCI_Command(hcibuf, 9);
1361 }
1362 
1364  hcibuf[0] = 0x2B; // HCI OCF = 2B
1365  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1366  hcibuf[2] = 0x09;
1367  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1368  hcibuf[4] = disc_bdaddr[1];
1369  hcibuf[5] = disc_bdaddr[2];
1370  hcibuf[6] = disc_bdaddr[3];
1371  hcibuf[7] = disc_bdaddr[4];
1372  hcibuf[8] = disc_bdaddr[5];
1373  hcibuf[9] = 0x03; // NoInputNoOutput
1374  hcibuf[10] = 0x00; // OOB authentication data not present
1375  hcibuf[11] = 0x00; // MITM Protection Not Required – No Bonding. Numeric comparison with automatic accept allowed
1376 
1377  HCI_Command(hcibuf, 12);
1378 }
1379 
1381  hcibuf[0] = 0x2C; // HCI OCF = 2C
1382  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1383  hcibuf[2] = 0x06; // parameter length 6
1384  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
1385  hcibuf[4] = disc_bdaddr[1];
1386  hcibuf[5] = disc_bdaddr[2];
1387  hcibuf[6] = disc_bdaddr[3];
1388  hcibuf[7] = disc_bdaddr[4];
1389  hcibuf[8] = disc_bdaddr[5];
1390 
1391  HCI_Command(hcibuf, 9);
1392 }
1393 
1395  hcibuf[0] = 0x11; // HCI OCF = 11
1396  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1397  hcibuf[2] = 0x02; // parameter length = 2
1398  hcibuf[3] = (uint8_t)(hci_handle & 0xFF); //connection handle - low byte
1399  hcibuf[4] = (uint8_t)((hci_handle >> 8) & 0x0F); //connection handle - high byte
1400 
1401  HCI_Command(hcibuf, 5);
1402 }
1403 
1404 void BTD::hci_disconnect(uint16_t handle) { // This is called by the different services
1406  hcibuf[0] = 0x06; // HCI OCF = 6
1407  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
1408  hcibuf[2] = 0x03; // parameter length = 3
1409  hcibuf[3] = (uint8_t)(handle & 0xFF); //connection handle - low byte
1410  hcibuf[4] = (uint8_t)((handle >> 8) & 0x0F); //connection handle - high byte
1411  hcibuf[5] = 0x13; // reason
1412 
1413  HCI_Command(hcibuf, 6);
1414 }
1415 
1416 void BTD::hci_write_class_of_device() { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html
1417  hcibuf[0] = 0x24; // HCI OCF = 24
1418  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
1419  hcibuf[2] = 0x03; // parameter length = 3
1420  hcibuf[3] = 0x04; // Robot
1421  hcibuf[4] = 0x08; // Toy
1422  hcibuf[5] = 0x00;
1423 
1424  HCI_Command(hcibuf, 6);
1425 }
1426 /*******************************************************************
1427  * *
1428  * HCI ACL Data Packet *
1429  * *
1430  * buf[0] buf[1] buf[2] buf[3]
1431  * 0 4 8 11 12 16 24 31 MSB
1432  * .-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1433  * | HCI Handle |PB |BC | Data Total Length | HCI ACL Data Packet
1434  * .-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1435  *
1436  * buf[4] buf[5] buf[6] buf[7]
1437  * 0 8 16 31 MSB
1438  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1439  * | Length | Channel ID | Basic L2CAP header
1440  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1441  *
1442  * buf[8] buf[9] buf[10] buf[11]
1443  * 0 8 16 31 MSB
1444  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
1445  * | Code | Identifier | Length | Control frame (C-frame)
1446  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-. (signaling packet format)
1447  */
1448 /************************************************************/
1449 /* L2CAP Commands */
1450 
1451 /************************************************************/
1452 void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh) {
1453  uint8_t buf[8 + nbytes];
1454  buf[0] = (uint8_t)(handle & 0xff); // HCI handle with PB,BC flag
1455  buf[1] = (uint8_t)(((handle >> 8) & 0x0f) | 0x20);
1456  buf[2] = (uint8_t)((4 + nbytes) & 0xff); // HCI ACL total data length
1457  buf[3] = (uint8_t)((4 + nbytes) >> 8);
1458  buf[4] = (uint8_t)(nbytes & 0xff); // L2CAP header: Length
1459  buf[5] = (uint8_t)(nbytes >> 8);
1460  buf[6] = channelLow;
1461  buf[7] = channelHigh;
1462 
1463  for(uint16_t i = 0; i < nbytes; i++) // L2CAP C-frame
1464  buf[8 + i] = data[i];
1465 
1466  uint8_t rcode = pUsb->outTransfer(bAddress, epInfo[ BTD_DATAOUT_PIPE ].epAddr, (8 + nbytes), buf);
1467  if(rcode) {
1468  delay(100); // This small delay prevents it from overflowing if it fails
1469 #ifdef DEBUG_USB_HOST
1470  Notify(PSTR("\r\nError sending L2CAP message: 0x"), 0x80);
1471  D_PrintHex<uint8_t > (rcode, 0x80);
1472  Notify(PSTR(" - Channel ID: "), 0x80);
1473  D_PrintHex<uint8_t > (channelHigh, 0x80);
1474  Notify(PSTR(" "), 0x80);
1475  D_PrintHex<uint8_t > (channelLow, 0x80);
1476 #endif
1477  }
1478 }
1479 
1480 void BTD::l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm) {
1481  l2capoutbuf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code
1482  l2capoutbuf[1] = rxid; // Identifier
1483  l2capoutbuf[2] = 0x04; // Length
1484  l2capoutbuf[3] = 0x00;
1485  l2capoutbuf[4] = (uint8_t)(psm & 0xff); // PSM
1486  l2capoutbuf[5] = (uint8_t)(psm >> 8);
1487  l2capoutbuf[6] = scid[0]; // Source CID
1488  l2capoutbuf[7] = scid[1];
1489 
1490  L2CAP_Command(handle, l2capoutbuf, 8);
1491 }
1492 
1493 void BTD::l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result) {
1494  l2capoutbuf[0] = L2CAP_CMD_CONNECTION_RESPONSE; // Code
1495  l2capoutbuf[1] = rxid; // Identifier
1496  l2capoutbuf[2] = 0x08; // Length
1497  l2capoutbuf[3] = 0x00;
1498  l2capoutbuf[4] = dcid[0]; // Destination CID
1499  l2capoutbuf[5] = dcid[1];
1500  l2capoutbuf[6] = scid[0]; // Source CID
1501  l2capoutbuf[7] = scid[1];
1502  l2capoutbuf[8] = result; // Result: Pending or Success
1503  l2capoutbuf[9] = 0x00;
1504  l2capoutbuf[10] = 0x00; // No further information
1505  l2capoutbuf[11] = 0x00;
1506 
1507  L2CAP_Command(handle, l2capoutbuf, 12);
1508 }
1509 
1510 void BTD::l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid) {
1511  l2capoutbuf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code
1512  l2capoutbuf[1] = rxid; // Identifier
1513  l2capoutbuf[2] = 0x08; // Length
1514  l2capoutbuf[3] = 0x00;
1515  l2capoutbuf[4] = dcid[0]; // Destination CID
1516  l2capoutbuf[5] = dcid[1];
1517  l2capoutbuf[6] = 0x00; // Flags
1518  l2capoutbuf[7] = 0x00;
1519  l2capoutbuf[8] = 0x01; // Config Opt: type = MTU (Maximum Transmission Unit) - Hint
1520  l2capoutbuf[9] = 0x02; // Config Opt: length
1521  l2capoutbuf[10] = 0xFF; // MTU
1522  l2capoutbuf[11] = 0xFF;
1523 
1524  L2CAP_Command(handle, l2capoutbuf, 12);
1525 }
1526 
1527 void BTD::l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid) {
1528  l2capoutbuf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code
1529  l2capoutbuf[1] = rxid; // Identifier
1530  l2capoutbuf[2] = 0x0A; // Length
1531  l2capoutbuf[3] = 0x00;
1532  l2capoutbuf[4] = scid[0]; // Source CID
1533  l2capoutbuf[5] = scid[1];
1534  l2capoutbuf[6] = 0x00; // Flag
1535  l2capoutbuf[7] = 0x00;
1536  l2capoutbuf[8] = 0x00; // Result
1537  l2capoutbuf[9] = 0x00;
1538  l2capoutbuf[10] = 0x01; // Config
1539  l2capoutbuf[11] = 0x02;
1540  l2capoutbuf[12] = 0xA0;
1541  l2capoutbuf[13] = 0x02;
1542 
1543  L2CAP_Command(handle, l2capoutbuf, 14);
1544 }
1545 
1546 void BTD::l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
1547  l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_REQUEST; // Code
1548  l2capoutbuf[1] = rxid; // Identifier
1549  l2capoutbuf[2] = 0x04; // Length
1550  l2capoutbuf[3] = 0x00;
1551  l2capoutbuf[4] = dcid[0];
1552  l2capoutbuf[5] = dcid[1];
1553  l2capoutbuf[6] = scid[0];
1554  l2capoutbuf[7] = scid[1];
1555 
1556  L2CAP_Command(handle, l2capoutbuf, 8);
1557 }
1558 
1559 void BTD::l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
1560  l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_RESPONSE; // Code
1561  l2capoutbuf[1] = rxid; // Identifier
1562  l2capoutbuf[2] = 0x04; // Length
1563  l2capoutbuf[3] = 0x00;
1564  l2capoutbuf[4] = dcid[0];
1565  l2capoutbuf[5] = dcid[1];
1566  l2capoutbuf[6] = scid[0];
1567  l2capoutbuf[7] = scid[1];
1568 
1569  L2CAP_Command(handle, l2capoutbuf, 8);
1570 }
1571 
1572 void BTD::l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh) {
1573  l2capoutbuf[0] = L2CAP_CMD_INFORMATION_RESPONSE; // Code
1574  l2capoutbuf[1] = rxid; // Identifier
1575  l2capoutbuf[2] = 0x08; // Length
1576  l2capoutbuf[3] = 0x00;
1577  l2capoutbuf[4] = infoTypeLow;
1578  l2capoutbuf[5] = infoTypeHigh;
1579  l2capoutbuf[6] = 0x00; // Result = success
1580  l2capoutbuf[7] = 0x00; // Result = success
1581  l2capoutbuf[8] = 0x00;
1582  l2capoutbuf[9] = 0x00;
1583  l2capoutbuf[10] = 0x00;
1584  l2capoutbuf[11] = 0x00;
1585 
1586  L2CAP_Command(handle, l2capoutbuf, 12);
1587 }
1588 
1589 /* PS3 Commands - only set Bluetooth address is implemented in this library */
1590 void BTD::setBdaddr(uint8_t* bdaddr) {
1591  /* Set the internal Bluetooth address */
1592  uint8_t buf[8];
1593  buf[0] = 0x01;
1594  buf[1] = 0x00;
1595 
1596  for(uint8_t i = 0; i < 6; i++)
1597  buf[i + 2] = bdaddr[5 - i]; // Copy into buffer, has to be written reversed, so it is MSB first
1598 
1599  // 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
1600  pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
1601 }
1602 
1603 void BTD::setMoveBdaddr(uint8_t* bdaddr) {
1604  /* Set the internal Bluetooth address */
1605  uint8_t buf[11];
1606  buf[0] = 0x05;
1607  buf[7] = 0x10;
1608  buf[8] = 0x01;
1609  buf[9] = 0x02;
1610  buf[10] = 0x12;
1611 
1612  for(uint8_t i = 0; i < 6; i++)
1613  buf[i + 1] = bdaddr[i];
1614 
1615  // 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
1616  pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);
1617 }
#define EV_COMMAND_STATUS
Definition: BTD.h:96
#define HCI_FLAG_DISCONNECT_COMPLETE
Definition: BTD.h:69
#define EV_NUM_COMPLETE_PKT
Definition: BTD.h:98
#define HCI_INQUIRY_STATE
Definition: BTD.h:51
#define PS3_PID
Definition: BTD.h:26
#define HCI_FLAG_INCOMING_REQUEST
Definition: BTD.h:71
#define EV_EXTENDED_INQUIRY_RESULT
Definition: BTD.h:107
#define HCI_FLAG_CMD_COMPLETE
Definition: BTD.h:67
#define HCI_CONNECT_DEVICE_STATE
Definition: BTD.h:52
#define EV_QOS_SETUP_COMPLETE
Definition: BTD.h:94
#define EV_DISCONNECT_COMPLETE
Definition: BTD.h:88
#define HCI_FLAG_LOCAL_EXTENDED_FEATURES
Definition: BTD.h:76
#define HCI_DONE_STATE
Definition: BTD.h:60
#define EV_REMOTE_NAME_COMPLETE
Definition: BTD.h:90
#define EV_MAX_SLOTS_CHANGE
Definition: BTD.h:103
#define HCI_CHECK_DEVICE_SERVICE
Definition: BTD.h:49
#define EV_IO_CAPABILITY_RESPONSE
Definition: BTD.h:109
#define HCI_DISCONNECT_STATE
Definition: BTD.h:61
#define BULK_MAXPKTSIZE
Definition: BTD.h:37
#define EV_INQUIRY_RESULT
Definition: BTD.h:85
#define L2CAP_CMD_DISCONNECT_REQUEST
Definition: BTD.h:181
#define EV_CHANGE_CONNECTION_LINK
Definition: BTD.h:92
#define HCI_FLAG_REMOTE_NAME_COMPLETE
Definition: BTD.h:70
#define HCI_FLAG_CONNECT_COMPLETE
Definition: BTD.h:68
#define L2CAP_CMD_CONFIG_REQUEST
Definition: BTD.h:179
#define HCI_WRITE_NAME_STATE
Definition: BTD.h:48
#define HCI_REMOTE_NAME_STATE
Definition: BTD.h:57
#define L2CAP_CMD_DISCONNECT_RESPONSE
Definition: BTD.h:182
#define EV_INQUIRY_COMPLETE
Definition: BTD.h:84
#define HCI_LOCAL_EXTENDED_FEATURES_STATE
Definition: BTD.h:62
#define PS3NAVIGATION_PID
Definition: BTD.h:27
#define BELKIN_F8T065BF_VID
Definition: BTD.h:33
#define hci_check_flag(flag)
Definition: BTD.h:79
#define HCI_FLAG_DEVICE_FOUND
Definition: BTD.h:74
#define PS3_VID
Definition: BTD.h:25
#define EV_AUTHENTICATION_COMPLETE
Definition: BTD.h:89
#define IOGEAR_GBU521_VID
Definition: BTD.h:31
#define EV_DATA_BUFFER_OVERFLOW
Definition: BTD.h:102
#define hci_clear_flag(flag)
Definition: BTD.h:81
#define L2CAP_CMD_CONFIG_RESPONSE
Definition: BTD.h:180
#define EV_COMMAND_COMPLETE
Definition: BTD.h:95
#define EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE
Definition: BTD.h:93
#define EV_IO_CAPABILITY_REQUEST
Definition: BTD.h:108
#define HCI_BDADDR_STATE
Definition: BTD.h:46
#define HCI_CONNECT_IN_STATE
Definition: BTD.h:56
#define EV_LINK_KEY_NOTIFICATION
Definition: BTD.h:101
#define EV_LINK_KEY_REQUEST
Definition: BTD.h:100
#define HCI_LOCAL_VERSION_STATE
Definition: BTD.h:47
#define EV_PIN_CODE_REQUEST
Definition: BTD.h:99
#define EV_ENCRYPTION_CHANGE
Definition: BTD.h:91
#define HCI_CLASS_STATE
Definition: BTD.h:45
#define HCI_FLAG_READ_BDADDR
Definition: BTD.h:72
#define HCI_FLAG_READ_VERSION
Definition: BTD.h:73
#define HCI_SET_EVENT_MASK_STATE
Definition: BTD.h:64
#define HCI_CONNECTED_DEVICE_STATE
Definition: BTD.h:53
#define HCI_INIT_STATE
Definition: BTD.h:43
#define L2CAP_CMD_CONNECTION_REQUEST
Definition: BTD.h:177
#define EV_ROLE_CHANGED
Definition: BTD.h:97
#define HCI_WRITE_SIMPLE_PAIRING_STATE
Definition: BTD.h:63
#define HCI_SCANNING_STATE
Definition: BTD.h:55
#define EV_LOOPBACK_COMMAND
Definition: BTD.h:104
#define BELKIN_F8T065BF_PID
Definition: BTD.h:34
#define HCI_FLAG_CONNECT_EVENT
Definition: BTD.h:75
#define EV_CONNECT_COMPLETE
Definition: BTD.h:86
#define bmREQ_HCI_OUT
Definition: BTD.h:40
#define EV_PAGE_SCAN_REP_MODE
Definition: BTD.h:105
#define hci_set_flag(flag)
Definition: BTD.h:80
#define EV_SIMPLE_PAIRING_COMPLETE
Definition: BTD.h:111
#define HCI_CONNECTED_STATE
Definition: BTD.h:58
#define EV_USER_CONFIRMATION_REQUEST
Definition: BTD.h:110
#define IOGEAR_GBU521_PID
Definition: BTD.h:32
#define BTD_MAX_ENDPOINTS
Definition: BTD.h:211
#define L2CAP_CMD_CONNECTION_RESPONSE
Definition: BTD.h:178
#define L2CAP_CMD_INFORMATION_RESPONSE
Definition: BTD.h:184
#define HCI_RESET_STATE
Definition: BTD.h:44
#define EV_INCOMING_CONNECT
Definition: BTD.h:87
#define PS3MOVE_PID
Definition: BTD.h:28
#define BTD_NUM_SERVICES
Definition: BTD.h:212
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition: UsbCore.h:95
#define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
Definition: UsbCore.h:100
#define USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET
Definition: UsbCore.h:103
#define USB_ERROR_FailGetDevDescr
Definition: UsbCore.h:104
#define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
Definition: UsbCore.h:92
#define USB_ERROR_EPINFO_IS_NULL
Definition: UsbCore.h:98
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition: UsbCore.h:97
#define USB_NAK_MAX_POWER
Definition: address.h:34
#define USB_NAK_NOWAIT
Definition: address.h:36
virtual void FreeAddress(uint8_t addr)=0
virtual uint8_t AllocAddress(uint8_t parent, bool is_hub=false, uint8_t port=0)=0
virtual UsbDevice * GetUsbDevicePtr(uint8_t addr)=0
void hci_user_confirmation_request_reply()
Definition: BTD.cpp:1380
const char * btdName
Definition: BTD.h:478
bool pairWithWii
Definition: BTD.h:507
static const uint8_t BTD_DATAOUT_PIPE
Definition: BTD.h:559
bool l2capConnectionClaimed
Definition: BTD.h:471
uint8_t bConfNum
Definition: BTD.h:546
void hci_disconnect(uint16_t handle)
Definition: BTD.cpp:1404
void hci_accept_connection()
Definition: BTD.cpp:1177
uint8_t my_bdaddr[6]
Definition: BTD.h:483
const char * btdPin
Definition: BTD.h:480
void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep)
Definition: BTD.cpp:330
static const uint8_t BTD_EVENT_PIPE
Definition: BTD.h:555
void hci_read_local_extended_features(uint8_t page_number)
Definition: BTD.cpp:1167
void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid)
Definition: BTD.cpp:1559
bool pairWithHIDDevice
Definition: BTD.h:524
void hci_io_capability_request_reply()
Definition: BTD.cpp:1363
bool motionPlusInside
Definition: BTD.h:509
void hci_write_simple_pairing_mode(bool enable)
Definition: BTD.cpp:1242
void hci_inquiry_cancel()
Definition: BTD.cpp:1265
static const uint8_t BTD_DATAIN_PIPE
Definition: BTD.h:557
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: BTD.cpp:136
static const uint8_t BTD_CONTROL_PIPE
Definition: BTD.h:553
USB * pUsb
Definition: BTD.h:539
bool incomingWii
Definition: BTD.h:505
uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed)
Definition: BTD.cpp:50
uint32_t qNextPollTime
Definition: BTD.h:550
void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t *scid, uint16_t psm)
Definition: BTD.cpp:1480
uint8_t bAddress
Definition: BTD.h:541
BTD(USB *p)
Definition: BTD.cpp:27
void disconnect()
Definition: BTD.cpp:397
void hci_inquiry()
Definition: BTD.cpp:1251
bool incomingHIDDevice
Definition: BTD.h:522
void hci_pin_code_request_reply()
Definition: BTD.cpp:1299
void hci_pin_code_negative_request_reply()
Definition: BTD.cpp:1335
uint8_t Poll()
Definition: BTD.cpp:385
void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t *scid)
Definition: BTD.cpp:1527
void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid, uint8_t result)
Definition: BTD.cpp:1493
void hci_write_scan_enable()
Definition: BTD.cpp:1127
void hci_remote_name()
Definition: BTD.cpp:1193
uint8_t bNumEP
Definition: BTD.h:548
EpInfo epInfo[BTD_MAX_ENDPOINTS]
Definition: BTD.h:543
void l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh)
Definition: BTD.cpp:1572
bool waitingForConnection
Definition: BTD.h:469
bool useSimplePairing
Definition: BTD.h:532
bool wiiUProController
Definition: BTD.h:511
void hci_write_class_of_device()
Definition: BTD.cpp:1416
bool rfcommConnectionClaimed
Definition: BTD.h:475
char remote_name[30]
Definition: BTD.h:489
uint16_t hci_handle
Definition: BTD.h:485
void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR *ep_ptr)
Definition: BTD.cpp:360
uint8_t Release()
Definition: BTD.cpp:379
void L2CAP_Command(uint16_t handle, uint8_t *data, uint8_t nbytes, uint8_t channelLow=0x01, uint8_t channelHigh=0x00)
Definition: BTD.cpp:1452
uint8_t disc_bdaddr[6]
Definition: BTD.h:487
void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t *dcid)
Definition: BTD.cpp:1510
void hci_write_local_name(const char *name)
Definition: BTD.cpp:1212
void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid)
Definition: BTD.cpp:1546
bool connectToWii
Definition: BTD.h:501
void hci_read_bdaddr()
Definition: BTD.cpp:1149
void hci_set_event_mask()
Definition: BTD.cpp:1224
uint8_t hci_version
Definition: BTD.h:495
void hci_authentication_request()
Definition: BTD.cpp:1394
void hci_read_local_version_information()
Definition: BTD.cpp:1158
void hci_reset()
Definition: BTD.cpp:1118
void hci_connect()
Definition: BTD.cpp:1273
void HCI_Command(uint8_t *data, uint16_t nbytes)
Definition: BTD.cpp:1113
bool connectToHIDDevice
Definition: BTD.h:518
void hci_link_key_request_negative_reply()
Definition: BTD.cpp:1349
bool sdpConnectionClaimed
Definition: BTD.h:473
void hci_write_scan_disable()
Definition: BTD.cpp:1140
virtual void Run()=0
virtual void disconnect()=0
virtual void ACLData(uint8_t *ACLData)=0
virtual void Reset()=0
Definition: UsbCore.h:212
uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr)
defined(USB_METHODS_INLINE)
Definition: Usb.cpp:801
uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value)
Definition: Usb.cpp:845
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition: Usb.cpp:836
uint8_t RegisterDeviceClass(USBDeviceConfig *pdev)
Definition: UsbCore.h:232
uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr)
Definition: Usb.cpp:806
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
AddressPool & GetAddressPool()
Definition: UsbCore.h:228
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition: Usb.cpp:64
uint8_t inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t *data, uint8_t bInterval=0)
Definition: Usb.cpp:209
uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data)
Definition: Usb.cpp:303
@ L
#define hrJERR
Definition: max3421e.h:227
#define hrNAK
Definition: max3421e.h:218
#define NotifyFailSetConfDescr(...)
Definition: message.h:60
#define NotifyFailUnknownDevice(...)
Definition: message.h:61
#define NotifyFail(...)
Definition: message.h:62
#define Notify(...)
Definition: message.h:51
#define NotifyFailGetConfDescr(...)
Definition: message.h:59
#define NotifyFailSetDevTblEntry(...)
Definition: message.h:58
#define NotifyStr(...)
Definition: message.h:52
#define Notifyc(...)
Definition: message.h:53
#define NotifyFailGetDevDescr(...)
Definition: message.h:57
Definition: address.h:39
uint8_t bmNakPower
Definition: address.h:49
uint8_t bmRcvToggle
Definition: address.h:48
uint8_t epAddr
Definition: address.h:40
uint8_t maxPktSize
Definition: address.h:41
uint8_t bmSndToggle
Definition: address.h:47
uint8_t bMaxPacketSize0
Definition: usb_ch9.h:112
uint8_t bNumConfigurations
Definition: usb_ch9.h:119
uint16_t idProduct
Definition: usb_ch9.h:114
uint8_t bDeviceClass
Definition: usb_ch9.h:109
uint8_t bEndpointAddress
Definition: usb_ch9.h:151
uint16_t wMaxPacketSize
Definition: usb_ch9.h:153
EpInfo * epinfo
Definition: address.h:83
bool lowspeed
Definition: address.h:86
#define USB_TRANSFER_TYPE_BULK
Definition: usb_ch9.h:92
#define bmUSB_TRANSFER_TYPE
Definition: usb_ch9.h:94
#define USB_TRANSFER_TYPE_INTERRUPT
Definition: usb_ch9.h:93
#define bmREQ_HID_OUT
Definition: usbhid.h:63
#define HID_REQUEST_SET_REPORT
Definition: usbhid.h:72
#define PSTR(str)