📄 nwk_broadcast.c
字号:
#pragma section @@DATA Nwk_broadcast at 0xF6FF
#include"includes.h"
BTR *BTT[NWK_NUM_BUFFERED_BROADCAST_MESSAGES]; // Broadcast Transaction Table
#define NWK_RREQ_FRAME_LENGTH 14
extern NWK_NIB nnib;
extern MCPS_DATA_INDICATION mcpsDataIndicationInfo;
extern NEIGHBOR_RECORD neighborTable[NEIGHBOR_TABLE_ENTRIES];
extern UINT16 nwkBroadcastAddress;
extern BYTE nwkRxFrameFromAPS[aMaxNWKFrameSize-NWK_HEADER_LENGTH];
extern NWK_FRAMES nwkConfirmationHandles[MAX_NWK_FRAMES];
extern MCPS_DATA_INFO mcpsDataRequestInfo;
extern ROUTE_DISCOVERY_ENTRY *routeDiscoveryTablePointer[NWK_ROUTE_DISCOVERY_TABLE_SIZE];
extern NEIGHBOR_TABLE_INFO currentNeighborTableInfo;
extern NEIGHBOR_RECORD *pCurrentNeighborRecord;
extern NEIGHBOR_RECORD currentNeighborRecord; // Node information.
//------------------------------------------------------------------------------------
// ZBOOL CreateNewBTR( BYTE *BTTIndex );
//
// DESCRIPTION:
// This function tries to create a new BTR.
// PARAMETER:
// BYTE *BTTIndex,
// A 'value-result' parameter,if the function completes successfully,use the BTTIndex to take
// back the BTR's index in the BTT.
// RETURN-VALUE:
// TRUE:create the new BTR successfully.
// FALSE:there is no room in the BTT or there is not enough memory to allocate to BTR.
//-----------------------------------------------------------------------------------
ZBOOL CreateNewBTR( BYTE *BTTIndex )
{
BYTE i;
BYTE j;
////First:find an empty palce in the BTT.
for (i=0; ((i<NWK_NUM_BUFFERED_BROADCAST_MESSAGES)&&(BTT[i]->dataLength!=-1)); i++)
{
}
if (i==NWK_NUM_BUFFERED_BROADCAST_MESSAGES)
{
return FALSE; //there is no room in BTT
}
// Second:try to allocate space for a new BTR.
if ((BTT[i] = (BTR *)malloc(sizeof(BTR))) == NULL)
{
return FALSE; //failed in allocating memory
}
memset(BTT[i],0,sizeof(BTR));
// Third:use the *BTTIndex to take back the the new entry's index in the BTT.
*BTTIndex = i;
//Fourth:do some initialization to the new created BTR.
printf("i is %d ",*BTTIndex);
BTT[i]->broadcastTime=getCurrentime();
printf("BTT[i]->broadcastTime is %d\n",BTT[i]->broadcastTime);
BTT[i]->btrInfo.nRetries=nnib.nwkMaxBroadcastRetries;
BTT[i]->btrInfo.bConfirmSent=0;
BTT[i]->broadcastJitterTimer=nwkcMaxBroadcastJitter;
BTT[i]->currentNeighbor=0xFf;
/*BTT[i]->currentNeighbor=0x10;*/
printf("BTT[i]->currentNeighbor is %ld \n",BTT[i]->currentNeighbor);
for(j=0;j<NEIGHBOR_TABLE_ENTRIES;j++)
{
BTT[i]->bMessageNotRelayed[j]=0; //initialize the flag array.
}
}
//--------------------------------------------------------------------------
// void MarkNeighborAsPasssiveACKed( BYTE BTTindex )
//
// DESCRIPTION:
// This function must be called while mcpsDataIndication parameters are valid.The indicated BTR will be updated to reflect that
// the source of the current message has rebroadcast the message.
// PARAMETER:
// BTTindex
// RETURN-VALUE:
// NO
//-----------------------------------------------------------------------------
//#ifndef NWK_OPT_RFD
void MarkNeighborAsPasssiveACKed( BYTE BTTindex )
{
BYTE j;
j = -1; // So we'll get 0 on the first increment.
do
{
j++;
pCurrentNeighborRecord = &(neighborTable[j]);
// ReadNeighborRecordIntoRAM( ¤tNeighborRecord, (void*)pCurrentNeighborRecord );
currentNeighborRecord=*pCurrentNeighborRecord;
}
while ((j < NumberOfEntriesInNeighborTable()) && (currentNeighborRecord.shortAddress != mcpsDataIndicationInfo.srcAddr.Short));
if (j<NumberOfEntriesInNeighborTable())
{
BTT[BTTindex]->bMessageNotRelayed[j]=0;
}
}
//#endif
//--------------------------------------------------------------------------
// void nwkDealWithRecivedRreqFromMLDE(void);
//
// DESCRIPTION:
// This function deals with the recived route request command through mcpsDataIndication function.
// PARAMETER:
// NO
// RETURN-VALUE:
// NO
// NOTE:
// The meaning of the function name is :network deals with recived route request command
// recived through mcpsDataIndication function;this route request command is probably from
// some remote device.
//-----------------------------------------------------------------------------
void nwkDealWithRecivedRreqFromMLDE(void)
{
//In this program,we use the global variable instead of passing parameter.
ZBOOL forwardRouteRequest; //means if we need to forward the rreq
ROUTE_REQUEST_COMMAND rreq;
//BYTE rreqFrame[NWK_RREQ_FRAME_LENGTH];
BYTE *rreqFrame;
BYTE *forwardRreqFrame;
BYTE routeDiscoveryTableIndex;
BYTE routeTableIndex;
UINT16 myMacShortAddress;
MAC_ENUM getAddressResult;
BYTE i;
BYTE *ptr;
UINT16 macCoordShortAddress;
UINT16 alongTreeNextAddress;
//NWK_TX_PACKET *forwardRreqAlongTreePacket=NULL;
BYTE *forwardRreqAlongTreePacket=NULL;
MCPS_DATA_INFO mcpsDataAlongTreeInfo;
UINT16 myMacPanId;
UINT8 macDsn;
BYTE msduLength;
#ifdef NWK_CALCULATE_LINK_QUALITY
BYTE lqi;
BYTE rssi;
#endif
forwardRouteRequest = FALSE;
// Get the Route Request command frame
rreqFrame=(BYTE*)&rreq;
//*rreqFrame++=NWK_COMMAND_ROUTE_REQUEST;
//*rreqFrame++=mcpsDataIndicationInfo.pMsdu[9];
//*rreqFrame++=mcpsDataIndicationInfo.pMsdu[10];
ByteArrayContentCopy(rreqFrame, &mcpsDataIndicationInfo.pMsdu[8], CMD_ROUTE_REQUEST_PAYLOAD_LENGTH);
// Update the path cost to get to us.
#ifdef NWK_CALCULATE_LINK_QUALITY
rreq.pathCost=rreq.pathCost+CONSTANT_LQL_VALUE;
#endif
#ifndef NWK_USE_TREE_ROUTING_ONLY
if (HaveRoutingCapacity( TRUE, rreq.routeRequestIdentifier, mcpsDataIndicationInfo.srcAddr.Short, rreq.destinationAddress, rreq.commandOptions ) &&
(mcpsDataIndicationInfo.dstAddr.Short== 0xFFFF))
{
// Try to find the matching route discovery entry.
//searching the route discovery table;
for (routeDiscoveryTableIndex = 0;(routeDiscoveryTableIndex < NWK_ROUTE_DISCOVERY_TABLE_SIZE) &&
!((routeDiscoveryTablePointer[routeDiscoveryTableIndex] != NULL) &&
(routeDiscoveryTablePointer[routeDiscoveryTableIndex]->routeRequestID == rreq.routeRequestIdentifier) &&
(routeDiscoveryTablePointer[routeDiscoveryTableIndex]->srcAddress == mcpsDataIndicationInfo.srcAddr.Short));
routeDiscoveryTableIndex++ ) {}
//please pay attention to the '!';the for loop will terminated when finding out matched entry or the 'routeDiscoveryTableIndex'==NWK_ROUTE_DISCOVERY_TABLE_SIZE
if (routeDiscoveryTableIndex < NWK_ROUTE_DISCOVERY_TABLE_SIZE)
{
//indicate that finding out the matched entry in the route discovery table,also indicates that we are in the process of discovering this route
//Update the time stamp, in case two nodes are trying to discover the same route, so we do not time out too soon on one of them.
routeDiscoveryTablePointer[routeDiscoveryTableIndex]->timeStamp=getCurrentime();
//justify if the new path is better than the old path.
if(rreq.pathCost<=routeDiscoveryTablePointer[routeDiscoveryTableIndex]->forwardCost)
{
//modify the corresponding fields in the matched entry in the route discovery table
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))))
///also can use the mcpsDataIndication.pMsdu[10] contacts mcpsDataIndication.pMsdu[11] to replace the rreq.destinationAddress
{
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -