30 connectToHIDDevice(false),
31 pairWithHIDDevice(false),
32 useSimplePairing(false),
38 simple_pairing_supported(false),
52 uint8_t buf[constBufSize];
100 #ifdef DEBUG_USB_HOST
101 Notify(
PSTR(
"\r\nOut of address space"), 0x80);
118 #ifdef DEBUG_USB_HOST
119 Notify(
PSTR(
"\r\nPlease create a hub instance in your code: \"USBHub Hub1(&Usb);\""), 0x80);
127 #ifdef DEBUG_USB_HOST
136 uint8_t
BTD::Init(uint8_t parent __attribute__((unused)), uint8_t port __attribute__((unused)),
bool lowspeed) {
148 #ifdef DEBUG_USB_HOST
158 #ifdef DEBUG_USB_HOST
160 D_PrintHex<uint8_t > (rcode, 0x80);
167 D_PrintHex<uint8_t > (
bAddress, 0x80);
174 #ifdef DEBUG_USB_HOST
184 goto FailSetDevTblEntry;
190 goto FailSetConfDescr;
192 #ifdef DEBUG_USB_HOST
195 Notify(
PSTR(
"\r\nDualshock 3 Controller Connected"), 0x80);
197 Notify(
PSTR(
"\r\nNavigation Controller Connected"), 0x80);
199 Notify(
PSTR(
"\r\nMotion Controller Connected"), 0x80);
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);
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);
217 D_PrintHex<uint8_t > (
my_bdaddr[0], 0x80);
229 for(uint8_t i = 0; i < num_of_conf; i++) {
238 goto FailGetConfDescr;
244 goto FailUnknownDevice;
249 goto FailSetDevTblEntry;
254 goto FailSetConfDescr;
256 hci_num_reset_loops = 100;
262 #ifdef DEBUG_USB_HOST
263 Notify(
PSTR(
"\r\nBluetooth Dongle Initialized"), 0x80);
270 #ifdef DEBUG_USB_HOST
276 #ifdef DEBUG_USB_HOST
282 #ifdef DEBUG_USB_HOST
288 #ifdef DEBUG_USB_HOST
294 #ifdef DEBUG_USB_HOST
295 Notify(
PSTR(
"\r\nBTD Init Failed, error code: "), 0x80);
302 void BTD::Initialize() {
313 btService[i]->
Reset();
320 incomingPSController =
false;
326 simple_pairing_supported =
false;
355 if(pollInterval < pep->bInterval)
362 Notify(
PSTR(
"\r\nEndpoint descriptor:"), 0x80);
364 D_PrintHex<uint8_t > (ep_ptr->bLength, 0x80);
366 D_PrintHex<uint8_t > (ep_ptr->bDescriptorType, 0x80);
368 D_PrintHex<uint8_t > (ep_ptr->bEndpointAddress, 0x80);
370 D_PrintHex<uint8_t > (ep_ptr->bmAttributes, 0x80);
372 D_PrintHex<uint16_t > (ep_ptr->wMaxPacketSize, 0x80);
374 D_PrintHex<uint8_t > (ep_ptr->bInterval, 0x80);
403 void BTD::HCI_event_task() {
407 if(!rcode || rcode ==
hrNAK) {
412 if((hcibuf[3] == 0x01) && (hcibuf[4] == 0x10)) {
421 }
else if((hcibuf[3] == 0x04) && (hcibuf[4] == 0x10)) {
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++) {
431 D_PrintHex<uint8_t > (hcibuf[8 + i], 0x80);
435 #ifdef DEBUG_USB_HOST
438 if(hcibuf[8 + 6] & (1U << 3)) {
439 simple_pairing_supported =
true;
440 #ifdef DEBUG_USB_HOST
444 simple_pairing_supported =
false;
445 #ifdef DEBUG_USB_HOST
449 #ifdef DEBUG_USB_HOST
450 Notify(
PSTR(
" secure simple pairing (controller support)"), 0x80);
452 }
else if(hcibuf[6] == 1) {
453 #ifdef DEBUG_USB_HOST
455 if(hcibuf[8 + 0] & (1U << 0))
459 Notify(
PSTR(
" secure simple pairing (host support)"), 0x80);
465 }
else if((hcibuf[3] == 0x09) && (hcibuf[4] == 0x10)) {
466 for(uint8_t i = 0; i < 6; i++)
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);
481 D_PrintHex<uint8_t > (hcibuf[4], 0x80);
483 D_PrintHex<uint8_t > (hcibuf[5], 0x80);
491 #ifdef DEBUG_USB_HOST
493 Notify(
PSTR(
"\r\nCouldn't find Wiimote"), 0x80);
495 Notify(
PSTR(
"\r\nCouldn't find HID device"), 0x80);
509 Notify(
PSTR(
"\r\nNumber of responses: "), 0x80);
512 for(uint8_t i = 0; i < hcibuf[2]; i++) {
513 uint8_t offset = 8 * hcibuf[2] + 3 * i;
515 for(uint8_t j = 0; j < 3; j++)
516 classOfDevice[j] = hcibuf[j + 4 + offset];
520 D_PrintHex<uint8_t > (classOfDevice[2], 0x80);
522 D_PrintHex<uint8_t > (classOfDevice[1], 0x80);
524 D_PrintHex<uint8_t > (classOfDevice[0], 0x80);
527 if(
pairWithWii && classOfDevice[2] == 0x00 && (classOfDevice[1] == 0x05) && (classOfDevice[0] & 0x0C)) {
528 checkRemoteName =
true;
530 for(uint8_t j = 0; j < 6; j++)
535 }
else if(
pairWithHIDDevice && (classOfDevice[1] & 0x0F) == 0x05 && (classOfDevice[0] & 0xC8)) {
536 #ifdef DEBUG_USB_HOST
537 checkRemoteName =
true;
539 if(classOfDevice[0] & 0x80)
541 if(classOfDevice[0] & 0x40)
543 if(classOfDevice[0] & 0x08)
546 for(uint8_t j = 0; j < 6; j++)
560 Notify(
PSTR(
"\r\nConnection established"), 0x80);
562 hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8);
566 #ifdef DEBUG_USB_HOST
567 Notify(
PSTR(
"\r\nConnection Failed: "), 0x80);
568 D_PrintHex<uint8_t > (hcibuf[2], 0x80);
582 for(uint8_t i = 0; i < min(
sizeof (
remote_name),
sizeof (hcibuf) - 9); i++) {
593 for(uint8_t i = 0; i < 6; i++)
596 for(uint8_t i = 0; i < 3; i++)
597 classOfDevice[i] = hcibuf[i + 8];
599 if((classOfDevice[1] & 0x0F) == 0x05 && (classOfDevice[0] & 0xC8)) {
600 #ifdef DEBUG_USB_HOST
601 if(classOfDevice[0] & 0x80)
602 Notify(
PSTR(
"\r\nMouse is connecting"), 0x80);
603 if(classOfDevice[0] & 0x40)
604 Notify(
PSTR(
"\r\nKeyboard is connecting"), 0x80);
605 if(classOfDevice[0] & 0x08)
606 Notify(
PSTR(
"\r\nGamepad is connecting"), 0x80);
613 D_PrintHex<uint8_t > (classOfDevice[2], 0x80);
615 D_PrintHex<uint8_t > (classOfDevice[1], 0x80);
617 D_PrintHex<uint8_t > (classOfDevice[0], 0x80);
624 #ifdef DEBUG_USB_HOST
625 Notify(
PSTR(
"\r\nPairing with Wiimote"), 0x80);
628 }
else if(
btdPin != NULL) {
629 #ifdef DEBUG_USB_HOST
630 Notify(
PSTR(
"\r\nBluetooth pin is set too: "), 0x80);
635 #ifdef DEBUG_USB_HOST
643 #ifdef DEBUG_USB_HOST
644 Notify(
PSTR(
"\r\nReceived Key Request"), 0x80);
652 #ifdef DEBUG_USB_HOST
653 Notify(
PSTR(
"\r\nPairing successful with Wiimote"), 0x80);
657 #ifdef DEBUG_USB_HOST
658 Notify(
PSTR(
"\r\nPairing successful with HID device"), 0x80);
663 Notify(
PSTR(
"\r\nPairing was successful"), 0x80);
667 #ifdef DEBUG_USB_HOST
669 D_PrintHex<uint8_t > (hcibuf[2], 0x80);
677 #ifdef DEBUG_USB_HOST
678 Notify(
PSTR(
"\r\nReceived IO Capability Request"), 0x80);
685 Notify(
PSTR(
"\r\nReceived IO Capability Response: "), 0x80);
687 D_PrintHex<uint8_t > (hcibuf[8], 0x80);
689 D_PrintHex<uint8_t > (hcibuf[9], 0x80);
690 Notify(
PSTR(
"\r\nAuthentication request: "), 0x80);
691 D_PrintHex<uint8_t > (hcibuf[10], 0x80);
696 #ifdef DEBUG_USB_HOST
697 Notify(
PSTR(
"\r\nUser confirmation Request"), 0x80);
700 for(uint8_t i = 0; i < 4; i++) {
702 D_PrintHex<uint8_t > (hcibuf[8 + i], 0x80);
713 Notify(
PSTR(
"\r\nSimple Pairing succeeded"), 0x80);
715 Notify(
PSTR(
"\r\nSimple Pairing failed: "), 0x80);
716 D_PrintHex<uint8_t > (hcibuf[2], 0x80);
735 if(hcibuf[0] != 0x00) {
737 D_PrintHex<uint8_t > (hcibuf[0], 0x80);
743 if(hcibuf[0] != 0x00) {
744 Notify(
PSTR(
"\r\nUnmanaged HCI Event: "), 0x80);
745 D_PrintHex<uint8_t > (hcibuf[0], 0x80);
747 for(uint16_t i = 0; i < hcibuf[1]; i++) {
748 D_PrintHex<uint8_t > (hcibuf[2 + i], 0x80);
759 D_PrintHex<uint8_t > (rcode, 0x80);
765 void BTD::HCI_task() {
769 if(hci_counter > hci_num_reset_loops) {
780 #ifdef DEBUG_USB_HOST
785 }
else if(hci_counter > hci_num_reset_loops) {
786 hci_num_reset_loops *= 10;
787 if(hci_num_reset_loops > 2000)
788 hci_num_reset_loops = 2000;
789 #ifdef DEBUG_USB_HOST
790 Notify(
PSTR(
"\r\nNo response to HCI Reset"), 0x80);
799 #ifdef DEBUG_USB_HOST
800 Notify(
PSTR(
"\r\nWrite class of device"), 0x80);
809 #ifdef DEBUG_USB_HOST
810 Notify(
PSTR(
"\r\nLocal Bluetooth Address: "), 0x80);
811 for(int8_t i = 5; i > 0; i--) {
812 D_PrintHex<uint8_t > (
my_bdaddr[i], 0x80);
815 D_PrintHex<uint8_t > (
my_bdaddr[0], 0x80);
838 #ifdef DEBUG_USB_HOST
839 Notify(
PSTR(
"\r\nThe name was set to: "), 0x80);
853 if(simple_pairing_supported) {
863 #ifdef DEBUG_USB_HOST
864 Notify(
PSTR(
"\r\nSimple pairing was enabled"), 0x80);
873 #ifdef DEBUG_USB_HOST
874 Notify(
PSTR(
"\r\nSet event mask completed"), 0x80);
882 #ifdef DEBUG_USB_HOST
884 Notify(
PSTR(
"\r\nStarting inquiry\r\nPress 1 & 2 on the Wiimote\r\nOr press the SYNC button if you are using a Wii U Pro Controller or a Wii Balance Board"), 0x80);
886 Notify(
PSTR(
"\r\nPlease enable discovery of your device"), 0x80);
897 #ifdef DEBUG_USB_HOST
903 Notify(
PSTR(
"\r\nNow just create the instance like so:"), 0x80);
909 Notify(
PSTR(
"\r\nAnd then press any button on the "), 0x80);
915 if(checkRemoteName) {
925 #ifdef DEBUG_USB_HOST
927 Notify(
PSTR(
"\r\nConnecting to Wiimote"), 0x80);
929 Notify(
PSTR(
"\r\nConnecting to HID device"), 0x80);
931 checkRemoteName =
false;
940 #ifdef DEBUG_USB_HOST
942 Notify(
PSTR(
"\r\nConnected to Wiimote"), 0x80);
944 Notify(
PSTR(
"\r\nConnected to HID device"), 0x80);
949 #ifdef DEBUG_USB_HOST
950 Notify(
PSTR(
"\r\nTrying to connect one more time..."), 0x80);
959 #ifdef DEBUG_USB_HOST
960 Notify(
PSTR(
"\r\nWait For Incoming Connection Request"), 0x80);
971 #ifdef DEBUG_USB_HOST
972 Notify(
PSTR(
"\r\nIncoming Connection Request"), 0x80);
982 #ifdef DEBUG_USB_HOST
987 if(strncmp((
const char*)
remote_name,
"Nintendo", 8) == 0) {
991 pairWiiUsingSync =
false;
992 #ifdef DEBUG_USB_HOST
993 Notify(
PSTR(
"\r\nWiimote is connecting"), 0x80);
995 if(strncmp((
const char*)
remote_name,
"Nintendo RVL-CNT-01-TR", 22) == 0) {
996 #ifdef DEBUG_USB_HOST
997 Notify(
PSTR(
" with Motion Plus Inside"), 0x80);
1000 }
else if(strncmp((
const char*)
remote_name,
"Nintendo RVL-CNT-01-UC", 22) == 0) {
1001 #ifdef DEBUG_USB_HOST
1002 Notify(
PSTR(
" - Wii U Pro Controller"), 0x80);
1005 }
else if(strncmp((
const char*)
remote_name,
"Nintendo RVL-WBC-01", 19) == 0) {
1006 #ifdef DEBUG_USB_HOST
1009 pairWiiUsingSync =
true;
1012 if(classOfDevice[2] == 0 && classOfDevice[1] == 0x25 && classOfDevice[0] == 0x08 && strncmp((
const char*)
remote_name,
"Wireless Controller", 19) == 0) {
1013 #ifdef DEBUG_USB_HOST
1014 Notify(
PSTR(
"\r\nPS4/PS5 controller is connecting"), 0x80);
1016 incomingPSController =
true;
1029 #ifdef DEBUG_USB_HOST
1030 Notify(
PSTR(
"\r\nConnected to Device: "), 0x80);
1031 for(int8_t i = 5; i > 0; i--) {
1037 if(incomingPSController)
1052 if(hci_counter > 1000) {
1060 #ifdef DEBUG_USB_HOST
1061 Notify(
PSTR(
"\r\nHCI Disconnected from Device"), 0x80);
1071 incomingPSController =
false;
1081 void BTD::ACL_event_task() {
1089 btService[i]->
ACLData(l2capinbuf);
1094 else if(rcode !=
hrNAK) {
1095 Notify(
PSTR(
"\r\nACL data in error: "), 0x80);
1096 D_PrintHex<uint8_t > (rcode, 0x80);
1101 btService[i]->
Run();
1110 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE ].epAddr,
bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL);
1116 hcibuf[1] = 0x03 << 2;
1125 hcibuf[1] = 0x03 << 2;
1137 hcibuf[1] = 0x03 << 2;
1147 hcibuf[1] = 0x04 << 2;
1156 hcibuf[1] = 0x04 << 2;
1165 hcibuf[1] = 0x04 << 2;
1167 hcibuf[3] = page_number;
1175 hcibuf[1] = 0x01 << 2;
1191 hcibuf[1] = 0x01 << 2;
1209 hcibuf[1] = 0x03 << 2;
1210 hcibuf[2] = strlen(name) + 1;
1212 for(i = 0; i < strlen(name); i++)
1213 hcibuf[i + 3] = name[i];
1214 hcibuf[i + 3] = 0x00;
1221 hcibuf[1] = 0x03 << 2;
1239 hcibuf[1] = 0x03 << 2;
1241 hcibuf[3] = enable ? 1 : 0;
1249 hcibuf[1] = 0x01 << 2;
1262 hcibuf[1] = 0x01 << 2;
1275 hcibuf[1] = 0x01 << 2;
1277 hcibuf[3] = bdaddr[0];
1278 hcibuf[4] = bdaddr[1];
1279 hcibuf[5] = bdaddr[2];
1280 hcibuf[6] = bdaddr[3];
1281 hcibuf[7] = bdaddr[4];
1282 hcibuf[8] = bdaddr[5];
1296 hcibuf[1] = 0x01 << 2;
1306 if(pairWiiUsingSync) {
1307 #ifdef DEBUG_USB_HOST
1308 Notify(
PSTR(
"\r\nPairing with Wii controller via SYNC"), 0x80);
1310 for(uint8_t i = 0; i < 6; i++)
1313 for(uint8_t i = 0; i < 6; i++)
1316 for(uint8_t i = 16; i < 26; i++)
1319 hcibuf[9] = strlen(
btdPin);
1321 for(i = 0; i < strlen(
btdPin); i++)
1322 hcibuf[i + 10] =
btdPin[i];
1324 hcibuf[i + 10] = 0x00;
1332 hcibuf[1] = 0x01 << 2;
1346 hcibuf[1] = 0x01 << 2;
1360 hcibuf[1] = 0x01 << 2;
1377 hcibuf[1] = 0x01 << 2;
1391 hcibuf[1] = 0x01 << 2;
1394 hcibuf[4] = (uint8_t)((
hci_handle >> 8) & 0x0F);
1402 hcibuf[1] = 0x01 << 2;
1404 hcibuf[3] = (uint8_t)(handle & 0xFF);
1405 hcibuf[4] = (uint8_t)((handle >> 8) & 0x0F);
1413 hcibuf[1] = 0x03 << 2;
1447 void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh) {
1448 uint8_t buf[8 + nbytes];
1449 buf[0] = (uint8_t)(handle & 0xff);
1450 buf[1] = (uint8_t)(((handle >> 8) & 0x0f) | 0x20);
1451 buf[2] = (uint8_t)((4 + nbytes) & 0xff);
1452 buf[3] = (uint8_t)((4 + nbytes) >> 8);
1453 buf[4] = (uint8_t)(nbytes & 0xff);
1454 buf[5] = (uint8_t)(nbytes >> 8);
1455 buf[6] = channelLow;
1456 buf[7] = channelHigh;
1458 for(uint16_t i = 0; i < nbytes; i++)
1459 buf[8 + i] = data[i];
1464 #ifdef DEBUG_USB_HOST
1465 Notify(
PSTR(
"\r\nError sending L2CAP message: 0x"), 0x80);
1466 D_PrintHex<uint8_t > (rcode, 0x80);
1468 D_PrintHex<uint8_t > (channelHigh, 0x80);
1470 D_PrintHex<uint8_t > (channelLow, 0x80);
1477 l2capoutbuf[1] = rxid;
1478 l2capoutbuf[2] = 0x04;
1479 l2capoutbuf[3] = 0x00;
1480 l2capoutbuf[4] = (uint8_t)(psm & 0xff);
1481 l2capoutbuf[5] = (uint8_t)(psm >> 8);
1482 l2capoutbuf[6] = scid[0];
1483 l2capoutbuf[7] = scid[1];
1490 l2capoutbuf[1] = rxid;
1491 l2capoutbuf[2] = 0x08;
1492 l2capoutbuf[3] = 0x00;
1493 l2capoutbuf[4] = dcid[0];
1494 l2capoutbuf[5] = dcid[1];
1495 l2capoutbuf[6] = scid[0];
1496 l2capoutbuf[7] = scid[1];
1497 l2capoutbuf[8] = result;
1498 l2capoutbuf[9] = 0x00;
1499 l2capoutbuf[10] = 0x00;
1500 l2capoutbuf[11] = 0x00;
1507 l2capoutbuf[1] = rxid;
1508 l2capoutbuf[2] = 0x08;
1509 l2capoutbuf[3] = 0x00;
1510 l2capoutbuf[4] = dcid[0];
1511 l2capoutbuf[5] = dcid[1];
1512 l2capoutbuf[6] = 0x00;
1513 l2capoutbuf[7] = 0x00;
1514 l2capoutbuf[8] = 0x01;
1515 l2capoutbuf[9] = 0x02;
1516 l2capoutbuf[10] = 0xFF;
1517 l2capoutbuf[11] = 0xFF;
1524 l2capoutbuf[1] = rxid;
1525 l2capoutbuf[2] = 0x0A;
1526 l2capoutbuf[3] = 0x00;
1527 l2capoutbuf[4] = scid[0];
1528 l2capoutbuf[5] = scid[1];
1529 l2capoutbuf[6] = 0x00;
1530 l2capoutbuf[7] = 0x00;
1531 l2capoutbuf[8] = 0x00;
1532 l2capoutbuf[9] = 0x00;
1533 l2capoutbuf[10] = 0x01;
1534 l2capoutbuf[11] = 0x02;
1535 l2capoutbuf[12] = 0xA0;
1536 l2capoutbuf[13] = 0x02;
1543 l2capoutbuf[1] = rxid;
1544 l2capoutbuf[2] = 0x04;
1545 l2capoutbuf[3] = 0x00;
1546 l2capoutbuf[4] = dcid[0];
1547 l2capoutbuf[5] = dcid[1];
1548 l2capoutbuf[6] = scid[0];
1549 l2capoutbuf[7] = scid[1];
1556 l2capoutbuf[1] = rxid;
1557 l2capoutbuf[2] = 0x04;
1558 l2capoutbuf[3] = 0x00;
1559 l2capoutbuf[4] = dcid[0];
1560 l2capoutbuf[5] = dcid[1];
1561 l2capoutbuf[6] = scid[0];
1562 l2capoutbuf[7] = scid[1];
1569 l2capoutbuf[1] = rxid;
1570 l2capoutbuf[2] = 0x08;
1571 l2capoutbuf[3] = 0x00;
1572 l2capoutbuf[4] = infoTypeLow;
1573 l2capoutbuf[5] = infoTypeHigh;
1574 l2capoutbuf[6] = 0x00;
1575 l2capoutbuf[7] = 0x00;
1576 l2capoutbuf[8] = 0x00;
1577 l2capoutbuf[9] = 0x00;
1578 l2capoutbuf[10] = 0x00;
1579 l2capoutbuf[11] = 0x00;
1585 void BTD::setBdaddr(uint8_t* bdaddr) {
1591 for(uint8_t i = 0; i < 6; i++)
1592 buf[i + 2] = bdaddr[5 - i];
1595 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
1598 void BTD::setMoveBdaddr(uint8_t* bdaddr) {
1607 for(uint8_t i = 0; i < 6; i++)
1608 buf[i + 1] = bdaddr[i];
1611 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);