Zigbee 点对点通信



Coordinator:

#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
//#include "Common.h"

#include "GenericApp.h"
#include "DebugTrace.h"

#if !defined( WIN32 )
  #include "OnBoard.h"
#endif

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"

/* RTOS */
#if defined( IAR_ARMCM3_LM )
#include "RTOS_App.h"
#endif 
#ifndef COMMON_H
#define COMMON_H
#include "zComdef.h"
#define GENERICAPP_ENDPOINT 10

#define GENERICAPP_PROFID 0x0F04
#define GENERICAPP_DEVICEID 0x0001
#define GENERICAPP_DEVICE_VERSION 0
#define GENERICAPP_FLAGS 0
#define GENERICAPP_MAX_CLUSTERS 1
#define GENERICAPP_CLUSTERID 1

#endif



// This list should be filled with Application specific Cluster IDs.
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
  GENERICAPP_CLUSTERID
};

const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
  GENERICAPP_ENDPOINT,              //  int Endpoint;10
  GENERICAPP_PROFID,                //  uint16 AppProfId[2];0x0F04
  GENERICAPP_DEVICEID,              //  uint16 AppDeviceId[2];0x0001
  GENERICAPP_DEVICE_VERSION,        //  int   AppDevVer:4;0
  GENERICAPP_FLAGS,                 //  int   AppFlags:4;0
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;1
  (cId_t *)GenericApp_ClusterList,  //  byte *pAppInClusterList;
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)GenericApp_ClusterList   //  byte *pAppInClusterList;
};


endPointDesc_t GenericApp_epDesc;


static void GenericApp_MessageMSGCB(afIncomingMSGPacket_t *pkt);//消息处理函数
//static void GenericApp_SendTheMessage(void);//数据发送函数
//extern void GenericApp_Init(byte task_id);
void GenericApp_Init(byte task_id)
{
  GenericApp_TaskID = task_id;
  GenericApp_TransID = 0;//将发送数据包的序列号初始化为0,在Zigbee协议栈中,每发送
                            //一个数据包,该发送序列号自动加1(协议栈里面的函数自动完成)
  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);//将节点描述符进行注册,只有注册以后才可以使用OSAL提供的服务

}

UINT16 GenericApp_ProcessEvent(byte task_id,UINT16 events)
{
  afIncomingMSGPacket_t *MSGpkt;
  if(events&SYS_EVENT_MSG)
  {
    MSGpkt = (afIncomingMSGPacket_t*)osal_msg_receive(GenericApp_TaskID);
   // MSGpkt = (afIncomingMSGPacket_t*)osal_msg_receive(GenericApp_TaskID);//从消息队列上接收消息,该消息中包含接收到的无线数据包
    while(MSGpkt)
    {
      switch(MSGpkt->hdr.event)
      {
        case AF_INCOMING_MSG_CMD:
          GenericApp_MessageMSGCB(MSGpkt);//消息处理函数(其形式可以改变,其他的这段函数代码的的形式基本上就是固定的)
          break;
        default:
          break;
       
      }
     
      osal_msg_deallocate((uint8 *)MSGpkt);//释放消息所占的存储空间
      MSGpkt = (afIncomingMSGPacket_t*)osal_msg_receive(GenericApp_TaskID);
    //处理完一个消息后,再从消息队列中接收消息,然后对其进行相应的处理,知道所有消息都处理完毕
    }
   
    return(events^SYS_EVENT_MSG);
  }
 
  return 0;
}

void GenericApp_MessageMSGCB(afIncomingMSGPacket_t *pkt)
{
  unsigned char buffer[3];
  osal_memcpy(buffer,pkt->cmd.Data,3);//将收到的数据拷贝到缓冲区,3是拷贝数据的长度
  switch(pkt->clusterId)
  {
    case GENERICAPP_CLUSTERID:
      osal_memcpy(buffer,pkt->cmd.Data,3);
      if((buffer[0]=='L')||(buffer[1]=='E')||(buffer[2]=='D'))
      {
        HalLedBlink(HAL_LED_2,0,50,500);//该函数是协议栈函数,功能是使某个LED闪烁
      }
      else
      {
        HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);
      }
     
        break;
  }

}




enddevice:


#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"

#include "GenericApp.h"
#include "DebugTrace.h"

#if !defined( WIN32 )
  #include "OnBoard.h"
#endif

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"

/* RTOS */
#if defined( IAR_ARMCM3_LM )
#include "RTOS_App.h"
#endif 

#define SEND_DATA_EVENT 0x01

// This list should be filled with Application specific Cluster IDs.
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
  GENERICAPP_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_CLUSTERS,          //  byte  AppNumInClusters;
 // (cId_t *)GenericApp_ClusterList,  //  byte *pAppInClusterList;
 // GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  //(cId_t *)GenericApp_ClusterList   //  byte *pAppInClusterList;
  0,
  (cId_t*)NULL,
  GENERICAPP_MAX_CLUSTERS,
  (cId_t*)GenericApp_ClusterList
};


endPointDesc_t GenericApp_epDesc;//节点描述符


//优先级任务ID
byte GenericApp_TaskID;  // Task ID for internal task/event processing
                          // This variable will be received when
                          // GenericApp_Init() is called.
//保存节点状态的变量
devStates_t GenericApp_NwkState;

//发送序列号
byte GenericApp_TransID;  // This is the unique message ID (counter)

//afAddrType_t GenericApp_DstAddr;


 void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
 void GenericApp_SendTheMessage(void);

#if defined( IAR_ARMCM3_LM )
static void GenericApp_ProcessRtosMessage( void );
#endif


void GenericApp_Init( uint8 task_id )//任务初始化函数,格式较为固定
{
  halUARTCfg_t uartConfig;
 
  GenericApp_TaskID = task_id;//初始化任务优先级(任务优先级有协议栈的操作系统OSAL分配)
  GenericApp_NwkState = DEV_INIT;//设备状态初始化,表示该节点没有连接到Zigbee网络
  GenericApp_TransID = 0;//将发送数据包的序列号初始化为0,在Zigbee协议栈中,每发送
                            //一个数据包,该发送序列号自动加1(协议栈里面的函数自动完成)

  //GenericApp_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
  //GenericApp_DstAddr.endPoint = 0;
  //GenericApp_DstAddr.addr.shortAddr = 0;

  // Fill out the endpoint description.
  GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;//下面几行为节点描述符进初始化
  GenericApp_epDesc.task_id = &GenericApp_TaskID;
  GenericApp_epDesc.simpleDesc
            = (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
  GenericApp_epDesc.latencyReq = noLatencyReqs;

  // Register the endpoint description with the AF
  afRegister( &GenericApp_epDesc );//注册,只有注册以后OSAL才能识别

}


uint16 GenericApp_ProcessEvent( uint8 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 ZDO_STATE_CHANGE:
          GenericApp_NwkState = (devStates_t)(MSGpkt->hdr.status);//读取设备类型
          if (GenericApp_NwkState == DEV_END_DEVICE) //对设备类型进行判断,如果是终端节点,就执行下面语句发送信息
          {
            GenericApp_SendTheMessage();
          }
          break;

        default:
          break;
      }

      // Release the memory
      osal_msg_deallocate( (uint8 *)MSGpkt );

      // Next
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
    }

    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }
  return 0;
 
}

void GenericApp_SendTheMessage(void)
{
  unsigned char theMessageData[3]={'L','E','D'};
 
  afAddrType_t my_DstAddr;//定义一个目的地址结构体
 
  my_DstAddr.addrMode=(afAddrMode_t)Addr16Bit;//目的地址填写模式
  my_DstAddr.endPoint=GENERICAPP_ENDPOINT; //配置目的器件的端口号#define GENERICAPP_ENDPOINT           10
  my_DstAddr.addr.shortAddr=0x0000;//配置目标地址
  //发送数据
  AF_DataRequest(&my_DstAddr,&GenericApp_epDesc,GENERICAPP_CLUSTERID,3,theMessageData,&GenericApp_TransID,AF_DISCV_ROUTE,AF_DEFAULT_RADIUS);


}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值