From 566d0cf9e33bedec239bc1c2aee4b5ef484129ed Mon Sep 17 00:00:00 2001 From: "fires.gc" Date: Wed, 29 Oct 2008 18:22:05 +0000 Subject: wiimote code cleaning git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@1001 8ced0084-cf51-0410-be5f-012b33b47a6e --- .../Core/Src/IPC_HLE/WII_IPC_HLE_Device_usb.cpp | 62 +- .../Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.cpp | 713 +++++++++++---------- Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.h | 39 +- Source/Core/Core/Src/Plugins/Plugin_Wiimote.cpp | 20 +- Source/Core/Core/Src/Plugins/Plugin_Wiimote.h | 4 +- 5 files changed, 421 insertions(+), 417 deletions(-) (limited to 'Source/Core') diff --git a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_usb.cpp b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_usb.cpp index a104743f48..b89f597e05 100644 --- a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_usb.cpp +++ b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_usb.cpp @@ -23,8 +23,6 @@ // ugly hacks for "SendEventNumberOfCompletedPackets" int g_HCICount = 0; int g_GlobalHandle = 0; -bool g_ConnectionRequested = false; - CWII_IPC_HLE_Device_usb_oh1_57e_305::CWII_IPC_HLE_Device_usb_oh1_57e_305(u32 _DeviceID, const std::string& _rDeviceName) : IWII_IPC_HLE_Device(_DeviceID, _rDeviceName) @@ -310,6 +308,15 @@ u32 CWII_IPC_HLE_Device_usb_oh1_57e_305::Update() g_HCICount = 0; } + if (m_AclFrameQue.empty()) + { + for (size_t i=0; iuclass[2]); LOG(WIIMOTE, " LinkType: %s", LinkType[pEventRequestConnection->LinkType]); - g_ConnectionRequested = true; - return true; }; @@ -605,6 +602,13 @@ bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventConnectionComplete(bdaddr_t _ AddEventToQueue(Event); + CWII_IPC_HLE_WiiMote* pWiimote = AccessWiiMote(_bd); + if (pWiimote) + { + pWiimote->EventConnectionAccepted(); + } + + g_GlobalHandle = pConnectionComplete->Connection_Handle; #ifdef LOGGING @@ -1554,14 +1558,6 @@ void CWII_IPC_HLE_Device_usb_oh1_57e_305::CommandAcceptCon(u8* _Input) } SendEventConnectionComplete(pAcceptCon->bdaddr); - - - - CWII_IPC_HLE_WiiMote* pWiimote = AccessWiiMote(pAcceptCon->bdaddr); - if (pWiimote) - { - pWiimote->EventConnectionAccepted(); - } } void CWII_IPC_HLE_Device_usb_oh1_57e_305::CommandReadClockOffset(u8* _Input) @@ -1577,14 +1573,8 @@ void CWII_IPC_HLE_Device_usb_oh1_57e_305::CommandReadClockOffset(u8* _Input) SendEventReadClockOffsetComplete(pReadClockOffset->con_handle); - if (g_ConnectionRequested) - { - CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(pReadClockOffset->con_handle); - SendEventRequestLinkKey(pWiiMote->GetBD()); - - // CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(pReadClockOffset->con_handle); - pWiiMote->Connect(); - } +// CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(pReadClockOffset->con_handle); +// SendEventRequestLinkKey(pWiiMote->GetBD()); } void CWII_IPC_HLE_Device_usb_oh1_57e_305::CommandReadRemoteVerInfo(u8* _Input) @@ -1625,12 +1615,11 @@ void CWII_IPC_HLE_Device_usb_oh1_57e_305::CommandWriteLinkPolicy(u8* _Input) SendEventCommandStatus(HCI_CMD_WRITE_LINK_POLICY_SETTINGS); - // just HB calls the WriteLinkPolicy... - if (g_ConnectionRequested) + CWII_IPC_HLE_WiiMote* pWiimote = AccessWiiMote(pLinkPolicy->con_handle); + if (pWiimote) { - CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(pLinkPolicy->con_handle); - pWiiMote->Connect(); - } + pWiimote->EventCommandWriteLinkPolicy(); + } } void CWII_IPC_HLE_Device_usb_oh1_57e_305::CommandAuthenticationRequested(u8* _Input) @@ -1738,13 +1727,6 @@ void CWII_IPC_HLE_Device_usb_oh1_57e_305::CommandLinkKeyRep(u8* _Input) Reply.bdaddr = pKeyRep->bdaddr; SendEventCommandComplete(HCI_CMD_LINK_KEY_REP, &Reply, sizeof(hci_link_key_rep_rp)); - - if (g_ConnectionRequested) - { - // CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(pKeyRep->bdaddr); - // SendEventAuthenticationCompleted(pWiiMote->GetConnectionHandle()); - // pWiiMote->Connect(); - } } diff --git a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.cpp b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.cpp index 58026c14e6..bd497270f1 100644 --- a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.cpp +++ b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.cpp @@ -16,10 +16,12 @@ // http://code.google.com/p/dolphin-emu/ #include "Common.h" +#include "WII_IPC_HLE_WiiMote.h" + #include "../Plugins/Plugin_Wiimote.h" #include "WII_IPC_HLE_Device_usb.h" -#include "WII_IPC_HLE_WiiMote.h" + #include "l2cap.h" #include "WiiMote_HID_Attr.h" @@ -27,9 +29,9 @@ #pragma pack(push, 1) #endif - -#define HIDP_OUTPUT_CHANNEL 0x11 -#define HIDP_INPUT_CHANNEL 0x13 +#define SDP_CHANNEL 0x01 +#define HIDP_CONTROL_CHANNEL 0x11 +#define HID_INTERRUPT_CHANNEL 0x13 struct SL2CAP_Header @@ -132,30 +134,21 @@ struct SL2CAP_CommandDisconnectionResponse // 0x07 static CWII_IPC_HLE_Device_usb_oh1_57e_305* s_Usb; -namespace Core { - void Callback_WiimoteInput(u16 _channelID, const void* _pData, u32 _Size) - { - const u8* pData = (const u8*)_pData; - LOG(WIIMOTE, "Callback_WiimoteInput: 0x%x", _channelID); - std::string Temp; - for (u32 j=0; j<_Size; j++) - { - char Buffer[128]; - sprintf(Buffer, "%02x ", pData[j]); - Temp.append(Buffer); - } - LOG(WIIMOTE, " Data: %s", Temp.c_str()); - - s_Usb->m_WiiMotes[0].SendL2capData(_channelID, _pData, _Size); - - // CCPU::Break(); - } -} CWII_IPC_HLE_WiiMote::CWII_IPC_HLE_WiiMote(CWII_IPC_HLE_Device_usb_oh1_57e_305* _pHost, int _Number) : m_Name("Nintendo RVL-CNT-01") , m_pHost(_pHost) , m_Connected(false) + , m_HIDControlChannel_Connected(false) + , m_HIDControlChannel_Config(false) + , m_HIDInterruptChannel_Connected(false) + , m_HIDInterruptChannel_Config(false) + , m_HIDControlChannel_ConnectedWait(false) + , m_HIDControlChannel_ConfigWait(false) + , m_HIDInterruptChannel_ConnectedWait(false) + , m_HIDInterruptChannel_ConfigWait(false) + , m_HIDInterruptChannelHost_Config(false) + , m_HIDControlChannelHost_Config(false) { s_Usb = _pHost; LOG(WIIMOTE, "Wiimote %i constructed", _Number); @@ -174,7 +167,6 @@ CWII_IPC_HLE_WiiMote::CWII_IPC_HLE_WiiMote(CWII_IPC_HLE_Device_usb_oh1_57e_305* m_BD.b[4] = 0x1a; m_BD.b[5] = 0x00; //_Number; - m_ControllerConnectionHandle = 0x100 + _Number; uclass[0]= 0x00; @@ -196,6 +188,89 @@ CWII_IPC_HLE_WiiMote::CWII_IPC_HLE_WiiMote(CWII_IPC_HLE_Device_usb_oh1_57e_305* memset(m_LinkKey, 0xA0 + _Number, 16); } +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// +// +// +// --- Simple and ugly state machine +// +// +// +// +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +bool CWII_IPC_HLE_WiiMote::Update() +{ + if (m_Connected == false) + return false; + + // try to connect HIDP_CONTROL_CHANNEL + if (!m_HIDControlChannel_Connected) + { + if (m_HIDControlChannel_ConnectedWait) + return false; + + m_HIDControlChannel_ConnectedWait = true; + SendConnectionRequest(0x0040, HIDP_CONTROL_CHANNEL); + + return true; + } + + // try to config HIDP_CONTROL_CHANNEL + if (!m_HIDControlChannel_Config) + { + if (m_HIDControlChannel_ConfigWait) + return false; + + SChannel& rChannel = m_Channel[0x0040]; + + m_HIDControlChannel_ConfigWait = true; + SendConfigurationRequest(rChannel.DCID); + return true; + } + + // try to connect HID_INTERRUPT_CHANNEL + if (!m_HIDInterruptChannel_Connected) + { + if (m_HIDInterruptChannel_ConnectedWait) + return false; + + m_HIDInterruptChannel_ConnectedWait = true; + SendConnectionRequest(0x0041, HID_INTERRUPT_CHANNEL); + return true; + } + + // try to config HID_INTERRUPT_CHANNEL + if (!m_HIDInterruptChannel_Config) + { + if (m_HIDInterruptChannel_ConfigWait) + return false; + + m_HIDInterruptChannel_ConfigWait = true; + + SChannel& rChannel = m_Channel[0x0041]; + SendConfigurationRequest(rChannel.DCID); + return true; + } + + return false; +} + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// +// +// +// --- Events +// +// +// +// +// +///////////////////////////////////////////////////////////////////////////////////////////////// + void CWII_IPC_HLE_WiiMote::EventConnectionAccepted() { m_Connected = true; @@ -217,7 +292,32 @@ bool CWII_IPC_HLE_WiiMote::EventPagingChanged(u8 _pageMode) return true; } +void CWII_IPC_HLE_WiiMote::EventCommandWriteLinkPolicy() +{ + // reset connection process + m_HIDControlChannel_Connected = false; + m_HIDControlChannel_Config = false; + m_HIDInterruptChannel_Connected = false; + m_HIDInterruptChannel_Config = false; + m_HIDControlChannel_ConnectedWait = false; + m_HIDControlChannel_ConfigWait = false; + m_HIDInterruptChannel_ConnectedWait = false; + m_HIDInterruptChannel_ConfigWait = false; +} + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// +// +// +// --- Input parsing +// +// +// +// +// +///////////////////////////////////////////////////////////////////////////////////////////////// void CWII_IPC_HLE_WiiMote::SendACLFrame(u8* _pData, u32 _Size) { @@ -231,7 +331,8 @@ void CWII_IPC_HLE_WiiMote::SendACLFrame(u8* _pData, u32 _Size) LOG(WIIMOTE, "L2Cap-SendFrame: Channel 0x%04x, Len 0x%x, DataSize 0x%x", pHeader->CID, pHeader->Length, DataSize); - if(pHeader->Length != DataSize) { + if(pHeader->Length != DataSize) + { LOG(WIIMOTE, "Faulty packet. It is dropped."); return; } @@ -244,180 +345,35 @@ void CWII_IPC_HLE_WiiMote::SendACLFrame(u8* _pData, u32 _Size) default: { + _dbg_assert_msg_(WIIMOTE, DoesChannelExist(pHeader->CID), "SendACLFrame to unknown channel %i", pHeader->CID); CChannelMap::iterator itr= m_Channel.find(pHeader->CID); if (itr != m_Channel.end()) { SChannel& rChannel = itr->second; switch(rChannel.PSM) { - case 0x01: //SDP - { - HandleSDP(pHeader->CID, pData, DataSize); - } + case SDP_CHANNEL: + HandleSDP(pHeader->CID, pData, DataSize); break; - case 0x11: // HID Output - { - PanicAlert("CWII_IPC_HLE_WiiMote: HID Output %x, %x",rChannel.SCID, rChannel.DCID); - PluginWiimote::Wiimote_Output(rChannel.DCID, pData, DataSize); - - //return handshake - // u8 handshake = 0; - // SendL2capData(rChannel.DCID, &handshake, 1); - } + case HIDP_CONTROL_CHANNEL: + PluginWiimote::Wiimote_ControlChannel(rChannel.DCID, pData, DataSize); break; - case 0x13: // HID Input - { - PluginWiimote::Wiimote_Input(rChannel.DCID, pData, DataSize); - - //return handshake - u8 handshake = 0; - SendL2capData(rChannel.DCID, &handshake, 1); - } + case HID_INTERRUPT_CHANNEL: + PluginWiimote::Wiimote_InterruptChannel(rChannel.DCID, pData, DataSize); break; default: - PanicAlert("channel 0x04%x has unknow PSM %x", pHeader->CID, rChannel.PSM); + PanicAlert("channel 0x04%x has unknown PSM %x", pHeader->CID, rChannel.PSM); break; } } - else - { - PanicAlert("SendACLFrame to unknown channel %i", pHeader->CID); - } - } break; } } -void CWII_IPC_HLE_WiiMote::SendCommandToACL(u8 _Ident, u8 _Code, u8 _CommandLength, u8* _pCommandData) -{ - u8 DataFrame[1024]; - u32 Offset = 0; - - SL2CAP_Header* pHeader = (SL2CAP_Header*)&DataFrame[Offset]; Offset += sizeof(SL2CAP_Header); - pHeader->CID = 0x0001; - pHeader->Length = sizeof(SL2CAP_Command) + _CommandLength; - - SL2CAP_Command* pCommand = (SL2CAP_Command*)&DataFrame[Offset]; Offset += sizeof(SL2CAP_Command); - pCommand->len = _CommandLength; - pCommand->ident = _Ident; - pCommand->code = _Code; - - memcpy(&DataFrame[Offset], _pCommandData, _CommandLength); - - LOG(WIIMOTE, " SendCommandToACL (answer)"); - LOG(WIIMOTE, " Ident: 0x%02x", _Ident); - LOG(WIIMOTE, " Code: 0x%02x", _Code); - - // send .... - m_pHost->SendACLFrame(GetConnectionHandle(), DataFrame, pHeader->Length + sizeof(SL2CAP_Header)); - - -/* // dump raw data - { - LOG(WIIMOTE, "m_pHost->SendACLFrame: 0x%x", GetConnectionHandle()); - std::string Temp; - for (u32 j=0; jLength + sizeof(SL2CAP_Header); j++) - { - char Buffer[128]; - sprintf(Buffer, "%02x ", DataFrame[j]); - Temp.append(Buffer); - } - LOG(WIIMOTE, " Data: %s", Temp.c_str()); - }*/ -} - -void CWII_IPC_HLE_WiiMote::Connect() -{ -// SendConnectionRequest(0x0040, 1); - - SendConnectionRequest(0x0040, HIDP_OUTPUT_CHANNEL); -// SendConnectionRequest(0x0041, HIDP_INPUT_CHANNEL); -} - -void CWII_IPC_HLE_WiiMote::SendConnectionRequest(u16 scid, u16 psm) -{ - // create the channel - SChannel& rChannel = m_Channel[scid]; - rChannel.PSM = psm; - rChannel.SCID = scid; - - l2cap_conn_req cr; - cr.psm = psm; - cr.scid = scid; - - LOG(WIIMOTE, " SendConnectionRequest()"); - LOG(WIIMOTE, " Psm: 0x%04x", cr.psm); - LOG(WIIMOTE, " Scid: 0x%04x", cr.scid); - - SendCommandToACL(L2CAP_CONN_REQ, L2CAP_CONN_REQ, sizeof(l2cap_conn_req), (u8*)&cr); -} - -void CWII_IPC_HLE_WiiMote::SendDisconnectRequest(u16 scid) -{ - // create the channel - SChannel& rChannel = m_Channel[scid]; - - l2cap_disconn_req cr; - cr.dcid = rChannel.DCID; - cr.scid = rChannel.SCID; - - LOG(WIIMOTE, " SendDisconnectionRequest()"); - LOG(WIIMOTE, " Dcid: 0x%04x", cr.dcid); - LOG(WIIMOTE, " Scid: 0x%04x", cr.scid); - - SendCommandToACL(L2CAP_DISCONN_REQ, L2CAP_DISCONN_REQ, sizeof(l2cap_disconn_req), (u8*)&cr); -} - -void CWII_IPC_HLE_WiiMote::SendConfigurationRequest(u16 scid, u16* MTU, u16* FlushTimeOut) -{ - _dbg_assert_(WIIMOTE, DoesChannelExist(scid)); - SChannel& rChannel = m_Channel[scid]; - - u8 Buffer[1024]; - int Offset = 0; - - l2cap_conf_req* cr = (l2cap_conf_req*)&Buffer[Offset]; - cr->dcid = rChannel.DCID; - cr->flags = 0; - Offset += sizeof(l2cap_conf_req); - - if (MTU != NULL) - { - SL2CAP_Options* pOptions = (SL2CAP_Options*)&Buffer[Offset]; - Offset += sizeof(SL2CAP_Options); - - pOptions->type = 1; - pOptions->length = 2; - - *(u16*)&Buffer[Offset] = *MTU; Offset += 2; - } - - if (FlushTimeOut != NULL) - { - SL2CAP_Options* pOptions = (SL2CAP_Options*)&Buffer[Offset]; - Offset += sizeof(SL2CAP_Options); - - pOptions->type = 2; - pOptions->length = 2; - - *(u16*)&Buffer[Offset] = *FlushTimeOut; Offset += 2; - } - - LOG(WIIMOTE, " SendConfigurationRequest()"); - LOG(WIIMOTE, " Dcid: 0x%04x", cr->dcid); - LOG(WIIMOTE, " Flags: 0x%04x", cr->flags); - - // hack: - static u8 ident = 99; - ident++; - SendCommandToACL(L2CAP_CONF_REQ, L2CAP_CONF_REQ, Offset, Buffer); -} - - void CWII_IPC_HLE_WiiMote::SignalChannel(u8* _pData, u32 _Size) { while (_Size >= sizeof(SL2CAP_Command)) @@ -462,27 +418,19 @@ void CWII_IPC_HLE_WiiMote::SignalChannel(u8* _pData, u32 _Size) } } -void CWII_IPC_HLE_WiiMote::SendL2capData(u16 scid, const void* _pData, u32 _Size) -{ - //allocate - u8 DataFrame[1024]; - u32 Offset = 0; - SL2CAP_Header* pHeader = (SL2CAP_Header*)DataFrame; - Offset += sizeof(SL2CAP_Header); - - _dbg_assert_(WIIMOTE, DoesChannelExist(scid)); - SChannel& rChannel = m_Channel[scid]; - - //assemble - pHeader->CID = rChannel.DCID; - pHeader->Length = _Size; - - memcpy(DataFrame + Offset, _pData, _Size); - Offset += _Size; +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// +// +// +// --- Send Commands To Device +// +// +// +// +// +///////////////////////////////////////////////////////////////////////////////////////////////// - //send - m_pHost->SendACLFrame(GetConnectionHandle(), DataFrame, Offset); -} void CWII_IPC_HLE_WiiMote::CommandConnectionReq(u8 _Ident, u8* _pData, u32 _Size) { @@ -508,47 +456,14 @@ void CWII_IPC_HLE_WiiMote::CommandConnectionReq(u8 _Ident, u8* _pData, u32 _Size Rsp.status = 0x00; SendCommandToACL(_Ident, L2CAP_CONN_RSP, sizeof(SL2CAP_ConnectionResponse), (u8*)&Rsp); -} -class CConfigurationResponse -{ -public: - CConfigurationResponse(u8 identifier, u16 scid, u16 flags, u16 result) - { - buffer[0] = 0x05; - buffer[1] = identifier; - *(u16*)&buffer[4] = scid; - *(u16*)&buffer[6] = flags; - *(u16*)&buffer[8] = result; - - length = 10; - - UpdateLen(); - } + // update state machine + if (rChannel.PSM == HIDP_CONTROL_CHANNEL) + m_HIDControlChannel_Connected = true; - void AddConfig(u8 type, u8 len, void* data) - { - buffer[length++] = type; - buffer[length++] = len; - memcpy(&buffer[length], data, len); - length += len; - - UpdateLen(); - } - - u16 getLen() { return length; } - u8* getBuffer() { return buffer; } - -private: - - void UpdateLen() - { - *(u16*)&buffer[2] = length; - } - - u8 buffer[1024]; - u16 length; -}; + if (rChannel.PSM == HID_INTERRUPT_CHANNEL) + m_HIDInterruptChannel_Connected = true; +} void CWII_IPC_HLE_WiiMote::CommandCofigurationReq(u8 _Ident, u8* _pData, u32 _Size) { @@ -567,7 +482,6 @@ void CWII_IPC_HLE_WiiMote::CommandCofigurationReq(u8 _Ident, u8* _pData, u32 _Si Offset += sizeof(SL2CAP_CommandConfigurationReq); - u8 TempBuffer[1024]; u32 RespLen = 0; @@ -578,9 +492,7 @@ void CWII_IPC_HLE_WiiMote::CommandCofigurationReq(u8 _Ident, u8* _pData, u32 _Si RespLen += sizeof(SL2CAP_CommandConfigurationResponse); - - - // prolly this code should be inside the channel... + // read configuration options while (Offset < _Size) { SL2CAP_Options* pOptions = (SL2CAP_Options*)&_pData[Offset]; @@ -618,11 +530,7 @@ void CWII_IPC_HLE_WiiMote::CommandCofigurationReq(u8 _Ident, u8* _pData, u32 _Si RespLen += OptionSize; } - SendCommandToACL(_Ident, L2CAP_CONF_RSP, RespLen, TempBuffer); - - // ugly - SendConfigurationRequest(Rsp->scid, &rChannel.MTU, &rChannel.FlushTimeOut); } void CWII_IPC_HLE_WiiMote::CommandConnectionResponse(u8 _Ident, u8* _pData, u32 _Size) @@ -644,12 +552,34 @@ void CWII_IPC_HLE_WiiMote::CommandConnectionResponse(u8 _Ident, u8* _pData, u32 SChannel& rChannel = m_Channel[rsp->scid]; rChannel.DCID = rsp->dcid; - static bool hack = true; - if (hack) - { - hack = false; - SendConnectionRequest(0x0041, HIDP_INPUT_CHANNEL); - } + // update state machine + if (rChannel.PSM == HIDP_CONTROL_CHANNEL) + m_HIDControlChannel_Connected = true; + + if (rChannel.PSM == HID_INTERRUPT_CHANNEL) + m_HIDInterruptChannel_Connected = true; +} + +void CWII_IPC_HLE_WiiMote::CommandCofigurationResponse(u8 _Ident, u8* _pData, u32 _Size) +{ + l2cap_conf_rsp* rsp = (l2cap_conf_rsp*)_pData; + + _dbg_assert_(WIIMOTE, _Size == sizeof(l2cap_conf_rsp)); + + LOG(WIIMOTE, " CommandCofigurationResponse"); + LOG(WIIMOTE, " SCID: 0x%04x", rsp->scid); + LOG(WIIMOTE, " Flags: 0x%04x", rsp->flags); + LOG(WIIMOTE, " Result: 0x%04x", rsp->result); + + _dbg_assert_(WIIMOTE, rsp->result == 0); + + // update state machine + SChannel& rChannel = m_Channel[rsp->scid]; + if (rChannel.PSM == HIDP_CONTROL_CHANNEL) + m_HIDControlChannel_Config = true; + + if (rChannel.PSM == HID_INTERRUPT_CHANNEL) + m_HIDInterruptChannel_Config = true; } void CWII_IPC_HLE_WiiMote::CommandDisconnectionReq(u8 _Ident, u8* _pData, u32 _Size) @@ -672,27 +602,110 @@ void CWII_IPC_HLE_WiiMote::CommandDisconnectionReq(u8 _Ident, u8* _pData, u32 _S SendCommandToACL(_Ident, L2CAP_DISCONN_RSP, sizeof(SL2CAP_CommandDisconnectionResponse), (u8*)&Rsp); } +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// +// +// +// --- Send Commands To Device +// +// +// +// +// +///////////////////////////////////////////////////////////////////////////////////////////////// -void CWII_IPC_HLE_WiiMote::CommandCofigurationResponse(u8 _Ident, u8* _pData, u32 _Size) { -#ifdef LOGGING - l2cap_conf_rsp* rsp = (l2cap_conf_rsp*)_pData; -#endif +void CWII_IPC_HLE_WiiMote::SendConnectionRequest(u16 scid, u16 psm) +{ + // create the channel + SChannel& rChannel = m_Channel[scid]; + rChannel.PSM = psm; + rChannel.SCID = scid; - _dbg_assert_(WIIMOTE, _Size == sizeof(l2cap_conf_rsp)); + l2cap_conn_req cr; + cr.psm = psm; + cr.scid = scid; - LOG(WIIMOTE, " CommandCofigurationResponse"); - LOG(WIIMOTE, " SCID: 0x%04x", rsp->scid); - LOG(WIIMOTE, " Flags: 0x%04x", rsp->flags); - LOG(WIIMOTE, " Result: 0x%04x", rsp->result); + LOG(WIIMOTE, " SendConnectionRequest()"); + LOG(WIIMOTE, " Psm: 0x%04x", cr.psm); + LOG(WIIMOTE, " Scid: 0x%04x", cr.scid); - _dbg_assert_(WIIMOTE, rsp->result == 0); + SendCommandToACL(L2CAP_CONN_REQ, L2CAP_CONN_REQ, sizeof(l2cap_conn_req), (u8*)&cr); } +void CWII_IPC_HLE_WiiMote::SendDisconnectRequest(u16 scid) +{ + // create the channel + SChannel& rChannel = m_Channel[scid]; + l2cap_disconn_req cr; + cr.dcid = rChannel.DCID; + cr.scid = rChannel.SCID; + LOG(WIIMOTE, " SendDisconnectionRequest()"); + LOG(WIIMOTE, " Dcid: 0x%04x", cr.dcid); + LOG(WIIMOTE, " Scid: 0x%04x", cr.scid); + SendCommandToACL(L2CAP_DISCONN_REQ, L2CAP_DISCONN_REQ, sizeof(l2cap_disconn_req), (u8*)&cr); +} + +void CWII_IPC_HLE_WiiMote::SendConfigurationRequest(u16 scid, u16* MTU, u16* FlushTimeOut) +{ + _dbg_assert_(WIIMOTE, DoesChannelExist(scid)); + SChannel& rChannel = m_Channel[scid]; + + u8 Buffer[1024]; + int Offset = 0; + + l2cap_conf_req* cr = (l2cap_conf_req*)&Buffer[Offset]; + cr->dcid = rChannel.DCID; + cr->flags = 0; + Offset += sizeof(l2cap_conf_req); + + if (MTU != NULL) + { + SL2CAP_Options* pOptions = (SL2CAP_Options*)&Buffer[Offset]; + Offset += sizeof(SL2CAP_Options); + + pOptions->type = 1; + pOptions->length = 2; + + *(u16*)&Buffer[Offset] = *MTU; Offset += 2; + } + + if (FlushTimeOut != NULL) + { + SL2CAP_Options* pOptions = (SL2CAP_Options*)&Buffer[Offset]; + Offset += sizeof(SL2CAP_Options); + + pOptions->type = 2; + pOptions->length = 2; + + *(u16*)&Buffer[Offset] = *FlushTimeOut; Offset += 2; + } + + LOG(WIIMOTE, " SendConfigurationRequest()"); + LOG(WIIMOTE, " Dcid: 0x%04x", cr->dcid); + LOG(WIIMOTE, " Flags: 0x%04x", cr->flags); + + // hack: + static u8 ident = 99; + ident++; + SendCommandToACL(L2CAP_CONF_REQ, L2CAP_CONF_REQ, Offset, Buffer); +} +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// +// +// +// --- SDP +// +// +// +// +// ///////////////////////////////////////////////////////////////////////////////////////////////// #define SDP_UINT8 0x08 @@ -701,9 +714,6 @@ void CWII_IPC_HLE_WiiMote::CommandCofigurationResponse(u8 _Ident, u8* _pData, u3 #define SDP_SEQ8 0x35 #define SDP_SEQ16 0x36 - - - void CWII_IPC_HLE_WiiMote::SDPSendServiceSearchResponse(u16 cid, u16 TransactionID, u8* pServiceSearchPattern, u16 MaximumServiceRecordCount) { // verify block... we hanlde search pattern for HID service only @@ -782,56 +792,6 @@ int ParseAttribList(u8* pAttribIDList, u16& _startID, u16& _endID) } -u8 g_Buffer[2048]; -u32 g_Size = 0; - - -u32 GenerateAttribBuffer(u16 _startID, u16 _endID) -{ - CBigEndianBuffer buffer(g_Buffer); - int Offset = 0; - - buffer.Write8(Offset, SDP_SEQ16); Offset++; - - Offset += 2; // save some memory for seq size - - // walk through the table - const CAttribTable& rAttribTable = GetAttribTable(); - CAttribTable::const_iterator itr = rAttribTable.begin(); - - u32 sequenceSize = 0; - while(itr != rAttribTable.end()) - { - const SAttrib& rAttrib = *itr; - - if ((rAttrib.ID >= _startID) && (rAttrib.ID <= _endID)) - { - _dbg_assert_(WIIMOTE, rAttrib.size <= 230); - - // ATTRIB TYPE ID - buffer.Write8(Offset, SDP_UINT16); Offset ++; - buffer.Write16(Offset, rAttrib.ID); Offset += 2; - sequenceSize += 3; - - // RAW ATTRIB DATA - memcpy(buffer.GetPointer(Offset), rAttrib.pData, rAttrib.size); - Offset += rAttrib.size; - - sequenceSize += rAttrib.size; - } - itr++; - } - - buffer.Write16(1, sequenceSize); - - g_Size = Offset; - - - - return g_Size; -} - - void CWII_IPC_HLE_WiiMote::SDPSendServiceAttributeResponse(u16 cid, u16 TransactionID, u32 ServiceHandle, u16 startAttrID, u16 endAttrID, u16 MaximumAttributeByteCount, u8* pContinuationState) @@ -862,52 +822,15 @@ void CWII_IPC_HLE_WiiMote::SDPSendServiceAttributeResponse(u16 cid, u16 Transact memcpy(buffer.GetPointer(Offset), pPacket, packetSize); Offset += packetSize; - - pHeader->Length = Offset - sizeof(SL2CAP_Header); m_pHost->SendACLFrame(GetConnectionHandle(), DataFrame, pHeader->Length + sizeof(SL2CAP_Header)); - -/* // dump raw data - { - LOG(WIIMOTE, "test response: 0x%x", GetConnectionHandle()); - for (u32 j=0; jLength + sizeof(SL2CAP_Header);) - { - std::string Temp; - for (int i=0; i<16; i++) - { - char Buffer[128]; - sprintf(Buffer, "%02x ", DataFrame[j++]); - Temp.append(Buffer); - - if (j >= pHeader->Length + sizeof(SL2CAP_Header)) - break; - } - - LOG(WIIMOTE, " Data: %s", Temp.c_str()); - } - - }*/ - - +// Debugger::PrintDataBuffer(LogTypes::WIIMOTE, DataFrame, pHeader->Length + sizeof(SL2CAP_Header), "test response: "); } void CWII_IPC_HLE_WiiMote::HandleSDP(u16 cid, u8* _pData, u32 _Size) { -/* // dump raw data - { - LOG(WIIMOTE, "HandleSDP: 0x%x", GetConnectionHandle()); - std::string Temp; - for (u32 j=0; j<_Size; j++) - { - char Buffer[128]; - sprintf(Buffer, "%02x ", _pData[j]); - Temp.append(Buffer); - } - LOG(WIIMOTE, " Data: %s", Temp.c_str()); - } -*/ - + // Debugger::PrintDataBuffer(LogTypes::WIIMOTE, _pData, _Size, "HandleSDP: "); CBigEndianBuffer buffer(_pData); @@ -953,4 +876,86 @@ void CWII_IPC_HLE_WiiMote::HandleSDP(u16 cid, u8* _pData, u32 _Size) PanicAlert("Unknown SDP command %x", _pData[0]); break; } -} \ No newline at end of file +} + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// +// +// +// --- Data Send Functions +// +// +// +// +// +///////////////////////////////////////////////////////////////////////////////////////////////// + + +void CWII_IPC_HLE_WiiMote::SendCommandToACL(u8 _Ident, u8 _Code, u8 _CommandLength, u8* _pCommandData) +{ + u8 DataFrame[1024]; + u32 Offset = 0; + + SL2CAP_Header* pHeader = (SL2CAP_Header*)&DataFrame[Offset]; Offset += sizeof(SL2CAP_Header); + pHeader->CID = 0x0001; + pHeader->Length = sizeof(SL2CAP_Command) + _CommandLength; + + SL2CAP_Command* pCommand = (SL2CAP_Command*)&DataFrame[Offset]; Offset += sizeof(SL2CAP_Command); + pCommand->len = _CommandLength; + pCommand->ident = _Ident; + pCommand->code = _Code; + + memcpy(&DataFrame[Offset], _pCommandData, _CommandLength); + + LOG(WIIMOTE, " SendCommandToACL (answer)"); + LOG(WIIMOTE, " Ident: 0x%02x", _Ident); + LOG(WIIMOTE, " Code: 0x%02x", _Code); + + // send .... + m_pHost->SendACLFrame(GetConnectionHandle(), DataFrame, pHeader->Length + sizeof(SL2CAP_Header)); + + // Debugger::PrintDataBuffer(LogTypes::WIIMOTE, DataFrame, pHeader->Length + sizeof(SL2CAP_Header), "m_pHost->SendACLFrame: "); +} + +void CWII_IPC_HLE_WiiMote::SendL2capData(u16 scid, const void* _pData, u32 _Size) +{ + //allocate + u8 DataFrame[1024]; + u32 Offset = 0; + SL2CAP_Header* pHeader = (SL2CAP_Header*)DataFrame; + Offset += sizeof(SL2CAP_Header); + + _dbg_assert_(WIIMOTE, DoesChannelExist(scid)); + SChannel& rChannel = m_Channel[scid]; + + //assemble + pHeader->CID = rChannel.DCID; + pHeader->Length = _Size; + + memcpy(DataFrame + Offset, _pData, _Size); + Offset += _Size; + + //send + m_pHost->SendACLFrame(GetConnectionHandle(), DataFrame, Offset); +} + + +namespace Core +{ + void Callback_WiimoteInput(u16 _channelID, const void* _pData, u32 _Size) + { + const u8* pData = (const u8*)_pData; + LOG(WIIMOTE, "Callback_WiimoteInput: 0x%x", _channelID); + std::string Temp; + for (u32 j=0; j<_Size; j++) + { + char Buffer[128]; + sprintf(Buffer, "%02x ", pData[j]); + Temp.append(Buffer); + } + LOG(WIIMOTE, " Data: %s", Temp.c_str()); + + s_Usb->m_WiiMotes[0].SendL2capData(_channelID, _pData, _Size); + } +} \ No newline at end of file diff --git a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.h b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.h index 78d5102475..0fac28cc4e 100644 --- a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.h +++ b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.h @@ -18,6 +18,7 @@ #define _WII_IPC_HLE_WII_MOTE_ #include +#include "hci.h" class CWII_IPC_HLE_Device_usb_oh1_57e_305; @@ -71,6 +72,9 @@ private: }; + + + class CWII_IPC_HLE_WiiMote { public: @@ -84,12 +88,14 @@ public: // ugly Host handling.... // we really have to clean all this code + bool Update(); bool IsConnected() const { return m_Connected; } void EventConnectionAccepted(); void EventDisconnect(); bool EventPagingChanged(u8 _pageMode); + void EventCommandWriteLinkPolicy(); const bdaddr_t& GetBD() const { return m_BD; } @@ -109,15 +115,26 @@ public: void SendACLFrame(u8* _pData, u32 _Size); //to wiimote - void Connect(); - void SendL2capData(u16 scid, const void* _pData, u32 _Size); //from wiimote const u8* GetLinkKey() const { return m_LinkKey; } private: + // state machine bool m_Connected; + bool m_HIDControlChannel_Connected; + bool m_HIDControlChannel_ConnectedWait; + bool m_HIDControlChannel_Config; + bool m_HIDControlChannelHost_Config; + bool m_HIDControlChannel_ConfigWait; + bool m_HIDInterruptChannel_Connected; + bool m_HIDInterruptChannel_ConnectedWait; + bool m_HIDInterruptChannel_Config; + bool m_HIDInterruptChannelHost_Config; + bool m_HIDInterruptChannel_ConfigWait; + + // STATE_TO_SAVE bdaddr_t m_BD; @@ -160,9 +177,9 @@ private: void SignalChannel(u8* _pData, u32 _Size); - void SendConnectionRequest(u16 scid, u16 psm); - void SendConfigurationRequest(u16 scid, u16* MTU, u16* FlushTimeOut); - void SendDisconnectRequest(u16 scid); + void SendConnectionRequest(u16 _SCID, u16 _PSM); + void SendConfigurationRequest(u16 _SCID, u16* _pMTU = NULL, u16* _pFlushTimeOut = NULL); + void SendDisconnectRequest(u16 _SCID); void CommandConnectionReq(u8 _Ident, u8* _pData, u32 _Size); void CommandCofigurationReq(u8 _Ident, u8* _pData, u32 _Size); @@ -176,14 +193,14 @@ private: // // should be inside the plugin // - void HandleSDP(u16 cid, u8* _pData, u32 _Size); - void SDPSendServiceSearchResponse(u16 cid, u16 TransactionID, u8* pServiceSearchPattern, u16 MaximumServiceRecordCount); + void HandleSDP(u16 _SCID, u8* _pData, u32 _Size); + void SDPSendServiceSearchResponse(u16 _SCID, u16 _TransactionID, u8* _pServiceSearchPattern, u16 _MaximumServiceRecordCount); - void SDPSendServiceAttributeResponse(u16 cid, u16 TransactionID, u32 ServiceHandle, - u16 startAttrID, u16 endAttrID, - u16 MaximumAttributeByteCount, u8* pContinuationState); + void SDPSendServiceAttributeResponse(u16 _SCID, u16 TransactionID, u32 _ServiceHandle, + u16 _StartAttrID, u16 _EndAttrID, + u16 _MaximumAttributeByteCount, u8* _pContinuationState); - u16 AddAttribToList(int attribID, u8* pBuffer); + u16 AddAttribToList(int _AttribID, u8* _pBuffer); }; #endif diff --git a/Source/Core/Core/Src/Plugins/Plugin_Wiimote.cpp b/Source/Core/Core/Src/Plugins/Plugin_Wiimote.cpp index af4060f644..8adff3313e 100644 --- a/Source/Core/Core/Src/Plugins/Plugin_Wiimote.cpp +++ b/Source/Core/Core/Src/Plugins/Plugin_Wiimote.cpp @@ -27,8 +27,8 @@ namespace PluginWiimote TDllConfig DllConfig = 0; TWiimote_Initialize Wiimote_Initialize = 0; TWiimote_Shutdown Wiimote_Shutdown = 0; - TWiimote_Output Wiimote_Output = 0; - TWiimote_Input Wiimote_Input = 0; + TWiimote_Output Wiimote_ControlChannel = 0; + TWiimote_Input Wiimote_InterruptChannel = 0; TWiimote_Update Wiimote_Update = 0; TWiimote_GetAttachedControllers Wiimote_GetAttachedControllers = 0; TWiimote_DoState Wiimote_DoState = 0; @@ -50,8 +50,8 @@ namespace PluginWiimote DllConfig = 0; Wiimote_Initialize = 0; Wiimote_Shutdown = 0; - Wiimote_Output = 0; - Wiimote_Input = 0; + Wiimote_ControlChannel = 0; + Wiimote_InterruptChannel = 0; Wiimote_Update = 0; Wiimote_GetAttachedControllers = 0; Wiimote_DoState = 0; @@ -66,8 +66,8 @@ namespace PluginWiimote DllConfig = reinterpret_cast (plugin.Get("DllConfig")); Wiimote_Initialize = reinterpret_cast (plugin.Get("Wiimote_Initialize")); Wiimote_Shutdown = reinterpret_cast (plugin.Get("Wiimote_Shutdown")); - Wiimote_Output = reinterpret_cast (plugin.Get("Wiimote_Output")); - Wiimote_Input = reinterpret_cast (plugin.Get("Wiimote_Input")); + Wiimote_ControlChannel = reinterpret_cast (plugin.Get("Wiimote_ControlChannel")); + Wiimote_InterruptChannel = reinterpret_cast (plugin.Get("Wiimote_InterruptChannel")); Wiimote_Update = reinterpret_cast (plugin.Get("Wiimote_Update")); Wiimote_GetAttachedControllers = reinterpret_cast (plugin.Get("Wiimote_GetAttachedControllers")); Wiimote_DoState = reinterpret_cast (plugin.Get("Wiimote_DoState")); @@ -76,16 +76,16 @@ namespace PluginWiimote LOG(MASTER_LOG, "%s: 0x%p", "DllConfig", DllConfig); LOG(MASTER_LOG, "%s: 0x%p", "Wiimote_Initialize", Wiimote_Initialize); LOG(MASTER_LOG, "%s: 0x%p", "Wiimote_Shutdown", Wiimote_Shutdown); - LOG(MASTER_LOG, "%s: 0x%p", "Wiimote_Output", Wiimote_Output); - LOG(MASTER_LOG, "%s: 0x%p", "Wiimote_Input", Wiimote_Input); + LOG(MASTER_LOG, "%s: 0x%p", "Wiimote_ControlChannel", Wiimote_ControlChannel); + LOG(MASTER_LOG, "%s: 0x%p", "Wiimote_InterruptChannel", Wiimote_InterruptChannel); LOG(MASTER_LOG, "%s: 0x%p", "Wiimote_Update", Wiimote_Update); LOG(MASTER_LOG, "%s: 0x%p", "Wiimote_GetAttachedControllers", Wiimote_GetAttachedControllers); LOG(MASTER_LOG, "%s: 0x%p", "Wiimote_DoState", Wiimote_DoState); if ((GetDllInfo != 0) && (Wiimote_Initialize != 0) && (Wiimote_Shutdown != 0) && - (Wiimote_Output != 0) && - (Wiimote_Input != 0) && + (Wiimote_ControlChannel != 0) && + (Wiimote_InterruptChannel != 0) && (Wiimote_Update != 0) && (Wiimote_GetAttachedControllers != 0) && (Wiimote_DoState != 0)) diff --git a/Source/Core/Core/Src/Plugins/Plugin_Wiimote.h b/Source/Core/Core/Src/Plugins/Plugin_Wiimote.h index 3e52f88514..fff6dea3fb 100644 --- a/Source/Core/Core/Src/Plugins/Plugin_Wiimote.h +++ b/Source/Core/Core/Src/Plugins/Plugin_Wiimote.h @@ -43,8 +43,8 @@ extern TGetDllInfo GetDllInfo; extern TDllConfig DllConfig; extern TWiimote_Initialize Wiimote_Initialize; extern TWiimote_Shutdown Wiimote_Shutdown; -extern TWiimote_Output Wiimote_Output; -extern TWiimote_Input Wiimote_Input; +extern TWiimote_Output Wiimote_ControlChannel; +extern TWiimote_Input Wiimote_InterruptChannel; extern TWiimote_Update Wiimote_Update; extern TWiimote_GetAttachedControllers Wiimote_GetAttachedControllers; extern TWiimote_DoState Wiimote_DoState; -- cgit v1.2.3