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

📄 zdobject.c

📁 Zigbee2006入门(源代码+文档讲解+系统推荐)
💻 C
📖 第 1 页 / 共 5 页
字号:
    {
      stat = ZDOBuildBuf[0];
    }
  }
#endif
  else
  {
    stat = ZDP_INVALID_REQTYPE;
  }

  if ( cnt != CACHE_EP_MAX )
  {
    ZDP_ActiveEPRsp( seq, src, stat, aoi, cnt, (uint8 *)ZDOBuildBuf, sty );
  }
  else
  {
    ZDP_GenericRsp( seq, src, ZDP_NOT_SUPPORTED, aoi, Active_EP_rsp, sty );
  }
}

/*********************************************************************
 * @fn          ZDO_ProcessMatchDescReq
 *
 * @brief       This function processes and responds to the
 *              Match_Desc_req message.
 *
 * @param       src  - Source address
 * @param       msg - input message containing search material
 * @param       sty - Security enable/disable
 *
 * @return      none
 */
void ZDO_ProcessMatchDescReq( byte seq, zAddrType_t *src, byte *msg, byte sty )
{
  byte epCnt = 0;
  byte numInClusters;
  uint16 *inClusters;
  byte numOutClusters;
  uint16 *outClusters;
  epList_t *epDesc;
  SimpleDescriptionFormat_t *sDesc = NULL;
  uint8 allocated;

  // Parse the incoming message
  uint16 aoi = BUILD_UINT16( msg[0], msg[1] );
  uint16 profileID = BUILD_UINT16( msg[2], msg[3] );
  msg += 4;
  numInClusters = *msg++;
  inClusters = NULL;
  if (numInClusters)  {
    if (!(inClusters=ZDO_CreateAlignedUINT16List(numInClusters, msg)))  {
      // can't allocate memory. drop message
      return;
    }
  }
  msg += numInClusters*sizeof(uint16);

  numOutClusters = *msg++;
  outClusters = NULL;
  if (numOutClusters)  {
    if (!(outClusters=ZDO_CreateAlignedUINT16List(numOutClusters, msg)))  {
      // can't allocate memory. drop message
      if (inClusters) {
        osal_mem_free(inClusters);
      }
      return;
    }
  }
  msg += numOutClusters*sizeof(uint16);

  if ( NWK_BROADCAST_SHORTADDR_DEVALL == aoi )
  {
#if defined( ZDO_CACHE ) && ( CACHE_DEV_MAX > 0 )
    if ( CACHE_SERVER )
    {
      ZDCacheProcessMatchDescReq( seq, src, numInClusters, inClusters,
                            numOutClusters, outClusters, profileID, aoi, sty );
    }
#endif
  }
  else if ( ADDR_BCAST_NOT_ME == NLME_IsAddressBroadcast(aoi) )
  {
    ZDP_MatchDescRsp( seq, src, ZDP_INVALID_REQTYPE,
                                   ZDAppNwkAddr.addr.shortAddr, 0, NULL, sty );
    if (inClusters)  {
      osal_mem_free(inClusters);
    }
    if (outClusters)  {
      osal_mem_free(outClusters);
    }
    return;
  }
  else if ( (ADDR_NOT_BCAST == NLME_IsAddressBroadcast(aoi)) && (aoi != ZDAppNwkAddr.addr.shortAddr) )
  {
#if defined( ZDO_CACHE ) && ( CACHE_DEV_MAX > 0 )
    if ( CACHE_SERVER )
    {
      ZDCacheProcessMatchDescReq( seq, src, numInClusters, inClusters,
                            numOutClusters, outClusters, profileID, aoi, sty );
    }
#else
    ZDP_MatchDescRsp( seq, src, ZDP_INVALID_REQTYPE,
                                   ZDAppNwkAddr.addr.shortAddr, 0, NULL, sty );
#endif
    if (inClusters)  {
      osal_mem_free(inClusters);
    }
    if (outClusters)  {
      osal_mem_free(outClusters);
    }
    return;
  }

  // First count the number of endpoints that match.
  epDesc = epList;
  while ( epDesc )
  {
    // Don't search endpoint 0 and check if response is allowed
    if ( epDesc->epDesc->endPoint != ZDO_EP && (epDesc->flags&eEP_AllowMatch) )
    {
      if ( epDesc->pfnDescCB )
      {
        sDesc = (SimpleDescriptionFormat_t *)epDesc->pfnDescCB( AF_DESCRIPTOR_SIMPLE, epDesc->epDesc->endPoint );
        allocated = TRUE;
      }
      else
      {
        sDesc = epDesc->epDesc->simpleDesc;
        allocated = FALSE;
      }

      if ( sDesc && sDesc->AppProfId == profileID )
      {
        uint8 *uint8Buf = (uint8 *)ZDOBuildBuf;

        // If there are no search input/ouput clusters - respond
        if ( ((numInClusters == 0) && (numOutClusters == 0))
            // Are there matching input clusters?
             || (ZDO_AnyClusterMatches( numInClusters, inClusters,
                  sDesc->AppNumInClusters, sDesc->pAppInClusterList ))
            // Are there matching output clusters?
             || (ZDO_AnyClusterMatches( numOutClusters, outClusters,
                  sDesc->AppNumOutClusters, sDesc->pAppOutClusterList ))     )
        {         
          // Notify the endpoint of the match.
          uint8 bufLen = sizeof( ZDO_MatchDescRspSent_t ) + (numOutClusters + numInClusters) * sizeof(uint16);
          ZDO_MatchDescRspSent_t *pRspSent = (ZDO_MatchDescRspSent_t *) osal_msg_allocate( bufLen );

          if (pRspSent)
          {
            pRspSent->hdr.event = ZDO_MATCH_DESC_RSP_SENT;
            pRspSent->nwkAddr = src->addr.shortAddr;
            pRspSent->numInClusters = numInClusters;
            pRspSent->numOutClusters = numOutClusters;

            if (numInClusters)
            {
              pRspSent->pInClusters = (uint16*) (pRspSent + 1);
              osal_memcpy(pRspSent->pInClusters, inClusters, numInClusters * sizeof(uint16));
            }
            else 
            {
              pRspSent->pInClusters = NULL;
            }
             
            if (numOutClusters)
            {
              pRspSent->pOutClusters = (uint16*)(pRspSent + 1) + numInClusters;
              osal_memcpy(pRspSent->pOutClusters, outClusters, numOutClusters * sizeof(uint16));
            }
            else 
            {
              pRspSent->pOutClusters = NULL;
            }
          
            osal_msg_send( *epDesc->epDesc->task_id, (uint8 *)pRspSent );
          }

          uint8Buf[epCnt++] = sDesc->EndPoint;
        }
      }

      if ( allocated )
        osal_mem_free( sDesc );
    }
    epDesc = epDesc->nextDesc;
  }

  // Send the message only if at least one match found.
  if ( epCnt )
  {
    if ( ZSuccess == ZDP_MatchDescRsp( seq, src, ZDP_SUCCESS,
                       ZDAppNwkAddr.addr.shortAddr, epCnt, (uint8 *)ZDOBuildBuf, sty ) )
    {
#if defined( LCD_SUPPORTED )
      HalLcdWriteScreen( "Match Desc Req", "Rsp Sent" );
#endif
    }
  }
  else
  {
#if defined( LCD_SUPPORTED )
    HalLcdWriteScreen( "Match Desc Req", "Non Matched" );
#endif
  }
  if (inClusters)  {
    osal_mem_free(inClusters);
  }
  if (outClusters)  {
    osal_mem_free(outClusters);
  }
}

