按照协议栈示例GenericApp中的用法:
单播有两种方式一种是绑定传输,一种是直接指定目标地址的单播传输
按照如下步骤
1.设定发送的目标地址
GenericApp_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent; GenericApp_DstAddr.endPoint = 0; GenericApp_DstAddr.addr.shortAddr = 0;
enum { AddrNotPresent = 0,//按照绑定表进行绑定传输 AddrGroup = 1,//组播传输 Addr16Bit = 2,//指定目标网络地址进行单播传输 Addr64Bit = 3,//指定IEEE地址进行单播传输 AddrBroadcast = 15//广播传输 };
// 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 );
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. }
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;
typedef struct { byte TransSeqNumber; uint16 DataLength; // Number of bytes in TransData byte *Data; } afMSGCommandFormat_t;
---------------------------
组播:
按照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 );
// 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;
// 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 );
aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );
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. }
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;
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. }