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

📄 mt_zdo.c

📁 Zigbee2006入门(源代码+文档讲解+系统推荐)
💻 C
📖 第 1 页 / 共 3 页
字号:
      devAddr.addr.shortAddr = BUILD_UINT16( pData[1], pData[0] );
      ret = (byte)ZDP_MgmtPermitJoinReq( &devAddr, pData[2], pData[3], false );
      break;
#endif


#if defined ( ZDO_USERDESCSET_REQUEST )
    case SPI_CMD_ZDO_USER_DESC_SET:
      // destination address
      devAddr.addrMode = Addr16Bit;
      devAddr.addr.shortAddr = BUILD_UINT16( pData[1], pData[0] );
      pData += 2;

      // Network address of interest
      shortAddr = BUILD_UINT16( pData[1], pData[0] );
      pData += 2;

      // User descriptor
      userDesc.len = *pData++;
      osal_memcpy( userDesc.desc, pData, userDesc.len );
      pData += 16;  // len of user desc

      ret =(byte)ZDP_UserDescSet( &devAddr, shortAddr, &userDesc, pData[0] );
      break;
#endif

#if defined ( ZDO_ENDDEVICE_ANNCE_REQUEST )
    case SPI_CMD_ZDO_END_DEV_ANNCE:
      // network address
      shortAddr = BUILD_UINT16( pData[1], pData[0] );
      pData += 2;

      // extended address
      ptr = pData;
      MT_ReverseBytes( ptr, Z_EXTADDR_LEN );
      pData += Z_EXTADDR_LEN;

      // security
      attr = *pData++;

      ret = (byte)ZDP_EndDeviceAnnce( shortAddr, ptr, *pData, attr );
      break;
#endif

#if defined (ZDO_SERVERDISC_REQUEST )
    case SPI_CMD_ZDO_SERVERDISC_REQ:
      
      // Service Mask
      uAttr = BUILD_UINT16( pData[1], pData[0] );
      pData += 2;
      attr = *pData++; // Security suite
      
      ret = (byte) ZDP_ServerDiscReq( uAttr, attr );
      break;
#endif
      
#if defined (ZDO_NETWORKSTART_REQUEST )
    case SPI_CMD_ZDO_NETWORK_START_REQ:
      ret = ZDApp_StartUpFromApp( ZDAPP_STARTUP_AUTO );
      break;
    
#endif
    
    default:
      break;
  }

  MT_SendSPIRespMsg( ret, cmd_id, len, respLen );
}

/*********************************************************************
 * Utility FUNCTIONS
 */

/*********************************************************************
 * @fn      zdo_MT_CopyRevExtAddr
 *
 */
byte *zdo_MT_CopyRevExtAddr( byte *dstMsg, byte *addr )
{
  // Copy the 64-bit address
  osal_cpyExtAddr( dstMsg, addr );
  // Reverse byte order
  MT_ReverseBytes( dstMsg, Z_EXTADDR_LEN );
  // Return ptr to next destination location
  return ( dstMsg + Z_EXTADDR_LEN );
}

/*********************************************************************
 * @fn      zdo_MT_MakeExtAddr
 *
 */
byte *zdo_MT_MakeExtAddr( zAddrType_t *devAddr, byte *pData )
{
  // Define a 64-bit address
  devAddr->addrMode = Addr64Bit;
  // Copy and reverse the 64-bit address
  zdo_MT_CopyRevExtAddr( devAddr->addr.extAddr, pData );
  // Return ptr to next destination location
  return ( pData + Z_EXTADDR_LEN );
}

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

#if defined ( ZDO_NWKADDR_REQUEST ) || defined ( ZDO_IEEEADDR_REQUEST )
/*********************************************************************
 * @fn      zdo_MTCB_NwkIEEEAddrRspCB
 *
 * @brief
 *
 *   Called by ZDO when a NWK_addr_rsp message is received.
 *
 * @param  SrcAddr  - Source address
 * @param  Status - response status
 * @param  IEEEAddr - 64 bit IEEE address of device
 * @param  nwkAddr - 16 bit network address of device
 * @param  NumAssocDev - number of associated devices to reporting device
 * @param  AssocDevList - array short addresses of associated devices
 *
 * @return  none
 */
