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

📄 mtel.c

📁 Zigbee2006入门(源代码+文档讲解+系统推荐)
💻 C
📖 第 1 页 / 共 4 页
字号:
            if ( pData[2] )
              _macCallbackSub |=  ( 1 << ( pData[1] & 0x0F ) );
            else
              _macCallbackSub &= ~( 1 << ( pData[1] & 0x0F ) );
          }
#endif

#if defined( MT_NWK_CB_FUNC )
          //If it is a NWK callback, set the corresponding callback subscription bit
          if (( callbackID & 0xFFF0 ) == SPI_NWK_CB_TYPE )
          {

            //Based on the action field, either enable or disable subscription
            if ( pData[2] )
              _nwkCallbackSub |=  ( 1 << ( pData[1] & 0x0F ) ) ;
            else
              _nwkCallbackSub &= ~( 1 << ( pData[1] & 0x0F ) );
          }
#endif

#if defined( MT_ZDO_FUNC )
          //If it is a APS callback, set the corresponding callback subscription bit
          if ( ((callbackID & 0xFFF0) == SPI_ZDO_CB_TYPE) ||
               ((callbackID & 0xFFF0) == SPI_ZDO_CB2_TYPE) )
          {
            //Based on the action field, either enable or disable subscription
            if ( pData[2] )
              _zdoCallbackSub |=  ( 1L << ( pData[1] & 0x1F ) );
            else
              _zdoCallbackSub &= ~( 1L << ( pData[1] & 0x1F ) );
          }
#endif

#if defined( MT_AF_CB_FUNC )
          // Set the corresponding callback subscription bit for an AF callback.
          if (( callbackID & 0xFFF0 ) == SPI_AF_CB_TYPE )
          {
            // Based on the action field, either enable or disable subscription.
            if ( pData[2] )
              _afCallbackSub |=  ( 1 << ( pData[1] & 0x0F ) );
            else
              _afCallbackSub &= ~( 1 << ( pData[1] & 0x0F ) );
          }
#endif
#if defined( MT_SAPI_CB_FUNC )
          // Set the corresponding callback subscription bit for an SAPI callback.
          if (( callbackID & 0xFFF0 ) == SPI_SAPI_CB_TYPE )
          {
            // Based on the action field, either enable or disable subscription.
            if ( pData[2] )
              _sapiCallbackSub |=  ( 1 << ( pData[1] & 0x0F ) );
            else
              _sapiCallbackSub &= ~( 1 << ( pData[1] & 0x0F ) );
          }
#endif
        }
        len = 1;
        ret = ZSUCCESS;
        break;

      case SPI_CMD_SYS_PING:
        // Get a message buffer to build response message
        // The Ping response now has capabilities included

        // Build Capabilities
        tmp16 = MT_CAP_MAC | MT_CAP_NWK | MT_CAP_AF |
              MT_CAP_ZDO | MT_CAP_USER_TEST | MT_CAP_SAPI_FUNC;

        // Convert to high byte first into temp buffer
        extAddr[0] = HI_UINT16( tmp16 );
        extAddr[1] = LO_UINT16( tmp16 );
        len = sizeof ( tmp16 );
        retValue = extAddr;
        break;

      case SPI_CMD_SYS_VERSION:
        {
#if !defined ( NONWK )
          uint8 i = NLME_GetProtocolVersion() - 1;
#else
          uint8 i = 1;   // just say '1.1' -- irrelevant if stack isn't there anyway
#endif

          // Get a message buffer to build response message
          len      = (byte)(osal_strlen( (char *)MTVersionString[i] ));
          retValue = (byte *)MTVersionString[i];
        }
          break;

      case SPI_CMD_SYS_SET_EXTADDR:
        // Incoming extended address is reversed
        MT_ReverseBytes( pData, Z_EXTADDR_LEN );

        if ( ZMacSetReq( ZMacExtAddr, pData ) == ZMacSuccess )
          ret = osal_nv_write( ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, pData );
        else
          ret = 1;
        len = 1;
        break;

      case SPI_CMD_SYS_GET_EXTADDR:
        ZMacGetReq( ZMacExtAddr, extAddr );

        // Outgoing extended address needs to be reversed
        MT_ReverseBytes( extAddr, Z_EXTADDR_LEN );

        len = Z_EXTADDR_LEN;
        retValue = extAddr;
        break;