#if defined ( ZDO_COORDINATOR )
/*********************************************************************
 * @fn          ZDO_ProcessEndDeviceBindReq
 *
 * @brief       This function processes and responds to the
 *              End_Device_Bind_req message.
 *
 * @param       SrcAddr  - Source address
 * @param       msg - input message containing search material
 * @param       SecurityUse - Security enable/disable
 *
 * @return      none
 */
void ZDO_ProcessEndDeviceBindReq( byte TransSeq, zAddrType_t *SrcAddr, byte *msg,
                                  byte SecurityUse )
{
  ZDEndDeviceBind_t bindReq;
  uint8  protoVer;

  protoVer = NLME_GetProtocolVersion();

  // Parse the message
  bindReq.TransSeq = TransSeq;
  bindReq.srcAddr = SrcAddr->addr.shortAddr;
  bindReq.SecurityUse = SecurityUse;

  bindReq.localCoordinator = BUILD_UINT16( msg[0], msg[1] );
  msg += 2;

  if ( protoVer != ZB_PROT_V1_0 )
  {
    osal_cpyExtAddr( &(bindReq.ieeeAddr), msg );
    msg += Z_EXTADDR_LEN;
  }

  bindReq.endpoint = *msg++;
  bindReq.profileID = BUILD_UINT16( msg[0], msg[1] );
  msg += 2;

  bindReq.numInClusters = *msg++;
  bindReq.inClusters = NULL;
  if ( bindReq.numInClusters )
  {
    if ( !(bindReq.inClusters = ZDO_CreateAlignedUINT16List( bindReq.numInClusters, msg )) )
    {
      // can't allocate memory. drop message
      return;
    }
  }
  msg += (bindReq.numInClusters * ((protoVer != ZB_PROT_V1_0) ? sizeof ( uint16 ) : sizeof( uint8 )));

  bindReq.numOutClusters = *msg++;
  bindReq.outClusters = NULL;
  if ( bindReq.numOutClusters )
  {
    if ( !(bindReq.outClusters=ZDO_CreateAlignedUINT16List( bindReq.numOutClusters, msg )) )
    {
      // can't allocate memory. drop message
      if ( bindReq.inClusters )
      {
        osal_mem_free( bindReq.inClusters );
      }
      return;
    }
  }

  ZDApp_EndDeviceBindReqCB( &bindReq );

  if ( bindReq.inClusters )
  {
    osal_mem_free( bindReq.inClusters );
  }
  if ( bindReq.outClusters )
  {
    osal_mem_free( bindReq.outClusters );
  }
}
#endif // ZDO_COORDINATOR

