原创 在sylixos中增加自定义的shell命令

2016-4-10 15:48 2398 28 28 分类: MCU/ 嵌入式 文集: SylixOS应用开发

对操作系统来讲,无论是PCWindowsLinux还是嵌入式系统VxWorksQNXSylixOSshell都是一个强大的调试手段,在进行驱动开发、应用开发、系统维护方面有着重要作用。

SylixOS提供了基本的shell命令,能够满足绝大多数情况下的需要,但在一些特殊应用中通过添加自定义的shell命令,可以极大的方便设备的维护和使用。

SylixOS中添加shell命令的函数为API_TshellKeywordAdd,该函数需要在内核模块中调用,函数介绍如下:

API_TShellKeywordAdd(pcKeyword,       /*  添加的 shell 命令     */
           pfuncCommand);       /*  执行 shell 命令的函数  */

下面使用一个简单的实例演示添加shell命令的过程,该命令可以实现三个整数的加法运算。

#define  __SYLIXOS_KERNEL
#include <SylixOS.h>
#include "linux/compat.h"
#include <module.h>
 
#include "../SylixOS/kernel/include/k_kernel.h"
#include "../SylixOS/system/include/s_system.h"
#include "../SylixOS/system/include/s_internal.h"
#include "sys/time.h"
/******************************************************************************
** 函数名称: shellCmdTest
** 功能描述: 添加的 shell 测试命令功能函数
******************************************************************************/
INT  shellCmdTest (UINT32  uiStart, UINT32  uiCount, UINT32  uiData)
{
    UINT32  uiRet;
 
    uiRet = uiStart + uiCount + uiData;
    printk("result:%d\n", uiRet);
 
    return  (ERROR_NONE);
}
/******************************************************************************
** 函数名称: tshellTestCmd
** 功能描述: 向内核添加的测试命令
******************************************************************************/
static INT  tshellTestCmd (INT  iArgC, PCHAR  ppcArgV[])
{
    UINT32  uiStart = 0;
    UINT32  uiCount = 0;
    UINT32  uiData  = 0;
 
    if (iArgC == 1) {
        printk("cmd do nothing!\n");
        return  (ERROR_NONE);
    }
    if (lib_strcmp(ppcArgV[2], "-g") == 0) {
    }
    sscanf(ppcArgV[1], "%d", &uiStart);
    sscanf(ppcArgV[2], "%d", &uiCount);
    sscanf(ppcArgV[3], "%d", &uiData);
 
    shellCmdTest(uiStart, uiCount, uiData);
 
    return  (ERROR_NONE);
}
/******************************************************************************
** 函数名称: module_init
** 功能描述: 内核模块被注册进系统时调用的函数
******************************************************************************/
int module_init (void)
{
    API_TShellKeywordAdd("numadd",                   /*  添加的 shell 命令           */
                             tshellTestCmd);            /*  执行 shell 命令的函数       */
    return  (ERROR_NONE);
}
/******************************************************************************
** 函数名称: module_exit
** 功能描述: 内核模块从系统时调用的函数
******************************************************************************/
void module_exit (void)
{
}

代码使用:

1.在控制台中将工作目录切换到“/lib/modules”,注册模块并测试shell,过程如下;

[root@sylixos_station:/lib/modules]# ls
modu_shell_add.ko               xinput.ko       xsiipc.ko
[root@sylixos_station:/lib/modules]# modulereg modu_shell_add.ko
hello_module init!
module modu_shell_add.ko register ok, handle : 0x30c74910
[root@sylixos_station:/lib/modules]# numadd 1 3 5
result:9
[root@sylixos_station:/lib/modules]#
注意:本例程的工程创建时,需要选择 kernel module工程类型
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
28
关闭 站长推荐上一条 /3 下一条