zigbee单播、组播、广播



zigbee单播、组播、广播  

单播:

按照协议栈示例GenericApp中的用法:
单播有两种方式一种是绑定传输,一种是直接指定目标地址的单播传输
按照如下步骤
1.设定发送的目标地址

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


设定发送的目标地址,这里地址模式AddrNotPresent,即按照绑定的方式进行单播,不需要指定目标地址,需要先将两个设备绑定,将两个设备绑定后即可通信
还有另外三种传送方式,如下:
enum
 {
 AddrNotPresent = 0,//按照绑定表进行绑定传输
AddrGroup = 1,//组播传输
Addr16Bit = 2,//指定目标网络地址进行单播传输
Addr64Bit = 3,//指定IEEE地址进行单播传输
AddrBroadcast = 15//广播传输
};


2.注册端点描述符
// 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 );


 3.在需要发送数据的地方,执行如下代码:
if ( AF_DataRequest( &GenericApp_DstAddr, &GenericApp_epDesc,
                        GENERICAPP_CLUSTERID,
                        (byte)osal_strlen( theMessageData ) + 1,
                        (byte *)&theMessageData,
                        &GenericApp_TransID,
                        AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
 {
     // Successfully requested to be sent.
 }
 else
 {
     // Error occurred in request to send.
 }


注意GENERICAPP_CLUSTERID必须为对方的输入cluster,且两方的简单描述符中的profileID必须一致
4.在接收设备任务循环中检测AF_INCOMING_MSG_CMD事件:
afIncomingMSGPacket_t结构的数据包进行处理
afIncomingMSGPacket_t结构如下:
typedef struct
 {
 osal_event_hdr_t hdr;
 uint16 groupId;
 uint16 clusterId;
 afAddrType_t srcAddr;
 byte endPoint;
 byte wasBroadcast;
 byte LinkQuality;
 byte SecurityUse;
 uint32 timestamp;
 afMSGCommandFormat_t cmd;
 } afIncomingMSGPacket_t;


其中afMSGCommandFormat_t结构如下:
typedef struct
 {
 byte   TransSeqNumber;
 uint16 DataLength;               // Number of bytes in TransData
 byte *Data;
 } afMSGCommandFormat_t;


提取出Data即可

---------------------------

组播:

按照SampleApp实验,组播的实现需要如下步骤:
1.声明一个组对象aps_Group_t SampleApp_Group;
2.对aps_Group_t结构体赋值,示例如下:

// By default, all devices start out in Group 1
 SampleApp_Group.ID = 0x0003;
 osal_memcpy( SampleApp_Group.name, "Group 3", 7 );


 3.设定通信的目标地址,示例如下:
// 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;


 4.注册端点描述符,示例如下:
// Fill out the endpoint description.
 SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT;
 SampleApp_epDesc.task_id = &SampleApp_TaskID;
 SampleApp_epDesc.simpleDesc
             = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;
 SampleApp_epDesc.latencyReq = noLatencyReqs;

// Register the endpoint description with the AF
 afRegister( &SampleApp_epDesc );


 5.在本任务里将端点加入到组中,示例如下:
aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );


 6.按照组播地址向对方发送数据,示例如下:
if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
                        SAMPLEAPP_PERIODIC_CLUSTERID,
                        1,
                        (uint8*)&SampleAppPeriodicCounter,
                        &SampleApp_TransID,
                        AF_DISCV_ROUTE,
                        AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
 {
 }
 else
 {
     // Error occurred in request to send.
 }


通信时候,发送设备的输出cluster设定为接收设备的输入cluster,另外profileID设定相同,即可通信
7.对数据的处理与单播的实现一样
8.若要把一个设备加入到组中的端点从组中移除,调用aps_RemoveGroup即可,示例如下:
aps_Group_t *grp;
 grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
 if ( grp )
 {
    // Remove from the group
    aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
 }


---------------------------

广播

按照SampleApp,执行如下步骤即可
1.声明afAddrType_t 的变量SampleApp_Periodic_DstAddr;
2.设定目标地址变量为广播地址,示例如下:

SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;
 SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
 SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;


 3.进行数据发送,示例如下:
if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
                       SAMPLEAPP_PERIODIC_CLUSTERID,
                        1,
                        (uint8*)&SampleAppPeriodicCounter,
                        &SampleApp_TransID,
                        AF_DISCV_ROUTE,
                        AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
 {
 }
 else
 {
     // Error occurred in request to send.
 }


通信时候,发送设备的输出cluster设定为接收设备的输入cluster,另外profileID设定相同,即可通信
4.对数据的处理与单播的实现一样

你可能感兴趣的:(zigbee单播、组播、广播)