ZIGBee组网流程

本文详细介绍了ZIGBee组网的过程,从操作系统初始化到设备的启动,包括协调器建立网络和终端设备、路由设备的网络发现与加入。主要涉及ZDO初始化、ZDApp任务、ZDO启动设备、网络初始化、网络状态变化等关键步骤。
摘要由CSDN通过智能技术生成

ZIGBee组网流程

第一个功能:协调器的组网,终端设备和路由设备发现网络以及加入网络
//第一步:Z-Stack  由 main()函数开始执行,main()函数共做了 2 件事:一是系统初始化,另外一件是开始执行轮转查询式操作系统
int main( void )                          

{

  .......

// Initialize the operating system

osal_init_system();  //第二步,操作系统初始化
......

osal_start_system(); //初始化完系统任务事件后,正式开始执行操作系统

  ......


//第二步,进入 osal_init_system()函数,执行操作系统初始化

uint8 osal_init_system( void )      //初始化操作系统,其中最重要的是,初始化操作系统的任务

{

// Initialize the Memory Allocation System

  osal_mem_init();

// Initialize the message queue

  osal_qHead = NULL;

// Initialize the timers

  osalTimerInit();

// Initialize the Power Management System

  osal_pwrmgr_init();

// Initialize the system tasks.

osalInitTasks();  //第三步,执行操作系统任务初始化函数

// Setup efficient search for the first free block of heap.

  osal_mem_kick();

  return ( SUCCESS );

}
//第三步,进入osalInitTasks()函数,执行操作系统任务初始化

void osalInitTasks( void )       //第三步,初始化操作系统任务

{

  uint8 taskID = 0;

  tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);

  osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));

//任务优先级由高向低依次排列,高优先级对应 taskID 的值反而小

  macTaskInit( taskID++ ); //不需要用户考虑

  nwk_init( taskID++ );      //不需要用户考虑

  Hal_Init( taskID++ );      //硬件抽象层初始化,需要我们考虑

#if defined( MT_TASK )      

  MT_TaskInit( taskID++ );

#endif

  APS_Init( taskID++ );       //不需要用户考虑

#if defined ( ZIGBEE_FRAGMENTATION ) 

  APSF_Init( taskID++ );

#endif

ZDApp_Init( taskID++ );   //第四步,ZDApp层,初始化 ,执行ZDApp_init函数后,如果是协调器将建立网络,如果是终端设备将加入网络。

#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT ) 

  ZDNwkMgr_Init( taskID++ );

#endif

  SerialApp_Init( taskID );  //应用层SerialApp层初始化,需要用户考虑     在此处设置了一个按键触发事件,
                                         //当有按键按下的时候,产生一个系统消息

}                           

//第四步,进入ZDApp_init()函数,执行ZDApp层初始化
//The first step

void ZDApp_Init( uint8 task_id )     //The first step,ZDApp层初始化。

{

// Save the task ID

  ZDAppTaskID = task_id;

// Initialize the ZDO global device short address storage

  ZDAppNwkAddr.addrMode = Addr16Bit;

  ZDAppNwkAddr.addr.shortAddr = INVALID_NODE_ADDR;

  (void)NLME_GetExtAddr();  // Load the saveExtAddr pointer.

// Check for manual "Hold Auto Start"

  ZDAppCheckForHoldKey();

// Initialize ZDO items and setup the device - type of device to create.

ZigBee组网流程如下: 1. 初始化协调器,设置协调器的PAN ID和网络信道。 2. 启动协调器,等待设备加入网络。 3. 初始化设备,设置设备的PAN ID和网络信道。 4. 启动设备,加入网络。 5. 协调器接收到设备的加入请求后,将设备加入网络并分配短地址。 6. 设备加入网络后,进行路由表的更新和路由发现。 以下是一个简单的ZigBee组网的示例代码,使用了TI的Z-Stack协议栈: ```c #include "ZComDef.h" #include "AF.h" #include "ZDApp.h" #include "ZDObject.h" #include "ZDProfile.h" #include "ZDConfig.h" #include "OSAL.h" #include "hal_defs.h" #include "hal_drivers.h" #include "hal_key.h" #include "hal_lcd.h" #include "hal_led.h" #include "hal_uart.h" #include "hal_timer.h" #include "mac_api.h" #include "aps.h" #define APP_ENDPOINT 1 #define APP_MSG_ID 0x10 #define APP_MSG_LEN 2 typedef struct { uint8 cmd; uint8 param; } appMsg_t; static uint8 appState = 0; static void appSendMsg(uint16 dstAddr, uint8 cmd, uint8 param) { appMsg_t *msg; uint8 bufLen = APP_MSG_LEN + sizeof(afAddrType_t); uint8 *buf = osal_mem_alloc(bufLen); if (buf != NULL) { msg = (appMsg_t *)(buf + sizeof(afAddrType_t)); msg->cmd = cmd; msg->param = param; afAddrType_t dstAddr; dstAddr.addrMode = afAddr16Bit; dstAddr.endPoint = APP_ENDPOINT; dstAddr.addr.shortAddr = dstAddr; AF_DataRequest(&dstAddr, &epDesc, APP_MSG_ID, bufLen, buf); osal_mem_free(buf); } } static void appRecvMsg(afIncomingMSGPacket_t *pkt) { if (pkt->clusterId == APP_MSG_ID) { appMsg_t *msg = (appMsg_t *)(pkt->cmd.Data); if (msg->cmd == 0x01) { halLedToggle(1); appState = msg->param; } } } static void appInit(void) { appState = 0; uint8 *extAddr = NULL; uint16 shortAddr = NLME_GetShortAddr(); uint8 bufLen = 8 + sizeof(afAddrType_t); uint8 *buf = osal_mem_alloc(bufLen); if (buf != NULL) { osal_memcpy(buf + sizeof(afAddrType_t), &shortAddr, sizeof(uint16)); osal_memcpy(buf + sizeof(afAddrType_t) + sizeof(uint16), extAddr, 8); ZDP_MgmtPermitJoinReq((afAddrType_t *)buf, 0); osal_mem_free(buf); } } void appMainLoop(void) { if (appState == 0) { appSendMsg(0xFFFF, 0x01, 0x01); } else if (appState == 1) { appSendMsg(0x0000, 0x01, 0x02); } else if (appState == 2) { appSendMsg(0x0001, 0x01, 0x03); } } void afIncomingMessage(afIncomingMSGPacket_t *pkt) { appRecvMsg(pkt); } void afDataConfirm(afStatus_t status) {} void ZDApp_Init(void) { appInit(); } uint16 ZDApp_eventLoop(uint8 taskId, uint16 events) { appMainLoop(); return events; } void main(void) { HAL_BOARD_INIT(); osal_init_system(); ZDApp_Init(); ZDApp_Startup(); while (1) { osal_run_system(); } } ``` 这个示例代码简单地实现了一个ZigBee设备的状态机,根据设备的状态依次发送不同的消息。在初始化时,设备向协调器发送一个Mgmt_Permit_Join_Req请求,允许其他设备加入网络。在主循环中,设备根据状态发送不同的消息,消息包括命令和参数,协调器接收到消息后进行处理并发送响应。当设备收到响应时,根据响应的参数更新设备的状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值