本次要完成的是协调器组网成功后每隔5秒广播一个“0123456789”,并且led1灯快速闪烁5次,终端收到"0123456789"后显示到串口上,并且给协调器发送一个OK,协调器收到OK后led2闪烁5次
Coordinate.c:
//头文件
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include "Coordinator.h"
#include "DebugTrace.h"
#if !defined( WIN32 )
#include "OnBoard.h"
#endif
#include "hal_led.h"
//输入簇
const cId_t GenericApp_ClusterInList[GENERICAPP_MAX_IN_CLUSTERS] =
{
GENERICAPP_IN_CLUSTERID
};
//输出簇
const cId_t GenericApp_ClusterOutList[GENERICAPP_MAX_OUT_CLUSTERS] =
{
GENERICAPP_OUT_CLUSTERID
};
//简单描述符
const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
GENERICAPP_ENDPOINT, // int Endpoint;
GENERICAPP_PROFID, // uint16 AppProfId[2];
GENERICAPP_DEVICEID, // uint16 AppDeviceId[2];
GENERICAPP_DEVICE_VERSION, // int AppDevVer:4;
GENERICAPP_FLAGS, // int AppFlags:4;
GENERICAPP_MAX_IN_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterInList, // byte *pAppInClusterList;
GENERICAPP_MAX_OUT_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterOutList // byte *pAppInClusterList;
};
//端点描述符
endPointDesc_t GenericApp_epDesc;
//任务ID号
byte GenericApp_TaskID;
//设备状态
devStates_t GenericApp_NwkState;
//发送序号
uint8 GenericApp_TransID;
//本地函数声明
static void GenericApp_SendTheMessage(void);//发送
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt );//接收
//任务初始化函数
void GenericApp_Init( byte task_id )
{
//身份证号码
GenericApp_TaskID = task_id;
GenericApp_TransID=0;
//初始化设备状态
GenericApp_NwkState=DEV_INIT;
//填写端点描述。
GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
GenericApp_epDesc.task_id = &GenericApp_TaskID;
GenericApp_epDesc.simpleDesc
= (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
GenericApp_epDesc.latencyReq = noLatencyReqs;
// 注册
afRegister( &GenericApp_epDesc );
}
//应用层事件处理函数 当应用层任务有时间发生自动调用该函数
/*
task_id:发生事件的任务id号
events:事件
*/
UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events )
{
//消息包指针
afIncomingMSGPacket_t *MSGpkt;
//是否是系统事件
if ( events & SYS_EVENT_MSG )
{
//从消息队列里读取(接收)当前任务的消息
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
while ( MSGpkt )
{
switch ( MSGpkt->hdr.event )
{
//接收到数据
case AF_INCOMING_MSG_CMD:
GenericApp_MessageMSGCB( MSGpkt );
break;
case ZDO_STATE_CHANGE:
GenericApp_NwkState=(devStates_t)(MSGpkt->hdr.status);
if(GenericApp_NwkState==DEV_ZB_COORD){
//组网成功启动用户事件定时发送数据
GenericApp_SendTheMessage();
osal_start_timerEx(GenericApp_TaskID,SEND_DATA_EBENT_5s,5000);
}
default:
break;
}
// 释放空间
osal_msg_deallocate( (uint8 *)MSGpkt );
// 再次循环读取当前任务消息
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
//用户事件
if(events&SEND_DATA_EBENT_5s){
GenericApp_SendTheMessage();
osal_start_timerEx(GenericApp_TaskID,SEND_DATA_EBENT_5s,5000);
return (events&SEND_DATA_EBENT_5s);
}
// Discard unknown events
return 0;
}
//事件处理函数
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
uint8 buffer[10]={0};
switch ( pkt->clusterId )
{
//终端节点发来的消息
case GENERICAPP_IN_CLUSTERID:
osal_memcpy(buffer,pkt->cmd.Data,2);
if(osal_memcmp(buffer,"OK",2)){
HalLedBlink(HAL_LED_2,5,50,300);
}
break;
default:
break;
}
}
//协调器发送数据
static void GenericApp_SendTheMessage(void){
char theMessage[]="0123456789";
afAddrType_t GenericApp_DstAddr;
GenericApp_DstAddr.addr.shortAddr=0xffff;//短逻辑地址
GenericApp_DstAddr.addrMode=afAddrBroadcast;//广播
GenericApp_DstAddr.endPoint=GENERICAPP_ENDPOINT;//目的地端点号
if(AF_DataRequest(&GenericApp_DstAddr,//目的地址
&GenericApp_epDesc,//源(发送端点号)
GENERICAPP_OUT_CLUSTERID,//命令号簇id
(uint16)osal_strlen(theMessage),//发送数据长度
(unsigned char*)theMessage,//消息内容
&GenericApp_TransID,//只想发送序号指针
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS)==afStatus_SUCCESS){
HalLedBlink(HAL_LED_1,5,50,300);
}else{
HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);
}
}
Coordinate.h:
#ifndef COORDINATOR
#define COORDINATOR
//输入群集个数
#define GENERICAPP_MAX_IN_CLUSTERS 1
//输出群集个数
#define GENERICAPP_MAX_OUT_CLUSTERS 1
//输入命令ID
#define GENERICAPP_IN_CLUSTERID 0001
//输出命令ID
#define GENERICAPP_OUT_CLUSTERID 0002
//端点ID
#define GENERICAPP_ENDPOINT 10
//profileid
#define GENERICAPP_PROFID 0x0F04
//设备id号
#define GENERICAPP_DEVICEID 0x0001
//版本号
#define GENERICAPP_DEVICE_VERSION 0
//保留位
#define GENERICAPP_FLAGS 0
//定时每5秒发送数据用户事件
#define SEND_DATA_EBENT_5s 0X10
//外部声明
void GenericApp_Init( byte task_id );
UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events );
#endif
EndDevce.c:
//头文件
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include "EndDevice.h"
#include "DebugTrace.h"
#if !defined( WIN32 )
#include "OnBoard.h"
#endif
#include "hal_led.h"
//输入簇
const cId_t GenericApp_ClusterInList[GENERICAPP_MAX_IN_CLUSTERS] =
{
GENERICAPP_IN_CLUSTERID
};
//输出簇
const cId_t GenericApp_ClusterOutList[GENERICAPP_MAX_OUT_CLUSTERS] =
{
GENERICAPP_OUT_CLUSTERID
};
//简单描述符
const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
GENERICAPP_ENDPOINT, // int Endpoint;
GENERICAPP_PROFID, // uint16 AppProfId[2];
GENERICAPP_DEVICEID, // uint16 AppDeviceId[2];
GENERICAPP_DEVICE_VERSION, // int AppDevVer:4;
GENERICAPP_FLAGS, // int AppFlags:4;
GENERICAPP_MAX_IN_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterInList, // byte *pAppInClusterList;
GENERICAPP_MAX_OUT_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterOutList // byte *pAppInClusterList;
};
//端点描述符
endPointDesc_t GenericApp_epDesc;
//任务ID号
byte GenericApp_TaskID;
//设备状态
devStates_t GenericApp_NwkState;
//发送数据序号
byte GenericApp_TransID;
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt );//接收
static void GenericApp_SendTheMessage(void);
static void rxCB(uint8 port,uint8 event);
//任务初始化函数
void GenericApp_Init( byte task_id )
{
//保存系统分配的任务id
GenericApp_TaskID = task_id;
//初始化发送序号
GenericApp_TransID=0;
//串口0初始化
halUARTCfg_t uartconfig;
uartconfig.configured=true;
uartconfig.baudRate=HAL_UART_BR_115200;
uartconfig.flowControl=false;
uartconfig.flowControlThreshold=64;
uartconfig.rx.maxBufSize=128;
uartconfig.tx.maxBufSize=128;
uartconfig.idleTimeout=6;
uartconfig.intEnable=true;
uartconfig.callBackFunc=rxCB;//回调函数
HalUARTOpen(0,&uartconfig);
//填写端点描述。
GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
GenericApp_epDesc.task_id = &GenericApp_TaskID;
GenericApp_epDesc.simpleDesc
= (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
GenericApp_epDesc.latencyReq = noLatencyReqs;
// 注册
afRegister( &GenericApp_epDesc );
}
UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events )
{
//消息包指针
afIncomingMSGPacket_t *MSGpkt;
//是否是系统事件
if ( events & SYS_EVENT_MSG )
{
//从消息队列里读取(接收)当前任务的消息
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
while ( MSGpkt )
{
switch ( MSGpkt->hdr.event )
{
//接收到数据
case AF_INCOMING_MSG_CMD:
GenericApp_MessageMSGCB( MSGpkt );
break;
case ZDO_STATE_CHANGE:
GenericApp_NwkState=(devStates_t)MSGpkt->hdr.status;
if((GenericApp_NwkState==DEV_END_DEVICE)||(GenericApp_NwkState==DEV_ROUTER)){
//向协调器发送
GenericApp_SendTheMessage();
}
break;
default:
break;
}
// 释放空间
osal_msg_deallocate( (uint8 *)MSGpkt );
// 再次循环读取当前任务消息
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
//用户事件
// Discard unknown events
return 0;
}
static void GenericApp_SendTheMessage(void){
char theMessage[]="OK";
afAddrType_t GenericApp_DstAddr;
//协调器网络地址是0x0000
GenericApp_DstAddr.addr.shortAddr=0x0000;
GenericApp_DstAddr.addrMode=afAddr16Bit;
GenericApp_DstAddr.endPoint=GENERICAPP_ENDPOINT;
if(AF_DataRequest(&GenericApp_DstAddr,//目的地址
&GenericApp_epDesc,//源(发送端点号)
GENERICAPP_OUT_CLUSTERID,//命令号簇id
(uint16)osal_strlen(theMessage),//发送数据长度
(unsigned char*)theMessage,//消息内容
&GenericApp_TransID,//只想发送序号指针
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS)==afStatus_SUCCESS){
HalLedBlink(HAL_LED_1,5,50,300);
}else{
HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);
}
}
//接收到事件处理函数
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
uint8 buffer[30];
switch ( pkt->clusterId )
{
//终端节点发来的消息
case GENERICAPP_IN_CLUSTERID:
osal_memcpy(buffer,pkt->cmd.Data,11);
if(osal_memcmp(buffer,"0123456789",10)){
HalUARTWrite(0,buffer,10);
HalUARTWrite(0,"\r\n",2);
//发送回复消息
GenericApp_SendTheMessage();
}
break;
default:
break;
}
}
static void rxCB(uint8 port,uint8 event){
}
EndDevice.h:
#ifndef ENDDEVICE_H
#define ENDDEVICE_H
//输入群集个数
#define GENERICAPP_MAX_IN_CLUSTERS 1
//输出群集个数
#define GENERICAPP_MAX_OUT_CLUSTERS 1
//输入命令ID
#define GENERICAPP_IN_CLUSTERID 0002
//输出命令ID
#define GENERICAPP_OUT_CLUSTERID 0001
//端点ID
#define GENERICAPP_ENDPOINT 10
//profileid
#define GENERICAPP_PROFID 0x0F04
//设备id号
#define GENERICAPP_DEVICEID 0x0001
//版本号
#define GENERICAPP_DEVICE_VERSION 0
//保留位
#define GENERICAPP_FLAGS 0
void GenericApp_Init( byte task_id );
UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events );
#endif