Skip to content

Commit 2920729

Browse files
CuRahmanmkardous-silabs
authored andcommitted
Improve Silabs Matter shell functionality/stability (#28475)
* Re-worked matter shell to allow for copy/paste, fix varying platform functionality, and increase stability * Update src/lib/shell/MainLoopSilabs.cpp Co-authored-by: mkardous-silabs <84793247+mkardous-silabs@users.noreply.github.com> * Added further clarification in comments --------- Co-authored-by: mkardous-silabs <84793247+mkardous-silabs@users.noreply.github.com>
1 parent 2e4ed32 commit 2920729

File tree

1 file changed

+60
-29
lines changed

1 file changed

+60
-29
lines changed

src/lib/shell/MainLoopSilabs.cpp

+60-29
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@ using chip::Shell::streamer_get;
3232

3333
namespace {
3434

35-
constexpr const char kShellPrompt[] = "matterCli > ";
35+
constexpr const char kShellPrompt[] = "matterCli> ";
36+
37+
// To track carriage returns of Windows return cases of '\r\n'
38+
bool haveCR = false;
3639

37-
// max > 1
3840
void ReadLine(char * buffer, size_t max)
3941
{
4042
size_t line_sz = 0;
43+
size_t read = 0;
44+
bool done = false;
4145

4246
// Read in characters until we get a line ending or EOT.
43-
for (bool done = false; !done;)
47+
while ((line_sz < max) && !done)
4448
{
4549
// Stop reading if we've run out of space in the buffer (still need to null-terminate).
4650
if (line_sz >= max - 1u)
@@ -52,45 +56,72 @@ void ReadLine(char * buffer, size_t max)
5256
#ifdef BRD4325A
5357
// for 917 SoC board, we need to create a rx event before we wait for the shell activity
5458
// NotifyShellProcessFromISR() is called once the buffer is filled
55-
if (streamer_read(streamer_get(), buffer + line_sz, 1) != 1)
59+
while (streamer_read(streamer_get(), buffer + read, 1) == 1)
5660
{
57-
continue;
61+
// Count how many characters were read; usually one but could be copy/paste
62+
read++;
5863
}
5964
#endif
6065
chip::WaitForShellActivity();
6166
#ifndef BRD4325A
6267
// for EFR32 boards
63-
if (streamer_read(streamer_get(), buffer + line_sz, 1) != 1)
68+
while (streamer_read(streamer_get(), buffer + read, 1) == 1)
6469
{
65-
continue;
70+
// Count how many characters were read; usually one but could be copy/paste
71+
read++;
6672
}
6773
#endif
68-
69-
// Process character we just read.
70-
switch (buffer[line_sz])
74+
// Process all characters that were read until we run out or exceed max char limit
75+
while (line_sz < read && line_sz < max)
7176
{
72-
case '\r':
73-
case '\n':
74-
streamer_printf(streamer_get(), "\r\n");
75-
buffer[line_sz] = '\0';
76-
line_sz++;
77-
done = true;
78-
break;
79-
case 0x7F:
80-
// Do not accept backspace character (i.e. don't increment line_sz) and remove 1 additional character if it exists.
81-
if (line_sz >= 1u)
77+
switch (buffer[line_sz])
8278
{
83-
streamer_printf(streamer_get(), "\b \b");
84-
line_sz--;
85-
}
86-
break;
87-
default:
88-
if (isprint(static_cast<int>(buffer[line_sz])) || buffer[line_sz] == '\t')
89-
{
90-
streamer_printf(streamer_get(), "%c", buffer[line_sz]);
79+
case '\r':
80+
// Mac OS return case of '\r' or beginning of Windows return case '\r\n'
81+
buffer[line_sz] = '\0';
82+
streamer_printf(streamer_get(), "\r\n");
83+
haveCR = true;
84+
done = true;
9185
line_sz++;
86+
break;
87+
case '\n':
88+
// True if Windows return case of '\r\n'
89+
if (haveCR)
90+
{
91+
// Do nothing - already taken care of with CR, return to loop and don't increment buffer
92+
haveCR = false;
93+
read--;
94+
}
95+
// Linux return case of just '\n'
96+
else
97+
{
98+
buffer[line_sz] = '\0';
99+
streamer_printf(streamer_get(), "\r\n");
100+
done = true;
101+
line_sz++;
102+
}
103+
break;
104+
case 0x7F:
105+
// Do not accept backspace character (i.e. don't increment line_sz) and remove 1 additional character if it exists.
106+
if (line_sz >= 1u)
107+
{
108+
// Delete backspace character + whatever came before it
109+
streamer_printf(streamer_get(), "\b \b");
110+
line_sz--;
111+
read--;
112+
}
113+
// Remove backspace character regardless
114+
read--;
115+
116+
break;
117+
default:
118+
if (isprint(static_cast<int>(buffer[line_sz])) || buffer[line_sz] == '\t')
119+
{
120+
streamer_printf(streamer_get(), "%c", buffer[line_sz]);
121+
line_sz++;
122+
}
123+
break;
92124
}
93-
break;
94125
}
95126
}
96127
}

0 commit comments

Comments
 (0)