⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zdsecmgr.c

📁 用IAR开发的ZIGBEE网络路由例子
💻 C
📖 第 1 页 / 共 5 页
字号:
#endif // defined ( ZDO_COORDINATOR )
#endif // defined ( ZDSECMGR_COMMERCIAL )

#if defined ( ZDO_COORDINATOR )
/******************************************************************************
 * @fn          ZDSecMgrDeviceValidate
 *
 * @brief       Decide whether device is allowed.
 *
 * @param       device - [in] ZDSecMgrDevice_t, device info
 *
 * @return      ZStatus_t
 */
ZStatus_t ZDSecMgrDeviceValidate( ZDSecMgrDevice_t* device )
{
  ZStatus_t status;


  if ( ZDSecMgrPermitJoiningEnabled == TRUE )
  {
    // device may be joining with a secure flag but it is ultimately the Trust
    // Center that decides -- check if expected pre configured device --
    // override settings
    if ( zgPreConfigKeys == TRUE )
    {
        device->secure = TRUE;
    }
    else
    {
        device->secure = FALSE;
    }

    //-------------------------------------------------------------------------
    #if defined ( ZDSECMGR_COMMERCIAL )
    //-------------------------------------------------------------------------
    status = ZDSecMgrDeviceValidateCM( device );
    //-------------------------------------------------------------------------
    #else // defined( ZDSECMGR_RESIDENTIAL )
    //-------------------------------------------------------------------------
    status = ZDSecMgrDeviceValidateRM( device );
    //-------------------------------------------------------------------------
    #endif // defined( ZDSECMGR_RESIDENTIAL )
    //-------------------------------------------------------------------------
  }
  else
  {
    status = ZNwkUnknownDevice;
  }

  return status;
}
#endif // defined ( ZDO_COORDINATOR )

#if defined ( ZDO_COORDINATOR )
/******************************************************************************
 * @fn          ZDSecMgrDeviceJoin
 *
 * @brief       Try to join this device.
 *
 * @param       device - [in] ZDSecMgrDevice_t, device info
 *
 * @return      ZStatus_t
 */
ZStatus_t ZDSecMgrDeviceJoin( ZDSecMgrDevice_t* device )
{
  ZStatus_t status;


  // attempt to validate device
  status = ZDSecMgrDeviceValidate( device );

  if ( status == ZSuccess )
  {
    //-------------------------------------------------------------------------
    #if defined ( ZDSECMGR_COMMERCIAL )
    //-------------------------------------------------------------------------
    ZDSecMgrDeviceCtrlSetup( device );
    //-------------------------------------------------------------------------
    #else // defined( ZDSECMGR_RESIDENTIAL )
    //-------------------------------------------------------------------------
    //send the nwk key data to the joining device
    status = ZDSecMgrSendNwkKey( device );
    //-------------------------------------------------------------------------
    #endif // defined( ZDSECMGR_RESIDENTIAL )
    //-------------------------------------------------------------------------
  }
  else
  {
    // not allowed, remove the device
    ZDSecMgrDeviceRemove( device );
  }

  return status;
}
#endif // defined ( ZDO_COORDINATOR )

#if defined ( ZDO_COORDINATOR )
/******************************************************************************
 * @fn          ZDSecMgrDeviceJoinDirect
 *
 * @brief       Try to join this device as a direct child.
 *
 * @param       device - [in] ZDSecMgrDevice_t, device info
 *
 * @return      ZStatus_t
 */
ZStatus_t ZDSecMgrDeviceJoinDirect( ZDSecMgrDevice_t* device )
{
  ZStatus_t status;

  status = ZDSecMgrDeviceJoin( device );

  if ( status == ZSuccess )
  {
    // set association status to authenticated
    ZDSecMgrAssocDeviceAuth( AssocGetWithShort( device->nwkAddr ) );
  }

  return status;
}
#endif // defined ( ZDO_COORDINATOR )

#if !defined ( ZDO_COORDINATOR ) || defined( SOFT_START )
/******************************************************************************
 * @fn          ZDSecMgrDeviceJoinFwd
 *
 * @brief       Forward join to Trust Center.
 *
 * @param       device - [in] ZDSecMgrDevice_t, device info
 *
 * @return      ZStatus_t
 */
ZStatus_t ZDSecMgrDeviceJoinFwd( ZDSecMgrDevice_t* device )
{
  ZStatus_t               status;
  APSME_UpdateDeviceReq_t req;


  // forward any joining device to the Trust Center -- the Trust Center will
  // decide if the device is allowed to join
  status = ZSuccess;

  //if ( status == ZSuccess )
  //{
    // forward authorization to the Trust Center
    req.dstAddr    = APSME_TRUSTCENTER_NWKADDR;
    req.devAddr    = device->nwkAddr;
    req.devExtAddr = device->extAddr;

    // set security status, option for router to reject if policy set
    if ( device->secure == TRUE )
    {
        req.status = APSME_UD_SECURED_JOIN;
    }
    else
    {
        req.status = APSME_UD_UNSECURED_JOIN;
    }

    // send and APSME_UPDATE_DEVICE request to the trust center
    status = APSME_UpdateDeviceReq( &req );
  //}
  //else
  //{
  //  // not allowed, remove the device
  //  ZDSecMgrDeviceRemove( device );
  //}

  return status;
}
#endif // !defined ( ZDO_COORDINATOR ) || defined ( SOFT_START )

#if defined ( RTR_NWK )
/******************************************************************************
 * @fn          ZDSecMgrDeviceNew
 *
 * @brief       Process a new device.
 *
 * @param       device - [in] ZDSecMgrDevice_t, device info
 *
 * @return      ZStatus_t
 */
ZStatus_t ZDSecMgrDeviceNew( ZDSecMgrDevice_t* joiner )
{
  ZStatus_t status;

  //---------------------------------------------------------------------------
  #if defined ( ZDO_COORDINATOR ) && !defined ( SOFT_START )
  //---------------------------------------------------------------------------
  // try to join this device
  status = ZDSecMgrDeviceJoinDirect( joiner );
  //---------------------------------------------------------------------------
  #elif defined ( ZDO_COORDINATOR ) && defined ( SOFT_START )
  //---------------------------------------------------------------------------
  // which mode -- COORD or ROUTER
  if ( ZDO_Config_Node_Descriptor.LogicalType == NODETYPE_COORDINATOR )
  {
    // try to join this device
    status = ZDSecMgrDeviceJoinDirect( joiner );
  }
  else
  {
    // forward join to Trust Center
    status = ZDSecMgrDeviceJoinFwd( joiner );
  }
  //---------------------------------------------------------------------------
  #else // !ZDO_COORDINATOR
  //---------------------------------------------------------------------------
  // forward join to Trust Center
  status = ZDSecMgrDeviceJoinFwd( joiner );
  //---------------------------------------------------------------------------
  #endif // !ZDO_COORDINATOR
  //---------------------------------------------------------------------------

  return status;
}
#endif // defined ( RTR_NWK )

#if defined ( RTR_NWK )
/******************************************************************************
 * @fn          ZDSecMgrAssocDeviceAuth
 *
 * @brief       Set associated device status to authenticated
 *
 * @param       assoc - [in, out] associated_devices_t
 *
 * @return      none
 */
void ZDSecMgrAssocDeviceAuth( associated_devices_t* assoc )
{
  if ( assoc != NULL )
  {
    assoc->devStatus |= DEV_SEC_AUTH_STATUS;
  }
}
#endif // defined ( RTR_NWK )
#endif // defined ( ZDSECMGR_SECURE )

/******************************************************************************
 * PUBLIC FUNCTIONS
 */
/******************************************************************************
 * @fn          ZDSecMgrInit
 *
 * @brief       Initialize ZigBee Device Security Manager.
 *
 * @param       none
 *
 * @return      none
 */
#if defined ( ZDSECMGR_COMMERCIAL )
#if ( ADDRMGR_CALLBACK_ENABLED == 1 )
void ZDSecMgrAddrMgrCB( uint8           update,
                        AddrMgrEntry_t* newEntry,
                        AddrMgrEntry_t* oldEntry )
{
  (void)update;
  (void)newEntry;
  (void)oldEntry;
}
#endif // ( ADDRMGR_CALLBACK_ENABLED == 1 )
#endif // defined ( ZDSECMGR_COMMERCIAL )

void ZDSecMgrInit( void )
{
  //---------------------------------------------------------------------------
  #if defined ( ZDSECMGR_COMMERCIAL )
  //---------------------------------------------------------------------------
  // initialize sub modules
  ZDSecMgrMasterKeyInit();
  ZDSecMgrEntryInit();
  ZDSecMgrCtrlInit();

  // configure SKKE slot data
  APSME_SKKE_SlotInit( ZDSECMGR_SKKE_SLOT_MAX );

  // register with Address Manager
  #if ( ADDRMGR_CALLBACK_ENABLED == 1 )
  AddrMgrRegister( ADDRMGR_REG_SECURITY, ZDSecMgrAddrMgrCB );
  #endif
  //---------------------------------------------------------------------------
  #endif // defined ( ZDSECMGR_COMMERCIAL )
  //---------------------------------------------------------------------------

  //---------------------------------------------------------------------------
  #if defined ( ZDSECMGR_SECURE ) && defined ( ZDO_COORDINATOR )
  //---------------------------------------------------------------------------
  // setup joining permissions
  ZDSecMgrPermitJoiningEnabled = TRUE;
  ZDSecMgrPermitJoiningTimed   = FALSE;
  //---------------------------------------------------------------------------
  #endif // defined ( ZDSECMGR_SECURE ) && defined ( ZDO_COORDINATOR )
  //---------------------------------------------------------------------------

  // configure security based on security mode and type of device
  ZDSecMgrConfig();
}

/******************************************************************************
 * @fn          ZDSecMgrConfig
 *
 * @brief       Configure ZigBee Device Security Manager.
 *
 * @param       none
 *
 * @return      none
 */
void ZDSecMgrConfig( void )
{
  #if defined ( ZDSECMGR_SECURE )
  SSP_Init();
  #endif

  //---------------------------------------------------------------------------
  #if defined ( ZDSECMGR_COMMERCIAL )
  //---------------------------------------------------------------------------
  {
    #if defined ( ZDO_COORDINATOR )
    {
      #if defined ( SOFT_START )
      {
        //switch here
        if ( ZDO_Config_Node_Descriptor.LogicalType == NODETYPE_COORDINATOR )
        {
          // COMMERCIAL MODE - COORDINATOR DEVICE
          APSME_SecurityCM_CD();
        }
        else
        {
          // COMMERCIAL MODE - ROUTER DEVICE
          APSME_SecurityCM_RD();
        }
      }
      #else
      {
        // COMMERCIAL MODE - COORDINATOR DEVICE
        APSME_SecurityCM_CD();
      }
      #endif
    }
    #elif defined ( RTR_NWK )
    {
      // COMMERCIAL MODE - ROUTER DEVICE
      APSME_SecurityCM_RD();
    }
    #else
    {
      // COMMERCIAL MODE - END DEVICE
      APSME_SecurityCM_ED();
    }
    #endif
  }
  //---------------------------------------------------------------------------
  #elif defined (ZDSECMGR_RESIDENTIAL )
  //---------------------------------------------------------------------------
  {
    #if defined ( ZDO_COORDINATOR )
    {
      #if defined ( SOFT_START )
      {
        //switch here
        if ( ZDO_Config_Node_Descriptor.LogicalType == NODETYPE_COORDINATOR )
        {
          // RESIDENTIAL MODE - COORDINATOR DEVICE
          APSME_SecurityRM_CD();
        }
        else
        {
          // RESIDENTIAL MODE - ROUTER DEVICE
          APSME_SecurityRM_RD();
        }
      }
      #else
      {
        // RESIDENTIAL MODE - COORDINATOR DEVICE
        APSME_SecurityRM_CD();
      }
      #endif
    }
    #elif defined ( RTR_NWK )
    {
      // RESIDENTIAL MODE - ROUTER DEVICE
      APSME_SecurityRM_RD();
    }
    #else
    {
      // RESIDENTIAL MODE - END DEVICE
      APSME_SecurityRM_ED();
    }
    #endif
  }
  //---------------------------------------------------------------------------
  #else
  //---------------------------------------------------------------------------
  {
    // NO SECURITY
    APSME_SecurityNM();
  }
  //---------------------------------------------------------------------------
  #endif
  //---------------------------------------------------------------------------
}

#if defined( ZDO_MGMT_PERMIT_JOIN_RESPONSE ) && defined( RTR_NWK )
/******************************************************************************
 * @fn          ZDSecMgrPermitJoining
 *
 * @brief       Process request to change joining permissions.
 *
 * @param       duration - [in] timed duration for join in seconds
 *                         - 0x00 not allowed
 *                         - 0xFF allowed without timeout
 *
 * @return      uint8 - success(TRUE:FALSE)
 */
uint8 ZDSecMgrPermitJoining( uint8 duration )
{
  //---------------------------------------------------------------------------
  #if defined ( ZDSECMGR_SECURE ) && defined ( ZDO_COORDINATOR )
  //---------------------------------------------------------------------------
  uint8 accept;


  ZDSecMgrPermitJoiningTimed = FALSE;

  if ( duration > 0 )
  {
    ZDSecMgrPermitJoiningEnabled = TRUE;

    if ( duration != 0xFF )
    {
      ZDSecMgrPermitJoiningTimed = TRUE;
    }
  }
  else
  {
    ZDSecMgrPermitJoiningEnabled = FALSE;
  }

  accept = TRUE;

  return accept;
  //---------------------------------------------------------------------------
  #else // !defined ( ZDSECMGR_SECURE ) || !defined ( ZDO_COORDINATOR )
  //---------------------------------------------------------------------------
  return FALSE;
  //---------------------------------------------------------------------------
  #endif // !defined ( ZDSECMGR_SECURE ) || !define

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -