diff --git a/firmware/console/binary/tunerstudio_commands.cpp b/firmware/console/binary/tunerstudio_commands.cpp index 74e6b110bb..358c453305 100644 --- a/firmware/console/binary/tunerstudio_commands.cpp +++ b/firmware/console/binary/tunerstudio_commands.cpp @@ -47,8 +47,9 @@ void TunerStudio::cmdOutputChannels(TsChannelBase* tsChannel, uint16_t offset, u tsState.outputChannelsCommandCounter++; updateTunerStudioState(); + tsChannel->assertPacketSize(count, false); // this method is invoked too often to print any debug information - tsChannel->writeCrcPacket(TS_RESPONSE_OK, reinterpret_cast(&engine->outputChannels) + offset, count); + tsChannel->writeCrcPacketSmall(TS_RESPONSE_OK, reinterpret_cast(&engine->outputChannels) + offset, count); } #endif // EFI_TUNER_STUDIO diff --git a/firmware/console/binary/tunerstudio_io.cpp b/firmware/console/binary/tunerstudio_io.cpp index 6977628705..3975a4f7a5 100644 --- a/firmware/console/binary/tunerstudio_io.cpp +++ b/firmware/console/binary/tunerstudio_io.cpp @@ -79,6 +79,14 @@ TsChannelBase::TsChannelBase(const char *name) { this->name = name; } +#define isBigPacket(size) ((size) > BLOCKING_FACTOR + 7) + +void TsChannelBase::assertPacketSize(size_t size, bool allowLongPackets) { + if (isBigPacket(size) && !allowLongPackets) { + firmwareError(OBD_PCM_Processor_Fault, "tried to send disallowed long packet of size %d", size); + } +} + /** * Adds size to the beginning of a packet and a crc32 at the end. Then send the packet. */ @@ -88,13 +96,9 @@ void TsChannelBase::writeCrcPacket(uint8_t responseCode, const uint8_t* buf, siz size = 0; } - bool isBigPacket = size > BLOCKING_FACTOR + 7; + assertPacketSize(size, allowLongPackets); - if (isBigPacket && !allowLongPackets) { - firmwareError(OBD_PCM_Processor_Fault, "tried to send disallowed long packet of size %d", size); - } - - if (isBigPacket) { + if (isBigPacket(size)) { // for larger packets we do not use a buffer for CRC calculation meaning data is now allowed to modify while pending writeCrcPacketLarge(responseCode, buf, size); } else { diff --git a/firmware/console/binary/tunerstudio_io.h b/firmware/console/binary/tunerstudio_io.h index ce8535b69c..01a5e64aa1 100644 --- a/firmware/console/binary/tunerstudio_io.h +++ b/firmware/console/binary/tunerstudio_io.h @@ -58,8 +58,10 @@ public: char scratchBuffer[BLOCKING_FACTOR + 30]; const char *name; -private: + void assertPacketSize(size_t size, bool allowLongPackets); void writeCrcPacketSmall(uint8_t responseCode, const uint8_t* buf, size_t size); + +private: void writeCrcPacketLarge(uint8_t responseCode, const uint8_t* buf, size_t size); };