Zigbee 协议栈

平台

协议 TI ZStack-CC2530-2.5.1a

协议栈对我们的作用

协议栈是协议的实现,可以理解为代码,函数库, 供上层应用调用,协议较底下的层与应用是相互独立的。商业化的协议栈就是给你写好了底层的代码, 符合协议标准,提供给你一个功能模块给你调用你需要关心的就是你的应用逻辑,数据从哪里到哪里,怎么存储,处理;还有系统里的设备之间的通信顺序什么的,当你的应用需要数据通信时, 调用组网函数给你组建你想要的网络;当你想从一个设备发数据到另一个设备时, 调用无线数据发送函数;当然,接收端就调用接收函数;当你的设备没事干的时候,你就调用睡眠函数;要干活的时候就调用唤醒函数。所以当你做具体应用时,不需要关心协议栈是怎么写的,里面的每条代码是什么意思。除非你要做协议研究。每个厂商的协议栈有区别,也就是函数名称和参数可能有区别,这个要看具体的例子、说明文档。

怎么使用协议栈

举个例子,用户实现-个简单的无线数据通信时的一 般步骤:
1、组网:调用协议栈的组网函数、加入网络函数,实现网络的建立与节点的加入。
2、发送:发送节点调用协议栈的无线数据发送函数,实现无线数据发送。
3、接收:接收节点调用协议栈的无线数据接收函数,实现无线数据接收。
是不是看上去很简单啊,其实协议栈很多都封装好了,下面我们大概看看无线发送函数:

afStatus_ t AF_ DataRequest(    afAddrType_ t *dstAddr,
								endPointDesc_ t *srcEP,
								uint16 cID,
								uint16 len,
								uint8 *buf,
								uint8 *transID,
								uint8 options,
								uint8
								radius )

用户调用该函数即可实现数据的无线数据的发送,此函数中有8个参数,用户需要将每个参数的含义理解以后,才能熟练使用该函数进行无线数据通信的目的。现在只讲其中最重要的两个参数,其它参数不需要死记硬背,以后用多了自然就记住了。

uint16 len,//发送数据的长度;
uint8 *buf,//指向存放发送数据的缓冲区的指针。

至于调用该函数后,如何初始化硬件进行数据发送等工作,用户不需要关心, ZigBee协议栈己经将所需要的工作做好了,我们只需要调用相应的API函数即可,而不必关心具体实现细节。看起来是不是很简单呢,是不是有动手试试的冲动。先别急还要先安装ZigBee协议栈才能进行开发调试呢,下面就动手安装ZigBee协议栈吧。

协议栈的安装、编译与下载

去TI官网下载 ZStack-CC2530-2.5.1a.exe 进行安装,路径你可以选择默认,同样你也可以选择你想要安装的位置。也许有人就困惑了,装完之后不是应该有个桌面图标的么?其实所谓的安装协议栈只是把一些文件解压到你安装的目录下。
在这里插入图片描述

Components (部件)

顾名思义这个是放我们的库的文件夹,里面放了一些我们用到的ZDO,driver , hal , zcl等库的代码

Documents (文件)

这个不用说大家都知道是放TI的开发文档的,里面很多都是讲述协议栈的API的有空时可以看看

Projects (项目例子)

这个文件夹放的是TI协议栈的例子程序,一个个例子程序都是以一个个project的形式给我们的,学好这些例子程序里面的一两个,基本你能做事情了。

Tools (工具)

这个文件夹是放T|的例子程序的一些上位机之类的程序,作为工具使用。

用IAR打开 \ZStack-2.5.1a\Projects\zstack\Samples\SampleApp\CC2530DB\SampleApp.eww
在这里插入图片描述

App 应用层目录

这是用户创建各种不同工程的区域,在这个目录中包含了应用层的内容和这个项目的主要内容。

HAL :硬件层目录

包含有与硬件相
关的配置和驱动及操作函数。

MAC:MAC层目录

包含了MAC层的参数配置文件及其MAC的LIB库的函数接口文件。

MT

实现通过串口可控制各层并与各层进行直接交互

NWK :网络层目录

包含网络层配置参数文件网络层库的函数接口文件及

APS层库的函数接口。

OSAL :协议栈的操作系统。

Profile : Application framework

应用框架层目录包含AF层处理函数文件。应用框架层是应用程序和APS层的无线数据接口。

Security:安全层目录

包含安全层处理函数.比如加密函数等

Services :地址处理函数目录

包括地址模式的定义及地址处理函数。

Tools :工程配置目录

包括空间划分及Z-Stack相关配置信息。

ZDO : ZDO目录

ZMac: MAC层目录

包括MAC层参数配置及MAC层LB库函数回调处理函数.

ZMain:主函数目录

包括入口函数及硬件配置文件。

Output :输出文件目录

由IAR IDE自动生成。

协议栈应用开发相关函数

OSAL(Operating System Abstraction Layer)

中文解释操作系统抽象层。它可以看做是一种机制,一种任务分配资源的机制,从而形成 了一个简单多任务的操作系统。

int main( void )

在ZMain.c

/*********************************************************************
 * @fn      main
 * @brief   First function called after startup.
 * @return  don't care
 */
int main( void )
{
  // Turn off interrupts //关闭所有中断
  osal_int_disable( INTS_ALL );

  // Initialization for board related stuff such as LEDs //初始化系统时钟
  HAL_BOARD_INIT();

  // Make sure supply voltage is high enough to run //检查芯片电压是否正常
  zmain_vdd_check();

  // Initialize board I/O //初始化IO , LED、Timer 等
  InitBoard( OB_COLD );

  // Initialze HAL drivers //初始化芯片各硬件模块
  HalDriverInit();

  // Initialize NV System//初始化Flash存储器
  osal_nv_init( NULL );





  // Initialize the MAC //初始化MAC层
  ZMacInit();

  // Determine the extended address //确定IEEE 64位地址
  zmain_ext_addr();

#if defined ZCL_KEY_ESTABLISH
  // Initialize the Certicom certificate information.
  zmain_cert_init();
#endif

  // Initialize basic NV items//初始化非易失变量
  zgInit();

#ifndef NONWK
  // Since the AF isn't a task, call it's initialization routine
  afInit();
#endif





  // Initialize the operating system//初始化操作系统
  osal_init_system();

  // Allow interrupts//使能全部中断
  osal_int_enable( INTS_ALL );

  // Final board initialization//最终板载初始化
  InitBoard( OB_READY );

  // Display information about this device//显示设备信息
  zmain_dev_info();

  /* Display the device info on the LCD */
#ifdef LCD_SUPPORTED
  zmain_lcd_init(); //初始化LCD
#endif

#ifdef WDT_IN_PM1
  /* If WDT is used, this is a good place to enable it. */
  WatchDogEnable( WDTIMX );
#endif

  osal_start_system(); // No Return from here//执行操作系统


  return 0;  // Shouldn't get here.
} // main()

osal_init_system()

右键 go to definition of…

/*********************************************************************
 * @fn      osal_init_system
 *
 * @brief
 *
 *   This function initializes the "task" system by creating the
 *   tasks defined in the task table (OSAL_Tasks.h).
 *
 * @param   void
 *
 * @return  SUCCESS
 */
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()
// The order in this table must be identical to the task initialization calls below in osalInitTask.
const pTaskEventHandlerFn tasksArr[] = {
  macEventLoop,
  nwk_event_loop,
  Hal_ProcessEvent,
#if defined( MT_TASK )
  MT_ProcessEvent,
#endif
  APS_event_loop,
#if defined ( ZIGBEE_FRAGMENTATION )
  APSF_ProcessEvent,
#endif
  ZDApp_event_loop,
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  ZDNwkMgr_event_loop,
#endif
  SampleApp_ProcessEventa
};

