凡是組網,必然涉及到單播,組播,廣播這幾個概念。
這幾個概念的含義一目瞭然。這節主要關注怎麼在zigbee中實現單播,組播,廣播。
回到 afStatus_t AF_DataRequest( afAddrType_t *dstAddr, endPointDesc_t *srcEP,
uint16 cID, uint16 len, uint8 *buf, uint8 *transID,
uint8 options, uint8 radius )
的第一個參數afAddrType_t *dstAddr, 是什麼類型的消息,由這個參數決定。
typedef struct
{
union
{
uint16 shortAddr;
ZLongAddr_t extAddr;
} addr;
afAddrMode_t addrMode; //決定了數據發送模式,這個值爲 afAddr16Bit表示單播, afAddrGroup 是組播, afAddrBroadcast 是廣播
uint8 endPoint;
uint16 panId; // used for the INTER_PAN feature
} afAddrType_t;
除了設置addrMode,目的地址還需要設置 shortAddr,它是節點的網絡地址,如協調器爲0x0000。
單播
將目的地址設置成單播形式即可。
Point_To_Point_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
Point_To_Point_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
Point_To_Point_DstAddr.addr.shortAddr = 0x0000; //發給協調器
組播
使用組播方式,難點是如何加入特定的組。 組播的要素(見結構體aps_Group_t)爲 組播ID, 組播名字。
typedef struct
{
uint16 ID; // Unique to this table
uint8 name[APS_GROUP_NAME_LEN]; // Human readable name of group
} aps_Group_t;
然後調用aps_AddGroup,將端口加入組播組。
如 // 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 );
廣播
使用廣播通信時,shortAddr的網絡地址可以爲0xffff, 0xfffd, 0xfffc
0xffff 表示數據在全網廣播,包括處於休眠的節點;
0xfffd 表示數據包只發往處於active的節點,不發往處於休眠的節點;
0xfffc表示數據包發往網絡中的所有路由器節點。
如:
SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;
SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;