transmitapp.c

来自「一些基于IRA环境开发的zigbee实例程序」· C语言 代码 · 共 826 行 · 第 1/2 页

C
826
字号
    return (events ^ TRANSMITAPP_SEND_MSG_EVT);
  }

  // Timed wait from error
  if ( events & TRANSMITAPP_SEND_ERR_EVT )
  {
    TransmitApp_SetSendEvt();

    // Return unprocessed events
    return (events ^ TRANSMITAPP_SEND_ERR_EVT);
  }

  // Receive timer
  if ( events & TRANSMITAPP_RCVTIMER_EVT )
  {
    // Setup to display the next result
    osal_start_timerEx( TransmitApp_TaskID, TRANSMITAPP_RCVTIMER_EVT,
                                            TRANSMITAPP_DISPLAY_TIMER );
    TransmitApp_DisplayResults();

    return (events ^ TRANSMITAPP_RCVTIMER_EVT);
  }

  // Discard unknown events
  return 0;
}

/*********************************************************************
 * Event Generation Functions
 */
/*********************************************************************
 * @fn      TransmitApp_ProcessZDOMsgs()
 *
 * @brief   Process response messages
 *
 * @param   none
 *
 * @return  none
 */
void TransmitApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg )
{
  switch ( inMsg->clusterID )
  {
    case End_Device_Bind_rsp:
      if ( ZDO_ParseBindRsp( inMsg ) == ZSuccess )
      {
        // Light LED
        HalLedSet( HAL_LED_4, HAL_LED_MODE_ON );
      }
#if defined(BLINK_LEDS)
      else
      {
        // Flash LED to show failure
        HalLedSet ( HAL_LED_4, HAL_LED_MODE_FLASH );
      }
#endif
      break;

    case Match_Desc_rsp:
      {
        ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( inMsg );
        if ( pRsp )
        {
          if ( pRsp->status == ZSuccess && pRsp->cnt )
          {
            TransmitApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
            TransmitApp_DstAddr.addr.shortAddr = pRsp->nwkAddr;
            // Take the first endpoint, Can be changed to search through endpoints
            TransmitApp_DstAddr.endPoint = pRsp->epList[0];

            // Light LED
            HalLedSet( HAL_LED_4, HAL_LED_MODE_ON );
          }
          osal_mem_free( pRsp );
        }
      }
      break;
  }
}
/*********************************************************************
 * @fn      TransmitApp_HandleKeys
 *
 * @brief   Handles all key events for this device.
 *
 * @param   shift - true if in shift/alt.
 * @param   keys - bit field for key events. Valid entries:
 *                 EVAL_SW4
 *                 EVAL_SW3
 *                 EVAL_SW2
 *                 EVAL_SW1
 *
 * @return  none
 */
void TransmitApp_HandleKeys( byte shift, byte keys )
{
  zAddrType_t dstAddr;

  // Shift is used to make each button/switch dual purpose.
 /* if ( shift )
  {
    if ( keys & HAL_KEY_SW_1 )
    {
      aps_Group_t group;

      // Assign yourself to group 1
      group.ID = 0x0001;
      group.name[0] = 0;
      aps_AddGroup( TRANSMITAPP_ENDPOINT, &group );
    }
    if ( keys & HAL_KEY_SW_2 )
    {
      // Change destination address to group 1
      TransmitApp_DstAddr.addrMode = afAddrGroup;
      TransmitApp_DstAddr.endPoint = 0;
      TransmitApp_DstAddr.addr.shortAddr = 0x001; // group address
    }
    if ( keys & HAL_KEY_SW_3 )
    {
    }
    if ( keys & HAL_KEY_SW_4 )
    {
    }
  }
  else   */
  {
    if ( keys & HAL_KEY_SW_1 )
    {
      TransmitApp_ChangeState();
    }

    if ( keys & HAL_KEY_SW_2 )
    {
      HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );

      // Initiate an End Device Bind Request for the mandatory endpoint
      dstAddr.addrMode = Addr16Bit;
      dstAddr.addr.shortAddr = 0x0000; // Coordinator
      ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                            TransmitApp_epDesc.endPoint,
                            TRANSMITAPP_PROFID,
                            TRANSMITAPP_MAX_CLUSTERS, (cId_t *)TransmitApp_ClusterList,
                            TRANSMITAPP_MAX_CLUSTERS, (cId_t *)TransmitApp_ClusterList,
                            FALSE );
    }

    if ( keys & HAL_KEY_SW_3 )
    {
      rxTotal = txTotal = 0;
      rxAccum = txAccum = 0;
      TransmitApp_DisplayResults();
    }

    if ( keys & HAL_KEY_SW_4 )
    {
      HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );

      // Initiate a Match Description Request (Service Discovery)
      dstAddr.addrMode = AddrBroadcast;
      dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
      ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR,
                        TRANSMITAPP_PROFID,
                        TRANSMITAPP_MAX_CLUSTERS, (cId_t *)TransmitApp_ClusterList,
                        TRANSMITAPP_MAX_CLUSTERS, (cId_t *)TransmitApp_ClusterList,
                        FALSE );
    }
  }
}

