diff --git a/api/icsneolegacy/icsneolegacyextra.cpp b/api/icsneolegacy/icsneolegacyextra.cpp index 0c33b29..e92305e 100644 --- a/api/icsneolegacy/icsneolegacyextra.cpp +++ b/api/icsneolegacy/icsneolegacyextra.cpp @@ -24,5 +24,5 @@ int LegacyDLLExport icsneoWaitForRxMessagesWithTimeOut(void* hObject, unsigned i neodevice_t* device = (neodevice_t*)hObject; if(device->device->getCurrentMessageCount() != 0) return true; - return bool(device->device->com->waitForMessageSync(MessageFilter(), std::chrono::milliseconds(iTimeOut))); + return bool(device->device->com->waitForMessageSync({}, std::chrono::milliseconds(iTimeOut))); } \ No newline at end of file diff --git a/communication/communication.cpp b/communication/communication.cpp index 2a62f50..7d839b4 100644 --- a/communication/communication.cpp +++ b/communication/communication.cpp @@ -112,9 +112,10 @@ void Communication::clearRedirectRead() { } bool Communication::getSettingsSync(std::vector& data, std::chrono::milliseconds timeout) { + static const std::shared_ptr filter = std::make_shared(Network::NetID::ReadSettings); std::shared_ptr msg = waitForMessageSync([this]() { return sendCommand(Command::ReadSettings, { 0, 0, 0, 1 /* Get Global Settings */, 0, 1 /* Subversion 1 */ }); - }, MessageFilter(Network::NetID::ReadSettings), timeout); + }, filter, timeout); if(!msg) return false; @@ -134,25 +135,35 @@ bool Communication::getSettingsSync(std::vector& data, std::chrono::mil } std::shared_ptr Communication::getSerialNumberSync(std::chrono::milliseconds timeout) { + static const std::shared_ptr filter = std::make_shared(Command::RequestSerialNumber); std::shared_ptr msg = waitForMessageSync([this]() { return sendCommand(Command::RequestSerialNumber); - }, Main51MessageFilter(Command::RequestSerialNumber), timeout); + }, filter, timeout); if(!msg) // Did not receive a message + { + std::cout << "didn't get a message" << std::endl; return std::shared_ptr(); + } auto m51 = std::dynamic_pointer_cast(msg); if(!m51) // Could not upcast for some reason + { + std::cout << "could not upcast" << std::endl; return std::shared_ptr(); + } - return std::dynamic_pointer_cast(m51); + auto ret = std::dynamic_pointer_cast(m51); + std::cout << "returning " << ret.get() << std::endl; + return ret; } optional< std::vector< optional > > Communication::getVersionsSync(std::chrono::milliseconds timeout) { + static const std::shared_ptr filter = std::make_shared(Message::Type::DeviceVersion); std::vector< optional > ret; std::shared_ptr msg = waitForMessageSync([this]() { return sendCommand(Command::GetMainVersion); - }, Main51MessageFilter(Command::GetMainVersion), timeout); + }, filter, timeout); if(!msg) // Did not receive a message return nullopt; @@ -167,7 +178,7 @@ optional< std::vector< optional > > Communication::getVersions msg = waitForMessageSync([this]() { return sendCommand(Command::GetSecondaryVersions); - }, Main51MessageFilter(Command::GetSecondaryVersions), timeout); + }, filter, timeout); if(msg) { // This one is allowed to fail ver = std::dynamic_pointer_cast(msg); if(ver && ver->ForChip != VersionMessage::MainChip) @@ -194,7 +205,8 @@ bool Communication::removeMessageCallback(int id) { } } -std::shared_ptr Communication::waitForMessageSync(std::function onceWaitingDo, MessageFilter f, std::chrono::milliseconds timeout) { +std::shared_ptr Communication::waitForMessageSync(std::function onceWaitingDo, + const std::shared_ptr& f, std::chrono::milliseconds timeout) { std::mutex m; std::condition_variable cv; std::shared_ptr returnedMessage; diff --git a/device/device.cpp b/device/device.cpp index 5aed359..dcd4191 100644 --- a/device/device.cpp +++ b/device/device.cpp @@ -345,8 +345,8 @@ bool Device::goOnline() { updateLEDState(); - MessageFilter filter(Network::NetID::Reset_Status); - filter.includeInternalInAny = true; + std::shared_ptr filter = std::make_shared(Network::NetID::Reset_Status); + filter->includeInternalInAny = true; // Wait until communication is enabled or 5 seconds, whichever comes first while((std::chrono::system_clock::now() - startTime) < std::chrono::seconds(5)) { @@ -388,8 +388,8 @@ bool Device::goOffline() { updateLEDState(); - MessageFilter filter(Network::NetID::Reset_Status); - filter.includeInternalInAny = true; + std::shared_ptr filter = std::make_shared(Network::NetID::Reset_Status); + filter->includeInternalInAny = true; // Wait until communication is disabled or 5 seconds, whichever comes first while((std::chrono::system_clock::now() - startTime) < std::chrono::seconds(5)) { diff --git a/device/extensions/flexray/controller.cpp b/device/extensions/flexray/controller.cpp index cd694d5..b8c61e6 100644 --- a/device/extensions/flexray/controller.cpp +++ b/device/extensions/flexray/controller.cpp @@ -593,6 +593,7 @@ uint16_t FlexRay::Controller::CalculateCycleFilter(uint8_t baseCycle, uint8_t cy } std::pair FlexRay::Controller::readRegister(ERAYRegister reg, std::chrono::milliseconds timeout) const { + static const std::shared_ptr filter = std::make_shared(icsneo::Network::NetID::FlexRayControl); if(timeout.count() <= 20) return {false, 0}; // Out of time! @@ -611,7 +612,7 @@ std::pair FlexRay::Controller::readRegister(ERAYRegister reg, st return false; // Command failed to send lastSent = std::chrono::steady_clock::now(); return true; - }, MessageFilter(icsneo::Network::NetID::FlexRayControl), timeout); + }, filter, timeout); if(auto frmsg = std::dynamic_pointer_cast(msg)) { if(frmsg->decoded && frmsg->controller == index && frmsg->opcode == FlexRay::Opcode::ReadCCRegs) resp = frmsg; diff --git a/device/idevicesettings.cpp b/device/idevicesettings.cpp index 6b89eab..52494df 100644 --- a/device/idevicesettings.cpp +++ b/device/idevicesettings.cpp @@ -230,7 +230,7 @@ bool IDeviceSettings::apply(bool temporary) { std::shared_ptr msg = std::dynamic_pointer_cast(com->waitForMessageSync([this, &bytestream]() { return com->sendCommand(Command::SetSettings, bytestream); - }, Main51MessageFilter(Command::SetSettings), std::chrono::milliseconds(1000))); + }, std::make_shared(Command::SetSettings), std::chrono::milliseconds(1000))); if(!msg || msg->data[0] != 1) { // We did not receive a response // Attempt to get the settings from the device so we're up to date if possible @@ -256,7 +256,7 @@ bool IDeviceSettings::apply(bool temporary) { msg = std::dynamic_pointer_cast(com->waitForMessageSync([this, &bytestream]() { return com->sendCommand(Command::SetSettings, bytestream); - }, Main51MessageFilter(Command::SetSettings), std::chrono::milliseconds(1000))); + }, std::make_shared(Command::SetSettings), std::chrono::milliseconds(1000))); if(!msg || msg->data[0] != 1) { // Attempt to get the settings from the device so we're up to date if possible if(refresh()) { @@ -269,7 +269,7 @@ bool IDeviceSettings::apply(bool temporary) { if(!temporary) { msg = std::dynamic_pointer_cast(com->waitForMessageSync([this]() { return com->sendCommand(Command::SaveSettings); - }, Main51MessageFilter(Command::SaveSettings), std::chrono::milliseconds(5000))); + }, std::make_shared(Command::SaveSettings), std::chrono::milliseconds(5000))); } applyingSettings = false; @@ -298,7 +298,7 @@ bool IDeviceSettings::applyDefaults(bool temporary) { std::shared_ptr msg = std::dynamic_pointer_cast(com->waitForMessageSync([this]() { return com->sendCommand(Command::SetDefaultSettings); - }, Main51MessageFilter(Command::SetDefaultSettings), std::chrono::milliseconds(1000))); + }, std::make_shared(Command::SetDefaultSettings), std::chrono::milliseconds(1000))); if(!msg || msg->data[0] != 1) { // Attempt to get the settings from the device so we're up to date if possible if(refresh()) { @@ -333,7 +333,7 @@ bool IDeviceSettings::applyDefaults(bool temporary) { msg = std::dynamic_pointer_cast(com->waitForMessageSync([this, &bytestream]() { return com->sendCommand(Command::SetSettings, bytestream); - }, Main51MessageFilter(Command::SetSettings), std::chrono::milliseconds(1000))); + }, std::make_shared(Command::SetSettings), std::chrono::milliseconds(1000))); if(!msg || msg->data[0] != 1) { // Attempt to get the settings from the device so we're up to date if possible if(refresh()) { @@ -346,7 +346,7 @@ bool IDeviceSettings::applyDefaults(bool temporary) { if(!temporary) { msg = std::dynamic_pointer_cast(com->waitForMessageSync([this]() { return com->sendCommand(Command::SaveSettings); - }, Main51MessageFilter(Command::SaveSettings), std::chrono::milliseconds(5000))); + }, std::make_shared(Command::SaveSettings), std::chrono::milliseconds(5000))); } applyingSettings = false; diff --git a/include/icsneo/communication/communication.h b/include/icsneo/communication/communication.h index 601d3fc..dfa49b6 100644 --- a/include/icsneo/communication/communication.h +++ b/include/icsneo/communication/communication.h @@ -60,7 +60,7 @@ public: int addMessageCallback(const MessageCallback& cb); bool removeMessageCallback(int id); std::shared_ptr waitForMessageSync( - MessageFilter f = MessageFilter(), + const std::shared_ptr& f = {}, std::chrono::milliseconds timeout = std::chrono::milliseconds(50)) { return waitForMessageSync([](){ return true; }, f, timeout); } @@ -68,7 +68,7 @@ public: // Return false to bail early, in case your initial command failed. std::shared_ptr waitForMessageSync( std::function onceWaitingDo, - MessageFilter f = MessageFilter(), + const std::shared_ptr& f = {}, std::chrono::milliseconds timeout = std::chrono::milliseconds(50)); std::function()> makeConfiguredPacketizer; diff --git a/include/icsneo/communication/message/filter/main51messagefilter.h b/include/icsneo/communication/message/filter/main51messagefilter.h index 2d3aa86..7456879 100644 --- a/include/icsneo/communication/message/filter/main51messagefilter.h +++ b/include/icsneo/communication/message/filter/main51messagefilter.h @@ -19,7 +19,7 @@ public: // We still guarantee it's a Main51Message if it matches because of the dynamic_pointer_cast below Main51MessageFilter(Command command) : command(command) { includeInternalInAny = true; } - bool match(const std::shared_ptr& message) const { + bool match(const std::shared_ptr& message) const override { if(!MessageFilter::match(message)) { //std::cout << "message filter did not match base for " << message->network << std::endl; return false;