ZStack-CC2530 simple API
首先感谢罗老师的教导。下面直接正题:
为什么要使用Simple API呢?
直接调用zstack的API来完成任务的功能,就像直接调用windows的API书写widows的应用程序一样麻烦。
有没有对zstack的API做一个封装而提供一个二次开发的API以使得zstack的任务更简单些?
有!!--------simple API和ZCL!!
利用Simple API开发任务的条件
1、一个用户任务;
2、一个端点
也就是说,多任务、多端点的情况是不是太合适。一般来说,一个用户任务、一个端点已经可以实现一个节点的功能了。
先来看一下Simple API的模板:
#include "ZComDef.h"
#include "OSAL.h"
#include "sapi.h"
#include "MySimpleApp.h"
//为simple API定义简单描述符
const SimpleDescriptionFormat_t zb_SimpleDesc
{
};
//用来处理用户的自定义事件
void zb_HandleOsalEvent( uint16 event )
{
}
//用来处理按键消息
void zb_HandleKeys( uint8 shift, uint8 keys )
{
}
//当构建网络或加入网络成功时被调用
void zb_StartConfirm( uint8 status )
{
}
//当数据包被接收方收到时调用
void zb_SendDataConfirm( uint8 handle, uint8 status )
{
}
void zb_BindConfirm( uint16 commandId, uint8 status )
{
}
//其它节点绑定到该节点时调用
void zb_AllowBindConfirm( uint16 source )
{
(void)source;
}
//用户任务通过zb_FindDeviceRequest通过节点的物理地址获取节点的网络地址时的调用
void zb_FindDeviceConfirm( uint8 searchType, uint8 *searchKey, uint8 *result )
{
}
/******************************************************************************
* @fn zb_ReceiveDataIndication
*
* @brief The zb_ReceiveDataIndication callback function is called
* asynchronously by the ZigBee stack to notify the application
* when data is received from a peer device.
*
* @param source - The short address of the peer device that sent the data
* command - The commandId associated with the data
* len - The number of bytes in the pData parameter
* pData - The data sent by the peer device
*
* @return none
*/
void zb_ReceiveDataIndication( uint16 source, uint16 command, uint16 len, uint8 *pData )
{
// Add your code here and remove the "(void)" lines.
(void)source;
(void)command;
(void)len;
(void)pData;
}
下面详细讲解:
利用Simple API的用户任务的编写
•void zb_StartConfirm( uint8 status ) 的定义
(1)调用时机
当节点成功构建网络或加入网络时被调用。当status为ZSUCCESS为成功。
(2)举例
void zb_StartConfirm( uint8 status )
{//根据status做点你想做事情添加代码 }
void zb_StartConfirm( uint8 status )
{ //什么都不想做,直接留空 }
• zb_HandleOsalEvent( uint16 event )的定义
(1)调用时机
当用户任务有自定义事件处理。
注意:sapi的框架把用户自定义事件限定在0---7号事件
(2)举例
void zb_HandleOsalEvent( uint16 event )
{ if(event & 用户自定义事件标记)
{ }
}
• void zb_SendDataConfirm( uint8 handle, uint8 status )的定义
(1)调用时机
当用户任务成功把数据包发送到目标后调用。
(2)参数含义
handle:数据包编号;status:ZSUCCESS表示发送成功;
•void zb_BindConfirm( uint16 commandId, uint8 status )的定义
(1)调用时机
用户任务的绑定成功后被调用。
(2)参数含义
commandId :绑定的命令号;status:ZSUCCESS表示成功;
• void zb_FindDeviceConfirm( uint8 searchType, uint8 *searchKey, uint8 *result )的定义
(1)调用时机
用户任务通过zb_FindDeviceRequest通过节点的物理地址获取节点的网络地址时的调用 。
• void zb_AllowBindConfirm( uint16 source )的定义
(1)调用时机
当另外一个设备试图绑定到这个设备时被被调用;
(2)参数含义
source:发送绑定请求设备的网络地址
•zb_HandleKeys( uint8 shift, uint8 keys )的定义
(1)调用时机
当按键被按下时;
注:同样需要根据目标板子对按键驱动进行移植;
Simple API提供给用户任务的函数
• void zb_SystemReset ( void ); (1)功能描述:重新启动节点
•void zb_StartRequest(void) (2)功能描述:加入或构建网络
•void zb_SendDataRequest ( uint16 destination, uint16 commandId, uint8 len, uint8 *pData, uint8 handle, uint8 txOptions, uint8 radius )
功能描述:发送数据包;
参数说明:
16-Bit short address of device [0-0xfffD]:单播
ZB_BROADCAST_ADDR: 广播发送ZB_BINDING_ADDR: 绑定发送
•void zb_AllowBind ( uint8 timeout )
功能描述:设备允许绑定
参数说明:
0x00:不允许绑定;
0xff: 一直允许绑定;
0---65:允许绑定的事件,单位:秒
SAPI在两层之间,其中和应用程序支持子层的接口不需要修改,和用户任务的接口需要做修改,原因有两点:
1.SAPI内有大量函数供用户任务调用;
2.SAPI内有些功能,自己不能确定功能,当有任务调用时可进一步干预SimpleAPI的行为,这是类似一种面向对象的编程思想——纯虚函数。
注意: 源文件sapi.c和头文件sapi.h保存在C:\Texas Instruments\ZStack-CC2530-2.3.0-1.4.0\Components\stack\sapi组件中。
希望对你有帮助。