This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2530: BugFix : The NWK frame counter not incremented on reset

Part Number: CC2530
Other Parts Discussed in Thread: SIMPLELINK-CC13X2-26X2-SDK, Z-STACK

Hi,

Zstack Version 3.0.2

Problem : On every reset coordinators network frame counter would be set 1250, 

routers would stop responding to the packets from the coordinator with frame counter  lower than last processed message.

Based on  's recommendation in the this post i tried debugging and found a bug in the following function:

void ZDApp_RestoreNwkSecMaterial(void)
{
  uint8 Found = FALSE;
  uint8 i;
  nwkSecMaterialDesc_t nwkSecMaterialDesc;
  uint8 UpdateFrameCounter = FALSE;

  //Search if we do have security material for this network
  for( i = 0; i < gMAX_NWK_SEC_MATERIAL_TABLE_ENTRIES; i++)
  {
    osal_nv_read(ZCD_NV_NWK_SEC_MATERIAL_TABLE_START + i,0,sizeof(nwkSecMaterialDesc_t),&nwkSecMaterialDesc);
    {
      if(osal_memcmp(_NIB.extendedPANID,nwkSecMaterialDesc.extendedPanID,Z_EXTADDR_LEN))
      {
        UpdateFrameCounter = TRUE;
        Found = TRUE;
        break;
      }
    }
  }    
  //Check if we do have frame counter stored in the generic
  if(!Found)
  {
    //The last entry readed has the Generic item, thefore, no need to read it again
    if(nwkSecMaterialDesc.FrameCounter)
    {
      UpdateFrameCounter = TRUE;
    }
  }  

  if(UpdateFrameCounter && (!FrameCounterUpdated))
  {
    FrameCounterUpdated = TRUE;
    
    // Increment the frame counter stored in NV
    nwkSecMaterialDesc.FrameCounter += ( MAX_NWK_FRAMECOUNTER_CHANGES +
                              NWK_FRAMECOUNTER_CHANGES_RESTORE_DELTA );
    
    nwkFrameCounter = nwkSecMaterialDesc.FrameCounter;
    
    osal_nv_write(ZCD_NV_NWK_SEC_MATERIAL_TABLE_START + i,0,sizeof(nwkSecMaterialDesc_t),&nwkSecMaterialDesc);//       <<<<<--------
    
    nwkFrameCounterChanges = 0;
  }
  return;
}

In this function in for loop since 'i' is incremented to "gMAX_NWK_SEC_MATERIAL_TABLE_ENTRIES "  

So osal_nv_write ( line 41) is done to an uninitialized NV ID, it fails and the frame counter remains the same on each reset

Bug Fix:

Instead NV write should be done on the last element of the table which contains the generic security material i.e. "ZCD_NV_NWK_SEC_MATERIAL_TABLE_START + i - 1"

as below

osal_nv_write(ZCD_NV_NWK_SEC_MATERIAL_TABLE_START + i - 1,0,sizeof(nwkSecMaterialDesc_t),&nwkSecMaterialDesc);

It worked for me.

Just need the confirmation if it correct or am i missing something here?

Thanks

  • Hi Aviral,

    Nice catch, this has already been resolved in the SIMPLELINK-CC13X2-26X2-SDK Z-Stack solution but did not propagate to Z-Stack 3.0.2, your solution is suitable but the recommended fix is as follows:

      //Check if we do have frame counter stored in the generic
      if(!Found)
      {
        // If we did not find the current network in NV, overwrite the generic entry,
        // which is the last entry in the table.
        if(nwkSecMaterialDesc.FrameCounter)
        {
          UpdateFrameCounter = TRUE;
          i = (gMAX_NWK_SEC_MATERIAL_TABLE_ENTRIES - 1);  // ADD THIS LINE
        }
      }

    I've updated the Z-Stack 3.0.2 Known Issues and Fixes E2E post to include this information.

    Regards,
    Ryan