/*********************************************************************
 * LOCAL FUNCTIONS
 */

/*********************************************************************
 * @fn      TransmitApp_MessageMSGCB
 *
 * @brief   Data message processor callback.  This function processes
 *          any incoming data - probably from other devices.  So, based
 *          on cluster ID, perform the intended action.
 *
 * @param   none
 *
 * @return  none
 */
void TransmitApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
  int16 i;
  uint8 error = FALSE;

  switch ( pkt->clusterId )
  {
    case TRANSMITAPP_CLUSTERID_TESTMSG:
      if (pkt->cmd.DataLength != TransmitApp_MaxDataLength)
      {
        error = TRUE;
      }

      for (i=4; i<pkt->cmd.DataLength; i++)
      {
        if (pkt->cmd.Data[i] != i%256)
          error = TRUE;
      }

      if (error)
      {
        // Display error LED
        HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
      }
      else
      {
        if ( !timerOn )
        {
          osal_start_timerEx( TransmitApp_TaskID, TRANSMITAPP_RCVTIMER_EVT,
                                                  TRANSMITAPP_DISPLAY_TIMER );
          clkShdw = osal_GetSystemClock();
          timerOn = TRUE;
        }
        rxAccum += pkt->cmd.DataLength;
      }
      break;

    // Could receive control messages in the future.
    default:
      break;
  }
}

/*********************************************************************
 * @fn      TransmitApp_SendTheMessage
 *
 * @brief   Send "the" message.
 *
 * @param   none
 *
 * @return  none
 */
void TransmitApp_SendTheMessage( void )
{
  uint16 len;
  uint8 tmp;

  // put the sequence number in the message
  tmp = HI_UINT8( TransmitApp_TransID );
  tmp += (tmp <= 9) ? ('0') : ('A' - 0x0A);
  TransmitApp_Msg[2] = tmp;
  tmp = LO_UINT8( TransmitApp_TransID );
  tmp += (tmp <= 9) ? ('0') : ('A' - 0x0A);
  TransmitApp_Msg[3] = tmp;

  len = TransmitApp_MaxDataLength;

#if defined ( TRANSMITAPP_RANDOM_LEN )
  len = (uint8)(osal_rand() & 0x7F);
  if( len > TransmitApp_MaxDataLength || len == 0 )
    len = TransmitApp_MaxDataLength;
#endif
	
  do {
    tmp = AF_DataRequest( &TransmitApp_DstAddr, &TransmitApp_epDesc,
                           TRANSMITAPP_CLUSTERID_TESTMSG,
                           len, TransmitApp_Msg,
                          &TransmitApp_TransID,
                           TRANSMITAPP_TX_OPTIONS,
                           AF_DEFAULT_RADIUS );

    if ( timesToSend )
    {
      timesToSend--;
    }
  } while ( (timesToSend != 0) && (afStatus_SUCCESS == tmp) );

  if ( afStatus_SUCCESS == tmp )
  {
    pktCounter++;
  }
  else
  {
    // Error, so wait (10 mSec) and try again.
    osal_start_timerEx( TransmitApp_TaskID, TRANSMITAPP_SEND_ERR_EVT, 10 );
  }
}

/*********************************************************************
 * @fn      TransmitApp_ChangeState
 *
 * @brief   Toggle the Sending/Waiting state flag
 *
 * @param   none
 *
 * @return  none
 */
