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

📄 mtel.c

📁 Zigbee2006入门(源代码+文档讲解+系统推荐)
💻 C
📖 第 1 页 / 共 4 页
字号:
 *   Build and send a debug message.
 *
 * @param   byte *data - pointer to the data portion of the debug message
 *
 * @return  void
 */
void MT_ProcessDebugMsg( mtDebugMsg_t *msg )
{
  byte *msg_ptr;
  byte dataLen;
  uint8 buf[11];
  uint8 *pBuf;

  // Calculate the data length based
  dataLen = 5 + (msg->numParams * sizeof ( uint16 ));

  // Get a message buffer to build the debug message
  msg_ptr = osal_msg_allocate( (byte)(SPI_0DATA_MSG_LEN + dataLen + 1) );
  if ( msg_ptr )
  {
    // Build the message
    pBuf = buf;
    *pBuf++ = msg->compID;
    *pBuf++ = msg->severity;
    *pBuf++ = msg->numParams;

    if ( msg->numParams >= 1 )
    {
      *pBuf++ = HI_UINT16( msg->param1 );
      *pBuf++ = LO_UINT16( msg->param1 );
    }

    if ( msg->numParams >= 2 )
    {
      *pBuf++ = HI_UINT16( msg->param2 );
      *pBuf++ = LO_UINT16( msg->param2 );
    }

    if ( msg->numParams == 3 )
    {
      *pBuf++ = HI_UINT16( msg->param3 );
      *pBuf++ = LO_UINT16( msg->param3 );
    }

    *pBuf++ = HI_UINT16( msg->timestamp );
    *pBuf++ = LO_UINT16( msg->timestamp );

#ifdef SPI_MGR_DEFAULT_PORT
    MT_BuildSPIMsg( SPI_CMD_DEBUG_MSG, &msg_ptr[1], dataLen, buf );
    HalUARTWrite ( SPI_MGR_DEFAULT_PORT, &msg_ptr[1], SPI_0DATA_MSG_LEN + dataLen );
#endif
    osal_msg_deallocate( msg_ptr );
  }
}
#endif // ZTOOL

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
/*********************************************************************
 * @fn      MT_ProcessDebugStr
 *
 * @brief
 *
 *   Build and send a debug string.
 *
 * @param   byte *dstr - pointer to the data portion of the debug message
 *
 * @return  void
 */
void MT_ProcessDebugStr( mtDebugStr_t *dstr )
{
  byte *msg_ptr;

  // Get a message buffer to build the debug message
  msg_ptr = osal_mem_alloc( (byte)(SPI_0DATA_MSG_LEN + dstr->sln) );
  if ( msg_ptr )
  {
#ifdef SPI_MGR_DEFAULT_PORT
    MT_BuildSPIMsg( SPI_RESPONSE_BIT | SPI_SYS_STRING_MSG, msg_ptr, dstr->sln, dstr->pString );
    HalUARTWrite ( SPI_MGR_DEFAULT_PORT, msg_ptr, SPI_0DATA_MSG_LEN + dstr->sln );
#endif
    osal_mem_free( msg_ptr );
  }
}
#endif // ZTOOL

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
/*********************************************************************
 * @fn      MT_ProcessSetNV
 *
 * @brief
 *
 *   The Set NV serial message.
 *
 * @param   byte *msg - pointer to the data
 *
 * @return  ZSuccess if successful
 *
 * @MT SPI_CMD_SYS_SET_NV
 */
byte MT_ProcessSetNV( byte *pData )
{
  uint16  attrib;
  uint16  attlen;

  attrib = (uint16) *pData++;
  attlen = osal_nv_item_len( attrib );

  return osal_nv_write( attrib, 0, attlen, pData );
}
#endif

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
/*********************************************************************
 * @fn      MT_ProcessGetNV
 *
 * @brief
 *
 *   The Get NV serial message.
 *
 * @param   byte *msg - pointer to the data
 *
 * @return  void
 *
 * @MT SPI_CMD_SYS_GET_NV
 *
 */
void MT_ProcessGetNV( byte *pData )
{
  uint16  attrib;
  uint16 attlen;
  uint16 buflen;
  uint8 *buf;

  attrib = (uint16)*pData;
  attlen = osal_nv_item_len( attrib );

  buflen = attlen + 2;
  buf = osal_mem_alloc( buflen );
  if ( buf != NULL )
  {
    osal_memset( buf, 0, buflen );

    buf[0] = osal_nv_read( attrib, 0, attlen, &buf[2] );
    buf[1] = (uint8)attrib;

    MT_BuildAndSendZToolResponse( (SPI_0DATA_MSG_LEN + buflen),
                                  (SPI_RESPONSE_BIT | SPI_CMD_SYS_GET_NV),
                                  buflen, buf );
    osal_mem_free( buf );
  }
}
#endif

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
#if !defined ( NONWK )
/***************************************************************************************************
 * @fn      MT_ProcessGetNvInfo
 *
 * @brief
 *
 *   The Get NV Info serial message.
 *
 * @param   byte *msg - pointer to the data
 *
 * @return  void
 *
 * @MT SPI_CMD_SYS_GET_NV_INFO
 *
 ***************************************************************************************************/
