更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
shmsg.c 文件参考

浏览源代码.

枚举

enum  { STAT_NORMAL_KEY , STAT_ESC_KEY , STAT_MULTI_KEY }
 

函数

CHARShellGetInputBuf (ShellCB *shellCB)
 
STATIC VOID ShellSaveHistoryCmd (const CHAR *string, ShellCB *shellCB)
 保存命令历史记录,这个函数写的不太好 更多...
 
STATIC VOID ShellNotify (ShellCB *shellCB)
 发送解析事件 更多...
 
STATIC INT32 ShellCmdLineCheckUDRL (const CHAR ch, ShellCB *shellCB)
 
LITE_OS_SEC_TEXT_MINOR VOID ShellCmdLineParse (CHAR c, pf_OUTPUT outputFunc, ShellCB *shellCB)
 对命令行内容解析 更多...
 
LITE_OS_SEC_TEXT_MINOR UINT32 ShellMsgTypeGet (CmdParsed *cmdParsed, const CHAR *cmdType)
 获取shell消息类型 更多...
 
STATIC UINT32 ShellMsgNameGetAndExec (CmdParsed *cmdParsed, const CHAR *output, UINT32 len)
 获取命令名称和参数,并执行 更多...
 
LITE_OS_SEC_TEXT_MINOR UINT32 ShellMsgParse (const VOID *msg)
 命令内容解析 更多...
 
LITE_OS_SEC_TEXT_MINOR UINT32 ShellEntry (UINTPTR param)
 读取命令行内容 更多...
 
STATIC VOID ShellCmdProcess (ShellCB *shellCB)
 
LITE_OS_SEC_TEXT_MINOR UINT32 ShellTask (UINTPTR param1, UINTPTR param2, UINTPTR param3, UINTPTR param4)
 shell 任务,处理解析,执行命令 更多...
 
LITE_OS_SEC_TEXT_MINOR UINT32 ShellTaskInit (ShellCB *shellCB)
 给控制台注册一个shell客户端任务 更多...
 
LITE_OS_SEC_TEXT_MINOR UINT32 ShellEntryInit (ShellCB *shellCB)
 进入shell客户端任务初始化,这个任务负责编辑命令,处理命令产生的过程,例如如何处理方向键,退格键,回车键等 更多...
 

枚举类型说明

◆ anonymous enum

anonymous enum
枚举值
STAT_NORMAL_KEY 

普通的按键

STAT_ESC_KEY 
STAT_MULTI_KEY 

在文件 shmsg.c109 行定义.