void TransmitApp_ChangeState( void )
{
  if ( TransmitApp_State == TRANSMITAPP_STATE_WAITING )
  {
    TransmitApp_State = TRANSMITAPP_STATE_SENDING;
    TransmitApp_SetSendEvt();
    timesToSend = TRANSMITAPP_INITIAL_MSG_COUNT;
  }
  else
  {
    TransmitApp_State = TRANSMITAPP_STATE_WAITING;
  }
}

/*********************************************************************
 * @fn      TransmitApp_SetSendEvt
 *
 * @brief   Set the event flag
 *
 * @param   none
 *
 * @return  none
 */
void TransmitApp_SetSendEvt( void )
{
#if defined( TRANSMITAPP_DELAY_SEND )
  // Adds a delay to sending the data
  osal_start_timerEx( TransmitApp_TaskID,
                    TRANSMITAPP_SEND_MSG_EVT, TRANSMITAPP_SEND_DELAY );
#else
  // No Delay - just send the data
  osal_set_event( TransmitApp_TaskID, TRANSMITAPP_SEND_MSG_EVT );
#endif
}

/*********************************************************************
 * @fn      TransmitApp_DisplayResults
 *
 * @brief   Display the results and clear the accumulators
 *
 * @param   none
 *
 * @return  none
 */
void TransmitApp_DisplayResults( void )
{
#ifdef LCD_SUPPORTED
  #define LCD_W  16
  uint32 rxShdw, txShdw, tmp;
  byte lcd_buf[LCD_W+1];
  byte idx;
#endif
  // The OSAL timers are not real-time, so calculate the actual time expired.
  uint32 msecs = osal_GetSystemClock() - clkShdw;
  clkShdw = osal_GetSystemClock();

  rxTotal += rxAccum;
  txTotal += txAccum;

#if defined ( LCD_SUPPORTED )
  rxShdw = (rxAccum * 1000 + msecs/2) / msecs;
  txShdw = (txAccum * 1000 + msecs/2) / msecs;

  osal_memset( lcd_buf, ' ', LCD_W );
  lcd_buf[LCD_W] = NULL;

  idx = 4;
  tmp = (rxShdw >= 100000) ? 99999 : rxShdw;
  do
  {
    lcd_buf[idx--] = (uint8) ('0' + (tmp % 10));
    tmp /= 10;
  } while ( tmp );

  idx = LCD_W-1;
  tmp = rxTotal;
  do
  {
    lcd_buf[idx--] = (uint8) ('0' + (tmp % 10));
    tmp /= 10;
  } while ( tmp );

 // HalLcdWriteString( (char*)lcd_buf, HAL_LCD_LINE_1 );
  Print8(HAL_LCD_LINE_2 ,0,"                ",1);
  Print8(HAL_LCD_LINE_2,0,(INT8U *)lcd_buf,1);
  osal_memset( lcd_buf, ' ', LCD_W );

  idx = 4;
  tmp = (txShdw >= 100000) ? 99999 : txShdw;
  do
  {
    lcd_buf[idx--] = (uint8) ('0' + (tmp % 10));
    tmp /= 10;
  } while ( tmp );

  idx = LCD_W-1;
  tmp = txTotal;
  do
  {
    lcd_buf[idx--] = (uint8) ('0' + (tmp % 10));
    tmp /= 10;
  } while ( tmp );

  //HalLcdWriteString( (char*)lcd_buf, HAL_LCD_LINE_2 );
  Print8(HAL_LCD_LINE_3 ,0,"                ",1);
  Print8(HAL_LCD_LINE_3 ,0,(INT8U *)lcd_buf,1);
#else
  DEBUG_INFO( COMPID_APP, SEVERITY_INFORMATION, 3, rxAccum,
                                              (uint16)msecs, (uint16)rxTotal );
#endif

  if ( (rxAccum == 0) && (txAccum == 0) )
  {
    osal_stop_timerEx( TransmitApp_TaskID, TRANSMITAPP_RCVTIMER_EVT );
    timerOn = FALSE;
  }

  rxAccum = txAccum = 0;
}

/*********************************************************************
*********************************************************************/

⌨️ 快捷键说明

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