chore: migrate project into clean repository

This commit is contained in:
yuuux
2026-08-13 16:50:52 +08:00
commit d1d25a09e7
27405 changed files with 9422808 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.13)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(listenai-cmake REQUIRED HINTS $ENV{ARCS_BASE})
project(arcs)
listenai_add_executable(${PROJECT_NAME})
add_subdirectory(src)

View File

@@ -0,0 +1 @@
osource "$ARCS_BASE/Kconfig"

View File

@@ -0,0 +1,330 @@
# Shell 示例
## 功能说明
演示 LISA Shell 组件的完整使用方法,通过5个典型示例展示如何创建和注册各种类型的 Shell 命令。
本示例是学习 LISA Shell 命令开发的完整教程,涵盖从简单命令到复杂命令组的各种使用场景,每个示例都包含详细的中文注释说明。
### 示例内容
- **示例1: 简单命令** - 不带参数的基础命令 (version)
- **示例2: 带参数命令** - 处理用户输入参数 (echo)
- **示例3: 可选参数** - 必选参数+可选参数的组合使用 (memread)
- **示例4: 命令组** - 组织多个相关子命令 (info)
- **示例5: 命令选项** - 处理命令行选项参数 (list)
## 硬件连接
- **PA2**: UART0 RX (接收)
- **PA3**: UART0 TX (发送)
连接到 PC 串口工具,配置为 **115200, 8N1, 无流控**
## 使用场景
适用于需要为嵌入式应用添加 Shell 命令行接口的场景:
- 学习如何创建自定义 Shell 命令
- 实现系统调试和配置接口
- 开发交互式命令行工具
- 提供设备远程控制功能
## 示例步骤
1. 初始化 LISA Shell 组件
2. 实现5种不同类型的命令示例
3. 使用 `SHELL_EXPORT_CMD` 宏注册命令
4. 通过串口工具连接并执行命令
5. 观察各种命令的实现和效果
## 编译
```{eval-rst}
.. include:: /sample_build.rst
```
## 预期输出
**终端日志:**
```
[INFO] [shell] === LISA Shell Usage Sample ===
[INFO] [shell] Shell initialized successfully
[INFO] [shell]
[INFO] [shell] This sample demonstrates:
[INFO] [shell] 1. Simple command (version)
[INFO] [shell] 2. Command with parameters (echo)
[INFO] [shell] 3. Optional parameters (memread)
[INFO] [shell] 4. Command group (info)
[INFO] [shell] 5. Command options (list)
[INFO] [shell]
[INFO] [shell] Type 'help' in shell to see all commands
```
**串口工具交互:**
```
letter shell 3.1
arcs@shell:/>help
version : show system version
echo : echo the input message
memread : read memory content
info : system information commands
list : list files (-l: verbose, -a: all)
...
arcs@shell:/>version
ARCS SDK Version: 1.0.0
Build Date: Jan 15 2025 10:30:45
arcs@shell:/>echo hello world
hello world
arcs@shell:/>memread 0x20000000 32
Memory at 0x20000000 (32 bytes):
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
-------------------------------------------------------
20000000: 12 34 56 78 9A BC DE F0 11 22 33 44 55 66 77 88
20000010: AA BB CC DD EE FF 00 11 22 33 44 55 66 77 88 99
arcs@shell:/>info
Usage: info <subcommand>
Available subcommands:
version - Show version
tasks - Show task list
arcs@shell:/>info version
Version: 1.0.0
arcs@shell:/>info tasks
Task List:
Name State Pri Stack Num
---------------------------------------------
IDLE R 0 128 1
shell_task B 5 512 2
main_task B 10 256 3
---------------------------------------------
arcs@shell:/>list
File List:
main.c shell.c
arcs@shell:/>list -l
File List:
Name Size Date
------------------------------------------
main.c 1234 2025-01-15
shell.c 5678 2025-01-14
```
## 核心 API
| API | 说明 |
|-----|------|
| `lisa_shell_init()` | 初始化 LISA Shell 组件 |
| `SHELL_EXPORT_CMD()` | 注册命令到 Shell |
| `printf()` | 在 Shell 中输出信息 |
| `LISA_LOGI()` / `LISA_LOGE()` | 输出日志信息 |
## 示例详解
### 示例1: 简单命令 (version)
演示最基本的命令实现,不需要处理任何参数。
```c
static int cmd_version(int argc, char **argv)
{
printf("ARCS SDK Version: 1.0.0\n");
printf("Build Date: %s %s\n", __DATE__, __TIME__);
return 0;
}
/* 注册命令 */
SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
version, /* Shell 中的命令名 */
cmd_version, /* 命令处理函数 */
show system version /* 帮助信息 */
);
```
**要点:**
- 命令函数返回 0 表示成功,-1 表示失败
- 使用 `printf()` 输出信息到 Shell
- `SHELL_EXPORT_CMD` 必须在全局作用域使用
### 示例2: 带参数命令 (echo)
演示如何接收和处理用户输入的参数。
```c
static int cmd_echo(int argc, char **argv)
{
/* argc: 参数个数 */
/* argv: 参数数组,argv[0]是第一个参数 */
if (argc < 1) {
printf("Usage: echo <message>\n");
return -1;
}
for (int i = 0; i < argc; i++) {
printf("%s ", argv[i]);
}
printf("\n");
return 0;
}
```
**要点:**
- `argc` 不包括命令名本身,只计算参数个数
- `argv[0]` 是第一个参数(不是命令名)
- 需要检查参数个数,避免数组越界
### 示例3: 可选参数 (memread)
演示如何处理必选参数和可选参数的组合。
```c
static int cmd_memread(int argc, char **argv)
{
uint32_t addr;
uint32_t len = 64; /* 可选参数的默认值 */
if (argc < 1) {
/* 必选参数缺失 */
printf("Usage: memread <address> [length]\n");
return -1;
}
/* 解析必选参数 */
addr = strtoul(argv[0], NULL, 0);
/* 解析可选参数 */
if (argc >= 2) {
len = strtoul(argv[1], NULL, 0);
}
/* 使用参数... */
}
```
**要点:**
- 可选参数需要设置合理的默认值
- 使用 `strtoul()` 可以自动识别十进制和十六进制(0x前缀)
- 需要对参数进行有效性检查
### 示例4: 命令组 (info)
演示如何组织多个相关子命令为一个命令组。
```c
/* 定义子命令表 */
static const struct shell_cmd_entry info_cmds[] = {
{"version", subcmd_info_version, "Show version"},
{"tasks", subcmd_info_tasks, "Show task list"},
};
/* 命令组处理函数 */
static int cmd_info_handler(int argc, char **argv)
{
/* argc包括命令名, argv[0]是命令名 info */
/* argv[1] 是子命令名 */
if (argc == 1) {
/* 没有子命令,显示帮助 */
info_show_help();
return 0;
}
/* 查找子命令 */
const char *subcmd = argv[1];
for (int i = 0; i < count; i++) {
if (strcmp(info_cmds[i].name, subcmd) == 0) {
/* 执行子命令,传递剩余参数 */
return info_cmds[i].handler(argc - 2, argv + 2);
}
}
/* 未找到子命令 */
printf("Error: unknown subcommand '%s'\n", subcmd);
return -1;
}
/* 注册命令组(注意使用 SHELL_CMD_DISABLE_RETURN 标志) */
SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_DISABLE_RETURN,
info,
cmd_info_handler,
system information commands
);
```
**要点:**
- 命令组的 `argc` 包括命令名,`argv[0]` 是命令名
- 子命令名在 `argv[1]`
- 传递给子命令的参数需要调整: `argc-2`, `argv+2`
- 命令组通常添加 `SHELL_CMD_DISABLE_RETURN` 标志
### 示例5: 命令选项 (list)
演示如何解析和处理类似 `-l`, `-a` 这样的命令行选项。
```c
static int cmd_list(int argc, char **argv)
{
int verbose = 0; /* -l 选项 */
int show_all = 0; /* -a 选项 */
/* 解析选项 */
for (int i = 0; i < argc; i++) {
if (argv[i][0] == '-') {
/* 这是一个选项 */
for (int j = 1; argv[i][j] != '\0'; j++) {
if (argv[i][j] == 'l') {
verbose = 1;
} else if (argv[i][j] == 'a') {
show_all = 1;
}
}
}
}
/* 根据选项执行不同的逻辑 */
if (verbose) {
/* 详细输出 */
} else {
/* 简单输出 */
}
}
```
**要点:**
- 选项以 `-` 开头
- 支持组合选项如 `-la`
- 根据选项标志执行不同的逻辑
## 配置说明
在 prj.conf 中需要启用以下配置:
```c
# 启用 LISA Shell
CONFIG_LISA_SHELL=y
```
## 注意事项
1. **命令注册位置**: `SHELL_EXPORT_CMD` 宏必须在全局作用域使用,不能在函数内部
2. **参数索引**: 命令函数的 `argc` 不包括命令名,`argv[0]` 是第一个参数
3. **命令组参数**: 命令组处理函数的 `argc` 包括命令名,`argv[0]` 是命令名本身
4. **返回值**: 命令函数返回 0 表示成功,-1 表示失败
5. **日志接口**: 使用 `LISA_LOGI()` / `LISA_LOGE()` 输出日志,消息使用英文
6. **注释规范**: 代码注释使用中文,便于理解功能
7. **内存安全**: 访问内存地址前需要检查有效性,避免系统崩溃

