From 425055d153f5bf8c3a06b44a5d510eb54a2e6d17 Mon Sep 17 00:00:00 2001 From: gabryelreyes Date: Wed, 27 Sep 2023 11:17:57 +0200 Subject: [PATCH 1/4] Read Payload after reading header in one call. Increases performance, as it requires only minimum 1 call to read frame. Before, at least 2 calls were required. --- src/SerialMuxProtServer.hpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/SerialMuxProtServer.hpp b/src/SerialMuxProtServer.hpp index c57a631..533ad81 100644 --- a/src/SerialMuxProtServer.hpp +++ b/src/SerialMuxProtServer.hpp @@ -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 { @@ -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)) { From 7d17632e97ab969b182c2a220c7cd8744a057542 Mon Sep 17 00:00:00 2001 From: gabryelreyes Date: Wed, 27 Sep 2023 12:19:41 +0200 Subject: [PATCH 2/4] Updated CI check command. Fail on defects medium and high --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 69c78c4..5efd702 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: From 8f44ea76ab15b9a3214c98623c7805c2c6e3cefa Mon Sep 17 00:00:00 2001 From: gabryelreyes Date: Wed, 27 Sep 2023 12:20:26 +0200 Subject: [PATCH 3/4] Added clang-format and fixed formatting --- .clang-format | 38 +++++++++++++++++++++++++++++++++++++ src/SerialMuxProtCommon.hpp | 3 ++- src/SerialMuxProtServer.hpp | 6 +++--- 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..34ad827 --- /dev/null +++ b/.clang-format @@ -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 \ No newline at end of file diff --git a/src/SerialMuxProtCommon.hpp b/src/SerialMuxProtCommon.hpp index 9b8a558..959d150 100644 --- a/src/SerialMuxProtCommon.hpp +++ b/src/SerialMuxProtCommon.hpp @@ -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) diff --git a/src/SerialMuxProtServer.hpp b/src/SerialMuxProtServer.hpp index 533ad81..27d6f0c 100644 --- a/src/SerialMuxProtServer.hpp +++ b/src/SerialMuxProtServer.hpp @@ -653,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); @@ -674,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; From c7027ff88e5332bbf73f84a52297bd08b671a246 Mon Sep 17 00:00:00 2001 From: gabryelreyes Date: Wed, 27 Sep 2023 12:21:14 +0200 Subject: [PATCH 4/4] Updated python gitignore and requirements --- python/.gitignore | 3 ++- python/requirements.txt | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/python/.gitignore b/python/.gitignore index ed8ebf5..c9f18c9 100644 --- a/python/.gitignore +++ b/python/.gitignore @@ -1 +1,2 @@ -__pycache__ \ No newline at end of file +__pycache__ +venv \ No newline at end of file diff --git a/python/requirements.txt b/python/requirements.txt index 1cd9905..0212aec 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -1 +1,2 @@ -keyboard==0.13.5 \ No newline at end of file +keyboard==0.13.5 +numpy==1.26.0 \ No newline at end of file