Computer Laboratory

Security Group

Routing in XMesh

While analysing code of existing implementations, we were also interested in the changes introduced into routing. There are several pieces of code that are of particular interest here.

selectRoute function

command result_t RouteSelect.selectRoute(TOS_MsgPtr Msg, uint8_t id, uint8_t resend)
{         
  Y+1 = Msg 
  Y+3  = id
  Y+4 = flag 
  Y+5 pMHMsg = Msg->data[0]
  Result = 1
            
  if (resend & UPDATE_TBL){
    updateDescendant(pMHMsg->sourceaddr, pMHMsg->originaddr);
  }         
  if (id == DOWNSTREAM){
    if (gpCurrentParent==NULL){
      if (pMHMsg->sourceaddr==TOS_LOCAL_ADDRESS)&&(pMHMsg->originaddr==TOS_LOCAL_ADDRESS){
         pMHMsg->seqNo = next_sequence_number();
         Msg->addr = TOS_BCAST_ADDR;
         //0x32d6
      } else {
        Result=0;
        //0x32d6
      }
    } else { 0x31e6
      Msg->addr = gpCurrentParent;
      if (gpCurrentParent->id != 0){
         if (flag & RESEND)
           resend |= FORCE_MONITOR;
      } //0x3220
      if ((gpCurrentParent!=TOS_UART_ADDR)&&(resend&FORCE_MONITOR)){
          pMHMsg->seqNo = next_sequence_number();
          //0x32d6
      } else {//0x3250
        if (gpCurrentParent->id==0){
          pMHMsg->seqNo=0;
          //0x32d6
        } else { //0x326a
          pMHMsg->seqNo = 0;
          //0x32d6
        }
      }
    } else { //0x3274
      if (id==UPSTREAM){
        if (resend&0x8){
          pMHMsg->seqNo=next_sequence_number();
          //0x32d6
        } else {
          var7=findDescendantEntry(pMHMsg->originaddr);
          if (var7!=-1){
            Msg->addr=getdestbl_from(var7);
            pMHMsg->seqNo=next_sequence_number();
            //0x32d6
          } else //0x32d4
            Result=FAIL;
            //0x32d6
        }
      } // 0x32d6
      pMHMsg->sourceaddr=TOS_LOCAL_ADDR;
     return Result;
  }

chooseParent function

command result_t MetricEwmaM.chooseParent()
{
long int ulNbrLinkCost, ulNbrTotalCost;
long int oldParentCost, oldParentLinkCost, ulMinTotalCost;
  
ulNbrLinkCost = -1;
ulNbrTotalCost = -1;
oldParentCost = -1;
oldParentLinkCost = -1;
ulMinTotalCost = -1; 
  
pNewParent = NULL;
pOldParent = NULL;

if (TOS_LOCAL_ADDRESS==0)
   return 
    
if (gpCurrentParent!=NULL)
    if (gpCurrentParent->flags & NBRFLAG_EST_HOLD)
       return;
pTable = get_nbrtbl_addr(0);
counter=0;
while (!counter > gRouteTableSize){
  if (counter>0)
    pTable++;
  if (pTable-> flags&NBRFLAG_VALID){
    if (pTable->flag&NBRFLAG_VALID) && (pTable->parent != TOS_LOCAL_ADDRESS)
        &&(pTable->parent!=-1) && (pTable->cost !=-1)
        && (pTable->childLiveliness==0))
       computeCost(pTable->cost, pTable->sendEst, pTable->receiveEst,
                   ulNbrLinkCost, ulNbrTotalCost);
       if (pTable==gpCurrentParent){
         pOldParent=pTable;
         oldParentCost=ulNbrTotalCost;
         oldParentLinkCost=ulNbrLinkCost;
       } else {
         if (ulMinTotalCost > ulNbrTotalCost){
           ulMinTotalCost = ulNbrTotalCost;
           pNewParent = pTable;
         }
       }
  }
  counter++;
}

if (pNewParent == NULL){
  pNewParent = gpCurrentParent;
  ulMinTotalCost = 0x60;  // 96
} else {
  if ((pOldParent != NULL) && (ulMinTotalCost + 0x180 > oldParentCost)){
      pNewParent = pOldParent;
      ulMinTotalCost = oldParentCost;
  }
}

if (pNewParent != NULL)
  atomic {
    gpCurrentParent = pNewParent;
    gpCurrentHopCount = pNewParent->hops;
    gpCurrentCost = gpCurrentCost >> 6;
    fast_formation = 0;
  }
else
  atomic {
    gbCurrentHopCount=255;
    gpCurrentCost = 0x00ff;
    fast_formation=1;
  }
}