1. 程式人生 > >ZStack單播組播廣播具體操作與原理資料整理

ZStack單播組播廣播具體操作與原理資料整理

// 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.對資料的處理與單播的實現一樣