Skip to content

Commit

Permalink
Wiimote: use inheritance instead of #ifdef
Browse files Browse the repository at this point in the history
  • Loading branch information
Tilka committed Dec 4, 2014
1 parent 02dae1d commit 74add95
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 300 deletions.
43 changes: 1 addition & 42 deletions Source/Core/Core/HW/WiimoteReal/IODummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ namespace WiimoteReal
{

WiimoteScanner::WiimoteScanner()
{
return;
}
{}

WiimoteScanner::~WiimoteScanner()
{}
Expand All @@ -30,43 +28,4 @@ bool WiimoteScanner::IsReady() const
return false;
}

void Wiimote::InitInternal()
{}

void Wiimote::TeardownInternal()
{}

bool Wiimote::ConnectInternal()
{
return 0;
}

void Wiimote::DisconnectInternal()
{
return;
}

bool Wiimote::IsConnected() const
{
return false;
}

void Wiimote::IOWakeup()
{}

int Wiimote::IORead(u8* buf)
{
return 0;
}

int Wiimote::IOWrite(const u8* buf, size_t len)
{
return 0;
}

void Wiimote::EnablePowerAssertionInternal()
{}
void Wiimote::DisablePowerAssertionInternal()
{}

};
109 changes: 63 additions & 46 deletions Source/Core/Core/HW/WiimoteReal/IONix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@
namespace WiimoteReal
{

class WiimoteLinux : public Wiimote
{
public:
WiimoteLinux(bdaddr_t bdaddr);
~WiimoteLinux() override;

protected:
bool ConnectInternal() override;
void DisconnectInternal() override;
bool IsConnected() const override;
void IOWakeup() override;
int IORead(u8* buf) override;
int IOWrite(u8 const* buf, size_t len) override;

private:
bdaddr_t m_bdaddr; // Bluetooth address
int m_cmd_sock; // Command socket
int m_int_sock; // Interrupt socket
int m_wakeup_pipe_w;
int m_wakeup_pipe_r;
};

WiimoteScanner::WiimoteScanner()
: m_want_wiimotes()
, device_id(-1)
Expand Down Expand Up @@ -102,8 +124,7 @@ void WiimoteScanner::FindWiimotes(std::vector<Wiimote*> & found_wiimotes, Wiimot
char bdaddr_str[18] = {};
ba2str(&scan_infos[i].bdaddr, bdaddr_str);

auto* const wm = new Wiimote;
wm->bdaddr = scan_infos[i].bdaddr;
Wiimote* wm = new WiimoteLinux(scan_infos[i].bdaddr);
if (IsBalanceBoardName(name))
{
found_board = wm;
Expand All @@ -120,80 +141,81 @@ void WiimoteScanner::FindWiimotes(std::vector<Wiimote*> & found_wiimotes, Wiimot

}

void Wiimote::InitInternal()
WiimoteLinux::WiimoteLinux(bdaddr_t bdaddr) : Wiimote(), m_bdaddr(bdaddr)
{
cmd_sock = -1;
int_sock = -1;
m_cmd_sock = -1;
m_int_sock = -1;

int fds[2];
if (pipe(fds))
{
ERROR_LOG(WIIMOTE, "pipe failed");
abort();
}
wakeup_pipe_w = fds[1];
wakeup_pipe_r = fds[0];
bdaddr = (bdaddr_t){{0, 0, 0, 0, 0, 0}};
m_wakeup_pipe_w = fds[1];
m_wakeup_pipe_r = fds[0];
m_bdaddr = (bdaddr_t){{0, 0, 0, 0, 0, 0}};
}

void Wiimote::TeardownInternal()
WiimoteLinux::~WiimoteLinux()
{
close(wakeup_pipe_w);
close(wakeup_pipe_r);
Shutdown();
close(m_wakeup_pipe_w);
close(m_wakeup_pipe_r);
}

// Connect to a wiimote with a known address.
bool Wiimote::ConnectInternal()
bool WiimoteLinux::ConnectInternal()
{
sockaddr_l2 addr;
addr.l2_family = AF_BLUETOOTH;
addr.l2_bdaddr = bdaddr;
addr.l2_bdaddr = m_bdaddr;
addr.l2_cid = 0;

// Output channel
addr.l2_psm = htobs(WM_OUTPUT_CHANNEL);
if ((cmd_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 ||
connect(cmd_sock, (sockaddr*)&addr, sizeof(addr)) < 0)
if ((m_cmd_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 ||
connect(m_cmd_sock, (sockaddr*)&addr, sizeof(addr)) < 0)
{
DEBUG_LOG(WIIMOTE, "Unable to open output socket to wiimote.");
close(cmd_sock);
cmd_sock = -1;
close(m_cmd_sock);
m_cmd_sock = -1;
return false;
}

// Input channel
addr.l2_psm = htobs(WM_INPUT_CHANNEL);
if ((int_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 ||
connect(int_sock, (sockaddr*)&addr, sizeof(addr)) < 0)
if ((m_int_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 ||
connect(m_int_sock, (sockaddr*)&addr, sizeof(addr)) < 0)
{
DEBUG_LOG(WIIMOTE, "Unable to open input socket from wiimote.");
close(int_sock);
close(cmd_sock);
int_sock = cmd_sock = -1;
close(m_int_sock);
close(m_cmd_sock);
m_int_sock = m_cmd_sock = -1;
return false;
}

return true;
}

void Wiimote::DisconnectInternal()
void WiimoteLinux::DisconnectInternal()
{
close(cmd_sock);
close(int_sock);
close(m_cmd_sock);
close(m_int_sock);

cmd_sock = -1;
int_sock = -1;
m_cmd_sock = -1;
m_int_sock = -1;
}

bool Wiimote::IsConnected() const
bool WiimoteLinux::IsConnected() const
{
return cmd_sock != -1;// && int_sock != -1;
return m_cmd_sock != -1;// && int_sock != -1;
}

void Wiimote::IOWakeup()
void WiimoteLinux::IOWakeup()
{
char c = 0;
if (write(wakeup_pipe_w, &c, 1) != 1)
if (write(m_wakeup_pipe_w, &c, 1) != 1)
{
ERROR_LOG(WIIMOTE, "Unable to write to wakeup pipe.");
}
Expand All @@ -202,36 +224,36 @@ void Wiimote::IOWakeup()
// positive = read packet
// negative = didn't read packet
// zero = error
int Wiimote::IORead(u8* buf)
int WiimoteLinux::IORead(u8* buf)
{
// Block select for 1/2000th of a second

fd_set fds;
FD_ZERO(&fds);
FD_SET(int_sock, &fds);
FD_SET(wakeup_pipe_r, &fds);
FD_SET(m_int_sock, &fds);
FD_SET(m_wakeup_pipe_r, &fds);

if (select(int_sock + 1, &fds, nullptr, nullptr, nullptr) == -1)
if (select(m_int_sock + 1, &fds, nullptr, nullptr, nullptr) == -1)
{
ERROR_LOG(WIIMOTE, "Unable to select wiimote %i input socket.", index + 1);
return -1;
}

if (FD_ISSET(wakeup_pipe_r, &fds))
if (FD_ISSET(m_wakeup_pipe_r, &fds))
{
char c;
if (read(wakeup_pipe_r, &c, 1) != 1)
if (read(m_wakeup_pipe_r, &c, 1) != 1)
{
ERROR_LOG(WIIMOTE, "Unable to read from wakeup pipe.");
}
return -1;
}

if (!FD_ISSET(int_sock, &fds))
if (!FD_ISSET(m_int_sock, &fds))
return -1;

// Read the pending message into the buffer
int r = read(int_sock, buf, MAX_PAYLOAD);
int r = read(m_int_sock, buf, MAX_PAYLOAD);
if (r == -1)
{
// Error reading data
Expand All @@ -250,14 +272,9 @@ int Wiimote::IORead(u8* buf)
return r;
}

int Wiimote::IOWrite(u8 const* buf, size_t len)
int WiimoteLinux::IOWrite(u8 const* buf, size_t len)
{
return write(int_sock, buf, (int)len);
return write(m_int_sock, buf, (int)len);
}

void Wiimote::EnablePowerAssertionInternal()
{}
void Wiimote::DisablePowerAssertionInternal()
{}

}; // WiimoteReal

0 comments on commit 74add95

Please sign in to comment.