void zdo_MTCB_NwkIEEEAddrRspCB( uint16 type, zAddrType_t *SrcAddr, byte Status,
                               byte *IEEEAddr, uint16 nwkAddr, byte NumAssocDev,
                               byte StartIndex, uint16 *AssocDevList )
{
  byte *pBuf;
  byte *msg;
  byte len;
  byte x;

  /*Allocate a message of size equivalent to the corresponding SPI message
  (plus a couple of bytes for MT use)so that the same buffer can be sent by
  MT to the test tool by simply setting the header bytes.*/

  /*In order to allocate the message , we need to know the length and this
  has to be calculated before we allocate the message*/

  if ( type == SPI_CB_ZDO_NWK_ADDR_RSP )
  {
    len = 1 + Z_EXTADDR_LEN +  1 + Z_EXTADDR_LEN + 2 + 1 + 1 + (2*8);
      // Addrmode + SrcAddr + Status + IEEEAddr + nwkAddr + NumAssocDev + StartIndex
  }
  else
  {
    len = 1 + Z_EXTADDR_LEN +  1 + Z_EXTADDR_LEN + 1 + 1 + (2*8);
      // Addrmode + SrcAddr + Status + IEEEAddr + NumAssocDev + StartIndex
  }

  pBuf = osal_mem_alloc( len );

  if ( pBuf )
  {
    msg = pBuf;

    //First fill in details
    if ( SrcAddr->addrMode == Addr16Bit )
    {
      *msg++ = Addr16Bit;
      for ( x = 0; x < (Z_EXTADDR_LEN - 2); x++ )
        *msg++ = 0;
      *msg++ = HI_UINT16( SrcAddr->addr.shortAddr );
      *msg++ = LO_UINT16( SrcAddr->addr.shortAddr );
    }
    else
    {
      *msg++ = Addr64Bit;
      msg = zdo_MT_CopyRevExtAddr( msg, SrcAddr->addr.extAddr );
    }

    *msg++ = Status;
    msg = zdo_MT_CopyRevExtAddr( msg, IEEEAddr );

    if ( type == SPI_CB_ZDO_NWK_ADDR_RSP )
    {
      *msg++ = HI_UINT16( nwkAddr );
      *msg++ = LO_UINT16( nwkAddr );
    }

    *msg++ = NumAssocDev;
    *msg++ = StartIndex;
    byte cnt = NumAssocDev - StartIndex;

    for ( x = 0; x < 8; x++ )
    {
      if ( x < cnt )
      {
        *msg++ = HI_UINT16( *AssocDevList );
        *msg++ = LO_UINT16( *AssocDevList );
        AssocDevList++;
      }
      else
      {
        *msg++ = 0;
        *msg++ = 0;
      }
    }

    MT_BuildAndSendZToolCB( type, len, pBuf );

    osal_mem_free( pBuf );
  }
}
#endif // ZDO_NWKADDR_REQUEST || ZDO_IEEEADDR_REQUEST

#if defined ( ZDO_NODEDESC_REQUEST )
/*********************************************************************
 * @fn      zdo_MTCB_NodeDescRspCB()
 *
 * @brief
 *
 *   Called by ZDO when a Node_Desc_rsp message is received.
 *
 * @param  SrcAddr  - Source address
 * @param  Status - response status
 * @param  nwkAddr - 16 bit network address of device
 * @param  pNodeDesc - pointer to the devices Node Descriptor
 *                     NULL if Status != ZDP_SUCCESS
 *
 * @return  none
 */
void zdo_MTCB_NodeDescRspCB( zAddrType_t *SrcAddr, byte Status, uint16 nwkAddr,
                             NodeDescriptorFormat_t *pNodeDesc )
{
  byte buf[18];
  byte *msg;

  msg = buf;

  //Fill up the data bytes
  *msg++ = Status;
  *msg++ = HI_UINT16( SrcAddr->addr.shortAddr );
  *msg++ = LO_UINT16( SrcAddr->addr.shortAddr );

  *msg++ = HI_UINT16( nwkAddr );
  *msg++ = LO_UINT16( nwkAddr );

  *msg++ = (byte)(pNodeDesc->LogicalType);

  // Since Z-Tool can't treat V1.0 and V1.1 differently,
  // we just output these two byte in both cases, although
  // in V1.0, they are always zeros.
  *msg++ = (byte) pNodeDesc->ComplexDescAvail;
  *msg++ = (byte) pNodeDesc->UserDescAvail;

  *msg++ = pNodeDesc->APSFlags;
  *msg++ = pNodeDesc->FrequencyBand;
  *msg++ = pNodeDesc->CapabilityFlags;
  *msg++ = pNodeDesc->ManufacturerCode[1];
  *msg++ = pNodeDesc->ManufacturerCode[0];
  *msg++ = pNodeDesc->MaxBufferSize;
  *msg++ = pNodeDesc->MaxTransferSize[1];
  *msg++ = pNodeDesc->MaxTransferSize[0];
  *msg++ = HI_UINT16( pNodeDesc->ServerMask);
  *msg++ = LO_UINT16( pNodeDesc->ServerMask);

  MT_BuildAndSendZToolCB( SPI_CB_ZDO_NODE_DESC_RSP, 18, buf );
}
#endif // ZDO_NODEDESC_REQUEST

#if defined ( ZDO_POWERDESC_REQUEST )
/*********************************************************************
 * @fn      zdo_MTCB_PowerDescRspCB()
 *
 * @brief
 *
 *   Called by ZDO when a Power_Desc_rsp message is received.
 *
 * @param  SrcAddr  - Source address
 * @param  Status - response status
 * @param  nwkAddr - 16 bit network address of device
 * @param  pPwrDesc - pointer to the devices Power Descriptor
 *                     NULL if Status != ZDP_SUCCESS
 *
 * @return  none
 */
void zdo_MTCB_PowerDescRspCB( zAddrType_t *SrcAddr, byte Status,
          uint16 nwkAddr, NodePowerDescriptorFormat_t *pPwrDesc )
{
  byte buf[9];
  byte *msg;

  msg = buf;

  //Fill up the data bytes
  *msg++ = Status;
  *msg++ = HI_UINT16( SrcAddr->addr.shortAddr );
  *msg++ = LO_UINT16( SrcAddr->addr.shortAddr );
  *msg++ = HI_UINT16( nwkAddr );
  *msg++ = LO_UINT16( nwkAddr );

  *msg++ = pPwrDesc->PowerMode;
  *msg++ = pPwrDesc->AvailablePowerSources;
  *msg++ = pPwrDesc->CurrentPowerSource;
  *msg   = pPwrDesc->CurrentPowerSourceLevel;

  MT_BuildAndSendZToolCB( SPI_CB_ZDO_POWER_DESC_RSP, 9, buf );
}
#endif // ZDO_POWERDESC_REQUEST

#if defined ( ZDO_SIMPLEDESC_REQUEST )
#define ZDO_SIMPLE_DESC_CB_LEN  78
/*********************************************************************
 * @fn      zdo_MTCB_SimpleDescRspCB()
 *
 * @brief
 *
 *   Called by ZDO when a Simple_Desc_rsp message is received.
 *
 * @param  SrcAddr  - Source address
 * @param  Status - response status
 * @param  nwkAddr - 16 bit network address of device
 * @param  EPIntf - Endpoint/Interface for description
 * @param  pSimpleDesc - pointer to the devices Simple Descriptor
 *                     NULL if Status != ZDP_SUCCESS
 *
 * @return  none
 */
void zdo_MTCB_SimpleDescRspCB( zAddrType_t *SrcAddr, byte Status,
          uint16 nwkAddr, byte EPIntf, SimpleDescriptionFormat_t *pSimpleDesc )
{
  byte *msgPtr;
  byte *msg;
  byte x;

  msgPtr = osal_mem_alloc( ZDO_SIMPLE_DESC_CB_LEN );
  if ( msgPtr )
  {
    msg = msgPtr;

    //Fill up the data bytes
    *msg++ = Status;
    *msg++ = HI_UINT16( SrcAddr->addr.shortAddr );
    *msg++ = LO_UINT16( SrcAddr->addr.shortAddr );
    *msg++ = HI_UINT16( nwkAddr );
    *msg++ = LO_UINT16( nwkAddr );

    *msg++ = EPIntf;

    *msg++ = HI_UINT16( pSimpleDesc->AppProfId );
    *msg++ = LO_UINT16( pSimpleDesc->AppProfId );
    *msg++ = HI_UINT16( pSimpleDesc->AppDeviceId );
    *msg++ = LO_UINT16( pSimpleDesc->AppDeviceId );

    *msg++ = pSimpleDesc->AppDevVer;
    *msg++ = pSimpleDesc->Reserved;

    *msg++ = pSimpleDesc->AppNumInClusters;
    // ZTool supports 16 bits the code has taken it into account      
    for ( x = 0; x < 16; x++ )
    {
      if ( x < pSimpleDesc->AppNumInClusters )
      {
        *msg++ = HI_UINT16( pSimpleDesc->pAppInClusterList[x]);
        *msg++ = LO_UINT16( pSimpleDesc->pAppInClusterList[x]);
      }
      else
      {
        *msg++ = 0;
        *msg++ = 0;
      }
    }
    *msg++ = pSimpleDesc->AppNumOutClusters;

    for ( x = 0; x < 16; x++ )
    {
      if ( x < pSimpleDesc->AppNumOutClusters )
      {
        *msg++ = HI_UINT16( pSimpleDesc->pAppOutClusterList[x]);
        *msg++ = LO_UINT16( pSimpleDesc->pAppOutClusterList[x]);
      }
      else
      {
        *msg++ = 0;
        *msg++ = 0;
      }
    }

    MT_BuildAndSendZToolCB( SPI_CB_ZDO_SIMPLE_DESC_RSP, ZDO_SIMPLE_DESC_CB_LEN, msgPtr );

    osal_mem_free( msgPtr );
  }
}
#endif // ZDO_SIMPLEDESC_REQUEST

