Skip to content

Commit 1160140

Browse files
pankorepull[bot]
authored andcommitted
[shell] matter shell with loguart (#26380)
1 parent 09bfdb4 commit 1160140

File tree

4 files changed

+176
-32
lines changed

4 files changed

+176
-32
lines changed

examples/platform/ameba/shell/launch_shell.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "task.h"
2222

2323
#include <lib/shell/Engine.h>
24+
#include <platform/CHIPDeviceLayer.h>
2425

2526
namespace {
2627

@@ -35,7 +36,11 @@ namespace chip {
3536

3637
void LaunchShell()
3738
{
38-
chip::Shell::Engine::Root().Init();
39+
if (chip::Shell::Engine::Root().Init() < 0)
40+
{
41+
ChipLogError(DeviceLayer, "Failed to initialize shell engine!");
42+
return;
43+
}
3944
xTaskCreate(MatterShellTask, "matter_shell", 2048, NULL, tskIDLE_PRIORITY + 1, NULL);
4045
}
4146

src/lib/shell/BUILD.gn

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static_library("shell") {
104104
]
105105
} else if (chip_device_platform == "ameba") {
106106
sources += [
107-
"MainLoopDefault.cpp",
107+
"MainLoopAmeba.cpp",
108108
"streamer_ameba.cpp",
109109
]
110110
} else if (chip_device_platform == "openiotsdk") {

src/lib/shell/MainLoopAmeba.cpp

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
*
3+
* Copyright (c) 2021 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "streamer.h"
19+
#include <lib/shell/Engine.h>
20+
#include <lib/support/CHIPMem.h>
21+
#include <platform/CHIPDeviceLayer.h>
22+
23+
#include <ctype.h>
24+
#include <string.h>
25+
26+
using chip::FormatCHIPError;
27+
using chip::Platform::MemoryAlloc;
28+
using chip::Platform::MemoryFree;
29+
using chip::Shell::Engine;
30+
using chip::Shell::streamer_get;
31+
32+
namespace {
33+
34+
size_t ReadLine(char * buffer, size_t max)
35+
{
36+
return streamer_read(streamer_get(), buffer, max);
37+
}
38+
39+
bool IsSeparator(char ch)
40+
{
41+
return (ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n');
42+
}
43+
44+
bool IsEscape(char ch)
45+
{
46+
return (ch == '\\');
47+
}
48+
49+
bool IsEscapable(char ch)
50+
{
51+
return IsSeparator(ch) || IsEscape(ch);
52+
}
53+
54+
int TokenizeLine(char * buffer, char ** tokens, int max_tokens)
55+
{
56+
size_t len = strlen(buffer);
57+
int cursor = 0;
58+
size_t i = 0;
59+
60+
// Strip leading spaces
61+
while (buffer[i] && buffer[i] == ' ')
62+
{
63+
i++;
64+
}
65+
66+
if (len <= i)
67+
{
68+
return 0;
69+
}
70+
71+
// The first token starts at the beginning.
72+
tokens[cursor++] = &buffer[i];
73+
74+
for (; i < len && cursor < max_tokens; i++)
75+
{
76+
if (IsEscape(buffer[i]) && IsEscapable(buffer[i + 1]))
77+
{
78+
// include the null terminator: strlen(cmd) = strlen(cmd + 1) + 1
79+
memmove(&buffer[i], &buffer[i + 1], strlen(&buffer[i]));
80+
}
81+
else if (IsSeparator(buffer[i]))
82+
{
83+
buffer[i] = 0;
84+
if (!IsSeparator(buffer[i + 1]))
85+
{
86+
tokens[cursor++] = &buffer[i + 1];
87+
}
88+
}
89+
}
90+
// If for too many arguments, overwrite last entry with guard.
91+
if (cursor >= max_tokens)
92+
{
93+
cursor = max_tokens - 1;
94+
}
95+
96+
tokens[cursor] = nullptr;
97+
98+
return cursor;
99+
}
100+
101+
void ProcessShellLine(intptr_t args)
102+
{
103+
int argc;
104+
char * argv[CHIP_SHELL_MAX_TOKENS];
105+
106+
char * line = reinterpret_cast<char *>(args);
107+
argc = TokenizeLine(line, argv, CHIP_SHELL_MAX_TOKENS);
108+
109+
if (argc > 0)
110+
{
111+
CHIP_ERROR retval = Engine::Root().ExecCommand(argc, argv);
112+
113+
if (retval != CHIP_NO_ERROR)
114+
{
115+
char errorStr[160];
116+
bool errorStrFound = FormatCHIPError(errorStr, sizeof(errorStr), retval);
117+
if (!errorStrFound)
118+
{
119+
errorStr[0] = 0;
120+
}
121+
streamer_printf(streamer_get(), "Error %s: %s\r\n", argv[0], errorStr);
122+
}
123+
else
124+
{
125+
streamer_printf(streamer_get(), "Done\r\n", argv[0]);
126+
}
127+
}
128+
}
129+
130+
} // namespace
131+
132+
namespace chip {
133+
namespace Shell {
134+
135+
void Engine::RunMainLoop()
136+
{
137+
char line[CHIP_SHELL_MAX_LINE_SIZE];
138+
while (true)
139+
{
140+
memset(line, 0, CHIP_SHELL_MAX_LINE_SIZE);
141+
if (ReadLine(line, CHIP_SHELL_MAX_LINE_SIZE) > 0u)
142+
{
143+
#if CONFIG_DEVICE_LAYER
144+
DeviceLayer::PlatformMgr().ScheduleWork(ProcessShellLine, reinterpret_cast<intptr_t>(line));
145+
#else
146+
ProcessShellLine(reinterpret_cast<intptr_t>(line));
147+
#endif
148+
}
149+
}
150+
}
151+
152+
} // namespace Shell
153+
} // namespace chip

src/lib/shell/streamer_ameba.cpp

+16-30
Original file line numberDiff line numberDiff line change
@@ -22,59 +22,45 @@
2222

2323
#include <lib/shell/Engine.h>
2424
#include <lib/shell/streamer.h>
25+
#include <platform/CHIPDeviceLayer.h>
2526

2627
#include <stdio.h>
2728
#include <string.h>
2829

29-
#include "serial_api.h"
30-
31-
#if defined(CONFIG_PLATFORM_8721D)
32-
#define UART_TX PA_18 // UART0 TX
33-
#define UART_RX PA_19 // UART0 RX
34-
#elif defined(CONFIG_PLATFORM_8710C)
35-
#define UART_TX PA_14 // UART0 TX
36-
#define UART_RX PA_13 // UART0 RX
37-
#endif
30+
extern QueueHandle_t shell_queue;
3831

3932
namespace chip {
4033
namespace Shell {
4134
namespace {
4235

43-
serial_t sobj;
44-
4536
int streamer_ameba_init(streamer_t * streamer)
4637
{
47-
(void) streamer;
48-
serial_init(&sobj, UART_TX, UART_RX);
49-
serial_baud(&sobj, 115200);
50-
serial_format(&sobj, 8, ParityNone, 1);
38+
// freertos queue should be initialized in ameba sdk
39+
if (shell_queue == NULL)
40+
{
41+
// queue not initialized
42+
return -1;
43+
}
44+
5145
return 0;
5246
}
5347

5448
ssize_t streamer_ameba_read(streamer_t * streamer, char * buffer, size_t length)
5549
{
56-
(void) streamer;
57-
uint16_t len_read = 0;
58-
59-
while (len_read < length)
50+
BaseType_t lineReceived = xQueueReceive(shell_queue, buffer, pdMS_TO_TICKS(10));
51+
if (lineReceived == pdTRUE)
6052
{
61-
*(buffer + len_read) = (char) serial_getc(&sobj);
62-
len_read++;
53+
return length;
6354
}
64-
return len_read;
55+
56+
return 0;
6557
}
6658

6759
ssize_t streamer_ameba_write(streamer_t * streamer, const char * buffer, size_t length)
6860
{
6961
(void) streamer;
70-
uint16_t len_written = 0;
71-
72-
while (len_written < length)
73-
{
74-
serial_putc(&sobj, *(buffer + len_written));
75-
len_written++;
76-
}
77-
return len_written;
62+
printf("[shell] %s\r\n", buffer);
63+
return length;
7864
}
7965

8066
static streamer_t streamer_ameba = {

0 commit comments

Comments
 (0)