mirror of
https://gitee.com/moluo-tech/CodeBrick.git
synced 2025-01-30 04:42:53 +08:00
支持AT指令解析及回显控制,解决缓冲区满之后收不到新数据的问题
This commit is contained in:
parent
9d5aca7e83
commit
d5e06567a4
168
framework/cli.c
168
framework/cli.c
@ -1,7 +1,7 @@
|
||||
/******************************************************************************
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD>
|
||||
*
|
||||
* Copyright (c) 2015-2020, <morro_luo@163.com>
|
||||
* Copyright (c) 2015-2020, <master_roger@sina.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
@ -12,29 +12,29 @@
|
||||
* 2017-07-04 Morro <EFBFBD>Ż<EFBFBD><EFBFBD>ֶηָ<EFBFBD><EFBFBD>
|
||||
*
|
||||
* 2020-07-05 Morro ʹ<EFBFBD><EFBFBD>cli_obj_t<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ֧<EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
*
|
||||
* 2021-08-30 Morro <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD>ղ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* 2020-08-29 Morro ֧<EFBFBD><EFBFBD>ATָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD>ղ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* 2020-02-16 Morro <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
******************************************************************************/
|
||||
#include "cli.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
static const cmd_item_t cmd_tbl_start SECTION("cli.cmd.0") = {0};
|
||||
static const cmd_item_t cmd_tbl_end SECTION("cli.cmd.4") = {0};
|
||||
/*
|
||||
/**
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param[in] keyword - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD><EFBFBD><EFBFBD>
|
||||
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
*/
|
||||
static const cmd_item_t *find_cmd(const char *keyword)
|
||||
static const cmd_item_t *find_cmd(const char *keyword, int n)
|
||||
{
|
||||
const cmd_item_t *it;
|
||||
for (it = &cmd_tbl_start + 1; it < &cmd_tbl_end; it++) {
|
||||
if (!strcasecmp(keyword, it->name))
|
||||
return it;
|
||||
if (!strncasecmp(keyword, it->name, n))
|
||||
return it;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@ -78,7 +78,7 @@ static size_t strsplit(char *s, const char *separator, char *list[], size_t len
|
||||
return count;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
*@brief <EFBFBD><EFBFBD>ӡһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڿ<EFBFBD><EFBFBD><EFBFBD>̨
|
||||
*@retval
|
||||
*/
|
||||
@ -90,11 +90,83 @@ static void cli_print(cli_obj_t *obj, const char *format, ...)
|
||||
va_start (args, format);
|
||||
len = vsnprintf (buf, sizeof(buf), format, args);
|
||||
va_end (args);
|
||||
obj->write(buf, len);
|
||||
obj->write(buf, len);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
/**
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param[in] line - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @return none
|
||||
**/
|
||||
static void process_line(cli_obj_t *obj)
|
||||
{
|
||||
char *argv[CLI_MAX_ARGS];
|
||||
int argc, ret, isat = 0;
|
||||
const cmd_item_t *it;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
if (obj->guard && !obj->guard(obj->recvbuf))
|
||||
return;
|
||||
|
||||
if (obj->echo) { //<2F><><EFBFBD><EFBFBD>
|
||||
obj->print(obj,"%s\r\n",obj->recvbuf);
|
||||
}
|
||||
|
||||
argc = strsplit(obj->recvbuf, ",",argv, CLI_MAX_ARGS);
|
||||
|
||||
const char *start, *end;
|
||||
|
||||
if (argv[0] == NULL)
|
||||
return;
|
||||
|
||||
if (strcasecmp("AT", argv[0]) == 0) {
|
||||
obj->print(obj, "OK\r\n");
|
||||
return;
|
||||
}
|
||||
#if CLI_AT_ENABLE != 0
|
||||
isat = strncasecmp(argv[0], "AT+", 3) == 0;
|
||||
#endif
|
||||
|
||||
start= !isat ? argv[0] : argv[0] + 3;
|
||||
|
||||
if ((end = strchr(start, '=')) != NULL) {
|
||||
obj->type = CLI_CMD_TYPE_SET;
|
||||
|
||||
} else if ((end = strchr(start, '?')) != NULL) {
|
||||
obj->type = CLI_CMD_TYPE_QUERY;
|
||||
|
||||
} else {
|
||||
obj->type = CLI_CMD_TYPE_EXEC;
|
||||
end = start + strlen(argv[0]);
|
||||
}
|
||||
if (start == end)
|
||||
return;
|
||||
|
||||
if ((it = find_cmd(start, end - start)) == NULL) {
|
||||
obj->print(obj, "%s\r\n", isat ? "ERROR" : "");
|
||||
return;
|
||||
}
|
||||
ret = it->handler(obj, argc, argv);
|
||||
if (isat) {
|
||||
obj->print(obj, "%s\r\n" ,ret ? "OK":"ERROR");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
||||
*/
|
||||
static int get_val(struct cli_obj *self)
|
||||
{
|
||||
char *p;
|
||||
p = strchr(self->recvbuf, '=');
|
||||
if ( p == NULL)
|
||||
return 0;
|
||||
return atoi(p + 1);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief cli <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
||||
* @param[in] p - cli<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
||||
* @return none
|
||||
@ -105,19 +177,24 @@ void cli_init(cli_obj_t *obj, const cli_port_t *p)
|
||||
obj->write = p->write;
|
||||
obj->print = cli_print;
|
||||
obj->enable = true;
|
||||
obj->get_val = get_val;
|
||||
obj->guard = p->cmd_guard;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>cli<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ(cli<EFBFBD><EFBFBD>ʱ<EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
||||
* @param[in] none
|
||||
* @return none
|
||||
**/
|
||||
void cli_enable(cli_obj_t *obj)
|
||||
{
|
||||
char a;
|
||||
obj->enable = true;
|
||||
obj->recvcnt = 0;
|
||||
while (obj->read(&a, 1) > 0) {}
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* @brief <EFBFBD>˳<EFBFBD>cli<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ(cli<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
||||
* @param[in] none
|
||||
* @return none
|
||||
@ -127,60 +204,62 @@ void cli_disable (cli_obj_t *obj)
|
||||
obj->enable = false;
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param[in] line - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
/**
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param[in] echo - <EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD>ؿ<EFBFBD><EFBFBD><EFBFBD>(0/1)
|
||||
* @return none
|
||||
**/
|
||||
static void process_line(cli_obj_t *obj)
|
||||
void cli_echo_ctrl (cli_obj_t *obj, int echo)
|
||||
{
|
||||
char *argv[CLI_MAX_ARGS];
|
||||
int argc;
|
||||
const cmd_item_t *it;
|
||||
argc = strsplit(obj->recvbuf, " ,",argv, CLI_MAX_ARGS);
|
||||
if ((it = find_cmd(argv[0])) == NULL) {
|
||||
obj->print(obj, "Unknown command '%s' - try 'help'\r\n", argv[0]);
|
||||
return;
|
||||
}
|
||||
it->handler(obj, argc, argv);
|
||||
obj->echo = echo;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* @brief ִ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>cli<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD>)
|
||||
* @param[in] none
|
||||
* @return none
|
||||
**/
|
||||
void cli_exec_cmd(cli_obj_t *obj, const char *cmd)
|
||||
{
|
||||
snprintf(obj->recvbuf, CLI_MAX_CMD_LEN, "%s", cmd);
|
||||
int len = strlen(cmd);
|
||||
if (len >= CLI_MAX_CMD_LEN - 1)
|
||||
return;
|
||||
strcpy(obj->recvbuf, cmd);
|
||||
process_line(obj);
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param[in] none
|
||||
* @return none
|
||||
**/
|
||||
void cli_process(cli_obj_t *obj)
|
||||
{
|
||||
int i;
|
||||
char buf[32];
|
||||
int i, readcnt;
|
||||
if (!obj->read || !obj->enable)
|
||||
return;
|
||||
i = obj->recvcnt;
|
||||
obj->recvcnt += obj->read(&obj->recvbuf[i], CLI_MAX_CMD_LEN - i);
|
||||
while (i < obj->recvcnt) {
|
||||
if (obj->recvbuf[i] == '\r' || obj->recvbuf[i] == '\n') { /*<2A><>ȡ1<C8A1><31>*/
|
||||
obj->recvbuf[i] = '\0';
|
||||
process_line(obj);
|
||||
obj->recvcnt = 0;
|
||||
|
||||
readcnt = obj->read(buf, sizeof(buf));
|
||||
|
||||
if (readcnt) {
|
||||
for (i = 0; i < readcnt; i++) {
|
||||
if (buf[i] == '\r' || buf[i] == '\n' || buf[i] == '\0') {
|
||||
obj->recvbuf[obj->recvcnt] = '\0';
|
||||
if (obj->recvcnt > 1)
|
||||
process_line(obj);
|
||||
obj->recvcnt = 0;
|
||||
} else {
|
||||
obj->recvbuf[obj->recvcnt++] = buf[i];
|
||||
|
||||
if (obj->recvcnt >= CLI_MAX_CMD_LEN) /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE>ǿ<EFBFBD><C7BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
obj->recvcnt = 0;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (obj->recvcnt >= CLI_MAX_CMD_LEN) /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE>ǿ<EFBFBD><C7BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
obj->recvcnt = 0;
|
||||
}
|
||||
|
||||
|
||||
#if 1
|
||||
/*******************************************************************************
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param[in] none
|
||||
@ -192,6 +271,7 @@ static int cmd_item_comparer(const void *item1,const void *item2)
|
||||
cmd_item_t *it2 = *((cmd_item_t **)item2);
|
||||
return strcmp(it1->name, it2->name);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
@ -204,16 +284,15 @@ static int do_help (struct cli_obj *s, int argc, char *argv[])
|
||||
const cmd_item_t *cmdtbl[CLI_MAX_CMDS];
|
||||
|
||||
if (argc == 2) {
|
||||
if ((item_start = find_cmd(argv[1])) != NULL)
|
||||
if ((item_start = find_cmd(argv[1], strlen(argv[1]))) != NULL)
|
||||
{
|
||||
s->print(s, item_start->brief); /*<2A><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>Ϣ----*/
|
||||
s->print(s, "\r\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
for (i = 0; i < item_end - item_start && i < CLI_MAX_CMDS; i++) {
|
||||
for (i = 0; i < item_end - item_start && i < CLI_MAX_ARGS; i++)
|
||||
cmdtbl[i] = &item_start[i];
|
||||
}
|
||||
count = i;
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ---------------------------------------------------------*/
|
||||
qsort(cmdtbl, i, sizeof(cmd_item_t*), cmd_item_comparer);
|
||||
@ -237,4 +316,3 @@ static int do_help (struct cli_obj *s, int argc, char *argv[])
|
||||
/*ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ---------------------------------------------------------------*/
|
||||
cmd_register("help", do_help, "list all command.");
|
||||
cmd_register("?", do_help, "alias for 'help'");
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
/******************************************************************************
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD>
|
||||
*
|
||||
* Copyright (c) 2015-2020, <morro_luo@163.com>
|
||||
* Copyright (c) 2015-2020, <master_roger@sina.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
@ -12,24 +12,41 @@
|
||||
* 2017-07-04 Morro <EFBFBD>Ż<EFBFBD><EFBFBD>ֶηָ<EFBFBD><EFBFBD>
|
||||
*
|
||||
* 2020-07-05 Morro ʹ<EFBFBD><EFBFBD>cli_obj_t<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ֧<EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
*
|
||||
* 2021-08-30 Morro <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD>ղ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* 2020-08-29 Morro ֧<EFBFBD><EFBFBD>ATָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD>ղ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* 2020-02-16 Morro <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
******************************************************************************/
|
||||
#ifndef _CMDLINE_H_
|
||||
#define _CMDLINE_H_
|
||||
|
||||
#include "comdef.h"
|
||||
|
||||
#define CLI_MAX_CMD_LEN 64 /*<2A><><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3><EFBFBD>*/
|
||||
#define CLI_MAX_ARGS 16 /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
#define CLI_MAX_CMDS 32 /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
#define CLI_MAX_CMD_LEN 256 /*<2A><><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3><EFBFBD>*/
|
||||
#define CLI_MAX_ARGS 64 /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
#define CLI_MAX_CMDS 64 /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
|
||||
/**
|
||||
* @brief CLI<EFBFBD><EFBFBD>ΪAT<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>,<EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD>ʱ<EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"AT+",
|
||||
*/
|
||||
#define CLI_AT_ENABLE 1
|
||||
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
#define CLI_CMD_TYPE_EXEC 0 /* <20><>ִͨ<CDA8><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
#define CLI_CMD_TYPE_QUERY 1 /* <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD> (XXX?)*/
|
||||
#define CLI_CMD_TYPE_SET 2 /* <20>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD> (XXX=YY)*/
|
||||
|
||||
struct cli_obj;
|
||||
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB6A8>*/
|
||||
typedef struct {
|
||||
char *name; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
/*<2A><><EFBFBD><EFBFBD><EEB4A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
char *name; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
/**
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @params o - cli <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @params argc - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @params argv - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ATָ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>trueʱ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD>ӦOK,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>falseʱ<EFBFBD><EFBFBD>
|
||||
* <EFBFBD><EFBFBD>ӦERROR
|
||||
*/
|
||||
int (*handler)(struct cli_obj *o, int argc, char *argv[]);
|
||||
const char *brief; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
}cmd_item_t;
|
||||
@ -50,18 +67,35 @@ typedef struct {
|
||||
|
||||
/*cli <20>ӿڶ<D3BF><DAB6><EFBFBD> -------------------------------------------------------------*/
|
||||
typedef struct {
|
||||
/**
|
||||
* @brief ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)д<EFBFBD>ӿ<EFBFBD>
|
||||
*/
|
||||
unsigned int (*write)(const void *buf, unsigned int len);
|
||||
/**
|
||||
* @brief ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
||||
*/
|
||||
unsigned int (*read) (void *buf, unsigned int len);
|
||||
|
||||
/**
|
||||
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>дNULL,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD>)
|
||||
* @retval true - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD>, false - <EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
*/
|
||||
int (*cmd_guard)(char *cmdline);
|
||||
|
||||
}cli_port_t;
|
||||
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>*/
|
||||
typedef struct cli_obj {
|
||||
int (*guard)(char *cmdline);
|
||||
unsigned int (*write)(const void *buf, unsigned int len);
|
||||
unsigned int (*read) (void *buf, unsigned int len);
|
||||
void (*print)(struct cli_obj *this, const char *fmt, ...);
|
||||
char recvbuf[CLI_MAX_CMD_LEN + 1]; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
unsigned short recvcnt; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ճ<EFBFBD><D5B3><EFBFBD>*/
|
||||
unsigned enable : 1;
|
||||
int (*get_val)(struct cli_obj *this);
|
||||
char recvbuf[CLI_MAX_CMD_LEN + 1]; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
unsigned short recvcnt; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ճ<EFBFBD><D5B3><EFBFBD>*/
|
||||
unsigned type : 3; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
unsigned enable : 1; /* CLI <20><><EFBFBD>ؿ<EFBFBD><D8BF><EFBFBD>*/
|
||||
unsigned echo : 1; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
}cli_obj_t;
|
||||
|
||||
void cli_init(cli_obj_t *obj, const cli_port_t *p);
|
||||
@ -70,10 +104,11 @@ void cli_enable(cli_obj_t *obj);
|
||||
|
||||
void cli_disable (cli_obj_t *obj);
|
||||
|
||||
void cli_echo_ctrl (cli_obj_t *obj, int echo);
|
||||
|
||||
void cli_exec_cmd(cli_obj_t *obj, const char *cmd);
|
||||
|
||||
void cli_process(cli_obj_t *obj);
|
||||
|
||||
|
||||
|
||||
#endif /* __CMDLINE_H */
|
||||
|
Loading…
x
Reference in New Issue
Block a user