rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Member Functions | Data Fields | Private Member Functions
TsChannelBase Class Referenceabstract

#include <tunerstudio_io.h>

Inheritance diagram for TsChannelBase:
Inheritance graph
[legend]
Collaboration diagram for TsChannelBase:
Collaboration graph
[legend]

Public Member Functions

 TsChannelBase (const char *name)
 
virtual void write (const uint8_t *buffer, size_t size, bool isEndOfPacket=false)=0
 
virtual size_t readTimeout (uint8_t *buffer, size_t size, int timeout)=0
 
virtual void flush ()
 
virtual bool isConfigured () const
 
virtual bool isReady () const
 
virtual void stop ()
 
size_t read (uint8_t *buffer, size_t size)
 
virtual void writeCrcPacket (uint8_t responseCode, const uint8_t *buf, size_t size, bool allowLongPackets=false)
 
void sendResponse (ts_response_format_e mode, const uint8_t *buffer, int size, bool allowLongPackets=false)
 
void assertPacketSize (size_t size, bool allowLongPackets)
 
uint32_t writePacketHeader (const uint8_t responseCode, const size_t size)
 
void crcAndWriteBuffer (const uint8_t responseCode, const size_t size)
 
void copyAndWriteSmallCrcPacket (uint8_t responseCode, const uint8_t *buf, size_t size)
 
void writeCrcResponse (uint8_t responseCode)
 

Data Fields

int bytesIn = 0
 
int bytesOut = 0
 
char scratchBuffer [scratchBuffer_SIZE+30]
 
page1_s page1
 
const charname
 
bool in_sync = false
 

Private Member Functions

bool isBigPacket (size_t size)
 
void writeCrcPacketLarge (uint8_t responseCode, const uint8_t *buf, size_t size)
 

Detailed Description

Definition at line 30 of file tunerstudio_io.h.

Constructor & Destructor Documentation

◆ TsChannelBase()

TsChannelBase::TsChannelBase ( const char name)

Definition at line 91 of file tunerstudio_io.cpp.

91 {
92 this->name = p_name;
93}

Member Function Documentation

◆ assertPacketSize()

void TsChannelBase::assertPacketSize ( size_t  size,
bool  allowLongPackets 
)

Definition at line 95 of file tunerstudio_io.cpp.

95 {
96 if (isBigPacket(size) && !allowLongPackets) {
97 criticalError("[USE PROPER CONSOLE VERSION ] disallowed long packet of size %d", size);
98 }
99}
bool isBigPacket(size_t size)
composite packet size

Referenced by TunerStudio::cmdOutputChannels(), and writeCrcPacket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ copyAndWriteSmallCrcPacket()

void TsChannelBase::copyAndWriteSmallCrcPacket ( uint8_t  responseCode,
const uint8_t *  buf,
size_t  size 
)

Definition at line 26 of file tunerstudio_io.cpp.

26 {
27 // don't transmit too large a buffer
28 criticalAssertVoid(!isBigPacket(size), "copyAndWriteSmallCrcPacket tried to transmit too large a packet")
29
30 // If transmitting data, copy it in to place in the scratch buffer
31 // We want to prevent the data changing itself (higher priority threads could write
32 // tsOutputChannels) during the CRC computation. Instead compute the CRC on our
33 // local buffer that nobody else will write.
34 if (size) {
35 memcpy(scratchBuffer + TS_PACKET_HEADER_SIZE, buf, size);
36 }
37
38 crcAndWriteBuffer(responseCode, size);
39}
void crcAndWriteBuffer(const uint8_t responseCode, const size_t size)
char scratchBuffer[scratchBuffer_SIZE+30]

Referenced by writeCrcPacket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ crcAndWriteBuffer()

void TsChannelBase::crcAndWriteBuffer ( const uint8_t  responseCode,
const size_t  size 
)

Definition at line 41 of file tunerstudio_io.cpp.