109 {
110 STAT_NORMAL_KEY, ///< 普通的按键
111 STAT_ESC_KEY, //<ESC>键在VT控制规范中时控制的起始键
112 STAT_MULTI_KEY //组合键
113};
@ STAT_ESC_KEY
<ESC>键,支持VT规范
Definition: shmsg.c:137
@ STAT_NORMAL_KEY
普通按键
Definition: shmsg.c:136
@ STAT_MULTI_KEY
组合键 例如: <ESC>[30m
Definition: shmsg.c:138

函数说明

◆ ShellCmdLineCheckUDRL()

STATIC INT32 ShellCmdLineCheckUDRL ( const CHAR  ch,
ShellCB shellCB 
)

在文件 shmsg.c131 行定义.

132{
133 INT32 ret = LOS_OK;
134 if (ch == 0x1b) { /* 0x1b: ESC *///按下<ESC>键(逃逸键)
135 shellCB->shellKeyType = STAT_ESC_KEY;//代表控制开始
136 return ret;
137 } else if (ch == 0x5b) { /* 0x5b: first Key combination */ //为[键 ,遵循 vt100 规则
138 if (shellCB->shellKeyType == STAT_ESC_KEY) {
139 shellCB->shellKeyType = STAT_MULTI_KEY;
140 return ret;
141 }
142 } else if (ch == 0x41) { /* up */ //上方向键
143 if (shellCB->shellKeyType == STAT_MULTI_KEY) {
145 shellCB->shellKeyType = STAT_NORMAL_KEY;
146 return ret;
147 }
148 } else if (ch == 0x42) { /* down *///下方向键
149 if (shellCB->shellKeyType == STAT_MULTI_KEY) {
150 shellCB->shellKeyType = STAT_NORMAL_KEY;
152 return ret;
153 }
154 } else if (ch == 0x43) { /* right *///右方向键
155 if (shellCB->shellKeyType == STAT_MULTI_KEY) {
156 shellCB->shellKeyType = STAT_NORMAL_KEY;
157 return ret;
158 }
159 } else if (ch == 0x44) { /* left *///左方向键
160 if (shellCB->shellKeyType == STAT_MULTI_KEY) {
161 shellCB->shellKeyType = STAT_NORMAL_KEY;
162 return ret;
163 }
164 }
165 return LOS_NOK;
166}
void OsShellHistoryShow(unsigned int value, ShellCB *shellCB)
显示shell命令历史记录,支持上下键方式
Definition: shcmd.c:540
@ CMD_KEY_DOWN
方向下键
Definition: shell.h:97
@ CMD_KEY_UP
方向上键
Definition: shell.h:96
signed int INT32
Definition: los_typedef.h:60
unsigned int shellKeyType
按键类型
Definition: shell.h:79
函数调用图:
这是这个函数的调用关系图:

◆ ShellCmdLineParse()

LITE_OS_SEC_TEXT_MINOR VOID ShellCmdLineParse ( CHAR  c,
pf_OUTPUT  outputFunc,
ShellCB shellCB 
)

对命令行内容解析

在文件 shmsg.c168 行定义.

169{
170 const CHAR ch = c;
171 INT32 ret;
172 //不是回车键和字符串结束,且偏移量为0
173 if ((shellCB->shellBufOffset == 0) && (ch != '\n') && (ch != '\0')) {
174 (VOID)memset_s(shellCB->shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN);//重置buf
175 }
176 //遇到回车或换行
177 if ((ch == '\r') || (ch == '\n')) {
178 if (shellCB->shellBufOffset < (SHOW_MAX_LEN - 1)) {
179 shellCB->shellBuf[shellCB->shellBufOffset] = '\0';//字符串结束
180 }
181 shellCB->shellBufOffset = 0;
182 (VOID)pthread_mutex_lock(&shellCB->keyMutex);
183 OsShellCmdPush(shellCB->shellBuf, shellCB->cmdKeyLink);//解析回车或换行
184 (VOID)pthread_mutex_unlock(&shellCB->keyMutex);
185 ShellNotify(shellCB);//通知任务解析shell命令
186 return;
187 } else if ((ch == '\b') || (ch == 0x7F)) { /* backspace or delete(0x7F) */ //遇到删除键
188 if ((shellCB->shellBufOffset > 0) && (shellCB->shellBufOffset < (SHOW_MAX_LEN - 1))) {
189 shellCB->shellBuf[shellCB->shellBufOffset - 1] = '\0';//填充`\0`
190 shellCB->shellBufOffset--;//buf减少
191 outputFunc("\b \b");//回调入参函数
192 }
193 return;
194 } else if (ch == 0x09) { /* 0x09: tab *///遇到tab键
195 if ((shellCB->shellBufOffset > 0) && (shellCB->shellBufOffset < (SHOW_MAX_LEN - 1))) {
196 ret = OsTabCompletion(shellCB->shellBuf, &shellCB->shellBufOffset);//解析tab键
197 if (ret > 1) {
198 outputFunc("OHOS # %s", shellCB->shellBuf);//回调入参函数
199 }
200 }
201 return;
202 }
203 /* parse the up/down/right/left key */
204 ret = ShellCmdLineCheckUDRL(ch, shellCB);//解析上下左右键
205 if (ret == LOS_OK) {
206 return;
207 }
208
209 if ((ch != '\n') && (ch != '\0')) {//普通的字符的处理
210 if (shellCB->shellBufOffset < (SHOW_MAX_LEN - 1)) {//buf范围
211 shellCB->shellBuf[shellCB->shellBufOffset] = ch;//直接加入
212 } else {
213 shellCB->shellBuf[SHOW_MAX_LEN - 1] = '\0';//加入字符串结束符
214 }
215 shellCB->shellBufOffset++;//偏移量增加
216 outputFunc("%c", ch);//向终端输出字符
217 }
218
219 shellCB->shellKeyType = STAT_NORMAL_KEY;//普通字符
220}
void OsShellCmdPush(const char *string, CmdKeyLink *cmdKeyLink)
将shell命令 string 以 CmdKeyLink 方式加入链表
Definition: shcmd.c:514
int OsTabCompletion(char *cmdKey, unsigned int *len)
tab键
Definition: shcmd.c:452
int ShellNotify(ShellCB *shellCB)
Definition: shmsg.c:126
char CHAR
Definition: los_typedef.h:63
int pthread_mutex_lock(pthread_mutex_t *mutex)
互斥锁加锁操作
int pthread_mutex_unlock(pthread_mutex_t *mutex)
解锁互斥锁
STATIC INT32 ShellCmdLineCheckUDRL(const CHAR ch, ShellCB *shellCB)
Definition: shmsg.c:131
char shellBuf[SHOW_MAX_LEN]
接受shell命令 buf大小
Definition: shell.h:83
void * cmdKeyLink
命令链表,所有敲过的命令链表
Definition: shell.h:75
unsigned int shellBufOffset
buf偏移量
Definition: shell.h:78
pthread_mutex_t keyMutex
操作cmdKeyLink的互斥量
Definition: shell.h:81
函数调用图:

◆ ShellCmdProcess()

STATIC VOID ShellCmdProcess ( ShellCB shellCB)

在文件 shmsg.c378 行定义.

379{
380 CHAR *buf = NULL;
381 while (1) {
382 buf = ShellGetInputBuf(shellCB);//获取命令buf
383 if (buf == NULL) {
384 break;
385 }
386 (VOID)ShellMsgParse(buf);//解析buf
387 ShellSaveHistoryCmd(buf, shellCB);//保存到历史记录中
388 shellCB->cmdMaskKeyLink = shellCB->cmdHistoryKeyLink;
389 }
390}
CHAR * ShellGetInputBuf(ShellCB *shellCB)
Definition: shmsg.c:50
STATIC VOID ShellSaveHistoryCmd(const CHAR *string, ShellCB *shellCB)
保存命令历史记录,这个函数写的不太好
Definition: shmsg.c:68
LITE_OS_SEC_TEXT_MINOR UINT32 ShellMsgParse(const VOID *msg)
命令内容解析
Definition: shmsg.c:289
void * cmdHistoryKeyLink
命令的历史记录链表,去重,10个
Definition: shell.h:76
void * cmdMaskKeyLink
主要用于方向键上下遍历历史命令
Definition: shell.h:77
函数调用图:
这是这个函数的调用关系图:

◆ ShellEntry()

LITE_OS_SEC_TEXT_MINOR UINT32 ShellEntry ( UINTPTR  param)

读取命令行内容

在文件 shmsg.c345 行定义.

346{
347 CHAR ch;
348 INT32 n = 0;
349 ShellCB *shellCB = (ShellCB *)param;
350
351 CONSOLE_CB *consoleCB = OsGetConsoleByID((INT32)shellCB->consoleID);//获取绑定的控制台,目的是从控制台读数据
352 if (consoleCB == NULL) {
353 PRINT_ERR("Shell task init error!\n");
354 return 1;
355 }
356
357 (VOID)memset_s(shellCB->shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN);//重置shell命令buf
358
359 while (1) {
360#ifdef LOSCFG_PLATFORM_CONSOLE
361 if (!IsConsoleOccupied(consoleCB)) {//控制台是否被占用
362#endif
363 /* is console ready for shell ? */
364 n = read(consoleCB->fd, &ch, 1);//系统调用,从控制台读取一个字符内容,字符一个个处理
365 if (n == 1) {//如果能读到一个字符
366 ShellCmdLineParse(ch, (pf_OUTPUT)dprintf, shellCB);
367 }
368 if (is_nonblock(consoleCB)) {//在非阻塞模式下暂停 50ms
369 LOS_Msleep(50); /* 50: 50MS for sleep */
370 }
371#ifdef LOSCFG_PLATFORM_CONSOLE
372 }
373#endif
374 }
375}
void ShellCmdLineParse(char c, OutputFunc outputFunc, ShellCB *shellCB)
Definition: shmsg.c:271
BOOL is_nonblock(const CONSOLE_CB *consoleCB)
Definition: console.c:1508
BOOL IsConsoleOccupied(const CONSOLE_CB *consoleCB)
控制台是否被占用
Definition: console.c:173
CONSOLE_CB * OsGetConsoleByID(INT32 consoleID)
获取参数控制台ID 获取对应的控制台控制块(描述符)
Definition: console.c:1546
LITE_OS_SEC_TEXT_MINOR VOID LOS_Msleep(UINT32 msecs)
Sleep the current task.
Definition: los_misc.c:44
void dprintf(const char *fmt,...)
Format and print data.
VOID(* pf_OUTPUT)(const CHAR *fmt,...)
Definition: los_printf.h:70
控制台控制块(描述符)
Definition: console.h:105
INT32 fd
系统文件句柄, 由内核分配
Definition: console.h:112
Definition: shell.h:71
unsigned int consoleID
控制台ID
Definition: shell.h:72
函数调用图:

◆ ShellEntryInit()

LITE_OS_SEC_TEXT_MINOR UINT32 ShellEntryInit ( ShellCB shellCB)

进入shell客户端任务初始化,这个任务负责编辑命令,处理命令产生的过程,例如如何处理方向键,退格键,回车键等

在文件 shmsg.c450 行定义.

451{
452 UINT32 ret;
453 CHAR *name = NULL;
454 TSK_INIT_PARAM_S initParam = {0};
455
456 if (shellCB->consoleID == CONSOLE_SERIAL) {//带串口功能的控制台
457 name = SERIAL_ENTRY_TASK_NAME;
458 } else if (shellCB->consoleID == CONSOLE_TELNET) {//带远程登录功能的控制台
459 name = TELNET_ENTRY_TASK_NAME;
460 } else {
461 return LOS_NOK;
462 }
463
464 initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)ShellEntry;//任务入口函数
465 initParam.usTaskPrio = 9; /* 9:shell task priority */
466 initParam.auwArgs[0] = (UINTPTR)shellCB;
467 initParam.uwStackSize = 0x1000;
468 initParam.pcName = name; //任务名称
469 initParam.uwResved = LOS_TASK_STATUS_DETACHED;
470
471 ret = LOS_TaskCreate(&shellCB->shellEntryHandle, &initParam);//创建shell任务
472#ifdef LOSCFG_PLATFORM_CONSOLE
473 (VOID)ConsoleTaskReg((INT32)shellCB->consoleID, shellCB->shellEntryHandle);//将shell注册到控制台
474#endif
475
476 return ret;
477}
void ShellEntry(ShellCB *shellCB)
Definition: shmsg.c:659
INT32 ConsoleTaskReg(INT32 consoleID, UINT32 taskID)
任务注册控制台,每个shell任务都有属于自己的控制台
Definition: console.c:1454
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskCreate(UINT32 *taskID, TSK_INIT_PARAM_S *initParam)
创建任务,并使该任务进入ready状态,如果就绪队列中没有更高优先级的任务,则运行该任务
Definition: los_task.c:718
VOID *(* TSK_ENTRY_FUNC)(UINTPTR param1, UINTPTR param2, UINTPTR param3, UINTPTR param4)
Define the type of a task entrance function.
Definition: los_task.h:480
unsigned long UINTPTR
Definition: los_typedef.h:68
unsigned int UINT32
Definition: los_typedef.h:57
pthread_t shellEntryHandle
shell客户端任务
Definition: shell.h:74
UINT16 usTaskPrio
Definition: los_task.h:505
UINTPTR auwArgs[4]
Definition: los_task.h:507
UINT32 uwStackSize
Definition: los_task.h:508
CHAR * pcName
Definition: los_task.h:509
TSK_ENTRY_FUNC pfnTaskEntry
Definition: los_task.h:504
UINT32 uwResved
Definition: los_task.h:513
函数调用图:
这是这个函数的调用关系图:

