45 uint8_t
BTD::Init(uint8_t parent, uint8_t port,
bool lowspeed) {
57 Notify(PSTR(
"\r\nBTD Init"), 0x80);
62 Notify(PSTR(
"\r\nAddress in use"), 0x80);
72 Notify(PSTR(
"\r\nAddress not found"), 0x80);
79 Notify(PSTR(
"\r\nepinfo is null"), 0x80);
116 #ifdef DEBUG_USB_HOST
117 Notify(PSTR(
"\r\nsetAddr: "), 0x80);
118 D_PrintHex<uint8_t > (rcode, 0x80);
123 Notify(PSTR(
"\r\nAddr: "), 0x80);
124 D_PrintHex<uint8_t > (
bAddress, 0x80);
140 goto FailSetDevTblEntry;
148 goto FailSetConfDescr;
150 #ifdef DEBUG_USB_HOST
153 Notify(PSTR(
"\r\nDualshock 3 Controller Connected"), 0x80);
155 Notify(PSTR(
"\r\nNavigation Controller Connected"), 0x80);
157 Notify(PSTR(
"\r\nMotion Controller Connected"), 0x80);
161 #ifdef DEBUG_USB_HOST
162 Notify(PSTR(
"\r\nPlease plug in the dongle before trying to pair with the PS3 Controller\n\ror set the Bluetooth address in the constructor of the PS3BT class"), 0x80);
169 #ifdef DEBUG_USB_HOST
170 Notify(PSTR(
"\r\nBluetooth Address was set to: "), 0x80);
171 for (int8_t i = 5; i > 0; i--) {
172 D_PrintHex<uint8_t > (
my_bdaddr[i], 0x80);
175 D_PrintHex<uint8_t > (
my_bdaddr[0], 0x80);
189 for (uint8_t i = 0; i < num_of_conf; i++) {
198 goto FailGetConfDescr;
204 goto FailUnknownDevice;
209 goto FailSetDevTblEntry;
216 goto FailSetConfDescr;
218 hci_num_reset_loops = 100;
224 #ifdef DEBUG_USB_HOST
225 Notify(PSTR(
"\r\nBluetooth Dongle Initialized"), 0x80);
232 #ifdef DEBUG_USB_HOST
238 #ifdef DEBUG_USB_HOST
244 #ifdef DEBUG_USB_HOST
250 #ifdef DEBUG_USB_HOST
256 #ifdef DEBUG_USB_HOST
262 #ifdef DEBUG_USB_HOST
263 Notify(PSTR(
"\r\nBTD Init Failed, error code: "), 0x80);
298 if (pollInterval < pep->bInterval)
305 Notify(PSTR(
"\r\nEndpoint descriptor:"), 0x80);
306 Notify(PSTR(
"\r\nLength:\t\t"), 0x80);
307 D_PrintHex<uint8_t > (ep_ptr->
bLength, 0x80);
308 Notify(PSTR(
"\r\nType:\t\t"), 0x80);
310 Notify(PSTR(
"\r\nAddress:\t"), 0x80);
312 Notify(PSTR(
"\r\nAttributes:\t"), 0x80);
314 Notify(PSTR(
"\r\nMaxPktSize:\t"), 0x80);
316 Notify(PSTR(
"\r\nPoll Intrv:\t"), 0x80);
317 D_PrintHex<uint8_t > (ep_ptr->
bInterval, 0x80);
325 btService[i]->
Reset();
346 void BTD::HCI_event_task() {
350 if (!rcode || rcode ==
hrNAK)
357 if ((hcibuf[3] == 0x01) && (hcibuf[4] == 0x10)) {
360 }
else if ((hcibuf[3] == 0x09) && (hcibuf[4] == 0x10)) {
361 for (uint8_t i = 0; i < 6; i++)
370 #ifdef DEBUG_USB_HOST
371 Notify(PSTR(
"\r\nHCI Command Failed: "), 0x80);
372 D_PrintHex<uint8_t > (hcibuf[2], 0x80);
374 D_PrintHex<uint8_t > (hcibuf[4], 0x80);
376 D_PrintHex<uint8_t > (hcibuf[5], 0x80);
382 if (inquiry_counter >= 5) {
384 #ifdef DEBUG_USB_HOST
385 Notify(PSTR(
"\r\nCouldn't find Wiimote"), 0x80);
397 Notify(PSTR(
"\r\nNumber of responses: "), 0x80);
400 for (uint8_t i = 0; i < hcibuf[2]; i++) {
401 if ((hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x04 && hcibuf[5 + 8 * hcibuf[2] + 3 * i] == 0x25 && hcibuf[6 + 8 * hcibuf[2] + 3 * i] == 0x00) || (hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x08 && hcibuf[5 + 8 * hcibuf[2] + 3 * i] == 0x05 && hcibuf[6 + 8 * hcibuf[2] + 3 * i] == 0x00)) {
402 if (hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x08)
417 Notify(PSTR(
"\r\nClass of device: "), 0x80);
418 D_PrintHex<uint8_t > (hcibuf[6 + 8 * hcibuf[2] + 3 * i], 0x80);
420 D_PrintHex<uint8_t > (hcibuf[5 + 8 * hcibuf[2] + 3 * i], 0x80);
422 D_PrintHex<uint8_t > (hcibuf[4 + 8 * hcibuf[2] + 3 * i], 0x80);
432 hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8);
437 Notify(PSTR(
"\r\nConnection Failed"), 0x80);
452 for (uint8_t i = 0; i < min(
sizeof (
remote_name),
sizeof (hcibuf) - 9); i++)
466 Notify(PSTR(
"\r\nClass of device: "), 0x80);
467 D_PrintHex<uint8_t > (hcibuf[10], 0x80);
469 D_PrintHex<uint8_t > (hcibuf[9], 0x80);
471 D_PrintHex<uint8_t > (hcibuf[8], 0x80);
478 #ifdef DEBUG_USB_HOST
479 Notify(PSTR(
"\r\nPairing with wiimote"), 0x80);
482 }
else if (
btdPin != NULL) {
483 #ifdef DEBUG_USB_HOST
484 Notify(PSTR(
"\r\nBluetooth pin is set too: "), 0x80);
489 #ifdef DEBUG_USB_HOST
490 Notify(PSTR(
"\r\nNo pin was set"), 0x80);
497 #ifdef DEBUG_USB_HOST
498 Notify(PSTR(
"\r\nReceived Key Request"), 0x80);
505 #ifdef DEBUG_USB_HOST
506 Notify(PSTR(
"\r\nPairing successful"), 0x80);
526 if (hcibuf[0] != 0x00) {
527 Notify(PSTR(
"\r\nUnmanaged HCI Event: "), 0x80);
528 D_PrintHex<uint8_t > (hcibuf[0], 0x80);
536 Notify(PSTR(
"\r\nHCI event error: "), 0x80);
537 D_PrintHex<uint8_t > (rcode, 0x80);
544 void BTD::HCI_task() {
548 if (hci_counter > hci_num_reset_loops) {
559 #ifdef DEBUG_USB_HOST
560 Notify(PSTR(
"\r\nHCI Reset complete"), 0x80);
564 }
else if (hci_counter > hci_num_reset_loops) {
565 hci_num_reset_loops *= 10;
566 if (hci_num_reset_loops > 2000)
567 hci_num_reset_loops = 2000;
568 #ifdef DEBUG_USB_HOST
569 Notify(PSTR(
"\r\nNo response to HCI Reset"), 0x80);
578 #ifdef DEBUG_USB_HOST
579 Notify(PSTR(
"\r\nWrite class of device"), 0x80);
588 #ifdef DEBUG_USB_HOST
589 Notify(PSTR(
"\r\nLocal Bluetooth Address: "), 0x80);
590 for (int8_t i = 5; i > 0; i--) {
591 D_PrintHex<uint8_t > (
my_bdaddr[i], 0x80);
594 D_PrintHex<uint8_t > (my_bdaddr[0], 0x80);
613 #ifdef DEBUG_USB_HOST
614 Notify(PSTR(
"\r\nThe name is set to: "), 0x80);
623 #ifdef DEBUG_USB_HOST
624 Notify(PSTR(
"\r\nStarting inquiry\r\nPress 1 & 2 on the Wiimote\r\nOr press sync if you are using a Wii U Pro Controller"), 0x80);
635 #ifdef DEBUG_USB_HOST
636 Notify(PSTR(
"\r\nWiimote found"), 0x80);
637 Notify(PSTR(
"\r\nNow just create the instance like so:"), 0x80);
638 Notify(PSTR(
"\r\nWII Wii(&Btd);"), 0x80);
639 Notify(PSTR(
"\r\nAnd then press any button on the Wiimote"), 0x80);
651 #ifdef DEBUG_USB_HOST
652 Notify(PSTR(
"\r\nConnecting to Wiimote"), 0x80);
662 #ifdef DEBUG_USB_HOST
663 Notify(PSTR(
"\r\nConnected to Wiimote"), 0x80);
668 #ifdef DEBUG_USB_HOST
669 Notify(PSTR(
"\r\nTrying to connect one more time..."), 0x80);
678 #ifdef DEBUG_USB_HOST
679 Notify(PSTR(
"\r\nWait For Incoming Connection Request"), 0x80);
690 #ifdef DEBUG_USB_HOST
691 Notify(PSTR(
"\r\nIncoming Connection Request"), 0x80);
701 #ifdef DEBUG_USB_HOST
702 Notify(PSTR(
"\r\nRemote Name: "), 0x80);
703 for (uint8_t i = 0; i < 30; i++) {
709 if (strncmp((
const char*)
remote_name,
"Nintendo", 8) == 0) {
711 #ifdef DEBUG_USB_HOST
712 Notify(PSTR(
"\r\nWiimote is connecting"), 0x80);
714 if (strncmp((
const char*)
remote_name,
"Nintendo RVL-CNT-01-TR", 22) == 0) {
715 #ifdef DEBUG_USB_HOST
716 Notify(PSTR(
" with Motion Plus Inside"), 0x80);
719 }
else if (strncmp((
const char*)
remote_name,
"Nintendo RVL-CNT-01-UC", 22) == 0) {
720 #ifdef DEBUG_USB_HOST
721 Notify(PSTR(
" - Wii U Pro Controller"), 0x80);
741 #ifdef DEBUG_USB_HOST
742 Notify(PSTR(
"\r\nConnected to Device: "), 0x80);
743 for (int8_t i = 5; i > 0; i--) {
747 D_PrintHex<uint8_t > (disc_bdaddr[0], 0x80);
761 if (hci_counter > 1000) {
769 #ifdef DEBUG_USB_HOST
770 Notify(PSTR(
"\r\nHCI Disconnected from Device"), 0x80);
788 void BTD::ACL_event_task() {
794 btService[i]->
ACLData(l2capinbuf);
797 else if (rcode !=
hrNAK) {
798 Notify(PSTR(
"\r\nACL data in error: "), 0x80);
799 D_PrintHex<uint8_t > (rcode, 0x80);
813 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE ].epAddr,
bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL);
819 hcibuf[1] = 0x03 << 2;
828 hcibuf[1] = 0x03 << 2;
840 hcibuf[1] = 0x03 << 2;
849 hcibuf[1] = 0x04 << 2;
857 hcibuf[1] = 0x04 << 2;
866 hcibuf[1] = 0x01 << 2;
882 hcibuf[1] = 0x01 << 2;
900 hcibuf[1] = 0x03 << 2;
901 hcibuf[2] = strlen(name) + 1;
903 for (i = 0; i < strlen(name); i++)
904 hcibuf[i + 3] = name[i];
905 hcibuf[i + 3] = 0x00;
913 hcibuf[1] = 0x01 << 2;
926 hcibuf[1] = 0x01 << 2;
935 hcibuf[1] = 0x01 << 2;
956 hcibuf[1] = 0x01 << 2;
967 #ifdef DEBUG_USB_HOST
968 Notify(PSTR(
"\r\nParing with Wii U Pro Controller"), 0x80);
970 for (uint8_t i = 0; i < 6; i++)
973 for (uint8_t i = 0; i < 6; i++)
976 for (uint8_t i = 16; i < 26; i++)
979 hcibuf[9] = strlen(
btdPin);
981 for (i = 0; i < strlen(
btdPin); i++)
982 hcibuf[i + 10] =
btdPin[i];
984 hcibuf[i + 10] = 0x00;
992 hcibuf[1] = 0x01 << 2;
1006 hcibuf[1] = 0x01 << 2;
1020 hcibuf[1] = 0x01 << 2;
1023 hcibuf[4] = (uint8_t)((
hci_handle >> 8) & 0x0F);
1031 hcibuf[1] = 0x01 << 2;
1033 hcibuf[3] = (uint8_t)(handle & 0xFF);
1034 hcibuf[4] = (uint8_t)((handle >> 8) & 0x0F);
1042 hcibuf[1] = 0x03 << 2;
1076 void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh) {
1077 uint8_t buf[8 + nbytes];
1078 buf[0] = (uint8_t)(handle & 0xff);
1079 buf[1] = (uint8_t)(((handle >> 8) & 0x0f) | 0x20);
1080 buf[2] = (uint8_t)((4 + nbytes) & 0xff);
1081 buf[3] = (uint8_t)((4 + nbytes) >> 8);
1082 buf[4] = (uint8_t)(nbytes & 0xff);
1083 buf[5] = (uint8_t)(nbytes >> 8);
1084 buf[6] = channelLow;
1085 buf[7] = channelHigh;
1087 for (uint16_t i = 0; i < nbytes; i++)
1088 buf[8 + i] = data[i];
1093 #ifdef DEBUG_USB_HOST
1094 Notify(PSTR(
"\r\nError sending L2CAP message: 0x"), 0x80);
1095 D_PrintHex<uint8_t > (rcode, 0x80);
1096 Notify(PSTR(
" - Channel ID: "), 0x80);
1097 D_PrintHex<uint8_t > (channelHigh, 0x80);
1099 D_PrintHex<uint8_t > (channelLow, 0x80);
1106 l2capoutbuf[1] = rxid;
1107 l2capoutbuf[2] = 0x04;
1108 l2capoutbuf[3] = 0x00;
1109 l2capoutbuf[4] = (uint8_t)(psm & 0xff);
1110 l2capoutbuf[5] = (uint8_t)(psm >> 8);
1111 l2capoutbuf[6] = scid[0];
1112 l2capoutbuf[7] = scid[1];
1119 l2capoutbuf[1] = rxid;
1120 l2capoutbuf[2] = 0x08;
1121 l2capoutbuf[3] = 0x00;
1122 l2capoutbuf[4] = dcid[0];
1123 l2capoutbuf[5] = dcid[1];
1124 l2capoutbuf[6] = scid[0];
1125 l2capoutbuf[7] = scid[1];
1126 l2capoutbuf[8] = result;
1127 l2capoutbuf[9] = 0x00;
1128 l2capoutbuf[10] = 0x00;
1129 l2capoutbuf[11] = 0x00;
1136 l2capoutbuf[1] = rxid;
1137 l2capoutbuf[2] = 0x08;
1138 l2capoutbuf[3] = 0x00;
1139 l2capoutbuf[4] = dcid[0];
1140 l2capoutbuf[5] = dcid[1];
1141 l2capoutbuf[6] = 0x00;
1142 l2capoutbuf[7] = 0x00;
1143 l2capoutbuf[8] = 0x01;
1144 l2capoutbuf[9] = 0x02;
1145 l2capoutbuf[10] = 0xFF;
1146 l2capoutbuf[11] = 0xFF;
1153 l2capoutbuf[1] = rxid;
1154 l2capoutbuf[2] = 0x0A;
1155 l2capoutbuf[3] = 0x00;
1156 l2capoutbuf[4] = scid[0];
1157 l2capoutbuf[5] = scid[1];
1158 l2capoutbuf[6] = 0x00;
1159 l2capoutbuf[7] = 0x00;
1160 l2capoutbuf[8] = 0x00;
1161 l2capoutbuf[9] = 0x00;
1162 l2capoutbuf[10] = 0x01;
1163 l2capoutbuf[11] = 0x02;
1164 l2capoutbuf[12] = 0xA0;
1165 l2capoutbuf[13] = 0x02;
1172 l2capoutbuf[1] = rxid;
1173 l2capoutbuf[2] = 0x04;
1174 l2capoutbuf[3] = 0x00;
1175 l2capoutbuf[4] = dcid[0];
1176 l2capoutbuf[5] = dcid[1];
1177 l2capoutbuf[6] = scid[0];
1178 l2capoutbuf[7] = scid[1];
1185 l2capoutbuf[1] = rxid;
1186 l2capoutbuf[2] = 0x04;
1187 l2capoutbuf[3] = 0x00;
1188 l2capoutbuf[4] = dcid[0];
1189 l2capoutbuf[5] = dcid[1];
1190 l2capoutbuf[6] = scid[0];
1191 l2capoutbuf[7] = scid[1];
1198 l2capoutbuf[1] = rxid;
1199 l2capoutbuf[2] = 0x08;
1200 l2capoutbuf[3] = 0x00;
1201 l2capoutbuf[4] = infoTypeLow;
1202 l2capoutbuf[5] = infoTypeHigh;
1203 l2capoutbuf[6] = 0x00;
1204 l2capoutbuf[7] = 0x00;
1205 l2capoutbuf[8] = 0x00;
1206 l2capoutbuf[9] = 0x00;
1207 l2capoutbuf[10] = 0x00;
1208 l2capoutbuf[11] = 0x00;
1214 void BTD::setBdaddr(uint8_t* bdaddr) {
1220 for (uint8_t i = 0; i < 6; i++)
1221 buf[i + 2] = bdaddr[5 - i];
1224 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
1227 void BTD::setMoveBdaddr(uint8_t* bdaddr) {
1236 for (uint8_t i = 0; i < 6; i++)
1237 buf[i + 1] = bdaddr[i];
1240 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);