41 {
42 criticalAssertVoid(!isBigPacket(size), "crcAndWriteBuffer tried to transmit too large a packet")
43
44 // Index 0/1 = packet size (big endian)
45 *(uint16_t*)scratchBuffer = SWAP_UINT16(size + 1);
46 // Index 2 = response code
47 scratchBuffer[2] = responseCode;
48
49 // CRC is computed on the responseCode and payload but not length
50 uint32_t crc = crc32(&scratchBuffer[2], size + 1); // command part of CRC
51
52 // Place the CRC at the end
53 crc = SWAP_UINT32(crc);
54 memcpy(scratchBuffer + size + TS_PACKET_HEADER_SIZE, &crc, sizeof(crc));
55
56 // Write to the underlying stream
57 write(reinterpret_cast<uint8_t*>(scratchBuffer), size + 7, true);
58 flush();
59}
virtual void flush()
virtual void write(const uint8_t *buffer, size_t size, bool isEndOfPacket=false)=0
uint32_t SWAP_UINT32(uint32_t x)
Definition efilib.h:27
uint16_t SWAP_UINT16(uint16_t x)
Definition efilib.h:22

Referenced by TunerStudio::cmdOutputChannels(), and copyAndWriteSmallCrcPacket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flush()

virtual void TsChannelBase::flush ( )
inlinevirtual

Definition at line 38 of file tunerstudio_io.h.

38{ }

Referenced by crcAndWriteBuffer(), TunerStudio::handlePlainCommand(), TunerStudio::handleScatteredReadCommand(), handleTestCommand(), sendResponse(), and writeCrcPacketLarge().

Here is the caller graph for this function:

◆ isBigPacket()

bool TsChannelBase::isBigPacket ( size_t  size)
private

Definition at line 22 of file tunerstudio_io.cpp.

22 {
23 return ((TS_PACKET_HEADER_SIZE + size + TS_PACKET_TAIL_SIZE) > sizeof(scratchBuffer));
24}

Referenced by assertPacketSize(), copyAndWriteSmallCrcPacket(), crcAndWriteBuffer(), and writeCrcPacket().

Here is the caller graph for this function:

◆ isConfigured()

virtual bool TsChannelBase::isConfigured ( ) const
inlinevirtual

Definition at line 39 of file tunerstudio_io.h.

39{ return true; }

◆ isReady()

virtual bool TsChannelBase::isReady ( ) const
inlinevirtual

Definition at line 40 of file tunerstudio_io.h.

40{ return true; }

Referenced by tsProcessOne().

Here is the caller graph for this function:

◆ read()

size_t TsChannelBase::read ( uint8_t *  buffer,
size_t  size 
)

Definition at line 17 of file tunerstudio_io.cpp.

17 {
18 return readTimeout(buffer, size, SR5_READ_TIMEOUT);
19}
virtual size_t readTimeout(uint8_t *buffer, size_t size, int timeout)=0
static BigBufferHandle buffer
Here is the call graph for this function:

◆ readTimeout()

virtual size_t TsChannelBase::readTimeout ( uint8_t *  buffer,
size_t  size,
int  timeout 
)
pure virtual

Implemented in SerialTsChannel, UartTsChannel, and UartDmaTsChannel.

Referenced by bluetoothSoftwareDisconnectNotify(), btReadLine(), read(), and tsProcessOne().

Here is the caller graph for this function:

◆ sendResponse()

void TsChannelBase::sendResponse ( ts_response_format_e  mode,
const uint8_t *  buffer,
int  size,
bool  allowLongPackets = false 
)

Definition at line 121 of file tunerstudio_io.cpp.

121 {
122 if (mode == TS_CRC) {
123 writeCrcPacket(TS_RESPONSE_OK, buffer, size, allowLongPackets);
124 } else {
125 if (size > 0) {
126 write(buffer, size, true);
127 flush();
128 }
129 }
130}
virtual void writeCrcPacket(uint8_t responseCode, const uint8_t *buf, size_t size, bool allowLongPackets=false)
@ TS_CRC

Referenced by TunerStudio::handleCrc32Check(), TunerStudio::handleCrcCommand(), handleGetConfigErorr(), handleGetVersion(), TunerStudio::handlePageReadCommand(), TunerStudio::handleQueryCommand(), and sendOkResponse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

virtual void TsChannelBase::stop ( )
inlinevirtual

Reimplemented in SerialTsChannel, and UartTsChannel.

Definition at line 41 of file tunerstudio_io.h.

41{ }

Referenced by findBaudIndex(), and runCommands().

Here is the caller graph for this function:

◆ write()

virtual void TsChannelBase::write ( const uint8_t *  buffer,
size_t  size,
bool  isEndOfPacket = false 
)
pure virtual

◆ writeCrcPacket()

void TsChannelBase::writeCrcPacket ( uint8_t  responseCode,
const uint8_t *  buf,
size_t  size,
bool  allowLongPackets = false 
)
virtual

Adds size to the beginning of a packet and a crc32 at the end. Then send the packet.

Definition at line 104 of file tunerstudio_io.cpp.

104 {
105 // don't transmit a null buffer...
106 if (!buf) {
107 size = 0;
108 }
109
110 assertPacketSize(size, allowLongPackets);
111
112 if (isBigPacket(size)) {
113 // for larger packets we do not use a buffer for CRC calculation meaning data is now allowed to modify while pending
114 writeCrcPacketLarge(responseCode, buf, size);
115 } else {
116 // for small packets we use a buffer for CRC calculation
117 copyAndWriteSmallCrcPacket(responseCode, buf, size);
118 }
119}
void copyAndWriteSmallCrcPacket(uint8_t responseCode, const uint8_t *buf, size_t size)
void writeCrcPacketLarge(uint8_t responseCode, const uint8_t *buf, size_t size)
void assertPacketSize(size_t size, bool allowLongPackets)

Referenced by handleGetText(), and sendResponse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeCrcPacketLarge()

void TsChannelBase::writeCrcPacketLarge ( uint8_t  responseCode,
const uint8_t *  buf,
size_t  size 
)
private

Definition at line 72 of file tunerstudio_io.cpp.

72 {
73 uint8_t crcBuffer[4];
74
75 // Command part of CRC
76 uint32_t crc = writePacketHeader(responseCode, size);
77 // Data part of CRC
78 crc = crc32inc((void*)buf, crc, size);
79 *(uint32_t*)crcBuffer = SWAP_UINT32(crc);
80
81 // If data, write that
82 if (size) {
83 write(buf, size, /*isEndOfPacket*/false);
84 }
85
86 // Lastly the CRC footer
87 write(crcBuffer, sizeof(crcBuffer), /*isEndOfPacket*/true);
88 flush();
89}
uint32_t writePacketHeader(const uint8_t responseCode, const size_t size)

Referenced by writeCrcPacket(), and writeCrcResponse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeCrcResponse()

void TsChannelBase::writeCrcResponse ( uint8_t  responseCode)
inline

Definition at line 76 of file tunerstudio_io.h.

76 {
77 writeCrcPacketLarge(responseCode, nullptr, 0);
78 }

Referenced by handleBurnCommand(), TunerStudio::handleExecuteCommand(), and sendErrorCode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writePacketHeader()

uint32_t TsChannelBase::writePacketHeader ( const uint8_t  responseCode,
const size_t  size 
)

Definition at line 61 of file tunerstudio_io.cpp.

61 {
62 uint8_t headerBuffer[3];
63 *(uint16_t*)headerBuffer = SWAP_UINT16(size + 1);
64 *(uint8_t*)(headerBuffer + 2) = responseCode;
65 // Write header
66 write(headerBuffer, sizeof(headerBuffer), /*isEndOfPacket*/false);
67
68 // Command part of CRC
69 return crc32((void*)(headerBuffer + 2), 1);
70}

Referenced by TunerStudio::handleScatteredReadCommand(), and writeCrcPacketLarge().

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ bytesIn

int TsChannelBase::bytesIn = 0

◆ bytesOut

int TsChannelBase::bytesOut = 0

Definition at line 47 of file tunerstudio_io.h.

Referenced by SerialTsChannel::write(), and UartTsChannel::write().

◆ in_sync

bool TsChannelBase::in_sync = false

Definition at line 90 of file tunerstudio_io.h.

Referenced by tsProcessOne().

◆ name

const char* TsChannelBase::name

Definition at line 68 of file tunerstudio_io.h.

Referenced by TsChannelBase(), tsProcessOne(), and tunerStudioDebug().

◆ page1

page1_s TsChannelBase::page1

Definition at line 66 of file tunerstudio_io.h.

Referenced by getWorkingPageAddr(), and TunerStudio::handleScatteredReadCommand().

◆ scratchBuffer

char TsChannelBase::scratchBuffer[scratchBuffer_SIZE+30]

The documentation for this class was generated from the following files: