Skip to content

Commit

Permalink
Merge pull request #3 from gabryelreyes/dev
Browse files Browse the repository at this point in the history
Improve reading from Stream and added clang formatting
  • Loading branch information
gabryelreyes authored Oct 6, 2023
2 parents fe2e718 + c7027ff commit 757cb21
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
38 changes: 38 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
BasedOnStyle: Microsoft
AccessModifierOffset: '-4'
AlignConsecutiveMacros: 'true'
AlignConsecutiveAssignments: 'true'
AlignConsecutiveDeclarations: 'true'
AlignTrailingComments: 'true'
AllowAllConstructorInitializersOnNextLine: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AllowShortBlocksOnASingleLine: 'false'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakTemplateDeclarations: 'true'
BreakAfterJavaFieldAnnotations: 'true'
BreakBeforeBraces: Allman
BreakConstructorInitializers: AfterColon
ColumnLimit: '120'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
IncludeBlocks: Regroup
IndentCaseLabels: 'false'
IndentWidth: '4'
Language: Cpp
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: All
PointerAlignment: Left
SortIncludes: 'false'
SortUsingDeclarations: 'true'
SpaceAfterCStyleCast: 'false'
SpaceAfterLogicalNot: 'false'
SpaceAfterTemplateKeyword: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeCtorInitializerColon: 'true'
SpaceBeforeInheritanceColon: 'true'
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyParentheses: 'false'
TabWidth: '4'
UseTab: Never
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
pip install --upgrade platformio
- name: Perform static checks on native environment
run: platformio check --environment native
run: platformio check --environment native --fail-on-defect=medium --fail-on-defect=high

# Perform tests
test:
Expand Down
3 changes: 2 additions & 1 deletion python/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__pycache__
__pycache__
venv
3 changes: 2 additions & 1 deletion python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
keyboard==0.13.5
keyboard==0.13.5
numpy==1.26.0
3 changes: 2 additions & 1 deletion src/SerialMuxProtCommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ SOFTWARE.
#define CONTROL_CHANNEL_NUMBER (0U)

/** DLC of Heartbeat Command. */
#define CONTROL_CHANNEL_PAYLOAD_LENGTH (CHANNEL_NAME_MAX_LEN + CONTROL_CHANNEL_PAYLOAD_DATA_LENGTH + CONTROL_CHANNEL_CMD_BYTE_LENGTH)
#define CONTROL_CHANNEL_PAYLOAD_LENGTH \
(CHANNEL_NAME_MAX_LEN + CONTROL_CHANNEL_PAYLOAD_DATA_LENGTH + CONTROL_CHANNEL_CMD_BYTE_LENGTH)

/** Index of the Command Byte of the Control Channel*/
#define CONTROL_CHANNEL_COMMAND_INDEX (0U)
Expand Down
35 changes: 29 additions & 6 deletions src/SerialMuxProtServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,16 @@ class SerialMuxProtServer
*/
void processRxData()
{
uint8_t expectedBytes = 0;
uint8_t dlc = 0;
uint8_t expectedBytes = 0;
uint8_t dlc = 0;
bool expectingHeader = false;

/* Determine how many bytes to read. */
if (HEADER_LEN > m_receivedBytes)
{
/* Header must be read. */
expectedBytes = (HEADER_LEN - m_receivedBytes);
expectedBytes = (HEADER_LEN - m_receivedBytes);
expectingHeader = true;
}
else
{
Expand All @@ -431,6 +433,27 @@ class SerialMuxProtServer
m_receivedBytes += m_stream.readBytes(&m_receiveFrame.raw[m_receivedBytes], expectedBytes);
}

if ((HEADER_LEN == m_receivedBytes) && (true == expectingHeader))
{
/* Header has been read. Get DLC of Rx Channel using Header. */
dlc = m_receiveFrame.fields.header.headerFields.m_dlc;

/* DLC = 0 means that the channel does not exist. */
if ((0U != dlc) && (MAX_RX_ATTEMPTS >= m_rxAttempts))
{
expectedBytes = (dlc - (m_receivedBytes - HEADER_LEN));
m_rxAttempts++;
}

if (0U != expectedBytes)
{
if (expectedBytes <= m_stream.available())
{
m_receivedBytes += m_stream.readBytes(&m_receiveFrame.raw[m_receivedBytes], expectedBytes);
}
}
}

/* Frame has been received. */
if ((0U != dlc) && ((HEADER_LEN + dlc) == m_receivedBytes))
{
Expand Down Expand Up @@ -630,11 +653,11 @@ class SerialMuxProtServer
* @param[in] size Size of the destination buffer in byte.
* @param[in] value Value.
*/
void uint32ToByteArray(uint8_t *buffer, size_t size, uint32_t value)
void uint32ToByteArray(uint8_t* buffer, size_t size, uint32_t value)
{
if ((nullptr != buffer) && (sizeof(uint32_t) <= size))
{
uint16_t hiBytes = ((value >> 16U) & 0xFFFF);
uint16_t hiBytes = ((value >> 16U) & 0xFFFF);
uint16_t lowBytes = ((value >> 0U) & 0xFFFF);

buffer[0U] = ((hiBytes >> 8U) & 0xFF);
Expand All @@ -651,7 +674,7 @@ class SerialMuxProtServer
* @param[out] value Destination integer.
* @returns true if succesfully parsed. Otherwise, false.
*/
bool byteArrayToUint32(const uint8_t *buffer, size_t size, uint32_t &value)
bool byteArrayToUint32(const uint8_t* buffer, size_t size, uint32_t& value)
{
bool isSuccess = false;

Expand Down

0 comments on commit 757cb21

Please sign in to comment.