|
| 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 |
0 commit comments