Update TunerStudio protocol files

This commit is contained in:
Andrey Gusakov 2022-05-16 10:23:24 +03:00
parent f985d01294
commit 58f14216ec
5 changed files with 195 additions and 283 deletions

View File

@ -59,12 +59,22 @@
* *
*/ */
#include "pch.h" //#include "os_access.h"
#include "os_access.h"
#include <cstring>
/* configuration */
#include "port.h"
/* chprintf */
#include "chprintf.h"
#include "tunerstudio.h" #include "tunerstudio.h"
#include "tunerstudio_impl.h" #include "tunerstudio_impl.h"
#include "crc.h"
#include "byteswap.h"
/*
#include "main_trigger_callback.h" #include "main_trigger_callback.h"
#include "flash_main.h" #include "flash_main.h"
@ -84,64 +94,30 @@
#include "mmc_card.h" #include "mmc_card.h"
#include "signature.h" #include "signature.h"
*/
#if EFI_SIMULATOR
#include "rusEfiFunctionalTest.h"
#endif /* EFI_SIMULATOR */
static void printErrorCounters() { static void printErrorCounters() {
efiPrintf("TunerStudio size=%d / total=%d / errors=%d / H=%d / O=%d / P=%d / B=%d", // efiPrintf("TunerStudio size=%d / total=%d / errors=%d / H=%d / O=%d / P=%d / B=%d",
sizeof(engine->outputChannels), tsState.totalCounter, tsState.errorCounter, tsState.queryCommandCounter, // sizeof(engine->outputChannels), tsState.totalCounter, tsState.errorCounter, tsState.queryCommandCounter,
tsState.outputChannelsCommandCounter, tsState.readPageCommandsCounter, tsState.burnCommandCounter); // tsState.outputChannelsCommandCounter, tsState.readPageCommandsCounter, tsState.burnCommandCounter);
efiPrintf("TunerStudio W=%d / C=%d / P=%d", tsState.writeValueCommandCounter, // efiPrintf("TunerStudio W=%d / C=%d / P=%d", tsState.writeValueCommandCounter,
tsState.writeChunkCommandCounter, tsState.pageCommandCounter); // tsState.writeChunkCommandCounter, tsState.pageCommandCounter);
} }
#if EFI_TUNER_STUDIO
/* 1S */ /* 1S */
#define TS_COMMUNICATION_TIMEOUT TIME_MS2I(1000) #define TS_COMMUNICATION_TIMEOUT TIME_MS2I(1000)
static efitimems_t previousWriteReportMs = 0;
static void resetTs() {
memset(&tsState, 0, sizeof(tsState));
}
static void printTsStats(void) {
#if EFI_PROD_CODE
#ifdef EFI_CONSOLE_RX_BRAIN_PIN
efiPrintf("Primary UART RX", hwPortname(EFI_CONSOLE_RX_BRAIN_PIN));
efiPrintf("Primary UART TX", hwPortname(EFI_CONSOLE_TX_BRAIN_PIN));
#endif
if (false) {
// todo: is this code needed somewhere else?
efiPrintf("TS RX on %s", hwPortname(engineConfiguration->binarySerialRxPin));
efiPrintf("TS TX on %s @%d", hwPortname(engineConfiguration->binarySerialTxPin),
engineConfiguration->tunerStudioSerialSpeed);
}
#endif /* EFI_PROD_CODE */
printErrorCounters();
}
static void setTsSpeed(int value) {
engineConfiguration->tunerStudioSerialSpeed = value;
printTsStats();
}
#endif // EFI_TUNER_STUDIO
void tunerStudioDebug(TsChannelBase* tsChannel, const char *msg) { void tunerStudioDebug(TsChannelBase* tsChannel, const char *msg) {
#if EFI_TUNER_STUDIO_VERBOSE //#if EFI_TUNER_STUDIO_VERBOSE
efiPrintf("%s: %s", tsChannel->name, msg); // efiPrintf("%s: %s", tsChannel->name, msg);
#endif /* EFI_TUNER_STUDIO_VERBOSE */ //#endif /* EFI_TUNER_STUDIO_VERBOSE */
} }
uint8_t* getWorkingPageAddr() { uint8_t* getWorkingPageAddr() {
return (uint8_t*)engineConfiguration; //return (uint8_t*)&GetConfiguration();
return NULL;
//return (uint8_t*)engineConfiguration;
} }
void sendOkResponse(TsChannelBase *tsChannel, ts_response_format_e mode) { void sendOkResponse(TsChannelBase *tsChannel, ts_response_format_e mode) {
@ -162,14 +138,8 @@ void TunerStudio::handlePageSelectCommand(TsChannelBase *tsChannel, ts_response_
sendOkResponse(tsChannel, mode); sendOkResponse(tsChannel, mode);
} }
#if EFI_TUNER_STUDIO #if 0
const void * getStructAddr(live_data_e structId) { const void * getStructAddr(live_data_e structId) {
#if EFI_UNIT_TEST
if (engine == nullptr) {
return nullptr;
}
#endif
switch (structId) { switch (structId) {
case LDS_output_channels: case LDS_output_channels:
return reinterpret_cast<const uint8_t*>(&engine->outputChannels); return reinterpret_cast<const uint8_t*>(&engine->outputChannels);
@ -258,8 +228,7 @@ static void handleGetStructContent(TsChannelBase* tsChannel, int structId, int s
} }
tsChannel->sendResponse(TS_CRC, (const uint8_t *)addr, size); tsChannel->sendResponse(TS_CRC, (const uint8_t *)addr, size);
} }
#endif
#endif // EFI_TUNER_STUDIO
bool validateOffsetCount(size_t offset, size_t count, TsChannelBase* tsChannel); bool validateOffsetCount(size_t offset, size_t count, TsChannelBase* tsChannel);
@ -273,8 +242,6 @@ void TunerStudio::handleWriteChunkCommand(TsChannelBase* tsChannel, ts_response_
void *content) { void *content) {
tsState.writeChunkCommandCounter++; tsState.writeChunkCommandCounter++;
efiPrintf("WRITE CHUNK mode=%d o=%d s=%d", mode, offset, count);
if (validateOffsetCount(offset, count, tsChannel)) { if (validateOffsetCount(offset, count, tsChannel)) {
return; return;
} }
@ -288,8 +255,6 @@ void TunerStudio::handleWriteChunkCommand(TsChannelBase* tsChannel, ts_response_
sendOkResponse(tsChannel, mode); sendOkResponse(tsChannel, mode);
} }
#if EFI_TUNER_STUDIO
void TunerStudio::handleCrc32Check(TsChannelBase *tsChannel, ts_response_format_e mode, uint16_t offset, uint16_t count) { void TunerStudio::handleCrc32Check(TsChannelBase *tsChannel, ts_response_format_e mode, uint16_t offset, uint16_t count) {
tsState.crc32CheckCommandCounter++; tsState.crc32CheckCommandCounter++;
@ -309,8 +274,8 @@ void TunerStudio::handleCrc32Check(TsChannelBase *tsChannel, ts_response_format_
* @note Writing values one by one is pretty slow * @note Writing values one by one is pretty slow
*/ */
void TunerStudio::handleWriteValueCommand(TsChannelBase* tsChannel, ts_response_format_e mode, uint16_t offset, uint8_t value) { void TunerStudio::handleWriteValueCommand(TsChannelBase* tsChannel, ts_response_format_e mode, uint16_t offset, uint8_t value) {
UNUSED(tsChannel); (void)tsChannel;
UNUSED(mode); (void)mode;
tsState.writeValueCommandCounter++; tsState.writeValueCommandCounter++;
@ -320,12 +285,6 @@ void TunerStudio::handleWriteValueCommand(TsChannelBase* tsChannel, ts_response_
return; return;
} }
efitimems_t nowMs = currentTimeMillis();
if (nowMs - previousWriteReportMs > 5) {
previousWriteReportMs = nowMs;
efiPrintf("offset %d: value=%d", offset, value);
}
// Skip the write if a preset was just loaded - we don't want to overwrite it // Skip the write if a preset was just loaded - we don't want to overwrite it
if (!rebootForPresetPending) { if (!rebootForPresetPending) {
getWorkingPageAddr()[offset] = value; getWorkingPageAddr()[offset] = value;
@ -340,9 +299,9 @@ void TunerStudio::handlePageReadCommand(TsChannelBase* tsChannel, ts_response_fo
return; return;
} }
#if EFI_TUNER_STUDIO_VERBOSE //#if EFI_TUNER_STUDIO_VERBOSE
efiPrintf("READ mode=%d offset=%d size=%d", mode, offset, count); // efiPrintf("READ mode=%d offset=%d size=%d", mode, offset, count);
#endif //#endif
if (validateOffsetCount(offset, count, tsChannel)) { if (validateOffsetCount(offset, count, tsChannel)) {
return; return;
@ -350,21 +309,13 @@ void TunerStudio::handlePageReadCommand(TsChannelBase* tsChannel, ts_response_fo
const uint8_t* addr = getWorkingPageAddr() + offset; const uint8_t* addr = getWorkingPageAddr() + offset;
tsChannel->sendResponse(mode, addr, count); tsChannel->sendResponse(mode, addr, count);
#if EFI_TUNER_STUDIO_VERBOSE //#if EFI_TUNER_STUDIO_VERBOSE
// efiPrintf("Sending %d done", count); // efiPrintf("Sending %d done", count);
#endif //#endif
} }
#endif // EFI_TUNER_STUDIO
void requestBurn(void) { void requestBurn(void) {
#if !EFI_UNIT_TEST SaveConfiguration();
onBurnRequest();
#if EFI_INTERNAL_FLASH
setNeedToWriteConfiguration();
#endif
#endif // !EFI_UNIT_TEST
} }
static void sendResponseCode(ts_response_format_e mode, TsChannelBase *tsChannel, const uint8_t responseCode) { static void sendResponseCode(ts_response_format_e mode, TsChannelBase *tsChannel, const uint8_t responseCode) {
@ -377,22 +328,13 @@ static void sendResponseCode(ts_response_format_e mode, TsChannelBase *tsChannel
* 'Burn' command is a command to commit the changes * 'Burn' command is a command to commit the changes
*/ */
static void handleBurnCommand(TsChannelBase* tsChannel, ts_response_format_e mode) { static void handleBurnCommand(TsChannelBase* tsChannel, ts_response_format_e mode) {
efitimems_t nowMs = currentTimeMillis();
tsState.burnCommandCounter++; tsState.burnCommandCounter++;
efiPrintf("got B (Burn) %s", mode == TS_PLAIN ? "plain" : "CRC"); SaveConfiguration();
// Skip the burn if a preset was just loaded - we don't want to overwrite it
if (!rebootForPresetPending) {
requestBurn();
}
sendResponseCode(mode, tsChannel, TS_RESPONSE_BURN_OK); sendResponseCode(mode, tsChannel, TS_RESPONSE_BURN_OK);
efiPrintf("BURN in %dms", currentTimeMillis() - nowMs);
} }
#if EFI_TUNER_STUDIO && (EFI_PROD_CODE || EFI_SIMULATOR)
static bool isKnownCommand(char command) { static bool isKnownCommand(char command) {
return command == TS_HELLO_COMMAND || command == TS_READ_COMMAND || command == TS_OUTPUT_COMMAND return command == TS_HELLO_COMMAND || command == TS_READ_COMMAND || command == TS_OUTPUT_COMMAND
|| command == TS_PAGE_COMMAND || command == TS_BURN_COMMAND || command == TS_SINGLE_WRITE_COMMAND || command == TS_PAGE_COMMAND || command == TS_BURN_COMMAND || command == TS_SINGLE_WRITE_COMMAND
@ -413,6 +355,9 @@ static bool isKnownCommand(char command) {
/** /**
* rusEfi own test command * rusEfi own test command
*/ */
#define VCS_VERSION "unknown"
static void handleTestCommand(TsChannelBase* tsChannel) { static void handleTestCommand(TsChannelBase* tsChannel) {
tsState.testCommandCounter++; tsState.testCommandCounter++;
char testOutputBuffer[64]; char testOutputBuffer[64];
@ -423,20 +368,17 @@ static void handleTestCommand(TsChannelBase* tsChannel) {
tunerStudioDebug(tsChannel, "got T (Test)"); tunerStudioDebug(tsChannel, "got T (Test)");
tsChannel->write((const uint8_t*)VCS_VERSION, sizeof(VCS_VERSION)); tsChannel->write((const uint8_t*)VCS_VERSION, sizeof(VCS_VERSION));
chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), " %d %d", engine->engineState.warnings.lastErrorCode, tsState.testCommandCounter); chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), __DATE__ "\r\n");
tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer));
chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), " uptime=%ds ", (int)getTimeNowSeconds());
tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer));
chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), __DATE__ " %s\r\n", PROTOCOL_TEST_RESPONSE_TAG);
tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer)); tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer));
/*
if (hasFirmwareError()) { if (hasFirmwareError()) {
const char* error = getCriticalErrorMessage(); const char* error = getCriticalErrorMessage();
chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), "error=%s\r\n", error); chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), "error=%s\r\n", error);
tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer)); tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer));
} }
*/
tsChannel->flush(); tsChannel->flush();
} }
@ -448,10 +390,9 @@ static void handleTestCommand(TsChannelBase* tsChannel) {
*/ */
void TunerStudio::handleQueryCommand(TsChannelBase* tsChannel, ts_response_format_e mode) { void TunerStudio::handleQueryCommand(TsChannelBase* tsChannel, ts_response_format_e mode) {
tsState.queryCommandCounter++; tsState.queryCommandCounter++;
#if EFI_TUNER_STUDIO_VERBOSE //#if EFI_TUNER_STUDIO_VERBOSE
efiPrintf("got S/H (queryCommand) mode=%d", mode); // efiPrintf("got S/H (queryCommand) mode=%d", mode);
printTsStats(); //#endif
#endif
const char *signature = getTsSignature(); const char *signature = getTsSignature();
tsChannel->sendResponse(mode, (const uint8_t *)signature, strlen(signature) + 1); tsChannel->sendResponse(mode, (const uint8_t *)signature, strlen(signature) + 1);
} }
@ -467,7 +408,7 @@ bool TunerStudio::handlePlainCommand(TsChannelBase* tsChannel, uint8_t command)
return false; return false;
} else if (command == TS_HELLO_COMMAND || command == TS_QUERY_COMMAND) { } else if (command == TS_HELLO_COMMAND || command == TS_QUERY_COMMAND) {
// We interpret 'Q' as TS_HELLO_COMMAND, since TS uses hardcoded 'Q' during ECU detection (scan all serial ports) // We interpret 'Q' as TS_HELLO_COMMAND, since TS uses hardcoded 'Q' during ECU detection (scan all serial ports)
efiPrintf("Got naked Query command"); //efiPrintf("Got naked Query command");
handleQueryCommand(tsChannel, TS_PLAIN); handleQueryCommand(tsChannel, TS_PLAIN);
return true; return true;
} else if (command == TS_TEST_COMMAND || command == 'T') { } else if (command == TS_TEST_COMMAND || command == 'T') {
@ -495,8 +436,6 @@ bool TunerStudio::handlePlainCommand(TsChannelBase* tsChannel, uint8_t command)
TunerStudio tsInstance; TunerStudio tsInstance;
static int tsProcessOne(TsChannelBase* tsChannel) { static int tsProcessOne(TsChannelBase* tsChannel) {
validateStack("communication", STACK_USAGE_COMMUNICATION, 128);
if (!tsChannel->isReady()) { if (!tsChannel->isReady()) {
chThdSleepMilliseconds(10); chThdSleepMilliseconds(10);
return -1; return -1;
@ -506,9 +445,6 @@ static int tsProcessOne(TsChannelBase* tsChannel) {
uint8_t firstByte; uint8_t firstByte;
int received = tsChannel->readTimeout(&firstByte, 1, TS_COMMUNICATION_TIMEOUT); int received = tsChannel->readTimeout(&firstByte, 1, TS_COMMUNICATION_TIMEOUT);
#if EFI_SIMULATOR
logMsg("received %d\r\n", received);
#endif
if (received != 1) { if (received != 1) {
// tunerStudioError("ERROR: no command"); // tunerStudioError("ERROR: no command");
@ -534,36 +470,32 @@ static int tsProcessOne(TsChannelBase* tsChannel) {
uint16_t incomingPacketSize = firstByte << 8 | secondByte; uint16_t incomingPacketSize = firstByte << 8 | secondByte;
if (incomingPacketSize == 0 || incomingPacketSize > (sizeof(tsChannel->scratchBuffer) - CRC_WRAPPING_SIZE)) { if (incomingPacketSize == 0 || incomingPacketSize > (sizeof(tsChannel->scratchBuffer) - CRC_WRAPPING_SIZE)) {
efiPrintf("process_ts: channel=%s invalid size: %d", tsChannel->name, incomingPacketSize); //efiPrintf("process_ts: channel=%s invalid size: %d", tsChannel->name, incomingPacketSize);
tunerStudioError(tsChannel, "process_ts: ERROR: CRC header size"); //tunerStudioError(tsChannel, "process_ts: ERROR: CRC header size");
sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN); sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN);
return -1; return -1;
} }
received = tsChannel->readTimeout((uint8_t* )tsChannel->scratchBuffer, 1, TS_COMMUNICATION_TIMEOUT); received = tsChannel->readTimeout((uint8_t* )tsChannel->scratchBuffer, 1, TS_COMMUNICATION_TIMEOUT);
if (received != 1) { if (received != 1) {
tunerStudioError(tsChannel, "ERROR: did not receive command"); //tunerStudioError(tsChannel, "ERROR: did not receive command");
sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN); sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN);
return -1; return -1;
} }
char command = tsChannel->scratchBuffer[0]; char command = tsChannel->scratchBuffer[0];
if (!isKnownCommand(command)) { if (!isKnownCommand(command)) {
efiPrintf("unexpected command %x", command); //efiPrintf("unexpected command %x", command);
sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND); sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND);
return -1; return -1;
} }
#if EFI_SIMULATOR
logMsg("command %c\r\n", command);
#endif
int expectedSize = incomingPacketSize + CRC_VALUE_SIZE - 1; int expectedSize = incomingPacketSize + CRC_VALUE_SIZE - 1;
received = tsChannel->readTimeout((uint8_t*)(tsChannel->scratchBuffer + 1), expectedSize, TS_COMMUNICATION_TIMEOUT); received = tsChannel->readTimeout((uint8_t*)(tsChannel->scratchBuffer + 1), expectedSize, TS_COMMUNICATION_TIMEOUT);
if (received != expectedSize) { if (received != expectedSize) {
efiPrintf("Got only %d bytes while expecting %d for command %c", received, //efiPrintf("Got only %d bytes while expecting %d for command %c", received,
expectedSize, command); // expectedSize, command);
tunerStudioError(tsChannel, "ERROR: not enough bytes in stream"); //tunerStudioError(tsChannel, "ERROR: not enough bytes in stream");
sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN); sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN);
return -1; return -1;
} }
@ -574,12 +506,12 @@ static int tsProcessOne(TsChannelBase* tsChannel) {
uint32_t actualCrc = crc32(tsChannel->scratchBuffer, incomingPacketSize); uint32_t actualCrc = crc32(tsChannel->scratchBuffer, incomingPacketSize);
if (actualCrc != expectedCrc) { if (actualCrc != expectedCrc) {
efiPrintf("TunerStudio: CRC %x %x %x %x", tsChannel->scratchBuffer[incomingPacketSize + 0], //efiPrintf("TunerStudio: CRC %x %x %x %x", tsChannel->scratchBuffer[incomingPacketSize + 0],
tsChannel->scratchBuffer[incomingPacketSize + 1], tsChannel->scratchBuffer[incomingPacketSize + 2], // tsChannel->scratchBuffer[incomingPacketSize + 1], tsChannel->scratchBuffer[incomingPacketSize + 2],
tsChannel->scratchBuffer[incomingPacketSize + 3]); // tsChannel->scratchBuffer[incomingPacketSize + 3]);
efiPrintf("TunerStudio: command %c actual CRC %x/expected %x", tsChannel->scratchBuffer[0], //efiPrintf("TunerStudio: command %c actual CRC %x/expected %x", tsChannel->scratchBuffer[0],
actualCrc, expectedCrc); // actualCrc, expectedCrc);
tunerStudioError(tsChannel, "ERROR: CRC issue"); tunerStudioError(tsChannel, "ERROR: CRC issue");
sendErrorCode(tsChannel, TS_RESPONSE_CRC_FAILURE); sendErrorCode(tsChannel, TS_RESPONSE_CRC_FAILURE);
return -1; return -1;
@ -588,7 +520,7 @@ static int tsProcessOne(TsChannelBase* tsChannel) {
int success = tsInstance.handleCrcCommand(tsChannel, tsChannel->scratchBuffer, incomingPacketSize); int success = tsInstance.handleCrcCommand(tsChannel, tsChannel->scratchBuffer, incomingPacketSize);
if (!success) { if (!success) {
efiPrintf("got unexpected TunerStudio command %x:%c", command, command); //efiPrintf("got unexpected TunerStudio command %x:%c", command, command);
return -1; return -1;
} }
@ -606,15 +538,13 @@ void TunerstudioThread::ThreadTask() {
// Until the end of time, process incoming messages. // Until the end of time, process incoming messages.
while (true) { while (true) {
if (tsProcessOne(channel) == 0) { if (tsProcessOne(channel) == 0) {
onDataArrived(true); //onDataArrived(true);
} else { } else {
onDataArrived(false); //onDataArrived(false);
} }
} }
} }
#endif // EFI_TUNER_STUDIO
tunerstudio_counters_s tsState; tunerstudio_counters_s tsState;
void tunerStudioError(TsChannelBase* tsChannel, const char *msg) { void tunerStudioError(TsChannelBase* tsChannel, const char *msg) {
@ -623,15 +553,10 @@ void tunerStudioError(TsChannelBase* tsChannel, const char *msg) {
tsState.errorCounter++; tsState.errorCounter++;
} }
#if EFI_TUNER_STUDIO
#if EFI_PROD_CODE || EFI_SIMULATOR
extern CommandHandler console_line_callback;
static void handleGetVersion(TsChannelBase* tsChannel) { static void handleGetVersion(TsChannelBase* tsChannel) {
char versionBuffer[32]; char versionBuffer[32];
chsnprintf(versionBuffer, sizeof(versionBuffer), "rusEFI v%d@%s", getRusEfiVersion(), VCS_VERSION); //chsnprintf(versionBuffer, sizeof(versionBuffer), "rusEFI v%d@%s", getRusEfiVersion(), VCS_VERSION);
chsnprintf(versionBuffer, sizeof(versionBuffer), "rusEFI Wideband Rev2");
tsChannel->sendResponse(TS_CRC, (const uint8_t *) versionBuffer, strlen(versionBuffer) + 1); tsChannel->sendResponse(TS_CRC, (const uint8_t *) versionBuffer, strlen(versionBuffer) + 1);
} }
@ -643,32 +568,25 @@ static void handleGetText(TsChannelBase* tsChannel) {
size_t outputSize; size_t outputSize;
const char* output = swapOutputBuffers(&outputSize); const char* output = swapOutputBuffers(&outputSize);
#if EFI_SIMULATOR
logMsg("get test sending [%d]\r\n", outputSize);
#endif
tsChannel->writeCrcPacket(TS_RESPONSE_COMMAND_OK, reinterpret_cast<const uint8_t*>(output), outputSize, true); tsChannel->writeCrcPacket(TS_RESPONSE_COMMAND_OK, reinterpret_cast<const uint8_t*>(output), outputSize, true);
#if EFI_SIMULATOR
logMsg("sent [%d]\r\n", outputSize);
#endif
} }
#endif // EFI_TEXT_LOGGING #endif // EFI_TEXT_LOGGING
#if 0
extern CommandHandler console_line_callback;
void TunerStudio::handleExecuteCommand(TsChannelBase* tsChannel, char *data, int incomingPacketSize) { void TunerStudio::handleExecuteCommand(TsChannelBase* tsChannel, char *data, int incomingPacketSize) {
data[incomingPacketSize] = 0; data[incomingPacketSize] = 0;
char *trimmed = efiTrim(data); char *trimmed = efiTrim(data);
#if EFI_SIMULATOR
logMsg("execute [%s]\r\n", trimmed);
#endif
(console_line_callback)(trimmed); (console_line_callback)(trimmed);
tsChannel->writeCrcPacket(TS_RESPONSE_COMMAND_OK, nullptr, 0); tsChannel->writeCrcPacket(TS_RESPONSE_COMMAND_OK, nullptr, 0);
} }
#endif
static int transmitted = 0;
int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int incomingPacketSize) { int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int incomingPacketSize) {
ScopePerf perf(PE::TunerStudioHandleCrcCommand); (void)incomingPacketSize;
char command = data[0]; char command = data[0];
data++; data++;
@ -695,15 +613,17 @@ int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int inco
handleGetText(tsChannel); handleGetText(tsChannel);
break; break;
#endif // EFI_TEXT_LOGGING #endif // EFI_TEXT_LOGGING
#if 0
case TS_EXECUTE: case TS_EXECUTE:
handleExecuteCommand(tsChannel, data, incomingPacketSize - 1); handleExecuteCommand(tsChannel, data, incomingPacketSize - 1);
break; break;
#endif
case TS_PAGE_COMMAND: case TS_PAGE_COMMAND:
handlePageSelectCommand(tsChannel, TS_CRC); handlePageSelectCommand(tsChannel, TS_CRC);
break; break;
case TS_GET_STRUCT: // case TS_GET_STRUCT:
handleGetStructContent(tsChannel, offset, count); // handleGetStructContent(tsChannel, offset, count);
break; // break;
case TS_CHUNK_WRITE_COMMAND: case TS_CHUNK_WRITE_COMMAND:
handleWriteChunkCommand(tsChannel, TS_CRC, offset, count, data + sizeof(TunerStudioWriteChunkRequest)); handleWriteChunkCommand(tsChannel, TS_CRC, offset, count, data + sizeof(TunerStudioWriteChunkRequest));
break; break;
@ -727,6 +647,7 @@ int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int inco
case 'T': case 'T':
handleTestCommand(tsChannel); handleTestCommand(tsChannel);
break; break;
#if 0
case TS_IO_TEST_COMMAND: case TS_IO_TEST_COMMAND:
{ {
uint16_t subsystem = SWAP_UINT16(data16[0]); uint16_t subsystem = SWAP_UINT16(data16[0]);
@ -738,81 +659,10 @@ int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int inco
engine->outputChannels.debugIntField3 = index; engine->outputChannels.debugIntField3 = index;
} }
#if EFI_PROD_CODE && EFI_ENGINE_CONTROL executeTSCommand(subsystem, index);
executeTSCommand(subsystem, index);
#endif /* EFI_PROD_CODE */
sendOkResponse(tsChannel, TS_CRC); sendOkResponse(tsChannel, TS_CRC);
} }
break; break;
#if EFI_TOOTH_LOGGER
case TS_SET_LOGGER_SWITCH:
switch(data[0]) {
case TS_COMPOSITE_ENABLE:
EnableToothLogger();
break;
case TS_COMPOSITE_DISABLE:
DisableToothLogger();
break;
default:
// dunno what that was, send NAK
return false;
}
sendOkResponse(tsChannel, TS_CRC);
break;
case TS_GET_COMPOSITE_BUFFER_DONE_DIFFERENTLY:
{
EnableToothLoggerIfNotEnabled();
const uint8_t* const buffer = GetToothLoggerBuffer().Buffer;
const uint8_t* const start = buffer + COMPOSITE_PACKET_SIZE * transmitted;
int currentEnd = getCompositeRecordCount();
// set debug_mode 40
if (engineConfiguration->debugMode == DBG_COMPOSITE_LOG) {
engine->outputChannels.debugIntField1 = currentEnd;
engine->outputChannels.debugIntField2 = transmitted;
}
if (currentEnd > transmitted) {
// more normal case - tail after head
tsChannel->sendResponse(TS_CRC, start, COMPOSITE_PACKET_SIZE * (currentEnd - transmitted), true);
transmitted = currentEnd;
} else if (currentEnd == transmitted) {
tsChannel->sendResponse(TS_CRC, start, 0);
} else {
// we are here if tail of buffer has reached the end of buffer and re-started from the start of buffer
// sending end of the buffer, next transmission would take care of the rest
tsChannel->sendResponse(TS_CRC, start, COMPOSITE_PACKET_SIZE * (COMPOSITE_PACKET_COUNT - transmitted), true);
transmitted = 0;
}
}
break;
case TS_GET_LOGGER_GET_BUFFER:
{
auto toothBuffer = GetToothLoggerBuffer();
tsChannel->sendResponse(TS_CRC, toothBuffer.Buffer, toothBuffer.Length, true);
}
break;
#endif /* EFI_TOOTH_LOGGER */
#if ENABLE_PERF_TRACE
case TS_PERF_TRACE_BEGIN:
perfTraceEnable();
sendOkResponse(tsChannel, TS_CRC);
break;
case TS_PERF_TRACE_GET_BUFFER:
{
auto trace = perfTraceGetBuffer();
tsChannel->sendResponse(TS_CRC, trace.Buffer, trace.Size, true);
}
break;
#endif /* ENABLE_PERF_TRACE */
case TS_GET_CONFIG_ERROR: { case TS_GET_CONFIG_ERROR: {
const char* configError = getCriticalErrorMessage(); const char* configError = getCriticalErrorMessage();
#if HW_CHECK_MODE #if HW_CHECK_MODE
@ -824,6 +674,7 @@ int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int inco
tsChannel->sendResponse(TS_CRC, reinterpret_cast<const uint8_t*>(configError), strlen(configError), true); tsChannel->sendResponse(TS_CRC, reinterpret_cast<const uint8_t*>(configError), strlen(configError), true);
break; break;
} }
#endif
default: default:
sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND); sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND);
tunerStudioError(tsChannel, "ERROR: ignoring unexpected command"); tunerStudioError(tsChannel, "ERROR: ignoring unexpected command");
@ -833,21 +684,15 @@ int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int inco
return true; return true;
} }
#endif // EFI_PROD_CODE || EFI_SIMULATOR
void startTunerStudioConnectivity(void) { void startTunerStudioConnectivity(void) {
// Assert tune & output channel struct sizes // Assert tune & output channel struct sizes
static_assert(sizeof(persistent_config_s) == TOTAL_CONFIG_SIZE, "TS datapage size mismatch"); // static_assert(sizeof(persistent_config_s) == TOTAL_CONFIG_SIZE, "TS datapage size mismatch");
// useful trick if you need to know how far off is the static_assert // useful trick if you need to know how far off is the static_assert
// char (*__kaboom)[sizeof(persistent_config_s)] = 1; // char (*__kaboom)[sizeof(persistent_config_s)] = 1;
// another useful trick // another useful trick
// static_assert(offsetof (engine_configuration_s,HD44780_e) == 700); // static_assert(offsetof (engine_configuration_s,HD44780_e) == 700);
memset(&tsState, 0, sizeof(tsState)); memset(&tsState, 0, sizeof(tsState));
addConsoleAction("tsinfo", printTsStats);
addConsoleAction("reset_ts", resetTs);
addConsoleActionI("set_ts_speed", setTsSpeed);
#if EFI_BLUETOOTH_SETUP #if EFI_BLUETOOTH_SETUP
// module initialization start (it waits for disconnect and then communicates to the module) // module initialization start (it waits for disconnect and then communicates to the module)
@ -869,5 +714,3 @@ void startTunerStudioConnectivity(void) {
addConsoleAction("bluetooth_cancel", bluetoothCancel); addConsoleAction("bluetooth_cancel", bluetoothCancel);
#endif /* EFI_BLUETOOTH_SETUP */ #endif /* EFI_BLUETOOTH_SETUP */
} }
#endif

View File

@ -6,11 +6,12 @@
*/ */
#pragma once #pragma once
#include "global.h"
#include "tunerstudio_io.h" #include "tunerstudio_io.h"
#include "electronic_throttle_generated.h" //#include "electronic_throttle_generated.h"
#include "knock_controller_generated.h" //#include "knock_controller_generated.h"
#include "FragmentEntry.h" //#include "FragmentEntry.h"
#include "thread_controller.h"
typedef struct { typedef struct {
int queryCommandCounter; int queryCommandCounter;
@ -33,13 +34,6 @@ void tunerStudioDebug(TsChannelBase* tsChannel, const char *msg);
void tunerStudioError(TsChannelBase* tsChannel, const char *msg); void tunerStudioError(TsChannelBase* tsChannel, const char *msg);
uint8_t* getWorkingPageAddr(); uint8_t* getWorkingPageAddr();
const void * getStructAddr(live_data_e structId);
#if EFI_TUNER_STUDIO
#include "thread_controller.h"
#include "thread_priority.h"
FragmentList getFragments();
void updateTunerStudioState(); void updateTunerStudioState();
@ -63,13 +57,13 @@ post_packed {
short int count; short int count;
} TunerStudioWriteChunkRequest; } TunerStudioWriteChunkRequest;
#if EFI_PROD_CODE || EFI_SIMULATOR #define CONNECTIVITY_THREAD_STACK (512)
#define CONNECTIVITY_THREAD_STACK (2 * UTILITY_THREAD_STACK_SIZE) #define CONNECTIVITY_THREAD_PRIO (NORMALPRIO + 1)
class TunerstudioThread : public ThreadController<CONNECTIVITY_THREAD_STACK> { class TunerstudioThread : public ThreadController<CONNECTIVITY_THREAD_STACK> {
public: public:
TunerstudioThread(const char* name) TunerstudioThread(const char* name)
: ThreadController(name, PRIO_CONSOLE) : ThreadController(name, CONNECTIVITY_THREAD_PRIO)
{ {
} }
@ -79,6 +73,3 @@ public:
void ThreadTask() override; void ThreadTask() override;
}; };
#endif
#endif /* EFI_TUNER_STUDIO */

View File

@ -5,6 +5,7 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <cstddef>
struct TsChannelBase; struct TsChannelBase;

View File

@ -5,19 +5,16 @@
* @author Andrey Belomutskiy, (c) 2012-2020 * @author Andrey Belomutskiy, (c) 2012-2020
*/ */
#include "pch.h" /* memcpy */
#include "os_access.h" #include <cstring>
#include "tunerstudio_io.h" #include "tunerstudio_io.h"
#include "crc.h"
#include "byteswap.h"
#if EFI_SIMULATOR
#include "rusEfiFunctionalTest.h"
#endif // EFI_SIMULATOR
#if EFI_PROD_CODE || EFI_SIMULATOR
size_t TsChannelBase::read(uint8_t* buffer, size_t size) { size_t TsChannelBase::read(uint8_t* buffer, size_t size) {
return readTimeout(buffer, size, SR5_READ_TIMEOUT); return readTimeout(buffer, size, SR5_READ_TIMEOUT);
} }
#endif
#define isBigPacket(size) ((size) > BLOCKING_FACTOR + 7) #define isBigPacket(size) ((size) > BLOCKING_FACTOR + 7)
@ -25,7 +22,7 @@ void TsChannelBase::copyAndWriteSmallCrcPacket(uint8_t responseCode, const uint8
auto scratchBuffer = this->scratchBuffer; auto scratchBuffer = this->scratchBuffer;
// don't transmit too large a buffer // don't transmit too large a buffer
efiAssertVoid(OBD_PCM_Processor_Fault, !isBigPacket(size), "copyAndWriteSmallCrcPacket tried to transmit too large a packet") chDbgAssert(!isBigPacket(size), "copyAndWriteSmallCrcPacket tried to transmit too large a packet");
// If transmitting data, copy it in to place in the scratch buffer // If transmitting data, copy it in to place in the scratch buffer
// We want to prevent the data changing itself (higher priority threads could write // We want to prevent the data changing itself (higher priority threads could write
@ -39,7 +36,7 @@ void TsChannelBase::copyAndWriteSmallCrcPacket(uint8_t responseCode, const uint8
} }
void TsChannelBase::crcAndWriteBuffer(uint8_t responseCode, size_t size) { void TsChannelBase::crcAndWriteBuffer(uint8_t responseCode, size_t size) {
efiAssertVoid(OBD_PCM_Processor_Fault, !isBigPacket(size), "crcAndWriteBuffer tried to transmit too large a packet") chDbgAssert(!isBigPacket(size), "crcAndWriteBuffer tried to transmit too large a packet");
auto scratchBuffer = this->scratchBuffer; auto scratchBuffer = this->scratchBuffer;
// Index 0/1 = packet size (big endian) // Index 0/1 = packet size (big endian)
@ -89,9 +86,7 @@ TsChannelBase::TsChannelBase(const char *name) {
} }
void TsChannelBase::assertPacketSize(size_t size, bool allowLongPackets) { void TsChannelBase::assertPacketSize(size_t size, bool allowLongPackets) {
if (isBigPacket(size) && !allowLongPackets) { chDbgAssert(!(isBigPacket(size) && !allowLongPackets), "tried to send disallowed long packet");
firmwareError(OBD_PCM_Processor_Fault, "tried to send disallowed long packet of size %d", size);
}
} }
/** /**

View File

@ -7,28 +7,110 @@
*/ */
#pragma once #pragma once
#include "global.h"
/* to get ChibiOS hal config options */
#include "hal.h"
#include "tunerstudio_impl.h" #include "tunerstudio_impl.h"
#if (!TS_NO_PRIMARY && defined(TS_PRIMARY_PORT)) /* TODO: find better place */
#define HAS_PRIMARY true #define BLOCKING_FACTOR 256
#else
#define HAS_PRIMARY false
#endif
#if (!TS_NO_SECONDARY && defined(TS_SECONDARY_PORT)) /* TODO: From autogenerated, rework! */
#define HAS_SECONDARY true #define TS_BURN_COMMAND 'B'
#else #define TS_BURN_COMMAND_char B
#define HAS_SECONDARY false #define TS_CHUNK_WRITE_COMMAND 'C'
#endif #define TS_CHUNK_WRITE_COMMAND_char C
#define ts_command_e_TS_BENCH_CATEGORY 22
#if EFI_USB_SERIAL #define ts_command_e_TS_CLEAR_WARNINGS 17
#include "usbconsole.h" #define ts_command_e_TS_COMMAND_1 1
#endif // EFI_USB_SERIAL #define ts_command_e_TS_COMMAND_11 11
#define ts_command_e_TS_COMMAND_12 12
#if EFI_PROD_CODE #define ts_command_e_TS_COMMAND_13 13
#include "pin_repository.h" #define ts_command_e_TS_COMMAND_14 14
#endif #define ts_command_e_TS_COMMAND_15 15
#define ts_command_e_TS_COMMAND_16 16
#define ts_command_e_TS_COMMAND_4 4
#define ts_command_e_TS_COMMAND_5 5
#define ts_command_e_TS_COMMAND_9 9
#define ts_command_e_TS_CRAZY 32
#define ts_command_e_TS_DEBUG_MODE 0
#define ts_command_e_TS_GRAB_PEDAL_UP 6
#define ts_command_e_TS_GRAB_PEDAL_WOT 7
#define ts_command_e_TS_GRAB_TPS_CLOSED 2
#define ts_command_e_TS_GRAB_TPS_WOT 3
#define ts_command_e_TS_IGNITION_CATEGORY 18
#define ts_command_e_TS_INJECTOR_CATEGORY 19
#define ts_command_e_TS_RESET_TLE8888 8
#define ts_command_e_TS_UNUSED_23 23
#define ts_command_e_TS_UNUSED_25 25
#define ts_command_e_TS_UNUSED_26 26
#define ts_command_e_TS_UNUSED_27 27
#define ts_command_e_TS_UNUSED_28 28
#define ts_command_e_TS_UNUSED_29 29
#define ts_command_e_TS_UNUSED_30 30
#define ts_command_e_TS_UNUSED_31 31
#define ts_command_e_TS_UNUSED_CJ125_CALIB 24
#define ts_command_e_TS_WIDEBAND 21
#define ts_command_e_TS_WRITE_FLASH 10
#define ts_command_e_TS_X14 20
#define TS_COMMAND_F 'F'
#define TS_COMMAND_F_char F
#define TS_COMPOSITE_DISABLE 2
#define TS_COMPOSITE_ENABLE 1
#define TS_CRC_CHECK_COMMAND 'k'
#define TS_CRC_CHECK_COMMAND_char k
#define TS_EXECUTE 'E'
#define TS_EXECUTE_char E
#define TS_FILE_VERSION 20210312
#define TS_GET_COMPOSITE_BUFFER_DONE_DIFFERENTLY '8'
#define TS_GET_COMPOSITE_BUFFER_DONE_DIFFERENTLY_char 8
#define TS_GET_CONFIG_ERROR 'e'
#define TS_GET_CONFIG_ERROR_char e
#define TS_GET_FIRMWARE_VERSION 'V'
#define TS_GET_FIRMWARE_VERSION_char V
#define TS_GET_LOGGER_GET_BUFFER 'L'
#define TS_GET_LOGGER_GET_BUFFER_char L
#define TS_GET_OUTPUTS_SIZE '4'
#define TS_GET_OUTPUTS_SIZE_char 4
#define TS_GET_STRUCT '9'
#define TS_GET_STRUCT_char 9
#define TS_GET_TEXT 'G'
#define TS_GET_TEXT_char G
#define TS_HELLO_COMMAND 'S'
#define TS_HELLO_COMMAND_char S
#define TS_IO_TEST_COMMAND 'Z'
#define TS_IO_TEST_COMMAND_char Z
#define TS_ONLINE_PROTOCOL 'z'
#define TS_ONLINE_PROTOCOL_char z
#define TS_OUTPUT_COMMAND 'O'
#define TS_OUTPUT_COMMAND_char O
#define TS_PAGE_COMMAND 'P'
#define TS_PAGE_COMMAND_char P
#define TS_PERF_TRACE_BEGIN '_'
#define TS_PERF_TRACE_BEGIN_char _
#define TS_PERF_TRACE_GET_BUFFER 'b'
#define TS_PERF_TRACE_GET_BUFFER_char b
#define TS_PROTOCOL "001"
#define TS_QUERY_COMMAND 'Q'
#define TS_QUERY_COMMAND_char Q
#define TS_READ_COMMAND 'R'
#define TS_READ_COMMAND_char R
#define TS_RESPONSE_BURN_OK 4
#define TS_RESPONSE_COMMAND_OK 7
#define TS_RESPONSE_CRC_FAILURE 0x82
#define TS_RESPONSE_FRAMING_ERROR 0x8D
#define TS_RESPONSE_OK 0
#define TS_RESPONSE_OUT_OF_RANGE 0x84
#define TS_RESPONSE_UNDERRUN 0x80
#define TS_RESPONSE_UNRECOGNIZED_COMMAND 0x83
#define TS_SET_LOGGER_SWITCH 'l'
#define TS_SET_LOGGER_SWITCH_char l
#define TS_SINGLE_WRITE_COMMAND 'W'
#define TS_SINGLE_WRITE_COMMAND_char W
#define TS_TEST_COMMAND 't'
#define TS_TEST_COMMAND_char t
#define TS_TOTAL_OUTPUT_SIZE 1016
class TsChannelBase { class TsChannelBase {
public: public: