From ed0c796b85a9ecb76d2220504d597da3457a94bd Mon Sep 17 00:00:00 2001 From: rusefi Date: Tue, 23 May 2017 14:37:03 -0400 Subject: [PATCH] extracting method & better method names --- firmware/console/binary/tunerstudio.cpp | 58 ++++++++++------------ firmware/console/binary/tunerstudio_io.cpp | 20 +++++--- firmware/console/binary/tunerstudio_io.h | 10 ++-- 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index 9f49b602ff..83c538e1ae 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -94,9 +94,6 @@ extern persistent_config_container_s persistentState; extern short currentPageId; -// that's 3 seconds -#define TS_READ_TIMEOUT MS2ST(3000) - /** * note the use-case where text console port is switched into * binary port @@ -239,7 +236,7 @@ void handlePageSelectCommand(ts_channel_s *tsChannel, ts_response_format_e mode, currentPageId = pageId; scheduleMsg(&tsLogger, "PAGE %d", currentPageId); - tsSendResponse(tsChannel, mode, NULL, 0); + sr5SendResponse(tsChannel, mode, NULL, 0); } static void onlineTuneBytes(int currentPageId, uint32_t offset, int count) { @@ -290,7 +287,7 @@ void handleWriteChunkCommand(ts_channel_s *tsChannel, ts_response_format_e mode, memcpy(addr, content, count); onlineTuneBytes(currentPageId, offset, count); - tsSendResponse(tsChannel, mode, NULL, 0); + sr5SendResponse(tsChannel, mode, NULL, 0); } void handleCrc32Check(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t pageId, uint16_t offset, @@ -307,7 +304,7 @@ void handleCrc32Check(ts_channel_s *tsChannel, ts_response_format_e mode, uint16 scheduleMsg(&tsLogger, "CRC32 response: %x", crc); - tsSendResponse(tsChannel, mode, (const uint8_t *) &crc, 4); + sr5SendResponse(tsChannel, mode, (const uint8_t *) &crc, 4); } /** @@ -350,7 +347,7 @@ void handleWriteValueCommand(ts_channel_s *tsChannel, ts_response_format_e mode, } static void sendErrorCode(ts_channel_s *tsChannel) { - tunerStudioWriteCrcPacket(tsChannel, TS_RESPONSE_CRC_FAILURE, NULL, 0); + sr5WriteCrcPacket(tsChannel, TS_RESPONSE_CRC_FAILURE, NULL, 0); } void handlePageReadCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t pageId, uint16_t offset, @@ -378,7 +375,7 @@ void handlePageReadCommand(ts_channel_s *tsChannel, ts_response_format_e mode, u } const uint8_t *addr = (const uint8_t *) (getWorkingPageAddr(currentPageId) + offset); - tsSendResponse(tsChannel, mode, addr, count); + sr5SendResponse(tsChannel, mode, addr, count); #if EFI_TUNER_STUDIO_VERBOSE || defined(__DOXYGEN__) // scheduleMsg(&tsLogger, "Sending %d done", count); #endif @@ -393,7 +390,7 @@ void requestBurn(void) { static void sendResponseCode(ts_response_format_e mode, ts_channel_s *tsChannel, const uint8_t responseCode) { if (mode == TS_CRC) { - tunerStudioWriteCrcPacket(tsChannel, responseCode, NULL, 0); + sr5WriteCrcPacket(tsChannel, responseCode, NULL, 0); } } @@ -457,7 +454,7 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel, bool isConsoleRedirect) { tsState.totalCounter++; uint8_t firstByte; - int received = chnReadTimeout(tsChannel->channel, &firstByte, 1, TS_READ_TIMEOUT); + int received = sr5ReadData(tsChannel, &firstByte, 1); #if EFI_SIMULATOR || defined(__DOXYGEN__) logMsg("received %d\r\n", received); #endif @@ -482,7 +479,7 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel, bool isConsoleRedirect) { continue; uint8_t secondByte; - received = chnReadTimeout(tsChannel->channel, &secondByte, 1, TS_READ_TIMEOUT); + received = sr5ReadData(tsChannel, &secondByte, 1); if (received != 1) { tunerStudioError("TS: ERROR: no second byte"); continue; @@ -493,7 +490,7 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel, bool isConsoleRedirect) { if (incomingPacketSize == BINARY_SWITCH_TAG) { // we are here if we get a binary switch request while already in binary mode. We will just ignore it. - tunerStudioWriteData(tsChannel, (const uint8_t *) &BINARY_RESPONSE, 2); + sr5WriteData(tsChannel, (const uint8_t *) &BINARY_RESPONSE, 2); continue; } @@ -504,7 +501,7 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel, bool isConsoleRedirect) { continue; } - received = chnReadTimeout(tsChannel->channel, (uint8_t* )tsChannel->crcReadBuffer, 1, TS_READ_TIMEOUT); + received = sr5ReadData(tsChannel, (uint8_t* )tsChannel->crcReadBuffer, 1); if (received != 1) { tunerStudioError("ERROR: did not receive command"); continue; @@ -524,8 +521,8 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel, bool isConsoleRedirect) { // scheduleMsg(logger, "TunerStudio: reading %d+4 bytes(s)", incomingPacketSize); - received = chnReadTimeout(tsChannel->channel, (uint8_t * ) (tsChannel->crcReadBuffer + 1), - incomingPacketSize + CRC_VALUE_SIZE - 1, TS_READ_TIMEOUT); + received = sr5ReadData(tsChannel, (uint8_t * ) (tsChannel->crcReadBuffer + 1), + incomingPacketSize + CRC_VALUE_SIZE - 1); int expectedSize = incomingPacketSize + CRC_VALUE_SIZE - 1; if (received != expectedSize) { scheduleMsg(&tsLogger, "Got only %d bytes while expecting %d for command %c", received, @@ -597,7 +594,7 @@ void handleQueryCommand(ts_channel_s *tsChannel, ts_response_format_e mode) { scheduleMsg(&tsLogger, "got S/H (queryCommand) mode=%d", mode); printTsStats(); #endif - tsSendResponse(tsChannel, mode, (const uint8_t *) TS_SIGNATURE, strlen(TS_SIGNATURE) + 1); + sr5SendResponse(tsChannel, mode, (const uint8_t *) TS_SIGNATURE, strlen(TS_SIGNATURE) + 1); } /** @@ -607,7 +604,7 @@ void handleOutputChannelsCommand(ts_channel_s *tsChannel, ts_response_format_e m tsState.outputChannelsCommandCounter++; prepareTunerStudioOutputs(); // this method is invoked too often to print any debug information - tsSendResponse(tsChannel, mode, (const uint8_t *) &tsOutputChannels, sizeof(TunerStudioOutputChannels)); + sr5SendResponse(tsChannel, mode, (const uint8_t *) &tsOutputChannels, sizeof(TunerStudioOutputChannels)); } #define TEST_RESPONSE_TAG " ts_p_alive\r\n" @@ -620,14 +617,14 @@ void handleTestCommand(ts_channel_s *tsChannel) { * extension of the protocol to simplify troubleshooting */ tunerStudioDebug("got T (Test)"); - tunerStudioWriteData(tsChannel, (const uint8_t *) VCS_VERSION, sizeof(VCS_VERSION)); + sr5WriteData(tsChannel, (const uint8_t *) VCS_VERSION, sizeof(VCS_VERSION)); chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), " %d %d", engine->engineState.lastErrorCode, tsState.testCommandCounter); - tunerStudioWriteData(tsChannel, (const uint8_t *) testOutputBuffer, strlen(testOutputBuffer)); + sr5WriteData(tsChannel, (const uint8_t *) testOutputBuffer, strlen(testOutputBuffer)); /** * Please note that this response is a magic constant used by dev console for protocol detection * @see EngineState#TS_PROTOCOL_TAG */ - tunerStudioWriteData(tsChannel, (const uint8_t *) TEST_RESPONSE_TAG, sizeof(TEST_RESPONSE_TAG)); + sr5WriteData(tsChannel, (const uint8_t *) TEST_RESPONSE_TAG, sizeof(TEST_RESPONSE_TAG)); } extern CommandHandler console_line_callback; @@ -635,7 +632,7 @@ extern CommandHandler console_line_callback; static void handleGetVersion(ts_channel_s *tsChannel, ts_response_format_e mode) { static char versionBuffer[32]; chsnprintf(versionBuffer, sizeof(versionBuffer), "rusEFI v%d@%s", getRusEfiVersion(), VCS_VERSION); - tsSendResponse(tsChannel, mode, (const uint8_t *) versionBuffer, strlen(versionBuffer) + 1); + sr5SendResponse(tsChannel, mode, (const uint8_t *) versionBuffer, strlen(versionBuffer) + 1); } static void handleGetText(ts_channel_s *tsChannel) { @@ -647,14 +644,14 @@ static void handleGetText(ts_channel_s *tsChannel) { logMsg("get test sending [%d]\r\n", outputSize); #endif - tunerStudioWriteCrcPacket(tsChannel, TS_RESPONSE_COMMAND_OK, output, outputSize); + sr5WriteCrcPacket(tsChannel, TS_RESPONSE_COMMAND_OK, output, outputSize); #if EFI_SIMULATOR || defined(__DOXYGEN__) logMsg("sent [%d]\r\n", outputSize); #endif } static void handleExecuteCommand(ts_channel_s *tsChannel, char *data, int incomingPacketSize) { - tunerStudioWriteCrcPacket(tsChannel, TS_RESPONSE_COMMAND_OK, NULL, 0); + sr5WriteCrcPacket(tsChannel, TS_RESPONSE_COMMAND_OK, NULL, 0); data[incomingPacketSize] = 0; char *trimmed = efiTrim(data); #if EFI_SIMULATOR || defined(__DOXYGEN__) @@ -675,7 +672,7 @@ bool handlePlainCommand(ts_channel_s *tsChannel, uint8_t command) { handleTestCommand(tsChannel); return true; } else if (command == TS_PAGE_COMMAND) { - int received = chnReadTimeout(tsChannel->channel, (uint8_t * )&pageIn, sizeof(pageIn), TS_READ_TIMEOUT); + int received = sr5ReadData(tsChannel, (uint8_t * )&pageIn, sizeof(pageIn)); if (received != sizeof(pageIn)) { tunerStudioError("ERROR: not enough for PAGE"); return true; @@ -685,7 +682,7 @@ bool handlePlainCommand(ts_channel_s *tsChannel, uint8_t command) { } else if (command == TS_BURN_COMMAND) { scheduleMsg(&tsLogger, "Got naked BURN"); uint16_t page; - int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&page, sizeof(page), TS_READ_TIMEOUT); + int recieved = sr5ReadData(tsChannel, (uint8_t * )&page, sizeof(page)); if (recieved != sizeof(page)) { tunerStudioError("ERROR: Not enough for plain burn"); return true; @@ -694,15 +691,13 @@ bool handlePlainCommand(ts_channel_s *tsChannel, uint8_t command) { return true; } else if (command == TS_CHUNK_WRITE_COMMAND) { scheduleMsg(&tsLogger, "Got naked CHUNK_WRITE"); - int received = chnReadTimeout(tsChannel->channel, (uint8_t * )&writeChunkRequest, sizeof(writeChunkRequest), - TS_READ_TIMEOUT); + int received = sr5ReadData(tsChannel, (uint8_t * )&writeChunkRequest, sizeof(writeChunkRequest)); if (received != sizeof(writeChunkRequest)) { scheduleMsg(&tsLogger, "ERROR: Not enough for plain chunk write header: %d", received); tsState.errorCounter++; return true; } - received = chnReadTimeout(tsChannel->channel, (uint8_t * )&tsChannel->crcReadBuffer, writeChunkRequest.count, - TS_READ_TIMEOUT); + received = sr5ReadData(tsChannel, (uint8_t * )&tsChannel->crcReadBuffer, writeChunkRequest.count); if (received != writeChunkRequest.count) { scheduleMsg(&tsLogger, "ERROR: Not enough for plain chunk write content: %d while expecting %d", received, writeChunkRequest.count); @@ -716,8 +711,7 @@ bool handlePlainCommand(ts_channel_s *tsChannel, uint8_t command) { return true; } else if (command == TS_READ_COMMAND) { //scheduleMsg(logger, "Got naked READ PAGE???"); - int received = chnReadTimeout(tsChannel->channel, (uint8_t * )&readRequest, sizeof(readRequest), - TS_READ_TIMEOUT); + int received = sr5ReadData(tsChannel, (uint8_t * )&readRequest, sizeof(readRequest)); if (received != sizeof(readRequest)) { tunerStudioError("Not enough for plain read header"); return true; @@ -733,7 +727,7 @@ bool handlePlainCommand(ts_channel_s *tsChannel, uint8_t command) { return true; } else if (command == TS_COMMAND_F) { tunerStudioDebug("not ignoring F"); - tunerStudioWriteData(tsChannel, (const uint8_t *) PROTOCOL, strlen(PROTOCOL)); + sr5WriteData(tsChannel, (const uint8_t *) PROTOCOL, strlen(PROTOCOL)); return true; } else { return false; diff --git a/firmware/console/binary/tunerstudio_io.cpp b/firmware/console/binary/tunerstudio_io.cpp index f1df9919ee..7c8b59f28a 100644 --- a/firmware/console/binary/tunerstudio_io.cpp +++ b/firmware/console/binary/tunerstudio_io.cpp @@ -73,7 +73,7 @@ BaseChannel * getTsSerialDevice(void) { #endif } -void tunerStudioWriteData(ts_channel_s *tsChannel, const uint8_t * buffer, int size) { +void sr5WriteData(ts_channel_s *tsChannel, const uint8_t * buffer, int size) { efiAssertVoid(getRemainingStack(chThdGetSelfX()) > 64, "tunerStudioWriteData"); #if EFI_SIMULATOR || defined(__DOXYGEN__) logMsg("chSequentialStreamWrite [%d]\r\n", size); @@ -93,12 +93,12 @@ void tunerStudioWriteData(ts_channel_s *tsChannel, const uint8_t * buffer, int s /** * Adds size to the beginning of a packet and a crc32 at the end. Then send the packet. */ -void tunerStudioWriteCrcPacket(ts_channel_s *tsChannel, const uint8_t responseCode, const void *buf, const uint16_t size) { +void sr5WriteCrcPacket(ts_channel_s *tsChannel, const uint8_t responseCode, const void *buf, const uint16_t size) { uint8_t *writeBuffer = tsChannel->writeBuffer; *(uint16_t *) writeBuffer = SWAP_UINT16(size + 1); // packet size including command *(uint8_t *) (writeBuffer + 2) = responseCode; - tunerStudioWriteData(tsChannel, writeBuffer, 3); // header + sr5WriteData(tsChannel, writeBuffer, 3); // header // CRC on whole packet uint32_t crc = crc32((void *) (writeBuffer + 2), 1); // command part of CRC @@ -106,18 +106,22 @@ void tunerStudioWriteCrcPacket(ts_channel_s *tsChannel, const uint8_t responseCo *(uint32_t *) (writeBuffer) = SWAP_UINT32(crc); if (size > 0) { - tunerStudioWriteData(tsChannel, (const uint8_t*)buf, size); // body + sr5WriteData(tsChannel, (const uint8_t*)buf, size); // body } - tunerStudioWriteData(tsChannel, writeBuffer, 4); // CRC footer + sr5WriteData(tsChannel, writeBuffer, 4); // CRC footer } -void tsSendResponse(ts_channel_s *tsChannel, ts_response_format_e mode, const uint8_t * buffer, int size) { +void sr5SendResponse(ts_channel_s *tsChannel, ts_response_format_e mode, const uint8_t * buffer, int size) { if (mode == TS_CRC) { - tunerStudioWriteCrcPacket(tsChannel, TS_RESPONSE_OK, buffer, size); + sr5WriteCrcPacket(tsChannel, TS_RESPONSE_OK, buffer, size); } else { if (size > 0) - tunerStudioWriteData(tsChannel, buffer, size); + sr5WriteData(tsChannel, buffer, size); } } +int sr5ReadData(ts_channel_s *tsChannel, uint8_t * buffer, int size) { + return chnReadTimeout(tsChannel->channel, buffer, size, SR5_READ_TIMEOUT); +} + diff --git a/firmware/console/binary/tunerstudio_io.h b/firmware/console/binary/tunerstudio_io.h index e887d6a310..2b95dc9a04 100644 --- a/firmware/console/binary/tunerstudio_io.h +++ b/firmware/console/binary/tunerstudio_io.h @@ -77,8 +77,12 @@ void startTsPort(void); // that's 3 seconds #define BINARY_IO_TIMEOUT MS2ST(3000) -void tunerStudioWriteData(ts_channel_s *tsChannel, const uint8_t * buffer, int size); -void tunerStudioWriteCrcPacket(ts_channel_s *tsChannel, const uint8_t responseCode, const void *buf, const uint16_t size); -void tsSendResponse(ts_channel_s *tsChannel, ts_response_format_e mode, const uint8_t * buffer, int size); +// that's 3 seconds +#define SR5_READ_TIMEOUT MS2ST(3000) + +void sr5WriteData(ts_channel_s *tsChannel, const uint8_t * buffer, int size); +void sr5WriteCrcPacket(ts_channel_s *tsChannel, const uint8_t responseCode, const void *buf, const uint16_t size); +void sr5SendResponse(ts_channel_s *tsChannel, ts_response_format_e mode, const uint8_t * buffer, int size); +int sr5ReadData(ts_channel_s *tsChannel, uint8_t * buffer, int size); #endif /* CONSOLE_TUNERSTUDIO_TUNERSTUDIO_IO_H_ */