#if defined ( REFLECTOR ) || defined ( ZDO_BIND_UNBIND_RESPONSE )

/*********************************************************************
 * @fn          ZDO_ProcessBindUnbindReq
 *
 * @brief       This function processes and responds to the
 *              Bind_req or Unbind_req message.
 *
 * @param       SrcAddr  - Source address
 * @param       msgClusterID - message cluster ID
 * @param       msg - input message containing search material
 * @param       SecurityUse - Security enable/disable
 *
 * @return      none
 */
void ZDO_ProcessBindUnbindReq( byte TransSeq, zAddrType_t *SrcAddr, uint16 msgClusterID,
                              byte *msg, byte SecurityUse )
{
  byte *SrcAddress;
  byte SrcEpIntf;
  uint16 ClusterID;
  zAddrType_t DstAddress;
  byte DstEpIntf;
  uint8 protoVer;

  protoVer = NLME_GetProtocolVersion();

  SrcAddress = msg;
  msg += Z_EXTADDR_LEN;
  SrcEpIntf = *msg++;

  if ( protoVer != ZB_PROT_V1_0 )
  {
    ClusterID = BUILD_UINT16( msg[0], msg[1] );
    msg += 2;
  }
  else
  {
    ClusterID = *msg++;
  }

  if ( protoVer != ZB_PROT_V1_0 )
  {
    DstAddress.addrMode = *msg++;
    if ( DstAddress.addrMode == Addr64Bit )
    {
      osal_cpyExtAddr( DstAddress.addr.extAddr, msg );
      msg += Z_EXTADDR_LEN;
      DstEpIntf = *msg;
    }
    else
    {
      DstAddress.addr.shortAddr = BUILD_UINT16( msg[0], msg[1] );
      msg += sizeof ( uint16 );
    }
  }
  else
  {
    DstAddress.addrMode = Addr64Bit;
    osal_cpyExtAddr( DstAddress.addr.extAddr, msg );
    msg += Z_EXTADDR_LEN;
    DstEpIntf = *msg;
  }


  if ( msgClusterID == Bind_req )
  {
    ZDApp_BindReqCB( TransSeq, SrcAddr, SrcAddress, SrcEpIntf,
                    ClusterID, &DstAddress, DstEpIntf, SecurityUse );
  }
  else
  {
    ZDApp_UnbindReqCB( TransSeq, SrcAddr, SrcAddress, SrcEpIntf,
                    ClusterID, &DstAddress, DstEpIntf, SecurityUse );
  }
}
#endif // REFLECTOR || ZDO_BIND_UNBIND_RESPONSE