◆ ShellGetInputBuf()

CHAR * ShellGetInputBuf ( ShellCB shellCB)

在文件 shmsg.c50 行定义.

51{
52 CmdKeyLink *cmdkey = shellCB->cmdKeyLink;//待处理的shell命令链表
53 CmdKeyLink *cmdNode = NULL;
54
55 (VOID)pthread_mutex_lock(&shellCB->keyMutex);
56 if ((cmdkey == NULL) || LOS_ListEmpty(&cmdkey->list)) {//链表为空的处理
57 (VOID)pthread_mutex_unlock(&shellCB->keyMutex);
58 return NULL;
59 }
60
61 cmdNode = LOS_DL_LIST_ENTRY(cmdkey->list.pstNext, CmdKeyLink, list);//获取当前命令项
62 LOS_ListDelete(&(cmdNode->list)); /* 'cmdNode' freed in history save process *///将自己摘出去,但在历史记录中还存在
63 (VOID)pthread_mutex_unlock(&shellCB->keyMutex);
64
65 return cmdNode->cmdString;//返回命令内容
66}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListDelete(LOS_DL_LIST *node)
Definition: los_list.h:292
LITE_OS_SEC_ALW_INLINE STATIC INLINE BOOL LOS_ListEmpty(LOS_DL_LIST *list)
Identify whether a specified doubly linked list is empty. | 判断链表是否为空
Definition: los_list.h:321
struct SH_List * pstNext
Definition: shell_list.h:52
函数调用图:
这是这个函数的调用关系图:

