19 #define DEBUG // Uncomment to print data for debugging
45 uint8_t
BTD::Init(uint8_t parent, uint8_t port,
bool lowspeed) {
57 Notify(PSTR(
"\r\nBTD Init"));
62 Notify(PSTR(
"\r\nAddress in use"));
72 Notify(PSTR(
"\r\nAddress not found"));
79 Notify(PSTR(
"\r\nepinfo is null"));
117 Notify(PSTR(
"\r\nsetAddr: "));
119 PrintHex<uint8_t>(rcode);
123 Notify(PSTR(
"\r\nAddr: "));
138 goto FailSetDevTblEntry;
151 Notify(PSTR(
"\r\nDualshock 3 Controller Connected"));
153 Notify(PSTR(
"\r\nNavigation Controller Connected"));
160 Notify(PSTR(
"\r\nMotion Controller Connected"));
176 for (uint8_t i=0; i<num_of_conf; i++) {
180 goto FailGetConfDescr;
186 goto FailUnknownDevice;
191 goto FailSetDevTblEntry;
200 hci_num_reset_loops = 100;
207 Notify(PSTR(
"\r\nBluetooth Dongle Initialized"));
215 Notify(PSTR(
"\r\ngetDevDescr"));
220 Notify(PSTR(
"\r\nsetDevTblEn"));
225 Notify(PSTR(
"\r\ngetConf"));
230 Notify(PSTR(
"\r\nsetConf"));
235 Notify(PSTR(
"\r\nUnknown Device Connected - VID: "));
236 PrintHex<uint16_t>(VID);
238 PrintHex<uint16_t>(PID);
245 Notify(PSTR(
"\r\nBTD Init Failed, error code: "));
279 if(pollInterval < pep->bInterval)
285 Notify(PSTR(
"\r\nEndpoint descriptor:"));
286 Notify(PSTR(
"\r\nLength:\t\t"));
287 PrintHex<uint8_t>(ep_ptr->
bLength);
288 Notify(PSTR(
"\r\nType:\t\t"));
290 Notify(PSTR(
"\r\nAddress:\t"));
292 Notify(PSTR(
"\r\nAttributes:\t"));
294 Notify(PSTR(
"\r\nMaxPktSize:\t"));
296 Notify(PSTR(
"\r\nPoll Intrv:\t"));
305 btService[i]->
Reset();
323 void BTD::HCI_event_task() {
327 if(!rcode || rcode ==
hrNAK)
334 if((hcibuf[3] == 0x01) && (hcibuf[4] == 0x10)) {
337 }
else if((hcibuf[3] == 0x09) && (hcibuf[4] == 0x10)) {
338 for (uint8_t i = 0; i < 6; i++)
348 Notify(PSTR(
"\r\nHCI Command Failed: "));
349 PrintHex<uint8_t>(hcibuf[2]);
351 PrintHex<uint8_t>(hcibuf[4]);
353 PrintHex<uint8_t>(hcibuf[5]);
359 if(inquiry_counter >= 5) {
362 Notify(PSTR(
"\r\nCouldn't find Wiimote"));
374 Notify(PSTR(
"\r\nNumber of responses: "));
375 Serial.print(hcibuf[2]);
377 for(uint8_t i = 0; i < hcibuf[2]; i++) {
378 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)) {
379 if(hcibuf[4+8*hcibuf[2]+3*i] == 0x08)
394 Notify(PSTR(
"\r\nClass of device: "));
395 PrintHex<uint8_t>(hcibuf[6+8*hcibuf[2]+3*i]);
397 PrintHex<uint8_t>(hcibuf[5+8*hcibuf[2]+3*i]);
399 PrintHex<uint8_t>(hcibuf[4+8*hcibuf[2]+3*i]);
409 hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8);
414 Notify(PSTR(
"\r\nConnection Failed"));
428 for (uint8_t i = 0; i < 30; i++)
447 Notify(PSTR(
"\r\nPairing with wiimote"));
453 Notify(PSTR(
"\r\nBluetooth pin is set too: "));
460 Notify(PSTR(
"\r\nNo pin was set"));
468 Notify(PSTR(
"\r\nReceived Key Request"));
476 Notify(PSTR(
"\r\nPairing successful"));
496 if(hcibuf[0] != 0x00) {
497 Notify(PSTR(
"\r\nUnmanaged HCI Event: "));
498 PrintHex<uint8_t>(hcibuf[0]);
507 Notify(PSTR(
"\r\nHCI event error: "));
508 PrintHex<uint8_t>(rcode);
514 void BTD::HCI_task() {
518 if (hci_counter > hci_num_reset_loops) {
530 Notify(PSTR(
"\r\nHCI Reset complete"));
535 else if (hci_counter > hci_num_reset_loops) {
536 hci_num_reset_loops *= 10;
537 if(hci_num_reset_loops > 2000)
538 hci_num_reset_loops = 2000;
540 Notify(PSTR(
"\r\nNo response to HCI Reset"));
550 Notify(PSTR(
"\r\nLocal Bluetooth Address: "));
551 for(int8_t i = 5; i > 0;i--) {
575 Notify(PSTR(
"\r\nThe name is set to: "));
585 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"));
598 Notify(PSTR(
"\r\nWiimote found"));
599 Notify(PSTR(
"\r\nNow just create the instance like so:"));
600 Notify(PSTR(
"\r\nWII Wii(&Btd);"));
601 Notify(PSTR(
"\r\nAnd then press any button on the Wiimote"));
614 Notify(PSTR(
"\r\nConnecting to Wiimote"));
625 Notify(PSTR(
"\r\nConnected to Wiimote"));
631 Notify(PSTR(
"\r\nTrying to connect one more time..."));
641 Notify(PSTR(
"\r\nWait For Incoming Connection Request"));
653 Notify(PSTR(
"\r\nIncoming Connection Request"));
664 Notify(PSTR(
"\r\nRemote Name: "));
665 for (uint8_t i = 0; i < 30; i++) {
671 if(strncmp((
const char*)
remote_name,
"Nintendo", 8) == 0) {
673 Notify(PSTR(
"\r\nWiimote is connecting"));
675 if(strncmp((
const char*)
remote_name,
"Nintendo RVL-CNT-01-TR", 22) == 0) {
677 Notify(PSTR(
" with Motion Plus Inside"));
681 else if(strncmp((
const char*)
remote_name,
"Nintendo RVL-CNT-01-UC", 22) == 0) {
683 Notify(PSTR(
" - Wii U Pro Controller"));
705 Notify(PSTR(
"\r\nConnected to Device: "));
706 for(int8_t i = 5; i>0;i--) {
724 if (hci_counter > 1000) {
733 Notify(PSTR(
"\r\nHCI Disconnected from Device"));
751 void BTD::ACL_event_task() {
757 btService[i]->
ACLData(l2capinbuf);
760 else if (rcode !=
hrNAK) {
761 Notify(PSTR(
"\r\nACL data in error: "));
762 PrintHex<uint8_t>(rcode);
775 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE ].epAddr,
bmREQ_HCI_OUT, 0x00, 0x00, 0x00 ,0x00, nbytes, nbytes, data, NULL);
780 hcibuf[1] = 0x03 << 2;
787 hcibuf[1] = 0x03 << 2;
797 hcibuf[1] = 0x03 << 2;
804 hcibuf[1] = 0x04 << 2;
810 hcibuf[1] = 0x04 << 2;
817 hcibuf[1] = 0x01 << 2;
832 hcibuf[1] = 0x01 << 2;
849 hcibuf[1] = 0x03 << 2;
850 hcibuf[2] = strlen(name)+1;
852 for(i = 0; i < strlen(name); i++)
853 hcibuf[i+3] = name[i];
861 hcibuf[1] = 0x01 << 2;
873 hcibuf[1] = 0x01 << 2;
881 hcibuf[1] = 0x01 << 2;
901 hcibuf[1] = 0x01 << 2;
917 for(uint8_t i = 16; i < 26; i++)
921 Notify(PSTR(
"\r\nParing with Wii U Pro Controller"));
930 for(uint8_t i = 16; i < 26; i++)
933 hcibuf[9] = strlen(
btdPin);
935 for(i = 0; i < strlen(
btdPin); i++)
945 hcibuf[1] = 0x01 << 2;
958 hcibuf[1] = 0x01 << 2;
971 hcibuf[1] = 0x01 << 2;
974 hcibuf[4] = (uint8_t)((
hci_handle >> 8) & 0x0F);
981 hcibuf[1] = 0x01 << 2;
983 hcibuf[3] = (uint8_t)(handle & 0xFF);
984 hcibuf[4] = (uint8_t)((handle >> 8) & 0x0F);
1014 void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh) {
1015 uint8_t buf[8+nbytes];
1016 buf[0] = (uint8_t)(handle & 0xff);
1017 buf[1] = (uint8_t)(((handle >> 8) & 0x0f) | 0x20);
1018 buf[2] = (uint8_t)((4 + nbytes) & 0xff);
1019 buf[3] = (uint8_t)((4 + nbytes) >> 8);
1020 buf[4] = (uint8_t)(nbytes & 0xff);
1021 buf[5] = (uint8_t)(nbytes >> 8);
1022 buf[6] = channelLow;
1023 buf[7] = channelHigh;
1025 for (uint16_t i = 0; i < nbytes; i++)
1026 buf[8 + i] = data[i];
1032 Notify(PSTR(
"\r\nError sending L2CAP message: 0x"));
1033 PrintHex<uint8_t>(rcode);
1034 Notify(PSTR(
" - Channel ID: "));
1035 Serial.print(channelHigh);
1037 Serial.print(channelLow);
1043 l2capoutbuf[1] = rxid;
1044 l2capoutbuf[2] = 0x04;
1045 l2capoutbuf[3] = 0x00;
1046 l2capoutbuf[4] = (uint8_t)(psm & 0xff);
1047 l2capoutbuf[5] = (uint8_t)(psm >> 8);
1048 l2capoutbuf[6] = scid[0];
1049 l2capoutbuf[7] = scid[1];
1055 l2capoutbuf[1] = rxid;
1056 l2capoutbuf[2] = 0x08;
1057 l2capoutbuf[3] = 0x00;
1058 l2capoutbuf[4] = dcid[0];
1059 l2capoutbuf[5] = dcid[1];
1060 l2capoutbuf[6] = scid[0];
1061 l2capoutbuf[7] = scid[1];
1062 l2capoutbuf[8] = result;
1063 l2capoutbuf[9] = 0x00;
1064 l2capoutbuf[10] = 0x00;
1065 l2capoutbuf[11] = 0x00;
1071 l2capoutbuf[1] = rxid;
1072 l2capoutbuf[2] = 0x08;
1073 l2capoutbuf[3] = 0x00;
1074 l2capoutbuf[4] = dcid[0];
1075 l2capoutbuf[5] = dcid[1];
1076 l2capoutbuf[6] = 0x00;
1077 l2capoutbuf[7] = 0x00;
1078 l2capoutbuf[8] = 0x01;
1079 l2capoutbuf[9] = 0x02;
1080 l2capoutbuf[10] = 0xFF;
1081 l2capoutbuf[11] = 0xFF;
1087 l2capoutbuf[1] = rxid;
1088 l2capoutbuf[2] = 0x0A;
1089 l2capoutbuf[3] = 0x00;
1090 l2capoutbuf[4] = scid[0];
1091 l2capoutbuf[5] = scid[1];
1092 l2capoutbuf[6] = 0x00;
1093 l2capoutbuf[7] = 0x00;
1094 l2capoutbuf[8] = 0x00;
1095 l2capoutbuf[9] = 0x00;
1096 l2capoutbuf[10] = 0x01;
1097 l2capoutbuf[11] = 0x02;
1098 l2capoutbuf[12] = 0xA0;
1099 l2capoutbuf[13] = 0x02;
1105 l2capoutbuf[1] = rxid;
1106 l2capoutbuf[2] = 0x04;
1107 l2capoutbuf[3] = 0x00;
1108 l2capoutbuf[4] = dcid[0];
1109 l2capoutbuf[5] = dcid[1];
1110 l2capoutbuf[6] = scid[0];
1111 l2capoutbuf[7] = scid[1];
1116 l2capoutbuf[1] = rxid;
1117 l2capoutbuf[2] = 0x04;
1118 l2capoutbuf[3] = 0x00;
1119 l2capoutbuf[4] = dcid[0];
1120 l2capoutbuf[5] = dcid[1];
1121 l2capoutbuf[6] = scid[0];
1122 l2capoutbuf[7] = scid[1];
1127 l2capoutbuf[1] = rxid;
1128 l2capoutbuf[2] = 0x08;
1129 l2capoutbuf[3] = 0x00;
1130 l2capoutbuf[4] = infoTypeLow;
1131 l2capoutbuf[5] = infoTypeHigh;
1132 l2capoutbuf[6] = 0x00;
1133 l2capoutbuf[7] = 0x00;
1134 l2capoutbuf[8] = 0x00;
1135 l2capoutbuf[9] = 0x00;
1136 l2capoutbuf[10] = 0x00;
1137 l2capoutbuf[11] = 0x00;
1142 void BTD::setBdaddr(uint8_t* BDADDR) {
1147 for (uint8_t i = 0; i < 6; i++)
1148 buf[i+2] = BDADDR[5 - i];
1151 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
1153 Notify(PSTR(
"\r\nBluetooth Address was set to: "));
1154 for(int8_t i = 5; i > 0; i--) {
1161 void BTD::setMoveBdaddr(uint8_t* BDADDR) {
1170 for (uint8_t i = 0; i < 6; i++)
1171 buf[i + 1] = BDADDR[i];
1174 pUsb->
ctrlReq(
bAddress,
epInfo[
BTD_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00,11,11, buf, NULL);
1176 Notify(PSTR(
"\r\nBluetooth Address was set to: "));
1177 for(int8_t i = 5; i > 0; i--) {