diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index bae7f7de45..395a116c31 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -99,6 +99,9 @@ EXTERN_ENGINE; +/* 1S */ +#define TS_COMMUNICATION_TIMEOUT TIME_MS2I(1000) + extern persistent_config_container_s persistentState; #if !defined(EFI_NO_CONFIG_WORKING_COPY) @@ -439,18 +442,18 @@ static bool isKnownCommand(char command) { TunerStudio tsInstance; -static void tsProcessOne(TsChannelBase* tsChannel) { +static int tsProcessOne(TsChannelBase* tsChannel) { validateStack("communication", STACK_USAGE_COMMUNICATION, 128); if (!tsChannel->isReady()) { chThdSleepMilliseconds(10); - return; + return -1; } tsState.totalCounter++; uint8_t firstByte; - int received = tsChannel->read(&firstByte, 1); + int received = tsChannel->readTimeout(&firstByte, 1, TS_COMMUNICATION_TIMEOUT); #if EFI_SIMULATOR logMsg("received %d\r\n", received); #endif @@ -461,19 +464,18 @@ static void tsProcessOne(TsChannelBase* tsChannel) { // assume there's connection loss and notify the bluetooth init code bluetoothSoftwareDisconnectNotify(); #endif /* EFI_BLUETOOTH_SETUP */ - return; + return -1; } - onDataArrived(); if (handlePlainCommand(tsChannel, firstByte)) { - return; + return -1; } uint8_t secondByte; - received = tsChannel->read(&secondByte, 1); + received = tsChannel->readTimeout(&secondByte, 1, TS_COMMUNICATION_TIMEOUT); if (received != 1) { tunerStudioError("TS: ERROR: no second byte"); - return; + return -1; } uint16_t incomingPacketSize = firstByte << 8 | secondByte; @@ -482,36 +484,35 @@ static void tsProcessOne(TsChannelBase* tsChannel) { efiPrintf("TunerStudio: invalid size: %d", incomingPacketSize); tunerStudioError("ERROR: CRC header size"); sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN); - return; + return -1; } - received = tsChannel->read((uint8_t* )tsChannel->scratchBuffer, 1); + received = tsChannel->readTimeout((uint8_t* )tsChannel->scratchBuffer, 1, TS_COMMUNICATION_TIMEOUT); if (received != 1) { tunerStudioError("ERROR: did not receive command"); sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN); - return; + return -1; } char command = tsChannel->scratchBuffer[0]; if (!isKnownCommand(command)) { efiPrintf("unexpected command %x", command); sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND); - return; + return -1; } #if EFI_SIMULATOR logMsg("command %c\r\n", command); #endif - received = tsChannel->read((uint8_t*)(tsChannel->scratchBuffer + 1), - incomingPacketSize + CRC_VALUE_SIZE - 1); int expectedSize = incomingPacketSize + CRC_VALUE_SIZE - 1; + received = tsChannel->readTimeout((uint8_t*)(tsChannel->scratchBuffer + 1), expectedSize, TS_COMMUNICATION_TIMEOUT); if (received != expectedSize) { efiPrintf("Got only %d bytes while expecting %d for command %c", received, expectedSize, command); tunerStudioError("ERROR: not enough bytes in stream"); sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN); - return; + return -1; } uint32_t expectedCrc = *(uint32_t*) (tsChannel->scratchBuffer + incomingPacketSize); @@ -528,14 +529,17 @@ static void tsProcessOne(TsChannelBase* tsChannel) { actualCrc, expectedCrc); tunerStudioError("ERROR: CRC issue"); sendErrorCode(tsChannel, TS_RESPONSE_CRC_FAILURE); - return; + return -1; } int success = tsInstance.handleCrcCommand(tsChannel, tsChannel->scratchBuffer, incomingPacketSize); if (!success) { efiPrintf("got unexpected TunerStudio command %x:%c", command, command); + return -1; } + + return 0; } void TunerstudioThread::ThreadTask() { @@ -548,7 +552,10 @@ void TunerstudioThread::ThreadTask() { // Until the end of time, process incoming messages. while(true) { - tsProcessOne(channel); + if (tsProcessOne(channel) == 0) + onDataArrived(true); + else + onDataArrived(false); } } diff --git a/firmware/console/console_io.cpp b/firmware/console/console_io.cpp index a788ad1ca3..e603dacc95 100644 --- a/firmware/console/console_io.cpp +++ b/firmware/console/console_io.cpp @@ -49,8 +49,8 @@ EXTERN_ENGINE; bool consoleByteArrived = false; -void onDataArrived(void) { - consoleByteArrived = true; +void onDataArrived(bool valid) { + consoleByteArrived = valid; } CommandHandler console_line_callback; diff --git a/firmware/console/console_io.h b/firmware/console/console_io.h index b01d8c62c4..486c6bdcf1 100644 --- a/firmware/console/console_io.h +++ b/firmware/console/console_io.h @@ -21,4 +21,4 @@ typedef void (*CommandHandler)(char *); void consoleOutputBuffer(const uint8_t *buf, int size); void startConsole(CommandHandler console_line_callback_p); -void onDataArrived(void); +void onDataArrived(bool valid); diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 6df3bb9ab7..dabeb2e3da 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -404,7 +404,6 @@ extern int totalLoggedBytes; offTimeMs = 50; onTimeMs = 450; } else if (consoleByteArrived) { - consoleByteArrived = false; offTimeMs = 100; onTimeMs = 33; #if EFI_INTERNAL_FLASH