◆ ShellMsgNameGetAndExec()

STATIC UINT32 ShellMsgNameGetAndExec ( CmdParsed cmdParsed,
const CHAR output,
UINT32  len 
)

获取命令名称和参数,并执行

在文件 shmsg.c247 行定义.

248{
249 UINT32 loop;
250 UINT32 ret;
251 const CHAR *tmpStr = NULL;
252 BOOL quotes = FALSE;
253 CHAR *msgName = (CHAR *)LOS_MemAlloc(m_aucSysMem0, len + 1);
254 if (msgName == NULL) {
255 PRINTK("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
256 return OS_INVALID;
257 }
258 /* Scan the 'output' string for command */
259 /* Notice: Command string must not have any special name */
260 for (tmpStr = output, loop = 0; (*tmpStr != '\0') && (loop < len);) {
261 /* If reach a double quotes, switch the quotes matching status */
262 if (*tmpStr == '\"') {
263 SWITCH_QUOTES_STATUS(quotes);
264 /* Ignore the double quote CHARactor itself */
265 tmpStr++;
266 continue;
267 }
268 /* If detected a space which the quotes matching status is false */
269 /* which said has detected the first space for seperator, finish this scan operation */
270 if ((*tmpStr == ' ') && (QUOTES_STATUS_CLOSE(quotes))) {
271 break;
272 }
273 msgName[loop] = *tmpStr++;
274 loop++;
275 }
276 msgName[loop] = '\0';
277 /* Scan the command list to check whether the command can be found */
278 ret = ShellMsgTypeGet(cmdParsed, msgName);
279 PRINTK("\n");
280 if (ret != LOS_OK) {
281 PRINTK("%s:command not found", msgName);
282 } else {
283 (VOID)OsCmdExec(cmdParsed, (CHAR *)output);//真正的执行命令 output为输出设备
284 }
285 (VOID)LOS_MemFree(m_aucSysMem0, msgName);
286 return ret;
287}
unsigned int OsCmdExec(CmdParsed *cmdParsed, char *cmdStr)
执行命令,shell是运行程序的程序.
Definition: shcmd.c:586
VOID * LOS_MemAlloc(VOID *pool, UINT32 size)
从指定内存池中申请size长度的内存,注意这可不是从内核堆空间中申请内存
Definition: los_memory.c:1123
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
释放从指定动态内存中申请的内存
Definition: los_memory.c:1369
UINT8 * m_aucSysMem0
异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。
Definition: los_memory.c:107
size_t BOOL
Definition: los_typedef.h:88
LITE_OS_SEC_TEXT_MINOR UINT32 ShellMsgTypeGet(CmdParsed *cmdParsed, const CHAR *cmdType)
获取shell消息类型
Definition: shmsg.c:222
函数调用图:
这是这个函数的调用关系图:

◆ ShellMsgParse()

LITE_OS_SEC_TEXT_MINOR UINT32 ShellMsgParse ( const VOID *  msg)

命令内容解析

在文件 shmsg.c289 行定义.

290{
291 CHAR *output = NULL;
292 UINT32 len, cmdLen, newLen;
293 CmdParsed cmdParsed;
294 UINT32 ret = OS_INVALID;
295 CHAR *buf = (CHAR *)msg;
296 CHAR *newMsg = NULL;
297 CHAR *cmd = "exec";
298
299 if (msg == NULL) {
300 goto END;
301 }
302
303 len = strlen(msg);
304 /* 2: strlen("./") */
305 if ((len > 2) && (buf[0] == '.') && (buf[1] == '/')) {
306 cmdLen = strlen(cmd);
307 newLen = len + 1 + cmdLen + 1;
308 newMsg = (CHAR *)LOS_MemAlloc(m_aucSysMem0, newLen);
309 if (newMsg == NULL) {
310 PRINTK("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
311 goto END;
312 }
313 (VOID)memcpy_s(newMsg, newLen, cmd, cmdLen);
314 newMsg[cmdLen] = ' ';
315 (VOID)memcpy_s(newMsg + cmdLen + 1, newLen - cmdLen - 1, (CHAR *)msg + 1, len);
316 msg = newMsg;
317 len = newLen - 1;
318 }
319 output = (CHAR *)LOS_MemAlloc(m_aucSysMem0, len + 1);
320 if (output == NULL) {
321 PRINTK("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
322 goto END;
323 }//对字符串缓冲区,调用函数“OsCmdKeyShift”来挤压和清除无用或过多的空间
324 /* Call function 'OsCmdKeyShift' to squeeze and clear useless or overmuch space if string buffer */
325 ret = OsCmdKeyShift((CHAR *)msg, output, len + 1);
326 if ((ret != LOS_OK) || (strlen(output) == 0)) {
327 ret = OS_INVALID;
328 goto END_FREE_OUTPUT;
329 }
330
331 (VOID)memset_s(&cmdParsed, sizeof(CmdParsed), 0, sizeof(CmdParsed));
332
333 ret = ShellMsgNameGetAndExec(&cmdParsed, output, len);////获取命令名称和参数,并执行
334
335END_FREE_OUTPUT:
336 (VOID)LOS_MemFree(m_aucSysMem0, output);
337END:
338 if (newMsg != NULL) {
339 (VOID)LOS_MemFree(m_aucSysMem0, newMsg);
340 }
341 return ret;
342}
unsigned int OsCmdKeyShift(const char *cmdKey, char *cmdOut, unsigned int size)
Definition: shcmd.c:386
STATIC UINT32 ShellMsgNameGetAndExec(CmdParsed *cmdParsed, const CHAR *output, UINT32 len)
获取命令名称和参数,并执行
Definition: shmsg.c:247
函数调用图:
这是这个函数的调用关系图:

◆ ShellMsgTypeGet()

LITE_OS_SEC_TEXT_MINOR UINT32 ShellMsgTypeGet ( CmdParsed cmdParsed,
const CHAR cmdType 
)

获取shell消息类型

在文件 shmsg.c222 行定义.

223{
224 CmdItemNode *curCmdItem = (CmdItemNode *)NULL;
225 UINT32 len;
226 UINT32 minLen;
227 CmdModInfo *cmdInfo = OsCmdInfoGet();//获取全局变量
228
229 if ((cmdParsed == NULL) || (cmdType == NULL)) {
230 return OS_INVALID;
231 }
232
233 len = strlen(cmdType);
234 LOS_DL_LIST_FOR_EACH_ENTRY(curCmdItem, &(cmdInfo->cmdList.list), CmdItemNode, list) {
235 if ((len == strlen(curCmdItem->cmd->cmdKey)) &&
236 (strncmp((CHAR *)(curCmdItem->cmd->cmdKey), cmdType, len) == 0)) {
237 minLen = (len < CMD_KEY_LEN) ? len : CMD_KEY_LEN;
238 (VOID)memcpy_s((CHAR *)(cmdParsed->cmdKeyword), CMD_KEY_LEN, cmdType, minLen);
239 cmdParsed->cmdType = curCmdItem->cmd->cmdType;
240 return LOS_OK;
241 }
242 }
243
244 return OS_INVALID;
245}
CmdModInfo * OsCmdInfoGet(VOID)
获取全局变量
Definition: shcmd.c:52
const CHAR * cmdKey
命令关键字,例如:ls 函数在Shell中访问的名称。
Definition: shcmd.h:57
CmdType cmdType
Definition: shcmd.h:54
CmdItem * cmd
命令项
Definition: shcmd.h:64
LOS_DL_LIST list
双向链表
Definition: shcmd.h:63
CmdItemNode cmdList
命令项节点
Definition: shcmd.h:69
char cmdKeyword[CMD_KEY_LEN]
Definition: shcmdparse.h:50
CmdType cmdType
Definition: shcmdparse.h:49
函数调用图:
这是这个函数的调用关系图:

◆ ShellNotify()

STATIC VOID ShellNotify ( ShellCB shellCB)

发送解析事件

在文件 shmsg.c104 行定义.

105{
106 (VOID)LOS_EventWrite(&shellCB->shellEvent, SHELL_CMD_PARSE_EVENT);
107}
LITE_OS_SEC_TEXT UINT32 LOS_EventWrite(PEVENT_CB_S eventCB, UINT32 events)
写指定的事件类型
Definition: los_event.c:318
EVENT_CB_S shellEvent
事件类型触发
Definition: shell.h:141
函数调用图:

◆ ShellSaveHistoryCmd()

STATIC VOID ShellSaveHistoryCmd ( const CHAR string,
ShellCB shellCB 
)

保存命令历史记录,这个函数写的不太好

在文件 shmsg.c68 行定义.

69{
70 CmdKeyLink *cmdHistory = shellCB->cmdHistoryKeyLink;//获取历史记录的源头
71 CmdKeyLink *cmdkey = LOS_DL_LIST_ENTRY(string, CmdKeyLink, cmdString);// @note_good ,获取CmdKeyLink,这里挺秒的,通过局部字符串找到整体
72 CmdKeyLink *cmdNxt = NULL;
73
74 if ((string == NULL) || (strlen(string) == 0)) {
75 return;
76 }
77
78 (VOID)pthread_mutex_lock(&shellCB->historyMutex);//对链表的操作都要拿互斥锁
79 if (cmdHistory->count != 0) { //有历史记录的情况
80 cmdNxt = LOS_DL_LIST_ENTRY(cmdHistory->list.pstPrev, CmdKeyLink, list);//获取最老的历史记录
81 if (strcmp(string, cmdNxt->cmdString) == 0) {//比较是否一样,这个地方感觉很怪,只比较一个吗?
82 (VOID)LOS_MemFree(m_aucSysMem0, (VOID *)cmdkey);
83 (VOID)pthread_mutex_unlock(&shellCB->historyMutex);
84 return;
85 }
86 }
87
88 if (cmdHistory->count == CMD_HISTORY_LEN) {//历史记录已满,一删一添导致历史记录永远是满的,所以一旦跑进来了
89 cmdNxt = LOS_DL_LIST_ENTRY(cmdHistory->list.pstNext, CmdKeyLink, list);//后续 ShellSaveHistoryCmd都会跑进来执行
90 LOS_ListDelete(&(cmdNxt->list));//先删除一个最早插入的
91 LOS_ListTailInsert(&(cmdHistory->list), &(cmdkey->list));//再从尾部挂入新的节点,变成最新的记录
92 (VOID)LOS_MemFree(m_aucSysMem0, (VOID *)cmdNxt);//释放已经删除的节点, @note_thinking , 建议和上一句换个位置,保证逻辑上的完整性
93 (VOID)pthread_mutex_unlock(&shellCB->historyMutex);//释放互斥锁
94 return;
95 }
96 //未满的情况下执行到此处
97 LOS_ListTailInsert(&(cmdHistory->list), &(cmdkey->list));//从尾部插入
98 cmdHistory->count++;//历史记录增加
99
100 (VOID)pthread_mutex_unlock(&shellCB->historyMutex);//释放互斥锁
101 return;
102}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListTailInsert(LOS_DL_LIST *list, LOS_DL_LIST *node)
Insert a node to the tail of a doubly linked list.
Definition: los_list.h:244
struct SH_List * pstPrev
Definition: shell_list.h:51
pthread_mutex_t historyMutex
操作cmdHistoryKeyLink的互斥量
Definition: shell.h:82
函数调用图:
这是这个函数的调用关系图:

◆ ShellTask()

LITE_OS_SEC_TEXT_MINOR UINT32 ShellTask ( UINTPTR  param1,
UINTPTR  param2,
UINTPTR  param3,
UINTPTR  param4 
)

shell 任务,处理解析,执行命令

在文件 shmsg.c392 行定义.

396{
397 UINT32 ret;
398 ShellCB *shellCB = (ShellCB *)param1;
399 (VOID)param2;
400 (VOID)param3;
401 (VOID)param4;
402
403 while (1) {
404 PRINTK("\nOHOS # ");//在没有事件的时候,会一直停留在此, 读取shell 输入事件 例如: cat weharmony.net 命令
405 ret = LOS_EventRead(&shellCB->shellEvent,
406 0xFFF, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
407 if (ret == SHELL_CMD_PARSE_EVENT) {//获得解析命令事件
408 ShellCmdProcess(shellCB);//处理命令
409 } else if (ret == CONSOLE_SHELL_KEY_EVENT) {//退出shell事件
410 break;
411 }
412 }
415 (VOID)LOS_EventDestroy(&shellCB->shellEvent);//注销事件
416 (VOID)LOS_MemFree((VOID *)m_aucSysMem0, shellCB);//释放shell控制块
417 return 0;
418}
void OsShellKeyDeInit(CmdKeyLink *cmdKeyLink)
shell的析构函数
Definition: shcmd.c:497
LITE_OS_SEC_TEXT_INIT UINT32 LOS_EventDestroy(PEVENT_CB_S eventCB)
销毁指定的事件控制块
Definition: los_event.c:334
LITE_OS_SEC_TEXT UINT32 LOS_EventRead(PEVENT_CB_S eventCB, UINT32 eventMask, UINT32 mode, UINT32 timeout)
读取指定事件类型,超时时间为相对时间:单位为Tick
Definition: los_event.c:313
STATIC VOID ShellCmdProcess(ShellCB *shellCB)
Definition: shmsg.c:378
函数调用图:

◆ ShellTaskInit()

LITE_OS_SEC_TEXT_MINOR UINT32 ShellTaskInit ( ShellCB shellCB)

给控制台注册一个shell客户端任务

在文件 shmsg.c425 行定义.

426{
427 CHAR *name = NULL;
428 TSK_INIT_PARAM_S initParam = {0};
429 //输入Shell命令的两种方式
430 if (shellCB->consoleID == CONSOLE_SERIAL) { //通过串口工具
431 name = SERIAL_SHELL_TASK_NAME;
432 } else if (shellCB->consoleID == CONSOLE_TELNET) {//通过远程工具
433 name = TELNET_SHELL_TASK_NAME;
434 } else {
435 return LOS_NOK;
436 }
437
438 initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)ShellTask;//任务入口函数,主要是解析shell命令
439 initParam.usTaskPrio = 9; /* 9:shell task priority */
440 initParam.auwArgs[0] = (UINTPTR)shellCB;
441 initParam.uwStackSize = 0x3000;
442 initParam.pcName = name;
443 initParam.uwResved = LOS_TASK_STATUS_DETACHED;
444
445 (VOID)LOS_EventInit(&shellCB->shellEvent);//初始化事件,以事件方式通知任务解析命令
446
447 return LOS_TaskCreate(&shellCB->shellTaskHandle, &initParam);//创建任务
448}
void * ShellTask(void *argv)
Definition: shmsg.c:601
LITE_OS_SEC_TEXT_INIT UINT32 LOS_EventInit(PEVENT_CB_S eventCB)
初始化一个事件控制块
Definition: los_event.c:95
pthread_t shellTaskHandle
shell服务端任务
Definition: shell.h:73
函数调用图:
这是这个函数的调用关系图: