From 2ef669be371ea18ffb67cc3b6842736127406abe Mon Sep 17 00:00:00 2001 From: "huanfeng.wang" Date: Tue, 9 Jan 2024 14:11:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eshell=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E6=B3=A8=E5=86=8C=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interface/arm_interface.h | 15 +++++ interface/typedef.h | 2 + osp/inc/ospShell.h | 6 +- osp/src/ospShell.c | 124 ++++++++++++++++++++++++++------------ 4 files changed, 105 insertions(+), 42 deletions(-) diff --git a/interface/arm_interface.h b/interface/arm_interface.h index c3af052..18ab48f 100644 --- a/interface/arm_interface.h +++ b/interface/arm_interface.h @@ -124,5 +124,20 @@ int32_t set_clk_mode(clk_flag_e pseudo_flag); int arm_csu_dma_1D_transfer(uint64_t addrSrc, uint64_t addrDst, uint32_t dataLen); int arm_csu_wait_done(uint8_t tag); +/* + 函数名称:osp_insert_cmd + 函数入参:name : shell命令,长度不要超过11个字符 + 函数入参:pfunc : 对应shell命令的回调函数 + 函数入参:desc : 对应shell命令的描述 + 函数入参:argnum : 对应shell命令的参数个数,小于等于6 + 函数功能:动态注册shell命令 + return value:0:success -1:error + 说明:动态注册命令中的打印需使用USHELL_PRINT + 比如:USHELL_PRINT("arg1:%d arg2:%d arg3:%d\n", arg1, arg2, arg3); +*/ +int32_t osp_insert_cmd(char *name, OSP_FUNCPTR pfunc, char *desc, uint32_t argnum); +void osp_ushell_output(const char *fmt, ...); +#define USHELL_PRINT(fmt, args...) osp_ushell_output(fmt "\n", ##args) + #endif /* __ARM_INTERFACE_H__ */ diff --git a/interface/typedef.h b/interface/typedef.h index 74b4889..98272e3 100644 --- a/interface/typedef.h +++ b/interface/typedef.h @@ -28,4 +28,6 @@ typedef unsigned long int uint64_t; //typedef long long int int64_t; //typedef unsigned long long int uint64_t; +typedef uint64_t (*OSP_FUNCPTR)(); + #endif diff --git a/osp/inc/ospShell.h b/osp/inc/ospShell.h index b09c483..beb5d73 100644 --- a/osp/inc/ospShell.h +++ b/osp/inc/ospShell.h @@ -18,6 +18,7 @@ extern "C" { #define SPU_SHELL_MSG_DADA_LEN(x) (x->u16DataLen - OSP_SW_MSG_INFO_SIZE) #define SPU_SHELL_MSG_HEAD_TO_COMM(x) (char *)((uint64_t)x + OSP_SW_MSG_INFO_SIZE) #define SPU_SHELL_DADA_LEN(x) (x->u16DataLen) +#define USHELL_PRINT(fmt, args...) osp_ushell_output(fmt "\n", ##args) typedef struct tag_Osp_Ape_Msg_Head { @@ -69,8 +70,7 @@ extern int32_t osp_shell_main(); extern int32_t osp_execcmd(void); extern int32_t osp_getcmdarg(char *cmd); extern void osp_set_prompt(); -extern int32_t osp_insert_cmd(char *name, OSP_FUNCPTR pfunc); -extern int32_t osp_insert_cmd_ext(char *name, OSP_FUNCPTR pfunc, char *desc, int32_t argnum); +extern int32_t osp_insert_cmd(char *name, OSP_FUNCPTR pfunc, char *desc, uint32_t argnum); int32_t osp_shell_init(void); bool osp_is_ape_cmd(char *pCmd); void osp_get_ape_id(uint8_t ucApeid); @@ -109,6 +109,8 @@ void osp_set_clk_mode(clk_flag_e pseudo_flag);*/ void osp_show_swqueue_info(); void osp_show_info_by_coreid(osp_sw_msg_info_t *pMsg); void osp_set_oam(uint8_t ucOam); +void osp_ushell_output(const char *fmt, ...); +void osp_ushell_test(); #ifdef __cplusplus diff --git a/osp/src/ospShell.c b/osp/src/ospShell.c index 4ef36c9..31c2aa4 100644 --- a/osp/src/ospShell.c +++ b/osp/src/ospShell.c @@ -6,7 +6,7 @@ #include #include #include - +#include #include "osp.h" #include "ospShell.h" #include "ospDbg.h" @@ -34,8 +34,6 @@ extern uint8_t g_ucPlatformMode; /*0: platform log日志写入文件 1: 网 extern OSP_CMD_RTN ospCmdRtnTbl[]; extern int32_t SHELL_CMD_NUM; -extern char *pShellBuf; - /*function prototype */ extern void osp_set_prompt(); @@ -972,57 +970,64 @@ void osp_shell_get_line() } } -int32_t osp_insert_cmd(char *name, OSP_FUNCPTR pfunc) +int32_t osp_insert_cmd(char *name, OSP_FUNCPTR pfunc, char *desc, uint32_t argnum) { - OSP_CMD_RTN *tlb; - int32_t num; + OSP_CMD_RTN *tlb = NULL; + uint8_t num = 0; + uint8_t i = 0; + uint8_t u8str_len = 0; - if ((name == NULL) || pfunc == NULL) + if ((NULL == name) || NULL == pfunc || NULL == desc) { - return -1; - - } + UCP_PRINT_ERROR("osp_insert_cmd_ext name:%p pfunc:%p desc:%p", name, pfunc, desc); + return OSP_ERROR; + } + + if (argnum > 6) + { + UCP_PRINT_ERROR("osp_insert_cmd argnum:%u beyond 6", argnum); + return OSP_ERROR; + } + + for (i = 0; i < MAX_EXT_CMD_NUM; i++) + { + if (strcmp(ospCmdRtnTblExt[i].cmd, name) == 0) + { + UCP_PRINT_ERROR("osp_insert_cmd i:%u cmd:%s has exist name:%s", i, ospCmdRtnTblExt[i].cmd, name); + return OSP_ERROR; + } + } + osp_sem_take(ospCmdInsertSem, -1); + tlb = ospCmdRtnTblExt; num = SHELL_CMD_NUM_EXT; - - memcpy(tlb[num].cmd, name, strlen(name) + 1); - tlb[num].wrapper = (OSP_FUNCPTR)osp_shell_wrapper; - tlb[num].routine = pfunc; - - SHELL_CMD_NUM_EXT++; - osp_sem_give(ospCmdInsertSem); - return 0; -} -int32_t osp_insert_cmd_ext(char *name, OSP_FUNCPTR pfunc, char *desc, int32_t argnum) -{ - OSP_CMD_RTN *tlb; - int32_t num; - - if ((name == NULL) || pfunc == NULL) + if(num >= MAX_EXT_CMD_NUM) + { + UCP_PRINT_ERROR("osp_insert_cmd num:%u beyond MAX_EXT_CMD_NUM:%d\r\n", num, MAX_EXT_CMD_NUM); + return OSP_ERROR; + } + + u8str_len = strlen(name); + if(u8str_len > 12) { - return -1; - - } - tlb = ospCmdRtnTblExt; - num = SHELL_CMD_NUM_EXT; + UCP_PRINT_ERROR("osp_insert_cmd u8str_len:%u beyond 12", u8str_len); + return OSP_ERROR; + } memcpy(tlb[num].cmd, name, strlen(name) + 1); memcpy(tlb[num].descption, desc, strlen(desc) + 1); - - tlb[num].wrapper = (OSP_FUNCPTR)osp_ape_shell_wrapper; - //tlb[num].routine = pfunc; - tlb[num].argnum = argnum; - - UCP_PRINT_SHELL("osp_insert_cmd_ext cmd:%s num:%u descption:%s desc:%s argnum:%u", - tlb[num].cmd,num, tlb[num].descption, desc, tlb[num].argnum); + tlb[num].wrapper = (OSP_FUNCPTR)osp_shell_wrapper; + tlb[num].routine = pfunc; + tlb[num].argnum = argnum; SHELL_CMD_NUM_EXT++; - - return 0; + + osp_sem_give(ospCmdInsertSem); + + return OSP_OK; } - int32_t osp_shell_init(void) { @@ -1038,8 +1043,47 @@ int32_t osp_shell_init(void) /*配置文件读取server ip ,port*/ osp_server_get(); + + osp_insert_cmd("test", (OSP_FUNCPTR)osp_ushell_test, "test", 0); + return 0; } + +void osp_ushell_output(const char *fmt, ...) +{ + uint32_t u32str_len = 0; + char acstr[256] = {0}; + va_list st_va_list; + + if(NULL == fmt) + { + UCP_PRINT_ERROR("osp_ushell_output fmt is null"); + return; + } + + va_start(st_va_list, fmt); + u32str_len = vsprintf((char *)acstr, fmt, st_va_list); + va_end(st_va_list); + + osp_net_shell_out(acstr, u32str_len); + + return; +} + +void osp_ushell_test() +{ + OSP_RTCTIME osptime; + + osp_get_rtc_time(&osptime); + sprintf((char *)g_time_now, \ + "[%04d-%02d-%02d-%02d:%02d:%02d:%02d]", \ + osptime.year, osptime.month, osptime.day, \ + osptime.hour, osptime.minute, osptime.second, osptime.usecond); + + USHELL_PRINT("%s", g_time_now); + + return; +} bool osp_is_ape_cmd(char *pCmd) {