Pass the packet response to completion callbacks.
This commit is contained in:
parent
879df7152c
commit
48c75ba224
@ -103,13 +103,13 @@ bool SMX::SMXDevice::IsConnectedLocked() const
|
||||
return m_pConnection->IsConnectedWithDeviceInfo() && m_bHaveConfig;
|
||||
}
|
||||
|
||||
void SMX::SMXDevice::SendCommand(string cmd, function<void()> pComplete)
|
||||
void SMX::SMXDevice::SendCommand(string cmd, function<void(string response)> pComplete)
|
||||
{
|
||||
LockMutex Lock(m_Lock);
|
||||
SendCommandLocked(cmd, pComplete);
|
||||
}
|
||||
|
||||
void SMX::SMXDevice::SendCommandLocked(string cmd, function<void()> pComplete)
|
||||
void SMX::SMXDevice::SendCommandLocked(string cmd, function<void(string response)> pComplete)
|
||||
{
|
||||
m_Lock.AssertLockedByCurrentThread();
|
||||
|
||||
@ -117,7 +117,7 @@ void SMX::SMXDevice::SendCommandLocked(string cmd, function<void()> pComplete)
|
||||
{
|
||||
// If we're not connected, just call pComplete.
|
||||
if(pComplete)
|
||||
pComplete();
|
||||
pComplete("");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -200,7 +200,7 @@ void SMX::SMXDevice::FactoryReset()
|
||||
// Send a factory reset command, and then read the new configuration.
|
||||
LockMutex Lock(m_Lock);
|
||||
SendCommandLocked("f\n");
|
||||
SendCommandLocked("g\n", [&] {
|
||||
SendCommandLocked("g\n", [&](string response) {
|
||||
// We now have the new configuration.
|
||||
m_Lock.AssertLockedByCurrentThread();
|
||||
CallUpdateCallback(SMXUpdateCallback_FactoryResetCommandComplete);
|
||||
@ -319,7 +319,7 @@ void SMX::SMXDevice::SendConfig()
|
||||
// Don't send another config packet until this one finishes, so if we get a bunch of
|
||||
// SetConfig calls quickly we won't spam the device, which can get slow.
|
||||
m_bSendingConfig = true;
|
||||
SendCommandLocked(sData, [&] {
|
||||
SendCommandLocked(sData, [&](string response) {
|
||||
m_bSendingConfig = false;
|
||||
});
|
||||
m_bSendConfig = false;
|
||||
@ -336,7 +336,7 @@ void SMX::SMXDevice::SendConfig()
|
||||
|
||||
// After we write the configuration, read back the updated configuration to
|
||||
// verify it.
|
||||
SendCommandLocked("g\n", [this]() {
|
||||
SendCommandLocked("g\n", [this](string response) {
|
||||
m_bWaitingForConfigResponse = false;
|
||||
});
|
||||
}
|
||||
|
@ -41,8 +41,8 @@ public:
|
||||
bool IsConnected() const;
|
||||
|
||||
// Send a raw command.
|
||||
void SendCommand(string sCmd, function<void()> pComplete=nullptr);
|
||||
void SendCommandLocked(string sCmd, function<void()> pComplete=nullptr);
|
||||
void SendCommand(string sCmd, function<void(string response)> pComplete=nullptr);
|
||||
void SendCommandLocked(string sCmd, function<void(string response)> pComplete=nullptr);
|
||||
|
||||
// Get basic info about the device.
|
||||
void GetInfo(SMXInfo &info);
|
||||
|
@ -41,7 +41,7 @@ bool SMX::SMXDeviceConnection::Open(shared_ptr<AutoCloseHandle> DeviceHandle, ws
|
||||
BeginAsyncRead(sError);
|
||||
|
||||
// Request device info.
|
||||
RequestDeviceInfo([&] {
|
||||
RequestDeviceInfo([&](string response) {
|
||||
Log(ssprintf("Received device info. Master version: %i, P%i", m_DeviceInfo.m_iFirmwareVersion, m_DeviceInfo.m_bP2+1));
|
||||
m_bGotInfo = true;
|
||||
});
|
||||
@ -59,14 +59,14 @@ void SMX::SMXDeviceConnection::Close()
|
||||
// If we're being closed while a command was in progress, call its completion
|
||||
// callback, so it's guaranteed to always be called.
|
||||
if(m_pCurrentCommand && m_pCurrentCommand->m_pComplete)
|
||||
m_pCurrentCommand->m_pComplete();
|
||||
m_pCurrentCommand->m_pComplete("");
|
||||
|
||||
// If any commands were queued with completion callbacks, call their completion
|
||||
// callbacks.
|
||||
for(auto &pendingCommand: m_aPendingCommands)
|
||||
{
|
||||
if(pendingCommand->m_pComplete)
|
||||
pendingCommand->m_pComplete();
|
||||
pendingCommand->m_pComplete("");
|
||||
}
|
||||
|
||||
m_hDevice.reset();
|
||||
@ -205,7 +205,7 @@ void SMX::SMXDeviceConnection::HandleUsbPacket(const string &buf)
|
||||
memcpy(m_DeviceInfo.m_Serial, sHexSerial.c_str(), 33);
|
||||
|
||||
if(m_pCurrentCommand->m_pComplete)
|
||||
m_pCurrentCommand->m_pComplete();
|
||||
m_pCurrentCommand->m_pComplete(sPacket);
|
||||
m_pCurrentCommand = nullptr;
|
||||
|
||||
break;
|
||||
@ -230,22 +230,24 @@ void SMX::SMXDeviceConnection::HandleUsbPacket(const string &buf)
|
||||
|
||||
m_sCurrentReadBuffer.append(sPacket);
|
||||
|
||||
if(cmd & PACKET_FLAG_END_OF_COMMAND)
|
||||
{
|
||||
if(!m_sCurrentReadBuffer.empty())
|
||||
m_sReadBuffers.push_back(m_sCurrentReadBuffer);
|
||||
m_sCurrentReadBuffer.clear();
|
||||
}
|
||||
|
||||
// Note that if PACKET_FLAG_HOST_CMD_FINISHED is set, PACKET_FLAG_END_OF_COMMAND
|
||||
// will always also be set.
|
||||
if(cmd & PACKET_FLAG_HOST_CMD_FINISHED)
|
||||
{
|
||||
// This tells us that a command we wrote to the device has finished executing, and
|
||||
// it's safe to start writing another.
|
||||
if(m_pCurrentCommand && m_pCurrentCommand->m_pComplete)
|
||||
m_pCurrentCommand->m_pComplete();
|
||||
m_pCurrentCommand->m_pComplete(m_sCurrentReadBuffer);
|
||||
m_pCurrentCommand = nullptr;
|
||||
}
|
||||
|
||||
if(cmd & PACKET_FLAG_END_OF_COMMAND)
|
||||
{
|
||||
if(!m_sCurrentReadBuffer.empty())
|
||||
m_sReadBuffers.push_back(m_sCurrentReadBuffer);
|
||||
m_sCurrentReadBuffer.clear();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@ -341,7 +343,7 @@ void SMX::SMXDeviceConnection::CheckWrites(wstring &error)
|
||||
// Request device info. This is the same as sending an 'i' command, but we can send it safely
|
||||
// at any time, even if another application is talking to the device, so we can do this during
|
||||
// enumeration.
|
||||
void SMX::SMXDeviceConnection::RequestDeviceInfo(function<void()> pComplete)
|
||||
void SMX::SMXDeviceConnection::RequestDeviceInfo(function<void(string response)> pComplete)
|
||||
{
|
||||
shared_ptr<PendingCommand> pPendingCommand = make_shared<PendingCommand>();
|
||||
pPendingCommand->m_pComplete = pComplete;
|
||||
@ -362,7 +364,7 @@ void SMX::SMXDeviceConnection::RequestDeviceInfo(function<void()> pComplete)
|
||||
m_aPendingCommands.push_back(pPendingCommand);
|
||||
}
|
||||
|
||||
void SMX::SMXDeviceConnection::SendCommand(const string &cmd, function<void()> pComplete)
|
||||
void SMX::SMXDeviceConnection::SendCommand(const string &cmd, function<void(string response)> pComplete)
|
||||
{
|
||||
shared_ptr<PendingCommand> pPendingCommand = make_shared<PendingCommand>();
|
||||
pPendingCommand->m_pComplete = pComplete;
|
||||
|
@ -60,12 +60,12 @@ public:
|
||||
|
||||
// Send a command. This must be a single complete command: partial writes and multiple
|
||||
// commands in a call aren't allowed.
|
||||
void SendCommand(const string &cmd, function<void()> pComplete=nullptr);
|
||||
void SendCommand(const string &cmd, function<void(string response)> pComplete=nullptr);
|
||||
|
||||
uint16_t GetInputState() const { return m_iInputState; }
|
||||
|
||||
private:
|
||||
void RequestDeviceInfo(function<void()> pComplete = nullptr);
|
||||
void RequestDeviceInfo(function<void(string response)> pComplete = nullptr);
|
||||
|
||||
void CheckReads(wstring &error);
|
||||
void BeginAsyncRead(wstring &error);
|
||||
@ -95,8 +95,8 @@ private:
|
||||
list<shared_ptr<PendingCommandPacket>> m_Packets;
|
||||
|
||||
// This is only called if m_bWaitForResponse if true. Otherwise, we send the command
|
||||
// and forget about it.
|
||||
function<void()> m_pComplete;
|
||||
// and forget about it. If the command has a response, it'll be in buf.
|
||||
function<void(string response)> m_pComplete;
|
||||
|
||||
// If true, once we send this command we won't send any other commands until we get
|
||||
// a response.
|
||||
|
@ -409,7 +409,7 @@ void SMX_LightsUpload_BeginUpload(int pad, SMX_LightsUploadCallback pCallback, v
|
||||
for(int i = 0; i < asCommands.size(); ++i)
|
||||
{
|
||||
const string &sCommand = asCommands[i];
|
||||
pDevice->SendCommand(sCommand, [i, iTotalCommands, pCallback, pUser]() {
|
||||
pDevice->SendCommand(sCommand, [i, iTotalCommands, pCallback, pUser](string response) {
|
||||
// Command #i has finished being sent.
|
||||
//
|
||||
// If this isn't the last command, make sure progress isn't 100.
|
||||
|
Loading…
x
Reference in New Issue
Block a user