📄 nwk_broadcast.c
字号:
}
else
{
//this path is worse,so discard it.
/*TODO:free the memory occupied by the rreq*/
}
}
else
{
//there is no matched entry in the route discovery table.
// Create the Routing and Route Discovery table entries.
if (!CreateRoutingTableEntries( rreq.destinationAddress, &routeDiscoveryTableIndex, &routeTableIndex))
{
/*TODO:free the memory occupied by the rreq*/
}
//initialize some remainder field in the new created entry in the route discovery table and routing table.
routeDiscoveryTablePointer[routeDiscoveryTableIndex]->routeRequestID=rreq.routeRequestIdentifier;
// routeDiscoveryTablePointer[routeDiscoveryTableIndex]->srcAddress=/*read from nwk header*/;
routeDiscoveryTablePointer[routeDiscoveryTableIndex]->senderAddress=mcpsDataIndicationInfo.srcAddr.Short;
routeDiscoveryTablePointer[routeDiscoveryTableIndex]->forwardCost=rreq.pathCost;
//justify if this device or the child device of this device is the destination of the rreq
while((getAddressResult=mlmeGetRequest(MAC_SHORT_ADDRESS,&myMacShortAddress))!=SUCCESS);
if((myMacShortAddress==rreq.destinationAddress)||(((NWKLookupNodeByShortAddr(rreq.destinationAddress)!=INVALID_NEIGHBOR_INDEX)&&(currentNeighborRecord.deviceInfo.bits.relationship== NEIGHBOR_IS_CHILD))))
{
if(myMacShortAddress!=rreq.destinationAddress)
{
//indicates that:I am not the destination,so one of my child is the destination
//change the pathCost use the constant lqi;
rreq.pathCost+=7;
}
// Destroy the current packet and start the Route Reply
{
CreateAndSendRouteReply( mcpsDataIndicationInfo.srcAddr.Short, routeDiscoveryTableIndex, &rreq );
}
}
else
{
//myself and my chid device aren't the destination of the RREQ
//set the forwardRouteRequest variable
forwardRouteRequest=TRUE;
}
}
if(forwardRouteRequest)
{
//do some operation about the forward
//first:we should buffer the recived RREQ and after a jitter time duration ,we can send it out
// if((routeDiscoveryTablePointer[routeDiscoveryTableIndex]->forwardRREQ=malloc(sizeof(ROUTE_REQUEST_COMMAND)+NWK_HEADER_LENGTH))!=NULL)
{
//successfully allocate the memory.
ptr=(BYTE*)&rreq;
forwardRreqFrame = routeDiscoveryTablePointer[routeDiscoveryTableIndex]->forwardRREQ;
//we should use the content of the rreq to initialize the memory pointed by forwardRREQ;
for(i=0;i<sizeof(ROUTE_REQUEST_COMMAND);i++)
{
*forwardRreqFrame++=*ptr++;
}
//then,construct the forwarded rreq's header.
/// ByteArrayContentCopy(forwardRreqFrame, mcpsDataIndicationInfo.pMsdu,sizeof(NWK_HEADER_LENGTH));
*forwardRreqFrame++=mcpsDataIndicationInfo.pMsdu[7];
*forwardRreqFrame++=mcpsDataIndicationInfo.pMsdu[6];
*forwardRreqFrame++=mcpsDataIndicationInfo.pMsdu[5];
*forwardRreqFrame++=mcpsDataIndicationInfo.pMsdu[4];
*forwardRreqFrame++=mcpsDataIndicationInfo.pMsdu[3];
*forwardRreqFrame++=mcpsDataIndicationInfo.pMsdu[2];
*forwardRreqFrame++=mcpsDataIndicationInfo.pMsdu[1];
*forwardRreqFrame++=mcpsDataIndicationInfo.pMsdu[0];
routeDiscoveryTablePointer[routeDiscoveryTableIndex]->rebroadcastJitterTimer=getCurrentime();
}
/*free the memory occupied by RREQ*/
}
}
else
#endif //end of the #ifndef NWK_USE_TREE_ROUTING_ONLY
{
//The device doesn't have routing capacity,now,we should check if the frame was
//recived along a valid path
#ifndef NWK_WORK_AS_END_DEVICE
// NOTE: The path is valid if one of the following condition is satisfied.
//(1) if was received from one of the device's child devices and the source device is a descendant of that child device (or that child device itself),
//(2) if the frame was received from the device's parent device and the source device is not my descendant.
getAddressResult=MAC_UNSUPPORTED_ATTRIBUTE;
while((getAddressResult=mlmeGetRequest(MAC_COORD_SHORT_ADDRESS, &macCoordShortAddress))!=SUCCESS);
getAddressResult=MAC_UNSUPPORTED_ATTRIBUTE;
while((getAddressResult=mlmeGetRequest(MAC_SHORT_ADDRESS,&myMacShortAddress))!=SUCCESS);
if((((NWKLookupNodeByShortAddr(mcpsDataIndicationInfo.srcAddr.Short)!=INVALID_NEIGHBOR_INDEX)&&(currentNeighborRecord.deviceInfo.bits.relationship==NEIGHBOR_IS_CHILD))&&
((mcpsDataIndicationInfo.srcAddr.Short==(mcpsDataIndicationInfo.pMsdu[1]<<8)| mcpsDataIndicationInfo.pMsdu[2])||(IsDescendant(mcpsDataIndicationInfo.srcAddr.Short,(mcpsDataIndicationInfo.pMsdu[1]<<8)| mcpsDataIndicationInfo.pMsdu[2],currentNeighborTableInfo.depth+1))))
||((macCoordShortAddress==mcpsDataIndicationInfo.srcAddr.Short)&& !IsDescendant(myMacShortAddress,(mcpsDataIndicationInfo.pMsdu[1]<<8)| mcpsDataIndicationInfo.pMsdu[2],currentNeighborTableInfo.depth)))
{
//the frame is from a valid path
//the device shall check is it is or one of its child device is the intended destination
if((myMacShortAddress==rreq.destinationAddress)||(((NWKLookupNodeByShortAddr(rreq.destinationAddress)!=INVALID_NEIGHBOR_INDEX)&&(currentNeighborRecord.deviceInfo.bits.relationship== NEIGHBOR_IS_CHILD))))
{
if(myMacShortAddress!=rreq.destinationAddress)
{
//the destination is one of my child device
rreq.pathCost += 7;
}
//free the memory occupied by the recived rreq and call CreateAndSendRouteReply
{
// CreateAndSendRouteReply( /*mcpsDataIndicationInfo.pMsdu[1]contacts mcpsDataIndicationInfo.pMsdu[2]*/, INVALID_ROUTE_DISCOVERY_TABLE_INDEX, &rreq );
}
}
else
{
//Myself and my child device aren't the destination of the rreq,so we need unicast the rreq.
if (RouteAlongTree( mcpsDataIndicationInfo.pMsdu[2], &(alongTreeNextAddress) ) )
{
//now we are forwarding the rreq along the tree
//we need to construct the outgoing rreq packet
// Prepare the Route Request message.
// Construct the NWK header- the route request command frame.
//can I use the mcpsDataAlongTreeInfo.pMsdu[] to replace the forwardRreqAlongTreePacket????????
*forwardRreqAlongTreePacket++=FRAME_TYPE_NWK_COMMAND| (ZIGBEE_PROTOCOL_VERSION<<2);
*forwardRreqAlongTreePacket++=0;
*forwardRreqAlongTreePacket++=mcpsDataIndicationInfo.pMsdu[2];
*forwardRreqAlongTreePacket++=mcpsDataIndicationInfo.pMsdu[3];
*forwardRreqAlongTreePacket++=myMacShortAddress&0xFF;
*forwardRreqAlongTreePacket++=myMacShortAddress>>8;
#ifdef NWK_DEFAULT_RADIUS
*forwardRreqAlongTreePacket++=NWK_DEFAULT_RADIUS;
#else
*forwardRreqAlongTreePacket++=2*nnib.nwkMaxDepth;
#endif
*forwardRreqAlongTreePacket++=/*mcpsDataIndicationInfo.pMsdu[7];*/randNumGenerator(0xff);
//Construct the NWK payload-the route request command frame.
ptr=(BYTE*)&rreq;
ByteArrayContentCopy(forwardRreqAlongTreePacket, ptr, sizeof(ROUTE_REQUEST_COMMAND));
//prepare the parameters for the function mcpsDataRequest
mcpsDataAlongTreeInfo.addrModes=AM_SHORT_16;
mcpsDataAlongTreeInfo.pSrcAddr->Short=myMacShortAddress;
getAddressResult=MAC_UNSUPPORTED_ATTRIBUTE;
while((getAddressResult=mlmeGetRequest(MAC_PAN_ID, &myMacPanId))!=SUCCESS);
mcpsDataAlongTreeInfo.srcPanId=myMacPanId;
getAddressResult=MAC_UNSUPPORTED_ATTRIBUTE;
while((getAddressResult=mlmeGetRequest(MAC_DSN,&macDsn))!=SUCCESS);
mcpsDataAlongTreeInfo.msduHandle=macDsn;
mcpsDataAlongTreeInfo.msduLength=NWK_HEADER_LENGTH+CMD_ROUTE_REQUEST_PAYLOAD_LENGTH;
mcpsDataAlongTreeInfo.pDestAddr->Short=alongTreeNextAddress;
mcpsDataAlongTreeInfo.destPanId=myMacPanId;
//mcpsDataAlongTreeInfo.pMsdu=/**/;
mcpsDataAlongTreeInfo.txOptions=TX_OPT_ACK_REQ;
//call the mcpsDataRequest function
mcpsDataRequest(mcpsDataAlongTreeInfo.addrModes, mcpsDataAlongTreeInfo.srcPanId, mcpsDataAlongTreeInfo.pSrcAddr, mcpsDataAlongTreeInfo.destPanId, mcpsDataAlongTreeInfo.pDestAddr, mcpsDataAlongTreeInfo.msduLength, mcpsDataAlongTreeInfo.pMsdu, mcpsDataAlongTreeInfo.msduHandle, mcpsDataAlongTreeInfo.txOptions);
}
else
{
//cann't find the valid next hop address along the tree
//discard the packet.
/*TODO:discard the packet*/
}
}
}
else
#endif
{
//the route request frame is from an invalid path,discard the packet.
/*free the memory occupied by the recived rreq*/
}
}
}
//------------------------------------------------------------------------------------
// void sendBroadcastFrameToAllNeighbors(BYTE BTTIndex);
//
// DESCRIPTION:
// This function is used to send nwk layer's broadcast frame to all neighbor devices .
// PARAMETER:
// BYTE BTTIndex,
// The index of the broadcast frame that will be sent out in the BTT array.
// RETURN-VALUE:
// NO
// NOTE:
// This function is used in broadcast procedure.
//---------------------------------------------------------------------------------------
void sendBroadcastFrameToAllNeighbors(BYTE BTTIndex)
{
BYTE i=0;
UINT16 macShortAddress;
BYTE msduHandle;
BYTE queueIndex;
UINT8 length=0;
nwkConstructDataFrameMsduLocally(0, nwkBroadcastAddress, nwkRxFrameFromAPS,&length);
//prepare parameters for the mcpsDataRequest
printf("开始给设备的所有邻居设备发送广播
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -