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

📄 zdapp.c

📁 Zigbee2006入门源代码,包括了Zigbee的入门介绍,和源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
 */
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;
      SSP_WriteNwkActiveKey( &keyItems );
      ret = true;
    }
    nwkFrameCounterChanges = MAX_NWK_FRAMECOUNTER_CHANGES; // Force a save for the first
  }
  return ( ret );
}
#endif

/*********************************************************************
 * @fn      ZDApp_SendEndDeviceBindReq()
 *
 * @brief
 *
 *   This function will look up the endpoint description
 *   and send an End Device Bind Request message.
 *
 * @param  endPoint - Endpoint to auto find
 *
 * @return  none
 */
void ZDApp_SendEndDeviceBindReq( byte endPoint )
{
#if defined ( ZDO_ENDDEVICEBIND_REQUEST )
  zAddrType_t dstAddr;
  SimpleDescriptionFormat_t *sDesc;
  byte free;
#if defined( LCD_SUPPORTED )
   afStatus_t stat;
#endif

  if ( (endPoint == ZDO_EP) || (endPoint > MAX_ENDPOINTS) )
  {
    return;   // Can't do for ZDO
  }

  HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );

  free = afFindSimpleDesc( &sDesc, endPoint );
  if ( sDesc != NULL )
  {
    dstAddr.addrMode = Addr16Bit;
    dstAddr.addr.shortAddr = 0;   // Zigbee Coordinator

#if defined( LCD_SUPPORTED )
    stat =
#endif
    ZDP_EndDeviceBindReq( &dstAddr,
#if defined ( REFLECTOR  )
                // We have a reflector, so tell the coordinator to send
                // binding messages this way
                NLME_GetShortAddr(),
#else
                // tell the coordinator to store if available
                NWK_PAN_COORD_ADDR,
#endif
                endPoint,sDesc->AppProfId,
                sDesc->AppNumOutClusters, sDesc->pAppOutClusterList,
                sDesc->AppNumInClusters, sDesc->pAppInClusterList,
                0 );

    if ( free )
    {
      osal_mem_free( sDesc );
    }

#if defined( LCD_SUPPORTED )
    if ( stat == ZSuccess )
    {
      HalLcdWriteScreen( "End Device Bind", "Sent" );
    }
    else
    {
      HalLcdWriteScreen( "End Device Bind", "Not Sent" );
    }
#endif
  }
  else
  {
#if defined( LCD_SUPPORTED )
    HalLcdWriteScreen( "End Device Bind", "EP not found" );
#endif
  }
#endif // ZDO_ENDDEVICEBIND_REQUEST
}

/*********************************************************************
 * @fn      ZDApp_AutoFindDestination()
 *
 * @brief
 *
 *   This function will try to find the Input Match for this device's
 *   (endpoint passed in) outputs.
 *
 * @param  endPoint - Endpoint to auto find
 * @param  task_id  - task ID override, if NULL use endpoint desc's
 *                    task_id
 *
 * @return  none
 */
void ZDApp_AutoFindDestinationEx( byte endPoint, uint8 *task_id )
{
#if defined ( ZDO_MATCH_REQUEST )
  zAddrType_t dstAddr;
  SimpleDescriptionFormat_t *sDesc;
  endPointDesc_t *tmpDesc;
#if defined( LCD_SUPPORTED )
  afStatus_t stat;
#endif

  if ( endPoint == ZDO_EP )
    return;   // Can't do for ZDO

  HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );

  tmpDesc = afFindEndPointDesc( endPoint );
  if ( tmpDesc )
  {
    if ( ZDApp_AutoFindMode_epDesc == NULL )
    {
      ZDApp_AutoFindMode_epDesc = (endPointDesc_t *)osal_mem_alloc( sizeof ( endPointDesc_t ) );
    }

    if ( ZDApp_AutoFindMode_epDesc )
    {
      osal_memcpy( ZDApp_AutoFindMode_epDesc, tmpDesc, sizeof( endPointDesc_t ) );
      if ( task_id )
      {
        // Override the Task ID, if needed.
        ZDApp_AutoFindMode_epDesc->task_id = task_id;
      }

      sDesc = ZDApp_AutoFindMode_epDesc->simpleDesc;

      // This message is sent to everyone
      dstAddr.addrMode = AddrBroadcast;
      dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;

  #if defined( LCD_SUPPORTED )
      stat =
  #endif
      ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR, sDesc->AppProfId,
                        sDesc->AppNumOutClusters, sDesc->pAppOutClusterList,
                        sDesc->AppNumInClusters, sDesc->pAppInClusterList, 0 );

  #if defined( LCD_SUPPORTED )
      if ( stat == ZSuccess )
      {
        HalLcdWriteScreen( "Auto Find", "Sent" );
        MatchRsps = 0;
      }
      else
      {
        HalLcdWriteScreen( "Auto Find", "Not Sent" );
      }
  #endif
    }
  }
  else
  {
#if defined( LCD_SUPPORTED )
    HalLcdWriteScreen( "Auto Find", "EP not found" );
#endif
  }
#endif // ZDO_MATCH_REQUEST
}

/*********************************************************************
 * @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
 */

#if defined ( ZDO_COORDINATOR )
/*********************************************************************
 * @fn      ZDApp_EndDeviceBindReqCB()
 *
 * @brief
 *
 *   Called by ZDO when an End Device Bind Request message is received.
 *
 * @param  bindReq  - binding request information
 * @param  SecurityUse - Security enable/disable
 *
 * @return  none
 */
void ZDApp_EndDeviceBindReqCB( ZDEndDeviceBind_t *bindReq )
{
#if defined ( COORDINATOR_BINDING )
  if ( bindReq->localCoordinator == 0x0000 )
  {
    ZDO_DoEndDeviceBind( bindReq );
  }
  else
#endif // COORDINATOR_BINDING
  {
    ZDO_MatchEndDeviceBind( bindReq );
  }
#if defined( LCD_SUPPORTED )

⌨️ 快捷键说明

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