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

📄 zdapp.c

📁 一些基于IRA环境开发的zigbee实例程序
💻 C
📖 第 1 页 / 共 5 页
字号:

#if ( SECURE != 0 )
/*********************************************************************
 * @fn      ZDApp_SaveNwkKey()
 *
 * @brief   Save off the Network key information.
 *
 * @param   none
 *
 * @return  none
 */
void ZDApp_SaveNwkKey( void )
{
  nwkActiveKeyItems keyItems;

  SSP_ReadNwkActiveKey( &keyItems );
  keyItems.frameCounter++;

  osal_nv_write( ZCD_NV_NWKKEY, 0, sizeof( nwkActiveKeyItems ),
                (void *)&keyItems );

  nwkFrameCounterChanges = 0;
}

/*********************************************************************
 * @fn      ZDApp_ResetNwkKey()
 *
 * @brief   Reset the Network key information in NV.
 *
 * @param   none
 *
 * @return  none
 */
void ZDApp_ResetNwkKey( void )
{
  nwkActiveKeyItems keyItems;

  osal_memset( &keyItems, 0, sizeof( nwkActiveKeyItems ) );
  osal_nv_write( ZCD_NV_NWKKEY, 0, sizeof( nwkActiveKeyItems ),
                (void *)&keyItems );
}
#endif

#if ( SECURE != 0 )
/*********************************************************************
 * @fn      ZDApp_RestoreNwkKey()
 *
 * @brief
 *
 *   Save off the Network key information.
 *
 * @param   none
 *
 * @return  true if restored from NV, false if not
 */
byte ZDApp_RestoreNwkKey( void )
{
  nwkActiveKeyItems keyItems;
  byte ret = false;

  if ( osal_nv_read( ZCD_NV_NWKKEY, 0, sizeof(nwkActiveKeyItems), (void*)&keyItems )
      == ZSUCCESS )
  {
    if ( keyItems.frameCounter > 0 )
    {
      // Restore the key information
      keyItems.frameCounter += MAX_NWK_FRAMECOUNTER_CHANGES;
      nwkFrameCounter = keyItems.frameCounter;
      ret = true;
    }

    // Force a save for the first frame counter increment
    nwkFrameCounterChanges = MAX_NWK_FRAMECOUNTER_CHANGES + 1;
  }
  return ( ret );
}
#endif

/*********************************************************************
 * @fn      ZDApp_ResetTimerStart
 *
 * @brief   Start the reset timer.
 *
 * @param   delay - delay time(ms) before reset
 *
 * @return  none
 */
void ZDApp_ResetTimerStart( uint16 delay )
{
  // Start the rest timer
  osal_start_timerEx( ZDAppTaskID, ZDO_DEVICE_RESET, delay );
}

/*********************************************************************
 * @fn      ZDApp_ResetTimerCancel
 *
 * @brief   Cancel the reset timer.
 *
 * @param   none
 *
 * @return  none
 */
void ZDApp_ResetTimerCancel( void )
{
  // Cancel the reset timer
  osal_stop_timerEx( ZDAppTaskID, ZDO_DEVICE_RESET );
}

/*********************************************************************
 * @fn      ZDApp_LeaveCtrlInit
 *
 * @brief   Initialize the leave control logic.
 *
 * @param   none
 *
 * @return  none
 */
void ZDApp_LeaveCtrlInit( void )
{
  uint8 status;


  // Initialize control state
  ZDApp_LeaveCtrl = ZDAPP_LEAVE_CTRL_INIT;

  status = osal_nv_item_init( ZCD_NV_LEAVE_CTRL,
                              sizeof(ZDApp_LeaveCtrl),
                              &ZDApp_LeaveCtrl );

  if ( status == ZSUCCESS )
  {
    // Read saved control
    osal_nv_read( ZCD_NV_LEAVE_CTRL,
                  0,
                  sizeof( uint8 ),
                  &ZDApp_LeaveCtrl);
  }
}

/*********************************************************************
 * @fn      ZDApp_LeaveCtrlSet
 *
 * @brief   Set the leave control logic.
 *
 * @param   ra - reassociate flag
 *
 * @return  none
 */