void MT_ProcessGetNvInfo( void )
{
  uint8 len;
  uint8 stat;
  uint8 *buf;
  uint8 *pBuf;
  uint16 tmp16;
  uint32 tmp32;

  // Get required length of buffer
  // Status + ExtAddr + ChanList + PanID  + SecLevel + PreCfgKey
  len = 1 + Z_EXTADDR_LEN + 4 + 2 + 1 + SEC_KEY_LEN;

  buf = osal_mem_alloc( len );
  if ( buf )
  {
    // Assume NV not available
    osal_memset( buf, 0xFF, len );

    // Skip over status
    pBuf = buf + 1;

    // Start with 64-bit extended address
    stat = osal_nv_read( ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, pBuf );
    if ( stat ) stat = 0x01;
    MT_ReverseBytes( pBuf, Z_EXTADDR_LEN );
    pBuf += Z_EXTADDR_LEN;

    // Scan channel list (bit mask)
    if (  osal_nv_read( ZCD_NV_CHANLIST, 0, sizeof( tmp32 ), &tmp32 ) )
      stat |= 0x02;
    else
    {
      pBuf[0] = BREAK_UINT32( tmp32, 3 );
      pBuf[1] = BREAK_UINT32( tmp32, 2 );
      pBuf[2] = BREAK_UINT32( tmp32, 1 );
      pBuf[3] = BREAK_UINT32( tmp32, 0 );
    }
    pBuf += sizeof( tmp32 );

    // ZigBee PanID
    if ( osal_nv_read( ZCD_NV_PANID, 0, sizeof( tmp16 ), &tmp16 ) )
      stat |= 0x04;
    else
    {
      pBuf[0] = HI_UINT16( tmp16 );
      pBuf[1] = LO_UINT16( tmp16 );
    }
    pBuf += sizeof( tmp16 );

    // Security level
    if ( osal_nv_read( ZCD_NV_SECURITY_LEVEL, 0, sizeof( uint8 ), pBuf++ ) )
      stat |= 0x08;

    // Pre-configured security key
    if ( osal_nv_read( ZCD_NV_PRECFGKEY, 0, SEC_KEY_LEN, pBuf ) )
      stat |= 0x10;

    // Status bit mask - bit=1 indicates failure
    *buf = stat;

    MT_BuildAndSendZToolResponse( (SPI_0DATA_MSG_LEN + len),
                                  (SPI_RESPONSE_BIT | SPI_CMD_SYS_GET_NV_INFO),
                                  len, buf );

    osal_mem_free( buf );
  }
}
#endif  // NONWK
#endif  // ZTOOL

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
#define DEVICE_INFO_RESPONSE_LEN 46
#define TYPE_COORDINATOR         1
#define TYPE_ROUTER              2
#define TYPE_ENDDEVICE           4
/***************************************************************************************************
 * @fn      MT_ProcessGetDeviceInfo
 *
 * @brief
 *
 *   The Get Device Info serial message.
 *
 * @param   byte *msg - pointer to the data
 *
 * @return  void
 ***************************************************************************************************/
void MT_ProcessGetDeviceInfo( void )
{
  byte *buf;
  byte *pBuf;
  uint8 deviceType = 0;
  uint16 shortAddr;
  uint16 *assocList;
  byte assocCnt;
  uint16 *puint16;
  byte x;

  buf = osal_mem_alloc( DEVICE_INFO_RESPONSE_LEN );
  if ( buf )
  {
    pBuf = buf;

    *pBuf++ = ZSUCCESS;

    osal_nv_read( ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, pBuf );
    // Outgoing extended address needs to be reversed
    MT_ReverseBytes( pBuf, Z_EXTADDR_LEN );
    pBuf += Z_EXTADDR_LEN;

#if !defined( NONWK )
    shortAddr = NLME_GetShortAddr();
#else
    shortAddr = 0;
#endif

    *pBuf++ = HI_UINT16( shortAddr );
    *pBuf++ = LO_UINT16( shortAddr );

    // Return device type
#if !defined( NONWK )
#if defined (ZDO_COORDINATOR)
    deviceType |= (uint8) TYPE_COORDINATOR;
  #if defined (SOFT_START)
    deviceType |= (uint8) TYPE_ROUTER;
  #endif
#endif
#if defined (RTR_NWK) && !defined (ZDO_COORDINATOR)
    deviceType |= (uint8) TYPE_ROUTER;
#elif !defined (RTR_NWK)
    deviceType |= (uint8) TYPE_ENDDEVICE;
#endif
#endif
    *pBuf++ = (byte) deviceType;

    //Return device state
#if !defined( NONWK )
    *pBuf++ = (byte)devState;
#else
    *pBuf++ = (byte)0;
#endif

#if defined(RTR_NWK) && !defined( NONWK )
    assocList = AssocMakeList( &assocCnt );
#else
    assocCnt = 0;
    assocList = NULL;
#endif

    *pBuf++ = assocCnt;

    // upto 16 devices
    osal_memset( pBuf, 0, (16 * sizeof(uint16)) );
    puint16 = assocList;
    for ( x = 0; x < assocCnt; x++ )
    {
      *pBuf++ = HI_UINT16( *puint16 );
      *pBuf++ = LO_UINT16( *puint16 );
      puint16++;
    }

    if ( assocList )
      osal_mem_free( assocList );

    MT_BuildAndSendZToolResponse( (SPI_0DATA_MSG_LEN + DEVICE_INFO_RESPONSE_LEN),
                                  (SPI_RESPONSE_BIT | SPI_CMD_SYS_GET_DEVICE_INFO),
                                  DEVICE_INFO_RESPONSE_LEN, buf );

    osal_mem_free( buf );
  }
}
#endif

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
/***************************************************************************************************
 * @fn      MT_ProcessSerialCommand
 *
 * @brief
 *
 *   Process Serial Message.
 *
 * @param   byte *msg - pointer to event message
 *
 * @return  void
 ***************************************************************************************************/