const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
uint16 *tasksEvents;

/*********************************************************************
 * FUNCTIONS
 *********************************************************************/

/*********************************************************************
 * @fn      osalInitTasks
 *
 * @brief   This function invokes the initialization function for each task.
 *
 * @param   void
 *
 * @return  none
 */
void osalInitTasks( void )  //任务初始化函数在OSAL. SampleApp.c文件中

{
  uint8 taskID = 0;

  // 分配内存,返回指向缓冲区的指针
  tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
  // 设置所分配的内存空间单元值为0
  osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));

  // 任务优先级由高向低依次排列,高优先级对应taskID 的值反而小
  macTaskInit( taskID++ );  //macTaskInit(0) ,用户不需考虑
  nwk_init( taskID++ );     //nwk_init(1),用户不需考虑
  Hal_Init( taskID++ );     //Hal_Init(2) ,用户需考虑
#if defined( MT_TASK )
  MT_TaskInit( taskID++ );
#endif
  APS_Init( taskID++ );      //APS_Init(3) ,用户不需考虑
#if defined ( ZIGBEE_FRAGMENTATION )
  APSF_Init( taskID++ );
#endif
  ZDApp_Init( taskID++ );    //ZDApp_Init(4) ,用户需考虑
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  ZDNwkMgr_Init( taskID++ );
#endif

///************************************关键函数
  //用户创建的任务
  SampleApp_Init( taskID );  // SampleApp_Init _Init(5) ,用户需考虑
///************************************


}

SampleApp_ Init()是我们应用协议栈例程的必要函数,用户通常在这里初始化自己的
东西。

osal_start_system()

/*********************************************************************
 * @fn      osal_start_system
 *
 * @brief
 *
 *   This function is the main loop function of the task system (if
 *   ZBIT and UBIT are not defined). This Function doesn't return.
 *
 * @param   void
 *
 * @return  none
 */
void osal_start_system( void )
{
#if !defined ( ZBIT ) && !defined ( UBIT )
  for(;;)  // Forever Loop
#endif
  {
    osal_run_system();
  }
}
osal_run_system();
/*********************************************************************
 * @fn      osal_run_system
 *
 * @brief
 *
 *   This function will make one pass through the OSAL taskEvents table
 *   and call the task_event_processor() function for the first task that
 *   is found with at least one event pending. If there are no pending
 *   events (all tasks), this function puts the processor into Sleep.
 *
 * @param   void
 *
 * @return  none
 */
void osal_run_system( void )
{
  uint8 idx = 0;

  osalTimeUpdate(); //扫描哪个事件被触发了,然后置相应的标志位
  Hal_ProcessPoll(); //轮询TIMER与UART

/*A do statement executes a statement one or more times, while its test-context expression has a **nonzero** value:

    do
        statement
        while (test);
*/

  //得到待处理的最高优先级任务索引1号idx
  do {
    if (tasksEvents[idx])  // Task is highest priority that is ready.
    {
      break;
    }
  } while (++idx < tasksCnt);

  if (idx < tasksCnt)
  {
    uint16 events;
    halIntState_t intState;

    HAL_ENTER_CRITICAL_SECTION(intState);// 进入临界区,保护
    events = tasksEvents[idx];//提取需要处理的任务中的事件
    tasksEvents[idx] = 0;  // Clear the Events for this task.tasksEvents[idx] = 0; //清除本次任务的事件
    HAL_EXIT_CRITICAL_SECTION(intState);

    activeTaskID = idx;
    events = (tasksArr[idx])( idx, events );//通过指针调用任务处理函数,关键
    activeTaskID = TASK_NO_TASK;

    HAL_ENTER_CRITICAL_SECTION(intState);
    tasksEvents[idx] |= events;  // Add back unprocessed events to the current task.//保存未处理的事件

    HAL_EXIT_CRITICAL_SECTION(intState);//退出临界区

  }
#if defined( POWER_SAVING )
  else  // Complete pass through all task events with no activity?
  {
    osal_pwrmgr_powerconserve();  // Put the processor/system into sleep
  }
#endif

  /* Yield in case cooperative scheduling is being used. */
#if defined (configUSE_PREEMPTION) && (configUSE_PREEMPTION == 0)
  {
    osal_task_yield();
  }
#endif
}

SampleApp_ Init()

用户应用任务初始化函数

/*********************************************************************
 * @fn      SampleApp_Init
 *
 * @brief   Initialization function for the Generic App Task.
 *          This is called during initialization and should contain
 *          any application specific initialization (ie. hardware
 *          initialization/setup, table initialization, power up
 *          notificaiton ... ).
 *
 * @param   task_id - the ID assigned by OSAL.  This ID should be
 *                    used to send messages and set timers.
 *
 * @return  none
 */
void SampleApp_Init( uint8 task_id )
{ 
  SampleApp_TaskID = task_id;   //osal分配的任务ID随着用户添加任务的增多而改变
  SampleApp_NwkState = DEV_INIT;//设备状态设定为ZDO层中定义的初始化状态
	/*设备状态设定为ZDO层中定义的初始化状态
	初始化应用设备的网络类型, 设备类型的改变都要产生一个事件一_ZDO_ STATE_ CHANGE ,
	从字面理解为ZDO状态发生了改变。所以在设备初始化的时候一定要把它初始化为什么状
	态都没有。那么它就要去检测整个环境,看是否能重新建立或者加入存在的网络。但是有一
	种情况例外,就是当NV_ RESTORE被设置的候( NV_ RESTORE是把信息保存在非易失存
	储器中),那么当设备断电或者某种意外重启时,由于网络状态存储在非易失存储器中,那
	么此时就只需要恢复其网络状态,而不需要重新建立或者加入网络了.这里需要设置
	NV RESTORE宏定义。*/

  SampleApp_TransID = 0;        //消息发送ID(多消息时有顺序之分)
  
  // Device hardware initialization can be added here or in main() (Zmain.c).
  // If the hardware is application specific - add it here.
  // If the hardware is other parts of the device add it in main().

 #if defined ( BUILD_ALL_DEVICES )
  // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START
  // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered
  // together - if they are - we will start up a coordinator. Otherwise,
  // the device will start as a router.
  if ( readCoordinatorJumper() )
    zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
  else
    zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
#endif // BUILD_ALL_DEVICES

//该段的意思是,如果设置了HOLD_AUTO_START宏定义,将会在启动芯片的时候会暂停启动
//流程,只有外部触发以后才会启动芯片。其实就是需要一个按钮触发它的启动流程。  
#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.
  ZDOInitDevice(0);
#endif

  // Setup for the periodic message's destination address 设置发送数据的方式和目的地址寻址模式
  // Broadcast to everyone 发送模式:广播发送
  SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//广播
  SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号
  SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;//指定目的网络地址为广播地址

  // Setup for the flash command's destination address - Group 1 组播发送
  SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; //组寻址
  SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号
  SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;//组号0x0001

  // Fill out the endpoint description. 定义本设备用来通信的APS层端点描述符
  SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号
  SampleApp_epDesc.task_id = &SampleApp_TaskID;   //SampleApp 描述符的任务ID
  SampleApp_epDesc.simpleDesc
            = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;//SampleApp简单描述符
  SampleApp_epDesc.latencyReq = noLatencyReqs;    //延时策略
/*向AF层登记描述符,登记endpoint description 到AF,要对该应用进行初始化并在AF
//进行登记,告诉应用层有这么一个EP已经开通可以使用,那么下层要是有关于该应用的信
//息或者应用要对下层做哪些操作,就自动得到下层的配合*/

  // Register the endpoint description with the AF
  afRegister( &SampleApp_epDesc );    //向AF层登记描述符

  // Register for all key events - This app will handle all key events
  RegisterForKeys( SampleApp_TaskID ); // 登记所有的按键事件

  // By default, all devices start out in Group 1
  SampleApp_Group.ID = 0x0001;//组号
  osal_memcpy( SampleApp_Group.name, "Group 1", 7  );//设定组名
  aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );//把该组登记添加到APS中

#if defined ( LCD_SUPPORTED )
  HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 ); //如果支持LCD,显示提示信息
#endif
}

SampleApp_ ProcessEvent()

用户应用任务的事件处理函数

/*********************************************************************
 * @fn      SampleApp_ProcessEvent
 *
 * @brief   Generic Application Task event processor.  This function
 *          is called to process all events for the task.  Events
 *          include timers, messages and any other user defined events.
 *
 * @param   task_id  - The OSAL assigned task ID.
 * @param   events - events to process.  This is a bit map and can
 *                   contain more than one event.
 *
 * @return  none
 */
//用户应用任务的事件处理函数
uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  (void)task_id;  // Intentionally unreferenced parameter

  if ( events & SYS_EVENT_MSG ) //接收系统消息再进行判断
  {
    //接收属于本应用任务SampleApp的消息,以SampleApp_TaskID标记
    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://接收数据事件,调用函数AF_DataRequest()接收数据
          SampleApp_MessageMSGCB( MSGpkt );//调用回调函数对收到的数据进行处理
          break;

        // Received whenever the device changes state in the network
        case ZDO_STATE_CHANGE:
          //只要网络状态发生改变,就通过ZDO_STATE_CHANGE事件通知所有的任务。
          //同时完成对协调器,路由器,终端的设置
          SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
          //if ( (SampleApp_NwkState == DEV_ZB_COORD)//实验中协调器只接收数据所以取消发送事件
          if ( (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;
      }

      // Release the memory 事件处理完了,释放消息占用的内存
      osal_msg_deallocate( (uint8 *)MSGpkt );

      // Next - if one is available 指针指向下一个放在缓冲区的待处理的事件,
      //返回while ( MSGpkt )重新处理事件,直到缓冲区没有等待处理事件为止
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
    }

    // return unprocessed events 返回未处理的事件
    return (events ^ SYS_EVENT_MSG);
  }
/*********************************************************************
 * @fn      SampleApp_ProcessEvent
 *
 * @brief   Generic Application Task event processor.  This function
 *          is called to process all events for the task.  Events
 *          include timers, messages and any other user defined events.
 *
 * @param   task_id  - The OSAL assigned task ID.
 * @param   events - events to process.  This is a bit map and can
 *                   contain more than one event.
 *
 * @return  none
 */
//用户应用任务的事件处理函数
uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  (void)task_id;  // Intentionally unreferenced parameter

  if ( events & SYS_EVENT_MSG ) //接收系统消息再进行判断
  {
    //接收属于本应用任务SampleApp的消息,以SampleApp_TaskID标记
    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://接收数据事件,调用函数AF_DataRequest()接收数据
          SampleApp_MessageMSGCB( MSGpkt );//调用回调函数对收到的数据进行处理
          break;

        // Received whenever the device changes state in the network
        case ZDO_STATE_CHANGE:
          //只要网络状态发生改变,就通过ZDO_STATE_CHANGE事件通知所有的任务。
          //同时完成对协调器,路由器,终端的设置
          SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
          //if ( (SampleApp_NwkState == DEV_ZB_COORD)//实验中协调器只接收数据所以取消发送事件
          if ( (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;
      }

      // Release the memory 事件处理完了,释放消息占用的内存
      osal_msg_deallocate( (uint8 *)MSGpkt );

      // Next - if one is available 指针指向下一个放在缓冲区的待处理的事件,
      //返回while ( MSGpkt )重新处理事件,直到缓冲区没有等待处理事件为止
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
    }

    // return unprocessed events 返回未处理的事件
    return (events ^ SYS_EVENT_MSG);
  }