#if !defined ( NONWK )
      case SPI_CMD_SYS_SET_PANID:
        tmp16 = BUILD_UINT16( pData[1], pData[0] );
        attLen = osal_nv_item_len( ZCD_NV_PANID );
        ret = osal_nv_write( ZCD_NV_PANID, 0, attLen, &tmp16 );
        len = 1;
        break;

      case SPI_CMD_SYS_SET_CHANNELS:
        tmp32 = BUILD_UINT32( pData[3], pData[2], pData[1], pData[0] );
        attLen = osal_nv_item_len( ZCD_NV_CHANLIST );
        ret = osal_nv_write( ZCD_NV_CHANLIST, 0, attLen, &tmp32 );
        len = 1;
        break;

      case SPI_CMD_SYS_SET_SECLEVEL:
        attLen = osal_nv_item_len( ZCD_NV_SECURITY_LEVEL );
        ret = osal_nv_write( ZCD_NV_SECURITY_LEVEL, 0, attLen, pData );
        len = 1;
        break;

      case SPI_CMD_SYS_SET_PRECFGKEY:
        attLen = osal_nv_item_len( ZCD_NV_PRECFGKEY );
        ret = osal_nv_write( ZCD_NV_PRECFGKEY, 0, attLen, pData );
        len = 1;
        break;

      case SPI_CMD_SYS_GET_NV_INFO:
        MT_ProcessGetNvInfo();
        break;
#endif // NONWK

      case SPI_CMD_SYS_GET_DEVICE_INFO:
        MT_ProcessGetDeviceInfo();
        break;

      case SPI_CMD_SYS_SET_NV:
        ret = MT_ProcessSetNV( pData );
        len = 1;
        break;

      case SPI_CMD_SYS_GET_NV:
        MT_ProcessGetNV( pData );
        break;

      case SPI_CMD_SYS_TIME_ALIVE:
        // Time since last reset (seconds)
        tmp32 = osal_GetSystemClock() / 1000;
        // Convert to high byte first into temp buffer
        extAddr[0] = BREAK_UINT32( tmp32, 3 );
        extAddr[1] = BREAK_UINT32( tmp32, 2 );
        extAddr[2] = BREAK_UINT32( tmp32, 1 );
        extAddr[3] = BREAK_UINT32( tmp32, 0 );
        len = sizeof ( tmp32 );
        retValue = extAddr;
        break;

      case SPI_CMD_SYS_KEY_EVENT:
        // Translate between SPI values to device values
        if ( pData[1] & 0x01 )
          x |= HAL_KEY_SW_1;
        if ( pData[1] & 0x02 )
          x |= HAL_KEY_SW_2;
        if ( pData[1] & 0x04 )
          x |= HAL_KEY_SW_3;
        if ( pData[1] & 0x08 )
          x |= HAL_KEY_SW_4;
#if defined ( HAL_KEY_SW_5 )
        if ( pData[1] & 0x10 )
          x |= HAL_KEY_SW_5;
#endif
#if defined ( HAL_KEY_SW_6 )
        if ( pData[1] & 0x20 )
          x |= HAL_KEY_SW_6;
#endif
#if defined ( HAL_KEY_SW_7 )
        if ( pData[1] & 0x40 )
          x |= HAL_KEY_SW_7;
#endif
#if defined ( HAL_KEY_SW_8 )
        if ( pData[1] & 0x80 )
          x |= HAL_KEY_SW_8;
#endif
        ret = OnBoard_SendKeys( x, pData[0]  );
        len = 1;
        break;

      case SPI_CMD_SYS_HEARTBEAT:
        ret = ZSUCCESS;
        len = 1;
        break;

#if !defined ( NONWK )
      case SPI_CMD_SYS_APP_MSG:
        ret = MTProcessAppMsg( pData, msg[2] );
        len = 0;
        break;
#endif // NONWK

      case SPI_CMD_SYS_LED_CONTROL:
#if (defined HAL_LED) && (HAL_LED == TRUE)
        ret = MTProcessLedControl( pData );
        len = 1;
#endif
        break;

#ifdef MT_MAC_FUNC
      case SPI_CMD_MAC_INIT:
      case SPI_CMD_MAC_ASSOCIATE_REQ:
      case SPI_CMD_MAC_ASSOCIATE_RSP:
      case SPI_CMD_MAC_DISASSOCIATE_REQ:
      case SPI_CMD_MAC_DATA_REQ:
      case SPI_CMD_MAC_GET_REQ:
      case SPI_CMD_MAC_SET_REQ:
      case SPI_CMD_MAC_START_REQ:
      case SPI_CMD_MAC_SCAN_REQ:
      case SPI_CMD_MAC_RESET_REQ:
      case SPI_CMD_MAC_GTS_REQ:
      case SPI_CMD_MAC_ORPHAN_RSP:
      case SPI_CMD_MAC_RX_ENABLE_REQ:
      case SPI_CMD_MAC_SYNC_REQ:
      case SPI_CMD_MAC_POLL_REQ:
      case SPI_CMD_MAC_PURGE_REQ:
        MT_MacCommandProcessing( cmd , len , pData );
        break;
#endif

#ifdef MT_NWK_FUNC
      case SPI_CMD_NWK_INIT:
      case SPI_CMD_NLDE_DATA_REQ:
      case SPI_CMD_NLME_INIT_COORD_REQ:
      case SPI_CMD_NLME_PERMIT_JOINING_REQ:
      case SPI_CMD_NLME_JOIN_REQ:
      case SPI_CMD_NLME_LEAVE_REQ:
      case SPI_CMD_NLME_RESET_REQ:
      case SPI_CMD_NLME_RX_STATE_REQ:
      case SPI_CMD_NLME_GET_REQ:
      case SPI_CMD_NLME_SET_REQ:
      case SPI_CMD_NLME_NWK_DISC_REQ:
      case SPI_CMD_NLME_ROUTE_DISC_REQ:
      case SPI_CMD_NLME_DIRECT_JOIN_REQ:
      case SPI_CMD_NLME_ORPHAN_JOIN_REQ:
      case SPI_CMD_NLME_START_ROUTER_REQ:
        MT_NwkCommandProcessing( cmd , len , pData );
        break;
#endif

#ifdef MT_ZDO_FUNC
      case SPI_CMD_ZDO_AUTO_ENDDEVICEBIND_REQ:
      case SPI_CMD_ZDO_AUTO_FIND_DESTINATION_REQ:
      case SPI_CMD_ZDO_NWK_ADDR_REQ:
      case SPI_CMD_ZDO_IEEE_ADDR_REQ:
      case SPI_CMD_ZDO_NODE_DESC_REQ:
      case SPI_CMD_ZDO_POWER_DESC_REQ:
      case SPI_CMD_ZDO_SIMPLE_DESC_REQ:
      case SPI_CMD_ZDO_ACTIVE_EPINT_REQ:
      case SPI_CMD_ZDO_MATCH_DESC_REQ:
      case SPI_CMD_ZDO_COMPLEX_DESC_REQ:
      case SPI_CMD_ZDO_USER_DESC_REQ:
      case SPI_CMD_ZDO_END_DEV_BIND_REQ:
      case SPI_CMD_ZDO_BIND_REQ:
      case SPI_CMD_ZDO_UNBIND_REQ:
      case SPI_CMD_ZDO_MGMT_NWKDISC_REQ:
      case SPI_CMD_ZDO_MGMT_LQI_REQ:
      case SPI_CMD_ZDO_MGMT_RTG_REQ:
      case SPI_CMD_ZDO_MGMT_BIND_REQ:
      case SPI_CMD_ZDO_MGMT_DIRECT_JOIN_REQ:
      case SPI_CMD_ZDO_USER_DESC_SET:
      case SPI_CMD_ZDO_END_DEV_ANNCE:
      case SPI_CMD_ZDO_MGMT_LEAVE_REQ:
      case SPI_CMD_ZDO_MGMT_PERMIT_JOIN_REQ:
      case SPI_CMD_ZDO_SERVERDISC_REQ:
      case SPI_CMD_ZDO_NETWORK_START_REQ:
        MT_ZdoCommandProcessing( cmd , len , pData );
        break;
#endif

#if defined ( MT_AF_FUNC )
      case SPI_CMD_AF_INIT:
      case SPI_CMD_AF_REGISTER:
      case SPI_CMD_AF_SENDMSG:
        MT_afCommandProcessing( cmd , len , pData );
        break;
#endif

#if defined ( MT_SAPI_FUNC )
      case SPI_CMD_SAPI_SYS_RESET:
      case SPI_CMD_SAPI_START_REQ:
      case SPI_CMD_SAPI_BIND_DEVICE:
      case SPI_CMD_SAPI_ALLOW_BIND:
      case SPI_CMD_SAPI_SEND_DATA:
      case SPI_CMD_SAPI_READ_CFG:
      case SPI_CMD_SAPI_WRITE_CFG:
      case SPI_CMD_SAPI_GET_DEV_INFO:
      case SPI_CMD_SAPI_FIND_DEV:
      case SPI_CMD_SAPI_PMT_JOIN:
        ret = MT_sapiCommandProcessing( cmd , len , pData );
        if ( ret == 0xff )
          len = 0;
        else
          len = 1;
        break;
