加载中…
个人资料
zhonglovc
zhonglovc
  • 博客等级:
  • 博客积分:0
  • 博客访问:224
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

zigbee 组网发送

(2018-01-11 22:54:56)
标签:

zigbee

组网发送

//终端部分代码
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDProfile.h"
#include "ZDObject.h"
#include
#include "Coordinator.h"
#include "DebugTrace.h"

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

#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"

#include "aps_groups.h"
#define  SEND_DATA_EVENT 0X01

const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] = 
{
  GENERICAPP_CLUSTERID_1,GENERICAPP_CLUSTERID_2
};
const SimpleDescriptionFormat_t GenericApp_SimpleDesc = 
{
  GENERICAPP_ENDPOINT,
  GENERICAPP_PROFID,
  GENERICAPP_DEVICEID,
  GENERICAPP_DEVICE_VERSION,
  GENERICAPP_FLAGS,
  0,
  (cId_t *)NULL,
  GENERICAPP_MAX_CLUSTERS,
  (cId_t *)GenericApp_ClusterList
};

endPointDesc_t GenericApp_epDesc;
byte GenericApp_TaskID;
byte GenericApp_TransID;
devStates_t GenericApp_NwkState;

void Send_Info(void);
void MSG_Handler(afIncomingMSGPacket_t *pkt);
static void rxCB(uint8 port,uint8 event);

void GenericApp_Init(byte task_id)
{
  halUARTCfg_t uartConfig;
  
  GenericApp_TaskID       = task_id;
  GenericApp_NwkState     = DEV_INIT;
  GenericApp_TransID      = 0;
  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);
  
  uartConfig.configured     = TRUE;
  uartConfig.baudRate       = HAL_UART_BR_115200;
  uartConfig.flowControl    = FALSE;
  uartConfig.callBackFunc   = rxCB;
  HalUARTOpen(0,&uartConfig);
  
  HalLedBlink(HAL_LED_2,0,50,250);
}

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 ZDO_STATE_CHANGE:     
          osal_set_event(GenericApp_TaskID,SEND_DATA_EVENT);
          HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);
        break;
        
      case AF_INCOMING_MSG_CMD:
        MSG_Handler(MSGpkt);
        break;
          
      default:
        break;
      }
      osal_msg_deallocate((uint8 *)MSGpkt);
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(GenericApp_TaskID);
    }
    return (events ^ SYS_EVENT_MSG);
  }
  if(events & SEND_DATA_EVENT)
  {
    Send_Info();                     
    ZDP_IEEEAddrReq(0x0000,0,0,0);
    osal_start_timerEx(GenericApp_TaskID,SEND_DATA_EVENT,5000);
    return(events ^ SEND_DATA_EVENT);
  }
  return 0;
}


void Send_Info(void)
{
  uint16 nwk;
  
  nwk = NLME_GetShortAddr();
   
  afAddrType_t my_DstAddr;
  my_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
  my_DstAddr.endPoint = GENERICAPP_ENDPOINT;
  my_DstAddr.addr.shortAddr = 0x0000;//往协调器发
  AF_DataRequest(&my_DstAddr,&GenericApp_epDesc,GENERICAPP_CLUSTERID_1,
                 2,(unsigned char*)&nwk,&GenericApp_TransID,AF_DISCV_ROUTE,AF_DEFAULT_RADIUS);
}

void MSG_Handler(afIncomingMSGPacket_t *pkt)
{
  char buf[30] = 0;
  unsigned char buffer[2] = {0x0A,0x0D};
  switch(pkt->clusterId)
  {
  case GENERICAPP_CLUSTERID_2:
    osal_memcpy(buf,pkt->cmd.Data,pkt->cmd.DataLength);
    HalUARTWrite(0,buf,pkt->cmd.DataLength);
    break;
  }
}

static void rxCB(uint8 port,uint8 event)
{
  unsigned char changeling[2] = {0x0A,0x0D};
  uint8 buf[256];
  uint8 len = 0;
  len = HalUARTRead(0,buf,256);
  if(len)
  {
    afAddrType_t my_DstAddr;
    my_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
    my_DstAddr.endPoint = GENERICAPP_ENDPOINT;
    my_DstAddr.addr.shortAddr = 0X0000;
    AF_DataRequest(&my_DstAddr,&GenericApp_epDesc,GENERICAPP_CLUSTERID_2,
                   len,buf,&GenericApp_TransID,AF_DISCV_ROUTE,AF_DEFAULT_RADIUS);
   
  }
}

void To_string(uint8 *dest,char* src,uint8 length)//二进制书转化为十六进制数 
{    
        uint8* xad;  
        uint8 i = 0;
        uint8 ch;   
        xad=src+length-1;  
      for(i=0;i
           {     ch=(*xad>>4)&0x0F;  //除以十六   
                  dest[i<<1]=ch+((ch<10)?'0':'7');    
                  ch=*xad&0x0F;     
                  dest[(i<<1)+1]=ch+((ch<10)?'0':'7');  
           
}


0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有