#if defined ( ZDO_NWKADDR_REQUEST ) || defined ( ZDO_IEEEADDR_REQUEST ) || defined ( REFLECTOR )
/*********************************************************************
 * @fn      ZDO_ProcessAddrRsp
 *
 * @brief   Process an incoming NWK_addr_rsp or IEEE_addr_rsp message and then
 *          invoke the corresponding CB function.
 *
 * @param   src - Source address of the request.
 * @param   cId - Cluster ID of the request.
 * @param   msg - Incoming request message.
 *
 * @return  none
 */
void ZDO_ProcessAddrRsp( zAddrType_t *src, uint16 cId, byte *msg, byte msgLen )
{
#if defined ( REFLECTOR )
  AddrMgrEntry_t addrEntry;
#endif
  uint16 aoi;
  uint16 *list = NULL;
  byte idx = 0;
  byte cnt = 0;

  byte stat = *msg++;
  byte *ieee = msg;
  msg += Z_EXTADDR_LEN;
  aoi = BUILD_UINT16( msg[0], msg[1] );

#if defined ( REFLECTOR )
  // Add this to the address manager
  addrEntry.user = ADDRMGR_USER_DEFAULT;
  addrEntry.nwkAddr = aoi;
  AddrMgrExtAddrSet( addrEntry.extAddr, ieee );
  AddrMgrEntryUpdate( &addrEntry );
#endif

  // NumAssocDev field is only present on success.
  if ( stat == ZDO_SUCCESS )
  {
    msg += 2;
    cnt = ( msgLen > 1 + Z_EXTADDR_LEN + 2 ) ? *msg++ : 0;   // Single req: msgLen = status + IEEEAddr + NWKAddr

    // StartIndex field is only present if NumAssocDev field is non-zero.
    if ( cnt != 0 )
    {
      idx = *msg++;

      if ( cnt > idx )
      {
        list = osal_mem_alloc( (short)(cnt * sizeof( uint16 )) );

        if ( list )
        {
          uint16 *pList = list;
          byte n = cnt - idx;

          while ( n != 0 )
          {
            *pList++ = BUILD_UINT16( msg[0], msg[1] );
            msg += sizeof( uint16 );
            n--;
          }
        }
      }
    }
  }

#if defined ( ZDO_NWKADDR_REQUEST )
  if ( cId == NWK_addr_rsp )
  {
    ZDApp_NwkAddrRspCB( src, stat, ieee, aoi, cnt, idx, list );
  }
#endif

#if defined ( ZDO_IEEEADDR_REQUEST )
  if ( cId == IEEE_addr_rsp )
  {
    ZDApp_IEEEAddrRspCB( src, stat, ieee, aoi, cnt, idx, list );
  }
#endif

  if ( list )
  {
    osal_mem_free( list );
  }
}

⌨️ 快捷键说明

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