void ZDApp_LeaveCtrlSet( uint8 ra )
{
  ZDApp_LeaveCtrl = ZDAPP_LEAVE_CTRL_SET;

  if ( ra == TRUE )
  {
    ZDApp_LeaveCtrl |= ZDAPP_LEAVE_CTRL_RA;
  }

  // Write the leave control
  osal_nv_write( ZCD_NV_LEAVE_CTRL,
                 0,
                 sizeof( uint8 ),
                 &ZDApp_LeaveCtrl);
}

/*********************************************************************
 * @fn      ZDApp_LeaveCtrlBypass
 *
 * @brief   Check if NV restore should be skipped during a leave reset.
 *
 * @param   none
 *
 * @return  uint8 - (TRUE bypass:FALSE do not bypass)
 */
uint8 ZDApp_LeaveCtrlBypass( void )
{
  uint8 bypass;

  if ( ZDApp_LeaveCtrl & ZDAPP_LEAVE_CTRL_SET )
  {
    bypass = TRUE;
  }
  else
  {
    bypass = FALSE;
  }

  return bypass;
}

/*********************************************************************
 * @fn      ZDApp_LeaveCtrlStartup
 *
 * @brief   Check for startup conditions during a leave reset.
 *
 * @param   state      - devState_t determined by leave control logic
 * @param   startDelay - startup delay
 *
 * @return  none
 */
void ZDApp_LeaveCtrlStartup( devStates_t* state, uint16* startDelay )
{
  *startDelay = 0;

  if ( ZDApp_LeaveCtrl & ZDAPP_LEAVE_CTRL_SET )
  {
    if ( ZDApp_LeaveCtrl & ZDAPP_LEAVE_CTRL_RA )
    {
      *startDelay = 5000;
    }
    else
    {
      *state = DEV_HOLD;
    }

    // Set leave control to initialized state
    ZDApp_LeaveCtrl = ZDAPP_LEAVE_CTRL_INIT;

    // Write initialized control
    osal_nv_write( ZCD_NV_LEAVE_CTRL,
                  0,
                  sizeof( uint8 ),
                  &ZDApp_LeaveCtrl);
  }
}

/*********************************************************************
 * @fn      ZDApp_LeaveReset
 *
 * @brief   Setup a device reset due to a leave indication/confirm.
 *
 * @param   ra - reassociate flag
 *
 * @return  none
 */
void ZDApp_LeaveReset( uint8 ra )
{
  ZDApp_LeaveCtrlSet( ra );

  ZDApp_ResetTimerStart( 5000 );
}

/*********************************************************************
 * @fn      ZDApp_LeaveUpdate
 *
 * @brief   Update local device data related to leaving device.
 *
 * @param   nwkAddr        - NWK address of leaving device
 * @param   extAddr        - EXT address of leaving device
 * @param   removeChildren - remove children of leaving device
 *
 * @return  none
 */
void ZDApp_LeaveUpdate( uint16 nwkAddr, uint8* extAddr,
                        uint8 removeChildren )
{
  /*
  AddrMgrEntry_t entry;
  */


  // Remove if child
  NLME_RemoveChild( extAddr, removeChildren );

  /*
  // Set NWK address to invalid
  entry.user    = ADDRMGR_USER_DEFAULT;
  entry.nwkAddr = INVALID_NODE_ADDR;
  AddrMgrExtAddrSet( entry.extAddr, extAddr );
  AddrMgrEntryUpdate( &entry );

  // Check
  if ( removeChildren == TRUE )
  {
    // Set index to INVALID_NODE_ADDR to start search
    entry.index = INVALID_NODE_ADDR;

    // Get first entry
    AddrMgrEntryGetNext( &entry );

    // Remove all descendents
    while ( entry.index != INVALID_NODE_ADDR )
    {
      // Check NWK address allocation algorithm
      if ( RTG_ANCESTOR( entry.nwkAddr, thisAddr ) != 0 )
      {
        // Set NWK address to invalid
        entry.nwkAddr = INVALID_NODE_ADDR;
        AddrMgrEntryUpdate( &entry );
      }

      // Get next entry
      AddrMgrEntryGetNext( &entry );
    }
  }
  */
}

/*********************************************************************
 * CALLBACK FUNCTIONS
 */

/*********************************************************************
 * @fn      ZDApp_SendEventMsg()
 *
 * @brief
 *
 *   Sends a Network Join message
 *
 * @param  cmd - command ID
 * @param  len - length (in bytes) of the buf field
 * @param  buf - buffer for the rest of the message.
 *
 * @return  none
 */
