Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Rework of WireProtocol (#1884)" #1897

Merged
merged 2 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/CLR/Include/WireProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ typedef struct WP_Message
WP_Packet m_header;
uint8_t* m_payload;

uint8_t* m_pos;
uint16_t m_size;
uint64_t m_payloadTicks;
int m_rxState;

void (*Initialize)(WP_Controller* parent);
void (*PrepareReception)(void);
void (*PrepareRequest)(unsigned int cmd, unsigned int flags, unsigned int payloadSize, unsigned char* payload);
Expand Down
20 changes: 2 additions & 18 deletions src/CLR/Include/WireProtocol_HAL_Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,8 @@
#include "WireProtocol.h"

//////////////////////////////////////////

///
/// @brief Receives n bytes from the Wire Protocol channel.
///
/// @param ptr Pointer to the buffer that will hold the received bytes.
/// @param size Number of bytes to read. On return it will have the number of bytes actually received.
/// @return bool true if any bytes where received, false otherwise.
///
bool WP_ReceiveBytes(uint8_t *ptr, uint16_t *size);

///
/// @brief Sends a message through the Wire Protocol channel.
///
/// @param message Message to send
/// @return bool true for transmition succesfull, false otherwise.
///
bool WP_TransmitMessage(WP_Message *message);

int WP_ReceiveBytes(uint8_t *ptr, unsigned short *size);
int WP_TransmitMessage(WP_Message *message);
void WP_CheckAvailableIncomingData();

#endif // _WIREPROTOCOL_HAL_INTERFACE_H_
4 changes: 2 additions & 2 deletions src/CLR/Include/WireProtocol_Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ extern "C"
{
#endif

void WP_Message_PrepareReception();
void WP_Message_Initialize(WP_Message *message);
void WP_Message_PrepareReception(WP_Message *message);
void WP_Message_PrepareRequest(
WP_Message *message,
uint32_t cmd,
Expand All @@ -56,7 +56,7 @@ extern "C"
int WP_Message_VerifyHeader(WP_Message *message);
int WP_Message_VerifyPayload(WP_Message *message);
void WP_Message_ReplyBadPacket(uint32_t flags);
int WP_Message_Process();
int WP_Message_Process(WP_Message *message);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions src/CLR/WireProtocol/WireProtocol_HAL_Interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

// provided as weak to be replaced by actual implementation by HAL interface
__nfweak bool WP_ReceiveBytes(uint8_t *ptr, uint16_t *size)
__nfweak int WP_ReceiveBytes(uint8_t *ptr, unsigned short *size)
{
(void)(ptr);
(void)(size);
Expand All @@ -26,7 +26,7 @@ __nfweak bool WP_ReceiveBytes(uint8_t *ptr, uint16_t *size)
}

// provided as weak to be replaced by actual implementation by HAL interface
__nfweak bool WP_TransmitMessage(WP_Message *message)
__nfweak int WP_TransmitMessage(WP_Message *message)
{
(void)(message);

Expand Down
126 changes: 59 additions & 67 deletions src/CLR/WireProtocol/WireProtocol_Message.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@
#include "WireProtocol_Message.h"

uint8_t receptionBuffer[sizeof(WP_Packet) + WP_PACKET_SIZE];
static uint16_t _lastOutboundMessage = 65535;
uint64_t _payloadTicks;
static uint8_t *_marker;
static uint8_t *_pos;
static uint16_t _size;
static uint8_t _rxState;
static WP_Message _inboundMessage;
static uint16_t lastOutboundMessage = 65535;
static uint8_t *marker;

// timeout to receive WP payload before bailing out
// 5 secs (100 nsecs units)
Expand Down Expand Up @@ -70,13 +65,15 @@ void WP_Message_Initialize(WP_Message *message)
{
memset(&message->m_header, 0, sizeof(message->m_header));
message->m_payload = NULL;
message->m_pos = NULL;
message->m_size = 0;
message->m_payloadTicks = 0;
message->m_rxState = ReceiveState_Idle;
}

void WP_Message_PrepareReception()
void WP_Message_PrepareReception(WP_Message *message)
{
_rxState = ReceiveState_Initialize;

WP_Message_Initialize(&_inboundMessage);
message->m_rxState = ReceiveState_Initialize;
}

void WP_Message_PrepareRequest(
Expand All @@ -88,7 +85,7 @@ void WP_Message_PrepareRequest(
{
memcpy(
&message->m_header.m_signature,
_marker ? _marker : (uint8_t *)MARKER_PACKET_V1,
marker ? marker : (uint8_t *)MARKER_PACKET_V1,
sizeof(message->m_header.m_signature));

#if defined(WP_IMPLEMENTS_CRC32)
Expand All @@ -97,7 +94,7 @@ void WP_Message_PrepareRequest(
message->m_header.m_crcData = 0;
#endif
message->m_header.m_cmd = cmd;
message->m_header.m_seq = _lastOutboundMessage++;
message->m_header.m_seq = lastOutboundMessage++;
message->m_header.m_seqReply = 0;
message->m_header.m_flags = flags;
message->m_header.m_size = payloadSize;
Expand All @@ -121,7 +118,7 @@ void WP_Message_PrepareReply(
{
memcpy(
&message->m_header.m_signature,
_marker ? _marker : (uint8_t *)MARKER_PACKET_V1,
marker ? marker : (uint8_t *)MARKER_PACKET_V1,
sizeof(message->m_header.m_signature));

#if defined(WP_IMPLEMENTS_CRC32)
Expand All @@ -130,7 +127,7 @@ void WP_Message_PrepareReply(
message->m_header.m_crcData = 0;
#endif
message->m_header.m_cmd = req->m_cmd;
message->m_header.m_seq = _lastOutboundMessage++;
message->m_header.m_seq = lastOutboundMessage++;
message->m_header.m_seqReply = req->m_seq;
message->m_header.m_flags = flags | WP_Flags_c_Reply;
message->m_header.m_size = payloadSize;
Expand Down Expand Up @@ -221,85 +218,81 @@ void WP_Message_ReplyBadPacket(uint32_t flags)
WP_TransmitMessage(&message);
}

int WP_Message_Process()
int WP_Message_Process(WP_Message *message)
{
uint8_t *buf = (uint8_t *)&_inboundMessage.m_header;
uint8_t *buf = (uint8_t *)&message->m_header;
uint16_t len;

while (true)
{
switch (_rxState)
switch (message->m_rxState)
{
case ReceiveState_Idle:
TRACE0(TRACE_STATE, "RxState==IDLE\n");
return true;

case ReceiveState_Initialize:
TRACE0(TRACE_STATE, "RxState==INIT\n");
WP_Message_Initialize(&_inboundMessage);

_rxState = ReceiveState_WaitingForHeader;
_pos = (uint8_t *)&_inboundMessage.m_header;
_size = sizeof(_inboundMessage.m_header);
WP_Message_Release(message);

message->m_rxState = ReceiveState_WaitingForHeader;
message->m_pos = (uint8_t *)&message->m_header;
message->m_size = sizeof(message->m_header);
break;

case ReceiveState_WaitingForHeader:
TRACE0(TRACE_STATE, "RxState==WaitForHeader\n");

if (WP_ReceiveBytes(_pos, &_size) == false)
if (WP_ReceiveBytes(message->m_pos, &message->m_size) == false)
{
// didn't receive the expected amount of bytes, returning false
TRACE0(TRACE_NODATA, "ReceiveBytes returned false - bailing out\n");

return false;
}

// Synch to the start of a message by looking for a valid MARKER
// Synch to the start of a message.
while (true)
{
len = sizeof(_inboundMessage.m_header) - _size;

len = sizeof(message->m_header) - message->m_size;
if (len <= 0)
{
break;
}

size_t lenCmp = min(len, sizeof(_inboundMessage.m_header.m_signature));
size_t lenCmp = min(len, sizeof(message->m_header.m_signature));

if (memcmp(&_inboundMessage.m_header, MARKER_DEBUGGER_V1, lenCmp) == 0)
if (memcmp(&message->m_header, MARKER_DEBUGGER_V1, lenCmp) == 0)
{
break;
}
if (memcmp(&_inboundMessage.m_header, MARKER_PACKET_V1, lenCmp) == 0)
if (memcmp(&message->m_header, MARKER_PACKET_V1, lenCmp) == 0)
{
break;
}

memmove(&buf[0], &buf[1], len - 1);

_pos--;
_size++;
message->m_pos--;
message->m_size++;
}

if (len >= sizeof(_inboundMessage.m_header.m_signature))
if (len >= sizeof(message->m_header.m_signature))
{
_rxState = ReceiveState_ReadingHeader;
message->m_rxState = ReceiveState_ReadingHeader;
}
break;

case ReceiveState_ReadingHeader:
TRACE0(TRACE_STATE, "RxState==ReadingHeader\n");
if (WP_ReceiveBytes(_pos, &_size) == false)
if (WP_ReceiveBytes(message->m_pos, &message->m_size) == false)
{
// didn't receive the expected amount of bytes, returning false
TRACE0(TRACE_NODATA, "ReceiveBytes returned false - bailing out\n");
return false;
}

if (_size == 0)
if (message->m_size == 0)
{
_rxState = ReceiveState_CompleteHeader;
message->m_rxState = ReceiveState_CompleteHeader;
}
break;

Expand All @@ -308,98 +301,97 @@ int WP_Message_Process()
TRACE0(TRACE_STATE, "RxState=CompleteHeader\n");
bool fBadPacket = true;

if (WP_Message_VerifyHeader(&_inboundMessage))
if (WP_Message_VerifyHeader(message))
{
TRACE(
TRACE_HEADERS,
"RXMSG: 0x%08X, 0x%08X, 0x%08X\n",
_inboundMessage.m_header.m_cmd,
_inboundMessage.m_header.m_flags,
_inboundMessage.m_header._size);
message->m_header.m_cmd,
message->m_header.m_flags,
message->m_header.m_size);

if (WP_App_ProcessHeader(&_inboundMessage))
if (WP_App_ProcessHeader(message))
{
fBadPacket = false;

if (_inboundMessage.m_header.m_size)
if (message->m_header.m_size)
{
if (_inboundMessage.m_payload == NULL)
if (message->m_payload == NULL) // Bad, no buffer...
{
// Bad, no buffer...
_rxState = ReceiveState_Initialize;
message->m_rxState = ReceiveState_Initialize;
}
else
{
_payloadTicks = HAL_Time_CurrentSysTicks();
_rxState = ReceiveState_ReadingPayload;
_pos = _inboundMessage.m_payload;
_size = _inboundMessage.m_header.m_size;
message->m_payloadTicks = HAL_Time_CurrentSysTicks();
message->m_rxState = ReceiveState_ReadingPayload;
message->m_pos = message->m_payload;
message->m_size = message->m_header.m_size;
}
}
else
{
_rxState = ReceiveState_CompletePayload;
message->m_rxState = ReceiveState_CompletePayload;
}
}
}

if (fBadPacket)
{
if ((_inboundMessage.m_header.m_flags & WP_Flags_c_NonCritical) == 0)
if ((message->m_header.m_flags & WP_Flags_c_NonCritical) == 0)
{
WP_Message_ReplyBadPacket(WP_Flags_c_BadHeader);
}

_rxState = ReceiveState_Initialize;
message->m_rxState = ReceiveState_Initialize;
}
}
break;

case ReceiveState_ReadingPayload:
{
TRACE(TRACE_STATE, "RxState=ReadingPayload. Expecting %d bytes.\n", message->_size);
TRACE(TRACE_STATE, "RxState=ReadingPayload. Expecting %d bytes.\n", message->m_size);

uint64_t curTicks = HAL_Time_CurrentSysTicks();

// If the time between consecutive payload bytes exceeds the timeout threshold then assume that
// the rest of the payload is not coming. Reinitialize to synch on the next header.

if (HAL_Time_SysTicksToTime_C(curTicks - _payloadTicks) < c_PayloadTimeout)
if (HAL_Time_SysTicksToTime_C(curTicks - message->m_payloadTicks) < c_PayloadTimeout)
{
_payloadTicks = curTicks;
message->m_payloadTicks = curTicks;

if (WP_ReceiveBytes(_pos, &_size) == false)
if (WP_ReceiveBytes(message->m_pos, &message->m_size) == false)
{
// didn't receive the expected amount of bytes, returning false
TRACE0(TRACE_NODATA, "ReceiveBytes returned false - bailing out\n");
return false;
}

if (_size == 0)
if (message->m_size == 0)
{
_rxState = ReceiveState_CompletePayload;
message->m_rxState = ReceiveState_CompletePayload;
}
}
else
{
TRACE0(TRACE_ERRORS, "RxError: Payload InterCharacterTimeout exceeded\n");
_rxState = ReceiveState_Initialize;
message->m_rxState = ReceiveState_Initialize;
}
}
break;

case ReceiveState_CompletePayload:
TRACE0(TRACE_STATE, "RxState=CompletePayload\n");
if (WP_Message_VerifyPayload(&_inboundMessage) == true)
if (WP_Message_VerifyPayload(message) == true)
{
WP_App_ProcessPayload(&_inboundMessage);
WP_App_ProcessPayload(message);
}
else
{
WP_Message_ReplyBadPacket(WP_Flags_c_BadPayload);
}

_rxState = ReceiveState_Initialize;
message->m_rxState = ReceiveState_Initialize;
break;

default:
Expand All @@ -417,7 +409,7 @@ void WP_SendProtocolMessage(WP_Message *message)
"TXMSG: 0x%08X, 0x%08X, 0x%08X\n",
message->m_header.m_cmd,
message->m_header.m_flags,
message->m_header._size);
message->m_header.m_size);
WP_TransmitMessage(message);
}

Expand All @@ -432,7 +424,7 @@ void WP_PrepareAndSendProtocolMessage(uint32_t cmd, uint32_t payloadSize, uint8_
"TXMSG: 0x%08X, 0x%08X, 0x%08X\n",
message.m_header.m_cmd,
message.m_header.m_flags,
message.m_header._size);
message.m_header.m_size);
WP_TransmitMessage(&message);
}

Expand Down
Loading