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