#if defined ( ZDO_ACTIVEEP_REQUEST ) || defined ( ZDO_MATCH_REQUEST )
/*********************************************************************
 * @fn      zdo_MTCB_ActiveEPRspCB()
 *
 * @brief
 *
 *   Called by ZDO when a Active_EP_rsp or Match_Desc_rsp message is received.
 *
 * @param  SrcAddr  - Source address
 * @param  Status - response status
 * @param  nwkAddr - Device's short address that this response describes
 * @param  epIntfCnt - number of epIntfList items
 * @param  epIntfList - array of active endpoint/interfaces.
 *
 * @return  none
 */
void zdo_MTCB_MatchActiveEPRspCB( uint16 type, zAddrType_t *SrcAddr, byte Status,
                  uint16 nwkAddr, byte epIntfCnt, byte *epIntfList )
{
  byte buf[22];
  byte *msg;
  byte x;

  msg = buf;

  //Fill up the data bytes
  *msg++ = Status;
  *msg++ = HI_UINT16( SrcAddr->addr.shortAddr );
  *msg++ = LO_UINT16( SrcAddr->addr.shortAddr );
  *msg++ = HI_UINT16( nwkAddr );
  *msg++ = LO_UINT16( nwkAddr );

  *msg++ = epIntfCnt;

  for ( x = 0; x < 16; x++ )
  {
    if ( x < epIntfCnt )
      *msg++ = *epIntfList++;
    else
      *msg++ = 0;
  }

  MT_BuildAndSendZToolCB( type, 22, buf );
}
#endif // ZDO_ACTIVEEP_REQUEST || ZDO_MATCH_REQUEST

#if defined ( ZDO_BIND_UNBIND_REQUEST ) || defined ( ZDO_ENDDEVICEBIND_REQUEST )
/*********************************************************************
 * @fn      zdo_MTCB_BindRspCB()
 *
 * @brief
 *
 *   Called to send MT callback response for binding responses
 *
 * @param  type - binding type (end device, bind, unbind)
 * @param  SrcAddr  - Source address
 * @param  Status - response status
 *
 * @return  none
 */
void zdo_MTCB_BindRspCB( uint16 type, zAddrType_t *SrcAddr, byte Status )
{
  byte buf[3];
  buf[0] = Status;
  buf[1] = HI_UINT16( SrcAddr->addr.shortAddr );
  buf[2] = LO_UINT16( SrcAddr->addr.shortAddr );
  MT_BuildAndSendZToolCB( type, 3, buf );
}
#endif // ZDO_BIND_UNBIND_REQUEST || ZDO_ENDDEVICEBIND_REQUEST

#if defined ( ZDO_MGMT_LQI_REQUEST )
/*********************************************************************
 * @fn      zdo_MTCB_MgmtLqiRspCB()
 *
 * @brief
 *
 *   Called to send MT callback response for Management LQI response
 *
 * @param  type - binding type (end device, bind, unbind)
 * @param  SrcAddr  - Source address
 * @param  Status - response status
 *
 * @return  none
 */
void zdo_MTCB_MgmtLqiRspCB( uint16 SrcAddr, byte Status, byte NeighborLqiEntries,
                            byte StartIndex, byte NeighborLqiCount,
                            neighborLqiItem_t *pList )
{
  byte *msgPtr;
  byte *msg;
  byte len;
  byte x;
  byte proVer = NLME_GetProtocolVersion();  
  
  /*Allocate a message of size equivalent to the corresponding SPI message
  (plus a couple of bytes for MT use)so that the same buffer can be sent by
  MT to the test tool by simply setting the header bytes.*/

  /*In order to allocate the message , we need to know the length and this
  has to be calculated before we allocate the message*/

  len = 2 + 1 + 1 + 1 + 1 + (ZDP_NEIGHBORLQI_SIZE * ZDO_MAX_LQI_ITEMS );

⌨️ 快捷键说明

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