void ZDApp_SendEventMsg( byte cmd, byte len, byte *buf )
{
  ZDApp_SendMsg( ZDAppTaskID, cmd, len, buf );
}

/*********************************************************************
 * @fn      ZDApp_SendMsg()
 *
 * @brief   Sends a OSAL message
 *
 * @param  taskID - Where to send the message
 * @param  cmd - command ID
 * @param  len - length (in bytes) of the buf field
 * @param  buf - buffer for the rest of the message.
 *
 * @return  none
 */
void ZDApp_SendMsg( byte taskID, byte cmd, byte len, byte *buf )
{
  osal_event_hdr_t *msgPtr;

  // Send the address to the task
  msgPtr = (osal_event_hdr_t *)osal_msg_allocate( len );
  if ( msgPtr )
  {
    if ( (len > 0) && (buf != NULL) )
      osal_memcpy( msgPtr, buf, len );

    msgPtr->event = cmd;
    osal_msg_send( taskID, (byte *)msgPtr );
  }
}

/*********************************************************************
 * Call Back Functions from NWK  - API
 */

/*********************************************************************
 * @fn          ZDO_NetworkDiscoveryConfirmCB
 *
 * @brief       This function returns a choice of PAN to join.
 *
 * @param       ResultCount - Number of routers discovered
 * @param               NetworkList - Pointer to list of network descriptors
 *
 * @return      ZStatus_t
 */
#define STACK_PROFILE_MAX 2
ZStatus_t ZDO_NetworkDiscoveryConfirmCB( byte ResultCount,
                                         networkDesc_t *NetworkList )
{
  networkDesc_t *pNwkDesc;
  ZDO_NetworkDiscoveryCfm_t msg;
  byte  i;
  uint8 stackProfile;
  uint8 stackProfilePro;
  uint8 selected;

#if defined ( ZDO_MGMT_NWKDISC_RESPONSE )
  if ( zdappMgmtNwkDiscReqInProgress )
  {
    zdappMgmtNwkDiscReqInProgress = false;
    ZDO_FinishProcessingMgmtNwkDiscReq( ResultCount, NetworkList );
    return ( ZSuccess );
  }
#endif

  // process discovery results
  stackProfilePro = FALSE;
  selected = FALSE;

  for ( stackProfile = 0; stackProfile < STACK_PROFILE_MAX; stackProfile++ )
  {
    pNwkDesc = NetworkList;
    for ( i = 0; i < ResultCount; i++, pNwkDesc = pNwkDesc->nextDesc )
    {
      if ( zgConfigPANID != 0xFFFF )
      {
        // PAN Id is preconfigured. check if it matches
        // only 14 bits of pan id is used
        if ( pNwkDesc->panId != ( zgConfigPANID & 0x3FFF ) )
          continue;
      }

      // check that network is allowing joining
      //------------------------------------------------------------
      #if defined( RTR_NWK )
      //------------------------------------------------------------
      if ( stackProfilePro == FALSE )
      {
        if ( !pNwkDesc->routerCapacity )
        {
          continue;
        }
      }
      else
      {
        if ( !pNwkDesc->deviceCapacity )
        {
          continue;
        }
      }
      //------------------------------------------------------------
      #else
      //------------------------------------------------------------
      if ( !pNwkDesc->deviceCapacity )
      {
        continue;
      }
      //------------------------------------------------------------
      #endif
      //------------------------------------------------------------
      // check version of zigbee protocol
      if ( pNwkDesc->version != _NIB.nwkProtocolVersion )
        continue;

      // check version of stack profile
      if ( pNwkDesc->stackProfile != zgStackProfile  )
      {
        if ( pNwkDesc->stackProfile == ZIGBEEPRO_PROFILE )
          stackProfilePro = TRUE;

        if ( stackProfile == 0 )
        {
          continue;
        }
      }

      // check if beacon order is the right value..
      //  if ( pNwkDesc->beaconOrder < ZDO_CONFIG_MAX_BO )
      //    continue;

      // choose this pan for joining
      break;
    }

    if (i < ResultCount)
    {
     selected = TRUE;
      break;
    }

    // break if selected or stack profile pro wasn't found
    if ( (selected == TRUE) || (stackProfilePro == FALSE) )

⌨️ 快捷键说明

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