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);
119 D_PrintHex<uint8_t > (rcode, 0x80);
123 Notify(PSTR(
"\r\nAddr: "), 0x80);
124 D_PrintHex<uint8_t > (
bAddress, 0x80);
138 goto FailSetDevTblEntry;
146 goto FailSetConfDescr;
148 #ifdef DEBUG_USB_HOST
151 Notify(PSTR(
"\r\nDualshock 3 Controller Connected"), 0x80);
153 Notify(PSTR(
"\r\nNavigation Controller Connected"), 0x80);
155 Notify(PSTR(
"\r\nMotion Controller Connected"), 0x80);
159 #ifdef DEBUG_USB_HOST
160 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);
167 #ifdef DEBUG_USB_HOST
168 Notify(PSTR(
"\r\nBluetooth Address was set to: "), 0x80);
169 for (int8_t i = 5; i > 0; i--) {
170 D_PrintHex<uint8_t > (
my_bdaddr[i], 0x80);
173 D_PrintHex<uint8_t > (
my_bdaddr[0], 0x80);
187 for (uint8_t i = 0; i < num_of_conf; i++) {
191 goto FailGetConfDescr;
197 goto FailUnknownDevice;
202 goto FailSetDevTblEntry;
209 goto FailSetConfDescr;
211 hci_num_reset_loops = 100;
217 #ifdef DEBUG_USB_HOST
218 Notify(PSTR(
"\r\nBluetooth Dongle Initialized"), 0x80);
225 #ifdef DEBUG_USB_HOST
231 #ifdef DEBUG_USB_HOST
237 #ifdef DEBUG_USB_HOST
243 #ifdef DEBUG_USB_HOST
249 #ifdef DEBUG_USB_HOST
255 #ifdef DEBUG_USB_HOST
256 Notify(PSTR(
"\r\nBTD Init Failed, error code: "), 0x80);
291 if (pollInterval < pep->bInterval)
298 Notify(PSTR(
"\r\nEndpoint descriptor:"), 0x80);
299 Notify(PSTR(
"\r\nLength:\t\t"), 0x80);
300 D_PrintHex<uint8_t > (ep_ptr->
bLength, 0x80);
301 Notify(PSTR(
"\r\nType:\t\t"), 0x80);
303 Notify(PSTR(
"\r\nAddress:\t"), 0x80);
305 Notify(PSTR(
"\r\nAttributes:\t"), 0x80);
307 Notify(PSTR(
"\r\nMaxPktSize:\t"), 0x80);
309 Notify(PSTR(
"\r\nPoll Intrv:\t"), 0x80);
310 D_PrintHex<uint8_t > (ep_ptr->
bInterval, 0x80);
318 btService[i]->
Reset();
339 void BTD::HCI_event_task() {
350 if ((hcibuf[3] == 0x01) && (hcibuf[4] == 0x10)) {
353 }
else if ((hcibuf[3] == 0x09) && (hcibuf[4] == 0x10)) {
354 for (uint8_t i = 0; i < 6; i++)
363 #ifdef DEBUG_USB_HOST
364 Notify(PSTR(
"\r\nHCI Command Failed: "), 0x80);
365 D_PrintHex<uint8_t > (hcibuf[2], 0x80);
367 D_PrintHex<uint8_t > (hcibuf[4], 0x80);
369 D_PrintHex<uint8_t > (hcibuf[5], 0x80);
375 if (inquiry_counter >= 5) {
377 #ifdef DEBUG_USB_HOST
378 Notify(PSTR(
"\r\nCouldn't find Wiimote"), 0x80);
390 Notify(PSTR(
"\r\nNumber of responses: "), 0x80);
393 for (uint8_t i = 0; i < hcibuf[2]; i++) {
394 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)) {
395 if (hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x08)
410 Notify(PSTR(
"\r\nClass of device: "), 0x80);
411 D_PrintHex<uint8_t > (hcibuf[6 + 8 * hcibuf[2] + 3 * i], 0x80);
413 D_PrintHex<uint8_t > (hcibuf[5 + 8 * hcibuf[2] + 3 * i], 0x80);
415 D_PrintHex<uint8_t > (hcibuf[4 + 8 * hcibuf[2] + 3 * i], 0x80);
425 hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8);
430 Notify(PSTR(
"\r\nConnection Failed"), 0x80);
445 for (uint8_t i = 0; i < min(
sizeof (
remote_name),
sizeof (hcibuf) - 9); i++)
459 Notify(PSTR(
"\r\nClass of device: "), 0x80);
460 D_PrintHex<uint8_t > (hcibuf[10], 0x80);
462 D_PrintHex<uint8_t > (hcibuf[9], 0x80);
464 D_PrintHex<uint8_t > (hcibuf[8], 0x80);
471 #ifdef DEBUG_USB_HOST
472 Notify(PSTR(
"\r\nPairing with wiimote"), 0x80);
475 }
else if (
btdPin != NULL) {
476 #ifdef DEBUG_USB_HOST
477 Notify(PSTR(
"\r\nBluetooth pin is set too: "), 0x80);
482 #ifdef DEBUG_USB_HOST
483 Notify(PSTR(
"\r\nNo pin was set"), 0x80);
490 #ifdef DEBUG_USB_HOST
491 Notify(PSTR(
"\r\nReceived Key Request"), 0x80);
498 #ifdef DEBUG_USB_HOST
499 Notify(PSTR(
"\r\nPairing successful"), 0x80);
519 if (hcibuf[0] != 0x00) {
520 Notify(PSTR(
"\r\nUnmanaged HCI Event: "), 0x80);
521 D_PrintHex<uint8_t > (hcibuf[0], 0x80);
528 else if (rcode !=
hrNAK) {
529 Notify(PSTR(
"\r\nHCI event error: "), 0x80);
530 D_PrintHex<uint8_t > (rcode, 0x80);
537 void BTD::HCI_task() {
541 if (hci_counter > hci_num_reset_loops) {
552 #ifdef DEBUG_USB_HOST
553 Notify(PSTR(
"\r\nHCI Reset complete"), 0x80);
557 }
else if (hci_counter > hci_num_reset_loops) {
558 hci_num_reset_loops *= 10;
559 if (hci_num_reset_loops > 2000)
560 hci_num_reset_loops = 2000;
561 #ifdef DEBUG_USB_HOST
562 Notify(PSTR(
"\r\nNo response to HCI Reset"), 0x80);
571 #ifdef DEBUG_USB_HOST
572 Notify(PSTR(
"\r\nWrite class of device"), 0x80);
581 #ifdef DEBUG_USB_HOST
582 Notify(PSTR(
"\r\nLocal Bluetooth Address: "), 0x80);
583 for (int8_t i = 5; i > 0; i--) {
584 D_PrintHex<uint8_t > (
my_bdaddr[i], 0x80);
587 D_PrintHex<uint8_t > (my_bdaddr[0], 0x80);
606 #ifdef DEBUG_USB_HOST
607 Notify(PSTR(
"\r\nThe name is set to: "), 0x80);
616 #ifdef DEBUG_USB_HOST
617 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);
628 #ifdef DEBUG_USB_HOST
629 Notify(PSTR(
"\r\nWiimote found"), 0x80);
630 Notify(PSTR(
"\r\nNow just create the instance like so:"), 0x80);
631 Notify(PSTR(
"\r\nWII Wii(&Btd);"), 0x80);
632 Notify(PSTR(
"\r\nAnd then press any button on the Wiimote"), 0x80);
644 #ifdef DEBUG_USB_HOST
645 Notify(PSTR(
"\r\nConnecting to Wiimote"), 0x80);
655 #ifdef DEBUG_USB_HOST
656 Notify(PSTR(
"\r\nConnected to Wiimote"), 0x80);
661 #ifdef DEBUG_USB_HOST
662 Notify(PSTR(
"\r\nTrying to connect one more time..."), 0x80);
671 #ifdef DEBUG_USB_HOST
672 Notify(PSTR(
"\r\nWait For Incoming Connection Request"), 0x80);
683 #ifdef DEBUG_USB_HOST
684 Notify(PSTR(
"\r\nIncoming Connection Request"), 0x80);
694 #ifdef DEBUG_USB_HOST
695 Notify(PSTR(
"\r\nRemote Name: "), 0x80);
696 for (uint8_t i = 0; i < 30; i++) {
702 if (strncmp((
const char*)
remote_name,
"Nintendo", 8) == 0) {
704 #ifdef DEBUG_USB_HOST
705 Notify(PSTR(
"\r\nWiimote is connecting"), 0x80);
707 if (strncmp((
const char*)
remote_name,
"Nintendo RVL-CNT-01-TR", 22) == 0) {
708 #ifdef DEBUG_USB_HOST
709 Notify(PSTR(
" with Motion Plus Inside"), 0x80);
712 }
else if (strncmp((
const char*)
remote_name,
"Nintendo RVL-CNT-01-UC", 22) == 0) {
713 #ifdef DEBUG_USB_HOST
714 Notify(PSTR(
" - Wii U Pro Controller"), 0x80);
734 #ifdef DEBUG_USB_HOST
735 Notify(PSTR(
"\r\nConnected to Device: "), 0x80);
736 for (int8_t i = 5; i > 0; i--) {
740 D_PrintHex<uint8_t > (disc_bdaddr[0], 0x80);
754 if (hci_counter > 1000) {
762 #ifdef DEBUG_USB_HOST
763 Notify(PSTR(
"\r\nHCI Disconnected from Device"), 0x80);
781 void BTD::ACL_event_task() {
787 btService[i]->
ACLData(l2capinbuf);
790 else if (rcode !=
hrNAK) {
791 Notify(PSTR(
"\r\nACL data in error: "), 0x80);
792 D_PrintHex<uint8_t > (rcode, 0x80);
806 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE ].epAddr,
bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL);
812 hcibuf[1] = 0x03 << 2;
821 hcibuf[1] = 0x03 << 2;
833 hcibuf[1] = 0x03 << 2;
842 hcibuf[1] = 0x04 << 2;
850 hcibuf[1] = 0x04 << 2;
859 hcibuf[1] = 0x01 << 2;
875 hcibuf[1] = 0x01 << 2;
893 hcibuf[1] = 0x03 << 2;
894 hcibuf[2] = strlen(name) + 1;
896 for (i = 0; i < strlen(name); i++)
897 hcibuf[i + 3] = name[i];
898 hcibuf[i + 3] = 0x00;
906 hcibuf[1] = 0x01 << 2;
919 hcibuf[1] = 0x01 << 2;
928 hcibuf[1] = 0x01 << 2;
949 hcibuf[1] = 0x01 << 2;
960 #ifdef DEBUG_USB_HOST
961 Notify(PSTR(
"\r\nParing with Wii U Pro Controller"), 0x80);
963 for (uint8_t i = 0; i < 6; i++)
966 for (uint8_t i = 0; i < 6; i++)
969 for (uint8_t i = 16; i < 26; i++)
972 hcibuf[9] = strlen(
btdPin);
974 for (i = 0; i < strlen(
btdPin); i++)
975 hcibuf[i + 10] =
btdPin[i];
977 hcibuf[i + 10] = 0x00;
985 hcibuf[1] = 0x01 << 2;
999 hcibuf[1] = 0x01 << 2;
1013 hcibuf[1] = 0x01 << 2;
1016 hcibuf[4] = (uint8_t)((
hci_handle >> 8) & 0x0F);
1024 hcibuf[1] = 0x01 << 2;
1026 hcibuf[3] = (uint8_t)(handle & 0xFF);
1027 hcibuf[4] = (uint8_t)((handle >> 8) & 0x0F);
1035 hcibuf[1] = 0x03 << 2;
1069 void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh) {
1070 uint8_t buf[8 + nbytes];
1071 buf[0] = (uint8_t)(handle & 0xff);
1072 buf[1] = (uint8_t)(((handle >> 8) & 0x0f) | 0x20);
1073 buf[2] = (uint8_t)((4 + nbytes) & 0xff);
1074 buf[3] = (uint8_t)((4 + nbytes) >> 8);
1075 buf[4] = (uint8_t)(nbytes & 0xff);
1076 buf[5] = (uint8_t)(nbytes >> 8);
1077 buf[6] = channelLow;
1078 buf[7] = channelHigh;
1080 for (uint16_t i = 0; i < nbytes; i++)
1081 buf[8 + i] = data[i];
1086 #ifdef DEBUG_USB_HOST
1087 Notify(PSTR(
"\r\nError sending L2CAP message: 0x"), 0x80);
1088 D_PrintHex<uint8_t > (rcode, 0x80);
1089 Notify(PSTR(
" - Channel ID: "), 0x80);
1090 D_PrintHex<uint8_t > (channelHigh, 0x80);
1092 D_PrintHex<uint8_t > (channelLow, 0x80);
1099 l2capoutbuf[1] = rxid;
1100 l2capoutbuf[2] = 0x04;
1101 l2capoutbuf[3] = 0x00;
1102 l2capoutbuf[4] = (uint8_t)(psm & 0xff);
1103 l2capoutbuf[5] = (uint8_t)(psm >> 8);
1104 l2capoutbuf[6] = scid[0];
1105 l2capoutbuf[7] = scid[1];
1112 l2capoutbuf[1] = rxid;
1113 l2capoutbuf[2] = 0x08;
1114 l2capoutbuf[3] = 0x00;
1115 l2capoutbuf[4] = dcid[0];
1116 l2capoutbuf[5] = dcid[1];
1117 l2capoutbuf[6] = scid[0];
1118 l2capoutbuf[7] = scid[1];
1119 l2capoutbuf[8] = result;
1120 l2capoutbuf[9] = 0x00;
1121 l2capoutbuf[10] = 0x00;
1122 l2capoutbuf[11] = 0x00;
1129 l2capoutbuf[1] = rxid;
1130 l2capoutbuf[2] = 0x08;
1131 l2capoutbuf[3] = 0x00;
1132 l2capoutbuf[4] = dcid[0];
1133 l2capoutbuf[5] = dcid[1];
1134 l2capoutbuf[6] = 0x00;
1135 l2capoutbuf[7] = 0x00;
1136 l2capoutbuf[8] = 0x01;
1137 l2capoutbuf[9] = 0x02;
1138 l2capoutbuf[10] = 0xFF;
1139 l2capoutbuf[11] = 0xFF;
1146 l2capoutbuf[1] = rxid;
1147 l2capoutbuf[2] = 0x0A;
1148 l2capoutbuf[3] = 0x00;
1149 l2capoutbuf[4] = scid[0];
1150 l2capoutbuf[5] = scid[1];
1151 l2capoutbuf[6] = 0x00;
1152 l2capoutbuf[7] = 0x00;
1153 l2capoutbuf[8] = 0x00;
1154 l2capoutbuf[9] = 0x00;
1155 l2capoutbuf[10] = 0x01;
1156 l2capoutbuf[11] = 0x02;
1157 l2capoutbuf[12] = 0xA0;
1158 l2capoutbuf[13] = 0x02;
1165 l2capoutbuf[1] = rxid;
1166 l2capoutbuf[2] = 0x04;
1167 l2capoutbuf[3] = 0x00;
1168 l2capoutbuf[4] = dcid[0];
1169 l2capoutbuf[5] = dcid[1];
1170 l2capoutbuf[6] = scid[0];
1171 l2capoutbuf[7] = scid[1];
1178 l2capoutbuf[1] = rxid;
1179 l2capoutbuf[2] = 0x04;
1180 l2capoutbuf[3] = 0x00;
1181 l2capoutbuf[4] = dcid[0];
1182 l2capoutbuf[5] = dcid[1];
1183 l2capoutbuf[6] = scid[0];
1184 l2capoutbuf[7] = scid[1];
1191 l2capoutbuf[1] = rxid;
1192 l2capoutbuf[2] = 0x08;
1193 l2capoutbuf[3] = 0x00;
1194 l2capoutbuf[4] = infoTypeLow;
1195 l2capoutbuf[5] = infoTypeHigh;
1196 l2capoutbuf[6] = 0x00;
1197 l2capoutbuf[7] = 0x00;
1198 l2capoutbuf[8] = 0x00;
1199 l2capoutbuf[9] = 0x00;
1200 l2capoutbuf[10] = 0x00;
1201 l2capoutbuf[11] = 0x00;
1207 void BTD::setBdaddr(uint8_t* bdaddr) {
1213 for (uint8_t i = 0; i < 6; i++)
1214 buf[i + 2] = bdaddr[5 - i];
1217 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
1220 void BTD::setMoveBdaddr(uint8_t* bdaddr) {
1229 for (uint8_t i = 0; i < 6; i++)
1230 buf[i + 1] = bdaddr[i];
1233 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);