port debugging of new files, need to eliminate all serial.print yet.

This commit is contained in:
Andrew J. Kroll 2013-03-28 04:37:09 -04:00
parent 629594f957
commit 9b224b9899
7 changed files with 1736 additions and 1713 deletions

251
BTD.cpp
View file

@ -54,12 +54,12 @@ uint8_t BTD::Init(uint8_t parent, uint8_t port, bool lowspeed) {
// get memory address of USB device address pool // get memory address of USB device address pool
AddressPool &addrPool = pUsb->GetAddressPool(); AddressPool &addrPool = pUsb->GetAddressPool();
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nBTD Init")); Notify(PSTR("\r\nBTD Init"), 0x80);
#endif #endif
// check if address has already been assigned to an instance // check if address has already been assigned to an instance
if (bAddress) { if (bAddress) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nAddress in use")); Notify(PSTR("\r\nAddress in use"), 0x80);
#endif #endif
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE; return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
} }
@ -69,14 +69,14 @@ uint8_t BTD::Init(uint8_t parent, uint8_t port, bool lowspeed) {
if (!p) { if (!p) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nAddress not found")); Notify(PSTR("\r\nAddress not found"), 0x80);
#endif #endif
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
} }
if (!p->epinfo) { if (!p->epinfo) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nepinfo is null")); Notify(PSTR("\r\nepinfo is null"), 0x80);
#endif #endif
return USB_ERROR_EPINFO_IS_NULL; return USB_ERROR_EPINFO_IS_NULL;
} }
@ -114,14 +114,14 @@ uint8_t BTD::Init(uint8_t parent, uint8_t port, bool lowspeed) {
addrPool.FreeAddress(bAddress); addrPool.FreeAddress(bAddress);
bAddress = 0; bAddress = 0;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetAddr: ")); Notify(PSTR("\r\nsetAddr: "), 0x80);
#endif #endif
PrintHex<uint8_t>(rcode); PrintHex<uint8_t > (rcode, 0x80);
return rcode; return rcode;
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nAddr: ")); Notify(PSTR("\r\nAddr: "), 0x80);
PrintHex<uint8_t>(bAddress); PrintHex<uint8_t > (bAddress, 0x80);
#endif #endif
p->lowspeed = false; p->lowspeed = false;
@ -148,16 +148,15 @@ uint8_t BTD::Init(uint8_t parent, uint8_t port, bool lowspeed) {
if (PID == PS3_PID || PID == PS3NAVIGATION_PID) { if (PID == PS3_PID || PID == PS3NAVIGATION_PID) {
#ifdef DEBUG #ifdef DEBUG
if (PID == PS3_PID) if (PID == PS3_PID)
Notify(PSTR("\r\nDualshock 3 Controller Connected")); Notify(PSTR("\r\nDualshock 3 Controller Connected"), 0x80);
else // must be a navigation controller else // must be a navigation controller
Notify(PSTR("\r\nNavigation Controller Connected")); Notify(PSTR("\r\nNavigation Controller Connected"), 0x80);
#endif #endif
/* Set internal bluetooth address */ /* Set internal bluetooth address */
setBdaddr(my_bdaddr); setBdaddr(my_bdaddr);
} } else { // must be a Motion controller
else { // must be a Motion controller
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nMotion Controller Connected")); Notify(PSTR("\r\nMotion Controller Connected"), 0x80);
#endif #endif
setMoveBdaddr(my_bdaddr); setMoveBdaddr(my_bdaddr);
} }
@ -165,8 +164,7 @@ uint8_t BTD::Init(uint8_t parent, uint8_t port, bool lowspeed) {
pUsb->setAddr(bAddress, 0, 0); // Reset address pUsb->setAddr(bAddress, 0, 0); // Reset address
Release(); // Release device Release(); // Release device
return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; // return return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; // return
} } else {
else {
num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations; num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations;
// check if attached device is a Bluetooth dongle and fill endpoint data structure // check if attached device is a Bluetooth dongle and fill endpoint data structure
@ -204,7 +202,7 @@ uint8_t BTD::Init(uint8_t parent, uint8_t port, bool lowspeed) {
bPollEnable = true; bPollEnable = true;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nBluetooth Dongle Initialized")); Notify(PSTR("\r\nBluetooth Dongle Initialized"), 0x80);
#endif #endif
} }
return 0; // Successful configuration return 0; // Successful configuration
@ -212,42 +210,43 @@ uint8_t BTD::Init(uint8_t parent, uint8_t port, bool lowspeed) {
/* diagnostic messages */ /* diagnostic messages */
FailGetDevDescr: FailGetDevDescr:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\ngetDevDescr")); Notify(PSTR("\r\ngetDevDescr"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailSetDevTblEntry: FailSetDevTblEntry:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetDevTblEn")); Notify(PSTR("\r\nsetDevTblEn"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailGetConfDescr: FailGetConfDescr:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\ngetConf")); Notify(PSTR("\r\ngetConf"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailSetConf: FailSetConf:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetConf")); Notify(PSTR("\r\nsetConf"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailUnknownDevice: FailUnknownDevice:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nUnknown Device Connected - VID: ")); Notify(PSTR("\r\nUnknown Device Connected - VID: "), 0x80);
PrintHex<uint16_t>(VID); PrintHex<uint16_t > (VID, 0x80);
Notify(PSTR(" PID: ")); Notify(PSTR(" PID: "), 0x80);
PrintHex<uint16_t>(PID); PrintHex<uint16_t > (PID, 0x80);
#endif #endif
pUsb->setAddr(bAddress, 0, 0); // Reset address pUsb->setAddr(bAddress, 0, 0); // Reset address
rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
goto Fail; goto Fail;
Fail: Fail:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nBTD Init Failed, error code: ")); Notify(PSTR("\r\nBTD Init Failed, error code: "), 0x80);
Serial.print(rcode); Serial.print(rcode);
#endif #endif
Release(); Release();
return rcode; return rcode;
} }
/* Extracts interrupt-IN, bulk-IN, bulk-OUT endpoint information from config descriptor */ /* Extracts interrupt-IN, bulk-IN, bulk-OUT endpoint information from config descriptor */
void BTD::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) { void BTD::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) {
//ErrorMessage<uint8_t>(PSTR("Conf.Val"),conf); //ErrorMessage<uint8_t>(PSTR("Conf.Val"),conf);
@ -280,21 +279,22 @@ void BTD::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto
pollInterval = pep->bInterval; pollInterval = pep->bInterval;
bNumEP++; bNumEP++;
} }
void BTD::PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr) { void BTD::PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr) {
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nEndpoint descriptor:")); Notify(PSTR("\r\nEndpoint descriptor:"), 0x80);
Notify(PSTR("\r\nLength:\t\t")); Notify(PSTR("\r\nLength:\t\t"), 0x80);
PrintHex<uint8_t>(ep_ptr->bLength); PrintHex<uint8_t > (ep_ptr->bLength, 0x80);
Notify(PSTR("\r\nType:\t\t")); Notify(PSTR("\r\nType:\t\t"), 0x80);
PrintHex<uint8_t>(ep_ptr->bDescriptorType); PrintHex<uint8_t > (ep_ptr->bDescriptorType, 0x80);
Notify(PSTR("\r\nAddress:\t")); Notify(PSTR("\r\nAddress:\t"), 0x80);
PrintHex<uint8_t>(ep_ptr->bEndpointAddress); PrintHex<uint8_t > (ep_ptr->bEndpointAddress, 0x80);
Notify(PSTR("\r\nAttributes:\t")); Notify(PSTR("\r\nAttributes:\t"), 0x80);
PrintHex<uint8_t>(ep_ptr->bmAttributes); PrintHex<uint8_t > (ep_ptr->bmAttributes, 0x80);
Notify(PSTR("\r\nMaxPktSize:\t")); Notify(PSTR("\r\nMaxPktSize:\t"), 0x80);
PrintHex<uint16_t>(ep_ptr->wMaxPacketSize); PrintHex<uint16_t > (ep_ptr->wMaxPacketSize, 0x80);
Notify(PSTR("\r\nPoll Intrv:\t")); Notify(PSTR("\r\nPoll Intrv:\t"), 0x80);
PrintHex<uint8_t>(ep_ptr->bInterval); PrintHex<uint8_t > (ep_ptr->bInterval, 0x80);
#endif #endif
} }
@ -309,6 +309,7 @@ uint8_t BTD::Release() {
bNumEP = 1; // must have to be reset to 1 bNumEP = 1; // must have to be reset to 1
return 0; return 0;
} }
uint8_t BTD::Poll() { uint8_t BTD::Poll() {
if (!bPollEnable) if (!bPollEnable)
return 0; return 0;
@ -345,12 +346,12 @@ void BTD::HCI_event_task() {
case EV_COMMAND_STATUS: case EV_COMMAND_STATUS:
if (hcibuf[2]) { // show status on serial if not OK if (hcibuf[2]) { // show status on serial if not OK
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHCI Command Failed: ")); Notify(PSTR("\r\nHCI Command Failed: "), 0x80);
PrintHex<uint8_t>(hcibuf[2]); PrintHex<uint8_t > (hcibuf[2], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(hcibuf[4]); PrintHex<uint8_t > (hcibuf[4], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(hcibuf[5]); PrintHex<uint8_t > (hcibuf[5], 0x80);
#endif #endif
} }
break; break;
@ -359,7 +360,7 @@ void BTD::HCI_event_task() {
if (inquiry_counter >= 5) { if (inquiry_counter >= 5) {
inquiry_counter = 0; inquiry_counter = 0;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nCouldn't find Wiimote")); Notify(PSTR("\r\nCouldn't find Wiimote"), 0x80);
#endif #endif
connectToWii = false; connectToWii = false;
pairWithWii = false; pairWithWii = false;
@ -371,7 +372,7 @@ void BTD::HCI_event_task() {
case EV_INQUIRY_RESULT: case EV_INQUIRY_RESULT:
if (hcibuf[2]) { // Check that there is more than zero responses if (hcibuf[2]) { // Check that there is more than zero responses
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nNumber of responses: ")); Notify(PSTR("\r\nNumber of responses: "), 0x80);
Serial.print(hcibuf[2]); Serial.print(hcibuf[2]);
#endif #endif
for (uint8_t i = 0; i < hcibuf[2]; i++) { for (uint8_t i = 0; i < hcibuf[2]; i++) {
@ -391,12 +392,12 @@ void BTD::HCI_event_task() {
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
else { else {
Notify(PSTR("\r\nClass of device: ")); Notify(PSTR("\r\nClass of device: "), 0x80);
PrintHex<uint8_t>(hcibuf[6+8*hcibuf[2]+3*i]); PrintHex<uint8_t > (hcibuf[6 + 8 * hcibuf[2] + 3 * i], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(hcibuf[5+8*hcibuf[2]+3*i]); PrintHex<uint8_t > (hcibuf[5 + 8 * hcibuf[2] + 3 * i], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(hcibuf[4+8*hcibuf[2]+3*i]); PrintHex<uint8_t > (hcibuf[4 + 8 * hcibuf[2] + 3 * i], 0x80);
} }
#endif #endif
} }
@ -411,7 +412,7 @@ void BTD::HCI_event_task() {
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
else { else {
Notify(PSTR("\r\nConnection Failed")); Notify(PSTR("\r\nConnection Failed"), 0x80);
} }
#endif #endif
break; break;
@ -444,20 +445,18 @@ void BTD::HCI_event_task() {
case EV_PIN_CODE_REQUEST: case EV_PIN_CODE_REQUEST:
if (pairWithWii) { if (pairWithWii) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nPairing with wiimote")); Notify(PSTR("\r\nPairing with wiimote"), 0x80);
#endif #endif
hci_pin_code_request_reply(); hci_pin_code_request_reply();
} } else if (btdPin != NULL) {
else if(btdPin != NULL) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nBluetooth pin is set too: ")); Notify(PSTR("\r\nBluetooth pin is set too: "), 0x80);
Serial.print(btdPin); Serial.print(btdPin);
#endif #endif
hci_pin_code_request_reply(); hci_pin_code_request_reply();
} } else {
else {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nNo pin was set")); Notify(PSTR("\r\nNo pin was set"), 0x80);
#endif #endif
hci_pin_code_negative_request_reply(); hci_pin_code_negative_request_reply();
} }
@ -465,7 +464,7 @@ void BTD::HCI_event_task() {
case EV_LINK_KEY_REQUEST: case EV_LINK_KEY_REQUEST:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nReceived Key Request")); Notify(PSTR("\r\nReceived Key Request"), 0x80);
#endif #endif
hci_link_key_request_negative_reply(); hci_link_key_request_negative_reply();
break; break;
@ -473,7 +472,7 @@ void BTD::HCI_event_task() {
case EV_AUTHENTICATION_COMPLETE: case EV_AUTHENTICATION_COMPLETE:
if (pairWithWii && !connectToWii) { if (pairWithWii && !connectToWii) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nPairing successful")); Notify(PSTR("\r\nPairing successful"), 0x80);
#endif #endif
connectToWii = true; // Only send the ACL data to the Wii service connectToWii = true; // Only send the ACL data to the Wii service
} }
@ -494,8 +493,8 @@ void BTD::HCI_event_task() {
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
default: default:
if (hcibuf[0] != 0x00) { if (hcibuf[0] != 0x00) {
Notify(PSTR("\r\nUnmanaged HCI Event: ")); Notify(PSTR("\r\nUnmanaged HCI Event: "), 0x80);
PrintHex<uint8_t>(hcibuf[0]); PrintHex<uint8_t > (hcibuf[0], 0x80);
} }
break; break;
#endif #endif
@ -504,8 +503,8 @@ void BTD::HCI_event_task() {
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
else { else {
Notify(PSTR("\r\nHCI event error: ")); Notify(PSTR("\r\nHCI event error: "), 0x80);
PrintHex<uint8_t>(rcode); PrintHex<uint8_t > (rcode, 0x80);
} }
#endif #endif
} }
@ -527,17 +526,16 @@ void BTD::HCI_task() {
if (hci_cmd_complete) { if (hci_cmd_complete) {
hci_counter = 0; hci_counter = 0;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHCI Reset complete")); Notify(PSTR("\r\nHCI Reset complete"), 0x80);
#endif #endif
hci_state = HCI_CLASS_STATE; hci_state = HCI_CLASS_STATE;
hci_write_class_of_device(); hci_write_class_of_device();
} } else if (hci_counter > hci_num_reset_loops) {
else if (hci_counter > hci_num_reset_loops) {
hci_num_reset_loops *= 10; hci_num_reset_loops *= 10;
if (hci_num_reset_loops > 2000) if (hci_num_reset_loops > 2000)
hci_num_reset_loops = 2000; hci_num_reset_loops = 2000;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nNo response to HCI Reset")); Notify(PSTR("\r\nNo response to HCI Reset"), 0x80);
#endif #endif
hci_state = HCI_INIT_STATE; hci_state = HCI_INIT_STATE;
hci_counter = 0; hci_counter = 0;
@ -547,7 +545,7 @@ void BTD::HCI_task() {
case HCI_CLASS_STATE: case HCI_CLASS_STATE:
if (hci_cmd_complete) { if (hci_cmd_complete) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nWrite class of device")); Notify(PSTR("\r\nWrite class of device"), 0x80);
#endif #endif
hci_state = HCI_BDADDR_STATE; hci_state = HCI_BDADDR_STATE;
hci_read_bdaddr(); hci_read_bdaddr();
@ -557,12 +555,12 @@ void BTD::HCI_task() {
case HCI_BDADDR_STATE: case HCI_BDADDR_STATE:
if (hci_read_bdaddr_complete) { if (hci_read_bdaddr_complete) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nLocal Bluetooth Address: ")); Notify(PSTR("\r\nLocal Bluetooth Address: "), 0x80);
for (int8_t i = 5; i > 0; i--) { for (int8_t i = 5; i > 0; i--) {
PrintHex<uint8_t>(my_bdaddr[i]); PrintHex<uint8_t > (my_bdaddr[i], 0x80);
Notify(PSTR(":")); Notify(PSTR(":"), 0x80);
} }
PrintHex<uint8_t>(my_bdaddr[0]); PrintHex<uint8_t > (my_bdaddr[0], 0x80);
#endif #endif
hci_read_local_version_information(); hci_read_local_version_information();
hci_state = HCI_LOCAL_VERSION_STATE; hci_state = HCI_LOCAL_VERSION_STATE;
@ -582,7 +580,7 @@ void BTD::HCI_task() {
case HCI_SET_NAME_STATE: case HCI_SET_NAME_STATE:
if (hci_cmd_complete) { if (hci_cmd_complete) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nThe name is set to: ")); Notify(PSTR("\r\nThe name is set to: "), 0x80);
Serial.print(btdName); Serial.print(btdName);
#endif #endif
hci_state = HCI_CHECK_WII_SERVICE; hci_state = HCI_CHECK_WII_SERVICE;
@ -592,12 +590,11 @@ void BTD::HCI_task() {
case HCI_CHECK_WII_SERVICE: case HCI_CHECK_WII_SERVICE:
if (pairWithWii) { // Check if it should try to connect to a wiimote if (pairWithWii) { // Check if it should try to connect to a wiimote
#ifdef DEBUG #ifdef DEBUG
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")); 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);
#endif #endif
hci_inquiry(); hci_inquiry();
hci_state = HCI_INQUIRY_STATE; hci_state = HCI_INQUIRY_STATE;
} } else
else
hci_state = HCI_SCANNING_STATE; // Don't try to connect to a Wiimote hci_state = HCI_SCANNING_STATE; // Don't try to connect to a Wiimote
break; break;
@ -605,10 +602,10 @@ void BTD::HCI_task() {
if (hci_wii_found) { if (hci_wii_found) {
hci_inquiry_cancel(); // Stop inquiry hci_inquiry_cancel(); // Stop inquiry
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nWiimote found")); Notify(PSTR("\r\nWiimote found"), 0x80);
Notify(PSTR("\r\nNow just create the instance like so:")); Notify(PSTR("\r\nNow just create the instance like so:"), 0x80);
Notify(PSTR("\r\nWII Wii(&Btd);")); Notify(PSTR("\r\nWII Wii(&Btd);"), 0x80);
Notify(PSTR("\r\nAnd then press any button on the Wiimote")); Notify(PSTR("\r\nAnd then press any button on the Wiimote"), 0x80);
#endif #endif
if (motionPlusInside) { if (motionPlusInside) {
hci_remote_name(); // We need to know the name to distinguish between a Wiimote and a Wii U Pro Controller hci_remote_name(); // We need to know the name to distinguish between a Wiimote and a Wii U Pro Controller
@ -621,7 +618,7 @@ void BTD::HCI_task() {
case HCI_CONNECT_WII_STATE: case HCI_CONNECT_WII_STATE:
if (hci_cmd_complete) { if (hci_cmd_complete) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nConnecting to Wiimote")); Notify(PSTR("\r\nConnecting to Wiimote"), 0x80);
#endif #endif
hci_connect(); hci_connect();
hci_state = HCI_CONNECTED_WII_STATE; hci_state = HCI_CONNECTED_WII_STATE;
@ -632,13 +629,13 @@ void BTD::HCI_task() {
if (hci_connect_event) { if (hci_connect_event) {
if (hci_connect_complete) { if (hci_connect_complete) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nConnected to Wiimote")); Notify(PSTR("\r\nConnected to Wiimote"), 0x80);
#endif #endif
hci_authentication_request(); // This will start the pairing with the wiimote hci_authentication_request(); // This will start the pairing with the wiimote
hci_state = HCI_SCANNING_STATE; hci_state = HCI_SCANNING_STATE;
} else { } else {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nTrying to connect one more time...")); Notify(PSTR("\r\nTrying to connect one more time..."), 0x80);
#endif #endif
hci_connect(); // Try to connect one more time hci_connect(); // Try to connect one more time
} }
@ -648,7 +645,7 @@ void BTD::HCI_task() {
case HCI_SCANNING_STATE: case HCI_SCANNING_STATE:
if (!connectToWii && !pairWithWii) { if (!connectToWii && !pairWithWii) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nWait For Incoming Connection Request")); Notify(PSTR("\r\nWait For Incoming Connection Request"), 0x80);
#endif #endif
hci_write_scan_enable(); hci_write_scan_enable();
watingForConnection = true; watingForConnection = true;
@ -660,7 +657,7 @@ void BTD::HCI_task() {
if (hci_incoming_connect_request) { if (hci_incoming_connect_request) {
watingForConnection = false; watingForConnection = false;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nIncoming Connection Request")); Notify(PSTR("\r\nIncoming Connection Request"), 0x80);
#endif #endif
hci_remote_name(); hci_remote_name();
hci_state = HCI_REMOTE_NAME_STATE; hci_state = HCI_REMOTE_NAME_STATE;
@ -671,7 +668,7 @@ void BTD::HCI_task() {
case HCI_REMOTE_NAME_STATE: case HCI_REMOTE_NAME_STATE:
if (hci_remote_name_complete) { if (hci_remote_name_complete) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nRemote Name: ")); Notify(PSTR("\r\nRemote Name: "), 0x80);
for (uint8_t i = 0; i < 30; i++) { for (uint8_t i = 0; i < 30; i++) {
if (remote_name[i] == NULL) if (remote_name[i] == NULL)
break; break;
@ -680,17 +677,16 @@ void BTD::HCI_task() {
#endif #endif
if (strncmp((const char*)remote_name, "Nintendo", 8) == 0) { if (strncmp((const char*)remote_name, "Nintendo", 8) == 0) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nWiimote is connecting")); Notify(PSTR("\r\nWiimote is connecting"), 0x80);
#endif #endif
if (strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-TR", 22) == 0) { if (strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-TR", 22) == 0) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR(" with Motion Plus Inside")); Notify(PSTR(" with Motion Plus Inside"), 0x80);
#endif #endif
motionPlusInside = true; motionPlusInside = true;
} } else if (strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-UC", 22) == 0) {
else if(strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-UC", 22) == 0) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR(" - Wii U Pro Controller")); Notify(PSTR(" - Wii U Pro Controller"), 0x80);
#endif #endif
motionPlusInside = true; motionPlusInside = true;
wiiUProController = true; wiiUProController = true;
@ -712,12 +708,12 @@ void BTD::HCI_task() {
case HCI_CONNECTED_STATE: case HCI_CONNECTED_STATE:
if (hci_connect_complete) { if (hci_connect_complete) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nConnected to Device: ")); Notify(PSTR("\r\nConnected to Device: "), 0x80);
for (int8_t i = 5; i > 0; i--) { for (int8_t i = 5; i > 0; i--) {
PrintHex<uint8_t>(disc_bdaddr[i]); PrintHex<uint8_t > (disc_bdaddr[i], 0x80);
Notify(PSTR(":")); Notify(PSTR(":"), 0x80);
} }
PrintHex<uint8_t>(disc_bdaddr[0]); PrintHex<uint8_t > (disc_bdaddr[0], 0x80);
#endif #endif
// Clear these flags for a new connection // Clear these flags for a new connection
l2capConnectionClaimed = false; l2capConnectionClaimed = false;
@ -740,7 +736,7 @@ void BTD::HCI_task() {
case HCI_DISCONNECT_STATE: case HCI_DISCONNECT_STATE:
if (hci_disconnect_complete) { if (hci_disconnect_complete) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHCI Disconnected from Device")); Notify(PSTR("\r\nHCI Disconnected from Device"), 0x80);
#endif #endif
hci_event_flag = 0; // Clear all flags hci_event_flag = 0; // Clear all flags
@ -768,8 +764,8 @@ void BTD::ACL_event_task() {
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
else if (rcode != hrNAK) { else if (rcode != hrNAK) {
Notify(PSTR("\r\nACL data in error: ")); Notify(PSTR("\r\nACL data in error: "), 0x80);
PrintHex<uint8_t>(rcode); PrintHex<uint8_t > (rcode, 0x80);
} }
#endif #endif
for (uint8_t i = 0; i < BTD_NUMSERVICES; i++) for (uint8_t i = 0; i < BTD_NUMSERVICES; i++)
@ -779,11 +775,13 @@ void BTD::ACL_event_task() {
/************************************************************/ /************************************************************/
/* HCI Commands */ /* HCI Commands */
/************************************************************/ /************************************************************/
void BTD::HCI_Command(uint8_t* data, uint16_t nbytes) { void BTD::HCI_Command(uint8_t* data, uint16_t nbytes) {
hci_event_flag &= ~HCI_FLAG_CMD_COMPLETE; hci_event_flag &= ~HCI_FLAG_CMD_COMPLETE;
pUsb->ctrlReq(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL); pUsb->ctrlReq(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL);
} }
void BTD::hci_reset() { void BTD::hci_reset() {
hci_event_flag = 0; // Clear all the flags hci_event_flag = 0; // Clear all the flags
hcibuf[0] = 0x03; // HCI OCF = 3 hcibuf[0] = 0x03; // HCI OCF = 3
@ -791,6 +789,7 @@ void BTD::hci_reset() {
hcibuf[2] = 0x00; hcibuf[2] = 0x00;
HCI_Command(hcibuf, 3); HCI_Command(hcibuf, 3);
} }
void BTD::hci_write_scan_enable() { void BTD::hci_write_scan_enable() {
hci_event_flag &= ~HCI_FLAG_INCOMING_REQUEST; hci_event_flag &= ~HCI_FLAG_INCOMING_REQUEST;
hcibuf[0] = 0x1A; // HCI OCF = 1A hcibuf[0] = 0x1A; // HCI OCF = 1A
@ -802,6 +801,7 @@ void BTD::hci_write_scan_enable() {
hcibuf[3] = 0x02; // Inquiry Scan disabled. Page Scan enabled. hcibuf[3] = 0x02; // Inquiry Scan disabled. Page Scan enabled.
HCI_Command(hcibuf, 4); HCI_Command(hcibuf, 4);
} }
void BTD::hci_write_scan_disable() { void BTD::hci_write_scan_disable() {
hcibuf[0] = 0x1A; // HCI OCF = 1A hcibuf[0] = 0x1A; // HCI OCF = 1A
hcibuf[1] = 0x03 << 2; // HCI OGF = 3 hcibuf[1] = 0x03 << 2; // HCI OGF = 3
@ -809,18 +809,21 @@ void BTD::hci_write_scan_disable() {
hcibuf[3] = 0x00; // Inquiry Scan disabled. Page Scan disabled. hcibuf[3] = 0x00; // Inquiry Scan disabled. Page Scan disabled.
HCI_Command(hcibuf, 4); HCI_Command(hcibuf, 4);
} }
void BTD::hci_read_bdaddr() { void BTD::hci_read_bdaddr() {
hcibuf[0] = 0x09; // HCI OCF = 9 hcibuf[0] = 0x09; // HCI OCF = 9
hcibuf[1] = 0x04 << 2; // HCI OGF = 4 hcibuf[1] = 0x04 << 2; // HCI OGF = 4
hcibuf[2] = 0x00; hcibuf[2] = 0x00;
HCI_Command(hcibuf, 3); HCI_Command(hcibuf, 3);
} }
void BTD::hci_read_local_version_information() { void BTD::hci_read_local_version_information() {
hcibuf[0] = 0x01; // HCI OCF = 1 hcibuf[0] = 0x01; // HCI OCF = 1
hcibuf[1] = 0x04 << 2; // HCI OGF = 4 hcibuf[1] = 0x04 << 2; // HCI OGF = 4
hcibuf[2] = 0x00; hcibuf[2] = 0x00;
HCI_Command(hcibuf, 3); HCI_Command(hcibuf, 3);
} }
void BTD::hci_accept_connection() { void BTD::hci_accept_connection() {
hci_event_flag &= ~HCI_FLAG_CONN_COMPLETE; hci_event_flag &= ~HCI_FLAG_CONN_COMPLETE;
hcibuf[0] = 0x09; // HCI OCF = 9 hcibuf[0] = 0x09; // HCI OCF = 9
@ -836,6 +839,7 @@ void BTD::hci_accept_connection() {
HCI_Command(hcibuf, 10); HCI_Command(hcibuf, 10);
} }
void BTD::hci_remote_name() { void BTD::hci_remote_name() {
hci_event_flag &= ~HCI_FLAG_REMOTE_NAME_COMPLETE; hci_event_flag &= ~HCI_FLAG_REMOTE_NAME_COMPLETE;
hcibuf[0] = 0x19; // HCI OCF = 19 hcibuf[0] = 0x19; // HCI OCF = 19
@ -854,6 +858,7 @@ void BTD::hci_remote_name() {
HCI_Command(hcibuf, 13); HCI_Command(hcibuf, 13);
} }
void BTD::hci_set_local_name(const char* name) { void BTD::hci_set_local_name(const char* name) {
hcibuf[0] = 0x13; // HCI OCF = 13 hcibuf[0] = 0x13; // HCI OCF = 13
hcibuf[1] = 0x03 << 2; // HCI OGF = 3 hcibuf[1] = 0x03 << 2; // HCI OGF = 3
@ -865,6 +870,7 @@ void BTD::hci_set_local_name(const char* name) {
HCI_Command(hcibuf, 4 + strlen(name)); HCI_Command(hcibuf, 4 + strlen(name));
} }
void BTD::hci_inquiry() { void BTD::hci_inquiry() {
hci_event_flag &= ~HCI_FLAG_WII_FOUND; hci_event_flag &= ~HCI_FLAG_WII_FOUND;
hcibuf[0] = 0x01; hcibuf[0] = 0x01;
@ -878,6 +884,7 @@ void BTD::hci_inquiry() {
HCI_Command(hcibuf, 8); HCI_Command(hcibuf, 8);
} }
void BTD::hci_inquiry_cancel() { void BTD::hci_inquiry_cancel() {
hcibuf[0] = 0x02; hcibuf[0] = 0x02;
hcibuf[1] = 0x01 << 2; // HCI OGF = 1 hcibuf[1] = 0x01 << 2; // HCI OGF = 1
@ -885,6 +892,7 @@ void BTD::hci_inquiry_cancel() {
HCI_Command(hcibuf, 3); HCI_Command(hcibuf, 3);
} }
void BTD::hci_connect() { void BTD::hci_connect() {
hci_event_flag &= ~(HCI_FLAG_CONN_COMPLETE | HCI_FLAG_CONNECT_EVENT); hci_event_flag &= ~(HCI_FLAG_CONN_COMPLETE | HCI_FLAG_CONNECT_EVENT);
hcibuf[0] = 0x05; hcibuf[0] = 0x05;
@ -906,6 +914,7 @@ void BTD::hci_connect() {
HCI_Command(hcibuf, 16); HCI_Command(hcibuf, 16);
} }
void BTD::hci_pin_code_request_reply() { void BTD::hci_pin_code_request_reply() {
hcibuf[0] = 0x0D; // HCI OCF = 0D hcibuf[0] = 0x0D; // HCI OCF = 0D
hcibuf[1] = 0x01 << 2; // HCI OGF = 1 hcibuf[1] = 0x01 << 2; // HCI OGF = 1
@ -928,7 +937,7 @@ void BTD::hci_pin_code_request_reply() {
hcibuf[i] = 0x00; // The rest should be 0 hcibuf[i] = 0x00; // The rest should be 0
} else if (pairWithWii && wiiUProController) { } else if (pairWithWii && wiiUProController) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nParing with Wii U Pro Controller")); Notify(PSTR("\r\nParing with Wii U Pro Controller"), 0x80);
#endif #endif
hcibuf[9] = 6; // Pin length is the length of the bt address hcibuf[9] = 6; // Pin length is the length of the bt address
hcibuf[10] = my_bdaddr[0]; // The pin is the Bluetooth dongles bt address backwards hcibuf[10] = my_bdaddr[0]; // The pin is the Bluetooth dongles bt address backwards
@ -950,6 +959,7 @@ void BTD::hci_pin_code_request_reply() {
HCI_Command(hcibuf, 26); HCI_Command(hcibuf, 26);
} }
void BTD::hci_pin_code_negative_request_reply() { void BTD::hci_pin_code_negative_request_reply() {
hcibuf[0] = 0x0E; // HCI OCF = 0E hcibuf[0] = 0x0E; // HCI OCF = 0E
hcibuf[1] = 0x01 << 2; // HCI OGF = 1 hcibuf[1] = 0x01 << 2; // HCI OGF = 1
@ -963,6 +973,7 @@ void BTD::hci_pin_code_negative_request_reply() {
HCI_Command(hcibuf, 9); HCI_Command(hcibuf, 9);
} }
void BTD::hci_link_key_request_negative_reply() { void BTD::hci_link_key_request_negative_reply() {
hcibuf[0] = 0x0C; // HCI OCF = 0C hcibuf[0] = 0x0C; // HCI OCF = 0C
hcibuf[1] = 0x01 << 2; // HCI OGF = 1 hcibuf[1] = 0x01 << 2; // HCI OGF = 1
@ -976,6 +987,7 @@ void BTD::hci_link_key_request_negative_reply() {
HCI_Command(hcibuf, 9); HCI_Command(hcibuf, 9);
} }
void BTD::hci_authentication_request() { void BTD::hci_authentication_request() {
hcibuf[0] = 0x11; // HCI OCF = 11 hcibuf[0] = 0x11; // HCI OCF = 11
hcibuf[1] = 0x01 << 2; // HCI OGF = 1 hcibuf[1] = 0x01 << 2; // HCI OGF = 1
@ -985,6 +997,7 @@ void BTD::hci_authentication_request() {
HCI_Command(hcibuf, 5); HCI_Command(hcibuf, 5);
} }
void BTD::hci_disconnect(uint16_t handle) { // This is called by the different services void BTD::hci_disconnect(uint16_t handle) { // This is called by the different services
hci_event_flag &= ~HCI_FLAG_DISCONN_COMPLETE; hci_event_flag &= ~HCI_FLAG_DISCONN_COMPLETE;
hcibuf[0] = 0x06; // HCI OCF = 6 hcibuf[0] = 0x06; // HCI OCF = 6
@ -996,6 +1009,7 @@ void BTD::hci_disconnect(uint16_t handle) { // This is called by the different s
HCI_Command(hcibuf, 6); HCI_Command(hcibuf, 6);
} }
void BTD::hci_write_class_of_device() { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html void BTD::hci_write_class_of_device() { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html
hcibuf[0] = 0x24; // HCI OCF = 3 hcibuf[0] = 0x24; // HCI OCF = 3
hcibuf[1] = 0x03 << 2; // HCI OGF = 3 hcibuf[1] = 0x03 << 2; // HCI OGF = 3
@ -1029,6 +1043,7 @@ void BTD::hci_write_class_of_device() { // See http://bluetooth-pentest.narod.ru
*/ */
/************************************************************/ /************************************************************/
/* L2CAP Commands */ /* L2CAP Commands */
/************************************************************/ /************************************************************/
void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh) { void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh) {
uint8_t buf[8 + nbytes]; uint8_t buf[8 + nbytes];
@ -1048,15 +1063,16 @@ void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t
if (rcode) { if (rcode) {
delay(100); // This small delay prevents it from overflowing if it fails delay(100); // This small delay prevents it from overflowing if it fails
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nError sending L2CAP message: 0x")); Notify(PSTR("\r\nError sending L2CAP message: 0x"), 0x80);
PrintHex<uint8_t>(rcode); PrintHex<uint8_t > (rcode, 0x80);
Notify(PSTR(" - Channel ID: ")); Notify(PSTR(" - Channel ID: "), 0x80);
Serial.print(channelHigh); Serial.print(channelHigh);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
Serial.print(channelLow); Serial.print(channelLow);
#endif #endif
} }
} }
void BTD::l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm) { void BTD::l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm) {
l2capoutbuf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code l2capoutbuf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code
l2capoutbuf[1] = rxid; // Identifier l2capoutbuf[1] = rxid; // Identifier
@ -1069,6 +1085,7 @@ void BTD::l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid,
L2CAP_Command(handle, l2capoutbuf, 8); L2CAP_Command(handle, l2capoutbuf, 8);
} }
void BTD::l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result) { void BTD::l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result) {
l2capoutbuf[0] = L2CAP_CMD_CONNECTION_RESPONSE; // Code l2capoutbuf[0] = L2CAP_CMD_CONNECTION_RESPONSE; // Code
l2capoutbuf[1] = rxid; // Identifier l2capoutbuf[1] = rxid; // Identifier
@ -1085,6 +1102,7 @@ void BTD::l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid
L2CAP_Command(handle, l2capoutbuf, 12); L2CAP_Command(handle, l2capoutbuf, 12);
} }
void BTD::l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid) { void BTD::l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid) {
l2capoutbuf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code l2capoutbuf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code
l2capoutbuf[1] = rxid; // Identifier l2capoutbuf[1] = rxid; // Identifier
@ -1101,6 +1119,7 @@ void BTD::l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid) {
L2CAP_Command(handle, l2capoutbuf, 12); L2CAP_Command(handle, l2capoutbuf, 12);
} }
void BTD::l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid) { void BTD::l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid) {
l2capoutbuf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code l2capoutbuf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code
l2capoutbuf[1] = rxid; // Identifier l2capoutbuf[1] = rxid; // Identifier
@ -1119,6 +1138,7 @@ void BTD::l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid) {
L2CAP_Command(handle, l2capoutbuf, 14); L2CAP_Command(handle, l2capoutbuf, 14);
} }
void BTD::l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) { void BTD::l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_REQUEST; // Code l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_REQUEST; // Code
l2capoutbuf[1] = rxid; // Identifier l2capoutbuf[1] = rxid; // Identifier
@ -1130,6 +1150,7 @@ void BTD::l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dc
l2capoutbuf[7] = scid[1]; l2capoutbuf[7] = scid[1];
L2CAP_Command(handle, l2capoutbuf, 8); L2CAP_Command(handle, l2capoutbuf, 8);
} }
void BTD::l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) { void BTD::l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_RESPONSE; // Code l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_RESPONSE; // Code
l2capoutbuf[1] = rxid; // Identifier l2capoutbuf[1] = rxid; // Identifier
@ -1141,6 +1162,7 @@ void BTD::l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* d
l2capoutbuf[7] = scid[1]; l2capoutbuf[7] = scid[1];
L2CAP_Command(handle, l2capoutbuf, 8); L2CAP_Command(handle, l2capoutbuf, 8);
} }
void BTD::l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh) { void BTD::l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh) {
l2capoutbuf[0] = L2CAP_CMD_INFORMATION_RESPONSE; // Code l2capoutbuf[0] = L2CAP_CMD_INFORMATION_RESPONSE; // Code
l2capoutbuf[1] = rxid; // Identifier l2capoutbuf[1] = rxid; // Identifier
@ -1169,14 +1191,15 @@ void BTD::setBdaddr(uint8_t* BDADDR) {
//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) //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)
pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL); pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nBluetooth Address was set to: ")); Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80);
for (int8_t i = 5; i > 0; i--) { for (int8_t i = 5; i > 0; i--) {
PrintHex<uint8_t>(my_bdaddr[i]); PrintHex<uint8_t > (my_bdaddr[i], 0x80);
Notify(PSTR(":")); Notify(PSTR(":"), 0x80);
} }
PrintHex<uint8_t>(my_bdaddr[0]); PrintHex<uint8_t > (my_bdaddr[0], 0x80);
#endif #endif
} }
void BTD::setMoveBdaddr(uint8_t* BDADDR) { void BTD::setMoveBdaddr(uint8_t* BDADDR) {
/* Set the internal bluetooth address */ /* Set the internal bluetooth address */
uint8_t buf[11]; uint8_t buf[11];
@ -1192,11 +1215,11 @@ void BTD::setMoveBdaddr(uint8_t* BDADDR) {
//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) //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)
pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL); pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nBluetooth Address was set to: ")); Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80);
for (int8_t i = 5; i > 0; i--) { for (int8_t i = 5; i > 0; i--) {
PrintHex<uint8_t>(my_bdaddr[i]); PrintHex<uint8_t > (my_bdaddr[i], 0x80);
Notify(PSTR(":")); Notify(PSTR(":"), 0x80);
} }
PrintHex<uint8_t>(my_bdaddr[0]); PrintHex<uint8_t > (my_bdaddr[0], 0x80);
#endif #endif
} }

View file

@ -256,9 +256,9 @@ void PS3BT::ACLData(uint8_t* ACLData) {
remote_name[i] = pBtd->remote_name[i]; // Store the remote name for the connection remote_name[i] = pBtd->remote_name[i]; // Store the remote name for the connection
#ifdef DEBUG #ifdef DEBUG
if(pBtd->hci_version < 3) { // Check the HCI Version of the Bluetooth dongle if(pBtd->hci_version < 3) { // Check the HCI Version of the Bluetooth dongle
Notify(PSTR("\r\nYour dongle may not support reading the analog buttons, sensors and status\r\nYour HCI Version is: ")); Notify(PSTR("\r\nYour dongle may not support reading the analog buttons, sensors and status\r\nYour HCI Version is: "), 0x80);
Serial.print(pBtd->hci_version); Serial.print(pBtd->hci_version);
Notify(PSTR("\r\nBut should be at least 3\r\nThis means that it doesn't support Bluetooth Version 2.0+EDR")); Notify(PSTR("\r\nBut should be at least 3\r\nThis means that it doesn't support Bluetooth Version 2.0+EDR"), 0x80);
} }
#endif #endif
} }
@ -270,32 +270,32 @@ void PS3BT::ACLData(uint8_t* ACLData) {
if ((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001) { //l2cap_control - Channel ID for ACL-U if ((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001) { //l2cap_control - Channel ID for ACL-U
if (l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) { if (l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nL2CAP Command Rejected - Reason: ")); Notify(PSTR("\r\nL2CAP Command Rejected - Reason: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[13]); PrintHex<uint8_t>(l2capinbuf[13], 0x80);
Serial.print(" "); Serial.print(" ");
PrintHex<uint8_t>(l2capinbuf[12]); PrintHex<uint8_t>(l2capinbuf[12], 0x80);
Serial.print(" Data: "); Serial.print(" Data: ");
PrintHex<uint8_t>(l2capinbuf[17]); PrintHex<uint8_t>(l2capinbuf[17], 0x80);
Serial.print(" "); Serial.print(" ");
PrintHex<uint8_t>(l2capinbuf[16]); PrintHex<uint8_t>(l2capinbuf[16], 0x80);
Serial.print(" "); Serial.print(" ");
PrintHex<uint8_t>(l2capinbuf[15]); PrintHex<uint8_t>(l2capinbuf[15], 0x80);
Serial.print(" "); Serial.print(" ");
PrintHex<uint8_t>(l2capinbuf[14]); PrintHex<uint8_t>(l2capinbuf[14], 0x80);
#endif #endif
} }
else if (l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) { else if (l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) {
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nL2CAP Connection Request - PSM: ")); Notify(PSTR("\r\nL2CAP Connection Request - PSM: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[13]); PrintHex<uint8_t>(l2capinbuf[13], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[12]); PrintHex<uint8_t>(l2capinbuf[12], 0x80);
Notify(PSTR(" SCID: ")); Notify(PSTR(" SCID: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[15]); PrintHex<uint8_t>(l2capinbuf[15], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[14]); PrintHex<uint8_t>(l2capinbuf[14], 0x80);
Notify(PSTR(" Identifier: ")); Notify(PSTR(" Identifier: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[9]); PrintHex<uint8_t>(l2capinbuf[9], 0x80);
#endif #endif
if ((l2capinbuf[12] | (l2capinbuf[13] << 8)) == HID_CTRL_PSM) { if ((l2capinbuf[12] | (l2capinbuf[13] << 8)) == HID_CTRL_PSM) {
identifier = l2capinbuf[9]; identifier = l2capinbuf[9];
@ -337,7 +337,7 @@ void PS3BT::ACLData(uint8_t* ACLData) {
else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_REQUEST) { else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_REQUEST) {
if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) { if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDisconnect Request: Control Channel")); Notify(PSTR("\r\nDisconnect Request: Control Channel"), 0x80);
#endif #endif
identifier = l2capinbuf[9]; identifier = l2capinbuf[9];
pBtd->l2cap_disconnection_response(hci_handle,identifier,control_dcid,control_scid); pBtd->l2cap_disconnection_response(hci_handle,identifier,control_dcid,control_scid);
@ -345,7 +345,7 @@ void PS3BT::ACLData(uint8_t* ACLData) {
} }
else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) { else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDisconnect Request: Interrupt Channel")); Notify(PSTR("\r\nDisconnect Request: Interrupt Channel"), 0x80);
#endif #endif
identifier = l2capinbuf[9]; identifier = l2capinbuf[9];
pBtd->l2cap_disconnection_response(hci_handle,identifier,interrupt_dcid,interrupt_scid); pBtd->l2cap_disconnection_response(hci_handle,identifier,interrupt_dcid,interrupt_scid);
@ -366,8 +366,8 @@ void PS3BT::ACLData(uint8_t* ACLData) {
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
else { else {
Notify(PSTR("\r\nL2CAP Unknown Signaling Command: ")); Notify(PSTR("\r\nL2CAP Unknown Signaling Command: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[8]); PrintHex<uint8_t>(l2capinbuf[8], 0x80);
} }
#endif #endif
} else if (l2capinbuf[6] == interrupt_dcid[0] && l2capinbuf[7] == interrupt_dcid[1]) { // l2cap_interrupt } else if (l2capinbuf[6] == interrupt_dcid[0] && l2capinbuf[7] == interrupt_dcid[1]) { // l2cap_interrupt
@ -380,8 +380,8 @@ void PS3BT::ACLData(uint8_t* ACLData) {
else if(PS3MoveConnected) else if(PS3MoveConnected)
ButtonState = (uint32_t)(l2capinbuf[10] | ((uint16_t)l2capinbuf[11] << 8) | ((uint32_t)l2capinbuf[12] << 16)); ButtonState = (uint32_t)(l2capinbuf[10] | ((uint16_t)l2capinbuf[11] << 8) | ((uint32_t)l2capinbuf[12] << 16));
//Notify(PSTR("\r\nButtonState"); //Notify(PSTR("\r\nButtonState", 0x80);
//PrintHex<uint32_t>(ButtonState); //PrintHex<uint32_t>(ButtonState, 0x80);
if(ButtonState != OldButtonState) { if(ButtonState != OldButtonState) {
ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
@ -390,7 +390,7 @@ void PS3BT::ACLData(uint8_t* ACLData) {
#ifdef PRINTREPORT // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers #ifdef PRINTREPORT // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers
for(uint8_t i = 10; i < 58;i++) { for(uint8_t i = 10; i < 58;i++) {
PrintHex<uint8_t>(l2capinbuf[i]); PrintHex<uint8_t>(l2capinbuf[i], 0x80);
Serial.print(" "); Serial.print(" ");
} }
Serial.println(); Serial.println();
@ -406,7 +406,7 @@ void PS3BT::L2CAP_task() {
case L2CAP_WAIT: case L2CAP_WAIT:
if (l2cap_connection_request_control_flag) { if (l2cap_connection_request_control_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHID Control Incoming Connection Request")); Notify(PSTR("\r\nHID Control Incoming Connection Request"), 0x80);
#endif #endif
pBtd->l2cap_connection_response(hci_handle,identifier, control_dcid, control_scid, PENDING); pBtd->l2cap_connection_response(hci_handle,identifier, control_dcid, control_scid, PENDING);
delay(1); delay(1);
@ -420,7 +420,7 @@ void PS3BT::L2CAP_task() {
case L2CAP_CONTROL_REQUEST: case L2CAP_CONTROL_REQUEST:
if (l2cap_config_request_control_flag) { if (l2cap_config_request_control_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHID Control Configuration Request")); Notify(PSTR("\r\nHID Control Configuration Request"), 0x80);
#endif #endif
pBtd->l2cap_config_response(hci_handle,identifier, control_scid); pBtd->l2cap_config_response(hci_handle,identifier, control_scid);
l2cap_state = L2CAP_CONTROL_SUCCESS; l2cap_state = L2CAP_CONTROL_SUCCESS;
@ -430,7 +430,7 @@ void PS3BT::L2CAP_task() {
case L2CAP_CONTROL_SUCCESS: case L2CAP_CONTROL_SUCCESS:
if (l2cap_config_success_control_flag) { if (l2cap_config_success_control_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHID Control Successfully Configured")); Notify(PSTR("\r\nHID Control Successfully Configured"), 0x80);
#endif #endif
l2cap_state = L2CAP_INTERRUPT_SETUP; l2cap_state = L2CAP_INTERRUPT_SETUP;
} }
@ -438,7 +438,7 @@ void PS3BT::L2CAP_task() {
case L2CAP_INTERRUPT_SETUP: case L2CAP_INTERRUPT_SETUP:
if (l2cap_connection_request_interrupt_flag) { if (l2cap_connection_request_interrupt_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHID Interrupt Incoming Connection Request")); Notify(PSTR("\r\nHID Interrupt Incoming Connection Request"), 0x80);
#endif #endif
pBtd->l2cap_connection_response(hci_handle,identifier, interrupt_dcid, interrupt_scid, PENDING); pBtd->l2cap_connection_response(hci_handle,identifier, interrupt_dcid, interrupt_scid, PENDING);
delay(1); delay(1);
@ -453,7 +453,7 @@ void PS3BT::L2CAP_task() {
case L2CAP_INTERRUPT_REQUEST: case L2CAP_INTERRUPT_REQUEST:
if (l2cap_config_request_interrupt_flag) { if (l2cap_config_request_interrupt_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHID Interrupt Configuration Request")); Notify(PSTR("\r\nHID Interrupt Configuration Request"), 0x80);
#endif #endif
pBtd->l2cap_config_response(hci_handle,identifier, interrupt_scid); pBtd->l2cap_config_response(hci_handle,identifier, interrupt_scid);
l2cap_state = L2CAP_INTERRUPT_SUCCESS; l2cap_state = L2CAP_INTERRUPT_SUCCESS;
@ -462,7 +462,7 @@ void PS3BT::L2CAP_task() {
case L2CAP_INTERRUPT_SUCCESS: case L2CAP_INTERRUPT_SUCCESS:
if (l2cap_config_success_interrupt_flag) { if (l2cap_config_success_interrupt_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHID Interrupt Successfully Configured")); Notify(PSTR("\r\nHID Interrupt Successfully Configured"), 0x80);
#endif #endif
if(remote_name[0] == 'M') { // First letter in Motion Controller ('M') if(remote_name[0] == 'M') { // First letter in Motion Controller ('M')
for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++) // Reset l2cap in buffer as it sometimes read it as a button has been pressed for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++) // Reset l2cap in buffer as it sometimes read it as a button has been pressed
@ -482,7 +482,7 @@ void PS3BT::L2CAP_task() {
case L2CAP_INTERRUPT_DISCONNECT: case L2CAP_INTERRUPT_DISCONNECT:
if (l2cap_disconnect_response_interrupt_flag) { if (l2cap_disconnect_response_interrupt_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDisconnected Interrupt Channel")); Notify(PSTR("\r\nDisconnected Interrupt Channel"), 0x80);
#endif #endif
identifier++; identifier++;
pBtd->l2cap_disconnection_request(hci_handle, identifier, control_scid, control_dcid); pBtd->l2cap_disconnection_request(hci_handle, identifier, control_scid, control_dcid);
@ -493,7 +493,7 @@ void PS3BT::L2CAP_task() {
case L2CAP_CONTROL_DISCONNECT: case L2CAP_CONTROL_DISCONNECT:
if (l2cap_disconnect_response_control_flag) { if (l2cap_disconnect_response_control_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDisconnected Control Channel")); Notify(PSTR("\r\nDisconnected Control Channel"), 0x80);
#endif #endif
pBtd->hci_disconnect(hci_handle); pBtd->hci_disconnect(hci_handle);
hci_handle = -1; // Reset handle hci_handle = -1; // Reset handle
@ -525,20 +525,20 @@ void PS3BT::Run() {
if (remote_name[0] == 'P') { // First letter in PLAYSTATION(R)3 Controller ('P') if (remote_name[0] == 'P') { // First letter in PLAYSTATION(R)3 Controller ('P')
setLedOn(LED1); setLedOn(LED1);
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDualshock 3 Controller Enabled\r\n")); Notify(PSTR("\r\nDualshock 3 Controller Enabled\r\n"), 0x80);
#endif #endif
PS3Connected = true; PS3Connected = true;
} else if (remote_name[0] == 'N') { // First letter in Navigation Controller ('N') } else if (remote_name[0] == 'N') { // First letter in Navigation Controller ('N')
setLedOn(LED1); // This just turns LED constantly on, on the Navigation controller setLedOn(LED1); // This just turns LED constantly on, on the Navigation controller
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nNavigation Controller Enabled\r\n")); Notify(PSTR("\r\nNavigation Controller Enabled\r\n"), 0x80);
#endif #endif
PS3NavigationConnected = true; PS3NavigationConnected = true;
} else if(remote_name[0] == 'M') { // First letter in Motion Controller ('M') } else if(remote_name[0] == 'M') { // First letter in Motion Controller ('M')
moveSetBulb(Red); moveSetBulb(Red);
timerBulbRumble = millis(); timerBulbRumble = millis();
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nMotion Controller Enabled\r\n")); Notify(PSTR("\r\nMotion Controller Enabled\r\n"), 0x80);
#endif #endif
PS3MoveConnected = true; PS3MoveConnected = true;
} }
@ -648,7 +648,7 @@ void PS3BT::moveSetBulb(Colors color) { //Use this to set the Color using the pr
void PS3BT::moveSetRumble(uint8_t rumble) { void PS3BT::moveSetRumble(uint8_t rumble) {
#ifdef DEBUG #ifdef DEBUG
if(rumble < 64 && rumble != 0) // The rumble value has to at least 64, or approximately 25% (64/255*100) if(rumble < 64 && rumble != 0) // The rumble value has to at least 64, or approximately 25% (64/255*100)
Notify(PSTR("\r\nThe rumble value has to at least 64, or approximately 25%")); Notify(PSTR("\r\nThe rumble value has to at least 64, or approximately 25%"), 0x80);
#endif #endif
//set the rumble value into the write buffer //set the rumble value into the write buffer
HIDMoveBuffer[7] = rumble; HIDMoveBuffer[7] = rumble;

View file

@ -72,12 +72,12 @@ uint8_t PS3USB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
// get memory address of USB device address pool // get memory address of USB device address pool
AddressPool &addrPool = pUsb->GetAddressPool(); AddressPool &addrPool = pUsb->GetAddressPool();
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nPS3USB Init")); Notify(PSTR("\r\nPS3USB Init"), 0x80);
#endif #endif
// check if address has already been assigned to an instance // check if address has already been assigned to an instance
if (bAddress) { if (bAddress) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nAddress in use")); Notify(PSTR("\r\nAddress in use"), 0x80);
#endif #endif
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE; return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
} }
@ -87,14 +87,14 @@ uint8_t PS3USB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
if (!p) { if (!p) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nAddress not found")); Notify(PSTR("\r\nAddress not found"), 0x80);
#endif #endif
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
} }
if (!p->epinfo) { if (!p->epinfo) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nepinfo is null")); Notify(PSTR("\r\nepinfo is null"), 0x80);
#endif #endif
return USB_ERROR_EPINFO_IS_NULL; return USB_ERROR_EPINFO_IS_NULL;
} }
@ -137,14 +137,14 @@ uint8_t PS3USB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
addrPool.FreeAddress(bAddress); addrPool.FreeAddress(bAddress);
bAddress = 0; bAddress = 0;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetAddr: ")); Notify(PSTR("\r\nsetAddr: "), 0x80);
#endif #endif
PrintHex<uint8_t>(rcode); PrintHex<uint8_t>(rcode, 0x80);
return rcode; return rcode;
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nAddr: ")); Notify(PSTR("\r\nAddr: "), 0x80);
PrintHex<uint8_t>(bAddress); PrintHex<uint8_t>(bAddress, 0x80);
#endif #endif
p->lowspeed = false; p->lowspeed = false;
@ -192,12 +192,12 @@ uint8_t PS3USB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
if(PID == PS3_PID || PID == PS3NAVIGATION_PID) { if(PID == PS3_PID || PID == PS3NAVIGATION_PID) {
if(PID == PS3_PID) { if(PID == PS3_PID) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDualshock 3 Controller Connected")); Notify(PSTR("\r\nDualshock 3 Controller Connected"), 0x80);
#endif #endif
PS3Connected = true; PS3Connected = true;
} else { // must be a navigation controller } else { // must be a navigation controller
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nNavigation Controller Connected")); Notify(PSTR("\r\nNavigation Controller Connected"), 0x80);
#endif #endif
PS3NavigationConnected = true; PS3NavigationConnected = true;
} }
@ -215,7 +215,7 @@ uint8_t PS3USB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
} }
else { // must be a Motion controller else { // must be a Motion controller
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nMotion Controller Connected")); Notify(PSTR("\r\nMotion Controller Connected"), 0x80);
#endif #endif
PS3MoveConnected = true; PS3MoveConnected = true;
setMoveBdaddr(my_bdaddr); // Set internal bluetooth address setMoveBdaddr(my_bdaddr); // Set internal bluetooth address
@ -227,38 +227,38 @@ uint8_t PS3USB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
} }
bPollEnable = true; bPollEnable = true;
Notify(PSTR("\r\n")); Notify(PSTR("\r\n"), 0x80);
timer = millis(); timer = millis();
return 0; // successful configuration return 0; // successful configuration
/* diagnostic messages */ /* diagnostic messages */
FailGetDevDescr: FailGetDevDescr:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\ngetDevDescr:")); Notify(PSTR("\r\ngetDevDescr:"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailSetDevTblEntry: FailSetDevTblEntry:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetDevTblEn:")); Notify(PSTR("\r\nsetDevTblEn:"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailSetConf: FailSetConf:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetConf:")); Notify(PSTR("\r\nsetConf:"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailUnknownDevice: FailUnknownDevice:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nUnknown Device Connected - VID: ")); Notify(PSTR("\r\nUnknown Device Connected - VID: "), 0x80);
PrintHex<uint16_t>(VID); PrintHex<uint16_t>(VID, 0x80);
Notify(PSTR(" PID: ")); Notify(PSTR(" PID: "), 0x80);
PrintHex<uint16_t>(PID); PrintHex<uint16_t>(PID, 0x80);
#endif #endif
rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
goto Fail; goto Fail;
Fail: Fail:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nPS3 Init Failed, error code: ")); Notify(PSTR("\r\nPS3 Init Failed, error code: "), 0x80);
Serial.print(rcode,HEX); Serial.print(rcode,HEX);
#endif #endif
Release(); Release();
@ -305,8 +305,8 @@ void PS3USB::readReport() {
ButtonState = (uint32_t)(readBuf[2] | ((uint16_t)readBuf[3] << 8) | ((uint32_t)readBuf[4] << 16)); ButtonState = (uint32_t)(readBuf[2] | ((uint16_t)readBuf[3] << 8) | ((uint32_t)readBuf[4] << 16));
//Notify(PSTR("\r\nButtonState"); //Notify(PSTR("\r\nButtonState", 0x80);
//PrintHex<uint32_t>(ButtonState); //PrintHex<uint32_t>(ButtonState, 0x80);
if(ButtonState != OldButtonState) { if(ButtonState != OldButtonState) {
ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
@ -319,7 +319,7 @@ void PS3USB::printReport() { //Uncomment "#define PRINTREPORT" to print the repo
if (readBuf == NULL) if (readBuf == NULL)
return; return;
for(uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE;i++) { for(uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE;i++) {
PrintHex<uint8_t>(readBuf[i]); PrintHex<uint8_t>(readBuf[i], 0x80);
Serial.print(" "); Serial.print(" ");
} }
Serial.println(); Serial.println();
@ -479,12 +479,12 @@ void PS3USB::setBdaddr(uint8_t* BDADDR) {
//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) //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)
pUsb->ctrlReq(bAddress,epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL); pUsb->ctrlReq(bAddress,epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nBluetooth Address was set to: ")); Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80);
for(int8_t i = 5; i > 0; i--) { for(int8_t i = 5; i > 0; i--) {
PrintHex<uint8_t>(my_bdaddr[i]); PrintHex<uint8_t>(my_bdaddr[i], 0x80);
Serial.print(":"); Serial.print(":");
} }
PrintHex<uint8_t>(my_bdaddr[0]); PrintHex<uint8_t>(my_bdaddr[0], 0x80);
#endif #endif
return; return;
} }
@ -518,7 +518,7 @@ void PS3USB::moveSetBulb(Colors color) { //Use this to set the Color using the p
void PS3USB::moveSetRumble(uint8_t rumble) { void PS3USB::moveSetRumble(uint8_t rumble) {
#ifdef DEBUG #ifdef DEBUG
if(rumble < 64 && rumble != 0) // The rumble value has to at least 64, or approximately 25% (64/255*100) if(rumble < 64 && rumble != 0) // The rumble value has to at least 64, or approximately 25% (64/255*100)
Notify(PSTR("\r\nThe rumble value has to at least 64, or approximately 25%")); Notify(PSTR("\r\nThe rumble value has to at least 64, or approximately 25%"), 0x80);
#endif #endif
//set the rumble value into the write buffer //set the rumble value into the write buffer
writeBuf[6] = rumble; writeBuf[6] = rumble;
@ -540,12 +540,12 @@ void PS3USB::setMoveBdaddr(uint8_t* BDADDR) {
//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) //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)
pUsb->ctrlReq(bAddress,epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00,11,11, buf, NULL); pUsb->ctrlReq(bAddress,epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00,11,11, buf, NULL);
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nBluetooth Address was set to: ")); Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80);
for(int8_t i = 5; i > 0; i--) { for(int8_t i = 5; i > 0; i--) {
PrintHex<uint8_t>(my_bdaddr[i]); PrintHex<uint8_t>(my_bdaddr[i], 0x80);
Serial.print(":"); Serial.print(":");
} }
PrintHex<uint8_t>(my_bdaddr[0]); PrintHex<uint8_t>(my_bdaddr[0], 0x80);
#endif #endif
return; return;
} }

138
SPP.cpp
View file

@ -96,31 +96,31 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
if ((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001) { //l2cap_control - Channel ID for ACL-U if ((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001) { //l2cap_control - Channel ID for ACL-U
if (l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) { if (l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nL2CAP Command Rejected - Reason: ")); Notify(PSTR("\r\nL2CAP Command Rejected - Reason: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[13]); PrintHex<uint8_t>(l2capinbuf[13], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[12]); PrintHex<uint8_t>(l2capinbuf[12], 0x80);
Notify(PSTR(" Data: ")); Notify(PSTR(" Data: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[17]); PrintHex<uint8_t>(l2capinbuf[17], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[16]); PrintHex<uint8_t>(l2capinbuf[16], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[15]); PrintHex<uint8_t>(l2capinbuf[15], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[14]); PrintHex<uint8_t>(l2capinbuf[14], 0x80);
#endif #endif
} else if (l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) { } else if (l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) {
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nL2CAP Connection Request - PSM: ")); Notify(PSTR("\r\nL2CAP Connection Request - PSM: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[13]); PrintHex<uint8_t>(l2capinbuf[13], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[12]); PrintHex<uint8_t>(l2capinbuf[12], 0x80);
Notify(PSTR(" SCID: ")); Notify(PSTR(" SCID: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[15]); PrintHex<uint8_t>(l2capinbuf[15], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[14]); PrintHex<uint8_t>(l2capinbuf[14], 0x80);
Notify(PSTR(" Identifier: ")); Notify(PSTR(" Identifier: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[9]); PrintHex<uint8_t>(l2capinbuf[9], 0x80);
#endif #endif
if ((l2capinbuf[12] | (l2capinbuf[13] << 8)) == SDP_PSM) { // It doesn't matter if it receives another reqeust, since it waits for the channel to disconnect in the L2CAP_SDP_DONE state, and the l2cap_event_flag will be cleared if so if ((l2capinbuf[12] | (l2capinbuf[13] << 8)) == SDP_PSM) { // It doesn't matter if it receives another reqeust, since it waits for the channel to disconnect in the L2CAP_SDP_DONE state, and the l2cap_event_flag will be cleared if so
identifier = l2capinbuf[9]; identifier = l2capinbuf[9];
@ -157,11 +157,11 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
} }
} else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_REQUEST) { } else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_REQUEST) {
if (l2capinbuf[12] == sdp_dcid[0] && l2capinbuf[13] == sdp_dcid[1]) { if (l2capinbuf[12] == sdp_dcid[0] && l2capinbuf[13] == sdp_dcid[1]) {
//Notify(PSTR("\r\nDisconnect Request: SDP Channel")); //Notify(PSTR("\r\nDisconnect Request: SDP Channel"), 0x80);
identifier = l2capinbuf[9]; identifier = l2capinbuf[9];
l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_SDP_REQUEST; l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_SDP_REQUEST;
} else if (l2capinbuf[12] == rfcomm_dcid[0] && l2capinbuf[13] == rfcomm_dcid[1]) { } else if (l2capinbuf[12] == rfcomm_dcid[0] && l2capinbuf[13] == rfcomm_dcid[1]) {
//Notify(PSTR("\r\nDisconnect Request: RFCOMM Channel")); //Notify(PSTR("\r\nDisconnect Request: RFCOMM Channel"), 0x80);
identifier = l2capinbuf[9]; identifier = l2capinbuf[9];
l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST; l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST;
} }
@ -177,15 +177,15 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
} }
} else if (l2capinbuf[8] == L2CAP_CMD_INFORMATION_REQUEST) { } else if (l2capinbuf[8] == L2CAP_CMD_INFORMATION_REQUEST) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nInformation request")); Notify(PSTR("\r\nInformation request"), 0x80);
#endif #endif
identifier = l2capinbuf[9]; identifier = l2capinbuf[9];
pBtd->l2cap_information_response(hci_handle,identifier,l2capinbuf[12],l2capinbuf[13]); pBtd->l2cap_information_response(hci_handle,identifier,l2capinbuf[12],l2capinbuf[13]);
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
else { else {
Notify(PSTR("\r\nL2CAP Unknown Signaling Command: ")); Notify(PSTR("\r\nL2CAP Unknown Signaling Command: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[8]); PrintHex<uint8_t>(l2capinbuf[8], 0x80);
} }
#endif #endif
} else if (l2capinbuf[6] == sdp_dcid[0] && l2capinbuf[7] == sdp_dcid[1]) { // SDP } else if (l2capinbuf[6] == sdp_dcid[0] && l2capinbuf[7] == sdp_dcid[1]) { // SDP
@ -226,20 +226,20 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
rfcommChannelConnection = rfcommChannel; rfcommChannelConnection = rfcommChannel;
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nRFCOMM Channel: ")); Notify(PSTR("\r\nRFCOMM Channel: "), 0x80);
Serial.print(rfcommChannel>>3,HEX); Serial.print(rfcommChannel>>3,HEX);
Notify(PSTR(" Direction: ")); Notify(PSTR(" Direction: "), 0x80);
Serial.print(rfcommDirection>>2,HEX); Serial.print(rfcommDirection>>2,HEX);
Notify(PSTR(" CommandResponse: ")); Notify(PSTR(" CommandResponse: "), 0x80);
Serial.print(rfcommCommandResponse>>1,HEX); Serial.print(rfcommCommandResponse>>1,HEX);
Notify(PSTR(" ChannelType: ")); Notify(PSTR(" ChannelType: "), 0x80);
Serial.print(rfcommChannelType,HEX); Serial.print(rfcommChannelType,HEX);
Notify(PSTR(" PF_BIT: ")); Notify(PSTR(" PF_BIT: "), 0x80);
Serial.print(rfcommPfBit,HEX); Serial.print(rfcommPfBit,HEX);
#endif #endif
if (rfcommChannelType == RFCOMM_DISC) { if (rfcommChannelType == RFCOMM_DISC) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nReceived Disconnect RFCOMM Command on channel: ")); Notify(PSTR("\r\nReceived Disconnect RFCOMM Command on channel: "), 0x80);
Serial.print(rfcommChannel>>3,HEX); Serial.print(rfcommChannel>>3,HEX);
#endif #endif
connected = false; connected = false;
@ -256,10 +256,10 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
rfcommAvailable += length; rfcommAvailable += length;
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nRFCOMM Data Available: ")); Notify(PSTR("\r\nRFCOMM Data Available: "), 0x80);
Serial.print(rfcommAvailable); Serial.print(rfcommAvailable);
if (offset) { if (offset) {
Notify(PSTR(" - Credit: 0x")); Notify(PSTR(" - Credit: 0x"), 0x80);
Serial.print(l2capinbuf[11],HEX); Serial.print(l2capinbuf[11],HEX);
} }
#endif #endif
@ -269,7 +269,7 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
#endif #endif
} else if (rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_RPN_CMD) { // UIH Remote Port Negotiation Command } else if (rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_RPN_CMD) { // UIH Remote Port Negotiation Command
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nReceived UIH Remote Port Negotiation Command")); Notify(PSTR("\r\nReceived UIH Remote Port Negotiation Command"), 0x80);
#endif #endif
rfcommbuf[0] = BT_RFCOMM_RPN_RSP; // Command rfcommbuf[0] = BT_RFCOMM_RPN_RSP; // Command
rfcommbuf[1] = l2capinbuf[12]; // Length and shiftet like so: length << 1 | 1 rfcommbuf[1] = l2capinbuf[12]; // Length and shiftet like so: length << 1 | 1
@ -284,7 +284,7 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
sendRfcomm(rfcommChannel,rfcommDirection,0,RFCOMM_UIH,rfcommPfBit,rfcommbuf,0x0A); // UIH Remote Port Negotiation Response sendRfcomm(rfcommChannel,rfcommDirection,0,RFCOMM_UIH,rfcommPfBit,rfcommbuf,0x0A); // UIH Remote Port Negotiation Response
} else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_MSC_CMD) { // UIH Modem Status Command } else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_MSC_CMD) { // UIH Modem Status Command
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSend UIH Modem Status Response")); Notify(PSTR("\r\nSend UIH Modem Status Response"), 0x80);
#endif #endif
rfcommbuf[0] = BT_RFCOMM_MSC_RSP; // UIH Modem Status Response rfcommbuf[0] = BT_RFCOMM_MSC_RSP; // UIH Modem Status Response
rfcommbuf[1] = 2 << 1 | 1; // Length and shiftet like so: length << 1 | 1 rfcommbuf[1] = 2 << 1 | 1; // Length and shiftet like so: length << 1 | 1
@ -295,12 +295,12 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
} else { } else {
if(rfcommChannelType == RFCOMM_SABM) { // SABM Command - this is sent twice: once for channel 0 and then for the channel to establish if(rfcommChannelType == RFCOMM_SABM) { // SABM Command - this is sent twice: once for channel 0 and then for the channel to establish
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nReceived SABM Command")); Notify(PSTR("\r\nReceived SABM Command"), 0x80);
#endif #endif
sendRfcomm(rfcommChannel,rfcommDirection,rfcommCommandResponse,RFCOMM_UA,rfcommPfBit,rfcommbuf,0x00); // UA Command sendRfcomm(rfcommChannel,rfcommDirection,rfcommCommandResponse,RFCOMM_UA,rfcommPfBit,rfcommbuf,0x00); // UA Command
} else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_PN_CMD) { // UIH Parameter Negotiation Command } else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_PN_CMD) { // UIH Parameter Negotiation Command
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nReceived UIH Parameter Negotiation Command")); Notify(PSTR("\r\nReceived UIH Parameter Negotiation Command"), 0x80);
#endif #endif
rfcommbuf[0] = BT_RFCOMM_PN_RSP; // UIH Parameter Negotiation Response rfcommbuf[0] = BT_RFCOMM_PN_RSP; // UIH Parameter Negotiation Response
rfcommbuf[1] = l2capinbuf[12]; // Length and shiftet like so: length << 1 | 1 rfcommbuf[1] = l2capinbuf[12]; // Length and shiftet like so: length << 1 | 1
@ -315,7 +315,7 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
sendRfcomm(rfcommChannel,rfcommDirection,0,RFCOMM_UIH,rfcommPfBit,rfcommbuf,0x0A); sendRfcomm(rfcommChannel,rfcommDirection,0,RFCOMM_UIH,rfcommPfBit,rfcommbuf,0x0A);
} else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_MSC_CMD) { // UIH Modem Status Command } else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_MSC_CMD) { // UIH Modem Status Command
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSend UIH Modem Status Response")); Notify(PSTR("\r\nSend UIH Modem Status Response"), 0x80);
#endif #endif
rfcommbuf[0] = BT_RFCOMM_MSC_RSP; // UIH Modem Status Response rfcommbuf[0] = BT_RFCOMM_MSC_RSP; // UIH Modem Status Response
rfcommbuf[1] = 2 << 1 | 1; // Length and shiftet like so: length << 1 | 1 rfcommbuf[1] = 2 << 1 | 1; // Length and shiftet like so: length << 1 | 1
@ -325,7 +325,7 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
delay(1); delay(1);
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSend UIH Modem Status Command")); Notify(PSTR("\r\nSend UIH Modem Status Command"), 0x80);
#endif #endif
rfcommbuf[0] = BT_RFCOMM_MSC_CMD; // UIH Modem Status Command rfcommbuf[0] = BT_RFCOMM_MSC_CMD; // UIH Modem Status Command
rfcommbuf[1] = 2 << 1 | 1; // Length and shiftet like so: length << 1 | 1 rfcommbuf[1] = 2 << 1 | 1; // Length and shiftet like so: length << 1 | 1
@ -336,7 +336,7 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
} else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_MSC_RSP) { // UIH Modem Status Response } else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_MSC_RSP) { // UIH Modem Status Response
if(!creditSent) { if(!creditSent) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSend UIH Command with credit")); Notify(PSTR("\r\nSend UIH Command with credit"), 0x80);
#endif #endif
sendRfcommCredit(rfcommChannelConnection,rfcommDirection,0,RFCOMM_UIH,0x10,sizeof(rfcommDataBuffer)); // Send credit sendRfcommCredit(rfcommChannelConnection,rfcommDirection,0,RFCOMM_UIH,0x10,sizeof(rfcommDataBuffer)); // Send credit
creditSent = true; creditSent = true;
@ -345,11 +345,11 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
} }
} else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[10] == 0x01) { // UIH Command with credit } else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[10] == 0x01) { // UIH Command with credit
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nReceived UIH Command with credit")); Notify(PSTR("\r\nReceived UIH Command with credit"), 0x80);
#endif #endif
} else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_RPN_CMD) { // UIH Remote Port Negotiation Command } else if(rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_RPN_CMD) { // UIH Remote Port Negotiation Command
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nReceived UIH Remote Port Negotiation Command")); Notify(PSTR("\r\nReceived UIH Remote Port Negotiation Command"), 0x80);
#endif #endif
rfcommbuf[0] = BT_RFCOMM_RPN_RSP; // Command rfcommbuf[0] = BT_RFCOMM_RPN_RSP; // Command
rfcommbuf[1] = l2capinbuf[12]; // Length and shiftet like so: length << 1 | 1 rfcommbuf[1] = l2capinbuf[12]; // Length and shiftet like so: length << 1 | 1
@ -363,7 +363,7 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
rfcommbuf[9] = l2capinbuf[20]; // Number of Frames rfcommbuf[9] = l2capinbuf[20]; // Number of Frames
sendRfcomm(rfcommChannel,rfcommDirection,0,RFCOMM_UIH,rfcommPfBit,rfcommbuf,0x0A); // UIH Remote Port Negotiation Response sendRfcomm(rfcommChannel,rfcommDirection,0,RFCOMM_UIH,rfcommPfBit,rfcommbuf,0x0A); // UIH Remote Port Negotiation Response
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nRFCOMM Connection is now established\r\n")); Notify(PSTR("\r\nRFCOMM Connection is now established\r\n"), 0x80);
#endif #endif
waitForLastCommand = false; waitForLastCommand = false;
creditSent = false; creditSent = false;
@ -371,20 +371,20 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
} }
#ifdef DEBUG #ifdef DEBUG
else if(rfcommChannelType != RFCOMM_DISC) { else if(rfcommChannelType != RFCOMM_DISC) {
Notify(PSTR("\r\nUnsupported RFCOMM Data - ChannelType: ")); Notify(PSTR("\r\nUnsupported RFCOMM Data - ChannelType: "), 0x80);
PrintHex<uint8_t>(rfcommChannelType); PrintHex<uint8_t>(rfcommChannelType, 0x80);
Notify(PSTR(" Command: ")); Notify(PSTR(" Command: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[11]); PrintHex<uint8_t>(l2capinbuf[11], 0x80);
} }
#endif #endif
} }
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
else { else {
Notify(PSTR("\r\nUnsupported L2CAP Data - Channel ID: ")); Notify(PSTR("\r\nUnsupported L2CAP Data - Channel ID: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[7]); PrintHex<uint8_t>(l2capinbuf[7], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[6]); PrintHex<uint8_t>(l2capinbuf[6], 0x80);
} }
#endif #endif
SDP_task(); SDP_task();
@ -394,7 +394,7 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
void SPP::Run() { void SPP::Run() {
if(waitForLastCommand && (millis() - timer) > 100) { // We will only wait 100ms and see if the UIH Remote Port Negotiation Command is send, as some deviced don't send it if(waitForLastCommand && (millis() - timer) > 100) { // We will only wait 100ms and see if the UIH Remote Port Negotiation Command is send, as some deviced don't send it
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nRFCOMM Connection is now established - Automatic\r\n")); Notify(PSTR("\r\nRFCOMM Connection is now established - Automatic\r\n"), 0x80);
#endif #endif
creditSent = false; creditSent = false;
waitForLastCommand = false; waitForLastCommand = false;
@ -408,7 +408,7 @@ void SPP::SDP_task() {
if (l2cap_connection_request_sdp_flag) { if (l2cap_connection_request_sdp_flag) {
l2cap_event_flag &= ~L2CAP_FLAG_CONNECTION_SDP_REQUEST; // Clear flag l2cap_event_flag &= ~L2CAP_FLAG_CONNECTION_SDP_REQUEST; // Clear flag
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSDP Incoming Connection Request")); Notify(PSTR("\r\nSDP Incoming Connection Request"), 0x80);
#endif #endif
pBtd->l2cap_connection_response(hci_handle,identifier, sdp_dcid, sdp_scid, PENDING); pBtd->l2cap_connection_response(hci_handle,identifier, sdp_dcid, sdp_scid, PENDING);
delay(1); delay(1);
@ -423,7 +423,7 @@ void SPP::SDP_task() {
if (l2cap_config_request_sdp_flag) { if (l2cap_config_request_sdp_flag) {
l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_SDP_REQUEST; // Clear flag l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_SDP_REQUEST; // Clear flag
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSDP Configuration Request")); Notify(PSTR("\r\nSDP Configuration Request"), 0x80);
#endif #endif
pBtd->l2cap_config_response(hci_handle,identifier, sdp_scid); pBtd->l2cap_config_response(hci_handle,identifier, sdp_scid);
l2cap_sdp_state = L2CAP_SDP_SUCCESS; l2cap_sdp_state = L2CAP_SDP_SUCCESS;
@ -433,7 +433,7 @@ void SPP::SDP_task() {
if (l2cap_config_success_sdp_flag) { if (l2cap_config_success_sdp_flag) {
l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_SDP_SUCCESS; // Clear flag l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_SDP_SUCCESS; // Clear flag
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSDP Successfully Configured")); Notify(PSTR("\r\nSDP Successfully Configured"), 0x80);
#endif #endif
firstMessage = true; // Reset bool firstMessage = true; // Reset bool
SDPConnected = true; SDPConnected = true;
@ -445,7 +445,7 @@ void SPP::SDP_task() {
l2cap_event_flag &= ~L2CAP_FLAG_DISCONNECT_SDP_REQUEST; // Clear flag l2cap_event_flag &= ~L2CAP_FLAG_DISCONNECT_SDP_REQUEST; // Clear flag
SDPConnected = false; SDPConnected = false;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDisconnected SDP Channel")); Notify(PSTR("\r\nDisconnected SDP Channel"), 0x80);
#endif #endif
pBtd->l2cap_disconnection_response(hci_handle,identifier,sdp_dcid,sdp_scid); pBtd->l2cap_disconnection_response(hci_handle,identifier,sdp_dcid,sdp_scid);
l2cap_sdp_state = L2CAP_SDP_WAIT; l2cap_sdp_state = L2CAP_SDP_WAIT;
@ -455,7 +455,7 @@ void SPP::SDP_task() {
case L2CAP_DISCONNECT_RESPONSE: // This is for both disconnection response from the RFCOMM and SDP channel if they were connected case L2CAP_DISCONNECT_RESPONSE: // This is for both disconnection response from the RFCOMM and SDP channel if they were connected
if (l2cap_disconnect_response_flag) { if (l2cap_disconnect_response_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDisconnected L2CAP Connection")); Notify(PSTR("\r\nDisconnected L2CAP Connection"), 0x80);
#endif #endif
RFCOMMConnected = false; RFCOMMConnected = false;
SDPConnected = false; SDPConnected = false;
@ -476,7 +476,7 @@ void SPP::RFCOMM_task()
if(l2cap_connection_request_rfcomm_flag) { if(l2cap_connection_request_rfcomm_flag) {
l2cap_event_flag &= ~L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST; // Clear flag l2cap_event_flag &= ~L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST; // Clear flag
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nRFCOMM Incoming Connection Request")); Notify(PSTR("\r\nRFCOMM Incoming Connection Request"), 0x80);
#endif #endif
pBtd->l2cap_connection_response(hci_handle,identifier, rfcomm_dcid, rfcomm_scid, PENDING); pBtd->l2cap_connection_response(hci_handle,identifier, rfcomm_dcid, rfcomm_scid, PENDING);
delay(1); delay(1);
@ -491,7 +491,7 @@ void SPP::RFCOMM_task()
if (l2cap_config_request_rfcomm_flag) { if (l2cap_config_request_rfcomm_flag) {
l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_RFCOMM_REQUEST; // Clear flag l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_RFCOMM_REQUEST; // Clear flag
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nRFCOMM Configuration Request")); Notify(PSTR("\r\nRFCOMM Configuration Request"), 0x80);
#endif #endif
pBtd->l2cap_config_response(hci_handle,identifier, rfcomm_scid); pBtd->l2cap_config_response(hci_handle,identifier, rfcomm_scid);
l2cap_rfcomm_state = L2CAP_RFCOMM_SUCCESS; l2cap_rfcomm_state = L2CAP_RFCOMM_SUCCESS;
@ -501,7 +501,7 @@ void SPP::RFCOMM_task()
if (l2cap_config_success_rfcomm_flag) { if (l2cap_config_success_rfcomm_flag) {
l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS; // Clear flag l2cap_event_flag &= ~L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS; // Clear flag
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nRFCOMM Successfully Configured")); Notify(PSTR("\r\nRFCOMM Successfully Configured"), 0x80);
#endif #endif
rfcommAvailable = 0; // Reset number of bytes available rfcommAvailable = 0; // Reset number of bytes available
bytesRead = 0; // Reset number of bytes received bytesRead = 0; // Reset number of bytes received
@ -515,7 +515,7 @@ void SPP::RFCOMM_task()
RFCOMMConnected = false; RFCOMMConnected = false;
connected = false; connected = false;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDisconnected RFCOMM Channel")); Notify(PSTR("\r\nDisconnected RFCOMM Channel"), 0x80);
#endif #endif
pBtd->l2cap_disconnection_response(hci_handle,identifier,rfcomm_dcid,rfcomm_scid); pBtd->l2cap_disconnection_response(hci_handle,identifier,rfcomm_dcid,rfcomm_scid);
l2cap_rfcomm_state = L2CAP_RFCOMM_WAIT; l2cap_rfcomm_state = L2CAP_RFCOMM_WAIT;
@ -666,10 +666,10 @@ void SPP::sendRfcomm(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t cha
l2capoutbuf[i+3] = data[i]; l2capoutbuf[i+3] = data[i];
l2capoutbuf[i+3] = calcFcs(l2capoutbuf); l2capoutbuf[i+3] = calcFcs(l2capoutbuf);
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR(" - RFCOMM Data: ")); Notify(PSTR(" - RFCOMM Data: "), 0x80);
for(i = 0; i < length+4; i++) { for(i = 0; i < length+4; i++) {
Serial.print(l2capoutbuf[i],HEX); Serial.print(l2capoutbuf[i],HEX);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
} }
#endif #endif
RFCOMM_Command(l2capoutbuf,length+4); RFCOMM_Command(l2capoutbuf,length+4);
@ -682,10 +682,10 @@ void SPP::sendRfcommCredit(uint8_t channel, uint8_t direction, uint8_t CR, uint8
l2capoutbuf[3] = credit; // Credit l2capoutbuf[3] = credit; // Credit
l2capoutbuf[4] = calcFcs(l2capoutbuf); l2capoutbuf[4] = calcFcs(l2capoutbuf);
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR(" - RFCOMM Credit Data: ")); Notify(PSTR(" - RFCOMM Credit Data: "), 0x80);
for(uint8_t i = 0; i < 5; i++) { for(uint8_t i = 0; i < 5; i++) {
Serial.print(l2capoutbuf[i],HEX); Serial.print(l2capoutbuf[i],HEX);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
} }
#endif #endif
RFCOMM_Command(l2capoutbuf,5); RFCOMM_Command(l2capoutbuf,5);
@ -902,9 +902,9 @@ uint8_t SPP::read() {
bytesRead = 0; bytesRead = 0;
sendRfcommCredit(rfcommChannelConnection,rfcommDirection,0,RFCOMM_UIH,0x10,sizeof(rfcommDataBuffer)); // Send more credit sendRfcommCredit(rfcommChannelConnection,rfcommDirection,0,RFCOMM_UIH,0x10,sizeof(rfcommDataBuffer)); // Send more credit
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nSent ")); Notify(PSTR("\r\nSent "), 0x80);
Serial.print(sizeof(rfcommDataBuffer)); Serial.print(sizeof(rfcommDataBuffer));
Notify(PSTR(" more credit")); Notify(PSTR(" more credit"), 0x80);
#endif #endif
} }
return output; return output;

174
Wii.cpp
View file

@ -118,7 +118,7 @@ void WII::Reset() {
void WII::disconnect() { // Use this void to disconnect any of the controllers void WII::disconnect() { // Use this void to disconnect any of the controllers
if(motionPlusConnected && !pBtd->motionPlusInside) { // Disable the Motion Plus extension if(motionPlusConnected && !pBtd->motionPlusInside) { // Disable the Motion Plus extension
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDeactivating Motion Plus")); Notify(PSTR("\r\nDeactivating Motion Plus"), 0x80);
#endif #endif
initExtension1(); // This will disable the Motion Plus extension initExtension1(); // This will disable the Motion Plus extension
} }
@ -144,18 +144,18 @@ void WII::ACLData(uint8_t* l2capinbuf) {
if ((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001) { //l2cap_control - Channel ID for ACL-U if ((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001) { //l2cap_control - Channel ID for ACL-U
if (l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) { if (l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nL2CAP Command Rejected - Reason: ")); Notify(PSTR("\r\nL2CAP Command Rejected - Reason: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[13]); PrintHex<uint8_t>(l2capinbuf[13], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[12]); PrintHex<uint8_t>(l2capinbuf[12], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[17]); PrintHex<uint8_t>(l2capinbuf[17], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[16]); PrintHex<uint8_t>(l2capinbuf[16], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[15]); PrintHex<uint8_t>(l2capinbuf[15], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[14]); PrintHex<uint8_t>(l2capinbuf[14], 0x80);
#endif #endif
} }
else if (l2capinbuf[8] == L2CAP_CMD_CONNECTION_RESPONSE) { else if (l2capinbuf[8] == L2CAP_CMD_CONNECTION_RESPONSE) {
@ -178,16 +178,16 @@ void WII::ACLData(uint8_t* l2capinbuf) {
} }
else if (l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) { else if (l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) {
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nL2CAP Connection Request - PSM: ")); Notify(PSTR("\r\nL2CAP Connection Request - PSM: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[13]); PrintHex<uint8_t>(l2capinbuf[13], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[12]); PrintHex<uint8_t>(l2capinbuf[12], 0x80);
Notify(PSTR(" SCID: ")); Notify(PSTR(" SCID: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[15]); PrintHex<uint8_t>(l2capinbuf[15], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
PrintHex<uint8_t>(l2capinbuf[14]); PrintHex<uint8_t>(l2capinbuf[14], 0x80);
Notify(PSTR(" Identifier: ")); Notify(PSTR(" Identifier: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[9]); PrintHex<uint8_t>(l2capinbuf[9], 0x80);
#endif #endif
if ((l2capinbuf[12] | (l2capinbuf[13] << 8)) == HID_CTRL_PSM) { if ((l2capinbuf[12] | (l2capinbuf[13] << 8)) == HID_CTRL_PSM) {
identifier = l2capinbuf[9]; identifier = l2capinbuf[9];
@ -229,7 +229,7 @@ void WII::ACLData(uint8_t* l2capinbuf) {
else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_REQUEST) { else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_REQUEST) {
if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) { if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDisconnect Request: Control Channel")); Notify(PSTR("\r\nDisconnect Request: Control Channel"), 0x80);
#endif #endif
identifier = l2capinbuf[9]; identifier = l2capinbuf[9];
pBtd->l2cap_disconnection_response(hci_handle,identifier,control_dcid,control_scid); pBtd->l2cap_disconnection_response(hci_handle,identifier,control_dcid,control_scid);
@ -237,7 +237,7 @@ void WII::ACLData(uint8_t* l2capinbuf) {
} }
else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) { else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDisconnect Request: Interrupt Channel")); Notify(PSTR("\r\nDisconnect Request: Interrupt Channel"), 0x80);
#endif #endif
identifier = l2capinbuf[9]; identifier = l2capinbuf[9];
pBtd->l2cap_disconnection_response(hci_handle,identifier,interrupt_dcid,interrupt_scid); pBtd->l2cap_disconnection_response(hci_handle,identifier,interrupt_dcid,interrupt_scid);
@ -259,8 +259,8 @@ void WII::ACLData(uint8_t* l2capinbuf) {
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
else { else {
identifier = l2capinbuf[9]; identifier = l2capinbuf[9];
Notify(PSTR("\r\nL2CAP Unknown Signaling Command: ")); Notify(PSTR("\r\nL2CAP Unknown Signaling Command: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[8]); PrintHex<uint8_t>(l2capinbuf[8], 0x80);
} }
#endif #endif
} else if (l2capinbuf[6] == interrupt_dcid[0] && l2capinbuf[7] == interrupt_dcid[1]) { // l2cap_interrupt } else if (l2capinbuf[6] == interrupt_dcid[0] && l2capinbuf[7] == interrupt_dcid[1]) { // l2cap_interrupt
@ -285,9 +285,9 @@ void WII::ACLData(uint8_t* l2capinbuf) {
else if(!unknownExtensionConnected) else if(!unknownExtensionConnected)
ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8)); ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8));
#ifdef PRINTREPORT #ifdef PRINTREPORT
Notify(PSTR("ButtonState: ")); Notify(PSTR("ButtonState: "), 0x80);
PrintHex<uint32_t>(ButtonState); PrintHex<uint32_t>(ButtonState, 0x80);
Notify(PSTR("\r\n")); Notify(PSTR("\r\n"), 0x80);
#endif #endif
if(ButtonState != OldButtonState) { if(ButtonState != OldButtonState) {
ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
@ -307,13 +307,13 @@ void WII::ACLData(uint8_t* l2capinbuf) {
batteryLevel = l2capinbuf[15]; // Update battery level batteryLevel = l2capinbuf[15]; // Update battery level
if(l2capinbuf[12] & 0x01) { if(l2capinbuf[12] & 0x01) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nWARNING: Battery is nearly empty")); Notify(PSTR("\r\nWARNING: Battery is nearly empty"), 0x80);
#endif #endif
} }
if(l2capinbuf[12] & 0x02) { // Check if a extension is connected if(l2capinbuf[12] & 0x02) { // Check if a extension is connected
#ifdef DEBUG #ifdef DEBUG
if(!unknownExtensionConnected) if(!unknownExtensionConnected)
Notify(PSTR("\r\nExtension connected")); Notify(PSTR("\r\nExtension connected"), 0x80);
#endif #endif
unknownExtensionConnected = true; unknownExtensionConnected = true;
#ifdef WIICAMERA #ifdef WIICAMERA
@ -323,11 +323,11 @@ void WII::ACLData(uint8_t* l2capinbuf) {
} }
else { else {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nExtension disconnected")); Notify(PSTR("\r\nExtension disconnected"), 0x80);
#endif #endif
if(motionPlusConnected) { if(motionPlusConnected) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR(" - from Motion Plus")); Notify(PSTR(" - from Motion Plus"), 0x80);
#endif #endif
l2cap_event_flag &= ~WII_FLAG_NUNCHUCK_CONNECTED; l2cap_event_flag &= ~WII_FLAG_NUNCHUCK_CONNECTED;
if(!activateNunchuck) // If it's already trying to initialize the Nunchuck don't set it to false if(!activateNunchuck) // If it's already trying to initialize the Nunchuck don't set it to false
@ -336,7 +336,7 @@ void WII::ACLData(uint8_t* l2capinbuf) {
} }
else if(nunchuckConnected) { else if(nunchuckConnected) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR(" - Nunchuck")); Notify(PSTR(" - Nunchuck"), 0x80);
#endif #endif
nunchuckConnected = false; // It must be the Nunchuck controller then nunchuckConnected = false; // It must be the Nunchuck controller then
l2cap_event_flag &= ~WII_FLAG_NUNCHUCK_CONNECTED; l2cap_event_flag &= ~WII_FLAG_NUNCHUCK_CONNECTED;
@ -352,64 +352,64 @@ void WII::ACLData(uint8_t* l2capinbuf) {
// See: http://wiibrew.org/wiki/Wiimote/Extension_Controllers // See: http://wiibrew.org/wiki/Wiimote/Extension_Controllers
if(l2capinbuf[16] == 0x00 && l2capinbuf[17] == 0xA4 && l2capinbuf[18] == 0x20 && l2capinbuf[19] == 0x00 && l2capinbuf[20] == 0x00) { if(l2capinbuf[16] == 0x00 && l2capinbuf[17] == 0xA4 && l2capinbuf[18] == 0x20 && l2capinbuf[19] == 0x00 && l2capinbuf[20] == 0x00) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nNunchuck connected")); Notify(PSTR("\r\nNunchuck connected"), 0x80);
#endif #endif
l2cap_event_flag |= WII_FLAG_NUNCHUCK_CONNECTED; l2cap_event_flag |= WII_FLAG_NUNCHUCK_CONNECTED;
} else if(l2capinbuf[16] == 0x00 && (l2capinbuf[17] == 0xA6 || l2capinbuf[17] == 0xA4) && l2capinbuf[18] == 0x20 && l2capinbuf[19] == 0x00 && l2capinbuf[20] == 0x05) { } else if(l2capinbuf[16] == 0x00 && (l2capinbuf[17] == 0xA6 || l2capinbuf[17] == 0xA4) && l2capinbuf[18] == 0x20 && l2capinbuf[19] == 0x00 && l2capinbuf[20] == 0x05) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nMotion Plus connected")); Notify(PSTR("\r\nMotion Plus connected"), 0x80);
#endif #endif
l2cap_event_flag |= WII_FLAG_MOTION_PLUS_CONNECTED; l2cap_event_flag |= WII_FLAG_MOTION_PLUS_CONNECTED;
} else if(l2capinbuf[16] == 0x00 && l2capinbuf[17] == 0xA4 && l2capinbuf[18] == 0x20 && l2capinbuf[19] == 0x04 && l2capinbuf[20] == 0x05) { } else if(l2capinbuf[16] == 0x00 && l2capinbuf[17] == 0xA4 && l2capinbuf[18] == 0x20 && l2capinbuf[19] == 0x04 && l2capinbuf[20] == 0x05) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nMotion Plus activated in normal mode")); Notify(PSTR("\r\nMotion Plus activated in normal mode"), 0x80);
#endif #endif
motionPlusConnected = true; motionPlusConnected = true;
} else if(l2capinbuf[16] == 0x00 && l2capinbuf[17] == 0xA4 && l2capinbuf[18] == 0x20 && l2capinbuf[19] == 0x05 && l2capinbuf[20] == 0x05) { } else if(l2capinbuf[16] == 0x00 && l2capinbuf[17] == 0xA4 && l2capinbuf[18] == 0x20 && l2capinbuf[19] == 0x05 && l2capinbuf[20] == 0x05) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nMotion Plus activated in Nunchuck pass-through mode")); Notify(PSTR("\r\nMotion Plus activated in Nunchuck pass-through mode"), 0x80);
#endif #endif
activateNunchuck = false; activateNunchuck = false;
motionPlusConnected = true; motionPlusConnected = true;
nunchuckConnected = true; nunchuckConnected = true;
} else if(l2capinbuf[16] == 0x00 && l2capinbuf[17] == 0xA6 && l2capinbuf[18] == 0x20 && (l2capinbuf[19] == 0x00 || l2capinbuf[19] == 0x04 || l2capinbuf[19] == 0x05 || l2capinbuf[19] == 0x07) && l2capinbuf[20] == 0x05) { } else if(l2capinbuf[16] == 0x00 && l2capinbuf[17] == 0xA6 && l2capinbuf[18] == 0x20 && (l2capinbuf[19] == 0x00 || l2capinbuf[19] == 0x04 || l2capinbuf[19] == 0x05 || l2capinbuf[19] == 0x07) && l2capinbuf[20] == 0x05) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nInactive Wii Motion Plus")); Notify(PSTR("\r\nInactive Wii Motion Plus"), 0x80);
Notify(PSTR("\r\nPlease unplug the Motion Plus, disconnect the Wiimote and then replug the Motion Plus Extension")); Notify(PSTR("\r\nPlease unplug the Motion Plus, disconnect the Wiimote and then replug the Motion Plus Extension"), 0x80);
#endif #endif
stateCounter = 300; // Skip the rest in "L2CAP_CHECK_MOTION_PLUS_STATE" stateCounter = 300; // Skip the rest in "L2CAP_CHECK_MOTION_PLUS_STATE"
} else if(l2capinbuf[16] == 0x00 && l2capinbuf[17] == 0xA4 && l2capinbuf[18] == 0x20 && l2capinbuf[19] == 0x01 && l2capinbuf[20] == 0x20) { } else if(l2capinbuf[16] == 0x00 && l2capinbuf[17] == 0xA4 && l2capinbuf[18] == 0x20 && l2capinbuf[19] == 0x01 && l2capinbuf[20] == 0x20) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nWii U Pro Controller connected")); Notify(PSTR("\r\nWii U Pro Controller connected"), 0x80);
#endif #endif
wiiUProControllerConnected = true; wiiUProControllerConnected = true;
} }
#ifdef DEBUG #ifdef DEBUG
else { else {
Notify(PSTR("\r\nUnknown Device: ")); Notify(PSTR("\r\nUnknown Device: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[13]); PrintHex<uint8_t>(l2capinbuf[13], 0x80);
PrintHex<uint8_t>(l2capinbuf[14]); PrintHex<uint8_t>(l2capinbuf[14], 0x80);
Notify(PSTR("\r\nData: ")); Notify(PSTR("\r\nData: "), 0x80);
for(uint8_t i = 0; i < ((l2capinbuf[12] >> 4)+1); i++) { // bit 4-7 is the length-1 for(uint8_t i = 0; i < ((l2capinbuf[12] >> 4)+1); i++) { // bit 4-7 is the length-1
PrintHex<uint8_t>(l2capinbuf[15+i]); PrintHex<uint8_t>(l2capinbuf[15+i], 0x80);
Notify(PSTR(" ")); Notify(PSTR(" "), 0x80);
} }
} }
#endif #endif
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
else { else {
Notify(PSTR("\r\nReport Error: ")); Notify(PSTR("\r\nReport Error: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[13]); PrintHex<uint8_t>(l2capinbuf[13], 0x80);
PrintHex<uint8_t>(l2capinbuf[14]); PrintHex<uint8_t>(l2capinbuf[14], 0x80);
} }
#endif #endif
break; break;
case 0x22: // Acknowledge output report, return function result case 0x22: // Acknowledge output report, return function result
#ifdef DEBUG #ifdef DEBUG
if(l2capinbuf[13] != 0x00) { // Check if there is an error if(l2capinbuf[13] != 0x00) { // Check if there is an error
Notify(PSTR("\r\nCommand failed: ")); Notify(PSTR("\r\nCommand failed: "), 0x80);
PrintHex<uint8_t>(l2capinbuf[12]); PrintHex<uint8_t>(l2capinbuf[12], 0x80);
} }
#endif #endif
break; break;
@ -511,7 +511,7 @@ void WII::ACLData(uint8_t* l2capinbuf) {
} else { } else {
if((micros() - timer) > 1000000) { // Loop for 1 sec before resetting the values if((micros() - timer) > 1000000) { // Loop for 1 sec before resetting the values
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nThe gyro values has been reset")); Notify(PSTR("\r\nThe gyro values has been reset"), 0x80);
#endif #endif
gyroYawZero = (l2capinbuf[15] | ((l2capinbuf[18] & 0xFC) << 6)); gyroYawZero = (l2capinbuf[15] | ((l2capinbuf[18] & 0xFC) << 6));
gyroRollZero = (l2capinbuf[16] | ((l2capinbuf[19] & 0xFC) << 6)); gyroRollZero = (l2capinbuf[16] | ((l2capinbuf[19] & 0xFC) << 6));
@ -546,7 +546,7 @@ void WII::ACLData(uint8_t* l2capinbuf) {
extensionConnected = true; extensionConnected = true;
unknownExtensionConnected = true; unknownExtensionConnected = true;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nExtension connected to Motion Plus")); Notify(PSTR("\r\nExtension connected to Motion Plus"), 0x80);
#endif #endif
} }
} }
@ -555,7 +555,7 @@ void WII::ACLData(uint8_t* l2capinbuf) {
extensionConnected = false; extensionConnected = false;
unknownExtensionConnected = true; unknownExtensionConnected = true;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nExtension disconnected from Motion Plus")); Notify(PSTR("\r\nExtension disconnected from Motion Plus"), 0x80);
#endif #endif
nunchuckConnected = false; // There is no extension connected to the Motion Plus if this report is sent nunchuckConnected = false; // There is no extension connected to the Motion Plus if this report is sent
} }
@ -581,7 +581,7 @@ void WII::ACLData(uint8_t* l2capinbuf) {
break; break;
#ifdef DEBUG #ifdef DEBUG
default: default:
Notify(PSTR("\r\nUnknown Report type: ")); Notify(PSTR("\r\nUnknown Report type: "), 0x80);
Serial.print(l2capinbuf[9],HEX); Serial.print(l2capinbuf[9],HEX);
break; break;
#endif #endif
@ -598,7 +598,7 @@ void WII::L2CAP_task() {
case L2CAP_CONTROL_SUCCESS: case L2CAP_CONTROL_SUCCESS:
if (l2cap_config_success_control_flag) { if (l2cap_config_success_control_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHID Control Successfully Configured")); Notify(PSTR("\r\nHID Control Successfully Configured"), 0x80);
#endif #endif
l2cap_state = L2CAP_INTERRUPT_SETUP; l2cap_state = L2CAP_INTERRUPT_SETUP;
} }
@ -607,7 +607,7 @@ void WII::L2CAP_task() {
case L2CAP_INTERRUPT_SETUP: case L2CAP_INTERRUPT_SETUP:
if (l2cap_connection_request_interrupt_flag) { if (l2cap_connection_request_interrupt_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHID Interrupt Incoming Connection Request")); Notify(PSTR("\r\nHID Interrupt Incoming Connection Request"), 0x80);
#endif #endif
pBtd->l2cap_connection_response(hci_handle,identifier, interrupt_dcid, interrupt_scid, PENDING); pBtd->l2cap_connection_response(hci_handle,identifier, interrupt_dcid, interrupt_scid, PENDING);
delay(1); delay(1);
@ -624,7 +624,7 @@ void WII::L2CAP_task() {
case L2CAP_CONTROL_CONNECT_REQUEST: case L2CAP_CONTROL_CONNECT_REQUEST:
if (l2cap_connected_control_flag) { if (l2cap_connected_control_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSend HID Control Config Request")); Notify(PSTR("\r\nSend HID Control Config Request"), 0x80);
#endif #endif
identifier++; identifier++;
pBtd->l2cap_config_request(hci_handle, identifier, control_scid); pBtd->l2cap_config_request(hci_handle, identifier, control_scid);
@ -635,7 +635,7 @@ void WII::L2CAP_task() {
case L2CAP_CONTROL_CONFIG_REQUEST: case L2CAP_CONTROL_CONFIG_REQUEST:
if(l2cap_config_success_control_flag) { if(l2cap_config_success_control_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSend HID Interrupt Connection Request")); Notify(PSTR("\r\nSend HID Interrupt Connection Request"), 0x80);
#endif #endif
identifier++; identifier++;
pBtd->l2cap_connection_request(hci_handle,identifier,interrupt_dcid,HID_INTR_PSM); pBtd->l2cap_connection_request(hci_handle,identifier,interrupt_dcid,HID_INTR_PSM);
@ -646,7 +646,7 @@ void WII::L2CAP_task() {
case L2CAP_INTERRUPT_CONNECT_REQUEST: case L2CAP_INTERRUPT_CONNECT_REQUEST:
if(l2cap_connected_interrupt_flag) { if(l2cap_connected_interrupt_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSend HID Interrupt Config Request")); Notify(PSTR("\r\nSend HID Interrupt Config Request"), 0x80);
#endif #endif
identifier++; identifier++;
pBtd->l2cap_config_request(hci_handle, identifier, interrupt_scid); pBtd->l2cap_config_request(hci_handle, identifier, interrupt_scid);
@ -657,7 +657,7 @@ void WII::L2CAP_task() {
case L2CAP_INTERRUPT_CONFIG_REQUEST: case L2CAP_INTERRUPT_CONFIG_REQUEST:
if(l2cap_config_success_interrupt_flag) { // Now the HID channels is established if(l2cap_config_success_interrupt_flag) { // Now the HID channels is established
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHID Channels Established")); Notify(PSTR("\r\nHID Channels Established"), 0x80);
#endif #endif
pBtd->connectToWii = false; pBtd->connectToWii = false;
pBtd->pairWithWii = false; pBtd->pairWithWii = false;
@ -672,7 +672,7 @@ void WII::L2CAP_task() {
case L2CAP_INTERRUPT_DISCONNECT: case L2CAP_INTERRUPT_DISCONNECT:
if (l2cap_disconnect_response_interrupt_flag) { if (l2cap_disconnect_response_interrupt_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDisconnected Interrupt Channel")); Notify(PSTR("\r\nDisconnected Interrupt Channel"), 0x80);
#endif #endif
identifier++; identifier++;
pBtd->l2cap_disconnection_request(hci_handle, identifier, control_scid, control_dcid); pBtd->l2cap_disconnection_request(hci_handle, identifier, control_scid, control_dcid);
@ -683,7 +683,7 @@ void WII::L2CAP_task() {
case L2CAP_CONTROL_DISCONNECT: case L2CAP_CONTROL_DISCONNECT:
if (l2cap_disconnect_response_control_flag) { if (l2cap_disconnect_response_control_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nDisconnected Control Channel")); Notify(PSTR("\r\nDisconnected Control Channel"), 0x80);
#endif #endif
pBtd->hci_disconnect(hci_handle); pBtd->hci_disconnect(hci_handle);
hci_handle = -1; // Reset handle hci_handle = -1; // Reset handle
@ -700,7 +700,7 @@ void WII::Run() {
pBtd->l2capConnectionClaimed = true; pBtd->l2capConnectionClaimed = true;
activeConnection = true; activeConnection = true;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSend HID Control Connection Request")); Notify(PSTR("\r\nSend HID Control Connection Request"), 0x80);
#endif #endif
hci_handle = pBtd->hci_handle; // Store the HCI Handle for the connection hci_handle = pBtd->hci_handle; // Store the HCI Handle for the connection
l2cap_event_flag = 0; // Reset flags l2cap_event_flag = 0; // Reset flags
@ -709,7 +709,7 @@ void WII::Run() {
l2cap_state = L2CAP_CONTROL_CONNECT_REQUEST; l2cap_state = L2CAP_CONTROL_CONNECT_REQUEST;
} else if (l2cap_connection_request_control_flag) { } else if (l2cap_connection_request_control_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nHID Control Incoming Connection Request")); Notify(PSTR("\r\nHID Control Incoming Connection Request"), 0x80);
#endif #endif
pBtd->l2cap_connection_response(hci_handle,identifier, control_dcid, control_scid, PENDING); pBtd->l2cap_connection_response(hci_handle,identifier, control_dcid, control_scid, PENDING);
delay(1); delay(1);
@ -724,7 +724,7 @@ void WII::Run() {
case L2CAP_CHECK_MOTION_PLUS_STATE: case L2CAP_CHECK_MOTION_PLUS_STATE:
#ifdef DEBUG #ifdef DEBUG
if(stateCounter == 0) // Only print onnce if(stateCounter == 0) // Only print onnce
Notify(PSTR("\r\nChecking if a Motion Plus is connected")); Notify(PSTR("\r\nChecking if a Motion Plus is connected"), 0x80);
#endif #endif
stateCounter++; stateCounter++;
if(stateCounter%200 == 0) if(stateCounter%200 == 0)
@ -736,7 +736,7 @@ void WII::Run() {
if(unknownExtensionConnected) { if(unknownExtensionConnected) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nA extension is also connected")); Notify(PSTR("\r\nA extension is also connected"), 0x80);
#endif #endif
activateNunchuck = true; // For we will just set this to true as this the only extension supported so far activateNunchuck = true; // For we will just set this to true as this the only extension supported so far
} }
@ -744,7 +744,7 @@ void WII::Run() {
} }
else if(stateCounter == 601) { // We will try three times to check for the motion plus else if(stateCounter == 601) { // We will try three times to check for the motion plus
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nNo Motion Plus was detected")); Notify(PSTR("\r\nNo Motion Plus was detected"), 0x80);
#endif #endif
stateCounter = 0; stateCounter = 0;
l2cap_state = L2CAP_CHECK_EXTENSION_STATE; l2cap_state = L2CAP_CHECK_EXTENSION_STATE;
@ -754,7 +754,7 @@ void WII::Run() {
case L2CAP_CHECK_EXTENSION_STATE: // This is used to check if there is anything plugged in to the extension port case L2CAP_CHECK_EXTENSION_STATE: // This is used to check if there is anything plugged in to the extension port
#ifdef DEBUG #ifdef DEBUG
if(stateCounter == 0) // Only print onnce if(stateCounter == 0) // Only print onnce
Notify(PSTR("\r\nChecking if there is any extension connected")); Notify(PSTR("\r\nChecking if there is any extension connected"), 0x80);
#endif #endif
stateCounter++; // We use this counter as there has to be a short delay between the commands stateCounter++; // We use this counter as there has to be a short delay between the commands
if(stateCounter == 1) if(stateCounter == 1)
@ -801,7 +801,7 @@ void WII::Run() {
if(unknownExtensionConnected) { if(unknownExtensionConnected) {
#ifdef DEBUG #ifdef DEBUG
if(stateCounter == 0) // Only print once if(stateCounter == 0) // Only print once
Notify(PSTR("\r\nChecking extension port")); Notify(PSTR("\r\nChecking extension port"), 0x80);
#endif #endif
stateCounter++; // We will use this counter as there has to be a short delay between the commands stateCounter++; // We will use this counter as there has to be a short delay between the commands
if(stateCounter == 50) if(stateCounter == 50)
@ -818,7 +818,7 @@ void WII::Run() {
else if(stateCounter == 250) { else if(stateCounter == 250) {
if(nunchuck_connected_flag) { if(nunchuck_connected_flag) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nNunchuck was reconnected")); Notify(PSTR("\r\nNunchuck was reconnected"), 0x80);
#endif #endif
activateNunchuck = true; activateNunchuck = true;
nunchuckConnected = true; nunchuckConnected = true;
@ -829,7 +829,7 @@ void WII::Run() {
else if (stateCounter == 300) { else if (stateCounter == 300) {
if(motionPlusConnected) { if(motionPlusConnected) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nReactivating the Motion Plus")); Notify(PSTR("\r\nReactivating the Motion Plus"), 0x80);
#endif #endif
initMotionPlus(); initMotionPlus();
} else } else
@ -963,12 +963,12 @@ void WII::activateMotionPlus() {
uint8_t buf[1]; uint8_t buf[1];
if(pBtd->wiiUProController) { if(pBtd->wiiUProController) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nActivating Wii U Pro Controller")); Notify(PSTR("\r\nActivating Wii U Pro Controller"), 0x80);
#endif #endif
buf[0] = 0x00; // It seems like you can send anything but 0x04, 0x05, and 0x07 buf[0] = 0x00; // It seems like you can send anything but 0x04, 0x05, and 0x07
} else if(activateNunchuck) { } else if(activateNunchuck) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nActivating Motion Plus in pass-through mode")); Notify(PSTR("\r\nActivating Motion Plus in pass-through mode"), 0x80);
#endif #endif
buf[0] = 0x05; // Activate nunchuck pass-through mode buf[0] = 0x05; // Activate nunchuck pass-through mode
} }
@ -976,7 +976,7 @@ void WII::activateMotionPlus() {
//buf[0] = 0x07; //buf[0] = 0x07;
else { else {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nActivating Motion Plus in normal mode")); Notify(PSTR("\r\nActivating Motion Plus in normal mode"), 0x80);
#endif #endif
buf[0] = 0x04; // Don't use any extension buf[0] = 0x04; // Don't use any extension
} }
@ -1060,58 +1060,58 @@ void WII::IRinitialize(){ // Turns on and initialises the IR camera
enableIRCamera1(); enableIRCamera1();
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nEnable IR Camera1 Complete")); Notify(PSTR("\r\nEnable IR Camera1 Complete"), 0x80);
#endif #endif
delay(80); delay(80);
enableIRCamera2(); enableIRCamera2();
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nEnable IR Camera2 Complete")); Notify(PSTR("\r\nEnable IR Camera2 Complete"), 0x80);
#endif #endif
delay(80); delay(80);
write0x08Value(); write0x08Value();
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nWrote hex number 0x08")); Notify(PSTR("\r\nWrote hex number 0x08"), 0x80);
#endif #endif
delay(80); delay(80);
writeSensitivityBlock1(); writeSensitivityBlock1();
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nWrote Sensitivity Block 1")); Notify(PSTR("\r\nWrote Sensitivity Block 1"), 0x80);
#endif #endif
delay(80); delay(80);
writeSensitivityBlock2(); writeSensitivityBlock2();
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nWrote Sensitivity Block 2")); Notify(PSTR("\r\nWrote Sensitivity Block 2"), 0x80);
#endif #endif
delay(80); delay(80);
uint8_t mode_num = 0x03; uint8_t mode_num = 0x03;
setWiiModeNumber(mode_num); // Change input for whatever mode you want i.e. 0x01, 0x03, or 0x05 setWiiModeNumber(mode_num); // Change input for whatever mode you want i.e. 0x01, 0x03, or 0x05
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSet Wii Mode Number To 0x")); Notify(PSTR("\r\nSet Wii Mode Number To 0x"), 0x80);
PrintHex<uint8_t>(mode_num); PrintHex<uint8_t>(mode_num, 0x80);
#endif #endif
delay(80); delay(80);
write0x08Value(); write0x08Value();
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nWrote Hex Number 0x08")); Notify(PSTR("\r\nWrote Hex Number 0x08"), 0x80);
#endif #endif
delay(80); delay(80);
setReportMode(false, 0x33); setReportMode(false, 0x33);
//setReportMode(false, 0x3f); // For full reporting mode, doesn't work yet //setReportMode(false, 0x3f); // For full reporting mode, doesn't work yet
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nSet Report Mode to 0x33")); Notify(PSTR("\r\nSet Report Mode to 0x33"), 0x80);
#endif #endif
delay(80); delay(80);
statusRequest(); // Used to update wiiState - call isIRCameraEnabled() afterwards to check if it actually worked statusRequest(); // Used to update wiiState - call isIRCameraEnabled() afterwards to check if it actually worked
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nIR Initialized")); Notify(PSTR("\r\nIR Initialized"), 0x80);
#endif #endif
} }

View file

@ -48,12 +48,12 @@ uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) {
// get memory address of USB device address pool // get memory address of USB device address pool
AddressPool &addrPool = pUsb->GetAddressPool(); AddressPool &addrPool = pUsb->GetAddressPool();
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nXBOXRECV Init")); Notify(PSTR("\r\nXBOXRECV Init"), 0x80);
#endif #endif
// check if address has already been assigned to an instance // check if address has already been assigned to an instance
if (bAddress) { if (bAddress) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nAddress in use")); Notify(PSTR("\r\nAddress in use"), 0x80);
#endif #endif
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE; return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
} }
@ -63,14 +63,14 @@ uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) {
if (!p) { if (!p) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nAddress not found")); Notify(PSTR("\r\nAddress not found"), 0x80);
#endif #endif
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
} }
if (!p->epinfo) { if (!p->epinfo) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nepinfo is null")); Notify(PSTR("\r\nepinfo is null"), 0x80);
#endif #endif
return USB_ERROR_EPINFO_IS_NULL; return USB_ERROR_EPINFO_IS_NULL;
} }
@ -98,7 +98,7 @@ uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) {
goto FailUnknownDevice; goto FailUnknownDevice;
else if(PID != XBOX_WIRELESS_RECEIVER_PID && PID != XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID) { else if(PID != XBOX_WIRELESS_RECEIVER_PID && PID != XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nYou'll need a wireless receiver for this libary to work")); Notify(PSTR("\r\nYou'll need a wireless receiver for this libary to work"), 0x80);
#endif #endif
goto FailUnknownDevice; goto FailUnknownDevice;
} }
@ -119,14 +119,14 @@ uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) {
addrPool.FreeAddress(bAddress); addrPool.FreeAddress(bAddress);
bAddress = 0; bAddress = 0;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetAddr: ")); Notify(PSTR("\r\nsetAddr: "), 0x80);
#endif #endif
PrintHex<uint8_t>(rcode); PrintHex<uint8_t>(rcode, 0x80);
return rcode; return rcode;
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nAddr: ")); Notify(PSTR("\r\nAddr: "), 0x80);
PrintHex<uint8_t>(bAddress); PrintHex<uint8_t>(bAddress, 0x80);
#endif #endif
p->lowspeed = false; p->lowspeed = false;
@ -210,7 +210,7 @@ uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) {
goto FailSetConf; goto FailSetConf;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nXbox Wireless Receiver Connected\r\n")); Notify(PSTR("\r\nXbox Wireless Receiver Connected\r\n"), 0x80);
#endif #endif
XboxReceiverConnected = true; XboxReceiverConnected = true;
bPollEnable = true; bPollEnable = true;
@ -219,31 +219,31 @@ uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) {
/* diagnostic messages */ /* diagnostic messages */
FailGetDevDescr: FailGetDevDescr:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\ngetDevDescr:")); Notify(PSTR("\r\ngetDevDescr:"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailSetDevTblEntry: FailSetDevTblEntry:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetDevTblEn:")); Notify(PSTR("\r\nsetDevTblEn:"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailSetConf: FailSetConf:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetConf:")); Notify(PSTR("\r\nsetConf:"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailUnknownDevice: FailUnknownDevice:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nUnknown Device Connected - VID: ")); Notify(PSTR("\r\nUnknown Device Connected - VID: "), 0x80);
PrintHex<uint16_t>(VID); PrintHex<uint16_t>(VID, 0x80);
Notify(PSTR(" PID: ")); Notify(PSTR(" PID: "), 0x80);
PrintHex<uint16_t>(PID); PrintHex<uint16_t>(PID, 0x80);
#endif #endif
rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
goto Fail; goto Fail;
Fail: Fail:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nXbox 360 Init Failed, error code: ")); Notify(PSTR("\r\nXbox 360 Init Failed, error code: "), 0x80);
Serial.print(rcode,HEX); Serial.print(rcode,HEX);
#endif #endif
Release(); Release();
@ -280,9 +280,9 @@ uint8_t XBOXRECV::Poll() {
pUsb->inTransfer(bAddress, epInfo[ inputPipe ].epAddr, &bufferSize, readBuf); pUsb->inTransfer(bAddress, epInfo[ inputPipe ].epAddr, &bufferSize, readBuf);
if(bufferSize > 0) { // The number of received bytes if(bufferSize > 0) { // The number of received bytes
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("Bytes Received: ")); Notify(PSTR("Bytes Received: "), 0x80);
Serial.print(bufferSize); Serial.print(bufferSize);
Notify(PSTR("\r\n")); Notify(PSTR("\r\n"), 0x80);
#endif #endif
readReport(i); readReport(i);
#ifdef PRINTREPORT #ifdef PRINTREPORT
@ -300,7 +300,7 @@ void XBOXRECV::readReport(uint8_t controller) {
if(readBuf[0] == 0x08 && readBuf[1] != Xbox360Connected[controller]) { if(readBuf[0] == 0x08 && readBuf[1] != Xbox360Connected[controller]) {
Xbox360Connected[controller] = readBuf[1]; Xbox360Connected[controller] = readBuf[1];
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("Controller ")); Notify(PSTR("Controller "), 0x80);
Serial.print(controller); Serial.print(controller);
#endif #endif
if(Xbox360Connected[controller]) { if(Xbox360Connected[controller]) {
@ -311,8 +311,8 @@ void XBOXRECV::readReport(uint8_t controller) {
case 0x40: str = PSTR(" as headset\r\n"); break; case 0x40: str = PSTR(" as headset\r\n"); break;
case 0xC0: str = PSTR(" as controller+headset\r\n"); break; case 0xC0: str = PSTR(" as controller+headset\r\n"); break;
} }
Notify(PSTR(": connected")); Notify(PSTR(": connected"), 0x80);
Notify(str); Notify(str, 0x80);
#endif #endif
LED led; LED led;
switch (controller) { switch (controller) {
@ -325,7 +325,7 @@ void XBOXRECV::readReport(uint8_t controller) {
} }
#ifdef DEBUG #ifdef DEBUG
else else
Notify(PSTR(": disconnected\r\n")); Notify(PSTR(": disconnected\r\n"), 0x80);
#endif #endif
return; return;
} }
@ -348,8 +348,8 @@ void XBOXRECV::readReport(uint8_t controller) {
hatValue[controller][RightHatX] = (int16_t)(((uint16_t)readBuf[15] << 8) | readBuf[14]); hatValue[controller][RightHatX] = (int16_t)(((uint16_t)readBuf[15] << 8) | readBuf[14]);
hatValue[controller][RightHatY] = (int16_t)(((uint16_t)readBuf[17] << 8) | readBuf[16]); hatValue[controller][RightHatY] = (int16_t)(((uint16_t)readBuf[17] << 8) | readBuf[16]);
//Notify(PSTR("\r\nButtonState: ")); //Notify(PSTR("\r\nButtonState: "), 0x80);
//PrintHex<uint32_t>(ButtonState[controller]); //PrintHex<uint32_t>(ButtonState[controller], 0x80);
if(ButtonState[controller] != OldButtonState[controller]) { if(ButtonState[controller] != OldButtonState[controller]) {
buttonStateChanged[controller] = true; buttonStateChanged[controller] = true;
@ -366,11 +366,11 @@ void XBOXRECV::printReport(uint8_t controller, uint8_t nBytes) { //Uncomment "#d
#ifdef PRINTREPORT #ifdef PRINTREPORT
if (readBuf == NULL) if (readBuf == NULL)
return; return;
Notify(PSTR("Controller ")); Notify(PSTR("Controller "), 0x80);
Serial.print(controller); Serial.print(controller);
Notify(PSTR(": ")); Notify(PSTR(": "), 0x80);
for(uint8_t i = 0; i < nBytes;i++) { for(uint8_t i = 0; i < nBytes;i++) {
PrintHex<uint8_t>(readBuf[i]); PrintHex<uint8_t>(readBuf[i], 0x80);
Serial.print(" "); Serial.print(" ");
} }
Serial.println(); Serial.println();
@ -449,7 +449,7 @@ void XBOXRECV::XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes) {
rcode = pUsb->outTransfer(bAddress, epInfo[ outputPipe ].epAddr, nbytes, data); rcode = pUsb->outTransfer(bAddress, epInfo[ outputPipe ].epAddr, nbytes, data);
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
if(rcode) if(rcode)
Notify(PSTR("Error sending Xbox message\r\n")); Notify(PSTR("Error sending Xbox message\r\n"), 0x80);
#endif #endif
} }
void XBOXRECV::setLedRaw(uint8_t controller, uint8_t value) { void XBOXRECV::setLedRaw(uint8_t controller, uint8_t value) {

View file

@ -46,12 +46,12 @@ uint8_t XBOXUSB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
// get memory address of USB device address pool // get memory address of USB device address pool
AddressPool &addrPool = pUsb->GetAddressPool(); AddressPool &addrPool = pUsb->GetAddressPool();
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nXBOXUSB Init")); Notify(PSTR("\r\nXBOXUSB Init"), 0x80);
#endif #endif
// check if address has already been assigned to an instance // check if address has already been assigned to an instance
if (bAddress) { if (bAddress) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nAddress in use")); Notify(PSTR("\r\nAddress in use"), 0x80);
#endif #endif
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE; return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
} }
@ -61,14 +61,14 @@ uint8_t XBOXUSB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
if (!p) { if (!p) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nAddress not found")); Notify(PSTR("\r\nAddress not found"), 0x80);
#endif #endif
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
} }
if (!p->epinfo) { if (!p->epinfo) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nepinfo is null")); Notify(PSTR("\r\nepinfo is null"), 0x80);
#endif #endif
return USB_ERROR_EPINFO_IS_NULL; return USB_ERROR_EPINFO_IS_NULL;
} }
@ -96,13 +96,13 @@ uint8_t XBOXUSB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
goto FailUnknownDevice; goto FailUnknownDevice;
if(PID == XBOX_WIRELESS_PID) { if(PID == XBOX_WIRELESS_PID) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nYou have plugged in a wireless Xbox 360 controller - it doesn't support USB communication")); Notify(PSTR("\r\nYou have plugged in a wireless Xbox 360 controller - it doesn't support USB communication"), 0x80);
#endif #endif
goto FailUnknownDevice; goto FailUnknownDevice;
} }
else if(PID == XBOX_WIRELESS_RECEIVER_PID || PID == XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID) { else if(PID == XBOX_WIRELESS_RECEIVER_PID || PID == XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID) {
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nThis library only supports Xbox 360 controllers via USB")); Notify(PSTR("\r\nThis library only supports Xbox 360 controllers via USB"), 0x80);
#endif #endif
goto FailUnknownDevice; goto FailUnknownDevice;
} }
@ -123,14 +123,14 @@ uint8_t XBOXUSB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
addrPool.FreeAddress(bAddress); addrPool.FreeAddress(bAddress);
bAddress = 0; bAddress = 0;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetAddr: ")); Notify(PSTR("\r\nsetAddr: "), 0x80);
#endif #endif
PrintHex<uint8_t>(rcode); PrintHex<uint8_t>(rcode, 0x80);
return rcode; return rcode;
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nAddr: ")); Notify(PSTR("\r\nAddr: "), 0x80);
PrintHex<uint8_t>(bAddress); PrintHex<uint8_t>(bAddress, 0x80);
#endif #endif
p->lowspeed = false; p->lowspeed = false;
@ -175,7 +175,7 @@ uint8_t XBOXUSB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
goto FailSetConf; goto FailSetConf;
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nXbox 360 Controller Connected\r\n")); Notify(PSTR("\r\nXbox 360 Controller Connected\r\n"), 0x80);
#endif #endif
setLedOn(LED1); setLedOn(LED1);
Xbox360Connected = true; Xbox360Connected = true;
@ -185,31 +185,31 @@ uint8_t XBOXUSB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
/* diagnostic messages */ /* diagnostic messages */
FailGetDevDescr: FailGetDevDescr:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\ngetDevDescr:")); Notify(PSTR("\r\ngetDevDescr:"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailSetDevTblEntry: FailSetDevTblEntry:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetDevTblEn:")); Notify(PSTR("\r\nsetDevTblEn:"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailSetConf: FailSetConf:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nsetConf:")); Notify(PSTR("\r\nsetConf:"), 0x80);
#endif #endif
goto Fail; goto Fail;
FailUnknownDevice: FailUnknownDevice:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nUnknown Device Connected - VID: ")); Notify(PSTR("\r\nUnknown Device Connected - VID: "), 0x80);
PrintHex<uint16_t>(VID); PrintHex<uint16_t>(VID, 0x80);
Notify(PSTR(" PID: ")); Notify(PSTR(" PID: "), 0x80);
PrintHex<uint16_t>(PID); PrintHex<uint16_t>(PID, 0x80);
#endif #endif
rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
goto Fail; goto Fail;
Fail: Fail:
#ifdef DEBUG #ifdef DEBUG
Notify(PSTR("\r\nXbox 360 Init Failed, error code: ")); Notify(PSTR("\r\nXbox 360 Init Failed, error code: "), 0x80);
Serial.print(rcode,HEX); Serial.print(rcode,HEX);
#endif #endif
Release(); Release();
@ -250,8 +250,8 @@ void XBOXUSB::readReport() {
hatValue[RightHatX] = (int16_t)(((uint16_t)readBuf[11] << 8) | readBuf[10]); hatValue[RightHatX] = (int16_t)(((uint16_t)readBuf[11] << 8) | readBuf[10]);
hatValue[RightHatY] = (int16_t)(((uint16_t)readBuf[13] << 8) | readBuf[12]); hatValue[RightHatY] = (int16_t)(((uint16_t)readBuf[13] << 8) | readBuf[12]);
//Notify(PSTR("\r\nButtonState")); //Notify(PSTR("\r\nButtonState"), 0x80);
//PrintHex<uint32_t>(ButtonState); //PrintHex<uint32_t>(ButtonState, 0x80);
if(ButtonState != OldButtonState) { if(ButtonState != OldButtonState) {
ButtonClickState = (ButtonState >> 16) & ((~OldButtonState) >> 16); // Update click state variable, but don't include the two trigger buttons L2 and R2 ButtonClickState = (ButtonState >> 16) & ((~OldButtonState) >> 16); // Update click state variable, but don't include the two trigger buttons L2 and R2
@ -268,7 +268,7 @@ void XBOXUSB::printReport() { //Uncomment "#define PRINTREPORT" to print the rep
if (readBuf == NULL) if (readBuf == NULL)
return; return;
for(uint8_t i = 0; i < XBOX_REPORT_BUFFER_SIZE;i++) { for(uint8_t i = 0; i < XBOX_REPORT_BUFFER_SIZE;i++) {
PrintHex<uint8_t>(readBuf[i]); PrintHex<uint8_t>(readBuf[i], 0x80);
Serial.print(" "); Serial.print(" ");
} }
Serial.println(); Serial.println();