概述
头文件的分析:
#ifndef SAMPLEAPP_H
#define SAMPLEAPP_H#ifdef __cplusplus
extern "C" //当用C++写的时候要用这个,C语言不用管。
{
#endif
#include "ZComDef.h"
#define SAMPLEAPP_ENDPOINT 20 //定义终端号
#define SAMPLEAPP_PROFID 0x0F08 //定义配置ID
#define SAMPLEAPP_DEVICEID 0x0001 //定义设备ID
#define SAMPLEAPP_DEVICE_VERSION 0 //定义设备版本
#define SAMPLEAPP_FLAGS 0 //定义软件标志??不知道用处我看的文档这个位是保留的
#define SAMPLEAPP_MAX_CLUSTERS 2 //定义簇最大数目 2个
#define SAMPLEAPP_PERIODIC_CLUSTERID 1 //定义簇1
#define SAMPLEAPP_FLASH_CLUSTERID 2 //定义簇2
#define SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT 5000 // Every 5 seconds //定义周期发送时间
#define SAMPLEAPP_SEND_PERIODIC_MSG_EVT 0x0001 //周期发送事件号,自定义事件
#define SAMPLEAPP_FLASH_GROUP 0x0001 //定义组播的发送组地址
#define SAMPLEAPP_FLASH_DURATION 1000 //定义LED的flash时间
extern void SampleApp_Init( uint8 task_id );
extern UINT16 SampleApp_ProcessEvent( uint8 task_id, uint16 events );
#ifdef __cplusplus
}
#endif
#endif /* SAMPLEAPP_H */
c文件的分析:
#include "OSAL.h"
#include "ZGlobals.h"
#include "AF.h"
#include "aps_groups.h"
#include "ZDApp.h"
#include "SampleApp.h"
#include "SampleAppHw.h"
#include "OnBoard.h"
/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "MT_UART.h"
#include "MT_APP.h"
#include "MT.h"
//定义了簇列表 加入头文件中的簇
const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] =
{
SAMPLEAPP_PERIODIC_CLUSTERID,
SAMPLEAPP_FLASH_CLUSTERID
};
//定义简单描述符,包含每个短点的具体信息
const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
{
SAMPLEAPP_ENDPOINT, // 端点号 int Endpoint;
SAMPLEAPP_PROFID, // 配置ID uint16 AppProfId[2];
SAMPLEAPP_DEVICEID, // 设备ID uint16 AppDeviceId[2];
SAMPLEAPP_DEVICE_VERSION, // 设备版本 int AppDevVer:4;
SAMPLEAPP_FLAGS, // 保留 int AppFlags:4;
SAMPLEAPP_MAX_CLUSTERS, // 应用输入簇最大个数 uint8 AppNumInClusters;
(cId_t *)SampleApp_ClusterList, // 应用输入簇列表 uint8 *pAppInClusterList;
SAMPLEAPP_MAX_CLUSTERS, // 应用输出簇最大个数 uint8 AppNumInClusters;
(cId_t *)SampleApp_ClusterList // 应用输出簇列表 uint8 *pAppInClusterList;
};
endPointDesc_t SampleApp_epDesc; //端点描述符,后面会赋值
uint8 SampleApp_TaskID; //分配到的任务号
devStates_t SampleApp_NwkState; //zigbee状态
uint8 SampleApp_TransID; //如果数据分为多帧,则这个就是帧序列号
afAddrType_t SampleApp_Periodic_DstAddr; //周期发送的目标地址
afAddrType_t SampleApp_Flash_DstAddr; //组播发送的地址,控制LED的闪动时间
aps_Group_t SampleApp_Group; //定义归属的组地址,需要需要组地址号和名字
uint8 SampleAppPeriodicCounter = 0;
uint8 SampleAppFlashCounter = 0;
void SampleApp_HandleKeys( uint8 shift, uint8 keys );
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
void SampleApp_SendPeriodicMessage( void );
void SampleApp_SendFlashMessage( uint16 flashTime );
void SampleApp_Init( uint8 task_id ) //初始化此任务
{
SampleApp_TaskID = task_id; //存储任务号
SampleApp_NwkState = DEV_INIT; //状态改为初始化状态
SampleApp_TransID = 0; //帧序列号初始化为0
/***********串口初始化************/
MT_UartInit();//初始化 //调用串口初始化
MT_UartRegisterTaskID(task_id); //登记任务号
HalUARTWrite(0,"Hello Worldn",12); //输出测试
#if defined ( BUILD_ALL_DEVICES )
//定义zigbee是做为路由还是协调器
if ( readCoordinatorJumper() )
zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
else
zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
#endif // BUILD_ALL_DEVICES
#if defined ( HOLD_AUTO_START )
// HOLD_AUTO_START is a compile option that will surpress ZDApp
// from starting the device and wait for the application to
// start the device.
//手动初始化zigbee还是自动初始化zigbee
ZDOInitDevice(0);
#endif
SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast; //广播模式
SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //终端接受
SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF; //地址
//panId 是给协调器用的
SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; //组播地址
SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //终端接受
SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; //组序列号
SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; //终端描述赋值
SampleApp_epDesc.task_id = &SampleApp_TaskID; //任务号
SampleApp_epDesc.simpleDesc
= (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc; //端口描述符
SampleApp_epDesc.latencyReq = noLatencyReqs; //都是用这个,意思不懂
afRegister( &SampleApp_epDesc ); //注册描述
RegisterForKeys( SampleApp_TaskID ); //注册键盘
SampleApp_Group.ID = 0x0001; //赋值组播地址
osal_memcpy( SampleApp_Group.name, "Group 1", 7 );
aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); //对端点号加入组播
#if defined ( LCD_SUPPORTED )
HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 );
#endif
}
//处理任务函数
uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
{
afIncomingMSGPacket_t *MSGpkt; //定义一个任务结构体指针,用来接收消息
(void)task_id; // Intentionally unreferenced parameter //故意定义一下未引用的任务ID
if ( events & SYS_EVENT_MSG ) //如果是系统事件
{
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); //获取消息
while ( MSGpkt ) //消息不为空,一直要处理消息
{
switch ( MSGpkt->hdr.event ) //查看是否为哪一个硬件消息
{
// Received when a key is pressed
case KEY_CHANGE: //按钮改变
SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
break;
// Received when a messages is received (OTA) for this endpoint
case AF_INCOMING_MSG_CMD: //收到一个新的信息
SampleApp_MessageMSGCB( MSGpkt ); //转入处理
break;
// Received whenever the device changes state in the network
case ZDO_STATE_CHANGE: //如果设备状态改变了
SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
if ( (SampleApp_NwkState == DEV_ZB_COORD)
|| (SampleApp_NwkState == DEV_ROUTER)
|| (SampleApp_NwkState == DEV_END_DEVICE) )
{
// Start sending the periodic message in a regular interval. //重新启动周期发送的事件
osal_start_timerEx( SampleApp_TaskID,
SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
}
else
{
// Device is no longer in the network
}
break;
default:
break;
}
osal_msg_deallocate( (uint8 *)MSGpkt ); //释放消息
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); //再次获取查看是否有消息
}
return (events ^ SYS_EVENT_MSG); //返回未处理的事件
}
// Send a message out - This event is generated by a timer
// (setup in SampleApp_Init()).
if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT ) //如果是自定义的发送周期信息事件
{
// Send the periodic message
SampleApp_SendPeriodicMessage(); //发送一个周期信息
// Setup to send message again in normal period (+ a little jitter)
//再次启动自定义的周期发送任务
osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
(SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );
return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT); //返回未处理的事件
}
return 0;
}
void SampleApp_HandleKeys( uint8 shift, uint8 keys )
{
(void)shift; // Intentionally unreferenced parameter
if ( keys & HAL_KEY_SW_1 )
{
/* This key sends the Flash Command is sent to Group 1.
* This device will not receive the Flash Command from this
* device (even if it belongs to group 1).
*/
SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION );
}
if ( keys & HAL_KEY_SW_2 )
{
aps_Group_t *grp;
grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
if ( grp )
{
// Remove from the group
aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
}
else
{
// Add to the flash group
aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );
}
}
}
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) //处理收到的消息
{
uint16 flashTime;
switch ( pkt->clusterId ) //根据簇序列号来处理
{
case SAMPLEAPP_PERIODIC_CLUSTERID:
break;
case SAMPLEAPP_FLASH_CLUSTERID:
flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] );
HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) );
break;
}
}
void SampleApp_SendPeriodicMessage( void )
{
//返回成功的状态
//第一个参数 目标地址
//第二个参数 本机端口
//第三个参数 簇ID号
//第四个参数 发送长度
//第五个参数 发送数据的地址
//第六个参数 多帧的序列号
//第七个参数 发送选项,与路由相关,这里是经路由转发
//第八个参数 发送半径,也就是可接力的次数吧??默认10次
if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID,
1,
(uint8*)&SampleAppPeriodicCounter,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
{
}
else
{
// Error occurred in request to send.
}
}
//发送led刷新时间的信息
void SampleApp_SendFlashMessage( uint16 flashTime )
{
uint8 buffer[3];
buffer[0] = (uint8)(SampleAppFlashCounter++);
buffer[1] = LO_UINT16( flashTime );
buffer[2] = HI_UINT16( flashTime );
//同上
if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_FLASH_CLUSTERID,
3,
buffer,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
{
}
else
{
// Error occurred in request to send.
}
}
最后
以上就是仁爱蛋挞为你收集整理的Z-Stack 样例程序分析1的全部内容,希望文章能够帮你解决Z-Stack 样例程序分析1所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复