View File

@@ -0,0 +1,177 @@
#!/bin/bash
set -e
usage() {
echo "使用方式: $0 [选项]"
echo "选项:"
echo " -S, --Source <path> 指定项目源码路径 (默认为当前脚本所在目录)"
echo " -t, --target <target> 指定构建目标 (如 menuconfig)"
echo " -C, --Clean 清理构建目录"
echo " -B, --build 构建输出目录"
echo " -h, --help 显示此帮助信息"
echo " -r, --release 以 Release 模式构建 (移除 DEBUG_PATH 信息)"
echo " -w, --warnings-as-errors 将警告视为错误"
echo " -D<var>=<value> 传递 CMake 变量 (可多次使用)"
echo ""
echo "示例:"
echo " $0 -S samples/helloworld -DBOARD=arcs_mini 指定板型构建"
echo " $0 -S samples/helloworld -DBOARD=arcs_evb 使用 EVB 板型"
echo " $0 -S samples/helloworld -t menuconfig -DBOARD=arcs_mini 运行 menuconfig"
echo " $0 -C -S samples/helloworld -DBOARD=arcs_mini 清理并重新构建"
echo " $0 -S samples/helloworld -DBOARD=my_board -DBOARD_SEARCH_PATH=/path/to/boards 使用自定义板型"
exit 1
}
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
PROJECT_PATH="$SCRIPT_DIR"
TARGET=""
CLEAN=false
OUTPUT="build"
WARNINGS_AS_ERRORS=false
RELEASE=false
ARCS_BASE_DIR_NAME="arcs-sdk"
ARCS_DEV_TOOLS_DIR_NAME="listenai-dev-tools"
ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME="gcc"
ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME="listenai-tools"
find_arcs_base() {
local current_dir=$(cd "$(dirname "$0")" && pwd)
local dir_name="$ARCS_BASE_DIR_NAME"
while [ "$current_dir" != "/" ]; do
if [ -d "$current_dir/$dir_name" ]; then
echo "Found ARCS_BASE: $current_dir/$dir_name"
export ARCS_BASE="$current_dir/$dir_name"
return 0
fi
current_dir=$(dirname "$current_dir")
done
echo "ARCS_BASE not found, Please add ARCS_BASE environment variable or set ARCS_BASE_DIR_NAME to the correct directory."
echo "Current Target ARCS_BASE directory name: $ARCS_BASE_DIR_NAME."
exit 1
}
find_dev_tools() {
local current_dir=$(cd "$(dirname "$0")" && pwd)
local dir_name="$ARCS_DEV_TOOLS_DIR_NAME"
echo "trying to find $dir_name in parent directories..."
while [ "$current_dir" != "/" ]; do
if [ -d "$current_dir/$dir_name" ]; then
echo "Found $dir_name: $current_dir/$dir_name"
if [ -d "$current_dir/$dir_name/$ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME" ]; then
echo "Found LISTENAI_TOOLS_PATH: $current_dir/$dir_name/$ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME"
export LISTENAI_TOOLS_PATH="$current_dir/$dir_name/$ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME"
fi
if [ -d "$current_dir/$dir_name/$ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME" ]; then
echo "Found NUCLEI_TOOLCHAIN_PATH: $current_dir/$dir_name/$ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME"
export NUCLEI_TOOLCHAIN_PATH="$current_dir/$dir_name/$ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME"
fi
return 0
fi
current_dir=$(dirname "$current_dir")
done
}
while [[ $# -gt 0 ]]; do
case $1 in
-S|--Source)
PROJECT_PATH="$2"
shift 2
;;
-B|--build)
OUTPUT="$2"
shift 2
;;
-t|--target)
TARGET="$2"
shift 2
;;
-C|--Clean)
CLEAN=true
shift 1
;;
-w|--warnings-as-errors)
WARNINGS_AS_ERRORS=true
shift 1
;;
-h|--help)
usage
;;
-r|--release)
RELEASE=true
shift 1
;;
-D*)
CMAKE_VARS+=("$1")
shift 1
;;
*)
echo "未知参数: $1"
usage
;;
esac
done
echo "Source: $PROJECT_PATH"
echo "Target: $TARGET"
echo "Clean : $CLEAN"
if [ -z "$LISTENAI_TOOLS_PATH" ] || [ -z "$NUCLEI_TOOLCHAIN_PATH" ]; then
find_dev_tools
fi
if [ -z "${LISTENAI_TOOLS_PATH}" ]; then
export LISTENAI_TOOLS_PATH="请添加 LISTENAI_TOOLS_PATH 环境变量,或在此行设置正确的路径"
echo "请添加 LISTENAI_TOOLS_PATH 环境变量或者修改脚本后, 注释脚本第 $LINENO 行";exit 1;
fi
if [ -z "${NUCLEI_TOOLCHAIN_PATH}" ]; then
export NUCLEI_TOOLCHAIN_PATH="请添加 NUCLEI_TOOLCHAIN_PATH 环境变量,或在此行设置正确的路径"
echo "请添加 NUCLEI_TOOLCHAIN_PATH 环境变量或者修改脚本后, 注释脚本第 $LINENO 行";exit 1;
fi
############### 下面代码不用修改 ##################
# 构建工具的位置
CMAKE_PROGRAM="$LISTENAI_TOOLS_PATH/cmake/bin/cmake"
NINJA_PROGRAM="$LISTENAI_TOOLS_PATH/ninja/ninja"
# 配置环境变量 ARCS_BASE
if [ -z "$ARCS_BASE" ]; then
find_arcs_base
fi
if [ "$CLEAN" = true ]; then
rm -rf $OUTPUT
fi
# Initialize CMAKE_VARS array if it doesn't exist
declare -a CMAKE_VARS
# Add warnings-as-errors flag if enabled
if [ "$WARNINGS_AS_ERRORS" = true ]; then
CMAKE_VARS+=("-DCMAKE_C_FLAGS=-Werror")
CMAKE_VARS+=("-DCMAKE_CXX_FLAGS=-Werror")
echo "Treating warnings as errors"
fi
# Add release flags if enabled
if [ "$RELEASE" = true ]; then
CMAKE_VARS+=("-DENABLE_DEBUG_PATH=OFF")
echo "Release mode enabled (-DENABLE_DEBUG_PATH=OFF)"
fi
$CMAKE_PROGRAM -B "$OUTPUT" -G Ninja -S "$PROJECT_PATH" \
-DCMAKE_MAKE_PROGRAM="$NINJA_PROGRAM" \
"${CMAKE_VARS[@]}"
if [ -z "$TARGET" ]; then
$CMAKE_PROGRAM --build "$OUTPUT" -j4
else
$CMAKE_PROGRAM --build "$OUTPUT" --target "$TARGET"
fi

