From 66b404fc5996e30a150c5b86e790d6685a5b9f80 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 18 Aug 2020 19:37:08 -0700 Subject: [PATCH] improve (#1702) --- firmware/console/binary/tunerstudio.cpp | 62 +++++++------------ firmware/console/binary/tunerstudio.h | 2 - firmware/tunerstudio/rusefi.input | 14 ++--- .../rusefi/binaryprotocol/BinaryProtocol.java | 18 +++--- .../BinaryProtocolCommands.java | 1 - 5 files changed, 38 insertions(+), 59 deletions(-) diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index 51960ca963..926881aa22 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -210,16 +210,10 @@ static void sendErrorCode(ts_channel_s *tsChannel) { sr5WriteCrcPacket(tsChannel, TS_RESPONSE_CRC_FAILURE, NULL, 0); } -static void handlePageSelectCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t pageId) { +static void handlePageSelectCommand(ts_channel_s *tsChannel, ts_response_format_e mode) { tsState.pageCommandCounter++; - scheduleMsg(&tsLogger, "PAGE %d", pageId); - - if (pageId == 0) { - sendOkResponse(tsChannel, mode); - } else { - sendErrorCode(tsChannel); - } + sendOkResponse(tsChannel, mode); } /** @@ -331,19 +325,17 @@ static void handleWriteChunkCommand(ts_channel_s *tsChannel, ts_response_format_ sendOkResponse(tsChannel, mode); } -static void handleCrc32Check(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t pageId) { - UNUSED(pageId); - +static void handleCrc32Check(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t offset, uint16_t count) { tsState.crc32CheckCommandCounter++; - uint16_t count = getTunerStudioPageSize(); + // Ensure we are reading from in bounds + if (validateOffsetCount(offset, count, tsChannel)) { + return; + } - uint32_t crc = SWAP_UINT32(crc32((void * ) getWorkingPageAddr(), count)); - -#if 0 - scheduleMsg(&tsLogger, "Sending CRC32 response: %x", crc); -#endif + const char* start = getWorkingPageAddr() + offset; + uint32_t crc = SWAP_UINT32(crc32(start, count)); sr5SendResponse(tsChannel, mode, (const uint8_t *) &crc, 4); } @@ -351,11 +343,9 @@ static void handleCrc32Check(ts_channel_s *tsChannel, ts_response_format_e mode, * 'Write' command receives a single value at a given offset * @note Writing values one by one is pretty slow */ -static void handleWriteValueCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t page, uint16_t offset, - uint8_t value) { +static void handleWriteValueCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t offset, uint8_t value) { UNUSED(tsChannel); UNUSED(mode); - UNUSED(page); tsState.writeValueCommandCounter++; @@ -382,20 +372,13 @@ static void handleWriteValueCommand(ts_channel_s *tsChannel, ts_response_format_ // scheduleMsg(logger, "va=%d", configWorkingCopy.boardConfiguration.idleValvePin); } -static void handlePageReadCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t pageId, uint16_t offset, - uint16_t count) { +static void handlePageReadCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t offset, uint16_t count) { tsState.readPageCommandsCounter++; #if EFI_TUNER_STUDIO_VERBOSE scheduleMsg(&tsLogger, "READ mode=%d offset=%d size=%d", mode, offset, count); #endif - if (pageId != 0) { - // something is not right here - tunerStudioError("ERROR: invalid page number"); - return; - } - if (validateOffsetCount(offset, count, tsChannel)) { return; } @@ -424,9 +407,7 @@ static void sendResponseCode(ts_response_format_e mode, ts_channel_s *tsChannel, /** * 'Burn' command is a command to commit the changes */ -static void handleBurnCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t page) { - UNUSED(page); - +static void handleBurnCommand(ts_channel_s *tsChannel, ts_response_format_e mode) { efitimems_t nowMs = currentTimeMillis(); tsState.burnCommandCounter++; @@ -738,10 +719,13 @@ int tunerStudioHandleCrcCommand(ts_channel_s *tsChannel, char *data, int incomin const uint16_t* data16 = reinterpret_cast(data); + uint16_t offset = data16[0]; + uint16_t count = data16[1]; + switch(command) { case TS_OUTPUT_COMMAND: - handleOutputChannelsCommand(tsChannel, TS_CRC, data16[0], data16[1]); + handleOutputChannelsCommand(tsChannel, TS_CRC, offset, count); break; case TS_HELLO_COMMAND: tunerStudioDebug("got Query command"); @@ -765,28 +749,28 @@ int tunerStudioHandleCrcCommand(ts_channel_s *tsChannel, char *data, int incomin handleExecuteCommand(tsChannel, data, incomingPacketSize - 1); break; case TS_PAGE_COMMAND: - handlePageSelectCommand(tsChannel, TS_CRC, data16[0]); + handlePageSelectCommand(tsChannel, TS_CRC); break; case TS_GET_STRUCT: - handleGetStructContent(tsChannel, data16[0], data16[1]); + handleGetStructContent(tsChannel, offset, count); break; case TS_CHUNK_WRITE_COMMAND: - handleWriteChunkCommand(tsChannel, TS_CRC, data16[1], data16[2], data + sizeof(TunerStudioWriteChunkRequest)); + handleWriteChunkCommand(tsChannel, TS_CRC, offset, count, data + sizeof(TunerStudioWriteChunkRequest)); break; case TS_SINGLE_WRITE_COMMAND: { uint8_t value = data[4]; - handleWriteValueCommand(tsChannel, TS_CRC, data16[0], data16[1], value); + handleWriteValueCommand(tsChannel, TS_CRC, offset, value); } break; case TS_CRC_CHECK_COMMAND: - handleCrc32Check(tsChannel, TS_CRC, data16[0]); + handleCrc32Check(tsChannel, TS_CRC, offset, count); break; case TS_BURN_COMMAND: - handleBurnCommand(tsChannel, TS_CRC, data16[0]); + handleBurnCommand(tsChannel, TS_CRC); break; case TS_READ_COMMAND: - handlePageReadCommand(tsChannel, TS_CRC, data16[0], data16[1], data16[2]); + handlePageReadCommand(tsChannel, TS_CRC, offset, count); break; case TS_TEST_COMMAND: [[fallthrough]]; diff --git a/firmware/console/binary/tunerstudio.h b/firmware/console/binary/tunerstudio.h index a7a2b19791..8480e832ec 100644 --- a/firmware/console/binary/tunerstudio.h +++ b/firmware/console/binary/tunerstudio.h @@ -75,14 +75,12 @@ typedef pre_packed struct typedef pre_packed struct post_packed { - short int page; short int offset; short int count; } TunerStudioWriteChunkRequest; typedef pre_packed struct post_packed { - short int page; short int offset; short int count; } TunerStudioReadRequest; diff --git a/firmware/tunerstudio/rusefi.input b/firmware/tunerstudio/rusefi.input index e773e984f8..e06d6854f6 100644 --- a/firmware/tunerstudio/rusefi.input +++ b/firmware/tunerstudio/rusefi.input @@ -46,16 +46,14 @@ enable2ndByteCanID = false endianness = little nPages = 1 - pageIdentifier = "\x00\x00" - pageReadCommand = "@@TS_READ_COMMAND_char@@\x00\x00%2o%2c" - burnCommand = "@@TS_BURN_COMMAND_char@@\x00\x00" - pageActivate = "@@TS_PAGE_COMMAND_char@@\x00\x00" - pageValueWrite = "@@TS_SINGLE_WRITE_COMMAND_char@@\x00\x00%2o%v" - pageChunkWrite = "@@TS_CHUNK_WRITE_COMMAND_char@@\x00\x00%2o%2c%v" - ; todo: make this command shorter one day, no need to have all these zeros - crc32CheckCommand = "k\x00\x00\x00\x00\x00\x00" + pageReadCommand = "@@TS_READ_COMMAND_char@@%2o%2c" + burnCommand = "@@TS_BURN_COMMAND_char@@" + pageActivate = "@@TS_PAGE_COMMAND_char@@" + pageValueWrite = "@@TS_SINGLE_WRITE_COMMAND_char@@%2o%v" + pageChunkWrite = "@@TS_CHUNK_WRITE_COMMAND_char@@%2o%2c%v" + crc32CheckCommand = "@@TS_CRC_CHECK_COMMAND@@%2o%2c" retrieveConfigError = "e" ;communication settings diff --git a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java b/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java index eebbaba9eb..bead76bb7f 100644 --- a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -362,11 +362,10 @@ public class BinaryProtocol implements BinaryProtocolCommands { int remainingSize = image.getSize() - offset; int requestSize = Math.min(remainingSize, Fields.BLOCKING_FACTOR); - byte packet[] = new byte[7]; + byte packet[] = new byte[5]; packet[0] = Fields.TS_READ_COMMAND; - putShort(packet, 1, 0); // page - putShort(packet, 3, swap16(offset)); - putShort(packet, 5, swap16(requestSize)); + putShort(packet, 1, swap16(offset)); + putShort(packet, 3, swap16(requestSize)); byte[] response = executeCommand(packet, "load image offset=" + offset); @@ -408,8 +407,10 @@ public class BinaryProtocol implements BinaryProtocolCommands { int crcOfLocallyCachedConfiguration = IoHelper.getCrc32(localCached.getContent()); log.info(String.format(CONFIGURATION_RUSEFI_BINARY + " Local cache CRC %x\n", crcOfLocallyCachedConfiguration)); - byte packet[] = new byte[7]; + byte packet[] = new byte[5]; packet[0] = COMMAND_CRC_CHECK_COMMAND; + putShort(packet, 1, swap16(/*offset = */ 0)); + putShort(packet, 3, swap16(localCached.getSize())); byte[] response = executeCommand(packet, "get CRC32"); if (checkResponseCode(response, RESPONSE_OK) && response.length == 5) { @@ -472,11 +473,10 @@ public class BinaryProtocol implements BinaryProtocolCommands { isBurnPending = true; - byte packet[] = new byte[7 + size]; + byte packet[] = new byte[5 + size]; packet[0] = COMMAND_CHUNK_WRITE; - putShort(packet, 1, 0); // page - putShort(packet, 3, swap16(offset)); - putShort(packet, 5, swap16(size)); + putShort(packet, 1, swap16(offset)); + putShort(packet, 3, swap16(size)); System.arraycopy(content, offset, packet, 7, size); diff --git a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocolCommands.java b/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocolCommands.java index def211dd50..14c6c7cdb5 100644 --- a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocolCommands.java +++ b/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocolCommands.java @@ -11,7 +11,6 @@ public interface BinaryProtocolCommands { byte RESPONSE_BURN_OK = Fields.TS_RESPONSE_BURN_OK; byte RESPONSE_COMMAND_OK = Fields.TS_RESPONSE_COMMAND_OK; char COMMAND_PROTOCOL = Fields.TS_COMMAND_F; - // todo: make crc32CheckCommand shorted one day later - no need in 6 empty bytes char COMMAND_CRC_CHECK_COMMAND = Fields.TS_CRC_CHECK_COMMAND; char COMMAND_CHUNK_WRITE = Fields.TS_CHUNK_WRITE_COMMAND; char COMMAND_BURN = Fields.TS_BURN_COMMAND;