#endif

#if defined ( MT_USER_TEST_FUNC )
      case SPI_CMD_USER_TEST:
        MT_ProcessAppUserCmd( pData );
        break;
#endif

      default:
        break;
    }

    if ( len )
    {
      MT_BuildAndSendZToolResponse( (SPI_0DATA_MSG_LEN + len),
                                    (SPI_RESPONSE_BIT | cmd),
                                    len, retValue );
    }
  }
#endif // ZTOOL

#if (defined HAL_LED) && (HAL_LED == TRUE)
/***************************************************************************************************
 * @fn      MTProcessLedControl
 *
 * @brief
 *
 *   Process the LED Control Message
 *
 * @param   data - input serial buffer
 *
 * @return  status
 ***************************************************************************************************/
byte MTProcessLedControl( byte *pData )
{
  byte iLed;
  byte Led;
  byte iMode;
  byte Mode;

  iLed = *pData++;
  iMode = *pData;

  if ( iLed == 1 )
    Led = HAL_LED_1;
  else if ( iLed == 2 )
    Led = HAL_LED_2;
  else if ( iLed == 3 )
    Led = HAL_LED_3;
  else if ( iLed == 4 )
    Led = HAL_LED_4;
  else if ( iLed == 0xFF )
    Led = HAL_LED_ALL;
  else
    Led = 0;

  if ( iMode == 0 )
    Mode = HAL_LED_MODE_OFF;
  else if ( iMode == 1 )
    Mode = HAL_LED_MODE_ON;
  else if ( iMode == 2 )
    Mode = HAL_LED_MODE_BLINK;
  else if ( iMode == 3 )
    Mode = HAL_LED_MODE_FLASH;
  else if ( iMode == 4 )
    Mode = HAL_LED_MODE_TOGGLE;
  else
    Led = 0;

  if ( Led != 0 )
  {
    HalLedSet (Led, Mode );
    return ( ZSuccess );
  }
  else
    return ( ZFailure );
}
#endif // HAL_LED

#if !defined ( NONWK )
/*********************************************************************
 * @fn      MTProcessAppMsg
 *
 * @brief
 *
 *   Process the User App Message
 *
 * @param   data - input serial buffer
 * @param   len - data length
 *
 * @return  status
 */
byte MTProcessAppMsg( byte *pData, byte len )
{
  byte ret = ZFailure;
  byte endpoint;
  endPointDesc_t *epDesc;
  mtSysAppMsg_t *msg;

  // Get the endpoint and skip past it.
  endpoint = *pData++;
  len--;

  // Look up the endpoint
  epDesc = afFindEndPointDesc( endpoint );

  if ( epDesc )
  {
    // Build and send the message to the APP
    msg = (mtSysAppMsg_t *)osal_msg_allocate( sizeof( mtSysAppMsg_t ) + len );
    if ( msg )
    {
      msg->hdr.event = MT_SYS_APP_MSG;
      msg->endpoint = endpoint;
      msg->appDataLen = len;
      msg->appData = (uint8*)(msg+1);

      osal_memcpy( msg->appData, pData, len );

      osal_msg_send( *(epDesc->task_id), (uint8 *)msg );

      ret = ZSuccess;
    }
  }

  return ret;
}
#endif // NONWK

#if defined ( ZTOOL_PORT )
/*********************************************************************
 * @fn      MTProcessAppRspMsg
 *
 * @brief
 *
 *   Process the User App Response Message
 *
 * @param   data - output serial buffer.  The first byte must be the
 *          endpoint that send this message.
 * @param   len - data length
 *
 * @return  none
 */
void MTProcessAppRspMsg( byte *pData, byte len )
{
  // Send out Reset Response message
  MT_BuildAndSendZToolResponse( (SPI_0DATA_MSG_LEN + len),
                                (SPI_RESPONSE_BIT | SPI_CMD_SYS_APP_MSG),
                                len, pData );
}
#endif // ZTOOL_PORT


#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
#if defined ( MT_USER_TEST_FUNC )
/*********************************************************************

⌨️ 快捷键说明

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