View File

@@ -0,0 +1,2 @@
# 项目可根据需要在此文件中设置对应的配置
CONFIG_LISA_SHELL=y

View File

@@ -0,0 +1,8 @@
tests:
samples.lisa_shell:
programmer: arcs
runner: uart
log_analyzer:
type: "regex"
pattern: "Type 'help' in shell to see all commands"

View File

@@ -0,0 +1,7 @@
listenai_library_sources(
main.c
)
listenai_include_directories(
./
)

View File

@@ -0,0 +1,372 @@
/*
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file main.c
* @brief LISA Shell 使用示例
*
* 本示例演示如何使用 LISA Shell 组件创建各种类型的命令:
* 1. 简单命令 - 不带参数的基础命令
* 2. 带参数命令 - 处理用户输入参数
* 3. 可选参数 - 必选参数+可选参数的组合
* 4. 命令组 - 组织多个相关子命令
* 5. 命令选项 - 处理命令行选项参数
*/
#define LOG_TAG "shell_sample"
#include <lisa_log.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "shell.h"
#include "lisa_shell.h"
#include "FreeRTOS.h"
#include "task.h"
/* ========== 示例1: 简单命令 - 不带参数 ========== */
/**
* @brief 显示版本信息命令
*
* 演示: 如何实现一个简单的无参数命令
*/
static int cmd_version(int argc, char **argv)
{
Shell *shell = shellGetCurrent();
shellPrint(shell, "ARCS SDK Version: 1.0.0\n");
shellPrint(shell, "Build Date: %s %s\n", __DATE__, __TIME__);
LISA_LOGI(LOG_TAG, "Display version information\n");
return 0;
}
/* 注册 version 命令 */
SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
version,
cmd_version,
show system version
);
/* ========== 示例2: 带参数的命令 ========== */
/**
* @brief echo 命令 - 回显输入的参数
*
* 演示: 如何处理命令参数
* @param argc 参数个数
* @param argv 参数数组,argv[0]是第一个参数
*/
static int cmd_echo(int argc, char **argv)
{
Shell *shell = shellGetCurrent();
/* 检查参数 */
if (argc < 1) {
shellPrint(shell, "Usage: echo <message>\n");
return -1;
}
/* 输出所有参数 */
for (int i = 0; i < argc; i++) {
shellPrint(shell, "%s ", argv[i]);
}
shellPrint(shell, "\n");
LISA_LOGI(LOG_TAG, "Echo %d arguments\n", argc);
return 0;
}
/* 注册 echo 命令 */
SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
echo,
cmd_echo,
echo the input message
);
/* ========== 示例3: 带可选参数的命令 ========== */
/**
* @brief 内存读取命令
*
* 演示: 如何处理必选参数和可选参数
* @param argc 参数个数
* @param argv argv[0]=地址(必选), argv[1]=长度(可选,默认64)
*/
static int cmd_memread(int argc, char **argv)
{
Shell *shell = shellGetCurrent();
uint32_t addr;
uint32_t len = 64; /* 默认长度 */
uint8_t *ptr;
/* 检查必选参数 */
if (argc < 1) {
shellPrint(shell, "Usage: memread <address> [length]\n");
shellPrint(shell, " address : memory address (hex format supported)\n");
shellPrint(shell, " length : bytes to read (optional, default 64)\n");
return -1;
}
/* 解析地址(支持十进制和十六进制) */
addr = strtoul(argv[0], NULL, 0);
/* 解析可选的长度参数 */
if (argc >= 2) {
len = strtoul(argv[1], NULL, 0);
if (len == 0 || len > 256) {
shellPrint(shell, "Error: length must be 1-256\n");
LISA_LOGE(LOG_TAG, "Invalid length parameter: %u\n", len);
return -1;
}
}
ptr = (uint8_t *)addr;
/* 显示内存内容 */
shellPrint(shell, "\nMemory at 0x%08X (%u bytes):\n", addr, len);
shellPrint(shell, " 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
shellPrint(shell, "-------------------------------------------------------\n");
for (uint32_t i = 0; i < len; i += 16) {
shellPrint(shell, "%08X:", addr + i);
for (uint32_t j = 0; j < 16 && (i + j) < len; j++) {
shellPrint(shell, " %02X", ptr[i + j]);
}
shellPrint(shell, "\n");
}
shellPrint(shell, "\n");
LISA_LOGI(LOG_TAG, "Read %u bytes from 0x%08X\n", len, addr);
return 0;
}
/* 注册 memread 命令 */
SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
memread,
cmd_memread,
read memory content
);
/* ========== 示例4: 命令组 - 组织相关命令 ========== */
/* 定义命令表结构 */
struct shell_cmd_entry {
const char *name; /* 子命令名称 */
int (*handler)(int argc, char **argv); /* 子命令处理函数 */
const char *help; /* 子命令帮助信息 */
};
/**
* @brief info 命令组的子命令: version
*
* 演示: 命令组中的子命令实现
*/
static int subcmd_info_version(int argc, char **argv)
{
Shell *shell = shellGetCurrent();
shellPrint(shell, "Version: 1.0.0\n");
LISA_LOGI(LOG_TAG, "Display version via info command\n");
return 0;
}
/**
* @brief info 命令组的子命令: tasks
*
* 演示: 显示 FreeRTOS 任务列表
*/
static int subcmd_info_tasks(int argc, char **argv)
{
Shell *shell = shellGetCurrent();
char *buffer;
/* 分配缓冲区 */
buffer = pvPortMalloc(1024);
if (buffer == NULL) {
shellPrint(shell, "Error: out of memory\n");
LISA_LOGE(LOG_TAG, "Failed to allocate memory for task list\n");
return -1;
}
/* 获取任务列表 */
shellPrint(shell, "\nTask List:\n");
shellPrint(shell, "Name State Pri Stack Num\n");
shellPrint(shell, "---------------------------------------------\n");
vTaskList(buffer);
shellPrint(shell, "%s", buffer);
shellPrint(shell, "---------------------------------------------\n");
vPortFree(buffer);
LISA_LOGI(LOG_TAG, "Display task list\n");
return 0;
}
/* info 命令组的子命令表 */
static const struct shell_cmd_entry info_cmds[] = {
{"version", subcmd_info_version, "Show version"},
{"tasks", subcmd_info_tasks, "Show task list"},
};
/**
* @brief 显示 info 命令组的帮助信息
*/
static void info_show_help(void)
{
Shell *shell = shellGetCurrent();
int count = sizeof(info_cmds) / sizeof(info_cmds[0]);
shellPrint(shell, "\nUsage: info <subcommand>\n");
shellPrint(shell, "\nAvailable subcommands:\n");
for (int i = 0; i < count; i++) {
shellPrint(shell, " %-12s - %s\n", info_cmds[i].name, info_cmds[i].help);
}
shellPrint(shell, "\n");
}
/**
* @brief info 命令组处理函数
*
* 演示: 如何实现命令组,组织多个相关子命令
* @param argc 参数个数(包括命令名)
* @param argv argv[0]是命令名, argv[1]是子命令名
*/
static int cmd_info_handler(int argc, char **argv)
{
Shell *shell = shellGetCurrent();
int count = sizeof(info_cmds) / sizeof(info_cmds[0]);
/* 没有子命令,显示帮助 */
if (argc == 1) {
info_show_help();
return 0;
}
/* 查找并执行子命令 */
const char *subcmd = argv[1];
for (int i = 0; i < count; i++) {
if (strcmp(info_cmds[i].name, subcmd) == 0) {
/* 找到子命令,执行它 */
/* argc-2: 去除命令名和子命令名 */
/* argv+2: 剩余的参数 */
return info_cmds[i].handler(argc - 2, argv + 2);
}
}
/* 未找到子命令 */
shellPrint(shell, "Error: unknown subcommand '%s'\n", subcmd);
info_show_help();
return -1;
}
/* 注册 info 命令组 */
SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_DISABLE_RETURN,
info,
cmd_info_handler,
system information commands
);
/* ========== 示例5: 带选项的命令 ========== */
/**
* @brief list 命令 - 演示如何处理命令选项
*
* 使用示例:
* list - 简单列表
* list -l - 详细列表
* list -a - 显示所有(包括隐藏项)
* list -la - 详细列表+显示所有
*/
static int cmd_list(int argc, char **argv)
{
Shell *shell = shellGetCurrent();
int verbose = 0; /* -l 选项 */
int show_all = 0; /* -a 选项 */
/* 解析选项 */
for (int i = 0; i < argc; i++) {
if (argv[i][0] == '-') {
for (int j = 1; argv[i][j] != '\0'; j++) {
if (argv[i][j] == 'l') {
verbose = 1;
} else if (argv[i][j] == 'a') {
show_all = 1;
} else {
shellPrint(shell, "Unknown option: -%c\n", argv[i][j]);
return -1;
}
}
}
}
/* 根据选项执行不同的显示 */
shellPrint(shell, "\nFile List:\n");
if (verbose) {
shellPrint(shell, "%-20s %10s %s\n", "Name", "Size", "Date");
shellPrint(shell, "------------------------------------------\n");
shellPrint(shell, "%-20s %10s %s\n", "main.c", "1234", "2025-01-15");
shellPrint(shell, "%-20s %10s %s\n", "shell.c", "5678", "2025-01-14");
if (show_all) {
shellPrint(shell, "%-20s %10s %s\n", ".config", "256", "2025-01-13");
}
} else {
shellPrint(shell, "main.c shell.c");
if (show_all) {
shellPrint(shell, " .config");
}
shellPrint(shell, "\n");
}
shellPrint(shell, "\n");
LISA_LOGI(LOG_TAG, "List files (verbose=%d, all=%d)\n", verbose, show_all);
return 0;
}
/* 注册 list 命令 */
SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
list,
cmd_list,
list files (-l: verbose, -a: all)
);
/* ========== 应用程序主函数 ========== */
/**
* @brief 应用程序入口
*/
int main(int argc, char **argv)
{
LISA_LOGI(LOG_TAG, "=== LISA Shell Usage Sample ===\n");
/* 初始化 LISA Shell */
int ret = lisa_shell_init();
if (ret != 0) {
LISA_LOGE(LOG_TAG, "Failed to initialize shell (error: %d)\n", ret);
return ret;
}
LISA_LOGI(LOG_TAG, "Shell initialized successfully\n");
LISA_LOGI(LOG_TAG, "This sample demonstrates:\n");
LISA_LOGI(LOG_TAG, " 1. Simple command (version)\n");
LISA_LOGI(LOG_TAG, " 2. Command with parameters (echo)\n");
LISA_LOGI(LOG_TAG, " 3. Optional parameters (memread)\n");
LISA_LOGI(LOG_TAG, " 4. Command group (info)\n");
LISA_LOGI(LOG_TAG, " 5. Command options (list)\n");
LISA_LOGI(LOG_TAG, "Type 'help' in shell to see all commands\n\n");
/* 主循环 */
while (1) {
vTaskDelay(pdMS_TO_TICKS(10000));
}
return 0;
}