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.

CC2652P: "bdb_TCAddJoiningDevice" should be after than "ZDSecMgrDeviceJoin"

Part Number: CC2652P

On the Coordinator's Processing, In function "ZDSecMgrDeviceJoinDirect",  "bdb_TCAddJoiningDevice" is executed after "ZDSecMgrDeviceJoin". But in function "ZDSecMgrUpdateDeviceInd", "bdb_TCAddJoiningDevice" is executed before than "ZDSecMgrDeviceJoin".

ZDSecMgrUpdateDeviceInd can be fixed like this.

void ZDSecMgrUpdateDeviceInd( ZDO_UpdateDeviceInd_t* ind )
{
  ZDSecMgrDevice_t device;
#if (ZG_BUILD_COORDINATOR_TYPE)
  uint8_t tcJoin = FALSE;  //fixed by luoyiming, 2019-10-22
#endif

  device.nwkAddr    = ind->devAddr;
  device.extAddr    = ind->devExtAddr;
  device.parentAddr = ind->srcAddr;
  device.devStatus  = DEV_SEC_INIT_STATUS;
  device.secure     = FALSE;

  // Trust Center should identify the type of JOIN/REJOIN and
  // Transport the NWK key accordingly, it will only be transported for:
  //              APSME_UD_STANDARD_UNSECURED_JOIN
  //   OR         APSME_UD_STANDARD_TRUST_CENTER_REJOIN
  if ( ind->status != APSME_UD_DEVICE_LEFT )
  {
    if ( ind->status == APSME_UD_STANDARD_SECURED_REJOIN )
    {
      device.devStatus &= ~DEV_SEC_INIT_STATUS;
      device.devStatus |=  DEV_SEC_AUTH_STATUS;
      device.secure = TRUE;
    }
    else if ( ind->status == APSME_UD_STANDARD_TRUST_CENTER_REJOIN )
    {
      device.devStatus &= ~DEV_SEC_INIT_STATUS;
      device.devStatus |=  DEV_SEC_AUTH_TC_REJOIN_STATUS;
      device.secure = FALSE;
    }
    else
    {
#if (ZG_BUILD_COORDINATOR_TYPE)
      uint8_t  found;
      uint16_t keyNvIndex, index;
      APSME_TCLinkKeyNVEntry_t TCLKDevEntry;

      keyNvIndex = APSME_SearchTCLinkKeyEntry(device.extAddr,&found, &TCLKDevEntry);

      //If found and it was verified, then allow it to join in a fresh state by erasing the key entry
      if((found == TRUE) && (TCLKDevEntry.keyAttributes == ZG_VERIFIED_KEY))
      {
        TCLKDevEntry.keyAttributes = ZG_DEFAULT_KEY;
        //Increase the shift by one. Validate the maximum shift of the seed which is 15
        TCLKDevEntry.SeedShift_IcIndex++;
        TCLKDevEntry.SeedShift_IcIndex &= 0x0F;

        TCLKDevEntry.rxFrmCntr = 0;
        TCLKDevEntry.txFrmCntr = 0;

        index = keyNvIndex;
        TCLinkKeyRAMEntry[index].rxFrmCntr = 0;
        TCLinkKeyRAMEntry[index].txFrmCntr = 0;

        //Update the entry
        osal_nv_write_ex(ZCD_NV_EX_TCLK_TABLE, keyNvIndex,
                         sizeof(APSME_TCLinkKeyNVEntry_t),
                         &TCLKDevEntry);
      }

      //old bdb_TCAddJoiningDevice
      tcJoin = TRUE; //TC Add Joining device after ZDSecMgrDeviceJoin, fixed by luoyiming 2019-10-22
#endif
    }

    if( ZSuccess == ZDSecMgrDeviceJoin( &device ) )
    {
#if (ZG_BUILD_COORDINATOR_TYPE)
      //TC Add Joining device after ZDSecMgrDeviceJoin, fixed by luoyiming 2019-10-22
      if( tcJoin == TRUE )
      {
        bdb_TCAddJoiningDevice(device.parentAddr,device.extAddr);
      }
#endif
    }
  }
  else
  {
      // remove the TCLK NV entry for a device which has left the network
#if (ZG_BUILD_COORDINATOR_TYPE)
      uint8_t  found;
      uint16_t keyNvIndex, index;
      APSME_TCLinkKeyNVEntry_t TCLKDevEntry;

      keyNvIndex = APSME_SearchTCLinkKeyEntry(device.extAddr,&found, &TCLKDevEntry);

      //If found and it was verified, erase the key entry
      if((found == TRUE) && (TCLKDevEntry.keyAttributes == ZG_VERIFIED_KEY))
      {
        memset(&TCLKDevEntry,0,sizeof(APSME_TCLinkKeyNVEntry_t));
        TCLKDevEntry.keyAttributes = ZG_DEFAULT_KEY;

        index = keyNvIndex;
        TCLinkKeyRAMEntry[index].rxFrmCntr = 0;
        TCLinkKeyRAMEntry[index].txFrmCntr = 0;
        TCLinkKeyRAMEntry[index].entryUsed = FALSE;

        //Update the entry
        osal_nv_write_ex(ZCD_NV_EX_TCLK_TABLE, keyNvIndex,
                         sizeof(APSME_TCLinkKeyNVEntry_t),
                         &TCLKDevEntry);
      }
#endif
    // execute devie leave notify callback, add by luoyiming
    if( NULL != pZDSecMgrDeviceLeaveNotifyCallback )
    {
      pZDSecMgrDeviceLeaveNotifyCallback( device.nwkAddr, device.extAddr, device.parentAddr );
    }
  }
}