From ab731f68dad1358b0db899f5e0556d12b955ccde Mon Sep 17 00:00:00 2001 From: Chirag Bansal Date: Thu, 30 Mar 2023 21:52:26 +0530 Subject: [PATCH 1/8] fixing uart to read only once --- examples/platform/silabs/SiWx917/uart.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/platform/silabs/SiWx917/uart.cpp b/examples/platform/silabs/SiWx917/uart.cpp index 85e81263035348..94fdb0be1f0677 100644 --- a/examples/platform/silabs/SiWx917/uart.cpp +++ b/examples/platform/silabs/SiWx917/uart.cpp @@ -139,8 +139,7 @@ void uartConsoleInit(void) DEBUGOUT("\r\n Receives data success \r\n"); } - // Creating the receive event as a temp buffer - // this is not used anywhere + // Creating the receive event and storing in the rx_buffer status = UARTdrv->Receive(&rx_buffer, 1); NVIC_EnableIRQ(USART0_IRQn); @@ -181,7 +180,10 @@ int16_t uartConsoleRead(char * Buf, uint16_t NbBytesToRead) { return UART_CONSOLE_ERR; } - status = UARTdrv->Receive(Buf, NbBytesToRead); + // storing the contents of rx_buffer in the Buf + *Buf = (char)rx_buffer; + // creating the uart receive and storing in the rx_buffer + status = UARTdrv->Receive(&rx_buffer, NbBytesToRead); if (status != ARM_DRIVER_OK) { return status; From 6236aba2d89a91a6ed0d1b8e8ae9bbb31c3cbcbb Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Thu, 30 Mar 2023 16:25:40 +0000 Subject: [PATCH 2/8] Restyled by clang-format --- examples/platform/silabs/SiWx917/uart.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/platform/silabs/SiWx917/uart.cpp b/examples/platform/silabs/SiWx917/uart.cpp index 94fdb0be1f0677..f07c66f6b45143 100644 --- a/examples/platform/silabs/SiWx917/uart.cpp +++ b/examples/platform/silabs/SiWx917/uart.cpp @@ -181,7 +181,7 @@ int16_t uartConsoleRead(char * Buf, uint16_t NbBytesToRead) return UART_CONSOLE_ERR; } // storing the contents of rx_buffer in the Buf - *Buf = (char)rx_buffer; + *Buf = (char) rx_buffer; // creating the uart receive and storing in the rx_buffer status = UARTdrv->Receive(&rx_buffer, NbBytesToRead); if (status != ARM_DRIVER_OK) From 0b559f24a4c0e2dbdea43a54c0f995ff9dbd19c8 Mon Sep 17 00:00:00 2001 From: Chirag Bansal Date: Mon, 10 Apr 2023 11:06:51 +0530 Subject: [PATCH 3/8] Uart fix to read character by character --- examples/platform/silabs/SiWx917/uart.cpp | 13 +- src/lib/shell/BUILD.gn | 6 +- src/lib/shell/MainLoopEFR32.cpp | 4 - src/lib/shell/MainLoopSiWx917.cpp | 207 ++++++++++++++++++ ...streamer_efr32.cpp => streamer_silabs.cpp} | 0 5 files changed, 211 insertions(+), 19 deletions(-) create mode 100644 src/lib/shell/MainLoopSiWx917.cpp rename src/lib/shell/{streamer_efr32.cpp => streamer_silabs.cpp} (100%) diff --git a/examples/platform/silabs/SiWx917/uart.cpp b/examples/platform/silabs/SiWx917/uart.cpp index 3c6f913437367e..74e77f43f11ad4 100644 --- a/examples/platform/silabs/SiWx917/uart.cpp +++ b/examples/platform/silabs/SiWx917/uart.cpp @@ -33,13 +33,8 @@ extern ARM_DRIVER_USART Driver_USART0; static ARM_DRIVER_USART * UARTdrv = &Driver_USART0; ARM_USART_CAPABILITIES drv_capabilities; -ARM_USART_STATUS status; - -#define BUFFER_SIZE 1 -uint8_t rx_buffer; #define BAUD_VALUE 115200 - #define UART_CONSOLE_ERR -1 // Negative value in case of UART Console action failed. Triggers a failure for PW_RPC void ARM_USART_SignalEvent(uint32_t event); @@ -141,9 +136,6 @@ void uartConsoleInit(void) DEBUGOUT("\r\n Receives data success \r\n"); } - // Creating the receive event and storing in the rx_buffer - status = UARTdrv->Receive(&rx_buffer, 1); - NVIC_EnableIRQ(USART0_IRQn); NVIC_SetPriority(USART0_IRQn, 7); @@ -182,10 +174,7 @@ int16_t uartConsoleRead(char * Buf, uint16_t NbBytesToRead) { return UART_CONSOLE_ERR; } - // storing the contents of rx_buffer in the Buf - *Buf = (char) rx_buffer; - // creating the uart receive and storing in the rx_buffer - status = UARTdrv->Receive(&rx_buffer, NbBytesToRead); + status = UARTdrv->Receive(Buf, NbBytesToRead); if (status != ARM_DRIVER_OK) { return status; diff --git a/src/lib/shell/BUILD.gn b/src/lib/shell/BUILD.gn index 43b0766a871328..bb5ce86b8d8f20 100644 --- a/src/lib/shell/BUILD.gn +++ b/src/lib/shell/BUILD.gn @@ -53,12 +53,12 @@ static_library("shell") { } else if (chip_device_platform == "efr32") { sources += [ "MainLoopEFR32.cpp", - "streamer_efr32.cpp", + "streamer_silabs.cpp", ] } else if (chip_device_platform == "SiWx917") { sources += [ - "MainLoopEFR32.cpp", - "streamer_efr32.cpp", + "MainLoopSiWx917.cpp", + "streamer_silabs.cpp", ] } else if (chip_device_platform == "k32w0") { sources += [ diff --git a/src/lib/shell/MainLoopEFR32.cpp b/src/lib/shell/MainLoopEFR32.cpp index b43673ba526dd6..7996300e2f5aac 100644 --- a/src/lib/shell/MainLoopEFR32.cpp +++ b/src/lib/shell/MainLoopEFR32.cpp @@ -176,10 +176,6 @@ void ProcessShellLine(intptr_t args) } } MemoryFree(line); -#ifdef BRD4325A - // small delay for uart print - vTaskDelay(1); -#endif streamer_printf(streamer_get(), kShellPrompt); } diff --git a/src/lib/shell/MainLoopSiWx917.cpp b/src/lib/shell/MainLoopSiWx917.cpp new file mode 100644 index 00000000000000..9093a87a19acaf --- /dev/null +++ b/src/lib/shell/MainLoopSiWx917.cpp @@ -0,0 +1,207 @@ +/* + * + * Copyright (c) 2021 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "matter_shell.h" +#include "streamer.h" +#include +#include +#include + +#include +#include + +using chip::FormatCHIPError; +using chip::Platform::MemoryAlloc; +using chip::Platform::MemoryFree; +using chip::Shell::Engine; +using chip::Shell::streamer_get; + +namespace { + +constexpr const char kShellPrompt[] = "matterCli > "; + +// max > 1 +void ReadLine(char * buffer, size_t max) +{ + size_t line_sz = 0; + + // Read in characters until we get a line ending or EOT. + for (bool done = false; !done;) + { + // Stop reading if we've run out of space in the buffer (still need to null-terminate). + if (line_sz >= max - 1u) + { + buffer[max - 1] = '\0'; + break; + } + + if (streamer_read(streamer_get(), buffer + line_sz, 1) != 1) + { + continue; + } + chip::WaitForShellActivity(); + + // Process character we just read. + switch (buffer[line_sz]) + { + case '\r': + case '\n': + streamer_printf(streamer_get(), "\r\n"); + buffer[line_sz] = '\0'; + line_sz++; + done = true; + break; + case 0x7F: + // Do not accept backspace character (i.e. don't increment line_sz) and remove 1 additional character if it exists. + if (line_sz >= 1u) + { + streamer_printf(streamer_get(), "\b \b"); + line_sz--; + } + break; + default: + if (isprint(static_cast(buffer[line_sz])) || buffer[line_sz] == '\t') + { + streamer_printf(streamer_get(), "%c", buffer[line_sz]); + line_sz++; + } + break; + } + } +} + +bool IsSeparator(char ch) +{ + return (ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n'); +} + +bool IsEscape(char ch) +{ + return (ch == '\\'); +} + +bool IsEscapable(char ch) +{ + return IsSeparator(ch) || IsEscape(ch); +} + +int TokenizeLine(char * buffer, char ** tokens, int max_tokens) +{ + size_t len = strlen(buffer); + int cursor = 0; + size_t i = 0; + + // Strip leading spaces + while (buffer[i] && buffer[i] == ' ') + { + i++; + } + + if (len <= i) + { + return 0; + } + + // The first token starts at the beginning. + tokens[cursor++] = &buffer[i]; + + for (; i < len && cursor < max_tokens; i++) + { + if (IsEscape(buffer[i]) && IsEscapable(buffer[i + 1])) + { + // include the null terminator: strlen(cmd) = strlen(cmd + 1) + 1 + memmove(&buffer[i], &buffer[i + 1], strlen(&buffer[i])); + } + else if (IsSeparator(buffer[i])) + { + buffer[i] = 0; + // Don't treat the previous character as a separator if this one is 0 + // otherwise the trailing space will become a token + if (!IsSeparator(buffer[i + 1]) && buffer[i + 1] != 0) + { + tokens[cursor++] = &buffer[i + 1]; + } + } + } + // If for too many arguments, overwrite last entry with guard. + if (cursor >= max_tokens) + { + cursor = max_tokens - 1; + } + + tokens[cursor] = nullptr; + + return cursor; +} + +void ProcessShellLine(intptr_t args) +{ + int argc; + char * argv[CHIP_SHELL_MAX_TOKENS]; + + char * line = reinterpret_cast(args); + argc = TokenizeLine(line, argv, CHIP_SHELL_MAX_TOKENS); + + if (argc > 0) + { + CHIP_ERROR retval = Engine::Root().ExecCommand(argc, argv); + + if (retval != CHIP_NO_ERROR) + { + char errorStr[160]; + bool errorStrFound = FormatCHIPError(errorStr, sizeof(errorStr), retval); + if (!errorStrFound) + { + errorStr[0] = 0; + } + streamer_printf(streamer_get(), "Error %s: %s\r\n", argv[0], errorStr); + } + else + { + streamer_printf(streamer_get(), "Done\r\n", argv[0]); + } + } + MemoryFree(line); +#ifdef BRD4325A + // small delay for uart print + vTaskDelay(10); +#endif + streamer_printf(streamer_get(), kShellPrompt); +} + +} // namespace + +namespace chip { +namespace Shell { + +void Engine::RunMainLoop() +{ + streamer_printf(streamer_get(), kShellPrompt); + + while (true) + { + char * line = static_cast(chip::Platform::MemoryAlloc(CHIP_SHELL_MAX_LINE_SIZE)); ReadLine(line, CHIP_SHELL_MAX_LINE_SIZE); +#if CONFIG_DEVICE_LAYER + DeviceLayer::PlatformMgr().ScheduleWork(ProcessShellLine, reinterpret_cast(line)); +#else + ProcessShellLine(reinterpret_cast(line)); +#endif + } +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/streamer_efr32.cpp b/src/lib/shell/streamer_silabs.cpp similarity index 100% rename from src/lib/shell/streamer_efr32.cpp rename to src/lib/shell/streamer_silabs.cpp From cff962c29360768e03839b49f59aff79d0bcf305 Mon Sep 17 00:00:00 2001 From: Chirag Bansal Date: Mon, 10 Apr 2023 11:19:03 +0530 Subject: [PATCH 4/8] restyling the PR --- src/lib/shell/MainLoopSiWx917.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib/shell/MainLoopSiWx917.cpp b/src/lib/shell/MainLoopSiWx917.cpp index 9093a87a19acaf..20127e04651e42 100644 --- a/src/lib/shell/MainLoopSiWx917.cpp +++ b/src/lib/shell/MainLoopSiWx917.cpp @@ -176,10 +176,10 @@ void ProcessShellLine(intptr_t args) } } MemoryFree(line); -#ifdef BRD4325A + // small delay for uart print vTaskDelay(10); -#endif + streamer_printf(streamer_get(), kShellPrompt); } @@ -194,7 +194,8 @@ void Engine::RunMainLoop() while (true) { - char * line = static_cast(chip::Platform::MemoryAlloc(CHIP_SHELL_MAX_LINE_SIZE)); ReadLine(line, CHIP_SHELL_MAX_LINE_SIZE); + char * line = static_cast(chip::Platform::MemoryAlloc(CHIP_SHELL_MAX_LINE_SIZE)); + ReadLine(line, CHIP_SHELL_MAX_LINE_SIZE); #if CONFIG_DEVICE_LAYER DeviceLayer::PlatformMgr().ScheduleWork(ProcessShellLine, reinterpret_cast(line)); #else From 4b7358607f737eeb8de3eb9e998a0073577edc85 Mon Sep 17 00:00:00 2001 From: Chirag Bansal Date: Mon, 10 Apr 2023 20:37:33 +0530 Subject: [PATCH 5/8] removing the duplicate files --- src/lib/shell/BUILD.gn | 9 ++------- .../shell/{MainLoopEFR32.cpp => MainLoopSilabs.cpp} | 12 ++++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) rename src/lib/shell/{MainLoopEFR32.cpp => MainLoopSilabs.cpp} (95%) diff --git a/src/lib/shell/BUILD.gn b/src/lib/shell/BUILD.gn index bb5ce86b8d8f20..585a1875e4dcea 100644 --- a/src/lib/shell/BUILD.gn +++ b/src/lib/shell/BUILD.gn @@ -50,14 +50,9 @@ static_library("shell") { "MainLoopESP32.cpp", "streamer_esp32.cpp", ] - } else if (chip_device_platform == "efr32") { + } else if (chip_device_platform == "efr32" || chip_device_platform == "SiWx917") { sources += [ - "MainLoopEFR32.cpp", - "streamer_silabs.cpp", - ] - } else if (chip_device_platform == "SiWx917") { - sources += [ - "MainLoopSiWx917.cpp", + "MainLoopSilabs.cpp", "streamer_silabs.cpp", ] } else if (chip_device_platform == "k32w0") { diff --git a/src/lib/shell/MainLoopEFR32.cpp b/src/lib/shell/MainLoopSilabs.cpp similarity index 95% rename from src/lib/shell/MainLoopEFR32.cpp rename to src/lib/shell/MainLoopSilabs.cpp index 7996300e2f5aac..03148ec60c0bbd 100644 --- a/src/lib/shell/MainLoopEFR32.cpp +++ b/src/lib/shell/MainLoopSilabs.cpp @@ -49,11 +49,19 @@ void ReadLine(char * buffer, size_t max) break; } +#ifdef BRD4325A + if (streamer_read(streamer_get(), buffer + line_sz, 1) != 1) + { + continue; + } +#endif chip::WaitForShellActivity(); +#ifndef BRD4325A if (streamer_read(streamer_get(), buffer + line_sz, 1) != 1) { continue; } +#endif // Process character we just read. switch (buffer[line_sz]) @@ -176,6 +184,10 @@ void ProcessShellLine(intptr_t args) } } MemoryFree(line); +#ifdef BRD4325A + // small delay for uart print + vTaskDelay(1); +#endif streamer_printf(streamer_get(), kShellPrompt); } From 8ada784fe2a0d663de6b0bebb346e629962267d6 Mon Sep 17 00:00:00 2001 From: Chirag Bansal Date: Mon, 10 Apr 2023 20:38:51 +0530 Subject: [PATCH 6/8] removing the siwx917 file --- src/lib/shell/MainLoopSiWx917.cpp | 208 ------------------------------ 1 file changed, 208 deletions(-) delete mode 100644 src/lib/shell/MainLoopSiWx917.cpp diff --git a/src/lib/shell/MainLoopSiWx917.cpp b/src/lib/shell/MainLoopSiWx917.cpp deleted file mode 100644 index 20127e04651e42..00000000000000 --- a/src/lib/shell/MainLoopSiWx917.cpp +++ /dev/null @@ -1,208 +0,0 @@ -/* - * - * Copyright (c) 2021 Project CHIP Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "matter_shell.h" -#include "streamer.h" -#include -#include -#include - -#include -#include - -using chip::FormatCHIPError; -using chip::Platform::MemoryAlloc; -using chip::Platform::MemoryFree; -using chip::Shell::Engine; -using chip::Shell::streamer_get; - -namespace { - -constexpr const char kShellPrompt[] = "matterCli > "; - -// max > 1 -void ReadLine(char * buffer, size_t max) -{ - size_t line_sz = 0; - - // Read in characters until we get a line ending or EOT. - for (bool done = false; !done;) - { - // Stop reading if we've run out of space in the buffer (still need to null-terminate). - if (line_sz >= max - 1u) - { - buffer[max - 1] = '\0'; - break; - } - - if (streamer_read(streamer_get(), buffer + line_sz, 1) != 1) - { - continue; - } - chip::WaitForShellActivity(); - - // Process character we just read. - switch (buffer[line_sz]) - { - case '\r': - case '\n': - streamer_printf(streamer_get(), "\r\n"); - buffer[line_sz] = '\0'; - line_sz++; - done = true; - break; - case 0x7F: - // Do not accept backspace character (i.e. don't increment line_sz) and remove 1 additional character if it exists. - if (line_sz >= 1u) - { - streamer_printf(streamer_get(), "\b \b"); - line_sz--; - } - break; - default: - if (isprint(static_cast(buffer[line_sz])) || buffer[line_sz] == '\t') - { - streamer_printf(streamer_get(), "%c", buffer[line_sz]); - line_sz++; - } - break; - } - } -} - -bool IsSeparator(char ch) -{ - return (ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n'); -} - -bool IsEscape(char ch) -{ - return (ch == '\\'); -} - -bool IsEscapable(char ch) -{ - return IsSeparator(ch) || IsEscape(ch); -} - -int TokenizeLine(char * buffer, char ** tokens, int max_tokens) -{ - size_t len = strlen(buffer); - int cursor = 0; - size_t i = 0; - - // Strip leading spaces - while (buffer[i] && buffer[i] == ' ') - { - i++; - } - - if (len <= i) - { - return 0; - } - - // The first token starts at the beginning. - tokens[cursor++] = &buffer[i]; - - for (; i < len && cursor < max_tokens; i++) - { - if (IsEscape(buffer[i]) && IsEscapable(buffer[i + 1])) - { - // include the null terminator: strlen(cmd) = strlen(cmd + 1) + 1 - memmove(&buffer[i], &buffer[i + 1], strlen(&buffer[i])); - } - else if (IsSeparator(buffer[i])) - { - buffer[i] = 0; - // Don't treat the previous character as a separator if this one is 0 - // otherwise the trailing space will become a token - if (!IsSeparator(buffer[i + 1]) && buffer[i + 1] != 0) - { - tokens[cursor++] = &buffer[i + 1]; - } - } - } - // If for too many arguments, overwrite last entry with guard. - if (cursor >= max_tokens) - { - cursor = max_tokens - 1; - } - - tokens[cursor] = nullptr; - - return cursor; -} - -void ProcessShellLine(intptr_t args) -{ - int argc; - char * argv[CHIP_SHELL_MAX_TOKENS]; - - char * line = reinterpret_cast(args); - argc = TokenizeLine(line, argv, CHIP_SHELL_MAX_TOKENS); - - if (argc > 0) - { - CHIP_ERROR retval = Engine::Root().ExecCommand(argc, argv); - - if (retval != CHIP_NO_ERROR) - { - char errorStr[160]; - bool errorStrFound = FormatCHIPError(errorStr, sizeof(errorStr), retval); - if (!errorStrFound) - { - errorStr[0] = 0; - } - streamer_printf(streamer_get(), "Error %s: %s\r\n", argv[0], errorStr); - } - else - { - streamer_printf(streamer_get(), "Done\r\n", argv[0]); - } - } - MemoryFree(line); - - // small delay for uart print - vTaskDelay(10); - - streamer_printf(streamer_get(), kShellPrompt); -} - -} // namespace - -namespace chip { -namespace Shell { - -void Engine::RunMainLoop() -{ - streamer_printf(streamer_get(), kShellPrompt); - - while (true) - { - char * line = static_cast(chip::Platform::MemoryAlloc(CHIP_SHELL_MAX_LINE_SIZE)); - ReadLine(line, CHIP_SHELL_MAX_LINE_SIZE); -#if CONFIG_DEVICE_LAYER - DeviceLayer::PlatformMgr().ScheduleWork(ProcessShellLine, reinterpret_cast(line)); -#else - ProcessShellLine(reinterpret_cast(line)); -#endif - } -} - -} // namespace Shell -} // namespace chip From e03d1783d494d2ee5cc16845709336b064f61122 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 10 Apr 2023 15:09:18 +0000 Subject: [PATCH 7/8] Restyled by gn --- src/lib/shell/BUILD.gn | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/shell/BUILD.gn b/src/lib/shell/BUILD.gn index 585a1875e4dcea..7f3d1d46cb62b1 100644 --- a/src/lib/shell/BUILD.gn +++ b/src/lib/shell/BUILD.gn @@ -50,7 +50,8 @@ static_library("shell") { "MainLoopESP32.cpp", "streamer_esp32.cpp", ] - } else if (chip_device_platform == "efr32" || chip_device_platform == "SiWx917") { + } else if (chip_device_platform == "efr32" || + chip_device_platform == "SiWx917") { sources += [ "MainLoopSilabs.cpp", "streamer_silabs.cpp", From f2aff415028cca5e0a3f005719041a693062eb85 Mon Sep 17 00:00:00 2001 From: Chirag Bansal Date: Tue, 11 Apr 2023 10:32:31 +0530 Subject: [PATCH 8/8] adding some comments --- src/lib/shell/MainLoopSilabs.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/shell/MainLoopSilabs.cpp b/src/lib/shell/MainLoopSilabs.cpp index 03148ec60c0bbd..2b755119fd8cd5 100644 --- a/src/lib/shell/MainLoopSilabs.cpp +++ b/src/lib/shell/MainLoopSilabs.cpp @@ -50,6 +50,8 @@ void ReadLine(char * buffer, size_t max) } #ifdef BRD4325A + // for 917 SoC board, we need to create a rx event before we wait for the shell activity + // NotifyShellProcessFromISR() is called once the buffer is filled if (streamer_read(streamer_get(), buffer + line_sz, 1) != 1) { continue; @@ -57,6 +59,7 @@ void ReadLine(char * buffer, size_t max) #endif chip::WaitForShellActivity(); #ifndef BRD4325A + // for EFR32 boards if (streamer_read(streamer_get(), buffer + line_sz, 1) != 1) { continue;