Silicon Lab Ember zigbee学习杂谈----------cli 扩展

stack version:5.6.0

Ember zigbee提供了丰富的命令行接口,在调试串口输入help你会看到具体cli内容(如图)


如果自己想定义一些cli命令,要怎么做呢?以下为详细步奏:

1、定义一个全局宏定义EMBER_AF_ENABLE_CUSTOM_COMMANDS,可在Appbuilder的Includes选项卡下的Additional Macros添加一个新的Macro(如图)


2、在程序中添加EmberCommandEntry emberAfCustomCommands[ ],可以在添加在文件<工程名_callback.c>中,如下例:

EmberCommandEntry emberAfCustomCommands[]={
  emberCommandEntryAction("end-device-bind-request",EnddeviceBindRequest, "u", "send a enddevice bind request."),
  emberCommandEntryAction("print-all-bind-table",Printallbindtable, "", "print all bind table exist."),
  emberCommandEntryAction("delete-bind-table",deletebindtable, "u", "delete bind table."),
  emberCommandEntryAction("send-command-by-bind",Sendcommandbybindtable,"uu","send a command by bind table"),
  emberCommandEntryAction("send-simple-descriptor-request",SendSimpleDescriptorCommand,"uv","send a simple descriptor command to a target"),
  emberCommandEntryAction("send-devbind-request",),
  emberCommandEntryTerminator()
};

emberCommandEntryAction( )实际就是构造一个EmberCommandEntry 结构体,原型如下:

#define emberCommandEntryAction(name, action, argumentTypes, description)        { (name), (action), (argumentTypes), (description), NULL }

emberCommandEntryTerminator( )实际是构建造一个空的EmberCommandEntry 结构体,原型如下:

#define emberCommandEntryTerminator()    { NULL, NULL, NULL, NULL, NULL }

具体参数含义请参考EmberCommandEntry结构体定义:

typedef PGM struct {
  /** Use letters, digits, and underscores, '_', for the command name.
   * Command names are case-sensitive.
   */
  PGM_P name;
  /** A reference to a function in the application that implements the 
   *  command.
   *  If this entry refers to a nested command, then action field
   *  has to be set to NULL.
   */
  CommandAction action;
  /** 
   * In case of normal (non-nested) commands, argumentTypes is a 
   * string that specifies the number and types of arguments the
   *  command accepts.  The argument specifiers are:
   *  - u:   one-byte unsigned integer.
   *  - v:   two-byte unsigned integer
   *  - w:   four-byte unsigned integer
   *  - s:   one-byte signed integer
   *  - b:   string.  The argument can be entered in ascii by using 
   *         quotes, for example: "foo".  Or it may be entered
   *         in hex by using curly braces, for example: { 08 A1 f2 }.
   *         There must be an even number of hex digits, and spaces
   *         are ignored.
   *  - *:   zero or more of the previous type.  
   *         If used, this must be the last specifier.
   *  - ?:   Unknown number of arguments. If used this must be the only
   *         character. This means, that command interpreter will not
   *         perform any validation of arguments, and will call the 
   *         action directly, trusting it that it will handle with 
   *         whatever arguments are passed in.
   *  Integer arguments can be either decimal or hexidecimal.
   *  A 0x prefix indicates a hexidecimal integer.  Example: 0x3ed.
   *
   *  In case of a nested command (action is NULL), then this field
   *  contains a pointer to the nested EmberCommandEntry array.
   */
  PGM_P argumentTypes;
  /** A description of the command.
   */
#if defined(EMBER_COMMAND_INTEPRETER_HAS_DESCRIPTION_FIELD)
  PGM_P description;
  /** An array of strings, with a NULL terminator, indicating what
      each argument is. */
  PGM_P const * argumentDescriptions;
#endif
} EmberCommandEntry;

在emberCommandEntryAction("end-device-bind-request",EnddeviceBindRequest, "u", "send a enddevice bind request.")中,end-device-bind-request是cli名、EnddeviceBindRequest是该cli调用的函数,“u”指定了参数个数和类型这里表示一个int8u参数、 "send a enddevice bind request."是对该cli的功能简单描述。

3、定义cli调用函数,如在上例中需要定义EnddeviceBindRequest函数,cli调用函数是返回值为void参数也为void的函数:

void EnddeviceBindRequest(void)
{
  int8u endpoint=(int8u)emberUnsignedCommandArgument(0);
  emberAfSendEndDeviceBind(endpoint);
}

可以通过emberUnsignedCommandArgument()来提取cli参数,emberUnsignedCommandArgument(0)表示第一个参数。

完成上述3个步骤后编译下载,再使用命令行就可以使用刚刚自定义的的cli了,自定义的cli都放在custom目录下,在调试串口输入custom就会看到自己定义的cli,如图:




猜你喜欢

转载自blog.csdn.net/wangchongttg/article/details/50388813