void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )

/*********************************************************************
 * LOCAL FUNCTIONS
 */

/*********************************************************************
 * @fn      SampleApp_MessageMSGCB
 *
 * @brief   Data message processor callback.  This function processes
 *          any incoming data - probably from other devices.  So, based
 *          on cluster ID, perform the intended action.
 *
 * @param   none
 *
 * @return  none
 */
//接收数据,参数为接收到的数据
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
  uint16 flashTime;
  byte buf[3]; 

  switch ( pkt->clusterId ) //判断簇ID
  {
    case SAMPLEAPP_PERIODIC_CLUSTERID: //收到广播数据
      osal_memset(buf, 0 , 3);
      osal_memcpy(buf, pkt->cmd.Data, 2); //复制数据到缓冲区中
      
      if(buf[0]=='D' && buf[1]=='1')      //判断收到的数据是否为"D1"         
      {
          HalLedBlink(HAL_LED_1, 0, 50, 500);//如果是则Led1间隔500ms闪烁
#if defined(ZDO_COORDINATOR) //协调器收到"D1"后,返回"D1"给终端,让终端Led1也闪烁
          SampleApp_SendPeriodicMessage();
#endif
      }
      else
      {
          HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);                   
      }
      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 )

分析发送周期信息

void SampleApp_SendPeriodicMessage( void )
{
  byte SendData[3]="D1";

  // 调用AF_DataRequest将数据无线广播出去
  if( AF_DataRequest( &SampleApp_Periodic_DstAddr,//发送目的地址+端点地址和传送模式
                       &SampleApp_epDesc,//源(答复或确认)终端的描述(比如操作系统中任务ID等)源EP
                       SAMPLEAPP_PERIODIC_CLUSTERID, //被Profile指定的有效的集群号
                       2,       // 发送数据长度
                       SendData,// 发送数据缓冲区
                       &SampleApp_TransID,     // 任务ID号
                       AF_DISCV_ROUTE,      // 有效位掩码的发送选项
                       AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )  //传送跳数,通常设置为AF_DEFAULT_RADIUS
  {
  }
  else
  {
    HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
    // Error occurred in request to send.
  }
}

void SampleApp_SendFlashMessage( uint16 flashTime )

/*********************************************************************
 * @fn      SampleApp_SendFlashMessage
 *
 * @brief   Send the flash message to group 1.
 *
 * @param   flashTime - in milliseconds
 *
 * @return  none
 */
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.
  }
}

AF_DataRequest()

/*********************************************************************
 * @fn      AF_DataRequest
 *
 * @brief   Common functionality for invoking APSDE_DataReq() for both
 *          SendMulti and MSG-Send.
 *
 * input parameters
 *
 * @param  *dstAddr - Full ZB destination address: Nwk Addr + End Point.
 * @param  *srcEP - Origination (i.e. respond to or ack to) End Point Descr.
 * @param   cID - A valid cluster ID as specified by the Profile.
 * @param   len - Number of bytes of data pointed to by next param.
 * @param  *buf - A pointer to the data bytes to send.
 * @param  *transID - A pointer to a byte which can be modified and which will
 *                    be used as the transaction sequence number of the msg.
 * @param   options - Valid bit mask of Tx options.
 * @param   radius - Normally set to AF_DEFAULT_RADIUS.
 *
 * output parameters
 *
 * @param  *transID - Incremented by one if the return value is success.
 *
 * @return  afStatus_t - See previous definition of afStatus_... types.
 */
uint8 AF_DataRequestDiscoverRoute = TRUE;
afStatus_t AF_DataRequest( afAddrType_t *dstAddr, endPointDesc_t *srcEP,
                           uint16 cID, uint16 len, uint8 *buf, uint8 *transID,
                           uint8 options, uint8 radius )

东西很多,慢慢来吧

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值