void MT_ProcessSerialCommand( byte *msg )
{
  UINT16 cmd;
  UINT16 callbackID;
  byte len;
  byte ret;
  byte *pData;
  uint16 tmp16;
  uint32 tmp32;
  byte extAddr[Z_EXTADDR_LEN];
  byte *retValue;
  byte x = 0;
#if !defined ( NONWK )
  uint16 attLen;
#endif // NONWK

  // dig out header info
  cmd = BUILD_UINT16( msg[1], msg[0] );
  save_cmd = cmd;
  len = msg[2];
  pData = &msg[3];

    // Setup for return;
    len = 0;
    retValue = &ret;

    //Process the contents of the message
    switch ( cmd )
    {
#ifdef MACSIM
      case SPI_CMD_ZIGNET_DATA:
        MACSIM_TranslateMsg( pData, len );
        break;
#endif

      case SPI_CMD_SYS_RAM_READ:
        extAddr[0] = MT_RAMRead( (UINT16)BUILD_UINT16( pData[1], pData[0] ), &extAddr[1] );
        len = MT_RAM_READ_RESP_LEN;
        retValue = extAddr;
        break;

      case SPI_CMD_SYS_RAM_WRITE:
        ret = MT_RAMWrite( (UINT16)BUILD_UINT16( pData[1], pData[0] ), pData[2] );
        len = MT_RAM_WRITE_RESP_LEN;
        break;

      case SPI_CMD_SYS_SET_DEBUG_THRESHOLD:
        ret = MT_SetDebugThreshold( pData[0], pData[1] );
        len = 1;
        break;

      case SPI_CMD_TRACE_SUB:
        break;

      case SPI_CMD_SYS_RESET:
        MT_Reset( pData[0] );
        break;

      case SPI_CMD_SYS_CALLBACK_SUB_CMD:
        // a callback value of 0xFFFF turns on all available callbacks
        callbackID = BUILD_UINT16( pData[1] , pData[0] );
        if ( callbackID == 0xFFFF )
        {
          // What is the action
          if ( pData[2] )
          {
            // Turn on
#if defined( MT_MAC_CB_FUNC )
            _macCallbackSub = 0xFFFF;
#endif
#if defined( MT_NWK_CB_FUNC )
            _nwkCallbackSub = 0xFFFF;
#endif

#if defined( MT_ZDO_FUNC )
            _zdoCallbackSub = 0xFFFFFFFF;
#endif
#if defined( MT_AF_CB_FUNC )
            _afCallbackSub = 0xFFFF;
#endif
#if defined( MT_SAPI_CB_FUNC )
            _sapiCallbackSub = 0xFFFF;
#endif
          }
          else
          {
            // Turn off
#if defined( MT_MAC_CB_FUNC )
            _macCallbackSub = 0x0000;
#endif
#if defined( MT_NWK_CB_FUNC )
            _nwkCallbackSub = 0x0000;
#endif

#if defined( MT_ZDO_FUNC )
            _zdoCallbackSub = 0x00000000;
#endif
#if defined( MT_AF_CB_FUNC )
            _afCallbackSub = 0x0000;
#endif
#if defined( MT_SAPI_CB_FUNC )
            _sapiCallbackSub = 0x0000;
#endif
          }
        }
        else
        {
          //First check which layer callbacks are desired and then set the preference

#if defined( MT_MAC_CB_FUNC )
          //If it is a MAC callback, set the corresponding callback subscription bit
          if (( callbackID & 0xFFF0 ) == SPI_MAC_CB_TYPE )
          {
            //Based on the action field, either enable or disable subscription

⌨️ 快捷键说明

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