start making TS protocol testable (#2043)

* start pulling out a class

* pull function out of loop

* there we go

* ok there we go for real

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2020-12-08 02:05:43 -06:00 committed by GitHub
parent 96c29840ce
commit cc59a42e3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 104 deletions

View File

@ -64,6 +64,7 @@
#include "allsensors.h"
#include "tunerstudio.h"
#include "tunerstudio_impl.h"
#include "main_trigger_callback.h"
#include "flash_main.h"
@ -459,22 +460,20 @@ static bool isKnownCommand(char command) {
|| command == TS_GET_CONFIG_ERROR;
}
// this function runs indefinitely
void runBinaryProtocolLoop(ts_channel_s *tsChannel) {
int wasReady = false;
TunerStudioBase tsInstance;
while (true) {
static void tsProcessOne(ts_channel_s* tsChannel) {
validateStack("communication", STACK_USAGE_COMMUNICATION, 128);
int isReady = sr5IsReady(tsChannel);
if (!isReady) {
chThdSleepMilliseconds(10);
wasReady = false;
continue;
tsChannel->wasReady = false;
return;
}
if (!wasReady) {
wasReady = true;
if (!tsChannel->wasReady) {
tsChannel->wasReady = true;
// scheduleSimpleMsg(&logger, "ts channel is now ready ", hTimeNow());
}
@ -492,19 +491,19 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel) {
// assume there's connection loss and notify the bluetooth init code
bluetoothSoftwareDisconnectNotify();
#endif /* EFI_BLUETOOTH_SETUP */
continue;
return;
}
onDataArrived();
// scheduleMsg(logger, "Got first=%x=[%c]", firstByte, firstByte);
if (handlePlainCommand(tsChannel, firstByte))
continue;
return;
uint8_t secondByte;
received = sr5ReadData(tsChannel, &secondByte, 1);
if (received != 1) {
tunerStudioError("TS: ERROR: no second byte");
continue;
return;
}
// scheduleMsg(logger, "Got secondByte=%x=[%c]", secondByte, secondByte);
@ -514,21 +513,21 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel) {
scheduleMsg(&tsLogger, "TunerStudio: invalid size: %d", incomingPacketSize);
tunerStudioError("ERROR: CRC header size");
sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN);
continue;
return;
}
received = sr5ReadData(tsChannel, (uint8_t* )tsChannel->scratchBuffer, 1);
if (received != 1) {
tunerStudioError("ERROR: did not receive command");
sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN);
continue;
return;
}
char command = tsChannel->scratchBuffer[0];
if (!isKnownCommand(command)) {
scheduleMsg(&tsLogger, "unexpected command %x", command);
sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND);
continue;
return;
}
#if EFI_SIMULATOR
@ -543,7 +542,7 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel) {
expectedSize, command);
tunerStudioError("ERROR: not enough bytes in stream");
sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN);
continue;
return;
}
uint32_t expectedCrc = *(uint32_t*) (tsChannel->scratchBuffer + incomingPacketSize);
@ -560,13 +559,19 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel) {
actualCrc, expectedCrc);
tunerStudioError("ERROR: CRC issue");
sendErrorCode(tsChannel, TS_RESPONSE_CRC_FAILURE);
continue;
return;
}
int success = tunerStudioHandleCrcCommand(tsChannel, tsChannel->scratchBuffer, incomingPacketSize);
int success = tsInstance.handleCrcCommand(tsChannel, tsChannel->scratchBuffer, incomingPacketSize);
if (!success)
print("got unexpected TunerStudio command %x:%c\r\n", command, command);
}
void runBinaryProtocolLoop(ts_channel_s *tsChannel)
{
// Until the end of time, process incoming messages.
while(true) {
tsProcessOne(tsChannel);
}
}
@ -724,8 +729,7 @@ bool handlePlainCommand(ts_channel_s *tsChannel, uint8_t command) {
static int transmitted = 0;
int tunerStudioHandleCrcCommand(ts_channel_s *tsChannel, char *data, int incomingPacketSize) {
int TunerStudioBase::handleCrcCommand(ts_channel_s *tsChannel, char *data, int incomingPacketSize) {
ScopePerf perf(PE::TunerStudioHandleCrcCommand);
char command = data[0];

View File

@ -36,7 +36,6 @@ extern tunerstudio_counters_s tsState;
* handle non CRC wrapped command
*/
bool handlePlainCommand(ts_channel_s *tsChannel, uint8_t command);
int tunerStudioHandleCrcCommand(ts_channel_s *tsChannel, char *data, int incomingPacketSize);
/**
* this command is part of protocol initialization
@ -67,28 +66,9 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel);
#endif
typedef pre_packed struct
post_packed {
post_packed {
short int offset;
short int count;
} TunerStudioOchRequest;
typedef pre_packed struct
post_packed {
short int offset;
short int count;
} TunerStudioWriteChunkRequest;
typedef pre_packed struct
post_packed {
short int offset;
short int count;
} TunerStudioReadRequest;
typedef pre_packed struct
post_packed {
short int offset;
unsigned char value;
} TunerStudioWriteValueRequest;
} TunerStudioWriteChunkRequest;
#endif /* EFI_TUNER_STUDIO */

View File

@ -0,0 +1,8 @@
#pragma once
class ts_channel_s;
class TunerStudioBase {
public:
int handleCrcCommand(ts_channel_s *tsChannel, char *data, int incomingPacketSize);
};

View File

@ -34,6 +34,8 @@ struct ts_channel_s {
#if TS_UART_DMA_MODE || PRIMARY_UART_DMA_MODE || TS_UART_MODE
UARTDriver *uartp = nullptr;
#endif // TS_UART_DMA_MODE
bool wasReady = false;
};
#define CRC_VALUE_SIZE 4